1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include <dbus/dbus.h>
28 #include <sys/types.h>
37 #include "mount-setup.h"
38 #include "hostname-setup.h"
39 #include "loopback-setup.h"
40 #include "load-fragment.h"
47 ACTION_DUMP_CONFIGURATION_ITEMS
48 } action
= ACTION_RUN
;
50 static char *default_unit
= NULL
;
51 static ManagerRunningAs running_as
= _MANAGER_RUNNING_AS_INVALID
;
53 static bool dump_core
= true;
54 static bool crash_shell
= false;
55 static int crash_chvt
= -1;
56 static bool confirm_spawn
= false;
57 static FILE* serialization
= NULL
;
59 _noreturn
static void freeze(void) {
64 static void nop_handler(int sig
) {
67 _noreturn
static void crash(int sig
) {
70 log_error("Caught <%s>, not dumping core.", strsignal(sig
));
75 /* We want to wait for the core process, hence let's enable SIGCHLD */
77 sa
.sa_handler
= nop_handler
;
78 sa
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
79 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
81 if ((pid
= fork()) < 0)
82 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig
), strerror(errno
));
87 /* Enable default signal handler for core dump */
89 sa
.sa_handler
= SIG_DFL
;
90 assert_se(sigaction(sig
, &sa
, NULL
) == 0);
92 /* Don't limit the core dump size */
94 rl
.rlim_cur
= RLIM_INFINITY
;
95 rl
.rlim_max
= RLIM_INFINITY
;
96 setrlimit(RLIMIT_CORE
, &rl
);
98 /* Just to be sure... */
99 assert_se(chdir("/") == 0);
101 /* Raise the signal again */
104 assert_not_reached("We shouldn't be here...");
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
));
116 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig
), (unsigned long long) pid
);
127 log_info("Executing crash shell in 10s...");
130 /* Let the kernel reap children for us */
132 sa
.sa_handler
= SIG_IGN
;
133 sa
.sa_flags
= SA_NOCLDSTOP
|SA_NOCLDWAIT
|SA_RESTART
;
134 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
136 if ((pid
= fork()) < 0)
137 log_error("Failed to fork off crash shell: %s", strerror(errno
));
139 execl("/bin/sh", "/bin/sh", NULL
);
141 log_error("execl() failed: %s", strerror(errno
));
145 log_info("Successfully spawned crash shall as pid %llu.", (unsigned long long) pid
);
148 log_info("Freezing execution.");
152 static void install_crash_handler(void) {
157 sa
.sa_handler
= crash
;
158 sa
.sa_flags
= SA_NODEFER
;
160 assert_se(sigaction(SIGSEGV
, &sa
, NULL
) == 0);
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);
165 assert_se(sigaction(SIGABRT
, &sa
, NULL
) == 0);
168 static int console_setup(bool do_reset
) {
169 int tty_fd
= -1, null_fd
= -1, r
= 0;
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. */
177 if ((tty_fd
= open_terminal("/dev/console", O_WRONLY
)) < 0) {
178 log_error("Failed to open /dev/console: %s", strerror(-tty_fd
));
183 if ((null_fd
= open("/dev/null", O_RDONLY
)) < 0) {
184 log_error("Failed to open /dev/null: %m");
190 assert(null_fd
>= 3);
193 if (reset_terminal(tty_fd
) < 0)
194 log_error("Failed to reset /dev/console: %m");
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");
208 close_nointr_nofail(tty_fd
);
211 close_nointr_nofail(null_fd
);
216 static int set_default_unit(const char *u
) {
221 if (!(c
= strdup(u
)))
229 static int parse_proc_cmdline_word(const char *word
) {
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
243 if (startswith(word
, "systemd.default="))
244 return set_default_unit(word
+ 16);
246 else if (startswith(word
, "systemd.log_target=")) {
248 if (log_set_target_from_string(word
+ 19) < 0)
249 log_warning("Failed to parse log target %s. Ignoring.", word
+ 19);
251 } else if (startswith(word
, "systemd.log_level=")) {
253 if (log_set_max_level_from_string(word
+ 18) < 0)
254 log_warning("Failed to parse log level %s. Ignoring.", word
+ 18);
256 } else if (startswith(word
, "systemd.dump_core=")) {
259 if ((r
= parse_boolean(word
+ 18)) < 0)
260 log_warning("Failed to parse dump core switch %s, Ignoring.", word
+ 18);
264 } else if (startswith(word
, "systemd.crash_shell=")) {
267 if ((r
= parse_boolean(word
+ 20)) < 0)
268 log_warning("Failed to parse crash shell switch %s, Ignoring.", word
+ 20);
273 } else if (startswith(word
, "systemd.confirm_spawn=")) {
276 if ((r
= parse_boolean(word
+ 22)) < 0)
277 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word
+ 22);
281 } else if (startswith(word
, "systemd.crash_chvt=")) {
284 if (safe_atoi(word
+ 19, &k
) < 0)
285 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word
+ 19);
289 } else if (startswith(word
, "systemd.")) {
291 log_warning("Unknown kernel switch %s. Ignoring.", word
);
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");
299 log_info("systemd.crash_chvt=N Change to VT #N on crash");
300 log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
305 /* SysV compatibility */
306 for (i
= 0; i
< ELEMENTSOF(rlmap
); i
+= 2)
307 if (streq(word
, rlmap
[i
]))
308 return set_default_unit(rlmap
[i
+1]);
314 static int parse_proc_cmdline(void) {
321 if ((r
= read_one_line_file("/proc/cmdline", &line
)) < 0) {
322 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno
));
326 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
329 if (!(word
= strndup(w
, l
))) {
334 r
= parse_proc_cmdline_word(word
);
348 static int parse_argv(int argc
, char *argv
[]) {
351 ARG_LOG_LEVEL
= 0x100,
356 ARG_DUMP_CONFIGURATION_ITEMS
,
361 static const struct option options
[] = {
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
},
379 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
384 if ((r
= log_set_max_level_from_string(optarg
)) < 0) {
385 log_error("Failed to parse log level %s.", optarg
);
393 if ((r
= log_set_target_from_string(optarg
)) < 0) {
394 log_error("Failed to parse log target %s.", optarg
);
402 if ((r
= set_default_unit(optarg
)) < 0) {
403 log_error("Failed to set default unit %s: %s", optarg
, strerror(-r
));
409 case ARG_RUNNING_AS
: {
412 if ((as
= manager_running_as_from_string(optarg
)) < 0) {
413 log_error("Failed to parse running as value %s", optarg
);
422 action
= ACTION_TEST
;
425 case ARG_DUMP_CONFIGURATION_ITEMS
:
426 action
= ACTION_DUMP_CONFIGURATION_ITEMS
;
429 case ARG_CONFIRM_SPAWN
:
430 confirm_spawn
= true;
433 case ARG_DESERIALIZE
: {
437 if ((r
= safe_atoi(optarg
, &fd
)) < 0 || fd
< 0) {
438 log_error("Failed to parse deserialize option %s.", optarg
);
442 if (!(f
= fdopen(fd
, "r"))) {
443 log_error("Failed to open serialization fd: %m");
448 fclose(serialization
);
456 action
= ACTION_HELP
;
463 log_error("Unknown option code %c", c
);
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
471 if (running_as
!= MANAGER_INIT
&& optind
< argc
) {
472 log_error("Excess arguments.");
479 static int help(void) {
481 printf("%s [options]\n\n"
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"
488 " --dump-configuration-items Dump understood unit configuration items\n"
489 " --confirm-spawn Ask for confirmation when spawning processes\n",
495 static int prepare_reexecute(Manager
*m
, FILE **_f
, FDSet
**_fds
) {
504 if ((r
= manager_open_serialization(&f
)) < 0) {
505 log_error("Failed to create serialization faile: %s", strerror(-r
));
509 if (!(fds
= fdset_new())) {
511 log_error("Failed to allocate fd set: %s", strerror(-r
));
515 if ((r
= manager_serialize(m
, f
, fds
)) < 0) {
516 log_error("Failed to serialize state: %s", strerror(-r
));
520 if (fseeko(f
, 0, SEEK_SET
) < 0) {
521 log_error("Failed to rewind serialization fd: %m");
525 if ((r
= fd_cloexec(fileno(f
), false)) < 0) {
526 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r
));
530 if ((r
= fdset_cloexec(fds
, false)) < 0) {
531 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r
));
549 int main(int argc
, char *argv
[]) {
555 bool reexecute
= false;
558 running_as
= MANAGER_INIT
;
559 else if (getuid() == 0)
560 running_as
= MANAGER_SYSTEM
;
562 running_as
= MANAGER_SESSION
;
564 if (set_default_unit(SPECIAL_DEFAULT_TARGET
) < 0)
567 /* Mount /proc, /sys and friends, so that /proc/cmdline and
568 * /proc/$PID/fd is available. */
569 if (mount_setup() < 0)
572 /* Reset all signal handlers. */
573 assert_se(reset_all_signal_handlers() == 0);
575 /* If we are init, we can block sigkill. Yay. */
576 ignore_signal(SIGKILL
);
577 ignore_signal(SIGPIPE
);
579 if (running_as
!= MANAGER_SESSION
)
580 if (parse_proc_cmdline() < 0)
583 log_parse_environment();
585 if (parse_argv(argc
, argv
) < 0)
588 if (action
== ACTION_HELP
) {
591 } else if (action
== ACTION_DUMP_CONFIGURATION_ITEMS
) {
592 unit_dump_config_items(stdout
);
597 assert_se(action
== ACTION_RUN
|| action
== ACTION_TEST
);
599 /* Remember open file descriptors for later deserialization */
601 if ((r
= fdset_new_fill(&fds
)) < 0) {
602 log_error("Failed to allocate fd set: %s", strerror(-r
));
606 assert_se(fdset_remove(fds
, fileno(serialization
)) >= 0);
608 close_all_fds(NULL
, 0);
610 /* Set up PATH unless it is already set */
612 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
613 running_as
== MANAGER_INIT
);
615 /* Move out of the way, so that we won't block unmounts */
616 assert_se(chdir("/") == 0);
618 if (running_as
!= MANAGER_SESSION
) {
619 /* Become a session leader if we aren't one yet. */
622 /* Disable the umask logic */
626 /* Reset the console, but only if this is really init and we
627 * are freshly booted */
628 if (running_as
== MANAGER_INIT
&& action
== ACTION_RUN
)
629 console_setup(getpid() == 1 && !serialization
);
631 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
632 dbus_connection_set_change_sigpipe(FALSE
);
634 /* Open the logging devices, if possible and necessary */
638 /* Make sure we leave a core dump without panicing the
641 install_crash_handler();
643 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as
));
645 if (running_as
== MANAGER_INIT
) {
650 if ((r
= manager_new(running_as
, confirm_spawn
, &m
)) < 0) {
651 log_error("Failed to allocate manager object: %s", strerror(-r
));
655 if ((r
= manager_startup(m
, serialization
, fds
)) < 0)
656 log_error("Failed to fully startup daemon: %s", strerror(-r
));
659 /* This will close all file descriptors that were opened, but
660 * not claimed by any unit. */
667 fclose(serialization
);
668 serialization
= NULL
;
670 log_debug("Activating default unit: %s", default_unit
);
672 if ((r
= manager_load_unit(m
, default_unit
, NULL
, &target
)) < 0) {
673 log_error("Failed to load default target: %s", strerror(-r
));
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
));
682 if (action
== ACTION_TEST
) {
683 printf("-> By units:\n");
684 manager_dump_units(m
, stdout
, "\t");
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
));
692 if (action
== ACTION_TEST
) {
693 printf("-> By jobs:\n");
694 manager_dump_jobs(m
, stdout
, "\t");
701 if ((r
= manager_loop(m
)) < 0) {
702 log_error("Failed to run mainloop: %s", strerror(-r
));
706 switch (m
->exit_code
) {
714 if ((r
= manager_reload(m
)) < 0)
715 log_error("Failed to reload: %s", strerror(-r
));
718 case MANAGER_REEXECUTE
:
719 if (prepare_reexecute(m
, &serialization
, &fds
) < 0)
723 log_debug("Reexecuting.");
727 assert_not_reached("Unknown exit code.");
740 const char *args
[11];
744 assert(serialization
);
747 args
[i
++] = SYSTEMD_BINARY_PATH
;
749 args
[i
++] = "--log-level";
750 args
[i
++] = log_level_to_string(log_get_max_level());
752 args
[i
++] = "--log-target";
753 args
[i
++] = log_target_to_string(log_get_target());
755 args
[i
++] = "--running-as";
756 args
[i
++] = manager_running_as_to_string(running_as
);
758 snprintf(sfd
, sizeof(sfd
), "%i", fileno(serialization
));
761 args
[i
++] = "--deserialize";
765 args
[i
++] = "--confirm-spawn";
769 assert(i
<= ELEMENTSOF(args
));
771 execv(args
[0], (char* const*) args
);
773 log_error("Failed to reexecute: %m");
777 fclose(serialization
);