main.c revision 5a85ca1cb622fda4a39c8a6f00dccea7f8a1e82a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdio.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <errno.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <string.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <unistd.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include <sys/types.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <sys/stat.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <getopt.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <signal.h>
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include <sys/wait.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <fcntl.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/prctl.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/mount.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#ifdef HAVE_VALGRIND_VALGRIND_H
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <valgrind/valgrind.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#endif
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#ifdef HAVE_SECCOMP
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include <seccomp.h>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#endif
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "sd-daemon.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "sd-messages.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "sd-bus.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "manager.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "log.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "load-fragment.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "fdset.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "special.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "conf-parser.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "missing.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "label.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "build.h"
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen#include "strv.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "def.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "virt.h"
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen#include "architecture.h"
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen#include "watchdog.h"
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen#include "path-util.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "switch-root.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "capability.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "killall.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "env-util.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "clock-util.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "fileio.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "dbus-manager.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "bus-error.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "bus-util.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "mount-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "loopback-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "hostname-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "machine-id-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "selinux-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "ima-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "smack-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#ifdef HAVE_KMOD
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#include "kmod-setup.h"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering#endif
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poetteringstatic enum {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen ACTION_RUN,
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen ACTION_HELP,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering ACTION_VERSION,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering ACTION_TEST,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering ACTION_DUMP_CONFIGURATION_ITEMS,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering ACTION_DONE
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering} arg_action = ACTION_RUN;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic char *arg_default_unit = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic SystemdRunningAs arg_running_as = _SYSTEMD_RUNNING_AS_INVALID;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic bool arg_dump_core = true;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic bool arg_crash_shell = false;
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poetteringstatic int arg_crash_chvt = -1;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic bool arg_confirm_spawn = false;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic bool arg_switched_root = false;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic char ***arg_join_controllers = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_runtime_watchdog = 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic char **arg_default_environment = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic struct rlimit *arg_default_rlimit[_RLIMIT_MAX] = {};
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic uint64_t arg_capability_bounding_set_drop = 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic nsec_t arg_timer_slack_nsec = (nsec_t) -1;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic usec_t arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic Set* arg_syscall_archs = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic FILE* arg_serialization = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_default_cpu_accounting = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_default_blockio_accounting = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_default_memory_accounting = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void nop_handler(int sig) {}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringnoreturn static void crash(int sig) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (getpid() != 1)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Pass this on immediately, if this is not PID 1 */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering raise(sig);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (!arg_dump_core)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct sigaction sa = {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .sa_handler = nop_handler,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .sa_flags = SA_NOCLDSTOP|SA_RESTART,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering };
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pid_t pid;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering /* We want to wait for the core process, hence let's enable SIGCHLD */
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering sigaction(SIGCHLD, &sa, NULL);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering pid = fork();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (pid < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (pid == 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct rlimit rl = {};
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Enable default signal handler for core dump */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering zero(sa);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sa.sa_handler = SIG_DFL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sigaction(sig, &sa, NULL);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Don't limit the core dump size */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rl.rlim_cur = RLIM_INFINITY;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rl.rlim_max = RLIM_INFINITY;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering setrlimit(RLIMIT_CORE, &rl);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Just to be sure... */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering chdir("/");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Raise the signal again */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering raise(sig);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering assert_not_reached("We shouldn't be here...");
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering _exit(1);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering } else {
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering siginfo_t status;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering int r;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering /* Order things nicely. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = wait_for_terminate(pid, &status);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering else if (status.si_code != CLD_DUMPED)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (arg_crash_chvt)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering chvt(arg_crash_chvt);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (arg_crash_shell) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct sigaction sa = {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .sa_handler = SIG_IGN,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering };
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pid_t pid;
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_info("Executing crash shell in 10s...");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering sleep(10);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Let the kernel reap children for us */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering pid = fork();
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering if (pid < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Failed to fork off crash shell: %m");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering else if (pid == 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering make_console_stdio();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering execl("/bin/sh", "/bin/sh", NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("execl() failed: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _exit(1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_info("Successfully spawned crash shell as pid "PID_FMT".", pid);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_info("Freezing execution.");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering freeze();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic void install_crash_handler(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering struct sigaction sa = {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering .sa_handler = crash,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering .sa_flags = SA_NODEFER,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering };
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int console_setup(bool do_reset) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int tty_fd, r;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* If we are init, we connect stdin/stdout/stderr to /dev/null
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering * and make sure we don't have a controlling tty. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen release_terminal();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (!do_reset)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (tty_fd < 0) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return -tty_fd;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* We don't want to force text mode.
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering * plymouth may be showing pictures already from initrd. */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = reset_terminal_fd(tty_fd, false);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Failed to reset /dev/console: %s", strerror(-r));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering safe_close(tty_fd);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int set_default_unit(const char *u) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen char *c;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen assert(u);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering c = strdup(u);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!c)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -ENOMEM;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering free(arg_default_unit);
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering arg_default_unit = c;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return 0;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering}
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poetteringstatic int parse_proc_cmdline_item(const char *key, const char *value) {
65b200e70d01cb7c513114b602c9b5cf41348f77Lennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering static const char * const rlmap[] = {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "emergency", SPECIAL_EMERGENCY_TARGET,
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "-b", SPECIAL_EMERGENCY_TARGET,
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "single", SPECIAL_RESCUE_TARGET,
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "-s", SPECIAL_RESCUE_TARGET,
0936416a1cba187dc76903dae73246f4cbddf312Lennart Poettering "s", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "S", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "1", SPECIAL_RESCUE_TARGET,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "2", SPECIAL_RUNLEVEL2_TARGET,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "3", SPECIAL_RUNLEVEL3_TARGET,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "4", SPECIAL_RUNLEVEL4_TARGET,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "5", SPECIAL_RUNLEVEL5_TARGET,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering };
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen int r;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert(key);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (streq(key, "systemd.unit") && value) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!in_initrd())
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return set_default_unit(value);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (streq(key, "rd.systemd.unit") && value) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return set_default_unit(value);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else if (streq(key, "systemd.log_target") && value) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (log_set_target_from_string(value) < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_warning("Failed to parse log target %s. Ignoring.", value);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else if (streq(key, "systemd.log_level") && value) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (log_set_max_level_from_string(value) < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_warning("Failed to parse log level %s. Ignoring.", value);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else if (streq(key, "systemd.log_color") && value) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (log_show_color_from_string(value) < 0)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_warning("Failed to parse log color setting %s. Ignoring.", value);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt } else if (streq(key, "systemd.log_location") && value) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (log_show_location_from_string(value) < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to parse log location setting %s. Ignoring.", value);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else if (streq(key, "systemd.dump_core") && value) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = parse_boolean(value);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to parse dump core switch %s. Ignoring.", value);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_dump_core = r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else if (streq(key, "systemd.crash_shell") && value) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = parse_boolean(value);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to parse crash shell switch %s. Ignoring.", value);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_crash_shell = r;
c52a97b896c914e17ba5be73c0e806455fd9ad4dLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else if (streq(key, "systemd.crash_chvt") && value) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (safe_atoi(value, &r) < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to parse crash chvt switch %s. Ignoring.", value);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_crash_chvt = r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (streq(key, "systemd.confirm_spawn") && value) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = parse_boolean(value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse confirm spawn switch %s. Ignoring.", value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_confirm_spawn = r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (streq(key, "systemd.show_status") && value) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = parse_show_status(value, &arg_show_status);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_warning("Failed to parse show status switch %s. Ignoring.", value);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering } else if (streq(key, "systemd.default_standard_output") && value) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering r = exec_output_from_string(value);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (r < 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_warning("Failed to parse default standard output switch %s. Ignoring.", value);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering else
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering arg_default_std_output = r;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (streq(key, "systemd.default_standard_error") && value) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = exec_output_from_string(value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse default standard error switch %s. Ignoring.", value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_default_std_error = r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (streq(key, "systemd.setenv") && value) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (env_assignment_is_valid(value)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char **env;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering env = strv_env_set(arg_default_environment, value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (env)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_default_environment = env;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Setting environment variable '%s' failed, ignoring: %s", value, strerror(ENOMEM));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (!streq(key, "systemd.restore_state") &&
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek !streq(key, "systemd.gpt_auto") &&
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering (startswith(key, "systemd.") || startswith(key, "rd.systemd."))) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering const char *c;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* Ignore systemd.journald.xyz and friends */
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek c = key;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (startswith(c, "rd."))
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek c += 3;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering if (startswith(c, "systemd."))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering c += 8;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (c[strcspn(c, ".=")] != '.') {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_warning("Unknown kernel switch %s. Ignoring.", key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_info("Supported kernel switches:\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.unit=UNIT Default unit to start\n"
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "rd.systemd.unit=UNIT Default unit to start when run in initrd\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.dump_core=0|1 Dump core on crash\n"
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "systemd.crash_shell=0|1 Run shell on crash\n"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering "systemd.crash_chvt=N Change to VT #N on crash\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "systemd.show_status=0|1|auto Show status updates on the console during bootup\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering " Log target\n"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "systemd.log_level=LEVEL Log level\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.log_color=0|1 Highlight important log messages\n"
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "systemd.log_location=0|1 Include code location in log messages\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek " Set default log output for services\n"
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering " Set default log error output for services\n"
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen "systemd.setenv=ASSIGNMENT Set an environment variable for all spawned processes\n"
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen "systemd.restore_state=0|1 Restore backlight/rfkill state at boot\n");
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering } else if (streq(key, "quiet") && !value) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (arg_show_status == _SHOW_STATUS_UNSET)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering arg_show_status = SHOW_STATUS_AUTO;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek } else if (streq(key, "debug") && !value) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_set_max_level(LOG_DEBUG);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (detect_container(NULL) > 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_set_target(LOG_TARGET_CONSOLE);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek } else if (!in_initrd() && !value) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek unsigned i;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek /* SysV compatibility */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (streq(key, rlmap[i]))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return set_default_unit(rlmap[i+1]);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen#define DEFINE_SETTER(name, func, descr) \
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen static int name(const char *unit, \
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen const char *filename, \
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek unsigned line, \
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek const char *section, \
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek unsigned section_line, \
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek const char *lvalue, \
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int ltype, \
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek const char *rvalue, \
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *data, \
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek void *userdata) { \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering \
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering int r; \
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering \
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(filename); \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(lvalue); \
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering assert(rvalue); \
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = func(rvalue); \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0) \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -r, \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "Invalid " descr "'%s': %s", \
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering rvalue, strerror(-r)); \
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering \
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0; \
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringDEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringDEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringDEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringDEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic int config_parse_cpu_affinity2(
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *unit,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *filename,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering unsigned line,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *section,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering unsigned section_line,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *lvalue,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int ltype,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *rvalue,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering void *data,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering void *userdata) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char *w;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering size_t l;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char *state;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering cpu_set_t *c = NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering unsigned ncpus = 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(filename);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(lvalue);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(rvalue);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering FOREACH_WORD_QUOTED(w, l, rvalue, state) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char *t;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering unsigned cpu;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!(t = strndup(w, l)))
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return log_oom();
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = safe_atou(t, &cpu);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering free(t);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!c)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!(c = cpu_set_malloc(&ncpus)))
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return log_oom();
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0 || cpu >= ncpus) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -r,
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering "Failed to parse CPU affinity '%s'", rvalue);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering CPU_FREE(c);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -EBADMSG;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (c) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_warning_unit(unit, "Failed to set CPU affinity: %m");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering CPU_FREE(c);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int config_parse_show_status(
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char* unit,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *filename,
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering unsigned line,
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering const char *section,
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering unsigned section_line,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *lvalue,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int ltype,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *rvalue,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering void *data,
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering void *userdata) {
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering int k;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering ShowStatus *b = data;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(filename);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(lvalue);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(rvalue);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(data);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering k = parse_show_status(rvalue, b);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (k < 0) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -k,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "Failed to parse show status setting, ignoring: %s", rvalue);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void strv_free_free(char ***l) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char ***i;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!l)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return;
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering for (i = l; *i; i++)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek strv_free(*i);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering free(l);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic void free_join_controllers(void) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strv_free_free(arg_join_controllers);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_join_controllers = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int config_parse_join_controllers(const char *unit,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char *filename,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering unsigned line,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char *section,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering unsigned section_line,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char *lvalue,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering int ltype,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char *rvalue,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering void *data,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering void *userdata) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek unsigned n = 0;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek char *state, *w;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek size_t length;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek assert(filename);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek assert(lvalue);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(rvalue);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek free_join_controllers();
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, length, rvalue, state) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char *s, **l;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek s = strndup(w, length);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!s)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return log_oom();
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen l = strv_split(s, ",");
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(s);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen strv_uniq(l);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (strv_length(l) <= 1) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering strv_free(l);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering continue;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!arg_join_controllers) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering arg_join_controllers = new(char**, 2);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (!arg_join_controllers) {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering strv_free(l);
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering return log_oom();
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_join_controllers[0] = l;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_join_controllers[1] = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek n = 1;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn } else {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn char ***a;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn char ***t;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn t = new0(char**, n+2);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (!t) {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn strv_free(l);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return log_oom();
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek n = 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek for (a = arg_join_controllers; *a; a++) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (strv_overlap(*a, l)) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (strv_extend_strv(&l, *a) < 0) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen strv_free(l);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen strv_free_free(t);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_oom();
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen } else {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen char **c;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen c = strv_copy(*a);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!c) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strv_free(l);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strv_free_free(t);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return log_oom();
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t[n++] = c;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek t[n++] = strv_uniq(l);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek strv_free_free(arg_join_controllers);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek arg_join_controllers = t;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int parse_config_file(void) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const ConfigTableItem items[] = {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "LogLevel", config_parse_level2, 0, NULL },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "LogTarget", config_parse_target, 0, NULL },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "LogColor", config_parse_color, 0, NULL },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "LogLocation", config_parse_location, 0, NULL },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "CapabilityBoundingSet", config_parse_bounding_set, 0, &arg_capability_bounding_set_drop },
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#ifdef HAVE_SECCOMP
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek { "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen#endif
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultLimitCPU", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CPU] },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultLimitFSIZE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_FSIZE] },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultLimitDATA", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_DATA] },
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen { "Manager", "DefaultLimitSTACK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_STACK] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitCORE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CORE] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitRSS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RSS] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitNOFILE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NOFILE] },
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen { "Manager", "DefaultLimitAS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_AS] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitNPROC", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NPROC] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MEMLOCK] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitLOCKS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_LOCKS] },
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen { "Manager", "DefaultLimitSIGPENDING", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_SIGPENDING] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MSGQUEUE] },
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering { "Manager", "DefaultLimitNICE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NICE] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitRTPRIO", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTPRIO] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultLimitRTTIME", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTTIME] },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultCPUAccounting", config_parse_bool, 0, &arg_default_cpu_accounting },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen { "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting },
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen {}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen };
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen _cleanup_fclose_ FILE *f;
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering const char *fn;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf";
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen f = fopen(fn, "re");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!f) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (errno == ENOENT)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_warning("Failed to open configuration file '%s': %m", fn);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = config_parse(NULL, fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, false, NULL);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_warning("Failed to parse configuration file: %s", strerror(-r));
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering enum {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_LOG_LEVEL = 0x100,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_LOG_TARGET,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_LOG_COLOR,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_LOG_LOCATION,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_UNIT,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_SYSTEM,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_USER,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_TEST,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_VERSION,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_DUMP_CONFIGURATION_ITEMS,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_DUMP_CORE,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_CRASH_SHELL,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_CONFIRM_SPAWN,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_SHOW_STATUS,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_DESERIALIZE,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_SWITCHED_ROOT,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_DEFAULT_STD_OUTPUT,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ARG_DEFAULT_STD_ERROR
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering };
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering static const struct option options[] = {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "log-target", required_argument, NULL, ARG_LOG_TARGET },
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "unit", required_argument, NULL, ARG_UNIT },
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering { "system", no_argument, NULL, ARG_SYSTEM },
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering { "user", no_argument, NULL, ARG_USER },
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek { "test", no_argument, NULL, ARG_TEST },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "help", no_argument, NULL, 'h' },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering {}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek };
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int c, r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(argc >= 1);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(argv);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (getpid() == 1)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering opterr = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering switch (c) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case ARG_LOG_LEVEL:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = log_set_max_level_from_string(optarg);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering if (r < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_error("Failed to parse log level %s.", optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case ARG_LOG_TARGET:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = log_set_target_from_string(optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed to parse log target %s.", optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek break;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek case ARG_LOG_COLOR:
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (optarg) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek r = log_show_color_from_string(optarg);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (r < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed to parse log color setting %s.", optarg);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return r;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_show_color(true);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering case ARG_LOG_LOCATION:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (optarg) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = log_show_location_from_string(optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed to parse log location setting %s.", optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_show_location(true);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case ARG_DEFAULT_STD_OUTPUT:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = exec_output_from_string(optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed to parse default standard output setting %s.", optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_default_std_output = r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case ARG_DEFAULT_STD_ERROR:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = exec_output_from_string(optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_error("Failed to parse default standard error output setting %s.", optarg);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_default_std_error = r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case ARG_UNIT:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = set_default_unit(optarg);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek }
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering break;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek case ARG_SYSTEM:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_running_as = SYSTEMD_SYSTEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek break;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek case ARG_USER:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_running_as = SYSTEMD_USER;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek break;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek case ARG_TEST:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_action = ACTION_TEST;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek case ARG_VERSION:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_action = ACTION_VERSION;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek case ARG_DUMP_CONFIGURATION_ITEMS:
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen break;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen case ARG_DUMP_CORE:
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering r = optarg ? parse_boolean(optarg) : 1;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_error("Failed to parse dump core boolean %s.", optarg);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen arg_dump_core = r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen break;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen case ARG_CRASH_SHELL:
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering r = optarg ? parse_boolean(optarg) : 1;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_error("Failed to parse crash shell boolean %s.", optarg);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_crash_shell = r;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering break;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering case ARG_CONFIRM_SPAWN:
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = optarg ? parse_boolean(optarg) : 1;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (r < 0) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_error("Failed to parse confirm spawn boolean %s.", optarg);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering arg_confirm_spawn = r;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering break;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek case ARG_SHOW_STATUS:
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (optarg) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = parse_show_status(optarg, &arg_show_status);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (r < 0) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek log_error("Failed to parse show status boolean %s.", optarg);
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen return r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_show_status = SHOW_STATUS_YES;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering break;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering case ARG_DESERIALIZE: {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering int fd;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek FILE *f;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = safe_atoi(optarg, &fd);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0 || fd < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed to parse deserialize option %s.", optarg);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r < 0 ? r : -EINVAL;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek fd_cloexec(fd, true);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen f = fdopen(fd, "r");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!f) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error("Failed to open serialization fd: %m");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -errno;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (arg_serialization)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering fclose(arg_serialization);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen arg_serialization = f;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering break;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen case ARG_SWITCHED_ROOT:
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering arg_switched_root = true;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen break;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek case 'h':
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek arg_action = ACTION_HELP;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen break;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek case 'D':
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_set_max_level(LOG_DEBUG);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek break;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek case 'b':
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek case 's':
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen case 'z':
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Just to eat away the sysvinit kernel
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek * cmdline args without getopt() error
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek * messages that we'll parse in
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek * parse_proc_cmdline_word() or ignore. */
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering case '?':
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek default:
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (getpid() != 1) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error("Unknown option code %c", c);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -EINVAL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering break;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (optind < argc && getpid() != 1) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Hmm, when we aren't run as init system
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen * let's complain about excess arguments */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Excess arguments.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -EINVAL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (detect_container(NULL) > 0) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen char **a;
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen /* All /proc/cmdline arguments the kernel didn't
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * understand it passed to us. We're not really
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering * interested in that usually since /proc/cmdline is
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * more interesting and complete. With one exception:
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * if we are run in a container /proc/cmdline is not
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * relevant for the container, hence we rely on argv[]
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * instead. */
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen for (a = argv; a < argv + argc; a++) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen _cleanup_free_ char *w;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen char *value;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen w = strdup(*a);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!w)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_oom();
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen value = strchr(w, '=');
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (value)
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering *(value++) = 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = parse_proc_cmdline_item(w, value);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int help(void) {
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen printf("%s [OPTIONS...]\n\n"
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering "Starts up and maintains the system or user services.\n\n"
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering " -h --help Show this help\n"
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek " --test Determine startup sequence, dump it and exit\n"
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering " --dump-configuration-items Dump understood unit configuration items\n"
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering " --unit=UNIT Set default unit\n"
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering " --system Run a system instance, even if PID != 1\n"
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering " --user Run a user instance\n"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering " --dump-core[=0|1] Dump core on crash\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --crash-shell[=0|1] Run shell on crash\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --confirm-spawn[=0|1] Ask for confirmation when spawning processes\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --show-status[=0|1] Show status updates on the console during bootup\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --log-color[=0|1] Highlight important log messages\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --log-location[=0|1] Include code location in log messages\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --default-standard-output= Set default standard output for services\n"
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering " --default-standard-error= Set default standard error output for services\n",
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering program_invocation_short_name);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringstatic int version(void) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering puts(PACKAGE_STRING);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering puts(SYSTEMD_FEATURES);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringstatic int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering FILE *f = NULL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering FDSet *fds = NULL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering int r;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering assert(m);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering assert(_f);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering assert(_fds);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering r = manager_open_serialization(m, &f);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (r < 0) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering log_error("Failed to create serialization file: %s", strerror(-r));
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering goto fail;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering }
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering /* Make sure nothing is really destructed when we shut down */
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering m->n_reloading ++;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering bus_manager_send_reloading(m, true);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering fds = fdset_new();
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (!fds) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering r = -ENOMEM;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering log_error("Failed to allocate fd set: %s", strerror(-r));
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = manager_serialize(m, f, fds, switching_root);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_error("Failed to serialize state: %s", strerror(-r));
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (fseeko(f, 0, SEEK_SET) < 0) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_error("Failed to rewind serialization fd: %m");
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = fd_cloexec(fileno(f), false);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering if (r < 0) {
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = fdset_cloexec(fds, false);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering *_f = f;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering *_fds = fds;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringfail:
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering fdset_free(fds);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (f)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering fclose(f);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering struct rlimit nl;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering int r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(saved_rlimit);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering /* Save the original RLIMIT_NOFILE so that we can reset it
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering * later when transitioning from the initrd to the main
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering * systemd or suchlike. */
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering log_error("Reading RLIMIT_NOFILE failed: %m");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return -errno;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
/* 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);
if (r < 0) {
log_error("Setting RLIMIT_NOFILE failed: %s", strerror(-r));
return r;
}
return 0;
}
static void test_mtab(void) {
static const char ok[] =
"/proc/self/mounts\0"
"/proc/mounts\0"
"../proc/self/mounts\0"
"../proc/mounts\0";
_cleanup_free_ char *p = NULL;
int r;
/* Check that /etc/mtab is a symlink to the right place or
* non-existing. But certainly not a file, or a symlink to
* some weird place... */
r = readlink_malloc("/etc/mtab", &p);
if (r == -ENOENT)
return;
if (r >= 0 && nulstr_contains(ok, p))
return;
log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
"This is not supported anymore. "
"Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
}
static void test_usr(void) {
/* Check that /usr is not a separate fs */
if (dir_is_empty("/usr") <= 0)
return;
log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
"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.");
}
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
* "cpuset" does't really work for groups with no initialized
* attributes. */
arg_join_controllers = new(char**, 3);
if (!arg_join_controllers)
return -ENOMEM;
arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL);
arg_join_controllers[2] = NULL;
if (!arg_join_controllers[0] || !arg_join_controllers[1]) {
free_join_controllers();
return -ENOMEM;
}
return 0;
}
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) {
log_error("Failed to add architecture to seccomp: %s", strerror(-r));
goto finish;
}
}
r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0);
if (r < 0) {
log_error("Failed to unset NO_NEW_PRIVS: %s", strerror(-r));
goto finish;
}
r = seccomp_load(seccomp);
if (r < 0)
log_error("Failed to add install architecture seccomp: %s", strerror(-r));
finish:
seccomp_release(seccomp);
return r;
#else
return 0;
#endif
}
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);
if (r == -ENOENT) {
r = parse_env_file("/usr/lib/os-release", NEWLINE,
"PRETTY_NAME", &pretty_name,
"ANSI_COLOR", &ansi_color,
NULL);
}
if (r < 0 && r != -ENOENT)
log_warning("Failed to read os-release file: %s", strerror(-r));
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);
}
static int write_container_id(void) {
const char *c;
c = getenv("container");
if (isempty(c))
return 0;
return write_string_file("/run/systemd/container", c);
}
int main(int argc, char *argv[]) {
Manager *m = NULL;
int r, retval = EXIT_FAILURE;
usec_t before_startup, after_startup;
char timespan[FORMAT_TIMESPAN_MAX];
FDSet *fds = NULL;
bool reexecute = false;
const char *shutdown_verb = NULL;
dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
dual_timestamp userspace_timestamp = { 0ULL, 0ULL };
dual_timestamp kernel_timestamp = { 0ULL, 0ULL };
dual_timestamp security_start_timestamp = { 0ULL, 0ULL };
dual_timestamp security_finish_timestamp = { 0ULL, 0ULL };
static char systemd[] = "systemd";
bool skip_setup = false;
unsigned j;
bool loaded_policy = false;
bool arm_reboot_watchdog = false;
bool queue_default_job = false;
char *switch_root_dir = NULL, *switch_root_init = NULL;
static struct rlimit saved_rlimit_nofile = { 0, 0 };
#ifdef HAVE_SYSV_COMPAT
if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
/* This is compatibility support for SysV, where
* calling init as a user is identical to telinit. */
errno = -ENOENT;
execv(SYSTEMCTL_BINARY_PATH, argv);
log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
return 1;
}
#endif
dual_timestamp_from_monotonic(&kernel_timestamp, 0);
dual_timestamp_get(&userspace_timestamp);
/* 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. */
if (strv_find(argv+1, "--deserialize"))
skip_setup = true;
/* If we have switched root, do all the special setup
* things */
if (strv_find(argv+1, "--switched-root"))
skip_setup = false;
/* 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);
saved_argv = argv;
saved_argc = argc;
log_show_color(isatty(STDERR_FILENO) > 0);
/* Disable the umask logic */
if (getpid() == 1)
umask(0);
if (getpid() == 1 && detect_container(NULL) <= 0) {
/* Running outside of a container as PID 1 */
arg_running_as = SYSTEMD_SYSTEM;
make_null_stdio();
log_set_target(LOG_TARGET_KMSG);
log_open();
if (in_initrd())
initrd_timestamp = userspace_timestamp;
if (!skip_setup) {
mount_setup_early();
dual_timestamp_get(&security_start_timestamp);
if (selinux_setup(&loaded_policy) < 0)
goto finish;
if (ima_setup() < 0)
goto finish;
if (smack_setup(&loaded_policy) < 0)
goto finish;
dual_timestamp_get(&security_finish_timestamp);
}
if (label_init(NULL) < 0)
goto finish;
if (!skip_setup) {
if (clock_is_localtime() > 0) {
int min;
/* The first-time call to settimeofday() does a time warp in the kernel */
r = clock_set_timezone(&min);
if (r < 0)
log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
else
log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
} else if (!in_initrd()) {
/*
* Do dummy first-time call to seal the kernel's time warp magic
*
* Do not call this this from inside the initrd. The initrd might not
* 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.
*/
clock_reset_timezone();
/* Tell the kernel our timezone */
r = clock_set_timezone(NULL);
if (r < 0)
log_error("Failed to set the kernel's timezone, ignoring: %s", strerror(-r));
}
}
/* 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 */
arg_running_as = SYSTEMD_SYSTEM;
log_set_target(LOG_TARGET_CONSOLE);
log_close_console(); /* force reopen of /dev/console */
log_open();
/* For the later on, see above... */
log_set_target(LOG_TARGET_JOURNAL);
/* clear the kernel timestamp,
* because we are in a container */
kernel_timestamp.monotonic = 0ULL;
kernel_timestamp.realtime = 0ULL;
} else {
/* Running as user instance */
arg_running_as = SYSTEMD_USER;
log_set_target(LOG_TARGET_AUTO);
log_open();
/* clear the kernel timestamp,
* because we are not PID 1 */
kernel_timestamp.monotonic = 0ULL;
kernel_timestamp.realtime = 0ULL;
}
/* Initialize default unit */
r = set_default_unit(SPECIAL_DEFAULT_TARGET);
if (r < 0) {
log_error("Failed to set default unit %s: %s", SPECIAL_DEFAULT_TARGET, strerror(-r));
goto finish;
}
r = initialize_join_controllers();
if (r < 0)
goto finish;
/* Mount /proc, /sys and friends, so that /proc/cmdline and
* /proc/$PID/fd is available. */
if (getpid() == 1) {
r = mount_setup(loaded_policy);
if (r < 0)
goto finish;
}
/* Reset all signal handlers. */
assert_se(reset_all_signal_handlers() == 0);
ignore_signals(SIGNALS_IGNORE, -1);
if (parse_config_file() < 0)
goto finish;
if (arg_running_as == SYSTEMD_SYSTEM)
if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
goto finish;
log_parse_environment();
if (parse_argv(argc, argv) < 0)
goto finish;
if (arg_action == ACTION_TEST &&
geteuid() == 0) {
log_error("Don't run test mode as root.");
goto finish;
}
if (arg_running_as == SYSTEMD_USER &&
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;
}
if (arg_running_as == SYSTEMD_SYSTEM &&
arg_action == ACTION_RUN &&
running_in_chroot() > 0) {
log_error("Cannot be run in a chroot() environment.");
goto finish;
}
if (arg_action == ACTION_HELP) {
retval = help();
goto finish;
} else if (arg_action == ACTION_VERSION) {
retval = version();
goto finish;
} else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
unit_dump_config_items(stdout);
retval = EXIT_SUCCESS;
goto finish;
} else if (arg_action == ACTION_DONE) {
retval = EXIT_SUCCESS;
goto finish;
}
if (arg_running_as == SYSTEMD_USER &&
!getenv("XDG_RUNTIME_DIR")) {
log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
goto finish;
}
assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
/* Close logging fds, in order not to confuse fdset below */
log_close();
/* Remember open file descriptors for later deserialization */
r = fdset_new_fill(&fds);
if (r < 0) {
log_error("Failed to allocate fd set: %s", strerror(-r));
goto finish;
} else
fdset_cloexec(fds, true);
if (arg_serialization)
assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0);
if (arg_running_as == SYSTEMD_SYSTEM)
/* Become a session leader if we aren't one yet. */
setsid();
/* Move out of the way, so that we won't block unmounts */
assert_se(chdir("/") == 0);
/* Reset the console, but only if this is really init and we
* are freshly booted */
if (arg_running_as == SYSTEMD_SYSTEM && arg_action == ACTION_RUN)
console_setup(getpid() == 1 && !skip_setup);
/* Open the logging devices, if possible and necessary */
log_open();
if (arg_show_status == _SHOW_STATUS_UNSET)
arg_show_status = SHOW_STATUS_YES;
/* Make sure we leave a core dump without panicing the
* kernel. */
if (getpid() == 1) {
install_crash_handler();
r = mount_cgroup_controllers(arg_join_controllers);
if (r < 0)
goto finish;
}
if (arg_running_as == SYSTEMD_SYSTEM) {
const char *virtualization = NULL;
log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES ")");
detect_virtualization(&virtualization);
if (virtualization)
log_info("Detected virtualization '%s'.", virtualization);
write_container_id();
log_info("Detected architecture '%s'.", architecture_to_string(uname_architecture()));
if (in_initrd())
log_info("Running in initial RAM disk.");
} else {
_cleanup_free_ char *t = uid_to_name(getuid());
log_debug(PACKAGE_STRING " running in user mode for user "PID_FMT"/%s. (" SYSTEMD_FEATURES ")",
getuid(), t);
}
if (arg_running_as == SYSTEMD_SYSTEM && !skip_setup) {
if (arg_show_status > 0 || plymouth_running())
status_welcome();
#ifdef HAVE_KMOD
if (detect_container(NULL) <= 0)
kmod_setup();
#endif
hostname_setup();
machine_id_setup("");
loopback_setup();
test_mtab();
test_usr();
}
if (arg_running_as == SYSTEMD_SYSTEM && arg_runtime_watchdog > 0)
watchdog_set_timeout(&arg_runtime_watchdog);
if (arg_timer_slack_nsec != (nsec_t) -1)
if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0)
log_error("Failed to adjust timer slack: %m");
if (arg_capability_bounding_set_drop) {
r = capability_bounding_set_drop_usermode(arg_capability_bounding_set_drop);
if (r < 0) {
log_error("Failed to drop capability bounding set of usermode helpers: %s", strerror(-r));
goto finish;
}
r = capability_bounding_set_drop(arg_capability_bounding_set_drop, true);
if (r < 0) {
log_error("Failed to drop capability bounding set: %s", strerror(-r));
goto finish;
}
}
if (arg_syscall_archs) {
r = enforce_syscall_archs(arg_syscall_archs);
if (r < 0)
goto finish;
}
if (arg_running_as == SYSTEMD_USER) {
/* Become reaper of our children */
if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) {
log_warning("Failed to make us a subreaper: %m");
if (errno == EINVAL)
log_info("Perhaps the kernel version is too old (< 3.4?)");
}
}
if (arg_running_as == SYSTEMD_SYSTEM)
bump_rlimit_nofile(&saved_rlimit_nofile);
r = manager_new(arg_running_as, &m);
if (r < 0) {
log_error("Failed to allocate manager object: %s", strerror(-r));
goto finish;
}
m->confirm_spawn = arg_confirm_spawn;
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_restart_usec = arg_default_restart_usec;
m->default_timeout_start_usec = arg_default_timeout_start_usec;
m->default_timeout_stop_usec = arg_default_timeout_stop_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;
m->default_blockio_accounting = arg_default_blockio_accounting;
m->default_memory_accounting = arg_default_memory_accounting;
m->runtime_watchdog = arg_runtime_watchdog;
m->shutdown_watchdog = arg_shutdown_watchdog;
m->userspace_timestamp = userspace_timestamp;
m->kernel_timestamp = kernel_timestamp;
m->initrd_timestamp = initrd_timestamp;
m->security_start_timestamp = security_start_timestamp;
m->security_finish_timestamp = security_finish_timestamp;
manager_set_default_rlimits(m, arg_default_rlimit);
manager_environment_add(m, NULL, arg_default_environment);
manager_set_show_status(m, arg_show_status);
/* Remember whether we should queue the default job */
queue_default_job = !arg_serialization || arg_switched_root;
before_startup = now(CLOCK_MONOTONIC);
r = manager_startup(m, arg_serialization, fds);
if (r < 0)
log_error("Failed to fully start up daemon: %s", strerror(-r));
/* This will close all file descriptors that were opened, but
* not claimed by any unit. */
fdset_free(fds);
fds = NULL;
if (arg_serialization) {
fclose(arg_serialization);
arg_serialization = NULL;
}
if (queue_default_job) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
Unit *target = NULL;
Job *default_unit_job;
log_debug("Activating default unit: %s", arg_default_unit);
r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
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)
log_error("Failed to load default target: %s", strerror(-target->load_error));
else if (target->load_state == UNIT_MASKED)
log_error("Default target masked.");
if (!target || target->load_state != UNIT_LOADED) {
log_info("Trying to load rescue target...");
r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
if (r < 0) {
log_error("Failed to load rescue target: %s", bus_error_message(&error, r));
goto finish;
} else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) {
log_error("Failed to load rescue target: %s", strerror(-target->load_error));
goto finish;
} else if (target->load_state == UNIT_MASKED) {
log_error("Rescue target masked.");
goto finish;
}
}
assert(target->load_state == UNIT_LOADED);
if (arg_action == ACTION_TEST) {
printf("-> By units:\n");
manager_dump_units(m, stdout, "\t");
}
r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, false, &error, &default_unit_job);
if (r == -EPERM) {
log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r));
r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &default_unit_job);
if (r < 0) {
log_error("Failed to start default target: %s", bus_error_message(&error, r));
goto finish;
}
} else if (r < 0) {
log_error("Failed to isolate default target: %s", bus_error_message(&error, r));
goto finish;
}
m->default_unit_job_id = default_unit_job->id;
after_startup = now(CLOCK_MONOTONIC);
log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
"Loaded units and determined initial transaction in %s.",
format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 0));
if (arg_action == ACTION_TEST) {
printf("-> By jobs:\n");
manager_dump_jobs(m, stdout, "\t");
retval = EXIT_SUCCESS;
goto finish;
}
}
for (;;) {
r = manager_loop(m);
if (r < 0) {
log_error("Failed to run mainloop: %s", strerror(-r));
goto finish;
}
switch (m->exit_code) {
case MANAGER_EXIT:
retval = EXIT_SUCCESS;
log_debug("Exit.");
goto finish;
case MANAGER_RELOAD:
log_info("Reloading.");
r = manager_reload(m);
if (r < 0)
log_error("Failed to reload: %s", strerror(-r));
break;
case MANAGER_REEXECUTE:
if (prepare_reexecute(m, &arg_serialization, &fds, false) < 0)
goto finish;
reexecute = true;
log_notice("Reexecuting.");
goto finish;
case MANAGER_SWITCH_ROOT:
/* Steal the switch root parameters */
switch_root_dir = m->switch_root;
switch_root_init = m->switch_root_init;
m->switch_root = m->switch_root_init = NULL;
if (!switch_root_init)
if (prepare_reexecute(m, &arg_serialization, &fds, true) < 0)
goto finish;
reexecute = true;
log_notice("Switching root.");
goto finish;
case MANAGER_REBOOT:
case MANAGER_POWEROFF:
case MANAGER_HALT:
case MANAGER_KEXEC: {
static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
[MANAGER_REBOOT] = "reboot",
[MANAGER_POWEROFF] = "poweroff",
[MANAGER_HALT] = "halt",
[MANAGER_KEXEC] = "kexec"
};
assert_se(shutdown_verb = table[m->exit_code]);
arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
log_notice("Shutting down.");
goto finish;
}
default:
assert_not_reached("Unknown exit code.");
}
}
finish:
if (m) {
manager_free(m);
m = NULL;
}
for (j = 0; j < ELEMENTSOF(arg_default_rlimit); j++) {
free(arg_default_rlimit[j]);
arg_default_rlimit[j] = NULL;
}
free(arg_default_unit);
arg_default_unit = NULL;
free_join_controllers();
strv_free(arg_default_environment);
arg_default_environment = NULL;
set_free(arg_syscall_archs);
arg_syscall_archs = NULL;
label_finish();
if (reexecute) {
const char **args;
unsigned i, args_size;
sigset_t ss, o_ss;
/* 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);
/* 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)
setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile);
if (switch_root_dir) {
/* Kill all remaining processes from the
* initrd, but don't wait for them, so that we
* can handle the SIGCHLD for them after
* deserializing. */
broadcast_signal(SIGTERM, false, true);
/* And switch root */
r = switch_root(switch_root_dir);
if (r < 0)
log_error("Failed to switch root, ignoring: %s", strerror(-r));
}
args_size = MAX(6, argc+1);
args = newa(const char*, args_size);
if (!switch_root_init) {
char sfd[16];
/* 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. */
assert(arg_serialization);
assert(fds);
snprintf(sfd, sizeof(sfd), "%i", fileno(arg_serialization));
char_array_0(sfd);
i = 0;
args[i++] = SYSTEMD_BINARY_PATH;
if (switch_root_dir)
args[i++] = "--switched-root";
args[i++] = arg_running_as == SYSTEMD_SYSTEM ? "--system" : "--user";
args[i++] = "--deserialize";
args[i++] = sfd;
args[i++] = NULL;
/* do not pass along the environment we inherit from the kernel or initrd */
if (switch_root_dir)
clearenv();
assert(i <= args_size);
execv(args[0], (char* const*) args);
}
/* 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.) */
if (arg_serialization) {
fclose(arg_serialization);
arg_serialization = NULL;
}
if (fds) {
fdset_free(fds);
fds = NULL;
}
/* Reopen the console */
make_console_stdio();
for (j = 1, i = 1; j < (unsigned) argc; j++)
args[i++] = argv[j];
args[i++] = NULL;
assert(i <= args_size);
/* reenable any blocked signals, especially important
* if we switch from initial ramdisk to init=... */
sigemptyset(&ss);
sigprocmask(SIG_SETMASK, &ss, &o_ss);
if (switch_root_init) {
args[0] = switch_root_init;
execv(args[0], (char* const*) args);
log_warning("Failed to execute configured init, trying fallback: %m");
}
args[0] = "/sbin/init";
execv(args[0], (char* const*) args);
if (errno == ENOENT) {
log_warning("No /sbin/init, trying fallback");
args[0] = "/bin/sh";
args[1] = NULL;
execv(args[0], (char* const*) args);
log_error("Failed to execute /bin/sh, giving up: %m");
} else
log_warning("Failed to execute /sbin/init, giving up: %m");
sigprocmask(SIG_SETMASK, &o_ss, NULL);
}
if (arg_serialization) {
fclose(arg_serialization);
arg_serialization = NULL;
}
if (fds) {
fdset_free(fds);
fds = NULL;
}
#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
if (shutdown_verb) {
char log_level[DECIMAL_STR_MAX(int) + 1];
const char* command_line[9] = {
SYSTEMD_SHUTDOWN_BINARY_PATH,
shutdown_verb,
"--log-level", log_level,
"--log-target",
};
unsigned pos = 5;
_cleanup_strv_free_ char **env_block = NULL;
assert(command_line[pos] == NULL);
env_block = strv_copy(environ);
snprintf(log_level, sizeof(log_level), "%d", log_get_max_level());
switch (log_get_target()) {
case LOG_TARGET_KMSG:
case LOG_TARGET_JOURNAL_OR_KMSG:
case LOG_TARGET_SYSLOG_OR_KMSG:
command_line[pos++] = "kmsg";
break;
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";
assert(pos < ELEMENTSOF(command_line));
if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
char *e;
/* If we reboot let's set the shutdown
* watchdog and tell the shutdown binary to
* repeatedly ping it */
watchdog_set_timeout(&arg_shutdown_watchdog);
watchdog_close(false);
/* Tell the binary how often to ping, ignore failure */
if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
strv_push(&env_block, e);
} else
watchdog_close(true);
/* Avoid the creation of new processes forked by the
* kernel; at this point, we will not listen to the
* signals anyway */
if (detect_container(NULL) <= 0)
cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
log_error("Failed to execute shutdown binary, %s: %m",
getpid() == 1 ? "freezing" : "quitting");
}
if (getpid() == 1)
freeze();
return retval;
}