***/
#include <errno.h>
+#include <fcntl.h>
#include <linux/fs.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
-#include <sys/utsname.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
-#include "sd-id128.h"
-
#include "alloc-util.h"
#include "conf-parser.h"
#include "def.h"
#include "env-util.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "sleep-config.h"
#include "string-util.h"
#include "strv.h"
+#include "time-util.h"
-int parse_sleep_config(const char *verb, bool *ret_allow, char ***ret_modes, char ***ret_states, usec_t *ret_delay) {
+int parse_sleep_config(SleepConfig **ret_sleep_config) {
+ _cleanup_(free_sleep_configp) SleepConfig *sc;
int allow_suspend = -1, allow_hibernate = -1,
allow_s2h = -1, allow_hybrid_sleep = -1;
- bool allow;
- _cleanup_strv_free_ char
- **suspend_mode = NULL, **suspend_state = NULL,
- **hibernate_mode = NULL, **hibernate_state = NULL,
- **hybrid_mode = NULL, **hybrid_state = NULL;
- _cleanup_strv_free_ char **modes, **states; /* always initialized below */
- usec_t delay = 180 * USEC_PER_MINUTE;
+
+ sc = new0(SleepConfig, 1);
+ if (!sc)
+ return log_oom();
const ConfigTableItem items[] = {
{ "Sleep", "AllowSuspend", config_parse_tristate, 0, &allow_suspend },
{ "Sleep", "AllowSuspendThenHibernate", config_parse_tristate, 0, &allow_s2h },
{ "Sleep", "AllowHybridSleep", config_parse_tristate, 0, &allow_hybrid_sleep },
- { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
- { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
- { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
- { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
- { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
- { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
+ { "Sleep", "SuspendMode", config_parse_strv, 0, &sc->suspend_modes },
+ { "Sleep", "SuspendState", config_parse_strv, 0, &sc->suspend_states },
+ { "Sleep", "HibernateMode", config_parse_strv, 0, &sc->hibernate_modes },
+ { "Sleep", "HibernateState", config_parse_strv, 0, &sc->hibernate_states },
+ { "Sleep", "HybridSleepMode", config_parse_strv, 0, &sc->hybrid_modes },
+ { "Sleep", "HybridSleepState", config_parse_strv, 0, &sc->hybrid_states },
- { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &delay},
+ { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &sc->hibernate_delay_sec},
{}
};
"Sleep\0", config_item_table_lookup, items,
CONFIG_PARSE_WARN, NULL);
- if (streq(verb, "suspend")) {
- allow = allow_suspend != 0;
-
- /* empty by default */
- modes = TAKE_PTR(suspend_mode);
-
- if (suspend_state)
- states = TAKE_PTR(suspend_state);
- else
- states = strv_new("mem", "standby", "freeze");
-
- } else if (streq(verb, "hibernate")) {
- allow = allow_hibernate != 0;
-
- if (hibernate_mode)
- modes = TAKE_PTR(hibernate_mode);
- else
- modes = strv_new("platform", "shutdown");
-
- if (hibernate_state)
- states = TAKE_PTR(hibernate_state);
- else
- states = strv_new("disk");
-
- } else if (streq(verb, "hybrid-sleep")) {
- allow = allow_hybrid_sleep > 0 ||
- (allow_suspend != 0 && allow_hibernate != 0);
-
- if (hybrid_mode)
- modes = TAKE_PTR(hybrid_mode);
- else
- modes = strv_new("suspend", "platform", "shutdown");
-
- if (hybrid_state)
- states = TAKE_PTR(hybrid_state);
- else
- states = strv_new("disk");
-
- } else if (streq(verb, "suspend-then-hibernate")) {
- allow = allow_s2h > 0 ||
- (allow_suspend != 0 && allow_hibernate != 0);
-
- modes = states = NULL;
- } else
- assert_not_reached("what verb");
-
- if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) ||
- (!states && !streq(verb, "suspend-then-hibernate")))
+ /* use default values unless set */
+ sc->allow_suspend = allow_suspend != 0;
+ sc->allow_hibernate = allow_hibernate != 0;
+ sc->allow_hybrid_sleep = allow_hybrid_sleep >= 0 ? allow_hybrid_sleep
+ : (allow_suspend != 0 && allow_hibernate != 0);
+ sc->allow_s2h = allow_s2h >= 0 ? allow_s2h
+ : (allow_suspend != 0 && allow_hibernate != 0);
+
+ if (!sc->suspend_states)
+ sc->suspend_states = strv_new("mem", "standby", "freeze");
+ if (!sc->hibernate_modes)
+ sc->hibernate_modes = strv_new("platform", "shutdown");
+ if (!sc->hibernate_states)
+ sc->hibernate_states = strv_new("disk");
+ if (!sc->hybrid_modes)
+ sc->hybrid_modes = strv_new("suspend", "platform", "shutdown");
+ if (!sc->hybrid_states)
+ sc->hybrid_states = strv_new("disk");
+ if (sc->hibernate_delay_sec == 0)
+ sc->hibernate_delay_sec = 2 * USEC_PER_HOUR;
+
+ /* ensure values set for all required fields */
+ if (!sc->suspend_states || !sc->hibernate_modes
+ || !sc->hibernate_states || !sc->hybrid_modes || !sc->hybrid_states)
return log_oom();
- if (ret_allow)
- *ret_allow = allow;
- if (ret_modes)
- *ret_modes = TAKE_PTR(modes);
- if (ret_states)
- *ret_states = TAKE_PTR(states);
- if (ret_delay)
- *ret_delay = delay;
+ *ret_sleep_config = TAKE_PTR(sc);
return 0;
}
if (!f) {
log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
"Failed to retrieve open /proc/swaps: %m");
- assert(errno > 0);
- return -errno;
+ return negative_errno();
}
(void) fscanf(f, "%*s %*s %*s %*s %*s\n");
+ // TODO: sort swaps in priority order rather than using first successful option
for (i = 1;; i++) {
_cleanup_free_ char *dev_field = NULL, *type_field = NULL;
size_t size_field, used_field;
return r;
}
-static int kernel_exists(void) {
- struct utsname u;
- sd_id128_t m;
- int i, r;
-
- /* Do some superficial checks whether the kernel we are currently running is still around. If it isn't we
- * shouldn't offer hibernation as we couldn't possible resume from hibernation again. Of course, this check is
- * very superficial, as the kernel's mere existance is hardly enough to know whether the hibernate/resume cycle
- * will succeed. However, the common case of kernel updates can be caught this way, and it's definitely worth
- * covering that. */
-
- for (i = 0;; i++) {
- _cleanup_free_ char *path = NULL;
-
- switch (i) {
-
- case 0:
- /* First, let's look in /lib/modules/`uname -r`/vmlinuz. This is where current Fedora places
- * its RPM-managed kernels. It's a good place, as it means compiled vendor code is monopolized
- * in /usr, and then the kernel image is stored along with its modules in the same
- * hierarchy. It's also what our 'kernel-install' script is written for. */
- if (uname(&u) < 0)
- return log_debug_errno(errno, "Failed to acquire kernel release: %m");
-
- path = strjoin("/lib/modules/", u.release, "/vmlinuz");
- break;
-
- case 1:
- /* Secondly, let's look in /boot/vmlinuz-`uname -r`. This is where older Fedora and other
- * distributions tend to place the kernel. */
- path = strjoin("/boot/vmlinuz-", u.release);
- break;
-
- case 2:
- /* For the other cases, we look in the EFI/boot partition, at the place where our
- * "kernel-install" script copies the kernel on install by default. */
- r = sd_id128_get_machine(&m);
- if (r < 0)
- return log_debug_errno(r, "Failed to read machine ID: %m");
-
- (void) asprintf(&path, "/efi/" SD_ID128_FORMAT_STR "/%s/linux", SD_ID128_FORMAT_VAL(m), u.release);
- break;
- case 3:
- (void) asprintf(&path, "/boot/" SD_ID128_FORMAT_STR "/%s/linux", SD_ID128_FORMAT_VAL(m), u.release);
- break;
- case 4:
- (void) asprintf(&path, "/boot/efi/" SD_ID128_FORMAT_STR "/%s/linux", SD_ID128_FORMAT_VAL(m), u.release);
- break;
-
- default:
- return false;
- }
-
- if (!path)
- return -ENOMEM;
-
- log_debug("Testing whether %s exists.", path);
-
- if (access(path, F_OK) >= 0)
- return true;
-
- if (errno != ENOENT)
- log_debug_errno(errno, "Failed to determine whether '%s' exists, ignoring: %m", path);
- }
-}
-
int read_fiemap(int fd, struct fiemap **ret) {
_cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
struct stat statinfo;
return 0;
}
-static int can_sleep_internal(const char *verb, bool check_allowed);
+static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config);
-static bool can_s2h(void) {
+static bool can_s2h(const SleepConfig *sleep_config) {
const char *p;
int r;
- r = access("/sys/class/rtc/rtc0/wakealarm", W_OK);
- if (r < 0) {
+ if (!clock_supported(CLOCK_BOOTTIME_ALARM)) {
log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
- "/sys/class/rct/rct0/wakealarm is not writable %m");
+ "CLOCK_BOOTTIME_ALARM is not supported");
return false;
}
FOREACH_STRING(p, "suspend", "hibernate") {
- r = can_sleep_internal(p, false);
- if (IN_SET(r, 0, -ENOSPC, -ENOMEDIUM, -EADV)) {
+ r = can_sleep_internal(p, false, sleep_config);
+ if (IN_SET(r, 0, -ENOSPC, -EADV)) {
log_debug("Unable to %s system.", p);
return false;
}
return true;
}
-static int can_sleep_internal(const char *verb, bool check_allowed) {
+static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config) {
bool allow;
- _cleanup_strv_free_ char **modes = NULL, **states = NULL;
+ char **modes = NULL, **states = NULL;
int r;
assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
- r = parse_sleep_config(verb, &allow, &modes, &states, NULL);
+ r = sleep_settings(verb, sleep_config, &allow, &modes, &states);
if (r < 0)
return false;
}
if (streq(verb, "suspend-then-hibernate"))
- return can_s2h();
+ return can_s2h(sleep_config);
if (!can_sleep_state(states) || !can_sleep_disk(modes))
return false;
if (streq(verb, "suspend"))
return true;
- if (kernel_exists() <= 0) {
- log_debug_errno(errno, "Couldn't find kernel, not offering hibernation.");
- return -ENOMEDIUM;
- }
-
if (!enough_swap_for_hibernation())
return -ENOSPC;
}
int can_sleep(const char *verb) {
- return can_sleep_internal(verb, true);
+ _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
+ int r;
+
+ r = parse_sleep_config(&sleep_config);
+ if (r < 0)
+ return r;
+
+ return can_sleep_internal(verb, true, sleep_config);
+}
+
+int sleep_settings(const char *verb, const SleepConfig *sleep_config, bool *ret_allow, char ***ret_modes, char ***ret_states) {
+
+ assert(verb);
+ assert(sleep_config);
+ assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
+
+ if (streq(verb, "suspend")) {
+ *ret_allow = sleep_config->allow_suspend;
+ *ret_modes = sleep_config->suspend_modes;
+ *ret_states = sleep_config->suspend_states;
+ } else if (streq(verb, "hibernate")) {
+ *ret_allow = sleep_config->allow_hibernate;
+ *ret_modes = sleep_config->hibernate_modes;
+ *ret_states = sleep_config->hibernate_states;
+ } else if (streq(verb, "hybrid-sleep")) {
+ *ret_allow = sleep_config->allow_hybrid_sleep;
+ *ret_modes = sleep_config->hybrid_modes;
+ *ret_states = sleep_config->hybrid_states;
+ } else if (streq(verb, "suspend-then-hibernate")) {
+ *ret_allow = sleep_config->allow_s2h;
+ *ret_modes = *ret_states = NULL;
+ }
+
+ /* suspend modes empty by default */
+ if ((!ret_modes && !streq(verb, "suspend")) || !ret_states)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No modes or states set for %s; Check sleep.conf", verb);
+
+ return 0;
+}
+
+void free_sleep_config(SleepConfig *sc) {
+ if (!sc)
+ return;
+
+ strv_free(sc->suspend_modes);
+ strv_free(sc->suspend_states);
+
+ strv_free(sc->hibernate_modes);
+ strv_free(sc->hibernate_states);
+
+ strv_free(sc->hybrid_modes);
+ strv_free(sc->hybrid_states);
+
+ free(sc);
}