2010-02-03 13:03:47 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2010-02-03 13:03:47 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2010-02-03 13:03:47 +01:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-02-03 13:03:47 +01:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
#include <errno.h>
|
2015-09-23 01:04:46 +02:00
|
|
|
#include <fcntl.h>
|
2010-04-06 23:40:24 +02:00
|
|
|
#include <getopt.h>
|
2010-04-10 21:40:40 +02:00
|
|
|
#include <signal.h>
|
2015-09-23 01:04:46 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-05-09 01:24:50 +02:00
|
|
|
#include <sys/mount.h>
|
2015-09-23 01:04:46 +02:00
|
|
|
#include <sys/prctl.h>
|
2015-09-23 23:13:06 +02:00
|
|
|
#include <sys/reboot.h>
|
2015-09-23 01:04:46 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2014-03-03 17:12:56 +01:00
|
|
|
#ifdef HAVE_SECCOMP
|
|
|
|
#include <seccomp.h>
|
|
|
|
#endif
|
2015-09-23 01:04:46 +02:00
|
|
|
#ifdef HAVE_VALGRIND_VALGRIND_H
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#endif
|
2013-11-20 22:11:10 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
#include "sd-bus.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "sd-daemon.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-02-21 02:28:54 +01:00
|
|
|
#include "architecture.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "build.h"
|
|
|
|
#include "bus-error.h"
|
|
|
|
#include "bus-util.h"
|
2015-10-26 23:32:16 +01:00
|
|
|
#include "capability-util.h"
|
2014-05-22 14:21:38 +02:00
|
|
|
#include "clock-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "conf-parser.h"
|
2015-09-30 21:50:22 +02:00
|
|
|
#include "cpu-set-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "dbus-manager.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "env-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "fdset.h"
|
2013-11-19 21:12:59 +01:00
|
|
|
#include "fileio.h"
|
2015-05-29 20:14:11 +02:00
|
|
|
#include "formats-util.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "hostname-setup.h"
|
|
|
|
#include "ima-setup.h"
|
|
|
|
#include "killall.h"
|
|
|
|
#include "kmod-setup.h"
|
2014-12-27 18:46:36 +01:00
|
|
|
#include "load-fragment.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "log.h"
|
2012-04-12 14:28:43 +02:00
|
|
|
#include "loopback-setup.h"
|
|
|
|
#include "machine-id-setup.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "manager.h"
|
|
|
|
#include "missing.h"
|
|
|
|
#include "mount-setup.h"
|
|
|
|
#include "pager.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-10-27 00:06:29 +01:00
|
|
|
#include "proc-cmdline.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "process-util.h"
|
2016-05-30 02:03:51 +02:00
|
|
|
#include "raw-clone.h"
|
2015-10-26 19:40:43 +01:00
|
|
|
#include "rlimit-util.h"
|
2012-04-12 14:28:43 +02:00
|
|
|
#include "selinux-setup.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "selinux-util.h"
|
|
|
|
#include "signal-util.h"
|
2013-03-07 20:06:58 +01:00
|
|
|
#include "smack-setup.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "special.h"
|
2015-10-26 22:01:44 +01:00
|
|
|
#include "stat-util.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "stdio-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "strv.h"
|
|
|
|
#include "switch-root.h"
|
|
|
|
#include "terminal-util.h"
|
2016-04-07 16:15:26 +02:00
|
|
|
#include "umask-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2015-09-23 01:04:46 +02:00
|
|
|
#include "virt.h"
|
|
|
|
#include "watchdog.h"
|
2012-04-12 14:28:43 +02:00
|
|
|
|
2010-04-06 23:40:24 +02:00
|
|
|
static enum {
|
|
|
|
ACTION_RUN,
|
2010-04-07 00:10:17 +02:00
|
|
|
ACTION_HELP,
|
2012-07-17 07:31:47 +02:00
|
|
|
ACTION_VERSION,
|
2010-04-10 17:53:17 +02:00
|
|
|
ACTION_TEST,
|
2016-08-01 12:38:25 +02:00
|
|
|
ACTION_DUMP_CONFIGURATION_ITEMS
|
2010-07-06 20:21:08 +02:00
|
|
|
} arg_action = ACTION_RUN;
|
|
|
|
static char *arg_default_unit = NULL;
|
2016-02-24 21:24:23 +01:00
|
|
|
static bool arg_system = false;
|
2010-07-06 20:21:08 +02:00
|
|
|
static bool arg_dump_core = true;
|
|
|
|
static int arg_crash_chvt = -1;
|
2015-09-23 23:13:06 +02:00
|
|
|
static bool arg_crash_shell = false;
|
|
|
|
static bool arg_crash_reboot = false;
|
2010-07-06 20:21:08 +02:00
|
|
|
static bool arg_confirm_spawn = false;
|
2014-02-17 16:17:08 +01:00
|
|
|
static ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
|
2012-05-22 02:35:22 +02:00
|
|
|
static bool arg_switched_root = false;
|
2016-02-19 19:25:13 +01:00
|
|
|
static bool arg_no_pager = false;
|
2011-08-23 00:37:35 +02:00
|
|
|
static char ***arg_join_controllers = NULL;
|
2012-01-05 23:54:45 +01:00
|
|
|
static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
|
2011-02-15 11:52:29 +01:00
|
|
|
static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
|
2013-11-04 17:47:43 +01:00
|
|
|
static usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC;
|
|
|
|
static usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC;
|
|
|
|
static usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
|
2013-11-08 16:01:22 +01:00
|
|
|
static usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL;
|
|
|
|
static unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST;
|
2012-04-05 22:08:10 +02:00
|
|
|
static usec_t arg_runtime_watchdog = 0;
|
|
|
|
static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
|
2013-06-09 07:08:46 +02:00
|
|
|
static char **arg_default_environment = NULL;
|
2014-03-05 02:29:58 +01:00
|
|
|
static struct rlimit *arg_default_rlimit[_RLIMIT_MAX] = {};
|
2016-01-07 23:00:04 +01:00
|
|
|
static uint64_t arg_capability_bounding_set = CAP_ALL;
|
2014-07-29 12:23:31 +02:00
|
|
|
static nsec_t arg_timer_slack_nsec = NSEC_INFINITY;
|
2014-03-24 16:22:34 +01:00
|
|
|
static usec_t arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE;
|
2014-02-13 01:35:27 +01:00
|
|
|
static Set* arg_syscall_archs = NULL;
|
|
|
|
static FILE* arg_serialization = NULL;
|
2014-02-24 23:50:10 +01:00
|
|
|
static bool arg_default_cpu_accounting = false;
|
2016-05-05 22:42:55 +02:00
|
|
|
static bool arg_default_io_accounting = false;
|
2014-02-24 23:50:10 +01:00
|
|
|
static bool arg_default_blockio_accounting = false;
|
|
|
|
static bool arg_default_memory_accounting = false;
|
2015-11-13 19:28:32 +01:00
|
|
|
static bool arg_default_tasks_accounting = true;
|
2016-07-19 17:29:00 +02:00
|
|
|
static uint64_t arg_default_tasks_max = UINT64_MAX;
|
2015-07-06 00:00:59 +02:00
|
|
|
static sd_id128_t arg_machine_id = {};
|
2010-04-10 22:35:37 +02:00
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
noreturn static void freeze_or_reboot(void) {
|
|
|
|
|
|
|
|
if (arg_crash_reboot) {
|
|
|
|
log_notice("Rebooting in 10s...");
|
|
|
|
(void) sleep(10);
|
|
|
|
|
|
|
|
log_notice("Rebooting now...");
|
|
|
|
(void) reboot(RB_AUTOBOOT);
|
|
|
|
log_emergency_errno(errno, "Failed to reboot: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
log_emergency("Freezing execution.");
|
|
|
|
freeze();
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:53:53 +01:00
|
|
|
noreturn static void crash(int sig) {
|
2015-09-30 17:08:04 +02:00
|
|
|
struct sigaction sa;
|
|
|
|
pid_t pid;
|
2010-04-10 21:40:40 +02:00
|
|
|
|
2013-06-27 02:28:12 +02:00
|
|
|
if (getpid() != 1)
|
|
|
|
/* Pass this on immediately, if this is not PID 1 */
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) raise(sig);
|
2013-06-27 02:28:12 +02:00
|
|
|
else if (!arg_dump_core)
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Caught <%s>, not dumping core.", signal_to_string(sig));
|
2010-04-10 21:40:40 +02:00
|
|
|
else {
|
2015-09-30 17:08:04 +02:00
|
|
|
sa = (struct sigaction) {
|
2015-09-23 01:32:44 +02:00
|
|
|
.sa_handler = nop_signal_handler,
|
2013-03-25 00:59:00 +01:00
|
|
|
.sa_flags = SA_NOCLDSTOP|SA_RESTART,
|
|
|
|
};
|
2010-04-10 21:40:40 +02:00
|
|
|
|
2010-04-13 20:26:54 +02:00
|
|
|
/* We want to wait for the core process, hence let's enable SIGCHLD */
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) sigaction(SIGCHLD, &sa, NULL);
|
2010-04-13 20:26:54 +02:00
|
|
|
|
2016-05-30 02:03:51 +02:00
|
|
|
pid = raw_clone(SIGCHLD);
|
2013-03-25 00:45:16 +01:00
|
|
|
if (pid < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_emergency_errno(errno, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
|
2010-04-10 21:40:40 +02:00
|
|
|
else if (pid == 0) {
|
|
|
|
/* Enable default signal handler for core dump */
|
2016-02-08 22:30:58 +01:00
|
|
|
|
2015-09-23 01:06:56 +02:00
|
|
|
sa = (struct sigaction) {
|
|
|
|
.sa_handler = SIG_DFL,
|
|
|
|
};
|
|
|
|
(void) sigaction(sig, &sa, NULL);
|
2010-04-10 21:40:40 +02:00
|
|
|
|
2016-02-08 22:30:58 +01:00
|
|
|
/* Don't limit the coredump size */
|
|
|
|
(void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY));
|
2010-04-10 21:40:40 +02:00
|
|
|
|
|
|
|
/* Just to be sure... */
|
2015-03-15 22:17:24 +01:00
|
|
|
(void) chdir("/");
|
2010-04-10 21:40:40 +02:00
|
|
|
|
|
|
|
/* Raise the signal again */
|
2014-12-17 05:53:23 +01:00
|
|
|
pid = raw_getpid();
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) kill(pid, sig); /* raise() would kill the parent */
|
2010-04-10 21:40:40 +02:00
|
|
|
|
|
|
|
assert_not_reached("We shouldn't be here...");
|
2015-09-23 01:06:56 +02:00
|
|
|
_exit(EXIT_FAILURE);
|
2010-04-10 22:35:37 +02:00
|
|
|
} else {
|
2010-09-15 14:48:59 +02:00
|
|
|
siginfo_t status;
|
|
|
|
int r;
|
2010-04-10 22:35:37 +02:00
|
|
|
|
|
|
|
/* Order things nicely. */
|
2013-03-25 00:45:16 +01:00
|
|
|
r = wait_for_terminate(pid, &status);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig));
|
2010-09-15 14:48:59 +02:00
|
|
|
else if (status.si_code != CLD_DUMPED)
|
2014-12-17 05:53:23 +01:00
|
|
|
log_emergency("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).",
|
|
|
|
signal_to_string(sig),
|
|
|
|
pid, sigchld_code_to_string(status.si_code),
|
|
|
|
status.si_status,
|
|
|
|
strna(status.si_code == CLD_EXITED
|
|
|
|
? exit_status_to_string(status.si_status, EXIT_STATUS_FULL)
|
|
|
|
: signal_to_string(status.si_status)));
|
2010-04-10 22:35:37 +02:00
|
|
|
else
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
|
2010-04-10 21:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
if (arg_crash_chvt >= 0)
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) chvt(arg_crash_chvt);
|
2010-04-10 23:36:43 +02:00
|
|
|
|
2015-09-30 17:08:04 +02:00
|
|
|
sa = (struct sigaction) {
|
|
|
|
.sa_handler = SIG_IGN,
|
|
|
|
.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Let the kernel reap children for us */
|
|
|
|
(void) sigaction(SIGCHLD, &sa, NULL);
|
2010-04-13 04:07:19 +02:00
|
|
|
|
2015-09-30 17:08:04 +02:00
|
|
|
if (arg_crash_shell) {
|
2015-09-23 23:13:06 +02:00
|
|
|
log_notice("Executing crash shell in 10s...");
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) sleep(10);
|
2010-04-10 22:35:37 +02:00
|
|
|
|
2016-05-30 02:03:51 +02:00
|
|
|
pid = raw_clone(SIGCHLD);
|
2012-07-10 19:19:59 +02:00
|
|
|
if (pid < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_emergency_errno(errno, "Failed to fork off crash shell: %m");
|
2010-04-13 20:26:54 +02:00
|
|
|
else if (pid == 0) {
|
2015-09-23 23:13:06 +02:00
|
|
|
(void) setsid();
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) make_console_stdio();
|
|
|
|
(void) execle("/bin/sh", "/bin/sh", NULL, environ);
|
2010-04-13 20:26:54 +02:00
|
|
|
|
2014-12-17 05:53:23 +01:00
|
|
|
log_emergency_errno(errno, "execle() failed: %m");
|
2015-09-23 01:06:56 +02:00
|
|
|
_exit(EXIT_FAILURE);
|
2015-09-23 23:13:06 +02:00
|
|
|
} else {
|
|
|
|
log_info("Spawned crash shell as PID "PID_FMT".", pid);
|
2015-09-30 15:12:19 +02:00
|
|
|
(void) wait_for_terminate(pid, NULL);
|
2015-09-23 23:13:06 +02:00
|
|
|
}
|
2010-04-10 22:35:37 +02:00
|
|
|
}
|
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
freeze_or_reboot();
|
2010-04-10 21:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void install_crash_handler(void) {
|
2015-01-27 01:47:37 +01:00
|
|
|
static const struct sigaction sa = {
|
2013-03-25 00:59:00 +01:00
|
|
|
.sa_handler = crash,
|
2015-01-27 01:47:37 +01:00
|
|
|
.sa_flags = SA_NODEFER, /* So that we can raise the signal again from the signal handler */
|
2013-03-25 00:59:00 +01:00
|
|
|
};
|
2015-01-27 01:47:37 +01:00
|
|
|
int r;
|
2010-04-10 21:40:40 +02:00
|
|
|
|
2015-01-27 01:47:37 +01:00
|
|
|
/* We ignore the return value here, since, we don't mind if we
|
|
|
|
* cannot set up a crash handler */
|
|
|
|
r = sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "I had trouble setting up the crash handler, ignoring: %m");
|
2010-04-10 21:40:40 +02:00
|
|
|
}
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2014-08-15 18:01:52 +02:00
|
|
|
static int console_setup(void) {
|
|
|
|
_cleanup_close_ int tty_fd = -1;
|
|
|
|
int r;
|
2010-04-13 02:06:27 +02:00
|
|
|
|
2012-01-29 21:55:51 +01:00
|
|
|
tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (tty_fd < 0)
|
|
|
|
return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
|
2010-04-13 02:06:27 +02:00
|
|
|
|
2014-08-15 18:01:52 +02:00
|
|
|
/* We don't want to force text mode. plymouth may be showing
|
|
|
|
* pictures already from initrd. */
|
2012-01-29 21:55:51 +01:00
|
|
|
r = reset_terminal_fd(tty_fd, false);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to reset /dev/console: %m");
|
2010-05-15 17:25:08 +02:00
|
|
|
|
2014-08-15 18:01:52 +02:00
|
|
|
return 0;
|
2010-04-13 02:06:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
static int parse_crash_chvt(const char *value) {
|
|
|
|
int b;
|
|
|
|
|
|
|
|
if (safe_atoi(value, &arg_crash_chvt) >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
b = parse_boolean(value);
|
|
|
|
if (b < 0)
|
|
|
|
return b;
|
|
|
|
|
|
|
|
if (b > 0)
|
|
|
|
arg_crash_chvt = 0; /* switch to where kmsg goes */
|
|
|
|
else
|
|
|
|
arg_crash_chvt = -1; /* turn off switching */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-06 00:00:59 +02:00
|
|
|
static int set_machine_id(const char *m) {
|
2016-07-21 20:22:42 +02:00
|
|
|
sd_id128_t t;
|
2016-04-26 20:26:15 +02:00
|
|
|
assert(m);
|
2015-07-06 00:00:59 +02:00
|
|
|
|
2016-07-21 20:22:42 +02:00
|
|
|
if (sd_id128_from_string(m, &t) < 0)
|
2015-07-06 00:00:59 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-07-21 20:22:42 +02:00
|
|
|
if (sd_id128_is_null(t))
|
2015-07-06 00:00:59 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-07-21 20:22:42 +02:00
|
|
|
arg_machine_id = t;
|
2015-07-06 00:00:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
static int parse_proc_cmdline_item(const char *key, const char *value) {
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
int r;
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
assert(key);
|
2010-08-17 03:29:46 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
if (streq(key, "systemd.unit") && value) {
|
2012-05-22 02:35:22 +02:00
|
|
|
|
|
|
|
if (!in_initrd())
|
2015-09-23 01:14:34 +02:00
|
|
|
return free_and_strdup(&arg_default_unit, value);
|
2012-05-22 02:35:22 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "rd.systemd.unit") && value) {
|
2012-05-22 02:35:22 +02:00
|
|
|
|
2014-06-19 16:14:56 +02:00
|
|
|
if (in_initrd())
|
2015-09-23 01:14:34 +02:00
|
|
|
return free_and_strdup(&arg_default_unit, value);
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.dump_core") && value) {
|
2010-04-10 22:35:37 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = parse_boolean(value);
|
2014-02-16 00:10:36 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse dump core switch %s. Ignoring.", value);
|
2010-04-10 22:35:37 +02:00
|
|
|
else
|
2010-07-06 20:21:08 +02:00
|
|
|
arg_dump_core = r;
|
2010-04-10 22:35:37 +02:00
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
} else if (streq(key, "systemd.crash_chvt") && value) {
|
|
|
|
|
|
|
|
if (parse_crash_chvt(value) < 0)
|
|
|
|
log_warning("Failed to parse crash chvt switch %s. Ignoring.", value);
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.crash_shell") && value) {
|
2010-04-10 22:35:37 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = parse_boolean(value);
|
2014-02-16 00:10:36 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse crash shell switch %s. Ignoring.", value);
|
2010-04-10 22:35:37 +02:00
|
|
|
else
|
2010-07-06 20:21:08 +02:00
|
|
|
arg_crash_shell = r;
|
2010-04-13 19:10:01 +02:00
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
} else if (streq(key, "systemd.crash_reboot") && value) {
|
2010-04-13 19:10:01 +02:00
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
r = parse_boolean(value);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning("Failed to parse crash reboot switch %s. Ignoring.", value);
|
2010-04-13 19:10:01 +02:00
|
|
|
else
|
2015-09-23 23:13:06 +02:00
|
|
|
arg_crash_reboot = r;
|
2010-04-13 19:10:01 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.confirm_spawn") && value) {
|
2010-04-10 23:36:43 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = parse_boolean(value);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning("Failed to parse confirm spawn switch %s. Ignoring.", value);
|
2010-04-10 23:36:43 +02:00
|
|
|
else
|
2014-03-06 17:05:55 +01:00
|
|
|
arg_confirm_spawn = r;
|
2010-04-10 23:36:43 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.show_status") && value) {
|
2010-07-07 00:00:59 +02:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = parse_show_status(value, &arg_show_status);
|
2014-01-28 04:27:07 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse show status switch %s. Ignoring.", value);
|
|
|
|
|
|
|
|
} else if (streq(key, "systemd.default_standard_output") && value) {
|
2011-02-15 11:52:29 +01:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = exec_output_from_string(value);
|
2014-02-16 00:10:36 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse default standard output switch %s. Ignoring.", value);
|
2011-02-15 11:52:29 +01:00
|
|
|
else
|
|
|
|
arg_default_std_output = r;
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.default_standard_error") && value) {
|
|
|
|
|
|
|
|
r = exec_output_from_string(value);
|
2014-02-16 00:10:36 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse default standard error switch %s. Ignoring.", value);
|
2011-02-15 11:52:29 +01:00
|
|
|
else
|
|
|
|
arg_default_std_error = r;
|
2012-02-07 21:31:20 +01:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "systemd.setenv") && value) {
|
|
|
|
|
|
|
|
if (env_assignment_is_valid(value)) {
|
2013-07-26 05:22:22 +02:00
|
|
|
char **env;
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
env = strv_env_set(arg_default_environment, value);
|
2013-07-26 05:22:22 +02:00
|
|
|
if (env)
|
|
|
|
arg_default_environment = env;
|
|
|
|
else
|
2014-11-28 14:45:55 +01:00
|
|
|
log_warning_errno(ENOMEM, "Setting environment variable '%s' failed, ignoring: %m", value);
|
2013-07-26 05:22:22 +02:00
|
|
|
} else
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
|
2010-07-07 00:00:59 +02:00
|
|
|
|
2015-07-06 00:00:59 +02:00
|
|
|
} else if (streq(key, "systemd.machine_id") && value) {
|
|
|
|
|
|
|
|
r = set_machine_id(value);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning("MachineID '%s' is not valid. Ignoring.", value);
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
} else if (streq(key, "quiet") && !value) {
|
2014-06-17 01:05:39 +02:00
|
|
|
|
2014-02-17 16:17:08 +01:00
|
|
|
if (arg_show_status == _SHOW_STATUS_UNSET)
|
2014-01-28 04:27:07 +01:00
|
|
|
arg_show_status = SHOW_STATUS_AUTO;
|
2014-03-06 17:05:55 +01:00
|
|
|
|
|
|
|
} else if (streq(key, "debug") && !value) {
|
2014-06-17 01:05:39 +02:00
|
|
|
|
2014-08-15 18:07:36 +02:00
|
|
|
/* Note that log_parse_environment() handles 'debug'
|
|
|
|
* too, and sets the log level to LOG_DEBUG. */
|
2014-06-17 01:05:39 +02:00
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
if (detect_container() > 0)
|
2014-04-05 19:59:01 +02:00
|
|
|
log_set_target(LOG_TARGET_CONSOLE);
|
2014-03-06 17:05:55 +01:00
|
|
|
|
2016-06-13 16:28:42 +02:00
|
|
|
} else if (!value) {
|
2015-11-03 12:24:52 +01:00
|
|
|
const char *target;
|
2010-04-06 23:40:24 +02:00
|
|
|
|
|
|
|
/* SysV compatibility */
|
2015-11-03 12:24:52 +01:00
|
|
|
target = runlevel_to_target(key);
|
|
|
|
if (target)
|
|
|
|
return free_and_strdup(&arg_default_unit, target);
|
2016-04-26 17:10:36 +02:00
|
|
|
|
|
|
|
} else if (streq(key, "systemd.default_timeout_start_sec") && value) {
|
|
|
|
|
|
|
|
r = parse_sec(value, &arg_default_timeout_start_usec);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to parse default start timeout: %s, ignoring.", value);
|
|
|
|
|
|
|
|
if (arg_default_timeout_start_usec <= 0)
|
|
|
|
arg_default_timeout_start_usec = USEC_INFINITY;
|
2010-04-06 23:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-16 04:25:58 +02:00
|
|
|
#define DEFINE_SETTER(name, func, descr) \
|
|
|
|
static int name(const char *unit, \
|
|
|
|
const char *filename, \
|
|
|
|
unsigned line, \
|
|
|
|
const char *section, \
|
2013-11-19 16:17:55 +01:00
|
|
|
unsigned section_line, \
|
2013-04-16 04:25:58 +02:00
|
|
|
const char *lvalue, \
|
|
|
|
int ltype, \
|
|
|
|
const char *rvalue, \
|
|
|
|
void *data, \
|
|
|
|
void *userdata) { \
|
|
|
|
\
|
|
|
|
int r; \
|
|
|
|
\
|
|
|
|
assert(filename); \
|
|
|
|
assert(lvalue); \
|
|
|
|
assert(rvalue); \
|
|
|
|
\
|
|
|
|
r = func(rvalue); \
|
|
|
|
if (r < 0) \
|
2015-09-23 01:10:47 +02:00
|
|
|
log_syntax(unit, LOG_ERR, filename, line, r, \
|
|
|
|
"Invalid " descr "'%s': %m", \
|
|
|
|
rvalue); \
|
2013-04-16 04:25:58 +02:00
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
}
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2013-04-16 04:25:58 +02:00
|
|
|
DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
|
|
|
|
DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
|
|
|
|
DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
|
|
|
|
DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2014-03-03 21:23:12 +01:00
|
|
|
static int config_parse_cpu_affinity2(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2015-08-31 05:46:27 +02:00
|
|
|
_cleanup_cpu_free_ cpu_set_t *c = NULL;
|
2015-09-25 02:58:49 +02:00
|
|
|
int ncpus;
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2015-09-30 20:16:51 +02:00
|
|
|
ncpus = parse_cpu_set_and_warn(rvalue, &c, unit, filename, line, lvalue);
|
2015-09-25 02:58:49 +02:00
|
|
|
if (ncpus < 0)
|
|
|
|
return ncpus;
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2015-09-25 02:58:49 +02:00
|
|
|
if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
|
|
|
|
log_warning("Failed to set CPU affinity: %m");
|
2010-07-07 01:10:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:23:12 +01:00
|
|
|
static int config_parse_show_status(
|
|
|
|
const char* unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
int k;
|
|
|
|
ShowStatus *b = data;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
k = parse_show_status(rvalue, b);
|
|
|
|
if (k < 0) {
|
2015-09-30 18:22:42 +02:00
|
|
|
log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
|
2014-03-03 21:23:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
static int config_parse_crash_chvt(
|
|
|
|
const char* unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
r = parse_crash_chvt(rvalue);
|
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-16 04:25:58 +02:00
|
|
|
static int config_parse_join_controllers(const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
2013-11-19 16:17:55 +01:00
|
|
|
unsigned section_line,
|
2013-04-16 04:25:58 +02:00
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2015-09-01 17:30:26 +02:00
|
|
|
const char *whole_rvalue = rvalue;
|
2011-08-23 00:37:35 +02:00
|
|
|
unsigned n = 0;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
2015-09-23 01:01:26 +02:00
|
|
|
arg_join_controllers = strv_free_free(arg_join_controllers);
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2015-08-31 06:00:30 +02:00
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *word = NULL;
|
|
|
|
char **l;
|
|
|
|
int r;
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2015-08-31 06:00:30 +02:00
|
|
|
r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
|
2015-09-01 17:30:26 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
|
2015-08-31 06:00:30 +02:00
|
|
|
return r;
|
2015-09-01 17:30:26 +02:00
|
|
|
}
|
2015-08-31 06:00:30 +02:00
|
|
|
if (r == 0)
|
|
|
|
break;
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2015-08-31 06:00:30 +02:00
|
|
|
l = strv_split(word, ",");
|
2015-09-01 17:31:34 +02:00
|
|
|
if (!l)
|
2015-09-23 01:11:08 +02:00
|
|
|
return log_oom();
|
2011-08-23 00:37:35 +02:00
|
|
|
strv_uniq(l);
|
|
|
|
|
|
|
|
if (strv_length(l) <= 1) {
|
|
|
|
strv_free(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg_join_controllers) {
|
|
|
|
arg_join_controllers = new(char**, 2);
|
|
|
|
if (!arg_join_controllers) {
|
|
|
|
strv_free(l);
|
2012-08-04 02:22:09 +02:00
|
|
|
return log_oom();
|
2011-08-23 00:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
arg_join_controllers[0] = l;
|
|
|
|
arg_join_controllers[1] = NULL;
|
|
|
|
|
|
|
|
n = 1;
|
|
|
|
} else {
|
|
|
|
char ***a;
|
|
|
|
char ***t;
|
|
|
|
|
|
|
|
t = new0(char**, n+2);
|
|
|
|
if (!t) {
|
|
|
|
strv_free(l);
|
2012-08-04 02:22:09 +02:00
|
|
|
return log_oom();
|
2011-08-23 00:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (a = arg_join_controllers; *a; a++) {
|
|
|
|
|
|
|
|
if (strv_overlap(*a, l)) {
|
2015-10-07 11:26:10 +02:00
|
|
|
if (strv_extend_strv(&l, *a, false) < 0) {
|
2011-08-23 00:37:35 +02:00
|
|
|
strv_free(l);
|
|
|
|
strv_free_free(t);
|
2012-08-04 02:22:09 +02:00
|
|
|
return log_oom();
|
2011-08-23 00:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
char **c;
|
|
|
|
|
|
|
|
c = strv_copy(*a);
|
|
|
|
if (!c) {
|
|
|
|
strv_free(l);
|
|
|
|
strv_free_free(t);
|
2012-08-04 02:22:09 +02:00
|
|
|
return log_oom();
|
2011-08-23 00:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t[n++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t[n++] = strv_uniq(l);
|
|
|
|
|
|
|
|
strv_free_free(arg_join_controllers);
|
|
|
|
arg_join_controllers = t;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 06:00:30 +02:00
|
|
|
if (!isempty(rvalue))
|
2015-09-30 18:22:42 +02:00
|
|
|
log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
|
2011-08-23 00:37:35 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-07 01:10:27 +02:00
|
|
|
static int parse_config_file(void) {
|
|
|
|
|
2011-08-01 00:43:05 +02:00
|
|
|
const ConfigTableItem items[] = {
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "LogLevel", config_parse_level2, 0, NULL },
|
|
|
|
{ "Manager", "LogTarget", config_parse_target, 0, NULL },
|
|
|
|
{ "Manager", "LogColor", config_parse_color, 0, NULL },
|
|
|
|
{ "Manager", "LogLocation", config_parse_location, 0, NULL },
|
|
|
|
{ "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
|
2015-09-23 23:13:06 +02:00
|
|
|
{ "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, NULL },
|
|
|
|
{ "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, NULL },
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
|
2015-09-23 23:13:06 +02:00
|
|
|
{ "Manager", "CrashReboot", config_parse_bool, 0, &arg_crash_reboot },
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status },
|
|
|
|
{ "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
|
|
|
|
{ "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
|
|
|
|
{ "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
|
|
|
|
{ "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
|
2016-01-07 23:00:04 +01:00
|
|
|
{ "Manager", "CapabilityBoundingSet", config_parse_capability_set, 0, &arg_capability_bounding_set },
|
2014-02-13 02:25:45 +01:00
|
|
|
#ifdef HAVE_SECCOMP
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs },
|
2014-02-13 02:25:45 +01:00
|
|
|
#endif
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
|
2014-03-24 16:22:34 +01:00
|
|
|
{ "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec },
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
|
|
|
|
{ "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
|
|
|
|
{ "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec },
|
|
|
|
{ "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec },
|
|
|
|
{ "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec },
|
2016-04-26 20:46:20 +02:00
|
|
|
{ "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval }, /* obsolete alias */
|
|
|
|
{ "Manager", "DefaultStartLimitIntervalSec",config_parse_sec, 0, &arg_default_start_limit_interval },
|
2014-02-13 01:35:27 +01:00
|
|
|
{ "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst },
|
|
|
|
{ "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
|
2016-02-01 21:07:09 +01:00
|
|
|
{ "Manager", "DefaultLimitCPU", config_parse_limit, RLIMIT_CPU, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitFSIZE", config_parse_limit, RLIMIT_FSIZE, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitDATA", config_parse_limit, RLIMIT_DATA, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitSTACK", config_parse_limit, RLIMIT_STACK, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitCORE", config_parse_limit, RLIMIT_CORE, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitRSS", config_parse_limit, RLIMIT_RSS, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitNOFILE", config_parse_limit, RLIMIT_NOFILE, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitAS", config_parse_limit, RLIMIT_AS, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitNPROC", config_parse_limit, RLIMIT_NPROC, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitMEMLOCK", config_parse_limit, RLIMIT_MEMLOCK, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitLOCKS", config_parse_limit, RLIMIT_LOCKS, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitSIGPENDING", config_parse_limit, RLIMIT_SIGPENDING, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, RLIMIT_MSGQUEUE, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitNICE", config_parse_limit, RLIMIT_NICE, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitRTPRIO", config_parse_limit, RLIMIT_RTPRIO, arg_default_rlimit },
|
|
|
|
{ "Manager", "DefaultLimitRTTIME", config_parse_limit, RLIMIT_RTTIME, arg_default_rlimit },
|
2014-02-24 23:50:10 +01:00
|
|
|
{ "Manager", "DefaultCPUAccounting", config_parse_bool, 0, &arg_default_cpu_accounting },
|
2016-05-05 22:42:55 +02:00
|
|
|
{ "Manager", "DefaultIOAccounting", config_parse_bool, 0, &arg_default_io_accounting },
|
2014-02-24 23:50:10 +01:00
|
|
|
{ "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting },
|
|
|
|
{ "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting },
|
2015-09-10 12:32:16 +02:00
|
|
|
{ "Manager", "DefaultTasksAccounting", config_parse_bool, 0, &arg_default_tasks_accounting },
|
2015-11-13 17:13:55 +01:00
|
|
|
{ "Manager", "DefaultTasksMax", config_parse_tasks_max, 0, &arg_default_tasks_max },
|
2014-02-13 01:35:27 +01:00
|
|
|
{}
|
2010-07-07 01:10:27 +02:00
|
|
|
};
|
|
|
|
|
2014-11-29 10:06:04 +01:00
|
|
|
const char *fn, *conf_dirs_nulstr;
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
fn = arg_system ?
|
2015-11-10 15:57:21 +01:00
|
|
|
PKGSYSCONFDIR "/system.conf" :
|
|
|
|
PKGSYSCONFDIR "/user.conf";
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
conf_dirs_nulstr = arg_system ?
|
2015-11-10 15:57:21 +01:00
|
|
|
CONF_PATHS_NULSTR("systemd/system.conf.d") :
|
|
|
|
CONF_PATHS_NULSTR("systemd/user.conf.d");
|
|
|
|
|
core: rework unit timeout handling, and add new setting RuntimeMaxSec=
This clean-ups timeout handling in PID 1. Specifically, instead of storing 0 in internal timeout variables as
indication for a disabled timeout, use USEC_INFINITY which is in-line with how we do this in the rest of our code
(following the logic that 0 means "no", and USEC_INFINITY means "never").
This also replace all usec_t additions with invocations to usec_add(), so that USEC_INFINITY is properly propagated,
and sd-event considers it has indication for turning off the event source.
This also alters the deserialization of the units to restart timeouts from the time they were originally started from.
Before this patch timeouts would be restarted beginning with the time of the deserialization, which could lead to
artificially prolonged timeouts if a daemon reload took place.
Finally, a new RuntimeMaxSec= setting is introduced for service units, that specifies a maximum runtime after which a
specific service is forcibly terminated. This is useful to put time limits on time-intensive processing jobs.
This also simplifies the various xyz_spawn() calls of the various types in that explicit distruction of the timers is
removed, as that is done anyway by the state change handlers, and a state change is always done when the xyz_spawn()
calls fail.
Fixes: #2249
2016-02-01 21:48:10 +01:00
|
|
|
config_parse_many(fn, conf_dirs_nulstr, "Manager\0", config_item_table_lookup, items, false, NULL);
|
|
|
|
|
|
|
|
/* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we used USEC_INFINITY
|
|
|
|
* like everywhere else. */
|
|
|
|
if (arg_default_timeout_start_usec <= 0)
|
|
|
|
arg_default_timeout_start_usec = USEC_INFINITY;
|
|
|
|
if (arg_default_timeout_stop_usec <= 0)
|
|
|
|
arg_default_timeout_stop_usec = USEC_INFINITY;
|
2010-07-07 01:10:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:26:27 +02:00
|
|
|
static void manager_set_defaults(Manager *m) {
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec;
|
|
|
|
m->default_std_output = arg_default_std_output;
|
|
|
|
m->default_std_error = arg_default_std_error;
|
|
|
|
m->default_timeout_start_usec = arg_default_timeout_start_usec;
|
|
|
|
m->default_timeout_stop_usec = arg_default_timeout_stop_usec;
|
|
|
|
m->default_restart_usec = arg_default_restart_usec;
|
|
|
|
m->default_start_limit_interval = arg_default_start_limit_interval;
|
|
|
|
m->default_start_limit_burst = arg_default_start_limit_burst;
|
|
|
|
m->default_cpu_accounting = arg_default_cpu_accounting;
|
2016-05-05 22:42:55 +02:00
|
|
|
m->default_io_accounting = arg_default_io_accounting;
|
2015-06-29 11:26:27 +02:00
|
|
|
m->default_blockio_accounting = arg_default_blockio_accounting;
|
|
|
|
m->default_memory_accounting = arg_default_memory_accounting;
|
2015-09-10 12:32:16 +02:00
|
|
|
m->default_tasks_accounting = arg_default_tasks_accounting;
|
2015-11-13 17:13:55 +01:00
|
|
|
m->default_tasks_max = arg_default_tasks_max;
|
2015-06-29 11:26:27 +02:00
|
|
|
|
|
|
|
manager_set_default_rlimits(m, arg_default_rlimit);
|
|
|
|
manager_environment_add(m, NULL, arg_default_environment);
|
|
|
|
}
|
|
|
|
|
2010-04-06 23:40:24 +02:00
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_LOG_LEVEL = 0x100,
|
|
|
|
ARG_LOG_TARGET,
|
2010-06-17 22:52:55 +02:00
|
|
|
ARG_LOG_COLOR,
|
|
|
|
ARG_LOG_LOCATION,
|
2010-06-09 15:38:46 +02:00
|
|
|
ARG_UNIT,
|
2010-07-13 18:57:58 +02:00
|
|
|
ARG_SYSTEM,
|
2010-11-15 22:12:41 +01:00
|
|
|
ARG_USER,
|
2010-04-10 17:53:17 +02:00
|
|
|
ARG_TEST,
|
2014-01-07 06:00:05 +01:00
|
|
|
ARG_NO_PAGER,
|
2012-07-17 07:31:47 +02:00
|
|
|
ARG_VERSION,
|
2010-04-13 02:06:27 +02:00
|
|
|
ARG_DUMP_CONFIGURATION_ITEMS,
|
2010-07-07 00:00:59 +02:00
|
|
|
ARG_DUMP_CORE,
|
2015-09-23 23:13:06 +02:00
|
|
|
ARG_CRASH_CHVT,
|
2010-07-07 00:00:59 +02:00
|
|
|
ARG_CRASH_SHELL,
|
2015-09-23 23:13:06 +02:00
|
|
|
ARG_CRASH_REBOOT,
|
2010-04-21 03:27:44 +02:00
|
|
|
ARG_CONFIRM_SPAWN,
|
2010-07-07 00:00:59 +02:00
|
|
|
ARG_SHOW_STATUS,
|
2010-05-23 03:45:33 +02:00
|
|
|
ARG_DESERIALIZE,
|
2012-05-21 19:48:04 +02:00
|
|
|
ARG_SWITCHED_ROOT,
|
2011-02-15 11:52:29 +01:00
|
|
|
ARG_DEFAULT_STD_OUTPUT,
|
2015-07-06 00:00:59 +02:00
|
|
|
ARG_DEFAULT_STD_ERROR,
|
|
|
|
ARG_MACHINE_ID
|
2010-04-06 23:40:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2010-04-21 03:27:44 +02:00
|
|
|
{ "log-level", required_argument, NULL, ARG_LOG_LEVEL },
|
|
|
|
{ "log-target", required_argument, NULL, ARG_LOG_TARGET },
|
2010-06-17 22:52:55 +02:00
|
|
|
{ "log-color", optional_argument, NULL, ARG_LOG_COLOR },
|
|
|
|
{ "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
|
2010-06-09 15:38:46 +02:00
|
|
|
{ "unit", required_argument, NULL, ARG_UNIT },
|
2010-07-13 18:57:58 +02:00
|
|
|
{ "system", no_argument, NULL, ARG_SYSTEM },
|
2010-11-15 22:12:41 +01:00
|
|
|
{ "user", no_argument, NULL, ARG_USER },
|
2010-04-21 03:27:44 +02:00
|
|
|
{ "test", no_argument, NULL, ARG_TEST },
|
2014-01-07 06:00:05 +01:00
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2010-04-21 03:27:44 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-07-17 07:31:47 +02:00
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2010-04-21 03:27:44 +02:00
|
|
|
{ "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
|
2012-05-08 23:36:55 +02:00
|
|
|
{ "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
|
2015-09-23 23:13:06 +02:00
|
|
|
{ "crash-chvt", required_argument, NULL, ARG_CRASH_CHVT },
|
2012-05-08 23:36:55 +02:00
|
|
|
{ "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
|
2015-09-23 23:13:06 +02:00
|
|
|
{ "crash-reboot", optional_argument, NULL, ARG_CRASH_REBOOT },
|
2012-05-08 23:36:55 +02:00
|
|
|
{ "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
|
2010-08-09 18:00:24 +02:00
|
|
|
{ "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
|
2010-04-21 03:27:44 +02:00
|
|
|
{ "deserialize", required_argument, NULL, ARG_DESERIALIZE },
|
2012-05-21 19:48:04 +02:00
|
|
|
{ "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
|
2011-02-15 11:52:29 +01:00
|
|
|
{ "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
|
|
|
|
{ "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
|
2015-07-06 00:00:59 +02:00
|
|
|
{ "machine-id", required_argument, NULL, ARG_MACHINE_ID },
|
2014-02-16 00:10:36 +01:00
|
|
|
{}
|
2010-04-06 23:40:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c, r;
|
|
|
|
|
|
|
|
assert(argc >= 1);
|
|
|
|
assert(argv);
|
|
|
|
|
2011-03-14 04:08:12 +01:00
|
|
|
if (getpid() == 1)
|
|
|
|
opterr = 0;
|
|
|
|
|
2011-03-10 23:01:42 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
|
2010-04-06 23:40:24 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case ARG_LOG_LEVEL:
|
2014-02-16 00:10:36 +01:00
|
|
|
r = log_set_max_level_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2010-04-06 23:40:24 +02:00
|
|
|
log_error("Failed to parse log level %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_LOG_TARGET:
|
2014-02-16 00:10:36 +01:00
|
|
|
r = log_set_target_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2010-04-06 23:40:24 +02:00
|
|
|
log_error("Failed to parse log target %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2010-06-17 22:52:55 +02:00
|
|
|
case ARG_LOG_COLOR:
|
|
|
|
|
2010-06-18 23:13:15 +02:00
|
|
|
if (optarg) {
|
2014-02-16 00:10:36 +01:00
|
|
|
r = log_show_color_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2010-06-18 23:13:15 +02:00
|
|
|
log_error("Failed to parse log color setting %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
log_show_color(true);
|
2010-06-17 22:52:55 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_LOG_LOCATION:
|
2010-06-18 23:13:15 +02:00
|
|
|
if (optarg) {
|
2014-02-16 00:10:36 +01:00
|
|
|
r = log_show_location_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2010-06-18 23:13:15 +02:00
|
|
|
log_error("Failed to parse log location setting %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
log_show_location(true);
|
2010-06-17 22:52:55 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-02-15 11:52:29 +01:00
|
|
|
case ARG_DEFAULT_STD_OUTPUT:
|
2014-02-16 00:10:36 +01:00
|
|
|
r = exec_output_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2011-02-15 11:52:29 +01:00
|
|
|
log_error("Failed to parse default standard output setting %s.", optarg);
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
arg_default_std_output = r;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_DEFAULT_STD_ERROR:
|
2014-02-16 00:10:36 +01:00
|
|
|
r = exec_output_from_string(optarg);
|
|
|
|
if (r < 0) {
|
2011-02-15 11:52:29 +01:00
|
|
|
log_error("Failed to parse default standard error output setting %s.", optarg);
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
arg_default_std_error = r;
|
|
|
|
break;
|
|
|
|
|
2010-06-09 15:38:46 +02:00
|
|
|
case ARG_UNIT:
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2015-09-23 01:14:34 +02:00
|
|
|
r = free_and_strdup(&arg_default_unit, optarg);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to set default unit %s: %m", optarg);
|
2010-04-06 23:40:24 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2010-07-13 18:57:58 +02:00
|
|
|
case ARG_SYSTEM:
|
2016-02-24 21:24:23 +01:00
|
|
|
arg_system = true;
|
2010-07-13 18:57:58 +02:00
|
|
|
break;
|
2010-04-06 23:55:42 +02:00
|
|
|
|
2010-11-15 22:12:41 +01:00
|
|
|
case ARG_USER:
|
2016-02-24 21:24:23 +01:00
|
|
|
arg_system = false;
|
2010-04-06 23:55:42 +02:00
|
|
|
break;
|
|
|
|
|
2010-04-07 00:10:17 +02:00
|
|
|
case ARG_TEST:
|
2010-07-06 20:21:08 +02:00
|
|
|
arg_action = ACTION_TEST;
|
2014-01-07 06:00:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_NO_PAGER:
|
|
|
|
arg_no_pager = true;
|
2010-04-07 00:10:17 +02:00
|
|
|
break;
|
|
|
|
|
2012-07-17 07:31:47 +02:00
|
|
|
case ARG_VERSION:
|
|
|
|
arg_action = ACTION_VERSION;
|
|
|
|
break;
|
|
|
|
|
2010-04-10 17:53:17 +02:00
|
|
|
case ARG_DUMP_CONFIGURATION_ITEMS:
|
2010-07-06 20:21:08 +02:00
|
|
|
arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
|
2010-04-10 17:53:17 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-07 00:00:59 +02:00
|
|
|
case ARG_DUMP_CORE:
|
2015-09-23 23:13:06 +02:00
|
|
|
if (!optarg)
|
|
|
|
arg_dump_core = true;
|
|
|
|
else {
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse dump core boolean: %s", optarg);
|
|
|
|
arg_dump_core = r;
|
2012-05-08 23:36:55 +02:00
|
|
|
}
|
2015-09-23 23:13:06 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_CRASH_CHVT:
|
|
|
|
r = parse_crash_chvt(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse crash virtual terminal index: %s", optarg);
|
2010-07-07 00:00:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_CRASH_SHELL:
|
2015-09-23 23:13:06 +02:00
|
|
|
if (!optarg)
|
|
|
|
arg_crash_shell = true;
|
|
|
|
else {
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
|
|
|
|
arg_crash_shell = r;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_CRASH_REBOOT:
|
|
|
|
if (!optarg)
|
|
|
|
arg_crash_reboot = true;
|
|
|
|
else {
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
|
|
|
|
arg_crash_reboot = r;
|
2012-05-08 23:36:55 +02:00
|
|
|
}
|
2010-07-07 00:00:59 +02:00
|
|
|
break;
|
|
|
|
|
2010-04-13 02:06:27 +02:00
|
|
|
case ARG_CONFIRM_SPAWN:
|
2012-05-08 23:36:55 +02:00
|
|
|
r = optarg ? parse_boolean(optarg) : 1;
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse confirm spawn boolean %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
arg_confirm_spawn = r;
|
2010-04-13 02:06:27 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-07 00:00:59 +02:00
|
|
|
case ARG_SHOW_STATUS:
|
2014-01-28 04:27:07 +01:00
|
|
|
if (optarg) {
|
|
|
|
r = parse_show_status(optarg, &arg_show_status);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse show status boolean %s.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
arg_show_status = SHOW_STATUS_YES;
|
2010-08-09 18:00:24 +02:00
|
|
|
break;
|
2012-05-08 23:36:55 +02:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
case ARG_DESERIALIZE: {
|
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
|
2012-12-22 19:30:07 +01:00
|
|
|
r = safe_atoi(optarg, &fd);
|
|
|
|
if (r < 0 || fd < 0) {
|
2010-04-21 03:27:44 +02:00
|
|
|
log_error("Failed to parse deserialize option %s.", optarg);
|
2015-09-23 23:13:06 +02:00
|
|
|
return -EINVAL;
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-23 23:13:06 +02:00
|
|
|
(void) fd_cloexec(fd, true);
|
2012-12-22 19:30:07 +01:00
|
|
|
|
|
|
|
f = fdopen(fd, "r");
|
2014-11-28 19:57:32 +01:00
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to open serialization fd: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-09-09 15:20:10 +02:00
|
|
|
safe_fclose(arg_serialization);
|
2014-02-13 01:35:27 +01:00
|
|
|
arg_serialization = f;
|
2010-04-21 03:27:44 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-21 19:48:04 +02:00
|
|
|
case ARG_SWITCHED_ROOT:
|
2012-05-22 02:35:22 +02:00
|
|
|
arg_switched_root = true;
|
2012-05-16 14:22:42 +02:00
|
|
|
break;
|
|
|
|
|
2015-07-06 00:00:59 +02:00
|
|
|
case ARG_MACHINE_ID:
|
|
|
|
r = set_machine_id(optarg);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("MachineID '%s' is not valid.", optarg);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-04-06 23:40:24 +02:00
|
|
|
case 'h':
|
2010-07-06 20:21:08 +02:00
|
|
|
arg_action = ACTION_HELP;
|
2010-04-06 23:40:24 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-13 20:06:29 +02:00
|
|
|
case 'D':
|
|
|
|
log_set_max_level(LOG_DEBUG);
|
|
|
|
break;
|
|
|
|
|
2011-03-10 23:01:42 +01:00
|
|
|
case 'b':
|
|
|
|
case 's':
|
|
|
|
case 'z':
|
|
|
|
/* Just to eat away the sysvinit kernel
|
|
|
|
* cmdline args without getopt() error
|
|
|
|
* messages that we'll parse in
|
|
|
|
* parse_proc_cmdline_word() or ignore. */
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2011-03-10 23:01:42 +01:00
|
|
|
case '?':
|
2014-08-02 17:12:21 +02:00
|
|
|
if (getpid() != 1)
|
2011-03-10 23:01:42 +01:00
|
|
|
return -EINVAL;
|
2014-08-02 17:12:21 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2011-03-10 23:01:42 +01:00
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option code.");
|
2010-04-06 23:40:24 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 03:35:59 +01:00
|
|
|
if (optind < argc && getpid() != 1) {
|
|
|
|
/* Hmm, when we aren't run as init system
|
|
|
|
* let's complain about excess arguments */
|
|
|
|
|
|
|
|
log_error("Excess arguments.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-04-06 23:40:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int help(void) {
|
|
|
|
|
2010-06-24 00:08:42 +02:00
|
|
|
printf("%s [OPTIONS...]\n\n"
|
2010-11-15 22:12:41 +01:00
|
|
|
"Starts up and maintains the system or user services.\n\n"
|
2010-04-10 17:53:17 +02:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --test Determine startup sequence, dump it and exit\n"
|
2014-01-07 06:00:05 +01:00
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2010-04-13 02:06:27 +02:00
|
|
|
" --dump-configuration-items Dump understood unit configuration items\n"
|
2010-07-07 00:00:59 +02:00
|
|
|
" --unit=UNIT Set default unit\n"
|
2010-07-13 18:57:58 +02:00
|
|
|
" --system Run a system instance, even if PID != 1\n"
|
2010-11-15 22:12:41 +01:00
|
|
|
" --user Run a user instance\n"
|
2015-09-23 23:13:06 +02:00
|
|
|
" --dump-core[=BOOL] Dump core on crash\n"
|
|
|
|
" --crash-vt=NR Change to specified VT on crash\n"
|
|
|
|
" --crash-reboot[=BOOL] Reboot on crash\n"
|
|
|
|
" --crash-shell[=BOOL] Run shell on crash\n"
|
|
|
|
" --confirm-spawn[=BOOL] Ask for confirmation when spawning processes\n"
|
|
|
|
" --show-status[=BOOL] Show status updates on the console during bootup\n"
|
2014-08-11 20:08:08 +02:00
|
|
|
" --log-target=TARGET Set log target (console, journal, kmsg, journal-or-kmsg, null)\n"
|
2010-07-07 00:00:59 +02:00
|
|
|
" --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
|
2015-09-23 23:13:06 +02:00
|
|
|
" --log-color[=BOOL] Highlight important log messages\n"
|
|
|
|
" --log-location[=BOOL] Include code location in log messages\n"
|
2011-02-15 11:52:29 +01:00
|
|
|
" --default-standard-output= Set default standard output for services\n"
|
|
|
|
" --default-standard-error= Set default standard error output for services\n",
|
2010-06-16 21:54:17 +02:00
|
|
|
program_invocation_short_name);
|
2010-04-06 23:40:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:05:24 +02:00
|
|
|
static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
|
2015-09-23 01:11:30 +02:00
|
|
|
_cleanup_fdset_free_ FDSet *fds = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2010-04-21 03:27:44 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(_f);
|
|
|
|
assert(_fds);
|
|
|
|
|
2012-07-18 01:46:52 +02:00
|
|
|
r = manager_open_serialization(m, &f);
|
2015-09-23 01:11:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to create serialization file: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2013-07-10 21:10:53 +02:00
|
|
|
/* Make sure nothing is really destructed when we shut down */
|
2016-02-23 05:32:04 +01:00
|
|
|
m->n_reloading++;
|
2013-11-19 21:12:59 +01:00
|
|
|
bus_manager_send_reloading(m, true);
|
2013-07-10 21:10:53 +02:00
|
|
|
|
2012-07-18 01:46:52 +02:00
|
|
|
fds = fdset_new();
|
2015-09-23 01:11:30 +02:00
|
|
|
if (!fds)
|
|
|
|
return log_oom();
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2013-04-08 14:05:24 +02:00
|
|
|
r = manager_serialize(m, f, fds, switching_root);
|
2015-09-23 01:11:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to serialize state: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-09-23 01:11:30 +02:00
|
|
|
if (fseeko(f, 0, SEEK_SET) == (off_t) -1)
|
|
|
|
return log_error_errno(errno, "Failed to rewind serialization fd: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-07-18 01:46:52 +02:00
|
|
|
r = fd_cloexec(fileno(f), false);
|
2015-09-23 01:11:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-07-18 01:46:52 +02:00
|
|
|
r = fdset_cloexec(fds, false);
|
2015-09-23 01:11:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization fds: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
|
|
|
*_f = f;
|
|
|
|
*_fds = fds;
|
|
|
|
|
2015-09-23 01:11:30 +02:00
|
|
|
f = NULL;
|
|
|
|
fds = NULL;
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-09-23 01:11:30 +02:00
|
|
|
return 0;
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 16:35:59 +02:00
|
|
|
static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
|
|
|
|
struct rlimit nl;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(saved_rlimit);
|
|
|
|
|
|
|
|
/* Save the original RLIMIT_NOFILE so that we can reset it
|
|
|
|
* later when transitioning from the initrd to the main
|
|
|
|
* systemd or suchlike. */
|
2014-11-28 19:57:32 +01:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0)
|
|
|
|
return log_error_errno(errno, "Reading RLIMIT_NOFILE failed: %m");
|
2012-09-17 16:35:59 +02:00
|
|
|
|
|
|
|
/* Make sure forked processes get the default kernel setting */
|
|
|
|
if (!arg_default_rlimit[RLIMIT_NOFILE]) {
|
|
|
|
struct rlimit *rl;
|
|
|
|
|
|
|
|
rl = newdup(struct rlimit, saved_rlimit, 1);
|
|
|
|
if (!rl)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
arg_default_rlimit[RLIMIT_NOFILE] = rl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bump up the resource limit for ourselves substantially */
|
|
|
|
nl.rlim_cur = nl.rlim_max = 64*1024;
|
|
|
|
r = setrlimit_closest(RLIMIT_NOFILE, &nl);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Setting RLIMIT_NOFILE failed: %m");
|
2012-09-17 16:35:59 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:10:20 +01:00
|
|
|
static void test_usr(void) {
|
|
|
|
|
2011-03-04 03:58:52 +01:00
|
|
|
/* Check that /usr is not a separate fs */
|
2011-02-23 01:10:20 +01:00
|
|
|
|
2011-03-30 02:12:46 +02:00
|
|
|
if (dir_is_empty("/usr") <= 0)
|
|
|
|
return;
|
|
|
|
|
2015-01-05 14:12:47 +01:00
|
|
|
log_warning("/usr appears to be on its own filesystem and is not already mounted. This is not a supported setup. "
|
2011-03-30 02:12:46 +02:00
|
|
|
"Some things will probably break (sometimes even silently) in mysterious ways. "
|
|
|
|
"Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:35:51 +02:00
|
|
|
static int initialize_join_controllers(void) {
|
|
|
|
/* By default, mount "cpu" + "cpuacct" together, and "net_cls"
|
|
|
|
* + "net_prio". We'd like to add "cpuset" to the mix, but
|
2014-12-29 10:45:58 +01:00
|
|
|
* "cpuset" doesn't really work for groups with no initialized
|
2012-09-24 11:35:51 +02:00
|
|
|
* attributes. */
|
|
|
|
|
|
|
|
arg_join_controllers = new(char**, 3);
|
|
|
|
if (!arg_join_controllers)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
|
2015-09-23 01:01:26 +02:00
|
|
|
if (!arg_join_controllers[0])
|
|
|
|
goto oom;
|
2013-04-23 14:28:10 +02:00
|
|
|
|
2015-09-23 01:01:26 +02:00
|
|
|
arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL);
|
|
|
|
if (!arg_join_controllers[1])
|
|
|
|
goto oom;
|
2012-09-24 11:35:51 +02:00
|
|
|
|
2015-09-23 01:01:26 +02:00
|
|
|
arg_join_controllers[2] = NULL;
|
2012-09-24 11:35:51 +02:00
|
|
|
return 0;
|
2015-09-23 01:01:26 +02:00
|
|
|
|
|
|
|
oom:
|
|
|
|
arg_join_controllers = strv_free_free(arg_join_controllers);
|
|
|
|
return -ENOMEM;
|
2012-09-24 11:35:51 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
static int enforce_syscall_archs(Set *archs) {
|
|
|
|
#ifdef HAVE_SECCOMP
|
|
|
|
scmp_filter_ctx *seccomp;
|
|
|
|
Iterator i;
|
|
|
|
void *id;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
seccomp = seccomp_init(SCMP_ACT_ALLOW);
|
|
|
|
if (!seccomp)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
SET_FOREACH(id, arg_syscall_archs, i) {
|
|
|
|
r = seccomp_arch_add(seccomp, PTR_TO_UINT32(id) - 1);
|
|
|
|
if (r == -EEXIST)
|
|
|
|
continue;
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to add architecture to seccomp: %m");
|
2014-02-13 01:35:27 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 01:14:14 +01:00
|
|
|
r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to unset NO_NEW_PRIVS: %m");
|
2014-02-17 01:14:14 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
r = seccomp_load(seccomp);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to add install architecture seccomp: %m");
|
2014-02-13 01:35:27 +01:00
|
|
|
|
|
|
|
finish:
|
|
|
|
seccomp_release(seccomp);
|
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:17:08 +01:00
|
|
|
static int status_welcome(void) {
|
|
|
|
_cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = parse_env_file("/etc/os-release", NEWLINE,
|
|
|
|
"PRETTY_NAME", &pretty_name,
|
|
|
|
"ANSI_COLOR", &ansi_color,
|
|
|
|
NULL);
|
2015-09-08 23:03:38 +02:00
|
|
|
if (r == -ENOENT)
|
2014-06-13 19:45:52 +02:00
|
|
|
r = parse_env_file("/usr/lib/os-release", NEWLINE,
|
|
|
|
"PRETTY_NAME", &pretty_name,
|
|
|
|
"ANSI_COLOR", &ansi_color,
|
|
|
|
NULL);
|
2014-02-17 16:17:08 +01:00
|
|
|
|
|
|
|
if (r < 0 && r != -ENOENT)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Failed to read os-release file: %m");
|
2014-02-17 16:17:08 +01:00
|
|
|
|
2016-03-16 14:27:37 +01:00
|
|
|
if (log_get_show_color())
|
|
|
|
return status_printf(NULL, false, false,
|
|
|
|
"\nWelcome to \x1B[%sm%s\x1B[0m!\n",
|
|
|
|
isempty(ansi_color) ? "1" : ansi_color,
|
|
|
|
isempty(pretty_name) ? "Linux" : pretty_name);
|
|
|
|
else
|
|
|
|
return status_printf(NULL, false, false,
|
|
|
|
"\nWelcome to %s!\n",
|
|
|
|
isempty(pretty_name) ? "Linux" : pretty_name);
|
2014-02-17 16:17:08 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 12:37:11 +02:00
|
|
|
static int write_container_id(void) {
|
|
|
|
const char *c;
|
2015-11-02 09:34:05 +01:00
|
|
|
int r;
|
2014-05-28 12:37:11 +02:00
|
|
|
|
|
|
|
c = getenv("container");
|
|
|
|
if (isempty(c))
|
|
|
|
return 0;
|
|
|
|
|
2016-04-07 16:15:26 +02:00
|
|
|
RUN_WITH_UMASK(0022)
|
|
|
|
r = write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE);
|
2015-11-02 09:34:05 +01:00
|
|
|
if (r < 0)
|
2015-11-04 13:18:59 +01:00
|
|
|
return log_warning_errno(r, "Failed to write /run/systemd/container, ignoring: %m");
|
2015-11-02 09:34:05 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bump_unix_max_dgram_qlen(void) {
|
|
|
|
_cleanup_free_ char *qlen = NULL;
|
|
|
|
unsigned long v;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel
|
|
|
|
* default of 16 is simply too low. We set the value really
|
|
|
|
* really early during boot, so that it is actually applied to
|
|
|
|
* all our sockets, including the $NOTIFY_SOCKET one. */
|
|
|
|
|
|
|
|
r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to read AF_UNIX datagram queue length, ignoring: %m");
|
|
|
|
|
|
|
|
r = safe_atolu(qlen, &v);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length, ignoring: %m");
|
|
|
|
|
|
|
|
if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
qlen = mfree(qlen);
|
|
|
|
if (asprintf(&qlen, "%lu\n", DEFAULT_UNIX_MAX_DGRAM_QLEN) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = write_string_file("/proc/sys/net/unix/max_dgram_qlen", qlen, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
|
|
|
|
"Failed to bump AF_UNIX datagram queue length, ignoring: %m");
|
|
|
|
|
|
|
|
return 1;
|
2014-05-28 12:37:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 18:54:21 +02:00
|
|
|
static int fixup_environment(void) {
|
|
|
|
_cleanup_free_ char *term = NULL;
|
|
|
|
int r;
|
|
|
|
|
2016-07-19 17:00:36 +02:00
|
|
|
/* We expect the environment to be set correctly
|
|
|
|
* if run inside a container. */
|
|
|
|
if (detect_container() > 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-06-20 18:54:21 +02:00
|
|
|
/* When started as PID1, the kernel uses /dev/console
|
|
|
|
* for our stdios and uses TERM=linux whatever the
|
|
|
|
* backend device used by the console. We try to make
|
|
|
|
* a better guess here since some consoles might not
|
|
|
|
* have support for color mode for example.
|
|
|
|
*
|
|
|
|
* However if TERM was configured through the kernel
|
|
|
|
* command line then leave it alone. */
|
|
|
|
|
|
|
|
r = get_proc_cmdline_key("TERM=", &term);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r == 0) {
|
2016-07-27 15:25:55 +02:00
|
|
|
term = strdup(default_term_for_tty("/dev/console"));
|
2016-06-20 18:54:21 +02:00
|
|
|
if (!term)
|
2016-07-19 17:00:36 +02:00
|
|
|
return -ENOMEM;
|
2016-06-20 18:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setenv("TERM", term, 1) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
Manager *m = NULL;
|
2010-08-31 21:05:54 +02:00
|
|
|
int r, retval = EXIT_FAILURE;
|
2011-07-28 23:41:57 +02:00
|
|
|
usec_t before_startup, after_startup;
|
|
|
|
char timespan[FORMAT_TIMESPAN_MAX];
|
2010-04-21 03:27:44 +02:00
|
|
|
FDSet *fds = NULL;
|
|
|
|
bool reexecute = false;
|
2010-10-14 00:52:26 +02:00
|
|
|
const char *shutdown_verb = NULL;
|
2015-02-03 18:16:35 +01:00
|
|
|
dual_timestamp initrd_timestamp = DUAL_TIMESTAMP_NULL;
|
|
|
|
dual_timestamp userspace_timestamp = DUAL_TIMESTAMP_NULL;
|
|
|
|
dual_timestamp kernel_timestamp = DUAL_TIMESTAMP_NULL;
|
|
|
|
dual_timestamp security_start_timestamp = DUAL_TIMESTAMP_NULL;
|
|
|
|
dual_timestamp security_finish_timestamp = DUAL_TIMESTAMP_NULL;
|
2012-02-01 22:33:15 +01:00
|
|
|
static char systemd[] = "systemd";
|
2012-05-21 19:48:04 +02:00
|
|
|
bool skip_setup = false;
|
2014-02-13 01:35:27 +01:00
|
|
|
unsigned j;
|
2011-07-28 23:52:23 +02:00
|
|
|
bool loaded_policy = false;
|
2012-04-05 22:08:10 +02:00
|
|
|
bool arm_reboot_watchdog = false;
|
2012-05-22 02:35:22 +02:00
|
|
|
bool queue_default_job = false;
|
2014-06-17 03:25:34 +02:00
|
|
|
bool empty_etc = false;
|
2012-05-21 19:33:39 +02:00
|
|
|
char *switch_root_dir = NULL, *switch_root_init = NULL;
|
2015-02-03 18:16:35 +01:00
|
|
|
struct rlimit saved_rlimit_nofile = RLIMIT_MAKE_CONST(0);
|
2014-11-06 06:05:38 +01:00
|
|
|
const char *error_message = NULL;
|
2010-04-04 22:49:26 +02:00
|
|
|
|
2011-07-28 23:42:47 +02:00
|
|
|
#ifdef HAVE_SYSV_COMPAT
|
2010-06-18 20:01:01 +02:00
|
|
|
if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
|
2011-02-21 15:32:17 +01:00
|
|
|
/* This is compatibility support for SysV, where
|
2010-06-18 20:01:01 +02:00
|
|
|
* calling init as a user is identical to telinit. */
|
|
|
|
|
|
|
|
execv(SYSTEMCTL_BINARY_PATH, argv);
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
|
2010-06-18 20:01:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-07-28 23:42:47 +02:00
|
|
|
#endif
|
2010-06-18 20:01:01 +02:00
|
|
|
|
2013-04-24 17:15:47 +02:00
|
|
|
dual_timestamp_from_monotonic(&kernel_timestamp, 0);
|
|
|
|
dual_timestamp_get(&userspace_timestamp);
|
|
|
|
|
2011-07-28 23:52:23 +02:00
|
|
|
/* Determine if this is a reexecution or normal bootup. We do
|
|
|
|
* the full command line parsing much later, so let's just
|
|
|
|
* have a quick peek here. */
|
2013-03-22 04:42:26 +01:00
|
|
|
if (strv_find(argv+1, "--deserialize"))
|
|
|
|
skip_setup = true;
|
2011-07-28 23:52:23 +02:00
|
|
|
|
2012-05-21 19:48:04 +02:00
|
|
|
/* If we have switched root, do all the special setup
|
|
|
|
* things */
|
2013-03-22 04:42:26 +01:00
|
|
|
if (strv_find(argv+1, "--switched-root"))
|
|
|
|
skip_setup = false;
|
2012-05-16 14:22:42 +02:00
|
|
|
|
2010-11-10 22:35:05 +01:00
|
|
|
/* If we get started via the /sbin/init symlink then we are
|
|
|
|
called 'init'. After a subsequent reexecution we are then
|
|
|
|
called 'systemd'. That is confusing, hence let's call us
|
|
|
|
systemd right-away. */
|
|
|
|
program_invocation_short_name = systemd;
|
|
|
|
prctl(PR_SET_NAME, systemd);
|
2012-02-01 22:33:15 +01:00
|
|
|
|
2011-06-30 04:16:10 +02:00
|
|
|
saved_argv = argv;
|
|
|
|
saved_argc = argc;
|
2010-11-10 22:35:05 +01:00
|
|
|
|
2014-08-11 20:08:08 +02:00
|
|
|
log_set_upgrade_syslog_to_journal(true);
|
2010-06-17 22:52:55 +02:00
|
|
|
|
2013-09-26 20:39:41 +02:00
|
|
|
/* Disable the umask logic */
|
|
|
|
if (getpid() == 1)
|
|
|
|
umask(0);
|
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
if (getpid() == 1 && detect_container() <= 0) {
|
2013-05-14 00:00:37 +02:00
|
|
|
|
2012-09-17 17:42:13 +02:00
|
|
|
/* Running outside of a container as PID 1 */
|
2016-02-24 21:24:23 +01:00
|
|
|
arg_system = true;
|
2012-09-17 17:42:13 +02:00
|
|
|
log_set_target(LOG_TARGET_KMSG);
|
|
|
|
log_open();
|
|
|
|
|
2013-07-23 12:25:32 +02:00
|
|
|
if (in_initrd())
|
2013-04-24 17:15:47 +02:00
|
|
|
initrd_timestamp = userspace_timestamp;
|
2012-05-16 14:22:44 +02:00
|
|
|
|
2012-05-21 19:48:04 +02:00
|
|
|
if (!skip_setup) {
|
2016-02-02 20:36:33 +01:00
|
|
|
r = mount_setup_early();
|
|
|
|
if (r < 0) {
|
|
|
|
error_message = "Failed to early mount API filesystems";
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-11-10 23:21:15 +01:00
|
|
|
dual_timestamp_get(&security_start_timestamp);
|
2014-11-06 06:05:38 +01:00
|
|
|
if (mac_selinux_setup(&loaded_policy) < 0) {
|
|
|
|
error_message = "Failed to load SELinux policy";
|
2011-07-28 23:52:23 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
} else if (mac_smack_setup(&loaded_policy) < 0) {
|
|
|
|
error_message = "Failed to load SMACK policy";
|
2013-03-07 20:06:58 +01:00
|
|
|
goto finish;
|
2016-08-02 14:58:30 +02:00
|
|
|
} else if (ima_setup() < 0) {
|
|
|
|
error_message = "Failed to load IMA policy";
|
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2013-11-10 23:21:15 +01:00
|
|
|
dual_timestamp_get(&security_finish_timestamp);
|
2012-03-15 19:06:11 +01:00
|
|
|
}
|
2011-07-28 23:52:23 +02:00
|
|
|
|
2016-03-02 02:35:55 +01:00
|
|
|
if (mac_selinux_init() < 0) {
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to initialize SELinux policy";
|
2010-08-06 00:42:24 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2011-05-24 20:23:07 +02:00
|
|
|
|
2012-09-17 16:41:13 +02:00
|
|
|
if (!skip_setup) {
|
2016-02-26 11:25:22 +01:00
|
|
|
if (clock_is_localtime(NULL) > 0) {
|
2011-07-28 23:52:23 +02:00
|
|
|
int min;
|
2011-05-24 20:23:07 +02:00
|
|
|
|
2014-07-27 00:11:08 +02:00
|
|
|
/*
|
|
|
|
* The very first call of settimeofday() also does a time warp in the kernel.
|
|
|
|
*
|
|
|
|
* In the rtc-in-local time mode, we set the kernel's timezone, and rely on
|
|
|
|
* external tools to take care of maintaining the RTC and do all adjustments.
|
|
|
|
* This matches the behavior of Windows, which leaves the RTC alone if the
|
|
|
|
* registry tells that the RTC runs in UTC.
|
|
|
|
*/
|
2014-05-22 14:21:38 +02:00
|
|
|
r = clock_set_timezone(&min);
|
2011-07-28 23:52:23 +02:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to apply local time delta, ignoring: %m");
|
2011-07-28 23:52:23 +02:00
|
|
|
else
|
|
|
|
log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
|
2012-10-27 16:23:32 +02:00
|
|
|
} else if (!in_initrd()) {
|
|
|
|
/*
|
2014-07-27 00:11:08 +02:00
|
|
|
* Do a dummy very first call to seal the kernel's time warp magic.
|
2012-10-27 16:23:32 +02:00
|
|
|
*
|
2016-07-10 14:48:23 +02:00
|
|
|
* Do not call this from inside the initrd. The initrd might not
|
2012-10-27 16:23:32 +02:00
|
|
|
* carry /etc/adjtime with LOCAL, but the real system could be set up
|
|
|
|
* that way. In such case, we need to delay the time-warp or the sealing
|
|
|
|
* until we reach the real system.
|
2014-07-27 00:11:08 +02:00
|
|
|
*
|
|
|
|
* Do no set the kernel's timezone. The concept of local time cannot
|
|
|
|
* be supported reliably, the time will jump or be incorrect at every daylight
|
|
|
|
* saving time change. All kernel local time concepts will be treated
|
|
|
|
* as UTC that way.
|
2012-10-27 16:23:32 +02:00
|
|
|
*/
|
2016-02-03 18:28:40 +01:00
|
|
|
(void) clock_reset_timewarp();
|
2012-09-17 16:41:13 +02:00
|
|
|
}
|
2016-02-03 18:28:40 +01:00
|
|
|
|
|
|
|
r = clock_apply_epoch();
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Current system time is before build time, but cannot correct: %m");
|
|
|
|
else if (r > 0)
|
|
|
|
log_info("System time before build time, advancing clock.");
|
2012-09-17 16:41:13 +02:00
|
|
|
}
|
2012-09-17 17:42:13 +02:00
|
|
|
|
|
|
|
/* Set the default for later on, but don't actually
|
|
|
|
* open the logs like this for now. Note that if we
|
|
|
|
* are transitioning from the initrd there might still
|
|
|
|
* be journal fd open, and we shouldn't attempt
|
|
|
|
* opening that before we parsed /proc/cmdline which
|
|
|
|
* might redirect output elsewhere. */
|
|
|
|
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
|
|
|
|
|
|
|
|
} else if (getpid() == 1) {
|
|
|
|
/* Running inside a container, as PID 1 */
|
2016-02-24 21:24:23 +01:00
|
|
|
arg_system = true;
|
2012-09-17 17:42:13 +02:00
|
|
|
log_set_target(LOG_TARGET_CONSOLE);
|
2013-12-18 05:07:34 +01:00
|
|
|
log_close_console(); /* force reopen of /dev/console */
|
2012-09-17 17:42:13 +02:00
|
|
|
log_open();
|
|
|
|
|
|
|
|
/* For the later on, see above... */
|
|
|
|
log_set_target(LOG_TARGET_JOURNAL);
|
|
|
|
|
2013-04-24 17:15:47 +02:00
|
|
|
/* clear the kernel timestamp,
|
|
|
|
* because we are in a container */
|
2016-02-29 13:56:57 +01:00
|
|
|
kernel_timestamp = DUAL_TIMESTAMP_NULL;
|
2013-04-24 17:15:47 +02:00
|
|
|
} else {
|
2012-09-17 17:42:13 +02:00
|
|
|
/* Running as user instance */
|
2016-02-24 21:24:23 +01:00
|
|
|
arg_system = false;
|
2011-07-01 22:35:34 +02:00
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
2011-07-25 21:22:57 +02:00
|
|
|
log_open();
|
2013-04-24 17:15:47 +02:00
|
|
|
|
|
|
|
/* clear the kernel timestamp,
|
|
|
|
* because we are not PID 1 */
|
2015-08-28 17:11:37 +02:00
|
|
|
kernel_timestamp = DUAL_TIMESTAMP_NULL;
|
2010-06-17 22:52:55 +02:00
|
|
|
}
|
2010-04-06 23:55:42 +02:00
|
|
|
|
2016-02-08 22:30:58 +01:00
|
|
|
if (getpid() == 1) {
|
|
|
|
/* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour the limit)
|
|
|
|
* will process core dumps for system services by default. */
|
|
|
|
(void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY));
|
|
|
|
|
|
|
|
/* But at the same time, turn off the core_pattern logic by default, so that no coredumps are stored
|
|
|
|
* until the systemd-coredump tool is enabled via sysctl. */
|
|
|
|
if (!skip_setup)
|
|
|
|
(void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
|
|
|
|
}
|
|
|
|
|
2016-06-24 16:08:43 +02:00
|
|
|
if (arg_system) {
|
2016-07-19 17:00:36 +02:00
|
|
|
if (fixup_environment() < 0) {
|
|
|
|
error_message = "Failed to fix up PID1 environment";
|
|
|
|
goto finish;
|
|
|
|
}
|
2016-06-20 18:54:21 +02:00
|
|
|
|
2016-06-20 21:45:28 +02:00
|
|
|
/* Try to figure out if we can use colors with the console. No
|
|
|
|
* need to do that for user instances since they never log
|
|
|
|
* into the console. */
|
|
|
|
log_show_color(colors_enabled());
|
|
|
|
make_null_stdio();
|
|
|
|
}
|
|
|
|
|
2011-08-23 00:37:35 +02:00
|
|
|
/* Initialize default unit */
|
2015-09-23 01:14:34 +02:00
|
|
|
r = free_and_strdup(&arg_default_unit, SPECIAL_DEFAULT_TARGET);
|
2012-08-06 16:41:29 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to set default unit %s: %m", SPECIAL_DEFAULT_TARGET);
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to set default unit";
|
2010-04-06 23:40:24 +02:00
|
|
|
goto finish;
|
2012-08-04 02:22:09 +02:00
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2012-09-24 11:35:51 +02:00
|
|
|
r = initialize_join_controllers();
|
2014-11-06 06:05:38 +01:00
|
|
|
if (r < 0) {
|
2015-05-11 13:49:29 +02:00
|
|
|
error_message = "Failed to initialize cgroup controllers";
|
2011-08-23 00:37:35 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2010-04-06 23:40:24 +02:00
|
|
|
/* Mount /proc, /sys and friends, so that /proc/cmdline and
|
|
|
|
* /proc/$PID/fd is available. */
|
2013-03-14 21:36:38 +01:00
|
|
|
if (getpid() == 1) {
|
2014-11-14 17:58:32 +01:00
|
|
|
|
|
|
|
/* Load the kernel modules early, so that we kdbus.ko is loaded before kdbusfs shall be mounted */
|
2014-11-14 15:18:56 +01:00
|
|
|
if (!skip_setup)
|
|
|
|
kmod_setup();
|
|
|
|
|
2011-08-23 00:37:35 +02:00
|
|
|
r = mount_setup(loaded_policy);
|
2014-11-06 06:05:38 +01:00
|
|
|
if (r < 0) {
|
|
|
|
error_message = "Failed to mount API filesystems";
|
2010-05-24 18:59:13 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2011-08-23 00:37:35 +02:00
|
|
|
}
|
2010-04-06 21:59:25 +02:00
|
|
|
|
|
|
|
/* Reset all signal handlers. */
|
2015-05-31 23:55:55 +02:00
|
|
|
(void) reset_all_signal_handlers();
|
|
|
|
(void) ignore_signals(SIGNALS_IGNORE, -1);
|
2010-04-13 02:01:28 +02:00
|
|
|
|
2016-07-19 17:29:00 +02:00
|
|
|
arg_default_tasks_max = system_tasks_max_scale(15U, 100U); /* 15% the system PIDs equals 4915 by default. */
|
|
|
|
|
2014-11-06 06:05:38 +01:00
|
|
|
if (parse_config_file() < 0) {
|
|
|
|
error_message = "Failed to parse config file";
|
2010-07-07 01:10:27 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2010-07-07 01:10:27 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system) {
|
2014-11-06 21:53:34 +01:00
|
|
|
r = parse_proc_cmdline(parse_proc_cmdline_item);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
|
2014-11-06 21:53:34 +01:00
|
|
|
}
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2014-08-15 18:07:36 +02:00
|
|
|
/* Note that this also parses bits from the kernel command
|
|
|
|
* line, including "debug". */
|
2010-04-06 23:40:24 +02:00
|
|
|
log_parse_environment();
|
|
|
|
|
2014-11-06 06:05:38 +01:00
|
|
|
if (parse_argv(argc, argv) < 0) {
|
|
|
|
error_message = "Failed to parse commandline arguments";
|
2010-04-06 23:40:24 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2012-10-06 01:11:52 +02:00
|
|
|
if (arg_action == ACTION_TEST &&
|
|
|
|
geteuid() == 0) {
|
2011-02-19 14:20:00 +01:00
|
|
|
log_error("Don't run test mode as root.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (!arg_system &&
|
2012-10-06 01:11:52 +02:00
|
|
|
arg_action == ACTION_RUN &&
|
|
|
|
sd_booted() <= 0) {
|
|
|
|
log_error("Trying to run as user instance, but the system has not been booted with systemd.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system &&
|
2011-03-09 23:59:27 +01:00
|
|
|
arg_action == ACTION_RUN &&
|
|
|
|
running_in_chroot() > 0) {
|
|
|
|
log_error("Cannot be run in a chroot() environment.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:41:24 +01:00
|
|
|
if (arg_action == ACTION_TEST)
|
|
|
|
skip_setup = true;
|
|
|
|
|
2016-02-19 19:25:13 +01:00
|
|
|
if (arg_action == ACTION_TEST || arg_action == ACTION_HELP)
|
|
|
|
pager_open(arg_no_pager, false);
|
2014-01-07 06:00:05 +01:00
|
|
|
|
2010-07-06 20:21:08 +02:00
|
|
|
if (arg_action == ACTION_HELP) {
|
2010-04-06 23:40:24 +02:00
|
|
|
retval = help();
|
|
|
|
goto finish;
|
2012-07-17 07:31:47 +02:00
|
|
|
} else if (arg_action == ACTION_VERSION) {
|
|
|
|
retval = version();
|
|
|
|
goto finish;
|
2010-07-06 20:21:08 +02:00
|
|
|
} else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
|
2016-08-04 22:52:24 +02:00
|
|
|
pager_open(arg_no_pager, false);
|
2010-04-10 17:53:17 +02:00
|
|
|
unit_dump_config_items(stdout);
|
2010-08-31 21:05:54 +02:00
|
|
|
retval = EXIT_SUCCESS;
|
2010-04-10 17:53:17 +02:00
|
|
|
goto finish;
|
2010-04-06 23:40:24 +02:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (!arg_system &&
|
2013-11-07 14:14:22 +01:00
|
|
|
!getenv("XDG_RUNTIME_DIR")) {
|
|
|
|
log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2010-07-06 20:21:08 +02:00
|
|
|
assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2011-07-25 21:22:57 +02:00
|
|
|
/* Close logging fds, in order not to confuse fdset below */
|
|
|
|
log_close();
|
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
/* Remember open file descriptors for later deserialization */
|
2012-12-22 19:30:07 +01:00
|
|
|
r = fdset_new_fill(&fds);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to allocate fd set: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to allocate fd set";
|
2012-12-22 19:30:07 +01:00
|
|
|
goto finish;
|
|
|
|
} else
|
|
|
|
fdset_cloexec(fds, true);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
if (arg_serialization)
|
|
|
|
assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system)
|
2010-04-13 02:06:27 +02:00
|
|
|
/* Become a session leader if we aren't one yet. */
|
|
|
|
setsid();
|
2010-04-06 21:59:25 +02:00
|
|
|
|
2013-07-11 00:48:52 +02:00
|
|
|
/* Move out of the way, so that we won't block unmounts */
|
2015-05-27 21:02:24 +02:00
|
|
|
assert_se(chdir("/") == 0);
|
2013-07-11 00:48:52 +02:00
|
|
|
|
2010-04-23 04:06:23 +02:00
|
|
|
/* Reset the console, but only if this is really init and we
|
|
|
|
* are freshly booted */
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system && arg_action == ACTION_RUN) {
|
2014-08-15 18:01:52 +02:00
|
|
|
|
|
|
|
/* If we are init, we connect stdin/stdout/stderr to
|
|
|
|
* /dev/null and make sure we don't have a controlling
|
|
|
|
* tty. */
|
|
|
|
release_terminal();
|
|
|
|
|
|
|
|
if (getpid() == 1 && !skip_setup)
|
|
|
|
console_setup();
|
|
|
|
}
|
2010-04-06 21:59:25 +02:00
|
|
|
|
2010-04-10 21:42:55 +02:00
|
|
|
/* Open the logging devices, if possible and necessary */
|
2010-05-15 17:25:08 +02:00
|
|
|
log_open();
|
2010-04-06 21:59:25 +02:00
|
|
|
|
2014-02-17 16:17:08 +01:00
|
|
|
if (arg_show_status == _SHOW_STATUS_UNSET)
|
|
|
|
arg_show_status = SHOW_STATUS_YES;
|
|
|
|
|
2010-04-13 02:00:30 +02:00
|
|
|
/* Make sure we leave a core dump without panicing the
|
|
|
|
* kernel. */
|
2013-03-22 15:05:51 +01:00
|
|
|
if (getpid() == 1) {
|
2010-04-10 22:35:37 +02:00
|
|
|
install_crash_handler();
|
2010-04-10 21:40:40 +02:00
|
|
|
|
2011-08-23 00:37:35 +02:00
|
|
|
r = mount_cgroup_controllers(arg_join_controllers);
|
|
|
|
if (r < 0)
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system) {
|
2015-09-07 13:42:47 +02:00
|
|
|
int v;
|
2012-06-28 13:46:45 +02:00
|
|
|
|
2014-01-07 14:41:24 +01:00
|
|
|
log_info(PACKAGE_STRING " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
|
|
|
|
arg_action == ACTION_TEST ? "test " : "" );
|
2012-06-28 13:46:45 +02:00
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
v = detect_virtualization();
|
|
|
|
if (v > 0)
|
|
|
|
log_info("Detected virtualization %s.", virtualization_to_string(v));
|
2012-06-28 13:46:45 +02:00
|
|
|
|
2014-05-28 12:37:11 +02:00
|
|
|
write_container_id();
|
|
|
|
|
2015-03-15 02:49:10 +01:00
|
|
|
log_info("Detected architecture %s.", architecture_to_string(uname_architecture()));
|
2014-02-21 02:28:54 +01:00
|
|
|
|
2012-07-02 11:38:32 +02:00
|
|
|
if (in_initrd())
|
|
|
|
log_info("Running in initial RAM disk.");
|
|
|
|
|
2014-07-07 11:47:46 +02:00
|
|
|
/* Let's check whether /etc is already populated. We
|
|
|
|
* don't actually really check for that, but use
|
|
|
|
* /etc/machine-id as flag file. This allows container
|
|
|
|
* managers and installers to provision a couple of
|
|
|
|
* files already. If the container manager wants to
|
|
|
|
* provision the machine ID itself it should pass
|
2014-12-10 20:00:08 +01:00
|
|
|
* $container_uuid to PID 1. */
|
2014-07-07 11:47:46 +02:00
|
|
|
|
2014-07-04 03:13:05 +02:00
|
|
|
empty_etc = access("/etc/machine-id", F_OK) < 0;
|
2014-06-17 03:25:34 +02:00
|
|
|
if (empty_etc)
|
|
|
|
log_info("Running with unpopulated /etc.");
|
2014-01-08 23:20:45 +01:00
|
|
|
} else {
|
2014-06-27 18:34:37 +02:00
|
|
|
_cleanup_free_ char *t;
|
|
|
|
|
|
|
|
t = uid_to_name(getuid());
|
2014-01-07 14:41:24 +01:00
|
|
|
log_debug(PACKAGE_STRING " running in %suser mode for user "UID_FMT"/%s. (" SYSTEMD_FEATURES ")",
|
|
|
|
arg_action == ACTION_TEST ? " test" : "", getuid(), t);
|
2014-01-08 23:20:45 +01:00
|
|
|
}
|
2010-04-06 23:55:42 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system && !skip_setup) {
|
2015-03-16 17:34:59 +01:00
|
|
|
if (arg_show_status > 0)
|
2010-08-17 18:00:48 +02:00
|
|
|
status_welcome();
|
|
|
|
|
|
|
|
hostname_setup();
|
2016-07-22 12:12:27 +02:00
|
|
|
machine_id_setup(NULL, arg_machine_id, NULL);
|
2010-08-17 18:00:48 +02:00
|
|
|
loopback_setup();
|
2015-11-02 09:34:05 +01:00
|
|
|
bump_unix_max_dgram_qlen();
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2011-02-23 01:10:20 +01:00
|
|
|
test_usr();
|
2010-05-09 18:02:38 +02:00
|
|
|
}
|
2010-04-10 05:00:38 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system && arg_runtime_watchdog > 0 && arg_runtime_watchdog != USEC_INFINITY)
|
2012-04-05 22:08:10 +02:00
|
|
|
watchdog_set_timeout(&arg_runtime_watchdog);
|
|
|
|
|
2014-07-29 12:23:31 +02:00
|
|
|
if (arg_timer_slack_nsec != NSEC_INFINITY)
|
2012-05-31 04:36:08 +02:00
|
|
|
if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to adjust timer slack: %m");
|
2012-05-31 04:36:08 +02:00
|
|
|
|
2016-01-07 23:00:04 +01:00
|
|
|
if (!cap_test_all(arg_capability_bounding_set)) {
|
|
|
|
r = capability_bounding_set_drop_usermode(arg_capability_bounding_set);
|
2012-05-24 04:00:56 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to drop capability bounding set of usermode helpers: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to drop capability bounding set of usermode helpers";
|
2012-05-24 04:00:56 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2016-01-07 23:00:04 +01:00
|
|
|
r = capability_bounding_set_drop(arg_capability_bounding_set, true);
|
2012-05-29 23:33:38 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to drop capability bounding set: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to drop capability bounding set";
|
2012-05-29 23:33:38 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2012-05-24 04:00:56 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
if (arg_syscall_archs) {
|
|
|
|
r = enforce_syscall_archs(arg_syscall_archs);
|
2014-11-06 06:05:38 +01:00
|
|
|
if (r < 0) {
|
|
|
|
error_message = "Failed to set syscall architectures";
|
2014-02-13 01:35:27 +01:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2014-02-13 01:35:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (!arg_system)
|
2012-07-25 00:13:11 +02:00
|
|
|
/* Become reaper of our children */
|
2015-06-05 08:39:02 +02:00
|
|
|
if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_warning_errno(errno, "Failed to make us a subreaper: %m");
|
2012-07-25 00:13:11 +02:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (arg_system) {
|
2012-09-17 16:35:59 +02:00
|
|
|
bump_rlimit_nofile(&saved_rlimit_nofile);
|
|
|
|
|
2014-06-17 03:25:34 +02:00
|
|
|
if (empty_etc) {
|
2015-05-15 12:44:22 +02:00
|
|
|
r = unit_file_preset_all(UNIT_FILE_SYSTEM, false, NULL, UNIT_FILE_PRESET_ENABLE_ONLY, false, NULL, 0);
|
2014-06-17 03:25:34 +02:00
|
|
|
if (r < 0)
|
2015-11-10 15:47:39 +01:00
|
|
|
log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r, "Failed to populate /etc with preset unit settings, ignoring: %m");
|
2014-06-17 03:25:34 +02:00
|
|
|
else
|
|
|
|
log_info("Populated /etc with preset unit settings.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
r = manager_new(arg_system ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, arg_action == ACTION_TEST, &m);
|
2012-04-05 22:08:10 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to allocate manager object: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to allocate manager object";
|
2009-11-18 00:42:52 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2010-07-07 00:00:59 +02:00
|
|
|
m->confirm_spawn = arg_confirm_spawn;
|
2012-04-05 22:08:10 +02:00
|
|
|
m->runtime_watchdog = arg_runtime_watchdog;
|
|
|
|
m->shutdown_watchdog = arg_shutdown_watchdog;
|
2013-04-24 17:15:47 +02:00
|
|
|
m->userspace_timestamp = userspace_timestamp;
|
|
|
|
m->kernel_timestamp = kernel_timestamp;
|
|
|
|
m->initrd_timestamp = initrd_timestamp;
|
2013-11-10 23:21:15 +01:00
|
|
|
m->security_start_timestamp = security_start_timestamp;
|
|
|
|
m->security_finish_timestamp = security_finish_timestamp;
|
2010-07-07 00:00:59 +02:00
|
|
|
|
2015-06-29 11:26:27 +02:00
|
|
|
manager_set_defaults(m);
|
2011-09-01 21:05:06 +02:00
|
|
|
manager_set_show_status(m, arg_show_status);
|
2014-07-07 19:25:31 +02:00
|
|
|
manager_set_first_boot(m, empty_etc);
|
2011-09-01 21:05:06 +02:00
|
|
|
|
2012-05-22 02:35:22 +02:00
|
|
|
/* Remember whether we should queue the default job */
|
2014-02-13 01:35:27 +01:00
|
|
|
queue_default_job = !arg_serialization || arg_switched_root;
|
2012-05-22 02:35:22 +02:00
|
|
|
|
2011-07-28 23:41:57 +02:00
|
|
|
before_startup = now(CLOCK_MONOTONIC);
|
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
r = manager_startup(m, arg_serialization, fds);
|
2012-04-05 22:08:10 +02:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to fully start up daemon: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-05-22 02:35:22 +02:00
|
|
|
/* This will close all file descriptors that were opened, but
|
|
|
|
* not claimed by any unit. */
|
2015-09-23 01:05:55 +02:00
|
|
|
fds = fdset_free(fds);
|
2010-01-29 03:18:09 +01:00
|
|
|
|
2015-09-09 15:20:10 +02:00
|
|
|
arg_serialization = safe_fclose(arg_serialization);
|
2012-05-22 02:35:22 +02:00
|
|
|
|
|
|
|
if (queue_default_job) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2010-08-17 20:42:53 +02:00
|
|
|
Unit *target = NULL;
|
2012-02-02 12:39:33 +01:00
|
|
|
Job *default_unit_job;
|
2010-07-08 02:43:18 +02:00
|
|
|
|
2010-07-06 20:21:08 +02:00
|
|
|
log_debug("Activating default unit: %s", arg_default_unit);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
|
2013-11-19 21:12:59 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error("Failed to load default target: %s", bus_error_message(&error, r));
|
|
|
|
else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND)
|
2014-11-28 17:09:20 +01:00
|
|
|
log_error_errno(target->load_error, "Failed to load default target: %m");
|
2012-01-15 12:04:08 +01:00
|
|
|
else if (target->load_state == UNIT_MASKED)
|
2010-10-08 18:21:52 +02:00
|
|
|
log_error("Default target masked.");
|
2010-04-04 22:49:26 +02:00
|
|
|
|
2012-01-15 12:04:08 +01:00
|
|
|
if (!target || target->load_state != UNIT_LOADED) {
|
2010-04-21 03:27:44 +02:00
|
|
|
log_info("Trying to load rescue target...");
|
2010-08-17 20:42:53 +02:00
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
|
|
|
|
if (r < 0) {
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Failed to load rescue target: %s", bus_error_message(&error, r));
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to load rescue target";
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
2013-06-28 18:37:15 +02:00
|
|
|
} else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) {
|
2014-11-28 17:09:20 +01:00
|
|
|
log_emergency_errno(target->load_error, "Failed to load rescue target: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to load rescue target";
|
2010-08-17 20:42:53 +02:00
|
|
|
goto finish;
|
2012-01-15 12:04:08 +01:00
|
|
|
} else if (target->load_state == UNIT_MASKED) {
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Rescue target masked.");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Rescue target masked";
|
2010-10-08 02:31:36 +02:00
|
|
|
goto finish;
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 02:00:40 +02:00
|
|
|
|
2012-01-15 12:04:08 +01:00
|
|
|
assert(target->load_state == UNIT_LOADED);
|
2010-10-08 02:31:36 +02:00
|
|
|
|
2010-07-06 20:21:08 +02:00
|
|
|
if (arg_action == ACTION_TEST) {
|
2010-04-23 20:25:55 +02:00
|
|
|
printf("-> By units:\n");
|
2010-04-21 03:27:44 +02:00
|
|
|
manager_dump_units(m, stdout, "\t");
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:52:31 +01:00
|
|
|
r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, &error, &default_unit_job);
|
2013-03-07 22:18:34 +01:00
|
|
|
if (r == -EPERM) {
|
2013-11-19 21:12:59 +01:00
|
|
|
log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r));
|
2013-03-07 22:18:34 +01:00
|
|
|
|
2015-11-12 20:14:33 +01:00
|
|
|
sd_bus_error_free(&error);
|
|
|
|
|
2015-11-12 19:52:31 +01:00
|
|
|
r = manager_add_job(m, JOB_START, target, JOB_REPLACE, &error, &default_unit_job);
|
2013-03-07 22:18:34 +01:00
|
|
|
if (r < 0) {
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Failed to start default target: %s", bus_error_message(&error, r));
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to start default target";
|
2013-03-07 22:18:34 +01:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
} else if (r < 0) {
|
2014-11-06 06:04:06 +01:00
|
|
|
log_emergency("Failed to isolate default target: %s", bus_error_message(&error, r));
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to isolate default target";
|
2010-04-08 02:00:40 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2013-03-07 22:18:34 +01:00
|
|
|
|
2012-02-02 12:39:33 +01:00
|
|
|
m->default_unit_job_id = default_unit_job->id;
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2011-07-31 18:13:59 +02:00
|
|
|
after_startup = now(CLOCK_MONOTONIC);
|
|
|
|
log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
|
|
|
|
"Loaded units and determined initial transaction in %s.",
|
2014-10-01 14:34:05 +02:00
|
|
|
format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 100 * USEC_PER_MSEC));
|
2011-07-31 18:13:59 +02:00
|
|
|
|
2010-07-06 20:21:08 +02:00
|
|
|
if (arg_action == ACTION_TEST) {
|
2010-04-23 20:25:55 +02:00
|
|
|
printf("-> By jobs:\n");
|
2010-04-21 03:27:44 +02:00
|
|
|
manager_dump_jobs(m, stdout, "\t");
|
2010-08-31 21:05:54 +02:00
|
|
|
retval = EXIT_SUCCESS;
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2010-04-07 00:10:17 +02:00
|
|
|
}
|
2010-01-28 02:44:47 +01:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
for (;;) {
|
2012-04-05 22:08:10 +02:00
|
|
|
r = manager_loop(m);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_emergency_errno(r, "Failed to run main loop: %m");
|
2014-11-06 06:05:38 +01:00
|
|
|
error_message = "Failed to run main loop";
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2010-01-19 04:15:20 +01:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
switch (m->exit_code) {
|
2010-04-07 00:10:17 +02:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
case MANAGER_RELOAD:
|
2010-07-07 17:44:11 +02:00
|
|
|
log_info("Reloading.");
|
2015-06-29 11:26:27 +02:00
|
|
|
|
|
|
|
r = parse_config_file();
|
|
|
|
if (r < 0)
|
|
|
|
log_error("Failed to parse config file.");
|
|
|
|
|
|
|
|
manager_set_defaults(m);
|
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
r = manager_reload(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to reload: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
break;
|
2010-01-20 04:02:39 +01:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
case MANAGER_REEXECUTE:
|
2012-05-09 01:24:50 +02:00
|
|
|
|
2014-11-06 06:05:38 +01:00
|
|
|
if (prepare_reexecute(m, &arg_serialization, &fds, false) < 0) {
|
2015-08-07 17:13:15 +02:00
|
|
|
error_message = "Failed to prepare for reexecution";
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
reexecute = true;
|
2010-07-07 17:44:11 +02:00
|
|
|
log_notice("Reexecuting.");
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
case MANAGER_SWITCH_ROOT:
|
|
|
|
/* Steal the switch root parameters */
|
2012-05-21 19:33:39 +02:00
|
|
|
switch_root_dir = m->switch_root;
|
2012-05-09 01:24:50 +02:00
|
|
|
switch_root_init = m->switch_root_init;
|
|
|
|
m->switch_root = m->switch_root_init = NULL;
|
|
|
|
|
|
|
|
if (!switch_root_init)
|
2014-11-06 06:05:38 +01:00
|
|
|
if (prepare_reexecute(m, &arg_serialization, &fds, true) < 0) {
|
2015-08-07 17:13:15 +02:00
|
|
|
error_message = "Failed to prepare for reexecution";
|
2012-05-09 01:24:50 +02:00
|
|
|
goto finish;
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2012-05-09 01:24:50 +02:00
|
|
|
|
|
|
|
reexecute = true;
|
|
|
|
log_notice("Switching root.");
|
|
|
|
goto finish;
|
|
|
|
|
2015-09-18 13:37:34 +02:00
|
|
|
case MANAGER_EXIT:
|
2015-09-23 19:50:10 +02:00
|
|
|
retval = m->return_value;
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
if (MANAGER_IS_USER(m)) {
|
2015-09-22 22:56:01 +02:00
|
|
|
log_debug("Exit.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallthrough */
|
2010-10-14 00:52:26 +02:00
|
|
|
case MANAGER_REBOOT:
|
|
|
|
case MANAGER_POWEROFF:
|
|
|
|
case MANAGER_HALT:
|
|
|
|
case MANAGER_KEXEC: {
|
|
|
|
static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
|
2015-09-18 13:37:34 +02:00
|
|
|
[MANAGER_EXIT] = "exit",
|
2010-10-14 00:52:26 +02:00
|
|
|
[MANAGER_REBOOT] = "reboot",
|
|
|
|
[MANAGER_POWEROFF] = "poweroff",
|
|
|
|
[MANAGER_HALT] = "halt",
|
|
|
|
[MANAGER_KEXEC] = "kexec"
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_se(shutdown_verb = table[m->exit_code]);
|
2012-04-05 22:08:10 +02:00
|
|
|
arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
|
2010-10-14 00:52:26 +02:00
|
|
|
|
|
|
|
log_notice("Shutting down.");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown exit code.");
|
|
|
|
}
|
|
|
|
}
|
2010-04-06 23:40:24 +02:00
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
finish:
|
2014-01-07 06:00:05 +01:00
|
|
|
pager_close();
|
|
|
|
|
2015-09-23 19:50:10 +02:00
|
|
|
if (m)
|
2015-01-29 01:23:07 +01:00
|
|
|
arg_shutdown_watchdog = m->shutdown_watchdog;
|
2015-09-23 01:01:26 +02:00
|
|
|
|
2014-11-08 16:06:12 +01:00
|
|
|
m = manager_free(m);
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2015-09-09 14:23:02 +02:00
|
|
|
for (j = 0; j < ELEMENTSOF(arg_default_rlimit); j++)
|
2015-09-08 18:43:11 +02:00
|
|
|
arg_default_rlimit[j] = mfree(arg_default_rlimit[j]);
|
2012-03-21 18:03:40 +01:00
|
|
|
|
2015-07-31 19:56:38 +02:00
|
|
|
arg_default_unit = mfree(arg_default_unit);
|
2015-09-23 01:01:26 +02:00
|
|
|
arg_join_controllers = strv_free_free(arg_join_controllers);
|
2015-07-31 19:56:38 +02:00
|
|
|
arg_default_environment = strv_free(arg_default_environment);
|
2015-09-09 23:12:07 +02:00
|
|
|
arg_syscall_archs = set_free(arg_syscall_archs);
|
2014-02-13 01:35:27 +01:00
|
|
|
|
2014-10-23 10:23:46 +02:00
|
|
|
mac_selinux_finish();
|
2010-08-30 21:31:40 +02:00
|
|
|
|
2010-04-21 03:27:44 +02:00
|
|
|
if (reexecute) {
|
2012-05-09 01:24:50 +02:00
|
|
|
const char **args;
|
2012-05-09 11:13:26 +02:00
|
|
|
unsigned i, args_size;
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
/* Close and disarm the watchdog, so that the new
|
|
|
|
* instance can reinitialize it, but doesn't get
|
|
|
|
* rebooted while we do that */
|
|
|
|
watchdog_close(true);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-09-17 16:35:59 +02:00
|
|
|
/* Reset the RLIMIT_NOFILE to the kernel default, so
|
|
|
|
* that the new systemd can pass the kernel default to
|
|
|
|
* its child processes */
|
|
|
|
if (saved_rlimit_nofile.rlim_cur > 0)
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile);
|
2012-09-17 16:35:59 +02:00
|
|
|
|
2012-05-21 19:33:39 +02:00
|
|
|
if (switch_root_dir) {
|
2012-07-17 17:44:26 +02:00
|
|
|
/* Kill all remaining processes from the
|
|
|
|
* initrd, but don't wait for them, so that we
|
|
|
|
* can handle the SIGCHLD for them after
|
|
|
|
* deserializing. */
|
2013-11-25 18:08:02 +01:00
|
|
|
broadcast_signal(SIGTERM, false, true);
|
2012-07-13 14:41:57 +02:00
|
|
|
|
2014-08-21 16:21:26 +02:00
|
|
|
/* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */
|
|
|
|
r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE);
|
2012-05-21 19:33:39 +02:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to switch root, trying to continue: %m");
|
2012-05-21 19:33:39 +02:00
|
|
|
}
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2016-06-13 16:10:06 +02:00
|
|
|
/* Reopen the console */
|
|
|
|
(void) make_console_stdio();
|
|
|
|
|
2012-05-16 14:22:42 +02:00
|
|
|
args_size = MAX(6, argc+1);
|
2012-05-09 11:13:26 +02:00
|
|
|
args = newa(const char*, args_size);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
if (!switch_root_init) {
|
2015-01-27 14:00:11 +01:00
|
|
|
char sfd[DECIMAL_STR_MAX(int) + 1];
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
/* First try to spawn ourselves with the right
|
|
|
|
* path, and with full serialization. We do
|
|
|
|
* this only if the user didn't specify an
|
|
|
|
* explicit init to spawn. */
|
2010-07-13 18:57:58 +02:00
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
assert(arg_serialization);
|
2012-05-09 01:24:50 +02:00
|
|
|
assert(fds);
|
2010-07-13 18:57:58 +02:00
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(sfd, "%i", fileno(arg_serialization));
|
2010-07-13 18:57:58 +02:00
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
i = 0;
|
|
|
|
args[i++] = SYSTEMD_BINARY_PATH;
|
2012-05-21 19:33:39 +02:00
|
|
|
if (switch_root_dir)
|
2012-05-21 19:48:04 +02:00
|
|
|
args[i++] = "--switched-root";
|
2016-02-24 21:24:23 +01:00
|
|
|
args[i++] = arg_system ? "--system" : "--user";
|
2012-05-09 01:24:50 +02:00
|
|
|
args[i++] = "--deserialize";
|
|
|
|
args[i++] = sfd;
|
|
|
|
args[i++] = NULL;
|
2010-07-13 18:57:58 +02:00
|
|
|
|
2012-05-09 11:13:26 +02:00
|
|
|
assert(i <= args_size);
|
2016-01-19 16:48:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want valgrind to print its memory usage summary before reexecution.
|
|
|
|
* Valgrind won't do this is on its own on exec(), but it will do it on exit().
|
|
|
|
* Hence, to ensure we get a summary here, fork() off a child, let it exit() cleanly,
|
|
|
|
* so that it prints the summary, and wait() for it in the parent, before proceeding into the exec().
|
|
|
|
*/
|
|
|
|
valgrind_summary_hack();
|
|
|
|
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) execv(args[0], (char* const*) args);
|
2012-05-09 01:24:50 +02:00
|
|
|
}
|
2010-08-09 18:00:24 +02:00
|
|
|
|
2012-05-09 01:24:50 +02:00
|
|
|
/* Try the fallback, if there is any, without any
|
|
|
|
* serialization. We pass the original argv[] and
|
|
|
|
* envp[]. (Well, modulo the ordering changes due to
|
|
|
|
* getopt() in argv[], and some cleanups in envp[],
|
|
|
|
* but let's hope that doesn't matter.) */
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-09-09 15:20:10 +02:00
|
|
|
arg_serialization = safe_fclose(arg_serialization);
|
2015-09-23 01:05:55 +02:00
|
|
|
fds = fdset_free(fds);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2014-02-13 01:35:27 +01:00
|
|
|
for (j = 1, i = 1; j < (unsigned) argc; j++)
|
2012-05-09 01:24:50 +02:00
|
|
|
args[i++] = argv[j];
|
2010-04-21 03:27:44 +02:00
|
|
|
args[i++] = NULL;
|
2012-05-09 11:13:26 +02:00
|
|
|
assert(i <= args_size);
|
2012-05-21 17:26:19 +02:00
|
|
|
|
2014-08-26 21:11:35 +02:00
|
|
|
/* Reenable any blocked signals, especially important
|
2014-06-13 16:41:06 +02:00
|
|
|
* if we switch from initial ramdisk to init=... */
|
2015-05-31 23:55:55 +02:00
|
|
|
(void) reset_all_signal_handlers();
|
|
|
|
(void) reset_signal_mask();
|
2014-06-13 16:41:06 +02:00
|
|
|
|
2012-05-21 17:26:19 +02:00
|
|
|
if (switch_root_init) {
|
|
|
|
args[0] = switch_root_init;
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) execv(args[0], (char* const*) args);
|
2014-11-28 19:29:59 +01:00
|
|
|
log_warning_errno(errno, "Failed to execute configured init, trying fallback: %m");
|
2012-05-21 17:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args[0] = "/sbin/init";
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) execv(args[0], (char* const*) args);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2012-05-22 19:11:10 +02:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
log_warning("No /sbin/init, trying fallback");
|
2012-05-21 17:26:19 +02:00
|
|
|
|
2012-05-22 19:11:10 +02:00
|
|
|
args[0] = "/bin/sh";
|
|
|
|
args[1] = NULL;
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) execv(args[0], (char* const*) args);
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to execute /bin/sh, giving up: %m");
|
2012-05-22 19:11:10 +02:00
|
|
|
} else
|
2014-11-28 19:29:59 +01:00
|
|
|
log_warning_errno(errno, "Failed to execute /sbin/init, giving up: %m");
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 15:20:10 +02:00
|
|
|
arg_serialization = safe_fclose(arg_serialization);
|
2015-09-23 01:05:55 +02:00
|
|
|
fds = fdset_free(fds);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2013-11-20 22:11:10 +01:00
|
|
|
#ifdef HAVE_VALGRIND_VALGRIND_H
|
|
|
|
/* If we are PID 1 and running under valgrind, then let's exit
|
|
|
|
* here explicitly. valgrind will only generate nice output on
|
|
|
|
* exit(), not on exec(), hence let's do the former not the
|
|
|
|
* latter here. */
|
|
|
|
if (getpid() == 1 && RUNNING_ON_VALGRIND)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2010-10-14 00:52:26 +02:00
|
|
|
if (shutdown_verb) {
|
2014-02-16 00:13:46 +01:00
|
|
|
char log_level[DECIMAL_STR_MAX(int) + 1];
|
2015-09-18 13:37:34 +02:00
|
|
|
char exit_code[DECIMAL_STR_MAX(uint8_t) + 1];
|
|
|
|
const char* command_line[11] = {
|
2010-10-14 00:52:26 +02:00
|
|
|
SYSTEMD_SHUTDOWN_BINARY_PATH,
|
|
|
|
shutdown_verb,
|
2014-02-16 00:13:46 +01:00
|
|
|
"--log-level", log_level,
|
|
|
|
"--log-target",
|
2010-10-14 00:52:26 +02:00
|
|
|
};
|
2014-02-16 00:13:46 +01:00
|
|
|
unsigned pos = 5;
|
2014-01-04 02:35:27 +01:00
|
|
|
_cleanup_strv_free_ char **env_block = NULL;
|
2014-02-17 16:18:17 +01:00
|
|
|
|
|
|
|
assert(command_line[pos] == NULL);
|
2014-01-04 02:35:27 +01:00
|
|
|
env_block = strv_copy(environ);
|
2010-10-14 00:52:26 +02:00
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(log_level, "%d", log_get_max_level());
|
2014-02-16 00:13:46 +01:00
|
|
|
|
|
|
|
switch (log_get_target()) {
|
2015-09-23 01:06:56 +02:00
|
|
|
|
2014-02-16 00:13:46 +01:00
|
|
|
case LOG_TARGET_KMSG:
|
|
|
|
case LOG_TARGET_JOURNAL_OR_KMSG:
|
|
|
|
case LOG_TARGET_SYSLOG_OR_KMSG:
|
|
|
|
command_line[pos++] = "kmsg";
|
|
|
|
break;
|
|
|
|
|
2015-06-30 15:08:49 +02:00
|
|
|
case LOG_TARGET_NULL:
|
|
|
|
command_line[pos++] = "null";
|
|
|
|
break;
|
|
|
|
|
2014-02-16 00:13:46 +01:00
|
|
|
case LOG_TARGET_CONSOLE:
|
|
|
|
default:
|
|
|
|
command_line[pos++] = "console";
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (log_get_show_color())
|
|
|
|
command_line[pos++] = "--log-color";
|
|
|
|
|
|
|
|
if (log_get_show_location())
|
|
|
|
command_line[pos++] = "--log-location";
|
|
|
|
|
2015-09-18 13:37:34 +02:00
|
|
|
if (streq(shutdown_verb, "exit")) {
|
|
|
|
command_line[pos++] = "--exit-code";
|
|
|
|
command_line[pos++] = exit_code;
|
2015-09-23 19:50:10 +02:00
|
|
|
xsprintf(exit_code, "%d", retval);
|
2015-09-18 13:37:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:32:53 +01:00
|
|
|
assert(pos < ELEMENTSOF(command_line));
|
2014-02-16 00:13:46 +01:00
|
|
|
|
2016-03-29 13:04:04 +02:00
|
|
|
if (arm_reboot_watchdog && arg_shutdown_watchdog > 0 && arg_shutdown_watchdog != USEC_INFINITY) {
|
2014-01-04 02:35:27 +01:00
|
|
|
char *e;
|
2012-04-12 03:19:28 +02:00
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
/* If we reboot let's set the shutdown
|
|
|
|
* watchdog and tell the shutdown binary to
|
|
|
|
* repeatedly ping it */
|
2015-02-12 12:28:48 +01:00
|
|
|
r = watchdog_set_timeout(&arg_shutdown_watchdog);
|
|
|
|
watchdog_close(r < 0);
|
2012-04-05 22:08:10 +02:00
|
|
|
|
2014-01-04 02:35:27 +01:00
|
|
|
/* Tell the binary how often to ping, ignore failure */
|
|
|
|
if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
|
2015-09-23 01:06:56 +02:00
|
|
|
(void) strv_push(&env_block, e);
|
2014-01-04 02:35:27 +01:00
|
|
|
} else
|
2012-04-05 22:08:10 +02:00
|
|
|
watchdog_close(true);
|
|
|
|
|
2013-07-10 23:50:28 +02:00
|
|
|
/* Avoid the creation of new processes forked by the
|
|
|
|
* kernel; at this point, we will not listen to the
|
|
|
|
* signals anyway */
|
2015-09-07 13:42:47 +02:00
|
|
|
if (detect_container() <= 0)
|
2015-09-01 18:02:43 +02:00
|
|
|
(void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
|
2013-07-04 20:31:18 +02:00
|
|
|
|
2012-04-12 03:19:28 +02:00
|
|
|
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to execute shutdown binary, %s: %m",
|
2014-02-16 00:13:46 +01:00
|
|
|
getpid() == 1 ? "freezing" : "quitting");
|
2010-10-14 00:52:26 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 06:05:38 +01:00
|
|
|
if (getpid() == 1) {
|
|
|
|
if (error_message)
|
|
|
|
manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
|
2015-09-19 00:45:05 +02:00
|
|
|
ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL,
|
2014-11-06 06:05:38 +01:00
|
|
|
"%s, freezing.", error_message);
|
2015-09-23 23:13:06 +02:00
|
|
|
freeze_or_reboot();
|
2014-11-06 06:05:38 +01:00
|
|
|
}
|
2010-04-13 04:19:02 +02:00
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
return retval;
|
|
|
|
}
|