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>
35 #include "mount-setup.h"
36 #include "hostname-setup.h"
37 #include "load-fragment.h"
43 ACTION_DUMP_CONFIGURATION_ITEMS
44 } action = ACTION_RUN;
46 static char *default_unit = NULL;
47 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
48 static bool dump_core = true;
50 _noreturn static void freeze(void) {
55 _noreturn static void crash(int sig) {
58 log_error("Caught <%s>, not dumping core.", strsignal(sig));
62 log_warning("Caugh <%s>, dumping core.", strsignal(sig));
64 if ((pid = fork()) < 0)
65 log_error("Caught <%s>, cannot dump core: %s", strsignal(sig), strerror(errno));
71 /* Enable default signal handler for core dump */
73 sa.sa_handler = SIG_DFL;
74 assert_se(sigaction(sig, &sa, NULL) == 0);
76 /* Don't limit the core dump size */
78 rl.rlim_cur = RLIM_INFINITY;
79 rl.rlim_max = RLIM_INFINITY;
80 setrlimit(RLIMIT_CORE, &rl);
82 /* Just to be sure... */
83 assert_se(chdir("/") == 0);
85 /* Raise the signal again */
88 assert_not_reached("We shouldn't be here...");
93 log_error("Freezing execution.");
97 static void install_crash_handler(void) {
102 sa.sa_handler = crash;
103 sa.sa_flags = SA_NODEFER;
105 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
106 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
109 static int set_default_unit(const char *u) {
114 if (!(c = strdup(u)))
122 static int parse_proc_cmdline_word(const char *word) {
124 static const char * const rlmap[] = {
125 "single", SPECIAL_RUNLEVEL1_TARGET,
126 "-s", SPECIAL_RUNLEVEL1_TARGET,
127 "s", SPECIAL_RUNLEVEL1_TARGET,
128 "S", SPECIAL_RUNLEVEL1_TARGET,
129 "1", SPECIAL_RUNLEVEL1_TARGET,
130 "2", SPECIAL_RUNLEVEL2_TARGET,
131 "3", SPECIAL_RUNLEVEL3_TARGET,
132 "4", SPECIAL_RUNLEVEL4_TARGET,
133 "5", SPECIAL_RUNLEVEL5_TARGET
136 if (startswith(word, "systemd.default="))
137 return set_default_unit(word + 16);
139 else if (startswith(word, "systemd.log_target=")) {
141 if (log_set_target_from_string(word + 19) < 0)
142 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
144 } else if (startswith(word, "systemd.log_level=")) {
146 if (log_set_max_level_from_string(word + 18) < 0)
147 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
152 /* SysV compatibility */
154 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
155 if (streq(word, rlmap[i]))
156 return set_default_unit(rlmap[i+1]);
162 static int parse_proc_cmdline(void) {
169 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
170 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
174 FOREACH_WORD_QUOTED(w, l, line, state) {
177 if (!(word = strndup(w, l))) {
182 r = parse_proc_cmdline_word(word);
196 static int parse_argv(int argc, char *argv[]) {
199 ARG_LOG_LEVEL = 0x100,
204 ARG_DUMP_CONFIGURATION_ITEMS
207 static const struct option options[] = {
208 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
209 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
210 { "default", required_argument, NULL, ARG_DEFAULT },
211 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
212 { "test", no_argument, NULL, ARG_TEST },
213 { "help", no_argument, NULL, 'h' },
214 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
223 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
228 if ((r = log_set_max_level_from_string(optarg)) < 0) {
229 log_error("Failed to parse log level %s.", optarg);
237 if ((r = log_set_target_from_string(optarg)) < 0) {
238 log_error("Failed to parse log target %s.", optarg);
246 if ((r = set_default_unit(optarg)) < 0) {
247 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
253 case ARG_RUNNING_AS: {
256 if ((as = manager_running_as_from_string(optarg)) < 0) {
257 log_error("Failed to parse running as value %s", optarg);
266 action = ACTION_TEST;
269 case ARG_DUMP_CONFIGURATION_ITEMS:
270 action = ACTION_DUMP_CONFIGURATION_ITEMS;
274 action = ACTION_HELP;
281 log_error("Unknown option code %c", c);
288 static int help(void) {
290 printf("%s [options]\n\n"
291 " -h --help Show this help\n"
292 " --default=UNIT Set default unit\n"
293 " --log-level=LEVEL Set log level\n"
294 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
295 " --running-as=AS Set running as (init, system, session)\n"
296 " --test Determine startup sequence, dump it and exit\n"
297 " --dump-configuration-items Dump understood unit configuration items\n",
303 int main(int argc, char *argv[]) {
310 running_as = MANAGER_INIT;
311 else if (getuid() == 0)
312 running_as = MANAGER_SYSTEM;
314 running_as = MANAGER_SESSION;
316 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
319 /* Mount /proc, /sys and friends, so that /proc/cmdline and
320 * /proc/$PID/fd is available. */
321 if (mount_setup() < 0)
324 /* Reset all signal handlers. */
325 assert_se(reset_all_signal_handlers() == 0);
327 /* Close all open files */
328 assert_se(close_all_fds(NULL, 0) == 0);
330 if (running_as != MANAGER_SESSION)
331 if (parse_proc_cmdline() < 0)
334 log_parse_environment();
336 if (parse_argv(argc, argv) < 0)
339 if (action == ACTION_HELP) {
342 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
343 unit_dump_config_items(stdout);
348 assert_se(action == ACTION_RUN || action == ACTION_TEST);
350 /* Set up PATH unless it is already set */
352 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
353 running_as == MANAGER_INIT);
355 /* Move out of the way, so that we won't block unmounts */
356 assert_se(chdir("/") == 0);
358 /* Become a session leader if we aren't one yet. */
361 /* Disable the umask logic */
364 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
365 dbus_connection_set_change_sigpipe(FALSE);
367 /* Open the logging devices, if possible and necessary*/
371 install_crash_handler();
374 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
376 if (running_as == MANAGER_INIT)
379 if ((r = manager_new(running_as, &m)) < 0) {
380 log_error("Failed to allocate manager object: %s", strerror(-r));
384 if ((r = manager_coldplug(m)) < 0) {
385 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
389 log_debug("Activating default unit: %s", default_unit);
391 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
392 log_error("Failed to load default target: %s", strerror(-r));
394 log_info("Trying to load rescue target...");
395 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
396 log_error("Failed to load rescue target: %s", strerror(-r));
401 if (action == ACTION_TEST) {
402 printf("→ By units:\n");
403 manager_dump_units(m, stdout, "\t");
406 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
407 log_error("Failed to start default target: %s", strerror(-r));
411 if (action == ACTION_TEST) {
412 printf("→ By jobs:\n");
413 manager_dump_jobs(m, stdout, "\t");
422 if ((r = manager_loop(m)) < 0) {
423 log_error("Failed to run mainloop: %s", strerror(-r));