]>
Commit | Line | Data |
---|---|---|
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ | |
2 | ||
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 | ||
22 | #include <dbus/dbus.h> | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <errno.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <getopt.h> | |
31 | #include <signal.h> | |
32 | #include <sys/wait.h> | |
33 | ||
34 | #include "manager.h" | |
35 | #include "log.h" | |
36 | #include "mount-setup.h" | |
37 | #include "hostname-setup.h" | |
38 | #include "load-fragment.h" | |
39 | ||
40 | static enum { | |
41 | ACTION_RUN, | |
42 | ACTION_HELP, | |
43 | ACTION_TEST, | |
44 | ACTION_DUMP_CONFIGURATION_ITEMS | |
45 | } action = ACTION_RUN; | |
46 | ||
47 | static char *default_unit = NULL; | |
48 | static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID; | |
49 | ||
50 | static bool dump_core = true; | |
51 | static bool crash_shell = false; | |
52 | static int crash_chvt = -1; | |
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 | ||
66 | if ((pid = fork()) < 0) | |
67 | log_error("Caught <%s>, cannot fork for core dump: %s", strsignal(sig), strerror(errno)); | |
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); | |
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); | |
103 | } | |
104 | } | |
105 | ||
106 | if (crash_chvt) | |
107 | chvt(crash_chvt); | |
108 | ||
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."); | |
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); | |
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); | |
134 | assert_se(sigaction(SIGABRT, &sa, NULL) == 0); | |
135 | } | |
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=")) | |
165 | return set_default_unit(word + 16); | |
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 | ||
177 | } else if (startswith(word, "systemd.dump_core=")) { | |
178 | int r; | |
179 | ||
180 | if ((r = parse_boolean(word + 18)) < 0) | |
181 | log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18); | |
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) | |
189 | log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20); | |
190 | else | |
191 | crash_shell = r; | |
192 | ||
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 | ||
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"); | |
211 | log_info("systemd.crash_chvt=N Change to VT #N on crash"); | |
212 | ||
213 | } else { | |
214 | unsigned i; | |
215 | ||
216 | /* SysV compatibility */ | |
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, | |
264 | ARG_DEFAULT, | |
265 | ARG_RUNNING_AS, | |
266 | ARG_TEST, | |
267 | ARG_DUMP_CONFIGURATION_ITEMS | |
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 }, | |
274 | { "running-as", required_argument, NULL, ARG_RUNNING_AS }, | |
275 | { "test", no_argument, NULL, ARG_TEST }, | |
276 | { "help", no_argument, NULL, 'h' }, | |
277 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, | |
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 | ||
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 | ||
328 | case ARG_TEST: | |
329 | action = ACTION_TEST; | |
330 | break; | |
331 | ||
332 | case ARG_DUMP_CONFIGURATION_ITEMS: | |
333 | action = ACTION_DUMP_CONFIGURATION_ITEMS; | |
334 | break; | |
335 | ||
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 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | static int help(void) { | |
352 | ||
353 | printf("%s [options]\n\n" | |
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", | |
361 | __progname); | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | int main(int argc, char *argv[]) { | |
367 | Manager *m = NULL; | |
368 | Unit *target = NULL; | |
369 | Job *job = NULL; | |
370 | int r, retval = 1; | |
371 | ||
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 | ||
379 | if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) | |
380 | goto finish; | |
381 | ||
382 | /* Mount /proc, /sys and friends, so that /proc/cmdline and | |
383 | * /proc/$PID/fd is available. */ | |
384 | if (mount_setup() < 0) | |
385 | goto finish; | |
386 | ||
387 | /* Reset all signal handlers. */ | |
388 | assert_se(reset_all_signal_handlers() == 0); | |
389 | ||
390 | /* Close all open files */ | |
391 | assert_se(close_all_fds(NULL, 0) == 0); | |
392 | ||
393 | if (running_as != MANAGER_SESSION) | |
394 | if (parse_proc_cmdline() < 0) | |
395 | goto finish; | |
396 | ||
397 | log_parse_environment(); | |
398 | ||
399 | if (parse_argv(argc, argv) < 0) | |
400 | goto finish; | |
401 | ||
402 | if (action == ACTION_HELP) { | |
403 | retval = help(); | |
404 | goto finish; | |
405 | } else if (action == ACTION_DUMP_CONFIGURATION_ITEMS) { | |
406 | unit_dump_config_items(stdout); | |
407 | retval = 0; | |
408 | goto finish; | |
409 | } | |
410 | ||
411 | assert_se(action == ACTION_RUN || action == ACTION_TEST); | |
412 | ||
413 | /* Set up PATH unless it is already set */ | |
414 | setenv("PATH", | |
415 | "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | |
416 | running_as == MANAGER_INIT); | |
417 | ||
418 | /* Move out of the way, so that we won't block unmounts */ | |
419 | assert_se(chdir("/") == 0); | |
420 | ||
421 | /* Become a session leader if we aren't one yet. */ | |
422 | setsid(); | |
423 | ||
424 | /* Disable the umask logic */ | |
425 | umask(0); | |
426 | ||
427 | /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */ | |
428 | dbus_connection_set_change_sigpipe(FALSE); | |
429 | ||
430 | /* Open the logging devices, if possible and necessary */ | |
431 | log_open_syslog(); | |
432 | log_open_kmsg(); | |
433 | ||
434 | /* Make sure we leave a core dump without panicing the | |
435 | * kernel. */ | |
436 | if (getpid() == 1) | |
437 | install_crash_handler(); | |
438 | ||
439 | log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); | |
440 | ||
441 | if (running_as == MANAGER_INIT) | |
442 | hostname_setup(); | |
443 | ||
444 | if ((r = manager_new(running_as, &m)) < 0) { | |
445 | log_error("Failed to allocate manager object: %s", strerror(-r)); | |
446 | goto finish; | |
447 | } | |
448 | ||
449 | if ((r = manager_coldplug(m)) < 0) { | |
450 | log_error("Failed to retrieve coldplug information: %s", strerror(-r)); | |
451 | goto finish; | |
452 | } | |
453 | ||
454 | log_debug("Activating default unit: %s", default_unit); | |
455 | ||
456 | if ((r = manager_load_unit(m, default_unit, &target)) < 0) { | |
457 | log_error("Failed to load default target: %s", strerror(-r)); | |
458 | ||
459 | log_info("Trying to load rescue target..."); | |
460 | if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, &target)) < 0) { | |
461 | log_error("Failed to load rescue target: %s", strerror(-r)); | |
462 | goto finish; | |
463 | } | |
464 | } | |
465 | ||
466 | if (action == ACTION_TEST) { | |
467 | printf("→ By units:\n"); | |
468 | manager_dump_units(m, stdout, "\t"); | |
469 | } | |
470 | ||
471 | if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &job)) < 0) { | |
472 | log_error("Failed to start default target: %s", strerror(-r)); | |
473 | goto finish; | |
474 | } | |
475 | ||
476 | if (action == ACTION_TEST) { | |
477 | printf("→ By jobs:\n"); | |
478 | manager_dump_jobs(m, stdout, "\t"); | |
479 | ||
480 | if (getpid() == 1) | |
481 | pause(); | |
482 | ||
483 | retval = 0; | |
484 | goto finish; | |
485 | } | |
486 | ||
487 | if ((r = manager_loop(m)) < 0) { | |
488 | log_error("Failed to run mainloop: %s", strerror(-r)); | |
489 | goto finish; | |
490 | } | |
491 | ||
492 | retval = 0; | |
493 | ||
494 | log_debug("Exit."); | |
495 | ||
496 | finish: | |
497 | if (m) | |
498 | manager_free(m); | |
499 | ||
500 | free(default_unit); | |
501 | ||
502 | dbus_shutdown(); | |
503 | ||
504 | return retval; | |
505 | } |