blob: 9201d3b37beea9b215c810afe0c93918d0eb75fd [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 *
David Hendricks42ad6522011-08-09 16:08:10 -070015 *
16 * power.c: power management routines
17 */
18
Daniel Eratf32a0032014-09-09 10:31:07 -070019#include <errno.h>
20#include <stdio.h>
David Hendricks42ad6522011-08-09 16:08:10 -070021#include <stdlib.h>
22#include <string.h>
Sam McNallye50562c2020-12-07 15:39:32 +110023#include <sys/stat.h>
Daniel Eratf32a0032014-09-09 10:31:07 -070024#include <sys/types.h>
25#include <unistd.h>
Paul Kocialkowskibea97e52017-03-26 17:20:44 +020026#include <limits.h>
David Hendricks42ad6522011-08-09 16:08:10 -070027
28#include "flash.h" /* for msg_* */
29#include "power.h"
30
Daniel Eratf32a0032014-09-09 10:31:07 -070031/*
Daniel Eratd6fca5d2018-02-05 10:34:21 -080032 * 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 Eratf32a0032014-09-09 10:31:07 -070040 */
Daniel Eratd6fca5d2018-02-05 10:34:21 -080041static 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 Hendricks42ad6522011-08-09 16:08:10 -070047
Nikolai Artemiev8d7e8392021-02-12 11:38:09 +110048int disable_power_management(void)
David Hendricks42ad6522011-08-09 16:08:10 -070049{
Daniel Eratf32a0032014-09-09 10:31:07 -070050 FILE *lock_file = NULL;
Daniel Eratd6fca5d2018-02-05 10:34:21 -080051 const char *path = NULL;
David Hendricks42ad6522011-08-09 16:08:10 -070052 int rc = 0;
Sam McNallye50562c2020-12-07 15:39:32 +110053 mode_t old_umask;
Daniel Erat4166d342017-12-05 14:32:52 -080054
Daniel Eratf32a0032014-09-09 10:31:07 -070055 msg_pdbg("%s: Disabling power management.\n", __func__);
56
Daniel Eratd6fca5d2018-02-05 10:34:21 -080057 path = get_powerd_lock_file_path();
58
Sam McNallye50562c2020-12-07 15:39:32 +110059 old_umask = umask(022);
60 lock_file = fopen(path, "w");
61 umask(old_umask);
62 if (!lock_file) {
Daniel Eratf32a0032014-09-09 10:31:07 -070063 msg_perr("%s: Failed to open %s for writing: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080064 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070065 return 1;
David Hendricks42ad6522011-08-09 16:08:10 -070066 }
67
Daniel Eratf32a0032014-09-09 10:31:07 -070068 if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
69 msg_perr("%s: Failed to write PID to %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080070 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070071 rc = 1;
David Hendricks42ad6522011-08-09 16:08:10 -070072 }
73
Daniel Eratf32a0032014-09-09 10:31:07 -070074 if (fclose(lock_file) != 0) {
75 msg_perr("%s: Failed to close %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080076 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070077 }
David Hendricks42ad6522011-08-09 16:08:10 -070078 return rc;
Daniel Eratf32a0032014-09-09 10:31:07 -070079
80}
81
Nikolai Artemiev8d7e8392021-02-12 11:38:09 +110082int restore_power_management(void)
Daniel Eratf32a0032014-09-09 10:31:07 -070083{
Daniel Eratd6fca5d2018-02-05 10:34:21 -080084 const char *path = NULL;
Daniel Eratf32a0032014-09-09 10:31:07 -070085 int result = 0;
Daniel Erat4166d342017-12-05 14:32:52 -080086
Daniel Eratf32a0032014-09-09 10:31:07 -070087 msg_pdbg("%s: Re-enabling power management.\n", __func__);
88
Daniel Eratd6fca5d2018-02-05 10:34:21 -080089 path = get_powerd_lock_file_path();
90
91 result = unlink(path);
Daniel Eratf32a0032014-09-09 10:31:07 -070092 if (result != 0 && errno != ENOENT) {
93 msg_perr("%s: Failed to unlink %s: %s\n",
Daniel Eratd6fca5d2018-02-05 10:34:21 -080094 __func__, path, strerror(errno));
Daniel Eratf32a0032014-09-09 10:31:07 -070095 return 1;
96 }
97 return 0;
David Hendricks42ad6522011-08-09 16:08:10 -070098}