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