]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-stub-pid1.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / nspawn / nspawn-stub-pid1.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <sys/reboot.h>
7 #include <sys/wait.h>
8 #include <sys/prctl.h>
9 #include <unistd.h>
10
11 #include "fd-util.h"
12 #include "log.h"
13 #include "missing.h"
14 #include "nspawn-stub-pid1.h"
15 #include "process-util.h"
16 #include "signal-util.h"
17 #include "time-util.h"
18 #include "def.h"
19
20 static int reset_environ(const char *new_environment, size_t length) {
21 unsigned long start, end;
22
23 start = (unsigned long) new_environment;
24 end = start + length;
25
26 if (prctl(PR_SET_MM, PR_SET_MM_ENV_START, start, 0, 0) < 0)
27 return -errno;
28
29 if (prctl(PR_SET_MM, PR_SET_MM_ENV_END, end, 0, 0) < 0)
30 return -errno;
31
32 return 0;
33 }
34
35 int stub_pid1(sd_id128_t uuid) {
36 enum {
37 STATE_RUNNING,
38 STATE_REBOOT,
39 STATE_POWEROFF,
40 } state = STATE_RUNNING;
41
42 sigset_t fullmask, oldmask, waitmask;
43 usec_t quit_usec = USEC_INFINITY;
44 pid_t pid;
45 int r;
46
47 /* The new environment we set up, on the stack. */
48 char new_environment[] =
49 "container=systemd-nspawn\0"
50 "container_uuid=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
51
52 /* Implements a stub PID 1, that reaps all processes and processes a couple of standard signals. This is useful
53 * for allowing arbitrary processes run in a container, and still have all zombies reaped. */
54
55 assert_se(sigfillset(&fullmask) >= 0);
56 assert_se(sigprocmask(SIG_BLOCK, &fullmask, &oldmask) >= 0);
57
58 pid = fork();
59 if (pid < 0)
60 return log_error_errno(errno, "Failed to fork child pid: %m");
61
62 if (pid == 0) {
63 /* Return in the child */
64 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) >= 0);
65 setsid();
66 return 0;
67 }
68
69 reset_all_signal_handlers();
70
71 log_close();
72 close_all_fds(NULL, 0);
73 log_open();
74
75 /* Flush out /proc/self/environ, so that we don't leak the environment from the host into the container. Also,
76 * set $container= and $container_uuid= so that clients in the container that query it from /proc/1/environ
77 * find them set. */
78 sd_id128_to_string(uuid, new_environment + sizeof(new_environment) - SD_ID128_STRING_MAX);
79 reset_environ(new_environment, sizeof(new_environment));
80
81 (void) rename_process("(sd-stubinit)");
82
83 assert_se(sigemptyset(&waitmask) >= 0);
84 assert_se(sigset_add_many(&waitmask,
85 SIGCHLD, /* posix: process died */
86 SIGINT, /* sysv: ctrl-alt-del */
87 SIGRTMIN+3, /* systemd: halt */
88 SIGRTMIN+4, /* systemd: poweroff */
89 SIGRTMIN+5, /* systemd: reboot */
90 SIGRTMIN+6, /* systemd: kexec */
91 SIGRTMIN+13, /* systemd: halt */
92 SIGRTMIN+14, /* systemd: poweroff */
93 SIGRTMIN+15, /* systemd: reboot */
94 SIGRTMIN+16, /* systemd: kexec */
95 -1) >= 0);
96
97 /* Note that we ignore SIGTERM (sysv's reexec), SIGHUP (reload), and all other signals here, since we don't
98 * support reexec/reloading in this stub process. */
99
100 for (;;) {
101 siginfo_t si;
102 usec_t current_usec;
103
104 si.si_pid = 0;
105 r = waitid(P_ALL, 0, &si, WEXITED|WNOHANG);
106 if (r < 0) {
107 r = log_error_errno(errno, "Failed to reap children: %m");
108 goto finish;
109 }
110
111 current_usec = now(CLOCK_MONOTONIC);
112
113 if (si.si_pid == pid || current_usec >= quit_usec) {
114
115 /* The child we started ourselves died or we reached a timeout. */
116
117 if (state == STATE_REBOOT) { /* dispatch a queued reboot */
118 (void) reboot(RB_AUTOBOOT);
119 r = log_error_errno(errno, "Failed to reboot: %m");
120 goto finish;
121
122 } else if (state == STATE_POWEROFF)
123 (void) reboot(RB_POWER_OFF); /* if this fails, fall back to normal exit. */
124
125 if (si.si_pid == pid && si.si_code == CLD_EXITED)
126 r = si.si_status; /* pass on exit code */
127 else
128 r = 255; /* signal, coredump, timeout, … */
129
130 goto finish;
131 }
132 if (si.si_pid != 0)
133 /* We reaped something. Retry until there's nothing more to reap. */
134 continue;
135
136 if (quit_usec == USEC_INFINITY)
137 r = sigwaitinfo(&waitmask, &si);
138 else {
139 struct timespec ts;
140 r = sigtimedwait(&waitmask, &si, timespec_store(&ts, quit_usec - current_usec));
141 }
142 if (r < 0) {
143 if (errno == EINTR) /* strace -p attach can result in EINTR, let's handle this nicely. */
144 continue;
145 if (errno == EAGAIN) /* timeout reached */
146 continue;
147
148 r = log_error_errno(errno, "Failed to wait for signal: %m");
149 goto finish;
150 }
151
152 if (si.si_signo == SIGCHLD)
153 continue; /* Let's reap this */
154
155 if (state != STATE_RUNNING)
156 continue;
157
158 /* Would love to use a switch() statement here, but SIGRTMIN is actually a function call, not a
159 * constant… */
160
161 if (si.si_signo == SIGRTMIN+3 ||
162 si.si_signo == SIGRTMIN+4 ||
163 si.si_signo == SIGRTMIN+13 ||
164 si.si_signo == SIGRTMIN+14)
165
166 state = STATE_POWEROFF;
167
168 else if (si.si_signo == SIGINT ||
169 si.si_signo == SIGRTMIN+5 ||
170 si.si_signo == SIGRTMIN+6 ||
171 si.si_signo == SIGRTMIN+15 ||
172 si.si_signo == SIGRTMIN+16)
173
174 state = STATE_REBOOT;
175 else
176 assert_not_reached("Got unexpected signal");
177
178 r = kill_and_sigcont(pid, SIGTERM);
179
180 /* Let's send a SIGHUP after the SIGTERM, as shells tend to ignore SIGTERM but do react to SIGHUP. We
181 * do it strictly in this order, so that the SIGTERM is dispatched first, and SIGHUP second for those
182 * processes which handle both. That's because services tend to bind configuration reload or something
183 * else to SIGHUP. */
184
185 if (r != -ESRCH)
186 (void) kill(pid, SIGHUP);
187
188 quit_usec = now(CLOCK_MONOTONIC) + DEFAULT_TIMEOUT_USEC;
189 }
190
191 finish:
192 _exit(r < 0 ? EXIT_FAILURE : r);
193 }