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 | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 34 | * Returns the path to a lock file in which flashrom's PID should be written to |
| 35 | * instruct powerd not to suspend or shut down. |
| 36 | * |
| 37 | * powerd now checks for arbitrary lock files within /run/lock/power_override, |
| 38 | * but flashrom needs to keep support for falling back to the old file at |
| 39 | * /run/lock/flashrom_power.lock indefinitely to support the case where a new |
| 40 | * version of flashrom is running against an old version of the OS during a |
| 41 | * system update. |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 42 | */ |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 43 | static const char *get_powerd_lock_file_path(void) |
| 44 | { |
| 45 | return access("/run/lock/power_override/", F_OK) == 0 ? |
| 46 | "/run/lock/power_override/flashrom.lock" : |
| 47 | "/run/lock/flashrom_powerd.lock"; |
| 48 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 49 | |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 50 | int disable_power_management() |
| 51 | { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 52 | FILE *lock_file = NULL; |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 53 | const char *path = NULL; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 54 | int rc = 0; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame] | 55 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 56 | msg_pdbg("%s: Disabling power management.\n", __func__); |
| 57 | |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 58 | path = get_powerd_lock_file_path(); |
| 59 | |
| 60 | if (!(lock_file = fopen(path, "w"))) { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 61 | msg_perr("%s: Failed to open %s for writing: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 62 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 63 | return 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 66 | if (fprintf(lock_file, "%ld", (long)getpid()) < 0) { |
| 67 | msg_perr("%s: Failed to write PID to %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 68 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 69 | rc = 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 72 | if (fclose(lock_file) != 0) { |
| 73 | msg_perr("%s: Failed to close %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 74 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 75 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 76 | return rc; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 77 | |
| 78 | } |
| 79 | |
| 80 | int restore_power_management() |
| 81 | { |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 82 | const char *path = NULL; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 83 | int result = 0; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame] | 84 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 85 | msg_pdbg("%s: Re-enabling power management.\n", __func__); |
| 86 | |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 87 | path = get_powerd_lock_file_path(); |
| 88 | |
| 89 | result = unlink(path); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 90 | if (result != 0 && errno != ENOENT) { |
| 91 | msg_perr("%s: Failed to unlink %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame^] | 92 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | return 0; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 96 | } |