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>
37 #include "mount-setup.h"
38 #include "hostname-setup.h"
39 #include "loopback-setup.h"
40 #include "kmod-setup.h"
41 #include "locale-setup.h"
42 #include "load-fragment.h"
45 #include "conf-parser.h"
46 #include "bus-errors.h"
55 ACTION_DUMP_CONFIGURATION_ITEMS,
57 } arg_action = ACTION_RUN;
59 static char *arg_default_unit = NULL;
60 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
62 static bool arg_dump_core = true;
63 static bool arg_crash_shell = false;
64 static int arg_crash_chvt = -1;
65 static bool arg_confirm_spawn = false;
66 static bool arg_show_status = true;
67 #ifdef HAVE_SYSV_COMPAT
68 static bool arg_sysv_console = true;
70 static bool arg_mount_auto = true;
71 static bool arg_swap_auto = true;
72 static char *arg_console = NULL;
74 static FILE* serialization = NULL;
76 _noreturn_ static void freeze(void) {
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 make_null_stdio(void) {
191 if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) {
192 log_error("Failed to open /dev/null: %m");
196 if ((r = make_stdio(null_fd)) < 0)
197 log_warning("Failed to dup2() device: %s", strerror(-r));
202 static int console_setup(bool do_reset) {
205 /* If we are init, we connect stdin/stdout/stderr to /dev/null
206 * and make sure we don't have a controlling tty. */
213 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
214 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
218 if ((r = reset_terminal(tty_fd)) < 0)
219 log_error("Failed to reset /dev/console: %s", strerror(-r));
221 close_nointr_nofail(tty_fd);
225 static int set_default_unit(const char *u) {
230 if (!(c = strdup(u)))
233 free(arg_default_unit);
234 arg_default_unit = c;
238 static int parse_proc_cmdline_word(const char *word) {
240 static const char * const rlmap[] = {
241 "emergency", SPECIAL_EMERGENCY_TARGET,
242 "single", SPECIAL_RESCUE_TARGET,
243 "-s", SPECIAL_RESCUE_TARGET,
244 "s", SPECIAL_RESCUE_TARGET,
245 "S", SPECIAL_RESCUE_TARGET,
246 "1", SPECIAL_RESCUE_TARGET,
247 "2", SPECIAL_RUNLEVEL2_TARGET,
248 "3", SPECIAL_RUNLEVEL3_TARGET,
249 "4", SPECIAL_RUNLEVEL4_TARGET,
250 "5", SPECIAL_RUNLEVEL5_TARGET,
255 if (startswith(word, "systemd.unit="))
256 return set_default_unit(word + 13);
258 else if (startswith(word, "systemd.log_target=")) {
260 if (log_set_target_from_string(word + 19) < 0)
261 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
263 } else if (startswith(word, "systemd.log_level=")) {
265 if (log_set_max_level_from_string(word + 18) < 0)
266 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
268 } else if (startswith(word, "systemd.log_color=")) {
270 if (log_show_color_from_string(word + 18) < 0)
271 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
273 } else if (startswith(word, "systemd.log_location=")) {
275 if (log_show_location_from_string(word + 21) < 0)
276 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
278 } else if (startswith(word, "systemd.dump_core=")) {
281 if ((r = parse_boolean(word + 18)) < 0)
282 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
286 } else if (startswith(word, "systemd.crash_shell=")) {
289 if ((r = parse_boolean(word + 20)) < 0)
290 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
294 } else if (startswith(word, "systemd.confirm_spawn=")) {
297 if ((r = parse_boolean(word + 22)) < 0)
298 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
300 arg_confirm_spawn = r;
302 } else if (startswith(word, "systemd.crash_chvt=")) {
305 if (safe_atoi(word + 19, &k) < 0)
306 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
310 } else if (startswith(word, "systemd.show_status=")) {
313 if ((r = parse_boolean(word + 20)) < 0)
314 log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
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-org-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");
347 } else if (startswith(word, "console=")) {
355 /* Ignore the console setting if set to a VT */
357 !startswith(k, "tty") ||
358 k[3+strspn(k+3, "0123456789")] != 0) {
360 if (!(w = strndup(k, l)))
367 } else if (streq(word, "quiet")) {
368 arg_show_status = false;
369 #ifdef HAVE_SYSV_COMPAT
370 arg_sysv_console = false;
375 /* SysV compatibility */
376 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
377 if (streq(word, rlmap[i]))
378 return set_default_unit(rlmap[i+1]);
384 static int config_parse_level(
385 const char *filename,
397 log_set_max_level_from_string(rvalue);
401 static int config_parse_target(
402 const char *filename,
414 log_set_target_from_string(rvalue);
418 static int config_parse_color(
419 const char *filename,
431 log_show_color_from_string(rvalue);
435 static int config_parse_location(
436 const char *filename,
448 log_show_location_from_string(rvalue);
452 static int config_parse_cpu_affinity(
453 const char *filename,
471 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
476 if (!(t = strndup(w, l)))
479 r = safe_atou(t, &cpu);
483 if (!(c = cpu_set_malloc(&ncpus)))
486 if (r < 0 || cpu >= ncpus) {
487 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
492 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
496 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
497 log_warning("Failed to set CPU affinity: %m");
505 static int parse_config_file(void) {
507 const ConfigItem items[] = {
508 { "LogLevel", config_parse_level, NULL, "Manager" },
509 { "LogTarget", config_parse_target, NULL, "Manager" },
510 { "LogColor", config_parse_color, NULL, "Manager" },
511 { "LogLocation", config_parse_location, NULL, "Manager" },
512 { "DumpCore", config_parse_bool, &arg_dump_core, "Manager" },
513 { "CrashShell", config_parse_bool, &arg_crash_shell, "Manager" },
514 { "ShowStatus", config_parse_bool, &arg_show_status, "Manager" },
515 #ifdef HAVE_SYSV_COMPAT
516 { "SysVConsole", config_parse_bool, &arg_sysv_console, "Manager" },
518 { "CrashChVT", config_parse_int, &arg_crash_chvt, "Manager" },
519 { "CPUAffinity", config_parse_cpu_affinity, NULL, "Manager" },
520 { "MountAuto", config_parse_bool, &arg_mount_auto, "Manager" },
521 { "SwapAuto", config_parse_bool, &arg_swap_auto, "Manager" },
522 { NULL, NULL, NULL, NULL }
525 static const char * const sections[] = {
534 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : SESSION_CONFIG_FILE;
536 if (!(f = fopen(fn, "re"))) {
540 log_warning("Failed to open configuration file '%s': %m", fn);
544 if ((r = config_parse(fn, f, sections, items, false, NULL)) < 0)
545 log_warning("Failed to parse configuration file: %s", strerror(-r));
552 static int parse_proc_cmdline(void) {
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,
607 static const struct option options[] = {
608 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
609 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
610 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
611 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
612 { "unit", required_argument, NULL, ARG_UNIT },
613 { "system", no_argument, NULL, ARG_SYSTEM },
614 { "session", no_argument, NULL, ARG_SESSION },
615 { "test", no_argument, NULL, ARG_TEST },
616 { "help", no_argument, NULL, 'h' },
617 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
618 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
619 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
620 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
621 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
622 #ifdef HAVE_SYSV_COMPAT
623 { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
625 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
626 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
635 while ((c = getopt_long(argc, argv, "hD", options, NULL)) >= 0)
640 if ((r = log_set_max_level_from_string(optarg)) < 0) {
641 log_error("Failed to parse log level %s.", optarg);
649 if ((r = log_set_target_from_string(optarg)) < 0) {
650 log_error("Failed to parse log target %s.", optarg);
659 if ((r = log_show_color_from_string(optarg)) < 0) {
660 log_error("Failed to parse log color setting %s.", optarg);
664 log_show_color(true);
668 case ARG_LOG_LOCATION:
671 if ((r = log_show_location_from_string(optarg)) < 0) {
672 log_error("Failed to parse log location setting %s.", optarg);
676 log_show_location(true);
682 if ((r = set_default_unit(optarg)) < 0) {
683 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
690 arg_running_as = MANAGER_SYSTEM;
694 arg_running_as = MANAGER_SESSION;
698 arg_action = ACTION_TEST;
701 case ARG_DUMP_CONFIGURATION_ITEMS:
702 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
706 arg_dump_core = true;
709 case ARG_CRASH_SHELL:
710 arg_crash_shell = true;
713 case ARG_CONFIRM_SPAWN:
714 arg_confirm_spawn = true;
717 case ARG_SHOW_STATUS:
720 if ((r = parse_boolean(optarg)) < 0) {
721 log_error("Failed to show status boolean %s.", optarg);
726 arg_show_status = true;
728 #ifdef HAVE_SYSV_COMPAT
729 case ARG_SYSV_CONSOLE:
732 if ((r = parse_boolean(optarg)) < 0) {
733 log_error("Failed to SysV console boolean %s.", optarg);
736 arg_sysv_console = r;
738 arg_sysv_console = true;
742 case ARG_DESERIALIZE: {
746 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
747 log_error("Failed to parse deserialize option %s.", optarg);
751 if (!(f = fdopen(fd, "r"))) {
752 log_error("Failed to open serialization fd: %m");
757 fclose(serialization);
764 case ARG_INTROSPECT: {
765 const char * const * i = NULL;
767 for (i = bus_interface_table; *i; i += 2)
768 if (!optarg || streq(i[0], optarg)) {
769 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
772 fputs("</node>\n", stdout);
779 log_error("Unknown interface %s.", optarg);
781 arg_action = ACTION_DONE;
786 arg_action = ACTION_HELP;
790 log_set_max_level(LOG_DEBUG);
797 log_error("Unknown option code %c", c);
801 /* PID 1 will get the kernel arguments as parameters, which we
802 * ignore and unconditionally read from
803 * /proc/cmdline. However, we need to ignore those arguments
805 if (arg_running_as != MANAGER_SYSTEM && optind < argc) {
806 log_error("Excess arguments.");
813 static int help(void) {
815 printf("%s [OPTIONS...]\n\n"
816 "Starts up and maintains the system or a session.\n\n"
817 " -h --help Show this help\n"
818 " --test Determine startup sequence, dump it and exit\n"
819 " --dump-configuration-items Dump understood unit configuration items\n"
820 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
821 " --unit=UNIT Set default unit\n"
822 " --system Run a system instance, even if PID != 1\n"
823 " --session Run a session instance\n"
824 " --dump-core Dump core on crash\n"
825 " --crash-shell Run shell on crash\n"
826 " --confirm-spawn Ask for confirmation when spawning processes\n"
827 " --show-status[=0|1] Show status updates on the console during bootup\n"
828 #ifdef HAVE_SYSV_COMPAT
829 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
831 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
832 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
833 " --log-color[=0|1] Highlight important log messages\n"
834 " --log-location[=0|1] Include code location in log messages\n",
835 program_invocation_short_name);
840 static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
849 if ((r = manager_open_serialization(m, &f)) < 0) {
850 log_error("Failed to create serialization faile: %s", strerror(-r));
854 if (!(fds = fdset_new())) {
856 log_error("Failed to allocate fd set: %s", strerror(-r));
860 if ((r = manager_serialize(m, f, fds)) < 0) {
861 log_error("Failed to serialize state: %s", strerror(-r));
865 if (fseeko(f, 0, SEEK_SET) < 0) {
866 log_error("Failed to rewind serialization fd: %m");
870 if ((r = fd_cloexec(fileno(f), false)) < 0) {
871 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
875 if ((r = fdset_cloexec(fds, false)) < 0) {
876 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
894 int main(int argc, char *argv[]) {
896 int r, retval = EXIT_FAILURE;
898 bool reexecute = false;
900 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
901 /* This is compatbility support for SysV, where
902 * calling init as a user is identical to telinit. */
905 execv(SYSTEMCTL_BINARY_PATH, argv);
906 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
910 log_show_color(isatty(STDERR_FILENO) > 0);
911 log_show_location(false);
912 log_set_max_level(LOG_INFO);
915 arg_running_as = MANAGER_SYSTEM;
916 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
918 if (label_init() < 0)
921 arg_running_as = MANAGER_SESSION;
922 log_set_target(LOG_TARGET_CONSOLE);
925 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
928 /* Mount /proc, /sys and friends, so that /proc/cmdline and
929 * /proc/$PID/fd is available. */
930 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
931 if (mount_setup() < 0)
934 /* Reset all signal handlers. */
935 assert_se(reset_all_signal_handlers() == 0);
937 /* If we are init, we can block sigkill. Yay. */
938 ignore_signals(SIGNALS_IGNORE, -1);
940 if (parse_config_file() < 0)
943 if (arg_running_as == MANAGER_SYSTEM)
944 if (parse_proc_cmdline() < 0)
947 log_parse_environment();
949 if (parse_argv(argc, argv) < 0)
952 if (arg_action == ACTION_HELP) {
955 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
956 unit_dump_config_items(stdout);
957 retval = EXIT_SUCCESS;
959 } else if (arg_action == ACTION_DONE) {
960 retval = EXIT_SUCCESS;
964 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
966 /* Remember open file descriptors for later deserialization */
968 if ((r = fdset_new_fill(&fds)) < 0) {
969 log_error("Failed to allocate fd set: %s", strerror(-r));
973 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
975 close_all_fds(NULL, 0);
977 /* Set up PATH unless it is already set */
979 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
980 arg_running_as == MANAGER_SYSTEM);
982 /* Move out of the way, so that we won't block unmounts */
983 assert_se(chdir("/") == 0);
985 if (arg_running_as == MANAGER_SYSTEM) {
986 /* Become a session leader if we aren't one yet. */
989 /* Disable the umask logic */
993 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
994 dbus_connection_set_change_sigpipe(FALSE);
996 /* Reset the console, but only if this is really init and we
997 * are freshly booted */
998 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
999 console_setup(getpid() == 1 && !serialization);
1003 /* Open the logging devices, if possible and necessary */
1006 /* Make sure we leave a core dump without panicing the
1009 install_crash_handler();
1011 log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
1012 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
1014 if (arg_running_as == MANAGER_SYSTEM && !serialization) {
1017 if (arg_show_status)
1024 mkdir_p("/dev/.systemd/ask-password/", 0755);
1027 if ((r = manager_new(arg_running_as, &m)) < 0) {
1028 log_error("Failed to allocate manager object: %s", strerror(-r));
1032 m->confirm_spawn = arg_confirm_spawn;
1033 m->show_status = arg_show_status;
1034 #ifdef HAVE_SYSV_COMPAT
1035 m->sysv_console = arg_sysv_console;
1037 m->mount_auto = arg_mount_auto;
1038 m->swap_auto = arg_swap_auto;
1041 manager_set_console(m, arg_console);
1043 if ((r = manager_startup(m, serialization, fds)) < 0)
1044 log_error("Failed to fully start up daemon: %s", strerror(-r));
1047 /* This will close all file descriptors that were opened, but
1048 * not claimed by any unit. */
1054 if (serialization) {
1055 fclose(serialization);
1056 serialization = NULL;
1059 Unit *target = NULL;
1061 dbus_error_init(&error);
1063 log_debug("Activating default unit: %s", arg_default_unit);
1065 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1066 log_error("Failed to load default target: %s", bus_error(&error, r));
1067 dbus_error_free(&error);
1068 } else if (target->meta.load_state != UNIT_LOADED)
1069 log_error("Failed to load default target: %s", strerror(-target->meta.load_error));
1071 if (!target || target->meta.load_state != UNIT_LOADED) {
1072 log_info("Trying to load rescue target...");
1074 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1075 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1076 dbus_error_free(&error);
1078 } else if (target->meta.load_state != UNIT_LOADED) {
1079 log_error("Failed to load rescue target: %s", strerror(-target->meta.load_error));
1084 if (arg_action == ACTION_TEST) {
1085 printf("-> By units:\n");
1086 manager_dump_units(m, stdout, "\t");
1089 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
1090 log_error("Failed to start default target: %s", bus_error(&error, r));
1091 dbus_error_free(&error);
1095 if (arg_action == ACTION_TEST) {
1096 printf("-> By jobs:\n");
1097 manager_dump_jobs(m, stdout, "\t");
1098 retval = EXIT_SUCCESS;
1104 if ((r = manager_loop(m)) < 0) {
1105 log_error("Failed to run mainloop: %s", strerror(-r));
1109 switch (m->exit_code) {
1112 retval = EXIT_SUCCESS;
1116 case MANAGER_RELOAD:
1117 log_info("Reloading.");
1118 if ((r = manager_reload(m)) < 0)
1119 log_error("Failed to reload: %s", strerror(-r));
1122 case MANAGER_REEXECUTE:
1123 if (prepare_reexecute(m, &serialization, &fds) < 0)
1127 log_notice("Reexecuting.");
1131 assert_not_reached("Unknown exit code.");
1139 free(arg_default_unit);
1147 const char *args[15];
1151 assert(serialization);
1154 args[i++] = SYSTEMD_BINARY_PATH;
1156 args[i++] = "--log-level";
1157 args[i++] = log_level_to_string(log_get_max_level());
1159 args[i++] = "--log-target";
1160 args[i++] = log_target_to_string(log_get_target());
1162 if (arg_running_as == MANAGER_SYSTEM)
1163 args[i++] = "--system";
1165 args[i++] = "--session";
1168 args[i++] = "--dump-core";
1170 if (arg_crash_shell)
1171 args[i++] = "--crash-shell";
1173 if (arg_confirm_spawn)
1174 args[i++] = "--confirm-spawn";
1176 if (arg_show_status)
1177 args[i++] = "--show-status=1";
1179 args[i++] = "--show-status=0";
1181 #ifdef HAVE_SYSV_COMPAT
1182 if (arg_sysv_console)
1183 args[i++] = "--sysv-console=1";
1185 args[i++] = "--sysv-console=0";
1188 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1191 args[i++] = "--deserialize";
1196 assert(i <= ELEMENTSOF(args));
1198 execv(args[0], (char* const*) args);
1200 log_error("Failed to reexecute: %m");
1204 fclose(serialization);