chiark / gitweb /
Prep v229: Consolidate system sleep functions.
authorSven Eden <yamakuzure@gmx.net>
Mon, 22 May 2017 07:12:52 +0000 (09:12 +0200)
committerSven Eden <yamakuzure@gmx.net>
Mon, 22 May 2017 07:12:52 +0000 (09:12 +0200)
Move sleep relevant functions from shared/sleep-config.* and
login/logind-action.* into login/logind-sleep.* - they aren't used outside
of elogind anyway.

Makefile.am
cb/elogind.cbp
src/login/logind-action.c
src/login/logind-dbus.c
src/login/logind-sleep.c [moved from src/shared/sleep-config.c with 61% similarity]
src/login/logind-sleep.h [moved from src/shared/sleep-config.h with 77% similarity]
src/login/logind.h

index b12457878337267aebc320e384281ac1ab4374cc..7facc7610600c1c5d8ad875a88b5947ab74fcbb8 100644 (file)
@@ -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 \
index 0e60aafcd21644cc06b11d9483695f8e57f8b92d..c4d879a64421f13f770b41aba1c41eed45c2424f 100644 (file)
                        <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>
                </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>
index e1db89a88c1a569078efbbf24a1322f569be36f6..4070405190724bb8030d97c8651420c440cce4b3 100644 (file)
@@ -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);
index aa361ed76edeca0956ea3a6ff57c5e85a62ae5fa..10432b2d34e674a2b0dd1738c420aa181ac4e587 100644 (file)
@@ -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)
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;
+}
+
similarity index 77%
rename from src/shared/sleep-config.h
rename to src/login/logind-sleep.h
index 0bfbaeff326f7058e7fbf587f0abec01df0afa77..c493c68ddd26c8e05150a10ac463a8e5b31af56c 100644 (file)
@@ -1,3 +1,5 @@
+#pragma once
+
 /***
   This file is part of systemd.
 
   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);
index a122ba10c4dc5d9fa42398b5bbe9fedf4f4aa699..986f98ccc849cc1c18600d6b2daecee5b7ee4bf4 100644 (file)
@@ -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;