1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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>
34 #include <sys/prctl.h>
38 #include "mount-setup.h"
39 #include "hostname-setup.h"
40 #include "loopback-setup.h"
41 #include "kmod-setup.h"
42 #include "locale-setup.h"
43 #include "selinux-setup.h"
44 #include "load-fragment.h"
47 #include "conf-parser.h"
48 #include "bus-errors.h"
58 ACTION_DUMP_CONFIGURATION_ITEMS
,
60 } arg_action
= ACTION_RUN
;
62 static char *arg_default_unit
= NULL
;
63 static ManagerRunningAs arg_running_as
= _MANAGER_RUNNING_AS_INVALID
;
65 static bool arg_dump_core
= true;
66 static bool arg_crash_shell
= false;
67 static int arg_crash_chvt
= -1;
68 static bool arg_confirm_spawn
= false;
69 static bool arg_show_status
= true;
70 #ifdef HAVE_SYSV_COMPAT
71 static bool arg_sysv_console
= true;
73 static bool arg_mount_auto
= true;
74 static bool arg_swap_auto
= true;
75 static char **arg_default_controllers
= NULL
;
76 static ExecOutput arg_default_std_output
= EXEC_OUTPUT_INHERIT
;
77 static ExecOutput arg_default_std_error
= EXEC_OUTPUT_INHERIT
;
79 static FILE* serialization
= NULL
;
81 static void nop_handler(int sig
) {
84 _noreturn_
static void crash(int sig
) {
87 log_error("Caught <%s>, not dumping core.", signal_to_string(sig
));
92 /* We want to wait for the core process, hence let's enable SIGCHLD */
94 sa
.sa_handler
= nop_handler
;
95 sa
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
96 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
98 if ((pid
= fork()) < 0)
99 log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig
), strerror(errno
));
104 /* Enable default signal handler for core dump */
106 sa
.sa_handler
= SIG_DFL
;
107 assert_se(sigaction(sig
, &sa
, NULL
) == 0);
109 /* Don't limit the core dump size */
111 rl
.rlim_cur
= RLIM_INFINITY
;
112 rl
.rlim_max
= RLIM_INFINITY
;
113 setrlimit(RLIMIT_CORE
, &rl
);
115 /* Just to be sure... */
116 assert_se(chdir("/") == 0);
118 /* Raise the signal again */
121 assert_not_reached("We shouldn't be here...");
128 /* Order things nicely. */
129 if ((r
= wait_for_terminate(pid
, &status
)) < 0)
130 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig
), strerror(-r
));
131 else if (status
.si_code
!= CLD_DUMPED
)
132 log_error("Caught <%s>, core dump failed.", signal_to_string(sig
));
134 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig
), (unsigned long) pid
);
139 chvt(arg_crash_chvt
);
141 if (arg_crash_shell
) {
145 log_info("Executing crash shell in 10s...");
148 /* Let the kernel reap children for us */
150 sa
.sa_handler
= SIG_IGN
;
151 sa
.sa_flags
= SA_NOCLDSTOP
|SA_NOCLDWAIT
|SA_RESTART
;
152 assert_se(sigaction(SIGCHLD
, &sa
, NULL
) == 0);
154 if ((pid
= fork()) < 0)
155 log_error("Failed to fork off crash shell: %s", strerror(errno
));
159 if ((fd
= acquire_terminal("/dev/console", false, true, true)) < 0)
160 log_error("Failed to acquire terminal: %s", strerror(-fd
));
161 else if ((r
= make_stdio(fd
)) < 0)
162 log_error("Failed to duplicate terminal fd: %s", strerror(-r
));
164 execl("/bin/sh", "/bin/sh", NULL
);
166 log_error("execl() failed: %s", strerror(errno
));
170 log_info("Successfully spawned crash shall as pid %lu.", (unsigned long) pid
);
173 log_info("Freezing execution.");
177 static void install_crash_handler(void) {
182 sa
.sa_handler
= crash
;
183 sa
.sa_flags
= SA_NODEFER
;
185 sigaction_many(&sa
, SIGNALS_CRASH_HANDLER
, -1);
188 static int console_setup(bool do_reset
) {
191 /* If we are init, we connect stdin/stdout/stderr to /dev/null
192 * and make sure we don't have a controlling tty. */
199 if ((tty_fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
)) < 0) {
200 log_error("Failed to open /dev/console: %s", strerror(-tty_fd
));
204 if ((r
= reset_terminal(tty_fd
)) < 0)
205 log_error("Failed to reset /dev/console: %s", strerror(-r
));
207 close_nointr_nofail(tty_fd
);
211 static int set_default_unit(const char *u
) {
216 if (!(c
= strdup(u
)))
219 free(arg_default_unit
);
220 arg_default_unit
= c
;
224 static int parse_proc_cmdline_word(const char *word
) {
226 static const char * const rlmap
[] = {
227 "emergency", SPECIAL_EMERGENCY_TARGET
,
228 "single", SPECIAL_RESCUE_TARGET
,
229 "-s", SPECIAL_RESCUE_TARGET
,
230 "s", SPECIAL_RESCUE_TARGET
,
231 "S", SPECIAL_RESCUE_TARGET
,
232 "1", SPECIAL_RESCUE_TARGET
,
233 "2", SPECIAL_RUNLEVEL2_TARGET
,
234 "3", SPECIAL_RUNLEVEL3_TARGET
,
235 "4", SPECIAL_RUNLEVEL4_TARGET
,
236 "5", SPECIAL_RUNLEVEL5_TARGET
,
241 if (startswith(word
, "systemd.unit="))
242 return set_default_unit(word
+ 13);
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.log_color=")) {
256 if (log_show_color_from_string(word
+ 18) < 0)
257 log_warning("Failed to parse log color setting %s. Ignoring.", word
+ 18);
259 } else if (startswith(word
, "systemd.log_location=")) {
261 if (log_show_location_from_string(word
+ 21) < 0)
262 log_warning("Failed to parse log location setting %s. Ignoring.", word
+ 21);
264 } else if (startswith(word
, "systemd.dump_core=")) {
267 if ((r
= parse_boolean(word
+ 18)) < 0)
268 log_warning("Failed to parse dump core switch %s, Ignoring.", word
+ 18);
272 } else if (startswith(word
, "systemd.crash_shell=")) {
275 if ((r
= parse_boolean(word
+ 20)) < 0)
276 log_warning("Failed to parse crash shell switch %s, Ignoring.", word
+ 20);
280 } else if (startswith(word
, "systemd.confirm_spawn=")) {
283 if ((r
= parse_boolean(word
+ 22)) < 0)
284 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word
+ 22);
286 arg_confirm_spawn
= r
;
288 } else if (startswith(word
, "systemd.crash_chvt=")) {
291 if (safe_atoi(word
+ 19, &k
) < 0)
292 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word
+ 19);
296 } else if (startswith(word
, "systemd.show_status=")) {
299 if ((r
= parse_boolean(word
+ 20)) < 0)
300 log_warning("Failed to parse show status switch %s, Ignoring.", word
+ 20);
303 } else if (startswith(word
, "systemd.default_standard_output=")) {
306 if ((r
= exec_output_from_string(word
+ 32)) < 0)
307 log_warning("Failed to parse default standard output switch %s, Ignoring.", word
+ 32);
309 arg_default_std_output
= r
;
310 } else if (startswith(word
, "systemd.default_standard_error=")) {
313 if ((r
= exec_output_from_string(word
+ 31)) < 0)
314 log_warning("Failed to parse default standard error switch %s, Ignoring.", word
+ 31);
316 arg_default_std_error
= r
;
317 #ifdef HAVE_SYSV_COMPAT
318 } else if (startswith(word
, "systemd.sysv_console=")) {
321 if ((r
= parse_boolean(word
+ 21)) < 0)
322 log_warning("Failed to parse SysV console switch %s, Ignoring.", word
+ 20);
324 arg_sysv_console
= r
;
327 } else if (startswith(word
, "systemd.")) {
329 log_warning("Unknown kernel switch %s. Ignoring.", word
);
331 log_info("Supported kernel switches:\n"
332 "systemd.unit=UNIT Default unit to start\n"
333 "systemd.dump_core=0|1 Dump core on crash\n"
334 "systemd.crash_shell=0|1 Run shell on crash\n"
335 "systemd.crash_chvt=N Change to VT #N on crash\n"
336 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
337 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
338 #ifdef HAVE_SYSV_COMPAT
339 "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
341 "systemd.log_target=console|kmsg|syslog|syslog-or-kmsg|null\n"
343 "systemd.log_level=LEVEL Log level\n"
344 "systemd.log_color=0|1 Highlight important log messages\n"
345 "systemd.log_location=0|1 Include code location in log messages\n"
346 "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
347 " Set default log output for services\n"
348 "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
349 " Set default log error output for services\n");
351 } else if (streq(word
, "quiet")) {
352 arg_show_status
= false;
353 #ifdef HAVE_SYSV_COMPAT
354 arg_sysv_console
= false;
359 /* SysV compatibility */
360 for (i
= 0; i
< ELEMENTSOF(rlmap
); i
+= 2)
361 if (streq(word
, rlmap
[i
]))
362 return set_default_unit(rlmap
[i
+1]);
368 static int config_parse_level(
369 const char *filename
,
381 log_set_max_level_from_string(rvalue
);
385 static int config_parse_target(
386 const char *filename
,
398 log_set_target_from_string(rvalue
);
402 static int config_parse_color(
403 const char *filename
,
415 log_show_color_from_string(rvalue
);
419 static int config_parse_location(
420 const char *filename
,
432 log_show_location_from_string(rvalue
);
436 static int config_parse_cpu_affinity(
437 const char *filename
,
455 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
460 if (!(t
= strndup(w
, l
)))
463 r
= safe_atou(t
, &cpu
);
467 if (!(c
= cpu_set_malloc(&ncpus
)))
470 if (r
< 0 || cpu
>= ncpus
) {
471 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename
, line
, rvalue
);
476 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
);
480 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus
), c
) < 0)
481 log_warning("Failed to set CPU affinity: %m");
489 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output
, exec_output
, ExecOutput
, "Failed to parse output specifier");
491 static int parse_config_file(void) {
493 const ConfigItem items
[] = {
494 { "LogLevel", config_parse_level
, NULL
, "Manager" },
495 { "LogTarget", config_parse_target
, NULL
, "Manager" },
496 { "LogColor", config_parse_color
, NULL
, "Manager" },
497 { "LogLocation", config_parse_location
, NULL
, "Manager" },
498 { "DumpCore", config_parse_bool
, &arg_dump_core
, "Manager" },
499 { "CrashShell", config_parse_bool
, &arg_crash_shell
, "Manager" },
500 { "ShowStatus", config_parse_bool
, &arg_show_status
, "Manager" },
501 #ifdef HAVE_SYSV_COMPAT
502 { "SysVConsole", config_parse_bool
, &arg_sysv_console
, "Manager" },
504 { "CrashChVT", config_parse_int
, &arg_crash_chvt
, "Manager" },
505 { "CPUAffinity", config_parse_cpu_affinity
, NULL
, "Manager" },
506 { "MountAuto", config_parse_bool
, &arg_mount_auto
, "Manager" },
507 { "SwapAuto", config_parse_bool
, &arg_swap_auto
, "Manager" },
508 { "DefaultControllers", config_parse_strv
, &arg_default_controllers
, "Manager" },
509 { "DefaultStandardOutput", config_parse_output
, &arg_default_std_output
, "Manager" },
510 { "DefaultStandardError", config_parse_output
, &arg_default_std_error
, "Manager" },
511 { NULL
, NULL
, NULL
, NULL
}
514 static const char * const sections
[] = {
523 fn
= arg_running_as
== MANAGER_SYSTEM
? SYSTEM_CONFIG_FILE
: USER_CONFIG_FILE
;
525 if (!(f
= fopen(fn
, "re"))) {
529 log_warning("Failed to open configuration file '%s': %m", fn
);
533 if ((r
= config_parse(fn
, f
, sections
, items
, false, NULL
)) < 0)
534 log_warning("Failed to parse configuration file: %s", strerror(-r
));
541 static int parse_proc_cmdline(void) {
542 char *line
, *w
, *state
;
546 if ((r
= read_one_line_file("/proc/cmdline", &line
)) < 0) {
547 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r
));
551 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
554 if (!(word
= strndup(w
, l
))) {
559 r
= parse_proc_cmdline_word(word
);
573 static int parse_argv(int argc
, char *argv
[]) {
576 ARG_LOG_LEVEL
= 0x100,
584 ARG_DUMP_CONFIGURATION_ITEMS
,
592 ARG_DEFAULT_STD_OUTPUT
,
593 ARG_DEFAULT_STD_ERROR
596 static const struct option options
[] = {
597 { "log-level", required_argument
, NULL
, ARG_LOG_LEVEL
},
598 { "log-target", required_argument
, NULL
, ARG_LOG_TARGET
},
599 { "log-color", optional_argument
, NULL
, ARG_LOG_COLOR
},
600 { "log-location", optional_argument
, NULL
, ARG_LOG_LOCATION
},
601 { "unit", required_argument
, NULL
, ARG_UNIT
},
602 { "system", no_argument
, NULL
, ARG_SYSTEM
},
603 { "user", no_argument
, NULL
, ARG_USER
},
604 { "test", no_argument
, NULL
, ARG_TEST
},
605 { "help", no_argument
, NULL
, 'h' },
606 { "dump-configuration-items", no_argument
, NULL
, ARG_DUMP_CONFIGURATION_ITEMS
},
607 { "dump-core", no_argument
, NULL
, ARG_DUMP_CORE
},
608 { "crash-shell", no_argument
, NULL
, ARG_CRASH_SHELL
},
609 { "confirm-spawn", no_argument
, NULL
, ARG_CONFIRM_SPAWN
},
610 { "show-status", optional_argument
, NULL
, ARG_SHOW_STATUS
},
611 #ifdef HAVE_SYSV_COMPAT
612 { "sysv-console", optional_argument
, NULL
, ARG_SYSV_CONSOLE
},
614 { "deserialize", required_argument
, NULL
, ARG_DESERIALIZE
},
615 { "introspect", optional_argument
, NULL
, ARG_INTROSPECT
},
616 { "default-standard-output", required_argument
, NULL
, ARG_DEFAULT_STD_OUTPUT
, },
617 { "default-standard-error", required_argument
, NULL
, ARG_DEFAULT_STD_ERROR
, },
626 while ((c
= getopt_long(argc
, argv
, "hD", options
, NULL
)) >= 0)
631 if ((r
= log_set_max_level_from_string(optarg
)) < 0) {
632 log_error("Failed to parse log level %s.", optarg
);
640 if ((r
= log_set_target_from_string(optarg
)) < 0) {
641 log_error("Failed to parse log target %s.", optarg
);
650 if ((r
= log_show_color_from_string(optarg
)) < 0) {
651 log_error("Failed to parse log color setting %s.", optarg
);
655 log_show_color(true);
659 case ARG_LOG_LOCATION
:
662 if ((r
= log_show_location_from_string(optarg
)) < 0) {
663 log_error("Failed to parse log location setting %s.", optarg
);
667 log_show_location(true);
671 case ARG_DEFAULT_STD_OUTPUT
:
673 if ((r
= exec_output_from_string(optarg
)) < 0) {
674 log_error("Failed to parse default standard output setting %s.", optarg
);
677 arg_default_std_output
= r
;
680 case ARG_DEFAULT_STD_ERROR
:
682 if ((r
= exec_output_from_string(optarg
)) < 0) {
683 log_error("Failed to parse default standard error output setting %s.", optarg
);
686 arg_default_std_error
= r
;
691 if ((r
= set_default_unit(optarg
)) < 0) {
692 log_error("Failed to set default unit %s: %s", optarg
, strerror(-r
));
699 arg_running_as
= MANAGER_SYSTEM
;
703 arg_running_as
= MANAGER_USER
;
707 arg_action
= ACTION_TEST
;
710 case ARG_DUMP_CONFIGURATION_ITEMS
:
711 arg_action
= ACTION_DUMP_CONFIGURATION_ITEMS
;
715 arg_dump_core
= true;
718 case ARG_CRASH_SHELL
:
719 arg_crash_shell
= true;
722 case ARG_CONFIRM_SPAWN
:
723 arg_confirm_spawn
= true;
726 case ARG_SHOW_STATUS
:
729 if ((r
= parse_boolean(optarg
)) < 0) {
730 log_error("Failed to show status boolean %s.", optarg
);
735 arg_show_status
= true;
737 #ifdef HAVE_SYSV_COMPAT
738 case ARG_SYSV_CONSOLE
:
741 if ((r
= parse_boolean(optarg
)) < 0) {
742 log_error("Failed to SysV console boolean %s.", optarg
);
745 arg_sysv_console
= r
;
747 arg_sysv_console
= true;
751 case ARG_DESERIALIZE
: {
755 if ((r
= safe_atoi(optarg
, &fd
)) < 0 || fd
< 0) {
756 log_error("Failed to parse deserialize option %s.", optarg
);
760 if (!(f
= fdopen(fd
, "r"))) {
761 log_error("Failed to open serialization fd: %m");
766 fclose(serialization
);
773 case ARG_INTROSPECT
: {
774 const char * const * i
= NULL
;
776 for (i
= bus_interface_table
; *i
; i
+= 2)
777 if (!optarg
|| streq(i
[0], optarg
)) {
778 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
781 fputs("</node>\n", stdout
);
788 log_error("Unknown interface %s.", optarg
);
790 arg_action
= ACTION_DONE
;
795 arg_action
= ACTION_HELP
;
799 log_set_max_level(LOG_DEBUG
);
806 log_error("Unknown option code %c", c
);
810 /* PID 1 will get the kernel arguments as parameters, which we
811 * ignore and unconditionally read from
812 * /proc/cmdline. However, we need to ignore those arguments
814 if (arg_running_as
!= MANAGER_SYSTEM
&& optind
< argc
) {
815 log_error("Excess arguments.");
822 static int help(void) {
824 printf("%s [OPTIONS...]\n\n"
825 "Starts up and maintains the system or user services.\n\n"
826 " -h --help Show this help\n"
827 " --test Determine startup sequence, dump it and exit\n"
828 " --dump-configuration-items Dump understood unit configuration items\n"
829 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
830 " --unit=UNIT Set default unit\n"
831 " --system Run a system instance, even if PID != 1\n"
832 " --user Run a user instance\n"
833 " --dump-core Dump core on crash\n"
834 " --crash-shell Run shell on crash\n"
835 " --confirm-spawn Ask for confirmation when spawning processes\n"
836 " --show-status[=0|1] Show status updates on the console during bootup\n"
837 #ifdef HAVE_SYSV_COMPAT
838 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
840 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
841 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
842 " --log-color[=0|1] Highlight important log messages\n"
843 " --log-location[=0|1] Include code location in log messages\n"
844 " --default-standard-output= Set default standard output for services\n"
845 " --default-standard-error= Set default standard error output for services\n",
846 program_invocation_short_name
);
851 static int prepare_reexecute(Manager
*m
, FILE **_f
, FDSet
**_fds
) {
860 if ((r
= manager_open_serialization(m
, &f
)) < 0) {
861 log_error("Failed to create serialization file: %s", strerror(-r
));
865 if (!(fds
= fdset_new())) {
867 log_error("Failed to allocate fd set: %s", strerror(-r
));
871 if ((r
= manager_serialize(m
, f
, fds
)) < 0) {
872 log_error("Failed to serialize state: %s", strerror(-r
));
876 if (fseeko(f
, 0, SEEK_SET
) < 0) {
877 log_error("Failed to rewind serialization fd: %m");
881 if ((r
= fd_cloexec(fileno(f
), false)) < 0) {
882 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r
));
886 if ((r
= fdset_cloexec(fds
, false)) < 0) {
887 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r
));
905 static struct dual_timestamp
* parse_initrd_timestamp(struct dual_timestamp
*t
) {
907 unsigned long long a
, b
;
911 if (!(e
= getenv("RD_TIMESTAMP")))
914 if (sscanf(e
, "%llu %llu", &a
, &b
) != 2)
917 t
->realtime
= (usec_t
) a
;
918 t
->monotonic
= (usec_t
) b
;
923 static void test_mtab(void) {
926 /* Check that /etc/mtab is a symlink */
928 if (readlink_malloc("/etc/mtab", &p
) >= 0) {
931 b
= streq(p
, "/proc/self/mounts") || streq(p
, "/proc/mounts");
938 log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
939 "This is not supported anymore. "
940 "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
943 static void test_usr(void) {
945 bool separate
= false;
947 /* Check that /usr is not a separate fs */
949 if (lstat("/", &a
) >= 0 && lstat("/usr", &b
) >= 0)
950 if (a
.st_dev
!= b
.st_dev
)
953 /* This check won't work usually during boot, since /usr is
954 * probably not mounted yet, hence let's add a second
955 * check. We just check whether /usr is an empty directory. */
957 if (dir_is_empty("/usr") > 0)
963 log_warning("/usr appears to be on a different file system than /. This is not supported anymore. "
964 "Some things will probably break (sometimes even silently) in mysterious ways.");
967 int main(int argc
, char *argv
[]) {
969 int r
, retval
= EXIT_FAILURE
;
971 bool reexecute
= false;
972 const char *shutdown_verb
= NULL
;
973 dual_timestamp initrd_timestamp
= { 0ULL, 0ULL };
974 char systemd
[] = "systemd";
976 if (getpid() != 1 && strstr(program_invocation_short_name
, "init")) {
977 /* This is compatibility support for SysV, where
978 * calling init as a user is identical to telinit. */
981 execv(SYSTEMCTL_BINARY_PATH
, argv
);
982 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH
": %m");
986 /* If we get started via the /sbin/init symlink then we are
987 called 'init'. After a subsequent reexecution we are then
988 called 'systemd'. That is confusing, hence let's call us
989 systemd right-away. */
991 program_invocation_short_name
= systemd
;
992 prctl(PR_SET_NAME
, systemd
);
994 log_show_color(isatty(STDERR_FILENO
) > 0);
995 log_show_location(false);
996 log_set_max_level(LOG_INFO
);
999 arg_running_as
= MANAGER_SYSTEM
;
1000 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
1002 /* This might actually not return, but cause a
1004 if (selinux_setup(argv
) < 0)
1007 if (label_init() < 0)
1010 arg_running_as
= MANAGER_USER
;
1011 log_set_target(LOG_TARGET_CONSOLE
);
1014 if (set_default_unit(SPECIAL_DEFAULT_TARGET
) < 0)
1017 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1018 * /proc/$PID/fd is available. */
1019 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
1020 if (mount_setup() < 0)
1023 /* Reset all signal handlers. */
1024 assert_se(reset_all_signal_handlers() == 0);
1026 /* If we are init, we can block sigkill. Yay. */
1027 ignore_signals(SIGNALS_IGNORE
, -1);
1029 if (parse_config_file() < 0)
1032 if (arg_running_as
== MANAGER_SYSTEM
)
1033 if (parse_proc_cmdline() < 0)
1036 log_parse_environment();
1038 if (parse_argv(argc
, argv
) < 0)
1041 if (arg_action
== ACTION_TEST
&& geteuid() == 0) {
1042 log_error("Don't run test mode as root.");
1046 /* If Plymouth is being run make sure we show the status, so
1047 * that there's something nice to see when people press Esc */
1048 if (access("/dev/.systemd/plymouth", F_OK
) >= 0)
1049 arg_show_status
= true;
1051 if (arg_action
== ACTION_HELP
) {
1054 } else if (arg_action
== ACTION_DUMP_CONFIGURATION_ITEMS
) {
1055 unit_dump_config_items(stdout
);
1056 retval
= EXIT_SUCCESS
;
1058 } else if (arg_action
== ACTION_DONE
) {
1059 retval
= EXIT_SUCCESS
;
1063 assert_se(arg_action
== ACTION_RUN
|| arg_action
== ACTION_TEST
);
1065 /* Remember open file descriptors for later deserialization */
1066 if (serialization
) {
1067 if ((r
= fdset_new_fill(&fds
)) < 0) {
1068 log_error("Failed to allocate fd set: %s", strerror(-r
));
1072 assert_se(fdset_remove(fds
, fileno(serialization
)) >= 0);
1074 close_all_fds(NULL
, 0);
1076 /* Set up PATH unless it is already set */
1078 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
1079 arg_running_as
== MANAGER_SYSTEM
);
1081 if (arg_running_as
== MANAGER_SYSTEM
) {
1082 /* Parse the data passed to us by the initrd and unset it */
1083 parse_initrd_timestamp(&initrd_timestamp
);
1084 filter_environ("RD_");
1086 /* Unset some environment variables passed in from the
1087 * kernel that don't really make sense for us. */
1092 /* Move out of the way, so that we won't block unmounts */
1093 assert_se(chdir("/") == 0);
1095 if (arg_running_as
== MANAGER_SYSTEM
) {
1096 /* Become a session leader if we aren't one yet. */
1099 /* Disable the umask logic */
1103 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1104 dbus_connection_set_change_sigpipe(FALSE
);
1106 /* Reset the console, but only if this is really init and we
1107 * are freshly booted */
1108 if (arg_running_as
== MANAGER_SYSTEM
&& arg_action
== ACTION_RUN
) {
1109 console_setup(getpid() == 1 && !serialization
);
1113 /* Open the logging devices, if possible and necessary */
1116 /* Make sure we leave a core dump without panicing the
1119 install_crash_handler();
1121 log_full(arg_running_as
== MANAGER_SYSTEM
? LOG_INFO
: LOG_DEBUG
,
1122 PACKAGE_STRING
" running in %s mode. (" SYSTEMD_FEATURES
"; " DISTRIBUTION
")", manager_running_as_to_string(arg_running_as
));
1124 if (arg_running_as
== MANAGER_SYSTEM
&& !serialization
) {
1127 if (arg_show_status
)
1134 mkdir_p("/dev/.systemd/ask-password/", 0755);
1140 if ((r
= manager_new(arg_running_as
, &m
)) < 0) {
1141 log_error("Failed to allocate manager object: %s", strerror(-r
));
1145 m
->confirm_spawn
= arg_confirm_spawn
;
1146 m
->show_status
= arg_show_status
;
1147 #ifdef HAVE_SYSV_COMPAT
1148 m
->sysv_console
= arg_sysv_console
;
1150 m
->mount_auto
= arg_mount_auto
;
1151 m
->swap_auto
= arg_swap_auto
;
1152 m
->default_std_output
= arg_default_std_output
;
1153 m
->default_std_error
= arg_default_std_error
;
1155 if (dual_timestamp_is_set(&initrd_timestamp
))
1156 m
->initrd_timestamp
= initrd_timestamp
;
1158 if (arg_default_controllers
)
1159 manager_set_default_controllers(m
, arg_default_controllers
);
1161 if ((r
= manager_startup(m
, serialization
, fds
)) < 0)
1162 log_error("Failed to fully start up daemon: %s", strerror(-r
));
1165 /* This will close all file descriptors that were opened, but
1166 * not claimed by any unit. */
1172 if (serialization
) {
1173 fclose(serialization
);
1174 serialization
= NULL
;
1177 Unit
*target
= NULL
;
1179 dbus_error_init(&error
);
1181 log_debug("Activating default unit: %s", arg_default_unit
);
1183 if ((r
= manager_load_unit(m
, arg_default_unit
, NULL
, &error
, &target
)) < 0) {
1184 log_error("Failed to load default target: %s", bus_error(&error
, r
));
1185 dbus_error_free(&error
);
1186 } else if (target
->meta
.load_state
== UNIT_ERROR
)
1187 log_error("Failed to load default target: %s", strerror(-target
->meta
.load_error
));
1188 else if (target
->meta
.load_state
== UNIT_MASKED
)
1189 log_error("Default target masked.");
1191 if (!target
|| target
->meta
.load_state
!= UNIT_LOADED
) {
1192 log_info("Trying to load rescue target...");
1194 if ((r
= manager_load_unit(m
, SPECIAL_RESCUE_TARGET
, NULL
, &error
, &target
)) < 0) {
1195 log_error("Failed to load rescue target: %s", bus_error(&error
, r
));
1196 dbus_error_free(&error
);
1198 } else if (target
->meta
.load_state
== UNIT_ERROR
) {
1199 log_error("Failed to load rescue target: %s", strerror(-target
->meta
.load_error
));
1201 } else if (target
->meta
.load_state
== UNIT_MASKED
) {
1202 log_error("Rescue target masked.");
1207 assert(target
->meta
.load_state
== UNIT_LOADED
);
1209 if (arg_action
== ACTION_TEST
) {
1210 printf("-> By units:\n");
1211 manager_dump_units(m
, stdout
, "\t");
1214 if ((r
= manager_add_job(m
, JOB_START
, target
, JOB_REPLACE
, false, &error
, NULL
)) < 0) {
1215 log_error("Failed to start default target: %s", bus_error(&error
, r
));
1216 dbus_error_free(&error
);
1220 if (arg_action
== ACTION_TEST
) {
1221 printf("-> By jobs:\n");
1222 manager_dump_jobs(m
, stdout
, "\t");
1223 retval
= EXIT_SUCCESS
;
1229 if ((r
= manager_loop(m
)) < 0) {
1230 log_error("Failed to run mainloop: %s", strerror(-r
));
1234 switch (m
->exit_code
) {
1237 retval
= EXIT_SUCCESS
;
1241 case MANAGER_RELOAD
:
1242 log_info("Reloading.");
1243 if ((r
= manager_reload(m
)) < 0)
1244 log_error("Failed to reload: %s", strerror(-r
));
1247 case MANAGER_REEXECUTE
:
1248 if (prepare_reexecute(m
, &serialization
, &fds
) < 0)
1252 log_notice("Reexecuting.");
1255 case MANAGER_REBOOT
:
1256 case MANAGER_POWEROFF
:
1258 case MANAGER_KEXEC
: {
1259 static const char * const table
[_MANAGER_EXIT_CODE_MAX
] = {
1260 [MANAGER_REBOOT
] = "reboot",
1261 [MANAGER_POWEROFF
] = "poweroff",
1262 [MANAGER_HALT
] = "halt",
1263 [MANAGER_KEXEC
] = "kexec"
1266 assert_se(shutdown_verb
= table
[m
->exit_code
]);
1268 log_notice("Shutting down.");
1273 assert_not_reached("Unknown exit code.");
1281 free(arg_default_unit
);
1282 strv_free(arg_default_controllers
);
1289 const char *args
[15];
1293 assert(serialization
);
1296 args
[i
++] = SYSTEMD_BINARY_PATH
;
1298 args
[i
++] = "--log-level";
1299 args
[i
++] = log_level_to_string(log_get_max_level());
1301 args
[i
++] = "--log-target";
1302 args
[i
++] = log_target_to_string(log_get_target());
1304 if (arg_running_as
== MANAGER_SYSTEM
)
1305 args
[i
++] = "--system";
1307 args
[i
++] = "--user";
1310 args
[i
++] = "--dump-core";
1312 if (arg_crash_shell
)
1313 args
[i
++] = "--crash-shell";
1315 if (arg_confirm_spawn
)
1316 args
[i
++] = "--confirm-spawn";
1318 if (arg_show_status
)
1319 args
[i
++] = "--show-status=1";
1321 args
[i
++] = "--show-status=0";
1323 #ifdef HAVE_SYSV_COMPAT
1324 if (arg_sysv_console
)
1325 args
[i
++] = "--sysv-console=1";
1327 args
[i
++] = "--sysv-console=0";
1330 snprintf(sfd
, sizeof(sfd
), "%i", fileno(serialization
));
1333 args
[i
++] = "--deserialize";
1338 assert(i
<= ELEMENTSOF(args
));
1340 execv(args
[0], (char* const*) args
);
1342 log_error("Failed to reexecute: %m");
1346 fclose(serialization
);
1351 if (shutdown_verb
) {
1352 const char * command_line
[] = {
1353 SYSTEMD_SHUTDOWN_BINARY_PATH
,
1358 execv(SYSTEMD_SHUTDOWN_BINARY_PATH
, (char **) command_line
);
1359 log_error("Failed to execute shutdown binary, freezing: %m");