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