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 | * |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 15 | * |
| 16 | * power.c: power management routines |
| 17 | */ |
| 18 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Sam McNally | e50562c | 2020-12-07 15:39:32 +1100 | [diff] [blame] | 23 | #include <sys/stat.h> |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
Paul Kocialkowski | bea97e5 | 2017-03-26 17:20:44 +0200 | [diff] [blame] | 26 | #include <limits.h> |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 27 | |
| 28 | #include "flash.h" /* for msg_* */ |
| 29 | #include "power.h" |
| 30 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 31 | /* |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 32 | * Returns the path to a lock file in which flashrom's PID should be written to |
| 33 | * instruct powerd not to suspend or shut down. |
| 34 | * |
| 35 | * powerd now checks for arbitrary lock files within /run/lock/power_override, |
| 36 | * but flashrom needs to keep support for falling back to the old file at |
| 37 | * /run/lock/flashrom_power.lock indefinitely to support the case where a new |
| 38 | * version of flashrom is running against an old version of the OS during a |
| 39 | * system update. |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 40 | */ |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 41 | static const char *get_powerd_lock_file_path(void) |
| 42 | { |
| 43 | return access("/run/lock/power_override/", F_OK) == 0 ? |
| 44 | "/run/lock/power_override/flashrom.lock" : |
| 45 | "/run/lock/flashrom_powerd.lock"; |
| 46 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 47 | |
Nikolai Artemiev | 8d7e839 | 2021-02-12 11:38:09 +1100 | [diff] [blame] | 48 | int disable_power_management(void) |
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 | FILE *lock_file = NULL; |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 51 | const char *path = NULL; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 52 | int rc = 0; |
Sam McNally | e50562c | 2020-12-07 15:39:32 +1100 | [diff] [blame] | 53 | mode_t old_umask; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame] | 54 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 55 | msg_pdbg("%s: Disabling power management.\n", __func__); |
| 56 | |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 57 | path = get_powerd_lock_file_path(); |
| 58 | |
Sam McNally | e50562c | 2020-12-07 15:39:32 +1100 | [diff] [blame] | 59 | old_umask = umask(022); |
| 60 | lock_file = fopen(path, "w"); |
| 61 | umask(old_umask); |
| 62 | if (!lock_file) { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 63 | msg_perr("%s: Failed to open %s for writing: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 64 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 65 | return 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 68 | if (fprintf(lock_file, "%ld", (long)getpid()) < 0) { |
| 69 | msg_perr("%s: Failed to write PID to %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 70 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 71 | rc = 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 74 | if (fclose(lock_file) != 0) { |
| 75 | msg_perr("%s: Failed to close %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 76 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 77 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 78 | return rc; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 79 | |
| 80 | } |
| 81 | |
Nikolai Artemiev | 8d7e839 | 2021-02-12 11:38:09 +1100 | [diff] [blame] | 82 | int restore_power_management(void) |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 83 | { |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 84 | const char *path = NULL; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 85 | int result = 0; |
Daniel Erat | 4166d34 | 2017-12-05 14:32:52 -0800 | [diff] [blame] | 86 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 87 | msg_pdbg("%s: Re-enabling power management.\n", __func__); |
| 88 | |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 89 | path = get_powerd_lock_file_path(); |
| 90 | |
| 91 | result = unlink(path); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 92 | if (result != 0 && errno != ENOENT) { |
| 93 | msg_perr("%s: Failed to unlink %s: %s\n", |
Daniel Erat | d6fca5d | 2018-02-05 10:34:21 -0800 | [diff] [blame] | 94 | __func__, path, strerror(errno)); |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | return 0; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 98 | } |