]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sleep/sleep.c
basic,shared: move disable_coredumps() to coredump-util.[ch]
[thirdparty/systemd.git] / src / sleep / sleep.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6edd7d0a 2/***
96b2fb93
LP
3 Copyright © 2010-2017 Canonical
4 Copyright © 2018 Dell Inc.
6edd7d0a
LP
5***/
6
6edd7d0a 7#include <errno.h>
ca78ad1d 8#include <fcntl.h>
19adb8a3 9#include <getopt.h>
17c40b3a 10#include <linux/fiemap.h>
1bbbefe7 11#include <poll.h>
ca78ad1d
ZJS
12#include <sys/stat.h>
13#include <sys/types.h>
1bbbefe7 14#include <sys/timerfd.h>
ca78ad1d 15#include <unistd.h>
6edd7d0a 16
f13f91f0 17#include "sd-bus.h"
aa62a893 18#include "sd-messages.h"
3f6fd1ba 19
64602c84 20#include "btrfs-util.h"
ba0fb5ac 21#include "bus-error.h"
f13f91f0
SS
22#include "bus-locator.h"
23#include "bus-util.h"
3f6fd1ba 24#include "def.h"
89711996 25#include "exec-util.h"
3ffd4af2 26#include "fd-util.h"
a5c32cff 27#include "fileio.h"
ba0fb5ac 28#include "format-util.h"
0f2d351f 29#include "io-util.h"
3f6fd1ba 30#include "log.h"
5e332028 31#include "main-func.h"
ed698d30 32#include "parse-util.h"
294bf0c3 33#include "pretty-print.h"
19adb8a3 34#include "sleep-config.h"
f13f91f0 35#include "special.h"
c58493c0 36#include "stdio-util.h"
07630cea 37#include "string-util.h"
3f6fd1ba 38#include "strv.h"
1bbbefe7 39#include "time-util.h"
3f6fd1ba 40#include "util.h"
19adb8a3 41
c8cd8ca3 42static SleepOperation arg_operation = _SLEEP_OPERATION_INVALID;
98dc9d1f 43
7bdf56a2 44static int write_hibernate_location_info(const HibernateLocation *hibernate_location) {
17c40b3a 45 char offset_str[DECIMAL_STR_MAX(uint64_t)];
52133271 46 char resume_str[DECIMAL_STR_MAX(unsigned) * 2 + STRLEN(":")];
17c40b3a
ML
47 int r;
48
7bdf56a2
ZS
49 assert(hibernate_location);
50 assert(hibernate_location->swap);
7bdf56a2 51
52133271
ZS
52 xsprintf(resume_str, "%u:%u", major(hibernate_location->devno), minor(hibernate_location->devno));
53 r = write_string_file("/sys/power/resume", resume_str, WRITE_STRING_FILE_DISABLE_BUFFER);
17c40b3a 54 if (r < 0)
7bdf56a2 55 return log_debug_errno(r, "Failed to write partition device to /sys/power/resume for '%s': '%s': %m",
52133271 56 hibernate_location->swap->device, resume_str);
17c40b3a 57
52133271 58 log_debug("Wrote resume= value for %s to /sys/power/resume: %s", hibernate_location->swap->device, resume_str);
ed698d30 59
7bdf56a2
ZS
60 /* if it's a swap partition, we're done */
61 if (streq(hibernate_location->swap->type, "partition"))
ed698d30 62 return r;
7bdf56a2
ZS
63
64 if (!streq(hibernate_location->swap->type, "file"))
baaa35ad 65 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
7bdf56a2 66 "Invalid hibernate type: %s", hibernate_location->swap->type);
17c40b3a
ML
67
68 /* Only available in 4.17+ */
52133271 69 if (hibernate_location->offset > 0 && access("/sys/power/resume_offset", W_OK) < 0) {
c695101f 70 if (errno == ENOENT) {
7bdf56a2 71 log_debug("Kernel too old, can't configure resume_offset for %s, ignoring: %" PRIu64,
52133271 72 hibernate_location->swap->device, hibernate_location->offset);
17c40b3a 73 return 0;
c695101f 74 }
ed698d30 75
3f2e15ab 76 return log_debug_errno(errno, "/sys/power/resume_offset not writable: %m");
c695101f 77 }
17c40b3a 78
52133271 79 xsprintf(offset_str, "%" PRIu64, hibernate_location->offset);
57512c89 80 r = write_string_file("/sys/power/resume_offset", offset_str, WRITE_STRING_FILE_DISABLE_BUFFER);
17c40b3a 81 if (r < 0)
7bdf56a2
ZS
82 return log_debug_errno(r, "Failed to write swap file offset to /sys/power/resume_offset for '%s': '%s': %m",
83 hibernate_location->swap->device, offset_str);
17c40b3a 84
7bdf56a2 85 log_debug("Wrote resume_offset= value for %s to /sys/power/resume_offset: %s", hibernate_location->swap->device, offset_str);
2002d8cd 86
17c40b3a
ML
87 return 0;
88}
89
19adb8a3
ZJS
90static int write_mode(char **modes) {
91 int r = 0;
19adb8a3
ZJS
92
93 STRV_FOREACH(mode, modes) {
aa62a893
LP
94 int k;
95
57512c89 96 k = write_string_file("/sys/power/disk", *mode, WRITE_STRING_FILE_DISABLE_BUFFER);
ed698d30 97 if (k >= 0)
19adb8a3 98 return 0;
aa62a893 99
ed698d30
LP
100 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode);
101 if (r >= 0)
19adb8a3
ZJS
102 r = k;
103 }
6edd7d0a 104
19adb8a3
ZJS
105 return r;
106}
6edd7d0a 107
2fd069b1 108static int write_state(FILE **f, char **states) {
19adb8a3
ZJS
109 int r = 0;
110
2c470205
LP
111 assert(f);
112 assert(*f);
113
19adb8a3
ZJS
114 STRV_FOREACH(state, states) {
115 int k;
116
57512c89 117 k = write_string_stream(*f, *state, WRITE_STRING_FILE_DISABLE_BUFFER);
ed698d30 118 if (k >= 0)
19adb8a3 119 return 0;
ed698d30
LP
120 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state);
121 if (r >= 0)
19adb8a3
ZJS
122 r = k;
123
2fd069b1
ZJS
124 fclose(*f);
125 *f = fopen("/sys/power/state", "we");
4a62c710 126 if (!*f)
0ca906ac 127 return -errno;
6edd7d0a
LP
128 }
129
19adb8a3
ZJS
130 return r;
131}
6edd7d0a 132
ba0fb5ac
LP
133static int lock_all_homes(void) {
134 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
135 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
136 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
137 int r;
138
139 /* Let's synchronously lock all home directories managed by homed that have been marked for it. This
140 * way the key material required to access these volumes is hopefully removed from memory. */
141
142 r = sd_bus_open_system(&bus);
143 if (r < 0)
144 return log_warning_errno(r, "Failed to connect to system bus, ignoring: %m");
145
146 r = sd_bus_message_new_method_call(
147 bus,
148 &m,
149 "org.freedesktop.home1",
150 "/org/freedesktop/home1",
151 "org.freedesktop.home1.Manager",
152 "LockAllHomes");
153 if (r < 0)
154 return bus_log_create_error(r);
155
156 /* If homed is not running it can't have any home directories active either. */
157 r = sd_bus_message_set_auto_start(m, false);
158 if (r < 0)
159 return log_error_errno(r, "Failed to disable auto-start of LockAllHomes() message: %m");
160
161 r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC, &error, NULL);
162 if (r < 0) {
1c5950bd
ZJS
163 if (!bus_error_is_unknown_service(&error))
164 return log_error_errno(r, "Failed to lock home directories: %s", bus_error_message(&error, r));
ba0fb5ac 165
75029e15
ZJS
166 log_debug("systemd-homed is not running, locking of home directories skipped.");
167 } else
168 log_debug("Successfully requested locking of all home directories.");
169 return 0;
ba0fb5ac
LP
170}
171
c8cd8ca3
LP
172static int execute(
173 const SleepConfig *sleep_config,
174 SleepOperation operation,
175 const char *action) {
176
e2cc6eca
LP
177 char *arguments[] = {
178 NULL,
179 (char*) "pre",
c8cd8ca3
LP
180 /* NB: we use 'arg_operation' instead of 'operation' here, as we want to communicate the overall
181 * operation here, not the specific one, in case of s2h. */
182 (char*) sleep_operation_to_string(arg_operation),
e2cc6eca
LP
183 NULL
184 };
b5084605
LP
185 static const char* const dirs[] = {
186 SYSTEM_SLEEP_PATH,
187 NULL
188 };
e2cc6eca 189
7bdf56a2 190 _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
c8cd8ca3
LP
191 _cleanup_fclose_ FILE *f = NULL;
192 char **modes, **states;
7bdf56a2 193 int r;
6524990f 194
c8cd8ca3
LP
195 assert(sleep_config);
196 assert(operation >= 0);
197 assert(operation < _SLEEP_OPERATION_MAX);
198 assert(operation != SLEEP_SUSPEND_THEN_HIBERNATE); /* Handled by execute_s2h() instead */
199
200 states = sleep_config->states[operation];
201 modes = sleep_config->modes[operation];
202
203 if (strv_isempty(states))
204 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
205 "No sleep states configured for sleep operation %s, can't sleep.",
206 sleep_operation_to_string(operation));
207
19adb8a3 208 /* This file is opened first, so that if we hit an error,
09692409 209 * we can abort before modifying any state. */
6edd7d0a 210 f = fopen("/sys/power/state", "we");
4a62c710
MS
211 if (!f)
212 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
6edd7d0a 213
57512c89
YW
214 setvbuf(f, NULL, _IONBF, 0);
215
c02540dc 216 /* Configure hibernation settings if we are supposed to hibernate */
17c40b3a 217 if (!strv_isempty(modes)) {
7bdf56a2 218 r = find_hibernate_location(&hibernate_location);
17c40b3a 219 if (r < 0)
c02540dc
LP
220 return log_error_errno(r, "Failed to find location to hibernate to: %m");
221 if (r == 0) { /* 0 means: no hibernation location was configured in the kernel so far, let's
222 * do it ourselves then. > 0 means: kernel already had a configured hibernation
223 * location which we shouldn't touch. */
7bdf56a2
ZS
224 r = write_hibernate_location_info(hibernate_location);
225 if (r < 0)
226 return log_error_errno(r, "Failed to prepare for hibernation: %m");
227 }
2002d8cd 228
17c40b3a
ML
229 r = write_mode(modes);
230 if (r < 0)
aea29a30 231 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
17c40b3a 232 }
19adb8a3 233
c8cd8ca3
LP
234 /* Pass an action string to the call-outs. This is mostly our operation string, except if the
235 * hibernate step of s-t-h fails, in which case we communicate that with a separate action. */
236 if (!action)
237 action = sleep_operation_to_string(operation);
238
ae463e4e
ZS
239 r = setenv("SYSTEMD_SLEEP_ACTION", action, 1);
240 if (r != 0)
c3565fe8 241 log_warning_errno(errno, "Error setting SYSTEMD_SLEEP_ACTION=%s, ignoring: %m", action);
ae463e4e 242
aed98342 243 (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
ba0fb5ac 244 (void) lock_all_homes();
6edd7d0a 245
19adb8a3 246 log_struct(LOG_INFO,
2b044526 247 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
c8cd8ca3
LP
248 LOG_MESSAGE("Entering sleep state '%s'...", sleep_operation_to_string(operation)),
249 "SLEEP=%s", sleep_operation_to_string(arg_operation));
19adb8a3 250
2fd069b1 251 r = write_state(&f, states);
19adb8a3 252 if (r < 0)
14250f09
LP
253 log_struct_errno(LOG_ERR, r,
254 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
c8cd8ca3
LP
255 LOG_MESSAGE("Failed to put system to sleep. System resumed again: %m"),
256 "SLEEP=%s", sleep_operation_to_string(arg_operation));
14250f09
LP
257 else
258 log_struct(LOG_INFO,
259 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
c8cd8ca3
LP
260 LOG_MESSAGE("System returned from sleep state."),
261 "SLEEP=%s", sleep_operation_to_string(arg_operation));
eb267289 262
6edd7d0a 263 arguments[1] = (char*) "post";
aed98342 264 (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
6edd7d0a 265
19adb8a3
ZJS
266 return r;
267}
6edd7d0a 268
1afe3d71 269static int custom_timer_suspend(const SleepConfig *sleep_config) {
746cf898 270 _cleanup_hashmap_free_ Hashmap *last_capacity = NULL, *current_capacity = NULL;
c58493c0
ML
271 int r;
272
28ca9c24 273 assert(sleep_config);
c58493c0 274
96d662fa
SS
275 while (battery_is_low() == 0) {
276 _cleanup_close_ int tfd = -1;
277 struct itimerspec ts = {};
746cf898 278 usec_t suspend_interval = sleep_config->hibernate_delay_sec, before_timestamp = 0, after_timestamp = 0, total_suspend_interval;
96d662fa 279 bool woken_by_timer;
96d662fa
SS
280
281 tfd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK|TFD_CLOEXEC);
282 if (tfd < 0)
283 return log_error_errno(errno, "Error creating timerfd: %m");
284
285 /* Store current battery capacity and current time before suspension */
746cf898
SS
286 r = fetch_batteries_capacity_by_name(&last_capacity);
287 if (r >= 0)
96d662fa 288 before_timestamp = now(CLOCK_BOOTTIME);
746cf898 289 else if (r == -ENOENT)
96d662fa
SS
290 /* In case of no battery, system suspend interval will be set to HibernateDelaySec=. */
291 log_debug_errno(r, "Suspend Interval value set to %s: %m", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
292 else
293 return log_error_errno(r, "Error fetching battery capacity percentage: %m");
294
746cf898 295 r = get_total_suspend_interval(last_capacity, &total_suspend_interval);
91ea7ebc 296 if (r < 0)
746cf898
SS
297 log_debug_errno(r, "Failed to estimate suspend interval using previous discharge rate, ignoring: %m");
298 else
299 suspend_interval = total_suspend_interval;
300
96d662fa
SS
301 log_debug("Set timerfd wake alarm for %s", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
302 /* Wake alarm for system with or without battery to hibernate or estimate discharge rate whichever is applicable */
303 timespec_store(&ts.it_value, suspend_interval);
304
305 if (timerfd_settime(tfd, 0, &ts, NULL) < 0)
306 return log_error_errno(errno, "Error setting battery estimate timer: %m");
307
308 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
309 if (r < 0)
310 return r;
c58493c0 311
96d662fa
SS
312 r = fd_wait_for_event(tfd, POLLIN, 0);
313 if (r < 0)
314 return log_error_errno(r, "Error polling timerfd: %m");
315 /* Store fd_wait status */
316 woken_by_timer = FLAGS_SET(r, POLLIN);
317
746cf898
SS
318 r = fetch_batteries_capacity_by_name(&current_capacity);
319 if (r < 0) {
320 /* In case of no battery or error while getting charge level, no need to measure
321 * discharge rate. Instead system should wakeup if it is manual wakeup or
322 * hibernate if this is a timer wakeup. */
96d662fa 323 log_debug_errno(r, "Battery capacity percentage unavailable, cannot estimate discharge rate: %m");
746cf898
SS
324 if (!woken_by_timer)
325 return 0;
96d662fa 326 break;
746cf898 327 }
96d662fa 328
746cf898
SS
329 after_timestamp = now(CLOCK_BOOTTIME);
330 log_debug("Attempting to estimate battery discharge rate after wakeup from %s sleep", FORMAT_TIMESPAN(after_timestamp - before_timestamp, USEC_PER_HOUR));
96d662fa 331
746cf898
SS
332 if (after_timestamp != before_timestamp) {
333 r = estimate_battery_discharge_rate_per_hour(last_capacity, current_capacity, before_timestamp, after_timestamp);
91ea7ebc 334 if (r < 0)
746cf898
SS
335 log_warning_errno(r, "Failed to estimate and update battery discharge rate, ignoring: %m");
336 } else
337 log_debug("System woke up too early to estimate discharge rate");
c58493c0 338
96d662fa
SS
339 if (!woken_by_timer)
340 /* Return as manual wakeup done. This also will return in case battery was charged during suspension */
341 return 0;
1afe3d71
SS
342
343 r = check_wakeup_type();
344 if (r < 0)
345 log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m");
346 if (r > 0) {
347 log_debug("wakeup type is APM timer");
348 /* system should hibernate */
349 break;
350 }
351 }
352
353 return 1;
354}
355
f13f91f0
SS
356/* Freeze when invoked and thaw on cleanup */
357static int freeze_thaw_user_slice(const char **method) {
358 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
359 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
360 int r;
361
362 if (!method || !*method)
363 return 0;
364
365 r = bus_connect_system_systemd(&bus);
366 if (r < 0)
367 return log_debug_errno(r, "Failed to open connection to systemd: %m");
368
369 r = bus_call_method(bus, bus_systemd_mgr, *method, &error, NULL, "s", SPECIAL_USER_SLICE);
370 if (r < 0)
371 return log_debug_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r));
372
373 return 1;
374}
375
1afe3d71 376static int execute_s2h(const SleepConfig *sleep_config) {
f13f91f0 377 _unused_ _cleanup_(freeze_thaw_user_slice) const char *auto_method_thaw = NULL;
1afe3d71
SS
378 int r, k;
379
380 assert(sleep_config);
381
f13f91f0
SS
382 r = freeze_thaw_user_slice(&(const char*) { "FreezeUnit" });
383 if (r < 0)
384 log_debug_errno(r, "Failed to freeze unit user.slice, ignoring: %m");
385 else
386 auto_method_thaw = "ThawUnit"; /* from now on we want automatic thawing */;
387
1afe3d71
SS
388 r = check_wakeup_type();
389 if (r < 0)
390 log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m");
391
392 k = battery_trip_point_alarm_exists();
393 if (k < 0)
394 log_debug_errno(k, "Failed to check whether acpi_btp support is enabled or not, ignoring: %m");
395
396 if (r >= 0 && k > 0) {
397 log_debug("Attempting to suspend...");
398 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
399 if (r < 0)
400 return r;
401
402 r = check_wakeup_type();
403 if (r < 0)
404 return log_debug_errno(r, "Failed to check hardware wakeup type: %m");
405
406 if (r == 0)
407 /* For APM Timer wakeup, system should hibernate else wakeup */
408 return 0;
409 } else {
410 r = custom_timer_suspend(sleep_config);
411 if(r < 0)
412 return log_debug_errno(r, "Suspend cycle with manual battery discharge rate estimation failed: %m");
413 if(r == 0)
414 /* manual wakeup */
415 return 0;
96d662fa 416 }
1afe3d71 417 /* For above custom timer, if 1 is returned, system will directly hibernate */
28ca9c24 418
96d662fa 419 log_debug("Attempting to hibernate");
c8cd8ca3 420 r = execute(sleep_config, SLEEP_HIBERNATE, NULL);
f05e1ae6 421 if (r < 0) {
b0c035e3 422 log_notice("Couldn't hibernate, will try to suspend again.");
0f2d351f 423
c8cd8ca3 424 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hibernate");
0f2d351f 425 if (r < 0)
b0c035e3 426 return r;
f05e1ae6
LP
427 }
428
429 return 0;
c58493c0
ML
430}
431
37ec0fdd
LP
432static int help(void) {
433 _cleanup_free_ char *link = NULL;
434 int r;
435
436 r = terminal_urlify_man("systemd-suspend.service", "8", &link);
437 if (r < 0)
438 return log_oom();
439
19adb8a3
ZJS
440 printf("%s COMMAND\n\n"
441 "Suspend the system, hibernate the system, or both.\n\n"
37ec0fdd
LP
442 " -h --help Show this help and exit\n"
443 " --version Print version string and exit\n"
444 "\nCommands:\n"
445 " suspend Suspend the system\n"
446 " hibernate Hibernate the system\n"
447 " hybrid-sleep Both hibernate and suspend the system\n"
e68c79db 448 " suspend-then-hibernate Initially suspend and then hibernate\n"
37ec0fdd 449 " the system after a fixed period of time\n"
bc556335
DDM
450 "\nSee the %s for details.\n",
451 program_invocation_short_name,
452 link);
37ec0fdd
LP
453
454 return 0;
19adb8a3 455}
6edd7d0a 456
19adb8a3
ZJS
457static int parse_argv(int argc, char *argv[]) {
458 enum {
459 ARG_VERSION = 0x100,
460 };
461
462 static const struct option options[] = {
463 { "help", no_argument, NULL, 'h' },
464 { "version", no_argument, NULL, ARG_VERSION },
eb9da376 465 {}
19adb8a3
ZJS
466 };
467
468 int c;
469
470 assert(argc >= 0);
471 assert(argv);
472
601185b4 473 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
79893116 474 switch (c) {
19adb8a3 475 case 'h':
37ec0fdd 476 return help();
19adb8a3
ZJS
477
478 case ARG_VERSION:
3f6fd1ba 479 return version();
19adb8a3
ZJS
480
481 case '?':
482 return -EINVAL;
483
484 default:
04499a70 485 assert_not_reached();
19adb8a3
ZJS
486 }
487
baaa35ad
ZJS
488 if (argc - optind != 1)
489 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
490 "Usage: %s COMMAND",
491 program_invocation_short_name);
19adb8a3 492
c8cd8ca3
LP
493 arg_operation = sleep_operation_from_string(argv[optind]);
494 if (arg_operation < 0)
495 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'.", argv[optind]);
19adb8a3
ZJS
496
497 return 1 /* work to do */;
498}
499
7caefb81 500static int run(int argc, char *argv[]) {
28ca9c24 501 _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
19adb8a3
ZJS
502 int r;
503
d2acb93d 504 log_setup();
19adb8a3
ZJS
505
506 r = parse_argv(argc, argv);
507 if (r <= 0)
7caefb81 508 return r;
19adb8a3 509
28ca9c24
ZS
510 r = parse_sleep_config(&sleep_config);
511 if (r < 0)
512 return r;
513
c8cd8ca3 514 if (!sleep_config->allow[arg_operation])
baaa35ad 515 return log_error_errno(SYNTHETIC_ERRNO(EACCES),
c8cd8ca3
LP
516 "Sleep operation \"%s\" is disabled by configuration, refusing.",
517 sleep_operation_to_string(arg_operation));
e8f1d00d 518
3d132111
LP
519 switch (arg_operation) {
520
521 case SLEEP_SUSPEND_THEN_HIBERNATE:
522 r = execute_s2h(sleep_config);
523 break;
524
525 case SLEEP_HYBRID_SLEEP:
526 r = execute(sleep_config, SLEEP_HYBRID_SLEEP, NULL);
527 if (r < 0) {
528 /* If we can't hybrid sleep, then let's try to suspend at least. After all, the user
529 * asked us to do both: suspend + hibernate, and it's almost certainly the
530 * hibernation that failed, hence still do the other thing, the suspend. */
531
532 log_notice("Couldn't hybrid sleep, will try to suspend instead.");
533
534 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hybrid-sleep");
535 }
536
537 break;
538
539 default:
540 r = execute(sleep_config, arg_operation, NULL);
541 break;
542 }
543
544 return r;
6edd7d0a 545}
7caefb81
ZJS
546
547DEFINE_MAIN_FUNCTION(run);