blob: 82b9f78ed0fd37e2d5f4301d46053f171b4f50eb [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_* */
31#include "power.h"
32
Daniel Eratf32a0032014-09-09 10:31:07 -070033/*
Daniel Eratd6fca5d2018-02-05 10:34:21 -080034 * 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 Eratf32a0032014-09-09 10:31:07 -070042 */
Daniel Eratd6fca5d2018-02-05 10:34:21 -080043static 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 Hendricks42ad6522011-08-09 16:08:10 -070049
David Hendricks42ad6522011-08-09 16:08:10 -070050int disable_power_management()
51{
Daniel Eratf32a0032014-09-09 10:31:07 -070052 FILE *lock_file = NULL;
Daniel Eratd6fca5d2018-02-05 10:34:21 -080053 const char *path = NULL;
David Hendricks42ad6522011-08-09 16:08:10 -070054 int rc = 0;
Daniel Erat4166d342017-12-05 14:32:52 -080055
Daniel Eratf32a0032014-09-09 10:31:07 -070056 msg_pdbg("%s: Disabling power management.\n", __func__);
57
Daniel Eratd6fca5d2018-02-05 10:34:21 -080058 path = get_powerd_lock_file_path();
59
60 if (!(lock_file = fopen(path, "w"))) {
Daniel Eratf32a0032014-09-09 10:31:07 -070061 msg_perr("%s: Failed to open %s for writing: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080062 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070063 return 1;
David Hendricks42ad6522011-08-09 16:08:10 -070064 }
65
Daniel Eratf32a0032014-09-09 10:31:07 -070066 if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
67 msg_perr("%s: Failed to write PID to %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080068 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070069 rc = 1;
David Hendricks42ad6522011-08-09 16:08:10 -070070 }
71
Daniel Eratf32a0032014-09-09 10:31:07 -070072 if (fclose(lock_file) != 0) {
73 msg_perr("%s: Failed to close %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080074 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070075 }
David Hendricks42ad6522011-08-09 16:08:10 -070076 return rc;
Daniel Eratf32a0032014-09-09 10:31:07 -070077
78}
79
80int restore_power_management()
81{
Daniel Eratd6fca5d2018-02-05 10:34:21 -080082 const char *path = NULL;
Daniel Eratf32a0032014-09-09 10:31:07 -070083 int result = 0;
Daniel Erat4166d342017-12-05 14:32:52 -080084
Daniel Eratf32a0032014-09-09 10:31:07 -070085 msg_pdbg("%s: Re-enabling power management.\n", __func__);
86
Daniel Eratd6fca5d2018-02-05 10:34:21 -080087 path = get_powerd_lock_file_path();
88
89 result = unlink(path);
Daniel Eratf32a0032014-09-09 10:31:07 -070090 if (result != 0 && errno != ENOENT) {
91 msg_perr("%s: Failed to unlink %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080092 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070093 return 1;
94 }
95 return 0;
David Hendricks42ad6522011-08-09 16:08:10 -070096}