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 = true;
53 _noreturn static void freeze(void) {
58 _noreturn static void crash(int sig) {
61 log_error("Caught <%s>, not dumping core.", strsignal(sig));
65 if ((pid = fork()) < 0)
66 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
72 /* Enable default signal handler for core dump */
74 sa.sa_handler = SIG_DFL;
75 assert_se(sigaction(sig, &sa, NULL) == 0);
77 /* Don't limit the core dump size */
79 rl.rlim_cur = RLIM_INFINITY;
80 rl.rlim_max = RLIM_INFINITY;
81 setrlimit(RLIMIT_CORE, &rl);
83 /* Just to be sure... */
84 assert_se(chdir("/") == 0);
86 /* Raise the signal again */
89 assert_not_reached("We shouldn't be here...");
95 /* Order things nicely. */
96 if ((r = waitpid(pid, &status, 0)) < 0)
97 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
98 else if (!WCOREDUMP(status))
99 log_error("Caught <%s>, core dump failed.", strsignal(sig));
101 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid);
106 log_info("Executing crash shell in 10s...");
109 execl("/bin/sh", "/bin/sh", NULL);
110 log_error("execl() failed: %s", strerror(errno));
113 log_info("Freezing execution.");
117 static void install_crash_handler(void) {
122 sa.sa_handler = crash;
123 sa.sa_flags = SA_NODEFER;
125 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
126 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
129 static int set_default_unit(const char *u) {
134 if (!(c = strdup(u)))
142 static int parse_proc_cmdline_word(const char *word) {
144 static const char * const rlmap[] = {
145 "single", SPECIAL_RUNLEVEL1_TARGET,
146 "-s", SPECIAL_RUNLEVEL1_TARGET,
147 "s", SPECIAL_RUNLEVEL1_TARGET,
148 "S", SPECIAL_RUNLEVEL1_TARGET,
149 "1", SPECIAL_RUNLEVEL1_TARGET,
150 "2", SPECIAL_RUNLEVEL2_TARGET,
151 "3", SPECIAL_RUNLEVEL3_TARGET,
152 "4", SPECIAL_RUNLEVEL4_TARGET,
153 "5", SPECIAL_RUNLEVEL5_TARGET
156 if (startswith(word, "systemd.default="))
157 return set_default_unit(word + 16);
159 else if (startswith(word, "systemd.log_target=")) {
161 if (log_set_target_from_string(word + 19) < 0)
162 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
164 } else if (startswith(word, "systemd.log_level=")) {
166 if (log_set_max_level_from_string(word + 18) < 0)
167 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
169 } else if (startswith(word, "systemd.dump_core=")) {
172 if ((r = parse_boolean(word + 18)) < 0)
173 log_warning("Failed to parse dump core switch %s, Ignoring", word + 18);
177 } else if (startswith(word, "systemd.crash_shell=")) {
180 if ((r = parse_boolean(word + 20)) < 0)
181 log_warning("Failed to parse crash shell switch %s, Ignoring", word + 20);
185 } else if (startswith(word, "systemd.")) {
187 log_warning("Unknown kernel switch %s. Ignoring.", word);
189 log_info("Supported kernel switches:");
190 log_info("systemd.default=UNIT Default unit to start");
191 log_info("systemd.log_target=console|kmsg|syslog Log target");
192 log_info("systemd.log_level=LEVEL Log level");
193 log_info("systemd.dump_core=0|1 Dump core on crash");
194 log_info("systemd.crash_shell=0|1 On crash run shell");
199 /* SysV compatibility */
200 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
201 if (streq(word, rlmap[i]))
202 return set_default_unit(rlmap[i+1]);
208 static int parse_proc_cmdline(void) {
215 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
216 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
220 FOREACH_WORD_QUOTED(w, l, line, state) {
223 if (!(word = strndup(w, l))) {
228 r = parse_proc_cmdline_word(word);
242 static int parse_argv(int argc, char *argv[]) {
245 ARG_LOG_LEVEL = 0x100,
250 ARG_DUMP_CONFIGURATION_ITEMS
253 static const struct option options[] = {
254 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
255 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
256 { "default", required_argument, NULL, ARG_DEFAULT },
257 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
258 { "test", no_argument, NULL, ARG_TEST },
259 { "help", no_argument, NULL, 'h' },
260 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
269 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
274 if ((r = log_set_max_level_from_string(optarg)) < 0) {
275 log_error("Failed to parse log level %s.", optarg);
283 if ((r = log_set_target_from_string(optarg)) < 0) {
284 log_error("Failed to parse log target %s.", optarg);
292 if ((r = set_default_unit(optarg)) < 0) {
293 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
299 case ARG_RUNNING_AS: {
302 if ((as = manager_running_as_from_string(optarg)) < 0) {
303 log_error("Failed to parse running as value %s", optarg);
312 action = ACTION_TEST;
315 case ARG_DUMP_CONFIGURATION_ITEMS:
316 action = ACTION_DUMP_CONFIGURATION_ITEMS;
320 action = ACTION_HELP;
327 log_error("Unknown option code %c", c);
334 static int help(void) {
336 printf("%s [options]\n\n"
337 " -h --help Show this help\n"
338 " --default=UNIT Set default unit\n"
339 " --log-level=LEVEL Set log level\n"
340 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
341 " --running-as=AS Set running as (init, system, session)\n"
342 " --test Determine startup sequence, dump it and exit\n"
343 " --dump-configuration-items Dump understood unit configuration items\n",
349 int main(int argc, char *argv[]) {
356 running_as = MANAGER_INIT;
357 else if (getuid() == 0)
358 running_as = MANAGER_SYSTEM;
360 running_as = MANAGER_SESSION;
362 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
365 /* Mount /proc, /sys and friends, so that /proc/cmdline and
366 * /proc/$PID/fd is available. */
367 if (mount_setup() < 0)
370 /* Reset all signal handlers. */
371 assert_se(reset_all_signal_handlers() == 0);
373 /* Close all open files */
374 assert_se(close_all_fds(NULL, 0) == 0);
376 if (running_as != MANAGER_SESSION)
377 if (parse_proc_cmdline() < 0)
380 log_parse_environment();
382 if (parse_argv(argc, argv) < 0)
385 if (action == ACTION_HELP) {
388 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
389 unit_dump_config_items(stdout);
394 assert_se(action == ACTION_RUN || action == ACTION_TEST);
396 /* Set up PATH unless it is already set */
398 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
399 running_as == MANAGER_INIT);
401 /* Move out of the way, so that we won't block unmounts */
402 assert_se(chdir("/") == 0);
404 /* Become a session leader if we aren't one yet. */
407 /* Disable the umask logic */
410 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
411 dbus_connection_set_change_sigpipe(FALSE);
413 /* Open the logging devices, if possible and necessary */
417 /* Make sure we leave a core dump */
419 install_crash_handler();
421 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
423 if (running_as == MANAGER_INIT)
426 if ((r = manager_new(running_as, &m)) < 0) {
427 log_error("Failed to allocate manager object: %s", strerror(-r));
431 if ((r = manager_coldplug(m)) < 0) {
432 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
436 log_debug("Activating default unit: %s", default_unit);
438 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
439 log_error("Failed to load default target: %s", strerror(-r));
441 log_info("Trying to load rescue target...");
442 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
443 log_error("Failed to load rescue target: %s", strerror(-r));
448 if (action == ACTION_TEST) {
449 printf("→ By units:\n");
450 manager_dump_units(m, stdout, "\t");
453 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
454 log_error("Failed to start default target: %s", strerror(-r));
458 if (action == ACTION_TEST) {
459 printf("→ By jobs:\n");
460 manager_dump_jobs(m, stdout, "\t");
469 if ((r = manager_loop(m)) < 0) {
470 log_error("Failed to run mainloop: %s", strerror(-r));