]> git.ipfire.org Git - people/ms/systemd.git/blame - main.c
device: allow easy identification of network interfaces without their full sysfs...
[people/ms/systemd.git] / 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) {
139 execl("/bin/sh", "/bin/sh", NULL);
140
141 log_error("execl() failed: %s", strerror(errno));
142 _exit(1);
143 }
c99b188e 144
6f5e3f35 145 log_info("Successfully spawned crash shall as pid %llu.", (unsigned long long) pid);
4fc935ca
LP
146 }
147
148 log_info("Freezing execution.");
97c4f35c
LP
149 freeze();
150}
151
152static void install_crash_handler(void) {
153 struct sigaction sa;
154
155 zero(sa);
156
157 sa.sa_handler = crash;
158 sa.sa_flags = SA_NODEFER;
159
160 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
5373d602
LP
161 assert_se(sigaction(SIGILL, &sa, NULL) == 0);
162 assert_se(sigaction(SIGFPE, &sa, NULL) == 0);
163 assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
164 assert_se(sigaction(SIGQUIT, &sa, NULL) == 0);
97c4f35c
LP
165 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
166}
f170852a 167
2146621b 168static int console_setup(bool do_reset) {
80876c20
LP
169 int tty_fd = -1, null_fd = -1, r = 0;
170
171 /* If we are init, we connect stdout/stderr to /dev/console
172 * and stdin to /dev/null and make sure we don't have a
173 * controlling tty. */
174
175 release_terminal();
176
177 if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) {
178 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
179 r = -tty_fd;
180 goto finish;
181 }
182
183 if ((null_fd = open("/dev/null", O_RDONLY)) < 0) {
184 log_error("Failed to open /dev/null: %m");
185 r = -errno;
186 goto finish;
187 }
188
189 assert(tty_fd >= 3);
190 assert(null_fd >= 3);
191
2146621b
LP
192 if (do_reset)
193 if (reset_terminal(tty_fd) < 0)
194 log_error("Failed to reset /dev/console: %m");
80876c20
LP
195
196 if (dup2(tty_fd, STDOUT_FILENO) < 0 ||
197 dup2(tty_fd, STDERR_FILENO) < 0 ||
198 dup2(null_fd, STDIN_FILENO) < 0) {
199 log_error("Failed to dup2() device: %m");
200 r = -errno;
201 goto finish;
202 }
203
204 r = 0;
205
206finish:
207 if (tty_fd >= 0)
a16e1123 208 close_nointr_nofail(tty_fd);
80876c20
LP
209
210 if (null_fd >= 0)
a16e1123 211 close_nointr_nofail(null_fd);
80876c20
LP
212
213 return r;
214}
215
f170852a
LP
216static int set_default_unit(const char *u) {
217 char *c;
218
219 assert(u);
220
221 if (!(c = strdup(u)))
222 return -ENOMEM;
223
224 free(default_unit);
225 default_unit = c;
226 return 0;
227}
228
229static int parse_proc_cmdline_word(const char *word) {
230
231 static const char * const rlmap[] = {
232 "single", SPECIAL_RUNLEVEL1_TARGET,
233 "-s", SPECIAL_RUNLEVEL1_TARGET,
234 "s", SPECIAL_RUNLEVEL1_TARGET,
235 "S", SPECIAL_RUNLEVEL1_TARGET,
236 "1", SPECIAL_RUNLEVEL1_TARGET,
237 "2", SPECIAL_RUNLEVEL2_TARGET,
238 "3", SPECIAL_RUNLEVEL3_TARGET,
239 "4", SPECIAL_RUNLEVEL4_TARGET,
240 "5", SPECIAL_RUNLEVEL5_TARGET
241 };
242
243 if (startswith(word, "systemd.default="))
82771ba1 244 return set_default_unit(word + 16);
f170852a
LP
245
246 else if (startswith(word, "systemd.log_target=")) {
247
248 if (log_set_target_from_string(word + 19) < 0)
249 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
250
251 } else if (startswith(word, "systemd.log_level=")) {
252
253 if (log_set_max_level_from_string(word + 18) < 0)
254 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
255
4fc935ca
LP
256 } else if (startswith(word, "systemd.dump_core=")) {
257 int r;
258
259 if ((r = parse_boolean(word + 18)) < 0)
601f6a1e 260 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
4fc935ca
LP
261 else
262 dump_core = r;
263
264 } else if (startswith(word, "systemd.crash_shell=")) {
265 int r;
266
267 if ((r = parse_boolean(word + 20)) < 0)
601f6a1e 268 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
4fc935ca
LP
269 else
270 crash_shell = r;
271
5e7ee61c
LP
272
273 } else if (startswith(word, "systemd.confirm_spawn=")) {
274 int r;
275
276 if ((r = parse_boolean(word + 22)) < 0)
277 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
278 else
279 confirm_spawn = r;
280
601f6a1e
LP
281 } else if (startswith(word, "systemd.crash_chvt=")) {
282 int k;
283
284 if (safe_atoi(word + 19, &k) < 0)
285 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
286 else
287 crash_chvt = k;
288
4fc935ca
LP
289 } else if (startswith(word, "systemd.")) {
290
291 log_warning("Unknown kernel switch %s. Ignoring.", word);
292
293 log_info("Supported kernel switches:");
294 log_info("systemd.default=UNIT Default unit to start");
295 log_info("systemd.log_target=console|kmsg|syslog Log target");
296 log_info("systemd.log_level=LEVEL Log level");
297 log_info("systemd.dump_core=0|1 Dump core on crash");
298 log_info("systemd.crash_shell=0|1 On crash run shell");
601f6a1e 299 log_info("systemd.crash_chvt=N Change to VT #N on crash");
5e7ee61c 300 log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
4fc935ca 301
f170852a
LP
302 } else {
303 unsigned i;
304
305 /* SysV compatibility */
f170852a
LP
306 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
307 if (streq(word, rlmap[i]))
308 return set_default_unit(rlmap[i+1]);
309 }
310
311 return 0;
312}
313
314static int parse_proc_cmdline(void) {
315 char *line;
316 int r;
317 char *w;
318 size_t l;
319 char *state;
320
321 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
322 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
323 return 0;
324 }
325
326 FOREACH_WORD_QUOTED(w, l, line, state) {
327 char *word;
328
329 if (!(word = strndup(w, l))) {
330 r = -ENOMEM;
331 goto finish;
332 }
333
334 r = parse_proc_cmdline_word(word);
335 free(word);
336
337 if (r < 0)
338 goto finish;
339 }
340
341 r = 0;
342
343finish:
344 free(line);
345 return r;
346}
347
348static int parse_argv(int argc, char *argv[]) {
349
350 enum {
351 ARG_LOG_LEVEL = 0x100,
352 ARG_LOG_TARGET,
a5dab5ce 353 ARG_DEFAULT,
e965d56d 354 ARG_RUNNING_AS,
e537352b 355 ARG_TEST,
80876c20 356 ARG_DUMP_CONFIGURATION_ITEMS,
a16e1123
LP
357 ARG_CONFIRM_SPAWN,
358 ARG_DESERIALIZE
f170852a
LP
359 };
360
361 static const struct option options[] = {
a16e1123
LP
362 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
363 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
364 { "default", required_argument, NULL, ARG_DEFAULT },
365 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
366 { "test", no_argument, NULL, ARG_TEST },
367 { "help", no_argument, NULL, 'h' },
368 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
369 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
370 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
371 { NULL, 0, NULL, 0 }
f170852a
LP
372 };
373
374 int c, r;
375
376 assert(argc >= 1);
377 assert(argv);
378
379 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
380
381 switch (c) {
382
383 case ARG_LOG_LEVEL:
384 if ((r = log_set_max_level_from_string(optarg)) < 0) {
385 log_error("Failed to parse log level %s.", optarg);
386 return r;
387 }
388
389 break;
390
391 case ARG_LOG_TARGET:
392
393 if ((r = log_set_target_from_string(optarg)) < 0) {
394 log_error("Failed to parse log target %s.", optarg);
395 return r;
396 }
397
398 break;
399
400 case ARG_DEFAULT:
401
402 if ((r = set_default_unit(optarg)) < 0) {
403 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
404 return r;
405 }
406
407 break;
408
a5dab5ce
LP
409 case ARG_RUNNING_AS: {
410 ManagerRunningAs as;
411
412 if ((as = manager_running_as_from_string(optarg)) < 0) {
413 log_error("Failed to parse running as value %s", optarg);
414 return -EINVAL;
415 }
416
417 running_as = as;
418 break;
419 }
420
e965d56d
LP
421 case ARG_TEST:
422 action = ACTION_TEST;
423 break;
424
e537352b
LP
425 case ARG_DUMP_CONFIGURATION_ITEMS:
426 action = ACTION_DUMP_CONFIGURATION_ITEMS;
427 break;
428
80876c20
LP
429 case ARG_CONFIRM_SPAWN:
430 confirm_spawn = true;
431 break;
432
a16e1123
LP
433 case ARG_DESERIALIZE: {
434 int fd;
435 FILE *f;
436
437 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
438 log_error("Failed to parse deserialize option %s.", optarg);
439 return r;
440 }
441
442 if (!(f = fdopen(fd, "r"))) {
443 log_error("Failed to open serialization fd: %m");
444 return r;
445 }
446
447 if (serialization)
448 fclose(serialization);
449
450 serialization = f;
451
452 break;
453 }
454
f170852a
LP
455 case 'h':
456 action = ACTION_HELP;
457 break;
458
459 case '?':
460 return -EINVAL;
461
462 default:
463 log_error("Unknown option code %c", c);
464 return -EINVAL;
465 }
466
51f0e189
LP
467 /* PID 1 will get the kernel arguments as parameters, which we
468 * ignore and unconditionally read from
469 * /proc/cmdline. However, we need to ignore those arguments
470 * here. */
471 if (running_as != MANAGER_INIT && optind < argc) {
472 log_error("Excess arguments.");
473 return -EINVAL;
474 }
475
f170852a
LP
476 return 0;
477}
478
479static int help(void) {
480
481 printf("%s [options]\n\n"
e537352b
LP
482 " -h --help Show this help\n"
483 " --default=UNIT Set default unit\n"
484 " --log-level=LEVEL Set log level\n"
485 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
486 " --running-as=AS Set running as (init, system, session)\n"
487 " --test Determine startup sequence, dump it and exit\n"
80876c20
LP
488 " --dump-configuration-items Dump understood unit configuration items\n"
489 " --confirm-spawn Ask for confirmation when spawning processes\n",
f170852a
LP
490 __progname);
491
492 return 0;
493}
494
a16e1123
LP
495static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
496 FILE *f = NULL;
497 FDSet *fds = NULL;
498 int r;
499
500 assert(m);
501 assert(_f);
502 assert(_fds);
503
504 if ((r = manager_open_serialization(&f)) < 0) {
505 log_error("Failed to create serialization faile: %s", strerror(-r));
506 goto fail;
507 }
508
509 if (!(fds = fdset_new())) {
510 r = -ENOMEM;
511 log_error("Failed to allocate fd set: %s", strerror(-r));
512 goto fail;
513 }
514
515 if ((r = manager_serialize(m, f, fds)) < 0) {
516 log_error("Failed to serialize state: %s", strerror(-r));
517 goto fail;
518 }
519
520 if (fseeko(f, 0, SEEK_SET) < 0) {
521 log_error("Failed to rewind serialization fd: %m");
522 goto fail;
523 }
524
525 if ((r = fd_cloexec(fileno(f), false)) < 0) {
526 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
527 goto fail;
528 }
529
530 if ((r = fdset_cloexec(fds, false)) < 0) {
531 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
532 goto fail;
533 }
534
535 *_f = f;
536 *_fds = fds;
537
538 return 0;
539
540fail:
541 fdset_free(fds);
542
543 if (f)
544 fclose(f);
545
546 return r;
547}
548
60918275
LP
549int main(int argc, char *argv[]) {
550 Manager *m = NULL;
87f0e418 551 Unit *target = NULL;
60918275
LP
552 Job *job = NULL;
553 int r, retval = 1;
a16e1123
LP
554 FDSet *fds = NULL;
555 bool reexecute = false;
27b14a22 556
a5dab5ce
LP
557 if (getpid() == 1)
558 running_as = MANAGER_INIT;
559 else if (getuid() == 0)
560 running_as = MANAGER_SYSTEM;
561 else
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
2146621b
LP
626 /* Reset the console, but only if this is really init and we
627 * are freshly booted */
8d025b23 628 if (running_as == MANAGER_INIT && action == ACTION_RUN)
2146621b 629 console_setup(getpid() == 1 && !serialization);
4ade7963
LP
630
631 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
632 dbus_connection_set_change_sigpipe(FALSE);
633
18149b9f 634 /* Open the logging devices, if possible and necessary */
4ade7963
LP
635 log_open_syslog();
636 log_open_kmsg();
637
5373d602
LP
638 /* Make sure we leave a core dump without panicing the
639 * kernel. */
4fc935ca
LP
640 if (getpid() == 1)
641 install_crash_handler();
97c4f35c 642
a5dab5ce
LP
643 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
644
af5bc85d 645 if (running_as == MANAGER_INIT) {
e537352b 646 hostname_setup();
af5bc85d
LP
647 loopback_setup();
648 }
302e8c4c 649
80876c20 650 if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) {
8e274523 651 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
652 goto finish;
653 }
654
a16e1123
LP
655 if ((r = manager_startup(m, serialization, fds)) < 0)
656 log_error("Failed to fully startup daemon: %s", strerror(-r));
657
658 if (fds) {
659 /* This will close all file descriptors that were opened, but
660 * not claimed by any unit. */
661
662 fdset_free(fds);
663 fds = NULL;
f50e0a01
LP
664 }
665
a16e1123
LP
666 if (serialization) {
667 fclose(serialization);
668 serialization = NULL;
669 } else {
670 log_debug("Activating default unit: %s", default_unit);
671
672 if ((r = manager_load_unit(m, default_unit, NULL, &target)) < 0) {
673 log_error("Failed to load default target: %s", strerror(-r));
27b14a22 674
a16e1123
LP
675 log_info("Trying to load rescue target...");
676 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &target)) < 0) {
677 log_error("Failed to load rescue target: %s", strerror(-r));
678 goto finish;
679 }
680 }
37d88da7 681
a16e1123 682 if (action == ACTION_TEST) {
40d50879 683 printf("-> By units:\n");
a16e1123
LP
684 manager_dump_units(m, stdout, "\t");
685 }
686
687 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
688 log_error("Failed to start default target: %s", strerror(-r));
37d88da7
LP
689 goto finish;
690 }
60918275 691
a16e1123 692 if (action == ACTION_TEST) {
40d50879 693 printf("-> By jobs:\n");
a16e1123
LP
694 manager_dump_jobs(m, stdout, "\t");
695 retval = 0;
696 goto finish;
697 }
e965d56d 698 }
d46de8a1 699
a16e1123
LP
700 for (;;) {
701 if ((r = manager_loop(m)) < 0) {
702 log_error("Failed to run mainloop: %s", strerror(-r));
703 goto finish;
704 }
11dd41ce 705
a16e1123 706 switch (m->exit_code) {
e965d56d 707
a16e1123
LP
708 case MANAGER_EXIT:
709 retval = 0;
710 log_debug("Exit.");
711 goto finish;
e965d56d 712
a16e1123
LP
713 case MANAGER_RELOAD:
714 if ((r = manager_reload(m)) < 0)
715 log_error("Failed to reload: %s", strerror(-r));
716 break;
cea8e32e 717
a16e1123 718 case MANAGER_REEXECUTE:
a16e1123
LP
719 if (prepare_reexecute(m, &serialization, &fds) < 0)
720 goto finish;
60918275 721
a16e1123
LP
722 reexecute = true;
723 log_debug("Reexecuting.");
724 goto finish;
725
726 default:
727 assert_not_reached("Unknown exit code.");
728 }
729 }
f170852a 730
60918275
LP
731finish:
732 if (m)
733 manager_free(m);
734
f170852a 735 free(default_unit);
b9cd2ec1 736
ea430986
LP
737 dbus_shutdown();
738
a16e1123
LP
739 if (reexecute) {
740 const char *args[11];
741 unsigned i = 0;
742 char sfd[16];
743
744 assert(serialization);
745 assert(fds);
746
747 args[i++] = SYSTEMD_BINARY_PATH;
748
749 args[i++] = "--log-level";
750 args[i++] = log_level_to_string(log_get_max_level());
751
752 args[i++] = "--log-target";
753 args[i++] = log_target_to_string(log_get_target());
754
755 args[i++] = "--running-as";
756 args[i++] = manager_running_as_to_string(running_as);
757
758 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
759 char_array_0(sfd);
760
761 args[i++] = "--deserialize";
762 args[i++] = sfd;
763
764 if (confirm_spawn)
765 args[i++] = "--confirm-spawn";
766
767 args[i++] = NULL;
768
769 assert(i <= ELEMENTSOF(args));
770
771 execv(args[0], (char* const*) args);
772
773 log_error("Failed to reexecute: %m");
774 }
775
776 if (serialization)
777 fclose(serialization);
778
779 if (fds)
780 fdset_free(fds);
781
c3b3c274
LP
782 if (getpid() == 1)
783 freeze();
784
60918275
LP
785 return retval;
786}