]>
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 | |
80876c20 LP |
167 | static int console_setup(void) { |
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 | ||
191 | if (reset_terminal(tty_fd) < 0) | |
192 | log_error("Failed to reset /dev/console: %m"); | |
193 | ||
194 | if (dup2(tty_fd, STDOUT_FILENO) < 0 || | |
195 | dup2(tty_fd, STDERR_FILENO) < 0 || | |
196 | dup2(null_fd, STDIN_FILENO) < 0) { | |
197 | log_error("Failed to dup2() device: %m"); | |
198 | r = -errno; | |
199 | goto finish; | |
200 | } | |
201 | ||
202 | r = 0; | |
203 | ||
204 | finish: | |
205 | if (tty_fd >= 0) | |
a16e1123 | 206 | close_nointr_nofail(tty_fd); |
80876c20 LP |
207 | |
208 | if (null_fd >= 0) | |
a16e1123 | 209 | close_nointr_nofail(null_fd); |
80876c20 LP |
210 | |
211 | return r; | |
212 | } | |
213 | ||
f170852a LP |
214 | static int set_default_unit(const char *u) { |
215 | char *c; | |
216 | ||
217 | assert(u); | |
218 | ||
219 | if (!(c = strdup(u))) | |
220 | return -ENOMEM; | |
221 | ||
222 | free(default_unit); | |
223 | default_unit = c; | |
224 | return 0; | |
225 | } | |
226 | ||
227 | static int parse_proc_cmdline_word(const char *word) { | |
228 | ||
229 | static const char * const rlmap[] = { | |
230 | "single", SPECIAL_RUNLEVEL1_TARGET, | |
231 | "-s", SPECIAL_RUNLEVEL1_TARGET, | |
232 | "s", SPECIAL_RUNLEVEL1_TARGET, | |
233 | "S", SPECIAL_RUNLEVEL1_TARGET, | |
234 | "1", SPECIAL_RUNLEVEL1_TARGET, | |
235 | "2", SPECIAL_RUNLEVEL2_TARGET, | |
236 | "3", SPECIAL_RUNLEVEL3_TARGET, | |
237 | "4", SPECIAL_RUNLEVEL4_TARGET, | |
238 | "5", SPECIAL_RUNLEVEL5_TARGET | |
239 | }; | |
240 | ||
241 | if (startswith(word, "systemd.default=")) | |
82771ba1 | 242 | return set_default_unit(word + 16); |
f170852a LP |
243 | |
244 | else if (startswith(word, "systemd.log_target=")) { | |
245 | ||
246 | if (log_set_target_from_string(word + 19) < 0) | |
247 | log_warning("Failed to parse log target %s. Ignoring.", word + 19); | |
248 | ||
249 | } else if (startswith(word, "systemd.log_level=")) { | |
250 | ||
251 | if (log_set_max_level_from_string(word + 18) < 0) | |
252 | log_warning("Failed to parse log level %s. Ignoring.", word + 18); | |
253 | ||
4fc935ca LP |
254 | } else if (startswith(word, "systemd.dump_core=")) { |
255 | int r; | |
256 | ||
257 | if ((r = parse_boolean(word + 18)) < 0) | |
601f6a1e | 258 | log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18); |
4fc935ca LP |
259 | else |
260 | dump_core = r; | |
261 | ||
262 | } else if (startswith(word, "systemd.crash_shell=")) { | |
263 | int r; | |
264 | ||
265 | if ((r = parse_boolean(word + 20)) < 0) | |
601f6a1e | 266 | log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20); |
4fc935ca LP |
267 | else |
268 | crash_shell = r; | |
269 | ||
5e7ee61c LP |
270 | |
271 | } else if (startswith(word, "systemd.confirm_spawn=")) { | |
272 | int r; | |
273 | ||
274 | if ((r = parse_boolean(word + 22)) < 0) | |
275 | log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22); | |
276 | else | |
277 | confirm_spawn = r; | |
278 | ||
601f6a1e LP |
279 | } else if (startswith(word, "systemd.crash_chvt=")) { |
280 | int k; | |
281 | ||
282 | if (safe_atoi(word + 19, &k) < 0) | |
283 | log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19); | |
284 | else | |
285 | crash_chvt = k; | |
286 | ||
4fc935ca LP |
287 | } else if (startswith(word, "systemd.")) { |
288 | ||
289 | log_warning("Unknown kernel switch %s. Ignoring.", word); | |
290 | ||
291 | log_info("Supported kernel switches:"); | |
292 | log_info("systemd.default=UNIT Default unit to start"); | |
293 | log_info("systemd.log_target=console|kmsg|syslog Log target"); | |
294 | log_info("systemd.log_level=LEVEL Log level"); | |
295 | log_info("systemd.dump_core=0|1 Dump core on crash"); | |
296 | log_info("systemd.crash_shell=0|1 On crash run shell"); | |
601f6a1e | 297 | log_info("systemd.crash_chvt=N Change to VT #N on crash"); |
5e7ee61c | 298 | log_info("systemd.confirm_spawn=0|1 Confirm every process spawn"); |
4fc935ca | 299 | |
f170852a LP |
300 | } else { |
301 | unsigned i; | |
302 | ||
303 | /* SysV compatibility */ | |
f170852a LP |
304 | for (i = 0; i < ELEMENTSOF(rlmap); i += 2) |
305 | if (streq(word, rlmap[i])) | |
306 | return set_default_unit(rlmap[i+1]); | |
307 | } | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | static int parse_proc_cmdline(void) { | |
313 | char *line; | |
314 | int r; | |
315 | char *w; | |
316 | size_t l; | |
317 | char *state; | |
318 | ||
319 | if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) { | |
320 | log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno)); | |
321 | return 0; | |
322 | } | |
323 | ||
324 | FOREACH_WORD_QUOTED(w, l, line, state) { | |
325 | char *word; | |
326 | ||
327 | if (!(word = strndup(w, l))) { | |
328 | r = -ENOMEM; | |
329 | goto finish; | |
330 | } | |
331 | ||
332 | r = parse_proc_cmdline_word(word); | |
333 | free(word); | |
334 | ||
335 | if (r < 0) | |
336 | goto finish; | |
337 | } | |
338 | ||
339 | r = 0; | |
340 | ||
341 | finish: | |
342 | free(line); | |
343 | return r; | |
344 | } | |
345 | ||
346 | static int parse_argv(int argc, char *argv[]) { | |
347 | ||
348 | enum { | |
349 | ARG_LOG_LEVEL = 0x100, | |
350 | ARG_LOG_TARGET, | |
a5dab5ce | 351 | ARG_DEFAULT, |
e965d56d | 352 | ARG_RUNNING_AS, |
e537352b | 353 | ARG_TEST, |
80876c20 | 354 | ARG_DUMP_CONFIGURATION_ITEMS, |
a16e1123 LP |
355 | ARG_CONFIRM_SPAWN, |
356 | ARG_DESERIALIZE | |
f170852a LP |
357 | }; |
358 | ||
359 | static const struct option options[] = { | |
a16e1123 LP |
360 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, |
361 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
362 | { "default", required_argument, NULL, ARG_DEFAULT }, | |
363 | { "running-as", required_argument, NULL, ARG_RUNNING_AS }, | |
364 | { "test", no_argument, NULL, ARG_TEST }, | |
365 | { "help", no_argument, NULL, 'h' }, | |
366 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, | |
367 | { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN }, | |
368 | { "deserialize", required_argument, NULL, ARG_DESERIALIZE }, | |
369 | { NULL, 0, NULL, 0 } | |
f170852a LP |
370 | }; |
371 | ||
372 | int c, r; | |
373 | ||
374 | assert(argc >= 1); | |
375 | assert(argv); | |
376 | ||
377 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
378 | ||
379 | switch (c) { | |
380 | ||
381 | case ARG_LOG_LEVEL: | |
382 | if ((r = log_set_max_level_from_string(optarg)) < 0) { | |
383 | log_error("Failed to parse log level %s.", optarg); | |
384 | return r; | |
385 | } | |
386 | ||
387 | break; | |
388 | ||
389 | case ARG_LOG_TARGET: | |
390 | ||
391 | if ((r = log_set_target_from_string(optarg)) < 0) { | |
392 | log_error("Failed to parse log target %s.", optarg); | |
393 | return r; | |
394 | } | |
395 | ||
396 | break; | |
397 | ||
398 | case ARG_DEFAULT: | |
399 | ||
400 | if ((r = set_default_unit(optarg)) < 0) { | |
401 | log_error("Failed to set default unit %s: %s", optarg, strerror(-r)); | |
402 | return r; | |
403 | } | |
404 | ||
405 | break; | |
406 | ||
a5dab5ce LP |
407 | case ARG_RUNNING_AS: { |
408 | ManagerRunningAs as; | |
409 | ||
410 | if ((as = manager_running_as_from_string(optarg)) < 0) { | |
411 | log_error("Failed to parse running as value %s", optarg); | |
412 | return -EINVAL; | |
413 | } | |
414 | ||
415 | running_as = as; | |
416 | break; | |
417 | } | |
418 | ||
e965d56d LP |
419 | case ARG_TEST: |
420 | action = ACTION_TEST; | |
421 | break; | |
422 | ||
e537352b LP |
423 | case ARG_DUMP_CONFIGURATION_ITEMS: |
424 | action = ACTION_DUMP_CONFIGURATION_ITEMS; | |
425 | break; | |
426 | ||
80876c20 LP |
427 | case ARG_CONFIRM_SPAWN: |
428 | confirm_spawn = true; | |
429 | break; | |
430 | ||
a16e1123 LP |
431 | case ARG_DESERIALIZE: { |
432 | int fd; | |
433 | FILE *f; | |
434 | ||
435 | if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) { | |
436 | log_error("Failed to parse deserialize option %s.", optarg); | |
437 | return r; | |
438 | } | |
439 | ||
440 | if (!(f = fdopen(fd, "r"))) { | |
441 | log_error("Failed to open serialization fd: %m"); | |
442 | return r; | |
443 | } | |
444 | ||
445 | if (serialization) | |
446 | fclose(serialization); | |
447 | ||
448 | serialization = f; | |
449 | ||
450 | break; | |
451 | } | |
452 | ||
f170852a LP |
453 | case 'h': |
454 | action = ACTION_HELP; | |
455 | break; | |
456 | ||
457 | case '?': | |
458 | return -EINVAL; | |
459 | ||
460 | default: | |
461 | log_error("Unknown option code %c", c); | |
462 | return -EINVAL; | |
463 | } | |
464 | ||
f170852a LP |
465 | return 0; |
466 | } | |
467 | ||
468 | static int help(void) { | |
469 | ||
470 | printf("%s [options]\n\n" | |
e537352b LP |
471 | " -h --help Show this help\n" |
472 | " --default=UNIT Set default unit\n" | |
473 | " --log-level=LEVEL Set log level\n" | |
474 | " --log-target=TARGET Set log target (console, syslog, kmsg)\n" | |
475 | " --running-as=AS Set running as (init, system, session)\n" | |
476 | " --test Determine startup sequence, dump it and exit\n" | |
80876c20 LP |
477 | " --dump-configuration-items Dump understood unit configuration items\n" |
478 | " --confirm-spawn Ask for confirmation when spawning processes\n", | |
f170852a LP |
479 | __progname); |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
a16e1123 LP |
484 | static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) { |
485 | FILE *f = NULL; | |
486 | FDSet *fds = NULL; | |
487 | int r; | |
488 | ||
489 | assert(m); | |
490 | assert(_f); | |
491 | assert(_fds); | |
492 | ||
493 | if ((r = manager_open_serialization(&f)) < 0) { | |
494 | log_error("Failed to create serialization faile: %s", strerror(-r)); | |
495 | goto fail; | |
496 | } | |
497 | ||
498 | if (!(fds = fdset_new())) { | |
499 | r = -ENOMEM; | |
500 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
501 | goto fail; | |
502 | } | |
503 | ||
504 | if ((r = manager_serialize(m, f, fds)) < 0) { | |
505 | log_error("Failed to serialize state: %s", strerror(-r)); | |
506 | goto fail; | |
507 | } | |
508 | ||
509 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
510 | log_error("Failed to rewind serialization fd: %m"); | |
511 | goto fail; | |
512 | } | |
513 | ||
514 | if ((r = fd_cloexec(fileno(f), false)) < 0) { | |
515 | log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r)); | |
516 | goto fail; | |
517 | } | |
518 | ||
519 | if ((r = fdset_cloexec(fds, false)) < 0) { | |
520 | log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r)); | |
521 | goto fail; | |
522 | } | |
523 | ||
524 | *_f = f; | |
525 | *_fds = fds; | |
526 | ||
527 | return 0; | |
528 | ||
529 | fail: | |
530 | fdset_free(fds); | |
531 | ||
532 | if (f) | |
533 | fclose(f); | |
534 | ||
535 | return r; | |
536 | } | |
537 | ||
60918275 LP |
538 | int main(int argc, char *argv[]) { |
539 | Manager *m = NULL; | |
87f0e418 | 540 | Unit *target = NULL; |
60918275 LP |
541 | Job *job = NULL; |
542 | int r, retval = 1; | |
a16e1123 LP |
543 | FDSet *fds = NULL; |
544 | bool reexecute = false; | |
27b14a22 | 545 | |
a5dab5ce LP |
546 | if (getpid() == 1) |
547 | running_as = MANAGER_INIT; | |
548 | else if (getuid() == 0) | |
549 | running_as = MANAGER_SYSTEM; | |
550 | else | |
551 | running_as = MANAGER_SESSION; | |
552 | ||
f170852a LP |
553 | if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) |
554 | goto finish; | |
60918275 | 555 | |
f170852a LP |
556 | /* Mount /proc, /sys and friends, so that /proc/cmdline and |
557 | * /proc/$PID/fd is available. */ | |
d89e521e LP |
558 | if (mount_setup() < 0) |
559 | goto finish; | |
4ade7963 LP |
560 | |
561 | /* Reset all signal handlers. */ | |
562 | assert_se(reset_all_signal_handlers() == 0); | |
563 | ||
078e4539 | 564 | /* If we are init, we can block sigkill. Yay. */ |
a337c6fc LP |
565 | ignore_signal(SIGKILL); |
566 | ignore_signal(SIGPIPE); | |
078e4539 | 567 | |
a5dab5ce LP |
568 | if (running_as != MANAGER_SESSION) |
569 | if (parse_proc_cmdline() < 0) | |
570 | goto finish; | |
f170852a LP |
571 | |
572 | log_parse_environment(); | |
573 | ||
574 | if (parse_argv(argc, argv) < 0) | |
575 | goto finish; | |
576 | ||
577 | if (action == ACTION_HELP) { | |
578 | retval = help(); | |
579 | goto finish; | |
e537352b LP |
580 | } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
581 | unit_dump_config_items(stdout); | |
582 | retval = 0; | |
583 | goto finish; | |
f170852a LP |
584 | } |
585 | ||
e965d56d | 586 | assert_se(action == ACTION_RUN || action == ACTION_TEST); |
f170852a | 587 | |
a16e1123 LP |
588 | /* Remember open file descriptors for later deserialization */ |
589 | if (serialization) { | |
590 | if ((r = fdset_new_fill(&fds)) < 0) { | |
591 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
592 | goto finish; | |
593 | } | |
594 | ||
595 | assert_se(fdset_remove(fds, fileno(serialization)) >= 0); | |
596 | } else | |
597 | close_all_fds(NULL, 0); | |
598 | ||
09082a94 | 599 | /* Set up PATH unless it is already set */ |
e537352b LP |
600 | setenv("PATH", |
601 | "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | |
602 | running_as == MANAGER_INIT); | |
09082a94 | 603 | |
f170852a LP |
604 | /* Move out of the way, so that we won't block unmounts */ |
605 | assert_se(chdir("/") == 0); | |
606 | ||
80876c20 LP |
607 | if (running_as != MANAGER_SESSION) { |
608 | /* Become a session leader if we aren't one yet. */ | |
609 | setsid(); | |
4ade7963 | 610 | |
80876c20 LP |
611 | /* Disable the umask logic */ |
612 | umask(0); | |
613 | } | |
614 | ||
615 | if (running_as == MANAGER_INIT) | |
616 | console_setup(); | |
4ade7963 LP |
617 | |
618 | /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */ | |
619 | dbus_connection_set_change_sigpipe(FALSE); | |
620 | ||
18149b9f | 621 | /* Open the logging devices, if possible and necessary */ |
4ade7963 LP |
622 | log_open_syslog(); |
623 | log_open_kmsg(); | |
624 | ||
5373d602 LP |
625 | /* Make sure we leave a core dump without panicing the |
626 | * kernel. */ | |
4fc935ca LP |
627 | if (getpid() == 1) |
628 | install_crash_handler(); | |
97c4f35c | 629 | |
a5dab5ce LP |
630 | log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); |
631 | ||
302e8c4c | 632 | if (running_as == MANAGER_INIT) |
e537352b | 633 | hostname_setup(); |
302e8c4c | 634 | |
80876c20 | 635 | if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) { |
8e274523 | 636 | log_error("Failed to allocate manager object: %s", strerror(-r)); |
60918275 LP |
637 | goto finish; |
638 | } | |
639 | ||
a16e1123 LP |
640 | if ((r = manager_startup(m, serialization, fds)) < 0) |
641 | log_error("Failed to fully startup daemon: %s", strerror(-r)); | |
642 | ||
643 | if (fds) { | |
644 | /* This will close all file descriptors that were opened, but | |
645 | * not claimed by any unit. */ | |
646 | ||
647 | fdset_free(fds); | |
648 | fds = NULL; | |
f50e0a01 LP |
649 | } |
650 | ||
a16e1123 LP |
651 | if (serialization) { |
652 | fclose(serialization); | |
653 | serialization = NULL; | |
654 | } else { | |
655 | log_debug("Activating default unit: %s", default_unit); | |
656 | ||
657 | if ((r = manager_load_unit(m, default_unit, NULL, &target)) < 0) { | |
658 | log_error("Failed to load default target: %s", strerror(-r)); | |
27b14a22 | 659 | |
a16e1123 LP |
660 | log_info("Trying to load rescue target..."); |
661 | if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &target)) < 0) { | |
662 | log_error("Failed to load rescue target: %s", strerror(-r)); | |
663 | goto finish; | |
664 | } | |
665 | } | |
37d88da7 | 666 | |
a16e1123 LP |
667 | if (action == ACTION_TEST) { |
668 | printf("→ By units:\n"); | |
669 | manager_dump_units(m, stdout, "\t"); | |
670 | } | |
671 | ||
672 | if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) { | |
673 | log_error("Failed to start default target: %s", strerror(-r)); | |
37d88da7 LP |
674 | goto finish; |
675 | } | |
60918275 | 676 | |
a16e1123 LP |
677 | if (action == ACTION_TEST) { |
678 | printf("→ By jobs:\n"); | |
679 | manager_dump_jobs(m, stdout, "\t"); | |
680 | retval = 0; | |
681 | goto finish; | |
682 | } | |
e965d56d | 683 | } |
d46de8a1 | 684 | |
a16e1123 LP |
685 | for (;;) { |
686 | if ((r = manager_loop(m)) < 0) { | |
687 | log_error("Failed to run mainloop: %s", strerror(-r)); | |
688 | goto finish; | |
689 | } | |
11dd41ce | 690 | |
a16e1123 | 691 | switch (m->exit_code) { |
e965d56d | 692 | |
a16e1123 LP |
693 | case MANAGER_EXIT: |
694 | retval = 0; | |
695 | log_debug("Exit."); | |
696 | goto finish; | |
e965d56d | 697 | |
a16e1123 LP |
698 | case MANAGER_RELOAD: |
699 | if ((r = manager_reload(m)) < 0) | |
700 | log_error("Failed to reload: %s", strerror(-r)); | |
701 | break; | |
cea8e32e | 702 | |
a16e1123 | 703 | case MANAGER_REEXECUTE: |
a16e1123 LP |
704 | if (prepare_reexecute(m, &serialization, &fds) < 0) |
705 | goto finish; | |
60918275 | 706 | |
a16e1123 LP |
707 | reexecute = true; |
708 | log_debug("Reexecuting."); | |
709 | goto finish; | |
710 | ||
711 | default: | |
712 | assert_not_reached("Unknown exit code."); | |
713 | } | |
714 | } | |
f170852a | 715 | |
60918275 LP |
716 | finish: |
717 | if (m) | |
718 | manager_free(m); | |
719 | ||
f170852a | 720 | free(default_unit); |
b9cd2ec1 | 721 | |
ea430986 LP |
722 | dbus_shutdown(); |
723 | ||
a16e1123 LP |
724 | if (reexecute) { |
725 | const char *args[11]; | |
726 | unsigned i = 0; | |
727 | char sfd[16]; | |
728 | ||
729 | assert(serialization); | |
730 | assert(fds); | |
731 | ||
732 | args[i++] = SYSTEMD_BINARY_PATH; | |
733 | ||
734 | args[i++] = "--log-level"; | |
735 | args[i++] = log_level_to_string(log_get_max_level()); | |
736 | ||
737 | args[i++] = "--log-target"; | |
738 | args[i++] = log_target_to_string(log_get_target()); | |
739 | ||
740 | args[i++] = "--running-as"; | |
741 | args[i++] = manager_running_as_to_string(running_as); | |
742 | ||
743 | snprintf(sfd, sizeof(sfd), "%i", fileno(serialization)); | |
744 | char_array_0(sfd); | |
745 | ||
746 | args[i++] = "--deserialize"; | |
747 | args[i++] = sfd; | |
748 | ||
749 | if (confirm_spawn) | |
750 | args[i++] = "--confirm-spawn"; | |
751 | ||
752 | args[i++] = NULL; | |
753 | ||
754 | assert(i <= ELEMENTSOF(args)); | |
755 | ||
756 | execv(args[0], (char* const*) args); | |
757 | ||
758 | log_error("Failed to reexecute: %m"); | |
759 | } | |
760 | ||
761 | if (serialization) | |
762 | fclose(serialization); | |
763 | ||
764 | if (fds) | |
765 | fdset_free(fds); | |
766 | ||
c3b3c274 LP |
767 | if (getpid() == 1) |
768 | freeze(); | |
769 | ||
60918275 LP |
770 | return retval; |
771 | } |