chiark / gitweb /
Drop my copyright headers
[elogind.git] / src / shared / sleep-config.c
index 1086e689f7d896813d5b3ea23be0c7326a6d08cf..d9f6d9402a82eb9b2f799da6cc6633a91d3d79bd 100644 (file)
@@ -1,22 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2013 Zbigniew Jędrzejewski-Szmek
-  Copyright 2018 Dell Inc.
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+  Copyright © 2018 Dell Inc.
 ***/
 
 //#include <errno.h>
@@ -48,7 +32,7 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t
                 **suspend_mode = NULL, **suspend_state = NULL,
                 **hibernate_mode = NULL, **hibernate_state = NULL,
                 **hybrid_mode = NULL, **hybrid_state = NULL;
-        char **modes, **states;
+        _cleanup_strv_free_ char **modes, **states; /* always initialized below */
         usec_t delay = 180 * USEC_PER_MINUTE;
 
         const ConfigTableItem items[] = {
@@ -104,16 +88,13 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t
                 assert_not_reached("what verb");
 
         if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) ||
-            (!states && !streq(verb, "suspend-then-hibernate"))) {
-                strv_free(modes);
-                strv_free(states);
+            (!states && !streq(verb, "suspend-then-hibernate")))
                 return log_oom();
-        }
 
         if (_modes)
-                *_modes = modes;
+                *_modes = TAKE_PTR(modes);
         if (_states)
-                *_states = states;
+                *_states = TAKE_PTR(states);
         if (_delay)
                 *_delay = delay;
 
@@ -247,7 +228,7 @@ int find_hibernate_location(char **device, char **type, size_t *size, size_t *us
         return -ENOSYS;
 }
 
-static bool enough_memory_for_hibernation(void) {
+static bool enough_swap_for_hibernation(void) {
         _cleanup_free_ char *active = NULL;
         unsigned long long act = 0;
         size_t size = 0, used = 0;
@@ -284,11 +265,11 @@ static bool enough_memory_for_hibernation(void) {
 
 int read_fiemap(int fd, struct fiemap **ret) {
         _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
-        int extents_size;
         struct stat statinfo;
         uint32_t result_extents = 0;
         uint64_t fiemap_start = 0, fiemap_length;
-        size_t fiemap_size = 1, result_fiemap_size = 1;
+        const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
+        size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
 
         if (fstat(fd, &statinfo) < 0)
                 return log_debug_errno(errno, "Cannot determine file size: %m");
@@ -296,12 +277,12 @@ int read_fiemap(int fd, struct fiemap **ret) {
                 return -ENOTTY;
         fiemap_length = statinfo.st_size;
 
-        /* zero this out in case we run on a file with no extents */
-        fiemap = new0(struct fiemap, 1);
+        /* Zero this out in case we run on a file with no extents */
+        fiemap = calloc(n_extra, sizeof(struct fiemap_extent));
         if (!fiemap)
                 return -ENOMEM;
 
-        result_fiemap = new(struct fiemap, 1);
+        result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent));
         if (!result_fiemap)
                 return -ENOMEM;
 
@@ -325,12 +306,10 @@ int read_fiemap(int fd, struct fiemap **ret) {
                 if (fiemap->fm_mapped_extents == 0)
                         break;
 
-                /* Result fiemap has to hold all the extents for the whole file */
-                extents_size = DIV_ROUND_UP(sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents,
-                                            sizeof(struct fiemap));
-
-                /* Resize fiemap to allow us to read in the extents */
-                if (!GREEDY_REALLOC0(fiemap, fiemap_size, extents_size))
+                /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
+                 * the extents for the whole file. Add space for the initial struct fiemap. */
+                if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
+                                     n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
                         return -ENOMEM;
 
                 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
@@ -339,12 +318,9 @@ int read_fiemap(int fd, struct fiemap **ret) {
                 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
                         return log_debug_errno(errno, "Failed to read extents: %m");
 
-                extents_size = DIV_ROUND_UP(sizeof(struct fiemap_extent) * (result_extents + fiemap->fm_mapped_extents),
-                                            sizeof(struct fiemap));
-
-                /* Resize result_fiemap to allow us to read in the extents */
-                if (!GREEDY_REALLOC(result_fiemap, result_fiemap_size,
-                                    extents_size))
+                /* Resize result_fiemap to allow us to copy in the extents */
+                if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
+                                    n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
                         return -ENOMEM;
 
                 memcpy(result_fiemap->fm_extents + result_extents,
@@ -354,7 +330,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
                 result_extents += fiemap->fm_mapped_extents;
 
                 /* Highly unlikely that it is zero */
-                if (fiemap->fm_mapped_extents > 0) {
+                if (_likely_(fiemap->fm_mapped_extents > 0)) {
                         uint32_t i = fiemap->fm_mapped_extents - 1;
 
                         fiemap_start = fiemap->fm_extents[i].fe_logical +
@@ -373,6 +349,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
 
 #if 0 /// elogind has to do, or better, *can* do it differently
 static bool can_s2h(void) {
+        const char *p;
         int r;
 
         r = access("/sys/class/rtc/rtc0/wakealarm", W_OK);
@@ -382,16 +359,14 @@ static bool can_s2h(void) {
                 return false;
         }
 
-        r = can_sleep("suspend");
-        if (r < 0) {
-                log_debug_errno(r, "Unable to suspend system.");
-                return false;
-        }
-
-        r = can_sleep("hibernate");
-        if (r < 0) {
-                log_debug_errno(r, "Unable to hibernate system.");
-                return false;
+        FOREACH_STRING(p, "suspend", "hibernate") {
+                r = can_sleep(p);
+                if (IN_SET(r, 0, -ENOSPC)) {
+                        log_debug("Unable to %s system.", p);
+                        return false;
+                }
+                if (r < 0)
+                        return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
         }
 
         return true;
@@ -432,5 +407,13 @@ int can_sleep(const char *verb) {
                 return false;
 
         return streq(verb, "suspend") || enough_memory_for_hibernation();
+        if (streq(verb, "suspend"))
+                return true;
+
+        if (!enough_memory_for_hibernation())
+        if (!enough_swap_for_hibernation())
+                return -ENOSPC;
+
+        return true;
 }
 #endif // 0