]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/main.c
units: fix conflicts between random seed services
[thirdparty/systemd.git] / src / main.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 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 38#include "hostname-setup.h"
af5bc85d 39#include "loopback-setup.h"
11c3a4ee 40#include "kmod-setup.h"
302e8c4c 41#include "load-fragment.h"
a16e1123 42#include "fdset.h"
514f4ef5 43#include "special.h"
487393e9 44#include "conf-parser.h"
398ef8ba 45#include "bus-errors.h"
ad780f19 46#include "missing.h"
e51bc1a2 47#include "label.h"
302e27c8 48#include "build.h"
60918275 49
f170852a
LP
50static enum {
51 ACTION_RUN,
e965d56d 52 ACTION_HELP,
e537352b 53 ACTION_TEST,
4288f619
LP
54 ACTION_DUMP_CONFIGURATION_ITEMS,
55 ACTION_DONE
fa0f4d8a 56} arg_action = ACTION_RUN;
f170852a 57
fa0f4d8a
LP
58static char *arg_default_unit = NULL;
59static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
60
61static bool arg_dump_core = true;
62static bool arg_crash_shell = false;
63static int arg_crash_chvt = -1;
64static bool arg_confirm_spawn = false;
9e58ff9c 65static bool arg_show_status = true;
6e98720f 66static bool arg_sysv_console = true;
4fc935ca 67
a16e1123 68static FILE* serialization = NULL;
80876c20 69
93a46b0b 70_noreturn_ static void freeze(void) {
97c4f35c
LP
71 for (;;)
72 pause();
73}
74
6f5e3f35
LP
75static void nop_handler(int sig) {
76}
77
93a46b0b 78_noreturn_ static void crash(int sig) {
97c4f35c 79
fa0f4d8a 80 if (!arg_dump_core)
582a507f 81 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
97c4f35c 82 else {
6f5e3f35 83 struct sigaction sa;
97c4f35c
LP
84 pid_t pid;
85
6f5e3f35
LP
86 /* We want to wait for the core process, hence let's enable SIGCHLD */
87 zero(sa);
88 sa.sa_handler = nop_handler;
89 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
90 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
91
97c4f35c 92 if ((pid = fork()) < 0)
582a507f 93 log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
97c4f35c
LP
94
95 else if (pid == 0) {
97c4f35c
LP
96 struct rlimit rl;
97
98 /* Enable default signal handler for core dump */
99 zero(sa);
100 sa.sa_handler = SIG_DFL;
101 assert_se(sigaction(sig, &sa, NULL) == 0);
102
103 /* Don't limit the core dump size */
104 zero(rl);
105 rl.rlim_cur = RLIM_INFINITY;
106 rl.rlim_max = RLIM_INFINITY;
107 setrlimit(RLIMIT_CORE, &rl);
108
109 /* Just to be sure... */
110 assert_se(chdir("/") == 0);
111
112 /* Raise the signal again */
113 raise(sig);
114
115 assert_not_reached("We shouldn't be here...");
116 _exit(1);
4fc935ca
LP
117
118 } else {
e364ad06 119 int status;
4fc935ca
LP
120
121 /* Order things nicely. */
e364ad06 122 if (waitpid(pid, &status, 0) < 0)
582a507f 123 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(errno));
4fc935ca 124 else if (!WCOREDUMP(status))
582a507f 125 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
4fc935ca 126 else
582a507f 127 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
97c4f35c
LP
128 }
129 }
130
fa0f4d8a
LP
131 if (arg_crash_chvt)
132 chvt(arg_crash_chvt);
601f6a1e 133
fa0f4d8a 134 if (arg_crash_shell) {
6f5e3f35
LP
135 struct sigaction sa;
136 pid_t pid;
8c43883a 137
4fc935ca
LP
138 log_info("Executing crash shell in 10s...");
139 sleep(10);
140
6f5e3f35
LP
141 /* Let the kernel reap children for us */
142 zero(sa);
143 sa.sa_handler = SIG_IGN;
144 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
145 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
8c43883a 146
6f5e3f35
LP
147 if ((pid = fork()) < 0)
148 log_error("Failed to fork off crash shell: %s", strerror(errno));
149 else if (pid == 0) {
843d2643 150 int fd, r;
ea5652c2 151
21de3988 152 if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
ea5652c2 153 log_error("Failed to acquire terminal: %s", strerror(-fd));
5b2a0903 154 else if ((r = make_stdio(fd)) < 0)
843d2643 155 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
ea5652c2 156
6f5e3f35
LP
157 execl("/bin/sh", "/bin/sh", NULL);
158
159 log_error("execl() failed: %s", strerror(errno));
160 _exit(1);
161 }
c99b188e 162
bb00e604 163 log_info("Successfully spawned crash shall as pid %lu.", (unsigned long) pid);
4fc935ca
LP
164 }
165
166 log_info("Freezing execution.");
97c4f35c
LP
167 freeze();
168}
169
170static void install_crash_handler(void) {
171 struct sigaction sa;
172
173 zero(sa);
174
175 sa.sa_handler = crash;
176 sa.sa_flags = SA_NODEFER;
177
1b91d3e8 178 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
97c4f35c 179}
f170852a 180
843d2643
LP
181static int make_null_stdio(void) {
182 int null_fd, r;
80876c20 183
c8513d54 184 if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) {
80876c20 185 log_error("Failed to open /dev/null: %m");
843d2643 186 return -errno;
80876c20
LP
187 }
188
843d2643
LP
189 if ((r = make_stdio(null_fd)) < 0)
190 log_warning("Failed to dup2() device: %s", strerror(-r));
80876c20 191
843d2643
LP
192 return r;
193}
80876c20 194
843d2643
LP
195static int console_setup(bool do_reset) {
196 int tty_fd, r;
80876c20 197
843d2643
LP
198 /* If we are init, we connect stdin/stdout/stderr to /dev/null
199 * and make sure we don't have a controlling tty. */
80876c20 200
843d2643
LP
201 release_terminal();
202
203 if (!do_reset)
204 return 0;
80876c20 205
843d2643
LP
206 if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
207 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
208 return -tty_fd;
209 }
80876c20 210
843d2643
LP
211 if ((r = reset_terminal(tty_fd)) < 0)
212 log_error("Failed to reset /dev/console: %s", strerror(-r));
213
214 close_nointr_nofail(tty_fd);
80876c20
LP
215 return r;
216}
217
f170852a
LP
218static int set_default_unit(const char *u) {
219 char *c;
220
221 assert(u);
222
223 if (!(c = strdup(u)))
224 return -ENOMEM;
225
fa0f4d8a
LP
226 free(arg_default_unit);
227 arg_default_unit = c;
f170852a
LP
228 return 0;
229}
230
231static int parse_proc_cmdline_word(const char *word) {
232
233 static const char * const rlmap[] = {
514f4ef5
LP
234 "single", SPECIAL_RESCUE_TARGET,
235 "-s", SPECIAL_RESCUE_TARGET,
236 "s", SPECIAL_RESCUE_TARGET,
237 "S", SPECIAL_RESCUE_TARGET,
238 "1", SPECIAL_RESCUE_TARGET,
f170852a
LP
239 "2", SPECIAL_RUNLEVEL2_TARGET,
240 "3", SPECIAL_RUNLEVEL3_TARGET,
241 "4", SPECIAL_RUNLEVEL4_TARGET,
242 "5", SPECIAL_RUNLEVEL5_TARGET
243 };
244
5192bd19
LP
245 assert(word);
246
2f198e2f
LP
247 if (startswith(word, "systemd.unit="))
248 return set_default_unit(word + 13);
f170852a
LP
249
250 else if (startswith(word, "systemd.log_target=")) {
251
252 if (log_set_target_from_string(word + 19) < 0)
253 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
254
255 } else if (startswith(word, "systemd.log_level=")) {
256
257 if (log_set_max_level_from_string(word + 18) < 0)
258 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
259
bbe63281
LP
260 } else if (startswith(word, "systemd.log_color=")) {
261
262 if (log_show_color_from_string(word + 18) < 0)
263 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
264
265 } else if (startswith(word, "systemd.log_location=")) {
266
267 if (log_show_location_from_string(word + 21) < 0)
268 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
269
4fc935ca
LP
270 } else if (startswith(word, "systemd.dump_core=")) {
271 int r;
272
273 if ((r = parse_boolean(word + 18)) < 0)
601f6a1e 274 log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
4fc935ca 275 else
fa0f4d8a 276 arg_dump_core = r;
4fc935ca
LP
277
278 } else if (startswith(word, "systemd.crash_shell=")) {
279 int r;
280
281 if ((r = parse_boolean(word + 20)) < 0)
601f6a1e 282 log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
4fc935ca 283 else
fa0f4d8a 284 arg_crash_shell = r;
5e7ee61c
LP
285
286 } else if (startswith(word, "systemd.confirm_spawn=")) {
287 int r;
288
289 if ((r = parse_boolean(word + 22)) < 0)
290 log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
291 else
fa0f4d8a 292 arg_confirm_spawn = r;
5e7ee61c 293
601f6a1e
LP
294 } else if (startswith(word, "systemd.crash_chvt=")) {
295 int k;
296
297 if (safe_atoi(word + 19, &k) < 0)
298 log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
299 else
fa0f4d8a 300 arg_crash_chvt = k;
601f6a1e 301
9e58ff9c
LP
302 } else if (startswith(word, "systemd.show_status=")) {
303 int r;
304
305 if ((r = parse_boolean(word + 20)) < 0)
306 log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
6e98720f 307 else
9e58ff9c 308 arg_show_status = r;
6e98720f
LP
309
310 } else if (startswith(word, "systemd.sysv_console=")) {
311 int r;
312
313 if ((r = parse_boolean(word + 21)) < 0)
314 log_warning("Failed to parse SysV console switch %s, Ignoring.", word + 20);
315 else
316 arg_sysv_console = r;
9e58ff9c 317
4fc935ca
LP
318 } else if (startswith(word, "systemd.")) {
319
320 log_warning("Unknown kernel switch %s. Ignoring.", word);
321
bbe63281
LP
322 log_info("Supported kernel switches:\n"
323 "systemd.unit=UNIT Default unit to start\n"
bbe63281 324 "systemd.dump_core=0|1 Dump core on crash\n"
9e58ff9c 325 "systemd.crash_shell=0|1 Run shell on crash\n"
bbe63281 326 "systemd.crash_chvt=N Change to VT #N on crash\n"
9e58ff9c 327 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
6e98720f
LP
328 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
329 "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
330 "systemd.log_target=console|kmsg|syslog|syslog-org-kmsg|null\n"
331 " Log target\n"
332 "systemd.log_level=LEVEL Log level\n"
333 "systemd.log_color=0|1 Highlight important log messages\n"
334 "systemd.log_location=0|1 Include code location in log messages\n");
4fc935ca 335
1cba2c90 336 } if (streq(word, "quiet")) {
6e98720f
LP
337 arg_show_status = false;
338 arg_sysv_console = false;
9e58ff9c 339 } else {
f170852a
LP
340 unsigned i;
341
342 /* SysV compatibility */
f170852a
LP
343 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
344 if (streq(word, rlmap[i]))
345 return set_default_unit(rlmap[i+1]);
346 }
347
348 return 0;
349}
350
487393e9
LP
351static int config_parse_level(
352 const char *filename,
353 unsigned line,
354 const char *section,
355 const char *lvalue,
356 const char *rvalue,
357 void *data,
358 void *userdata) {
359
360 assert(filename);
361 assert(lvalue);
362 assert(rvalue);
363
364 log_set_max_level_from_string(rvalue);
365 return 0;
366}
367
368static int config_parse_target(
369 const char *filename,
370 unsigned line,
371 const char *section,
372 const char *lvalue,
373 const char *rvalue,
374 void *data,
375 void *userdata) {
376
377 assert(filename);
378 assert(lvalue);
379 assert(rvalue);
380
381 log_set_target_from_string(rvalue);
382 return 0;
383}
384
385static int config_parse_color(
386 const char *filename,
387 unsigned line,
388 const char *section,
389 const char *lvalue,
390 const char *rvalue,
391 void *data,
392 void *userdata) {
393
394 assert(filename);
395 assert(lvalue);
396 assert(rvalue);
397
398 log_show_color_from_string(rvalue);
399 return 0;
400}
401
402static int config_parse_location(
403 const char *filename,
404 unsigned line,
405 const char *section,
406 const char *lvalue,
407 const char *rvalue,
408 void *data,
409 void *userdata) {
410
411 assert(filename);
412 assert(lvalue);
413 assert(rvalue);
414
415 log_show_location_from_string(rvalue);
416 return 0;
417}
418
419static int config_parse_cpu_affinity(
420 const char *filename,
421 unsigned line,
422 const char *section,
423 const char *lvalue,
424 const char *rvalue,
425 void *data,
426 void *userdata) {
427
428 char *w;
429 size_t l;
430 char *state;
431 cpu_set_t *c = NULL;
432 unsigned ncpus = 0;
433
434 assert(filename);
435 assert(lvalue);
436 assert(rvalue);
437
f60f22df 438 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
487393e9
LP
439 char *t;
440 int r;
441 unsigned cpu;
442
443 if (!(t = strndup(w, l)))
444 return -ENOMEM;
445
446 r = safe_atou(t, &cpu);
447 free(t);
448
449 if (!c)
450 if (!(c = cpu_set_malloc(&ncpus)))
451 return -ENOMEM;
452
453 if (r < 0 || cpu >= ncpus) {
454 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
455 CPU_FREE(c);
456 return -EBADMSG;
457 }
458
459 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
460 }
461
462 if (c) {
463 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
464 log_warning("Failed to set CPU affinity: %m");
465
466 CPU_FREE(c);
467 }
468
469 return 0;
470}
471
472static int parse_config_file(void) {
473
474 const ConfigItem items[] = {
475 { "LogLevel", config_parse_level, NULL, "Manager" },
476 { "LogTarget", config_parse_target, NULL, "Manager" },
477 { "LogColor", config_parse_color, NULL, "Manager" },
478 { "LogLocation", config_parse_location, NULL, "Manager" },
479 { "DumpCore", config_parse_bool, &arg_dump_core, "Manager" },
480 { "CrashShell", config_parse_bool, &arg_crash_shell, "Manager" },
481 { "ShowStatus", config_parse_bool, &arg_show_status, "Manager" },
6e98720f 482 { "SysVConsole", config_parse_bool, &arg_sysv_console,"Manager" },
487393e9
LP
483 { "CrashChVT", config_parse_int, &arg_crash_chvt, "Manager" },
484 { "CPUAffinity", config_parse_cpu_affinity, NULL, "Manager" },
485 { NULL, NULL, NULL, NULL }
486 };
487
488 static const char * const sections[] = {
489 "Manager",
490 NULL
491 };
492
493 FILE *f;
494 const char *fn;
495 int r;
496
497 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : SESSION_CONFIG_FILE;
498
499 if (!(f = fopen(fn, "re"))) {
500 if (errno == ENOENT)
501 return 0;
502
503 log_warning("Failed to open configuration file '%s': %m", fn);
504 return 0;
505 }
506
507 if ((r = config_parse(fn, f, sections, items, false, NULL)) < 0)
508 log_warning("Failed to parse configuration file: %s", strerror(-r));
509
510 fclose(f);
511
512 return 0;
513}
514
f170852a
LP
515static int parse_proc_cmdline(void) {
516 char *line;
517 int r;
518 char *w;
519 size_t l;
520 char *state;
521
522 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
e364ad06 523 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
f170852a
LP
524 return 0;
525 }
526
527 FOREACH_WORD_QUOTED(w, l, line, state) {
528 char *word;
529
530 if (!(word = strndup(w, l))) {
531 r = -ENOMEM;
532 goto finish;
533 }
534
535 r = parse_proc_cmdline_word(word);
536 free(word);
537
538 if (r < 0)
539 goto finish;
540 }
541
542 r = 0;
543
544finish:
545 free(line);
546 return r;
547}
548
549static int parse_argv(int argc, char *argv[]) {
550
551 enum {
552 ARG_LOG_LEVEL = 0x100,
553 ARG_LOG_TARGET,
bbe63281
LP
554 ARG_LOG_COLOR,
555 ARG_LOG_LOCATION,
2f198e2f 556 ARG_UNIT,
edb9aaa8
LP
557 ARG_SYSTEM,
558 ARG_SESSION,
e537352b 559 ARG_TEST,
80876c20 560 ARG_DUMP_CONFIGURATION_ITEMS,
9e58ff9c
LP
561 ARG_DUMP_CORE,
562 ARG_CRASH_SHELL,
a16e1123 563 ARG_CONFIRM_SPAWN,
9e58ff9c 564 ARG_SHOW_STATUS,
6e98720f 565 ARG_SYSV_CONSOLE,
4288f619
LP
566 ARG_DESERIALIZE,
567 ARG_INTROSPECT
f170852a
LP
568 };
569
570 static const struct option options[] = {
a16e1123
LP
571 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
572 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bbe63281
LP
573 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
574 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2f198e2f 575 { "unit", required_argument, NULL, ARG_UNIT },
edb9aaa8
LP
576 { "system", no_argument, NULL, ARG_SYSTEM },
577 { "session", no_argument, NULL, ARG_SESSION },
a16e1123
LP
578 { "test", no_argument, NULL, ARG_TEST },
579 { "help", no_argument, NULL, 'h' },
580 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
9e58ff9c
LP
581 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
582 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
a16e1123 583 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
6e98720f
LP
584 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
585 { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
a16e1123 586 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
4288f619 587 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
a16e1123 588 { NULL, 0, NULL, 0 }
f170852a
LP
589 };
590
591 int c, r;
592
593 assert(argc >= 1);
594 assert(argv);
595
1d2e23ab 596 while ((c = getopt_long(argc, argv, "hD", options, NULL)) >= 0)
f170852a
LP
597
598 switch (c) {
599
600 case ARG_LOG_LEVEL:
601 if ((r = log_set_max_level_from_string(optarg)) < 0) {
602 log_error("Failed to parse log level %s.", optarg);
603 return r;
604 }
605
606 break;
607
608 case ARG_LOG_TARGET:
609
610 if ((r = log_set_target_from_string(optarg)) < 0) {
611 log_error("Failed to parse log target %s.", optarg);
612 return r;
613 }
614
615 break;
616
bbe63281
LP
617 case ARG_LOG_COLOR:
618
d0b170c8
LP
619 if (optarg) {
620 if ((r = log_show_color_from_string(optarg)) < 0) {
621 log_error("Failed to parse log color setting %s.", optarg);
622 return r;
623 }
624 } else
625 log_show_color(true);
bbe63281
LP
626
627 break;
628
629 case ARG_LOG_LOCATION:
630
d0b170c8
LP
631 if (optarg) {
632 if ((r = log_show_location_from_string(optarg)) < 0) {
633 log_error("Failed to parse log location setting %s.", optarg);
634 return r;
635 }
636 } else
637 log_show_location(true);
bbe63281
LP
638
639 break;
640
2f198e2f 641 case ARG_UNIT:
f170852a
LP
642
643 if ((r = set_default_unit(optarg)) < 0) {
644 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
645 return r;
646 }
647
648 break;
649
edb9aaa8
LP
650 case ARG_SYSTEM:
651 arg_running_as = MANAGER_SYSTEM;
652 break;
a5dab5ce 653
edb9aaa8
LP
654 case ARG_SESSION:
655 arg_running_as = MANAGER_SESSION;
a5dab5ce 656 break;
a5dab5ce 657
e965d56d 658 case ARG_TEST:
fa0f4d8a 659 arg_action = ACTION_TEST;
e965d56d
LP
660 break;
661
e537352b 662 case ARG_DUMP_CONFIGURATION_ITEMS:
fa0f4d8a 663 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
e537352b
LP
664 break;
665
9e58ff9c
LP
666 case ARG_DUMP_CORE:
667 arg_dump_core = true;
668 break;
669
670 case ARG_CRASH_SHELL:
671 arg_crash_shell = true;
672 break;
673
80876c20 674 case ARG_CONFIRM_SPAWN:
fa0f4d8a 675 arg_confirm_spawn = true;
80876c20
LP
676 break;
677
9e58ff9c 678 case ARG_SHOW_STATUS:
6e98720f
LP
679
680 if (optarg) {
681 if ((r = parse_boolean(optarg)) < 0) {
682 log_error("Failed to show status boolean %s.", optarg);
683 return r;
684 }
685 arg_show_status = r;
686 } else
687 arg_show_status = true;
688 break;
689
690 case ARG_SYSV_CONSOLE:
691
692 if (optarg) {
693 if ((r = parse_boolean(optarg)) < 0) {
694 log_error("Failed to SysV console boolean %s.", optarg);
695 return r;
696 }
697 arg_sysv_console = r;
698 } else
699 arg_sysv_console = true;
9e58ff9c
LP
700 break;
701
a16e1123
LP
702 case ARG_DESERIALIZE: {
703 int fd;
704 FILE *f;
705
706 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
707 log_error("Failed to parse deserialize option %s.", optarg);
708 return r;
709 }
710
711 if (!(f = fdopen(fd, "r"))) {
712 log_error("Failed to open serialization fd: %m");
713 return r;
714 }
715
716 if (serialization)
717 fclose(serialization);
718
719 serialization = f;
720
721 break;
722 }
723
4288f619
LP
724 case ARG_INTROSPECT: {
725 const char * const * i = NULL;
726
727 for (i = bus_interface_table; *i; i += 2)
728 if (!optarg || streq(i[0], optarg)) {
729 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
730 "<node>\n", stdout);
731 fputs(i[1], stdout);
732 fputs("</node>\n", stdout);
733
734 if (optarg)
735 break;
736 }
737
738 if (!i[0] && optarg)
739 log_error("Unknown interface %s.", optarg);
740
fa0f4d8a 741 arg_action = ACTION_DONE;
4288f619
LP
742 break;
743 }
744
f170852a 745 case 'h':
fa0f4d8a 746 arg_action = ACTION_HELP;
f170852a
LP
747 break;
748
1d2e23ab
LP
749 case 'D':
750 log_set_max_level(LOG_DEBUG);
751 break;
752
f170852a
LP
753 case '?':
754 return -EINVAL;
755
756 default:
757 log_error("Unknown option code %c", c);
758 return -EINVAL;
759 }
760
51f0e189
LP
761 /* PID 1 will get the kernel arguments as parameters, which we
762 * ignore and unconditionally read from
763 * /proc/cmdline. However, we need to ignore those arguments
764 * here. */
fa0f4d8a 765 if (arg_running_as != MANAGER_SYSTEM && optind < argc) {
51f0e189
LP
766 log_error("Excess arguments.");
767 return -EINVAL;
768 }
769
f170852a
LP
770 return 0;
771}
772
773static int help(void) {
774
2e33c433 775 printf("%s [OPTIONS...]\n\n"
bbe63281 776 "Starts up and maintains the system or a session.\n\n"
e537352b 777 " -h --help Show this help\n"
e537352b 778 " --test Determine startup sequence, dump it and exit\n"
80876c20 779 " --dump-configuration-items Dump understood unit configuration items\n"
bbe63281 780 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
9e58ff9c 781 " --unit=UNIT Set default unit\n"
edb9aaa8
LP
782 " --system Run a system instance, even if PID != 1\n"
783 " --session Run a session instance\n"
9e58ff9c
LP
784 " --dump-core Dump core on crash\n"
785 " --crash-shell Run shell on crash\n"
786 " --confirm-spawn Ask for confirmation when spawning processes\n"
6e98720f
LP
787 " --show-status[=0|1] Show status updates on the console during bootup\n"
788 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
bbe63281 789 " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
9e58ff9c 790 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
2218198b 791 " --log-color[=0|1] Highlight important log messages\n"
bbe63281 792 " --log-location[=0|1] Include code location in log messages\n",
5b6319dc 793 program_invocation_short_name);
f170852a
LP
794
795 return 0;
796}
797
a16e1123
LP
798static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
799 FILE *f = NULL;
800 FDSet *fds = NULL;
801 int r;
802
803 assert(m);
804 assert(_f);
805 assert(_fds);
806
d8d5ab98 807 if ((r = manager_open_serialization(m, &f)) < 0) {
a16e1123
LP
808 log_error("Failed to create serialization faile: %s", strerror(-r));
809 goto fail;
810 }
811
812 if (!(fds = fdset_new())) {
813 r = -ENOMEM;
814 log_error("Failed to allocate fd set: %s", strerror(-r));
815 goto fail;
816 }
817
818 if ((r = manager_serialize(m, f, fds)) < 0) {
819 log_error("Failed to serialize state: %s", strerror(-r));
820 goto fail;
821 }
822
823 if (fseeko(f, 0, SEEK_SET) < 0) {
824 log_error("Failed to rewind serialization fd: %m");
825 goto fail;
826 }
827
828 if ((r = fd_cloexec(fileno(f), false)) < 0) {
829 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
830 goto fail;
831 }
832
833 if ((r = fdset_cloexec(fds, false)) < 0) {
834 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
835 goto fail;
836 }
837
838 *_f = f;
839 *_fds = fds;
840
841 return 0;
842
843fail:
844 fdset_free(fds);
845
846 if (f)
847 fclose(f);
848
849 return r;
850}
851
60918275
LP
852int main(int argc, char *argv[]) {
853 Manager *m = NULL;
87f0e418 854 Unit *target = NULL;
60918275 855 int r, retval = 1;
a16e1123
LP
856 FDSet *fds = NULL;
857 bool reexecute = false;
27b14a22 858
2cb1a60d
LP
859 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
860 /* This is compatbility support for SysV, where
861 * calling init as a user is identical to telinit. */
862
863 errno = -ENOENT;
864 execv(SYSTEMCTL_BINARY_PATH, argv);
865 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
866 return 1;
867 }
868
2cc59dbf 869 log_show_color(isatty(STDERR_FILENO) > 0);
bbe63281 870 log_show_location(false);
7c706717 871 log_set_max_level(LOG_INFO);
bbe63281 872
843d2643 873 if (getpid() == 1) {
fa0f4d8a 874 arg_running_as = MANAGER_SYSTEM;
843d2643 875 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
0ff4cdd9
LP
876
877 if (label_init() < 0)
878 goto finish;
bbe63281 879 } else {
fa0f4d8a 880 arg_running_as = MANAGER_SESSION;
bbe63281
LP
881 log_set_target(LOG_TARGET_CONSOLE);
882 }
a5dab5ce 883
f170852a
LP
884 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
885 goto finish;
60918275 886
f170852a
LP
887 /* Mount /proc, /sys and friends, so that /proc/cmdline and
888 * /proc/$PID/fd is available. */
ca326f6f 889 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
8efe3c01
LP
890 if (mount_setup() < 0)
891 goto finish;
4ade7963
LP
892
893 /* Reset all signal handlers. */
894 assert_se(reset_all_signal_handlers() == 0);
895
078e4539 896 /* If we are init, we can block sigkill. Yay. */
9a34ec5f 897 ignore_signals(SIGNALS_IGNORE, -1);
078e4539 898
487393e9
LP
899 if (parse_config_file() < 0)
900 goto finish;
901
fa0f4d8a 902 if (arg_running_as == MANAGER_SYSTEM)
a5dab5ce
LP
903 if (parse_proc_cmdline() < 0)
904 goto finish;
f170852a
LP
905
906 log_parse_environment();
907
908 if (parse_argv(argc, argv) < 0)
909 goto finish;
910
fa0f4d8a 911 if (arg_action == ACTION_HELP) {
f170852a
LP
912 retval = help();
913 goto finish;
fa0f4d8a 914 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
e537352b
LP
915 unit_dump_config_items(stdout);
916 retval = 0;
917 goto finish;
fa0f4d8a 918 } else if (arg_action == ACTION_DONE) {
4288f619
LP
919 retval = 0;
920 goto finish;
f170852a
LP
921 }
922
fa0f4d8a 923 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
f170852a 924
a16e1123
LP
925 /* Remember open file descriptors for later deserialization */
926 if (serialization) {
927 if ((r = fdset_new_fill(&fds)) < 0) {
928 log_error("Failed to allocate fd set: %s", strerror(-r));
929 goto finish;
930 }
931
932 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
933 } else
934 close_all_fds(NULL, 0);
935
09082a94 936 /* Set up PATH unless it is already set */
e537352b
LP
937 setenv("PATH",
938 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
fa0f4d8a 939 arg_running_as == MANAGER_SYSTEM);
09082a94 940
f170852a
LP
941 /* Move out of the way, so that we won't block unmounts */
942 assert_se(chdir("/") == 0);
943
fa0f4d8a 944 if (arg_running_as == MANAGER_SYSTEM) {
80876c20
LP
945 /* Become a session leader if we aren't one yet. */
946 setsid();
4ade7963 947
80876c20
LP
948 /* Disable the umask logic */
949 umask(0);
950 }
951
843d2643
LP
952 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
953 dbus_connection_set_change_sigpipe(FALSE);
954
2146621b
LP
955 /* Reset the console, but only if this is really init and we
956 * are freshly booted */
fa0f4d8a 957 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
2146621b 958 console_setup(getpid() == 1 && !serialization);
843d2643
LP
959 make_null_stdio();
960 }
4ade7963 961
18149b9f 962 /* Open the logging devices, if possible and necessary */
843d2643 963 log_open();
4ade7963 964
5373d602
LP
965 /* Make sure we leave a core dump without panicing the
966 * kernel. */
4fc935ca
LP
967 if (getpid() == 1)
968 install_crash_handler();
97c4f35c 969
302e27c8
LP
970 log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
971 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES ")", manager_running_as_to_string(arg_running_as));
a5dab5ce 972
888c6216
LP
973 if (arg_running_as == MANAGER_SYSTEM && !serialization) {
974 if (arg_show_status)
975 status_welcome();
976
888c6216
LP
977 kmod_setup();
978 hostname_setup();
979 loopback_setup();
af5bc85d 980 }
302e8c4c 981
9e58ff9c 982 if ((r = manager_new(arg_running_as, &m)) < 0) {
8e274523 983 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
984 goto finish;
985 }
986
9e58ff9c
LP
987 m->confirm_spawn = arg_confirm_spawn;
988 m->show_status = arg_show_status;
6e98720f 989 m->sysv_console = arg_sysv_console;
9e58ff9c 990
a16e1123 991 if ((r = manager_startup(m, serialization, fds)) < 0)
6e2ef85b 992 log_error("Failed to fully start up daemon: %s", strerror(-r));
a16e1123
LP
993
994 if (fds) {
995 /* This will close all file descriptors that were opened, but
996 * not claimed by any unit. */
997
998 fdset_free(fds);
999 fds = NULL;
f50e0a01
LP
1000 }
1001
a16e1123
LP
1002 if (serialization) {
1003 fclose(serialization);
1004 serialization = NULL;
1005 } else {
398ef8ba
LP
1006 DBusError error;
1007
1008 dbus_error_init(&error);
1009
fa0f4d8a 1010 log_debug("Activating default unit: %s", arg_default_unit);
a16e1123 1011
398ef8ba
LP
1012 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1013 log_error("Failed to load default target: %s", bus_error(&error, r));
1014 dbus_error_free(&error);
27b14a22 1015
a16e1123 1016 log_info("Trying to load rescue target...");
398ef8ba
LP
1017 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1018 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1019 dbus_error_free(&error);
a16e1123
LP
1020 goto finish;
1021 }
1022 }
37d88da7 1023
fa0f4d8a 1024 if (arg_action == ACTION_TEST) {
40d50879 1025 printf("-> By units:\n");
a16e1123
LP
1026 manager_dump_units(m, stdout, "\t");
1027 }
1028
0ff4cdd9 1029 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
398ef8ba
LP
1030 log_error("Failed to start default target: %s", bus_error(&error, r));
1031 dbus_error_free(&error);
37d88da7
LP
1032 goto finish;
1033 }
60918275 1034
fa0f4d8a 1035 if (arg_action == ACTION_TEST) {
40d50879 1036 printf("-> By jobs:\n");
a16e1123
LP
1037 manager_dump_jobs(m, stdout, "\t");
1038 retval = 0;
1039 goto finish;
1040 }
e965d56d 1041 }
d46de8a1 1042
a16e1123
LP
1043 for (;;) {
1044 if ((r = manager_loop(m)) < 0) {
1045 log_error("Failed to run mainloop: %s", strerror(-r));
1046 goto finish;
1047 }
11dd41ce 1048
a16e1123 1049 switch (m->exit_code) {
e965d56d 1050
a16e1123
LP
1051 case MANAGER_EXIT:
1052 retval = 0;
1053 log_debug("Exit.");
1054 goto finish;
e965d56d 1055
a16e1123 1056 case MANAGER_RELOAD:
e015090f 1057 log_info("Reloading.");
a16e1123
LP
1058 if ((r = manager_reload(m)) < 0)
1059 log_error("Failed to reload: %s", strerror(-r));
1060 break;
cea8e32e 1061
a16e1123 1062 case MANAGER_REEXECUTE:
a16e1123
LP
1063 if (prepare_reexecute(m, &serialization, &fds) < 0)
1064 goto finish;
60918275 1065
a16e1123 1066 reexecute = true;
e015090f 1067 log_notice("Reexecuting.");
a16e1123
LP
1068 goto finish;
1069
1070 default:
1071 assert_not_reached("Unknown exit code.");
1072 }
1073 }
f170852a 1074
60918275
LP
1075finish:
1076 if (m)
1077 manager_free(m);
1078
fa0f4d8a 1079 free(arg_default_unit);
b9cd2ec1 1080
ea430986
LP
1081 dbus_shutdown();
1082
a16e1123 1083 if (reexecute) {
6e98720f 1084 const char *args[15];
a16e1123
LP
1085 unsigned i = 0;
1086 char sfd[16];
1087
1088 assert(serialization);
1089 assert(fds);
1090
1091 args[i++] = SYSTEMD_BINARY_PATH;
1092
1093 args[i++] = "--log-level";
1094 args[i++] = log_level_to_string(log_get_max_level());
1095
1096 args[i++] = "--log-target";
1097 args[i++] = log_target_to_string(log_get_target());
1098
edb9aaa8
LP
1099 if (arg_running_as == MANAGER_SYSTEM)
1100 args[i++] = "--system";
1101 else
1102 args[i++] = "--session";
1103
1104 if (arg_dump_core)
1105 args[i++] = "--dump-core";
1106
1107 if (arg_crash_shell)
1108 args[i++] = "--crash-shell";
1109
1110 if (arg_confirm_spawn)
1111 args[i++] = "--confirm-spawn";
1112
1113 if (arg_show_status)
6e98720f
LP
1114 args[i++] = "--show-status=1";
1115 else
1116 args[i++] = "--show-status=0";
1117
1118 if (arg_sysv_console)
1119 args[i++] = "--sysv-console=1";
1120 else
1121 args[i++] = "--sysv-console=0";
a16e1123
LP
1122
1123 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1124 char_array_0(sfd);
1125
1126 args[i++] = "--deserialize";
1127 args[i++] = sfd;
1128
a16e1123
LP
1129 args[i++] = NULL;
1130
1131 assert(i <= ELEMENTSOF(args));
1132
1133 execv(args[0], (char* const*) args);
1134
1135 log_error("Failed to reexecute: %m");
1136 }
1137
1138 if (serialization)
1139 fclose(serialization);
1140
1141 if (fds)
1142 fdset_free(fds);
1143
c3b3c274
LP
1144 if (getpid() == 1)
1145 freeze();
1146
56cf987f
DW
1147 label_finish();
1148
60918275
LP
1149 return retval;
1150}