1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
28 #include <sys/types.h>
34 #include "mount-setup.h"
40 } action = ACTION_RUN;
42 static char *default_unit = NULL;
43 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
45 static int set_default_unit(const char *u) {
58 static int parse_proc_cmdline_word(const char *word) {
60 static const char * const rlmap[] = {
61 "single", SPECIAL_RUNLEVEL1_TARGET,
62 "-s", SPECIAL_RUNLEVEL1_TARGET,
63 "s", SPECIAL_RUNLEVEL1_TARGET,
64 "S", SPECIAL_RUNLEVEL1_TARGET,
65 "1", SPECIAL_RUNLEVEL1_TARGET,
66 "2", SPECIAL_RUNLEVEL2_TARGET,
67 "3", SPECIAL_RUNLEVEL3_TARGET,
68 "4", SPECIAL_RUNLEVEL4_TARGET,
69 "5", SPECIAL_RUNLEVEL5_TARGET
72 if (startswith(word, "systemd.default="))
73 return set_default_unit(word + 15);
75 else if (startswith(word, "systemd.log_target=")) {
77 if (log_set_target_from_string(word + 19) < 0)
78 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
80 } else if (startswith(word, "systemd.log_level=")) {
82 if (log_set_max_level_from_string(word + 18) < 0)
83 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
88 /* SysV compatibility */
90 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
91 if (streq(word, rlmap[i]))
92 return set_default_unit(rlmap[i+1]);
98 static int parse_proc_cmdline(void) {
105 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
106 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
110 FOREACH_WORD_QUOTED(w, l, line, state) {
113 if (!(word = strndup(w, l))) {
118 r = parse_proc_cmdline_word(word);
132 static int parse_argv(int argc, char *argv[]) {
135 ARG_LOG_LEVEL = 0x100,
142 static const struct option options[] = {
143 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
144 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
145 { "default", required_argument, NULL, ARG_DEFAULT },
146 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
147 { "test", no_argument, NULL, ARG_TEST },
148 { "help", no_argument, NULL, 'h' },
157 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
162 if ((r = log_set_max_level_from_string(optarg)) < 0) {
163 log_error("Failed to parse log level %s.", optarg);
171 if ((r = log_set_target_from_string(optarg)) < 0) {
172 log_error("Failed to parse log target %s.", optarg);
180 if ((r = set_default_unit(optarg)) < 0) {
181 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
187 case ARG_RUNNING_AS: {
190 if ((as = manager_running_as_from_string(optarg)) < 0) {
191 log_error("Failed to parse running as value %s", optarg);
200 action = ACTION_TEST;
204 action = ACTION_HELP;
211 log_error("Unknown option code %c", c);
218 static int help(void) {
220 printf("%s [options]\n\n"
221 " -h --help Show this help\n"
222 " --default=UNIT Set default unit\n"
223 " --log-level=LEVEL Set log level\n"
224 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
225 " --running-as=AS Set running as (init, system, session)\n"
226 " --test Determine startup sequence, dump it and exit\n",
232 int main(int argc, char *argv[]) {
239 running_as = MANAGER_INIT;
240 else if (getuid() == 0)
241 running_as = MANAGER_SYSTEM;
243 running_as = MANAGER_SESSION;
245 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
248 /* Mount /proc, /sys and friends, so that /proc/cmdline and
249 * /proc/$PID/fd is available. */
250 if (mount_setup() < 0)
253 /* Reset all signal handlers. */
254 assert_se(reset_all_signal_handlers() == 0);
256 /* Close all open files */
257 assert_se(close_all_fds(NULL, 0) == 0);
259 if (running_as != MANAGER_SESSION)
260 if (parse_proc_cmdline() < 0)
263 log_parse_environment();
265 if (parse_argv(argc, argv) < 0)
268 if (action == ACTION_HELP) {
273 assert_se(action == ACTION_RUN || action == ACTION_TEST);
275 /* Move out of the way, so that we won't block unmounts */
276 assert_se(chdir("/") == 0);
278 /* Become a session leader if we aren't one yet. */
281 /* Disable the umask logic */
284 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
285 dbus_connection_set_change_sigpipe(FALSE);
287 /* Open the logging devices, if possible and necessary*/
291 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
293 if ((r = manager_new(running_as, &m)) < 0) {
294 log_error("Failed to allocate manager object: %s", strerror(-r));
298 if ((r = manager_coldplug(m)) < 0) {
299 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
303 log_debug("Activating default unit: %s", default_unit);
305 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
306 log_error("Failed to load default target: %s", strerror(-r));
310 if (action == ACTION_TEST) {
311 printf("→ By units:\n");
312 manager_dump_units(m, stdout, "\t");
315 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
316 log_error("Failed to start default target: %s", strerror(-r));
320 if (action == ACTION_TEST) {
321 printf("→ By jobs:\n");
322 manager_dump_jobs(m, stdout, "\t");
331 if ((r = manager_loop(m)) < 0) {
332 log_error("Failed to run mainloop: %s", strerror(-r));