1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7 Copyright 2018 Dell Inc.
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.
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.
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/>.
27 #include "sd-messages.h"
29 #include "parse-util.h"
31 #include "exec-util.h"
35 #include "sleep-config.h"
36 #include "stdio-util.h"
37 #include "string-util.h"
41 static char* arg_verb
= NULL
;
43 static int write_mode(char **modes
) {
47 STRV_FOREACH(mode
, modes
) {
50 k
= write_string_file("/sys/power/disk", *mode
, 0);
54 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m",
61 log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");
66 static int write_state(FILE **f
, char **states
) {
70 STRV_FOREACH(state
, states
) {
73 k
= write_string_stream(*f
, *state
, 0);
76 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m",
82 *f
= fopen("/sys/power/state", "we");
84 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
90 static int execute(char **modes
, char **states
) {
98 static const char* const dirs
[] = {
104 _cleanup_fclose_
FILE *f
= NULL
;
106 /* This file is opened first, so that if we hit an error,
107 * we can abort before modifying any state. */
108 f
= fopen("/sys/power/state", "we");
110 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
112 /* Configure the hibernation mode */
113 r
= write_mode(modes
);
117 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
120 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR
,
121 LOG_MESSAGE("Suspending system..."),
122 "SLEEP=%s", arg_verb
,
125 r
= write_state(&f
, states
);
130 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
131 LOG_MESSAGE("System resumed."),
132 "SLEEP=%s", arg_verb
,
135 arguments
[1] = (char*) "post";
136 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
141 static int read_wakealarm(uint64_t *result
) {
142 _cleanup_free_
char *t
= NULL
;
144 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t
) >= 0)
145 return safe_atou64(t
, result
);
149 static int write_wakealarm(const char *str
) {
151 _cleanup_fclose_
FILE *f
= NULL
;
154 f
= fopen("/sys/class/rtc/rtc0/wakealarm", "we");
156 return log_error_errno(errno
, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
158 r
= write_string_stream(f
, str
, 0);
160 return log_error_errno(r
, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str
);
165 static int execute_s2h(usec_t hibernate_delay_sec
) {
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)];
173 r
= parse_sleep_config("suspend", &suspend_modes
, &suspend_states
,
178 r
= parse_sleep_config("hibernate", &hibernate_modes
,
179 &hibernate_states
, NULL
);
183 r
= read_wakealarm(&orig_time
);
185 return log_error_errno(errno
, "Failed to read time: %d", r
);
187 orig_time
+= hibernate_delay_sec
/ USEC_PER_SEC
;
188 xsprintf(time_str
, "%" PRIu64
, orig_time
);
190 r
= write_wakealarm(time_str
);
194 log_debug("Set RTC wake alarm for %s", time_str
);
196 r
= execute(suspend_modes
, suspend_states
);
200 r
= read_wakealarm(&cmp_time
);
202 return log_error_errno(errno
, "Failed to read time: %d", r
);
205 r
= write_wakealarm("0");
209 log_debug("Woke up at %"PRIu64
, cmp_time
);
211 /* if woken up after alarm time, hibernate */
212 if (cmp_time
>= orig_time
)
213 r
= execute(hibernate_modes
, hibernate_states
);
218 static void help(void) {
219 printf("%s COMMAND\n\n"
220 "Suspend the system, hibernate the system, or both.\n\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"
227 " suspend-to-hibernate Initially suspend and then hibernate\n"
228 " the system after a fixed period of time\n"
229 , program_invocation_short_name
);
232 static int parse_argv(int argc
, char *argv
[]) {
237 static const struct option options
[] = {
238 { "help", no_argument
, NULL
, 'h' },
239 { "version", no_argument
, NULL
, ARG_VERSION
},
248 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
261 assert_not_reached("Unhandled option");
264 if (argc
- optind
!= 1) {
265 log_error("Usage: %s COMMAND",
266 program_invocation_short_name
);
270 arg_verb
= argv
[optind
];
272 if (!streq(arg_verb
, "suspend") &&
273 !streq(arg_verb
, "hibernate") &&
274 !streq(arg_verb
, "hybrid-sleep") &&
275 !streq(arg_verb
, "suspend-to-hibernate")) {
276 log_error("Unknown command '%s'.", arg_verb
);
280 return 1 /* work to do */;
283 int main(int argc
, char *argv
[]) {
284 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
288 log_set_target(LOG_TARGET_AUTO
);
289 log_parse_environment();
292 r
= parse_argv(argc
, argv
);
296 r
= parse_sleep_config(arg_verb
, &modes
, &states
, &delay
);
300 if (streq(arg_verb
, "suspend-to-hibernate"))
301 r
= execute_s2h(delay
);
303 r
= execute(modes
, states
);
305 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;