chiark / gitweb /
Prep v229: Consolidate system sleep functions.
[elogind.git] / src / login / logind-sleep.c
similarity index 61%
rename from src/shared/sleep-config.c
rename to src/login/logind-sleep.c
index e8998d6ff8721572f204c6295b388f4dd1713a24..cd6691aced8747b65e8d4f1a13d0e4c98456d863 100644 (file)
 #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;
+}
+