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