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"
35 #include "hostname-setup.h"
36 #include "load-fragment.h"
42 ACTION_DUMP_CONFIGURATION_ITEMS
43 } action = ACTION_RUN;
45 static char *default_unit = NULL;
46 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
48 static int set_default_unit(const char *u) {
61 static int parse_proc_cmdline_word(const char *word) {
63 static const char * const rlmap[] = {
64 "single", SPECIAL_RUNLEVEL1_TARGET,
65 "-s", SPECIAL_RUNLEVEL1_TARGET,
66 "s", SPECIAL_RUNLEVEL1_TARGET,
67 "S", SPECIAL_RUNLEVEL1_TARGET,
68 "1", SPECIAL_RUNLEVEL1_TARGET,
69 "2", SPECIAL_RUNLEVEL2_TARGET,
70 "3", SPECIAL_RUNLEVEL3_TARGET,
71 "4", SPECIAL_RUNLEVEL4_TARGET,
72 "5", SPECIAL_RUNLEVEL5_TARGET
75 if (startswith(word, "systemd.default="))
76 return set_default_unit(word + 16);
78 else if (startswith(word, "systemd.log_target=")) {
80 if (log_set_target_from_string(word + 19) < 0)
81 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
83 } else if (startswith(word, "systemd.log_level=")) {
85 if (log_set_max_level_from_string(word + 18) < 0)
86 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
91 /* SysV compatibility */
93 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
94 if (streq(word, rlmap[i]))
95 return set_default_unit(rlmap[i+1]);
101 static int parse_proc_cmdline(void) {
108 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
109 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
113 FOREACH_WORD_QUOTED(w, l, line, state) {
116 if (!(word = strndup(w, l))) {
121 r = parse_proc_cmdline_word(word);
135 static int parse_argv(int argc, char *argv[]) {
138 ARG_LOG_LEVEL = 0x100,
143 ARG_DUMP_CONFIGURATION_ITEMS
146 static const struct option options[] = {
147 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
148 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
149 { "default", required_argument, NULL, ARG_DEFAULT },
150 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
151 { "test", no_argument, NULL, ARG_TEST },
152 { "help", no_argument, NULL, 'h' },
153 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
162 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
167 if ((r = log_set_max_level_from_string(optarg)) < 0) {
168 log_error("Failed to parse log level %s.", optarg);
176 if ((r = log_set_target_from_string(optarg)) < 0) {
177 log_error("Failed to parse log target %s.", optarg);
185 if ((r = set_default_unit(optarg)) < 0) {
186 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
192 case ARG_RUNNING_AS: {
195 if ((as = manager_running_as_from_string(optarg)) < 0) {
196 log_error("Failed to parse running as value %s", optarg);
205 action = ACTION_TEST;
208 case ARG_DUMP_CONFIGURATION_ITEMS:
209 action = ACTION_DUMP_CONFIGURATION_ITEMS;
213 action = ACTION_HELP;
220 log_error("Unknown option code %c", c);
227 static int help(void) {
229 printf("%s [options]\n\n"
230 " -h --help Show this help\n"
231 " --default=UNIT Set default unit\n"
232 " --log-level=LEVEL Set log level\n"
233 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
234 " --running-as=AS Set running as (init, system, session)\n"
235 " --test Determine startup sequence, dump it and exit\n"
236 " --dump-configuration-items Dump understood unit configuration items\n",
242 int main(int argc, char *argv[]) {
249 running_as = MANAGER_INIT;
250 else if (getuid() == 0)
251 running_as = MANAGER_SYSTEM;
253 running_as = MANAGER_SESSION;
255 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
258 /* Mount /proc, /sys and friends, so that /proc/cmdline and
259 * /proc/$PID/fd is available. */
260 if (mount_setup() < 0)
263 /* Reset all signal handlers. */
264 assert_se(reset_all_signal_handlers() == 0);
266 /* Close all open files */
267 assert_se(close_all_fds(NULL, 0) == 0);
269 if (running_as != MANAGER_SESSION)
270 if (parse_proc_cmdline() < 0)
273 log_parse_environment();
275 if (parse_argv(argc, argv) < 0)
278 if (action == ACTION_HELP) {
281 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
282 unit_dump_config_items(stdout);
287 assert_se(action == ACTION_RUN || action == ACTION_TEST);
289 /* Set up PATH unless it is already set */
291 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
292 running_as == MANAGER_INIT);
294 /* Move out of the way, so that we won't block unmounts */
295 assert_se(chdir("/") == 0);
297 /* Become a session leader if we aren't one yet. */
300 /* Disable the umask logic */
303 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
304 dbus_connection_set_change_sigpipe(FALSE);
306 /* Open the logging devices, if possible and necessary*/
310 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
312 if (running_as == MANAGER_INIT)
315 if ((r = manager_new(running_as, &m)) < 0) {
316 log_error("Failed to allocate manager object: %s", strerror(-r));
320 if ((r = manager_coldplug(m)) < 0) {
321 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
325 log_debug("Activating default unit: %s", default_unit);
327 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
328 log_error("Failed to load default target: %s", strerror(-r));
330 log_info("Trying to load rescue target...");
331 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
332 log_error("Failed to load rescue target: %s", strerror(-r));
337 if (action == ACTION_TEST) {
338 printf("→ By units:\n");
339 manager_dump_units(m, stdout, "\t");
342 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
343 log_error("Failed to start default target: %s", strerror(-r));
347 if (action == ACTION_TEST) {
348 printf("→ By jobs:\n");
349 manager_dump_jobs(m, stdout, "\t");
358 if ((r = manager_loop(m)) < 0) {
359 log_error("Failed to run mainloop: %s", strerror(-r));