]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sleep: add HibernateOnACPower= option (#33846)
authorMarc Reisner <reisner.marc@gmail.com>
Sun, 4 Aug 2024 23:01:34 +0000 (18:01 -0500)
committerGitHub <noreply@github.com>
Sun, 4 Aug 2024 23:01:34 +0000 (00:01 +0100)
* Add HibernateOnACPower= systemd-sleep configuration option

man/systemd-sleep.conf.xml
src/shared/sleep-config.c
src/shared/sleep-config.h
src/sleep/sleep.c
src/sleep/sleep.conf

index 623e614b6ab852b14eca9404fda9f1d3b8b60020..c02b44f3de62e2a6fafbc603cc59011ab768faae 100644 (file)
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><varname>HibernateOnACPower=</varname></term>
+
+        <listitem>
+          <para>Whether to allow hibernation when the system has AC power. Only used by
+          <citerefentry><refentrytitle>systemd-suspend-then-hibernate.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+          when <varname>HibernateDelaySec=</varname> is set.</para>
+
+          <para>If this option is disabled, the countdown of <varname>HibernateDelaySec=</varname> starts only
+          after AC power is disconnected, keeping the system in the suspend state otherwise.</para>
+
+          <para>This option is only effective on systems with a battery.</para>
+
+          <xi:include href="version-info.xml" xpointer="v257"/>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><varname>SuspendEstimationSec=</varname></term>
 
index 3d4d3317109d76d82005204f4651595d87597112..947ef582bd6b619bd9baf57ffbb0353e875a67ed 100644 (file)
@@ -124,7 +124,8 @@ int parse_sleep_config(SleepConfig **ret) {
                 return log_oom();
 
         *sc = (SleepConfig) {
-                .hibernate_delay_usec = USEC_INFINITY,
+                .hibernate_delay_usec  = USEC_INFINITY,
+                .hibernate_on_ac_power = true,
         };
 
         const ConfigTableItem items[] = {
@@ -145,6 +146,7 @@ int parse_sleep_config(SleepConfig **ret) {
                 { "Sleep", "MemorySleepMode",           config_parse_sleep_mode,  0,               &sc->mem_modes               },
 
                 { "Sleep", "HibernateDelaySec",         config_parse_sec,         0,               &sc->hibernate_delay_usec    },
+                { "Sleep", "HibernateOnACPower",        config_parse_bool,        0,               &sc->hibernate_on_ac_power   },
                 { "Sleep", "SuspendEstimationSec",      config_parse_sec,         0,               &sc->suspend_estimation_usec },
                 {}
         };
index b59bce8fc4e052bbbb3823277b77184665249ee6..b05efaea95ee2ba8851f6272150b3aa2f4f32294 100644 (file)
@@ -33,6 +33,7 @@ typedef struct SleepConfig {
         char **mem_modes;                           /* /sys/power/mem_sleep */
 
         usec_t hibernate_delay_usec;
+        bool hibernate_on_ac_power;
         usec_t suspend_estimation_usec;
 } SleepConfig;
 
index 15ef7ae9131842de8fd4906f52876cd2f6b2618f..3f113fff7449fc3aa2efcf0a2ba1107b9281e949 100644 (file)
@@ -390,7 +390,15 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
                         }
                 }
 
-                /* Do not suspend more than HibernateDelaySec= */
+                /* Do not suspend more than HibernateDelaySec= unless HibernateOnACPower=no and currently on AC power */
+                if (!sleep_config->hibernate_on_ac_power) {
+                        /* Do not allow "decay" to suspend if the system has no battery. */
+                        if (hashmap_isempty(last_capacity))
+                                log_once(LOG_WARNING, "HibernateOnACPower=no was ignored because the system does not have a battery.");
+                        else if (on_ac_power() > 0)
+                                hibernate_timestamp = usec_add(now(CLOCK_BOOTTIME), sleep_config->hibernate_delay_usec);
+                }
+
                 usec_t before_timestamp = now(CLOCK_BOOTTIME);
                 suspend_interval = MIN(suspend_interval, usec_sub_unsigned(hibernate_timestamp, before_timestamp));
                 if (suspend_interval <= 0)
index 98430348a7bf7fddf1cf4cbe78abc2e52a68ccf4..153f747342db20d36274b5b12333b36b0732b1ec 100644 (file)
@@ -25,4 +25,5 @@
 #HibernateMode=platform shutdown
 #MemorySleepMode=
 #HibernateDelaySec=
+#HibernateOnACPower=yes
 #SuspendEstimationSec=60min