chiark / gitweb /
082ac4bb1b7a2c254531453ee372acc1cfd6bfd9
[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 #include "hostname-setup.h"
36 #include "load-fragment.h"
37
38 static enum {
39         ACTION_RUN,
40         ACTION_HELP,
41         ACTION_TEST,
42         ACTION_DUMP_CONFIGURATION_ITEMS
43 } action = ACTION_RUN;
44
45 static char *default_unit = NULL;
46 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
47
48 static int set_default_unit(const char *u) {
49         char *c;
50
51         assert(u);
52
53         if (!(c = strdup(u)))
54                 return -ENOMEM;
55
56         free(default_unit);
57         default_unit = c;
58         return 0;
59 }
60
61 static int parse_proc_cmdline_word(const char *word) {
62
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
73         };
74
75         if (startswith(word, "systemd.default="))
76                 return set_default_unit(word + 16);
77
78         else if (startswith(word, "systemd.log_target=")) {
79
80                 if (log_set_target_from_string(word + 19) < 0)
81                         log_warning("Failed to parse log target %s. Ignoring.", word + 19);
82
83         } else if (startswith(word, "systemd.log_level=")) {
84
85                 if (log_set_max_level_from_string(word + 18) < 0)
86                         log_warning("Failed to parse log level %s. Ignoring.", word + 18);
87
88         } else {
89                 unsigned i;
90
91                 /* SysV compatibility */
92
93                 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
94                         if (streq(word, rlmap[i]))
95                                 return set_default_unit(rlmap[i+1]);
96         }
97
98         return 0;
99 }
100
101 static int parse_proc_cmdline(void) {
102         char *line;
103         int r;
104         char *w;
105         size_t l;
106         char *state;
107
108         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
109                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
110                 return 0;
111         }
112
113         FOREACH_WORD_QUOTED(w, l, line, state) {
114                 char *word;
115
116                 if (!(word = strndup(w, l))) {
117                         r = -ENOMEM;
118                         goto finish;
119                 }
120
121                 r = parse_proc_cmdline_word(word);
122                 free(word);
123
124                 if (r < 0)
125                         goto finish;
126         }
127
128         r = 0;
129
130 finish:
131         free(line);
132         return r;
133 }
134
135 static int parse_argv(int argc, char *argv[]) {
136
137         enum {
138                 ARG_LOG_LEVEL = 0x100,
139                 ARG_LOG_TARGET,
140                 ARG_DEFAULT,
141                 ARG_RUNNING_AS,
142                 ARG_TEST,
143                 ARG_DUMP_CONFIGURATION_ITEMS
144         };
145
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 },
154                 { NULL,         0,                 NULL, 0 }
155         };
156
157         int c, r;
158
159         assert(argc >= 1);
160         assert(argv);
161
162         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
163
164                 switch (c) {
165
166                 case ARG_LOG_LEVEL:
167                         if ((r = log_set_max_level_from_string(optarg)) < 0) {
168                                 log_error("Failed to parse log level %s.", optarg);
169                                 return r;
170                         }
171
172                         break;
173
174                 case ARG_LOG_TARGET:
175
176                         if ((r = log_set_target_from_string(optarg)) < 0) {
177                                 log_error("Failed to parse log target %s.", optarg);
178                                 return r;
179                         }
180
181                         break;
182
183                 case ARG_DEFAULT:
184
185                         if ((r = set_default_unit(optarg)) < 0) {
186                                 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
187                                 return r;
188                         }
189
190                         break;
191
192                 case ARG_RUNNING_AS: {
193                         ManagerRunningAs as;
194
195                         if ((as = manager_running_as_from_string(optarg)) < 0) {
196                                 log_error("Failed to parse running as value %s", optarg);
197                                 return -EINVAL;
198                         }
199
200                         running_as = as;
201                         break;
202                 }
203
204                 case ARG_TEST:
205                         action = ACTION_TEST;
206                         break;
207
208                 case ARG_DUMP_CONFIGURATION_ITEMS:
209                         action = ACTION_DUMP_CONFIGURATION_ITEMS;
210                         break;
211
212                 case 'h':
213                         action = ACTION_HELP;
214                         break;
215
216                 case '?':
217                         return -EINVAL;
218
219                 default:
220                         log_error("Unknown option code %c", c);
221                         return -EINVAL;
222                 }
223
224         return 0;
225 }
226
227 static int help(void) {
228
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",
237                __progname);
238
239         return 0;
240 }
241
242 int main(int argc, char *argv[]) {
243         Manager *m = NULL;
244         Unit *target = NULL;
245         Job *job = NULL;
246         int r, retval = 1;
247
248         if (getpid() == 1)
249                 running_as = MANAGER_INIT;
250         else if (getuid() == 0)
251                 running_as = MANAGER_SYSTEM;
252         else
253                 running_as = MANAGER_SESSION;
254
255         if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
256                 goto finish;
257
258         /* Mount /proc, /sys and friends, so that /proc/cmdline and
259          * /proc/$PID/fd is available. */
260         if (mount_setup() < 0)
261                 goto finish;
262
263         /* Reset all signal handlers. */
264         assert_se(reset_all_signal_handlers() == 0);
265
266         /* Close all open files */
267         assert_se(close_all_fds(NULL, 0) == 0);
268
269         if (running_as != MANAGER_SESSION)
270                 if (parse_proc_cmdline() < 0)
271                         goto finish;
272
273         log_parse_environment();
274
275         if (parse_argv(argc, argv) < 0)
276                 goto finish;
277
278         if (action == ACTION_HELP) {
279                 retval = help();
280                 goto finish;
281         } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
282                 unit_dump_config_items(stdout);
283                 retval = 0;
284                 goto finish;
285         }
286
287         assert_se(action == ACTION_RUN || action == ACTION_TEST);
288
289         /* Set up PATH unless it is already set */
290         setenv("PATH",
291                "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
292                running_as == MANAGER_INIT);
293
294         /* Move out of the way, so that we won't block unmounts */
295         assert_se(chdir("/")  == 0);
296
297         /* Become a session leader if we aren't one yet. */
298         setsid();
299
300         /* Disable the umask logic */
301         umask(0);
302
303         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
304         dbus_connection_set_change_sigpipe(FALSE);
305
306         /* Open the logging devices, if possible and necessary*/
307         log_open_syslog();
308         log_open_kmsg();
309
310         log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
311
312         if (running_as == MANAGER_INIT)
313                 hostname_setup();
314
315         if ((r = manager_new(running_as, &m)) < 0) {
316                 log_error("Failed to allocate manager object: %s", strerror(-r));
317                 goto finish;
318         }
319
320         if ((r = manager_coldplug(m)) < 0) {
321                 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
322                 goto finish;
323         }
324
325         log_debug("Activating default unit: %s", default_unit);
326
327         if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
328                 log_error("Failed to load default target: %s", strerror(-r));
329
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));
333                         goto finish;
334                 }
335         }
336
337         if (action == ACTION_TEST) {
338                 printf("→ By units:\n");
339                 manager_dump_units(m, stdout, "\t");
340         }
341
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));
344                 goto finish;
345         }
346
347         if (action == ACTION_TEST) {
348                 printf("→ By jobs:\n");
349                 manager_dump_jobs(m, stdout, "\t");
350
351                 if (getpid() == 1)
352                         pause();
353
354                 retval = 0;
355                 goto finish;
356         }
357
358         if ((r = manager_loop(m)) < 0) {
359                 log_error("Failed to run mainloop: %s", strerror(-r));
360                 goto finish;
361         }
362
363         retval = 0;
364
365         log_debug("Exit.");
366
367 finish:
368         if (m)
369                 manager_free(m);
370
371         free(default_unit);
372
373         dbus_shutdown();
374
375         return retval;
376 }