diff options
-rw-r--r-- | Makefile.am | 4 | ||||
-rw-r--r-- | cb/elogind.cbp | 8 | ||||
-rw-r--r-- | src/login/logind-action.c | 102 | ||||
-rw-r--r-- | src/login/logind-dbus.c | 6 | ||||
-rw-r--r-- | src/login/logind-sleep.c (renamed from src/shared/sleep-config.c) | 218 | ||||
-rw-r--r-- | src/login/logind-sleep.h (renamed from src/shared/sleep-config.h) | 13 | ||||
-rw-r--r-- | src/login/logind.h | 1 |
7 files changed, 139 insertions, 213 deletions
diff --git a/Makefile.am b/Makefile.am index b12457878..7facc7610 100644 --- a/Makefile.am +++ b/Makefile.am @@ -440,8 +440,6 @@ noinst_LTLIBRARIES += \ libshared.la libshared_la_SOURCES = \ - src/shared/sleep-config.c \ - src/shared/sleep-config.h \ src/shared/conf-parser.c \ src/shared/conf-parser.h \ src/shared/pager.c \ @@ -655,6 +653,8 @@ libelogind_core_la_SOURCES = \ src/login/logind-session.h \ src/login/logind-session-device.c \ src/login/logind-session-device.h \ + src/login/logind-sleep.c \ + src/login/logind-sleep.h \ src/login/logind-user.c \ src/login/logind-user.h \ src/login/logind-inhibit.c \ diff --git a/cb/elogind.cbp b/cb/elogind.cbp index 0e60aafcd..c4d879a64 100644 --- a/cb/elogind.cbp +++ b/cb/elogind.cbp @@ -436,6 +436,10 @@ <Option compilerVar="CC" /> </Unit> <Unit filename="../src/login/logind-session.h" /> + <Unit filename="../src/login/logind-sleep.c"> + <Option compilerVar="CC" /> + </Unit> + <Unit filename="../src/login/logind-sleep.h" /> <Unit filename="../src/login/logind-user-dbus.c"> <Option compilerVar="CC" /> </Unit> @@ -485,10 +489,6 @@ </Unit> <Unit filename="../src/shared/pager.h" /> <Unit filename="../src/shared/path-lookup.h" /> - <Unit filename="../src/shared/sleep-config.c"> - <Option compilerVar="CC" /> - </Unit> - <Unit filename="../src/shared/sleep-config.h" /> <Unit filename="../src/shared/spawn-polkit-agent.c"> <Option compilerVar="CC" /> </Unit> diff --git a/src/login/logind-action.c b/src/login/logind-action.c index e1db89a88..407040519 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -26,7 +26,7 @@ #include "formats-util.h" #include "logind-action.h" #include "process-util.h" -#include "sleep-config.h" +//#include "sleep-config.h" //#include "special.h" #include "string-table.h" #include "terminal-util.h" @@ -113,11 +113,11 @@ int manager_handle_action( } if (handle == HANDLE_SUSPEND) - supported = can_sleep("suspend") > 0; + supported = can_sleep(m, "suspend") > 0; else if (handle == HANDLE_HIBERNATE) - supported = can_sleep("hibernate") > 0; + supported = can_sleep(m, "hibernate") > 0; else if (handle == HANDLE_HYBRID_SLEEP) - supported = can_sleep("hybrid-sleep") > 0; + supported = can_sleep(m, "hybrid-sleep") > 0; else if (handle == HANDLE_KEXEC) supported = access(KEXEC, X_OK) >= 0; else @@ -194,100 +194,6 @@ static int run_helper(const char *helper) { return wait_for_terminate_and_warn(helper, pid, true); } -static int write_mode(char **modes) { - int r = 0; - char **mode; - - STRV_FOREACH(mode, modes) { - int k; - - k = write_string_file("/sys/power/disk", *mode, 0); - if (k == 0) - return 0; - - log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", - *mode); - if (r == 0) - r = k; - } - - if (r < 0) - log_error_errno(r, "Failed to write mode to /sys/power/disk: %m"); - - return r; -} - -static int write_state(FILE **f, char **states) { - char **state; - int r = 0; - - STRV_FOREACH(state, states) { - int k; - - k = write_string_stream(*f, *state, true); - if (k == 0) - return 0; - log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", - *state); - if (r == 0) - r = k; - - fclose(*f); - *f = fopen("/sys/power/state", "we"); - if (!*f) - return log_error_errno(errno, "Failed to open /sys/power/state: %m"); - } - - return r; -} - -static int do_sleep(const char *arg_verb, char **modes, char **states) { - char *arguments[] = { - NULL, - (char*) "pre", - (char*) arg_verb, - NULL - }; - static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL}; - - int r; - _cleanup_fclose_ FILE *f = NULL; - - /* 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"); - if (!f) - return log_error_errno(errno, "Failed to open /sys/power/state: %m"); - - /* Configure the hibernation mode */ - r = write_mode(modes); - if (r < 0) - return r; - - execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments); - - log_struct(LOG_INFO, - LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START), - LOG_MESSAGE("Suspending system..."), - "SLEEP=%s", arg_verb, - NULL); - - r = write_state(&f, states); - if (r < 0) - return r; - - log_struct(LOG_INFO, - LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP), - LOG_MESSAGE("System resumed."), - "SLEEP=%s", arg_verb, - NULL); - - arguments[1] = (char*) "post"; - execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments); - - return r; -} - int shutdown_or_sleep(Manager *m, HandleAction action) { assert(m); diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index aa361ed76..10432b2d3 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -41,7 +41,7 @@ #include "path-util.h" #include "process-util.h" #include "selinux-util.h" -#include "sleep-config.h" +//#include "sleep-config.h" //#include "special.h" #include "strv.h" #include "terminal-util.h" @@ -1789,7 +1789,7 @@ static int method_do_shutdown_or_sleep( return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress"); if (sleep_verb) { - r = can_sleep(sleep_verb); + r = can_sleep(m, sleep_verb); if (r < 0) return r; @@ -2148,7 +2148,7 @@ static int method_can_shutdown_or_sleep( assert(action_ignore_inhibit); if (sleep_verb) { - r = can_sleep(sleep_verb); + r = can_sleep(m, sleep_verb); if (r < 0) return r; if (r == 0) diff --git a/src/shared/sleep-config.c b/src/login/logind-sleep.c index e8998d6ff..cd6691ace 100644 --- a/src/shared/sleep-config.c +++ b/src/login/logind-sleep.c @@ -22,105 +22,24 @@ #include <stddef.h> #include <stdio.h> #include <string.h> -#include <syslog.h> -#include <unistd.h> +//#include <syslog.h> +//#include <unistd.h> -#include "alloc-util.h" -#include "conf-parser.h" -#include "def.h" +#include "sd-messages.h" + +//#include "alloc-util.h" +//#include "conf-parser.h" +//#include "def.h" #include "fd-util.h" #include "fileio.h" #include "log.h" -#include "macro.h" +#include "logind-sleep.h" +//#include "macro.h" #include "parse-util.h" -#include "sleep-config.h" #include "string-util.h" #include "strv.h" -#define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0) - -#if 0 /// UNNEEDED by elogind -int parse_sleep_config(const char *verb, char ***_modes, char ***_states) { -#else -/// really only used in here. -static int parse_sleep_config(const char *verb, char ***_modes, char ***_states) { -#endif // 0 - _cleanup_strv_free_ char - **suspend_mode = NULL, **suspend_state = NULL, - **hibernate_mode = NULL, **hibernate_state = NULL, - **hybrid_mode = NULL, **hybrid_state = NULL; - char **modes, **states; - - const ConfigTableItem items[] = { - { "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 }, - {} - }; - -#if 0 /// elogind has its own config file - config_parse_many(PKGSYSCONFDIR "/sleep.conf", - CONF_PATHS_NULSTR("systemd/sleep.conf.d"), - "Sleep\0", config_item_table_lookup, items, - false, NULL); -#else - const char* logind_conf = getenv("ELOGIND_CONF_FILE"); - if (!logind_conf) - logind_conf = PKGSYSCONFDIR "/logind.conf"; - config_parse(NULL, logind_conf, NULL, "Sleep\0", - config_item_table_lookup, items, false, - false, true, NULL); -#endif // 0 - - if (streq(verb, "suspend")) { - /* empty by default */ - USE(modes, suspend_mode); - - if (suspend_state) - USE(states, suspend_state); - else - states = strv_new("mem", "standby", "freeze", NULL); - - } else if (streq(verb, "hibernate")) { - if (hibernate_mode) - USE(modes, hibernate_mode); - else - modes = strv_new("platform", "shutdown", NULL); - - if (hibernate_state) - USE(states, hibernate_state); - else - states = strv_new("disk", NULL); - - } else if (streq(verb, "hybrid-sleep")) { - if (hybrid_mode) - USE(modes, hybrid_mode); - else - modes = strv_new("suspend", "platform", "shutdown", NULL); - - if (hybrid_state) - USE(states, hybrid_state); - else - states = strv_new("disk", NULL); - - } else - assert_not_reached("what verb"); - - if ((!modes && !streq(verb, "suspend")) || !states) { - strv_free(modes); - strv_free(states); - return log_oom(); - } - - *_modes = modes; - *_states = states; - return 0; -} - -int can_sleep_state(char **types) { +static int can_sleep_state(char **types) { char **type; int r; _cleanup_free_ char *p = NULL; @@ -149,7 +68,7 @@ int can_sleep_state(char **types) { return false; } -int can_sleep_disk(char **types) { +static int can_sleep_disk(char **types) { char **type; int r; _cleanup_free_ char *p = NULL; @@ -268,20 +187,123 @@ static bool enough_memory_for_hibernation(void) { return r; } -int can_sleep(const char *verb) { - _cleanup_strv_free_ char **modes = NULL, **states = NULL; - int r; +int can_sleep(Manager *m, const char *verb) { assert(streq(verb, "suspend") || streq(verb, "hibernate") || streq(verb, "hybrid-sleep")); - r = parse_sleep_config(verb, &modes, &states); - if (r < 0) + if ( streq(verb, "suspend") + && ( !can_sleep_state(m->suspend_state) + || !can_sleep_disk(m->suspend_mode) ) ) + return false; + + if ( streq(verb, "hibernate") + && ( !can_sleep_state(m->hibernate_state) + || !can_sleep_disk(m->hibernate_mode) ) ) return false; - if (!can_sleep_state(states) || !can_sleep_disk(modes)) + if ( streq(verb, "hybrid-sleep") + && ( !can_sleep_state(m->hybrid_sleep_state) + || !can_sleep_disk(m->hybrid_sleep_mode) ) ) return false; + return streq(verb, "suspend") || enough_memory_for_hibernation(); } + +static int write_mode(char **modes) { + int r = 0; + char **mode; + + STRV_FOREACH(mode, modes) { + int k; + + k = write_string_file("/sys/power/disk", *mode, 0); + if (k == 0) + return 0; + + log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", + *mode); + if (r == 0) + r = k; + } + + if (r < 0) + log_error_errno(r, "Failed to write mode to /sys/power/disk: %m"); + + return r; +} + +static int write_state(FILE **f, char **states) { + char **state; + int r = 0; + + STRV_FOREACH(state, states) { + int k; + + k = write_string_stream(*f, *state, true); + if (k == 0) + return 0; + log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", + *state); + if (r == 0) + r = k; + + fclose(*f); + *f = fopen("/sys/power/state", "we"); + if (!*f) + return log_error_errno(errno, "Failed to open /sys/power/state: %m"); + } + + return r; +} + +int do_sleep(const char *arg_verb, char **modes, char **states) { + + char *arguments[] = { + NULL, + (char*) "pre", + (char*) arg_verb, + NULL + }; + static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL}; + + int r; + _cleanup_fclose_ FILE *f = NULL; + + /* 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"); + if (!f) + return log_error_errno(errno, "Failed to open /sys/power/state: %m"); + + /* Configure the hibernation mode */ + r = write_mode(modes); + if (r < 0) + return r; + + execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments); + + log_struct(LOG_INFO, + LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START), + LOG_MESSAGE("Suspending system..."), + "SLEEP=%s", arg_verb, + NULL); + + r = write_state(&f, states); + if (r < 0) + return r; + + log_struct(LOG_INFO, + LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP), + LOG_MESSAGE("System resumed."), + "SLEEP=%s", arg_verb, + NULL); + + arguments[1] = (char*) "post"; + execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments); + + return r; +} + diff --git a/src/shared/sleep-config.h b/src/login/logind-sleep.h index 0bfbaeff3..c493c68dd 100644 --- a/src/shared/sleep-config.h +++ b/src/login/logind-sleep.h @@ -1,3 +1,5 @@ +#pragma once + /*** This file is part of systemd. @@ -17,12 +19,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#pragma once - -#if 0 /// UNNEEDED by elogind -int parse_sleep_config(const char *verb, char ***modes, char ***states); -#endif // 0 +#include "logind.h" -int can_sleep(const char *verb); -int can_sleep_disk(char **types); -int can_sleep_state(char **types); +int can_sleep(Manager *m, const char *verb); +int do_sleep(const char *arg_verb, char **modes, char **states); diff --git a/src/login/logind.h b/src/login/logind.h index a122ba10c..986f98ccc 100644 --- a/src/login/logind.h +++ b/src/login/logind.h @@ -37,6 +37,7 @@ typedef struct Manager Manager; #include "logind-button.h" #include "logind-device.h" #include "logind-inhibit.h" +#include "logind-sleep.h" struct Manager { sd_event *event; |