chiark / gitweb /
path: follow symbolic link for parent path
[elogind.git] / src / shared / sleep-config.c
index 70a08960690060abd583c20f5650733e4b3935e4..ae14c6bd4ddb3e9aba43295fb88dc78eeba6f098 100644 (file)
@@ -31,6 +31,7 @@
 #define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0)
 
 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
+
         _cleanup_strv_free_ char
                 **suspend_mode = NULL, **suspend_state = NULL,
                 **hibernate_mode = NULL, **hibernate_state = NULL,
@@ -44,21 +45,13 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
                 { "Sleep",   "HibernateState",   config_parse_strv,  0, &hibernate_state },
                 { "Sleep",   "HybridSleepMode",  config_parse_strv,  0, &hybrid_mode  },
                 { "Sleep",   "HybridSleepState", config_parse_strv,  0, &hybrid_state },
-                {}};
+                {}
+        };
 
-        int r;
-        FILE _cleanup_fclose_ *f;
-
-        f = fopen(PKGSYSCONFDIR "/sleep.conf", "re");
-        if (!f)
-                log_full(errno == ENOENT ? LOG_DEBUG: LOG_WARNING,
-                         "Failed to open configuration file " PKGSYSCONFDIR "/sleep.conf: %m");
-        else {
-                r = config_parse(NULL, PKGSYSCONFDIR "/sleep.conf", f, "Sleep\0",
-                                 config_item_table_lookup, (void*) items, false, false, NULL);
-                if (r < 0)
-                        log_warning("Failed to parse configuration file: %s", strerror(-r));
-        }
+        config_parse_many(PKGSYSCONFDIR "/sleep.conf",
+                          CONF_DIRS_NULSTR("systemd/sleep.conf"),
+                          "Sleep\0", config_item_table_lookup, items,
+                          false, NULL);
 
         if (streq(verb, "suspend")) {
                 /* empty by default */
@@ -106,7 +99,7 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
 }
 
 int can_sleep_state(char **types) {
-        char *w, *state, **type;
+        char **type;
         int r;
         _cleanup_free_ char *p = NULL;
 
@@ -122,11 +115,12 @@ int can_sleep_state(char **types) {
                 return false;
 
         STRV_FOREACH(type, types) {
+                const char *word, *state;
                 size_t l, k;
 
                 k = strlen(*type);
-                FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state)
-                        if (l == k && memcmp(w, *type, l) == 0)
+                FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
+                        if (l == k && memcmp(word, *type, l) == 0)
                                 return true;
         }
 
@@ -134,7 +128,7 @@ int can_sleep_state(char **types) {
 }
 
 int can_sleep_disk(char **types) {
-        char *w, *state, **type;
+        char **type;
         int r;
         _cleanup_free_ char *p = NULL;
 
@@ -150,14 +144,18 @@ int can_sleep_disk(char **types) {
                 return false;
 
         STRV_FOREACH(type, types) {
+                const char *word, *state;
                 size_t l, k;
 
                 k = strlen(*type);
-                FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state) {
-                        if (l == k && memcmp(w, *type, l) == 0)
+                FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
+                        if (l == k && memcmp(word, *type, l) == 0)
                                 return true;
 
-                        if (l == k + 2 && w[0] == '[' && memcmp(w + 1, *type, l - 2) == 0 && w[l-1] == ']')
+                        if (l == k + 2 &&
+                            word[0] == '[' &&
+                            memcmp(word + 1, *type, l - 2) == 0 &&
+                            word[l-1] == ']')
                                 return true;
                 }
         }
@@ -174,7 +172,7 @@ static int hibernation_partition_size(size_t *size, size_t *used) {
         assert(size);
         assert(used);
 
-        f = fopen("/proc/swaps", "r");
+        f = fopen("/proc/swaps", "re");
         if (!f) {
                 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
                          "Failed to retrieve open /proc/swaps: %m");
@@ -220,8 +218,8 @@ static int hibernation_partition_size(size_t *size, size_t *used) {
 
 static bool enough_memory_for_hibernation(void) {
         _cleanup_free_ char *active = NULL;
-        unsigned long long act;
-        size_t size, used;
+        unsigned long long act = 0;
+        size_t size = 0, used = 0;
         int r;
 
         r = hibernation_partition_size(&size, &used);
@@ -230,14 +228,14 @@ static bool enough_memory_for_hibernation(void) {
 
         r = get_status_field("/proc/meminfo", "\nActive(anon):", &active);
         if (r < 0) {
-                log_error("Failed to retrieve Active(anon) from /proc/meminfo: %s", strerror(-r));
+                log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
                 return false;
         }
 
         r = safe_atollu(active, &act);
         if (r < 0) {
-                log_error("Failed to parse Active(anon) from /proc/meminfo: %s: %s",
-                          active, strerror(-r));
+                log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
+                                active);
                 return false;
         }