blob: f8805cfa25d9d62c192a8998ed0564e9737e5264 [file] [log] [blame]
David Hendricks42ad6522011-08-09 16:08:10 -07001/*
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 Eratf32a0032014-09-09 10:31:07 -070022#include <errno.h>
23#include <stdio.h>
David Hendricks42ad6522011-08-09 16:08:10 -070024#include <stdlib.h>
25#include <string.h>
Daniel Eratf32a0032014-09-09 10:31:07 -070026#include <sys/types.h>
27#include <unistd.h>
Paul Kocialkowskibea97e52017-03-26 17:20:44 +020028#include <limits.h>
David Hendricks42ad6522011-08-09 16:08:10 -070029
30#include "flash.h" /* for msg_* */
David Hendricksea66c642016-06-09 13:28:58 -070031#include "locks.h" /* for SYSTEM_LOCKFILE_DIR */
David Hendricks42ad6522011-08-09 16:08:10 -070032#include "power.h"
33
Daniel Eratf32a0032014-09-09 10:31:07 -070034/*
35 * Path to a file containing flashrom's PID. While present, powerd avoids
36 * suspending or shutting down the system.
37 */
David Hendricksea66c642016-06-09 13:28:58 -070038static const char powerd_lock_file_name[] = "flashrom_powerd.lock";
David Hendricks42ad6522011-08-09 16:08:10 -070039
David Hendricks42ad6522011-08-09 16:08:10 -070040int disable_power_management()
41{
Daniel Eratf32a0032014-09-09 10:31:07 -070042 FILE *lock_file = NULL;
David Hendricks42ad6522011-08-09 16:08:10 -070043 int rc = 0;
David Hendricksea66c642016-06-09 13:28:58 -070044 char lock_file_path[PATH_MAX];
David Hendricks42ad6522011-08-09 16:08:10 -070045
Daniel Eratf32a0032014-09-09 10:31:07 -070046 msg_pdbg("%s: Disabling power management.\n", __func__);
47
David Hendricksea66c642016-06-09 13:28:58 -070048 if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s",
49 SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0)
50 return 1;
51
Daniel Eratf32a0032014-09-09 10:31:07 -070052 if (!(lock_file = fopen(lock_file_path, "w"))) {
53 msg_perr("%s: Failed to open %s for writing: %s\n",
54 __func__, lock_file_path, strerror(errno));
55 return 1;
David Hendricks42ad6522011-08-09 16:08:10 -070056 }
57
Daniel Eratf32a0032014-09-09 10:31:07 -070058 if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
59 msg_perr("%s: Failed to write PID to %s: %s\n",
60 __func__, lock_file_path, strerror(errno));
61 rc = 1;
David Hendricks42ad6522011-08-09 16:08:10 -070062 }
63
Daniel Eratf32a0032014-09-09 10:31:07 -070064 if (fclose(lock_file) != 0) {
65 msg_perr("%s: Failed to close %s: %s\n",
66 __func__, lock_file_path, strerror(errno));
67 }
David Hendricks42ad6522011-08-09 16:08:10 -070068 return rc;
Daniel Eratf32a0032014-09-09 10:31:07 -070069
70}
71
72int restore_power_management()
73{
74 int result = 0;
David Hendricksea66c642016-06-09 13:28:58 -070075 char lock_file_path[PATH_MAX];
Daniel Eratf32a0032014-09-09 10:31:07 -070076
77 msg_pdbg("%s: Re-enabling power management.\n", __func__);
78
David Hendricksea66c642016-06-09 13:28:58 -070079 if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s",
80 SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0)
81 return 1;
82
Daniel Eratf32a0032014-09-09 10:31:07 -070083 result = unlink(lock_file_path);
84 if (result != 0 && errno != ENOENT) {
85 msg_perr("%s: Failed to unlink %s: %s\n",
86 __func__, lock_file_path, strerror(errno));
87 return 1;
88 }
89 return 0;
David Hendricks42ad6522011-08-09 16:08:10 -070090}