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 *
16 * power.c: power management routines
17 */
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <limits.h>
27
28 #include "flash.h" /* for msg_* */
29 #include "power.h"
30
31 /*
32 * Path of a lock file which flashrom's PID should be written to instruct
33 * powerd not to suspend or shut down. powerd checks for arbitrary lock files
34 * within /run/lock/power_override.
35 */
36 #define POWERD_LOCK_FILE_PATH "/run/lock/power_override/flashrom.lock"
37
38 /* Files created by powerd to announce an imminent shutdown or suspend. */
39 #define POWERD_SUSPEND_ANNOUNCED_PATH "/run/power_manager/power/suspend_announced"
40 #define POWERD_SHUTDOWN_ANNOUNCED_PATH "/run/power_manager/power/shutdown_announced"
41
42 /*
43 * Env var set by powerd's setuid helper. If running from powerd, ignore
44 * suspend/shutdown announced files.
45 */
46 #define POWERD_SETUID_HELPER_ENV "POWERD_SETUID_HELPER"
47
check_suspend_imminent(void)48 static bool check_suspend_imminent(void)
49 {
50 if (getenv(POWERD_SETUID_HELPER_ENV)) {
51 msg_pdbg("%s env var is set: skipping check for suspend/shutdown"
52 "announcment files.\n", POWERD_SETUID_HELPER_ENV);
53 return false;
54 }
55
56 struct stat s;
57 if ((stat(POWERD_SUSPEND_ANNOUNCED_PATH, &s) == 0) ||
58 (stat(POWERD_SHUTDOWN_ANNOUNCED_PATH, &s) == 0)) {
59 msg_perr("Cannot disable power management, the system "
60 "is already preparing to suspend/shutdown. Aborting.\n");
61 return true;
62 }
63 return false;
64 }
65
disable_power_management(void)66 int disable_power_management(void)
67 {
68 FILE *lock_file = NULL;
69 int rc = 0;
70 mode_t old_umask;
71
72 msg_pdbg("%s: Disabling power management.\n", __func__);
73
74 old_umask = umask(022);
75 lock_file = fopen(POWERD_LOCK_FILE_PATH, "w");
76 umask(old_umask);
77 if (!lock_file) {
78 msg_perr("%s: Failed to open %s for writing: %s\n",
79 __func__, POWERD_LOCK_FILE_PATH, strerror(errno));
80 return 1;
81 }
82
83 if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
84 msg_perr("%s: Failed to write PID to %s: %s\n",
85 __func__, POWERD_LOCK_FILE_PATH, strerror(errno));
86 rc = 1;
87 }
88
89 if (fclose(lock_file) != 0) {
90 msg_perr("%s: Failed to close %s: %s\n",
91 __func__, POWERD_LOCK_FILE_PATH, strerror(errno));
92 }
93
94 /* Check after creating lock file to avoid race with powerd. */
95 if (check_suspend_imminent()) {
96 restore_power_management();
97 return 2;
98 }
99
100 return rc;
101 }
102
restore_power_management(void)103 int restore_power_management(void)
104 {
105 int result = 0;
106
107 msg_pdbg("%s: Re-enabling power management.\n", __func__);
108
109 result = unlink(POWERD_LOCK_FILE_PATH);
110 if (result != 0 && errno != ENOENT) {
111 msg_perr("%s: Failed to unlink %s: %s\n",
112 __func__, POWERD_LOCK_FILE_PATH, strerror(errno));
113 return 1;
114 }
115 return 0;
116 }
117