]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sleep/sleep.c
16ad48b3862cd27d45779e996de75981eccbcd8b
[thirdparty/systemd.git] / src / sleep / sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2010-2017 Canonical
4 Copyright © 2018 Dell Inc.
5 ***/
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <getopt.h>
10 #include <poll.h>
11 #include <sys/timerfd.h>
12 #include <sys/types.h>
13 #include <sys/utsname.h>
14 #include <unistd.h>
15
16 #include "sd-bus.h"
17 #include "sd-device.h"
18 #include "sd-id128.h"
19 #include "sd-messages.h"
20
21 #include "battery-capacity.h"
22 #include "battery-util.h"
23 #include "build.h"
24 #include "bus-error.h"
25 #include "bus-locator.h"
26 #include "bus-unit-util.h"
27 #include "bus-util.h"
28 #include "constants.h"
29 #include "devnum-util.h"
30 #include "efivars.h"
31 #include "env-util.h"
32 #include "exec-util.h"
33 #include "fd-util.h"
34 #include "fileio.h"
35 #include "format-util.h"
36 #include "hibernate-util.h"
37 #include "id128-util.h"
38 #include "io-util.h"
39 #include "json.h"
40 #include "log.h"
41 #include "main-func.h"
42 #include "os-util.h"
43 #include "parse-util.h"
44 #include "pretty-print.h"
45 #include "sleep-config.h"
46 #include "special.h"
47 #include "stdio-util.h"
48 #include "string-util.h"
49 #include "strv.h"
50 #include "time-util.h"
51
52 #define DEFAULT_HIBERNATE_DELAY_USEC_NO_BATTERY (2 * USEC_PER_HOUR)
53
54 static SleepOperation arg_operation = _SLEEP_OPERATION_INVALID;
55
56 static int write_efi_hibernate_location(const HibernationDevice *hibernation_device, bool required) {
57 int log_level = required ? LOG_ERR : LOG_DEBUG;
58
59 #if ENABLE_EFI
60 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
61 _cleanup_free_ char *formatted = NULL, *id = NULL, *image_id = NULL,
62 *version_id = NULL, *image_version = NULL;
63 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
64 const char *uuid_str;
65 sd_id128_t uuid;
66 struct utsname uts = {};
67 int r, log_level_ignore = required ? LOG_WARNING : LOG_DEBUG;
68
69 assert(hibernation_device);
70
71 if (!is_efi_boot())
72 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
73 "Not an EFI boot, passing HibernateLocation via EFI variable is not possible.");
74
75 r = sd_device_new_from_devnum(&device, 'b', hibernation_device->devno);
76 if (r < 0)
77 return log_full_errno(log_level, r, "Failed to create sd-device object for '%s': %m",
78 hibernation_device->path);
79
80 r = sd_device_get_property_value(device, "ID_FS_UUID", &uuid_str);
81 if (r < 0)
82 return log_full_errno(log_level, r, "Failed to get filesystem UUID for device '%s': %m",
83 hibernation_device->path);
84
85 r = sd_id128_from_string(uuid_str, &uuid);
86 if (r < 0)
87 return log_full_errno(log_level, r, "Failed to parse ID_FS_UUID '%s' for device '%s': %m",
88 uuid_str, hibernation_device->path);
89
90 if (uname(&uts) < 0)
91 log_full_errno(log_level_ignore, errno, "Failed to get kernel info, ignoring: %m");
92
93 r = parse_os_release(NULL,
94 "ID", &id,
95 "IMAGE_ID", &image_id,
96 "VERSION_ID", &version_id,
97 "IMAGE_VERSION", &image_version);
98 if (r < 0)
99 log_full_errno(log_level_ignore, r, "Failed to parse os-release, ignoring: %m");
100
101 r = json_build(&v, JSON_BUILD_OBJECT(
102 JSON_BUILD_PAIR_UUID("uuid", uuid),
103 JSON_BUILD_PAIR_UNSIGNED("offset", hibernation_device->offset),
104 JSON_BUILD_PAIR_CONDITION(!isempty(uts.release), "kernelVersion", JSON_BUILD_STRING(uts.release)),
105 JSON_BUILD_PAIR_CONDITION(id, "osReleaseId", JSON_BUILD_STRING(id)),
106 JSON_BUILD_PAIR_CONDITION(image_id, "osReleaseImageId", JSON_BUILD_STRING(image_id)),
107 JSON_BUILD_PAIR_CONDITION(version_id, "osReleaseVersionId", JSON_BUILD_STRING(version_id)),
108 JSON_BUILD_PAIR_CONDITION(image_version, "osReleaseImageVersion", JSON_BUILD_STRING(image_version))));
109 if (r < 0)
110 return log_full_errno(log_level, r, "Failed to build JSON object: %m");
111
112 r = json_variant_format(v, 0, &formatted);
113 if (r < 0)
114 return log_full_errno(log_level, r, "Failed to format JSON object: %m");
115
116 r = efi_set_variable_string(EFI_SYSTEMD_VARIABLE(HibernateLocation), formatted);
117 if (r < 0)
118 return log_full_errno(log_level, r, "Failed to set EFI variable HibernateLocation: %m");
119
120 log_debug("Set EFI variable HibernateLocation to '%s'.", formatted);
121 return 0;
122 #else
123 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
124 "EFI support not enabled, passing HibernateLocation via EFI variable is not possible.");
125 #endif
126 }
127
128 static int write_state(int fd, char * const *states) {
129 int r = 0;
130
131 assert(fd >= 0);
132 assert(states);
133
134 STRV_FOREACH(state, states) {
135 _cleanup_fclose_ FILE *f = NULL;
136 int k;
137
138 k = fdopen_independent(fd, "we", &f);
139 if (k < 0)
140 return RET_GATHER(r, k);
141
142 k = write_string_stream(f, *state, WRITE_STRING_FILE_DISABLE_BUFFER);
143 if (k >= 0) {
144 log_debug("Using sleep state '%s'.", *state);
145 return 0;
146 }
147
148 RET_GATHER(r, log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state));
149 }
150
151 return r;
152 }
153
154 static int write_mode(char * const *modes) {
155 int r = 0;
156
157 STRV_FOREACH(mode, modes) {
158 int k;
159
160 k = write_string_file("/sys/power/disk", *mode, WRITE_STRING_FILE_DISABLE_BUFFER);
161 if (k >= 0) {
162 log_debug("Using sleep disk mode '%s'.", *mode);
163 return 0;
164 }
165
166 RET_GATHER(r, log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode));
167 }
168
169 return r;
170 }
171
172 static int lock_all_homes(void) {
173 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
174 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
175 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
176 int r;
177
178 /* Let's synchronously lock all home directories managed by homed that have been marked for it. This
179 * way the key material required to access these volumes is hopefully removed from memory. */
180
181 r = sd_bus_open_system(&bus);
182 if (r < 0)
183 return log_error_errno(r, "Failed to connect to system bus: %m");
184
185 r = bus_message_new_method_call(bus, &m, bus_home_mgr, "LockAllHomes");
186 if (r < 0)
187 return bus_log_create_error(r);
188
189 /* If homed is not running it can't have any home directories active either. */
190 r = sd_bus_message_set_auto_start(m, false);
191 if (r < 0)
192 return log_error_errno(r, "Failed to disable auto-start of LockAllHomes() message: %m");
193
194 r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC, &error, NULL);
195 if (r < 0) {
196 if (!bus_error_is_unknown_service(&error))
197 return log_error_errno(r, "Failed to lock home directories: %s", bus_error_message(&error, r));
198
199 log_debug("systemd-homed is not running, locking of home directories skipped.");
200 } else
201 log_debug("Successfully requested locking of all home directories.");
202 return 0;
203 }
204
205 static int execute(
206 const SleepConfig *sleep_config,
207 SleepOperation operation,
208 const char *action) {
209
210 const char *arguments[] = {
211 NULL,
212 "pre",
213 /* NB: we use 'arg_operation' instead of 'operation' here, as we want to communicate the overall
214 * operation here, not the specific one, in case of s2h. */
215 sleep_operation_to_string(arg_operation),
216 NULL
217 };
218 static const char* const dirs[] = {
219 SYSTEM_SLEEP_PATH,
220 NULL
221 };
222
223 _cleanup_close_ int state_fd = -EBADF;
224 int r;
225
226 assert(sleep_config);
227 assert(operation >= 0);
228 assert(operation < _SLEEP_OPERATION_CONFIG_MAX); /* Others are handled by execute_s2h() instead */
229
230 if (strv_isempty(sleep_config->states[operation]))
231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
232 "No sleep states configured for sleep operation %s, can't sleep.",
233 sleep_operation_to_string(operation));
234
235 /* This file is opened first, so that if we hit an error, we can abort before modifying any state. */
236 state_fd = open("/sys/power/state", O_WRONLY|O_CLOEXEC);
237 if (state_fd < 0)
238 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
239
240 /* Configure hibernation settings if we are supposed to hibernate */
241 if (sleep_operation_is_hibernation(operation)) {
242 _cleanup_(hibernation_device_done) HibernationDevice hibernation_device = {};
243 bool resume_set;
244
245 r = find_suitable_hibernation_device(&hibernation_device);
246 if (r < 0)
247 return log_error_errno(r, "Failed to find location to hibernate to: %m");
248 resume_set = r > 0;
249
250 r = write_efi_hibernate_location(&hibernation_device, !resume_set);
251 if (!resume_set) {
252 if (r == -EOPNOTSUPP)
253 return log_error_errno(r, "No valid 'resume=' option found, refusing to hibernate.");
254 if (r < 0)
255 return r;
256
257 r = write_resume_config(hibernation_device.devno, hibernation_device.offset, hibernation_device.path);
258 if (r < 0)
259 goto fail;
260 }
261
262 r = write_mode(sleep_config->modes[operation]);
263 if (r < 0) {
264 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
265 goto fail;
266 }
267 }
268
269 /* Pass an action string to the call-outs. This is mostly our operation string, except if the
270 * hibernate step of s-t-h fails, in which case we communicate that with a separate action. */
271 if (!action)
272 action = sleep_operation_to_string(operation);
273
274 if (setenv("SYSTEMD_SLEEP_ACTION", action, /* overwrite = */ 1) < 0)
275 log_warning_errno(errno, "Failed to set SYSTEMD_SLEEP_ACTION=%s, ignoring: %m", action);
276
277 (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, (char **) arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
278 (void) lock_all_homes();
279
280 log_struct(LOG_INFO,
281 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
282 LOG_MESSAGE("Performing sleep operation '%s'...", sleep_operation_to_string(operation)),
283 "SLEEP=%s", sleep_operation_to_string(arg_operation));
284
285 r = write_state(state_fd, sleep_config->states[operation]);
286 if (r < 0)
287 log_struct_errno(LOG_ERR, r,
288 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
289 LOG_MESSAGE("Failed to put system to sleep. System resumed again: %m"),
290 "SLEEP=%s", sleep_operation_to_string(arg_operation));
291 else
292 log_struct(LOG_INFO,
293 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
294 LOG_MESSAGE("System returned from sleep operation '%s'.", sleep_operation_to_string(arg_operation)),
295 "SLEEP=%s", sleep_operation_to_string(arg_operation));
296
297 arguments[1] = "post";
298 (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, (char **) arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
299
300 if (r >= 0)
301 return 0;
302
303 fail:
304 if (sleep_operation_is_hibernation(operation))
305 clear_efi_hibernate_location_and_warn();
306
307 return r;
308 }
309
310 /* Return true if wakeup type is APM timer */
311 static int check_wakeup_type(void) {
312 static const char dmi_object_path[] = "/sys/firmware/dmi/entries/1-0/raw";
313 uint8_t wakeup_type_byte, tablesize;
314 _cleanup_free_ char *buf = NULL;
315 size_t bufsize;
316 int r;
317
318 /* implementation via dmi/entries */
319 r = read_full_virtual_file(dmi_object_path, &buf, &bufsize);
320 if (r < 0)
321 return log_debug_errno(r, "Unable to read %s: %m", dmi_object_path);
322 if (bufsize < 25)
323 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
324 "Only read %zu bytes from %s (expected 25)",
325 bufsize, dmi_object_path);
326
327 /* index 1 stores the size of table */
328 tablesize = (uint8_t) buf[1];
329 if (tablesize < 25)
330 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
331 "Table size less than the index[0x18] where waketype byte is available.");
332
333 wakeup_type_byte = (uint8_t) buf[24];
334 /* 0 is Reserved and 8 is AC Power Restored. As per table 12 in
335 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.4.0.pdf */
336 if (wakeup_type_byte >= 128)
337 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Expected value in range 0-127");
338
339 if (wakeup_type_byte == 3) {
340 log_debug("DMI BIOS System Information indicates wakeup type is APM Timer");
341 return true;
342 }
343
344 return false;
345 }
346
347 static int custom_timer_suspend(const SleepConfig *sleep_config) {
348 usec_t hibernate_timestamp;
349 int r;
350
351 assert(sleep_config);
352
353 hibernate_timestamp = usec_add(now(CLOCK_BOOTTIME), sleep_config->hibernate_delay_usec);
354
355 while (battery_is_discharging_and_low() == 0) {
356 _cleanup_hashmap_free_ Hashmap *last_capacity = NULL, *current_capacity = NULL;
357 _cleanup_close_ int tfd = -EBADF;
358 struct itimerspec ts = {};
359 usec_t suspend_interval;
360 bool woken_by_timer;
361
362 tfd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK|TFD_CLOEXEC);
363 if (tfd < 0)
364 return log_error_errno(errno, "Error creating timerfd: %m");
365
366 /* Store current battery capacity before suspension */
367 r = fetch_batteries_capacity_by_name(&last_capacity);
368 if (r < 0)
369 return log_error_errno(r, "Error fetching battery capacity percentage: %m");
370
371 if (hashmap_isempty(last_capacity))
372 /* In case of no battery, system suspend interval will be set to HibernateDelaySec= or 2 hours. */
373 suspend_interval = timestamp_is_set(hibernate_timestamp)
374 ? sleep_config->hibernate_delay_usec : DEFAULT_HIBERNATE_DELAY_USEC_NO_BATTERY;
375 else {
376 r = get_total_suspend_interval(last_capacity, &suspend_interval);
377 if (r < 0) {
378 log_debug_errno(r, "Failed to estimate suspend interval using previous discharge rate, ignoring: %m");
379 /* In case of any errors, especially when we do not know the battery
380 * discharging rate, system suspend interval will be set to
381 * SuspendEstimationSec=. */
382 suspend_interval = sleep_config->suspend_estimation_usec;
383 }
384 }
385
386 /* Do not suspend more than HibernateDelaySec= */
387 usec_t before_timestamp = now(CLOCK_BOOTTIME);
388 suspend_interval = MIN(suspend_interval, usec_sub_unsigned(hibernate_timestamp, before_timestamp));
389 if (suspend_interval <= 0)
390 break; /* system should hibernate */
391
392 log_debug("Set timerfd wake alarm for %s", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
393 /* Wake alarm for system with or without battery to hibernate or estimate discharge rate whichever is applicable */
394 timespec_store(&ts.it_value, suspend_interval);
395
396 if (timerfd_settime(tfd, 0, &ts, NULL) < 0)
397 return log_error_errno(errno, "Error setting battery estimate timer: %m");
398
399 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
400 if (r < 0)
401 return r;
402
403 r = fd_wait_for_event(tfd, POLLIN, 0);
404 if (r < 0)
405 return log_error_errno(r, "Error polling timerfd: %m");
406 /* Store fd_wait status */
407 woken_by_timer = FLAGS_SET(r, POLLIN);
408
409 r = fetch_batteries_capacity_by_name(&current_capacity);
410 if (r < 0 || hashmap_isempty(current_capacity)) {
411 /* In case of no battery or error while getting charge level, no need to measure
412 * discharge rate. Instead the system should wake up if it is manual wakeup or
413 * hibernate if this is a timer wakeup. */
414 if (r < 0)
415 log_debug_errno(r, "Battery capacity percentage unavailable, cannot estimate discharge rate: %m");
416 else
417 log_debug("No battery found.");
418 if (!woken_by_timer)
419 return 0;
420 break;
421 }
422
423 usec_t after_timestamp = now(CLOCK_BOOTTIME);
424 log_debug("Attempting to estimate battery discharge rate after wakeup from %s sleep",
425 FORMAT_TIMESPAN(after_timestamp - before_timestamp, USEC_PER_HOUR));
426
427 if (after_timestamp != before_timestamp) {
428 r = estimate_battery_discharge_rate_per_hour(last_capacity, current_capacity, before_timestamp, after_timestamp);
429 if (r < 0)
430 log_warning_errno(r, "Failed to estimate and update battery discharge rate, ignoring: %m");
431 } else
432 log_debug("System woke up too early to estimate discharge rate.");
433
434 if (!woken_by_timer)
435 /* Return as manual wakeup done. This also will return in case battery was charged during suspension */
436 return 0;
437
438 r = check_wakeup_type();
439 if (r > 0) {
440 log_debug("wakeup type is APM timer");
441 /* system should hibernate */
442 break;
443 }
444 }
445
446 return 1;
447 }
448
449 static int execute_s2h(const SleepConfig *sleep_config) {
450 int r;
451
452 assert(sleep_config);
453
454 /* Only check if we have automated battery alarms if HibernateDelaySec= is not set, as in that case
455 * we'll busy poll for the configured interval instead */
456 if (!timestamp_is_set(sleep_config->hibernate_delay_usec)) {
457 r = check_wakeup_type();
458 if (r < 0)
459 log_warning_errno(r, "Failed to check hardware wakeup type, ignoring: %m");
460 else {
461 r = battery_trip_point_alarm_exists();
462 if (r < 0)
463 log_warning_errno(r, "Failed to check whether acpi_btp support is enabled or not, ignoring: %m");
464 }
465 } else
466 r = 0; /* Force fallback path */
467
468 if (r > 0) { /* If we have both wakeup alarms and battery trip point support, use them */
469 log_debug("Attempting to suspend...");
470 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
471 if (r < 0)
472 return r;
473
474 r = check_wakeup_type();
475 if (r < 0)
476 return log_error_errno(r, "Failed to check hardware wakeup type: %m");
477
478 if (r == 0)
479 /* For APM Timer wakeup, system should hibernate else wakeup */
480 return 0;
481 } else {
482 r = custom_timer_suspend(sleep_config);
483 if (r < 0)
484 return log_debug_errno(r, "Suspend cycle with manual battery discharge rate estimation failed: %m");
485 if (r == 0)
486 /* manual wakeup */
487 return 0;
488 }
489 /* For above custom timer, if 1 is returned, system will directly hibernate */
490
491 log_debug("Attempting to hibernate");
492 r = execute(sleep_config, SLEEP_HIBERNATE, NULL);
493 if (r < 0) {
494 log_notice("Couldn't hibernate, will try to suspend again.");
495
496 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hibernate");
497 if (r < 0)
498 return r;
499 }
500
501 return 0;
502 }
503
504 static int help(void) {
505 _cleanup_free_ char *link = NULL;
506 int r;
507
508 r = terminal_urlify_man("systemd-suspend.service", "8", &link);
509 if (r < 0)
510 return log_oom();
511
512 printf("%s COMMAND\n\n"
513 "Suspend the system, hibernate the system, or both.\n\n"
514 " -h --help Show this help and exit\n"
515 " --version Print version string and exit\n"
516 "\nCommands:\n"
517 " suspend Suspend the system\n"
518 " hibernate Hibernate the system\n"
519 " hybrid-sleep Both hibernate and suspend the system\n"
520 " suspend-then-hibernate Initially suspend and then hibernate\n"
521 " the system after a fixed period of time or\n"
522 " when battery is low\n"
523 "\nSee the %s for details.\n",
524 program_invocation_short_name,
525 link);
526
527 return 0;
528 }
529
530 static int parse_argv(int argc, char *argv[]) {
531
532 enum {
533 ARG_VERSION = 0x100,
534 };
535
536 static const struct option options[] = {
537 { "help", no_argument, NULL, 'h' },
538 { "version", no_argument, NULL, ARG_VERSION },
539 {}
540 };
541
542 int c;
543
544 assert(argc >= 0);
545 assert(argv);
546
547 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
548 switch (c) {
549
550 case 'h':
551 return help();
552
553 case ARG_VERSION:
554 return version();
555
556 case '?':
557 return -EINVAL;
558
559 default:
560 assert_not_reached();
561
562 }
563
564 if (argc - optind != 1)
565 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
566 "Usage: %s COMMAND",
567 program_invocation_short_name);
568
569 arg_operation = sleep_operation_from_string(argv[optind]);
570 if (arg_operation < 0)
571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'.", argv[optind]);
572
573 return 1 /* work to do */;
574 }
575
576 static int run(int argc, char *argv[]) {
577 _cleanup_(unit_freezer_done_thaw) UnitFreezer user_slice_freezer = {};
578 _cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
579 int r;
580
581 log_setup();
582
583 r = parse_argv(argc, argv);
584 if (r <= 0)
585 return r;
586
587 r = parse_sleep_config(&sleep_config);
588 if (r < 0)
589 return r;
590
591 if (!sleep_config->allow[arg_operation])
592 return log_error_errno(SYNTHETIC_ERRNO(EACCES),
593 "Sleep operation \"%s\" is disabled by configuration, refusing.",
594 sleep_operation_to_string(arg_operation));
595
596 /* Freeze the user sessions */
597 r = getenv_bool("SYSTEMD_SLEEP_FREEZE_USER_SESSIONS");
598 if (r < 0 && r != -ENXIO)
599 log_warning_errno(r, "Cannot parse value of $SYSTEMD_SLEEP_FREEZE_USER_SESSIONS, ignoring.");
600 if (r != 0) {
601 r = unit_freezer_new_freeze(SPECIAL_USER_SLICE, &user_slice_freezer);
602 if (r < 0)
603 log_warning_errno(r, "Failed to freeze user sessions, ignoring: %m");
604 else
605 log_info("Froze user sessions");
606 } else
607 log_notice("User sessions remain unfrozen on explicit request "
608 "($SYSTEMD_SLEEP_FREEZE_USER_SESSIONS is set to false). This is not recommended, "
609 "and might result in unexpected behavior, particularly in sysupend-then-hibernate "
610 "operations or setups with encrypted home directories.");
611
612 switch (arg_operation) {
613
614 case SLEEP_SUSPEND_THEN_HIBERNATE:
615 r = execute_s2h(sleep_config);
616 break;
617
618 case SLEEP_HYBRID_SLEEP:
619 r = execute(sleep_config, SLEEP_HYBRID_SLEEP, NULL);
620 if (r < 0) {
621 /* If we can't hybrid sleep, then let's try to suspend at least. After all, the user
622 * asked us to do both: suspend + hibernate, and it's almost certainly the
623 * hibernation that failed, hence still do the other thing, the suspend. */
624
625 log_notice_errno(r, "Couldn't hybrid sleep, will try to suspend instead: %m");
626
627 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hybrid-sleep");
628 }
629
630 break;
631
632 default:
633 r = execute(sleep_config, arg_operation, NULL);
634 break;
635
636 }
637
638 return r;
639 }
640
641 DEFINE_MAIN_FUNCTION(run);