From caa7531f137aeb260fbda53e89a22f516836e89b Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Sat, 29 Aug 2015 11:20:59 +0200 Subject: [PATCH] Incorporate sleep.conf into logind.conf * src/login/logind-action.c (shutdown_or_sleep, do_sleep): Take modes from the manager instead of parsing them ourselves. * src/login/logind-dbus.c (execute_shutdown_or_sleep): Adapt to shutdown_or_sleep prototype change. * src/login/logind-gperf.gperf: Add config items from sleep.conf. * src/login/logind.c (manager_new): Wire up defaults for new config items. (manager_free): Free new config items. (manager_parse_config_file): Arrange to parse a single elogind/logind.conf file, not grovelling all over the filesystem. Take the file from the ELOGIND_CONF_FILE environment variable if present. --- src/login/logind-action.c | 15 ++++-------- src/login/logind-dbus.c | 4 +-- src/login/logind-gperf.gperf | 6 +++++ src/login/logind.c | 47 ++++++++++++++++++++++++++++++++---- src/login/logind.conf | 8 ++++++ src/login/logind.h | 6 ++++- 6 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/login/logind-action.c b/src/login/logind-action.c index b0ff774df..d7b1538a3 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -223,8 +223,7 @@ static int write_state(FILE **f, char **states) { return r; } -static int do_sleep(const char *arg_verb) { - _cleanup_strv_free_ char **modes = NULL, **states = NULL; +static int do_sleep(const char *arg_verb, const char **modes, const char **states) { char *arguments[] = { NULL, (char*) "pre", @@ -235,10 +234,6 @@ static int do_sleep(const char *arg_verb) { int r; _cleanup_fclose_ FILE *f = NULL; - r = parse_sleep_config(arg_verb, &modes, &states); - if (r < 0) - return r; - /* This file is opened first, so that if we hit an error, * we can abort before modifying any state. */ f = fopen("/sys/power/state", "we"); @@ -274,7 +269,7 @@ static int do_sleep(const char *arg_verb) { return r; } -int shutdown_or_sleep(HandleAction action) { +int shutdown_or_sleep(Manager *m, HandleAction action) { switch (action) { case HANDLE_POWEROFF: return run_helper(HALT); @@ -285,11 +280,11 @@ int shutdown_or_sleep(HandleAction action) { case HANDLE_KEXEC: return run_helper(KEXEC); case HANDLE_SUSPEND: - return do_sleep("suspend"); + return do_sleep("suspend", m->suspend_mode, m->suspend_state); case HANDLE_HIBERNATE: - return do_sleep("hibernate"); + return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state); case HANDLE_HYBRID_SLEEP: - return do_sleep("hybrid-sleep"); + return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state); default: return -EINVAL; } diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 301c4de2f..859ad3b58 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1430,9 +1430,7 @@ static int execute_shutdown_or_sleep( bus_manager_log_shutdown(m, w, action); - /* FIXME: here do the thing. */ - - r = shutdown_or_sleep(action); + r = shutdown_or_sleep(m, action); if (r < 0) return r; diff --git a/src/login/logind-gperf.gperf b/src/login/logind-gperf.gperf index d9fa06c1f..9705dd5b1 100644 --- a/src/login/logind-gperf.gperf +++ b/src/login/logind-gperf.gperf @@ -32,3 +32,9 @@ Login.IdleAction, config_parse_handle_action, 0, offsetof(Manag Login.IdleActionSec, config_parse_sec, 0, offsetof(Manager, idle_action_usec) Login.RuntimeDirectorySize, config_parse_tmpfs_size, 0, offsetof(Manager, runtime_dir_size) Login.RemoveIPC, config_parse_bool, 0, offsetof(Manager, remove_ipc) +Sleep.SuspendMode, config_parse_strv, 0, offsetof(Manager, suspend_mode) +Sleep.SuspendState, config_parse_strv, 0, offsetof(Manager, suspend_state) +Sleep.HibernateMode, config_parse_strv, 0, offsetof(Manager, hibernate_mode) +Sleep.HibernateState, config_parse_strv, 0, offsetof(Manager, hibernate_state) +Sleep.HybridSleepMode, config_parse_strv, 0, offsetof(Manager, hybrid_sleep_mode) +Sleep.HybridSleepState, config_parse_strv, 0, offsetof(Manager, hybrid_sleep_state) diff --git a/src/login/logind.c b/src/login/logind.c index 5217e1a20..4cd320d8c 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -76,6 +76,23 @@ Manager *manager_new(void) { if (!m->kill_exclude_users) goto fail; + m->suspend_mode = NULL; + m->suspend_state = strv_new("mem", "standby", "freeze", NULL); + if (!m->suspend_state) + goto fail; + m->hibernate_mode = strv_new("platform", "shutdown", NULL); + if (!m->hibernate_mode) + goto fail; + m->hibernate_state = strv_new("disk", NULL); + if (!m->hibernate_state) + goto fail; + m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL); + if (!m->hybrid_sleep_mode) + goto fail; + m->hybrid_sleep_state = strv_new("disk", NULL); + if (!m->hybrid_sleep_state) + goto fail; + m->udev = udev_new(); if (!m->udev) goto fail; @@ -86,6 +103,7 @@ Manager *manager_new(void) { sd_event_set_watchdog(m->event, true); + return m; fail: @@ -161,6 +179,13 @@ void manager_free(Manager *m) { strv_free(m->kill_only_users); strv_free(m->kill_exclude_users); + strv_free(m->suspend_mode); + strv_free(m->suspend_state); + strv_free(m->hibernate_mode); + strv_free(m->hibernate_state); + strv_free(m->hybrid_sleep_mode); + strv_free(m->hybrid_sleep_state); + free(m); } @@ -974,13 +999,25 @@ int manager_run(Manager *m) { } static int manager_parse_config_file(Manager *m) { + const char *unit, *logind_conf, *sections; + FILE *file; + bool relaxed, allow_include, warn; + assert(m); - return config_parse_many("/etc/systemd/logind.conf", - CONF_DIRS_NULSTR("systemd/logind.conf"), - "Login\0", - config_item_perf_lookup, logind_gperf_lookup, - false, m); + unit = NULL; + logind_conf = getenv("ELOGIND_CONF_FILE"); + if (!logind_conf) + logind_conf = PKGSYSCONFDIR "/logind.conf"; + sections = "Login\0Sleep\0"; + file = NULL; + relaxed = false; + allow_include = false; + warn = true; + + return config_parse(unit, logind_conf, file, sections, + config_item_perf_lookup, logind_gperf_lookup, + relaxed, allow_include, warn, m); } int main(int argc, char *argv[]) { diff --git a/src/login/logind.conf b/src/login/logind.conf index 3775b8310..04038bad2 100644 --- a/src/login/logind.conf +++ b/src/login/logind.conf @@ -30,3 +30,11 @@ #IdleActionSec=30min #RuntimeDirectorySize=10% #RemoveIPC=yes + +[Sleep] +#SuspendState=mem standby freeze +#SuspendMode= +#HibernateState=disk +#HibernateMode=platform shutdown +#HybridSleepState=disk +#HybridSleepMode=suspend platform shutdown diff --git a/src/login/logind.h b/src/login/logind.h index 5af01cb79..90c972df6 100644 --- a/src/login/logind.h +++ b/src/login/logind.h @@ -103,6 +103,10 @@ struct Manager { bool remove_ipc; + char **suspend_state, **suspend_mode; + char **hibernate_state, **hibernate_mode; + char **hybrid_sleep_state, **hybrid_sleep_mode; + Hashmap *polkit_registry; usec_t holdoff_timeout_usec; @@ -145,7 +149,7 @@ bool manager_is_docked_or_multiple_displays(Manager *m); extern const sd_bus_vtable manager_vtable[]; int bus_manager_shutdown_or_sleep_now_or_later(Manager *m, HandleAction action, InhibitWhat w, sd_bus_error *error); -int shutdown_or_sleep(HandleAction action); +int shutdown_or_sleep(Manager *m, HandleAction action); int manager_send_changed(Manager *manager, const char *property, ...) _sentinel_; -- 2.30.2