]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/main.c
journal: guarantee async-signal-safety in sd_journald_sendv
[thirdparty/systemd.git] / src / core / main.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 16 Lesser General Public License for more details.
a7334b09 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
60918275
LP
22#include <stdio.h>
23#include <errno.h>
24#include <string.h>
16354eff 25#include <unistd.h>
4ade7963
LP
26#include <sys/types.h>
27#include <sys/stat.h>
f170852a 28#include <getopt.h>
97c4f35c 29#include <signal.h>
4fc935ca 30#include <sys/wait.h>
80876c20 31#include <fcntl.h>
f3b6a3ed 32#include <sys/prctl.h>
664f88a7 33#include <sys/mount.h>
60918275 34
54b434b1
LP
35#ifdef HAVE_VALGRIND_VALGRIND_H
36#include <valgrind/valgrind.h>
37#endif
38
718db961
LP
39#include "sd-daemon.h"
40#include "sd-messages.h"
41#include "sd-bus.h"
60918275 42#include "manager.h"
16354eff 43#include "log.h"
302e8c4c 44#include "load-fragment.h"
a16e1123 45#include "fdset.h"
514f4ef5 46#include "special.h"
487393e9 47#include "conf-parser.h"
ad780f19 48#include "missing.h"
e51bc1a2 49#include "label.h"
302e27c8 50#include "build.h"
06d4c99a 51#include "strv.h"
f6a6225e 52#include "def.h"
b52aae1d 53#include "virt.h"
e96d6be7 54#include "watchdog.h"
664f88a7 55#include "path-util.h"
41669317 56#include "switch-root.h"
ec8927ca 57#include "capability.h"
bd3fa1d2 58#include "killall.h"
0c4025d1
LP
59#include "env-util.h"
60#include "hwclock.h"
718db961
LP
61#include "fileio.h"
62#include "dbus-manager.h"
63#include "bus-error.h"
64#include "bus-util.h"
60918275 65
b6e66135
LP
66#include "mount-setup.h"
67#include "loopback-setup.h"
b6e66135
LP
68#include "hostname-setup.h"
69#include "machine-id-setup.h"
b6e66135
LP
70#include "selinux-setup.h"
71#include "ima-setup.h"
ffbd2c4d 72#include "smack-setup.h"
718db961
LP
73#ifdef HAVE_KMOD
74#include "kmod-setup.h"
75#endif
b6e66135 76
f170852a
LP
77static enum {
78 ACTION_RUN,
e965d56d 79 ACTION_HELP,
9ba0bc4e 80 ACTION_VERSION,
e537352b 81 ACTION_TEST,
4288f619
LP
82 ACTION_DUMP_CONFIGURATION_ITEMS,
83 ACTION_DONE
fa0f4d8a 84} arg_action = ACTION_RUN;
f170852a 85
fa0f4d8a 86static char *arg_default_unit = NULL;
67445f4e 87static SystemdRunningAs arg_running_as = _SYSTEMD_RUNNING_AS_INVALID;
fa0f4d8a
LP
88
89static bool arg_dump_core = true;
90static bool arg_crash_shell = false;
91static int arg_crash_chvt = -1;
92static bool arg_confirm_spawn = false;
9e58ff9c 93static bool arg_show_status = true;
bf4df7c3 94static bool arg_switched_root = false;
0c85a4f3 95static char ***arg_join_controllers = NULL;
706343f4 96static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
0a494f1f 97static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
1f19a534
OS
98static usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC;
99static usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC;
100static usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
3f41e1e5
LN
101static usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL;
102static unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST;
e96d6be7
LP
103static usec_t arg_runtime_watchdog = 0;
104static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
97d0e5f8 105static char **arg_default_environment = NULL;
c93ff2e9 106static struct rlimit *arg_default_rlimit[RLIMIT_NLIMITS] = {};
ec8927ca 107static uint64_t arg_capability_bounding_set_drop = 0;
aa0f64ac 108static nsec_t arg_timer_slack_nsec = (nsec_t) -1;
4fc935ca 109
a16e1123 110static FILE* serialization = NULL;
80876c20 111
6f5e3f35
LP
112static void nop_handler(int sig) {
113}
114
919ce0b7 115noreturn static void crash(int sig) {
97c4f35c 116
abb26902
LP
117 if (getpid() != 1)
118 /* Pass this on immediately, if this is not PID 1 */
119 raise(sig);
120 else if (!arg_dump_core)
582a507f 121 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
97c4f35c 122 else {
b92bea5d
ZJS
123 struct sigaction sa = {
124 .sa_handler = nop_handler,
125 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
126 };
97c4f35c
LP
127 pid_t pid;
128
6f5e3f35 129 /* We want to wait for the core process, hence let's enable SIGCHLD */
abb26902 130 sigaction(SIGCHLD, &sa, NULL);
6f5e3f35 131
e62d8c39
ZJS
132 pid = fork();
133 if (pid < 0)
f5f6d0e2 134 log_error("Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
97c4f35c
LP
135
136 else if (pid == 0) {
b92bea5d 137 struct rlimit rl = {};
97c4f35c
LP
138
139 /* Enable default signal handler for core dump */
140 zero(sa);
141 sa.sa_handler = SIG_DFL;
abb26902 142 sigaction(sig, &sa, NULL);
97c4f35c
LP
143
144 /* Don't limit the core dump size */
97c4f35c
LP
145 rl.rlim_cur = RLIM_INFINITY;
146 rl.rlim_max = RLIM_INFINITY;
147 setrlimit(RLIMIT_CORE, &rl);
148
149 /* Just to be sure... */
abb26902 150 chdir("/");
97c4f35c
LP
151
152 /* Raise the signal again */
153 raise(sig);
154
155 assert_not_reached("We shouldn't be here...");
156 _exit(1);
4fc935ca
LP
157
158 } else {
8e12a6ae
LP
159 siginfo_t status;
160 int r;
4fc935ca
LP
161
162 /* Order things nicely. */
e62d8c39
ZJS
163 r = wait_for_terminate(pid, &status);
164 if (r < 0)
7989e1f2 165 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
8e12a6ae 166 else if (status.si_code != CLD_DUMPED)
7989e1f2 167 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
4fc935ca 168 else
ccd06097 169 log_error("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
97c4f35c
LP
170 }
171 }
172
fa0f4d8a
LP
173 if (arg_crash_chvt)
174 chvt(arg_crash_chvt);
601f6a1e 175
fa0f4d8a 176 if (arg_crash_shell) {
b92bea5d
ZJS
177 struct sigaction sa = {
178 .sa_handler = SIG_IGN,
179 .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
180 };
6f5e3f35 181 pid_t pid;
8c43883a 182
4fc935ca
LP
183 log_info("Executing crash shell in 10s...");
184 sleep(10);
185
6f5e3f35 186 /* Let the kernel reap children for us */
6f5e3f35 187 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
8c43883a 188
cd3bd60a
LP
189 pid = fork();
190 if (pid < 0)
14212119 191 log_error("Failed to fork off crash shell: %m");
6f5e3f35 192 else if (pid == 0) {
cd3bd60a 193 make_console_stdio();
6f5e3f35
LP
194 execl("/bin/sh", "/bin/sh", NULL);
195
14212119 196 log_error("execl() failed: %m");
6f5e3f35
LP
197 _exit(1);
198 }
c99b188e 199
ccd06097 200 log_info("Successfully spawned crash shell as pid "PID_FMT".", pid);
4fc935ca
LP
201 }
202
203 log_info("Freezing execution.");
97c4f35c
LP
204 freeze();
205}
206
207static void install_crash_handler(void) {
b92bea5d
ZJS
208 struct sigaction sa = {
209 .sa_handler = crash,
210 .sa_flags = SA_NODEFER,
211 };
97c4f35c 212
1b91d3e8 213 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
97c4f35c 214}
f170852a 215
843d2643
LP
216static int console_setup(bool do_reset) {
217 int tty_fd, r;
80876c20 218
843d2643
LP
219 /* If we are init, we connect stdin/stdout/stderr to /dev/null
220 * and make sure we don't have a controlling tty. */
80876c20 221
843d2643
LP
222 release_terminal();
223
224 if (!do_reset)
225 return 0;
80876c20 226
512947d4
MS
227 tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
228 if (tty_fd < 0) {
843d2643
LP
229 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
230 return -tty_fd;
231 }
80876c20 232
512947d4
MS
233 /* We don't want to force text mode.
234 * plymouth may be showing pictures already from initrd. */
235 r = reset_terminal_fd(tty_fd, false);
236 if (r < 0)
843d2643
LP
237 log_error("Failed to reset /dev/console: %s", strerror(-r));
238
239 close_nointr_nofail(tty_fd);
80876c20
LP
240 return r;
241}
242
f170852a
LP
243static int set_default_unit(const char *u) {
244 char *c;
245
246 assert(u);
247
bf4df7c3
LP
248 c = strdup(u);
249 if (!c)
f170852a
LP
250 return -ENOMEM;
251
fa0f4d8a
LP
252 free(arg_default_unit);
253 arg_default_unit = c;
bf4df7c3 254
f170852a
LP
255 return 0;
256}
257
258static int parse_proc_cmdline_word(const char *word) {
259
260 static const char * const rlmap[] = {
ed370f5d 261 "emergency", SPECIAL_EMERGENCY_TARGET,
099663ff 262 "-b", SPECIAL_EMERGENCY_TARGET,
ed370f5d
LP
263 "single", SPECIAL_RESCUE_TARGET,
264 "-s", SPECIAL_RESCUE_TARGET,
265 "s", SPECIAL_RESCUE_TARGET,
266 "S", SPECIAL_RESCUE_TARGET,
267 "1", SPECIAL_RESCUE_TARGET,
268 "2", SPECIAL_RUNLEVEL2_TARGET,
269 "3", SPECIAL_RUNLEVEL3_TARGET,
270 "4", SPECIAL_RUNLEVEL4_TARGET,
271 "5", SPECIAL_RUNLEVEL5_TARGET,
f170852a
LP
272 };
273
5192bd19
LP
274 assert(word);
275
bf4df7c3
LP
276 if (startswith(word, "systemd.unit=")) {
277
278 if (!in_initrd())
279 return set_default_unit(word + 13);
280
281 } else if (startswith(word, "rd.systemd.unit=")) {
282
283 if (in_initrd())
284 return set_default_unit(word + 16);
f170852a 285
bf4df7c3 286 } else if (startswith(word, "systemd.log_target=")) {
f170852a
LP
287
288 if (log_set_target_from_string(word + 19) < 0)
289 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
290
291 } else if (startswith(word, "systemd.log_level=")) {
292
293 if (log_set_max_level_from_string(word + 18) < 0)
294 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
295
bbe63281
LP
296 } else if (startswith(word, "systemd.log_color=")) {
297
298 if (log_show_color_from_string(word + 18) < 0)
299 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
300
301 } else if (startswith(word, "systemd.log_location=")) {
302
303 if (log_show_location_from_string(word + 21) < 0)
304 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
305
4fc935ca
LP
306 } else if (startswith(word, "systemd.dump_core=")) {
307 int r;
308
309 if ((r = parse_boolean(word + 18)) < 0)
509b6efb 310 log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
4fc935ca 311 else
fa0f4d8a 312 arg_dump_core = r;
4fc935ca
LP
313
314 } else if (startswith(word, "systemd.crash_shell=")) {
315 int r;
316
317 if ((r = parse_boolean(word + 20)) < 0)
509b6efb 318 log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
4fc935ca 319 else
fa0f4d8a 320 arg_crash_shell = r;
5e7ee61c
LP
321
322 } else if (startswith(word, "systemd.confirm_spawn=")) {
323 int r;
324
325 if ((r = parse_boolean(word + 22)) < 0)
509b6efb 326 log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
5e7ee61c 327 else
fa0f4d8a 328 arg_confirm_spawn = r;
5e7ee61c 329
601f6a1e
LP
330 } else if (startswith(word, "systemd.crash_chvt=")) {
331 int k;
332
333 if (safe_atoi(word + 19, &k) < 0)
509b6efb 334 log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
601f6a1e 335 else
fa0f4d8a 336 arg_crash_chvt = k;
601f6a1e 337
9e58ff9c
LP
338 } else if (startswith(word, "systemd.show_status=")) {
339 int r;
340
341 if ((r = parse_boolean(word + 20)) < 0)
509b6efb 342 log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
6e98720f 343 else
9e58ff9c 344 arg_show_status = r;
0a494f1f
LP
345 } else if (startswith(word, "systemd.default_standard_output=")) {
346 int r;
347
348 if ((r = exec_output_from_string(word + 32)) < 0)
509b6efb 349 log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
0a494f1f
LP
350 else
351 arg_default_std_output = r;
352 } else if (startswith(word, "systemd.default_standard_error=")) {
353 int r;
354
355 if ((r = exec_output_from_string(word + 31)) < 0)
509b6efb 356 log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
0a494f1f
LP
357 else
358 arg_default_std_error = r;
9e7c5357 359 } else if (startswith(word, "systemd.setenv=")) {
0c4025d1 360 _cleanup_free_ char *cenv = NULL;
9e7c5357
WD
361
362 cenv = strdup(word + 15);
363 if (!cenv)
364 return -ENOMEM;
365
e21fea24
KS
366 if (env_assignment_is_valid(cenv)) {
367 char **env;
368
369 env = strv_env_set(arg_default_environment, cenv);
370 if (env)
371 arg_default_environment = env;
372 else
373 log_warning("Setting environment variable '%s' failed, ignoring: %m", cenv);
374 } else
375 log_warning("Environment variable name '%s' is not valid. Ignoring.", cenv);
9e58ff9c 376
66a78c2b
LP
377 } else if (startswith(word, "systemd.") ||
378 (in_initrd() && startswith(word, "rd.systemd."))) {
4fc935ca 379
dd7c30c3
LP
380 const char *c;
381
382 /* Ignore systemd.journald.xyz and friends */
383 c = word;
384 if (startswith(c, "rd."))
385 c += 3;
386 if (startswith(c, "systemd."))
387 c += 8;
388 if (c[strcspn(c, ".=")] != '.') {
389
390 log_warning("Unknown kernel switch %s. Ignoring.", word);
391
392 log_info("Supported kernel switches:\n"
393 "systemd.unit=UNIT Default unit to start\n"
394 "rd.systemd.unit=UNIT Default unit to start when run in initrd\n"
395 "systemd.dump_core=0|1 Dump core on crash\n"
396 "systemd.crash_shell=0|1 Run shell on crash\n"
397 "systemd.crash_chvt=N Change to VT #N on crash\n"
398 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
399 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
400 "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
401 " Log target\n"
402 "systemd.log_level=LEVEL Log level\n"
403 "systemd.log_color=0|1 Highlight important log messages\n"
404 "systemd.log_location=0|1 Include code location in log messages\n"
405 "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
406 " Set default log output for services\n"
407 "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
408 " Set default log error output for services\n"
409 "systemd.setenv=ASSIGNMENT Set an environment variable for all spawned processes\n");
410 }
4fc935ca 411
d081dffb 412 } else if (streq(word, "quiet"))
6e98720f 413 arg_show_status = false;
bd6d2963
KS
414 else if (streq(word, "debug")) {
415 /* Log to kmsg, the journal socket will fill up before the
416 * journal is started and tools running during that time
417 * will block with every log message for for 60 seconds,
418 * before they give up. */
9749cd77 419 log_set_max_level(LOG_DEBUG);
220ec97a 420 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG);
bd6d2963 421 } else if (!in_initrd()) {
f170852a
LP
422 unsigned i;
423
424 /* SysV compatibility */
f170852a
LP
425 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
426 if (streq(word, rlmap[i]))
427 return set_default_unit(rlmap[i+1]);
428 }
429
430 return 0;
431}
432
e8e581bf
ZJS
433#define DEFINE_SETTER(name, func, descr) \
434 static int name(const char *unit, \
435 const char *filename, \
436 unsigned line, \
437 const char *section, \
71a61510 438 unsigned section_line, \
e8e581bf
ZJS
439 const char *lvalue, \
440 int ltype, \
441 const char *rvalue, \
442 void *data, \
443 void *userdata) { \
444 \
445 int r; \
446 \
447 assert(filename); \
448 assert(lvalue); \
449 assert(rvalue); \
450 \
451 r = func(rvalue); \
452 if (r < 0) \
453 log_syntax(unit, LOG_ERR, filename, line, -r, \
454 "Invalid " descr "'%s': %s", \
455 rvalue, strerror(-r)); \
456 \
457 return 0; \
458 }
487393e9 459
e8e581bf
ZJS
460DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
461DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
462DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
463DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
487393e9 464
e8e581bf
ZJS
465static int config_parse_cpu_affinity2(const char *unit,
466 const char *filename,
467 unsigned line,
468 const char *section,
71a61510 469 unsigned section_line,
e8e581bf
ZJS
470 const char *lvalue,
471 int ltype,
472 const char *rvalue,
473 void *data,
474 void *userdata) {
487393e9
LP
475
476 char *w;
477 size_t l;
478 char *state;
479 cpu_set_t *c = NULL;
480 unsigned ncpus = 0;
481
482 assert(filename);
483 assert(lvalue);
484 assert(rvalue);
485
f60f22df 486 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
487393e9
LP
487 char *t;
488 int r;
489 unsigned cpu;
490
491 if (!(t = strndup(w, l)))
14212119 492 return log_oom();
487393e9
LP
493
494 r = safe_atou(t, &cpu);
495 free(t);
496
497 if (!c)
498 if (!(c = cpu_set_malloc(&ncpus)))
14212119 499 return log_oom();
487393e9
LP
500
501 if (r < 0 || cpu >= ncpus) {
e8e581bf
ZJS
502 log_syntax(unit, LOG_ERR, filename, line, -r,
503 "Failed to parse CPU affinity '%s'", rvalue);
487393e9
LP
504 CPU_FREE(c);
505 return -EBADMSG;
506 }
507
508 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
509 }
510
511 if (c) {
512 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
e8e581bf 513 log_warning_unit(unit, "Failed to set CPU affinity: %m");
487393e9
LP
514
515 CPU_FREE(c);
516 }
517
518 return 0;
519}
520
0c85a4f3
LP
521static void strv_free_free(char ***l) {
522 char ***i;
523
524 if (!l)
525 return;
526
527 for (i = l; *i; i++)
528 strv_free(*i);
529
530 free(l);
531}
532
533static void free_join_controllers(void) {
0c85a4f3
LP
534 strv_free_free(arg_join_controllers);
535 arg_join_controllers = NULL;
536}
537
e8e581bf
ZJS
538static int config_parse_join_controllers(const char *unit,
539 const char *filename,
540 unsigned line,
541 const char *section,
71a61510 542 unsigned section_line,
e8e581bf
ZJS
543 const char *lvalue,
544 int ltype,
545 const char *rvalue,
546 void *data,
547 void *userdata) {
0c85a4f3
LP
548
549 unsigned n = 0;
550 char *state, *w;
551 size_t length;
552
553 assert(filename);
554 assert(lvalue);
555 assert(rvalue);
556
557 free_join_controllers();
558
559 FOREACH_WORD_QUOTED(w, length, rvalue, state) {
560 char *s, **l;
561
562 s = strndup(w, length);
563 if (!s)
14212119 564 return log_oom();
0c85a4f3
LP
565
566 l = strv_split(s, ",");
567 free(s);
568
569 strv_uniq(l);
570
571 if (strv_length(l) <= 1) {
572 strv_free(l);
573 continue;
574 }
575
576 if (!arg_join_controllers) {
577 arg_join_controllers = new(char**, 2);
578 if (!arg_join_controllers) {
579 strv_free(l);
14212119 580 return log_oom();
0c85a4f3
LP
581 }
582
583 arg_join_controllers[0] = l;
584 arg_join_controllers[1] = NULL;
585
586 n = 1;
587 } else {
588 char ***a;
589 char ***t;
590
591 t = new0(char**, n+2);
592 if (!t) {
593 strv_free(l);
14212119 594 return log_oom();
0c85a4f3
LP
595 }
596
597 n = 0;
598
599 for (a = arg_join_controllers; *a; a++) {
600
601 if (strv_overlap(*a, l)) {
e3e45d4f 602 if (strv_extend_strv(&l, *a) < 0) {
0c85a4f3
LP
603 strv_free(l);
604 strv_free_free(t);
14212119 605 return log_oom();
0c85a4f3
LP
606 }
607
0c85a4f3
LP
608 } else {
609 char **c;
610
611 c = strv_copy(*a);
612 if (!c) {
613 strv_free(l);
614 strv_free_free(t);
14212119 615 return log_oom();
0c85a4f3
LP
616 }
617
618 t[n++] = c;
619 }
620 }
621
622 t[n++] = strv_uniq(l);
623
624 strv_free_free(arg_join_controllers);
625 arg_join_controllers = t;
626 }
627 }
628
629 return 0;
630}
631
487393e9
LP
632static int parse_config_file(void) {
633
f975e971
LP
634 const ConfigTableItem items[] = {
635 { "Manager", "LogLevel", config_parse_level2, 0, NULL },
636 { "Manager", "LogTarget", config_parse_target, 0, NULL },
637 { "Manager", "LogColor", config_parse_color, 0, NULL },
638 { "Manager", "LogLocation", config_parse_location, 0, NULL },
639 { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
640 { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
641 { "Manager", "ShowStatus", config_parse_bool, 0, &arg_show_status },
f975e971
LP
642 { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt },
643 { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
f975e971
LP
644 { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
645 { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
1f19a534
OS
646 { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec },
647 { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec },
648 { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec },
3f41e1e5
LN
649 { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval },
650 { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst },
0c85a4f3 651 { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
7f602784
LP
652 { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
653 { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
ec8927ca 654 { "Manager", "CapabilityBoundingSet", config_parse_bounding_set, 0, &arg_capability_bounding_set_drop },
aa0f64ac 655 { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
97d0e5f8 656 { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
c93ff2e9
FC
657 { "Manager", "DefaultLimitCPU", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CPU]},
658 { "Manager", "DefaultLimitFSIZE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_FSIZE]},
659 { "Manager", "DefaultLimitDATA", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_DATA]},
660 { "Manager", "DefaultLimitSTACK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_STACK]},
661 { "Manager", "DefaultLimitCORE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CORE]},
662 { "Manager", "DefaultLimitRSS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RSS]},
663 { "Manager", "DefaultLimitNOFILE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NOFILE]},
664 { "Manager", "DefaultLimitAS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_AS]},
665 { "Manager", "DefaultLimitNPROC", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NPROC]},
666 { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MEMLOCK]},
667 { "Manager", "DefaultLimitLOCKS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_LOCKS]},
668 { "Manager", "DefaultLimitSIGPENDING",config_parse_limit, 0, &arg_default_rlimit[RLIMIT_SIGPENDING]},
669 { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MSGQUEUE]},
670 { "Manager", "DefaultLimitNICE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NICE]},
671 { "Manager", "DefaultLimitRTPRIO", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTPRIO]},
672 { "Manager", "DefaultLimitRTTIME", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTTIME]},
f975e971 673 { NULL, NULL, NULL, 0, NULL }
487393e9
LP
674 };
675
7fd1b19b 676 _cleanup_fclose_ FILE *f;
487393e9
LP
677 const char *fn;
678 int r;
679
19adb8a3 680 fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf";
f975e971
LP
681 f = fopen(fn, "re");
682 if (!f) {
487393e9
LP
683 if (errno == ENOENT)
684 return 0;
685
686 log_warning("Failed to open configuration file '%s': %m", fn);
687 return 0;
688 }
689
db5c0122 690 r = config_parse(NULL, fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, false, NULL);
f975e971 691 if (r < 0)
487393e9
LP
692 log_warning("Failed to parse configuration file: %s", strerror(-r));
693
487393e9
LP
694 return 0;
695}
696
f170852a 697static int parse_proc_cmdline(void) {
7fd1b19b 698 _cleanup_free_ char *line = NULL;
543295ad 699 char *w, *state;
f170852a 700 size_t l;
74df0fca 701 int r;
f170852a 702
74df0fca
LP
703 r = proc_cmdline(&line);
704 if (r < 0)
e364ad06 705 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
74df0fca 706 if (r <= 0)
f170852a 707 return 0;
f170852a
LP
708
709 FOREACH_WORD_QUOTED(w, l, line, state) {
7fd1b19b 710 _cleanup_free_ char *word;
f170852a 711
543295ad
ZJS
712 word = strndup(w, l);
713 if (!word)
714 return log_oom();
f170852a
LP
715
716 r = parse_proc_cmdline_word(word);
14212119
SL
717 if (r < 0) {
718 log_error("Failed on cmdline argument %s: %s", word, strerror(-r));
543295ad 719 return r;
14212119 720 }
f170852a
LP
721 }
722
543295ad 723 return 0;
f170852a
LP
724}
725
726static int parse_argv(int argc, char *argv[]) {
727
728 enum {
729 ARG_LOG_LEVEL = 0x100,
730 ARG_LOG_TARGET,
bbe63281
LP
731 ARG_LOG_COLOR,
732 ARG_LOG_LOCATION,
2f198e2f 733 ARG_UNIT,
edb9aaa8 734 ARG_SYSTEM,
af2d49f7 735 ARG_USER,
e537352b 736 ARG_TEST,
9ba0bc4e 737 ARG_VERSION,
80876c20 738 ARG_DUMP_CONFIGURATION_ITEMS,
9e58ff9c
LP
739 ARG_DUMP_CORE,
740 ARG_CRASH_SHELL,
a16e1123 741 ARG_CONFIRM_SPAWN,
9e58ff9c 742 ARG_SHOW_STATUS,
4288f619 743 ARG_DESERIALIZE,
2660882b 744 ARG_SWITCHED_ROOT,
0a494f1f
LP
745 ARG_DEFAULT_STD_OUTPUT,
746 ARG_DEFAULT_STD_ERROR
f170852a
LP
747 };
748
749 static const struct option options[] = {
a16e1123
LP
750 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
751 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bbe63281
LP
752 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
753 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2f198e2f 754 { "unit", required_argument, NULL, ARG_UNIT },
edb9aaa8 755 { "system", no_argument, NULL, ARG_SYSTEM },
af2d49f7 756 { "user", no_argument, NULL, ARG_USER },
a16e1123
LP
757 { "test", no_argument, NULL, ARG_TEST },
758 { "help", no_argument, NULL, 'h' },
9ba0bc4e 759 { "version", no_argument, NULL, ARG_VERSION },
a16e1123 760 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
a5d87bf0
LP
761 { "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
762 { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
763 { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
6e98720f 764 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
a16e1123 765 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
2660882b 766 { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
0a494f1f
LP
767 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
768 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
a16e1123 769 { NULL, 0, NULL, 0 }
f170852a
LP
770 };
771
772 int c, r;
773
774 assert(argc >= 1);
775 assert(argv);
776
b770165a
LP
777 if (getpid() == 1)
778 opterr = 0;
779
099663ff 780 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
f170852a
LP
781
782 switch (c) {
783
784 case ARG_LOG_LEVEL:
785 if ((r = log_set_max_level_from_string(optarg)) < 0) {
786 log_error("Failed to parse log level %s.", optarg);
787 return r;
788 }
789
790 break;
791
792 case ARG_LOG_TARGET:
793
794 if ((r = log_set_target_from_string(optarg)) < 0) {
795 log_error("Failed to parse log target %s.", optarg);
796 return r;
797 }
798
799 break;
800
bbe63281
LP
801 case ARG_LOG_COLOR:
802
d0b170c8
LP
803 if (optarg) {
804 if ((r = log_show_color_from_string(optarg)) < 0) {
805 log_error("Failed to parse log color setting %s.", optarg);
806 return r;
807 }
808 } else
809 log_show_color(true);
bbe63281
LP
810
811 break;
812
813 case ARG_LOG_LOCATION:
814
d0b170c8
LP
815 if (optarg) {
816 if ((r = log_show_location_from_string(optarg)) < 0) {
817 log_error("Failed to parse log location setting %s.", optarg);
818 return r;
819 }
820 } else
821 log_show_location(true);
bbe63281
LP
822
823 break;
824
0a494f1f
LP
825 case ARG_DEFAULT_STD_OUTPUT:
826
827 if ((r = exec_output_from_string(optarg)) < 0) {
828 log_error("Failed to parse default standard output setting %s.", optarg);
829 return r;
830 } else
831 arg_default_std_output = r;
832 break;
833
834 case ARG_DEFAULT_STD_ERROR:
835
836 if ((r = exec_output_from_string(optarg)) < 0) {
837 log_error("Failed to parse default standard error output setting %s.", optarg);
838 return r;
839 } else
840 arg_default_std_error = r;
841 break;
842
2f198e2f 843 case ARG_UNIT:
f170852a
LP
844
845 if ((r = set_default_unit(optarg)) < 0) {
846 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
847 return r;
848 }
849
850 break;
851
edb9aaa8 852 case ARG_SYSTEM:
67445f4e 853 arg_running_as = SYSTEMD_SYSTEM;
edb9aaa8 854 break;
a5dab5ce 855
af2d49f7 856 case ARG_USER:
67445f4e 857 arg_running_as = SYSTEMD_USER;
a5dab5ce 858 break;
a5dab5ce 859
e965d56d 860 case ARG_TEST:
fa0f4d8a 861 arg_action = ACTION_TEST;
e965d56d
LP
862 break;
863
9ba0bc4e
ZJS
864 case ARG_VERSION:
865 arg_action = ACTION_VERSION;
866 break;
867
e537352b 868 case ARG_DUMP_CONFIGURATION_ITEMS:
fa0f4d8a 869 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
e537352b
LP
870 break;
871
9e58ff9c 872 case ARG_DUMP_CORE:
a5d87bf0
LP
873 r = optarg ? parse_boolean(optarg) : 1;
874 if (r < 0) {
875 log_error("Failed to parse dump core boolean %s.", optarg);
876 return r;
877 }
878 arg_dump_core = r;
9e58ff9c
LP
879 break;
880
881 case ARG_CRASH_SHELL:
a5d87bf0
LP
882 r = optarg ? parse_boolean(optarg) : 1;
883 if (r < 0) {
884 log_error("Failed to parse crash shell boolean %s.", optarg);
885 return r;
886 }
887 arg_crash_shell = r;
9e58ff9c
LP
888 break;
889
80876c20 890 case ARG_CONFIRM_SPAWN:
a5d87bf0
LP
891 r = optarg ? parse_boolean(optarg) : 1;
892 if (r < 0) {
893 log_error("Failed to parse confirm spawn boolean %s.", optarg);
894 return r;
895 }
896 arg_confirm_spawn = r;
80876c20
LP
897 break;
898
9e58ff9c 899 case ARG_SHOW_STATUS:
a5d87bf0
LP
900 r = optarg ? parse_boolean(optarg) : 1;
901 if (r < 0) {
902 log_error("Failed to parse show status boolean %s.", optarg);
903 return r;
904 }
905 arg_show_status = r;
6e98720f 906 break;
a5d87bf0 907
a16e1123
LP
908 case ARG_DESERIALIZE: {
909 int fd;
910 FILE *f;
911
01e10de3
LP
912 r = safe_atoi(optarg, &fd);
913 if (r < 0 || fd < 0) {
a16e1123 914 log_error("Failed to parse deserialize option %s.", optarg);
01e10de3 915 return r < 0 ? r : -EINVAL;
a16e1123
LP
916 }
917
01e10de3
LP
918 fd_cloexec(fd, true);
919
920 f = fdopen(fd, "r");
921 if (!f) {
a16e1123 922 log_error("Failed to open serialization fd: %m");
01e10de3 923 return -errno;
a16e1123
LP
924 }
925
926 if (serialization)
927 fclose(serialization);
928
929 serialization = f;
930
931 break;
932 }
933
2660882b 934 case ARG_SWITCHED_ROOT:
bf4df7c3 935 arg_switched_root = true;
d03bc1b8
HH
936 break;
937
f170852a 938 case 'h':
fa0f4d8a 939 arg_action = ACTION_HELP;
f170852a
LP
940 break;
941
1d2e23ab
LP
942 case 'D':
943 log_set_max_level(LOG_DEBUG);
944 break;
945
099663ff
LP
946 case 'b':
947 case 's':
948 case 'z':
949 /* Just to eat away the sysvinit kernel
950 * cmdline args without getopt() error
951 * messages that we'll parse in
952 * parse_proc_cmdline_word() or ignore. */
f170852a 953
099663ff 954 case '?':
f170852a 955 default:
099663ff
LP
956 if (getpid() != 1) {
957 log_error("Unknown option code %c", c);
958 return -EINVAL;
959 }
960
961 break;
f170852a
LP
962 }
963
d821e6d6
LP
964 if (optind < argc && getpid() != 1) {
965 /* Hmm, when we aren't run as init system
966 * let's complain about excess arguments */
967
968 log_error("Excess arguments.");
969 return -EINVAL;
970 }
971
972 if (detect_container(NULL) > 0) {
973 char **a;
974
975 /* All /proc/cmdline arguments the kernel didn't
976 * understand it passed to us. We're not really
977 * interested in that usually since /proc/cmdline is
978 * more interesting and complete. With one exception:
979 * if we are run in a container /proc/cmdline is not
980 * relevant for the container, hence we rely on argv[]
981 * instead. */
982
74df0fca
LP
983 for (a = argv; a < argv + argc; a++) {
984 r = parse_proc_cmdline_word(*a);
985 if (r < 0) {
14212119 986 log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
d821e6d6 987 return r;
14212119 988 }
74df0fca 989 }
51f0e189
LP
990 }
991
f170852a
LP
992 return 0;
993}
994
995static int help(void) {
996
2e33c433 997 printf("%s [OPTIONS...]\n\n"
af2d49f7 998 "Starts up and maintains the system or user services.\n\n"
e537352b 999 " -h --help Show this help\n"
e537352b 1000 " --test Determine startup sequence, dump it and exit\n"
80876c20 1001 " --dump-configuration-items Dump understood unit configuration items\n"
9e58ff9c 1002 " --unit=UNIT Set default unit\n"
edb9aaa8 1003 " --system Run a system instance, even if PID != 1\n"
af2d49f7 1004 " --user Run a user instance\n"
a5d87bf0
LP
1005 " --dump-core[=0|1] Dump core on crash\n"
1006 " --crash-shell[=0|1] Run shell on crash\n"
1007 " --confirm-spawn[=0|1] Ask for confirmation when spawning processes\n"
6e98720f 1008 " --show-status[=0|1] Show status updates on the console during bootup\n"
4cfa2c99 1009 " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
9e58ff9c 1010 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
2218198b 1011 " --log-color[=0|1] Highlight important log messages\n"
0a494f1f
LP
1012 " --log-location[=0|1] Include code location in log messages\n"
1013 " --default-standard-output= Set default standard output for services\n"
1014 " --default-standard-error= Set default standard error output for services\n",
5b6319dc 1015 program_invocation_short_name);
f170852a
LP
1016
1017 return 0;
1018}
1019
9ba0bc4e
ZJS
1020static int version(void) {
1021 puts(PACKAGE_STRING);
9ba0bc4e
ZJS
1022 puts(SYSTEMD_FEATURES);
1023
1024 return 0;
1025}
1026
b3680f49 1027static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
a16e1123
LP
1028 FILE *f = NULL;
1029 FDSet *fds = NULL;
1030 int r;
1031
1032 assert(m);
1033 assert(_f);
1034 assert(_fds);
1035
6b78f9b4
LP
1036 r = manager_open_serialization(m, &f);
1037 if (r < 0) {
35b8ca3a 1038 log_error("Failed to create serialization file: %s", strerror(-r));
a16e1123
LP
1039 goto fail;
1040 }
1041
71445ae7
LP
1042 /* Make sure nothing is really destructed when we shut down */
1043 m->n_reloading ++;
718db961 1044 bus_manager_send_reloading(m, true);
71445ae7 1045
6b78f9b4
LP
1046 fds = fdset_new();
1047 if (!fds) {
a16e1123
LP
1048 r = -ENOMEM;
1049 log_error("Failed to allocate fd set: %s", strerror(-r));
1050 goto fail;
1051 }
1052
b3680f49 1053 r = manager_serialize(m, f, fds, switching_root);
6b78f9b4 1054 if (r < 0) {
a16e1123
LP
1055 log_error("Failed to serialize state: %s", strerror(-r));
1056 goto fail;
1057 }
1058
1059 if (fseeko(f, 0, SEEK_SET) < 0) {
1060 log_error("Failed to rewind serialization fd: %m");
1061 goto fail;
1062 }
1063
6b78f9b4
LP
1064 r = fd_cloexec(fileno(f), false);
1065 if (r < 0) {
a16e1123
LP
1066 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
1067 goto fail;
1068 }
1069
6b78f9b4
LP
1070 r = fdset_cloexec(fds, false);
1071 if (r < 0) {
a16e1123
LP
1072 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
1073 goto fail;
1074 }
1075
1076 *_f = f;
1077 *_fds = fds;
1078
1079 return 0;
1080
1081fail:
1082 fdset_free(fds);
1083
1084 if (f)
1085 fclose(f);
1086
1087 return r;
1088}
1089
4096d6f5
LP
1090static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
1091 struct rlimit nl;
1092 int r;
1093
1094 assert(saved_rlimit);
1095
1096 /* Save the original RLIMIT_NOFILE so that we can reset it
1097 * later when transitioning from the initrd to the main
1098 * systemd or suchlike. */
1099 if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0) {
1100 log_error("Reading RLIMIT_NOFILE failed: %m");
1101 return -errno;
1102 }
1103
1104 /* Make sure forked processes get the default kernel setting */
1105 if (!arg_default_rlimit[RLIMIT_NOFILE]) {
1106 struct rlimit *rl;
1107
1108 rl = newdup(struct rlimit, saved_rlimit, 1);
1109 if (!rl)
1110 return log_oom();
1111
1112 arg_default_rlimit[RLIMIT_NOFILE] = rl;
1113 }
1114
1115 /* Bump up the resource limit for ourselves substantially */
1116 nl.rlim_cur = nl.rlim_max = 64*1024;
1117 r = setrlimit_closest(RLIMIT_NOFILE, &nl);
1118 if (r < 0) {
1119 log_error("Setting RLIMIT_NOFILE failed: %s", strerror(-r));
1120 return r;
1121 }
1122
1123 return 0;
1124}
1125
6ee5bbf8
LP
1126static void test_mtab(void) {
1127 char *p;
1128
80758717
LP
1129 /* Check that /etc/mtab is a symlink */
1130
6ee5bbf8
LP
1131 if (readlink_malloc("/etc/mtab", &p) >= 0) {
1132 bool b;
1133
ed86ebc4 1134 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
6ee5bbf8
LP
1135 free(p);
1136
1137 if (b)
1138 return;
1139 }
1140
80758717
LP
1141 log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
1142 "This is not supported anymore. "
1143 "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
1144}
1145
1146static void test_usr(void) {
80758717 1147
ed1c99fc 1148 /* Check that /usr is not a separate fs */
80758717 1149
871c44a7
LP
1150 if (dir_is_empty("/usr") <= 0)
1151 return;
1152
2376ce13 1153 log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
871c44a7
LP
1154 "Some things will probably break (sometimes even silently) in mysterious ways. "
1155 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1156}
1157
1158static void test_cgroups(void) {
1159
1160 if (access("/proc/cgroups", F_OK) >= 0)
1161 return;
1162
1163 log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
1164 "Systems without control groups are not supported. "
1165 "We will now sleep for 10s, and then continue boot-up. "
1166 "Expect breakage and please do not file bugs. "
966a5d37
LP
1167 "Instead fix your kernel and enable CONFIG_CGROUPS. "
1168 "Consult http://0pointer.de/blog/projects/cgroups-vs-cgroups.html for more information.");
871c44a7
LP
1169
1170 sleep(10);
6ee5bbf8
LP
1171}
1172
a07fdfa3
LP
1173static int initialize_join_controllers(void) {
1174 /* By default, mount "cpu" + "cpuacct" together, and "net_cls"
1175 * + "net_prio". We'd like to add "cpuset" to the mix, but
1176 * "cpuset" does't really work for groups with no initialized
1177 * attributes. */
1178
1179 arg_join_controllers = new(char**, 3);
1180 if (!arg_join_controllers)
1181 return -ENOMEM;
1182
1183 arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
a07fdfa3 1184 arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL);
a6b26d90
ZJS
1185 arg_join_controllers[2] = NULL;
1186
1187 if (!arg_join_controllers[0] || !arg_join_controllers[1]) {
1188 free_join_controllers();
a07fdfa3 1189 return -ENOMEM;
a6b26d90 1190 }
a07fdfa3 1191
a07fdfa3
LP
1192 return 0;
1193}
1194
60918275
LP
1195int main(int argc, char *argv[]) {
1196 Manager *m = NULL;
22f4096c 1197 int r, retval = EXIT_FAILURE;
9d76d730
LP
1198 usec_t before_startup, after_startup;
1199 char timespan[FORMAT_TIMESPAN_MAX];
a16e1123
LP
1200 FDSet *fds = NULL;
1201 bool reexecute = false;
b9080b03 1202 const char *shutdown_verb = NULL;
e9ddabc2 1203 dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
c3a170f3
HH
1204 dual_timestamp userspace_timestamp = { 0ULL, 0ULL };
1205 dual_timestamp kernel_timestamp = { 0ULL, 0ULL };
c2e0d600
TA
1206 dual_timestamp security_start_timestamp = { 0ULL, 0ULL };
1207 dual_timestamp security_finish_timestamp = { 0ULL, 0ULL };
5d6b1584 1208 static char systemd[] = "systemd";
2660882b 1209 bool skip_setup = false;
0b3325e7
LP
1210 int j;
1211 bool loaded_policy = false;
e96d6be7 1212 bool arm_reboot_watchdog = false;
bf4df7c3 1213 bool queue_default_job = false;
41669317 1214 char *switch_root_dir = NULL, *switch_root_init = NULL;
4096d6f5 1215 static struct rlimit saved_rlimit_nofile = { 0, 0 };
27b14a22 1216
058dc6f3 1217#ifdef HAVE_SYSV_COMPAT
2cb1a60d 1218 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
35b8ca3a 1219 /* This is compatibility support for SysV, where
2cb1a60d
LP
1220 * calling init as a user is identical to telinit. */
1221
1222 errno = -ENOENT;
1223 execv(SYSTEMCTL_BINARY_PATH, argv);
1224 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1225 return 1;
1226 }
058dc6f3 1227#endif
2cb1a60d 1228
c3a170f3
HH
1229 dual_timestamp_from_monotonic(&kernel_timestamp, 0);
1230 dual_timestamp_get(&userspace_timestamp);
1231
0b3325e7
LP
1232 /* Determine if this is a reexecution or normal bootup. We do
1233 * the full command line parsing much later, so let's just
1234 * have a quick peek here. */
db813c2a
LP
1235 if (strv_find(argv+1, "--deserialize"))
1236 skip_setup = true;
0b3325e7 1237
2660882b
LP
1238 /* If we have switched root, do all the special setup
1239 * things */
db813c2a
LP
1240 if (strv_find(argv+1, "--switched-root"))
1241 skip_setup = false;
d03bc1b8 1242
f3b6a3ed
LP
1243 /* If we get started via the /sbin/init symlink then we are
1244 called 'init'. After a subsequent reexecution we are then
1245 called 'systemd'. That is confusing, hence let's call us
1246 systemd right-away. */
f3b6a3ed
LP
1247 program_invocation_short_name = systemd;
1248 prctl(PR_SET_NAME, systemd);
5d6b1584 1249
9a0e6896
LP
1250 saved_argv = argv;
1251 saved_argc = argc;
f3b6a3ed 1252
2cc59dbf 1253 log_show_color(isatty(STDERR_FILENO) > 0);
bbe63281 1254
90dc8c2e
MG
1255 /* Disable the umask logic */
1256 if (getpid() == 1)
1257 umask(0);
1258
a866073d 1259 if (getpid() == 1 && detect_container(NULL) <= 0) {
4f8d551f 1260
a866073d 1261 /* Running outside of a container as PID 1 */
67445f4e 1262 arg_running_as = SYSTEMD_SYSTEM;
a866073d
LP
1263 make_null_stdio();
1264 log_set_target(LOG_TARGET_KMSG);
1265 log_open();
1266
21bf2ab0 1267 if (in_initrd())
c3a170f3 1268 initrd_timestamp = userspace_timestamp;
c3ba6250 1269
2660882b 1270 if (!skip_setup) {
8f838d8a 1271 mount_setup_early();
c2e0d600 1272 dual_timestamp_get(&security_start_timestamp);
0b3325e7
LP
1273 if (selinux_setup(&loaded_policy) < 0)
1274 goto finish;
81611586
RS
1275 if (ima_setup() < 0)
1276 goto finish;
ffbd2c4d
NC
1277 if (smack_setup() < 0)
1278 goto finish;
c2e0d600 1279 dual_timestamp_get(&security_finish_timestamp);
81611586 1280 }
0b3325e7 1281
e9a5ef7c 1282 if (label_init(NULL) < 0)
0ff4cdd9 1283 goto finish;
7948c4df 1284
72edcff5 1285 if (!skip_setup) {
0b3325e7
LP
1286 if (hwclock_is_localtime() > 0) {
1287 int min;
7948c4df 1288
72edcff5
KS
1289 /* The first-time call to settimeofday() does a time warp in the kernel */
1290 r = hwclock_set_timezone(&min);
0b3325e7
LP
1291 if (r < 0)
1292 log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
1293 else
1294 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
19e65613
KS
1295 } else if (!in_initrd()) {
1296 /*
1297 * Do dummy first-time call to seal the kernel's time warp magic
1298 *
1299 * Do not call this this from inside the initrd. The initrd might not
1300 * carry /etc/adjtime with LOCAL, but the real system could be set up
1301 * that way. In such case, we need to delay the time-warp or the sealing
1302 * until we reach the real system.
1303 */
72edcff5 1304 hwclock_reset_timezone();
871e5809 1305
e9dd9f95 1306 /* Tell the kernel our timezone */
72edcff5
KS
1307 r = hwclock_set_timezone(NULL);
1308 if (r < 0)
e9dd9f95 1309 log_error("Failed to set the kernel's timezone, ignoring: %s", strerror(-r));
72edcff5
KS
1310 }
1311 }
a866073d
LP
1312
1313 /* Set the default for later on, but don't actually
1314 * open the logs like this for now. Note that if we
1315 * are transitioning from the initrd there might still
1316 * be journal fd open, and we shouldn't attempt
1317 * opening that before we parsed /proc/cmdline which
1318 * might redirect output elsewhere. */
1319 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1320
1321 } else if (getpid() == 1) {
a866073d 1322 /* Running inside a container, as PID 1 */
67445f4e 1323 arg_running_as = SYSTEMD_SYSTEM;
a866073d 1324 log_set_target(LOG_TARGET_CONSOLE);
99f09825 1325 log_close_console(); /* force reopen of /dev/console */
a866073d
LP
1326 log_open();
1327
1328 /* For the later on, see above... */
1329 log_set_target(LOG_TARGET_JOURNAL);
1330
c3a170f3
HH
1331 /* clear the kernel timestamp,
1332 * because we are in a container */
1333 kernel_timestamp.monotonic = 0ULL;
1334 kernel_timestamp.realtime = 0ULL;
a866073d 1335
c3a170f3 1336 } else {
a866073d 1337 /* Running as user instance */
67445f4e 1338 arg_running_as = SYSTEMD_USER;
eeecf6e6 1339 log_set_target(LOG_TARGET_AUTO);
871e5809 1340 log_open();
c3a170f3
HH
1341
1342 /* clear the kernel timestamp,
1343 * because we are not PID 1 */
1344 kernel_timestamp.monotonic = 0ULL;
1345 kernel_timestamp.realtime = 0ULL;
bbe63281 1346 }
a5dab5ce 1347
0c85a4f3 1348 /* Initialize default unit */
6afa301b
LP
1349 r = set_default_unit(SPECIAL_DEFAULT_TARGET);
1350 if (r < 0) {
14212119 1351 log_error("Failed to set default unit %s: %s", SPECIAL_DEFAULT_TARGET, strerror(-r));
f170852a 1352 goto finish;
14212119 1353 }
60918275 1354
a07fdfa3
LP
1355 r = initialize_join_controllers();
1356 if (r < 0)
0c85a4f3
LP
1357 goto finish;
1358
f170852a
LP
1359 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1360 * /proc/$PID/fd is available. */
c1dae1b3 1361 if (getpid() == 1) {
0c85a4f3
LP
1362 r = mount_setup(loaded_policy);
1363 if (r < 0)
8efe3c01 1364 goto finish;
0c85a4f3 1365 }
4ade7963
LP
1366
1367 /* Reset all signal handlers. */
1368 assert_se(reset_all_signal_handlers() == 0);
1369
9a34ec5f 1370 ignore_signals(SIGNALS_IGNORE, -1);
078e4539 1371
487393e9
LP
1372 if (parse_config_file() < 0)
1373 goto finish;
1374
67445f4e 1375 if (arg_running_as == SYSTEMD_SYSTEM)
a5dab5ce
LP
1376 if (parse_proc_cmdline() < 0)
1377 goto finish;
f170852a
LP
1378
1379 log_parse_environment();
1380
1381 if (parse_argv(argc, argv) < 0)
1382 goto finish;
1383
6bae23a0
TB
1384 if (arg_action == ACTION_TEST &&
1385 geteuid() == 0) {
b5c6cf87
LP
1386 log_error("Don't run test mode as root.");
1387 goto finish;
1388 }
1389
6bae23a0
TB
1390 if (arg_running_as == SYSTEMD_USER &&
1391 arg_action == ACTION_RUN &&
1392 sd_booted() <= 0) {
1393 log_error("Trying to run as user instance, but the system has not been booted with systemd.");
1394 goto finish;
1395 }
1396
67445f4e 1397 if (arg_running_as == SYSTEMD_SYSTEM &&
fe783b03
LP
1398 arg_action == ACTION_RUN &&
1399 running_in_chroot() > 0) {
1400 log_error("Cannot be run in a chroot() environment.");
1401 goto finish;
1402 }
1403
fa0f4d8a 1404 if (arg_action == ACTION_HELP) {
f170852a
LP
1405 retval = help();
1406 goto finish;
9ba0bc4e
ZJS
1407 } else if (arg_action == ACTION_VERSION) {
1408 retval = version();
1409 goto finish;
fa0f4d8a 1410 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
e537352b 1411 unit_dump_config_items(stdout);
22f4096c 1412 retval = EXIT_SUCCESS;
e537352b 1413 goto finish;
fa0f4d8a 1414 } else if (arg_action == ACTION_DONE) {
22f4096c 1415 retval = EXIT_SUCCESS;
4288f619 1416 goto finish;
f170852a
LP
1417 }
1418
8be28fb1
KS
1419 if (arg_running_as == SYSTEMD_USER &&
1420 !getenv("XDG_RUNTIME_DIR")) {
1421 log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
1422 goto finish;
1423 }
1424
fa0f4d8a 1425 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
f170852a 1426
871e5809
LP
1427 /* Close logging fds, in order not to confuse fdset below */
1428 log_close();
1429
a16e1123 1430 /* Remember open file descriptors for later deserialization */
01e10de3
LP
1431 r = fdset_new_fill(&fds);
1432 if (r < 0) {
1433 log_error("Failed to allocate fd set: %s", strerror(-r));
1434 goto finish;
1435 } else
1436 fdset_cloexec(fds, true);
a16e1123 1437
01e10de3 1438 if (serialization)
a16e1123 1439 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
a16e1123 1440
90dc8c2e 1441 if (arg_running_as == SYSTEMD_SYSTEM)
80876c20
LP
1442 /* Become a session leader if we aren't one yet. */
1443 setsid();
4ade7963 1444
befb5b6a
LP
1445 /* Move out of the way, so that we won't block unmounts */
1446 assert_se(chdir("/") == 0);
1447
2146621b
LP
1448 /* Reset the console, but only if this is really init and we
1449 * are freshly booted */
67445f4e 1450 if (arg_running_as == SYSTEMD_SYSTEM && arg_action == ACTION_RUN)
2660882b 1451 console_setup(getpid() == 1 && !skip_setup);
4ade7963 1452
18149b9f 1453 /* Open the logging devices, if possible and necessary */
843d2643 1454 log_open();
4ade7963 1455
5373d602
LP
1456 /* Make sure we leave a core dump without panicing the
1457 * kernel. */
ab422445 1458 if (getpid() == 1) {
4fc935ca 1459 install_crash_handler();
97c4f35c 1460
0c85a4f3
LP
1461 r = mount_cgroup_controllers(arg_join_controllers);
1462 if (r < 0)
1463 goto finish;
1464 }
1465
67445f4e 1466 if (arg_running_as == SYSTEMD_SYSTEM) {
c20f5ac7
LP
1467 const char *virtualization = NULL;
1468
bc270841 1469 log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES ")");
c20f5ac7
LP
1470
1471 detect_virtualization(&virtualization);
1472 if (virtualization)
1473 log_info("Detected virtualization '%s'.", virtualization);
1474
26a1efdf
LP
1475 if (in_initrd())
1476 log_info("Running in initial RAM disk.");
1477
d8160f21
ZJS
1478 } else {
1479 _cleanup_free_ char *t = uid_to_name(getuid());
1480 log_debug(PACKAGE_STRING " running in user mode for user "PID_FMT"/%s. (" SYSTEMD_FEATURES ")",
1481 getuid(), t);
1482 }
a5dab5ce 1483
67445f4e 1484 if (arg_running_as == SYSTEMD_SYSTEM && !skip_setup) {
6faa1114 1485 if (arg_show_status || plymouth_running())
888c6216
LP
1486 status_welcome();
1487
e3043162 1488#ifdef HAVE_KMOD
b43b8f7a
KS
1489 if (detect_container(NULL) <= 0)
1490 kmod_setup();
e3043162 1491#endif
888c6216 1492 hostname_setup();
d7ccca2e 1493 machine_id_setup();
888c6216 1494 loopback_setup();
490aed58 1495
6ee5bbf8 1496 test_mtab();
80758717 1497 test_usr();
871c44a7 1498 test_cgroups();
af5bc85d 1499 }
302e8c4c 1500
67445f4e 1501 if (arg_running_as == SYSTEMD_SYSTEM && arg_runtime_watchdog > 0)
e96d6be7
LP
1502 watchdog_set_timeout(&arg_runtime_watchdog);
1503
aa0f64ac
LP
1504 if (arg_timer_slack_nsec != (nsec_t) -1)
1505 if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0)
1506 log_error("Failed to adjust timer slack: %m");
1507
ec8927ca 1508 if (arg_capability_bounding_set_drop) {
31c885e9 1509 r = capability_bounding_set_drop_usermode(arg_capability_bounding_set_drop);
ec8927ca 1510 if (r < 0) {
31c885e9 1511 log_error("Failed to drop capability bounding set of usermode helpers: %s", strerror(-r));
ec8927ca
LP
1512 goto finish;
1513 }
31c885e9 1514 r = capability_bounding_set_drop(arg_capability_bounding_set_drop, true);
939b8f14 1515 if (r < 0) {
31c885e9 1516 log_error("Failed to drop capability bounding set: %s", strerror(-r));
939b8f14
LP
1517 goto finish;
1518 }
ec8927ca
LP
1519 }
1520
67445f4e 1521 if (arg_running_as == SYSTEMD_USER) {
d4447f4d 1522 /* Become reaper of our children */
8b8ffe68
LP
1523 if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) {
1524 log_warning("Failed to make us a subreaper: %m");
1525 if (errno == EINVAL)
ddfa5101 1526 log_info("Perhaps the kernel version is too old (< 3.4?)");
8b8ffe68 1527 }
d4447f4d
AK
1528 }
1529
67445f4e 1530 if (arg_running_as == SYSTEMD_SYSTEM)
4096d6f5
LP
1531 bump_rlimit_nofile(&saved_rlimit_nofile);
1532
e3dd987c 1533 r = manager_new(arg_running_as, &m);
e96d6be7 1534 if (r < 0) {
8e274523 1535 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
1536 goto finish;
1537 }
1538
9e58ff9c 1539 m->confirm_spawn = arg_confirm_spawn;
0a494f1f
LP
1540 m->default_std_output = arg_default_std_output;
1541 m->default_std_error = arg_default_std_error;
1f19a534
OS
1542 m->default_restart_usec = arg_default_restart_usec;
1543 m->default_timeout_start_usec = arg_default_timeout_start_usec;
1544 m->default_timeout_stop_usec = arg_default_timeout_stop_usec;
3f41e1e5
LN
1545 m->default_start_limit_interval = arg_default_start_limit_interval;
1546 m->default_start_limit_burst = arg_default_start_limit_burst;
e96d6be7
LP
1547 m->runtime_watchdog = arg_runtime_watchdog;
1548 m->shutdown_watchdog = arg_shutdown_watchdog;
c3a170f3
HH
1549 m->userspace_timestamp = userspace_timestamp;
1550 m->kernel_timestamp = kernel_timestamp;
c3a170f3 1551 m->initrd_timestamp = initrd_timestamp;
c2e0d600
TA
1552 m->security_start_timestamp = security_start_timestamp;
1553 m->security_finish_timestamp = security_finish_timestamp;
9e58ff9c 1554
c93ff2e9
FC
1555 manager_set_default_rlimits(m, arg_default_rlimit);
1556
97d0e5f8 1557 if (arg_default_environment)
718db961 1558 manager_environment_add(m, NULL, arg_default_environment);
97d0e5f8 1559
27d340c7
LP
1560 manager_set_show_status(m, arg_show_status);
1561
bf4df7c3
LP
1562 /* Remember whether we should queue the default job */
1563 queue_default_job = !serialization || arg_switched_root;
1564
9d76d730
LP
1565 before_startup = now(CLOCK_MONOTONIC);
1566
e96d6be7
LP
1567 r = manager_startup(m, serialization, fds);
1568 if (r < 0)
6e2ef85b 1569 log_error("Failed to fully start up daemon: %s", strerror(-r));
a16e1123 1570
bf4df7c3
LP
1571 /* This will close all file descriptors that were opened, but
1572 * not claimed by any unit. */
01e10de3 1573 fdset_free(fds);
6c081276 1574 fds = NULL;
f50e0a01 1575
a16e1123
LP
1576 if (serialization) {
1577 fclose(serialization);
1578 serialization = NULL;
bf4df7c3
LP
1579 }
1580
1581 if (queue_default_job) {
718db961 1582 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1c27d3f3 1583 Unit *target = NULL;
bacbccb7 1584 Job *default_unit_job;
398ef8ba 1585
fa0f4d8a 1586 log_debug("Activating default unit: %s", arg_default_unit);
a16e1123 1587
e96d6be7 1588 r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
718db961
LP
1589 if (r < 0)
1590 log_error("Failed to load default target: %s", bus_error_message(&error, r));
1591 else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND)
ac155bb8
MS
1592 log_error("Failed to load default target: %s", strerror(-target->load_error));
1593 else if (target->load_state == UNIT_MASKED)
6daf4f90 1594 log_error("Default target masked.");
27b14a22 1595
ac155bb8 1596 if (!target || target->load_state != UNIT_LOADED) {
a16e1123 1597 log_info("Trying to load rescue target...");
1c27d3f3 1598
e96d6be7
LP
1599 r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
1600 if (r < 0) {
718db961 1601 log_error("Failed to load rescue target: %s", bus_error_message(&error, r));
a16e1123 1602 goto finish;
11ddb6f4 1603 } else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) {
ac155bb8 1604 log_error("Failed to load rescue target: %s", strerror(-target->load_error));
1c27d3f3 1605 goto finish;
ac155bb8 1606 } else if (target->load_state == UNIT_MASKED) {
6daf4f90 1607 log_error("Rescue target masked.");
00dc5d76 1608 goto finish;
a16e1123
LP
1609 }
1610 }
37d88da7 1611
ac155bb8 1612 assert(target->load_state == UNIT_LOADED);
00dc5d76 1613
fa0f4d8a 1614 if (arg_action == ACTION_TEST) {
40d50879 1615 printf("-> By units:\n");
a16e1123
LP
1616 manager_dump_units(m, stdout, "\t");
1617 }
1618
95f1b47d 1619 r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, false, &error, &default_unit_job);
ab17a050 1620 if (r == -EPERM) {
718db961 1621 log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r));
ab17a050
LP
1622
1623 r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &default_unit_job);
1624 if (r < 0) {
718db961 1625 log_error("Failed to start default target: %s", bus_error_message(&error, r));
ab17a050
LP
1626 goto finish;
1627 }
1628 } else if (r < 0) {
718db961 1629 log_error("Failed to isolate default target: %s", bus_error_message(&error, r));
37d88da7
LP
1630 goto finish;
1631 }
ab17a050 1632
bacbccb7 1633 m->default_unit_job_id = default_unit_job->id;
60918275 1634
07672f49
LP
1635 after_startup = now(CLOCK_MONOTONIC);
1636 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1637 "Loaded units and determined initial transaction in %s.",
2fa4092c 1638 format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 0));
07672f49 1639
fa0f4d8a 1640 if (arg_action == ACTION_TEST) {
40d50879 1641 printf("-> By jobs:\n");
a16e1123 1642 manager_dump_jobs(m, stdout, "\t");
22f4096c 1643 retval = EXIT_SUCCESS;
a16e1123
LP
1644 goto finish;
1645 }
e965d56d 1646 }
d46de8a1 1647
a16e1123 1648 for (;;) {
e96d6be7
LP
1649 r = manager_loop(m);
1650 if (r < 0) {
a16e1123
LP
1651 log_error("Failed to run mainloop: %s", strerror(-r));
1652 goto finish;
1653 }
11dd41ce 1654
a16e1123 1655 switch (m->exit_code) {
e965d56d 1656
a16e1123 1657 case MANAGER_EXIT:
22f4096c 1658 retval = EXIT_SUCCESS;
a16e1123
LP
1659 log_debug("Exit.");
1660 goto finish;
e965d56d 1661
a16e1123 1662 case MANAGER_RELOAD:
e015090f 1663 log_info("Reloading.");
e96d6be7
LP
1664 r = manager_reload(m);
1665 if (r < 0)
a16e1123
LP
1666 log_error("Failed to reload: %s", strerror(-r));
1667 break;
cea8e32e 1668
a16e1123 1669 case MANAGER_REEXECUTE:
664f88a7 1670
b3680f49 1671 if (prepare_reexecute(m, &serialization, &fds, false) < 0)
a16e1123 1672 goto finish;
60918275 1673
a16e1123 1674 reexecute = true;
e015090f 1675 log_notice("Reexecuting.");
a16e1123
LP
1676 goto finish;
1677
664f88a7
LP
1678 case MANAGER_SWITCH_ROOT:
1679 /* Steal the switch root parameters */
41669317 1680 switch_root_dir = m->switch_root;
664f88a7
LP
1681 switch_root_init = m->switch_root_init;
1682 m->switch_root = m->switch_root_init = NULL;
1683
1684 if (!switch_root_init)
b3680f49 1685 if (prepare_reexecute(m, &serialization, &fds, true) < 0)
664f88a7
LP
1686 goto finish;
1687
1688 reexecute = true;
1689 log_notice("Switching root.");
1690 goto finish;
1691
b9080b03
FF
1692 case MANAGER_REBOOT:
1693 case MANAGER_POWEROFF:
1694 case MANAGER_HALT:
1695 case MANAGER_KEXEC: {
1696 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1697 [MANAGER_REBOOT] = "reboot",
1698 [MANAGER_POWEROFF] = "poweroff",
1699 [MANAGER_HALT] = "halt",
1700 [MANAGER_KEXEC] = "kexec"
1701 };
1702
1703 assert_se(shutdown_verb = table[m->exit_code]);
e96d6be7 1704 arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
b9080b03
FF
1705
1706 log_notice("Shutting down.");
1707 goto finish;
1708 }
1709
a16e1123
LP
1710 default:
1711 assert_not_reached("Unknown exit code.");
1712 }
1713 }
f170852a 1714
60918275
LP
1715finish:
1716 if (m)
1717 manager_free(m);
1718
c93ff2e9 1719 for (j = 0; j < RLIMIT_NLIMITS; j++)
4096d6f5 1720 free(arg_default_rlimit[j]);
c93ff2e9 1721
fa0f4d8a 1722 free(arg_default_unit);
0c85a4f3 1723 free_join_controllers();
b9cd2ec1 1724
b2bb3dbe
LP
1725 label_finish();
1726
a16e1123 1727 if (reexecute) {
664f88a7 1728 const char **args;
e564a982 1729 unsigned i, args_size;
a16e1123 1730
664f88a7
LP
1731 /* Close and disarm the watchdog, so that the new
1732 * instance can reinitialize it, but doesn't get
1733 * rebooted while we do that */
1734 watchdog_close(true);
a16e1123 1735
4096d6f5
LP
1736 /* Reset the RLIMIT_NOFILE to the kernel default, so
1737 * that the new systemd can pass the kernel default to
1738 * its child processes */
1739 if (saved_rlimit_nofile.rlim_cur > 0)
1740 setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile);
1741
41669317 1742 if (switch_root_dir) {
cee530bb
LP
1743 /* Kill all remaining processes from the
1744 * initrd, but don't wait for them, so that we
1745 * can handle the SIGCHLD for them after
1746 * deserializing. */
0bee65f0 1747 broadcast_signal(SIGTERM, false, true);
bd3fa1d2
LP
1748
1749 /* And switch root */
41669317
LP
1750 r = switch_root(switch_root_dir);
1751 if (r < 0)
1752 log_error("Failed to switch root, ignoring: %s", strerror(-r));
1753 }
a16e1123 1754
d03bc1b8 1755 args_size = MAX(6, argc+1);
e564a982 1756 args = newa(const char*, args_size);
a16e1123 1757
664f88a7
LP
1758 if (!switch_root_init) {
1759 char sfd[16];
a16e1123 1760
664f88a7
LP
1761 /* First try to spawn ourselves with the right
1762 * path, and with full serialization. We do
1763 * this only if the user didn't specify an
1764 * explicit init to spawn. */
edb9aaa8 1765
664f88a7
LP
1766 assert(serialization);
1767 assert(fds);
edb9aaa8 1768
664f88a7
LP
1769 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1770 char_array_0(sfd);
edb9aaa8 1771
664f88a7
LP
1772 i = 0;
1773 args[i++] = SYSTEMD_BINARY_PATH;
41669317 1774 if (switch_root_dir)
2660882b 1775 args[i++] = "--switched-root";
67445f4e 1776 args[i++] = arg_running_as == SYSTEMD_SYSTEM ? "--system" : "--user";
664f88a7
LP
1777 args[i++] = "--deserialize";
1778 args[i++] = sfd;
1779 args[i++] = NULL;
edb9aaa8 1780
ce8aba56
KS
1781 /* do not pass along the environment we inherit from the kernel or initrd */
1782 if (switch_root_dir)
1783 clearenv();
1784
e564a982 1785 assert(i <= args_size);
664f88a7
LP
1786 execv(args[0], (char* const*) args);
1787 }
6e98720f 1788
664f88a7
LP
1789 /* Try the fallback, if there is any, without any
1790 * serialization. We pass the original argv[] and
1791 * envp[]. (Well, modulo the ordering changes due to
1792 * getopt() in argv[], and some cleanups in envp[],
1793 * but let's hope that doesn't matter.) */
a16e1123 1794
b8f83232 1795 if (serialization) {
664f88a7 1796 fclose(serialization);
b8f83232
LP
1797 serialization = NULL;
1798 }
a16e1123 1799
b8f83232 1800 if (fds) {
664f88a7 1801 fdset_free(fds);
b8f83232
LP
1802 fds = NULL;
1803 }
a16e1123 1804
a504223d
HH
1805 /* Reopen the console */
1806 make_console_stdio();
1807
b8f83232 1808 for (j = 1, i = 1; j < argc; j++)
664f88a7 1809 args[i++] = argv[j];
a16e1123 1810 args[i++] = NULL;
e564a982 1811 assert(i <= args_size);
b8f83232
LP
1812
1813 if (switch_root_init) {
1814 args[0] = switch_root_init;
1815 execv(args[0], (char* const*) args);
1816 log_warning("Failed to execute configured init, trying fallback: %m");
1817 }
1818
1819 args[0] = "/sbin/init";
a16e1123
LP
1820 execv(args[0], (char* const*) args);
1821
745e2fb7
KS
1822 if (errno == ENOENT) {
1823 log_warning("No /sbin/init, trying fallback");
b8f83232 1824
745e2fb7
KS
1825 args[0] = "/bin/sh";
1826 args[1] = NULL;
1827 execv(args[0], (char* const*) args);
1828 log_error("Failed to execute /bin/sh, giving up: %m");
1829 } else
1830 log_warning("Failed to execute /sbin/init, giving up: %m");
a16e1123
LP
1831 }
1832
1833 if (serialization)
1834 fclose(serialization);
1835
1836 if (fds)
1837 fdset_free(fds);
1838
54b434b1
LP
1839#ifdef HAVE_VALGRIND_VALGRIND_H
1840 /* If we are PID 1 and running under valgrind, then let's exit
1841 * here explicitly. valgrind will only generate nice output on
1842 * exit(), not on exec(), hence let's do the former not the
1843 * latter here. */
1844 if (getpid() == 1 && RUNNING_ON_VALGRIND)
1845 return 0;
1846#endif
1847
b9080b03
FF
1848 if (shutdown_verb) {
1849 const char * command_line[] = {
1850 SYSTEMD_SHUTDOWN_BINARY_PATH,
1851 shutdown_verb,
1852 NULL
1853 };
e3e45d4f
SP
1854 _cleanup_strv_free_ char **env_block = NULL;
1855 env_block = strv_copy(environ);
b9080b03 1856
e96d6be7 1857 if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
e3e45d4f 1858 char *e;
d18f337c 1859
e96d6be7
LP
1860 /* If we reboot let's set the shutdown
1861 * watchdog and tell the shutdown binary to
1862 * repeatedly ping it */
1863 watchdog_set_timeout(&arg_shutdown_watchdog);
1864 watchdog_close(false);
1865
e3e45d4f
SP
1866 /* Tell the binary how often to ping, ignore failure */
1867 if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
1868 strv_push(&env_block, e);
1869 } else
e96d6be7
LP
1870 watchdog_close(true);
1871
66713f77
LP
1872 /* Avoid the creation of new processes forked by the
1873 * kernel; at this point, we will not listen to the
1874 * signals anyway */
1875 if (detect_container(NULL) <= 0)
1876 cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
ad929bcc 1877
d18f337c 1878 execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
b9080b03
FF
1879 log_error("Failed to execute shutdown binary, freezing: %m");
1880 }
1881
c3b3c274
LP
1882 if (getpid() == 1)
1883 freeze();
1884
60918275
LP
1885 return retval;
1886}