]>
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> |
60918275 LP |
33 | |
34 | #include "manager.h" | |
16354eff | 35 | #include "log.h" |
4ade7963 | 36 | #include "mount-setup.h" |
302e8c4c LP |
37 | #include "hostname-setup.h" |
38 | #include "load-fragment.h" | |
60918275 | 39 | |
f170852a LP |
40 | static enum { |
41 | ACTION_RUN, | |
e965d56d | 42 | ACTION_HELP, |
e537352b LP |
43 | ACTION_TEST, |
44 | ACTION_DUMP_CONFIGURATION_ITEMS | |
f170852a LP |
45 | } action = ACTION_RUN; |
46 | ||
47 | static char *default_unit = NULL; | |
a5dab5ce | 48 | static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID; |
4fc935ca | 49 | |
97c4f35c | 50 | static bool dump_core = true; |
601f6a1e LP |
51 | static bool crash_shell = false; |
52 | static int crash_chvt = -1; | |
97c4f35c LP |
53 | |
54 | _noreturn static void freeze(void) { | |
55 | for (;;) | |
56 | pause(); | |
57 | } | |
58 | ||
59 | _noreturn static void crash(int sig) { | |
60 | ||
61 | if (!dump_core) | |
62 | log_error("Caught <%s>, not dumping core.", strsignal(sig)); | |
63 | else { | |
64 | pid_t pid; | |
65 | ||
97c4f35c | 66 | if ((pid = fork()) < 0) |
4fc935ca | 67 | log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno)); |
97c4f35c LP |
68 | |
69 | else if (pid == 0) { | |
70 | struct sigaction sa; | |
71 | struct rlimit rl; | |
72 | ||
73 | /* Enable default signal handler for core dump */ | |
74 | zero(sa); | |
75 | sa.sa_handler = SIG_DFL; | |
76 | assert_se(sigaction(sig, &sa, NULL) == 0); | |
77 | ||
78 | /* Don't limit the core dump size */ | |
79 | zero(rl); | |
80 | rl.rlim_cur = RLIM_INFINITY; | |
81 | rl.rlim_max = RLIM_INFINITY; | |
82 | setrlimit(RLIMIT_CORE, &rl); | |
83 | ||
84 | /* Just to be sure... */ | |
85 | assert_se(chdir("/") == 0); | |
86 | ||
87 | /* Raise the signal again */ | |
88 | raise(sig); | |
89 | ||
90 | assert_not_reached("We shouldn't be here..."); | |
91 | _exit(1); | |
4fc935ca LP |
92 | |
93 | } else { | |
94 | int status, r; | |
95 | ||
96 | /* Order things nicely. */ | |
97 | if ((r = waitpid(pid, &status, 0)) < 0) | |
98 | log_error("Caught <%s>, waitpid() failed: %s", strsignal(sig), strerror(errno)); | |
99 | else if (!WCOREDUMP(status)) | |
100 | log_error("Caught <%s>, core dump failed.", strsignal(sig)); | |
101 | else | |
102 | log_error("Caught <%s>, dumped core as pid %llu.", strsignal(sig), (unsigned long long) pid); | |
97c4f35c LP |
103 | } |
104 | } | |
105 | ||
601f6a1e LP |
106 | if (crash_chvt) |
107 | chvt(crash_chvt); | |
108 | ||
4fc935ca LP |
109 | if (crash_shell) { |
110 | log_info("Executing crash shell in 10s..."); | |
111 | sleep(10); | |
112 | ||
113 | execl("/bin/sh", "/bin/sh", NULL); | |
114 | log_error("execl() failed: %s", strerror(errno)); | |
115 | } | |
116 | ||
117 | log_info("Freezing execution."); | |
97c4f35c LP |
118 | freeze(); |
119 | } | |
120 | ||
121 | static void install_crash_handler(void) { | |
122 | struct sigaction sa; | |
123 | ||
124 | zero(sa); | |
125 | ||
126 | sa.sa_handler = crash; | |
127 | sa.sa_flags = SA_NODEFER; | |
128 | ||
129 | assert_se(sigaction(SIGSEGV, &sa, NULL) == 0); | |
5373d602 LP |
130 | assert_se(sigaction(SIGILL, &sa, NULL) == 0); |
131 | assert_se(sigaction(SIGFPE, &sa, NULL) == 0); | |
132 | assert_se(sigaction(SIGBUS, &sa, NULL) == 0); | |
133 | assert_se(sigaction(SIGQUIT, &sa, NULL) == 0); | |
97c4f35c LP |
134 | assert_se(sigaction(SIGABRT, &sa, NULL) == 0); |
135 | } | |
f170852a LP |
136 | |
137 | static int set_default_unit(const char *u) { | |
138 | char *c; | |
139 | ||
140 | assert(u); | |
141 | ||
142 | if (!(c = strdup(u))) | |
143 | return -ENOMEM; | |
144 | ||
145 | free(default_unit); | |
146 | default_unit = c; | |
147 | return 0; | |
148 | } | |
149 | ||
150 | static int parse_proc_cmdline_word(const char *word) { | |
151 | ||
152 | static const char * const rlmap[] = { | |
153 | "single", SPECIAL_RUNLEVEL1_TARGET, | |
154 | "-s", SPECIAL_RUNLEVEL1_TARGET, | |
155 | "s", SPECIAL_RUNLEVEL1_TARGET, | |
156 | "S", SPECIAL_RUNLEVEL1_TARGET, | |
157 | "1", SPECIAL_RUNLEVEL1_TARGET, | |
158 | "2", SPECIAL_RUNLEVEL2_TARGET, | |
159 | "3", SPECIAL_RUNLEVEL3_TARGET, | |
160 | "4", SPECIAL_RUNLEVEL4_TARGET, | |
161 | "5", SPECIAL_RUNLEVEL5_TARGET | |
162 | }; | |
163 | ||
164 | if (startswith(word, "systemd.default=")) | |
82771ba1 | 165 | return set_default_unit(word + 16); |
f170852a LP |
166 | |
167 | else if (startswith(word, "systemd.log_target=")) { | |
168 | ||
169 | if (log_set_target_from_string(word + 19) < 0) | |
170 | log_warning("Failed to parse log target %s. Ignoring.", word + 19); | |
171 | ||
172 | } else if (startswith(word, "systemd.log_level=")) { | |
173 | ||
174 | if (log_set_max_level_from_string(word + 18) < 0) | |
175 | log_warning("Failed to parse log level %s. Ignoring.", word + 18); | |
176 | ||
4fc935ca LP |
177 | } else if (startswith(word, "systemd.dump_core=")) { |
178 | int r; | |
179 | ||
180 | if ((r = parse_boolean(word + 18)) < 0) | |
601f6a1e | 181 | log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18); |
4fc935ca LP |
182 | else |
183 | dump_core = r; | |
184 | ||
185 | } else if (startswith(word, "systemd.crash_shell=")) { | |
186 | int r; | |
187 | ||
188 | if ((r = parse_boolean(word + 20)) < 0) | |
601f6a1e | 189 | log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20); |
4fc935ca LP |
190 | else |
191 | crash_shell = r; | |
192 | ||
601f6a1e LP |
193 | } else if (startswith(word, "systemd.crash_chvt=")) { |
194 | int k; | |
195 | ||
196 | if (safe_atoi(word + 19, &k) < 0) | |
197 | log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19); | |
198 | else | |
199 | crash_chvt = k; | |
200 | ||
4fc935ca LP |
201 | } else if (startswith(word, "systemd.")) { |
202 | ||
203 | log_warning("Unknown kernel switch %s. Ignoring.", word); | |
204 | ||
205 | log_info("Supported kernel switches:"); | |
206 | log_info("systemd.default=UNIT Default unit to start"); | |
207 | log_info("systemd.log_target=console|kmsg|syslog Log target"); | |
208 | log_info("systemd.log_level=LEVEL Log level"); | |
209 | log_info("systemd.dump_core=0|1 Dump core on crash"); | |
210 | log_info("systemd.crash_shell=0|1 On crash run shell"); | |
601f6a1e | 211 | log_info("systemd.crash_chvt=N Change to VT #N on crash"); |
4fc935ca | 212 | |
f170852a LP |
213 | } else { |
214 | unsigned i; | |
215 | ||
216 | /* SysV compatibility */ | |
f170852a LP |
217 | for (i = 0; i < ELEMENTSOF(rlmap); i += 2) |
218 | if (streq(word, rlmap[i])) | |
219 | return set_default_unit(rlmap[i+1]); | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | static int parse_proc_cmdline(void) { | |
226 | char *line; | |
227 | int r; | |
228 | char *w; | |
229 | size_t l; | |
230 | char *state; | |
231 | ||
232 | if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) { | |
233 | log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(errno)); | |
234 | return 0; | |
235 | } | |
236 | ||
237 | FOREACH_WORD_QUOTED(w, l, line, state) { | |
238 | char *word; | |
239 | ||
240 | if (!(word = strndup(w, l))) { | |
241 | r = -ENOMEM; | |
242 | goto finish; | |
243 | } | |
244 | ||
245 | r = parse_proc_cmdline_word(word); | |
246 | free(word); | |
247 | ||
248 | if (r < 0) | |
249 | goto finish; | |
250 | } | |
251 | ||
252 | r = 0; | |
253 | ||
254 | finish: | |
255 | free(line); | |
256 | return r; | |
257 | } | |
258 | ||
259 | static int parse_argv(int argc, char *argv[]) { | |
260 | ||
261 | enum { | |
262 | ARG_LOG_LEVEL = 0x100, | |
263 | ARG_LOG_TARGET, | |
a5dab5ce | 264 | ARG_DEFAULT, |
e965d56d | 265 | ARG_RUNNING_AS, |
e537352b LP |
266 | ARG_TEST, |
267 | ARG_DUMP_CONFIGURATION_ITEMS | |
f170852a LP |
268 | }; |
269 | ||
270 | static const struct option options[] = { | |
271 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, | |
272 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
273 | { "default", required_argument, NULL, ARG_DEFAULT }, | |
a5dab5ce | 274 | { "running-as", required_argument, NULL, ARG_RUNNING_AS }, |
e965d56d | 275 | { "test", no_argument, NULL, ARG_TEST }, |
f170852a | 276 | { "help", no_argument, NULL, 'h' }, |
e537352b | 277 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, |
f170852a LP |
278 | { NULL, 0, NULL, 0 } |
279 | }; | |
280 | ||
281 | int c, r; | |
282 | ||
283 | assert(argc >= 1); | |
284 | assert(argv); | |
285 | ||
286 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
287 | ||
288 | switch (c) { | |
289 | ||
290 | case ARG_LOG_LEVEL: | |
291 | if ((r = log_set_max_level_from_string(optarg)) < 0) { | |
292 | log_error("Failed to parse log level %s.", optarg); | |
293 | return r; | |
294 | } | |
295 | ||
296 | break; | |
297 | ||
298 | case ARG_LOG_TARGET: | |
299 | ||
300 | if ((r = log_set_target_from_string(optarg)) < 0) { | |
301 | log_error("Failed to parse log target %s.", optarg); | |
302 | return r; | |
303 | } | |
304 | ||
305 | break; | |
306 | ||
307 | case ARG_DEFAULT: | |
308 | ||
309 | if ((r = set_default_unit(optarg)) < 0) { | |
310 | log_error("Failed to set default unit %s: %s", optarg, strerror(-r)); | |
311 | return r; | |
312 | } | |
313 | ||
314 | break; | |
315 | ||
a5dab5ce LP |
316 | case ARG_RUNNING_AS: { |
317 | ManagerRunningAs as; | |
318 | ||
319 | if ((as = manager_running_as_from_string(optarg)) < 0) { | |
320 | log_error("Failed to parse running as value %s", optarg); | |
321 | return -EINVAL; | |
322 | } | |
323 | ||
324 | running_as = as; | |
325 | break; | |
326 | } | |
327 | ||
e965d56d LP |
328 | case ARG_TEST: |
329 | action = ACTION_TEST; | |
330 | break; | |
331 | ||
e537352b LP |
332 | case ARG_DUMP_CONFIGURATION_ITEMS: |
333 | action = ACTION_DUMP_CONFIGURATION_ITEMS; | |
334 | break; | |
335 | ||
f170852a LP |
336 | case 'h': |
337 | action = ACTION_HELP; | |
338 | break; | |
339 | ||
340 | case '?': | |
341 | return -EINVAL; | |
342 | ||
343 | default: | |
344 | log_error("Unknown option code %c", c); | |
345 | return -EINVAL; | |
346 | } | |
347 | ||
f170852a LP |
348 | return 0; |
349 | } | |
350 | ||
351 | static int help(void) { | |
352 | ||
353 | printf("%s [options]\n\n" | |
e537352b LP |
354 | " -h --help Show this help\n" |
355 | " --default=UNIT Set default unit\n" | |
356 | " --log-level=LEVEL Set log level\n" | |
357 | " --log-target=TARGET Set log target (console, syslog, kmsg)\n" | |
358 | " --running-as=AS Set running as (init, system, session)\n" | |
359 | " --test Determine startup sequence, dump it and exit\n" | |
360 | " --dump-configuration-items Dump understood unit configuration items\n", | |
f170852a LP |
361 | __progname); |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
60918275 LP |
366 | int main(int argc, char *argv[]) { |
367 | Manager *m = NULL; | |
87f0e418 | 368 | Unit *target = NULL; |
60918275 LP |
369 | Job *job = NULL; |
370 | int r, retval = 1; | |
27b14a22 | 371 | |
a5dab5ce LP |
372 | if (getpid() == 1) |
373 | running_as = MANAGER_INIT; | |
374 | else if (getuid() == 0) | |
375 | running_as = MANAGER_SYSTEM; | |
376 | else | |
377 | running_as = MANAGER_SESSION; | |
378 | ||
f170852a LP |
379 | if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) |
380 | goto finish; | |
60918275 | 381 | |
f170852a LP |
382 | /* Mount /proc, /sys and friends, so that /proc/cmdline and |
383 | * /proc/$PID/fd is available. */ | |
d89e521e LP |
384 | if (mount_setup() < 0) |
385 | goto finish; | |
4ade7963 LP |
386 | |
387 | /* Reset all signal handlers. */ | |
388 | assert_se(reset_all_signal_handlers() == 0); | |
389 | ||
078e4539 LP |
390 | /* If we are init, we can block sigkill. Yay. */ |
391 | signal(SIGKILL, SIG_IGN); | |
392 | signal(SIGPIPE, SIG_IGN); | |
393 | ||
f170852a LP |
394 | /* Close all open files */ |
395 | assert_se(close_all_fds(NULL, 0) == 0); | |
396 | ||
a5dab5ce LP |
397 | if (running_as != MANAGER_SESSION) |
398 | if (parse_proc_cmdline() < 0) | |
399 | goto finish; | |
f170852a LP |
400 | |
401 | log_parse_environment(); | |
402 | ||
403 | if (parse_argv(argc, argv) < 0) | |
404 | goto finish; | |
405 | ||
406 | if (action == ACTION_HELP) { | |
407 | retval = help(); | |
408 | goto finish; | |
e537352b LP |
409 | } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
410 | unit_dump_config_items(stdout); | |
411 | retval = 0; | |
412 | goto finish; | |
f170852a LP |
413 | } |
414 | ||
e965d56d | 415 | assert_se(action == ACTION_RUN || action == ACTION_TEST); |
f170852a | 416 | |
09082a94 | 417 | /* Set up PATH unless it is already set */ |
e537352b LP |
418 | setenv("PATH", |
419 | "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | |
420 | running_as == MANAGER_INIT); | |
09082a94 | 421 | |
f170852a LP |
422 | /* Move out of the way, so that we won't block unmounts */ |
423 | assert_se(chdir("/") == 0); | |
424 | ||
4ade7963 LP |
425 | /* Become a session leader if we aren't one yet. */ |
426 | setsid(); | |
427 | ||
428 | /* Disable the umask logic */ | |
429 | umask(0); | |
430 | ||
431 | /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */ | |
432 | dbus_connection_set_change_sigpipe(FALSE); | |
433 | ||
18149b9f | 434 | /* Open the logging devices, if possible and necessary */ |
4ade7963 LP |
435 | log_open_syslog(); |
436 | log_open_kmsg(); | |
437 | ||
5373d602 LP |
438 | /* Make sure we leave a core dump without panicing the |
439 | * kernel. */ | |
4fc935ca LP |
440 | if (getpid() == 1) |
441 | install_crash_handler(); | |
97c4f35c | 442 | |
a5dab5ce LP |
443 | log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); |
444 | ||
302e8c4c | 445 | if (running_as == MANAGER_INIT) |
e537352b | 446 | hostname_setup(); |
302e8c4c | 447 | |
a5dab5ce | 448 | if ((r = manager_new(running_as, &m)) < 0) { |
8e274523 | 449 | log_error("Failed to allocate manager object: %s", strerror(-r)); |
60918275 LP |
450 | goto finish; |
451 | } | |
452 | ||
f50e0a01 LP |
453 | if ((r = manager_coldplug(m)) < 0) { |
454 | log_error("Failed to retrieve coldplug information: %s", strerror(-r)); | |
455 | goto finish; | |
456 | } | |
457 | ||
27b14a22 LP |
458 | log_debug("Activating default unit: %s", default_unit); |
459 | ||
460 | if ((r = manager_load_unit(m, default_unit, &target)) < 0) { | |
c22cbe26 | 461 | log_error("Failed to load default target: %s", strerror(-r)); |
37d88da7 LP |
462 | |
463 | log_info("Trying to load rescue target..."); | |
464 | if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) { | |
465 | log_error("Failed to load rescue target: %s", strerror(-r)); | |
466 | goto finish; | |
467 | } | |
60918275 LP |
468 | } |
469 | ||
e965d56d LP |
470 | if (action == ACTION_TEST) { |
471 | printf("→ By units:\n"); | |
472 | manager_dump_units(m, stdout, "\t"); | |
473 | } | |
d46de8a1 | 474 | |
8f5847c4 LP |
475 | if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) { |
476 | log_error("Failed to start default target: %s", strerror(-r)); | |
477 | goto finish; | |
478 | } | |
11dd41ce | 479 | |
e965d56d LP |
480 | if (action == ACTION_TEST) { |
481 | printf("→ By jobs:\n"); | |
482 | manager_dump_jobs(m, stdout, "\t"); | |
483 | ||
484 | if (getpid() == 1) | |
485 | pause(); | |
486 | ||
487 | retval = 0; | |
488 | goto finish; | |
489 | } | |
cea8e32e | 490 | |
c20cae32 LP |
491 | if ((r = manager_loop(m)) < 0) { |
492 | log_error("Failed to run mainloop: %s", strerror(-r)); | |
493 | goto finish; | |
494 | } | |
9152c765 | 495 | |
60918275 LP |
496 | retval = 0; |
497 | ||
f170852a LP |
498 | log_debug("Exit."); |
499 | ||
60918275 LP |
500 | finish: |
501 | if (m) | |
502 | manager_free(m); | |
503 | ||
f170852a | 504 | free(default_unit); |
b9cd2ec1 | 505 | |
ea430986 LP |
506 | dbus_shutdown(); |
507 | ||
60918275 LP |
508 | return retval; |
509 | } |