main.c revision e21fea24ae2a7a04f6d5c9d2bbbaf5833d248952
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2010 Lennart Poettering
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Lesser General Public License for more details.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic SystemdRunningAs arg_running_as = _SYSTEMD_RUNNING_AS_INVALID;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool arg_dump_core = true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool arg_crash_shell = false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool arg_confirm_spawn = false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool arg_show_status = true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool arg_switched_root = false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic char **arg_default_environment = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic struct rlimit *arg_default_rlimit[RLIMIT_NLIMITS] = {};
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic uint64_t arg_capability_bounding_set_drop = 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic nsec_t arg_timer_slack_nsec = (nsec_t) -1;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Pass this on immediately, if this is not PID 1 */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We want to wait for the core process, hence let's enable SIGCHLD */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else if (pid == 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Enable default signal handler for core dump */
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek /* Don't limit the core dump size */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Just to be sure... */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Raise the signal again */
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek assert_not_reached("We shouldn't be here...");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Order things nicely. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_info("Executing crash shell in 10s...");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Let the kernel reap children for us */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to fork off crash shell: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else if (pid == 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void install_crash_handler(void) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* If we are init, we connect stdin/stdout/stderr to /dev/null
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * and make sure we don't have a controlling tty. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We don't want to force text mode.
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * plymouth may be showing pictures already from initrd. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to reset /dev/console: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int set_default_unit(const char *u) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int parse_proc_cmdline_word(const char *word) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier static const char * const rlmap[] = {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "rd.systemd.unit=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.log_target=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (log_set_target_from_string(word + 19) < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to parse log target %s. Ignoring.", word + 19);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.log_level=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (log_set_max_level_from_string(word + 18) < 0)
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen log_warning("Failed to parse log level %s. Ignoring.", word + 18);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.log_color=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (log_show_color_from_string(word + 18) < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.log_location=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (log_show_location_from_string(word + 21) < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.dump_core=")) {
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.crash_shell=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.confirm_spawn=")) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (startswith(word, "systemd.crash_chvt=")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (startswith(word, "systemd.show_status=")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (startswith(word, "systemd.default_standard_output=")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = exec_output_from_string(word + 32)) < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (startswith(word, "systemd.default_standard_error=")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = exec_output_from_string(word + 31)) < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (startswith(word, "systemd.setenv=")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer env = strv_env_set(arg_default_environment, cenv);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Setting environment variable '%s' failed, ignoring: %m", cenv);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Environment variable name '%s' is not valid. Ignoring.", cenv);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (in_initrd() && startswith(word, "rd.systemd."))) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *c;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Ignore systemd.journald.xyz and friends */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Unknown kernel switch %s. Ignoring.", word);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.unit=UNIT Default unit to start\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "rd.systemd.unit=UNIT Default unit to start when run in initrd\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.dump_core=0|1 Dump core on crash\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.crash_shell=0|1 Run shell on crash\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.crash_chvt=N Change to VT #N on crash\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.show_status=0|1 Show status updates on the console during bootup\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer " Log target\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.log_level=LEVEL Log level\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.log_color=0|1 Highlight important log messages\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.log_location=0|1 Include code location in log messages\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer " Set default log output for services\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer " Set default log error output for services\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "systemd.setenv=ASSIGNMENT Set an environment variable for all spawned processes\n");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (!in_initrd()) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* SysV compatibility */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *filename, \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *section, \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *lvalue, \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *rvalue, \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_syntax(unit, LOG_ERR, filename, line, -r, \
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int config_parse_cpu_affinity2(const char *unit,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned ncpus = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!(t = strndup(w, l)))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_unit(unit, "Failed to set CPU affinity: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void strv_free_free(char ***l) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (i = l; *i; i++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void free_join_controllers(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int config_parse_join_controllers(const char *unit,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned n = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer FOREACH_WORD_QUOTED(w, length, rvalue, state) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *s, **l;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (a = arg_join_controllers; *a; a++) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int parse_config_file(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "LogLevel", config_parse_level2, 0, NULL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "LogTarget", config_parse_target, 0, NULL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "LogColor", config_parse_color, 0, NULL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "LogLocation", config_parse_location, 0, NULL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "ShowStatus", config_parse_bool, 0, &arg_show_status },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "CapabilityBoundingSet", config_parse_bounding_set, 0, &arg_capability_bounding_set_drop },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitCPU", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CPU]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitFSIZE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_FSIZE]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitDATA", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_DATA]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitSTACK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_STACK]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitCORE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CORE]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitRSS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RSS]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitNOFILE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NOFILE]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitAS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_AS]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitNPROC", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NPROC]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MEMLOCK]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitLOCKS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_LOCKS]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitSIGPENDING",config_parse_limit, 0, &arg_default_rlimit[RLIMIT_SIGPENDING]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MSGQUEUE]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitNICE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NICE]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitRTPRIO", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTPRIO]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "Manager", "DefaultLimitRTTIME", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTTIME]},
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *fn;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf";
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to open configuration file '%s': %m", fn);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = config_parse(NULL, fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, false, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse configuration file: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int parse_proc_cmdline(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Don't read /proc/cmdline if we are in a container, since
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * that is only relevant for the host system */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = read_one_line_file("/proc/cmdline", &line);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed on cmdline argument %s: %s", word, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "log-target", required_argument, NULL, ARG_LOG_TARGET },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "introspect", optional_argument, NULL, ARG_INTROSPECT },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer switch (c) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = log_set_max_level_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse log level %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = log_set_target_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse log target %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = log_show_color_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse log color setting %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = log_show_location_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse log location setting %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = exec_output_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse default standard output setting %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((r = exec_output_from_string(optarg)) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse default standard error output setting %s.", optarg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to parse dump core boolean %s.", optarg);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to parse crash shell boolean %s.", optarg);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to parse confirm spawn boolean %s.", optarg);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to parse show status boolean %s.", optarg);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0 || fd < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to parse deserialize option %s.", optarg);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r < 0 ? r : -EINVAL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to open serialization fd: %m");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer const char * const * i = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!i[0] && optarg)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Just to eat away the sysvinit kernel
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * cmdline args without getopt() error
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * messages that we'll parse in
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * parse_proc_cmdline_word() or ignore. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Hmm, when we aren't run as init system
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * let's complain about excess arguments */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* All /proc/cmdline arguments the kernel didn't
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * understand it passed to us. We're not really
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * interested in that usually since /proc/cmdline is
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * more interesting and complete. With one exception:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * if we are run in a container /proc/cmdline is not
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * relevant for the container, hence we rely on argv[]
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * instead. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if ((r = parse_proc_cmdline_word(*a)) < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int help(void) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer "Starts up and maintains the system or user services.\n\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " -h --help Show this help\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --test Determine startup sequence, dump it and exit\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --dump-configuration-items Dump understood unit configuration items\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --introspect[=INTERFACE] Extract D-Bus interface data\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --unit=UNIT Set default unit\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --system Run a system instance, even if PID != 1\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --user Run a user instance\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --dump-core[=0|1] Dump core on crash\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --crash-shell[=0|1] Run shell on crash\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --confirm-spawn[=0|1] Ask for confirmation when spawning processes\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --show-status[=0|1] Show status updates on the console during bootup\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --log-color[=0|1] Highlight important log messages\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --log-location[=0|1] Include code location in log messages\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --default-standard-output= Set default standard output for services\n"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer " --default-standard-error= Set default standard error output for services\n",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int version(void) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to create serialization file: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Make sure nothing is really destructed when we shut down */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to allocate fd set: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = manager_serialize(m, f, fds, switching_root);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to serialize state: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to rewind serialization fd: %m");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Save the original RLIMIT_NOFILE so that we can reset it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * later when transitioning from the initrd to the main
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * systemd or suchlike. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0) {
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer /* Make sure forked processes get the default kernel setting */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Bump up the resource limit for ourselves substantially */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Setting RLIMIT_NOFILE failed: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void test_mtab(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Check that /etc/mtab is a symlink */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "This is not supported anymore. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void test_usr(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Check that /usr is not a separate fs */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Some things will probably break (sometimes even silently) in mysterious ways. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void test_cgroups(void) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Systems without control groups are not supported. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "We will now sleep for 10s, and then continue boot-up. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Expect breakage and please do not file bugs. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Instead fix your kernel and enable CONFIG_CGROUPS. "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Consult http://0pointer.de/blog/projects/cgroups-vs-cgroups.html for more information.");
static int initialize_join_controllers(void) {
if (!arg_join_controllers)
return -ENOMEM;
return -ENOMEM;
bool reexecute = false;
bool skip_setup = false;
bool loaded_policy = false;
bool arm_reboot_watchdog = false;
bool queue_default_job = false;
#ifdef HAVE_SYSV_COMPAT
skip_setup = true;
skip_setup = false;
log_open();
if (in_initrd())
if (!skip_setup) {
goto finish;
if (ima_setup() < 0)
goto finish;
if (smack_setup() < 0)
goto finish;
goto finish;
if (!skip_setup) {
if (hwclock_is_localtime() > 0) {
int min;
} else if (!in_initrd()) {
log_open();
log_open();
goto finish;
r = initialize_join_controllers();
goto finish;
goto finish;
if (parse_config_file() < 0)
goto finish;
if (parse_proc_cmdline() < 0)
goto finish;
goto finish;
geteuid() == 0) {
goto finish;
sd_booted() <= 0) {
goto finish;
running_in_chroot() > 0) {
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
log_close();
goto finish;
if (serialization)
setsid();
umask(0);
log_open();
goto finish;
if (virtualization)
if (in_initrd())
#ifdef HAVE_KMOD
kmod_setup();
test_mtab();
test_usr();
test_cgroups();
goto finish;
goto finish;
goto finish;
if (serialization) {
if (queue_default_job) {
goto finish;
goto finish;
goto finish;
if (r == -EPERM) {
goto finish;
goto finish;
goto finish;
r = manager_loop(m);
goto finish;
switch (m->exit_code) {
case MANAGER_EXIT:
goto finish;
case MANAGER_RELOAD:
r = manager_reload(m);
case MANAGER_REEXECUTE:
goto finish;
reexecute = true;
goto finish;
case MANAGER_SWITCH_ROOT:
if (!switch_root_init)
goto finish;
reexecute = true;
goto finish;
case MANAGER_REBOOT:
case MANAGER_POWEROFF:
case MANAGER_HALT:
case MANAGER_KEXEC: {
goto finish;
manager_free(m);
for (j = 0; j < RLIMIT_NLIMITS; j++)
label_finish();
if (reexecute) {
const char **args;
unsigned i, args_size;
watchdog_close(true);
if (switch_root_dir) {
if (!switch_root_init) {
if (switch_root_dir)
if (serialization) {
if (fds) {
if (switch_root_init) {
if (serialization)
if (fds)
if (shutdown_verb) {
const char * command_line[] = {
char **env_block;
watchdog_close(false);
char_array_0(e);
watchdog_close(true);
freeze();
return retval;