main.c revision a0a3844815b0f346dba03f41245c620f432e462f
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/***
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright 2010 Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen***/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include <dbus/dbus.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0a6f50c0afdfc434b492493bd9efab20cbee8623Thomas Hindoe Paaboel Andersen#include <stdio.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include <string.h>
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen#include <unistd.h>
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include <sys/types.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/stat.h>
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering#include <getopt.h>
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#include <signal.h>
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek#include <sys/wait.h>
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering#include <fcntl.h>
3df3e884ae1237ef0d4d23b0e80f4ffda95ac135Ronny Chevalier#include <sys/prctl.h>
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "manager.h"
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include "log.h"
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include "load-fragment.h"
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include "fdset.h"
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include "special.h"
5f402ae84bbc08fe8de5682e371b3f66c387da52Daniel Mack#include "conf-parser.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "bus-errors.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "missing.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "label.h"
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen#include "build.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "strv.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "def.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "virt.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "watchdog.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "mount-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "loopback-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "kmod-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "hostname-setup.h"
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen#include "machine-id-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "locale-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "selinux-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "ima-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic enum {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ACTION_RUN,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ACTION_HELP,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ACTION_TEST,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ACTION_DUMP_CONFIGURATION_ITEMS,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ACTION_DONE
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering} arg_action = ACTION_RUN;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poetteringstatic char *arg_default_unit = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_dump_core = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_crash_shell = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int arg_crash_chvt = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_confirm_spawn = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_show_status = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#ifdef HAVE_SYSV_COMPAT
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_sysv_console = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#endif
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_mount_auto = true;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringstatic bool arg_swap_auto = true;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringstatic char **arg_default_controllers = NULL;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringstatic char ***arg_join_controllers = NULL;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringstatic ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poetteringstatic ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poetteringstatic usec_t arg_runtime_watchdog = 0;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poetteringstatic usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poetteringstatic FILE* serialization = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void nop_handler(int sig) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_noreturn_ static void crash(int sig) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!arg_dump_core)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct sigaction sa;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pid_t pid;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* We want to wait for the core process, hence let's enable SIGCHLD */
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt zero(sa);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_handler = nop_handler;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((pid = fork()) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering else if (pid == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct rlimit rl;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Enable default signal handler for core dump */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering zero(sa);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_handler = SIG_DFL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sigaction(sig, &sa, NULL) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen /* Don't limit the core dump size */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering zero(rl);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rl.rlim_cur = RLIM_INFINITY;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rl.rlim_max = RLIM_INFINITY;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering setrlimit(RLIMIT_CORE, &rl);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Just to be sure... */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(chdir("/") == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Raise the signal again */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering raise(sig);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_not_reached("We shouldn't be here...");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _exit(1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering siginfo_t status;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen /* Order things nicely. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = wait_for_terminate(pid, &status)) < 0)
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else if (status.si_code != CLD_DUMPED)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (arg_crash_chvt)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering chvt(arg_crash_chvt);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (arg_crash_shell) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct sigaction sa;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pid_t pid;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_info("Executing crash shell in 10s...");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sleep(10);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Let the kernel reap children for us */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering zero(sa);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_handler = SIG_IGN;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((pid = fork()) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to fork off crash shell: %s", strerror(errno));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else if (pid == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd, r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to acquire terminal: %s", strerror(-fd));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else if ((r = make_stdio(fd)) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to duplicate terminal fd: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering execl("/bin/sh", "/bin/sh", NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("execl() failed: %s", strerror(errno));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _exit(1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_info("Freezing execution.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering freeze();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void install_crash_handler(void) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt struct sigaction sa;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering zero(sa);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sa.sa_handler = crash;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen sa.sa_flags = SA_NODEFER;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int console_setup(bool do_reset) {
cc98b3025eeb89addb76a27390cb2baca4eab8b9Torstein Husebø int tty_fd, r;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* If we are init, we connect stdin/stdout/stderr to /dev/null
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * and make sure we don't have a controlling tty. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen release_terminal();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!do_reset)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (tty_fd < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return -tty_fd;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* We don't want to force text mode.
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen * plymouth may be showing pictures already from initrd. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = reset_terminal_fd(tty_fd, false);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Failed to reset /dev/console: %s", strerror(-r));
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering close_nointr_nofail(tty_fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int set_default_unit(const char *u) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *c;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(u);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!(c = strdup(u)))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen free(arg_default_unit);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_default_unit = c;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int parse_proc_cmdline_word(const char *word) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering static const char * const rlmap[] = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "emergency", SPECIAL_EMERGENCY_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "-b", SPECIAL_EMERGENCY_TARGET,
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen "single", SPECIAL_RESCUE_TARGET,
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen "-s", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "s", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "S", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "1", SPECIAL_RESCUE_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "2", SPECIAL_RUNLEVEL2_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "3", SPECIAL_RUNLEVEL3_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "4", SPECIAL_RUNLEVEL4_TARGET,
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen "5", SPECIAL_RUNLEVEL5_TARGET,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(word);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (startswith(word, "systemd.unit="))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return set_default_unit(word + 13);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen else if (startswith(word, "systemd.log_target=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (log_set_target_from_string(word + 19) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse log target %s. Ignoring.", word + 19);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.log_level=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (log_set_max_level_from_string(word + 18) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse log level %s. Ignoring.", word + 18);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.log_color=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (log_show_color_from_string(word + 18) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.log_location=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (log_show_location_from_string(word + 21) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.dump_core=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = parse_boolean(word + 18)) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_dump_core = r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.crash_shell=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = parse_boolean(word + 20)) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_crash_shell = r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.confirm_spawn=")) {
0014a4ad505d119c7ac4346d9d774c3f17f663a5Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = parse_boolean(word + 22)) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_confirm_spawn = r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.crash_chvt=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (safe_atoi(word + 19, &k) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_crash_chvt = k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (startswith(word, "systemd.show_status=")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = parse_boolean(word + 20)) < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering else
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_show_status = r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (startswith(word, "systemd.default_standard_output=")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering int r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if ((r = exec_output_from_string(word + 32)) < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering else
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_default_std_output = r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (startswith(word, "systemd.default_standard_error=")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering int r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if ((r = exec_output_from_string(word + 31)) < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering else
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_default_std_error = r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (startswith(word, "systemd.setenv=")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering char *cenv, *eq;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering int r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering cenv = strdup(word + 15);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (!cenv)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return -ENOMEM;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering eq = strchr(cenv, '=');
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (!eq) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = unsetenv(cenv);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("unsetenv failed %s. Ignoring.", strerror(errno));
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering *eq = 0;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = setenv(cenv, eq + 1, 1);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("setenv failed %s. Ignoring.", strerror(errno));
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering }
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering free(cenv);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#ifdef HAVE_SYSV_COMPAT
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (startswith(word, "systemd.sysv_console=")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering int r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if ((r = parse_boolean(word + 21)) < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("Failed to parse SysV console switch %s. Ignoring.", word + 20);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering else
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_sysv_console = r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#endif
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (startswith(word, "systemd.")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_warning("Unknown kernel switch %s. Ignoring.", word);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_info("Supported kernel switches:\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.unit=UNIT Default unit to start\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.dump_core=0|1 Dump core on crash\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.crash_shell=0|1 Run shell on crash\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.crash_chvt=N Change to VT #N on crash\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.show_status=0|1 Show status updates on the console during bootup\n"
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt#ifdef HAVE_SYSV_COMPAT
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#endif
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering " Log target\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.log_level=LEVEL Log level\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.log_color=0|1 Highlight important log messages\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.log_location=0|1 Include code location in log messages\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
8d3d7072e609ef0e0fb37e1d19a29307d58146c3Michal Schmidt " Set default log output for services\n"
8d3d7072e609ef0e0fb37e1d19a29307d58146c3Michal Schmidt "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering " Set default log error output for services\n");
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else if (streq(word, "quiet")) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_show_status = false;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#ifdef HAVE_SYSV_COMPAT
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering arg_sysv_console = false;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering#endif
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering } else {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering unsigned i;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering /* SysV compatibility */
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (streq(word, rlmap[i]))
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return set_default_unit(rlmap[i+1]);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return 0;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}
c92e531c82a9815ec349aa1bf31236b86b2d5311Lennart Poettering
c92e531c82a9815ec349aa1bf31236b86b2d5311Lennart Poetteringstatic int config_parse_level2(
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *filename,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned line,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *section,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *lvalue,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int ltype,
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering const char *rvalue,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering void *data,
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering void *userdata) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert(filename);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering assert(lvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen assert(rvalue);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_set_max_level_from_string(rvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return 0;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersenstatic int config_parse_target(
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *filename,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned line,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *section,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *lvalue,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen int ltype,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *rvalue,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen void *data,
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering void *userdata) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering assert(filename);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering assert(lvalue);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert(rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_set_target_from_string(rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int config_parse_color(
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *filename,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned line,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *section,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *lvalue,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int ltype,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *rvalue,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering void *data,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering void *userdata) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen assert(filename);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen assert(lvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen assert(rvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_show_color_from_string(rvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return 0;
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering}
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poetteringstatic int config_parse_location(
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering const char *filename,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering unsigned line,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering const char *section,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering const char *lvalue,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering int ltype,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering const char *rvalue,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering void *data,
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering void *userdata) {
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(filename);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(lvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen assert(rvalue);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_show_location_from_string(rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poetteringstatic int config_parse_cpu_affinity2(
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering const char *filename,
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering unsigned line,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *section,
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering const char *lvalue,
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering int ltype,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *rvalue,
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering void *data,
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering void *userdata) {
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering char *w;
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering size_t l;
096b6773886bd7a0c8c97aa684b0b67dfae58355Lennart Poettering char *state;
096b6773886bd7a0c8c97aa684b0b67dfae58355Lennart Poettering cpu_set_t *c = NULL;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned ncpus = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(filename);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(lvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering FOREACH_WORD_QUOTED(w, l, rvalue, state) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char *t;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned cpu;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (!(t = strndup(w, l)))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = safe_atou(t, &cpu);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering free(t);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (!c)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (!(c = cpu_set_malloc(&ncpus)))
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -ENOMEM;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0 || cpu >= ncpus) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering CPU_FREE(c);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return -EBADMSG;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (c) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_warning("Failed to set CPU affinity: %m");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering CPU_FREE(c);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return 0;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering}
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poetteringstatic void strv_free_free(char ***l) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char ***i;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!l)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering for (i = l; *i; i++)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(*i);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering free(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering}
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poetteringstatic void free_join_controllers(void) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!arg_join_controllers)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free_free(arg_join_controllers);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering arg_join_controllers = NULL;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering}
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poetteringstatic int config_parse_join_controllers(
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering const char *filename,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering unsigned line,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering const char *section,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering const char *lvalue,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering int ltype,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering const char *rvalue,
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering void *data,
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt void *userdata) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering unsigned n = 0;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char *state, *w;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering size_t length;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt assert(filename);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering assert(lvalue);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering assert(rvalue);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering free_join_controllers();
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering FOREACH_WORD_QUOTED(w, length, rvalue, state) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char *s, **l;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering s = strndup(w, length);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!s)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return -ENOMEM;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering l = strv_split(s, ",");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering free(s);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_uniq(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (strv_length(l) <= 1) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering continue;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!arg_join_controllers) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering arg_join_controllers = new(char**, 2);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!arg_join_controllers) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return -ENOMEM;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering arg_join_controllers[0] = l;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering arg_join_controllers[1] = NULL;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering n = 1;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering } else {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char ***a;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char ***t;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering t = new0(char**, n+2);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!t) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering return -ENOMEM;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering n = 0;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering for (a = arg_join_controllers; *a; a++) {
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering if (strv_overlap(*a, l)) {
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering char **c;
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering c = strv_merge(*a, l);
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering if (!c) {
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering strv_free(l);
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering strv_free_free(t);
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering return -ENOMEM;
36a03ca2a8952ca1acb29fbe796210c27ff71affLennart Poettering }
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(l);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering l = c;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering } else {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char **c;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering c = strv_copy(*a);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!c) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering strv_free(l);
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen strv_free_free(t);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering t[n++] = c;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering t[n++] = strv_uniq(l);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt strv_free_free(arg_join_controllers);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen arg_join_controllers = t;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return 0;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersenstatic int parse_config_file(void) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const ConfigTableItem items[] = {
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "Manager", "LogLevel", config_parse_level2, 0, NULL },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "Manager", "LogTarget", config_parse_target, 0, NULL },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "Manager", "LogColor", config_parse_color, 0, NULL },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "Manager", "LogLocation", config_parse_location, 0, NULL },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "ShowStatus", config_parse_bool, 0, &arg_show_status },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen#ifdef HAVE_SYSV_COMPAT
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "SysVConsole", config_parse_bool, 0, &arg_sysv_console },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen#endif
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "MountAuto", config_parse_bool, 0, &arg_mount_auto },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "SwapAuto", config_parse_bool, 0, &arg_swap_auto },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "DefaultControllers", config_parse_strv, 0, &arg_default_controllers },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "RuntimeWatchdogSec", config_parse_usec, 0, &arg_runtime_watchdog },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "Manager", "ShutdownWatchdogSec", config_parse_usec, 0, &arg_shutdown_watchdog },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { NULL, NULL, NULL, 0, NULL }
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen };
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen FILE *f;
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen const char *fn;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering int r;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek f = fopen(fn, "re");
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek if (!f) {
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek if (errno == ENOENT)
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek return 0;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek log_warning("Failed to open configuration file '%s': %m", fn);
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering return 0;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek }
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek r = config_parse(fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, NULL);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek if (r < 0)
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek log_warning("Failed to parse configuration file: %s", strerror(-r));
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek fclose(f);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek return 0;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering}
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmekstatic int parse_proc_cmdline(void) {
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek char *line, *w, *state;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek int r;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering size_t l;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek /* Don't read /proc/cmdline if we are in a container, since
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek * that is only relevant for the host system */
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek if (detect_container(NULL) > 0)
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek return 0;
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return 0;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering FOREACH_WORD_QUOTED(w, l, line, state) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *word;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!(word = strndup(w, l))) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = -ENOMEM;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto finish;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering r = parse_proc_cmdline_word(word);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering free(word);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (r < 0)
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering goto finish;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering }
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering r = 0;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersenfinish:
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen free(line);
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen return r;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering enum {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_LOG_LEVEL = 0x100,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_LOG_TARGET,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_LOG_COLOR,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_LOG_LOCATION,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_UNIT,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_SYSTEM,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_USER,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_TEST,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_DUMP_CONFIGURATION_ITEMS,
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering ARG_DUMP_CORE,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_CRASH_SHELL,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_CONFIRM_SPAWN,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_SHOW_STATUS,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_SYSV_CONSOLE,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_DESERIALIZE,
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering ARG_INTROSPECT,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_DEFAULT_STD_OUTPUT,
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen ARG_DEFAULT_STD_ERROR
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen };
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering static const struct option options[] = {
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "unit", required_argument, NULL, ARG_UNIT },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "system", no_argument, NULL, ARG_SYSTEM },
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen { "user", no_argument, NULL, ARG_USER },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "test", no_argument, NULL, ARG_TEST },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "help", no_argument, NULL, 'h' },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering#ifdef HAVE_SYSV_COMPAT
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering#endif
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek { "introspect", optional_argument, NULL, ARG_INTROSPECT },
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen { NULL, 0, NULL, 0 }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen };
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek int c, r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(argc >= 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(argv);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (getpid() == 1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering opterr = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen switch (c) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering case ARG_LOG_LEVEL:
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering if ((r = log_set_max_level_from_string(optarg)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to parse log level %s.", optarg);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case ARG_LOG_TARGET:
40a1eebde6be7ac3f1885147fc24e06ad1da260cDavid Herrmann
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if ((r = log_set_target_from_string(optarg)) < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error("Failed to parse log target %s.", optarg);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case ARG_LOG_COLOR:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (optarg) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = log_show_color_from_string(optarg)) < 0) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Failed to parse log color setting %s.", optarg);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_show_color(true);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_LOG_LOCATION:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (optarg) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = log_show_location_from_string(optarg)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to parse log location setting %s.", optarg);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_show_location(true);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case ARG_DEFAULT_STD_OUTPUT:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = exec_output_from_string(optarg)) < 0) {
a38d99451f2bf8026ec51aee91662292e823c6a8Lennart Poettering log_error("Failed to parse default standard output setting %s.", optarg);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_default_std_output = r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen case ARG_DEFAULT_STD_ERROR:
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = exec_output_from_string(optarg)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to parse default standard error output setting %s.", optarg);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_default_std_error = r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen case ARG_UNIT:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if ((r = set_default_unit(optarg)) < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering case ARG_SYSTEM:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering arg_running_as = MANAGER_SYSTEM;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2a1288ff89322a2f49c79f6d1832c8164c14a05cLennart Poettering case ARG_USER:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_running_as = MANAGER_USER;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_TEST:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_action = ACTION_TEST;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case ARG_DUMP_CONFIGURATION_ITEMS:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering break;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering case ARG_DUMP_CORE:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_dump_core = true;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_CRASH_SHELL:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_crash_shell = true;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_CONFIRM_SPAWN:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_confirm_spawn = true;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_SHOW_STATUS:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (optarg) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if ((r = parse_boolean(optarg)) < 0) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Failed to show status boolean %s.", optarg);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering arg_show_status = r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering arg_show_status = true;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#ifdef HAVE_SYSV_COMPAT
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_SYSV_CONSOLE:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (optarg) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if ((r = parse_boolean(optarg)) < 0) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Failed to SysV console boolean %s.", optarg);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_sysv_console = r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering arg_sysv_console = true;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering break;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#endif
a5f035960006556beab51c42e6948985635e261aLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering case ARG_DESERIALIZE: {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int fd;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering FILE *f;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error("Failed to parse deserialize option %s.", optarg);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!(f = fdopen(fd, "r"))) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to open serialization fd: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (serialization)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering fclose(serialization);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering serialization = f;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case ARG_INTROSPECT: {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char * const * i = NULL;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering for (i = bus_interface_table; *i; i += 2)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!optarg || streq(i[0], optarg)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering "<node>\n", stdout);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering fputs(i[1], stdout);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering fputs("</node>\n", stdout);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (optarg)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!i[0] && optarg)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Unknown interface %s.", optarg);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering arg_action = ACTION_DONE;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen break;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'h':
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen arg_action = ACTION_HELP;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'D':
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_set_max_level(LOG_DEBUG);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case 'b':
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case 's':
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering case 'z':
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* Just to eat away the sysvinit kernel
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * cmdline args without getopt() error
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * messages that we'll parse in
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * parse_proc_cmdline_word() or ignore. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case '?':
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering default:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (getpid() != 1) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Unknown option code %c", c);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return -EINVAL;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (optind < argc && getpid() != 1) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Hmm, when we aren't run as init system
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * let's complain about excess arguments */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Excess arguments.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (detect_container(NULL) > 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char **a;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* All /proc/cmdline arguments the kernel didn't
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * understand it passed to us. We're not really
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * interested in that usually since /proc/cmdline is
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * more interesting and complete. With one exception:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * if we are run in a container /proc/cmdline is not
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering * relevant for the container, hence we rely on argv[]
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * instead. */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen for (a = argv; a < argv + argc; a++)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = parse_proc_cmdline_word(*a)) < 0)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int help(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering printf("%s [OPTIONS...]\n\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "Starts up and maintains the system or user services.\n\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " -h --help Show this help\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --test Determine startup sequence, dump it and exit\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --dump-configuration-items Dump understood unit configuration items\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --introspect[=INTERFACE] Extract D-Bus interface data\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --unit=UNIT Set default unit\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --system Run a system instance, even if PID != 1\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --user Run a user instance\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --dump-core Dump core on crash\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --crash-shell Run shell on crash\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --confirm-spawn Ask for confirmation when spawning processes\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --show-status[=0|1] Show status updates on the console during bootup\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#ifdef HAVE_SYSV_COMPAT
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#endif
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --log-color[=0|1] Highlight important log messages\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --log-location[=0|1] Include code location in log messages\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --default-standard-output= Set default standard output for services\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " --default-standard-error= Set default standard error output for services\n",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering program_invocation_short_name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering FILE *f = NULL;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering FDSet *fds = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(_f);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert(_fds);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* Make sure nothing is really destructed when we shut down */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering m->n_reloading ++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = manager_open_serialization(m, &f)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to create serialization file: %s", strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto fail;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (!(fds = fdset_new())) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to allocate fd set: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = manager_serialize(m, f, fds)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to serialize state: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fseeko(f, 0, SEEK_SET) < 0) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("Failed to rewind serialization fd: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = fd_cloexec(fileno(f), false)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((r = fdset_cloexec(fds, false)) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *_f = f;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *_fds = fds;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringfail:
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering fdset_free(fds);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (f)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fclose(f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering const char *e;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned long long a, b;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(t);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering e = getenv("RD_TIMESTAMP");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!e)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (sscanf(e, "%llu %llu", &a, &b) != 2)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering t->realtime = (usec_t) a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering t->monotonic = (usec_t) b;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return t;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_mtab(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *p;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* Check that /etc/mtab is a symlink */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (readlink_malloc("/etc/mtab", &p) >= 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool b;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (b)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "This is not supported anymore. "
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_usr(void) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* Check that /usr is not a separate fs */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (dir_is_empty("/usr") <= 0)
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering return;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "Some things will probably break (sometimes even silently) in mysterious ways. "
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_cgroups(void) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (access("/proc/cgroups", F_OK) >= 0)
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "Systems without control groups are not supported. "
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "We will now sleep for 10s, and then continue boot-up. "
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "Expect breakage and please do not file bugs. "
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering "Instead fix your kernel and enable CONFIG_CGROUPS. "
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering "Consult http://0pointer.de/blog/projects/cgroups-vs-cgroups.html for more information.");
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sleep(10);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering}
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
2c27fbca2d88214bd305272308a370a962818f1eLennart Poetteringint main(int argc, char *argv[]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Manager *m = NULL;
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering int r, retval = EXIT_FAILURE;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering usec_t before_startup, after_startup;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering char timespan[FORMAT_TIMESPAN_MAX];
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering FDSet *fds = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering bool reexecute = false;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering const char *shutdown_verb = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering static char systemd[] = "systemd";
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering bool is_reexec = false;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering int j;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering bool loaded_policy = false;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering bool arm_reboot_watchdog = false;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#ifdef HAVE_SYSV_COMPAT
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* This is compatibility support for SysV, where
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * calling init as a user is identical to telinit. */
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering errno = -ENOENT;
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering execv(SYSTEMCTL_BINARY_PATH, argv);
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#endif
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* Determine if this is a reexecution or normal bootup. We do
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * the full command line parsing much later, so let's just
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * have a quick peek here. */
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering for (j = 1; j < argc; j++)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (streq(argv[j], "--deserialize")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering is_reexec = true;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering break;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* If we get started via the /sbin/init symlink then we are
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering called 'init'. After a subsequent reexecution we are then
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering called 'systemd'. That is confusing, hence let's call us
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering systemd right-away. */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering program_invocation_short_name = systemd;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering prctl(PR_SET_NAME, systemd);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering saved_argv = argv;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering saved_argc = argc;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_show_color(isatty(STDERR_FILENO) > 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_show_location(false);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_set_max_level(LOG_INFO);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (getpid() == 1) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering arg_running_as = MANAGER_SYSTEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_JOURNAL : LOG_TARGET_JOURNAL_OR_KMSG);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!is_reexec) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (selinux_setup(&loaded_policy) < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ima_setup() < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_open();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (label_init(NULL) < 0)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering goto finish;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!is_reexec)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (hwclock_is_localtime() > 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int min;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering r = hwclock_apply_localtime_delta(&min);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (r < 0)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering else
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering } else {
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering arg_running_as = MANAGER_USER;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering log_set_target(LOG_TARGET_AUTO);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering log_open();
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering }
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering /* Initialize default unit */
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering goto finish;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering /* By default, mount "cpu" and "cpuacct" together */
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering arg_join_controllers = new(char**, 2);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering if (!arg_join_controllers)
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering goto finish;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering arg_join_controllers[1] = NULL;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering if (!arg_join_controllers[0])
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering goto finish;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Mount /proc, /sys and friends, so that /proc/cmdline and
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * /proc/$PID/fd is available. */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = mount_setup(loaded_policy);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering goto finish;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* Reset all signal handlers. */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering assert_se(reset_all_signal_handlers() == 0);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* If we are init, we can block sigkill. Yay. */
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering ignore_signals(SIGNALS_IGNORE, -1);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (parse_config_file() < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering goto finish;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (arg_running_as == MANAGER_SYSTEM)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (parse_proc_cmdline() < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering goto finish;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_parse_environment();
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (parse_argv(argc, argv) < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering goto finish;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (arg_action == ACTION_TEST && geteuid() == 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Don't run test mode as root.");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto finish;
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (arg_running_as == MANAGER_SYSTEM &&
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering arg_action == ACTION_RUN &&
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering running_in_chroot() > 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Cannot be run in a chroot() environment.");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto finish;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (arg_action == ACTION_HELP) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering retval = help();
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto finish;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering unit_dump_config_items(stdout);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering retval = EXIT_SUCCESS;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto finish;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering } else if (arg_action == ACTION_DONE) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering retval = EXIT_SUCCESS;
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering goto finish;
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering }
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering /* Close logging fds, in order not to confuse fdset below */
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering log_close();
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering
556a22945fcc88ca27ae7ecc46c9bb2727e37895Lennart Poettering /* Remember open file descriptors for later deserialization */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (serialization) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = fdset_new_fill(&fds);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Failed to allocate fd set: %s", strerror(-r));
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering goto finish;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering } else
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering close_all_fds(NULL, 0);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Set up PATH unless it is already set */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering setenv("PATH",
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#ifdef HAVE_SPLIT_USR
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#else
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#endif
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering arg_running_as == MANAGER_SYSTEM);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (arg_running_as == MANAGER_SYSTEM) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* Parse the data passed to us. We leave this
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * variables set, but the manager later on will not
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * pass them on to our children. */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering parse_initrd_timestamp(&initrd_timestamp);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* Unset some environment variables passed in from the
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * kernel that don't really make sense for us. */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering unsetenv("HOME");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering unsetenv("TERM");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering /* When we are invoked by a shell, these might be set,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * but make little sense to pass on */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering unsetenv("PWD");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering unsetenv("SHLVL");
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering unsetenv("_");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* When we are invoked by a tool chroot-like such as
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering * nspawn, these might be set, but make little sense
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering * to pass on */
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering unsetenv("USER");
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering unsetenv("LOGNAME");
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering /* All other variables are left as is, so that clients
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering * can still read them via /proc/1/environ */
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering }
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering /* Move out of the way, so that we won't block unmounts */
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering assert_se(chdir("/") == 0);
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering if (arg_running_as == MANAGER_SYSTEM) {
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering /* Become a session leader if we aren't one yet. */
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering setsid();
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering /* Disable the umask logic */
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering umask(0);
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering }
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering dbus_connection_set_change_sigpipe(FALSE);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* Reset the console, but only if this is really init and we
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering * are freshly booted */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering console_setup(getpid() == 1 && !is_reexec);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering make_null_stdio();
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering /* Open the logging devices, if possible and necessary */
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering log_open();
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering /* Make sure we leave a core dump without panicing the
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering * kernel. */
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (getpid() == 1)
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering install_crash_handler();
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering r = mount_cgroup_controllers(arg_join_controllers);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (r < 0)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering goto finish;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
locale_setup();
if (arg_show_status || plymouth_running())
status_welcome();
kmod_setup();
hostname_setup();
machine_id_setup();
loopback_setup();
test_mtab();
test_usr();
test_cgroups();
}
if (arg_running_as == MANAGER_SYSTEM && arg_runtime_watchdog > 0)
watchdog_set_timeout(&arg_runtime_watchdog);
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;
#ifdef HAVE_SYSV_COMPAT
m->sysv_console = arg_sysv_console;
#endif
m->mount_auto = arg_mount_auto;
m->swap_auto = arg_swap_auto;
m->default_std_output = arg_default_std_output;
m->default_std_error = arg_default_std_error;
m->runtime_watchdog = arg_runtime_watchdog;
m->shutdown_watchdog = arg_shutdown_watchdog;
if (dual_timestamp_is_set(&initrd_timestamp))
m->initrd_timestamp = initrd_timestamp;
if (arg_default_controllers)
manager_set_default_controllers(m, arg_default_controllers);
manager_set_show_status(m, arg_show_status);
before_startup = now(CLOCK_MONOTONIC);
r = manager_startup(m, serialization, fds);
if (r < 0)
log_error("Failed to fully start up daemon: %s", strerror(-r));
if (fds) {
/* This will close all file descriptors that were opened, but
* not claimed by any unit. */
fdset_free(fds);
fds = NULL;
}
if (serialization) {
fclose(serialization);
serialization = NULL;
} else {
DBusError error;
Unit *target = NULL;
Job *default_unit_job;
dbus_error_init(&error);
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(&error, r));
dbus_error_free(&error);
} else if (target->load_state == UNIT_ERROR)
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(&error, r));
dbus_error_free(&error);
goto finish;
} else if (target->load_state == UNIT_ERROR) {
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_REPLACE, false, &error, &default_unit_job);
if (r < 0) {
log_error("Failed to start default target: %s", bus_error(&error, r));
dbus_error_free(&error);
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));
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, &serialization, &fds) < 0)
goto finish;
reexecute = true;
log_notice("Reexecuting.");
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);
free(arg_default_unit);
strv_free(arg_default_controllers);
free_join_controllers();
dbus_shutdown();
label_finish();
if (reexecute) {
const char *args[15];
unsigned i = 0;
char sfd[16];
assert(serialization);
assert(fds);
args[i++] = SYSTEMD_BINARY_PATH;
args[i++] = "--log-level";
args[i++] = log_level_to_string(log_get_max_level());
args[i++] = "--log-target";
args[i++] = log_target_to_string(log_get_target());
if (arg_running_as == MANAGER_SYSTEM)
args[i++] = "--system";
else
args[i++] = "--user";
if (arg_dump_core)
args[i++] = "--dump-core";
if (arg_crash_shell)
args[i++] = "--crash-shell";
if (arg_confirm_spawn)
args[i++] = "--confirm-spawn";
if (arg_show_status)
args[i++] = "--show-status=1";
else
args[i++] = "--show-status=0";
#ifdef HAVE_SYSV_COMPAT
if (arg_sysv_console)
args[i++] = "--sysv-console=1";
else
args[i++] = "--sysv-console=0";
#endif
snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
char_array_0(sfd);
args[i++] = "--deserialize";
args[i++] = sfd;
args[i++] = NULL;
assert(i <= ELEMENTSOF(args));
/* 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);
execv(args[0], (char* const*) args);
log_error("Failed to reexecute: %m");
}
if (serialization)
fclose(serialization);
if (fds)
fdset_free(fds);
if (shutdown_verb) {
const char * command_line[] = {
SYSTEMD_SHUTDOWN_BINARY_PATH,
shutdown_verb,
NULL
};
char **env_block;
if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
char e[32];
/* 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 */
snprintf(e, sizeof(e), "WATCHDOG_USEC=%llu", (unsigned long long) arg_shutdown_watchdog);
char_array_0(e);
env_block = strv_append(environ, e);
} else {
env_block = strv_copy(environ);
watchdog_close(true);
}
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
free(env_block);
log_error("Failed to execute shutdown binary, freezing: %m");
}
if (getpid() == 1)
freeze();
return retval;
}