Skip to content

Commit 6edd7d0

Browse files
committed
sleep: implement suspend/hibernate as first class targets
1 parent 4943c1c commit 6edd7d0

File tree

17 files changed

+243
-11
lines changed

17 files changed

+243
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/systemd-sleep
12
/systemd-inhibit
23
/systemd-remount-fs
34
/build-aux

Makefile.am

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ usergeneratordir=$(pkglibexecdir)/user-generators
6767
pkgincludedir=$(includedir)/systemd
6868
systemgeneratordir=$(rootlibexecdir)/system-generators
6969
systemshutdowndir=$(rootlibexecdir)/system-shutdown
70+
systemsleepdir=$(rootlibexecdir)/system-sleep
7071
systemunitdir=$(rootprefix)/lib/systemd/system
7172
udevlibexecdir=$(rootprefix)/lib/udev
7273
udevhomedir = $(libexecdir)/udev
@@ -117,6 +118,7 @@ AM_CPPFLAGS = \
117118
-DSYSTEMD_CGROUP_AGENT_PATH=\"$(rootlibexecdir)/systemd-cgroups-agent\" \
118119
-DSYSTEMD_BINARY_PATH=\"$(rootlibexecdir)/systemd\" \
119120
-DSYSTEMD_SHUTDOWN_BINARY_PATH=\"$(rootlibexecdir)/systemd-shutdown\" \
121+
-DSYSTEMD_SLEEP_BINARY_PATH=\"$(rootlibexecdir)/systemd-sleep\" \
120122
-DSYSTEMCTL_BINARY_PATH=\"$(rootbindir)/systemctl\" \
121123
-DSYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH=\"$(rootbindir)/systemd-tty-ask-password-agent\" \
122124
-DSYSTEMD_STDIO_BRIDGE_BINARY_PATH=\"$(bindir)/systemd-stdio-bridge\" \
@@ -230,7 +232,8 @@ rootlibexec_PROGRAMS = \
230232
systemd-fsck \
231233
systemd-timestamp \
232234
systemd-ac-power \
233-
systemd-sysctl
235+
systemd-sysctl \
236+
systemd-sleep
234237

235238
systemgenerator_PROGRAMS = \
236239
systemd-getty-generator
@@ -266,6 +269,7 @@ dist_systemunit_DATA = \
266269
units/nss-lookup.target \
267270
units/nss-user-lookup.target \
268271
units/mail-transfer-agent.target \
272+
units/hibernate.target \
269273
units/http-daemon.target \
270274
units/poweroff.target \
271275
units/reboot.target \
@@ -276,7 +280,9 @@ dist_systemunit_DATA = \
276280
units/final.target \
277281
units/umount.target \
278282
units/sigpwr.target \
283+
units/sleep.target \
279284
units/sockets.target \
285+
units/suspend.target \
280286
units/swap.target \
281287
units/systemd-initctl.socket \
282288
units/systemd-shutdownd.socket \
@@ -318,12 +324,14 @@ nodist_systemunit_DATA = \
318324
units/systemd-sysctl.service \
319325
units/halt.service \
320326
units/emergency.service \
327+
units/hibernate.service \
321328
units/poweroff.service \
322329
units/reboot.service \
323330
units/kexec.service \
324331
units/fsck@.service \
325332
units/fsck-root.service \
326333
units/rescue.service \
334+
units/suspend.service \
327335
units/user@.service \
328336
units/systemd-udev.service \
329337
units/systemd-udev-trigger.service \
@@ -1062,6 +1070,13 @@ systemd_sysctl_SOURCES = \
10621070
systemd_sysctl_LDADD = \
10631071
libsystemd-shared.la
10641072

1073+
# ------------------------------------------------------------------------------
1074+
systemd_sleep_SOURCES = \
1075+
src/sleep/sleep.c
1076+
1077+
systemd_sleep_LDADD = \
1078+
libsystemd-shared.la
1079+
10651080
# ------------------------------------------------------------------------------
10661081
systemd_fsck_SOURCES = \
10671082
src/fsck/fsck.c
@@ -3027,6 +3042,7 @@ systemd-install-data-hook:
30273042
$(DESTDIR)$(prefix)/lib/sysctl.d \
30283043
$(DESTDIR)$(sysconfdir)/sysctl.d \
30293044
$(DESTDIR)$(systemshutdowndir) \
3045+
$(DESTDIR)$(systemsleepdir) \
30303046
$(DESTDIR)$(systemgeneratordir) \
30313047
$(DESTDIR)$(usergeneratordir)
30323048
$(MKDIR_P) -m 0755 \

TODO

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ Features:
5353

5454
* ExecOnFailure=/usr/bin/foo
5555

56-
* logind: add "mode" flag to poweroff/suspend inhibit logic so that we can
57-
support both "inhibit" and "delay" mode.
58-
5956
* fedora: make sshd and pam_loginuid work in nspawn containers
6057

6158
* fix utmp for console logins in containers
@@ -72,8 +69,6 @@ Features:
7269

7370
* journald: allow forwarding of log data to specific TTY instea dof console
7471

75-
* suspend/hibernate/hybrid support, auto-suspend logic with idle hint
76-
7772
* add RequiredBy to [Install]
7873

7974
* udev: move to LGPL

man/systemctl.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,16 @@
11491149
<option>--user</option> option) and
11501150
will fail otherwise.</para></listitem>
11511151
</varlistentry>
1152+
<varlistentry>
1153+
<term><command>suspend</command></term>
1154+
1155+
<listitem><para>Suspend the system.</para></listitem>
1156+
</varlistentry>
1157+
<varlistentry>
1158+
<term><command>hibernate</command></term>
1159+
1160+
<listitem><para>Hibernate the system.</para></listitem>
1161+
</varlistentry>
11521162
</variablelist>
11531163

11541164
</refsect1>

man/systemd.special.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<filename>exit.service</filename>,
5959
<filename>final.service</filename>,
6060
<filename>graphical.target</filename>,
61+
<filename>hibernate.target</filename>,
6162
<filename>http-daemon.target</filename>,
6263
<filename>halt.target</filename>,
6364
<filename>kbrequest.target</filename>,
@@ -80,7 +81,9 @@
8081
<filename>runlevel5.target</filename>,
8182
<filename>shutdown.target</filename>,
8283
<filename>sigpwr.target</filename>,
84+
<filename>sleep.target</filename>,
8385
<filename>sockets.target</filename>,
86+
<filename>suspend.target</filename>,
8487
<filename>swap.target</filename>,
8588
<filename>sysinit.target</filename>,
8689
<filename>syslog.service</filename>,
@@ -239,6 +242,15 @@
239242
during installation.</para>
240243
</listitem>
241244
</varlistentry>
245+
<varlistentry>
246+
<term><filename>hibernate.target</filename></term>
247+
<listitem>
248+
<para>A special target unit
249+
for hibernating the
250+
system. This pulls in
251+
<filename>sleep.target</filename>.</para>
252+
</listitem>
253+
</varlistentry>
242254
<varlistentry>
243255
<term><filename>http-daemon.target</filename></term>
244256
<listitem>
@@ -590,6 +602,19 @@
590602
power fails.</para>
591603
</listitem>
592604
</varlistentry>
605+
<varlistentry>
606+
<term><filename>sleep.target</filename></term>
607+
<listitem>
608+
<para>A special target unit
609+
that is pulled in by
610+
<filename>suspend.target</filename>
611+
and
612+
<filename>hibernate.target</filename>
613+
and may be used to hook units
614+
into the sleep state
615+
logic.</para>
616+
</listitem>
617+
</varlistentry>
593618
<varlistentry>
594619
<term><filename>sockets.target</filename></term>
595620
<listitem>
@@ -604,6 +629,15 @@
604629
during installation.</para>
605630
</listitem>
606631
</varlistentry>
632+
<varlistentry>
633+
<term><filename>suspend.target</filename></term>
634+
<listitem>
635+
<para>A special target unit
636+
for suspending the
637+
system. This pulls in
638+
<filename>sleep.target</filename>.</para>
639+
</listitem>
640+
</varlistentry>
607641
<varlistentry>
608642
<term><filename>swap.target</filename></term>
609643
<listitem>

src/core/shutdown.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ int main(int argc, char *argv[]) {
309309
unsigned retries;
310310
bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
311311
bool killed_everbody = false, in_container, use_watchdog = false;
312+
char *arguments[3];
312313

313314
log_parse_environment();
314315
log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
@@ -442,7 +443,10 @@ int main(int argc, char *argv[]) {
442443
if (retries >= FINALIZE_ATTEMPTS)
443444
log_error("Too many iterations, giving up.");
444445

445-
execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, NULL);
446+
arguments[0] = NULL;
447+
arguments[1] = argv[1];
448+
arguments[2] = NULL;
449+
execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, arguments);
446450

447451
/* If we are in a container, just exit, this will kill our
448452
* container for good. */

src/core/special.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#define SPECIAL_REBOOT_TARGET "reboot.target"
3737
#define SPECIAL_KEXEC_TARGET "kexec.target"
3838
#define SPECIAL_EXIT_TARGET "exit.target"
39+
#define SPECIAL_SUSPEND_TARGET "suspend.target"
40+
#define SPECIAL_HIBERNATE_TARGET "hibernate.target"
3941

4042
/* Special boot targets */
4143
#define SPECIAL_RESCUE_TARGET "rescue.target"

src/shared/util.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,8 +4106,7 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) {
41064106
_argv[1] = NULL;
41074107
argv = _argv;
41084108
} else
4109-
if (!argv[0])
4110-
argv[0] = path;
4109+
argv[0] = path;
41114110

41124111
execv(path, argv);
41134112

src/sleep/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile

src/sleep/sleep.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
/***
4+
This file is part of systemd.
5+
6+
Copyright 2012 Lennart Poettering
7+
8+
systemd is free software; you can redistribute it and/or modify it
9+
under the terms of the GNU Lesser General Public License as published by
10+
the Free Software Foundation; either version 2.1 of the License, or
11+
(at your option) any later version.
12+
13+
systemd is distributed in the hope that it will be useful, but
14+
WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public License
19+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
20+
***/
21+
22+
#include <stdio.h>
23+
#include <errno.h>
24+
#include <string.h>
25+
26+
#include "log.h"
27+
#include "util.h"
28+
29+
int main(int argc, char *argv[]) {
30+
const char *verb;
31+
char* arguments[4];
32+
int r;
33+
FILE *f;
34+
35+
log_set_target(LOG_TARGET_AUTO);
36+
log_parse_environment();
37+
log_open();
38+
39+
if (argc != 2) {
40+
log_error("Invalid number of arguments.");
41+
r = -EINVAL;
42+
goto finish;
43+
}
44+
45+
if (streq(argv[1], "suspend"))
46+
verb = "mem";
47+
else if (streq(argv[1], "hibernate"))
48+
verb = "disk";
49+
else {
50+
log_error("Unknown action '%s'.", argv[1]);
51+
r = -EINVAL;
52+
goto finish;
53+
}
54+
55+
f = fopen("/sys/power/state", "we");
56+
if (!f) {
57+
log_error("Failed to open /sys/power/state: %m");
58+
r = -errno;
59+
goto finish;
60+
}
61+
62+
arguments[0] = NULL;
63+
arguments[1] = (char*) "pre";
64+
arguments[2] = argv[1];
65+
arguments[3] = NULL;
66+
execute_directory(SYSTEMD_SLEEP_BINARY_PATH, NULL, arguments);
67+
68+
fputs(verb, f);
69+
fputc('\n', f);
70+
fflush(f);
71+
72+
r = ferror(f) ? -errno : 0;
73+
74+
arguments[1] = (char*) "post";
75+
execute_directory(SYSTEMD_SLEEP_BINARY_PATH, NULL, arguments);
76+
77+
fclose(f);
78+
79+
finish:
80+
81+
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
82+
83+
}

0 commit comments

Comments
 (0)