]> git.ipfire.org Git - people/ms/systemd.git/blob - main.c
0eb64d265928e918b61e0797ebd825ed0b365ae6
[people/ms/systemd.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 #include <signal.h>
32 #include <sys/wait.h>
33 #include <fcntl.h>
34
35 #include "manager.h"
36 #include "log.h"
37 #include "mount-setup.h"
38 #include "hostname-setup.h"
39 #include "load-fragment.h"
40
41 static enum {
42 ACTION_RUN,
43 ACTION_HELP,
44 ACTION_TEST,
45 ACTION_DUMP_CONFIGURATION_ITEMS
46 } action = ACTION_RUN;
47
48 static char *default_unit = NULL;
49 static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID;
50
51 static bool dump_core = true;
52 static bool crash_shell = false;
53 static int crash_chvt = -1;
54
55 static bool confirm_spawn = false;
56
57 _noreturn static void freeze(void) {
58 for (;;)
59 pause();
60 }
61
62 _noreturn static void crash(int sig) {
63
64 if (!dump_core)
65 log_error("Caught <%s>, not dumping core.", strsignal(sig));
66 else {
67 pid_t pid;
68
69 if ((pid = fork()) < 0)
70 log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno));
71
72 else if (pid == 0) {
73 struct sigaction sa;
74 struct rlimit rl;
75
76 /* Enable default signal handler for core dump */
77 zero(sa);
78 sa.sa_handler = SIG_DFL;
79 assert_se(sigaction(sig, &sa, NULL) == 0);
80
81 /* Don't limit the core dump size */
82 zero(rl);
83 rl.rlim_cur = RLIM_INFINITY;
84 rl.rlim_max = RLIM_INFINITY;
85 setrlimit(RLIMIT_CORE, &rl);
86
87 /* Just to be sure... */
88 assert_se(chdir("/") == 0);
89
90 /* Raise the signal again */
91 raise(sig);
92
93 assert_not_reached("We shouldn't be here...");
94 _exit(1);
95
96 } else {
97 int status, r;
98
99 /* Order things nicely. */
100 if ((r = waitpid(pid, &status, 0)) < 0)
101 log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno));
102 else if (!WCOREDUMP(status))
103 log_error("Caught <%s>, core dump failed.", strsignal(sig));
104 else
105 log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid);
106 }
107 }
108
109 if (crash_chvt)
110 chvt(crash_chvt);
111
112 if (crash_shell) {
113 log_info("Executing crash shell in 10s...");
114 sleep(10);
115
116 execl("/bin/sh", "/bin/sh", NULL);
117 log_error("execl() failed: %s", strerror(errno));
118 }
119
120 log_info("Freezing execution.");
121 freeze();
122 }
123
124 static void install_crash_handler(void) {
125 struct sigaction sa;
126
127 zero(sa);
128
129 sa.sa_handler = crash;
130 sa.sa_flags = SA_NODEFER;
131
132 assert_se(sigaction(SIGSEGV, &sa, NULL) == 0);
133 assert_se(sigaction(SIGILL, &sa, NULL) == 0);
134 assert_se(sigaction(SIGFPE, &sa, NULL) == 0);
135 assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
136 assert_se(sigaction(SIGQUIT, &sa, NULL) == 0);
137 assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
138 }
139
140 static int console_setup(void) {
141 int tty_fd = -1, null_fd = -1, r = 0;
142
143 /* If we are init, we connect stdout/stderr to /dev/console
144 * and stdin to /dev/null and make sure we don't have a
145 * controlling tty. */
146
147 release_terminal();
148
149 if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) {
150 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
151 r = -tty_fd;
152 goto finish;
153 }
154
155 if ((null_fd = open("/dev/null", O_RDONLY)) < 0) {
156 log_error("Failed to open /dev/null: %m");
157 r = -errno;
158 goto finish;
159 }
160
161 assert(tty_fd >= 3);
162 assert(null_fd >= 3);
163
164 if (reset_terminal(tty_fd) < 0)
165 log_error("Failed to reset /dev/console: %m");
166
167 if (dup2(tty_fd, STDOUT_FILENO) < 0 ||
168 dup2(tty_fd, STDERR_FILENO) < 0 ||
169 dup2(null_fd, STDIN_FILENO) < 0) {
170 log_error("Failed to dup2() device: %m");
171 r = -errno;
172 goto finish;
173 }
174
175 r = 0;
176
177 finish:
178 if (tty_fd >= 0)
179 close_nointr(tty_fd);
180
181 if (null_fd >= 0)
182 close_nointr(null_fd);
183
184 return r;
185 }
186
187 static int set_default_unit(const char *u) {
188 char *c;
189
190 assert(u);
191
192 if (!(c = strdup(u)))
193 return -ENOMEM;
194
195 free(default_unit);
196 default_unit = c;
197 return 0;
198 }
199
200 static int parse_proc_cmdline_word(const char *word) {
201
202 static const char * const rlmap[] = {
203 "single", SPECIAL_RUNLEVEL1_TARGET,
204 "-s", SPECIAL_RUNLEVEL1_TARGET,
205 "s", SPECIAL_RUNLEVEL1_TARGET,
206 "S", SPECIAL_RUNLEVEL1_TARGET,
207 "1", SPECIAL_RUNLEVEL1_TARGET,
208 "2", SPECIAL_RUNLEVEL2_TARGET,
209 "3", SPECIAL_RUNLEVEL3_TARGET,
210 "4", SPECIAL_RUNLEVEL4_TARGET,
211 "5", SPECIAL_RUNLEVEL5_TARGET
212 };
213
214 if (startswith(word, "systemd.default="))
215 return set_default_unit(word + 16);
216
217 else if (startswith(word, "systemd.log_target=")) {
218
219 if (log_set_target_from_string(word + 19) < 0)
220 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
221
222 } else if (startswith(word, "systemd.log_level=")) {
223
224 if (log_set_max_level_from_string(word + 18) < 0)
225 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
226
227 } else if (startswith(word, "systemd.dump_core=")) {
228 int r;
229
230 if ((r = parse_boolean(word + 18)) < 0)
231 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
232 else
233 dump_core = r;
234
235 } else if (startswith(word, "systemd.crash_shell=")) {
236 int r;
237
238 if ((r = parse_boolean(word + 20)) < 0)
239 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
240 else
241 crash_shell = r;
242
243 } else if (startswith(word, "systemd.crash_chvt=")) {
244 int k;
245
246 if (safe_atoi(word + 19, &k) < 0)
247 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
248 else
249 crash_chvt = k;
250
251 } else if (startswith(word, "systemd.")) {
252
253 log_warning("Unknown kernel switch %s. Ignoring.", word);
254
255 log_info("Supported kernel switches:");
256 log_info("systemd.default=UNIT Default unit to start");
257 log_info("systemd.log_target=console|kmsg|syslog Log target");
258 log_info("systemd.log_level=LEVEL Log level");
259 log_info("systemd.dump_core=0|1 Dump core on crash");
260 log_info("systemd.crash_shell=0|1 On crash run shell");
261 log_info("systemd.crash_chvt=N Change to VT #N on crash");
262
263 } else {
264 unsigned i;
265
266 /* SysV compatibility */
267 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
268 if (streq(word, rlmap[i]))
269 return set_default_unit(rlmap[i+1]);
270 }
271
272 return 0;
273 }
274
275 static int parse_proc_cmdline(void) {
276 char *line;
277 int r;
278 char *w;
279 size_t l;
280 char *state;
281
282 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
283 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno));
284 return 0;
285 }
286
287 FOREACH_WORD_QUOTED(w, l, line, state) {
288 char *word;
289
290 if (!(word = strndup(w, l))) {
291 r = -ENOMEM;
292 goto finish;
293 }
294
295 r = parse_proc_cmdline_word(word);
296 free(word);
297
298 if (r < 0)
299 goto finish;
300 }
301
302 r = 0;
303
304 finish:
305 free(line);
306 return r;
307 }
308
309 static int parse_argv(int argc, char *argv[]) {
310
311 enum {
312 ARG_LOG_LEVEL = 0x100,
313 ARG_LOG_TARGET,
314 ARG_DEFAULT,
315 ARG_RUNNING_AS,
316 ARG_TEST,
317 ARG_DUMP_CONFIGURATION_ITEMS,
318 ARG_CONFIRM_SPAWN
319 };
320
321 static const struct option options[] = {
322 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
323 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
324 { "default", required_argument, NULL, ARG_DEFAULT },
325 { "running-as", required_argument, NULL, ARG_RUNNING_AS },
326 { "test", no_argument, NULL, ARG_TEST },
327 { "help", no_argument, NULL, 'h' },
328 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
329 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
330 { NULL, 0, NULL, 0 }
331 };
332
333 int c, r;
334
335 assert(argc >= 1);
336 assert(argv);
337
338 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
339
340 switch (c) {
341
342 case ARG_LOG_LEVEL:
343 if ((r = log_set_max_level_from_string(optarg)) < 0) {
344 log_error("Failed to parse log level %s.", optarg);
345 return r;
346 }
347
348 break;
349
350 case ARG_LOG_TARGET:
351
352 if ((r = log_set_target_from_string(optarg)) < 0) {
353 log_error("Failed to parse log target %s.", optarg);
354 return r;
355 }
356
357 break;
358
359 case ARG_DEFAULT:
360
361 if ((r = set_default_unit(optarg)) < 0) {
362 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
363 return r;
364 }
365
366 break;
367
368 case ARG_RUNNING_AS: {
369 ManagerRunningAs as;
370
371 if ((as = manager_running_as_from_string(optarg)) < 0) {
372 log_error("Failed to parse running as value %s", optarg);
373 return -EINVAL;
374 }
375
376 running_as = as;
377 break;
378 }
379
380 case ARG_TEST:
381 action = ACTION_TEST;
382 break;
383
384 case ARG_DUMP_CONFIGURATION_ITEMS:
385 action = ACTION_DUMP_CONFIGURATION_ITEMS;
386 break;
387
388 case ARG_CONFIRM_SPAWN:
389 confirm_spawn = true;
390 break;
391
392 case 'h':
393 action = ACTION_HELP;
394 break;
395
396 case '?':
397 return -EINVAL;
398
399 default:
400 log_error("Unknown option code %c", c);
401 return -EINVAL;
402 }
403
404 return 0;
405 }
406
407 static int help(void) {
408
409 printf("%s [options]\n\n"
410 " -h --help Show this help\n"
411 " --default=UNIT Set default unit\n"
412 " --log-level=LEVEL Set log level\n"
413 " --log-target=TARGET Set log target (console, syslog, kmsg)\n"
414 " --running-as=AS Set running as (init, system, session)\n"
415 " --test Determine startup sequence, dump it and exit\n"
416 " --dump-configuration-items Dump understood unit configuration items\n"
417 " --confirm-spawn Ask for confirmation when spawning processes\n",
418 __progname);
419
420 return 0;
421 }
422
423 int main(int argc, char *argv[]) {
424 Manager *m = NULL;
425 Unit *target = NULL;
426 Job *job = NULL;
427 int r, retval = 1;
428
429 if (getpid() == 1)
430 running_as = MANAGER_INIT;
431 else if (getuid() == 0)
432 running_as = MANAGER_SYSTEM;
433 else
434 running_as = MANAGER_SESSION;
435
436 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
437 goto finish;
438
439 /* Mount /proc, /sys and friends, so that /proc/cmdline and
440 * /proc/$PID/fd is available. */
441 if (mount_setup() < 0)
442 goto finish;
443
444 /* Reset all signal handlers. */
445 assert_se(reset_all_signal_handlers() == 0);
446
447 /* If we are init, we can block sigkill. Yay. */
448 signal(SIGKILL, SIG_IGN);
449 signal(SIGPIPE, SIG_IGN);
450
451 /* Close all open files */
452 assert_se(close_all_fds(NULL, 0) == 0);
453
454 if (running_as != MANAGER_SESSION)
455 if (parse_proc_cmdline() < 0)
456 goto finish;
457
458 log_parse_environment();
459
460 if (parse_argv(argc, argv) < 0)
461 goto finish;
462
463 if (action == ACTION_HELP) {
464 retval = help();
465 goto finish;
466 } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) {
467 unit_dump_config_items(stdout);
468 retval = 0;
469 goto finish;
470 }
471
472 assert_se(action == ACTION_RUN || action == ACTION_TEST);
473
474 /* Set up PATH unless it is already set */
475 setenv("PATH",
476 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
477 running_as == MANAGER_INIT);
478
479 /* Move out of the way, so that we won't block unmounts */
480 assert_se(chdir("/") == 0);
481
482 if (running_as != MANAGER_SESSION) {
483 /* Become a session leader if we aren't one yet. */
484 setsid();
485
486 /* Disable the umask logic */
487 umask(0);
488 }
489
490 if (running_as == MANAGER_INIT)
491 console_setup();
492
493 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
494 dbus_connection_set_change_sigpipe(FALSE);
495
496 /* Open the logging devices, if possible and necessary */
497 log_open_syslog();
498 log_open_kmsg();
499
500 /* Make sure we leave a core dump without panicing the
501 * kernel. */
502 if (getpid() == 1)
503 install_crash_handler();
504
505 log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as));
506
507 if (running_as == MANAGER_INIT)
508 hostname_setup();
509
510 if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) {
511 log_error("Failed to allocate manager object: %s", strerror(-r));
512 goto finish;
513 }
514
515 if ((r = manager_coldplug(m)) < 0) {
516 log_error("Failed to retrieve coldplug information: %s", strerror(-r));
517 goto finish;
518 }
519
520 log_debug("Activating default unit: %s", default_unit);
521
522 if ((r = manager_load_unit(m, default_unit, &target)) < 0) {
523 log_error("Failed to load default target: %s", strerror(-r));
524
525 log_info("Trying to load rescue target...");
526 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) {
527 log_error("Failed to load rescue target: %s", strerror(-r));
528 goto finish;
529 }
530 }
531
532 if (action == ACTION_TEST) {
533 printf("→ By units:\n");
534 manager_dump_units(m, stdout, "\t");
535 }
536
537 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) {
538 log_error("Failed to start default target: %s", strerror(-r));
539 goto finish;
540 }
541
542 if (action == ACTION_TEST) {
543 printf("→ By jobs:\n");
544 manager_dump_jobs(m, stdout, "\t");
545
546 if (getpid() == 1)
547 pause();
548
549 retval = 0;
550 goto finish;
551 }
552
553 if ((r = manager_loop(m)) < 0) {
554 log_error("Failed to run mainloop: %s", strerror(-r));
555 goto finish;
556 }
557
558 retval = 0;
559
560 log_debug("Exit.");
561
562 finish:
563 if (m)
564 manager_free(m);
565
566 free(default_unit);
567
568 dbus_shutdown();
569
570 return retval;
571 }