]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sleep/sleep.c
tree-wide: remove unused variables (#8612)
[thirdparty/systemd.git] / src / sleep / sleep.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6edd7d0a
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
19adb8a3 6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
c58493c0 7 Copyright 2018 Dell Inc.
6edd7d0a
LP
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
6edd7d0a 23#include <errno.h>
19adb8a3 24#include <getopt.h>
3f6fd1ba 25#include <stdio.h>
6edd7d0a 26
aa62a893 27#include "sd-messages.h"
3f6fd1ba 28
c58493c0 29#include "parse-util.h"
3f6fd1ba 30#include "def.h"
89711996 31#include "exec-util.h"
3ffd4af2 32#include "fd-util.h"
a5c32cff 33#include "fileio.h"
3f6fd1ba 34#include "log.h"
19adb8a3 35#include "sleep-config.h"
c58493c0 36#include "stdio-util.h"
07630cea 37#include "string-util.h"
3f6fd1ba
LP
38#include "strv.h"
39#include "util.h"
19adb8a3
ZJS
40
41static char* arg_verb = NULL;
42
43static int write_mode(char **modes) {
44 int r = 0;
45 char **mode;
46
47 STRV_FOREACH(mode, modes) {
aa62a893
LP
48 int k;
49
4c1fc3e4 50 k = write_string_file("/sys/power/disk", *mode, 0);
19adb8a3
ZJS
51 if (k == 0)
52 return 0;
aa62a893 53
c33b3297
MS
54 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
55 *mode);
19adb8a3
ZJS
56 if (r == 0)
57 r = k;
58 }
6edd7d0a 59
19adb8a3 60 if (r < 0)
c33b3297 61 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
6edd7d0a 62
19adb8a3
ZJS
63 return r;
64}
6edd7d0a 65
2fd069b1 66static int write_state(FILE **f, char **states) {
19adb8a3
ZJS
67 char **state;
68 int r = 0;
69
70 STRV_FOREACH(state, states) {
71 int k;
72
b1837133 73 k = write_string_stream(*f, *state, 0);
19adb8a3
ZJS
74 if (k == 0)
75 return 0;
c33b3297
MS
76 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
77 *state);
19adb8a3
ZJS
78 if (r == 0)
79 r = k;
80
2fd069b1
ZJS
81 fclose(*f);
82 *f = fopen("/sys/power/state", "we");
4a62c710
MS
83 if (!*f)
84 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
6edd7d0a
LP
85 }
86
19adb8a3
ZJS
87 return r;
88}
6edd7d0a 89
19adb8a3 90static int execute(char **modes, char **states) {
e2cc6eca
LP
91
92 char *arguments[] = {
93 NULL,
94 (char*) "pre",
95 arg_verb,
96 NULL
97 };
b5084605
LP
98 static const char* const dirs[] = {
99 SYSTEM_SLEEP_PATH,
100 NULL
101 };
e2cc6eca 102
19adb8a3 103 int r;
2fd069b1 104 _cleanup_fclose_ FILE *f = NULL;
6524990f 105
19adb8a3 106 /* This file is opened first, so that if we hit an error,
09692409 107 * we can abort before modifying any state. */
6edd7d0a 108 f = fopen("/sys/power/state", "we");
4a62c710
MS
109 if (!f)
110 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
6edd7d0a 111
19adb8a3
ZJS
112 /* Configure the hibernation mode */
113 r = write_mode(modes);
114 if (r < 0)
115 return r;
116
c6e47247 117 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
6edd7d0a 118
19adb8a3 119 log_struct(LOG_INFO,
2b044526 120 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
e2cc6eca
LP
121 LOG_MESSAGE("Suspending system..."),
122 "SLEEP=%s", arg_verb,
19adb8a3
ZJS
123 NULL);
124
2fd069b1 125 r = write_state(&f, states);
19adb8a3
ZJS
126 if (r < 0)
127 return r;
128
129 log_struct(LOG_INFO,
2b044526 130 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
a5ccdb98 131 LOG_MESSAGE("System resumed."),
e2cc6eca 132 "SLEEP=%s", arg_verb,
19adb8a3 133 NULL);
eb267289 134
6edd7d0a 135 arguments[1] = (char*) "post";
c6e47247 136 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
6edd7d0a 137
19adb8a3
ZJS
138 return r;
139}
6edd7d0a 140
c58493c0
ML
141static int read_wakealarm(uint64_t *result) {
142 _cleanup_free_ char *t = NULL;
143
144 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
145 return safe_atou64(t, result);
146 return -EBADF;
147}
148
149static int write_wakealarm(const char *str) {
150
151 _cleanup_fclose_ FILE *f = NULL;
152 int r;
153
154 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
155 if (!f)
156 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
157
158 r = write_string_stream(f, str, 0);
159 if (r < 0)
160 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
161
162 return 0;
163}
164
165static int execute_s2h(usec_t hibernate_delay_sec) {
166
167 _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
168 **suspend_modes = NULL, **suspend_states = NULL;
169 usec_t orig_time, cmp_time;
170 char time_str[DECIMAL_STR_MAX(uint64_t)];
171 int r;
172
173 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
174 NULL);
175 if (r < 0)
176 return r;
177
178 r = parse_sleep_config("hibernate", &hibernate_modes,
179 &hibernate_states, NULL);
180 if (r < 0)
181 return r;
182
183 r = read_wakealarm(&orig_time);
184 if (r < 0)
185 return log_error_errno(errno, "Failed to read time: %d", r);
186
187 orig_time += hibernate_delay_sec / USEC_PER_SEC;
188 xsprintf(time_str, "%" PRIu64, orig_time);
189
190 r = write_wakealarm(time_str);
191 if (r < 0)
192 return r;
193
194 log_debug("Set RTC wake alarm for %s", time_str);
195
196 r = execute(suspend_modes, suspend_states);
197 if (r < 0)
198 return r;
199
200 r = read_wakealarm(&cmp_time);
201 if (r < 0)
202 return log_error_errno(errno, "Failed to read time: %d", r);
203
204 /* reset RTC */
205 r = write_wakealarm("0");
206 if (r < 0)
207 return r;
208
209 log_debug("Woke up at %"PRIu64, cmp_time);
210
211 /* if woken up after alarm time, hibernate */
212 if (cmp_time >= orig_time)
213 r = execute(hibernate_modes, hibernate_states);
214
215 return r;
216}
217
601185b4 218static void help(void) {
19adb8a3
ZJS
219 printf("%s COMMAND\n\n"
220 "Suspend the system, hibernate the system, or both.\n\n"
221 "Commands:\n"
222 " -h --help Show this help and exit\n"
223 " --version Print version string and exit\n"
224 " suspend Suspend the system\n"
225 " hibernate Hibernate the system\n"
226 " hybrid-sleep Both hibernate and suspend the system\n"
e68c79db 227 " suspend-then-hibernate Initially suspend and then hibernate\n"
c58493c0 228 " the system after a fixed period of time\n"
601185b4 229 , program_invocation_short_name);
19adb8a3 230}
6edd7d0a 231
19adb8a3
ZJS
232static int parse_argv(int argc, char *argv[]) {
233 enum {
234 ARG_VERSION = 0x100,
235 };
236
237 static const struct option options[] = {
238 { "help", no_argument, NULL, 'h' },
239 { "version", no_argument, NULL, ARG_VERSION },
eb9da376 240 {}
19adb8a3
ZJS
241 };
242
243 int c;
244
245 assert(argc >= 0);
246 assert(argv);
247
601185b4 248 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
19adb8a3
ZJS
249 switch(c) {
250 case 'h':
601185b4
ZJS
251 help();
252 return 0; /* done */
19adb8a3
ZJS
253
254 case ARG_VERSION:
3f6fd1ba 255 return version();
19adb8a3
ZJS
256
257 case '?':
258 return -EINVAL;
259
260 default:
eb9da376 261 assert_not_reached("Unhandled option");
19adb8a3
ZJS
262 }
263
264 if (argc - optind != 1) {
265 log_error("Usage: %s COMMAND",
266 program_invocation_short_name);
267 return -EINVAL;
268 }
269
270 arg_verb = argv[optind];
6edd7d0a 271
19adb8a3
ZJS
272 if (!streq(arg_verb, "suspend") &&
273 !streq(arg_verb, "hibernate") &&
c58493c0 274 !streq(arg_verb, "hybrid-sleep") &&
e68c79db 275 !streq(arg_verb, "suspend-then-hibernate")) {
19adb8a3
ZJS
276 log_error("Unknown command '%s'.", arg_verb);
277 return -EINVAL;
278 }
279
280 return 1 /* work to do */;
281}
282
283int main(int argc, char *argv[]) {
284 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
c58493c0 285 usec_t delay = 0;
19adb8a3
ZJS
286 int r;
287
288 log_set_target(LOG_TARGET_AUTO);
289 log_parse_environment();
290 log_open();
291
292 r = parse_argv(argc, argv);
293 if (r <= 0)
294 goto finish;
295
c58493c0 296 r = parse_sleep_config(arg_verb, &modes, &states, &delay);
19adb8a3
ZJS
297 if (r < 0)
298 goto finish;
299
e68c79db 300 if (streq(arg_verb, "suspend-then-hibernate"))
c58493c0
ML
301 r = execute_s2h(delay);
302 else
303 r = execute(modes, states);
19adb8a3
ZJS
304finish:
305 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
6edd7d0a 306}