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 "machine-id-setup.h"
45 #include "load-fragment.h"
48 #include "conf-parser.h"
49 #include "bus-errors.h"
60 ACTION_DUMP_CONFIGURATION_ITEMS,
62 } arg_action = ACTION_RUN;
64 static char *arg_default_unit = NULL;
65 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
67 static bool arg_dump_core = true;
68 static bool arg_crash_shell = false;
69 static int arg_crash_chvt = -1;
70 static bool arg_confirm_spawn = false;
71 static bool arg_show_status = true;
72 #ifdef HAVE_SYSV_COMPAT
73 static bool arg_sysv_console = true;
75 static bool arg_mount_auto = true;
76 static bool arg_swap_auto = true;
77 static char **arg_default_controllers = NULL;
78 static ExecOutput arg_default_std_output = EXEC_OUTPUT_INHERIT;
79 static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
81 static FILE* serialization = NULL;
83 static void nop_handler(int sig) {
86 _noreturn_ static void crash(int sig) {
89 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
94 /* We want to wait for the core process, hence let's enable SIGCHLD */
96 sa.sa_handler = nop_handler;
97 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
98 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
100 if ((pid = fork()) < 0)
101 log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
106 /* Enable default signal handler for core dump */
108 sa.sa_handler = SIG_DFL;
109 assert_se(sigaction(sig, &sa, NULL) == 0);
111 /* Don't limit the core dump size */
113 rl.rlim_cur = RLIM_INFINITY;
114 rl.rlim_max = RLIM_INFINITY;
115 setrlimit(RLIMIT_CORE, &rl);
117 /* Just to be sure... */
118 assert_se(chdir("/") == 0);
120 /* Raise the signal again */
123 assert_not_reached("We shouldn't be here...");
130 /* Order things nicely. */
131 if ((r = wait_for_terminate(pid, &status)) < 0)
132 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
133 else if (status.si_code != CLD_DUMPED)
134 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
136 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
141 chvt(arg_crash_chvt);
143 if (arg_crash_shell) {
147 log_info("Executing crash shell in 10s...");
150 /* Let the kernel reap children for us */
152 sa.sa_handler = SIG_IGN;
153 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
154 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
156 if ((pid = fork()) < 0)
157 log_error("Failed to fork off crash shell: %s", strerror(errno));
161 if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
162 log_error("Failed to acquire terminal: %s", strerror(-fd));
163 else if ((r = make_stdio(fd)) < 0)
164 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
166 execl("/bin/sh", "/bin/sh", NULL);
168 log_error("execl() failed: %s", strerror(errno));
172 log_info("Successfully spawned crash shall as pid %lu.", (unsigned long) pid);
175 log_info("Freezing execution.");
179 static void install_crash_handler(void) {
184 sa.sa_handler = crash;
185 sa.sa_flags = SA_NODEFER;
187 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
190 static int console_setup(bool do_reset) {
193 /* If we are init, we connect stdin/stdout/stderr to /dev/null
194 * and make sure we don't have a controlling tty. */
201 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
202 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
206 if ((r = reset_terminal(tty_fd)) < 0)
207 log_error("Failed to reset /dev/console: %s", strerror(-r));
209 close_nointr_nofail(tty_fd);
213 static int set_default_unit(const char *u) {
218 if (!(c = strdup(u)))
221 free(arg_default_unit);
222 arg_default_unit = c;
226 static int parse_proc_cmdline_word(const char *word) {
228 static const char * const rlmap[] = {
229 "emergency", SPECIAL_EMERGENCY_TARGET,
230 "-b", SPECIAL_EMERGENCY_TARGET,
231 "single", SPECIAL_RESCUE_TARGET,
232 "-s", SPECIAL_RESCUE_TARGET,
233 "s", SPECIAL_RESCUE_TARGET,
234 "S", SPECIAL_RESCUE_TARGET,
235 "1", SPECIAL_RESCUE_TARGET,
236 "2", SPECIAL_RUNLEVEL2_TARGET,
237 "3", SPECIAL_RUNLEVEL3_TARGET,
238 "4", SPECIAL_RUNLEVEL4_TARGET,
239 "5", SPECIAL_RUNLEVEL5_TARGET,
244 if (startswith(word, "systemd.unit="))
245 return set_default_unit(word + 13);
247 else if (startswith(word, "systemd.log_target=")) {
249 if (log_set_target_from_string(word + 19) < 0)
250 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
252 } else if (startswith(word, "systemd.log_level=")) {
254 if (log_set_max_level_from_string(word + 18) < 0)
255 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
257 } else if (startswith(word, "systemd.log_color=")) {
259 if (log_show_color_from_string(word + 18) < 0)
260 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
262 } else if (startswith(word, "systemd.log_location=")) {
264 if (log_show_location_from_string(word + 21) < 0)
265 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
267 } else if (startswith(word, "systemd.dump_core=")) {
270 if ((r = parse_boolean(word + 18)) < 0)
271 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
275 } else if (startswith(word, "systemd.crash_shell=")) {
278 if ((r = parse_boolean(word + 20)) < 0)
279 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
283 } else if (startswith(word, "systemd.confirm_spawn=")) {
286 if ((r = parse_boolean(word + 22)) < 0)
287 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
289 arg_confirm_spawn = r;
291 } else if (startswith(word, "systemd.crash_chvt=")) {
294 if (safe_atoi(word + 19, &k) < 0)
295 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
299 } else if (startswith(word, "systemd.show_status=")) {
302 if ((r = parse_boolean(word + 20)) < 0)
303 log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
306 } else if (startswith(word, "systemd.default_standard_output=")) {
309 if ((r = exec_output_from_string(word + 32)) < 0)
310 log_warning("Failed to parse default standard output switch %s, Ignoring.", word + 32);
312 arg_default_std_output = r;
313 } else if (startswith(word, "systemd.default_standard_error=")) {
316 if ((r = exec_output_from_string(word + 31)) < 0)
317 log_warning("Failed to parse default standard error switch %s, Ignoring.", word + 31);
319 arg_default_std_error = r;
320 #ifdef HAVE_SYSV_COMPAT
321 } else if (startswith(word, "systemd.sysv_console=")) {
324 if ((r = parse_boolean(word + 21)) < 0)
325 log_warning("Failed to parse SysV console switch %s, Ignoring.", word + 20);
327 arg_sysv_console = r;
330 } else if (startswith(word, "systemd.")) {
332 log_warning("Unknown kernel switch %s. Ignoring.", word);
334 log_info("Supported kernel switches:\n"
335 "systemd.unit=UNIT Default unit to start\n"
336 "systemd.dump_core=0|1 Dump core on crash\n"
337 "systemd.crash_shell=0|1 Run shell on crash\n"
338 "systemd.crash_chvt=N Change to VT #N on crash\n"
339 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
340 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
341 #ifdef HAVE_SYSV_COMPAT
342 "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
344 "systemd.log_target=console|kmsg|syslog|syslog-or-kmsg|null\n"
346 "systemd.log_level=LEVEL Log level\n"
347 "systemd.log_color=0|1 Highlight important log messages\n"
348 "systemd.log_location=0|1 Include code location in log messages\n"
349 "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
350 " Set default log output for services\n"
351 "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
352 " Set default log error output for services\n");
354 } else if (streq(word, "quiet")) {
355 arg_show_status = false;
356 #ifdef HAVE_SYSV_COMPAT
357 arg_sysv_console = false;
362 /* SysV compatibility */
363 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
364 if (streq(word, rlmap[i]))
365 return set_default_unit(rlmap[i+1]);
371 static int config_parse_level(
372 const char *filename,
385 log_set_max_level_from_string(rvalue);
389 static int config_parse_target(
390 const char *filename,
403 log_set_target_from_string(rvalue);
407 static int config_parse_color(
408 const char *filename,
421 log_show_color_from_string(rvalue);
425 static int config_parse_location(
426 const char *filename,
439 log_show_location_from_string(rvalue);
443 static int config_parse_cpu_affinity(
444 const char *filename,
463 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
468 if (!(t = strndup(w, l)))
471 r = safe_atou(t, &cpu);
475 if (!(c = cpu_set_malloc(&ncpus)))
478 if (r < 0 || cpu >= ncpus) {
479 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
484 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
488 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
489 log_warning("Failed to set CPU affinity: %m");
497 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
499 static int parse_config_file(void) {
501 const ConfigItem items[] = {
502 { "LogLevel", config_parse_level, 0, NULL, "Manager" },
503 { "LogTarget", config_parse_target, 0, NULL, "Manager" },
504 { "LogColor", config_parse_color, 0, NULL, "Manager" },
505 { "LogLocation", config_parse_location, 0, NULL, "Manager" },
506 { "DumpCore", config_parse_bool, 0, &arg_dump_core, "Manager" },
507 { "CrashShell", config_parse_bool, 0, &arg_crash_shell, "Manager" },
508 { "ShowStatus", config_parse_bool, 0, &arg_show_status, "Manager" },
509 #ifdef HAVE_SYSV_COMPAT
510 { "SysVConsole", config_parse_bool, 0, &arg_sysv_console, "Manager" },
512 { "CrashChVT", config_parse_int, 0, &arg_crash_chvt, "Manager" },
513 { "CPUAffinity", config_parse_cpu_affinity, 0, NULL, "Manager" },
514 { "MountAuto", config_parse_bool, 0, &arg_mount_auto, "Manager" },
515 { "SwapAuto", config_parse_bool, 0, &arg_swap_auto, "Manager" },
516 { "DefaultControllers", config_parse_strv, 0, &arg_default_controllers, "Manager" },
517 { "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output, "Manager" },
518 { "DefaultStandardError", config_parse_output, 0, &arg_default_std_error, "Manager" },
519 { NULL, NULL, 0, NULL, NULL }
522 static const char * const sections[] = {
531 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
533 if (!(f = fopen(fn, "re"))) {
537 log_warning("Failed to open configuration file '%s': %m", fn);
541 if ((r = config_parse(fn, f, sections, items, false, NULL)) < 0)
542 log_warning("Failed to parse configuration file: %s", strerror(-r));
549 static int parse_proc_cmdline(void) {
550 char *line, *w, *state;
554 /* Don't read /proc/cmdline if we are in a container, since
555 * that is only relevant for the host system */
556 if (detect_container(NULL) > 0)
559 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
560 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
564 FOREACH_WORD_QUOTED(w, l, line, state) {
567 if (!(word = strndup(w, l))) {
572 r = parse_proc_cmdline_word(word);
586 static int parse_argv(int argc, char *argv[]) {
589 ARG_LOG_LEVEL = 0x100,
597 ARG_DUMP_CONFIGURATION_ITEMS,
605 ARG_DEFAULT_STD_OUTPUT,
606 ARG_DEFAULT_STD_ERROR
609 static const struct option options[] = {
610 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
611 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
612 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
613 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
614 { "unit", required_argument, NULL, ARG_UNIT },
615 { "system", no_argument, NULL, ARG_SYSTEM },
616 { "user", no_argument, NULL, ARG_USER },
617 { "test", no_argument, NULL, ARG_TEST },
618 { "help", no_argument, NULL, 'h' },
619 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
620 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
621 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
622 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
623 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
624 #ifdef HAVE_SYSV_COMPAT
625 { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
627 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
628 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
629 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
630 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
642 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
647 if ((r = log_set_max_level_from_string(optarg)) < 0) {
648 log_error("Failed to parse log level %s.", optarg);
656 if ((r = log_set_target_from_string(optarg)) < 0) {
657 log_error("Failed to parse log target %s.", optarg);
666 if ((r = log_show_color_from_string(optarg)) < 0) {
667 log_error("Failed to parse log color setting %s.", optarg);
671 log_show_color(true);
675 case ARG_LOG_LOCATION:
678 if ((r = log_show_location_from_string(optarg)) < 0) {
679 log_error("Failed to parse log location setting %s.", optarg);
683 log_show_location(true);
687 case ARG_DEFAULT_STD_OUTPUT:
689 if ((r = exec_output_from_string(optarg)) < 0) {
690 log_error("Failed to parse default standard output setting %s.", optarg);
693 arg_default_std_output = r;
696 case ARG_DEFAULT_STD_ERROR:
698 if ((r = exec_output_from_string(optarg)) < 0) {
699 log_error("Failed to parse default standard error output setting %s.", optarg);
702 arg_default_std_error = r;
707 if ((r = set_default_unit(optarg)) < 0) {
708 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
715 arg_running_as = MANAGER_SYSTEM;
719 arg_running_as = MANAGER_USER;
723 arg_action = ACTION_TEST;
726 case ARG_DUMP_CONFIGURATION_ITEMS:
727 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
731 arg_dump_core = true;
734 case ARG_CRASH_SHELL:
735 arg_crash_shell = true;
738 case ARG_CONFIRM_SPAWN:
739 arg_confirm_spawn = true;
742 case ARG_SHOW_STATUS:
745 if ((r = parse_boolean(optarg)) < 0) {
746 log_error("Failed to show status boolean %s.", optarg);
751 arg_show_status = true;
753 #ifdef HAVE_SYSV_COMPAT
754 case ARG_SYSV_CONSOLE:
757 if ((r = parse_boolean(optarg)) < 0) {
758 log_error("Failed to SysV console boolean %s.", optarg);
761 arg_sysv_console = r;
763 arg_sysv_console = true;
767 case ARG_DESERIALIZE: {
771 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
772 log_error("Failed to parse deserialize option %s.", optarg);
776 if (!(f = fdopen(fd, "r"))) {
777 log_error("Failed to open serialization fd: %m");
782 fclose(serialization);
789 case ARG_INTROSPECT: {
790 const char * const * i = NULL;
792 for (i = bus_interface_table; *i; i += 2)
793 if (!optarg || streq(i[0], optarg)) {
794 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
797 fputs("</node>\n", stdout);
804 log_error("Unknown interface %s.", optarg);
806 arg_action = ACTION_DONE;
811 arg_action = ACTION_HELP;
815 log_set_max_level(LOG_DEBUG);
821 /* Just to eat away the sysvinit kernel
822 * cmdline args without getopt() error
823 * messages that we'll parse in
824 * parse_proc_cmdline_word() or ignore. */
829 log_error("Unknown option code %c", c);
836 if (optind < argc && getpid() != 1) {
837 /* Hmm, when we aren't run as init system
838 * let's complain about excess arguments */
840 log_error("Excess arguments.");
844 if (detect_container(NULL) > 0) {
847 /* All /proc/cmdline arguments the kernel didn't
848 * understand it passed to us. We're not really
849 * interested in that usually since /proc/cmdline is
850 * more interesting and complete. With one exception:
851 * if we are run in a container /proc/cmdline is not
852 * relevant for the container, hence we rely on argv[]
855 for (a = argv; a < argv + argc; a++)
856 if ((r = parse_proc_cmdline_word(*a)) < 0)
863 static int help(void) {
865 printf("%s [OPTIONS...]\n\n"
866 "Starts up and maintains the system or user services.\n\n"
867 " -h --help Show this help\n"
868 " --test Determine startup sequence, dump it and exit\n"
869 " --dump-configuration-items Dump understood unit configuration items\n"
870 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
871 " --unit=UNIT Set default unit\n"
872 " --system Run a system instance, even if PID != 1\n"
873 " --user Run a user instance\n"
874 " --dump-core Dump core on crash\n"
875 " --crash-shell Run shell on crash\n"
876 " --confirm-spawn Ask for confirmation when spawning processes\n"
877 " --show-status[=0|1] Show status updates on the console during bootup\n"
878 #ifdef HAVE_SYSV_COMPAT
879 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
881 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
882 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
883 " --log-color[=0|1] Highlight important log messages\n"
884 " --log-location[=0|1] Include code location in log messages\n"
885 " --default-standard-output= Set default standard output for services\n"
886 " --default-standard-error= Set default standard error output for services\n",
887 program_invocation_short_name);
892 static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
901 if ((r = manager_open_serialization(m, &f)) < 0) {
902 log_error("Failed to create serialization file: %s", strerror(-r));
906 if (!(fds = fdset_new())) {
908 log_error("Failed to allocate fd set: %s", strerror(-r));
912 if ((r = manager_serialize(m, f, fds)) < 0) {
913 log_error("Failed to serialize state: %s", strerror(-r));
917 if (fseeko(f, 0, SEEK_SET) < 0) {
918 log_error("Failed to rewind serialization fd: %m");
922 if ((r = fd_cloexec(fileno(f), false)) < 0) {
923 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
927 if ((r = fdset_cloexec(fds, false)) < 0) {
928 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
946 static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
948 unsigned long long a, b;
952 if (!(e = getenv("RD_TIMESTAMP")))
955 if (sscanf(e, "%llu %llu", &a, &b) != 2)
958 t->realtime = (usec_t) a;
959 t->monotonic = (usec_t) b;
964 static void test_mtab(void) {
967 /* Check that /etc/mtab is a symlink */
969 if (readlink_malloc("/etc/mtab", &p) >= 0) {
972 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
979 log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
980 "This is not supported anymore. "
981 "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
984 static void test_usr(void) {
986 /* Check that /usr is not a separate fs */
988 if (dir_is_empty("/usr") <= 0)
991 log_warning("/usr appears to be on a different file system than /. This is not supported anymore. "
992 "Some things will probably break (sometimes even silently) in mysterious ways. "
993 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
996 static void test_cgroups(void) {
998 if (access("/proc/cgroups", F_OK) >= 0)
1001 log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
1002 "Systems without control groups are not supported. "
1003 "We will now sleep for 10s, and then continue boot-up. "
1004 "Expect breakage and please do not file bugs. "
1005 "Instead fix your kernel and enable CONFIG_CGROUPS." );
1010 int main(int argc, char *argv[]) {
1012 int r, retval = EXIT_FAILURE;
1014 bool reexecute = false;
1015 const char *shutdown_verb = NULL;
1016 dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
1017 char systemd[] = "systemd";
1019 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
1020 /* This is compatibility support for SysV, where
1021 * calling init as a user is identical to telinit. */
1024 execv(SYSTEMCTL_BINARY_PATH, argv);
1025 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1029 /* If we get started via the /sbin/init symlink then we are
1030 called 'init'. After a subsequent reexecution we are then
1031 called 'systemd'. That is confusing, hence let's call us
1032 systemd right-away. */
1034 program_invocation_short_name = systemd;
1035 prctl(PR_SET_NAME, systemd);
1037 log_show_color(isatty(STDERR_FILENO) > 0);
1038 log_show_location(false);
1039 log_set_max_level(LOG_INFO);
1041 if (getpid() == 1) {
1042 arg_running_as = MANAGER_SYSTEM;
1043 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_SYSLOG_OR_KMSG);
1045 /* This might actually not return, but cause a
1047 if (selinux_setup(argv) < 0)
1050 if (label_init() < 0)
1053 arg_running_as = MANAGER_USER;
1054 log_set_target(LOG_TARGET_CONSOLE);
1057 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
1060 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1061 * /proc/$PID/fd is available. */
1062 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
1063 if (mount_setup() < 0)
1066 /* Reset all signal handlers. */
1067 assert_se(reset_all_signal_handlers() == 0);
1069 /* If we are init, we can block sigkill. Yay. */
1070 ignore_signals(SIGNALS_IGNORE, -1);
1072 if (parse_config_file() < 0)
1075 if (arg_running_as == MANAGER_SYSTEM)
1076 if (parse_proc_cmdline() < 0)
1079 log_parse_environment();
1081 if (parse_argv(argc, argv) < 0)
1084 if (arg_action == ACTION_TEST && geteuid() == 0) {
1085 log_error("Don't run test mode as root.");
1089 if (arg_running_as == MANAGER_SYSTEM &&
1090 arg_action == ACTION_RUN &&
1091 running_in_chroot() > 0) {
1092 log_error("Cannot be run in a chroot() environment.");
1096 if (arg_action == ACTION_HELP) {
1099 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
1100 unit_dump_config_items(stdout);
1101 retval = EXIT_SUCCESS;
1103 } else if (arg_action == ACTION_DONE) {
1104 retval = EXIT_SUCCESS;
1108 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
1110 /* Remember open file descriptors for later deserialization */
1111 if (serialization) {
1112 if ((r = fdset_new_fill(&fds)) < 0) {
1113 log_error("Failed to allocate fd set: %s", strerror(-r));
1117 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
1119 close_all_fds(NULL, 0);
1121 /* Set up PATH unless it is already set */
1123 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
1124 arg_running_as == MANAGER_SYSTEM);
1126 if (arg_running_as == MANAGER_SYSTEM) {
1127 /* Parse the data passed to us by the initrd and unset it */
1128 parse_initrd_timestamp(&initrd_timestamp);
1129 filter_environ("RD_");
1131 /* Unset some environment variables passed in from the
1132 * kernel that don't really make sense for us. */
1136 /* All other variables are left as is, so that clients
1137 * can still read them via /proc/1/environ */
1140 /* Move out of the way, so that we won't block unmounts */
1141 assert_se(chdir("/") == 0);
1143 if (arg_running_as == MANAGER_SYSTEM) {
1144 /* Become a session leader if we aren't one yet. */
1147 /* Disable the umask logic */
1151 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1152 dbus_connection_set_change_sigpipe(FALSE);
1154 /* Reset the console, but only if this is really init and we
1155 * are freshly booted */
1156 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
1157 console_setup(getpid() == 1 && !serialization);
1161 /* Open the logging devices, if possible and necessary */
1164 /* Make sure we leave a core dump without panicing the
1167 install_crash_handler();
1169 log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
1170 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
1172 if (arg_running_as == MANAGER_SYSTEM && !serialization) {
1175 if (arg_show_status || plymouth_running())
1188 if ((r = manager_new(arg_running_as, &m)) < 0) {
1189 log_error("Failed to allocate manager object: %s", strerror(-r));
1193 m->confirm_spawn = arg_confirm_spawn;
1194 m->show_status = arg_show_status;
1195 #ifdef HAVE_SYSV_COMPAT
1196 m->sysv_console = arg_sysv_console;
1198 m->mount_auto = arg_mount_auto;
1199 m->swap_auto = arg_swap_auto;
1200 m->default_std_output = arg_default_std_output;
1201 m->default_std_error = arg_default_std_error;
1203 if (dual_timestamp_is_set(&initrd_timestamp))
1204 m->initrd_timestamp = initrd_timestamp;
1206 if (arg_default_controllers)
1207 manager_set_default_controllers(m, arg_default_controllers);
1209 if ((r = manager_startup(m, serialization, fds)) < 0)
1210 log_error("Failed to fully start up daemon: %s", strerror(-r));
1213 /* This will close all file descriptors that were opened, but
1214 * not claimed by any unit. */
1220 if (serialization) {
1221 fclose(serialization);
1222 serialization = NULL;
1225 Unit *target = NULL;
1227 dbus_error_init(&error);
1229 log_debug("Activating default unit: %s", arg_default_unit);
1231 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1232 log_error("Failed to load default target: %s", bus_error(&error, r));
1233 dbus_error_free(&error);
1234 } else if (target->meta.load_state == UNIT_ERROR)
1235 log_error("Failed to load default target: %s", strerror(-target->meta.load_error));
1236 else if (target->meta.load_state == UNIT_MASKED)
1237 log_error("Default target masked.");
1239 if (!target || target->meta.load_state != UNIT_LOADED) {
1240 log_info("Trying to load rescue target...");
1242 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1243 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1244 dbus_error_free(&error);
1246 } else if (target->meta.load_state == UNIT_ERROR) {
1247 log_error("Failed to load rescue target: %s", strerror(-target->meta.load_error));
1249 } else if (target->meta.load_state == UNIT_MASKED) {
1250 log_error("Rescue target masked.");
1255 assert(target->meta.load_state == UNIT_LOADED);
1257 if (arg_action == ACTION_TEST) {
1258 printf("-> By units:\n");
1259 manager_dump_units(m, stdout, "\t");
1262 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
1263 log_error("Failed to start default target: %s", bus_error(&error, r));
1264 dbus_error_free(&error);
1268 if (arg_action == ACTION_TEST) {
1269 printf("-> By jobs:\n");
1270 manager_dump_jobs(m, stdout, "\t");
1271 retval = EXIT_SUCCESS;
1277 if ((r = manager_loop(m)) < 0) {
1278 log_error("Failed to run mainloop: %s", strerror(-r));
1282 switch (m->exit_code) {
1285 retval = EXIT_SUCCESS;
1289 case MANAGER_RELOAD:
1290 log_info("Reloading.");
1291 if ((r = manager_reload(m)) < 0)
1292 log_error("Failed to reload: %s", strerror(-r));
1295 case MANAGER_REEXECUTE:
1296 if (prepare_reexecute(m, &serialization, &fds) < 0)
1300 log_notice("Reexecuting.");
1303 case MANAGER_REBOOT:
1304 case MANAGER_POWEROFF:
1306 case MANAGER_KEXEC: {
1307 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1308 [MANAGER_REBOOT] = "reboot",
1309 [MANAGER_POWEROFF] = "poweroff",
1310 [MANAGER_HALT] = "halt",
1311 [MANAGER_KEXEC] = "kexec"
1314 assert_se(shutdown_verb = table[m->exit_code]);
1316 log_notice("Shutting down.");
1321 assert_not_reached("Unknown exit code.");
1329 free(arg_default_unit);
1330 strv_free(arg_default_controllers);
1337 const char *args[15];
1341 assert(serialization);
1344 args[i++] = SYSTEMD_BINARY_PATH;
1346 args[i++] = "--log-level";
1347 args[i++] = log_level_to_string(log_get_max_level());
1349 args[i++] = "--log-target";
1350 args[i++] = log_target_to_string(log_get_target());
1352 if (arg_running_as == MANAGER_SYSTEM)
1353 args[i++] = "--system";
1355 args[i++] = "--user";
1358 args[i++] = "--dump-core";
1360 if (arg_crash_shell)
1361 args[i++] = "--crash-shell";
1363 if (arg_confirm_spawn)
1364 args[i++] = "--confirm-spawn";
1366 if (arg_show_status)
1367 args[i++] = "--show-status=1";
1369 args[i++] = "--show-status=0";
1371 #ifdef HAVE_SYSV_COMPAT
1372 if (arg_sysv_console)
1373 args[i++] = "--sysv-console=1";
1375 args[i++] = "--sysv-console=0";
1378 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1381 args[i++] = "--deserialize";
1386 assert(i <= ELEMENTSOF(args));
1388 execv(args[0], (char* const*) args);
1390 log_error("Failed to reexecute: %m");
1394 fclose(serialization);
1399 if (shutdown_verb) {
1400 const char * command_line[] = {
1401 SYSTEMD_SHUTDOWN_BINARY_PATH,
1406 execv(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line);
1407 log_error("Failed to execute shutdown binary, freezing: %m");