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