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"
39 } action = ACTION_RUN;
41 static char *default_unit = NULL;
42 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
44 static int set_default_unit(const char *u) {
57 static int parse_proc_cmdline_word(const char *word) {
59 static const char * const rlmap[] = {
60 "single", SPECIAL_RUNLEVEL1_TARGET,
61 "-s", SPECIAL_RUNLEVEL1_TARGET,
62 "s", SPECIAL_RUNLEVEL1_TARGET,
63 "S", SPECIAL_RUNLEVEL1_TARGET,
64 "1", SPECIAL_RUNLEVEL1_TARGET,
65 "2", SPECIAL_RUNLEVEL2_TARGET,
66 "3", SPECIAL_RUNLEVEL3_TARGET,
67 "4", SPECIAL_RUNLEVEL4_TARGET,
68 "5", SPECIAL_RUNLEVEL5_TARGET
71 if (startswith(word, "systemd.default="))
72 return set_default_unit(word + 15);
74 else if (startswith(word, "systemd.log_target=")) {
76 if (log_set_target_from_string(word + 19) < 0)
77 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
79 } else if (startswith(word, "systemd.log_level=")) {
81 if (log_set_max_level_from_string(word + 18) < 0)
82 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
87 /* SysV compatibility */
89 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
90 if (streq(word, rlmap[i]))
91 return set_default_unit(rlmap[i+1]);
97 static int parse_proc_cmdline(void) {
104 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
105 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
109 FOREACH_WORD_QUOTED(w, l, line, state) {
112 if (!(word = strndup(w, l))) {
117 r = parse_proc_cmdline_word(word);
131 static int parse_argv(int argc, char *argv[]) {
134 ARG_LOG_LEVEL = 0x100,
140 static const struct option options[] = {
141 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
142 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
143 { "default", required_argument, NULL, ARG_DEFAULT },
144 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
145 { "help", no_argument, NULL, 'h' },
154 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
159 if ((r = log_set_max_level_from_string(optarg)) < 0) {
160 log_error("Failed to parse log level %s.", optarg);
168 if ((r = log_set_target_from_string(optarg)) < 0) {
169 log_error("Failed to parse log target %s.", optarg);
177 if ((r = set_default_unit(optarg)) < 0) {
178 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
184 case ARG_RUNNING_AS: {
187 if ((as = manager_running_as_from_string(optarg)) < 0) {
188 log_error("Failed to parse running as value %s", optarg);
197 action = ACTION_HELP;
204 log_error("Unknown option code %c", c);
209 log_error("Too many arguments.");
216 static int help(void) {
218 printf("%s [options]\n\n"
219 " -h --help Show this help\n"
220 " --default=UNIT Set default unit\n"
221 " --log-level=LEVEL Set log level\n"
222 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
223 " --running-as=AS Set running as (init, system, sesstion)\n",
229 int main(int argc, char *argv[]) {
236 running_as = MANAGER_INIT;
237 else if (getuid() == 0)
238 running_as = MANAGER_SYSTEM;
240 running_as = MANAGER_SESSION;
242 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
245 /* Mount /proc, /sys and friends, so that /proc/cmdline and
246 * /proc/$PID/fd is available. */
249 /* Reset all signal handlers. */
250 assert_se(reset_all_signal_handlers() == 0);
252 /* Close all open files */
253 assert_se(close_all_fds(NULL, 0) == 0);
255 if (running_as != MANAGER_SESSION)
256 if (parse_proc_cmdline() < 0)
259 log_parse_environment();
261 if (parse_argv(argc, argv) < 0)
264 if (action == ACTION_HELP) {
269 assert_se(action == ACTION_RUN);
271 /* Move out of the way, so that we won't block unmounts */
272 assert_se(chdir("/") == 0);
274 /* Become a session leader if we aren't one yet. */
277 /* Disable the umask logic */
280 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
281 dbus_connection_set_change_sigpipe(FALSE);
283 /* Open the logging devices, if possible and necessary*/
287 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
289 if ((r = manager_new(running_as, &m)) < 0) {
290 log_error("Failed to allocate manager object: %s", strerror(-r));
294 if ((r = manager_coldplug(m)) < 0) {
295 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
299 log_debug("Activating default unit: %s", default_unit);
301 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
302 log_error("Failed to load default target: %s", strerror(-r));
306 printf("→ By units:\n");
307 manager_dump_units(m, stdout, "\t");
309 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
310 log_error("Failed to start default target: %s", strerror(-r));
314 printf("→ By jobs:\n");
315 manager_dump_jobs(m, stdout, "\t");
317 if ((r = manager_loop(m)) < 0) {
318 log_error("Failed to run mainloop: %s", strerror(-r));