X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fmain.c;h=458fdca55e86f9b465ef8bd6347b95067fdb19ee;hb=bfba3256a02a0871579c4ee48d787dfe4585fd8d;hp=7c66665e843957f931149bc12e166467f40ffae4;hpb=af6da548aa14c57da7f17b3a1f2211efdb811d19;p=elogind.git diff --git a/src/core/main.c b/src/core/main.c index 7c66665e8..458fdca55 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -51,6 +51,7 @@ #include "path-util.h" #include "switch-root.h" #include "capability.h" +#include "killall.h" #include "mount-setup.h" #include "loopback-setup.h" @@ -65,6 +66,7 @@ static enum { ACTION_RUN, ACTION_HELP, + ACTION_VERSION, ACTION_TEST, ACTION_DUMP_CONFIGURATION_ITEMS, ACTION_DONE @@ -164,19 +166,14 @@ _noreturn_ static void crash(int sig) { sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART; assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); - if ((pid = fork()) < 0) - log_error("Failed to fork off crash shell: %s", strerror(errno)); + pid = fork(); + if (pid < 0) + log_error("Failed to fork off crash shell: %m"); else if (pid == 0) { - int fd, r; - - if ((fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1)) < 0) - log_error("Failed to acquire terminal: %s", strerror(-fd)); - else if ((r = make_stdio(fd)) < 0) - log_error("Failed to duplicate terminal fd: %s", strerror(-r)); - + make_console_stdio(); execl("/bin/sh", "/bin/sh", NULL); - log_error("execl() failed: %s", strerror(errno)); + log_error("execl() failed: %m"); _exit(1); } @@ -353,12 +350,12 @@ static int parse_proc_cmdline_word(const char *word) { if (!eq) { r = unsetenv(cenv); if (r < 0) - log_warning("unsetenv failed %s. Ignoring.", strerror(errno)); + log_warning("unsetenv failed %m. Ignoring."); } else { *eq = 0; r = setenv(cenv, eq + 1, 1); if (r < 0) - log_warning("setenv failed %s. Ignoring.", strerror(errno)); + log_warning("setenv failed %m. Ignoring."); } free(cenv); @@ -498,14 +495,14 @@ static int config_parse_cpu_affinity2( unsigned cpu; if (!(t = strndup(w, l))) - return -ENOMEM; + return log_oom(); r = safe_atou(t, &cpu); free(t); if (!c) if (!(c = cpu_set_malloc(&ncpus))) - return -ENOMEM; + return log_oom(); if (r < 0 || cpu >= ncpus) { log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue); @@ -571,7 +568,7 @@ static int config_parse_join_controllers( s = strndup(w, length); if (!s) - return -ENOMEM; + return log_oom(); l = strv_split(s, ","); free(s); @@ -587,7 +584,7 @@ static int config_parse_join_controllers( arg_join_controllers = new(char**, 2); if (!arg_join_controllers) { strv_free(l); - return -ENOMEM; + return log_oom(); } arg_join_controllers[0] = l; @@ -601,7 +598,7 @@ static int config_parse_join_controllers( t = new0(char**, n+2); if (!t) { strv_free(l); - return -ENOMEM; + return log_oom(); } n = 0; @@ -615,7 +612,7 @@ static int config_parse_join_controllers( if (!c) { strv_free(l); strv_free_free(t); - return -ENOMEM; + return log_oom(); } strv_free(l); @@ -627,7 +624,7 @@ static int config_parse_join_controllers( if (!c) { strv_free(l); strv_free_free(t); - return -ENOMEM; + return log_oom(); } t[n++] = c; @@ -730,10 +727,13 @@ static int parse_proc_cmdline(void) { } r = parse_proc_cmdline_word(word); - free(word); - - if (r < 0) + if (r < 0) { + log_error("Failed on cmdline argument %s: %s", word, strerror(-r)); + free(word); goto finish; + } + + free(word); } r = 0; @@ -754,6 +754,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_SYSTEM, ARG_USER, ARG_TEST, + ARG_VERSION, ARG_DUMP_CONFIGURATION_ITEMS, ARG_DUMP_CORE, ARG_CRASH_SHELL, @@ -776,6 +777,7 @@ static int parse_argv(int argc, char *argv[]) { { "user", no_argument, NULL, ARG_USER }, { "test", no_argument, NULL, ARG_TEST }, { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, { "dump-core", optional_argument, NULL, ARG_DUMP_CORE }, { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL }, @@ -881,6 +883,10 @@ static int parse_argv(int argc, char *argv[]) { arg_action = ACTION_TEST; break; + case ARG_VERSION: + arg_action = ACTION_VERSION; + break; + case ARG_DUMP_CONFIGURATION_ITEMS: arg_action = ACTION_DUMP_CONFIGURATION_ITEMS; break; @@ -1014,8 +1020,10 @@ static int parse_argv(int argc, char *argv[]) { * instead. */ for (a = argv; a < argv + argc; a++) - if ((r = parse_proc_cmdline_word(*a)) < 0) + if ((r = parse_proc_cmdline_word(*a)) < 0) { + log_error("Failed on cmdline argument %s: %s", *a, strerror(-r)); return r; + } } return 0; @@ -1047,7 +1055,15 @@ static int help(void) { return 0; } -static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) { +static int version(void) { + puts(PACKAGE_STRING); + puts(DISTRIBUTION); + puts(SYSTEMD_FEATURES); + + return 0; +} + +static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool serialize_jobs) { FILE *f = NULL; FDSet *fds = NULL; int r; @@ -1059,18 +1075,21 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) { /* Make sure nothing is really destructed when we shut down */ m->n_reloading ++; - if ((r = manager_open_serialization(m, &f)) < 0) { + r = manager_open_serialization(m, &f); + if (r < 0) { log_error("Failed to create serialization file: %s", strerror(-r)); goto fail; } - if (!(fds = fdset_new())) { + fds = fdset_new(); + if (!fds) { r = -ENOMEM; log_error("Failed to allocate fd set: %s", strerror(-r)); goto fail; } - if ((r = manager_serialize(m, f, fds)) < 0) { + r = manager_serialize(m, f, fds, serialize_jobs); + if (r < 0) { log_error("Failed to serialize state: %s", strerror(-r)); goto fail; } @@ -1080,12 +1099,14 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) { goto fail; } - if ((r = fd_cloexec(fileno(f), false)) < 0) { + r = fd_cloexec(fileno(f), false); + if (r < 0) { log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r)); goto fail; } - if ((r = fdset_cloexec(fds, false)) < 0) { + r = fdset_cloexec(fds, false); + if (r < 0) { log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r)); goto fail; } @@ -1245,6 +1266,8 @@ int main(int argc, char *argv[]) { } arg_running_as = MANAGER_SYSTEM; + + make_null_stdio(); log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_JOURNAL : LOG_TARGET_JOURNAL_OR_KMSG); if (!skip_setup) { @@ -1277,16 +1300,20 @@ int main(int argc, char *argv[]) { } /* Initialize default unit */ - if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) + r = set_default_unit(SPECIAL_DEFAULT_TARGET); + if (r < 0) { + log_error("Failed to set default unit %s: %s", SPECIAL_DEFAULT_TARGET, strerror(-r)); goto finish; + } /* By default, mount "cpu" and "cpuacct" together */ - arg_join_controllers = new(char**, 2); + arg_join_controllers = new(char**, 3); if (!arg_join_controllers) goto finish; - arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL); - arg_join_controllers[1] = NULL; + arg_join_controllers[0] = strv_new("cpu", "cpuacct", "cpuset", NULL); + arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL); + arg_join_controllers[2] = NULL; if (!arg_join_controllers[0]) goto finish; @@ -1332,6 +1359,9 @@ int main(int argc, char *argv[]) { if (arg_action == ACTION_HELP) { retval = help(); goto finish; + } else if (arg_action == ACTION_VERSION) { + retval = version(); + goto finish; } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) { unit_dump_config_items(stdout); retval = EXIT_SUCCESS; @@ -1411,10 +1441,8 @@ int main(int argc, char *argv[]) { /* Reset the console, but only if this is really init and we * are freshly booted */ - if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) { + if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) console_setup(getpid() == 1 && !skip_setup); - make_null_stdio(); - } /* Open the logging devices, if possible and necessary */ log_open(); @@ -1430,8 +1458,20 @@ int main(int argc, char *argv[]) { goto finish; } - log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG, - PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as)); + if (arg_running_as == MANAGER_SYSTEM) { + const char *virtualization = NULL; + + log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")"); + + detect_virtualization(&virtualization); + if (virtualization) + log_info("Detected virtualization '%s'.", virtualization); + + if (in_initrd()) + log_info("Running in initial RAM disk."); + + } else + log_debug(PACKAGE_STRING " running in user mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")"); if (arg_running_as == MANAGER_SYSTEM && !skip_setup) { locale_setup(); @@ -1469,6 +1509,15 @@ int main(int argc, char *argv[]) { } } + if (arg_running_as == MANAGER_USER) { + /* Become reaper of our children */ + if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) { + log_warning("Failed to make us a subreaper: %m"); + if (errno == EINVAL) + log_info("Perhaps the kernel version is too old (< 3.4?)"); + } + } + r = manager_new(arg_running_as, &m); if (r < 0) { log_error("Failed to allocate manager object: %s", strerror(-r)); @@ -1598,7 +1647,7 @@ int main(int argc, char *argv[]) { case MANAGER_REEXECUTE: - if (prepare_reexecute(m, &serialization, &fds) < 0) + if (prepare_reexecute(m, &serialization, &fds, true) < 0) goto finish; reexecute = true; @@ -1612,7 +1661,7 @@ int main(int argc, char *argv[]) { m->switch_root = m->switch_root_init = NULL; if (!switch_root_init) - if (prepare_reexecute(m, &serialization, &fds) < 0) + if (prepare_reexecute(m, &serialization, &fds, false) < 0) goto finish; reexecute = true; @@ -1666,6 +1715,13 @@ finish: watchdog_close(true); if (switch_root_dir) { + /* Kill all remaining processes from the + * initrd, but don't wait for them, so that we + * can handle the SIGCHLD for them after + * deserializing. */ + broadcast_signal(SIGTERM, false); + + /* And switch root */ r = switch_root(switch_root_dir); if (r < 0) log_error("Failed to switch root, ignoring: %s", strerror(-r)); @@ -1717,6 +1773,9 @@ finish: fds = NULL; } + /* Reopen the console */ + make_console_stdio(); + for (j = 1, i = 1; j < argc; j++) args[i++] = argv[j]; args[i++] = NULL;