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;
43 static int set_default_unit(const char *u) {
56 static int parse_proc_cmdline_word(const char *word) {
58 static const char * const rlmap[] = {
59 "single", SPECIAL_RUNLEVEL1_TARGET,
60 "-s", SPECIAL_RUNLEVEL1_TARGET,
61 "s", SPECIAL_RUNLEVEL1_TARGET,
62 "S", SPECIAL_RUNLEVEL1_TARGET,
63 "1", SPECIAL_RUNLEVEL1_TARGET,
64 "2", SPECIAL_RUNLEVEL2_TARGET,
65 "3", SPECIAL_RUNLEVEL3_TARGET,
66 "4", SPECIAL_RUNLEVEL4_TARGET,
67 "5", SPECIAL_RUNLEVEL5_TARGET
70 if (startswith(word, "systemd.default="))
71 return set_default_unit(word + 15);
73 else if (startswith(word, "systemd.log_target=")) {
75 if (log_set_target_from_string(word + 19) < 0)
76 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
78 } else if (startswith(word, "systemd.log_level=")) {
80 if (log_set_max_level_from_string(word + 18) < 0)
81 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
86 /* SysV compatibility */
88 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
89 if (streq(word, rlmap[i]))
90 return set_default_unit(rlmap[i+1]);
96 static int parse_proc_cmdline(void) {
103 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
104 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
108 FOREACH_WORD_QUOTED(w, l, line, state) {
111 if (!(word = strndup(w, l))) {
116 r = parse_proc_cmdline_word(word);
130 static int parse_argv(int argc, char *argv[]) {
133 ARG_LOG_LEVEL = 0x100,
138 static const struct option options[] = {
139 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
140 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
141 { "default", required_argument, NULL, ARG_DEFAULT },
142 { "help", no_argument, NULL, 'h' },
151 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
156 if ((r = log_set_max_level_from_string(optarg)) < 0) {
157 log_error("Failed to parse log level %s.", optarg);
165 if ((r = log_set_target_from_string(optarg)) < 0) {
166 log_error("Failed to parse log target %s.", optarg);
174 if ((r = set_default_unit(optarg)) < 0) {
175 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
182 action = ACTION_HELP;
189 log_error("Unknown option code %c", c);
194 log_error("Too many arguments.");
201 static int help(void) {
203 printf("%s [options]\n\n"
204 " -h --help Show this help\n"
205 " --default=UNIT Set default unit\n"
206 " --log-level=LEVEL Set log level\n"
207 " --log-target=TARGET Set log target (console, syslog, kmsg)\n",
213 int main(int argc, char *argv[]) {
219 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
222 /* Mount /proc, /sys and friends, so that /proc/cmdline and
223 * /proc/$PID/fd is available. */
226 /* Reset all signal handlers. */
227 assert_se(reset_all_signal_handlers() == 0);
229 /* Close all open files */
230 assert_se(close_all_fds(NULL, 0) == 0);
232 if (parse_proc_cmdline() < 0)
235 log_parse_environment();
237 if (parse_argv(argc, argv) < 0)
240 if (action == ACTION_HELP) {
245 assert_se(action == ACTION_RUN);
247 /* Move out of the way, so that we won't block unmounts */
248 assert_se(chdir("/") == 0);
250 /* Become a session leader if we aren't one yet. */
253 /* Disable the umask logic */
256 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
257 dbus_connection_set_change_sigpipe(FALSE);
259 /* Open the logging devices, if possible and necessary*/
263 if ((r = manager_new(&m)) < 0) {
264 log_error("Failed to allocate manager object: %s", strerror(-r));
268 if ((r = manager_coldplug(m)) < 0) {
269 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
273 log_debug("Activating default unit: %s", default_unit);
275 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
276 log_error("Failed to load default target: %s", strerror(-r));
280 printf("→ By units:\n");
281 manager_dump_units(m, stdout, "\t");
283 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
284 log_error("Failed to start default target: %s", strerror(-r));
288 printf("→ By jobs:\n");
289 manager_dump_jobs(m, stdout, "\t");
291 if ((r = manager_loop(m)) < 0) {
292 log_error("Failed to run mainloop: %s", strerror(-r));