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