]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/main.c
Merge pull request #2706 from whot/hwdb-updates
[thirdparty/systemd.git] / src / core / main.c
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
60918275 20#include <errno.h>
3dfc9763 21#include <fcntl.h>
f170852a 22#include <getopt.h>
97c4f35c 23#include <signal.h>
3dfc9763
LP
24#include <stdio.h>
25#include <string.h>
664f88a7 26#include <sys/mount.h>
3dfc9763 27#include <sys/prctl.h>
b9e74c39 28#include <sys/reboot.h>
3dfc9763
LP
29#include <sys/stat.h>
30#include <unistd.h>
b64a3d86
LP
31#ifdef HAVE_SECCOMP
32#include <seccomp.h>
33#endif
3dfc9763
LP
34#ifdef HAVE_VALGRIND_VALGRIND_H
35#include <valgrind/valgrind.h>
36#endif
54b434b1 37
718db961 38#include "sd-bus.h"
cf0fbc49 39#include "sd-daemon.h"
3dfc9763 40
b5efdb8a 41#include "alloc-util.h"
d9d93745 42#include "architecture.h"
3dfc9763
LP
43#include "build.h"
44#include "bus-error.h"
45#include "bus-util.h"
430f0182 46#include "capability-util.h"
24efb112 47#include "clock-util.h"
3dfc9763 48#include "conf-parser.h"
618234a5 49#include "cpu-set-util.h"
3dfc9763
LP
50#include "dbus-manager.h"
51#include "def.h"
52#include "env-util.h"
3ffd4af2 53#include "fd-util.h"
3dfc9763 54#include "fdset.h"
718db961 55#include "fileio.h"
24882e06 56#include "formats-util.h"
f4f15635 57#include "fs-util.h"
3dfc9763
LP
58#include "hostname-setup.h"
59#include "ima-setup.h"
60#include "killall.h"
61#include "kmod-setup.h"
d7b8eec7 62#include "load-fragment.h"
3dfc9763 63#include "log.h"
b6e66135 64#include "loopback-setup.h"
b6e66135 65#include "machine-id-setup.h"
3dfc9763
LP
66#include "manager.h"
67#include "missing.h"
68#include "mount-setup.h"
69#include "pager.h"
6bedfcbb 70#include "parse-util.h"
4e731273 71#include "proc-cmdline.h"
3dfc9763 72#include "process-util.h"
78f22b97 73#include "rlimit-util.h"
b6e66135 74#include "selinux-setup.h"
3dfc9763
LP
75#include "selinux-util.h"
76#include "signal-util.h"
ffbd2c4d 77#include "smack-setup.h"
3dfc9763 78#include "special.h"
8fcde012 79#include "stat-util.h"
15a5e950 80#include "stdio-util.h"
3dfc9763
LP
81#include "strv.h"
82#include "switch-root.h"
83#include "terminal-util.h"
b1d4f8e1 84#include "user-util.h"
3dfc9763
LP
85#include "virt.h"
86#include "watchdog.h"
b6e66135 87
f170852a
LP
88static enum {
89 ACTION_RUN,
e965d56d 90 ACTION_HELP,
9ba0bc4e 91 ACTION_VERSION,
e537352b 92 ACTION_TEST,
4288f619
LP
93 ACTION_DUMP_CONFIGURATION_ITEMS,
94 ACTION_DONE
fa0f4d8a 95} arg_action = ACTION_RUN;
fa0f4d8a 96static char *arg_default_unit = NULL;
b2c23da8 97static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
fa0f4d8a 98static bool arg_dump_core = true;
fa0f4d8a 99static int arg_crash_chvt = -1;
b9e74c39
LP
100static bool arg_crash_shell = false;
101static bool arg_crash_reboot = false;
fa0f4d8a 102static bool arg_confirm_spawn = false;
b6e2f329 103static ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
bf4df7c3 104static bool arg_switched_root = false;
b87c2aa6 105static int arg_no_pager = -1;
0c85a4f3 106static char ***arg_join_controllers = NULL;
706343f4 107static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
0a494f1f 108static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
1f19a534
OS
109static usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC;
110static usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC;
111static usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
3f41e1e5
LN
112static usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL;
113static unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST;
e96d6be7
LP
114static usec_t arg_runtime_watchdog = 0;
115static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
97d0e5f8 116static char **arg_default_environment = NULL;
517d56b1 117static struct rlimit *arg_default_rlimit[_RLIMIT_MAX] = {};
a103496c 118static uint64_t arg_capability_bounding_set = CAP_ALL;
3a43da28 119static nsec_t arg_timer_slack_nsec = NSEC_INFINITY;
bd8f585b 120static usec_t arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE;
d3b1c508
LP
121static Set* arg_syscall_archs = NULL;
122static FILE* arg_serialization = NULL;
085afe36
LP
123static bool arg_default_cpu_accounting = false;
124static bool arg_default_blockio_accounting = false;
125static bool arg_default_memory_accounting = false;
9ded9cd1
LP
126static bool arg_default_tasks_accounting = true;
127static uint64_t arg_default_tasks_max = UINT64_C(512);
ee48dbd5 128static sd_id128_t arg_machine_id = {};
4fc935ca 129
b87c2aa6
ZJS
130static void pager_open_if_enabled(void) {
131
132 if (arg_no_pager <= 0)
133 return;
134
135 pager_open(false);
136}
137
b9e74c39
LP
138noreturn static void freeze_or_reboot(void) {
139
140 if (arg_crash_reboot) {
141 log_notice("Rebooting in 10s...");
142 (void) sleep(10);
143
144 log_notice("Rebooting now...");
145 (void) reboot(RB_AUTOBOOT);
146 log_emergency_errno(errno, "Failed to reboot: %m");
147 }
148
149 log_emergency("Freezing execution.");
150 freeze();
151}
152
919ce0b7 153noreturn static void crash(int sig) {
7d06dad9
MS
154 struct sigaction sa;
155 pid_t pid;
97c4f35c 156
abb26902
LP
157 if (getpid() != 1)
158 /* Pass this on immediately, if this is not PID 1 */
92ca4cac 159 (void) raise(sig);
abb26902 160 else if (!arg_dump_core)
4104970e 161 log_emergency("Caught <%s>, not dumping core.", signal_to_string(sig));
97c4f35c 162 else {
7d06dad9 163 sa = (struct sigaction) {
189d5bac 164 .sa_handler = nop_signal_handler,
b92bea5d
ZJS
165 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
166 };
97c4f35c 167
6f5e3f35 168 /* We want to wait for the core process, hence let's enable SIGCHLD */
92ca4cac 169 (void) sigaction(SIGCHLD, &sa, NULL);
6f5e3f35 170
ee05e779 171 pid = raw_clone(SIGCHLD, NULL);
e62d8c39 172 if (pid < 0)
56f64d95 173 log_emergency_errno(errno, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
97c4f35c 174 else if (pid == 0) {
97c4f35c 175 /* Enable default signal handler for core dump */
15a90032 176
92ca4cac
LP
177 sa = (struct sigaction) {
178 .sa_handler = SIG_DFL,
179 };
180 (void) sigaction(sig, &sa, NULL);
97c4f35c 181
15a90032
LP
182 /* Don't limit the coredump size */
183 (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY));
97c4f35c
LP
184
185 /* Just to be sure... */
e62d9b81 186 (void) chdir("/");
97c4f35c
LP
187
188 /* Raise the signal again */
ee05e779 189 pid = raw_getpid();
92ca4cac 190 (void) kill(pid, sig); /* raise() would kill the parent */
97c4f35c
LP
191
192 assert_not_reached("We shouldn't be here...");
92ca4cac 193 _exit(EXIT_FAILURE);
4fc935ca 194 } else {
8e12a6ae
LP
195 siginfo_t status;
196 int r;
4fc935ca
LP
197
198 /* Order things nicely. */
e62d8c39
ZJS
199 r = wait_for_terminate(pid, &status);
200 if (r < 0)
da927ba9 201 log_emergency_errno(r, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig));
8e12a6ae 202 else if (status.si_code != CLD_DUMPED)
ee05e779
ZJS
203 log_emergency("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).",
204 signal_to_string(sig),
205 pid, sigchld_code_to_string(status.si_code),
206 status.si_status,
207 strna(status.si_code == CLD_EXITED
208 ? exit_status_to_string(status.si_status, EXIT_STATUS_FULL)
209 : signal_to_string(status.si_status)));
4fc935ca 210 else
4104970e 211 log_emergency("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
97c4f35c
LP
212 }
213 }
214
b9e74c39 215 if (arg_crash_chvt >= 0)
92ca4cac 216 (void) chvt(arg_crash_chvt);
601f6a1e 217
7d06dad9
MS
218 sa = (struct sigaction) {
219 .sa_handler = SIG_IGN,
220 .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
221 };
222
223 /* Let the kernel reap children for us */
224 (void) sigaction(SIGCHLD, &sa, NULL);
8c43883a 225
7d06dad9 226 if (arg_crash_shell) {
b9e74c39 227 log_notice("Executing crash shell in 10s...");
92ca4cac 228 (void) sleep(10);
4fc935ca 229
ee05e779 230 pid = raw_clone(SIGCHLD, NULL);
cd3bd60a 231 if (pid < 0)
56f64d95 232 log_emergency_errno(errno, "Failed to fork off crash shell: %m");
6f5e3f35 233 else if (pid == 0) {
b9e74c39 234 (void) setsid();
92ca4cac
LP
235 (void) make_console_stdio();
236 (void) execle("/bin/sh", "/bin/sh", NULL, environ);
6f5e3f35 237
ee05e779 238 log_emergency_errno(errno, "execle() failed: %m");
92ca4cac 239 _exit(EXIT_FAILURE);
b9e74c39
LP
240 } else {
241 log_info("Spawned crash shell as PID "PID_FMT".", pid);
4cf0b03b 242 (void) wait_for_terminate(pid, NULL);
b9e74c39 243 }
4fc935ca
LP
244 }
245
b9e74c39 246 freeze_or_reboot();
97c4f35c
LP
247}
248
249static void install_crash_handler(void) {
297d563d 250 static const struct sigaction sa = {
b92bea5d 251 .sa_handler = crash,
297d563d 252 .sa_flags = SA_NODEFER, /* So that we can raise the signal again from the signal handler */
b92bea5d 253 };
297d563d 254 int r;
97c4f35c 255
297d563d
LP
256 /* We ignore the return value here, since, we don't mind if we
257 * cannot set up a crash handler */
258 r = sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
259 if (r < 0)
260 log_debug_errno(r, "I had trouble setting up the crash handler, ignoring: %m");
97c4f35c 261}
f170852a 262
56d96fc0
LP
263static int console_setup(void) {
264 _cleanup_close_ int tty_fd = -1;
265 int r;
80876c20 266
512947d4 267 tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
23bbb0de
MS
268 if (tty_fd < 0)
269 return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
80876c20 270
56d96fc0
LP
271 /* We don't want to force text mode. plymouth may be showing
272 * pictures already from initrd. */
512947d4 273 r = reset_terminal_fd(tty_fd, false);
23bbb0de
MS
274 if (r < 0)
275 return log_error_errno(r, "Failed to reset /dev/console: %m");
843d2643 276
56d96fc0 277 return 0;
80876c20
LP
278}
279
b9e74c39
LP
280static int parse_crash_chvt(const char *value) {
281 int b;
282
283 if (safe_atoi(value, &arg_crash_chvt) >= 0)
284 return 0;
285
286 b = parse_boolean(value);
287 if (b < 0)
288 return b;
289
290 if (b > 0)
291 arg_crash_chvt = 0; /* switch to where kmsg goes */
292 else
293 arg_crash_chvt = -1; /* turn off switching */
294
295 return 0;
296}
297
ee48dbd5
NC
298static int set_machine_id(const char *m) {
299
300 if (sd_id128_from_string(m, &arg_machine_id) < 0)
301 return -EINVAL;
302
303 if (sd_id128_is_null(arg_machine_id))
304 return -EINVAL;
305
306 return 0;
307}
308
059cb385 309static int parse_proc_cmdline_item(const char *key, const char *value) {
f170852a 310
059cb385 311 int r;
f170852a 312
059cb385 313 assert(key);
5192bd19 314
059cb385 315 if (streq(key, "systemd.unit") && value) {
bf4df7c3
LP
316
317 if (!in_initrd())
e6e242ad 318 return free_and_strdup(&arg_default_unit, value);
bf4df7c3 319
059cb385 320 } else if (streq(key, "rd.systemd.unit") && value) {
bf4df7c3 321
ca05941b 322 if (in_initrd())
e6e242ad 323 return free_and_strdup(&arg_default_unit, value);
f170852a 324
059cb385 325 } else if (streq(key, "systemd.dump_core") && value) {
4fc935ca 326
059cb385 327 r = parse_boolean(value);
fb472900 328 if (r < 0)
059cb385 329 log_warning("Failed to parse dump core switch %s. Ignoring.", value);
4fc935ca 330 else
fa0f4d8a 331 arg_dump_core = r;
4fc935ca 332
b9e74c39
LP
333 } else if (streq(key, "systemd.crash_chvt") && value) {
334
335 if (parse_crash_chvt(value) < 0)
336 log_warning("Failed to parse crash chvt switch %s. Ignoring.", value);
337
059cb385 338 } else if (streq(key, "systemd.crash_shell") && value) {
4fc935ca 339
059cb385 340 r = parse_boolean(value);
fb472900 341 if (r < 0)
059cb385 342 log_warning("Failed to parse crash shell switch %s. Ignoring.", value);
4fc935ca 343 else
fa0f4d8a 344 arg_crash_shell = r;
5e7ee61c 345
b9e74c39 346 } else if (streq(key, "systemd.crash_reboot") && value) {
5e7ee61c 347
b9e74c39
LP
348 r = parse_boolean(value);
349 if (r < 0)
350 log_warning("Failed to parse crash reboot switch %s. Ignoring.", value);
5e7ee61c 351 else
b9e74c39 352 arg_crash_reboot = r;
5e7ee61c 353
059cb385 354 } else if (streq(key, "systemd.confirm_spawn") && value) {
601f6a1e 355
059cb385
LP
356 r = parse_boolean(value);
357 if (r < 0)
358 log_warning("Failed to parse confirm spawn switch %s. Ignoring.", value);
601f6a1e 359 else
059cb385 360 arg_confirm_spawn = r;
601f6a1e 361
059cb385 362 } else if (streq(key, "systemd.show_status") && value) {
9e58ff9c 363
059cb385 364 r = parse_show_status(value, &arg_show_status);
d450b6f2 365 if (r < 0)
059cb385
LP
366 log_warning("Failed to parse show status switch %s. Ignoring.", value);
367
368 } else if (streq(key, "systemd.default_standard_output") && value) {
0a494f1f 369
059cb385 370 r = exec_output_from_string(value);
fb472900 371 if (r < 0)
059cb385 372 log_warning("Failed to parse default standard output switch %s. Ignoring.", value);
0a494f1f
LP
373 else
374 arg_default_std_output = r;
0a494f1f 375
059cb385
LP
376 } else if (streq(key, "systemd.default_standard_error") && value) {
377
378 r = exec_output_from_string(value);
fb472900 379 if (r < 0)
059cb385 380 log_warning("Failed to parse default standard error switch %s. Ignoring.", value);
0a494f1f
LP
381 else
382 arg_default_std_error = r;
9e7c5357 383
059cb385
LP
384 } else if (streq(key, "systemd.setenv") && value) {
385
386 if (env_assignment_is_valid(value)) {
e21fea24
KS
387 char **env;
388
059cb385 389 env = strv_env_set(arg_default_environment, value);
e21fea24
KS
390 if (env)
391 arg_default_environment = env;
392 else
279d3c9c 393 log_warning_errno(ENOMEM, "Setting environment variable '%s' failed, ignoring: %m", value);
e21fea24 394 } else
059cb385 395 log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
9e58ff9c 396
ee48dbd5
NC
397 } else if (streq(key, "systemd.machine_id") && value) {
398
399 r = set_machine_id(value);
400 if (r < 0)
401 log_warning("MachineID '%s' is not valid. Ignoring.", value);
402
059cb385 403 } else if (streq(key, "quiet") && !value) {
d7b15e0a 404
b6e2f329 405 if (arg_show_status == _SHOW_STATUS_UNSET)
d450b6f2 406 arg_show_status = SHOW_STATUS_AUTO;
059cb385
LP
407
408 } else if (streq(key, "debug") && !value) {
d7b15e0a 409
1de1c9c3
LP
410 /* Note that log_parse_environment() handles 'debug'
411 * too, and sets the log level to LOG_DEBUG. */
d7b15e0a 412
75f86906 413 if (detect_container() > 0)
b2103dcc 414 log_set_target(LOG_TARGET_CONSOLE);
059cb385
LP
415
416 } else if (!in_initrd() && !value) {
e2c9a131 417 const char *target;
f170852a
LP
418
419 /* SysV compatibility */
e2c9a131
EV
420 target = runlevel_to_target(key);
421 if (target)
422 return free_and_strdup(&arg_default_unit, target);
f170852a
LP
423 }
424
425 return 0;
426}
427
e8e581bf
ZJS
428#define DEFINE_SETTER(name, func, descr) \
429 static int name(const char *unit, \
430 const char *filename, \
431 unsigned line, \
432 const char *section, \
71a61510 433 unsigned section_line, \
e8e581bf
ZJS
434 const char *lvalue, \
435 int ltype, \
436 const char *rvalue, \
437 void *data, \
438 void *userdata) { \
439 \
440 int r; \
441 \
442 assert(filename); \
443 assert(lvalue); \
444 assert(rvalue); \
445 \
446 r = func(rvalue); \
447 if (r < 0) \
d1cefe0a
LP
448 log_syntax(unit, LOG_ERR, filename, line, r, \
449 "Invalid " descr "'%s': %m", \
450 rvalue); \
e8e581bf
ZJS
451 \
452 return 0; \
453 }
487393e9 454
e8e581bf
ZJS
455DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
456DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
457DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
458DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
487393e9 459
4d7213b2
LP
460static int config_parse_cpu_affinity2(
461 const char *unit,
462 const char *filename,
463 unsigned line,
464 const char *section,
465 unsigned section_line,
466 const char *lvalue,
467 int ltype,
468 const char *rvalue,
469 void *data,
470 void *userdata) {
487393e9 471
4b40bc38 472 _cleanup_cpu_free_ cpu_set_t *c = NULL;
f5c72b73 473 int ncpus;
487393e9 474
765d143b 475 ncpus = parse_cpu_set_and_warn(rvalue, &c, unit, filename, line, lvalue);
f5c72b73
FB
476 if (ncpus < 0)
477 return ncpus;
487393e9 478
f5c72b73
FB
479 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
480 log_warning("Failed to set CPU affinity: %m");
487393e9 481
487393e9
LP
482 return 0;
483}
484
4d7213b2
LP
485static int config_parse_show_status(
486 const char* unit,
487 const char *filename,
488 unsigned line,
489 const char *section,
490 unsigned section_line,
491 const char *lvalue,
492 int ltype,
493 const char *rvalue,
494 void *data,
495 void *userdata) {
496
497 int k;
498 ShowStatus *b = data;
499
500 assert(filename);
501 assert(lvalue);
502 assert(rvalue);
503 assert(data);
504
505 k = parse_show_status(rvalue, b);
506 if (k < 0) {
12ca818f 507 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
4d7213b2
LP
508 return 0;
509 }
510
511 return 0;
512}
513
b9e74c39
LP
514static int config_parse_crash_chvt(
515 const char* unit,
516 const char *filename,
517 unsigned line,
518 const char *section,
519 unsigned section_line,
520 const char *lvalue,
521 int ltype,
522 const char *rvalue,
523 void *data,
524 void *userdata) {
525
526 int r;
527
528 assert(filename);
529 assert(lvalue);
530 assert(rvalue);
b9e74c39
LP
531
532 r = parse_crash_chvt(rvalue);
533 if (r < 0) {
534 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
535 return 0;
536 }
537
538 return 0;
539}
540
e8e581bf
ZJS
541static int config_parse_join_controllers(const char *unit,
542 const char *filename,
543 unsigned line,
544 const char *section,
71a61510 545 unsigned section_line,
e8e581bf
ZJS
546 const char *lvalue,
547 int ltype,
548 const char *rvalue,
549 void *data,
550 void *userdata) {
0c85a4f3 551
3875c85b 552 const char *whole_rvalue = rvalue;
0c85a4f3 553 unsigned n = 0;
0c85a4f3
LP
554
555 assert(filename);
556 assert(lvalue);
557 assert(rvalue);
558
fe382237 559 arg_join_controllers = strv_free_free(arg_join_controllers);
0c85a4f3 560
d4ebeb4f
FB
561 for (;;) {
562 _cleanup_free_ char *word = NULL;
563 char **l;
564 int r;
0c85a4f3 565
d4ebeb4f 566 r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
3875c85b
FB
567 if (r < 0) {
568 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
d4ebeb4f 569 return r;
3875c85b 570 }
d4ebeb4f
FB
571 if (r == 0)
572 break;
0c85a4f3 573
d4ebeb4f 574 l = strv_split(word, ",");
1592ec21 575 if (!l)
aa8aeac0 576 return log_oom();
0c85a4f3
LP
577 strv_uniq(l);
578
579 if (strv_length(l) <= 1) {
580 strv_free(l);
581 continue;
582 }
583
584 if (!arg_join_controllers) {
585 arg_join_controllers = new(char**, 2);
586 if (!arg_join_controllers) {
587 strv_free(l);
14212119 588 return log_oom();
0c85a4f3
LP
589 }
590
591 arg_join_controllers[0] = l;
592 arg_join_controllers[1] = NULL;
593
594 n = 1;
595 } else {
596 char ***a;
597 char ***t;
598
599 t = new0(char**, n+2);
600 if (!t) {
601 strv_free(l);
14212119 602 return log_oom();
0c85a4f3
LP
603 }
604
605 n = 0;
606
607 for (a = arg_join_controllers; *a; a++) {
608
609 if (strv_overlap(*a, l)) {
e287086b 610 if (strv_extend_strv(&l, *a, false) < 0) {
0c85a4f3
LP
611 strv_free(l);
612 strv_free_free(t);
14212119 613 return log_oom();
0c85a4f3
LP
614 }
615
0c85a4f3
LP
616 } else {
617 char **c;
618
619 c = strv_copy(*a);
620 if (!c) {
621 strv_free(l);
622 strv_free_free(t);
14212119 623 return log_oom();
0c85a4f3
LP
624 }
625
626 t[n++] = c;
627 }
628 }
629
630 t[n++] = strv_uniq(l);
631
632 strv_free_free(arg_join_controllers);
633 arg_join_controllers = t;
634 }
635 }
d4ebeb4f 636 if (!isempty(rvalue))
12ca818f 637 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
0c85a4f3
LP
638
639 return 0;
640}
641
487393e9
LP
642static int parse_config_file(void) {
643
f975e971 644 const ConfigTableItem items[] = {
d3b1c508
LP
645 { "Manager", "LogLevel", config_parse_level2, 0, NULL },
646 { "Manager", "LogTarget", config_parse_target, 0, NULL },
647 { "Manager", "LogColor", config_parse_color, 0, NULL },
648 { "Manager", "LogLocation", config_parse_location, 0, NULL },
649 { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
b9e74c39
LP
650 { "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, NULL },
651 { "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, NULL },
d3b1c508 652 { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
b9e74c39 653 { "Manager", "CrashReboot", config_parse_bool, 0, &arg_crash_reboot },
d3b1c508 654 { "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status },
d3b1c508
LP
655 { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
656 { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
657 { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
658 { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
a103496c 659 { "Manager", "CapabilityBoundingSet", config_parse_capability_set, 0, &arg_capability_bounding_set },
89fffa27 660#ifdef HAVE_SECCOMP
d3b1c508 661 { "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs },
89fffa27 662#endif
d3b1c508 663 { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
bd8f585b 664 { "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec },
d3b1c508
LP
665 { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
666 { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
667 { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec },
668 { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec },
669 { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec },
670 { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval },
671 { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst },
672 { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
d0a7c5f6
LP
673 { "Manager", "DefaultLimitCPU", config_parse_limit, RLIMIT_CPU, arg_default_rlimit },
674 { "Manager", "DefaultLimitFSIZE", config_parse_limit, RLIMIT_FSIZE, arg_default_rlimit },
675 { "Manager", "DefaultLimitDATA", config_parse_limit, RLIMIT_DATA, arg_default_rlimit },
676 { "Manager", "DefaultLimitSTACK", config_parse_limit, RLIMIT_STACK, arg_default_rlimit },
677 { "Manager", "DefaultLimitCORE", config_parse_limit, RLIMIT_CORE, arg_default_rlimit },
678 { "Manager", "DefaultLimitRSS", config_parse_limit, RLIMIT_RSS, arg_default_rlimit },
679 { "Manager", "DefaultLimitNOFILE", config_parse_limit, RLIMIT_NOFILE, arg_default_rlimit },
680 { "Manager", "DefaultLimitAS", config_parse_limit, RLIMIT_AS, arg_default_rlimit },
681 { "Manager", "DefaultLimitNPROC", config_parse_limit, RLIMIT_NPROC, arg_default_rlimit },
682 { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, RLIMIT_MEMLOCK, arg_default_rlimit },
683 { "Manager", "DefaultLimitLOCKS", config_parse_limit, RLIMIT_LOCKS, arg_default_rlimit },
684 { "Manager", "DefaultLimitSIGPENDING", config_parse_limit, RLIMIT_SIGPENDING, arg_default_rlimit },
685 { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, RLIMIT_MSGQUEUE, arg_default_rlimit },
686 { "Manager", "DefaultLimitNICE", config_parse_limit, RLIMIT_NICE, arg_default_rlimit },
687 { "Manager", "DefaultLimitRTPRIO", config_parse_limit, RLIMIT_RTPRIO, arg_default_rlimit },
688 { "Manager", "DefaultLimitRTTIME", config_parse_limit, RLIMIT_RTTIME, arg_default_rlimit },
085afe36
LP
689 { "Manager", "DefaultCPUAccounting", config_parse_bool, 0, &arg_default_cpu_accounting },
690 { "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting },
691 { "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting },
03a7b521 692 { "Manager", "DefaultTasksAccounting", config_parse_bool, 0, &arg_default_tasks_accounting },
0af20ea2 693 { "Manager", "DefaultTasksMax", config_parse_tasks_max, 0, &arg_default_tasks_max },
d3b1c508 694 {}
487393e9
LP
695 };
696
1b907b5c 697 const char *fn, *conf_dirs_nulstr;
487393e9 698
75eb6154
LP
699 fn = arg_running_as == MANAGER_SYSTEM ?
700 PKGSYSCONFDIR "/system.conf" :
701 PKGSYSCONFDIR "/user.conf";
702
703 conf_dirs_nulstr = arg_running_as == MANAGER_SYSTEM ?
704 CONF_PATHS_NULSTR("systemd/system.conf.d") :
705 CONF_PATHS_NULSTR("systemd/user.conf.d");
706
36c16a7c
LP
707 config_parse_many(fn, conf_dirs_nulstr, "Manager\0", config_item_table_lookup, items, false, NULL);
708
709 /* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we used USEC_INFINITY
710 * like everywhere else. */
711 if (arg_default_timeout_start_usec <= 0)
712 arg_default_timeout_start_usec = USEC_INFINITY;
713 if (arg_default_timeout_stop_usec <= 0)
714 arg_default_timeout_stop_usec = USEC_INFINITY;
487393e9 715
487393e9
LP
716 return 0;
717}
718
06af2a04
TB
719static void manager_set_defaults(Manager *m) {
720
721 assert(m);
722
723 m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec;
724 m->default_std_output = arg_default_std_output;
725 m->default_std_error = arg_default_std_error;
726 m->default_timeout_start_usec = arg_default_timeout_start_usec;
727 m->default_timeout_stop_usec = arg_default_timeout_stop_usec;
728 m->default_restart_usec = arg_default_restart_usec;
729 m->default_start_limit_interval = arg_default_start_limit_interval;
730 m->default_start_limit_burst = arg_default_start_limit_burst;
731 m->default_cpu_accounting = arg_default_cpu_accounting;
732 m->default_blockio_accounting = arg_default_blockio_accounting;
733 m->default_memory_accounting = arg_default_memory_accounting;
03a7b521 734 m->default_tasks_accounting = arg_default_tasks_accounting;
0af20ea2 735 m->default_tasks_max = arg_default_tasks_max;
06af2a04
TB
736
737 manager_set_default_rlimits(m, arg_default_rlimit);
738 manager_environment_add(m, NULL, arg_default_environment);
739}
740
f170852a
LP
741static int parse_argv(int argc, char *argv[]) {
742
743 enum {
744 ARG_LOG_LEVEL = 0x100,
745 ARG_LOG_TARGET,
bbe63281
LP
746 ARG_LOG_COLOR,
747 ARG_LOG_LOCATION,
2f198e2f 748 ARG_UNIT,
edb9aaa8 749 ARG_SYSTEM,
af2d49f7 750 ARG_USER,
e537352b 751 ARG_TEST,
b87c2aa6 752 ARG_NO_PAGER,
9ba0bc4e 753 ARG_VERSION,
80876c20 754 ARG_DUMP_CONFIGURATION_ITEMS,
9e58ff9c 755 ARG_DUMP_CORE,
b9e74c39 756 ARG_CRASH_CHVT,
9e58ff9c 757 ARG_CRASH_SHELL,
b9e74c39 758 ARG_CRASH_REBOOT,
a16e1123 759 ARG_CONFIRM_SPAWN,
9e58ff9c 760 ARG_SHOW_STATUS,
4288f619 761 ARG_DESERIALIZE,
2660882b 762 ARG_SWITCHED_ROOT,
0a494f1f 763 ARG_DEFAULT_STD_OUTPUT,
ee48dbd5
NC
764 ARG_DEFAULT_STD_ERROR,
765 ARG_MACHINE_ID
f170852a
LP
766 };
767
768 static const struct option options[] = {
a16e1123
LP
769 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
770 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bbe63281
LP
771 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
772 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2f198e2f 773 { "unit", required_argument, NULL, ARG_UNIT },
edb9aaa8 774 { "system", no_argument, NULL, ARG_SYSTEM },
af2d49f7 775 { "user", no_argument, NULL, ARG_USER },
a16e1123 776 { "test", no_argument, NULL, ARG_TEST },
b87c2aa6 777 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a16e1123 778 { "help", no_argument, NULL, 'h' },
9ba0bc4e 779 { "version", no_argument, NULL, ARG_VERSION },
a16e1123 780 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
a5d87bf0 781 { "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
b9e74c39 782 { "crash-chvt", required_argument, NULL, ARG_CRASH_CHVT },
a5d87bf0 783 { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
b9e74c39 784 { "crash-reboot", optional_argument, NULL, ARG_CRASH_REBOOT },
a5d87bf0 785 { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
6e98720f 786 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
a16e1123 787 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
2660882b 788 { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
0a494f1f
LP
789 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
790 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
ee48dbd5 791 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
fb472900 792 {}
f170852a
LP
793 };
794
795 int c, r;
796
797 assert(argc >= 1);
798 assert(argv);
799
b770165a
LP
800 if (getpid() == 1)
801 opterr = 0;
802
099663ff 803 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
f170852a
LP
804
805 switch (c) {
806
807 case ARG_LOG_LEVEL:
fb472900
ZJS
808 r = log_set_max_level_from_string(optarg);
809 if (r < 0) {
f170852a
LP
810 log_error("Failed to parse log level %s.", optarg);
811 return r;
812 }
813
814 break;
815
816 case ARG_LOG_TARGET:
fb472900
ZJS
817 r = log_set_target_from_string(optarg);
818 if (r < 0) {
f170852a
LP
819 log_error("Failed to parse log target %s.", optarg);
820 return r;
821 }
822
823 break;
824
bbe63281
LP
825 case ARG_LOG_COLOR:
826
d0b170c8 827 if (optarg) {
fb472900
ZJS
828 r = log_show_color_from_string(optarg);
829 if (r < 0) {
d0b170c8
LP
830 log_error("Failed to parse log color setting %s.", optarg);
831 return r;
832 }
833 } else
834 log_show_color(true);
bbe63281
LP
835
836 break;
837
838 case ARG_LOG_LOCATION:
d0b170c8 839 if (optarg) {
fb472900
ZJS
840 r = log_show_location_from_string(optarg);
841 if (r < 0) {
d0b170c8
LP
842 log_error("Failed to parse log location setting %s.", optarg);
843 return r;
844 }
845 } else
846 log_show_location(true);
bbe63281
LP
847
848 break;
849
0a494f1f 850 case ARG_DEFAULT_STD_OUTPUT:
fb472900
ZJS
851 r = exec_output_from_string(optarg);
852 if (r < 0) {
0a494f1f
LP
853 log_error("Failed to parse default standard output setting %s.", optarg);
854 return r;
855 } else
856 arg_default_std_output = r;
857 break;
858
859 case ARG_DEFAULT_STD_ERROR:
fb472900
ZJS
860 r = exec_output_from_string(optarg);
861 if (r < 0) {
0a494f1f
LP
862 log_error("Failed to parse default standard error output setting %s.", optarg);
863 return r;
864 } else
865 arg_default_std_error = r;
866 break;
867
2f198e2f 868 case ARG_UNIT:
f170852a 869
e6e242ad 870 r = free_and_strdup(&arg_default_unit, optarg);
23bbb0de
MS
871 if (r < 0)
872 return log_error_errno(r, "Failed to set default unit %s: %m", optarg);
f170852a
LP
873
874 break;
875
edb9aaa8 876 case ARG_SYSTEM:
b2c23da8 877 arg_running_as = MANAGER_SYSTEM;
edb9aaa8 878 break;
a5dab5ce 879
af2d49f7 880 case ARG_USER:
b2c23da8 881 arg_running_as = MANAGER_USER;
a5dab5ce 882 break;
a5dab5ce 883
e965d56d 884 case ARG_TEST:
fa0f4d8a 885 arg_action = ACTION_TEST;
b87c2aa6
ZJS
886 if (arg_no_pager < 0)
887 arg_no_pager = true;
888 break;
889
890 case ARG_NO_PAGER:
891 arg_no_pager = true;
e965d56d
LP
892 break;
893
9ba0bc4e
ZJS
894 case ARG_VERSION:
895 arg_action = ACTION_VERSION;
896 break;
897
e537352b 898 case ARG_DUMP_CONFIGURATION_ITEMS:
fa0f4d8a 899 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
e537352b
LP
900 break;
901
9e58ff9c 902 case ARG_DUMP_CORE:
b9e74c39
LP
903 if (!optarg)
904 arg_dump_core = true;
905 else {
906 r = parse_boolean(optarg);
907 if (r < 0)
908 return log_error_errno(r, "Failed to parse dump core boolean: %s", optarg);
909 arg_dump_core = r;
a5d87bf0 910 }
b9e74c39
LP
911 break;
912
913 case ARG_CRASH_CHVT:
914 r = parse_crash_chvt(optarg);
915 if (r < 0)
916 return log_error_errno(r, "Failed to parse crash virtual terminal index: %s", optarg);
9e58ff9c
LP
917 break;
918
919 case ARG_CRASH_SHELL:
b9e74c39
LP
920 if (!optarg)
921 arg_crash_shell = true;
922 else {
923 r = parse_boolean(optarg);
924 if (r < 0)
925 return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
926 arg_crash_shell = r;
927 }
928 break;
929
930 case ARG_CRASH_REBOOT:
931 if (!optarg)
932 arg_crash_reboot = true;
933 else {
934 r = parse_boolean(optarg);
935 if (r < 0)
936 return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
937 arg_crash_reboot = r;
a5d87bf0 938 }
9e58ff9c
LP
939 break;
940
80876c20 941 case ARG_CONFIRM_SPAWN:
a5d87bf0
LP
942 r = optarg ? parse_boolean(optarg) : 1;
943 if (r < 0) {
944 log_error("Failed to parse confirm spawn boolean %s.", optarg);
945 return r;
946 }
947 arg_confirm_spawn = r;
80876c20
LP
948 break;
949
9e58ff9c 950 case ARG_SHOW_STATUS:
d450b6f2
ZJS
951 if (optarg) {
952 r = parse_show_status(optarg, &arg_show_status);
953 if (r < 0) {
954 log_error("Failed to parse show status boolean %s.", optarg);
955 return r;
956 }
957 } else
958 arg_show_status = SHOW_STATUS_YES;
6e98720f 959 break;
a5d87bf0 960
a16e1123
LP
961 case ARG_DESERIALIZE: {
962 int fd;
963 FILE *f;
964
01e10de3
LP
965 r = safe_atoi(optarg, &fd);
966 if (r < 0 || fd < 0) {
a16e1123 967 log_error("Failed to parse deserialize option %s.", optarg);
b9e74c39 968 return -EINVAL;
a16e1123
LP
969 }
970
b9e74c39 971 (void) fd_cloexec(fd, true);
01e10de3
LP
972
973 f = fdopen(fd, "r");
4a62c710
MS
974 if (!f)
975 return log_error_errno(errno, "Failed to open serialization fd: %m");
a16e1123 976
74ca738f 977 safe_fclose(arg_serialization);
d3b1c508 978 arg_serialization = f;
a16e1123
LP
979
980 break;
981 }
982
2660882b 983 case ARG_SWITCHED_ROOT:
bf4df7c3 984 arg_switched_root = true;
d03bc1b8
HH
985 break;
986
ee48dbd5
NC
987 case ARG_MACHINE_ID:
988 r = set_machine_id(optarg);
989 if (r < 0) {
990 log_error("MachineID '%s' is not valid.", optarg);
991 return r;
992 }
993 break;
994
f170852a 995 case 'h':
fa0f4d8a 996 arg_action = ACTION_HELP;
b87c2aa6
ZJS
997 if (arg_no_pager < 0)
998 arg_no_pager = true;
f170852a
LP
999 break;
1000
1d2e23ab
LP
1001 case 'D':
1002 log_set_max_level(LOG_DEBUG);
1003 break;
1004
099663ff
LP
1005 case 'b':
1006 case 's':
1007 case 'z':
1008 /* Just to eat away the sysvinit kernel
1009 * cmdline args without getopt() error
1010 * messages that we'll parse in
1011 * parse_proc_cmdline_word() or ignore. */
f170852a 1012
099663ff 1013 case '?':
601185b4 1014 if (getpid() != 1)
099663ff 1015 return -EINVAL;
601185b4
ZJS
1016 else
1017 return 0;
099663ff 1018
601185b4
ZJS
1019 default:
1020 assert_not_reached("Unhandled option code.");
f170852a
LP
1021 }
1022
d821e6d6
LP
1023 if (optind < argc && getpid() != 1) {
1024 /* Hmm, when we aren't run as init system
1025 * let's complain about excess arguments */
1026
1027 log_error("Excess arguments.");
1028 return -EINVAL;
1029 }
1030
f170852a
LP
1031 return 0;
1032}
1033
1034static int help(void) {
1035
2e33c433 1036 printf("%s [OPTIONS...]\n\n"
af2d49f7 1037 "Starts up and maintains the system or user services.\n\n"
e537352b 1038 " -h --help Show this help\n"
e537352b 1039 " --test Determine startup sequence, dump it and exit\n"
b87c2aa6 1040 " --no-pager Do not pipe output into a pager\n"
80876c20 1041 " --dump-configuration-items Dump understood unit configuration items\n"
9e58ff9c 1042 " --unit=UNIT Set default unit\n"
edb9aaa8 1043 " --system Run a system instance, even if PID != 1\n"
af2d49f7 1044 " --user Run a user instance\n"
b9e74c39
LP
1045 " --dump-core[=BOOL] Dump core on crash\n"
1046 " --crash-vt=NR Change to specified VT on crash\n"
1047 " --crash-reboot[=BOOL] Reboot on crash\n"
1048 " --crash-shell[=BOOL] Run shell on crash\n"
1049 " --confirm-spawn[=BOOL] Ask for confirmation when spawning processes\n"
1050 " --show-status[=BOOL] Show status updates on the console during bootup\n"
c1dc6153 1051 " --log-target=TARGET Set log target (console, journal, kmsg, journal-or-kmsg, null)\n"
9e58ff9c 1052 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
b9e74c39
LP
1053 " --log-color[=BOOL] Highlight important log messages\n"
1054 " --log-location[=BOOL] Include code location in log messages\n"
0a494f1f
LP
1055 " --default-standard-output= Set default standard output for services\n"
1056 " --default-standard-error= Set default standard error output for services\n",
5b6319dc 1057 program_invocation_short_name);
f170852a
LP
1058
1059 return 0;
1060}
1061
b3680f49 1062static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
48b90859
LP
1063 _cleanup_fdset_free_ FDSet *fds = NULL;
1064 _cleanup_fclose_ FILE *f = NULL;
a16e1123
LP
1065 int r;
1066
1067 assert(m);
1068 assert(_f);
1069 assert(_fds);
1070
6b78f9b4 1071 r = manager_open_serialization(m, &f);
48b90859
LP
1072 if (r < 0)
1073 return log_error_errno(r, "Failed to create serialization file: %m");
a16e1123 1074
71445ae7
LP
1075 /* Make sure nothing is really destructed when we shut down */
1076 m->n_reloading ++;
718db961 1077 bus_manager_send_reloading(m, true);
71445ae7 1078
6b78f9b4 1079 fds = fdset_new();
48b90859
LP
1080 if (!fds)
1081 return log_oom();
a16e1123 1082
b3680f49 1083 r = manager_serialize(m, f, fds, switching_root);
48b90859
LP
1084 if (r < 0)
1085 return log_error_errno(r, "Failed to serialize state: %m");
a16e1123 1086
48b90859
LP
1087 if (fseeko(f, 0, SEEK_SET) == (off_t) -1)
1088 return log_error_errno(errno, "Failed to rewind serialization fd: %m");
a16e1123 1089
6b78f9b4 1090 r = fd_cloexec(fileno(f), false);
48b90859
LP
1091 if (r < 0)
1092 return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization: %m");
a16e1123 1093
6b78f9b4 1094 r = fdset_cloexec(fds, false);
48b90859
LP
1095 if (r < 0)
1096 return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization fds: %m");
a16e1123
LP
1097
1098 *_f = f;
1099 *_fds = fds;
1100
48b90859
LP
1101 f = NULL;
1102 fds = NULL;
a16e1123 1103
48b90859 1104 return 0;
a16e1123
LP
1105}
1106
4096d6f5
LP
1107static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
1108 struct rlimit nl;
1109 int r;
1110
1111 assert(saved_rlimit);
1112
1113 /* Save the original RLIMIT_NOFILE so that we can reset it
1114 * later when transitioning from the initrd to the main
1115 * systemd or suchlike. */
4a62c710
MS
1116 if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0)
1117 return log_error_errno(errno, "Reading RLIMIT_NOFILE failed: %m");
4096d6f5
LP
1118
1119 /* Make sure forked processes get the default kernel setting */
1120 if (!arg_default_rlimit[RLIMIT_NOFILE]) {
1121 struct rlimit *rl;
1122
1123 rl = newdup(struct rlimit, saved_rlimit, 1);
1124 if (!rl)
1125 return log_oom();
1126
1127 arg_default_rlimit[RLIMIT_NOFILE] = rl;
1128 }
1129
1130 /* Bump up the resource limit for ourselves substantially */
1131 nl.rlim_cur = nl.rlim_max = 64*1024;
1132 r = setrlimit_closest(RLIMIT_NOFILE, &nl);
23bbb0de
MS
1133 if (r < 0)
1134 return log_error_errno(r, "Setting RLIMIT_NOFILE failed: %m");
4096d6f5
LP
1135
1136 return 0;
1137}
1138
80758717 1139static void test_usr(void) {
80758717 1140
ed1c99fc 1141 /* Check that /usr is not a separate fs */
80758717 1142
871c44a7
LP
1143 if (dir_is_empty("/usr") <= 0)
1144 return;
1145
8b173b5e 1146 log_warning("/usr appears to be on its own filesystem and is not already mounted. This is not a supported setup. "
871c44a7
LP
1147 "Some things will probably break (sometimes even silently) in mysterious ways. "
1148 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1149}
1150
a07fdfa3
LP
1151static int initialize_join_controllers(void) {
1152 /* By default, mount "cpu" + "cpuacct" together, and "net_cls"
1153 * + "net_prio". We'd like to add "cpuset" to the mix, but
f131770b 1154 * "cpuset" doesn't really work for groups with no initialized
a07fdfa3
LP
1155 * attributes. */
1156
1157 arg_join_controllers = new(char**, 3);
1158 if (!arg_join_controllers)
1159 return -ENOMEM;
1160
1161 arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
fe382237
LP
1162 if (!arg_join_controllers[0])
1163 goto oom;
a6b26d90 1164
fe382237
LP
1165 arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL);
1166 if (!arg_join_controllers[1])
1167 goto oom;
a07fdfa3 1168
fe382237 1169 arg_join_controllers[2] = NULL;
a07fdfa3 1170 return 0;
fe382237
LP
1171
1172oom:
1173 arg_join_controllers = strv_free_free(arg_join_controllers);
1174 return -ENOMEM;
a07fdfa3
LP
1175}
1176
d3b1c508
LP
1177static int enforce_syscall_archs(Set *archs) {
1178#ifdef HAVE_SECCOMP
1179 scmp_filter_ctx *seccomp;
1180 Iterator i;
1181 void *id;
1182 int r;
1183
1184 seccomp = seccomp_init(SCMP_ACT_ALLOW);
1185 if (!seccomp)
1186 return log_oom();
1187
1188 SET_FOREACH(id, arg_syscall_archs, i) {
1189 r = seccomp_arch_add(seccomp, PTR_TO_UINT32(id) - 1);
1190 if (r == -EEXIST)
1191 continue;
1192 if (r < 0) {
da927ba9 1193 log_error_errno(r, "Failed to add architecture to seccomp: %m");
d3b1c508
LP
1194 goto finish;
1195 }
1196 }
1197
8a8bf3c0
LP
1198 r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0);
1199 if (r < 0) {
da927ba9 1200 log_error_errno(r, "Failed to unset NO_NEW_PRIVS: %m");
8a8bf3c0
LP
1201 goto finish;
1202 }
1203
d3b1c508
LP
1204 r = seccomp_load(seccomp);
1205 if (r < 0)
da927ba9 1206 log_error_errno(r, "Failed to add install architecture seccomp: %m");
d3b1c508
LP
1207
1208finish:
1209 seccomp_release(seccomp);
1210 return r;
1211#else
1212 return 0;
1213#endif
1214}
1215
b6e2f329
LP
1216static int status_welcome(void) {
1217 _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
1218 int r;
1219
1220 r = parse_env_file("/etc/os-release", NEWLINE,
1221 "PRETTY_NAME", &pretty_name,
1222 "ANSI_COLOR", &ansi_color,
1223 NULL);
ece174c5 1224 if (r == -ENOENT)
5ae4d543
LP
1225 r = parse_env_file("/usr/lib/os-release", NEWLINE,
1226 "PRETTY_NAME", &pretty_name,
1227 "ANSI_COLOR", &ansi_color,
1228 NULL);
b6e2f329
LP
1229
1230 if (r < 0 && r != -ENOENT)
da927ba9 1231 log_warning_errno(r, "Failed to read os-release file: %m");
b6e2f329
LP
1232
1233 return status_printf(NULL, false, false,
1234 "\nWelcome to \x1B[%sm%s\x1B[0m!\n",
1235 isempty(ansi_color) ? "1" : ansi_color,
1236 isempty(pretty_name) ? "Linux" : pretty_name);
1237}
1238
fdd25311
LP
1239static int write_container_id(void) {
1240 const char *c;
19854865 1241 int r;
fdd25311
LP
1242
1243 c = getenv("container");
1244 if (isempty(c))
1245 return 0;
1246
19854865
LP
1247 r = write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE);
1248 if (r < 0)
f1f849b0 1249 return log_warning_errno(r, "Failed to write /run/systemd/container, ignoring: %m");
19854865
LP
1250
1251 return 1;
1252}
1253
1254static int bump_unix_max_dgram_qlen(void) {
1255 _cleanup_free_ char *qlen = NULL;
1256 unsigned long v;
1257 int r;
1258
1259 /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel
1260 * default of 16 is simply too low. We set the value really
1261 * really early during boot, so that it is actually applied to
1262 * all our sockets, including the $NOTIFY_SOCKET one. */
1263
1264 r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen);
1265 if (r < 0)
1266 return log_warning_errno(r, "Failed to read AF_UNIX datagram queue length, ignoring: %m");
1267
1268 r = safe_atolu(qlen, &v);
1269 if (r < 0)
1270 return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length, ignoring: %m");
1271
1272 if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN)
1273 return 0;
1274
1275 qlen = mfree(qlen);
1276 if (asprintf(&qlen, "%lu\n", DEFAULT_UNIX_MAX_DGRAM_QLEN) < 0)
1277 return log_oom();
1278
1279 r = write_string_file("/proc/sys/net/unix/max_dgram_qlen", qlen, 0);
1280 if (r < 0)
1281 return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
1282 "Failed to bump AF_UNIX datagram queue length, ignoring: %m");
1283
1284 return 1;
fdd25311
LP
1285}
1286
60918275
LP
1287int main(int argc, char *argv[]) {
1288 Manager *m = NULL;
22f4096c 1289 int r, retval = EXIT_FAILURE;
9d76d730
LP
1290 usec_t before_startup, after_startup;
1291 char timespan[FORMAT_TIMESPAN_MAX];
a16e1123
LP
1292 FDSet *fds = NULL;
1293 bool reexecute = false;
b9080b03 1294 const char *shutdown_verb = NULL;
86caf095
LP
1295 dual_timestamp initrd_timestamp = DUAL_TIMESTAMP_NULL;
1296 dual_timestamp userspace_timestamp = DUAL_TIMESTAMP_NULL;
1297 dual_timestamp kernel_timestamp = DUAL_TIMESTAMP_NULL;
1298 dual_timestamp security_start_timestamp = DUAL_TIMESTAMP_NULL;
1299 dual_timestamp security_finish_timestamp = DUAL_TIMESTAMP_NULL;
5d6b1584 1300 static char systemd[] = "systemd";
2660882b 1301 bool skip_setup = false;
d3b1c508 1302 unsigned j;
0b3325e7 1303 bool loaded_policy = false;
e96d6be7 1304 bool arm_reboot_watchdog = false;
bf4df7c3 1305 bool queue_default_job = false;
5f5c2f38 1306 bool empty_etc = false;
41669317 1307 char *switch_root_dir = NULL, *switch_root_init = NULL;
86caf095 1308 struct rlimit saved_rlimit_nofile = RLIMIT_MAKE_CONST(0);
cb6531be 1309 const char *error_message = NULL;
27b14a22 1310
058dc6f3 1311#ifdef HAVE_SYSV_COMPAT
2cb1a60d 1312 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
35b8ca3a 1313 /* This is compatibility support for SysV, where
2cb1a60d
LP
1314 * calling init as a user is identical to telinit. */
1315
2cb1a60d 1316 execv(SYSTEMCTL_BINARY_PATH, argv);
56f64d95 1317 log_error_errno(errno, "Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
2cb1a60d
LP
1318 return 1;
1319 }
058dc6f3 1320#endif
2cb1a60d 1321
c3a170f3
HH
1322 dual_timestamp_from_monotonic(&kernel_timestamp, 0);
1323 dual_timestamp_get(&userspace_timestamp);
1324
0b3325e7
LP
1325 /* Determine if this is a reexecution or normal bootup. We do
1326 * the full command line parsing much later, so let's just
1327 * have a quick peek here. */
db813c2a
LP
1328 if (strv_find(argv+1, "--deserialize"))
1329 skip_setup = true;
0b3325e7 1330
2660882b
LP
1331 /* If we have switched root, do all the special setup
1332 * things */
db813c2a
LP
1333 if (strv_find(argv+1, "--switched-root"))
1334 skip_setup = false;
d03bc1b8 1335
f3b6a3ed
LP
1336 /* If we get started via the /sbin/init symlink then we are
1337 called 'init'. After a subsequent reexecution we are then
1338 called 'systemd'. That is confusing, hence let's call us
1339 systemd right-away. */
f3b6a3ed
LP
1340 program_invocation_short_name = systemd;
1341 prctl(PR_SET_NAME, systemd);
5d6b1584 1342
9a0e6896
LP
1343 saved_argv = argv;
1344 saved_argc = argc;
f3b6a3ed 1345
2cc59dbf 1346 log_show_color(isatty(STDERR_FILENO) > 0);
c1dc6153 1347 log_set_upgrade_syslog_to_journal(true);
bbe63281 1348
90dc8c2e
MG
1349 /* Disable the umask logic */
1350 if (getpid() == 1)
1351 umask(0);
1352
75f86906 1353 if (getpid() == 1 && detect_container() <= 0) {
4f8d551f 1354
a866073d 1355 /* Running outside of a container as PID 1 */
b2c23da8 1356 arg_running_as = MANAGER_SYSTEM;
a866073d
LP
1357 make_null_stdio();
1358 log_set_target(LOG_TARGET_KMSG);
1359 log_open();
1360
21bf2ab0 1361 if (in_initrd())
c3a170f3 1362 initrd_timestamp = userspace_timestamp;
c3ba6250 1363
2660882b 1364 if (!skip_setup) {
d723cd65
AK
1365 r = mount_setup_early();
1366 if (r < 0) {
1367 error_message = "Failed to early mount API filesystems";
1368 goto finish;
1369 }
c2e0d600 1370 dual_timestamp_get(&security_start_timestamp);
cb6531be
ZJS
1371 if (mac_selinux_setup(&loaded_policy) < 0) {
1372 error_message = "Failed to load SELinux policy";
0b3325e7 1373 goto finish;
cb6531be
ZJS
1374 } else if (ima_setup() < 0) {
1375 error_message = "Failed to load IMA policy";
81611586 1376 goto finish;
cb6531be
ZJS
1377 } else if (mac_smack_setup(&loaded_policy) < 0) {
1378 error_message = "Failed to load SMACK policy";
ffbd2c4d 1379 goto finish;
cb6531be 1380 }
c2e0d600 1381 dual_timestamp_get(&security_finish_timestamp);
81611586 1382 }
0b3325e7 1383
cb6531be
ZJS
1384 if (mac_selinux_init(NULL) < 0) {
1385 error_message = "Failed to initialize SELinux policy";
0ff4cdd9 1386 goto finish;
cb6531be 1387 }
7948c4df 1388
72edcff5 1389 if (!skip_setup) {
24efb112 1390 if (clock_is_localtime() > 0) {
0b3325e7 1391 int min;
7948c4df 1392
c264aeab
KS
1393 /*
1394 * The very first call of settimeofday() also does a time warp in the kernel.
1395 *
1396 * In the rtc-in-local time mode, we set the kernel's timezone, and rely on
1397 * external tools to take care of maintaining the RTC and do all adjustments.
1398 * This matches the behavior of Windows, which leaves the RTC alone if the
1399 * registry tells that the RTC runs in UTC.
1400 */
24efb112 1401 r = clock_set_timezone(&min);
0b3325e7 1402 if (r < 0)
da927ba9 1403 log_error_errno(r, "Failed to apply local time delta, ignoring: %m");
0b3325e7
LP
1404 else
1405 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
19e65613
KS
1406 } else if (!in_initrd()) {
1407 /*
c264aeab 1408 * Do a dummy very first call to seal the kernel's time warp magic.
19e65613
KS
1409 *
1410 * Do not call this this from inside the initrd. The initrd might not
1411 * carry /etc/adjtime with LOCAL, but the real system could be set up
1412 * that way. In such case, we need to delay the time-warp or the sealing
1413 * until we reach the real system.
c264aeab
KS
1414 *
1415 * Do no set the kernel's timezone. The concept of local time cannot
1416 * be supported reliably, the time will jump or be incorrect at every daylight
1417 * saving time change. All kernel local time concepts will be treated
1418 * as UTC that way.
19e65613 1419 */
021dd87b 1420 (void) clock_reset_timewarp();
72edcff5 1421 }
021dd87b
LP
1422
1423 r = clock_apply_epoch();
1424 if (r < 0)
1425 log_error_errno(r, "Current system time is before build time, but cannot correct: %m");
1426 else if (r > 0)
1427 log_info("System time before build time, advancing clock.");
72edcff5 1428 }
a866073d
LP
1429
1430 /* Set the default for later on, but don't actually
1431 * open the logs like this for now. Note that if we
1432 * are transitioning from the initrd there might still
1433 * be journal fd open, and we shouldn't attempt
1434 * opening that before we parsed /proc/cmdline which
1435 * might redirect output elsewhere. */
1436 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1437
1438 } else if (getpid() == 1) {
a866073d 1439 /* Running inside a container, as PID 1 */
b2c23da8 1440 arg_running_as = MANAGER_SYSTEM;
a866073d 1441 log_set_target(LOG_TARGET_CONSOLE);
99f09825 1442 log_close_console(); /* force reopen of /dev/console */
a866073d
LP
1443 log_open();
1444
1445 /* For the later on, see above... */
1446 log_set_target(LOG_TARGET_JOURNAL);
1447
c3a170f3
HH
1448 /* clear the kernel timestamp,
1449 * because we are in a container */
1450 kernel_timestamp.monotonic = 0ULL;
1451 kernel_timestamp.realtime = 0ULL;
a866073d 1452
c3a170f3 1453 } else {
a866073d 1454 /* Running as user instance */
b2c23da8 1455 arg_running_as = MANAGER_USER;
eeecf6e6 1456 log_set_target(LOG_TARGET_AUTO);
871e5809 1457 log_open();
c3a170f3
HH
1458
1459 /* clear the kernel timestamp,
1460 * because we are not PID 1 */
6513d561 1461 kernel_timestamp = DUAL_TIMESTAMP_NULL;
bbe63281 1462 }
a5dab5ce 1463
15a90032
LP
1464 if (getpid() == 1) {
1465 /* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour the limit)
1466 * will process core dumps for system services by default. */
1467 (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY));
1468
1469 /* But at the same time, turn off the core_pattern logic by default, so that no coredumps are stored
1470 * until the systemd-coredump tool is enabled via sysctl. */
1471 if (!skip_setup)
1472 (void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
1473 }
1474
0c85a4f3 1475 /* Initialize default unit */
e6e242ad 1476 r = free_and_strdup(&arg_default_unit, SPECIAL_DEFAULT_TARGET);
6afa301b 1477 if (r < 0) {
da927ba9 1478 log_emergency_errno(r, "Failed to set default unit %s: %m", SPECIAL_DEFAULT_TARGET);
cb6531be 1479 error_message = "Failed to set default unit";
f170852a 1480 goto finish;
14212119 1481 }
60918275 1482
a07fdfa3 1483 r = initialize_join_controllers();
cb6531be 1484 if (r < 0) {
ff9b60f3 1485 error_message = "Failed to initialize cgroup controllers";
0c85a4f3 1486 goto finish;
cb6531be 1487 }
0c85a4f3 1488
f170852a
LP
1489 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1490 * /proc/$PID/fd is available. */
c1dae1b3 1491 if (getpid() == 1) {
f84f9974
LP
1492
1493 /* Load the kernel modules early, so that we kdbus.ko is loaded before kdbusfs shall be mounted */
2e75e2a8
DM
1494 if (!skip_setup)
1495 kmod_setup();
2e75e2a8 1496
0c85a4f3 1497 r = mount_setup(loaded_policy);
cb6531be
ZJS
1498 if (r < 0) {
1499 error_message = "Failed to mount API filesystems";
8efe3c01 1500 goto finish;
cb6531be 1501 }
0c85a4f3 1502 }
4ade7963
LP
1503
1504 /* Reset all signal handlers. */
ce30c8dc
LP
1505 (void) reset_all_signal_handlers();
1506 (void) ignore_signals(SIGNALS_IGNORE, -1);
078e4539 1507
cb6531be
ZJS
1508 if (parse_config_file() < 0) {
1509 error_message = "Failed to parse config file";
487393e9 1510 goto finish;
cb6531be 1511 }
487393e9 1512
b2c23da8 1513 if (arg_running_as == MANAGER_SYSTEM) {
b5884878
LP
1514 r = parse_proc_cmdline(parse_proc_cmdline_item);
1515 if (r < 0)
da927ba9 1516 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
b5884878 1517 }
f170852a 1518
1de1c9c3
LP
1519 /* Note that this also parses bits from the kernel command
1520 * line, including "debug". */
f170852a
LP
1521 log_parse_environment();
1522
cb6531be
ZJS
1523 if (parse_argv(argc, argv) < 0) {
1524 error_message = "Failed to parse commandline arguments";
f170852a 1525 goto finish;
cb6531be 1526 }
f170852a 1527
6bae23a0
TB
1528 if (arg_action == ACTION_TEST &&
1529 geteuid() == 0) {
b5c6cf87
LP
1530 log_error("Don't run test mode as root.");
1531 goto finish;
1532 }
1533
b2c23da8 1534 if (arg_running_as == MANAGER_USER &&
6bae23a0
TB
1535 arg_action == ACTION_RUN &&
1536 sd_booted() <= 0) {
1537 log_error("Trying to run as user instance, but the system has not been booted with systemd.");
1538 goto finish;
1539 }
1540
b2c23da8 1541 if (arg_running_as == MANAGER_SYSTEM &&
fe783b03
LP
1542 arg_action == ACTION_RUN &&
1543 running_in_chroot() > 0) {
1544 log_error("Cannot be run in a chroot() environment.");
1545 goto finish;
1546 }
1547
0d8c31ff
ZJS
1548 if (arg_action == ACTION_TEST)
1549 skip_setup = true;
1550
b87c2aa6
ZJS
1551 pager_open_if_enabled();
1552
fa0f4d8a 1553 if (arg_action == ACTION_HELP) {
f170852a
LP
1554 retval = help();
1555 goto finish;
9ba0bc4e
ZJS
1556 } else if (arg_action == ACTION_VERSION) {
1557 retval = version();
1558 goto finish;
fa0f4d8a 1559 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
e537352b 1560 unit_dump_config_items(stdout);
22f4096c 1561 retval = EXIT_SUCCESS;
e537352b 1562 goto finish;
fa0f4d8a 1563 } else if (arg_action == ACTION_DONE) {
22f4096c 1564 retval = EXIT_SUCCESS;
4288f619 1565 goto finish;
f170852a
LP
1566 }
1567
b2c23da8 1568 if (arg_running_as == MANAGER_USER &&
8be28fb1
KS
1569 !getenv("XDG_RUNTIME_DIR")) {
1570 log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
1571 goto finish;
1572 }
1573
fa0f4d8a 1574 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
f170852a 1575
871e5809
LP
1576 /* Close logging fds, in order not to confuse fdset below */
1577 log_close();
1578
a16e1123 1579 /* Remember open file descriptors for later deserialization */
01e10de3
LP
1580 r = fdset_new_fill(&fds);
1581 if (r < 0) {
da927ba9 1582 log_emergency_errno(r, "Failed to allocate fd set: %m");
cb6531be 1583 error_message = "Failed to allocate fd set";
01e10de3
LP
1584 goto finish;
1585 } else
1586 fdset_cloexec(fds, true);
a16e1123 1587
d3b1c508
LP
1588 if (arg_serialization)
1589 assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0);
a16e1123 1590
b2c23da8 1591 if (arg_running_as == MANAGER_SYSTEM)
80876c20
LP
1592 /* Become a session leader if we aren't one yet. */
1593 setsid();
4ade7963 1594
befb5b6a 1595 /* Move out of the way, so that we won't block unmounts */
d250afe7 1596 assert_se(chdir("/") == 0);
befb5b6a 1597
2146621b
LP
1598 /* Reset the console, but only if this is really init and we
1599 * are freshly booted */
b2c23da8 1600 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
56d96fc0
LP
1601
1602 /* If we are init, we connect stdin/stdout/stderr to
1603 * /dev/null and make sure we don't have a controlling
1604 * tty. */
1605 release_terminal();
1606
1607 if (getpid() == 1 && !skip_setup)
1608 console_setup();
1609 }
4ade7963 1610
18149b9f 1611 /* Open the logging devices, if possible and necessary */
843d2643 1612 log_open();
4ade7963 1613
b6e2f329
LP
1614 if (arg_show_status == _SHOW_STATUS_UNSET)
1615 arg_show_status = SHOW_STATUS_YES;
1616
5373d602
LP
1617 /* Make sure we leave a core dump without panicing the
1618 * kernel. */
ab422445 1619 if (getpid() == 1) {
4fc935ca 1620 install_crash_handler();
97c4f35c 1621
0c85a4f3
LP
1622 r = mount_cgroup_controllers(arg_join_controllers);
1623 if (r < 0)
1624 goto finish;
1625 }
1626
b2c23da8 1627 if (arg_running_as == MANAGER_SYSTEM) {
75f86906 1628 int v;
c20f5ac7 1629
0d8c31ff
ZJS
1630 log_info(PACKAGE_STRING " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
1631 arg_action == ACTION_TEST ? "test " : "" );
c20f5ac7 1632
75f86906
LP
1633 v = detect_virtualization();
1634 if (v > 0)
1635 log_info("Detected virtualization %s.", virtualization_to_string(v));
c20f5ac7 1636
fdd25311
LP
1637 write_container_id();
1638
d3f86679 1639 log_info("Detected architecture %s.", architecture_to_string(uname_architecture()));
d9d93745 1640
26a1efdf
LP
1641 if (in_initrd())
1642 log_info("Running in initial RAM disk.");
1643
3408ba01
LP
1644 /* Let's check whether /etc is already populated. We
1645 * don't actually really check for that, but use
1646 * /etc/machine-id as flag file. This allows container
1647 * managers and installers to provision a couple of
1648 * files already. If the container manager wants to
1649 * provision the machine ID itself it should pass
ee33e53a 1650 * $container_uuid to PID 1. */
3408ba01 1651
baa1bdf7 1652 empty_etc = access("/etc/machine-id", F_OK) < 0;
5f5c2f38
LP
1653 if (empty_etc)
1654 log_info("Running with unpopulated /etc.");
d8160f21 1655 } else {
1f97091d
LP
1656 _cleanup_free_ char *t;
1657
1658 t = uid_to_name(getuid());
0d8c31ff
ZJS
1659 log_debug(PACKAGE_STRING " running in %suser mode for user "UID_FMT"/%s. (" SYSTEMD_FEATURES ")",
1660 arg_action == ACTION_TEST ? " test" : "", getuid(), t);
d8160f21 1661 }
a5dab5ce 1662
b2c23da8 1663 if (arg_running_as == MANAGER_SYSTEM && !skip_setup) {
031886ed 1664 if (arg_show_status > 0)
888c6216
LP
1665 status_welcome();
1666
888c6216 1667 hostname_setup();
ee48dbd5 1668 machine_id_setup(NULL, arg_machine_id);
888c6216 1669 loopback_setup();
19854865 1670 bump_unix_max_dgram_qlen();
490aed58 1671
80758717 1672 test_usr();
af5bc85d 1673 }
302e8c4c 1674
b2c23da8 1675 if (arg_running_as == MANAGER_SYSTEM && arg_runtime_watchdog > 0)
e96d6be7
LP
1676 watchdog_set_timeout(&arg_runtime_watchdog);
1677
3a43da28 1678 if (arg_timer_slack_nsec != NSEC_INFINITY)
aa0f64ac 1679 if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0)
56f64d95 1680 log_error_errno(errno, "Failed to adjust timer slack: %m");
aa0f64ac 1681
a103496c
IP
1682 if (!cap_test_all(arg_capability_bounding_set)) {
1683 r = capability_bounding_set_drop_usermode(arg_capability_bounding_set);
ec8927ca 1684 if (r < 0) {
da927ba9 1685 log_emergency_errno(r, "Failed to drop capability bounding set of usermode helpers: %m");
cb6531be 1686 error_message = "Failed to drop capability bounding set of usermode helpers";
ec8927ca
LP
1687 goto finish;
1688 }
a103496c 1689 r = capability_bounding_set_drop(arg_capability_bounding_set, true);
939b8f14 1690 if (r < 0) {
da927ba9 1691 log_emergency_errno(r, "Failed to drop capability bounding set: %m");
cb6531be 1692 error_message = "Failed to drop capability bounding set";
939b8f14
LP
1693 goto finish;
1694 }
ec8927ca
LP
1695 }
1696
d3b1c508
LP
1697 if (arg_syscall_archs) {
1698 r = enforce_syscall_archs(arg_syscall_archs);
cb6531be
ZJS
1699 if (r < 0) {
1700 error_message = "Failed to set syscall architectures";
d3b1c508 1701 goto finish;
cb6531be 1702 }
d3b1c508
LP
1703 }
1704
61b9b203 1705 if (arg_running_as == MANAGER_USER)
d4447f4d 1706 /* Become reaper of our children */
61b9b203 1707 if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)
56f64d95 1708 log_warning_errno(errno, "Failed to make us a subreaper: %m");
d4447f4d 1709
b2c23da8 1710 if (arg_running_as == MANAGER_SYSTEM) {
4096d6f5
LP
1711 bump_rlimit_nofile(&saved_rlimit_nofile);
1712
5f5c2f38 1713 if (empty_etc) {
304b3079 1714 r = unit_file_preset_all(UNIT_FILE_SYSTEM, false, NULL, UNIT_FILE_PRESET_ENABLE_ONLY, false, NULL, 0);
5f5c2f38 1715 if (r < 0)
287a0de4 1716 log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r, "Failed to populate /etc with preset unit settings, ignoring: %m");
5f5c2f38
LP
1717 else
1718 log_info("Populated /etc with preset unit settings.");
1719 }
1720 }
1721
0d8c31ff 1722 r = manager_new(arg_running_as, arg_action == ACTION_TEST, &m);
e96d6be7 1723 if (r < 0) {
da927ba9 1724 log_emergency_errno(r, "Failed to allocate manager object: %m");
cb6531be 1725 error_message = "Failed to allocate manager object";
60918275
LP
1726 goto finish;
1727 }
1728
9e58ff9c 1729 m->confirm_spawn = arg_confirm_spawn;
e96d6be7
LP
1730 m->runtime_watchdog = arg_runtime_watchdog;
1731 m->shutdown_watchdog = arg_shutdown_watchdog;
c3a170f3
HH
1732 m->userspace_timestamp = userspace_timestamp;
1733 m->kernel_timestamp = kernel_timestamp;
c3a170f3 1734 m->initrd_timestamp = initrd_timestamp;
c2e0d600
TA
1735 m->security_start_timestamp = security_start_timestamp;
1736 m->security_finish_timestamp = security_finish_timestamp;
9e58ff9c 1737
06af2a04 1738 manager_set_defaults(m);
27d340c7 1739 manager_set_show_status(m, arg_show_status);
e2680723 1740 manager_set_first_boot(m, empty_etc);
27d340c7 1741
bf4df7c3 1742 /* Remember whether we should queue the default job */
d3b1c508 1743 queue_default_job = !arg_serialization || arg_switched_root;
bf4df7c3 1744
9d76d730
LP
1745 before_startup = now(CLOCK_MONOTONIC);
1746
d3b1c508 1747 r = manager_startup(m, arg_serialization, fds);
e96d6be7 1748 if (r < 0)
da927ba9 1749 log_error_errno(r, "Failed to fully start up daemon: %m");
a16e1123 1750
bf4df7c3
LP
1751 /* This will close all file descriptors that were opened, but
1752 * not claimed by any unit. */
2feceb5e 1753 fds = fdset_free(fds);
f50e0a01 1754
74ca738f 1755 arg_serialization = safe_fclose(arg_serialization);
bf4df7c3
LP
1756
1757 if (queue_default_job) {
4afd3348 1758 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1c27d3f3 1759 Unit *target = NULL;
bacbccb7 1760 Job *default_unit_job;
398ef8ba 1761
fa0f4d8a 1762 log_debug("Activating default unit: %s", arg_default_unit);
a16e1123 1763
e96d6be7 1764 r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
718db961
LP
1765 if (r < 0)
1766 log_error("Failed to load default target: %s", bus_error_message(&error, r));
1767 else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND)
c33b3297 1768 log_error_errno(target->load_error, "Failed to load default target: %m");
ac155bb8 1769 else if (target->load_state == UNIT_MASKED)
6daf4f90 1770 log_error("Default target masked.");
27b14a22 1771
ac155bb8 1772 if (!target || target->load_state != UNIT_LOADED) {
a16e1123 1773 log_info("Trying to load rescue target...");
1c27d3f3 1774
e96d6be7
LP
1775 r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
1776 if (r < 0) {
4104970e 1777 log_emergency("Failed to load rescue target: %s", bus_error_message(&error, r));
cb6531be 1778 error_message = "Failed to load rescue target";
a16e1123 1779 goto finish;
11ddb6f4 1780 } else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) {
c33b3297 1781 log_emergency_errno(target->load_error, "Failed to load rescue target: %m");
cb6531be 1782 error_message = "Failed to load rescue target";
1c27d3f3 1783 goto finish;
ac155bb8 1784 } else if (target->load_state == UNIT_MASKED) {
4104970e 1785 log_emergency("Rescue target masked.");
cb6531be 1786 error_message = "Rescue target masked";
00dc5d76 1787 goto finish;
a16e1123
LP
1788 }
1789 }
37d88da7 1790
ac155bb8 1791 assert(target->load_state == UNIT_LOADED);
00dc5d76 1792
fa0f4d8a 1793 if (arg_action == ACTION_TEST) {
40d50879 1794 printf("-> By units:\n");
a16e1123
LP
1795 manager_dump_units(m, stdout, "\t");
1796 }
1797
4bd29fe5 1798 r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, &error, &default_unit_job);
ab17a050 1799 if (r == -EPERM) {
718db961 1800 log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r));
ab17a050 1801
5022ce71
LP
1802 sd_bus_error_free(&error);
1803
4bd29fe5 1804 r = manager_add_job(m, JOB_START, target, JOB_REPLACE, &error, &default_unit_job);
ab17a050 1805 if (r < 0) {
4104970e 1806 log_emergency("Failed to start default target: %s", bus_error_message(&error, r));
cb6531be 1807 error_message = "Failed to start default target";
ab17a050
LP
1808 goto finish;
1809 }
1810 } else if (r < 0) {
4104970e 1811 log_emergency("Failed to isolate default target: %s", bus_error_message(&error, r));
cb6531be 1812 error_message = "Failed to isolate default target";
37d88da7
LP
1813 goto finish;
1814 }
ab17a050 1815
bacbccb7 1816 m->default_unit_job_id = default_unit_job->id;
60918275 1817
07672f49
LP
1818 after_startup = now(CLOCK_MONOTONIC);
1819 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1820 "Loaded units and determined initial transaction in %s.",
fdb14b7e 1821 format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 100 * USEC_PER_MSEC));
07672f49 1822
fa0f4d8a 1823 if (arg_action == ACTION_TEST) {
40d50879 1824 printf("-> By jobs:\n");
a16e1123 1825 manager_dump_jobs(m, stdout, "\t");
22f4096c 1826 retval = EXIT_SUCCESS;
a16e1123
LP
1827 goto finish;
1828 }
e965d56d 1829 }
d46de8a1 1830
a16e1123 1831 for (;;) {
e96d6be7
LP
1832 r = manager_loop(m);
1833 if (r < 0) {
da927ba9 1834 log_emergency_errno(r, "Failed to run main loop: %m");
cb6531be 1835 error_message = "Failed to run main loop";
a16e1123
LP
1836 goto finish;
1837 }
11dd41ce 1838
a16e1123 1839 switch (m->exit_code) {
e965d56d 1840
a16e1123 1841 case MANAGER_RELOAD:
e015090f 1842 log_info("Reloading.");
06af2a04
TB
1843
1844 r = parse_config_file();
1845 if (r < 0)
1846 log_error("Failed to parse config file.");
1847
1848 manager_set_defaults(m);
1849
e96d6be7
LP
1850 r = manager_reload(m);
1851 if (r < 0)
da927ba9 1852 log_error_errno(r, "Failed to reload: %m");
a16e1123 1853 break;
cea8e32e 1854
a16e1123 1855 case MANAGER_REEXECUTE:
664f88a7 1856
cb6531be 1857 if (prepare_reexecute(m, &arg_serialization, &fds, false) < 0) {
6b9af963 1858 error_message = "Failed to prepare for reexecution";
a16e1123 1859 goto finish;
cb6531be 1860 }
60918275 1861
a16e1123 1862 reexecute = true;
e015090f 1863 log_notice("Reexecuting.");
a16e1123
LP
1864 goto finish;
1865
664f88a7
LP
1866 case MANAGER_SWITCH_ROOT:
1867 /* Steal the switch root parameters */
41669317 1868 switch_root_dir = m->switch_root;
664f88a7
LP
1869 switch_root_init = m->switch_root_init;
1870 m->switch_root = m->switch_root_init = NULL;
1871
1872 if (!switch_root_init)
cb6531be 1873 if (prepare_reexecute(m, &arg_serialization, &fds, true) < 0) {
6b9af963 1874 error_message = "Failed to prepare for reexecution";
664f88a7 1875 goto finish;
cb6531be 1876 }
664f88a7
LP
1877
1878 reexecute = true;
1879 log_notice("Switching root.");
1880 goto finish;
1881
287419c1 1882 case MANAGER_EXIT:
97792515
LP
1883 retval = m->return_value;
1884
8ebfe0cb 1885 if (m->running_as == MANAGER_USER) {
8ebfe0cb
DH
1886 log_debug("Exit.");
1887 goto finish;
1888 }
1889
1890 /* fallthrough */
b9080b03
FF
1891 case MANAGER_REBOOT:
1892 case MANAGER_POWEROFF:
1893 case MANAGER_HALT:
1894 case MANAGER_KEXEC: {
1895 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
287419c1 1896 [MANAGER_EXIT] = "exit",
b9080b03
FF
1897 [MANAGER_REBOOT] = "reboot",
1898 [MANAGER_POWEROFF] = "poweroff",
1899 [MANAGER_HALT] = "halt",
1900 [MANAGER_KEXEC] = "kexec"
1901 };
1902
1903 assert_se(shutdown_verb = table[m->exit_code]);
e96d6be7 1904 arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
b9080b03
FF
1905
1906 log_notice("Shutting down.");
1907 goto finish;
1908 }
1909
a16e1123
LP
1910 default:
1911 assert_not_reached("Unknown exit code.");
1912 }
1913 }
f170852a 1914
60918275 1915finish:
b87c2aa6
ZJS
1916 pager_close();
1917
97792515 1918 if (m)
81f5fc2d 1919 arg_shutdown_watchdog = m->shutdown_watchdog;
fe382237 1920
06d8d842 1921 m = manager_free(m);
60918275 1922
1f6b4113 1923 for (j = 0; j < ELEMENTSOF(arg_default_rlimit); j++)
a1e58e8e 1924 arg_default_rlimit[j] = mfree(arg_default_rlimit[j]);
c93ff2e9 1925
97b11eed 1926 arg_default_unit = mfree(arg_default_unit);
fe382237 1927 arg_join_controllers = strv_free_free(arg_join_controllers);
97b11eed 1928 arg_default_environment = strv_free(arg_default_environment);
525d3cc7 1929 arg_syscall_archs = set_free(arg_syscall_archs);
d3b1c508 1930
cc56fafe 1931 mac_selinux_finish();
b2bb3dbe 1932
a16e1123 1933 if (reexecute) {
664f88a7 1934 const char **args;
e564a982 1935 unsigned i, args_size;
a16e1123 1936
664f88a7
LP
1937 /* Close and disarm the watchdog, so that the new
1938 * instance can reinitialize it, but doesn't get
1939 * rebooted while we do that */
1940 watchdog_close(true);
a16e1123 1941
4096d6f5
LP
1942 /* Reset the RLIMIT_NOFILE to the kernel default, so
1943 * that the new systemd can pass the kernel default to
1944 * its child processes */
1945 if (saved_rlimit_nofile.rlim_cur > 0)
92ca4cac 1946 (void) setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile);
4096d6f5 1947
41669317 1948 if (switch_root_dir) {
cee530bb
LP
1949 /* Kill all remaining processes from the
1950 * initrd, but don't wait for them, so that we
1951 * can handle the SIGCHLD for them after
1952 * deserializing. */
0bee65f0 1953 broadcast_signal(SIGTERM, false, true);
bd3fa1d2 1954
5a4bf02f
HH
1955 /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */
1956 r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE);
41669317 1957 if (r < 0)
da927ba9 1958 log_error_errno(r, "Failed to switch root, trying to continue: %m");
41669317 1959 }
a16e1123 1960
d03bc1b8 1961 args_size = MAX(6, argc+1);
e564a982 1962 args = newa(const char*, args_size);
a16e1123 1963
664f88a7 1964 if (!switch_root_init) {
5ffa8c81 1965 char sfd[DECIMAL_STR_MAX(int) + 1];
a16e1123 1966
664f88a7
LP
1967 /* First try to spawn ourselves with the right
1968 * path, and with full serialization. We do
1969 * this only if the user didn't specify an
1970 * explicit init to spawn. */
edb9aaa8 1971
d3b1c508 1972 assert(arg_serialization);
664f88a7 1973 assert(fds);
edb9aaa8 1974
5ffa8c81 1975 xsprintf(sfd, "%i", fileno(arg_serialization));
edb9aaa8 1976
664f88a7
LP
1977 i = 0;
1978 args[i++] = SYSTEMD_BINARY_PATH;
41669317 1979 if (switch_root_dir)
2660882b 1980 args[i++] = "--switched-root";
b2c23da8 1981 args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
664f88a7
LP
1982 args[i++] = "--deserialize";
1983 args[i++] = sfd;
1984 args[i++] = NULL;
edb9aaa8 1985
ce8aba56
KS
1986 /* do not pass along the environment we inherit from the kernel or initrd */
1987 if (switch_root_dir)
92ca4cac 1988 (void) clearenv();
ce8aba56 1989
e564a982 1990 assert(i <= args_size);
dcadc967
EV
1991
1992 /*
1993 * We want valgrind to print its memory usage summary before reexecution.
1994 * Valgrind won't do this is on its own on exec(), but it will do it on exit().
1995 * Hence, to ensure we get a summary here, fork() off a child, let it exit() cleanly,
1996 * so that it prints the summary, and wait() for it in the parent, before proceeding into the exec().
1997 */
1998 valgrind_summary_hack();
1999
92ca4cac 2000 (void) execv(args[0], (char* const*) args);
664f88a7 2001 }
6e98720f 2002
664f88a7
LP
2003 /* Try the fallback, if there is any, without any
2004 * serialization. We pass the original argv[] and
2005 * envp[]. (Well, modulo the ordering changes due to
2006 * getopt() in argv[], and some cleanups in envp[],
2007 * but let's hope that doesn't matter.) */
a16e1123 2008
74ca738f 2009 arg_serialization = safe_fclose(arg_serialization);
2feceb5e 2010 fds = fdset_free(fds);
a16e1123 2011
a504223d 2012 /* Reopen the console */
92ca4cac 2013 (void) make_console_stdio();
a504223d 2014
d3b1c508 2015 for (j = 1, i = 1; j < (unsigned) argc; j++)
664f88a7 2016 args[i++] = argv[j];
a16e1123 2017 args[i++] = NULL;
e564a982 2018 assert(i <= args_size);
b8f83232 2019
1b6d7fa7 2020 /* Reenable any blocked signals, especially important
5a85ca1c 2021 * if we switch from initial ramdisk to init=... */
ce30c8dc
LP
2022 (void) reset_all_signal_handlers();
2023 (void) reset_signal_mask();
5a85ca1c 2024
b8f83232
LP
2025 if (switch_root_init) {
2026 args[0] = switch_root_init;
92ca4cac 2027 (void) execv(args[0], (char* const*) args);
56f64d95 2028 log_warning_errno(errno, "Failed to execute configured init, trying fallback: %m");
b8f83232
LP
2029 }
2030
2031 args[0] = "/sbin/init";
92ca4cac 2032 (void) execv(args[0], (char* const*) args);
a16e1123 2033
745e2fb7
KS
2034 if (errno == ENOENT) {
2035 log_warning("No /sbin/init, trying fallback");
b8f83232 2036
745e2fb7
KS
2037 args[0] = "/bin/sh";
2038 args[1] = NULL;
92ca4cac 2039 (void) execv(args[0], (char* const*) args);
56f64d95 2040 log_error_errno(errno, "Failed to execute /bin/sh, giving up: %m");
745e2fb7 2041 } else
56f64d95 2042 log_warning_errno(errno, "Failed to execute /sbin/init, giving up: %m");
a16e1123
LP
2043 }
2044
74ca738f 2045 arg_serialization = safe_fclose(arg_serialization);
2feceb5e 2046 fds = fdset_free(fds);
a16e1123 2047
54b434b1
LP
2048#ifdef HAVE_VALGRIND_VALGRIND_H
2049 /* If we are PID 1 and running under valgrind, then let's exit
2050 * here explicitly. valgrind will only generate nice output on
2051 * exit(), not on exec(), hence let's do the former not the
2052 * latter here. */
2053 if (getpid() == 1 && RUNNING_ON_VALGRIND)
2054 return 0;
2055#endif
2056
b9080b03 2057 if (shutdown_verb) {
b1e90ec5 2058 char log_level[DECIMAL_STR_MAX(int) + 1];
287419c1
AC
2059 char exit_code[DECIMAL_STR_MAX(uint8_t) + 1];
2060 const char* command_line[11] = {
b9080b03
FF
2061 SYSTEMD_SHUTDOWN_BINARY_PATH,
2062 shutdown_verb,
b1e90ec5
ZJS
2063 "--log-level", log_level,
2064 "--log-target",
b9080b03 2065 };
b1e90ec5 2066 unsigned pos = 5;
e3e45d4f 2067 _cleanup_strv_free_ char **env_block = NULL;
15bd5aee
LP
2068
2069 assert(command_line[pos] == NULL);
e3e45d4f 2070 env_block = strv_copy(environ);
b9080b03 2071
5ffa8c81 2072 xsprintf(log_level, "%d", log_get_max_level());
b1e90ec5
ZJS
2073
2074 switch (log_get_target()) {
92ca4cac 2075
b1e90ec5
ZJS
2076 case LOG_TARGET_KMSG:
2077 case LOG_TARGET_JOURNAL_OR_KMSG:
2078 case LOG_TARGET_SYSLOG_OR_KMSG:
2079 command_line[pos++] = "kmsg";
2080 break;
2081
10f00ff1
ILG
2082 case LOG_TARGET_NULL:
2083 command_line[pos++] = "null";
2084 break;
2085
b1e90ec5
ZJS
2086 case LOG_TARGET_CONSOLE:
2087 default:
2088 command_line[pos++] = "console";
2089 break;
2090 };
2091
2092 if (log_get_show_color())
2093 command_line[pos++] = "--log-color";
2094
2095 if (log_get_show_location())
2096 command_line[pos++] = "--log-location";
2097
287419c1
AC
2098 if (streq(shutdown_verb, "exit")) {
2099 command_line[pos++] = "--exit-code";
2100 command_line[pos++] = exit_code;
97792515 2101 xsprintf(exit_code, "%d", retval);
287419c1
AC
2102 }
2103
26abdc73 2104 assert(pos < ELEMENTSOF(command_line));
b1e90ec5 2105
e96d6be7 2106 if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
e3e45d4f 2107 char *e;
d18f337c 2108
e96d6be7
LP
2109 /* If we reboot let's set the shutdown
2110 * watchdog and tell the shutdown binary to
2111 * repeatedly ping it */
c2cc6b9a
LP
2112 r = watchdog_set_timeout(&arg_shutdown_watchdog);
2113 watchdog_close(r < 0);
e96d6be7 2114
e3e45d4f
SP
2115 /* Tell the binary how often to ping, ignore failure */
2116 if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
92ca4cac 2117 (void) strv_push(&env_block, e);
e3e45d4f 2118 } else
e96d6be7
LP
2119 watchdog_close(true);
2120
66713f77
LP
2121 /* Avoid the creation of new processes forked by the
2122 * kernel; at this point, we will not listen to the
2123 * signals anyway */
75f86906 2124 if (detect_container() <= 0)
e155a0aa 2125 (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
ad929bcc 2126
d18f337c 2127 execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
56f64d95 2128 log_error_errno(errno, "Failed to execute shutdown binary, %s: %m",
b1e90ec5 2129 getpid() == 1 ? "freezing" : "quitting");
b9080b03
FF
2130 }
2131
cb6531be
ZJS
2132 if (getpid() == 1) {
2133 if (error_message)
2134 manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
1fc464f6 2135 ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL,
cb6531be 2136 "%s, freezing.", error_message);
b9e74c39 2137 freeze_or_reboot();
cb6531be 2138 }
c3b3c274 2139
60918275
LP
2140 return retval;
2141}