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