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