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