main.c revision 934da0359966a77ff4f1505a4663e34aa434f4d3
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
#include <fcntl.h>
#include "manager.h"
#include "log.h"
#include "mount-setup.h"
#include "hostname-setup.h"
#include "loopback-setup.h"
#include "kmod-setup.h"
#include "locale-setup.h"
#include "selinux-setup.h"
#include "load-fragment.h"
#include "fdset.h"
#include "special.h"
#include "conf-parser.h"
#include "bus-errors.h"
#include "missing.h"
#include "label.h"
#include "build.h"
#include "strv.h"
static enum {
} arg_action = ACTION_RUN;
static char *arg_default_unit = NULL;
static bool arg_dump_core = true;
static bool arg_crash_shell = false;
static int arg_crash_chvt = -1;
static bool arg_confirm_spawn = false;
static bool arg_show_status = true;
#ifdef HAVE_SYSV_COMPAT
static bool arg_sysv_console = true;
#endif
static bool arg_mount_auto = true;
static bool arg_swap_auto = true;
static char **arg_default_controllers = NULL;
static void nop_handler(int sig) {
}
if (!arg_dump_core)
else {
/* We want to wait for the core process, hence let's enable SIGCHLD */
else if (pid == 0) {
/* Enable default signal handler for core dump */
/* Don't limit the core dump size */
/* Just to be sure... */
/* Raise the signal again */
assert_not_reached("We shouldn't be here...");
_exit(1);
} else {
int r;
/* Order things nicely. */
else
}
}
if (arg_crash_chvt)
if (arg_crash_shell) {
log_info("Executing crash shell in 10s...");
sleep(10);
/* Let the kernel reap children for us */
else if (pid == 0) {
int fd, r;
else if ((r = make_stdio(fd)) < 0)
_exit(1);
}
}
log_info("Freezing execution.");
freeze();
}
static void install_crash_handler(void) {
}
static int console_setup(bool do_reset) {
int tty_fd, r;
* and make sure we don't have a controlling tty. */
if (!do_reset)
return 0;
return -tty_fd;
}
if ((r = reset_terminal(tty_fd)) < 0)
return r;
}
static int set_default_unit(const char *u) {
char *c;
assert(u);
if (!(c = strdup(u)))
return -ENOMEM;
arg_default_unit = c;
return 0;
}
static int parse_proc_cmdline_word(const char *word) {
static const char * const rlmap[] = {
"emergency", SPECIAL_EMERGENCY_TARGET,
"single", SPECIAL_RESCUE_TARGET,
"-s", SPECIAL_RESCUE_TARGET,
"s", SPECIAL_RESCUE_TARGET,
"S", SPECIAL_RESCUE_TARGET,
"1", SPECIAL_RESCUE_TARGET,
"2", SPECIAL_RUNLEVEL2_TARGET,
"3", SPECIAL_RUNLEVEL3_TARGET,
"4", SPECIAL_RUNLEVEL4_TARGET,
"5", SPECIAL_RUNLEVEL5_TARGET,
};
int r;
else
arg_dump_core = r;
int r;
else
arg_crash_shell = r;
int r;
else
arg_confirm_spawn = r;
int k;
else
arg_crash_chvt = k;
int r;
else
arg_show_status = r;
int r;
else
int r;
else
#ifdef HAVE_SYSV_COMPAT
int r;
else
arg_sysv_console = r;
#endif
log_info("Supported kernel switches:\n"
"systemd.unit=UNIT Default unit to start\n"
"systemd.dump_core=0|1 Dump core on crash\n"
"systemd.crash_shell=0|1 Run shell on crash\n"
"systemd.crash_chvt=N Change to VT #N on crash\n"
"systemd.confirm_spawn=0|1 Confirm every process spawn\n"
"systemd.show_status=0|1 Show status updates on the console during bootup\n"
#ifdef HAVE_SYSV_COMPAT
"systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
#endif
"systemd.log_target=console|kmsg|syslog|syslog-or-kmsg|null\n"
" Log target\n"
"systemd.log_level=LEVEL Log level\n"
"systemd.log_color=0|1 Highlight important log messages\n"
"systemd.log_location=0|1 Include code location in log messages\n"
"systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
" Set default log output for services\n"
"systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
" Set default log error output for services\n");
arg_show_status = false;
#ifdef HAVE_SYSV_COMPAT
arg_sysv_console = false;
#endif
} else {
unsigned i;
/* SysV compatibility */
}
return 0;
}
static int config_parse_level(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
static int config_parse_target(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
static int config_parse_color(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
static int config_parse_location(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
static int config_parse_cpu_affinity(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
char *w;
size_t l;
char *state;
unsigned ncpus = 0;
char *t;
int r;
unsigned cpu;
if (!(t = strndup(w, l)))
return -ENOMEM;
free(t);
if (!c)
if (!(c = cpu_set_malloc(&ncpus)))
return -ENOMEM;
CPU_FREE(c);
return -EBADMSG;
}
}
if (c) {
log_warning("Failed to set CPU affinity: %m");
CPU_FREE(c);
}
return 0;
}
static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
static int parse_config_file(void) {
const ConfigItem items[] = {
#ifdef HAVE_SYSV_COMPAT
#endif
};
static const char * const sections[] = {
"Manager",
};
FILE *f;
const char *fn;
int r;
return 0;
return 0;
}
fclose(f);
return 0;
}
static int parse_proc_cmdline(void) {
int r;
size_t l;
return 0;
}
char *word;
r = -ENOMEM;
goto finish;
}
r = parse_proc_cmdline_word(word);
if (r < 0)
goto finish;
}
r = 0;
return r;
}
enum {
ARG_LOG_LEVEL = 0x100,
};
#ifdef HAVE_SYSV_COMPAT
#endif
};
int c, r;
switch (c) {
case ARG_LOG_LEVEL:
if ((r = log_set_max_level_from_string(optarg)) < 0) {
return r;
}
break;
case ARG_LOG_TARGET:
if ((r = log_set_target_from_string(optarg)) < 0) {
return r;
}
break;
case ARG_LOG_COLOR:
if (optarg) {
if ((r = log_show_color_from_string(optarg)) < 0) {
return r;
}
} else
log_show_color(true);
break;
case ARG_LOG_LOCATION:
if (optarg) {
if ((r = log_show_location_from_string(optarg)) < 0) {
return r;
}
} else
log_show_location(true);
break;
case ARG_DEFAULT_STD_OUTPUT:
if ((r = exec_output_from_string(optarg)) < 0) {
return r;
} else
break;
case ARG_DEFAULT_STD_ERROR:
if ((r = exec_output_from_string(optarg)) < 0) {
return r;
} else
break;
case ARG_UNIT:
if ((r = set_default_unit(optarg)) < 0) {
return r;
}
break;
case ARG_SYSTEM:
break;
case ARG_USER:
break;
case ARG_TEST:
break;
break;
case ARG_DUMP_CORE:
arg_dump_core = true;
break;
case ARG_CRASH_SHELL:
arg_crash_shell = true;
break;
case ARG_CONFIRM_SPAWN:
arg_confirm_spawn = true;
break;
case ARG_SHOW_STATUS:
if (optarg) {
if ((r = parse_boolean(optarg)) < 0) {
return r;
}
arg_show_status = r;
} else
arg_show_status = true;
break;
#ifdef HAVE_SYSV_COMPAT
case ARG_SYSV_CONSOLE:
if (optarg) {
if ((r = parse_boolean(optarg)) < 0) {
return r;
}
arg_sysv_console = r;
} else
arg_sysv_console = true;
break;
#endif
case ARG_DESERIALIZE: {
int fd;
FILE *f;
return r;
}
log_error("Failed to open serialization fd: %m");
return r;
}
if (serialization)
serialization = f;
break;
}
case ARG_INTROSPECT: {
const char * const * i = NULL;
for (i = bus_interface_table; *i; i += 2)
"<node>\n", stdout);
if (optarg)
break;
}
if (!i[0] && optarg)
break;
}
case 'h':
break;
case 'D':
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
/* PID 1 will get the kernel arguments as parameters, which we
* ignore and unconditionally read from
* here. */
log_error("Excess arguments.");
return -EINVAL;
}
return 0;
}
static int help(void) {
printf("%s [OPTIONS...]\n\n"
"Starts up and maintains the system or user services.\n\n"
" -h --help Show this help\n"
" --test Determine startup sequence, dump it and exit\n"
" --dump-configuration-items Dump understood unit configuration items\n"
" --introspect[=INTERFACE] Extract D-Bus interface data\n"
" --unit=UNIT Set default unit\n"
" --system Run a system instance, even if PID != 1\n"
" --user Run a user instance\n"
" --dump-core Dump core on crash\n"
" --crash-shell Run shell on crash\n"
" --confirm-spawn Ask for confirmation when spawning processes\n"
" --show-status[=0|1] Show status updates on the console during bootup\n"
#ifdef HAVE_SYSV_COMPAT
" --sysv-console[=0|1] Connect output of SysV scripts to console\n"
#endif
" --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
" --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
" --log-color[=0|1] Highlight important log messages\n"
" --log-location[=0|1] Include code location in log messages\n"
" --default-standard-output= Set default standard output for services\n"
" --default-standard-error= Set default standard error output for services\n",
return 0;
}
int r;
assert(m);
if ((r = manager_open_serialization(m, &f)) < 0) {
goto fail;
}
r = -ENOMEM;
goto fail;
}
if ((r = manager_serialize(m, f, fds)) < 0) {
goto fail;
}
log_error("Failed to rewind serialization fd: %m");
goto fail;
}
if ((r = fd_cloexec(fileno(f), false)) < 0) {
goto fail;
}
if ((r = fdset_cloexec(fds, false)) < 0) {
goto fail;
}
*_f = f;
return 0;
fail:
if (f)
fclose(f);
return r;
}
const char *e;
unsigned long long a, b;
assert(t);
if (!(e = getenv("RD_TIMESTAMP")))
return NULL;
return NULL;
return t;
}
static void test_mtab(void) {
char *p;
if (readlink_malloc("/etc/mtab", &p) >= 0) {
bool b;
free(p);
if (b)
return;
}
"This is not supported anymore. "
"Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
}
int r, retval = EXIT_FAILURE;
bool reexecute = false;
const char *shutdown_verb = NULL;
char systemd[] = "systemd";
/* This is compatbility support for SysV, where
* calling init as a user is identical to telinit. */
return 1;
}
called 'init'. After a subsequent reexecution we are then
called 'systemd'. That is confusing, hence let's call us
systemd right-away. */
log_show_location(false);
if (getpid() == 1) {
/* This might actually not return, but cause a
* reexecution */
if (selinux_setup(argv) < 0)
goto finish;
if (label_init() < 0)
goto finish;
} else {
}
if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
goto finish;
if (mount_setup() < 0)
goto finish;
/* Reset all signal handlers. */
assert_se(reset_all_signal_handlers() == 0);
/* If we are init, we can block sigkill. Yay. */
if (parse_config_file() < 0)
goto finish;
if (arg_running_as == MANAGER_SYSTEM)
if (parse_proc_cmdline() < 0)
goto finish;
goto finish;
/* If Plymouth is being run make sure we show the status, so
* that there's something nice to see when people press Esc */
arg_show_status = true;
if (arg_action == ACTION_HELP) {
goto finish;
} else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
goto finish;
} else if (arg_action == ACTION_DONE) {
goto finish;
}
/* Remember open file descriptors for later deserialization */
if (serialization) {
if ((r = fdset_new_fill(&fds)) < 0) {
goto finish;
}
} else
close_all_fds(NULL, 0);
/* Set up PATH unless it is already set */
setenv("PATH",
if (arg_running_as == MANAGER_SYSTEM) {
/* Parse the data passed to us by the initrd and unset it */
filter_environ("RD_");
/* Unset some environment variables passed in from the
* kernel that don't really make sense for us. */
unsetenv("HOME");
unsetenv("TERM");
}
/* Move out of the way, so that we won't block unmounts */
if (arg_running_as == MANAGER_SYSTEM) {
/* Become a session leader if we aren't one yet. */
setsid();
/* Disable the umask logic */
umask(0);
}
/* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
/* Reset the console, but only if this is really init and we
* are freshly booted */
}
/* Open the logging devices, if possible and necessary */
log_open();
/* Make sure we leave a core dump without panicing the
* kernel. */
if (getpid() == 1)
PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
locale_setup();
if (arg_show_status)
kmod_setup();
test_mtab();
}
if ((r = manager_new(arg_running_as, &m)) < 0) {
goto finish;
}
m->show_status = arg_show_status;
#ifdef HAVE_SYSV_COMPAT
m->sysv_console = arg_sysv_console;
#endif
m->mount_auto = arg_mount_auto;
m->swap_auto = arg_swap_auto;
if (fds) {
/* This will close all file descriptors that were opened, but
* not claimed by any unit. */
}
if (serialization) {
} else {
log_error("Default target masked.");
log_info("Trying to load rescue target...");
goto finish;
goto finish;
log_error("Rescue target masked.");
goto finish;
}
}
if (arg_action == ACTION_TEST) {
printf("-> By units:\n");
}
goto finish;
}
if (arg_action == ACTION_TEST) {
printf("-> By jobs:\n");
goto finish;
}
}
for (;;) {
if ((r = manager_loop(m)) < 0) {
goto finish;
}
switch (m->exit_code) {
case MANAGER_EXIT:
log_debug("Exit.");
goto finish;
case MANAGER_RELOAD:
log_info("Reloading.");
if ((r = manager_reload(m)) < 0)
break;
case MANAGER_REEXECUTE:
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"
};
log_notice("Shutting down.");
goto finish;
}
default:
assert_not_reached("Unknown exit code.");
}
}
if (m)
manager_free(m);
label_finish();
if (reexecute) {
const char *args[15];
unsigned i = 0;
char sfd[16];
args[i++] = SYSTEMD_BINARY_PATH;
args[i++] = "--log-level";
args[i++] = "--log-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
args[i++] = "--deserialize";
log_error("Failed to reexecute: %m");
}
if (serialization)
if (fds)
if (shutdown_verb) {
const char * command_line[] = {
};
log_error("Failed to execute shutdown binary, freezing: %m");
}
if (getpid() == 1)
freeze();
return retval;
}