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> |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 28 | |
| 29 | #include "flash.h" /* for msg_* */ |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 30 | #include "locks.h" /* for SYSTEM_LOCKFILE_DIR */ |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 31 | #include "power.h" |
| 32 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Path to a file containing flashrom's PID. While present, powerd avoids |
| 35 | * suspending or shutting down the system. |
| 36 | */ |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 37 | static const char powerd_lock_file_name[] = "flashrom_powerd.lock"; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 38 | |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 39 | int disable_power_management() |
| 40 | { |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 41 | FILE *lock_file = NULL; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 42 | int rc = 0; |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 43 | char lock_file_path[PATH_MAX]; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 44 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 45 | msg_pdbg("%s: Disabling power management.\n", __func__); |
| 46 | |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 47 | if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s", |
| 48 | SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0) |
| 49 | return 1; |
| 50 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 51 | if (!(lock_file = fopen(lock_file_path, "w"))) { |
| 52 | msg_perr("%s: Failed to open %s for writing: %s\n", |
| 53 | __func__, lock_file_path, strerror(errno)); |
| 54 | return 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 57 | if (fprintf(lock_file, "%ld", (long)getpid()) < 0) { |
| 58 | msg_perr("%s: Failed to write PID to %s: %s\n", |
| 59 | __func__, lock_file_path, strerror(errno)); |
| 60 | rc = 1; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 63 | if (fclose(lock_file) != 0) { |
| 64 | msg_perr("%s: Failed to close %s: %s\n", |
| 65 | __func__, lock_file_path, strerror(errno)); |
| 66 | } |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 67 | return rc; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 68 | |
| 69 | } |
| 70 | |
| 71 | int restore_power_management() |
| 72 | { |
| 73 | int result = 0; |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 74 | char lock_file_path[PATH_MAX]; |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 75 | |
| 76 | msg_pdbg("%s: Re-enabling power management.\n", __func__); |
| 77 | |
David Hendricks | ea66c64 | 2016-06-09 13:28:58 -0700 | [diff] [blame^] | 78 | if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s", |
| 79 | SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0) |
| 80 | return 1; |
| 81 | |
Daniel Erat | f32a003 | 2014-09-09 10:31:07 -0700 | [diff] [blame] | 82 | result = unlink(lock_file_path); |
| 83 | if (result != 0 && errno != ENOENT) { |
| 84 | msg_perr("%s: Failed to unlink %s: %s\n", |
| 85 | __func__, lock_file_path, strerror(errno)); |
| 86 | return 1; |
| 87 | } |
| 88 | return 0; |
David Hendricks | 42ad652 | 2011-08-09 16:08:10 -0700 | [diff] [blame] | 89 | } |