]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sleep/sleep.c
sleep: fetch_batteries_capacity_by_name() does not return -ENOENT
[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"
d6b4d1c7 21#include "build.h"
ba0fb5ac 22#include "bus-error.h"
f13f91f0
SS
23#include "bus-locator.h"
24#include "bus-util.h"
28db6fbf 25#include "constants.h"
89711996 26#include "exec-util.h"
3ffd4af2 27#include "fd-util.h"
a5c32cff 28#include "fileio.h"
ba0fb5ac 29#include "format-util.h"
0f2d351f 30#include "io-util.h"
3f6fd1ba 31#include "log.h"
5e332028 32#include "main-func.h"
ed698d30 33#include "parse-util.h"
294bf0c3 34#include "pretty-print.h"
19adb8a3 35#include "sleep-config.h"
f13f91f0 36#include "special.h"
c58493c0 37#include "stdio-util.h"
07630cea 38#include "string-util.h"
3f6fd1ba 39#include "strv.h"
1bbbefe7 40#include "time-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) {
c58493c0
ML
270 int r;
271
28ca9c24 272 assert(sleep_config);
c58493c0 273
96d662fa 274 while (battery_is_low() == 0) {
de5d8b40 275 _cleanup_hashmap_free_ Hashmap *last_capacity = NULL, *current_capacity = NULL;
254d1313 276 _cleanup_close_ int tfd = -EBADF;
96d662fa 277 struct itimerspec ts = {};
d812e104 278 usec_t suspend_interval = sleep_config->hibernate_delay_usec, 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
d812e104 285 /* Store current battery capacity before suspension */
746cf898 286 r = fetch_batteries_capacity_by_name(&last_capacity);
d812e104 287 if (r < 0)
96d662fa
SS
288 return log_error_errno(r, "Error fetching battery capacity percentage: %m");
289
746cf898 290 r = get_total_suspend_interval(last_capacity, &total_suspend_interval);
91ea7ebc 291 if (r < 0)
746cf898
SS
292 log_debug_errno(r, "Failed to estimate suspend interval using previous discharge rate, ignoring: %m");
293 else
294 suspend_interval = total_suspend_interval;
295
d812e104
YW
296 usec_t before_timestamp = now(CLOCK_BOOTTIME);
297
96d662fa
SS
298 log_debug("Set timerfd wake alarm for %s", FORMAT_TIMESPAN(suspend_interval, USEC_PER_SEC));
299 /* Wake alarm for system with or without battery to hibernate or estimate discharge rate whichever is applicable */
300 timespec_store(&ts.it_value, suspend_interval);
301
302 if (timerfd_settime(tfd, 0, &ts, NULL) < 0)
303 return log_error_errno(errno, "Error setting battery estimate timer: %m");
304
305 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
306 if (r < 0)
307 return r;
c58493c0 308
96d662fa
SS
309 r = fd_wait_for_event(tfd, POLLIN, 0);
310 if (r < 0)
311 return log_error_errno(r, "Error polling timerfd: %m");
312 /* Store fd_wait status */
313 woken_by_timer = FLAGS_SET(r, POLLIN);
314
746cf898 315 r = fetch_batteries_capacity_by_name(&current_capacity);
d812e104 316 if (r < 0 || hashmap_isempty(current_capacity)) {
746cf898 317 /* In case of no battery or error while getting charge level, no need to measure
d812e104
YW
318 * discharge rate. Instead the system should wake up if it is manual wakeup or
319 * hibernate if this is a timer wakeup. */
320 if (r < 0)
321 log_debug_errno(r, "Battery capacity percentage unavailable, cannot estimate discharge rate: %m");
322 else
323 log_debug("No battery found.");
746cf898
SS
324 if (!woken_by_timer)
325 return 0;
96d662fa 326 break;
746cf898 327 }
96d662fa 328
d812e104 329 usec_t after_timestamp = now(CLOCK_BOOTTIME);
099810a6
ZJS
330 log_debug("Attempting to estimate battery discharge rate after wakeup from %s sleep",
331 FORMAT_TIMESPAN(after_timestamp - before_timestamp, USEC_PER_HOUR));
96d662fa 332
746cf898
SS
333 if (after_timestamp != before_timestamp) {
334 r = estimate_battery_discharge_rate_per_hour(last_capacity, current_capacity, before_timestamp, after_timestamp);
91ea7ebc 335 if (r < 0)
746cf898
SS
336 log_warning_errno(r, "Failed to estimate and update battery discharge rate, ignoring: %m");
337 } else
338 log_debug("System woke up too early to estimate discharge rate");
c58493c0 339
96d662fa
SS
340 if (!woken_by_timer)
341 /* Return as manual wakeup done. This also will return in case battery was charged during suspension */
342 return 0;
1afe3d71
SS
343
344 r = check_wakeup_type();
345 if (r < 0)
346 log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m");
347 if (r > 0) {
348 log_debug("wakeup type is APM timer");
349 /* system should hibernate */
350 break;
351 }
352 }
353
354 return 1;
355}
356
f13f91f0
SS
357/* Freeze when invoked and thaw on cleanup */
358static int freeze_thaw_user_slice(const char **method) {
359 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
360 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
361 int r;
362
363 if (!method || !*method)
364 return 0;
365
366 r = bus_connect_system_systemd(&bus);
367 if (r < 0)
368 return log_debug_errno(r, "Failed to open connection to systemd: %m");
369
432a3211 370 /* Wait for 1.5 seconds at maximum for freeze operation */
371 (void) sd_bus_set_method_call_timeout(bus, 1500 * USEC_PER_MSEC);
372
f13f91f0
SS
373 r = bus_call_method(bus, bus_systemd_mgr, *method, &error, NULL, "s", SPECIAL_USER_SLICE);
374 if (r < 0)
375 return log_debug_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r));
376
377 return 1;
378}
379
1afe3d71 380static int execute_s2h(const SleepConfig *sleep_config) {
efa736d3 381 _unused_ _cleanup_(freeze_thaw_user_slice) const char *auto_method_thaw = "ThawUnit";
1afe3d71
SS
382 int r, k;
383
384 assert(sleep_config);
385
f13f91f0
SS
386 r = freeze_thaw_user_slice(&(const char*) { "FreezeUnit" });
387 if (r < 0)
388 log_debug_errno(r, "Failed to freeze unit user.slice, ignoring: %m");
f13f91f0 389
1afe3d71
SS
390 r = check_wakeup_type();
391 if (r < 0)
392 log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m");
393
394 k = battery_trip_point_alarm_exists();
395 if (k < 0)
396 log_debug_errno(k, "Failed to check whether acpi_btp support is enabled or not, ignoring: %m");
397
398 if (r >= 0 && k > 0) {
399 log_debug("Attempting to suspend...");
400 r = execute(sleep_config, SLEEP_SUSPEND, NULL);
401 if (r < 0)
402 return r;
403
404 r = check_wakeup_type();
405 if (r < 0)
406 return log_debug_errno(r, "Failed to check hardware wakeup type: %m");
407
408 if (r == 0)
409 /* For APM Timer wakeup, system should hibernate else wakeup */
410 return 0;
411 } else {
412 r = custom_timer_suspend(sleep_config);
413 if(r < 0)
414 return log_debug_errno(r, "Suspend cycle with manual battery discharge rate estimation failed: %m");
415 if(r == 0)
416 /* manual wakeup */
417 return 0;
96d662fa 418 }
1afe3d71 419 /* For above custom timer, if 1 is returned, system will directly hibernate */
28ca9c24 420
96d662fa 421 log_debug("Attempting to hibernate");
c8cd8ca3 422 r = execute(sleep_config, SLEEP_HIBERNATE, NULL);
f05e1ae6 423 if (r < 0) {
b0c035e3 424 log_notice("Couldn't hibernate, will try to suspend again.");
0f2d351f 425
c8cd8ca3 426 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hibernate");
0f2d351f 427 if (r < 0)
b0c035e3 428 return r;
f05e1ae6
LP
429 }
430
431 return 0;
c58493c0
ML
432}
433
37ec0fdd
LP
434static int help(void) {
435 _cleanup_free_ char *link = NULL;
436 int r;
437
438 r = terminal_urlify_man("systemd-suspend.service", "8", &link);
439 if (r < 0)
440 return log_oom();
441
19adb8a3
ZJS
442 printf("%s COMMAND\n\n"
443 "Suspend the system, hibernate the system, or both.\n\n"
37ec0fdd
LP
444 " -h --help Show this help and exit\n"
445 " --version Print version string and exit\n"
446 "\nCommands:\n"
447 " suspend Suspend the system\n"
448 " hibernate Hibernate the system\n"
449 " hybrid-sleep Both hibernate and suspend the system\n"
e68c79db 450 " suspend-then-hibernate Initially suspend and then hibernate\n"
37ec0fdd 451 " the system after a fixed period of time\n"
bc556335
DDM
452 "\nSee the %s for details.\n",
453 program_invocation_short_name,
454 link);
37ec0fdd
LP
455
456 return 0;
19adb8a3 457}
6edd7d0a 458
19adb8a3
ZJS
459static int parse_argv(int argc, char *argv[]) {
460 enum {
461 ARG_VERSION = 0x100,
462 };
463
464 static const struct option options[] = {
465 { "help", no_argument, NULL, 'h' },
466 { "version", no_argument, NULL, ARG_VERSION },
eb9da376 467 {}
19adb8a3
ZJS
468 };
469
470 int c;
471
472 assert(argc >= 0);
473 assert(argv);
474
601185b4 475 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
79893116 476 switch (c) {
19adb8a3 477 case 'h':
37ec0fdd 478 return help();
19adb8a3
ZJS
479
480 case ARG_VERSION:
3f6fd1ba 481 return version();
19adb8a3
ZJS
482
483 case '?':
484 return -EINVAL;
485
486 default:
04499a70 487 assert_not_reached();
19adb8a3
ZJS
488 }
489
baaa35ad
ZJS
490 if (argc - optind != 1)
491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
492 "Usage: %s COMMAND",
493 program_invocation_short_name);
19adb8a3 494
c8cd8ca3
LP
495 arg_operation = sleep_operation_from_string(argv[optind]);
496 if (arg_operation < 0)
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'.", argv[optind]);
19adb8a3
ZJS
498
499 return 1 /* work to do */;
500}
501
7caefb81 502static int run(int argc, char *argv[]) {
28ca9c24 503 _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
19adb8a3
ZJS
504 int r;
505
d2acb93d 506 log_setup();
19adb8a3
ZJS
507
508 r = parse_argv(argc, argv);
509 if (r <= 0)
7caefb81 510 return r;
19adb8a3 511
28ca9c24
ZS
512 r = parse_sleep_config(&sleep_config);
513 if (r < 0)
514 return r;
515
c8cd8ca3 516 if (!sleep_config->allow[arg_operation])
baaa35ad 517 return log_error_errno(SYNTHETIC_ERRNO(EACCES),
c8cd8ca3
LP
518 "Sleep operation \"%s\" is disabled by configuration, refusing.",
519 sleep_operation_to_string(arg_operation));
e8f1d00d 520
3d132111
LP
521 switch (arg_operation) {
522
523 case SLEEP_SUSPEND_THEN_HIBERNATE:
524 r = execute_s2h(sleep_config);
525 break;
526
527 case SLEEP_HYBRID_SLEEP:
528 r = execute(sleep_config, SLEEP_HYBRID_SLEEP, NULL);
529 if (r < 0) {
530 /* If we can't hybrid sleep, then let's try to suspend at least. After all, the user
531 * asked us to do both: suspend + hibernate, and it's almost certainly the
532 * hibernation that failed, hence still do the other thing, the suspend. */
533
534 log_notice("Couldn't hybrid sleep, will try to suspend instead.");
535
536 r = execute(sleep_config, SLEEP_SUSPEND, "suspend-after-failed-hybrid-sleep");
537 }
538
539 break;
540
541 default:
542 r = execute(sleep_config, arg_operation, NULL);
543 break;
544 }
545
546 return r;
6edd7d0a 547}
7caefb81
ZJS
548
549DEFINE_MAIN_FUNCTION(run);