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