]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/main.c
main: show welcome string only when asked for
[thirdparty/systemd.git] / src / main.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <dbus/dbus.h>
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <getopt.h>
31 #include <signal.h>
32 #include <sys/wait.h>
33 #include <fcntl.h>
34
35 #include "manager.h"
36 #include "log.h"
37 #include "mount-setup.h"
38 #include "hostname-setup.h"
39 #include "loopback-setup.h"
40 #include "kmod-setup.h"
41 #include "modprobe-setup.h"
42 #include "load-fragment.h"
43 #include "fdset.h"
44 #include "special.h"
45 #include "conf-parser.h"
46
47 static enum {
48 ACTION_RUN,
49 ACTION_HELP,
50 ACTION_TEST,
51 ACTION_DUMP_CONFIGURATION_ITEMS,
52 ACTION_DONE
53 } arg_action = ACTION_RUN;
54
55 static char *arg_default_unit = NULL;
56 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
57
58 static bool arg_dump_core = true;
59 static bool arg_crash_shell = false;
60 static int arg_crash_chvt = -1;
61 static bool arg_confirm_spawn = false;
62 static bool arg_nomodules = false;
63 static bool arg_show_status = true;
64
65 static FILE* serialization = NULL;
66
67 _noreturn_ static void freeze(void) {
68 for (;;)
69 pause();
70 }
71
72 static void nop_handler(int sig) {
73 }
74
75 _noreturn_ static void crash(int sig) {
76
77 if (!arg_dump_core)
78 log_error("Caught <%s>, not dumping core.", strsignal(sig));
79 else {
80 struct sigaction sa;
81 pid_t pid;
82
83 /* We want to wait for the core process, hence let's enable SIGCHLD */
84 zero(sa);
85 sa.sa_handler = nop_handler;
86 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
87 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
88
89 if ((pid = fork()) < 0)
90 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
91
92 else if (pid == 0) {
93 struct rlimit rl;
94
95 /* Enable default signal handler for core dump */
96 zero(sa);
97 sa.sa_handler = SIG_DFL;
98 assert_se(sigaction(sig, &sa, NULL) == 0);
99
100 /* Don't limit the core dump size */
101 zero(rl);
102 rl.rlim_cur = RLIM_INFINITY;
103 rl.rlim_max = RLIM_INFINITY;
104 setrlimit(RLIMIT_CORE, &rl);
105
106 /* Just to be sure... */
107 assert_se(chdir("/") == 0);
108
109 /* Raise the signal again */
110 raise(sig);
111
112 assert_not_reached("We shouldn't be here...");
113 _exit(1);
114
115 } else {
116 int status, r;
117
118 /* Order things nicely. */
119 if ((r = waitpid(pid, &status, 0)) < 0)
120 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
121 else if (!WCOREDUMP(status))
122 log_error("Caught <%s>, core dump failed.", strsignal(sig));
123 else
124 log_error("Caught <%s>, dumped core as pid %lu.", strsignal(sig), (unsigned long) pid);
125 }
126 }
127
128 if (arg_crash_chvt)
129 chvt(arg_crash_chvt);
130
131 if (arg_crash_shell) {
132 struct sigaction sa;
133 pid_t pid;
134
135 log_info("Executing crash shell in 10s...");
136 sleep(10);
137
138 /* Let the kernel reap children for us */
139 zero(sa);
140 sa.sa_handler = SIG_IGN;
141 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
142 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
143
144 if ((pid = fork()) < 0)
145 log_error("Failed to fork off crash shell: %s", strerror(errno));
146 else if (pid == 0) {
147 int fd, r;
148
149 if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
150 log_error("Failed to acquire terminal: %s", strerror(-fd));
151 else if ((r = make_stdio(fd)) < 0)
152 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
153
154 execl("/bin/sh", "/bin/sh", NULL);
155
156 log_error("execl() failed: %s", strerror(errno));
157 _exit(1);
158 }
159
160 log_info("Successfully spawned crash shall as pid %lu.", (unsigned long) pid);
161 }
162
163 log_info("Freezing execution.");
164 freeze();
165 }
166
167 static void install_crash_handler(void) {
168 struct sigaction sa;
169
170 zero(sa);
171
172 sa.sa_handler = crash;
173 sa.sa_flags = SA_NODEFER;
174
175 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
176 }
177
178 static int make_null_stdio(void) {
179 int null_fd, r;
180
181 if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) {
182 log_error("Failed to open /dev/null: %m");
183 return -errno;
184 }
185
186 if ((r = make_stdio(null_fd)) < 0)
187 log_warning("Failed to dup2() device: %s", strerror(-r));
188
189 return r;
190 }
191
192 static int console_setup(bool do_reset) {
193 int tty_fd, r;
194
195 /* If we are init, we connect stdin/stdout/stderr to /dev/null
196 * and make sure we don't have a controlling tty. */
197
198 release_terminal();
199
200 if (!do_reset)
201 return 0;
202
203 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
204 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
205 return -tty_fd;
206 }
207
208 if ((r = reset_terminal(tty_fd)) < 0)
209 log_error("Failed to reset /dev/console: %s", strerror(-r));
210
211 close_nointr_nofail(tty_fd);
212 return r;
213 }
214
215 static int set_default_unit(const char *u) {
216 char *c;
217
218 assert(u);
219
220 if (!(c = strdup(u)))
221 return -ENOMEM;
222
223 free(arg_default_unit);
224 arg_default_unit = c;
225 return 0;
226 }
227
228 static int parse_proc_cmdline_word(const char *word) {
229
230 static const char * const rlmap[] = {
231 "single", SPECIAL_RESCUE_TARGET,
232 "-s", SPECIAL_RESCUE_TARGET,
233 "s", SPECIAL_RESCUE_TARGET,
234 "S", SPECIAL_RESCUE_TARGET,
235 "1", SPECIAL_RESCUE_TARGET,
236 "2", SPECIAL_RUNLEVEL2_TARGET,
237 "3", SPECIAL_RUNLEVEL3_TARGET,
238 "4", SPECIAL_RUNLEVEL4_TARGET,
239 "5", SPECIAL_RUNLEVEL5_TARGET
240 };
241 bool ignore_quiet = false;
242
243 if (startswith(word, "systemd.unit="))
244 return set_default_unit(word + 13);
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
256 } else if (startswith(word, "systemd.log_color=")) {
257
258 if (log_show_color_from_string(word + 18) < 0)
259 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
260
261 } else if (startswith(word, "systemd.log_location=")) {
262
263 if (log_show_location_from_string(word + 21) < 0)
264 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
265
266 } else if (startswith(word, "systemd.dump_core=")) {
267 int r;
268
269 if ((r = parse_boolean(word + 18)) < 0)
270 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
271 else
272 arg_dump_core = r;
273
274 } else if (startswith(word, "systemd.crash_shell=")) {
275 int r;
276
277 if ((r = parse_boolean(word + 20)) < 0)
278 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
279 else
280 arg_crash_shell = r;
281
282 } else if (startswith(word, "systemd.confirm_spawn=")) {
283 int r;
284
285 if ((r = parse_boolean(word + 22)) < 0)
286 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
287 else
288 arg_confirm_spawn = r;
289
290 } else if (startswith(word, "systemd.crash_chvt=")) {
291 int k;
292
293 if (safe_atoi(word + 19, &k) < 0)
294 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
295 else
296 arg_crash_chvt = k;
297
298 } else if (startswith(word, "systemd.show_status=")) {
299 int r;
300
301 if ((r = parse_boolean(word + 20)) < 0)
302 log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
303 else {
304 arg_show_status = r;
305 ignore_quiet = true;
306 }
307
308 } else if (startswith(word, "systemd.")) {
309
310 log_warning("Unknown kernel switch %s. Ignoring.", word);
311
312 log_info("Supported kernel switches:\n"
313 "systemd.unit=UNIT Default unit to start\n"
314 "systemd.log_target=console|kmsg|syslog| Log target\n"
315 " syslog-org-kmsg|null\n"
316 "systemd.log_level=LEVEL Log level\n"
317 "systemd.log_color=0|1 Highlight important log messages\n"
318 "systemd.log_location=0|1 Include code location in log messages\n"
319 "systemd.dump_core=0|1 Dump core on crash\n"
320 "systemd.crash_shell=0|1 Run shell on crash\n"
321 "systemd.crash_chvt=N Change to VT #N on crash\n"
322 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
323 "systemd.show_status=0|1 Show status updates on the console during bootup\n");
324
325 } else if (streq(word, "nomodules"))
326 arg_nomodules = true;
327 else if (streq(word, "quiet")) {
328 if (!ignore_quiet)
329 arg_show_status = false;
330 } else {
331 unsigned i;
332
333 /* SysV compatibility */
334 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
335 if (streq(word, rlmap[i]))
336 return set_default_unit(rlmap[i+1]);
337 }
338
339 return 0;
340 }
341
342 static int config_parse_level(
343 const char *filename,
344 unsigned line,
345 const char *section,
346 const char *lvalue,
347 const char *rvalue,
348 void *data,
349 void *userdata) {
350
351 assert(filename);
352 assert(lvalue);
353 assert(rvalue);
354
355 log_set_max_level_from_string(rvalue);
356 return 0;
357 }
358
359 static int config_parse_target(
360 const char *filename,
361 unsigned line,
362 const char *section,
363 const char *lvalue,
364 const char *rvalue,
365 void *data,
366 void *userdata) {
367
368 assert(filename);
369 assert(lvalue);
370 assert(rvalue);
371
372 log_set_target_from_string(rvalue);
373 return 0;
374 }
375
376 static int config_parse_color(
377 const char *filename,
378 unsigned line,
379 const char *section,
380 const char *lvalue,
381 const char *rvalue,
382 void *data,
383 void *userdata) {
384
385 assert(filename);
386 assert(lvalue);
387 assert(rvalue);
388
389 log_show_color_from_string(rvalue);
390 return 0;
391 }
392
393 static int config_parse_location(
394 const char *filename,
395 unsigned line,
396 const char *section,
397 const char *lvalue,
398 const char *rvalue,
399 void *data,
400 void *userdata) {
401
402 assert(filename);
403 assert(lvalue);
404 assert(rvalue);
405
406 log_show_location_from_string(rvalue);
407 return 0;
408 }
409
410 static int config_parse_cpu_affinity(
411 const char *filename,
412 unsigned line,
413 const char *section,
414 const char *lvalue,
415 const char *rvalue,
416 void *data,
417 void *userdata) {
418
419 char *w;
420 size_t l;
421 char *state;
422 cpu_set_t *c = NULL;
423 unsigned ncpus = 0;
424
425 assert(filename);
426 assert(lvalue);
427 assert(rvalue);
428
429 FOREACH_WORD(w, l, rvalue, state) {
430 char *t;
431 int r;
432 unsigned cpu;
433
434 if (!(t = strndup(w, l)))
435 return -ENOMEM;
436
437 r = safe_atou(t, &cpu);
438 free(t);
439
440 if (!c)
441 if (!(c = cpu_set_malloc(&ncpus)))
442 return -ENOMEM;
443
444 if (r < 0 || cpu >= ncpus) {
445 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
446 CPU_FREE(c);
447 return -EBADMSG;
448 }
449
450 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
451 }
452
453 if (c) {
454 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
455 log_warning("Failed to set CPU affinity: %m");
456
457 CPU_FREE(c);
458 }
459
460 return 0;
461 }
462
463 static int parse_config_file(void) {
464
465 const ConfigItem items[] = {
466 { "LogLevel", config_parse_level, NULL, "Manager" },
467 { "LogTarget", config_parse_target, NULL, "Manager" },
468 { "LogColor", config_parse_color, NULL, "Manager" },
469 { "LogLocation", config_parse_location, NULL, "Manager" },
470 { "DumpCore", config_parse_bool, &arg_dump_core, "Manager" },
471 { "CrashShell", config_parse_bool, &arg_crash_shell, "Manager" },
472 { "ShowStatus", config_parse_bool, &arg_show_status, "Manager" },
473 { "CrashChVT", config_parse_int, &arg_crash_chvt, "Manager" },
474 { "CPUAffinity", config_parse_cpu_affinity, NULL, "Manager" },
475 { NULL, NULL, NULL, NULL }
476 };
477
478 static const char * const sections[] = {
479 "Manager",
480 NULL
481 };
482
483 FILE *f;
484 const char *fn;
485 int r;
486
487 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : SESSION_CONFIG_FILE;
488
489 if (!(f = fopen(fn, "re"))) {
490 if (errno == ENOENT)
491 return 0;
492
493 log_warning("Failed to open configuration file '%s': %m", fn);
494 return 0;
495 }
496
497 if ((r = config_parse(fn, f, sections, items, false, NULL)) < 0)
498 log_warning("Failed to parse configuration file: %s", strerror(-r));
499
500 fclose(f);
501
502 return 0;
503 }
504
505 static int parse_proc_cmdline(void) {
506 char *line;
507 int r;
508 char *w;
509 size_t l;
510 char *state;
511
512 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
513 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
514 return 0;
515 }
516
517 FOREACH_WORD_QUOTED(w, l, line, state) {
518 char *word;
519
520 if (!(word = strndup(w, l))) {
521 r = -ENOMEM;
522 goto finish;
523 }
524
525 r = parse_proc_cmdline_word(word);
526 free(word);
527
528 if (r < 0)
529 goto finish;
530 }
531
532 r = 0;
533
534 finish:
535 free(line);
536 return r;
537 }
538
539 static int parse_argv(int argc, char *argv[]) {
540
541 enum {
542 ARG_LOG_LEVEL = 0x100,
543 ARG_LOG_TARGET,
544 ARG_LOG_COLOR,
545 ARG_LOG_LOCATION,
546 ARG_UNIT,
547 ARG_RUNNING_AS,
548 ARG_TEST,
549 ARG_DUMP_CONFIGURATION_ITEMS,
550 ARG_DUMP_CORE,
551 ARG_CRASH_SHELL,
552 ARG_CONFIRM_SPAWN,
553 ARG_SHOW_STATUS,
554 ARG_DESERIALIZE,
555 ARG_INTROSPECT
556 };
557
558 static const struct option options[] = {
559 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
560 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
561 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
562 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
563 { "unit", required_argument, NULL, ARG_UNIT },
564 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
565 { "test", no_argument, NULL, ARG_TEST },
566 { "help", no_argument, NULL, 'h' },
567 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
568 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
569 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
570 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
571 { "show-status", no_argument, NULL, ARG_SHOW_STATUS },
572 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
573 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
574 { NULL, 0, NULL, 0 }
575 };
576
577 int c, r;
578
579 assert(argc >= 1);
580 assert(argv);
581
582 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
583
584 switch (c) {
585
586 case ARG_LOG_LEVEL:
587 if ((r = log_set_max_level_from_string(optarg)) < 0) {
588 log_error("Failed to parse log level %s.", optarg);
589 return r;
590 }
591
592 break;
593
594 case ARG_LOG_TARGET:
595
596 if ((r = log_set_target_from_string(optarg)) < 0) {
597 log_error("Failed to parse log target %s.", optarg);
598 return r;
599 }
600
601 break;
602
603 case ARG_LOG_COLOR:
604
605 if (optarg) {
606 if ((r = log_show_color_from_string(optarg)) < 0) {
607 log_error("Failed to parse log color setting %s.", optarg);
608 return r;
609 }
610 } else
611 log_show_color(true);
612
613 break;
614
615 case ARG_LOG_LOCATION:
616
617 if (optarg) {
618 if ((r = log_show_location_from_string(optarg)) < 0) {
619 log_error("Failed to parse log location setting %s.", optarg);
620 return r;
621 }
622 } else
623 log_show_location(true);
624
625 break;
626
627 case ARG_UNIT:
628
629 if ((r = set_default_unit(optarg)) < 0) {
630 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
631 return r;
632 }
633
634 break;
635
636 case ARG_RUNNING_AS: {
637 ManagerRunningAs as;
638
639 if ((as = manager_running_as_from_string(optarg)) < 0) {
640 log_error("Failed to parse running as value %s", optarg);
641 return -EINVAL;
642 }
643
644 arg_running_as = as;
645 break;
646 }
647
648 case ARG_TEST:
649 arg_action = ACTION_TEST;
650 break;
651
652 case ARG_DUMP_CONFIGURATION_ITEMS:
653 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
654 break;
655
656 case ARG_DUMP_CORE:
657 arg_dump_core = true;
658 break;
659
660 case ARG_CRASH_SHELL:
661 arg_crash_shell = true;
662 break;
663
664 case ARG_CONFIRM_SPAWN:
665 arg_confirm_spawn = true;
666 break;
667
668 case ARG_SHOW_STATUS:
669 arg_show_status = true;
670 break;
671
672 case ARG_DESERIALIZE: {
673 int fd;
674 FILE *f;
675
676 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
677 log_error("Failed to parse deserialize option %s.", optarg);
678 return r;
679 }
680
681 if (!(f = fdopen(fd, "r"))) {
682 log_error("Failed to open serialization fd: %m");
683 return r;
684 }
685
686 if (serialization)
687 fclose(serialization);
688
689 serialization = f;
690
691 break;
692 }
693
694 case ARG_INTROSPECT: {
695 const char * const * i = NULL;
696
697 for (i = bus_interface_table; *i; i += 2)
698 if (!optarg || streq(i[0], optarg)) {
699 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
700 "<node>\n", stdout);
701 fputs(i[1], stdout);
702 fputs("</node>\n", stdout);
703
704 if (optarg)
705 break;
706 }
707
708 if (!i[0] && optarg)
709 log_error("Unknown interface %s.", optarg);
710
711 arg_action = ACTION_DONE;
712 break;
713 }
714
715 case 'h':
716 arg_action = ACTION_HELP;
717 break;
718
719 case '?':
720 return -EINVAL;
721
722 default:
723 log_error("Unknown option code %c", c);
724 return -EINVAL;
725 }
726
727 /* PID 1 will get the kernel arguments as parameters, which we
728 * ignore and unconditionally read from
729 * /proc/cmdline. However, we need to ignore those arguments
730 * here. */
731 if (arg_running_as != MANAGER_SYSTEM && optind < argc) {
732 log_error("Excess arguments.");
733 return -EINVAL;
734 }
735
736 return 0;
737 }
738
739 static int help(void) {
740
741 printf("%s [OPTIONS...]\n\n"
742 "Starts up and maintains the system or a session.\n\n"
743 " -h --help Show this help\n"
744 " --test Determine startup sequence, dump it and exit\n"
745 " --dump-configuration-items Dump understood unit configuration items\n"
746 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
747 " --unit=UNIT Set default unit\n"
748 " --running-as=AS Set running as (system, session)\n"
749 " --dump-core Dump core on crash\n"
750 " --crash-shell Run shell on crash\n"
751 " --confirm-spawn Ask for confirmation when spawning processes\n"
752 " --show-status Show status updates on the console during bootup\n"
753 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
754 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
755 " --log-color[=0|1] Highlight important log messages\n"
756 " --log-location[=0|1] Include code location in log messages\n",
757 program_invocation_short_name);
758
759 return 0;
760 }
761
762 static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
763 FILE *f = NULL;
764 FDSet *fds = NULL;
765 int r;
766
767 assert(m);
768 assert(_f);
769 assert(_fds);
770
771 if ((r = manager_open_serialization(&f)) < 0) {
772 log_error("Failed to create serialization faile: %s", strerror(-r));
773 goto fail;
774 }
775
776 if (!(fds = fdset_new())) {
777 r = -ENOMEM;
778 log_error("Failed to allocate fd set: %s", strerror(-r));
779 goto fail;
780 }
781
782 if ((r = manager_serialize(m, f, fds)) < 0) {
783 log_error("Failed to serialize state: %s", strerror(-r));
784 goto fail;
785 }
786
787 if (fseeko(f, 0, SEEK_SET) < 0) {
788 log_error("Failed to rewind serialization fd: %m");
789 goto fail;
790 }
791
792 if ((r = fd_cloexec(fileno(f), false)) < 0) {
793 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
794 goto fail;
795 }
796
797 if ((r = fdset_cloexec(fds, false)) < 0) {
798 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
799 goto fail;
800 }
801
802 *_f = f;
803 *_fds = fds;
804
805 return 0;
806
807 fail:
808 fdset_free(fds);
809
810 if (f)
811 fclose(f);
812
813 return r;
814 }
815
816 int main(int argc, char *argv[]) {
817 Manager *m = NULL;
818 Unit *target = NULL;
819 Job *job = NULL;
820 int r, retval = 1;
821 FDSet *fds = NULL;
822 bool reexecute = false;
823
824 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
825 /* This is compatbility support for SysV, where
826 * calling init as a user is identical to telinit. */
827
828 errno = -ENOENT;
829 execv(SYSTEMCTL_BINARY_PATH, argv);
830 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
831 return 1;
832 }
833
834 log_show_color(true);
835 log_show_location(false);
836 log_set_max_level(LOG_DEBUG);
837
838 if (getpid() == 1) {
839 arg_running_as = MANAGER_SYSTEM;
840 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
841 } else {
842 arg_running_as = MANAGER_SESSION;
843 log_set_target(LOG_TARGET_CONSOLE);
844 }
845
846 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
847 goto finish;
848
849 /* Mount /proc, /sys and friends, so that /proc/cmdline and
850 * /proc/$PID/fd is available. */
851 if (geteuid() == 0)
852 if (mount_setup() < 0)
853 goto finish;
854
855 /* Reset all signal handlers. */
856 assert_se(reset_all_signal_handlers() == 0);
857
858 /* If we are init, we can block sigkill. Yay. */
859 ignore_signals(SIGNALS_IGNORE, -1);
860
861 if (parse_config_file() < 0)
862 goto finish;
863
864 if (arg_running_as == MANAGER_SYSTEM)
865 if (parse_proc_cmdline() < 0)
866 goto finish;
867
868 log_parse_environment();
869
870 if (parse_argv(argc, argv) < 0)
871 goto finish;
872
873 if (arg_action == ACTION_HELP) {
874 retval = help();
875 goto finish;
876 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
877 unit_dump_config_items(stdout);
878 retval = 0;
879 goto finish;
880 } else if (arg_action == ACTION_DONE) {
881 retval = 0;
882 goto finish;
883 }
884
885 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
886
887 /* Remember open file descriptors for later deserialization */
888 if (serialization) {
889 if ((r = fdset_new_fill(&fds)) < 0) {
890 log_error("Failed to allocate fd set: %s", strerror(-r));
891 goto finish;
892 }
893
894 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
895 } else
896 close_all_fds(NULL, 0);
897
898 /* Set up PATH unless it is already set */
899 setenv("PATH",
900 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
901 arg_running_as == MANAGER_SYSTEM);
902
903 /* Move out of the way, so that we won't block unmounts */
904 assert_se(chdir("/") == 0);
905
906 if (arg_running_as == MANAGER_SYSTEM) {
907 /* Become a session leader if we aren't one yet. */
908 setsid();
909
910 /* Disable the umask logic */
911 umask(0);
912 }
913
914 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
915 dbus_connection_set_change_sigpipe(FALSE);
916
917 /* Reset the console, but only if this is really init and we
918 * are freshly booted */
919 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
920 console_setup(getpid() == 1 && !serialization);
921 make_null_stdio();
922 }
923
924 /* Open the logging devices, if possible and necessary */
925 log_open();
926
927 /* Make sure we leave a core dump without panicing the
928 * kernel. */
929 if (getpid() == 1)
930 install_crash_handler();
931
932 log_debug(PACKAGE_STRING " running in %s mode.", manager_running_as_to_string(arg_running_as));
933
934 if (arg_running_as == MANAGER_SYSTEM && !serialization) {
935 if (arg_show_status)
936 status_welcome();
937 modprobe_setup(arg_nomodules);
938 kmod_setup();
939 hostname_setup();
940 loopback_setup();
941 }
942
943 if ((r = manager_new(arg_running_as, &m)) < 0) {
944 log_error("Failed to allocate manager object: %s", strerror(-r));
945 goto finish;
946 }
947
948 m->confirm_spawn = arg_confirm_spawn;
949 m->show_status = arg_show_status;
950
951 if ((r = manager_startup(m, serialization, fds)) < 0)
952 log_error("Failed to fully start up daemon: %s", strerror(-r));
953
954 if (fds) {
955 /* This will close all file descriptors that were opened, but
956 * not claimed by any unit. */
957
958 fdset_free(fds);
959 fds = NULL;
960 }
961
962 if (serialization) {
963 fclose(serialization);
964 serialization = NULL;
965 } else {
966 log_debug("Activating default unit: %s", arg_default_unit);
967
968 if ((r = manager_load_unit(m, arg_default_unit, NULL, &target)) < 0) {
969 log_error("Failed to load default target: %s", strerror(-r));
970
971 log_info("Trying to load rescue target...");
972 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &target)) < 0) {
973 log_error("Failed to load rescue target: %s", strerror(-r));
974 goto finish;
975 }
976 }
977
978 if (arg_action == ACTION_TEST) {
979 printf("-> By units:\n");
980 manager_dump_units(m, stdout, "\t");
981 }
982
983 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
984 log_error("Failed to start default target: %s", strerror(-r));
985 goto finish;
986 }
987
988 if (arg_action == ACTION_TEST) {
989 printf("-> By jobs:\n");
990 manager_dump_jobs(m, stdout, "\t");
991 retval = 0;
992 goto finish;
993 }
994 }
995
996 for (;;) {
997 if ((r = manager_loop(m)) < 0) {
998 log_error("Failed to run mainloop: %s", strerror(-r));
999 goto finish;
1000 }
1001
1002 switch (m->exit_code) {
1003
1004 case MANAGER_EXIT:
1005 retval = 0;
1006 log_debug("Exit.");
1007 goto finish;
1008
1009 case MANAGER_RELOAD:
1010 if ((r = manager_reload(m)) < 0)
1011 log_error("Failed to reload: %s", strerror(-r));
1012 break;
1013
1014 case MANAGER_REEXECUTE:
1015 if (prepare_reexecute(m, &serialization, &fds) < 0)
1016 goto finish;
1017
1018 reexecute = true;
1019 log_debug("Reexecuting.");
1020 goto finish;
1021
1022 default:
1023 assert_not_reached("Unknown exit code.");
1024 }
1025 }
1026
1027 finish:
1028 if (m)
1029 manager_free(m);
1030
1031 free(arg_default_unit);
1032
1033 dbus_shutdown();
1034
1035 if (reexecute) {
1036 const char *args[11];
1037 unsigned i = 0;
1038 char sfd[16];
1039
1040 assert(serialization);
1041 assert(fds);
1042
1043 args[i++] = SYSTEMD_BINARY_PATH;
1044
1045 args[i++] = "--log-level";
1046 args[i++] = log_level_to_string(log_get_max_level());
1047
1048 args[i++] = "--log-target";
1049 args[i++] = log_target_to_string(log_get_target());
1050
1051 args[i++] = "--running-as";
1052 args[i++] = manager_running_as_to_string(arg_running_as);
1053
1054 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1055 char_array_0(sfd);
1056
1057 args[i++] = "--deserialize";
1058 args[i++] = sfd;
1059
1060 if (arg_confirm_spawn)
1061 args[i++] = "--confirm-spawn";
1062
1063 args[i++] = NULL;
1064
1065 assert(i <= ELEMENTSOF(args));
1066
1067 execv(args[0], (char* const*) args);
1068
1069 log_error("Failed to reexecute: %m");
1070 }
1071
1072 if (serialization)
1073 fclose(serialization);
1074
1075 if (fds)
1076 fdset_free(fds);
1077
1078 if (getpid() == 1)
1079 freeze();
1080
1081 return retval;
1082 }