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