chiark / gitweb /
main: parse our own command line and the kernel command line
[elogind.git] / main.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <dbus/dbus.h>
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <getopt.h>
31
32 #include "manager.h"
33 #include "log.h"
34 #include "mount-setup.h"
35
36 static enum {
37         ACTION_RUN,
38         ACTION_HELP
39 } action = ACTION_RUN;
40
41 static char *default_unit = NULL;
42
43 static int set_default_unit(const char *u) {
44         char *c;
45
46         assert(u);
47
48         if (!(c = strdup(u)))
49                 return -ENOMEM;
50
51         free(default_unit);
52         default_unit = c;
53         return 0;
54 }
55
56 static int parse_proc_cmdline_word(const char *word) {
57
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
68         };
69
70         if (startswith(word, "systemd.default="))
71                 return set_default_unit(word + 15);
72
73         else if (startswith(word, "systemd.log_target=")) {
74
75                 if (log_set_target_from_string(word + 19) < 0)
76                         log_warning("Failed to parse log target %s. Ignoring.", word + 19);
77
78         } else if (startswith(word, "systemd.log_level=")) {
79
80                 if (log_set_max_level_from_string(word + 18) < 0)
81                         log_warning("Failed to parse log level %s. Ignoring.", word + 18);
82
83         } else {
84                 unsigned i;
85
86                 /* SysV compatibility */
87
88                 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
89                         if (streq(word, rlmap[i]))
90                                 return set_default_unit(rlmap[i+1]);
91         }
92
93         return 0;
94 }
95
96 static int parse_proc_cmdline(void) {
97         char *line;
98         int r;
99         char *w;
100         size_t l;
101         char *state;
102
103         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
104                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
105                 return 0;
106         }
107
108         FOREACH_WORD_QUOTED(w, l, line, state) {
109                 char *word;
110
111                 if (!(word = strndup(w, l))) {
112                         r = -ENOMEM;
113                         goto finish;
114                 }
115
116                 r = parse_proc_cmdline_word(word);
117                 free(word);
118
119                 if (r < 0)
120                         goto finish;
121         }
122
123         r = 0;
124
125 finish:
126         free(line);
127         return r;
128 }
129
130 static int parse_argv(int argc, char *argv[]) {
131
132         enum {
133                 ARG_LOG_LEVEL = 0x100,
134                 ARG_LOG_TARGET,
135                 ARG_DEFAULT
136         };
137
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' },
143                 { NULL,         0,                 NULL, 0 }
144         };
145
146         int c, r;
147
148         assert(argc >= 1);
149         assert(argv);
150
151         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
152
153                 switch (c) {
154
155                 case ARG_LOG_LEVEL:
156                         if ((r = log_set_max_level_from_string(optarg)) < 0) {
157                                 log_error("Failed to parse log level %s.", optarg);
158                                 return r;
159                         }
160
161                         break;
162
163                 case ARG_LOG_TARGET:
164
165                         if ((r = log_set_target_from_string(optarg)) < 0) {
166                                 log_error("Failed to parse log target %s.", optarg);
167                                 return r;
168                         }
169
170                         break;
171
172                 case ARG_DEFAULT:
173
174                         if ((r = set_default_unit(optarg)) < 0) {
175                                 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
176                                 return r;
177                         }
178
179                         break;
180
181                 case 'h':
182                         action = ACTION_HELP;
183                         break;
184
185                 case '?':
186                         return -EINVAL;
187
188                 default:
189                         log_error("Unknown option code %c", c);
190                         return -EINVAL;
191                 }
192
193         if (optind < argc) {
194                 log_error("Too many arguments.");
195                 return -EINVAL;
196         }
197
198         return 0;
199 }
200
201 static int help(void) {
202
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",
208                __progname);
209
210         return 0;
211 }
212
213 int main(int argc, char *argv[]) {
214         Manager *m = NULL;
215         Unit *target = NULL;
216         Job *job = NULL;
217         int r, retval = 1;
218
219         if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
220                 goto finish;
221
222         /* Mount /proc, /sys and friends, so that /proc/cmdline and
223          * /proc/$PID/fd is available. */
224         mount_setup();
225
226         /* Reset all signal handlers. */
227         assert_se(reset_all_signal_handlers() == 0);
228
229         /* Close all open files */
230         assert_se(close_all_fds(NULL, 0) == 0);
231
232         if (parse_proc_cmdline() < 0)
233                 goto finish;
234
235         log_parse_environment();
236
237         if (parse_argv(argc, argv) < 0)
238                 goto finish;
239
240         if (action == ACTION_HELP) {
241                 retval = help();
242                 goto finish;
243         }
244
245         assert_se(action == ACTION_RUN);
246
247         /* Move out of the way, so that we won't block unmounts */
248         assert_se(chdir("/")  == 0);
249
250         /* Become a session leader if we aren't one yet. */
251         setsid();
252
253         /* Disable the umask logic */
254         umask(0);
255
256         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
257         dbus_connection_set_change_sigpipe(FALSE);
258
259         /* Open the logging devices, if possible and necessary*/
260         log_open_syslog();
261         log_open_kmsg();
262
263         if ((r = manager_new(&m)) < 0) {
264                 log_error("Failed to allocate manager object: %s", strerror(-r));
265                 goto finish;
266         }
267
268         if ((r = manager_coldplug(m)) < 0) {
269                 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
270                 goto finish;
271         }
272
273         log_debug("Activating default unit: %s", default_unit);
274
275         if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
276                 log_error("Failed to load default target: %s", strerror(-r));
277                 goto finish;
278         }
279
280         printf("→ By units:\n");
281         manager_dump_units(m, stdout, "\t");
282
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));
285                 goto finish;
286         }
287
288         printf("→ By jobs:\n");
289         manager_dump_jobs(m, stdout, "\t");
290
291         if ((r = manager_loop(m)) < 0) {
292                 log_error("Failed to run mainloop: %s", strerror(-r));
293                 goto finish;
294         }
295
296         retval = 0;
297
298         log_debug("Exit.");
299
300 finish:
301         if (m)
302                 manager_free(m);
303
304         free(default_unit);
305
306         dbus_shutdown();
307
308         return retval;
309 }