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>
36 #include "mount-setup.h"
37 #include "hostname-setup.h"
38 #include "load-fragment.h"
44 ACTION_DUMP_CONFIGURATION_ITEMS
45 } action = ACTION_RUN;
47 static char *default_unit = NULL;
48 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
50 static bool dump_core = true;
51 static bool crash_shell = false;
52 static int crash_chvt = -1;
54 _noreturn static void freeze(void) {
59 _noreturn static void crash(int sig) {
62 log_error("Caught <%s>, not dumping core.", strsignal(sig));
66 if ((pid = fork()) < 0)
67 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
73 /* Enable default signal handler for core dump */
75 sa.sa_handler = SIG_DFL;
76 assert_se(sigaction(sig, &sa, NULL) == 0);
78 /* Don't limit the core dump size */
80 rl.rlim_cur = RLIM_INFINITY;
81 rl.rlim_max = RLIM_INFINITY;
82 setrlimit(RLIMIT_CORE, &rl);
84 /* Just to be sure... */
85 assert_se(chdir("/") == 0);
87 /* Raise the signal again */
90 assert_not_reached("We shouldn't be here...");
96 /* Order things nicely. */
97 if ((r = waitpid(pid, &status, 0)) < 0)
98 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
99 else if (!WCOREDUMP(status))
100 log_error("Caught <%s>, core dump failed.", strsignal(sig));
102 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid);
110 log_info("Executing crash shell in 10s...");
113 execl("/bin/sh", "/bin/sh", NULL);
114 log_error("execl() failed: %s", strerror(errno));
117 log_info("Freezing execution.");
121 static void install_crash_handler(void) {
126 sa.sa_handler = crash;
127 sa.sa_flags = SA_NODEFER;
129 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
130 assert_se(sigaction(SIGILL, &sa, NULL) == 0);
131 assert_se(sigaction(SIGFPE, &sa, NULL) == 0);
132 assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
133 assert_se(sigaction(SIGQUIT, &sa, NULL) == 0);
134 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
137 static int set_default_unit(const char *u) {
142 if (!(c = strdup(u)))
150 static int parse_proc_cmdline_word(const char *word) {
152 static const char * const rlmap[] = {
153 "single", SPECIAL_RUNLEVEL1_TARGET,
154 "-s", SPECIAL_RUNLEVEL1_TARGET,
155 "s", SPECIAL_RUNLEVEL1_TARGET,
156 "S", SPECIAL_RUNLEVEL1_TARGET,
157 "1", SPECIAL_RUNLEVEL1_TARGET,
158 "2", SPECIAL_RUNLEVEL2_TARGET,
159 "3", SPECIAL_RUNLEVEL3_TARGET,
160 "4", SPECIAL_RUNLEVEL4_TARGET,
161 "5", SPECIAL_RUNLEVEL5_TARGET
164 if (startswith(word, "systemd.default="))
165 return set_default_unit(word + 16);
167 else if (startswith(word, "systemd.log_target=")) {
169 if (log_set_target_from_string(word + 19) < 0)
170 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
172 } else if (startswith(word, "systemd.log_level=")) {
174 if (log_set_max_level_from_string(word + 18) < 0)
175 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
177 } else if (startswith(word, "systemd.dump_core=")) {
180 if ((r = parse_boolean(word + 18)) < 0)
181 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
185 } else if (startswith(word, "systemd.crash_shell=")) {
188 if ((r = parse_boolean(word + 20)) < 0)
189 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
193 } else if (startswith(word, "systemd.crash_chvt=")) {
196 if (safe_atoi(word + 19, &k) < 0)
197 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
201 } else if (startswith(word, "systemd.")) {
203 log_warning("Unknown kernel switch %s. Ignoring.", word);
205 log_info("Supported kernel switches:");
206 log_info("systemd.default=UNIT Default unit to start");
207 log_info("systemd.log_target=console|kmsg|syslog Log target");
208 log_info("systemd.log_level=LEVEL Log level");
209 log_info("systemd.dump_core=0|1 Dump core on crash");
210 log_info("systemd.crash_shell=0|1 On crash run shell");
211 log_info("systemd.crash_chvt=N Change to VT #N on crash");
216 /* SysV compatibility */
217 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
218 if (streq(word, rlmap[i]))
219 return set_default_unit(rlmap[i+1]);
225 static int parse_proc_cmdline(void) {
232 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
233 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
237 FOREACH_WORD_QUOTED(w, l, line, state) {
240 if (!(word = strndup(w, l))) {
245 r = parse_proc_cmdline_word(word);
259 static int parse_argv(int argc, char *argv[]) {
262 ARG_LOG_LEVEL = 0x100,
267 ARG_DUMP_CONFIGURATION_ITEMS
270 static const struct option options[] = {
271 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
272 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
273 { "default", required_argument, NULL, ARG_DEFAULT },
274 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
275 { "test", no_argument, NULL, ARG_TEST },
276 { "help", no_argument, NULL, 'h' },
277 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
286 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
291 if ((r = log_set_max_level_from_string(optarg)) < 0) {
292 log_error("Failed to parse log level %s.", optarg);
300 if ((r = log_set_target_from_string(optarg)) < 0) {
301 log_error("Failed to parse log target %s.", optarg);
309 if ((r = set_default_unit(optarg)) < 0) {
310 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
316 case ARG_RUNNING_AS: {
319 if ((as = manager_running_as_from_string(optarg)) < 0) {
320 log_error("Failed to parse running as value %s", optarg);
329 action = ACTION_TEST;
332 case ARG_DUMP_CONFIGURATION_ITEMS:
333 action = ACTION_DUMP_CONFIGURATION_ITEMS;
337 action = ACTION_HELP;
344 log_error("Unknown option code %c", c);
351 static int help(void) {
353 printf("%s [options]\n\n"
354 " -h --help Show this help\n"
355 " --default=UNIT Set default unit\n"
356 " --log-level=LEVEL Set log level\n"
357 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
358 " --running-as=AS Set running as (init, system, session)\n"
359 " --test Determine startup sequence, dump it and exit\n"
360 " --dump-configuration-items Dump understood unit configuration items\n",
366 int main(int argc, char *argv[]) {
373 running_as = MANAGER_INIT;
374 else if (getuid() == 0)
375 running_as = MANAGER_SYSTEM;
377 running_as = MANAGER_SESSION;
379 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
382 /* Mount /proc, /sys and friends, so that /proc/cmdline and
383 * /proc/$PID/fd is available. */
384 if (mount_setup() < 0)
387 /* Reset all signal handlers. */
388 assert_se(reset_all_signal_handlers() == 0);
390 /* Close all open files */
391 assert_se(close_all_fds(NULL, 0) == 0);
393 if (running_as != MANAGER_SESSION)
394 if (parse_proc_cmdline() < 0)
397 log_parse_environment();
399 if (parse_argv(argc, argv) < 0)
402 if (action == ACTION_HELP) {
405 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
406 unit_dump_config_items(stdout);
411 assert_se(action == ACTION_RUN || action == ACTION_TEST);
413 /* Set up PATH unless it is already set */
415 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
416 running_as == MANAGER_INIT);
418 /* Move out of the way, so that we won't block unmounts */
419 assert_se(chdir("/") == 0);
421 /* Become a session leader if we aren't one yet. */
424 /* Disable the umask logic */
427 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
428 dbus_connection_set_change_sigpipe(FALSE);
430 /* Open the logging devices, if possible and necessary */
434 /* Make sure we leave a core dump without panicing the
437 install_crash_handler();
439 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
441 if (running_as == MANAGER_INIT)
444 if ((r = manager_new(running_as, &m)) < 0) {
445 log_error("Failed to allocate manager object: %s", strerror(-r));
449 if ((r = manager_coldplug(m)) < 0) {
450 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
454 log_debug("Activating default unit: %s", default_unit);
456 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
457 log_error("Failed to load default target: %s", strerror(-r));
459 log_info("Trying to load rescue target...");
460 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
461 log_error("Failed to load rescue target: %s", strerror(-r));
466 if (action == ACTION_TEST) {
467 printf("→ By units:\n");
468 manager_dump_units(m, stdout, "\t");
471 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
472 log_error("Failed to start default target: %s", strerror(-r));
476 if (action == ACTION_TEST) {
477 printf("→ By jobs:\n");
478 manager_dump_jobs(m, stdout, "\t");
487 if ((r = manager_loop(m)) < 0) {
488 log_error("Failed to run mainloop: %s", strerror(-r));