]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sleep: freeze and thaw user.slice to save resource while suspend
authorSonali Srivastava <srivastava.sonali1@gmail.com>
Wed, 17 Aug 2022 07:24:20 +0000 (12:54 +0530)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 26 Aug 2022 02:16:35 +0000 (11:16 +0900)
src/sleep/sleep.c

index d8e6380d0a622ee02b3cdf4f5ac9fe6f853b0082..3461d3e45f0987a98265c3f6a853d742a29ba697 100644 (file)
 #include <sys/timerfd.h>
 #include <unistd.h>
 
+#include "sd-bus.h"
 #include "sd-messages.h"
 
 #include "btrfs-util.h"
 #include "bus-error.h"
+#include "bus-locator.h"
+#include "bus-util.h"
 #include "def.h"
 #include "exec-util.h"
 #include "fd-util.h"
@@ -29,6 +32,7 @@
 #include "parse-util.h"
 #include "pretty-print.h"
 #include "sleep-config.h"
+#include "special.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -349,11 +353,38 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
         return 1;
 }
 
+/* Freeze when invoked and thaw on cleanup */
+static int freeze_thaw_user_slice(const char **method) {
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
+        int r;
+
+        if (!method || !*method)
+                return 0;
+
+        r = bus_connect_system_systemd(&bus);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to open connection to systemd: %m");
+
+        r = bus_call_method(bus, bus_systemd_mgr, *method, &error, NULL, "s", SPECIAL_USER_SLICE);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r));
+
+        return 1;
+}
+
 static int execute_s2h(const SleepConfig *sleep_config) {
+        _unused_ _cleanup_(freeze_thaw_user_slice) const char *auto_method_thaw = NULL;
         int r, k;
 
         assert(sleep_config);
 
+        r = freeze_thaw_user_slice(&(const char*) { "FreezeUnit" });
+        if (r < 0)
+                log_debug_errno(r, "Failed to freeze unit user.slice, ignoring: %m");
+        else
+                auto_method_thaw = "ThawUnit"; /* from now on we want automatic thawing */;
+
         r = check_wakeup_type();
         if (r < 0)
                 log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m");