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