blob: 65162f9233858f0a4bded562a3ba5fd9378f9d46 [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>
David Hendricks42ad6522011-08-09 16:08:10 -070028
29#include "flash.h" /* for msg_* */
30#include "power.h"
31
Daniel Eratf32a0032014-09-09 10:31:07 -070032/*
33 * Path to a file containing flashrom's PID. While present, powerd avoids
34 * suspending or shutting down the system.
35 */
36static const char lock_file_path[] = "/var/lock/flashrom_powerd.lock";
David Hendricks42ad6522011-08-09 16:08:10 -070037
David Hendricks42ad6522011-08-09 16:08:10 -070038int disable_power_management()
39{
Daniel Eratf32a0032014-09-09 10:31:07 -070040 FILE *lock_file = NULL;
David Hendricks42ad6522011-08-09 16:08:10 -070041 int rc = 0;
42
Daniel Eratf32a0032014-09-09 10:31:07 -070043 msg_pdbg("%s: Disabling power management.\n", __func__);
44
45 if (!(lock_file = fopen(lock_file_path, "w"))) {
46 msg_perr("%s: Failed to open %s for writing: %s\n",
47 __func__, lock_file_path, strerror(errno));
48 return 1;
David Hendricks42ad6522011-08-09 16:08:10 -070049 }
50
Daniel Eratf32a0032014-09-09 10:31:07 -070051 if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
52 msg_perr("%s: Failed to write PID to %s: %s\n",
53 __func__, lock_file_path, strerror(errno));
54 rc = 1;
David Hendricks42ad6522011-08-09 16:08:10 -070055 }
56
Daniel Eratf32a0032014-09-09 10:31:07 -070057 if (fclose(lock_file) != 0) {
58 msg_perr("%s: Failed to close %s: %s\n",
59 __func__, lock_file_path, strerror(errno));
60 }
David Hendricks42ad6522011-08-09 16:08:10 -070061 return rc;
Daniel Eratf32a0032014-09-09 10:31:07 -070062
63}
64
65int restore_power_management()
66{
67 int result = 0;
68
69 msg_pdbg("%s: Re-enabling power management.\n", __func__);
70
71 result = unlink(lock_file_path);
72 if (result != 0 && errno != ENOENT) {
73 msg_perr("%s: Failed to unlink %s: %s\n",
74 __func__, lock_file_path, strerror(errno));
75 return 1;
76 }
77 return 0;
David Hendricks42ad6522011-08-09 16:08:10 -070078}