main.c revision e99e38bbdcca3fe5956823bdb3d38544ccf93221
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
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 "load-fragment.h"
#include "fdset.h"
static enum {
} action = ACTION_RUN;
static char *default_unit = NULL;
static bool dump_core = true;
static bool crash_shell = false;
static int crash_chvt = -1;
static bool confirm_spawn = false;
for (;;)
pause();
}
static void nop_handler(int sig) {
}
if (!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 status, r;
/* Order things nicely. */
else
}
}
if (crash_chvt)
if (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;
_exit(1);
}
if ((r = make_stdio(fd)) < 0) {
_exit(1);
}
_exit(1);
}
}
log_info("Freezing execution.");
freeze();
}
static void install_crash_handler(void) {
}
static int make_null_stdio(void) {
int null_fd, r;
return -errno;
}
if ((r = make_stdio(null_fd)) < 0)
return r;
}
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;
default_unit = c;
return 0;
}
static int parse_proc_cmdline_word(const char *word) {
static const char * const rlmap[] = {
"single", SPECIAL_RUNLEVEL1_TARGET,
"-s", SPECIAL_RUNLEVEL1_TARGET,
"s", SPECIAL_RUNLEVEL1_TARGET,
"S", SPECIAL_RUNLEVEL1_TARGET,
"1", SPECIAL_RUNLEVEL1_TARGET,
"2", SPECIAL_RUNLEVEL2_TARGET,
"3", SPECIAL_RUNLEVEL3_TARGET,
"4", SPECIAL_RUNLEVEL4_TARGET,
};
int r;
else
dump_core = r;
int r;
else
crash_shell = r;
int r;
else
confirm_spawn = r;
int k;
else
crash_chvt = k;
log_info("Supported kernel switches:");
log_info("systemd.default=UNIT Default unit to start");
log_info("systemd.log_target=console|kmsg|syslog Log target");
log_info("systemd.log_level=LEVEL Log level");
log_info("systemd.dump_core=0|1 Dump core on crash");
log_info("systemd.confirm_spawn=0|1 Confirm every process spawn");
} else {
unsigned i;
/* SysV compatibility */
}
return 0;
}
static int parse_proc_cmdline(void) {
char *line;
int r;
char *w;
size_t l;
char *state;
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,
};
};
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_DEFAULT:
if ((r = set_default_unit(optarg)) < 0) {
return r;
}
break;
case ARG_RUNNING_AS: {
return -EINVAL;
}
running_as = as;
break;
}
case ARG_TEST:
break;
break;
case ARG_CONFIRM_SPAWN:
confirm_spawn = true;
break;
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 'h':
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"
" -h --help Show this help\n"
" --default=UNIT Set default unit\n"
" --log-level=LEVEL Set log level\n"
" --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg)\n"
" --running-as=AS Set running as (init, system, session)\n"
" --test Determine startup sequence, dump it and exit\n"
" --dump-configuration-items Dump understood unit configuration items\n"
" --confirm-spawn Ask for confirmation when spawning processes\n",
return 0;
}
int r;
assert(m);
if ((r = manager_open_serialization(&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;
}
int r, retval = 1;
bool reexecute = false;
if (getpid() == 1) {
} 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 (running_as != MANAGER_SESSION)
if (parse_proc_cmdline() < 0)
goto finish;
goto finish;
if (action == ACTION_HELP) {
goto finish;
} else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
retval = 0;
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",
running_as == MANAGER_INIT);
/* Move out of the way, so that we won't block unmounts */
if (running_as != MANAGER_SESSION) {
/* 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)
if (running_as == MANAGER_INIT) {
}
goto finish;
}
if (fds) {
/* This will close all file descriptors that were opened, but
* not claimed by any unit. */
}
if (serialization) {
} else {
log_info("Trying to load rescue target...");
goto finish;
}
}
if (action == ACTION_TEST) {
printf("-> By units:\n");
}
goto finish;
}
if (action == ACTION_TEST) {
printf("-> By jobs:\n");
retval = 0;
goto finish;
}
}
for (;;) {
if ((r = manager_loop(m)) < 0) {
goto finish;
}
switch (m->exit_code) {
case MANAGER_EXIT:
retval = 0;
log_debug("Exit.");
goto finish;
case MANAGER_RELOAD:
if ((r = manager_reload(m)) < 0)
break;
case MANAGER_REEXECUTE:
goto finish;
reexecute = true;
log_debug("Reexecuting.");
goto finish;
default:
assert_not_reached("Unknown exit code.");
}
}
if (m)
manager_free(m);
if (reexecute) {
const char *args[11];
unsigned i = 0;
char sfd[16];
args[i++] = SYSTEMD_BINARY_PATH;
args[i++] = "--log-level";
args[i++] = "--log-target";
args[i++] = "--running-as";
args[i++] = "--deserialize";
if (confirm_spawn)
args[i++] = "--confirm-spawn";
log_error("Failed to reexecute: %m");
}
if (serialization)
if (fds)
if (getpid() == 1)
freeze();
return retval;
}