]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/main.c
systemctl: fix parsing of LoadError property for systemctl show
[thirdparty/systemd.git] / src / 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
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
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
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
ea430986
LP
22#include <dbus/dbus.h>
23
60918275
LP
24#include <stdio.h>
25#include <errno.h>
26#include <string.h>
16354eff 27#include <unistd.h>
4ade7963
LP
28#include <sys/types.h>
29#include <sys/stat.h>
f170852a 30#include <getopt.h>
97c4f35c 31#include <signal.h>
4fc935ca 32#include <sys/wait.h>
80876c20 33#include <fcntl.h>
f3b6a3ed 34#include <sys/prctl.h>
60918275
LP
35
36#include "manager.h"
16354eff 37#include "log.h"
4ade7963 38#include "mount-setup.h"
302e8c4c 39#include "hostname-setup.h"
af5bc85d 40#include "loopback-setup.h"
11c3a4ee 41#include "kmod-setup.h"
72bca11b 42#include "locale-setup.h"
c4dcdb9f 43#include "selinux-setup.h"
d7ccca2e 44#include "machine-id-setup.h"
302e8c4c 45#include "load-fragment.h"
a16e1123 46#include "fdset.h"
514f4ef5 47#include "special.h"
487393e9 48#include "conf-parser.h"
398ef8ba 49#include "bus-errors.h"
ad780f19 50#include "missing.h"
e51bc1a2 51#include "label.h"
302e27c8 52#include "build.h"
06d4c99a 53#include "strv.h"
f6a6225e 54#include "def.h"
60918275 55
f170852a
LP
56static enum {
57 ACTION_RUN,
e965d56d 58 ACTION_HELP,
e537352b 59 ACTION_TEST,
4288f619
LP
60 ACTION_DUMP_CONFIGURATION_ITEMS,
61 ACTION_DONE
fa0f4d8a 62} arg_action = ACTION_RUN;
f170852a 63
fa0f4d8a
LP
64static char *arg_default_unit = NULL;
65static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
66
67static bool arg_dump_core = true;
68static bool arg_crash_shell = false;
69static int arg_crash_chvt = -1;
70static bool arg_confirm_spawn = false;
9e58ff9c 71static bool arg_show_status = true;
07459bb6 72#ifdef HAVE_SYSV_COMPAT
6e98720f 73static bool arg_sysv_console = true;
07459bb6 74#endif
d3689161 75static bool arg_mount_auto = true;
173a8d04 76static bool arg_swap_auto = true;
06d4c99a 77static char **arg_default_controllers = NULL;
934da035 78static ExecOutput arg_default_std_output = EXEC_OUTPUT_INHERIT;
0a494f1f 79static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
4fc935ca 80
a16e1123 81static FILE* serialization = NULL;
80876c20 82
6f5e3f35
LP
83static void nop_handler(int sig) {
84}
85
93a46b0b 86_noreturn_ static void crash(int sig) {
97c4f35c 87
fa0f4d8a 88 if (!arg_dump_core)
582a507f 89 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
97c4f35c 90 else {
6f5e3f35 91 struct sigaction sa;
97c4f35c
LP
92 pid_t pid;
93
6f5e3f35
LP
94 /* We want to wait for the core process, hence let's enable SIGCHLD */
95 zero(sa);
96 sa.sa_handler = nop_handler;
97 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
98 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
99
97c4f35c 100 if ((pid = fork()) < 0)
582a507f 101 log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
97c4f35c
LP
102
103 else if (pid == 0) {
97c4f35c
LP
104 struct rlimit rl;
105
106 /* Enable default signal handler for core dump */
107 zero(sa);
108 sa.sa_handler = SIG_DFL;
109 assert_se(sigaction(sig, &sa, NULL) == 0);
110
111 /* Don't limit the core dump size */
112 zero(rl);
113 rl.rlim_cur = RLIM_INFINITY;
114 rl.rlim_max = RLIM_INFINITY;
115 setrlimit(RLIMIT_CORE, &rl);
116
117 /* Just to be sure... */
118 assert_se(chdir("/") == 0);
119
120 /* Raise the signal again */
121 raise(sig);
122
123 assert_not_reached("We shouldn't be here...");
124 _exit(1);
4fc935ca
LP
125
126 } else {
8e12a6ae
LP
127 siginfo_t status;
128 int r;
4fc935ca
LP
129
130 /* Order things nicely. */
8e12a6ae
LP
131 if ((r = wait_for_terminate(pid, &status)) < 0)
132 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
133 else if (status.si_code != CLD_DUMPED)
582a507f 134 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
4fc935ca 135 else
582a507f 136 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
97c4f35c
LP
137 }
138 }
139
fa0f4d8a
LP
140 if (arg_crash_chvt)
141 chvt(arg_crash_chvt);
601f6a1e 142
fa0f4d8a 143 if (arg_crash_shell) {
6f5e3f35
LP
144 struct sigaction sa;
145 pid_t pid;
8c43883a 146
4fc935ca
LP
147 log_info("Executing crash shell in 10s...");
148 sleep(10);
149
6f5e3f35
LP
150 /* Let the kernel reap children for us */
151 zero(sa);
152 sa.sa_handler = SIG_IGN;
153 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
154 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
8c43883a 155
6f5e3f35
LP
156 if ((pid = fork()) < 0)
157 log_error("Failed to fork off crash shell: %s", strerror(errno));
158 else if (pid == 0) {
843d2643 159 int fd, r;
ea5652c2 160
21de3988 161 if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
ea5652c2 162 log_error("Failed to acquire terminal: %s", strerror(-fd));
5b2a0903 163 else if ((r = make_stdio(fd)) < 0)
843d2643 164 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
ea5652c2 165
6f5e3f35
LP
166 execl("/bin/sh", "/bin/sh", NULL);
167
168 log_error("execl() failed: %s", strerror(errno));
169 _exit(1);
170 }
c99b188e 171
f8e08a77 172 log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
4fc935ca
LP
173 }
174
175 log_info("Freezing execution.");
97c4f35c
LP
176 freeze();
177}
178
179static void install_crash_handler(void) {
180 struct sigaction sa;
181
182 zero(sa);
183
184 sa.sa_handler = crash;
185 sa.sa_flags = SA_NODEFER;
186
1b91d3e8 187 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
97c4f35c 188}
f170852a 189
843d2643
LP
190static int console_setup(bool do_reset) {
191 int tty_fd, r;
80876c20 192
843d2643
LP
193 /* If we are init, we connect stdin/stdout/stderr to /dev/null
194 * and make sure we don't have a controlling tty. */
80876c20 195
843d2643
LP
196 release_terminal();
197
198 if (!do_reset)
199 return 0;
80876c20 200
843d2643
LP
201 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
202 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
203 return -tty_fd;
204 }
80876c20 205
6ea832a2 206 if ((r = reset_terminal_fd(tty_fd)) < 0)
843d2643
LP
207 log_error("Failed to reset /dev/console: %s", strerror(-r));
208
209 close_nointr_nofail(tty_fd);
80876c20
LP
210 return r;
211}
212
f170852a
LP
213static int set_default_unit(const char *u) {
214 char *c;
215
216 assert(u);
217
218 if (!(c = strdup(u)))
219 return -ENOMEM;
220
fa0f4d8a
LP
221 free(arg_default_unit);
222 arg_default_unit = c;
f170852a
LP
223 return 0;
224}
225
226static int parse_proc_cmdline_word(const char *word) {
227
228 static const char * const rlmap[] = {
ed370f5d 229 "emergency", SPECIAL_EMERGENCY_TARGET,
099663ff 230 "-b", SPECIAL_EMERGENCY_TARGET,
ed370f5d
LP
231 "single", SPECIAL_RESCUE_TARGET,
232 "-s", SPECIAL_RESCUE_TARGET,
233 "s", SPECIAL_RESCUE_TARGET,
234 "S", SPECIAL_RESCUE_TARGET,
235 "1", SPECIAL_RESCUE_TARGET,
236 "2", SPECIAL_RUNLEVEL2_TARGET,
237 "3", SPECIAL_RUNLEVEL3_TARGET,
238 "4", SPECIAL_RUNLEVEL4_TARGET,
239 "5", SPECIAL_RUNLEVEL5_TARGET,
f170852a
LP
240 };
241
5192bd19
LP
242 assert(word);
243
2f198e2f
LP
244 if (startswith(word, "systemd.unit="))
245 return set_default_unit(word + 13);
f170852a
LP
246
247 else if (startswith(word, "systemd.log_target=")) {
248
249 if (log_set_target_from_string(word + 19) < 0)
250 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
251
252 } else if (startswith(word, "systemd.log_level=")) {
253
254 if (log_set_max_level_from_string(word + 18) < 0)
255 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
256
bbe63281
LP
257 } else if (startswith(word, "systemd.log_color=")) {
258
259 if (log_show_color_from_string(word + 18) < 0)
260 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
261
262 } else if (startswith(word, "systemd.log_location=")) {
263
264 if (log_show_location_from_string(word + 21) < 0)
265 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
266
4fc935ca
LP
267 } else if (startswith(word, "systemd.dump_core=")) {
268 int r;
269
270 if ((r = parse_boolean(word + 18)) < 0)
601f6a1e 271 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
4fc935ca 272 else
fa0f4d8a 273 arg_dump_core = r;
4fc935ca
LP
274
275 } else if (startswith(word, "systemd.crash_shell=")) {
276 int r;
277
278 if ((r = parse_boolean(word + 20)) < 0)
601f6a1e 279 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
4fc935ca 280 else
fa0f4d8a 281 arg_crash_shell = r;
5e7ee61c
LP
282
283 } else if (startswith(word, "systemd.confirm_spawn=")) {
284 int r;
285
286 if ((r = parse_boolean(word + 22)) < 0)
287 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
288 else
fa0f4d8a 289 arg_confirm_spawn = r;
5e7ee61c 290
601f6a1e
LP
291 } else if (startswith(word, "systemd.crash_chvt=")) {
292 int k;
293
294 if (safe_atoi(word + 19, &k) < 0)
295 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
296 else
fa0f4d8a 297 arg_crash_chvt = k;
601f6a1e 298
9e58ff9c
LP
299 } else if (startswith(word, "systemd.show_status=")) {
300 int r;
301
302 if ((r = parse_boolean(word + 20)) < 0)
303 log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
6e98720f 304 else
9e58ff9c 305 arg_show_status = r;
0a494f1f
LP
306 } else if (startswith(word, "systemd.default_standard_output=")) {
307 int r;
308
309 if ((r = exec_output_from_string(word + 32)) < 0)
310 log_warning("Failed to parse default standard output switch %s, Ignoring.", word + 32);
311 else
312 arg_default_std_output = r;
313 } else if (startswith(word, "systemd.default_standard_error=")) {
314 int r;
315
316 if ((r = exec_output_from_string(word + 31)) < 0)
317 log_warning("Failed to parse default standard error switch %s, Ignoring.", word + 31);
318 else
319 arg_default_std_error = r;
07459bb6 320#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
321 } else if (startswith(word, "systemd.sysv_console=")) {
322 int r;
323
324 if ((r = parse_boolean(word + 21)) < 0)
325 log_warning("Failed to parse SysV console switch %s, Ignoring.", word + 20);
326 else
327 arg_sysv_console = r;
07459bb6 328#endif
9e58ff9c 329
4fc935ca
LP
330 } else if (startswith(word, "systemd.")) {
331
332 log_warning("Unknown kernel switch %s. Ignoring.", word);
333
bbe63281
LP
334 log_info("Supported kernel switches:\n"
335 "systemd.unit=UNIT Default unit to start\n"
bbe63281 336 "systemd.dump_core=0|1 Dump core on crash\n"
9e58ff9c 337 "systemd.crash_shell=0|1 Run shell on crash\n"
bbe63281 338 "systemd.crash_chvt=N Change to VT #N on crash\n"
9e58ff9c 339 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
6e98720f 340 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
07459bb6 341#ifdef HAVE_SYSV_COMPAT
6e98720f 342 "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
07459bb6 343#endif
87d1969b 344 "systemd.log_target=console|kmsg|syslog|syslog-or-kmsg|null\n"
6e98720f
LP
345 " Log target\n"
346 "systemd.log_level=LEVEL Log level\n"
347 "systemd.log_color=0|1 Highlight important log messages\n"
0a494f1f
LP
348 "systemd.log_location=0|1 Include code location in log messages\n"
349 "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
350 " Set default log output for services\n"
351 "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
352 " Set default log error output for services\n");
4fc935ca 353
a9c501a5 354 } else if (streq(word, "quiet")) {
6e98720f 355 arg_show_status = false;
07459bb6 356#ifdef HAVE_SYSV_COMPAT
6e98720f 357 arg_sysv_console = false;
07459bb6 358#endif
9e58ff9c 359 } else {
f170852a
LP
360 unsigned i;
361
362 /* SysV compatibility */
f170852a
LP
363 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
364 if (streq(word, rlmap[i]))
365 return set_default_unit(rlmap[i+1]);
366 }
367
368 return 0;
369}
370
487393e9
LP
371static int config_parse_level(
372 const char *filename,
373 unsigned line,
374 const char *section,
375 const char *lvalue,
3731f1ea 376 int ltype,
487393e9
LP
377 const char *rvalue,
378 void *data,
379 void *userdata) {
380
381 assert(filename);
382 assert(lvalue);
383 assert(rvalue);
384
385 log_set_max_level_from_string(rvalue);
386 return 0;
387}
388
389static int config_parse_target(
390 const char *filename,
391 unsigned line,
392 const char *section,
393 const char *lvalue,
3731f1ea 394 int ltype,
487393e9
LP
395 const char *rvalue,
396 void *data,
397 void *userdata) {
398
399 assert(filename);
400 assert(lvalue);
401 assert(rvalue);
402
403 log_set_target_from_string(rvalue);
404 return 0;
405}
406
407static int config_parse_color(
408 const char *filename,
409 unsigned line,
410 const char *section,
411 const char *lvalue,
3731f1ea 412 int ltype,
487393e9
LP
413 const char *rvalue,
414 void *data,
415 void *userdata) {
416
417 assert(filename);
418 assert(lvalue);
419 assert(rvalue);
420
421 log_show_color_from_string(rvalue);
422 return 0;
423}
424
425static int config_parse_location(
426 const char *filename,
427 unsigned line,
428 const char *section,
429 const char *lvalue,
3731f1ea 430 int ltype,
487393e9
LP
431 const char *rvalue,
432 void *data,
433 void *userdata) {
434
435 assert(filename);
436 assert(lvalue);
437 assert(rvalue);
438
439 log_show_location_from_string(rvalue);
440 return 0;
441}
442
443static int config_parse_cpu_affinity(
444 const char *filename,
445 unsigned line,
446 const char *section,
447 const char *lvalue,
3731f1ea 448 int ltype,
487393e9
LP
449 const char *rvalue,
450 void *data,
451 void *userdata) {
452
453 char *w;
454 size_t l;
455 char *state;
456 cpu_set_t *c = NULL;
457 unsigned ncpus = 0;
458
459 assert(filename);
460 assert(lvalue);
461 assert(rvalue);
462
f60f22df 463 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
487393e9
LP
464 char *t;
465 int r;
466 unsigned cpu;
467
468 if (!(t = strndup(w, l)))
469 return -ENOMEM;
470
471 r = safe_atou(t, &cpu);
472 free(t);
473
474 if (!c)
475 if (!(c = cpu_set_malloc(&ncpus)))
476 return -ENOMEM;
477
478 if (r < 0 || cpu >= ncpus) {
479 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
480 CPU_FREE(c);
481 return -EBADMSG;
482 }
483
484 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
485 }
486
487 if (c) {
488 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
489 log_warning("Failed to set CPU affinity: %m");
490
491 CPU_FREE(c);
492 }
493
494 return 0;
495}
496
0a494f1f
LP
497static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
498
487393e9
LP
499static int parse_config_file(void) {
500
501 const ConfigItem items[] = {
2b583ce6
KS
502 { "LogLevel", config_parse_level, 0, NULL, "Manager" },
503 { "LogTarget", config_parse_target, 0, NULL, "Manager" },
504 { "LogColor", config_parse_color, 0, NULL, "Manager" },
505 { "LogLocation", config_parse_location, 0, NULL, "Manager" },
506 { "DumpCore", config_parse_bool, 0, &arg_dump_core, "Manager" },
507 { "CrashShell", config_parse_bool, 0, &arg_crash_shell, "Manager" },
508 { "ShowStatus", config_parse_bool, 0, &arg_show_status, "Manager" },
07459bb6 509#ifdef HAVE_SYSV_COMPAT
2b583ce6 510 { "SysVConsole", config_parse_bool, 0, &arg_sysv_console, "Manager" },
07459bb6 511#endif
2b583ce6
KS
512 { "CrashChVT", config_parse_int, 0, &arg_crash_chvt, "Manager" },
513 { "CPUAffinity", config_parse_cpu_affinity, 0, NULL, "Manager" },
514 { "MountAuto", config_parse_bool, 0, &arg_mount_auto, "Manager" },
515 { "SwapAuto", config_parse_bool, 0, &arg_swap_auto, "Manager" },
516 { "DefaultControllers", config_parse_strv, 0, &arg_default_controllers, "Manager" },
517 { "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output, "Manager" },
518 { "DefaultStandardError", config_parse_output, 0, &arg_default_std_error, "Manager" },
519 { NULL, NULL, 0, NULL, NULL }
487393e9
LP
520 };
521
522 static const char * const sections[] = {
523 "Manager",
524 NULL
525 };
526
527 FILE *f;
528 const char *fn;
529 int r;
530
af2d49f7 531 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
487393e9
LP
532
533 if (!(f = fopen(fn, "re"))) {
534 if (errno == ENOENT)
535 return 0;
536
537 log_warning("Failed to open configuration file '%s': %m", fn);
538 return 0;
539 }
540
541 if ((r = config_parse(fn, f, sections, items, false, NULL)) < 0)
542 log_warning("Failed to parse configuration file: %s", strerror(-r));
543
544 fclose(f);
545
546 return 0;
547}
548
f170852a 549static int parse_proc_cmdline(void) {
52661efd 550 char *line, *w, *state;
f170852a 551 int r;
f170852a 552 size_t l;
f170852a 553
b770165a
LP
554 /* Don't read /proc/cmdline if we are in a container, since
555 * that is only relevant for the host system */
556 if (detect_container(NULL) > 0)
557 return 0;
558
f170852a 559 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
e364ad06 560 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
f170852a
LP
561 return 0;
562 }
563
564 FOREACH_WORD_QUOTED(w, l, line, state) {
565 char *word;
566
567 if (!(word = strndup(w, l))) {
568 r = -ENOMEM;
569 goto finish;
570 }
571
572 r = parse_proc_cmdline_word(word);
573 free(word);
574
575 if (r < 0)
576 goto finish;
577 }
578
579 r = 0;
580
581finish:
582 free(line);
583 return r;
584}
585
586static int parse_argv(int argc, char *argv[]) {
587
588 enum {
589 ARG_LOG_LEVEL = 0x100,
590 ARG_LOG_TARGET,
bbe63281
LP
591 ARG_LOG_COLOR,
592 ARG_LOG_LOCATION,
2f198e2f 593 ARG_UNIT,
edb9aaa8 594 ARG_SYSTEM,
af2d49f7 595 ARG_USER,
e537352b 596 ARG_TEST,
80876c20 597 ARG_DUMP_CONFIGURATION_ITEMS,
9e58ff9c
LP
598 ARG_DUMP_CORE,
599 ARG_CRASH_SHELL,
a16e1123 600 ARG_CONFIRM_SPAWN,
9e58ff9c 601 ARG_SHOW_STATUS,
6e98720f 602 ARG_SYSV_CONSOLE,
4288f619 603 ARG_DESERIALIZE,
0a494f1f
LP
604 ARG_INTROSPECT,
605 ARG_DEFAULT_STD_OUTPUT,
606 ARG_DEFAULT_STD_ERROR
f170852a
LP
607 };
608
609 static const struct option options[] = {
a16e1123
LP
610 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
611 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bbe63281
LP
612 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
613 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2f198e2f 614 { "unit", required_argument, NULL, ARG_UNIT },
edb9aaa8 615 { "system", no_argument, NULL, ARG_SYSTEM },
af2d49f7 616 { "user", no_argument, NULL, ARG_USER },
a16e1123
LP
617 { "test", no_argument, NULL, ARG_TEST },
618 { "help", no_argument, NULL, 'h' },
619 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
9e58ff9c
LP
620 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
621 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
a16e1123 622 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
6e98720f 623 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
07459bb6 624#ifdef HAVE_SYSV_COMPAT
6e98720f 625 { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
07459bb6 626#endif
a16e1123 627 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
4288f619 628 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
0a494f1f
LP
629 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
630 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
a16e1123 631 { NULL, 0, NULL, 0 }
f170852a
LP
632 };
633
634 int c, r;
635
636 assert(argc >= 1);
637 assert(argv);
638
b770165a
LP
639 if (getpid() == 1)
640 opterr = 0;
641
099663ff 642 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
f170852a
LP
643
644 switch (c) {
645
646 case ARG_LOG_LEVEL:
647 if ((r = log_set_max_level_from_string(optarg)) < 0) {
648 log_error("Failed to parse log level %s.", optarg);
649 return r;
650 }
651
652 break;
653
654 case ARG_LOG_TARGET:
655
656 if ((r = log_set_target_from_string(optarg)) < 0) {
657 log_error("Failed to parse log target %s.", optarg);
658 return r;
659 }
660
661 break;
662
bbe63281
LP
663 case ARG_LOG_COLOR:
664
d0b170c8
LP
665 if (optarg) {
666 if ((r = log_show_color_from_string(optarg)) < 0) {
667 log_error("Failed to parse log color setting %s.", optarg);
668 return r;
669 }
670 } else
671 log_show_color(true);
bbe63281
LP
672
673 break;
674
675 case ARG_LOG_LOCATION:
676
d0b170c8
LP
677 if (optarg) {
678 if ((r = log_show_location_from_string(optarg)) < 0) {
679 log_error("Failed to parse log location setting %s.", optarg);
680 return r;
681 }
682 } else
683 log_show_location(true);
bbe63281
LP
684
685 break;
686
0a494f1f
LP
687 case ARG_DEFAULT_STD_OUTPUT:
688
689 if ((r = exec_output_from_string(optarg)) < 0) {
690 log_error("Failed to parse default standard output setting %s.", optarg);
691 return r;
692 } else
693 arg_default_std_output = r;
694 break;
695
696 case ARG_DEFAULT_STD_ERROR:
697
698 if ((r = exec_output_from_string(optarg)) < 0) {
699 log_error("Failed to parse default standard error output setting %s.", optarg);
700 return r;
701 } else
702 arg_default_std_error = r;
703 break;
704
2f198e2f 705 case ARG_UNIT:
f170852a
LP
706
707 if ((r = set_default_unit(optarg)) < 0) {
708 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
709 return r;
710 }
711
712 break;
713
edb9aaa8
LP
714 case ARG_SYSTEM:
715 arg_running_as = MANAGER_SYSTEM;
716 break;
a5dab5ce 717
af2d49f7
LP
718 case ARG_USER:
719 arg_running_as = MANAGER_USER;
a5dab5ce 720 break;
a5dab5ce 721
e965d56d 722 case ARG_TEST:
fa0f4d8a 723 arg_action = ACTION_TEST;
e965d56d
LP
724 break;
725
e537352b 726 case ARG_DUMP_CONFIGURATION_ITEMS:
fa0f4d8a 727 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
e537352b
LP
728 break;
729
9e58ff9c
LP
730 case ARG_DUMP_CORE:
731 arg_dump_core = true;
732 break;
733
734 case ARG_CRASH_SHELL:
735 arg_crash_shell = true;
736 break;
737
80876c20 738 case ARG_CONFIRM_SPAWN:
fa0f4d8a 739 arg_confirm_spawn = true;
80876c20
LP
740 break;
741
9e58ff9c 742 case ARG_SHOW_STATUS:
6e98720f
LP
743
744 if (optarg) {
745 if ((r = parse_boolean(optarg)) < 0) {
746 log_error("Failed to show status boolean %s.", optarg);
747 return r;
748 }
749 arg_show_status = r;
750 } else
751 arg_show_status = true;
752 break;
07459bb6 753#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
754 case ARG_SYSV_CONSOLE:
755
756 if (optarg) {
757 if ((r = parse_boolean(optarg)) < 0) {
758 log_error("Failed to SysV console boolean %s.", optarg);
759 return r;
760 }
761 arg_sysv_console = r;
762 } else
763 arg_sysv_console = true;
9e58ff9c 764 break;
07459bb6 765#endif
9e58ff9c 766
a16e1123
LP
767 case ARG_DESERIALIZE: {
768 int fd;
769 FILE *f;
770
771 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
772 log_error("Failed to parse deserialize option %s.", optarg);
773 return r;
774 }
775
776 if (!(f = fdopen(fd, "r"))) {
777 log_error("Failed to open serialization fd: %m");
778 return r;
779 }
780
781 if (serialization)
782 fclose(serialization);
783
784 serialization = f;
785
786 break;
787 }
788
4288f619
LP
789 case ARG_INTROSPECT: {
790 const char * const * i = NULL;
791
792 for (i = bus_interface_table; *i; i += 2)
793 if (!optarg || streq(i[0], optarg)) {
794 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
795 "<node>\n", stdout);
796 fputs(i[1], stdout);
797 fputs("</node>\n", stdout);
798
799 if (optarg)
800 break;
801 }
802
803 if (!i[0] && optarg)
804 log_error("Unknown interface %s.", optarg);
805
fa0f4d8a 806 arg_action = ACTION_DONE;
4288f619
LP
807 break;
808 }
809
f170852a 810 case 'h':
fa0f4d8a 811 arg_action = ACTION_HELP;
f170852a
LP
812 break;
813
1d2e23ab
LP
814 case 'D':
815 log_set_max_level(LOG_DEBUG);
816 break;
817
099663ff
LP
818 case 'b':
819 case 's':
820 case 'z':
821 /* Just to eat away the sysvinit kernel
822 * cmdline args without getopt() error
823 * messages that we'll parse in
824 * parse_proc_cmdline_word() or ignore. */
f170852a 825
099663ff 826 case '?':
f170852a 827 default:
099663ff
LP
828 if (getpid() != 1) {
829 log_error("Unknown option code %c", c);
830 return -EINVAL;
831 }
832
833 break;
f170852a
LP
834 }
835
d821e6d6
LP
836 if (optind < argc && getpid() != 1) {
837 /* Hmm, when we aren't run as init system
838 * let's complain about excess arguments */
839
840 log_error("Excess arguments.");
841 return -EINVAL;
842 }
843
844 if (detect_container(NULL) > 0) {
845 char **a;
846
847 /* All /proc/cmdline arguments the kernel didn't
848 * understand it passed to us. We're not really
849 * interested in that usually since /proc/cmdline is
850 * more interesting and complete. With one exception:
851 * if we are run in a container /proc/cmdline is not
852 * relevant for the container, hence we rely on argv[]
853 * instead. */
854
855 for (a = argv; a < argv + argc; a++)
856 if ((r = parse_proc_cmdline_word(*a)) < 0)
857 return r;
51f0e189
LP
858 }
859
f170852a
LP
860 return 0;
861}
862
863static int help(void) {
864
2e33c433 865 printf("%s [OPTIONS...]\n\n"
af2d49f7 866 "Starts up and maintains the system or user services.\n\n"
e537352b 867 " -h --help Show this help\n"
e537352b 868 " --test Determine startup sequence, dump it and exit\n"
80876c20 869 " --dump-configuration-items Dump understood unit configuration items\n"
bbe63281 870 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
9e58ff9c 871 " --unit=UNIT Set default unit\n"
edb9aaa8 872 " --system Run a system instance, even if PID != 1\n"
af2d49f7 873 " --user Run a user instance\n"
9e58ff9c
LP
874 " --dump-core Dump core on crash\n"
875 " --crash-shell Run shell on crash\n"
876 " --confirm-spawn Ask for confirmation when spawning processes\n"
6e98720f 877 " --show-status[=0|1] Show status updates on the console during bootup\n"
07459bb6 878#ifdef HAVE_SYSV_COMPAT
6e98720f 879 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
07459bb6 880#endif
bbe63281 881 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
9e58ff9c 882 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
2218198b 883 " --log-color[=0|1] Highlight important log messages\n"
0a494f1f
LP
884 " --log-location[=0|1] Include code location in log messages\n"
885 " --default-standard-output= Set default standard output for services\n"
886 " --default-standard-error= Set default standard error output for services\n",
5b6319dc 887 program_invocation_short_name);
f170852a
LP
888
889 return 0;
890}
891
a16e1123
LP
892static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
893 FILE *f = NULL;
894 FDSet *fds = NULL;
895 int r;
896
897 assert(m);
898 assert(_f);
899 assert(_fds);
900
a7556052
LP
901 /* Make sure nothing is really destructed when we shut down */
902 m->n_reloading ++;
903
d8d5ab98 904 if ((r = manager_open_serialization(m, &f)) < 0) {
35b8ca3a 905 log_error("Failed to create serialization file: %s", strerror(-r));
a16e1123
LP
906 goto fail;
907 }
908
909 if (!(fds = fdset_new())) {
910 r = -ENOMEM;
911 log_error("Failed to allocate fd set: %s", strerror(-r));
912 goto fail;
913 }
914
915 if ((r = manager_serialize(m, f, fds)) < 0) {
916 log_error("Failed to serialize state: %s", strerror(-r));
917 goto fail;
918 }
919
920 if (fseeko(f, 0, SEEK_SET) < 0) {
921 log_error("Failed to rewind serialization fd: %m");
922 goto fail;
923 }
924
925 if ((r = fd_cloexec(fileno(f), false)) < 0) {
926 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
927 goto fail;
928 }
929
930 if ((r = fdset_cloexec(fds, false)) < 0) {
931 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
932 goto fail;
933 }
934
935 *_f = f;
936 *_fds = fds;
937
938 return 0;
939
940fail:
941 fdset_free(fds);
942
943 if (f)
944 fclose(f);
945
946 return r;
947}
948
e9ddabc2
LP
949static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
950 const char *e;
951 unsigned long long a, b;
952
953 assert(t);
954
955 if (!(e = getenv("RD_TIMESTAMP")))
956 return NULL;
957
958 if (sscanf(e, "%llu %llu", &a, &b) != 2)
959 return NULL;
960
961 t->realtime = (usec_t) a;
962 t->monotonic = (usec_t) b;
963
964 return t;
965}
966
6ee5bbf8
LP
967static void test_mtab(void) {
968 char *p;
969
80758717
LP
970 /* Check that /etc/mtab is a symlink */
971
6ee5bbf8
LP
972 if (readlink_malloc("/etc/mtab", &p) >= 0) {
973 bool b;
974
ed86ebc4 975 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
6ee5bbf8
LP
976 free(p);
977
978 if (b)
979 return;
980 }
981
80758717
LP
982 log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
983 "This is not supported anymore. "
984 "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
985}
986
987static void test_usr(void) {
80758717 988
ed1c99fc 989 /* Check that /usr is not a separate fs */
80758717 990
871c44a7
LP
991 if (dir_is_empty("/usr") <= 0)
992 return;
993
2376ce13 994 log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
871c44a7
LP
995 "Some things will probably break (sometimes even silently) in mysterious ways. "
996 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
997}
998
999static void test_cgroups(void) {
1000
1001 if (access("/proc/cgroups", F_OK) >= 0)
1002 return;
1003
1004 log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
1005 "Systems without control groups are not supported. "
1006 "We will now sleep for 10s, and then continue boot-up. "
1007 "Expect breakage and please do not file bugs. "
1008 "Instead fix your kernel and enable CONFIG_CGROUPS." );
1009
1010 sleep(10);
6ee5bbf8
LP
1011}
1012
60918275
LP
1013int main(int argc, char *argv[]) {
1014 Manager *m = NULL;
22f4096c 1015 int r, retval = EXIT_FAILURE;
9d76d730
LP
1016 usec_t before_startup, after_startup;
1017 char timespan[FORMAT_TIMESPAN_MAX];
a16e1123
LP
1018 FDSet *fds = NULL;
1019 bool reexecute = false;
b9080b03 1020 const char *shutdown_verb = NULL;
e9ddabc2 1021 dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
f3b6a3ed 1022 char systemd[] = "systemd";
0b3325e7
LP
1023 bool is_reexec = false;
1024 int j;
1025 bool loaded_policy = false;
27b14a22 1026
058dc6f3 1027#ifdef HAVE_SYSV_COMPAT
2cb1a60d 1028 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
35b8ca3a 1029 /* This is compatibility support for SysV, where
2cb1a60d
LP
1030 * calling init as a user is identical to telinit. */
1031
1032 errno = -ENOENT;
1033 execv(SYSTEMCTL_BINARY_PATH, argv);
1034 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1035 return 1;
1036 }
058dc6f3 1037#endif
2cb1a60d 1038
0b3325e7
LP
1039 /* Determine if this is a reexecution or normal bootup. We do
1040 * the full command line parsing much later, so let's just
1041 * have a quick peek here. */
1042
1043 for (j = 1; j < argc; j++)
1044 if (streq(argv[j], "--deserialize")) {
1045 break;
1046 is_reexec = true;
1047 }
1048
f3b6a3ed
LP
1049 /* If we get started via the /sbin/init symlink then we are
1050 called 'init'. After a subsequent reexecution we are then
1051 called 'systemd'. That is confusing, hence let's call us
1052 systemd right-away. */
1053
1054 program_invocation_short_name = systemd;
1055 prctl(PR_SET_NAME, systemd);
9a0e6896
LP
1056 saved_argv = argv;
1057 saved_argc = argc;
f3b6a3ed 1058
2cc59dbf 1059 log_show_color(isatty(STDERR_FILENO) > 0);
bbe63281 1060 log_show_location(false);
7c706717 1061 log_set_max_level(LOG_INFO);
bbe63281 1062
843d2643 1063 if (getpid() == 1) {
fa0f4d8a 1064 arg_running_as = MANAGER_SYSTEM;
90df7e56 1065 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_SYSLOG_OR_KMSG);
0ff4cdd9 1066
0b3325e7
LP
1067 if (!is_reexec)
1068 if (selinux_setup(&loaded_policy) < 0)
1069 goto finish;
1070
1071 log_open();
c4dcdb9f 1072
0ff4cdd9
LP
1073 if (label_init() < 0)
1074 goto finish;
7948c4df 1075
0b3325e7
LP
1076 if (!is_reexec)
1077 if (hwclock_is_localtime() > 0) {
1078 int min;
7948c4df 1079
0b3325e7
LP
1080 r = hwclock_apply_localtime_delta(&min);
1081 if (r < 0)
1082 log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
1083 else
1084 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
1085 }
871e5809 1086
bbe63281 1087 } else {
af2d49f7 1088 arg_running_as = MANAGER_USER;
eeecf6e6 1089 log_set_target(LOG_TARGET_AUTO);
871e5809 1090 log_open();
bbe63281 1091 }
a5dab5ce 1092
f170852a
LP
1093 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
1094 goto finish;
60918275 1095
f170852a
LP
1096 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1097 * /proc/$PID/fd is available. */
ca326f6f 1098 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
0b3325e7 1099 if (mount_setup(loaded_policy) < 0)
8efe3c01 1100 goto finish;
4ade7963
LP
1101
1102 /* Reset all signal handlers. */
1103 assert_se(reset_all_signal_handlers() == 0);
1104
078e4539 1105 /* If we are init, we can block sigkill. Yay. */
9a34ec5f 1106 ignore_signals(SIGNALS_IGNORE, -1);
078e4539 1107
487393e9
LP
1108 if (parse_config_file() < 0)
1109 goto finish;
1110
fa0f4d8a 1111 if (arg_running_as == MANAGER_SYSTEM)
a5dab5ce
LP
1112 if (parse_proc_cmdline() < 0)
1113 goto finish;
f170852a
LP
1114
1115 log_parse_environment();
1116
1117 if (parse_argv(argc, argv) < 0)
1118 goto finish;
1119
b5c6cf87
LP
1120 if (arg_action == ACTION_TEST && geteuid() == 0) {
1121 log_error("Don't run test mode as root.");
1122 goto finish;
1123 }
1124
fe783b03
LP
1125 if (arg_running_as == MANAGER_SYSTEM &&
1126 arg_action == ACTION_RUN &&
1127 running_in_chroot() > 0) {
1128 log_error("Cannot be run in a chroot() environment.");
1129 goto finish;
1130 }
1131
fa0f4d8a 1132 if (arg_action == ACTION_HELP) {
f170852a
LP
1133 retval = help();
1134 goto finish;
fa0f4d8a 1135 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
e537352b 1136 unit_dump_config_items(stdout);
22f4096c 1137 retval = EXIT_SUCCESS;
e537352b 1138 goto finish;
fa0f4d8a 1139 } else if (arg_action == ACTION_DONE) {
22f4096c 1140 retval = EXIT_SUCCESS;
4288f619 1141 goto finish;
f170852a
LP
1142 }
1143
fa0f4d8a 1144 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
f170852a 1145
871e5809
LP
1146 /* Close logging fds, in order not to confuse fdset below */
1147 log_close();
1148
a16e1123
LP
1149 /* Remember open file descriptors for later deserialization */
1150 if (serialization) {
1151 if ((r = fdset_new_fill(&fds)) < 0) {
1152 log_error("Failed to allocate fd set: %s", strerror(-r));
1153 goto finish;
1154 }
1155
1156 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
1157 } else
1158 close_all_fds(NULL, 0);
1159
09082a94 1160 /* Set up PATH unless it is already set */
e537352b
LP
1161 setenv("PATH",
1162 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
fa0f4d8a 1163 arg_running_as == MANAGER_SYSTEM);
09082a94 1164
39439087 1165 if (arg_running_as == MANAGER_SYSTEM) {
e9ddabc2
LP
1166 /* Parse the data passed to us by the initrd and unset it */
1167 parse_initrd_timestamp(&initrd_timestamp);
1168 filter_environ("RD_");
1169
1170 /* Unset some environment variables passed in from the
1171 * kernel that don't really make sense for us. */
39439087
LP
1172 unsetenv("HOME");
1173 unsetenv("TERM");
b770165a
LP
1174
1175 /* All other variables are left as is, so that clients
1176 * can still read them via /proc/1/environ */
39439087 1177 }
1104f3c1 1178
f170852a
LP
1179 /* Move out of the way, so that we won't block unmounts */
1180 assert_se(chdir("/") == 0);
1181
fa0f4d8a 1182 if (arg_running_as == MANAGER_SYSTEM) {
80876c20
LP
1183 /* Become a session leader if we aren't one yet. */
1184 setsid();
4ade7963 1185
80876c20
LP
1186 /* Disable the umask logic */
1187 umask(0);
1188 }
1189
843d2643
LP
1190 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1191 dbus_connection_set_change_sigpipe(FALSE);
1192
2146621b
LP
1193 /* Reset the console, but only if this is really init and we
1194 * are freshly booted */
fa0f4d8a 1195 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
0b3325e7 1196 console_setup(getpid() == 1 && !is_reexec);
843d2643
LP
1197 make_null_stdio();
1198 }
4ade7963 1199
18149b9f 1200 /* Open the logging devices, if possible and necessary */
843d2643 1201 log_open();
4ade7963 1202
5373d602
LP
1203 /* Make sure we leave a core dump without panicing the
1204 * kernel. */
4fc935ca
LP
1205 if (getpid() == 1)
1206 install_crash_handler();
97c4f35c 1207
302e27c8 1208 log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
7d568925 1209 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
a5dab5ce 1210
0b3325e7 1211 if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
72bca11b
LP
1212 locale_setup();
1213
6faa1114 1214 if (arg_show_status || plymouth_running())
888c6216
LP
1215 status_welcome();
1216
888c6216
LP
1217 kmod_setup();
1218 hostname_setup();
d7ccca2e 1219 machine_id_setup();
888c6216 1220 loopback_setup();
490aed58 1221
6ee5bbf8 1222 test_mtab();
80758717 1223 test_usr();
871c44a7 1224 test_cgroups();
af5bc85d 1225 }
302e8c4c 1226
9e58ff9c 1227 if ((r = manager_new(arg_running_as, &m)) < 0) {
8e274523 1228 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
1229 goto finish;
1230 }
1231
9e58ff9c
LP
1232 m->confirm_spawn = arg_confirm_spawn;
1233 m->show_status = arg_show_status;
07459bb6 1234#ifdef HAVE_SYSV_COMPAT
6e98720f 1235 m->sysv_console = arg_sysv_console;
07459bb6 1236#endif
d3689161 1237 m->mount_auto = arg_mount_auto;
173a8d04 1238 m->swap_auto = arg_swap_auto;
0a494f1f
LP
1239 m->default_std_output = arg_default_std_output;
1240 m->default_std_error = arg_default_std_error;
9e58ff9c 1241
e9ddabc2
LP
1242 if (dual_timestamp_is_set(&initrd_timestamp))
1243 m->initrd_timestamp = initrd_timestamp;
1244
06d4c99a
LP
1245 if (arg_default_controllers)
1246 manager_set_default_controllers(m, arg_default_controllers);
1247
9d76d730
LP
1248 before_startup = now(CLOCK_MONOTONIC);
1249
a16e1123 1250 if ((r = manager_startup(m, serialization, fds)) < 0)
6e2ef85b 1251 log_error("Failed to fully start up daemon: %s", strerror(-r));
a16e1123
LP
1252
1253 if (fds) {
1254 /* This will close all file descriptors that were opened, but
1255 * not claimed by any unit. */
1256
1257 fdset_free(fds);
1258 fds = NULL;
f50e0a01
LP
1259 }
1260
a16e1123
LP
1261 if (serialization) {
1262 fclose(serialization);
1263 serialization = NULL;
1264 } else {
398ef8ba 1265 DBusError error;
1c27d3f3 1266 Unit *target = NULL;
398ef8ba
LP
1267
1268 dbus_error_init(&error);
1269
fa0f4d8a 1270 log_debug("Activating default unit: %s", arg_default_unit);
a16e1123 1271
398ef8ba
LP
1272 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1273 log_error("Failed to load default target: %s", bus_error(&error, r));
1274 dbus_error_free(&error);
00dc5d76 1275 } else if (target->meta.load_state == UNIT_ERROR)
1c27d3f3 1276 log_error("Failed to load default target: %s", strerror(-target->meta.load_error));
6daf4f90
LP
1277 else if (target->meta.load_state == UNIT_MASKED)
1278 log_error("Default target masked.");
27b14a22 1279
1c27d3f3 1280 if (!target || target->meta.load_state != UNIT_LOADED) {
a16e1123 1281 log_info("Trying to load rescue target...");
1c27d3f3 1282
398ef8ba
LP
1283 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1284 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1285 dbus_error_free(&error);
a16e1123 1286 goto finish;
00dc5d76 1287 } else if (target->meta.load_state == UNIT_ERROR) {
1c27d3f3
LP
1288 log_error("Failed to load rescue target: %s", strerror(-target->meta.load_error));
1289 goto finish;
6daf4f90
LP
1290 } else if (target->meta.load_state == UNIT_MASKED) {
1291 log_error("Rescue target masked.");
00dc5d76 1292 goto finish;
a16e1123
LP
1293 }
1294 }
37d88da7 1295
00dc5d76
LP
1296 assert(target->meta.load_state == UNIT_LOADED);
1297
fa0f4d8a 1298 if (arg_action == ACTION_TEST) {
40d50879 1299 printf("-> By units:\n");
a16e1123
LP
1300 manager_dump_units(m, stdout, "\t");
1301 }
1302
0ff4cdd9 1303 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
398ef8ba
LP
1304 log_error("Failed to start default target: %s", bus_error(&error, r));
1305 dbus_error_free(&error);
37d88da7
LP
1306 goto finish;
1307 }
60918275 1308
07672f49
LP
1309 after_startup = now(CLOCK_MONOTONIC);
1310 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1311 "Loaded units and determined initial transaction in %s.",
1312 format_timespan(timespan, sizeof(timespan), after_startup - before_startup));
1313
fa0f4d8a 1314 if (arg_action == ACTION_TEST) {
40d50879 1315 printf("-> By jobs:\n");
a16e1123 1316 manager_dump_jobs(m, stdout, "\t");
22f4096c 1317 retval = EXIT_SUCCESS;
a16e1123
LP
1318 goto finish;
1319 }
e965d56d 1320 }
d46de8a1 1321
a16e1123
LP
1322 for (;;) {
1323 if ((r = manager_loop(m)) < 0) {
1324 log_error("Failed to run mainloop: %s", strerror(-r));
1325 goto finish;
1326 }
11dd41ce 1327
a16e1123 1328 switch (m->exit_code) {
e965d56d 1329
a16e1123 1330 case MANAGER_EXIT:
22f4096c 1331 retval = EXIT_SUCCESS;
a16e1123
LP
1332 log_debug("Exit.");
1333 goto finish;
e965d56d 1334
a16e1123 1335 case MANAGER_RELOAD:
e015090f 1336 log_info("Reloading.");
a16e1123
LP
1337 if ((r = manager_reload(m)) < 0)
1338 log_error("Failed to reload: %s", strerror(-r));
1339 break;
cea8e32e 1340
a16e1123 1341 case MANAGER_REEXECUTE:
a16e1123
LP
1342 if (prepare_reexecute(m, &serialization, &fds) < 0)
1343 goto finish;
60918275 1344
a16e1123 1345 reexecute = true;
e015090f 1346 log_notice("Reexecuting.");
a16e1123
LP
1347 goto finish;
1348
b9080b03
FF
1349 case MANAGER_REBOOT:
1350 case MANAGER_POWEROFF:
1351 case MANAGER_HALT:
1352 case MANAGER_KEXEC: {
1353 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1354 [MANAGER_REBOOT] = "reboot",
1355 [MANAGER_POWEROFF] = "poweroff",
1356 [MANAGER_HALT] = "halt",
1357 [MANAGER_KEXEC] = "kexec"
1358 };
1359
1360 assert_se(shutdown_verb = table[m->exit_code]);
1361
1362 log_notice("Shutting down.");
1363 goto finish;
1364 }
1365
a16e1123
LP
1366 default:
1367 assert_not_reached("Unknown exit code.");
1368 }
1369 }
f170852a 1370
60918275
LP
1371finish:
1372 if (m)
1373 manager_free(m);
1374
fa0f4d8a 1375 free(arg_default_unit);
06d4c99a 1376 strv_free(arg_default_controllers);
b9cd2ec1 1377
ea430986
LP
1378 dbus_shutdown();
1379
b2bb3dbe
LP
1380 label_finish();
1381
a16e1123 1382 if (reexecute) {
6e98720f 1383 const char *args[15];
a16e1123
LP
1384 unsigned i = 0;
1385 char sfd[16];
1386
1387 assert(serialization);
1388 assert(fds);
1389
1390 args[i++] = SYSTEMD_BINARY_PATH;
1391
1392 args[i++] = "--log-level";
1393 args[i++] = log_level_to_string(log_get_max_level());
1394
1395 args[i++] = "--log-target";
1396 args[i++] = log_target_to_string(log_get_target());
1397
edb9aaa8
LP
1398 if (arg_running_as == MANAGER_SYSTEM)
1399 args[i++] = "--system";
1400 else
af2d49f7 1401 args[i++] = "--user";
edb9aaa8
LP
1402
1403 if (arg_dump_core)
1404 args[i++] = "--dump-core";
1405
1406 if (arg_crash_shell)
1407 args[i++] = "--crash-shell";
1408
1409 if (arg_confirm_spawn)
1410 args[i++] = "--confirm-spawn";
1411
1412 if (arg_show_status)
6e98720f
LP
1413 args[i++] = "--show-status=1";
1414 else
1415 args[i++] = "--show-status=0";
1416
07459bb6 1417#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
1418 if (arg_sysv_console)
1419 args[i++] = "--sysv-console=1";
1420 else
1421 args[i++] = "--sysv-console=0";
07459bb6 1422#endif
a16e1123
LP
1423
1424 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1425 char_array_0(sfd);
1426
1427 args[i++] = "--deserialize";
1428 args[i++] = sfd;
1429
a16e1123
LP
1430 args[i++] = NULL;
1431
1432 assert(i <= ELEMENTSOF(args));
1433
1434 execv(args[0], (char* const*) args);
1435
1436 log_error("Failed to reexecute: %m");
1437 }
1438
1439 if (serialization)
1440 fclose(serialization);
1441
1442 if (fds)
1443 fdset_free(fds);
1444
b9080b03
FF
1445 if (shutdown_verb) {
1446 const char * command_line[] = {
1447 SYSTEMD_SHUTDOWN_BINARY_PATH,
1448 shutdown_verb,
1449 NULL
1450 };
1451
1452 execv(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line);
1453 log_error("Failed to execute shutdown binary, freezing: %m");
1454 }
1455
c3b3c274
LP
1456 if (getpid() == 1)
1457 freeze();
1458
60918275
LP
1459 return retval;
1460}