]>
Commit | Line | Data |
---|---|---|
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" | |
a16e1123 | 40 | #include "fdset.h" |
60918275 | 41 | |
f170852a LP |
42 | static enum { |
43 | ACTION_RUN, | |
e965d56d | 44 | ACTION_HELP, |
e537352b LP |
45 | ACTION_TEST, |
46 | ACTION_DUMP_CONFIGURATION_ITEMS | |
f170852a LP |
47 | } action = ACTION_RUN; |
48 | ||
49 | static char *default_unit = NULL; | |
a5dab5ce | 50 | static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID; |
4fc935ca | 51 | |
97c4f35c | 52 | static bool dump_core = true; |
601f6a1e LP |
53 | static bool crash_shell = false; |
54 | static int crash_chvt = -1; | |
80876c20 | 55 | static bool confirm_spawn = false; |
a16e1123 | 56 | static FILE* serialization = NULL; |
80876c20 | 57 | |
97c4f35c LP |
58 | _noreturn static void freeze(void) { |
59 | for (;;) | |
60 | pause(); | |
61 | } | |
62 | ||
6f5e3f35 LP |
63 | static void nop_handler(int sig) { |
64 | } | |
65 | ||
97c4f35c LP |
66 | _noreturn static void crash(int sig) { |
67 | ||
68 | if (!dump_core) | |
69 | log_error("Caught <%s>, not dumping core.", strsignal(sig)); | |
70 | else { | |
6f5e3f35 | 71 | struct sigaction sa; |
97c4f35c LP |
72 | pid_t pid; |
73 | ||
6f5e3f35 LP |
74 | /* We want to wait for the core process, hence let's enable SIGCHLD */ |
75 | zero(sa); | |
76 | sa.sa_handler = nop_handler; | |
77 | sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; | |
78 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); | |
79 | ||
97c4f35c | 80 | if ((pid = fork()) < 0) |
4fc935ca | 81 | log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno)); |
97c4f35c LP |
82 | |
83 | else if (pid == 0) { | |
97c4f35c LP |
84 | struct rlimit rl; |
85 | ||
86 | /* Enable default signal handler for core dump */ | |
87 | zero(sa); | |
88 | sa.sa_handler = SIG_DFL; | |
89 | assert_se(sigaction(sig, &sa, NULL) == 0); | |
90 | ||
91 | /* Don't limit the core dump size */ | |
92 | zero(rl); | |
93 | rl.rlim_cur = RLIM_INFINITY; | |
94 | rl.rlim_max = RLIM_INFINITY; | |
95 | setrlimit(RLIMIT_CORE, &rl); | |
96 | ||
97 | /* Just to be sure... */ | |
98 | assert_se(chdir("/") == 0); | |
99 | ||
100 | /* Raise the signal again */ | |
101 | raise(sig); | |
102 | ||
103 | assert_not_reached("We shouldn't be here..."); | |
104 | _exit(1); | |
4fc935ca LP |
105 | |
106 | } else { | |
107 | int status, r; | |
108 | ||
109 | /* Order things nicely. */ | |
110 | if ((r = waitpid(pid, &status, 0)) < 0) | |
111 | log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno)); | |
112 | else if (!WCOREDUMP(status)) | |
113 | log_error("Caught <%s>, core dump failed.", strsignal(sig)); | |
114 | else | |
115 | log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid); | |
97c4f35c LP |
116 | } |
117 | } | |
118 | ||
601f6a1e LP |
119 | if (crash_chvt) |
120 | chvt(crash_chvt); | |
121 | ||
4fc935ca | 122 | if (crash_shell) { |
6f5e3f35 LP |
123 | struct sigaction sa; |
124 | pid_t pid; | |
8c43883a | 125 | |
4fc935ca LP |
126 | log_info("Executing crash shell in 10s..."); |
127 | sleep(10); | |
128 | ||
6f5e3f35 LP |
129 | /* Let the kernel reap children for us */ |
130 | zero(sa); | |
131 | sa.sa_handler = SIG_IGN; | |
132 | sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART; | |
133 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); | |
8c43883a | 134 | |
6f5e3f35 LP |
135 | if ((pid = fork()) < 0) |
136 | log_error("Failed to fork off crash shell: %s", strerror(errno)); | |
137 | else if (pid == 0) { | |
138 | execl("/bin/sh", "/bin/sh", NULL); | |
139 | ||
140 | log_error("execl() failed: %s", strerror(errno)); | |
141 | _exit(1); | |
142 | } | |
c99b188e | 143 | |
6f5e3f35 | 144 | log_info("Successfully spawned crash shall as pid %llu.", (unsigned long long) pid); |
4fc935ca LP |
145 | } |
146 | ||
147 | log_info("Freezing execution."); | |
97c4f35c LP |
148 | freeze(); |
149 | } | |
150 | ||
151 | static void install_crash_handler(void) { | |
152 | struct sigaction sa; | |
153 | ||
154 | zero(sa); | |
155 | ||
156 | sa.sa_handler = crash; | |
157 | sa.sa_flags = SA_NODEFER; | |
158 | ||
159 | assert_se(sigaction(SIGSEGV, &sa, NULL) == 0); | |
5373d602 LP |
160 | assert_se(sigaction(SIGILL, &sa, NULL) == 0); |
161 | assert_se(sigaction(SIGFPE, &sa, NULL) == 0); | |
162 | assert_se(sigaction(SIGBUS, &sa, NULL) == 0); | |
163 | assert_se(sigaction(SIGQUIT, &sa, NULL) == 0); | |
97c4f35c LP |
164 | assert_se(sigaction(SIGABRT, &sa, NULL) == 0); |
165 | } | |
f170852a | 166 | |
2146621b | 167 | static int console_setup(bool do_reset) { |
80876c20 LP |
168 | int tty_fd = -1, null_fd = -1, r = 0; |
169 | ||
170 | /* If we are init, we connect stdout/stderr to /dev/console | |
171 | * and stdin to /dev/null and make sure we don't have a | |
172 | * controlling tty. */ | |
173 | ||
174 | release_terminal(); | |
175 | ||
176 | if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) { | |
177 | log_error("Failed to open /dev/console: %s", strerror(-tty_fd)); | |
178 | r = -tty_fd; | |
179 | goto finish; | |
180 | } | |
181 | ||
182 | if ((null_fd = open("/dev/null", O_RDONLY)) < 0) { | |
183 | log_error("Failed to open /dev/null: %m"); | |
184 | r = -errno; | |
185 | goto finish; | |
186 | } | |
187 | ||
188 | assert(tty_fd >= 3); | |
189 | assert(null_fd >= 3); | |
190 | ||
2146621b LP |
191 | if (do_reset) |
192 | if (reset_terminal(tty_fd) < 0) | |
193 | log_error("Failed to reset /dev/console: %m"); | |
80876c20 LP |
194 | |
195 | if (dup2(tty_fd, STDOUT_FILENO) < 0 || | |
196 | dup2(tty_fd, STDERR_FILENO) < 0 || | |
197 | dup2(null_fd, STDIN_FILENO) < 0) { | |
198 | log_error("Failed to dup2() device: %m"); | |
199 | r = -errno; | |
200 | goto finish; | |
201 | } | |
202 | ||
203 | r = 0; | |
204 | ||
205 | finish: | |
206 | if (tty_fd >= 0) | |
a16e1123 | 207 | close_nointr_nofail(tty_fd); |
80876c20 LP |
208 | |
209 | if (null_fd >= 0) | |
a16e1123 | 210 | close_nointr_nofail(null_fd); |
80876c20 LP |
211 | |
212 | return r; | |
213 | } | |
214 | ||
f170852a LP |
215 | static int set_default_unit(const char *u) { |
216 | char *c; | |
217 | ||
218 | assert(u); | |
219 | ||
220 | if (!(c = strdup(u))) | |
221 | return -ENOMEM; | |
222 | ||
223 | free(default_unit); | |
224 | default_unit = c; | |
225 | return 0; | |
226 | } | |
227 | ||
228 | static int parse_proc_cmdline_word(const char *word) { | |
229 | ||
230 | static const char * const rlmap[] = { | |
231 | "single", SPECIAL_RUNLEVEL1_TARGET, | |
232 | "-s", SPECIAL_RUNLEVEL1_TARGET, | |
233 | "s", SPECIAL_RUNLEVEL1_TARGET, | |
234 | "S", SPECIAL_RUNLEVEL1_TARGET, | |
235 | "1", SPECIAL_RUNLEVEL1_TARGET, | |
236 | "2", SPECIAL_RUNLEVEL2_TARGET, | |
237 | "3", SPECIAL_RUNLEVEL3_TARGET, | |
238 | "4", SPECIAL_RUNLEVEL4_TARGET, | |
239 | "5", SPECIAL_RUNLEVEL5_TARGET | |
240 | }; | |
241 | ||
242 | if (startswith(word, "systemd.default=")) | |
82771ba1 | 243 | return set_default_unit(word + 16); |
f170852a LP |
244 | |
245 | else if (startswith(word, "systemd.log_target=")) { | |
246 | ||
247 | if (log_set_target_from_string(word + 19) < 0) | |
248 | log_warning("Failed to parse log target %s. Ignoring.", word + 19); | |
249 | ||
250 | } else if (startswith(word, "systemd.log_level=")) { | |
251 | ||
252 | if (log_set_max_level_from_string(word + 18) < 0) | |
253 | log_warning("Failed to parse log level %s. Ignoring.", word + 18); | |
254 | ||
4fc935ca LP |
255 | } else if (startswith(word, "systemd.dump_core=")) { |
256 | int r; | |
257 | ||
258 | if ((r = parse_boolean(word + 18)) < 0) | |
601f6a1e | 259 | log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18); |
4fc935ca LP |
260 | else |
261 | dump_core = r; | |
262 | ||
263 | } else if (startswith(word, "systemd.crash_shell=")) { | |
264 | int r; | |
265 | ||
266 | if ((r = parse_boolean(word + 20)) < 0) | |
601f6a1e | 267 | log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20); |
4fc935ca LP |
268 | else |
269 | crash_shell = r; | |
270 | ||
5e7ee61c LP |
271 | |
272 | } else if (startswith(word, "systemd.confirm_spawn=")) { | |
273 | int r; | |
274 | ||
275 | if ((r = parse_boolean(word + 22)) < 0) | |
276 | log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22); | |
277 | else | |
278 | confirm_spawn = r; | |
279 | ||
601f6a1e LP |
280 | } else if (startswith(word, "systemd.crash_chvt=")) { |
281 | int k; | |
282 | ||
283 | if (safe_atoi(word + 19, &k) < 0) | |
284 | log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19); | |
285 | else | |
286 | crash_chvt = k; | |
287 | ||
4fc935ca LP |
288 | } else if (startswith(word, "systemd.")) { |
289 | ||
290 | log_warning("Unknown kernel switch %s. Ignoring.", word); | |
291 | ||
292 | log_info("Supported kernel switches:"); | |
293 | log_info("systemd.default=UNIT Default unit to start"); | |
294 | log_info("systemd.log_target=console|kmsg|syslog Log target"); | |
295 | log_info("systemd.log_level=LEVEL Log level"); | |
296 | log_info("systemd.dump_core=0|1 Dump core on crash"); | |
297 | log_info("systemd.crash_shell=0|1 On crash run shell"); | |
601f6a1e | 298 | log_info("systemd.crash_chvt=N Change to VT #N on crash"); |
5e7ee61c | 299 | log_info("systemd.confirm_spawn=0|1 Confirm every process spawn"); |
4fc935ca | 300 | |
f170852a LP |
301 | } else { |
302 | unsigned i; | |
303 | ||
304 | /* SysV compatibility */ | |
f170852a LP |
305 | for (i = 0; i < ELEMENTSOF(rlmap); i += 2) |
306 | if (streq(word, rlmap[i])) | |
307 | return set_default_unit(rlmap[i+1]); | |
308 | } | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | static int parse_proc_cmdline(void) { | |
314 | char *line; | |
315 | int r; | |
316 | char *w; | |
317 | size_t l; | |
318 | char *state; | |
319 | ||
320 | if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) { | |
321 | log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno)); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | FOREACH_WORD_QUOTED(w, l, line, state) { | |
326 | char *word; | |
327 | ||
328 | if (!(word = strndup(w, l))) { | |
329 | r = -ENOMEM; | |
330 | goto finish; | |
331 | } | |
332 | ||
333 | r = parse_proc_cmdline_word(word); | |
334 | free(word); | |
335 | ||
336 | if (r < 0) | |
337 | goto finish; | |
338 | } | |
339 | ||
340 | r = 0; | |
341 | ||
342 | finish: | |
343 | free(line); | |
344 | return r; | |
345 | } | |
346 | ||
347 | static int parse_argv(int argc, char *argv[]) { | |
348 | ||
349 | enum { | |
350 | ARG_LOG_LEVEL = 0x100, | |
351 | ARG_LOG_TARGET, | |
a5dab5ce | 352 | ARG_DEFAULT, |
e965d56d | 353 | ARG_RUNNING_AS, |
e537352b | 354 | ARG_TEST, |
80876c20 | 355 | ARG_DUMP_CONFIGURATION_ITEMS, |
a16e1123 LP |
356 | ARG_CONFIRM_SPAWN, |
357 | ARG_DESERIALIZE | |
f170852a LP |
358 | }; |
359 | ||
360 | static const struct option options[] = { | |
a16e1123 LP |
361 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, |
362 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
363 | { "default", required_argument, NULL, ARG_DEFAULT }, | |
364 | { "running-as", required_argument, NULL, ARG_RUNNING_AS }, | |
365 | { "test", no_argument, NULL, ARG_TEST }, | |
366 | { "help", no_argument, NULL, 'h' }, | |
367 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, | |
368 | { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN }, | |
369 | { "deserialize", required_argument, NULL, ARG_DESERIALIZE }, | |
370 | { NULL, 0, NULL, 0 } | |
f170852a LP |
371 | }; |
372 | ||
373 | int c, r; | |
374 | ||
375 | assert(argc >= 1); | |
376 | assert(argv); | |
377 | ||
378 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
379 | ||
380 | switch (c) { | |
381 | ||
382 | case ARG_LOG_LEVEL: | |
383 | if ((r = log_set_max_level_from_string(optarg)) < 0) { | |
384 | log_error("Failed to parse log level %s.", optarg); | |
385 | return r; | |
386 | } | |
387 | ||
388 | break; | |
389 | ||
390 | case ARG_LOG_TARGET: | |
391 | ||
392 | if ((r = log_set_target_from_string(optarg)) < 0) { | |
393 | log_error("Failed to parse log target %s.", optarg); | |
394 | return r; | |
395 | } | |
396 | ||
397 | break; | |
398 | ||
399 | case ARG_DEFAULT: | |
400 | ||
401 | if ((r = set_default_unit(optarg)) < 0) { | |
402 | log_error("Failed to set default unit %s: %s", optarg, strerror(-r)); | |
403 | return r; | |
404 | } | |
405 | ||
406 | break; | |
407 | ||
a5dab5ce LP |
408 | case ARG_RUNNING_AS: { |
409 | ManagerRunningAs as; | |
410 | ||
411 | if ((as = manager_running_as_from_string(optarg)) < 0) { | |
412 | log_error("Failed to parse running as value %s", optarg); | |
413 | return -EINVAL; | |
414 | } | |
415 | ||
416 | running_as = as; | |
417 | break; | |
418 | } | |
419 | ||
e965d56d LP |
420 | case ARG_TEST: |
421 | action = ACTION_TEST; | |
422 | break; | |
423 | ||
e537352b LP |
424 | case ARG_DUMP_CONFIGURATION_ITEMS: |
425 | action = ACTION_DUMP_CONFIGURATION_ITEMS; | |
426 | break; | |
427 | ||
80876c20 LP |
428 | case ARG_CONFIRM_SPAWN: |
429 | confirm_spawn = true; | |
430 | break; | |
431 | ||
a16e1123 LP |
432 | case ARG_DESERIALIZE: { |
433 | int fd; | |
434 | FILE *f; | |
435 | ||
436 | if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) { | |
437 | log_error("Failed to parse deserialize option %s.", optarg); | |
438 | return r; | |
439 | } | |
440 | ||
441 | if (!(f = fdopen(fd, "r"))) { | |
442 | log_error("Failed to open serialization fd: %m"); | |
443 | return r; | |
444 | } | |
445 | ||
446 | if (serialization) | |
447 | fclose(serialization); | |
448 | ||
449 | serialization = f; | |
450 | ||
451 | break; | |
452 | } | |
453 | ||
f170852a LP |
454 | case 'h': |
455 | action = ACTION_HELP; | |
456 | break; | |
457 | ||
458 | case '?': | |
459 | return -EINVAL; | |
460 | ||
461 | default: | |
462 | log_error("Unknown option code %c", c); | |
463 | return -EINVAL; | |
464 | } | |
465 | ||
f170852a LP |
466 | return 0; |
467 | } | |
468 | ||
469 | static int help(void) { | |
470 | ||
471 | printf("%s [options]\n\n" | |
e537352b LP |
472 | " -h --help Show this help\n" |
473 | " --default=UNIT Set default unit\n" | |
474 | " --log-level=LEVEL Set log level\n" | |
475 | " --log-target=TARGET Set log target (console, syslog, kmsg)\n" | |
476 | " --running-as=AS Set running as (init, system, session)\n" | |
477 | " --test Determine startup sequence, dump it and exit\n" | |
80876c20 LP |
478 | " --dump-configuration-items Dump understood unit configuration items\n" |
479 | " --confirm-spawn Ask for confirmation when spawning processes\n", | |
f170852a LP |
480 | __progname); |
481 | ||
482 | return 0; | |
483 | } | |
484 | ||
a16e1123 LP |
485 | static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) { |
486 | FILE *f = NULL; | |
487 | FDSet *fds = NULL; | |
488 | int r; | |
489 | ||
490 | assert(m); | |
491 | assert(_f); | |
492 | assert(_fds); | |
493 | ||
494 | if ((r = manager_open_serialization(&f)) < 0) { | |
495 | log_error("Failed to create serialization faile: %s", strerror(-r)); | |
496 | goto fail; | |
497 | } | |
498 | ||
499 | if (!(fds = fdset_new())) { | |
500 | r = -ENOMEM; | |
501 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
502 | goto fail; | |
503 | } | |
504 | ||
505 | if ((r = manager_serialize(m, f, fds)) < 0) { | |
506 | log_error("Failed to serialize state: %s", strerror(-r)); | |
507 | goto fail; | |
508 | } | |
509 | ||
510 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
511 | log_error("Failed to rewind serialization fd: %m"); | |
512 | goto fail; | |
513 | } | |
514 | ||
515 | if ((r = fd_cloexec(fileno(f), false)) < 0) { | |
516 | log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r)); | |
517 | goto fail; | |
518 | } | |
519 | ||
520 | if ((r = fdset_cloexec(fds, false)) < 0) { | |
521 | log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r)); | |
522 | goto fail; | |
523 | } | |
524 | ||
525 | *_f = f; | |
526 | *_fds = fds; | |
527 | ||
528 | return 0; | |
529 | ||
530 | fail: | |
531 | fdset_free(fds); | |
532 | ||
533 | if (f) | |
534 | fclose(f); | |
535 | ||
536 | return r; | |
537 | } | |
538 | ||
60918275 LP |
539 | int main(int argc, char *argv[]) { |
540 | Manager *m = NULL; | |
87f0e418 | 541 | Unit *target = NULL; |
60918275 LP |
542 | Job *job = NULL; |
543 | int r, retval = 1; | |
a16e1123 LP |
544 | FDSet *fds = NULL; |
545 | bool reexecute = false; | |
27b14a22 | 546 | |
a5dab5ce LP |
547 | if (getpid() == 1) |
548 | running_as = MANAGER_INIT; | |
549 | else if (getuid() == 0) | |
550 | running_as = MANAGER_SYSTEM; | |
551 | else | |
552 | running_as = MANAGER_SESSION; | |
553 | ||
f170852a LP |
554 | if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) |
555 | goto finish; | |
60918275 | 556 | |
f170852a LP |
557 | /* Mount /proc, /sys and friends, so that /proc/cmdline and |
558 | * /proc/$PID/fd is available. */ | |
d89e521e LP |
559 | if (mount_setup() < 0) |
560 | goto finish; | |
4ade7963 LP |
561 | |
562 | /* Reset all signal handlers. */ | |
563 | assert_se(reset_all_signal_handlers() == 0); | |
564 | ||
078e4539 | 565 | /* If we are init, we can block sigkill. Yay. */ |
a337c6fc LP |
566 | ignore_signal(SIGKILL); |
567 | ignore_signal(SIGPIPE); | |
078e4539 | 568 | |
a5dab5ce LP |
569 | if (running_as != MANAGER_SESSION) |
570 | if (parse_proc_cmdline() < 0) | |
571 | goto finish; | |
f170852a LP |
572 | |
573 | log_parse_environment(); | |
574 | ||
575 | if (parse_argv(argc, argv) < 0) | |
576 | goto finish; | |
577 | ||
578 | if (action == ACTION_HELP) { | |
579 | retval = help(); | |
580 | goto finish; | |
e537352b LP |
581 | } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
582 | unit_dump_config_items(stdout); | |
583 | retval = 0; | |
584 | goto finish; | |
f170852a LP |
585 | } |
586 | ||
e965d56d | 587 | assert_se(action == ACTION_RUN || action == ACTION_TEST); |
f170852a | 588 | |
a16e1123 LP |
589 | /* Remember open file descriptors for later deserialization */ |
590 | if (serialization) { | |
591 | if ((r = fdset_new_fill(&fds)) < 0) { | |
592 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
593 | goto finish; | |
594 | } | |
595 | ||
596 | assert_se(fdset_remove(fds, fileno(serialization)) >= 0); | |
597 | } else | |
598 | close_all_fds(NULL, 0); | |
599 | ||
09082a94 | 600 | /* Set up PATH unless it is already set */ |
e537352b LP |
601 | setenv("PATH", |
602 | "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | |
603 | running_as == MANAGER_INIT); | |
09082a94 | 604 | |
f170852a LP |
605 | /* Move out of the way, so that we won't block unmounts */ |
606 | assert_se(chdir("/") == 0); | |
607 | ||
80876c20 LP |
608 | if (running_as != MANAGER_SESSION) { |
609 | /* Become a session leader if we aren't one yet. */ | |
610 | setsid(); | |
4ade7963 | 611 | |
80876c20 LP |
612 | /* Disable the umask logic */ |
613 | umask(0); | |
614 | } | |
615 | ||
2146621b LP |
616 | /* Reset the console, but only if this is really init and we |
617 | * are freshly booted */ | |
80876c20 | 618 | if (running_as == MANAGER_INIT) |
2146621b | 619 | console_setup(getpid() == 1 && !serialization); |
4ade7963 LP |
620 | |
621 | /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */ | |
622 | dbus_connection_set_change_sigpipe(FALSE); | |
623 | ||
18149b9f | 624 | /* Open the logging devices, if possible and necessary */ |
4ade7963 LP |
625 | log_open_syslog(); |
626 | log_open_kmsg(); | |
627 | ||
5373d602 LP |
628 | /* Make sure we leave a core dump without panicing the |
629 | * kernel. */ | |
4fc935ca LP |
630 | if (getpid() == 1) |
631 | install_crash_handler(); | |
97c4f35c | 632 | |
a5dab5ce LP |
633 | log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); |
634 | ||
302e8c4c | 635 | if (running_as == MANAGER_INIT) |
e537352b | 636 | hostname_setup(); |
302e8c4c | 637 | |
80876c20 | 638 | if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) { |
8e274523 | 639 | log_error("Failed to allocate manager object: %s", strerror(-r)); |
60918275 LP |
640 | goto finish; |
641 | } | |
642 | ||
a16e1123 LP |
643 | if ((r = manager_startup(m, serialization, fds)) < 0) |
644 | log_error("Failed to fully startup daemon: %s", strerror(-r)); | |
645 | ||
646 | if (fds) { | |
647 | /* This will close all file descriptors that were opened, but | |
648 | * not claimed by any unit. */ | |
649 | ||
650 | fdset_free(fds); | |
651 | fds = NULL; | |
f50e0a01 LP |
652 | } |
653 | ||
a16e1123 LP |
654 | if (serialization) { |
655 | fclose(serialization); | |
656 | serialization = NULL; | |
657 | } else { | |
658 | log_debug("Activating default unit: %s", default_unit); | |
659 | ||
660 | if ((r = manager_load_unit(m, default_unit, NULL, &target)) < 0) { | |
661 | log_error("Failed to load default target: %s", strerror(-r)); | |
27b14a22 | 662 | |
a16e1123 LP |
663 | log_info("Trying to load rescue target..."); |
664 | if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &target)) < 0) { | |
665 | log_error("Failed to load rescue target: %s", strerror(-r)); | |
666 | goto finish; | |
667 | } | |
668 | } | |
37d88da7 | 669 | |
a16e1123 | 670 | if (action == ACTION_TEST) { |
40d50879 | 671 | printf("-> By units:\n"); |
a16e1123 LP |
672 | manager_dump_units(m, stdout, "\t"); |
673 | } | |
674 | ||
675 | if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) { | |
676 | log_error("Failed to start default target: %s", strerror(-r)); | |
37d88da7 LP |
677 | goto finish; |
678 | } | |
60918275 | 679 | |
a16e1123 | 680 | if (action == ACTION_TEST) { |
40d50879 | 681 | printf("-> By jobs:\n"); |
a16e1123 LP |
682 | manager_dump_jobs(m, stdout, "\t"); |
683 | retval = 0; | |
684 | goto finish; | |
685 | } | |
e965d56d | 686 | } |
d46de8a1 | 687 | |
a16e1123 LP |
688 | for (;;) { |
689 | if ((r = manager_loop(m)) < 0) { | |
690 | log_error("Failed to run mainloop: %s", strerror(-r)); | |
691 | goto finish; | |
692 | } | |
11dd41ce | 693 | |
a16e1123 | 694 | switch (m->exit_code) { |
e965d56d | 695 | |
a16e1123 LP |
696 | case MANAGER_EXIT: |
697 | retval = 0; | |
698 | log_debug("Exit."); | |
699 | goto finish; | |
e965d56d | 700 | |
a16e1123 LP |
701 | case MANAGER_RELOAD: |
702 | if ((r = manager_reload(m)) < 0) | |
703 | log_error("Failed to reload: %s", strerror(-r)); | |
704 | break; | |
cea8e32e | 705 | |
a16e1123 | 706 | case MANAGER_REEXECUTE: |
a16e1123 LP |
707 | if (prepare_reexecute(m, &serialization, &fds) < 0) |
708 | goto finish; | |
60918275 | 709 | |
a16e1123 LP |
710 | reexecute = true; |
711 | log_debug("Reexecuting."); | |
712 | goto finish; | |
713 | ||
714 | default: | |
715 | assert_not_reached("Unknown exit code."); | |
716 | } | |
717 | } | |
f170852a | 718 | |
60918275 LP |
719 | finish: |
720 | if (m) | |
721 | manager_free(m); | |
722 | ||
f170852a | 723 | free(default_unit); |
b9cd2ec1 | 724 | |
ea430986 LP |
725 | dbus_shutdown(); |
726 | ||
a16e1123 LP |
727 | if (reexecute) { |
728 | const char *args[11]; | |
729 | unsigned i = 0; | |
730 | char sfd[16]; | |
731 | ||
732 | assert(serialization); | |
733 | assert(fds); | |
734 | ||
735 | args[i++] = SYSTEMD_BINARY_PATH; | |
736 | ||
737 | args[i++] = "--log-level"; | |
738 | args[i++] = log_level_to_string(log_get_max_level()); | |
739 | ||
740 | args[i++] = "--log-target"; | |
741 | args[i++] = log_target_to_string(log_get_target()); | |
742 | ||
743 | args[i++] = "--running-as"; | |
744 | args[i++] = manager_running_as_to_string(running_as); | |
745 | ||
746 | snprintf(sfd, sizeof(sfd), "%i", fileno(serialization)); | |
747 | char_array_0(sfd); | |
748 | ||
749 | args[i++] = "--deserialize"; | |
750 | args[i++] = sfd; | |
751 | ||
752 | if (confirm_spawn) | |
753 | args[i++] = "--confirm-spawn"; | |
754 | ||
755 | args[i++] = NULL; | |
756 | ||
757 | assert(i <= ELEMENTSOF(args)); | |
758 | ||
759 | execv(args[0], (char* const*) args); | |
760 | ||
761 | log_error("Failed to reexecute: %m"); | |
762 | } | |
763 | ||
764 | if (serialization) | |
765 | fclose(serialization); | |
766 | ||
767 | if (fds) | |
768 | fdset_free(fds); | |
769 | ||
c3b3c274 LP |
770 | if (getpid() == 1) |
771 | freeze(); | |
772 | ||
60918275 LP |
773 | return retval; |
774 | } |