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"
46 ACTION_DUMP_CONFIGURATION_ITEMS
47 } action
= ACTION_RUN
;
49 static char *default_unit
= NULL
;
50 static ManagerRunningAs running_as
= _MANAGER_RUNNING_AS_INVALID
;
52 static bool dump_core
= true;
53 static bool crash_shell
= false;
54 static int crash_chvt
= -1;
55 static bool confirm_spawn
= false;
56 static FILE* serialization
= NULL
;
58 _noreturn
static void freeze(void) {
63 static void nop_handler(int sig
) {
66 _noreturn
static void crash(int sig
) {
69 log_error("Caught <%s>, not dumping core.", strsignal(sig
));
74 /* We want to wait for the core process, hence let's enable SIGCHLD */
76 sa
.sa_handler
= nop_handler
;
77 sa
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
78 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
80 if ((pid
= fork()) < 0)
81 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig
), strerror(errno
));
86 /* Enable default signal handler for core dump */
88 sa
.sa_handler
= SIG_DFL
;
89 assert_se(sigaction(sig
, &sa
, NULL
) == 0);
91 /* Don't limit the core dump size */
93 rl
.rlim_cur
= RLIM_INFINITY
;
94 rl
.rlim_max
= RLIM_INFINITY
;
95 setrlimit(RLIMIT_CORE
, &rl
);
97 /* Just to be sure... */
98 assert_se(chdir("/") == 0);
100 /* Raise the signal again */
103 assert_not_reached("We shouldn't be here...");
109 /* Order things nicely. */
110 if ((r
= waitpid(pid
, &status
, 0)) < 0)
111 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig
), strerror(errno
));
112 else if (!WCOREDUMP(status
))
113 log_error("Caught <%s>, core dump failed.", strsignal(sig
));
115 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig
), (unsigned long long) pid
);
126 log_info("Executing crash shell in 10s...");
129 /* Let the kernel reap children for us */
131 sa
.sa_handler
= SIG_IGN
;
132 sa
.sa_flags
= SA_NOCLDSTOP
|SA_NOCLDWAIT
|SA_RESTART
;
133 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
135 if ((pid
= fork()) < 0)
136 log_error("Failed to fork off crash shell: %s", strerror(errno
));
138 execl("/bin/sh", "/bin/sh", NULL
);
140 log_error("execl() failed: %s", strerror(errno
));
144 log_info("Successfully spawned crash shall as pid %llu.", (unsigned long long) pid
);
147 log_info("Freezing execution.");
151 static void install_crash_handler(void) {
156 sa
.sa_handler
= crash
;
157 sa
.sa_flags
= SA_NODEFER
;
159 assert_se(sigaction(SIGSEGV
, &sa
, NULL
) == 0);
160 assert_se(sigaction(SIGILL
, &sa
, NULL
) == 0);
161 assert_se(sigaction(SIGFPE
, &sa
, NULL
) == 0);
162 assert_se(sigaction(SIGBUS
, &sa
, NULL
) == 0);
163 assert_se(sigaction(SIGQUIT
, &sa
, NULL
) == 0);
164 assert_se(sigaction(SIGABRT
, &sa
, NULL
) == 0);
167 static int console_setup(void) {
168 int tty_fd
= -1, null_fd
= -1, r
= 0;
170 /* If we are init, we connect stdout/stderr to /dev/console
171 * and stdin to /dev/null and make sure we don't have a
172 * controlling tty. */
176 if ((tty_fd
= open_terminal("/dev/console", O_WRONLY
)) < 0) {
177 log_error("Failed to open /dev/console: %s", strerror(-tty_fd
));
182 if ((null_fd
= open("/dev/null", O_RDONLY
)) < 0) {
183 log_error("Failed to open /dev/null: %m");
189 assert(null_fd
>= 3);
191 if (reset_terminal(tty_fd
) < 0)
192 log_error("Failed to reset /dev/console: %m");
194 if (dup2(tty_fd
, STDOUT_FILENO
) < 0 ||
195 dup2(tty_fd
, STDERR_FILENO
) < 0 ||
196 dup2(null_fd
, STDIN_FILENO
) < 0) {
197 log_error("Failed to dup2() device: %m");
206 close_nointr_nofail(tty_fd
);
209 close_nointr_nofail(null_fd
);
214 static int set_default_unit(const char *u
) {
219 if (!(c
= strdup(u
)))
227 static int parse_proc_cmdline_word(const char *word
) {
229 static const char * const rlmap
[] = {
230 "single", SPECIAL_RUNLEVEL1_TARGET
,
231 "-s", SPECIAL_RUNLEVEL1_TARGET
,
232 "s", SPECIAL_RUNLEVEL1_TARGET
,
233 "S", SPECIAL_RUNLEVEL1_TARGET
,
234 "1", SPECIAL_RUNLEVEL1_TARGET
,
235 "2", SPECIAL_RUNLEVEL2_TARGET
,
236 "3", SPECIAL_RUNLEVEL3_TARGET
,
237 "4", SPECIAL_RUNLEVEL4_TARGET
,
238 "5", SPECIAL_RUNLEVEL5_TARGET
241 if (startswith(word
, "systemd.default="))
242 return set_default_unit(word
+ 16);
244 else if (startswith(word
, "systemd.log_target=")) {
246 if (log_set_target_from_string(word
+ 19) < 0)
247 log_warning("Failed to parse log target %s. Ignoring.", word
+ 19);
249 } else if (startswith(word
, "systemd.log_level=")) {
251 if (log_set_max_level_from_string(word
+ 18) < 0)
252 log_warning("Failed to parse log level %s. Ignoring.", word
+ 18);
254 } else if (startswith(word
, "systemd.dump_core=")) {
257 if ((r
= parse_boolean(word
+ 18)) < 0)
258 log_warning("Failed to parse dump core switch %s, Ignoring.", word
+ 18);
262 } else if (startswith(word
, "systemd.crash_shell=")) {
265 if ((r
= parse_boolean(word
+ 20)) < 0)
266 log_warning("Failed to parse crash shell switch %s, Ignoring.", word
+ 20);
271 } else if (startswith(word
, "systemd.confirm_spawn=")) {
274 if ((r
= parse_boolean(word
+ 22)) < 0)
275 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word
+ 22);
279 } else if (startswith(word
, "systemd.crash_chvt=")) {
282 if (safe_atoi(word
+ 19, &k
) < 0)
283 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word
+ 19);
287 } else if (startswith(word
, "systemd.")) {
289 log_warning("Unknown kernel switch %s. Ignoring.", word
);
291 log_info("Supported kernel switches:");
292 log_info("systemd.default=UNIT Default unit to start");
293 log_info("systemd.log_target=console|kmsg|syslog Log target");
294 log_info("systemd.log_level=LEVEL Log level");
295 log_info("systemd.dump_core=0|1 Dump core on crash");
296 log_info("systemd.crash_shell=0|1 On crash run shell");
297 log_info("systemd.crash_chvt=N Change to VT #N on crash");
298 log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
303 /* SysV compatibility */
304 for (i
= 0; i
< ELEMENTSOF(rlmap
); i
+= 2)
305 if (streq(word
, rlmap
[i
]))
306 return set_default_unit(rlmap
[i
+1]);
312 static int parse_proc_cmdline(void) {
319 if ((r
= read_one_line_file("/proc/cmdline", &line
)) < 0) {
320 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno
));
324 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
327 if (!(word
= strndup(w
, l
))) {
332 r
= parse_proc_cmdline_word(word
);
346 static int parse_argv(int argc
, char *argv
[]) {
349 ARG_LOG_LEVEL
= 0x100,
354 ARG_DUMP_CONFIGURATION_ITEMS
,
359 static const struct option options
[] = {
360 { "log-level", required_argument
, NULL
, ARG_LOG_LEVEL
},
361 { "log-target", required_argument
, NULL
, ARG_LOG_TARGET
},
362 { "default", required_argument
, NULL
, ARG_DEFAULT
},
363 { "running-as", required_argument
, NULL
, ARG_RUNNING_AS
},
364 { "test", no_argument
, NULL
, ARG_TEST
},
365 { "help", no_argument
, NULL
, 'h' },
366 { "dump-configuration-items", no_argument
, NULL
, ARG_DUMP_CONFIGURATION_ITEMS
},
367 { "confirm-spawn", no_argument
, NULL
, ARG_CONFIRM_SPAWN
},
368 { "deserialize", required_argument
, NULL
, ARG_DESERIALIZE
},
377 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
382 if ((r
= log_set_max_level_from_string(optarg
)) < 0) {
383 log_error("Failed to parse log level %s.", optarg
);
391 if ((r
= log_set_target_from_string(optarg
)) < 0) {
392 log_error("Failed to parse log target %s.", optarg
);
400 if ((r
= set_default_unit(optarg
)) < 0) {
401 log_error("Failed to set default unit %s: %s", optarg
, strerror(-r
));
407 case ARG_RUNNING_AS
: {
410 if ((as
= manager_running_as_from_string(optarg
)) < 0) {
411 log_error("Failed to parse running as value %s", optarg
);
420 action
= ACTION_TEST
;
423 case ARG_DUMP_CONFIGURATION_ITEMS
:
424 action
= ACTION_DUMP_CONFIGURATION_ITEMS
;
427 case ARG_CONFIRM_SPAWN
:
428 confirm_spawn
= true;
431 case ARG_DESERIALIZE
: {
435 if ((r
= safe_atoi(optarg
, &fd
)) < 0 || fd
< 0) {
436 log_error("Failed to parse deserialize option %s.", optarg
);
440 if (!(f
= fdopen(fd
, "r"))) {
441 log_error("Failed to open serialization fd: %m");
446 fclose(serialization
);
454 action
= ACTION_HELP
;
461 log_error("Unknown option code %c", c
);
468 static int help(void) {
470 printf("%s [options]\n\n"
471 " -h --help Show this help\n"
472 " --default=UNIT Set default unit\n"
473 " --log-level=LEVEL Set log level\n"
474 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
475 " --running-as=AS Set running as (init, system, session)\n"
476 " --test Determine startup sequence, dump it and exit\n"
477 " --dump-configuration-items Dump understood unit configuration items\n"
478 " --confirm-spawn Ask for confirmation when spawning processes\n",
484 static int prepare_reexecute(Manager
*m
, FILE **_f
, FDSet
**_fds
) {
493 if ((r
= manager_open_serialization(&f
)) < 0) {
494 log_error("Failed to create serialization faile: %s", strerror(-r
));
498 if (!(fds
= fdset_new())) {
500 log_error("Failed to allocate fd set: %s", strerror(-r
));
504 if ((r
= manager_serialize(m
, f
, fds
)) < 0) {
505 log_error("Failed to serialize state: %s", strerror(-r
));
509 if (fseeko(f
, 0, SEEK_SET
) < 0) {
510 log_error("Failed to rewind serialization fd: %m");
514 if ((r
= fd_cloexec(fileno(f
), false)) < 0) {
515 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r
));
519 if ((r
= fdset_cloexec(fds
, false)) < 0) {
520 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r
));
538 int main(int argc
, char *argv
[]) {
544 bool reexecute
= false;
547 running_as
= MANAGER_INIT
;
548 else if (getuid() == 0)
549 running_as
= MANAGER_SYSTEM
;
551 running_as
= MANAGER_SESSION
;
553 if (set_default_unit(SPECIAL_DEFAULT_TARGET
) < 0)
556 /* Mount /proc, /sys and friends, so that /proc/cmdline and
557 * /proc/$PID/fd is available. */
558 if (mount_setup() < 0)
561 /* Reset all signal handlers. */
562 assert_se(reset_all_signal_handlers() == 0);
564 /* If we are init, we can block sigkill. Yay. */
565 ignore_signal(SIGKILL
);
566 ignore_signal(SIGPIPE
);
568 if (running_as
!= MANAGER_SESSION
)
569 if (parse_proc_cmdline() < 0)
572 log_parse_environment();
574 if (parse_argv(argc
, argv
) < 0)
577 if (action
== ACTION_HELP
) {
580 } else if (action
== ACTION_DUMP_CONFIGURATION_ITEMS
) {
581 unit_dump_config_items(stdout
);
586 assert_se(action
== ACTION_RUN
|| action
== ACTION_TEST
);
588 /* Remember open file descriptors for later deserialization */
590 if ((r
= fdset_new_fill(&fds
)) < 0) {
591 log_error("Failed to allocate fd set: %s", strerror(-r
));
595 assert_se(fdset_remove(fds
, fileno(serialization
)) >= 0);
597 close_all_fds(NULL
, 0);
599 /* Set up PATH unless it is already set */
601 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
602 running_as
== MANAGER_INIT
);
604 /* Move out of the way, so that we won't block unmounts */
605 assert_se(chdir("/") == 0);
607 if (running_as
!= MANAGER_SESSION
) {
608 /* Become a session leader if we aren't one yet. */
611 /* Disable the umask logic */
615 if (running_as
== MANAGER_INIT
)
618 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
619 dbus_connection_set_change_sigpipe(FALSE
);
621 /* Open the logging devices, if possible and necessary */
625 /* Make sure we leave a core dump without panicing the
628 install_crash_handler();
630 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as
));
632 if (running_as
== MANAGER_INIT
)
635 if ((r
= manager_new(running_as
, confirm_spawn
, &m
)) < 0) {
636 log_error("Failed to allocate manager object: %s", strerror(-r
));
640 if ((r
= manager_startup(m
, serialization
, fds
)) < 0)
641 log_error("Failed to fully startup daemon: %s", strerror(-r
));
644 /* This will close all file descriptors that were opened, but
645 * not claimed by any unit. */
652 fclose(serialization
);
653 serialization
= NULL
;
655 log_debug("Activating default unit: %s", default_unit
);
657 if ((r
= manager_load_unit(m
, default_unit
, NULL
, &target
)) < 0) {
658 log_error("Failed to load default target: %s", strerror(-r
));
660 log_info("Trying to load rescue target...");
661 if ((r
= manager_load_unit(m
, SPECIAL_RESCUE_TARGET
, NULL
, &target
)) < 0) {
662 log_error("Failed to load rescue target: %s", strerror(-r
));
667 if (action
== ACTION_TEST
) {
668 printf("→ By units:\n");
669 manager_dump_units(m
, stdout
, "\t");
672 if ((r
= manager_add_job(m
, JOB_START
, target
, JOB_REPLACE
, false, &job
)) < 0) {
673 log_error("Failed to start default target: %s", strerror(-r
));
677 if (action
== ACTION_TEST
) {
678 printf("→ By jobs:\n");
679 manager_dump_jobs(m
, stdout
, "\t");
686 if ((r
= manager_loop(m
)) < 0) {
687 log_error("Failed to run mainloop: %s", strerror(-r
));
691 switch (m
->exit_code
) {
699 if ((r
= manager_reload(m
)) < 0)
700 log_error("Failed to reload: %s", strerror(-r
));
703 case MANAGER_REEXECUTE
:
704 if (prepare_reexecute(m
, &serialization
, &fds
) < 0)
708 log_debug("Reexecuting.");
712 assert_not_reached("Unknown exit code.");
725 const char *args
[11];
729 assert(serialization
);
732 args
[i
++] = SYSTEMD_BINARY_PATH
;
734 args
[i
++] = "--log-level";
735 args
[i
++] = log_level_to_string(log_get_max_level());
737 args
[i
++] = "--log-target";
738 args
[i
++] = log_target_to_string(log_get_target());
740 args
[i
++] = "--running-as";
741 args
[i
++] = manager_running_as_to_string(running_as
);
743 snprintf(sfd
, sizeof(sfd
), "%i", fileno(serialization
));
746 args
[i
++] = "--deserialize";
750 args
[i
++] = "--confirm-spawn";
754 assert(i
<= ELEMENTSOF(args
));
756 execv(args
[0], (char* const*) args
);
758 log_error("Failed to reexecute: %m");
762 fclose(serialization
);