]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/main.c
units: replace a few non-essential API mounts with automount units
[thirdparty/systemd.git] / src / main.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
60918275
LP
34
35#include "manager.h"
16354eff 36#include "log.h"
4ade7963 37#include "mount-setup.h"
302e8c4c 38#include "hostname-setup.h"
af5bc85d 39#include "loopback-setup.h"
302e8c4c 40#include "load-fragment.h"
a16e1123 41#include "fdset.h"
60918275 42
f170852a
LP
43static enum {
44 ACTION_RUN,
e965d56d 45 ACTION_HELP,
e537352b
LP
46 ACTION_TEST,
47 ACTION_DUMP_CONFIGURATION_ITEMS
f170852a
LP
48} action = ACTION_RUN;
49
50static char *default_unit = NULL;
a5dab5ce 51static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
4fc935ca 52
97c4f35c 53static bool dump_core = true;
601f6a1e
LP
54static bool crash_shell = false;
55static int crash_chvt = -1;
80876c20 56static bool confirm_spawn = false;
a16e1123 57static FILE* serialization = NULL;
80876c20 58
97c4f35c
LP
59_noreturn static void freeze(void) {
60 for (;;)
61 pause();
62}
63
6f5e3f35
LP
64static void nop_handler(int sig) {
65}
66
97c4f35c
LP
67_noreturn static void crash(int sig) {
68
69 if (!dump_core)
70 log_error("Caught <%s>, not dumping core.", strsignal(sig));
71 else {
6f5e3f35 72 struct sigaction sa;
97c4f35c
LP
73 pid_t pid;
74
6f5e3f35
LP
75 /* We want to wait for the core process, hence let's enable SIGCHLD */
76 zero(sa);
77 sa.sa_handler = nop_handler;
78 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
79 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
80
97c4f35c 81 if ((pid = fork()) < 0)
4fc935ca 82 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
97c4f35c
LP
83
84 else if (pid == 0) {
97c4f35c
LP
85 struct rlimit rl;
86
87 /* Enable default signal handler for core dump */
88 zero(sa);
89 sa.sa_handler = SIG_DFL;
90 assert_se(sigaction(sig, &sa, NULL) == 0);
91
92 /* Don't limit the core dump size */
93 zero(rl);
94 rl.rlim_cur = RLIM_INFINITY;
95 rl.rlim_max = RLIM_INFINITY;
96 setrlimit(RLIMIT_CORE, &rl);
97
98 /* Just to be sure... */
99 assert_se(chdir("/") == 0);
100
101 /* Raise the signal again */
102 raise(sig);
103
104 assert_not_reached("We shouldn't be here...");
105 _exit(1);
4fc935ca
LP
106
107 } else {
108 int status, r;
109
110 /* Order things nicely. */
111 if ((r = waitpid(pid, &status, 0)) < 0)
112 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
113 else if (!WCOREDUMP(status))
114 log_error("Caught <%s>, core dump failed.", strsignal(sig));
115 else
116 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid);
97c4f35c
LP
117 }
118 }
119
601f6a1e
LP
120 if (crash_chvt)
121 chvt(crash_chvt);
122
4fc935ca 123 if (crash_shell) {
6f5e3f35
LP
124 struct sigaction sa;
125 pid_t pid;
8c43883a 126
4fc935ca
LP
127 log_info("Executing crash shell in 10s...");
128 sleep(10);
129
6f5e3f35
LP
130 /* Let the kernel reap children for us */
131 zero(sa);
132 sa.sa_handler = SIG_IGN;
133 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
134 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
8c43883a 135
6f5e3f35
LP
136 if ((pid = fork()) < 0)
137 log_error("Failed to fork off crash shell: %s", strerror(errno));
138 else if (pid == 0) {
843d2643 139 int fd, r;
ea5652c2
LP
140
141 if ((fd = acquire_terminal("/dev/console", false, true)) < 0) {
142 log_error("Failed to acquire terminal: %s", strerror(-fd));
143 _exit(1);
144 }
145
843d2643
LP
146 if ((r = make_stdio(fd)) < 0) {
147 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
ea5652c2
LP
148 _exit(1);
149 }
150
6f5e3f35
LP
151 execl("/bin/sh", "/bin/sh", NULL);
152
153 log_error("execl() failed: %s", strerror(errno));
154 _exit(1);
155 }
c99b188e 156
6f5e3f35 157 log_info("Successfully spawned crash shall as pid %llu.", (unsigned long long) pid);
4fc935ca
LP
158 }
159
160 log_info("Freezing execution.");
97c4f35c
LP
161 freeze();
162}
163
164static void install_crash_handler(void) {
165 struct sigaction sa;
166
167 zero(sa);
168
169 sa.sa_handler = crash;
170 sa.sa_flags = SA_NODEFER;
171
172 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
5373d602
LP
173 assert_se(sigaction(SIGILL, &sa, NULL) == 0);
174 assert_se(sigaction(SIGFPE, &sa, NULL) == 0);
175 assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
176 assert_se(sigaction(SIGQUIT, &sa, NULL) == 0);
97c4f35c
LP
177 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
178}
f170852a 179
843d2643
LP
180static int make_null_stdio(void) {
181 int null_fd, r;
80876c20 182
843d2643 183 if ((null_fd = open("/dev/null", O_RDWR)) < 0) {
80876c20 184 log_error("Failed to open /dev/null: %m");
843d2643 185 return -errno;
80876c20
LP
186 }
187
843d2643
LP
188 if ((r = make_stdio(null_fd)) < 0)
189 log_warning("Failed to dup2() device: %s", strerror(-r));
80876c20 190
843d2643
LP
191 return r;
192}
80876c20 193
843d2643
LP
194static int console_setup(bool do_reset) {
195 int tty_fd, r;
80876c20 196
843d2643
LP
197 /* If we are init, we connect stdin/stdout/stderr to /dev/null
198 * and make sure we don't have a controlling tty. */
80876c20 199
843d2643
LP
200 release_terminal();
201
202 if (!do_reset)
203 return 0;
80876c20 204
843d2643
LP
205 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
206 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
207 return -tty_fd;
208 }
80876c20 209
843d2643
LP
210 if ((r = reset_terminal(tty_fd)) < 0)
211 log_error("Failed to reset /dev/console: %s", strerror(-r));
212
213 close_nointr_nofail(tty_fd);
80876c20
LP
214 return r;
215}
216
f170852a
LP
217static int set_default_unit(const char *u) {
218 char *c;
219
220 assert(u);
221
222 if (!(c = strdup(u)))
223 return -ENOMEM;
224
225 free(default_unit);
226 default_unit = c;
227 return 0;
228}
229
230static int parse_proc_cmdline_word(const char *word) {
231
232 static const char * const rlmap[] = {
233 "single", SPECIAL_RUNLEVEL1_TARGET,
234 "-s", SPECIAL_RUNLEVEL1_TARGET,
235 "s", SPECIAL_RUNLEVEL1_TARGET,
236 "S", SPECIAL_RUNLEVEL1_TARGET,
237 "1", SPECIAL_RUNLEVEL1_TARGET,
238 "2", SPECIAL_RUNLEVEL2_TARGET,
239 "3", SPECIAL_RUNLEVEL3_TARGET,
240 "4", SPECIAL_RUNLEVEL4_TARGET,
241 "5", SPECIAL_RUNLEVEL5_TARGET
242 };
243
244 if (startswith(word, "systemd.default="))
82771ba1 245 return set_default_unit(word + 16);
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
4fc935ca
LP
257 } else if (startswith(word, "systemd.dump_core=")) {
258 int r;
259
260 if ((r = parse_boolean(word + 18)) < 0)
601f6a1e 261 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
4fc935ca
LP
262 else
263 dump_core = r;
264
265 } else if (startswith(word, "systemd.crash_shell=")) {
266 int r;
267
268 if ((r = parse_boolean(word + 20)) < 0)
601f6a1e 269 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
4fc935ca
LP
270 else
271 crash_shell = r;
272
5e7ee61c
LP
273
274 } else if (startswith(word, "systemd.confirm_spawn=")) {
275 int r;
276
277 if ((r = parse_boolean(word + 22)) < 0)
278 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
279 else
280 confirm_spawn = r;
281
601f6a1e
LP
282 } else if (startswith(word, "systemd.crash_chvt=")) {
283 int k;
284
285 if (safe_atoi(word + 19, &k) < 0)
286 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
287 else
288 crash_chvt = k;
289
4fc935ca
LP
290 } else if (startswith(word, "systemd.")) {
291
292 log_warning("Unknown kernel switch %s. Ignoring.", word);
293
294 log_info("Supported kernel switches:");
295 log_info("systemd.default=UNIT Default unit to start");
296 log_info("systemd.log_target=console|kmsg|syslog Log target");
297 log_info("systemd.log_level=LEVEL Log level");
298 log_info("systemd.dump_core=0|1 Dump core on crash");
299 log_info("systemd.crash_shell=0|1 On crash run shell");
601f6a1e 300 log_info("systemd.crash_chvt=N Change to VT #N on crash");
5e7ee61c 301 log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
4fc935ca 302
f170852a
LP
303 } else {
304 unsigned i;
305
306 /* SysV compatibility */
f170852a
LP
307 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
308 if (streq(word, rlmap[i]))
309 return set_default_unit(rlmap[i+1]);
310 }
311
312 return 0;
313}
314
315static int parse_proc_cmdline(void) {
316 char *line;
317 int r;
318 char *w;
319 size_t l;
320 char *state;
321
322 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
323 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
324 return 0;
325 }
326
327 FOREACH_WORD_QUOTED(w, l, line, state) {
328 char *word;
329
330 if (!(word = strndup(w, l))) {
331 r = -ENOMEM;
332 goto finish;
333 }
334
335 r = parse_proc_cmdline_word(word);
336 free(word);
337
338 if (r < 0)
339 goto finish;
340 }
341
342 r = 0;
343
344finish:
345 free(line);
346 return r;
347}
348
349static int parse_argv(int argc, char *argv[]) {
350
351 enum {
352 ARG_LOG_LEVEL = 0x100,
353 ARG_LOG_TARGET,
a5dab5ce 354 ARG_DEFAULT,
e965d56d 355 ARG_RUNNING_AS,
e537352b 356 ARG_TEST,
80876c20 357 ARG_DUMP_CONFIGURATION_ITEMS,
a16e1123
LP
358 ARG_CONFIRM_SPAWN,
359 ARG_DESERIALIZE
f170852a
LP
360 };
361
362 static const struct option options[] = {
a16e1123
LP
363 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
364 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
365 { "default", required_argument, NULL, ARG_DEFAULT },
366 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
367 { "test", no_argument, NULL, ARG_TEST },
368 { "help", no_argument, NULL, 'h' },
369 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
370 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
371 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
372 { NULL, 0, NULL, 0 }
f170852a
LP
373 };
374
375 int c, r;
376
377 assert(argc >= 1);
378 assert(argv);
379
380 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
381
382 switch (c) {
383
384 case ARG_LOG_LEVEL:
385 if ((r = log_set_max_level_from_string(optarg)) < 0) {
386 log_error("Failed to parse log level %s.", optarg);
387 return r;
388 }
389
390 break;
391
392 case ARG_LOG_TARGET:
393
394 if ((r = log_set_target_from_string(optarg)) < 0) {
395 log_error("Failed to parse log target %s.", optarg);
396 return r;
397 }
398
399 break;
400
401 case ARG_DEFAULT:
402
403 if ((r = set_default_unit(optarg)) < 0) {
404 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
405 return r;
406 }
407
408 break;
409
a5dab5ce
LP
410 case ARG_RUNNING_AS: {
411 ManagerRunningAs as;
412
413 if ((as = manager_running_as_from_string(optarg)) < 0) {
414 log_error("Failed to parse running as value %s", optarg);
415 return -EINVAL;
416 }
417
418 running_as = as;
419 break;
420 }
421
e965d56d
LP
422 case ARG_TEST:
423 action = ACTION_TEST;
424 break;
425
e537352b
LP
426 case ARG_DUMP_CONFIGURATION_ITEMS:
427 action = ACTION_DUMP_CONFIGURATION_ITEMS;
428 break;
429
80876c20
LP
430 case ARG_CONFIRM_SPAWN:
431 confirm_spawn = true;
432 break;
433
a16e1123
LP
434 case ARG_DESERIALIZE: {
435 int fd;
436 FILE *f;
437
438 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
439 log_error("Failed to parse deserialize option %s.", optarg);
440 return r;
441 }
442
443 if (!(f = fdopen(fd, "r"))) {
444 log_error("Failed to open serialization fd: %m");
445 return r;
446 }
447
448 if (serialization)
449 fclose(serialization);
450
451 serialization = f;
452
453 break;
454 }
455
f170852a
LP
456 case 'h':
457 action = ACTION_HELP;
458 break;
459
460 case '?':
461 return -EINVAL;
462
463 default:
464 log_error("Unknown option code %c", c);
465 return -EINVAL;
466 }
467
51f0e189
LP
468 /* PID 1 will get the kernel arguments as parameters, which we
469 * ignore and unconditionally read from
470 * /proc/cmdline. However, we need to ignore those arguments
471 * here. */
472 if (running_as != MANAGER_INIT && optind < argc) {
473 log_error("Excess arguments.");
474 return -EINVAL;
475 }
476
f170852a
LP
477 return 0;
478}
479
480static int help(void) {
481
482 printf("%s [options]\n\n"
e537352b
LP
483 " -h --help Show this help\n"
484 " --default=UNIT Set default unit\n"
485 " --log-level=LEVEL Set log level\n"
843d2643 486 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg)\n"
e537352b
LP
487 " --running-as=AS Set running as (init, system, session)\n"
488 " --test Determine startup sequence, dump it and exit\n"
80876c20
LP
489 " --dump-configuration-items Dump understood unit configuration items\n"
490 " --confirm-spawn Ask for confirmation when spawning processes\n",
f170852a
LP
491 __progname);
492
493 return 0;
494}
495
a16e1123
LP
496static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
497 FILE *f = NULL;
498 FDSet *fds = NULL;
499 int r;
500
501 assert(m);
502 assert(_f);
503 assert(_fds);
504
505 if ((r = manager_open_serialization(&f)) < 0) {
506 log_error("Failed to create serialization faile: %s", strerror(-r));
507 goto fail;
508 }
509
510 if (!(fds = fdset_new())) {
511 r = -ENOMEM;
512 log_error("Failed to allocate fd set: %s", strerror(-r));
513 goto fail;
514 }
515
516 if ((r = manager_serialize(m, f, fds)) < 0) {
517 log_error("Failed to serialize state: %s", strerror(-r));
518 goto fail;
519 }
520
521 if (fseeko(f, 0, SEEK_SET) < 0) {
522 log_error("Failed to rewind serialization fd: %m");
523 goto fail;
524 }
525
526 if ((r = fd_cloexec(fileno(f), false)) < 0) {
527 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
528 goto fail;
529 }
530
531 if ((r = fdset_cloexec(fds, false)) < 0) {
532 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
533 goto fail;
534 }
535
536 *_f = f;
537 *_fds = fds;
538
539 return 0;
540
541fail:
542 fdset_free(fds);
543
544 if (f)
545 fclose(f);
546
547 return r;
548}
549
60918275
LP
550int main(int argc, char *argv[]) {
551 Manager *m = NULL;
87f0e418 552 Unit *target = NULL;
60918275
LP
553 Job *job = NULL;
554 int r, retval = 1;
a16e1123
LP
555 FDSet *fds = NULL;
556 bool reexecute = false;
27b14a22 557
843d2643 558 if (getpid() == 1) {
a5dab5ce 559 running_as = MANAGER_INIT;
843d2643
LP
560 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
561 } else
a5dab5ce
LP
562 running_as = MANAGER_SESSION;
563
f170852a
LP
564 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
565 goto finish;
60918275 566
f170852a
LP
567 /* Mount /proc, /sys and friends, so that /proc/cmdline and
568 * /proc/$PID/fd is available. */
d89e521e
LP
569 if (mount_setup() < 0)
570 goto finish;
4ade7963
LP
571
572 /* Reset all signal handlers. */
573 assert_se(reset_all_signal_handlers() == 0);
574
078e4539 575 /* If we are init, we can block sigkill. Yay. */
a337c6fc
LP
576 ignore_signal(SIGKILL);
577 ignore_signal(SIGPIPE);
078e4539 578
a5dab5ce
LP
579 if (running_as != MANAGER_SESSION)
580 if (parse_proc_cmdline() < 0)
581 goto finish;
f170852a
LP
582
583 log_parse_environment();
584
585 if (parse_argv(argc, argv) < 0)
586 goto finish;
587
588 if (action == ACTION_HELP) {
589 retval = help();
590 goto finish;
e537352b
LP
591 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
592 unit_dump_config_items(stdout);
593 retval = 0;
594 goto finish;
f170852a
LP
595 }
596
e965d56d 597 assert_se(action == ACTION_RUN || action == ACTION_TEST);
f170852a 598
a16e1123
LP
599 /* Remember open file descriptors for later deserialization */
600 if (serialization) {
601 if ((r = fdset_new_fill(&fds)) < 0) {
602 log_error("Failed to allocate fd set: %s", strerror(-r));
603 goto finish;
604 }
605
606 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
607 } else
608 close_all_fds(NULL, 0);
609
09082a94 610 /* Set up PATH unless it is already set */
e537352b
LP
611 setenv("PATH",
612 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
613 running_as == MANAGER_INIT);
09082a94 614
f170852a
LP
615 /* Move out of the way, so that we won't block unmounts */
616 assert_se(chdir("/") == 0);
617
80876c20
LP
618 if (running_as != MANAGER_SESSION) {
619 /* Become a session leader if we aren't one yet. */
620 setsid();
4ade7963 621
80876c20
LP
622 /* Disable the umask logic */
623 umask(0);
624 }
625
843d2643
LP
626 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
627 dbus_connection_set_change_sigpipe(FALSE);
628
2146621b
LP
629 /* Reset the console, but only if this is really init and we
630 * are freshly booted */
843d2643 631 if (running_as != MANAGER_SESSION && action == ACTION_RUN) {
2146621b 632 console_setup(getpid() == 1 && !serialization);
843d2643
LP
633 make_null_stdio();
634 }
4ade7963 635
18149b9f 636 /* Open the logging devices, if possible and necessary */
843d2643 637 log_open();
4ade7963 638
5373d602
LP
639 /* Make sure we leave a core dump without panicing the
640 * kernel. */
4fc935ca
LP
641 if (getpid() == 1)
642 install_crash_handler();
97c4f35c 643
a5dab5ce
LP
644 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
645
af5bc85d 646 if (running_as == MANAGER_INIT) {
e537352b 647 hostname_setup();
af5bc85d
LP
648 loopback_setup();
649 }
302e8c4c 650
80876c20 651 if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) {
8e274523 652 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
653 goto finish;
654 }
655
a16e1123 656 if ((r = manager_startup(m, serialization, fds)) < 0)
6e2ef85b 657 log_error("Failed to fully start up daemon: %s", strerror(-r));
a16e1123
LP
658
659 if (fds) {
660 /* This will close all file descriptors that were opened, but
661 * not claimed by any unit. */
662
663 fdset_free(fds);
664 fds = NULL;
f50e0a01
LP
665 }
666
a16e1123
LP
667 if (serialization) {
668 fclose(serialization);
669 serialization = NULL;
670 } else {
671 log_debug("Activating default unit: %s", default_unit);
672
673 if ((r = manager_load_unit(m, default_unit, NULL, &target)) < 0) {
674 log_error("Failed to load default target: %s", strerror(-r));
27b14a22 675
a16e1123
LP
676 log_info("Trying to load rescue target...");
677 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &target)) < 0) {
678 log_error("Failed to load rescue target: %s", strerror(-r));
679 goto finish;
680 }
681 }
37d88da7 682
a16e1123 683 if (action == ACTION_TEST) {
40d50879 684 printf("-> By units:\n");
a16e1123
LP
685 manager_dump_units(m, stdout, "\t");
686 }
687
688 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
689 log_error("Failed to start default target: %s", strerror(-r));
37d88da7
LP
690 goto finish;
691 }
60918275 692
a16e1123 693 if (action == ACTION_TEST) {
40d50879 694 printf("-> By jobs:\n");
a16e1123
LP
695 manager_dump_jobs(m, stdout, "\t");
696 retval = 0;
697 goto finish;
698 }
e965d56d 699 }
d46de8a1 700
a16e1123
LP
701 for (;;) {
702 if ((r = manager_loop(m)) < 0) {
703 log_error("Failed to run mainloop: %s", strerror(-r));
704 goto finish;
705 }
11dd41ce 706
a16e1123 707 switch (m->exit_code) {
e965d56d 708
a16e1123
LP
709 case MANAGER_EXIT:
710 retval = 0;
711 log_debug("Exit.");
712 goto finish;
e965d56d 713
a16e1123
LP
714 case MANAGER_RELOAD:
715 if ((r = manager_reload(m)) < 0)
716 log_error("Failed to reload: %s", strerror(-r));
717 break;
cea8e32e 718
a16e1123 719 case MANAGER_REEXECUTE:
a16e1123
LP
720 if (prepare_reexecute(m, &serialization, &fds) < 0)
721 goto finish;
60918275 722
a16e1123
LP
723 reexecute = true;
724 log_debug("Reexecuting.");
725 goto finish;
726
727 default:
728 assert_not_reached("Unknown exit code.");
729 }
730 }
f170852a 731
60918275
LP
732finish:
733 if (m)
734 manager_free(m);
735
f170852a 736 free(default_unit);
b9cd2ec1 737
ea430986
LP
738 dbus_shutdown();
739
a16e1123
LP
740 if (reexecute) {
741 const char *args[11];
742 unsigned i = 0;
743 char sfd[16];
744
745 assert(serialization);
746 assert(fds);
747
748 args[i++] = SYSTEMD_BINARY_PATH;
749
750 args[i++] = "--log-level";
751 args[i++] = log_level_to_string(log_get_max_level());
752
753 args[i++] = "--log-target";
754 args[i++] = log_target_to_string(log_get_target());
755
756 args[i++] = "--running-as";
757 args[i++] = manager_running_as_to_string(running_as);
758
759 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
760 char_array_0(sfd);
761
762 args[i++] = "--deserialize";
763 args[i++] = sfd;
764
765 if (confirm_spawn)
766 args[i++] = "--confirm-spawn";
767
768 args[i++] = NULL;
769
770 assert(i <= ELEMENTSOF(args));
771
772 execv(args[0], (char* const*) args);
773
774 log_error("Failed to reexecute: %m");
775 }
776
777 if (serialization)
778 fclose(serialization);
779
780 if (fds)
781 fdset_free(fds);
782
c3b3c274
LP
783 if (getpid() == 1)
784 freeze();
785
60918275
LP
786 return retval;
787}