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 "load-fragment.h"
45 ACTION_DUMP_CONFIGURATION_ITEMS
46 } action = ACTION_RUN;
48 static char *default_unit = NULL;
49 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
51 static bool dump_core = true;
52 static bool crash_shell = false;
53 static int crash_chvt = -1;
55 static bool confirm_spawn = false;
57 _noreturn static void freeze(void) {
62 _noreturn static void crash(int sig) {
65 log_error("Caught <%s>, not dumping core.", strsignal(sig));
69 if ((pid = fork()) < 0)
70 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
76 /* Enable default signal handler for core dump */
78 sa.sa_handler = SIG_DFL;
79 assert_se(sigaction(sig, &sa, NULL) == 0);
81 /* Don't limit the core dump size */
83 rl.rlim_cur = RLIM_INFINITY;
84 rl.rlim_max = RLIM_INFINITY;
85 setrlimit(RLIMIT_CORE, &rl);
87 /* Just to be sure... */
88 assert_se(chdir("/") == 0);
90 /* Raise the signal again */
93 assert_not_reached("We shouldn't be here...");
99 /* Order things nicely. */
100 if ((r = waitpid(pid, &status, 0)) < 0)
101 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
102 else if (!WCOREDUMP(status))
103 log_error("Caught <%s>, core dump failed.", strsignal(sig));
105 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid);
115 log_info("Executing crash shell in 10s...");
118 /* Make sure the signal is not delivered inside the
120 assert_se(sigemptyset(&mask) == 0);
121 assert_se(sigaddset(&mask, sig) == 0);
122 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
126 execl("/bin/sh", "/bin/sh", NULL);
127 log_error("execl() failed: %s", strerror(errno));
130 log_info("Freezing execution.");
134 static void install_crash_handler(void) {
139 sa.sa_handler = crash;
140 sa.sa_flags = SA_NODEFER;
142 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
143 assert_se(sigaction(SIGILL, &sa, NULL) == 0);
144 assert_se(sigaction(SIGFPE, &sa, NULL) == 0);
145 assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
146 assert_se(sigaction(SIGQUIT, &sa, NULL) == 0);
147 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
150 static int console_setup(void) {
151 int tty_fd = -1, null_fd = -1, r = 0;
153 /* If we are init, we connect stdout/stderr to /dev/console
154 * and stdin to /dev/null and make sure we don't have a
155 * controlling tty. */
159 if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) {
160 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
165 if ((null_fd = open("/dev/null", O_RDONLY)) < 0) {
166 log_error("Failed to open /dev/null: %m");
172 assert(null_fd >= 3);
174 if (reset_terminal(tty_fd) < 0)
175 log_error("Failed to reset /dev/console: %m");
177 if (dup2(tty_fd, STDOUT_FILENO) < 0 ||
178 dup2(tty_fd, STDERR_FILENO) < 0 ||
179 dup2(null_fd, STDIN_FILENO) < 0) {
180 log_error("Failed to dup2() device: %m");
189 close_nointr(tty_fd);
192 close_nointr(null_fd);
197 static int set_default_unit(const char *u) {
202 if (!(c = strdup(u)))
210 static int parse_proc_cmdline_word(const char *word) {
212 static const char * const rlmap[] = {
213 "single", SPECIAL_RUNLEVEL1_TARGET,
214 "-s", SPECIAL_RUNLEVEL1_TARGET,
215 "s", SPECIAL_RUNLEVEL1_TARGET,
216 "S", SPECIAL_RUNLEVEL1_TARGET,
217 "1", SPECIAL_RUNLEVEL1_TARGET,
218 "2", SPECIAL_RUNLEVEL2_TARGET,
219 "3", SPECIAL_RUNLEVEL3_TARGET,
220 "4", SPECIAL_RUNLEVEL4_TARGET,
221 "5", SPECIAL_RUNLEVEL5_TARGET
224 if (startswith(word, "systemd.default="))
225 return set_default_unit(word + 16);
227 else if (startswith(word, "systemd.log_target=")) {
229 if (log_set_target_from_string(word + 19) < 0)
230 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
232 } else if (startswith(word, "systemd.log_level=")) {
234 if (log_set_max_level_from_string(word + 18) < 0)
235 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
237 } else if (startswith(word, "systemd.dump_core=")) {
240 if ((r = parse_boolean(word + 18)) < 0)
241 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
245 } else if (startswith(word, "systemd.crash_shell=")) {
248 if ((r = parse_boolean(word + 20)) < 0)
249 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
254 } else if (startswith(word, "systemd.confirm_spawn=")) {
257 if ((r = parse_boolean(word + 22)) < 0)
258 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
262 } else if (startswith(word, "systemd.crash_chvt=")) {
265 if (safe_atoi(word + 19, &k) < 0)
266 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
270 } else if (startswith(word, "systemd.")) {
272 log_warning("Unknown kernel switch %s. Ignoring.", word);
274 log_info("Supported kernel switches:");
275 log_info("systemd.default=UNIT Default unit to start");
276 log_info("systemd.log_target=console|kmsg|syslog Log target");
277 log_info("systemd.log_level=LEVEL Log level");
278 log_info("systemd.dump_core=0|1 Dump core on crash");
279 log_info("systemd.crash_shell=0|1 On crash run shell");
280 log_info("systemd.crash_chvt=N Change to VT #N on crash");
281 log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
286 /* SysV compatibility */
287 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
288 if (streq(word, rlmap[i]))
289 return set_default_unit(rlmap[i+1]);
295 static int parse_proc_cmdline(void) {
302 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
303 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
307 FOREACH_WORD_QUOTED(w, l, line, state) {
310 if (!(word = strndup(w, l))) {
315 r = parse_proc_cmdline_word(word);
329 static int parse_argv(int argc, char *argv[]) {
332 ARG_LOG_LEVEL = 0x100,
337 ARG_DUMP_CONFIGURATION_ITEMS,
341 static const struct option options[] = {
342 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
343 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
344 { "default", required_argument, NULL, ARG_DEFAULT },
345 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
346 { "test", no_argument, NULL, ARG_TEST },
347 { "help", no_argument, NULL, 'h' },
348 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
349 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
358 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
363 if ((r = log_set_max_level_from_string(optarg)) < 0) {
364 log_error("Failed to parse log level %s.", optarg);
372 if ((r = log_set_target_from_string(optarg)) < 0) {
373 log_error("Failed to parse log target %s.", optarg);
381 if ((r = set_default_unit(optarg)) < 0) {
382 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
388 case ARG_RUNNING_AS: {
391 if ((as = manager_running_as_from_string(optarg)) < 0) {
392 log_error("Failed to parse running as value %s", optarg);
401 action = ACTION_TEST;
404 case ARG_DUMP_CONFIGURATION_ITEMS:
405 action = ACTION_DUMP_CONFIGURATION_ITEMS;
408 case ARG_CONFIRM_SPAWN:
409 confirm_spawn = true;
413 action = ACTION_HELP;
420 log_error("Unknown option code %c", c);
427 static int help(void) {
429 printf("%s [options]\n\n"
430 " -h --help Show this help\n"
431 " --default=UNIT Set default unit\n"
432 " --log-level=LEVEL Set log level\n"
433 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
434 " --running-as=AS Set running as (init, system, session)\n"
435 " --test Determine startup sequence, dump it and exit\n"
436 " --dump-configuration-items Dump understood unit configuration items\n"
437 " --confirm-spawn Ask for confirmation when spawning processes\n",
443 int main(int argc, char *argv[]) {
450 running_as = MANAGER_INIT;
451 else if (getuid() == 0)
452 running_as = MANAGER_SYSTEM;
454 running_as = MANAGER_SESSION;
456 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
459 /* Mount /proc, /sys and friends, so that /proc/cmdline and
460 * /proc/$PID/fd is available. */
461 if (mount_setup() < 0)
464 /* Reset all signal handlers. */
465 assert_se(reset_all_signal_handlers() == 0);
467 /* If we are init, we can block sigkill. Yay. */
468 ignore_signal(SIGKILL);
469 ignore_signal(SIGPIPE);
471 /* Close all open files */
472 assert_se(close_all_fds(NULL, 0) == 0);
474 if (running_as != MANAGER_SESSION)
475 if (parse_proc_cmdline() < 0)
478 log_parse_environment();
480 if (parse_argv(argc, argv) < 0)
483 if (action == ACTION_HELP) {
486 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
487 unit_dump_config_items(stdout);
492 assert_se(action == ACTION_RUN || action == ACTION_TEST);
494 /* Set up PATH unless it is already set */
496 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
497 running_as == MANAGER_INIT);
499 /* Move out of the way, so that we won't block unmounts */
500 assert_se(chdir("/") == 0);
502 if (running_as != MANAGER_SESSION) {
503 /* Become a session leader if we aren't one yet. */
506 /* Disable the umask logic */
510 if (running_as == MANAGER_INIT)
513 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
514 dbus_connection_set_change_sigpipe(FALSE);
516 /* Open the logging devices, if possible and necessary */
520 /* Make sure we leave a core dump without panicing the
523 install_crash_handler();
525 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
527 if (running_as == MANAGER_INIT)
530 if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) {
531 log_error("Failed to allocate manager object: %s", strerror(-r));
535 if ((r = manager_coldplug(m)) < 0) {
536 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
540 log_debug("Activating default unit: %s", default_unit);
542 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
543 log_error("Failed to load default target: %s", strerror(-r));
545 log_info("Trying to load rescue target...");
546 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
547 log_error("Failed to load rescue target: %s", strerror(-r));
552 if (action == ACTION_TEST) {
553 printf("→ By units:\n");
554 manager_dump_units(m, stdout, "\t");
557 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
558 log_error("Failed to start default target: %s", strerror(-r));
562 if (action == ACTION_TEST) {
563 printf("→ By jobs:\n");
564 manager_dump_jobs(m, stdout, "\t");
573 if ((r = manager_loop(m)) < 0) {
574 log_error("Failed to run mainloop: %s", strerror(-r));