David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2011 The Chromium OS Authors |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * power.c: power management routines |
| 20 | */ |
| 21 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <stdio.h> |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
Paul Kocialkowski | bea97e5 | 2017-03-26 17:20:44 +0200 | [diff] [blame] | 28 | #include <limits.h> |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 29 | |
| 30 | #include "flash.h" /* for msg_* */ |
| 31 | #include "power.h" |
| 32 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 33 | /* |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 34 | * Path to directory and file containing flashrom's PID. |
| 35 | * While present, powerd avoids suspending or shutting down the system. |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 36 | */ |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 37 | #define POWERD_LOCK_DIR "/run/lock/power_override" |
| 38 | #define POWERD_LOCK_FILE POWERD_LOCK_DIR "/flashrom.lock" |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 39 | |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 40 | int disable_power_management() |
| 41 | { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 42 | FILE *lock_file = NULL; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 43 | int rc = 0; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 44 | |
| 45 | /* Don't do anything if powerd isn't expected to run. */ |
| 46 | if (access(POWERD_LOCK_DIR, F_OK) != 0) { |
| 47 | return 0; |
| 48 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 49 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 50 | msg_pdbg("%s: Disabling power management.\n", __func__); |
| 51 | |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 52 | if (!(lock_file = fopen(POWERD_LOCK_FILE, "w"))) { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 53 | msg_perr("%s: Failed to open %s for writing: %s\n", |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 54 | __func__, POWERD_LOCK_FILE, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 55 | return 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 58 | if (fprintf(lock_file, "%ld", (long)getpid()) < 0) { |
| 59 | msg_perr("%s: Failed to write PID to %s: %s\n", |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 60 | __func__, POWERD_LOCK_FILE, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 61 | rc = 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 64 | if (fclose(lock_file) != 0) { |
| 65 | msg_perr("%s: Failed to close %s: %s\n", |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 66 | __func__, POWERD_LOCK_FILE, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 67 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 68 | return rc; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 69 | |
| 70 | } |
| 71 | |
| 72 | int restore_power_management() |
| 73 | { |
| 74 | int result = 0; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 75 | |
| 76 | /* Don't do anything if powerd isn't expected to run. */ |
| 77 | if (access(POWERD_LOCK_DIR, F_OK) != 0) { |
| 78 | return 0; |
| 79 | } |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 80 | |
| 81 | msg_pdbg("%s: Re-enabling power management.\n", __func__); |
| 82 | |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 83 | result = unlink(POWERD_LOCK_FILE); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 84 | if (result != 0 && errno != ENOENT) { |
| 85 | msg_perr("%s: Failed to unlink %s: %s\n", |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame^] | 86 | __func__, POWERD_LOCK_FILE, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | return 0; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 90 | } |