]>
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 | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
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 | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <stdio.h> |
23 | #include <errno.h> | |
24 | #include <string.h> | |
16354eff | 25 | #include <unistd.h> |
4ade7963 LP |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> | |
f170852a | 28 | #include <getopt.h> |
97c4f35c | 29 | #include <signal.h> |
4fc935ca | 30 | #include <sys/wait.h> |
80876c20 | 31 | #include <fcntl.h> |
f3b6a3ed | 32 | #include <sys/prctl.h> |
664f88a7 | 33 | #include <sys/mount.h> |
60918275 | 34 | |
54b434b1 LP |
35 | #ifdef HAVE_VALGRIND_VALGRIND_H |
36 | #include <valgrind/valgrind.h> | |
37 | #endif | |
b64a3d86 LP |
38 | #ifdef HAVE_SECCOMP |
39 | #include <seccomp.h> | |
40 | #endif | |
54b434b1 | 41 | |
718db961 LP |
42 | #include "sd-daemon.h" |
43 | #include "sd-messages.h" | |
44 | #include "sd-bus.h" | |
60918275 | 45 | #include "manager.h" |
16354eff | 46 | #include "log.h" |
302e8c4c | 47 | #include "load-fragment.h" |
a16e1123 | 48 | #include "fdset.h" |
514f4ef5 | 49 | #include "special.h" |
487393e9 | 50 | #include "conf-parser.h" |
ad780f19 | 51 | #include "missing.h" |
e51bc1a2 | 52 | #include "label.h" |
302e27c8 | 53 | #include "build.h" |
06d4c99a | 54 | #include "strv.h" |
f6a6225e | 55 | #include "def.h" |
b52aae1d | 56 | #include "virt.h" |
d9d93745 | 57 | #include "architecture.h" |
e96d6be7 | 58 | #include "watchdog.h" |
664f88a7 | 59 | #include "path-util.h" |
41669317 | 60 | #include "switch-root.h" |
ec8927ca | 61 | #include "capability.h" |
bd3fa1d2 | 62 | #include "killall.h" |
0c4025d1 | 63 | #include "env-util.h" |
24efb112 | 64 | #include "clock-util.h" |
718db961 LP |
65 | #include "fileio.h" |
66 | #include "dbus-manager.h" | |
67 | #include "bus-error.h" | |
68 | #include "bus-util.h" | |
60918275 | 69 | |
b6e66135 LP |
70 | #include "mount-setup.h" |
71 | #include "loopback-setup.h" | |
b6e66135 LP |
72 | #include "hostname-setup.h" |
73 | #include "machine-id-setup.h" | |
b6e66135 LP |
74 | #include "selinux-setup.h" |
75 | #include "ima-setup.h" | |
ffbd2c4d | 76 | #include "smack-setup.h" |
718db961 LP |
77 | #ifdef HAVE_KMOD |
78 | #include "kmod-setup.h" | |
79 | #endif | |
b6e66135 | 80 | |
f170852a LP |
81 | static enum { |
82 | ACTION_RUN, | |
e965d56d | 83 | ACTION_HELP, |
9ba0bc4e | 84 | ACTION_VERSION, |
e537352b | 85 | ACTION_TEST, |
4288f619 LP |
86 | ACTION_DUMP_CONFIGURATION_ITEMS, |
87 | ACTION_DONE | |
fa0f4d8a | 88 | } arg_action = ACTION_RUN; |
fa0f4d8a | 89 | static char *arg_default_unit = NULL; |
67445f4e | 90 | static SystemdRunningAs arg_running_as = _SYSTEMD_RUNNING_AS_INVALID; |
fa0f4d8a LP |
91 | static bool arg_dump_core = true; |
92 | static bool arg_crash_shell = false; | |
93 | static int arg_crash_chvt = -1; | |
94 | static bool arg_confirm_spawn = false; | |
b6e2f329 | 95 | static ShowStatus arg_show_status = _SHOW_STATUS_UNSET; |
bf4df7c3 | 96 | static bool arg_switched_root = false; |
0c85a4f3 | 97 | static char ***arg_join_controllers = NULL; |
706343f4 | 98 | static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL; |
0a494f1f | 99 | static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT; |
1f19a534 OS |
100 | static usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC; |
101 | static usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC; | |
102 | static usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC; | |
3f41e1e5 LN |
103 | static usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL; |
104 | static unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST; | |
e96d6be7 LP |
105 | static usec_t arg_runtime_watchdog = 0; |
106 | static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE; | |
97d0e5f8 | 107 | static char **arg_default_environment = NULL; |
517d56b1 | 108 | static struct rlimit *arg_default_rlimit[_RLIMIT_MAX] = {}; |
ec8927ca | 109 | static uint64_t arg_capability_bounding_set_drop = 0; |
aa0f64ac | 110 | static nsec_t arg_timer_slack_nsec = (nsec_t) -1; |
bd8f585b | 111 | static usec_t arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE; |
d3b1c508 LP |
112 | static Set* arg_syscall_archs = NULL; |
113 | static FILE* arg_serialization = NULL; | |
085afe36 LP |
114 | static bool arg_default_cpu_accounting = false; |
115 | static bool arg_default_blockio_accounting = false; | |
116 | static bool arg_default_memory_accounting = false; | |
4fc935ca | 117 | |
d3b1c508 | 118 | static void nop_handler(int sig) {} |
6f5e3f35 | 119 | |
919ce0b7 | 120 | noreturn static void crash(int sig) { |
97c4f35c | 121 | |
abb26902 LP |
122 | if (getpid() != 1) |
123 | /* Pass this on immediately, if this is not PID 1 */ | |
124 | raise(sig); | |
125 | else if (!arg_dump_core) | |
582a507f | 126 | log_error("Caught <%s>, not dumping core.", signal_to_string(sig)); |
97c4f35c | 127 | else { |
b92bea5d ZJS |
128 | struct sigaction sa = { |
129 | .sa_handler = nop_handler, | |
130 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, | |
131 | }; | |
97c4f35c LP |
132 | pid_t pid; |
133 | ||
6f5e3f35 | 134 | /* We want to wait for the core process, hence let's enable SIGCHLD */ |
abb26902 | 135 | sigaction(SIGCHLD, &sa, NULL); |
6f5e3f35 | 136 | |
e62d8c39 ZJS |
137 | pid = fork(); |
138 | if (pid < 0) | |
f5f6d0e2 | 139 | log_error("Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig)); |
97c4f35c LP |
140 | |
141 | else if (pid == 0) { | |
b92bea5d | 142 | struct rlimit rl = {}; |
97c4f35c LP |
143 | |
144 | /* Enable default signal handler for core dump */ | |
145 | zero(sa); | |
146 | sa.sa_handler = SIG_DFL; | |
abb26902 | 147 | sigaction(sig, &sa, NULL); |
97c4f35c LP |
148 | |
149 | /* Don't limit the core dump size */ | |
97c4f35c LP |
150 | rl.rlim_cur = RLIM_INFINITY; |
151 | rl.rlim_max = RLIM_INFINITY; | |
152 | setrlimit(RLIMIT_CORE, &rl); | |
153 | ||
154 | /* Just to be sure... */ | |
abb26902 | 155 | chdir("/"); |
97c4f35c LP |
156 | |
157 | /* Raise the signal again */ | |
158 | raise(sig); | |
159 | ||
160 | assert_not_reached("We shouldn't be here..."); | |
161 | _exit(1); | |
4fc935ca LP |
162 | |
163 | } else { | |
8e12a6ae LP |
164 | siginfo_t status; |
165 | int r; | |
4fc935ca LP |
166 | |
167 | /* Order things nicely. */ | |
e62d8c39 ZJS |
168 | r = wait_for_terminate(pid, &status); |
169 | if (r < 0) | |
7989e1f2 | 170 | log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r)); |
8e12a6ae | 171 | else if (status.si_code != CLD_DUMPED) |
7989e1f2 | 172 | log_error("Caught <%s>, core dump failed.", signal_to_string(sig)); |
4fc935ca | 173 | else |
ccd06097 | 174 | log_error("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid); |
97c4f35c LP |
175 | } |
176 | } | |
177 | ||
fa0f4d8a LP |
178 | if (arg_crash_chvt) |
179 | chvt(arg_crash_chvt); | |
601f6a1e | 180 | |
fa0f4d8a | 181 | if (arg_crash_shell) { |
b92bea5d ZJS |
182 | struct sigaction sa = { |
183 | .sa_handler = SIG_IGN, | |
184 | .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART, | |
185 | }; | |
6f5e3f35 | 186 | pid_t pid; |
8c43883a | 187 | |
4fc935ca LP |
188 | log_info("Executing crash shell in 10s..."); |
189 | sleep(10); | |
190 | ||
6f5e3f35 | 191 | /* Let the kernel reap children for us */ |
6f5e3f35 | 192 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); |
8c43883a | 193 | |
cd3bd60a LP |
194 | pid = fork(); |
195 | if (pid < 0) | |
14212119 | 196 | log_error("Failed to fork off crash shell: %m"); |
6f5e3f35 | 197 | else if (pid == 0) { |
cd3bd60a | 198 | make_console_stdio(); |
6f5e3f35 LP |
199 | execl("/bin/sh", "/bin/sh", NULL); |
200 | ||
14212119 | 201 | log_error("execl() failed: %m"); |
6f5e3f35 LP |
202 | _exit(1); |
203 | } | |
c99b188e | 204 | |
ccd06097 | 205 | log_info("Successfully spawned crash shell as pid "PID_FMT".", pid); |
4fc935ca LP |
206 | } |
207 | ||
208 | log_info("Freezing execution."); | |
97c4f35c LP |
209 | freeze(); |
210 | } | |
211 | ||
212 | static void install_crash_handler(void) { | |
b92bea5d ZJS |
213 | struct sigaction sa = { |
214 | .sa_handler = crash, | |
215 | .sa_flags = SA_NODEFER, | |
216 | }; | |
97c4f35c | 217 | |
1b91d3e8 | 218 | sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1); |
97c4f35c | 219 | } |
f170852a | 220 | |
843d2643 LP |
221 | static int console_setup(bool do_reset) { |
222 | int tty_fd, r; | |
80876c20 | 223 | |
843d2643 LP |
224 | /* If we are init, we connect stdin/stdout/stderr to /dev/null |
225 | * and make sure we don't have a controlling tty. */ | |
80876c20 | 226 | |
843d2643 LP |
227 | release_terminal(); |
228 | ||
229 | if (!do_reset) | |
230 | return 0; | |
80876c20 | 231 | |
512947d4 MS |
232 | tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
233 | if (tty_fd < 0) { | |
843d2643 LP |
234 | log_error("Failed to open /dev/console: %s", strerror(-tty_fd)); |
235 | return -tty_fd; | |
236 | } | |
80876c20 | 237 | |
512947d4 MS |
238 | /* We don't want to force text mode. |
239 | * plymouth may be showing pictures already from initrd. */ | |
240 | r = reset_terminal_fd(tty_fd, false); | |
241 | if (r < 0) | |
843d2643 LP |
242 | log_error("Failed to reset /dev/console: %s", strerror(-r)); |
243 | ||
03e334a1 | 244 | safe_close(tty_fd); |
80876c20 LP |
245 | return r; |
246 | } | |
247 | ||
f170852a LP |
248 | static int set_default_unit(const char *u) { |
249 | char *c; | |
250 | ||
251 | assert(u); | |
252 | ||
bf4df7c3 LP |
253 | c = strdup(u); |
254 | if (!c) | |
f170852a LP |
255 | return -ENOMEM; |
256 | ||
fa0f4d8a LP |
257 | free(arg_default_unit); |
258 | arg_default_unit = c; | |
bf4df7c3 | 259 | |
f170852a LP |
260 | return 0; |
261 | } | |
262 | ||
059cb385 | 263 | static int parse_proc_cmdline_item(const char *key, const char *value) { |
f170852a LP |
264 | |
265 | static const char * const rlmap[] = { | |
ed370f5d | 266 | "emergency", SPECIAL_EMERGENCY_TARGET, |
099663ff | 267 | "-b", SPECIAL_EMERGENCY_TARGET, |
ed370f5d LP |
268 | "single", SPECIAL_RESCUE_TARGET, |
269 | "-s", SPECIAL_RESCUE_TARGET, | |
270 | "s", SPECIAL_RESCUE_TARGET, | |
271 | "S", SPECIAL_RESCUE_TARGET, | |
272 | "1", SPECIAL_RESCUE_TARGET, | |
273 | "2", SPECIAL_RUNLEVEL2_TARGET, | |
274 | "3", SPECIAL_RUNLEVEL3_TARGET, | |
275 | "4", SPECIAL_RUNLEVEL4_TARGET, | |
276 | "5", SPECIAL_RUNLEVEL5_TARGET, | |
f170852a | 277 | }; |
059cb385 | 278 | int r; |
f170852a | 279 | |
059cb385 | 280 | assert(key); |
5192bd19 | 281 | |
059cb385 | 282 | if (streq(key, "systemd.unit") && value) { |
bf4df7c3 LP |
283 | |
284 | if (!in_initrd()) | |
059cb385 | 285 | return set_default_unit(value); |
bf4df7c3 | 286 | |
059cb385 | 287 | } else if (streq(key, "rd.systemd.unit") && value) { |
bf4df7c3 | 288 | |
ca05941b LP |
289 | if (in_initrd()) |
290 | return set_default_unit(value); | |
f170852a | 291 | |
059cb385 | 292 | } else if (streq(key, "systemd.log_target") && value) { |
f170852a | 293 | |
059cb385 LP |
294 | if (log_set_target_from_string(value) < 0) |
295 | log_warning("Failed to parse log target %s. Ignoring.", value); | |
f170852a | 296 | |
059cb385 | 297 | } else if (streq(key, "systemd.log_level") && value) { |
f170852a | 298 | |
059cb385 LP |
299 | if (log_set_max_level_from_string(value) < 0) |
300 | log_warning("Failed to parse log level %s. Ignoring.", value); | |
f170852a | 301 | |
059cb385 | 302 | } else if (streq(key, "systemd.log_color") && value) { |
bbe63281 | 303 | |
059cb385 LP |
304 | if (log_show_color_from_string(value) < 0) |
305 | log_warning("Failed to parse log color setting %s. Ignoring.", value); | |
bbe63281 | 306 | |
059cb385 | 307 | } else if (streq(key, "systemd.log_location") && value) { |
bbe63281 | 308 | |
059cb385 LP |
309 | if (log_show_location_from_string(value) < 0) |
310 | log_warning("Failed to parse log location setting %s. Ignoring.", value); | |
bbe63281 | 311 | |
059cb385 | 312 | } else if (streq(key, "systemd.dump_core") && value) { |
4fc935ca | 313 | |
059cb385 | 314 | r = parse_boolean(value); |
fb472900 | 315 | if (r < 0) |
059cb385 | 316 | log_warning("Failed to parse dump core switch %s. Ignoring.", value); |
4fc935ca | 317 | else |
fa0f4d8a | 318 | arg_dump_core = r; |
4fc935ca | 319 | |
059cb385 | 320 | } else if (streq(key, "systemd.crash_shell") && value) { |
4fc935ca | 321 | |
059cb385 | 322 | r = parse_boolean(value); |
fb472900 | 323 | if (r < 0) |
059cb385 | 324 | log_warning("Failed to parse crash shell switch %s. Ignoring.", value); |
4fc935ca | 325 | else |
fa0f4d8a | 326 | arg_crash_shell = r; |
5e7ee61c | 327 | |
059cb385 | 328 | } else if (streq(key, "systemd.crash_chvt") && value) { |
5e7ee61c | 329 | |
059cb385 LP |
330 | if (safe_atoi(value, &r) < 0) |
331 | log_warning("Failed to parse crash chvt switch %s. Ignoring.", value); | |
5e7ee61c | 332 | else |
059cb385 | 333 | arg_crash_chvt = r; |
5e7ee61c | 334 | |
059cb385 | 335 | } else if (streq(key, "systemd.confirm_spawn") && value) { |
601f6a1e | 336 | |
059cb385 LP |
337 | r = parse_boolean(value); |
338 | if (r < 0) | |
339 | log_warning("Failed to parse confirm spawn switch %s. Ignoring.", value); | |
601f6a1e | 340 | else |
059cb385 | 341 | arg_confirm_spawn = r; |
601f6a1e | 342 | |
059cb385 | 343 | } else if (streq(key, "systemd.show_status") && value) { |
9e58ff9c | 344 | |
059cb385 | 345 | r = parse_show_status(value, &arg_show_status); |
d450b6f2 | 346 | if (r < 0) |
059cb385 LP |
347 | log_warning("Failed to parse show status switch %s. Ignoring.", value); |
348 | ||
349 | } else if (streq(key, "systemd.default_standard_output") && value) { | |
0a494f1f | 350 | |
059cb385 | 351 | r = exec_output_from_string(value); |
fb472900 | 352 | if (r < 0) |
059cb385 | 353 | log_warning("Failed to parse default standard output switch %s. Ignoring.", value); |
0a494f1f LP |
354 | else |
355 | arg_default_std_output = r; | |
0a494f1f | 356 | |
059cb385 LP |
357 | } else if (streq(key, "systemd.default_standard_error") && value) { |
358 | ||
359 | r = exec_output_from_string(value); | |
fb472900 | 360 | if (r < 0) |
059cb385 | 361 | log_warning("Failed to parse default standard error switch %s. Ignoring.", value); |
0a494f1f LP |
362 | else |
363 | arg_default_std_error = r; | |
9e7c5357 | 364 | |
059cb385 LP |
365 | } else if (streq(key, "systemd.setenv") && value) { |
366 | ||
367 | if (env_assignment_is_valid(value)) { | |
e21fea24 KS |
368 | char **env; |
369 | ||
059cb385 | 370 | env = strv_env_set(arg_default_environment, value); |
e21fea24 KS |
371 | if (env) |
372 | arg_default_environment = env; | |
373 | else | |
059cb385 | 374 | log_warning("Setting environment variable '%s' failed, ignoring: %s", value, strerror(ENOMEM)); |
e21fea24 | 375 | } else |
059cb385 | 376 | log_warning("Environment variable name '%s' is not valid. Ignoring.", value); |
9e58ff9c | 377 | |
059cb385 | 378 | } else if (streq(key, "quiet") && !value) { |
d7b15e0a LP |
379 | |
380 | log_set_max_level(LOG_NOTICE); | |
381 | ||
b6e2f329 | 382 | if (arg_show_status == _SHOW_STATUS_UNSET) |
d450b6f2 | 383 | arg_show_status = SHOW_STATUS_AUTO; |
059cb385 LP |
384 | |
385 | } else if (streq(key, "debug") && !value) { | |
d7b15e0a | 386 | |
9749cd77 | 387 | log_set_max_level(LOG_DEBUG); |
d7b15e0a | 388 | |
b2103dcc KS |
389 | if (detect_container(NULL) > 0) |
390 | log_set_target(LOG_TARGET_CONSOLE); | |
059cb385 LP |
391 | |
392 | } else if (!in_initrd() && !value) { | |
f170852a LP |
393 | unsigned i; |
394 | ||
395 | /* SysV compatibility */ | |
f170852a | 396 | for (i = 0; i < ELEMENTSOF(rlmap); i += 2) |
059cb385 | 397 | if (streq(key, rlmap[i])) |
f170852a LP |
398 | return set_default_unit(rlmap[i+1]); |
399 | } | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
e8e581bf ZJS |
404 | #define DEFINE_SETTER(name, func, descr) \ |
405 | static int name(const char *unit, \ | |
406 | const char *filename, \ | |
407 | unsigned line, \ | |
408 | const char *section, \ | |
71a61510 | 409 | unsigned section_line, \ |
e8e581bf ZJS |
410 | const char *lvalue, \ |
411 | int ltype, \ | |
412 | const char *rvalue, \ | |
413 | void *data, \ | |
414 | void *userdata) { \ | |
415 | \ | |
416 | int r; \ | |
417 | \ | |
418 | assert(filename); \ | |
419 | assert(lvalue); \ | |
420 | assert(rvalue); \ | |
421 | \ | |
422 | r = func(rvalue); \ | |
423 | if (r < 0) \ | |
424 | log_syntax(unit, LOG_ERR, filename, line, -r, \ | |
425 | "Invalid " descr "'%s': %s", \ | |
426 | rvalue, strerror(-r)); \ | |
427 | \ | |
428 | return 0; \ | |
429 | } | |
487393e9 | 430 | |
e8e581bf ZJS |
431 | DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level") |
432 | DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target") | |
433 | DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" ) | |
434 | DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location") | |
487393e9 | 435 | |
4d7213b2 LP |
436 | static int config_parse_cpu_affinity2( |
437 | const char *unit, | |
438 | const char *filename, | |
439 | unsigned line, | |
440 | const char *section, | |
441 | unsigned section_line, | |
442 | const char *lvalue, | |
443 | int ltype, | |
444 | const char *rvalue, | |
445 | void *data, | |
446 | void *userdata) { | |
487393e9 LP |
447 | |
448 | char *w; | |
449 | size_t l; | |
450 | char *state; | |
451 | cpu_set_t *c = NULL; | |
452 | unsigned ncpus = 0; | |
453 | ||
454 | assert(filename); | |
455 | assert(lvalue); | |
456 | assert(rvalue); | |
457 | ||
f60f22df | 458 | FOREACH_WORD_QUOTED(w, l, rvalue, state) { |
487393e9 LP |
459 | char *t; |
460 | int r; | |
461 | unsigned cpu; | |
462 | ||
463 | if (!(t = strndup(w, l))) | |
14212119 | 464 | return log_oom(); |
487393e9 LP |
465 | |
466 | r = safe_atou(t, &cpu); | |
467 | free(t); | |
468 | ||
469 | if (!c) | |
470 | if (!(c = cpu_set_malloc(&ncpus))) | |
14212119 | 471 | return log_oom(); |
487393e9 LP |
472 | |
473 | if (r < 0 || cpu >= ncpus) { | |
e8e581bf ZJS |
474 | log_syntax(unit, LOG_ERR, filename, line, -r, |
475 | "Failed to parse CPU affinity '%s'", rvalue); | |
487393e9 LP |
476 | CPU_FREE(c); |
477 | return -EBADMSG; | |
478 | } | |
479 | ||
480 | CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); | |
481 | } | |
482 | ||
483 | if (c) { | |
484 | if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0) | |
e8e581bf | 485 | log_warning_unit(unit, "Failed to set CPU affinity: %m"); |
487393e9 LP |
486 | |
487 | CPU_FREE(c); | |
488 | } | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
4d7213b2 LP |
493 | static int config_parse_show_status( |
494 | const char* unit, | |
495 | const char *filename, | |
496 | unsigned line, | |
497 | const char *section, | |
498 | unsigned section_line, | |
499 | const char *lvalue, | |
500 | int ltype, | |
501 | const char *rvalue, | |
502 | void *data, | |
503 | void *userdata) { | |
504 | ||
505 | int k; | |
506 | ShowStatus *b = data; | |
507 | ||
508 | assert(filename); | |
509 | assert(lvalue); | |
510 | assert(rvalue); | |
511 | assert(data); | |
512 | ||
513 | k = parse_show_status(rvalue, b); | |
514 | if (k < 0) { | |
515 | log_syntax(unit, LOG_ERR, filename, line, -k, | |
516 | "Failed to parse show status setting, ignoring: %s", rvalue); | |
517 | return 0; | |
518 | } | |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
0c85a4f3 LP |
523 | static void strv_free_free(char ***l) { |
524 | char ***i; | |
525 | ||
526 | if (!l) | |
527 | return; | |
528 | ||
529 | for (i = l; *i; i++) | |
530 | strv_free(*i); | |
531 | ||
532 | free(l); | |
533 | } | |
534 | ||
535 | static void free_join_controllers(void) { | |
0c85a4f3 LP |
536 | strv_free_free(arg_join_controllers); |
537 | arg_join_controllers = NULL; | |
538 | } | |
539 | ||
e8e581bf ZJS |
540 | static int config_parse_join_controllers(const char *unit, |
541 | const char *filename, | |
542 | unsigned line, | |
543 | const char *section, | |
71a61510 | 544 | unsigned section_line, |
e8e581bf ZJS |
545 | const char *lvalue, |
546 | int ltype, | |
547 | const char *rvalue, | |
548 | void *data, | |
549 | void *userdata) { | |
0c85a4f3 LP |
550 | |
551 | unsigned n = 0; | |
552 | char *state, *w; | |
553 | size_t length; | |
554 | ||
555 | assert(filename); | |
556 | assert(lvalue); | |
557 | assert(rvalue); | |
558 | ||
559 | free_join_controllers(); | |
560 | ||
561 | FOREACH_WORD_QUOTED(w, length, rvalue, state) { | |
562 | char *s, **l; | |
563 | ||
564 | s = strndup(w, length); | |
565 | if (!s) | |
14212119 | 566 | return log_oom(); |
0c85a4f3 LP |
567 | |
568 | l = strv_split(s, ","); | |
569 | free(s); | |
570 | ||
571 | strv_uniq(l); | |
572 | ||
573 | if (strv_length(l) <= 1) { | |
574 | strv_free(l); | |
575 | continue; | |
576 | } | |
577 | ||
578 | if (!arg_join_controllers) { | |
579 | arg_join_controllers = new(char**, 2); | |
580 | if (!arg_join_controllers) { | |
581 | strv_free(l); | |
14212119 | 582 | return log_oom(); |
0c85a4f3 LP |
583 | } |
584 | ||
585 | arg_join_controllers[0] = l; | |
586 | arg_join_controllers[1] = NULL; | |
587 | ||
588 | n = 1; | |
589 | } else { | |
590 | char ***a; | |
591 | char ***t; | |
592 | ||
593 | t = new0(char**, n+2); | |
594 | if (!t) { | |
595 | strv_free(l); | |
14212119 | 596 | return log_oom(); |
0c85a4f3 LP |
597 | } |
598 | ||
599 | n = 0; | |
600 | ||
601 | for (a = arg_join_controllers; *a; a++) { | |
602 | ||
603 | if (strv_overlap(*a, l)) { | |
e3e45d4f | 604 | if (strv_extend_strv(&l, *a) < 0) { |
0c85a4f3 LP |
605 | strv_free(l); |
606 | strv_free_free(t); | |
14212119 | 607 | return log_oom(); |
0c85a4f3 LP |
608 | } |
609 | ||
0c85a4f3 LP |
610 | } else { |
611 | char **c; | |
612 | ||
613 | c = strv_copy(*a); | |
614 | if (!c) { | |
615 | strv_free(l); | |
616 | strv_free_free(t); | |
14212119 | 617 | return log_oom(); |
0c85a4f3 LP |
618 | } |
619 | ||
620 | t[n++] = c; | |
621 | } | |
622 | } | |
623 | ||
624 | t[n++] = strv_uniq(l); | |
625 | ||
626 | strv_free_free(arg_join_controllers); | |
627 | arg_join_controllers = t; | |
628 | } | |
629 | } | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
487393e9 LP |
634 | static int parse_config_file(void) { |
635 | ||
f975e971 | 636 | const ConfigTableItem items[] = { |
d3b1c508 LP |
637 | { "Manager", "LogLevel", config_parse_level2, 0, NULL }, |
638 | { "Manager", "LogTarget", config_parse_target, 0, NULL }, | |
639 | { "Manager", "LogColor", config_parse_color, 0, NULL }, | |
640 | { "Manager", "LogLocation", config_parse_location, 0, NULL }, | |
641 | { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core }, | |
642 | { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell }, | |
643 | { "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status }, | |
644 | { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt }, | |
645 | { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL }, | |
646 | { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers }, | |
647 | { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog }, | |
648 | { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog }, | |
649 | { "Manager", "CapabilityBoundingSet", config_parse_bounding_set, 0, &arg_capability_bounding_set_drop }, | |
89fffa27 | 650 | #ifdef HAVE_SECCOMP |
d3b1c508 | 651 | { "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs }, |
89fffa27 | 652 | #endif |
d3b1c508 | 653 | { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec }, |
bd8f585b | 654 | { "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec }, |
d3b1c508 LP |
655 | { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output }, |
656 | { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error }, | |
657 | { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec }, | |
658 | { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec }, | |
659 | { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec }, | |
660 | { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval }, | |
661 | { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst }, | |
662 | { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment }, | |
663 | { "Manager", "DefaultLimitCPU", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CPU] }, | |
664 | { "Manager", "DefaultLimitFSIZE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_FSIZE] }, | |
665 | { "Manager", "DefaultLimitDATA", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_DATA] }, | |
666 | { "Manager", "DefaultLimitSTACK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_STACK] }, | |
667 | { "Manager", "DefaultLimitCORE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_CORE] }, | |
668 | { "Manager", "DefaultLimitRSS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RSS] }, | |
669 | { "Manager", "DefaultLimitNOFILE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NOFILE] }, | |
670 | { "Manager", "DefaultLimitAS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_AS] }, | |
671 | { "Manager", "DefaultLimitNPROC", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NPROC] }, | |
672 | { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MEMLOCK] }, | |
673 | { "Manager", "DefaultLimitLOCKS", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_LOCKS] }, | |
674 | { "Manager", "DefaultLimitSIGPENDING", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_SIGPENDING] }, | |
675 | { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_MSGQUEUE] }, | |
676 | { "Manager", "DefaultLimitNICE", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_NICE] }, | |
677 | { "Manager", "DefaultLimitRTPRIO", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTPRIO] }, | |
678 | { "Manager", "DefaultLimitRTTIME", config_parse_limit, 0, &arg_default_rlimit[RLIMIT_RTTIME] }, | |
085afe36 LP |
679 | { "Manager", "DefaultCPUAccounting", config_parse_bool, 0, &arg_default_cpu_accounting }, |
680 | { "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting }, | |
681 | { "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting }, | |
d3b1c508 | 682 | {} |
487393e9 LP |
683 | }; |
684 | ||
7fd1b19b | 685 | _cleanup_fclose_ FILE *f; |
487393e9 LP |
686 | const char *fn; |
687 | int r; | |
688 | ||
19adb8a3 | 689 | fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf"; |
f975e971 LP |
690 | f = fopen(fn, "re"); |
691 | if (!f) { | |
487393e9 LP |
692 | if (errno == ENOENT) |
693 | return 0; | |
694 | ||
695 | log_warning("Failed to open configuration file '%s': %m", fn); | |
696 | return 0; | |
697 | } | |
698 | ||
e9f3d2d5 | 699 | r = config_parse(NULL, fn, f, "Manager\0", config_item_table_lookup, items, false, false, NULL); |
f975e971 | 700 | if (r < 0) |
487393e9 LP |
701 | log_warning("Failed to parse configuration file: %s", strerror(-r)); |
702 | ||
487393e9 LP |
703 | return 0; |
704 | } | |
705 | ||
f170852a LP |
706 | static int parse_argv(int argc, char *argv[]) { |
707 | ||
708 | enum { | |
709 | ARG_LOG_LEVEL = 0x100, | |
710 | ARG_LOG_TARGET, | |
bbe63281 LP |
711 | ARG_LOG_COLOR, |
712 | ARG_LOG_LOCATION, | |
2f198e2f | 713 | ARG_UNIT, |
edb9aaa8 | 714 | ARG_SYSTEM, |
af2d49f7 | 715 | ARG_USER, |
e537352b | 716 | ARG_TEST, |
9ba0bc4e | 717 | ARG_VERSION, |
80876c20 | 718 | ARG_DUMP_CONFIGURATION_ITEMS, |
9e58ff9c LP |
719 | ARG_DUMP_CORE, |
720 | ARG_CRASH_SHELL, | |
a16e1123 | 721 | ARG_CONFIRM_SPAWN, |
9e58ff9c | 722 | ARG_SHOW_STATUS, |
4288f619 | 723 | ARG_DESERIALIZE, |
2660882b | 724 | ARG_SWITCHED_ROOT, |
0a494f1f LP |
725 | ARG_DEFAULT_STD_OUTPUT, |
726 | ARG_DEFAULT_STD_ERROR | |
f170852a LP |
727 | }; |
728 | ||
729 | static const struct option options[] = { | |
a16e1123 LP |
730 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, |
731 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
bbe63281 LP |
732 | { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, |
733 | { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, | |
2f198e2f | 734 | { "unit", required_argument, NULL, ARG_UNIT }, |
edb9aaa8 | 735 | { "system", no_argument, NULL, ARG_SYSTEM }, |
af2d49f7 | 736 | { "user", no_argument, NULL, ARG_USER }, |
a16e1123 LP |
737 | { "test", no_argument, NULL, ARG_TEST }, |
738 | { "help", no_argument, NULL, 'h' }, | |
9ba0bc4e | 739 | { "version", no_argument, NULL, ARG_VERSION }, |
a16e1123 | 740 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, |
a5d87bf0 LP |
741 | { "dump-core", optional_argument, NULL, ARG_DUMP_CORE }, |
742 | { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL }, | |
743 | { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN }, | |
6e98720f | 744 | { "show-status", optional_argument, NULL, ARG_SHOW_STATUS }, |
a16e1123 | 745 | { "deserialize", required_argument, NULL, ARG_DESERIALIZE }, |
2660882b | 746 | { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT }, |
0a494f1f LP |
747 | { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, }, |
748 | { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, }, | |
fb472900 | 749 | {} |
f170852a LP |
750 | }; |
751 | ||
752 | int c, r; | |
753 | ||
754 | assert(argc >= 1); | |
755 | assert(argv); | |
756 | ||
b770165a LP |
757 | if (getpid() == 1) |
758 | opterr = 0; | |
759 | ||
099663ff | 760 | while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0) |
f170852a LP |
761 | |
762 | switch (c) { | |
763 | ||
764 | case ARG_LOG_LEVEL: | |
fb472900 ZJS |
765 | r = log_set_max_level_from_string(optarg); |
766 | if (r < 0) { | |
f170852a LP |
767 | log_error("Failed to parse log level %s.", optarg); |
768 | return r; | |
769 | } | |
770 | ||
771 | break; | |
772 | ||
773 | case ARG_LOG_TARGET: | |
fb472900 ZJS |
774 | r = log_set_target_from_string(optarg); |
775 | if (r < 0) { | |
f170852a LP |
776 | log_error("Failed to parse log target %s.", optarg); |
777 | return r; | |
778 | } | |
779 | ||
780 | break; | |
781 | ||
bbe63281 LP |
782 | case ARG_LOG_COLOR: |
783 | ||
d0b170c8 | 784 | if (optarg) { |
fb472900 ZJS |
785 | r = log_show_color_from_string(optarg); |
786 | if (r < 0) { | |
d0b170c8 LP |
787 | log_error("Failed to parse log color setting %s.", optarg); |
788 | return r; | |
789 | } | |
790 | } else | |
791 | log_show_color(true); | |
bbe63281 LP |
792 | |
793 | break; | |
794 | ||
795 | case ARG_LOG_LOCATION: | |
d0b170c8 | 796 | if (optarg) { |
fb472900 ZJS |
797 | r = log_show_location_from_string(optarg); |
798 | if (r < 0) { | |
d0b170c8 LP |
799 | log_error("Failed to parse log location setting %s.", optarg); |
800 | return r; | |
801 | } | |
802 | } else | |
803 | log_show_location(true); | |
bbe63281 LP |
804 | |
805 | break; | |
806 | ||
0a494f1f | 807 | case ARG_DEFAULT_STD_OUTPUT: |
fb472900 ZJS |
808 | r = exec_output_from_string(optarg); |
809 | if (r < 0) { | |
0a494f1f LP |
810 | log_error("Failed to parse default standard output setting %s.", optarg); |
811 | return r; | |
812 | } else | |
813 | arg_default_std_output = r; | |
814 | break; | |
815 | ||
816 | case ARG_DEFAULT_STD_ERROR: | |
fb472900 ZJS |
817 | r = exec_output_from_string(optarg); |
818 | if (r < 0) { | |
0a494f1f LP |
819 | log_error("Failed to parse default standard error output setting %s.", optarg); |
820 | return r; | |
821 | } else | |
822 | arg_default_std_error = r; | |
823 | break; | |
824 | ||
2f198e2f | 825 | case ARG_UNIT: |
f170852a | 826 | |
fb472900 ZJS |
827 | r = set_default_unit(optarg); |
828 | if (r < 0) { | |
f170852a LP |
829 | log_error("Failed to set default unit %s: %s", optarg, strerror(-r)); |
830 | return r; | |
831 | } | |
832 | ||
833 | break; | |
834 | ||
edb9aaa8 | 835 | case ARG_SYSTEM: |
67445f4e | 836 | arg_running_as = SYSTEMD_SYSTEM; |
edb9aaa8 | 837 | break; |
a5dab5ce | 838 | |
af2d49f7 | 839 | case ARG_USER: |
67445f4e | 840 | arg_running_as = SYSTEMD_USER; |
a5dab5ce | 841 | break; |
a5dab5ce | 842 | |
e965d56d | 843 | case ARG_TEST: |
fa0f4d8a | 844 | arg_action = ACTION_TEST; |
e965d56d LP |
845 | break; |
846 | ||
9ba0bc4e ZJS |
847 | case ARG_VERSION: |
848 | arg_action = ACTION_VERSION; | |
849 | break; | |
850 | ||
e537352b | 851 | case ARG_DUMP_CONFIGURATION_ITEMS: |
fa0f4d8a | 852 | arg_action = ACTION_DUMP_CONFIGURATION_ITEMS; |
e537352b LP |
853 | break; |
854 | ||
9e58ff9c | 855 | case ARG_DUMP_CORE: |
a5d87bf0 LP |
856 | r = optarg ? parse_boolean(optarg) : 1; |
857 | if (r < 0) { | |
858 | log_error("Failed to parse dump core boolean %s.", optarg); | |
859 | return r; | |
860 | } | |
861 | arg_dump_core = r; | |
9e58ff9c LP |
862 | break; |
863 | ||
864 | case ARG_CRASH_SHELL: | |
a5d87bf0 LP |
865 | r = optarg ? parse_boolean(optarg) : 1; |
866 | if (r < 0) { | |
867 | log_error("Failed to parse crash shell boolean %s.", optarg); | |
868 | return r; | |
869 | } | |
870 | arg_crash_shell = r; | |
9e58ff9c LP |
871 | break; |
872 | ||
80876c20 | 873 | case ARG_CONFIRM_SPAWN: |
a5d87bf0 LP |
874 | r = optarg ? parse_boolean(optarg) : 1; |
875 | if (r < 0) { | |
876 | log_error("Failed to parse confirm spawn boolean %s.", optarg); | |
877 | return r; | |
878 | } | |
879 | arg_confirm_spawn = r; | |
80876c20 LP |
880 | break; |
881 | ||
9e58ff9c | 882 | case ARG_SHOW_STATUS: |
d450b6f2 ZJS |
883 | if (optarg) { |
884 | r = parse_show_status(optarg, &arg_show_status); | |
885 | if (r < 0) { | |
886 | log_error("Failed to parse show status boolean %s.", optarg); | |
887 | return r; | |
888 | } | |
889 | } else | |
890 | arg_show_status = SHOW_STATUS_YES; | |
6e98720f | 891 | break; |
a5d87bf0 | 892 | |
a16e1123 LP |
893 | case ARG_DESERIALIZE: { |
894 | int fd; | |
895 | FILE *f; | |
896 | ||
01e10de3 LP |
897 | r = safe_atoi(optarg, &fd); |
898 | if (r < 0 || fd < 0) { | |
a16e1123 | 899 | log_error("Failed to parse deserialize option %s.", optarg); |
01e10de3 | 900 | return r < 0 ? r : -EINVAL; |
a16e1123 LP |
901 | } |
902 | ||
01e10de3 LP |
903 | fd_cloexec(fd, true); |
904 | ||
905 | f = fdopen(fd, "r"); | |
906 | if (!f) { | |
a16e1123 | 907 | log_error("Failed to open serialization fd: %m"); |
01e10de3 | 908 | return -errno; |
a16e1123 LP |
909 | } |
910 | ||
d3b1c508 LP |
911 | if (arg_serialization) |
912 | fclose(arg_serialization); | |
a16e1123 | 913 | |
d3b1c508 | 914 | arg_serialization = f; |
a16e1123 LP |
915 | |
916 | break; | |
917 | } | |
918 | ||
2660882b | 919 | case ARG_SWITCHED_ROOT: |
bf4df7c3 | 920 | arg_switched_root = true; |
d03bc1b8 HH |
921 | break; |
922 | ||
f170852a | 923 | case 'h': |
fa0f4d8a | 924 | arg_action = ACTION_HELP; |
f170852a LP |
925 | break; |
926 | ||
1d2e23ab LP |
927 | case 'D': |
928 | log_set_max_level(LOG_DEBUG); | |
929 | break; | |
930 | ||
099663ff LP |
931 | case 'b': |
932 | case 's': | |
933 | case 'z': | |
934 | /* Just to eat away the sysvinit kernel | |
935 | * cmdline args without getopt() error | |
936 | * messages that we'll parse in | |
937 | * parse_proc_cmdline_word() or ignore. */ | |
f170852a | 938 | |
099663ff | 939 | case '?': |
f170852a | 940 | default: |
099663ff LP |
941 | if (getpid() != 1) { |
942 | log_error("Unknown option code %c", c); | |
943 | return -EINVAL; | |
944 | } | |
945 | ||
946 | break; | |
f170852a LP |
947 | } |
948 | ||
d821e6d6 LP |
949 | if (optind < argc && getpid() != 1) { |
950 | /* Hmm, when we aren't run as init system | |
951 | * let's complain about excess arguments */ | |
952 | ||
953 | log_error("Excess arguments."); | |
954 | return -EINVAL; | |
955 | } | |
956 | ||
957 | if (detect_container(NULL) > 0) { | |
958 | char **a; | |
959 | ||
960 | /* All /proc/cmdline arguments the kernel didn't | |
961 | * understand it passed to us. We're not really | |
962 | * interested in that usually since /proc/cmdline is | |
963 | * more interesting and complete. With one exception: | |
964 | * if we are run in a container /proc/cmdline is not | |
965 | * relevant for the container, hence we rely on argv[] | |
966 | * instead. */ | |
967 | ||
74df0fca | 968 | for (a = argv; a < argv + argc; a++) { |
059cb385 LP |
969 | _cleanup_free_ char *w; |
970 | char *value; | |
971 | ||
972 | w = strdup(*a); | |
973 | if (!w) | |
974 | return log_oom(); | |
975 | ||
976 | value = strchr(w, '='); | |
977 | if (value) | |
978 | *(value++) = 0; | |
979 | ||
980 | r = parse_proc_cmdline_item(w, value); | |
74df0fca | 981 | if (r < 0) { |
14212119 | 982 | log_error("Failed on cmdline argument %s: %s", *a, strerror(-r)); |
d821e6d6 | 983 | return r; |
14212119 | 984 | } |
74df0fca | 985 | } |
51f0e189 LP |
986 | } |
987 | ||
f170852a LP |
988 | return 0; |
989 | } | |
990 | ||
991 | static int help(void) { | |
992 | ||
2e33c433 | 993 | printf("%s [OPTIONS...]\n\n" |
af2d49f7 | 994 | "Starts up and maintains the system or user services.\n\n" |
e537352b | 995 | " -h --help Show this help\n" |
e537352b | 996 | " --test Determine startup sequence, dump it and exit\n" |
80876c20 | 997 | " --dump-configuration-items Dump understood unit configuration items\n" |
9e58ff9c | 998 | " --unit=UNIT Set default unit\n" |
edb9aaa8 | 999 | " --system Run a system instance, even if PID != 1\n" |
af2d49f7 | 1000 | " --user Run a user instance\n" |
a5d87bf0 LP |
1001 | " --dump-core[=0|1] Dump core on crash\n" |
1002 | " --crash-shell[=0|1] Run shell on crash\n" | |
1003 | " --confirm-spawn[=0|1] Ask for confirmation when spawning processes\n" | |
6e98720f | 1004 | " --show-status[=0|1] Show status updates on the console during bootup\n" |
4cfa2c99 | 1005 | " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n" |
9e58ff9c | 1006 | " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n" |
2218198b | 1007 | " --log-color[=0|1] Highlight important log messages\n" |
0a494f1f LP |
1008 | " --log-location[=0|1] Include code location in log messages\n" |
1009 | " --default-standard-output= Set default standard output for services\n" | |
1010 | " --default-standard-error= Set default standard error output for services\n", | |
5b6319dc | 1011 | program_invocation_short_name); |
f170852a LP |
1012 | |
1013 | return 0; | |
1014 | } | |
1015 | ||
9ba0bc4e ZJS |
1016 | static int version(void) { |
1017 | puts(PACKAGE_STRING); | |
9ba0bc4e ZJS |
1018 | puts(SYSTEMD_FEATURES); |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
b3680f49 | 1023 | static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) { |
a16e1123 LP |
1024 | FILE *f = NULL; |
1025 | FDSet *fds = NULL; | |
1026 | int r; | |
1027 | ||
1028 | assert(m); | |
1029 | assert(_f); | |
1030 | assert(_fds); | |
1031 | ||
6b78f9b4 LP |
1032 | r = manager_open_serialization(m, &f); |
1033 | if (r < 0) { | |
35b8ca3a | 1034 | log_error("Failed to create serialization file: %s", strerror(-r)); |
a16e1123 LP |
1035 | goto fail; |
1036 | } | |
1037 | ||
71445ae7 LP |
1038 | /* Make sure nothing is really destructed when we shut down */ |
1039 | m->n_reloading ++; | |
718db961 | 1040 | bus_manager_send_reloading(m, true); |
71445ae7 | 1041 | |
6b78f9b4 LP |
1042 | fds = fdset_new(); |
1043 | if (!fds) { | |
a16e1123 LP |
1044 | r = -ENOMEM; |
1045 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
1046 | goto fail; | |
1047 | } | |
1048 | ||
b3680f49 | 1049 | r = manager_serialize(m, f, fds, switching_root); |
6b78f9b4 | 1050 | if (r < 0) { |
a16e1123 LP |
1051 | log_error("Failed to serialize state: %s", strerror(-r)); |
1052 | goto fail; | |
1053 | } | |
1054 | ||
1055 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
1056 | log_error("Failed to rewind serialization fd: %m"); | |
1057 | goto fail; | |
1058 | } | |
1059 | ||
6b78f9b4 LP |
1060 | r = fd_cloexec(fileno(f), false); |
1061 | if (r < 0) { | |
a16e1123 LP |
1062 | log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r)); |
1063 | goto fail; | |
1064 | } | |
1065 | ||
6b78f9b4 LP |
1066 | r = fdset_cloexec(fds, false); |
1067 | if (r < 0) { | |
a16e1123 LP |
1068 | log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r)); |
1069 | goto fail; | |
1070 | } | |
1071 | ||
1072 | *_f = f; | |
1073 | *_fds = fds; | |
1074 | ||
1075 | return 0; | |
1076 | ||
1077 | fail: | |
1078 | fdset_free(fds); | |
1079 | ||
1080 | if (f) | |
1081 | fclose(f); | |
1082 | ||
1083 | return r; | |
1084 | } | |
1085 | ||
4096d6f5 LP |
1086 | static int bump_rlimit_nofile(struct rlimit *saved_rlimit) { |
1087 | struct rlimit nl; | |
1088 | int r; | |
1089 | ||
1090 | assert(saved_rlimit); | |
1091 | ||
1092 | /* Save the original RLIMIT_NOFILE so that we can reset it | |
1093 | * later when transitioning from the initrd to the main | |
1094 | * systemd or suchlike. */ | |
1095 | if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0) { | |
1096 | log_error("Reading RLIMIT_NOFILE failed: %m"); | |
1097 | return -errno; | |
1098 | } | |
1099 | ||
1100 | /* Make sure forked processes get the default kernel setting */ | |
1101 | if (!arg_default_rlimit[RLIMIT_NOFILE]) { | |
1102 | struct rlimit *rl; | |
1103 | ||
1104 | rl = newdup(struct rlimit, saved_rlimit, 1); | |
1105 | if (!rl) | |
1106 | return log_oom(); | |
1107 | ||
1108 | arg_default_rlimit[RLIMIT_NOFILE] = rl; | |
1109 | } | |
1110 | ||
1111 | /* Bump up the resource limit for ourselves substantially */ | |
1112 | nl.rlim_cur = nl.rlim_max = 64*1024; | |
1113 | r = setrlimit_closest(RLIMIT_NOFILE, &nl); | |
1114 | if (r < 0) { | |
1115 | log_error("Setting RLIMIT_NOFILE failed: %s", strerror(-r)); | |
1116 | return r; | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
6ee5bbf8 | 1122 | static void test_mtab(void) { |
6ee5bbf8 | 1123 | |
5b4c0131 LP |
1124 | static const char ok[] = |
1125 | "/proc/self/mounts\0" | |
1126 | "/proc/mounts\0" | |
1127 | "../proc/self/mounts\0" | |
1128 | "../proc/mounts\0"; | |
80758717 | 1129 | |
5b4c0131 LP |
1130 | _cleanup_free_ char *p = NULL; |
1131 | int r; | |
6ee5bbf8 | 1132 | |
5b4c0131 LP |
1133 | /* Check that /etc/mtab is a symlink to the right place or |
1134 | * non-existing. But certainly not a file, or a symlink to | |
1135 | * some weird place... */ | |
6ee5bbf8 | 1136 | |
5b4c0131 LP |
1137 | r = readlink_malloc("/etc/mtab", &p); |
1138 | if (r == -ENOENT) | |
1139 | return; | |
1140 | if (r >= 0 && nulstr_contains(ok, p)) | |
1141 | return; | |
6ee5bbf8 | 1142 | |
80758717 LP |
1143 | log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. " |
1144 | "This is not supported anymore. " | |
1145 | "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output."); | |
1146 | } | |
1147 | ||
1148 | static void test_usr(void) { | |
80758717 | 1149 | |
ed1c99fc | 1150 | /* Check that /usr is not a separate fs */ |
80758717 | 1151 | |
871c44a7 LP |
1152 | if (dir_is_empty("/usr") <= 0) |
1153 | return; | |
1154 | ||
2376ce13 | 1155 | log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. " |
871c44a7 LP |
1156 | "Some things will probably break (sometimes even silently) in mysterious ways. " |
1157 | "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information."); | |
1158 | } | |
1159 | ||
a07fdfa3 LP |
1160 | static int initialize_join_controllers(void) { |
1161 | /* By default, mount "cpu" + "cpuacct" together, and "net_cls" | |
1162 | * + "net_prio". We'd like to add "cpuset" to the mix, but | |
1163 | * "cpuset" does't really work for groups with no initialized | |
1164 | * attributes. */ | |
1165 | ||
1166 | arg_join_controllers = new(char**, 3); | |
1167 | if (!arg_join_controllers) | |
1168 | return -ENOMEM; | |
1169 | ||
1170 | arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL); | |
a07fdfa3 | 1171 | arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL); |
a6b26d90 ZJS |
1172 | arg_join_controllers[2] = NULL; |
1173 | ||
1174 | if (!arg_join_controllers[0] || !arg_join_controllers[1]) { | |
1175 | free_join_controllers(); | |
a07fdfa3 | 1176 | return -ENOMEM; |
a6b26d90 | 1177 | } |
a07fdfa3 | 1178 | |
a07fdfa3 LP |
1179 | return 0; |
1180 | } | |
1181 | ||
d3b1c508 LP |
1182 | static int enforce_syscall_archs(Set *archs) { |
1183 | #ifdef HAVE_SECCOMP | |
1184 | scmp_filter_ctx *seccomp; | |
1185 | Iterator i; | |
1186 | void *id; | |
1187 | int r; | |
1188 | ||
1189 | seccomp = seccomp_init(SCMP_ACT_ALLOW); | |
1190 | if (!seccomp) | |
1191 | return log_oom(); | |
1192 | ||
1193 | SET_FOREACH(id, arg_syscall_archs, i) { | |
1194 | r = seccomp_arch_add(seccomp, PTR_TO_UINT32(id) - 1); | |
1195 | if (r == -EEXIST) | |
1196 | continue; | |
1197 | if (r < 0) { | |
1198 | log_error("Failed to add architecture to seccomp: %s", strerror(-r)); | |
1199 | goto finish; | |
1200 | } | |
1201 | } | |
1202 | ||
8a8bf3c0 LP |
1203 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); |
1204 | if (r < 0) { | |
1205 | log_error("Failed to unset NO_NEW_PRIVS: %s", strerror(-r)); | |
1206 | goto finish; | |
1207 | } | |
1208 | ||
d3b1c508 LP |
1209 | r = seccomp_load(seccomp); |
1210 | if (r < 0) | |
1211 | log_error("Failed to add install architecture seccomp: %s", strerror(-r)); | |
1212 | ||
1213 | finish: | |
1214 | seccomp_release(seccomp); | |
1215 | return r; | |
1216 | #else | |
1217 | return 0; | |
1218 | #endif | |
1219 | } | |
1220 | ||
b6e2f329 LP |
1221 | static int status_welcome(void) { |
1222 | _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL; | |
1223 | int r; | |
1224 | ||
1225 | r = parse_env_file("/etc/os-release", NEWLINE, | |
1226 | "PRETTY_NAME", &pretty_name, | |
1227 | "ANSI_COLOR", &ansi_color, | |
1228 | NULL); | |
5ae4d543 LP |
1229 | if (r == -ENOENT) { |
1230 | r = parse_env_file("/usr/lib/os-release", NEWLINE, | |
1231 | "PRETTY_NAME", &pretty_name, | |
1232 | "ANSI_COLOR", &ansi_color, | |
1233 | NULL); | |
1234 | } | |
b6e2f329 LP |
1235 | |
1236 | if (r < 0 && r != -ENOENT) | |
5ae4d543 | 1237 | log_warning("Failed to read os-release file: %s", strerror(-r)); |
b6e2f329 LP |
1238 | |
1239 | return status_printf(NULL, false, false, | |
1240 | "\nWelcome to \x1B[%sm%s\x1B[0m!\n", | |
1241 | isempty(ansi_color) ? "1" : ansi_color, | |
1242 | isempty(pretty_name) ? "Linux" : pretty_name); | |
1243 | } | |
1244 | ||
fdd25311 LP |
1245 | static int write_container_id(void) { |
1246 | const char *c; | |
1247 | ||
1248 | c = getenv("container"); | |
1249 | if (isempty(c)) | |
1250 | return 0; | |
1251 | ||
1252 | return write_string_file("/run/systemd/container", c); | |
1253 | } | |
1254 | ||
60918275 LP |
1255 | int main(int argc, char *argv[]) { |
1256 | Manager *m = NULL; | |
22f4096c | 1257 | int r, retval = EXIT_FAILURE; |
9d76d730 LP |
1258 | usec_t before_startup, after_startup; |
1259 | char timespan[FORMAT_TIMESPAN_MAX]; | |
a16e1123 LP |
1260 | FDSet *fds = NULL; |
1261 | bool reexecute = false; | |
b9080b03 | 1262 | const char *shutdown_verb = NULL; |
e9ddabc2 | 1263 | dual_timestamp initrd_timestamp = { 0ULL, 0ULL }; |
c3a170f3 HH |
1264 | dual_timestamp userspace_timestamp = { 0ULL, 0ULL }; |
1265 | dual_timestamp kernel_timestamp = { 0ULL, 0ULL }; | |
c2e0d600 TA |
1266 | dual_timestamp security_start_timestamp = { 0ULL, 0ULL }; |
1267 | dual_timestamp security_finish_timestamp = { 0ULL, 0ULL }; | |
5d6b1584 | 1268 | static char systemd[] = "systemd"; |
2660882b | 1269 | bool skip_setup = false; |
d3b1c508 | 1270 | unsigned j; |
0b3325e7 | 1271 | bool loaded_policy = false; |
e96d6be7 | 1272 | bool arm_reboot_watchdog = false; |
bf4df7c3 | 1273 | bool queue_default_job = false; |
5f5c2f38 | 1274 | bool empty_etc = false; |
41669317 | 1275 | char *switch_root_dir = NULL, *switch_root_init = NULL; |
4096d6f5 | 1276 | static struct rlimit saved_rlimit_nofile = { 0, 0 }; |
27b14a22 | 1277 | |
058dc6f3 | 1278 | #ifdef HAVE_SYSV_COMPAT |
2cb1a60d | 1279 | if (getpid() != 1 && strstr(program_invocation_short_name, "init")) { |
35b8ca3a | 1280 | /* This is compatibility support for SysV, where |
2cb1a60d LP |
1281 | * calling init as a user is identical to telinit. */ |
1282 | ||
1283 | errno = -ENOENT; | |
1284 | execv(SYSTEMCTL_BINARY_PATH, argv); | |
1285 | log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m"); | |
1286 | return 1; | |
1287 | } | |
058dc6f3 | 1288 | #endif |
2cb1a60d | 1289 | |
c3a170f3 HH |
1290 | dual_timestamp_from_monotonic(&kernel_timestamp, 0); |
1291 | dual_timestamp_get(&userspace_timestamp); | |
1292 | ||
0b3325e7 LP |
1293 | /* Determine if this is a reexecution or normal bootup. We do |
1294 | * the full command line parsing much later, so let's just | |
1295 | * have a quick peek here. */ | |
db813c2a LP |
1296 | if (strv_find(argv+1, "--deserialize")) |
1297 | skip_setup = true; | |
0b3325e7 | 1298 | |
2660882b LP |
1299 | /* If we have switched root, do all the special setup |
1300 | * things */ | |
db813c2a LP |
1301 | if (strv_find(argv+1, "--switched-root")) |
1302 | skip_setup = false; | |
d03bc1b8 | 1303 | |
f3b6a3ed LP |
1304 | /* If we get started via the /sbin/init symlink then we are |
1305 | called 'init'. After a subsequent reexecution we are then | |
1306 | called 'systemd'. That is confusing, hence let's call us | |
1307 | systemd right-away. */ | |
f3b6a3ed LP |
1308 | program_invocation_short_name = systemd; |
1309 | prctl(PR_SET_NAME, systemd); | |
5d6b1584 | 1310 | |
9a0e6896 LP |
1311 | saved_argv = argv; |
1312 | saved_argc = argc; | |
f3b6a3ed | 1313 | |
2cc59dbf | 1314 | log_show_color(isatty(STDERR_FILENO) > 0); |
bbe63281 | 1315 | |
90dc8c2e MG |
1316 | /* Disable the umask logic */ |
1317 | if (getpid() == 1) | |
1318 | umask(0); | |
1319 | ||
a866073d | 1320 | if (getpid() == 1 && detect_container(NULL) <= 0) { |
4f8d551f | 1321 | |
a866073d | 1322 | /* Running outside of a container as PID 1 */ |
67445f4e | 1323 | arg_running_as = SYSTEMD_SYSTEM; |
a866073d LP |
1324 | make_null_stdio(); |
1325 | log_set_target(LOG_TARGET_KMSG); | |
1326 | log_open(); | |
1327 | ||
21bf2ab0 | 1328 | if (in_initrd()) |
c3a170f3 | 1329 | initrd_timestamp = userspace_timestamp; |
c3ba6250 | 1330 | |
2660882b | 1331 | if (!skip_setup) { |
8f838d8a | 1332 | mount_setup_early(); |
c2e0d600 | 1333 | dual_timestamp_get(&security_start_timestamp); |
0b3325e7 LP |
1334 | if (selinux_setup(&loaded_policy) < 0) |
1335 | goto finish; | |
81611586 RS |
1336 | if (ima_setup() < 0) |
1337 | goto finish; | |
e49d3c01 | 1338 | if (smack_setup(&loaded_policy) < 0) |
ffbd2c4d | 1339 | goto finish; |
c2e0d600 | 1340 | dual_timestamp_get(&security_finish_timestamp); |
81611586 | 1341 | } |
0b3325e7 | 1342 | |
e9a5ef7c | 1343 | if (label_init(NULL) < 0) |
0ff4cdd9 | 1344 | goto finish; |
7948c4df | 1345 | |
72edcff5 | 1346 | if (!skip_setup) { |
24efb112 | 1347 | if (clock_is_localtime() > 0) { |
0b3325e7 | 1348 | int min; |
7948c4df | 1349 | |
72edcff5 | 1350 | /* The first-time call to settimeofday() does a time warp in the kernel */ |
24efb112 | 1351 | r = clock_set_timezone(&min); |
0b3325e7 LP |
1352 | if (r < 0) |
1353 | log_error("Failed to apply local time delta, ignoring: %s", strerror(-r)); | |
1354 | else | |
1355 | log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min); | |
19e65613 KS |
1356 | } else if (!in_initrd()) { |
1357 | /* | |
1358 | * Do dummy first-time call to seal the kernel's time warp magic | |
1359 | * | |
1360 | * Do not call this this from inside the initrd. The initrd might not | |
1361 | * carry /etc/adjtime with LOCAL, but the real system could be set up | |
1362 | * that way. In such case, we need to delay the time-warp or the sealing | |
1363 | * until we reach the real system. | |
1364 | */ | |
24efb112 | 1365 | clock_reset_timezone(); |
871e5809 | 1366 | |
e9dd9f95 | 1367 | /* Tell the kernel our timezone */ |
24efb112 | 1368 | r = clock_set_timezone(NULL); |
72edcff5 | 1369 | if (r < 0) |
e9dd9f95 | 1370 | log_error("Failed to set the kernel's timezone, ignoring: %s", strerror(-r)); |
72edcff5 KS |
1371 | } |
1372 | } | |
a866073d LP |
1373 | |
1374 | /* Set the default for later on, but don't actually | |
1375 | * open the logs like this for now. Note that if we | |
1376 | * are transitioning from the initrd there might still | |
1377 | * be journal fd open, and we shouldn't attempt | |
1378 | * opening that before we parsed /proc/cmdline which | |
1379 | * might redirect output elsewhere. */ | |
1380 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
1381 | ||
1382 | } else if (getpid() == 1) { | |
a866073d | 1383 | /* Running inside a container, as PID 1 */ |
67445f4e | 1384 | arg_running_as = SYSTEMD_SYSTEM; |
a866073d | 1385 | log_set_target(LOG_TARGET_CONSOLE); |
99f09825 | 1386 | log_close_console(); /* force reopen of /dev/console */ |
a866073d LP |
1387 | log_open(); |
1388 | ||
1389 | /* For the later on, see above... */ | |
1390 | log_set_target(LOG_TARGET_JOURNAL); | |
1391 | ||
c3a170f3 HH |
1392 | /* clear the kernel timestamp, |
1393 | * because we are in a container */ | |
1394 | kernel_timestamp.monotonic = 0ULL; | |
1395 | kernel_timestamp.realtime = 0ULL; | |
a866073d | 1396 | |
c3a170f3 | 1397 | } else { |
a866073d | 1398 | /* Running as user instance */ |
67445f4e | 1399 | arg_running_as = SYSTEMD_USER; |
eeecf6e6 | 1400 | log_set_target(LOG_TARGET_AUTO); |
871e5809 | 1401 | log_open(); |
c3a170f3 HH |
1402 | |
1403 | /* clear the kernel timestamp, | |
1404 | * because we are not PID 1 */ | |
1405 | kernel_timestamp.monotonic = 0ULL; | |
1406 | kernel_timestamp.realtime = 0ULL; | |
bbe63281 | 1407 | } |
a5dab5ce | 1408 | |
0c85a4f3 | 1409 | /* Initialize default unit */ |
6afa301b LP |
1410 | r = set_default_unit(SPECIAL_DEFAULT_TARGET); |
1411 | if (r < 0) { | |
14212119 | 1412 | log_error("Failed to set default unit %s: %s", SPECIAL_DEFAULT_TARGET, strerror(-r)); |
f170852a | 1413 | goto finish; |
14212119 | 1414 | } |
60918275 | 1415 | |
a07fdfa3 LP |
1416 | r = initialize_join_controllers(); |
1417 | if (r < 0) | |
0c85a4f3 LP |
1418 | goto finish; |
1419 | ||
f170852a LP |
1420 | /* Mount /proc, /sys and friends, so that /proc/cmdline and |
1421 | * /proc/$PID/fd is available. */ | |
c1dae1b3 | 1422 | if (getpid() == 1) { |
0c85a4f3 LP |
1423 | r = mount_setup(loaded_policy); |
1424 | if (r < 0) | |
8efe3c01 | 1425 | goto finish; |
0c85a4f3 | 1426 | } |
4ade7963 LP |
1427 | |
1428 | /* Reset all signal handlers. */ | |
1429 | assert_se(reset_all_signal_handlers() == 0); | |
1430 | ||
9a34ec5f | 1431 | ignore_signals(SIGNALS_IGNORE, -1); |
078e4539 | 1432 | |
487393e9 LP |
1433 | if (parse_config_file() < 0) |
1434 | goto finish; | |
1435 | ||
67445f4e | 1436 | if (arg_running_as == SYSTEMD_SYSTEM) |
059cb385 | 1437 | if (parse_proc_cmdline(parse_proc_cmdline_item) < 0) |
a5dab5ce | 1438 | goto finish; |
f170852a LP |
1439 | |
1440 | log_parse_environment(); | |
1441 | ||
1442 | if (parse_argv(argc, argv) < 0) | |
1443 | goto finish; | |
1444 | ||
6bae23a0 TB |
1445 | if (arg_action == ACTION_TEST && |
1446 | geteuid() == 0) { | |
b5c6cf87 LP |
1447 | log_error("Don't run test mode as root."); |
1448 | goto finish; | |
1449 | } | |
1450 | ||
6bae23a0 TB |
1451 | if (arg_running_as == SYSTEMD_USER && |
1452 | arg_action == ACTION_RUN && | |
1453 | sd_booted() <= 0) { | |
1454 | log_error("Trying to run as user instance, but the system has not been booted with systemd."); | |
1455 | goto finish; | |
1456 | } | |
1457 | ||
67445f4e | 1458 | if (arg_running_as == SYSTEMD_SYSTEM && |
fe783b03 LP |
1459 | arg_action == ACTION_RUN && |
1460 | running_in_chroot() > 0) { | |
1461 | log_error("Cannot be run in a chroot() environment."); | |
1462 | goto finish; | |
1463 | } | |
1464 | ||
fa0f4d8a | 1465 | if (arg_action == ACTION_HELP) { |
f170852a LP |
1466 | retval = help(); |
1467 | goto finish; | |
9ba0bc4e ZJS |
1468 | } else if (arg_action == ACTION_VERSION) { |
1469 | retval = version(); | |
1470 | goto finish; | |
fa0f4d8a | 1471 | } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
e537352b | 1472 | unit_dump_config_items(stdout); |
22f4096c | 1473 | retval = EXIT_SUCCESS; |
e537352b | 1474 | goto finish; |
fa0f4d8a | 1475 | } else if (arg_action == ACTION_DONE) { |
22f4096c | 1476 | retval = EXIT_SUCCESS; |
4288f619 | 1477 | goto finish; |
f170852a LP |
1478 | } |
1479 | ||
8be28fb1 KS |
1480 | if (arg_running_as == SYSTEMD_USER && |
1481 | !getenv("XDG_RUNTIME_DIR")) { | |
1482 | log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set."); | |
1483 | goto finish; | |
1484 | } | |
1485 | ||
fa0f4d8a | 1486 | assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST); |
f170852a | 1487 | |
871e5809 LP |
1488 | /* Close logging fds, in order not to confuse fdset below */ |
1489 | log_close(); | |
1490 | ||
a16e1123 | 1491 | /* Remember open file descriptors for later deserialization */ |
01e10de3 LP |
1492 | r = fdset_new_fill(&fds); |
1493 | if (r < 0) { | |
1494 | log_error("Failed to allocate fd set: %s", strerror(-r)); | |
1495 | goto finish; | |
1496 | } else | |
1497 | fdset_cloexec(fds, true); | |
a16e1123 | 1498 | |
d3b1c508 LP |
1499 | if (arg_serialization) |
1500 | assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0); | |
a16e1123 | 1501 | |
90dc8c2e | 1502 | if (arg_running_as == SYSTEMD_SYSTEM) |
80876c20 LP |
1503 | /* Become a session leader if we aren't one yet. */ |
1504 | setsid(); | |
4ade7963 | 1505 | |
befb5b6a LP |
1506 | /* Move out of the way, so that we won't block unmounts */ |
1507 | assert_se(chdir("/") == 0); | |
1508 | ||
2146621b LP |
1509 | /* Reset the console, but only if this is really init and we |
1510 | * are freshly booted */ | |
67445f4e | 1511 | if (arg_running_as == SYSTEMD_SYSTEM && arg_action == ACTION_RUN) |
2660882b | 1512 | console_setup(getpid() == 1 && !skip_setup); |
4ade7963 | 1513 | |
18149b9f | 1514 | /* Open the logging devices, if possible and necessary */ |
843d2643 | 1515 | log_open(); |
4ade7963 | 1516 | |
b6e2f329 LP |
1517 | if (arg_show_status == _SHOW_STATUS_UNSET) |
1518 | arg_show_status = SHOW_STATUS_YES; | |
1519 | ||
5373d602 LP |
1520 | /* Make sure we leave a core dump without panicing the |
1521 | * kernel. */ | |
ab422445 | 1522 | if (getpid() == 1) { |
4fc935ca | 1523 | install_crash_handler(); |
97c4f35c | 1524 | |
0c85a4f3 LP |
1525 | r = mount_cgroup_controllers(arg_join_controllers); |
1526 | if (r < 0) | |
1527 | goto finish; | |
1528 | } | |
1529 | ||
67445f4e | 1530 | if (arg_running_as == SYSTEMD_SYSTEM) { |
c20f5ac7 LP |
1531 | const char *virtualization = NULL; |
1532 | ||
bc270841 | 1533 | log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES ")"); |
c20f5ac7 LP |
1534 | |
1535 | detect_virtualization(&virtualization); | |
1536 | if (virtualization) | |
1537 | log_info("Detected virtualization '%s'.", virtualization); | |
1538 | ||
fdd25311 LP |
1539 | write_container_id(); |
1540 | ||
d9d93745 LP |
1541 | log_info("Detected architecture '%s'.", architecture_to_string(uname_architecture())); |
1542 | ||
26a1efdf LP |
1543 | if (in_initrd()) |
1544 | log_info("Running in initial RAM disk."); | |
1545 | ||
3408ba01 LP |
1546 | /* Let's check whether /etc is already populated. We |
1547 | * don't actually really check for that, but use | |
1548 | * /etc/machine-id as flag file. This allows container | |
1549 | * managers and installers to provision a couple of | |
1550 | * files already. If the container manager wants to | |
1551 | * provision the machine ID itself it should pass | |
1552 | * $container_uuid to PID 1.*/ | |
1553 | ||
baa1bdf7 | 1554 | empty_etc = access("/etc/machine-id", F_OK) < 0; |
5f5c2f38 LP |
1555 | if (empty_etc) |
1556 | log_info("Running with unpopulated /etc."); | |
d8160f21 | 1557 | } else { |
1f97091d LP |
1558 | _cleanup_free_ char *t; |
1559 | ||
1560 | t = uid_to_name(getuid()); | |
1561 | log_debug(PACKAGE_STRING " running in user mode for user "UID_FMT"/%s. (" SYSTEMD_FEATURES ")", getuid(), strna(t)); | |
d8160f21 | 1562 | } |
a5dab5ce | 1563 | |
67445f4e | 1564 | if (arg_running_as == SYSTEMD_SYSTEM && !skip_setup) { |
d450b6f2 | 1565 | if (arg_show_status > 0 || plymouth_running()) |
888c6216 LP |
1566 | status_welcome(); |
1567 | ||
e3043162 | 1568 | #ifdef HAVE_KMOD |
c47fc1f0 | 1569 | kmod_setup(); |
e3043162 | 1570 | #endif |
888c6216 | 1571 | hostname_setup(); |
489388fb | 1572 | machine_id_setup(NULL); |
888c6216 | 1573 | loopback_setup(); |
490aed58 | 1574 | |
6ee5bbf8 | 1575 | test_mtab(); |
80758717 | 1576 | test_usr(); |
af5bc85d | 1577 | } |
302e8c4c | 1578 | |
67445f4e | 1579 | if (arg_running_as == SYSTEMD_SYSTEM && arg_runtime_watchdog > 0) |
e96d6be7 LP |
1580 | watchdog_set_timeout(&arg_runtime_watchdog); |
1581 | ||
aa0f64ac LP |
1582 | if (arg_timer_slack_nsec != (nsec_t) -1) |
1583 | if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0) | |
1584 | log_error("Failed to adjust timer slack: %m"); | |
1585 | ||
ec8927ca | 1586 | if (arg_capability_bounding_set_drop) { |
31c885e9 | 1587 | r = capability_bounding_set_drop_usermode(arg_capability_bounding_set_drop); |
ec8927ca | 1588 | if (r < 0) { |
31c885e9 | 1589 | log_error("Failed to drop capability bounding set of usermode helpers: %s", strerror(-r)); |
ec8927ca LP |
1590 | goto finish; |
1591 | } | |
31c885e9 | 1592 | r = capability_bounding_set_drop(arg_capability_bounding_set_drop, true); |
939b8f14 | 1593 | if (r < 0) { |
31c885e9 | 1594 | log_error("Failed to drop capability bounding set: %s", strerror(-r)); |
939b8f14 LP |
1595 | goto finish; |
1596 | } | |
ec8927ca LP |
1597 | } |
1598 | ||
d3b1c508 LP |
1599 | if (arg_syscall_archs) { |
1600 | r = enforce_syscall_archs(arg_syscall_archs); | |
1601 | if (r < 0) | |
1602 | goto finish; | |
1603 | } | |
1604 | ||
67445f4e | 1605 | if (arg_running_as == SYSTEMD_USER) { |
d4447f4d | 1606 | /* Become reaper of our children */ |
8b8ffe68 LP |
1607 | if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) { |
1608 | log_warning("Failed to make us a subreaper: %m"); | |
1609 | if (errno == EINVAL) | |
ddfa5101 | 1610 | log_info("Perhaps the kernel version is too old (< 3.4?)"); |
8b8ffe68 | 1611 | } |
d4447f4d AK |
1612 | } |
1613 | ||
5f5c2f38 | 1614 | if (arg_running_as == SYSTEMD_SYSTEM) { |
4096d6f5 LP |
1615 | bump_rlimit_nofile(&saved_rlimit_nofile); |
1616 | ||
5f5c2f38 LP |
1617 | if (empty_etc) { |
1618 | r = unit_file_preset_all(UNIT_FILE_SYSTEM, false, NULL, UNIT_FILE_PRESET_FULL, false, NULL, 0); | |
1619 | if (r < 0) | |
1620 | log_warning("Failed to populate /etc with preset unit settings, ignoring: %s", strerror(-r)); | |
1621 | else | |
1622 | log_info("Populated /etc with preset unit settings."); | |
1623 | } | |
1624 | } | |
1625 | ||
e3dd987c | 1626 | r = manager_new(arg_running_as, &m); |
e96d6be7 | 1627 | if (r < 0) { |
8e274523 | 1628 | log_error("Failed to allocate manager object: %s", strerror(-r)); |
60918275 LP |
1629 | goto finish; |
1630 | } | |
1631 | ||
9e58ff9c | 1632 | m->confirm_spawn = arg_confirm_spawn; |
bd8f585b | 1633 | m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec; |
0a494f1f LP |
1634 | m->default_std_output = arg_default_std_output; |
1635 | m->default_std_error = arg_default_std_error; | |
1f19a534 OS |
1636 | m->default_restart_usec = arg_default_restart_usec; |
1637 | m->default_timeout_start_usec = arg_default_timeout_start_usec; | |
1638 | m->default_timeout_stop_usec = arg_default_timeout_stop_usec; | |
3f41e1e5 LN |
1639 | m->default_start_limit_interval = arg_default_start_limit_interval; |
1640 | m->default_start_limit_burst = arg_default_start_limit_burst; | |
085afe36 LP |
1641 | m->default_cpu_accounting = arg_default_cpu_accounting; |
1642 | m->default_blockio_accounting = arg_default_blockio_accounting; | |
1643 | m->default_memory_accounting = arg_default_memory_accounting; | |
e96d6be7 LP |
1644 | m->runtime_watchdog = arg_runtime_watchdog; |
1645 | m->shutdown_watchdog = arg_shutdown_watchdog; | |
c3a170f3 HH |
1646 | m->userspace_timestamp = userspace_timestamp; |
1647 | m->kernel_timestamp = kernel_timestamp; | |
c3a170f3 | 1648 | m->initrd_timestamp = initrd_timestamp; |
c2e0d600 TA |
1649 | m->security_start_timestamp = security_start_timestamp; |
1650 | m->security_finish_timestamp = security_finish_timestamp; | |
9e58ff9c | 1651 | |
c93ff2e9 | 1652 | manager_set_default_rlimits(m, arg_default_rlimit); |
b6e2f329 | 1653 | manager_environment_add(m, NULL, arg_default_environment); |
27d340c7 | 1654 | manager_set_show_status(m, arg_show_status); |
e2680723 | 1655 | manager_set_first_boot(m, empty_etc); |
27d340c7 | 1656 | |
bf4df7c3 | 1657 | /* Remember whether we should queue the default job */ |
d3b1c508 | 1658 | queue_default_job = !arg_serialization || arg_switched_root; |
bf4df7c3 | 1659 | |
9d76d730 LP |
1660 | before_startup = now(CLOCK_MONOTONIC); |
1661 | ||
d3b1c508 | 1662 | r = manager_startup(m, arg_serialization, fds); |
e96d6be7 | 1663 | if (r < 0) |
6e2ef85b | 1664 | log_error("Failed to fully start up daemon: %s", strerror(-r)); |
a16e1123 | 1665 | |
bf4df7c3 LP |
1666 | /* This will close all file descriptors that were opened, but |
1667 | * not claimed by any unit. */ | |
01e10de3 | 1668 | fdset_free(fds); |
6c081276 | 1669 | fds = NULL; |
f50e0a01 | 1670 | |
d3b1c508 LP |
1671 | if (arg_serialization) { |
1672 | fclose(arg_serialization); | |
1673 | arg_serialization = NULL; | |
bf4df7c3 LP |
1674 | } |
1675 | ||
1676 | if (queue_default_job) { | |
718db961 | 1677 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
1c27d3f3 | 1678 | Unit *target = NULL; |
bacbccb7 | 1679 | Job *default_unit_job; |
398ef8ba | 1680 | |
fa0f4d8a | 1681 | log_debug("Activating default unit: %s", arg_default_unit); |
a16e1123 | 1682 | |
e96d6be7 | 1683 | r = manager_load_unit(m, arg_default_unit, NULL, &error, &target); |
718db961 LP |
1684 | if (r < 0) |
1685 | log_error("Failed to load default target: %s", bus_error_message(&error, r)); | |
1686 | else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) | |
ac155bb8 MS |
1687 | log_error("Failed to load default target: %s", strerror(-target->load_error)); |
1688 | else if (target->load_state == UNIT_MASKED) | |
6daf4f90 | 1689 | log_error("Default target masked."); |
27b14a22 | 1690 | |
ac155bb8 | 1691 | if (!target || target->load_state != UNIT_LOADED) { |
a16e1123 | 1692 | log_info("Trying to load rescue target..."); |
1c27d3f3 | 1693 | |
e96d6be7 LP |
1694 | r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target); |
1695 | if (r < 0) { | |
718db961 | 1696 | log_error("Failed to load rescue target: %s", bus_error_message(&error, r)); |
a16e1123 | 1697 | goto finish; |
11ddb6f4 | 1698 | } else if (target->load_state == UNIT_ERROR || target->load_state == UNIT_NOT_FOUND) { |
ac155bb8 | 1699 | log_error("Failed to load rescue target: %s", strerror(-target->load_error)); |
1c27d3f3 | 1700 | goto finish; |
ac155bb8 | 1701 | } else if (target->load_state == UNIT_MASKED) { |
6daf4f90 | 1702 | log_error("Rescue target masked."); |
00dc5d76 | 1703 | goto finish; |
a16e1123 LP |
1704 | } |
1705 | } | |
37d88da7 | 1706 | |
ac155bb8 | 1707 | assert(target->load_state == UNIT_LOADED); |
00dc5d76 | 1708 | |
fa0f4d8a | 1709 | if (arg_action == ACTION_TEST) { |
40d50879 | 1710 | printf("-> By units:\n"); |
a16e1123 LP |
1711 | manager_dump_units(m, stdout, "\t"); |
1712 | } | |
1713 | ||
95f1b47d | 1714 | r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, false, &error, &default_unit_job); |
ab17a050 | 1715 | if (r == -EPERM) { |
718db961 | 1716 | log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r)); |
ab17a050 LP |
1717 | |
1718 | r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &default_unit_job); | |
1719 | if (r < 0) { | |
718db961 | 1720 | log_error("Failed to start default target: %s", bus_error_message(&error, r)); |
ab17a050 LP |
1721 | goto finish; |
1722 | } | |
1723 | } else if (r < 0) { | |
718db961 | 1724 | log_error("Failed to isolate default target: %s", bus_error_message(&error, r)); |
37d88da7 LP |
1725 | goto finish; |
1726 | } | |
ab17a050 | 1727 | |
bacbccb7 | 1728 | m->default_unit_job_id = default_unit_job->id; |
60918275 | 1729 | |
07672f49 LP |
1730 | after_startup = now(CLOCK_MONOTONIC); |
1731 | log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG, | |
1732 | "Loaded units and determined initial transaction in %s.", | |
2fa4092c | 1733 | format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 0)); |
07672f49 | 1734 | |
fa0f4d8a | 1735 | if (arg_action == ACTION_TEST) { |
40d50879 | 1736 | printf("-> By jobs:\n"); |
a16e1123 | 1737 | manager_dump_jobs(m, stdout, "\t"); |
22f4096c | 1738 | retval = EXIT_SUCCESS; |
a16e1123 LP |
1739 | goto finish; |
1740 | } | |
e965d56d | 1741 | } |
d46de8a1 | 1742 | |
a16e1123 | 1743 | for (;;) { |
e96d6be7 LP |
1744 | r = manager_loop(m); |
1745 | if (r < 0) { | |
a16e1123 LP |
1746 | log_error("Failed to run mainloop: %s", strerror(-r)); |
1747 | goto finish; | |
1748 | } | |
11dd41ce | 1749 | |
a16e1123 | 1750 | switch (m->exit_code) { |
e965d56d | 1751 | |
a16e1123 | 1752 | case MANAGER_EXIT: |
22f4096c | 1753 | retval = EXIT_SUCCESS; |
a16e1123 LP |
1754 | log_debug("Exit."); |
1755 | goto finish; | |
e965d56d | 1756 | |
a16e1123 | 1757 | case MANAGER_RELOAD: |
e015090f | 1758 | log_info("Reloading."); |
e96d6be7 LP |
1759 | r = manager_reload(m); |
1760 | if (r < 0) | |
a16e1123 LP |
1761 | log_error("Failed to reload: %s", strerror(-r)); |
1762 | break; | |
cea8e32e | 1763 | |
a16e1123 | 1764 | case MANAGER_REEXECUTE: |
664f88a7 | 1765 | |
d3b1c508 | 1766 | if (prepare_reexecute(m, &arg_serialization, &fds, false) < 0) |
a16e1123 | 1767 | goto finish; |
60918275 | 1768 | |
a16e1123 | 1769 | reexecute = true; |
e015090f | 1770 | log_notice("Reexecuting."); |
a16e1123 LP |
1771 | goto finish; |
1772 | ||
664f88a7 LP |
1773 | case MANAGER_SWITCH_ROOT: |
1774 | /* Steal the switch root parameters */ | |
41669317 | 1775 | switch_root_dir = m->switch_root; |
664f88a7 LP |
1776 | switch_root_init = m->switch_root_init; |
1777 | m->switch_root = m->switch_root_init = NULL; | |
1778 | ||
1779 | if (!switch_root_init) | |
d3b1c508 | 1780 | if (prepare_reexecute(m, &arg_serialization, &fds, true) < 0) |
664f88a7 LP |
1781 | goto finish; |
1782 | ||
1783 | reexecute = true; | |
1784 | log_notice("Switching root."); | |
1785 | goto finish; | |
1786 | ||
b9080b03 FF |
1787 | case MANAGER_REBOOT: |
1788 | case MANAGER_POWEROFF: | |
1789 | case MANAGER_HALT: | |
1790 | case MANAGER_KEXEC: { | |
1791 | static const char * const table[_MANAGER_EXIT_CODE_MAX] = { | |
1792 | [MANAGER_REBOOT] = "reboot", | |
1793 | [MANAGER_POWEROFF] = "poweroff", | |
1794 | [MANAGER_HALT] = "halt", | |
1795 | [MANAGER_KEXEC] = "kexec" | |
1796 | }; | |
1797 | ||
1798 | assert_se(shutdown_verb = table[m->exit_code]); | |
e96d6be7 | 1799 | arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT; |
b9080b03 FF |
1800 | |
1801 | log_notice("Shutting down."); | |
1802 | goto finish; | |
1803 | } | |
1804 | ||
a16e1123 LP |
1805 | default: |
1806 | assert_not_reached("Unknown exit code."); | |
1807 | } | |
1808 | } | |
f170852a | 1809 | |
60918275 | 1810 | finish: |
d3b1c508 | 1811 | if (m) { |
60918275 | 1812 | manager_free(m); |
d3b1c508 LP |
1813 | m = NULL; |
1814 | } | |
60918275 | 1815 | |
d3b1c508 | 1816 | for (j = 0; j < ELEMENTSOF(arg_default_rlimit); j++) { |
4096d6f5 | 1817 | free(arg_default_rlimit[j]); |
d3b1c508 LP |
1818 | arg_default_rlimit[j] = NULL; |
1819 | } | |
c93ff2e9 | 1820 | |
fa0f4d8a | 1821 | free(arg_default_unit); |
d3b1c508 LP |
1822 | arg_default_unit = NULL; |
1823 | ||
0c85a4f3 | 1824 | free_join_controllers(); |
b9cd2ec1 | 1825 | |
d3b1c508 LP |
1826 | strv_free(arg_default_environment); |
1827 | arg_default_environment = NULL; | |
1828 | ||
1829 | set_free(arg_syscall_archs); | |
1830 | arg_syscall_archs = NULL; | |
1831 | ||
b2bb3dbe LP |
1832 | label_finish(); |
1833 | ||
a16e1123 | 1834 | if (reexecute) { |
664f88a7 | 1835 | const char **args; |
e564a982 | 1836 | unsigned i, args_size; |
9bfcda95 | 1837 | sigset_t ss; |
a16e1123 | 1838 | |
664f88a7 LP |
1839 | /* Close and disarm the watchdog, so that the new |
1840 | * instance can reinitialize it, but doesn't get | |
1841 | * rebooted while we do that */ | |
1842 | watchdog_close(true); | |
a16e1123 | 1843 | |
4096d6f5 LP |
1844 | /* Reset the RLIMIT_NOFILE to the kernel default, so |
1845 | * that the new systemd can pass the kernel default to | |
1846 | * its child processes */ | |
1847 | if (saved_rlimit_nofile.rlim_cur > 0) | |
1848 | setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile); | |
1849 | ||
41669317 | 1850 | if (switch_root_dir) { |
cee530bb LP |
1851 | /* Kill all remaining processes from the |
1852 | * initrd, but don't wait for them, so that we | |
1853 | * can handle the SIGCHLD for them after | |
1854 | * deserializing. */ | |
0bee65f0 | 1855 | broadcast_signal(SIGTERM, false, true); |
bd3fa1d2 LP |
1856 | |
1857 | /* And switch root */ | |
41669317 LP |
1858 | r = switch_root(switch_root_dir); |
1859 | if (r < 0) | |
1860 | log_error("Failed to switch root, ignoring: %s", strerror(-r)); | |
1861 | } | |
a16e1123 | 1862 | |
d03bc1b8 | 1863 | args_size = MAX(6, argc+1); |
e564a982 | 1864 | args = newa(const char*, args_size); |
a16e1123 | 1865 | |
664f88a7 LP |
1866 | if (!switch_root_init) { |
1867 | char sfd[16]; | |
a16e1123 | 1868 | |
664f88a7 LP |
1869 | /* First try to spawn ourselves with the right |
1870 | * path, and with full serialization. We do | |
1871 | * this only if the user didn't specify an | |
1872 | * explicit init to spawn. */ | |
edb9aaa8 | 1873 | |
d3b1c508 | 1874 | assert(arg_serialization); |
664f88a7 | 1875 | assert(fds); |
edb9aaa8 | 1876 | |
d3b1c508 | 1877 | snprintf(sfd, sizeof(sfd), "%i", fileno(arg_serialization)); |
664f88a7 | 1878 | char_array_0(sfd); |
edb9aaa8 | 1879 | |
664f88a7 LP |
1880 | i = 0; |
1881 | args[i++] = SYSTEMD_BINARY_PATH; | |
41669317 | 1882 | if (switch_root_dir) |
2660882b | 1883 | args[i++] = "--switched-root"; |
67445f4e | 1884 | args[i++] = arg_running_as == SYSTEMD_SYSTEM ? "--system" : "--user"; |
664f88a7 LP |
1885 | args[i++] = "--deserialize"; |
1886 | args[i++] = sfd; | |
1887 | args[i++] = NULL; | |
edb9aaa8 | 1888 | |
ce8aba56 KS |
1889 | /* do not pass along the environment we inherit from the kernel or initrd */ |
1890 | if (switch_root_dir) | |
1891 | clearenv(); | |
1892 | ||
e564a982 | 1893 | assert(i <= args_size); |
664f88a7 LP |
1894 | execv(args[0], (char* const*) args); |
1895 | } | |
6e98720f | 1896 | |
664f88a7 LP |
1897 | /* Try the fallback, if there is any, without any |
1898 | * serialization. We pass the original argv[] and | |
1899 | * envp[]. (Well, modulo the ordering changes due to | |
1900 | * getopt() in argv[], and some cleanups in envp[], | |
1901 | * but let's hope that doesn't matter.) */ | |
a16e1123 | 1902 | |
d3b1c508 LP |
1903 | if (arg_serialization) { |
1904 | fclose(arg_serialization); | |
1905 | arg_serialization = NULL; | |
b8f83232 | 1906 | } |
a16e1123 | 1907 | |
b8f83232 | 1908 | if (fds) { |
664f88a7 | 1909 | fdset_free(fds); |
b8f83232 LP |
1910 | fds = NULL; |
1911 | } | |
a16e1123 | 1912 | |
a504223d HH |
1913 | /* Reopen the console */ |
1914 | make_console_stdio(); | |
1915 | ||
d3b1c508 | 1916 | for (j = 1, i = 1; j < (unsigned) argc; j++) |
664f88a7 | 1917 | args[i++] = argv[j]; |
a16e1123 | 1918 | args[i++] = NULL; |
e564a982 | 1919 | assert(i <= args_size); |
b8f83232 | 1920 | |
5a85ca1c RO |
1921 | /* reenable any blocked signals, especially important |
1922 | * if we switch from initial ramdisk to init=... */ | |
9bfcda95 LP |
1923 | reset_all_signal_handlers(); |
1924 | ||
1925 | assert_se(sigemptyset(&ss) == 0); | |
1926 | assert_se(sigprocmask(SIG_SETMASK, &ss, NULL) == 0); | |
5a85ca1c | 1927 | |
b8f83232 LP |
1928 | if (switch_root_init) { |
1929 | args[0] = switch_root_init; | |
1930 | execv(args[0], (char* const*) args); | |
1931 | log_warning("Failed to execute configured init, trying fallback: %m"); | |
1932 | } | |
1933 | ||
1934 | args[0] = "/sbin/init"; | |
a16e1123 LP |
1935 | execv(args[0], (char* const*) args); |
1936 | ||
745e2fb7 KS |
1937 | if (errno == ENOENT) { |
1938 | log_warning("No /sbin/init, trying fallback"); | |
b8f83232 | 1939 | |
745e2fb7 KS |
1940 | args[0] = "/bin/sh"; |
1941 | args[1] = NULL; | |
1942 | execv(args[0], (char* const*) args); | |
1943 | log_error("Failed to execute /bin/sh, giving up: %m"); | |
1944 | } else | |
1945 | log_warning("Failed to execute /sbin/init, giving up: %m"); | |
a16e1123 LP |
1946 | } |
1947 | ||
d3b1c508 LP |
1948 | if (arg_serialization) { |
1949 | fclose(arg_serialization); | |
1950 | arg_serialization = NULL; | |
1951 | } | |
a16e1123 | 1952 | |
d3b1c508 | 1953 | if (fds) { |
a16e1123 | 1954 | fdset_free(fds); |
d3b1c508 LP |
1955 | fds = NULL; |
1956 | } | |
a16e1123 | 1957 | |
54b434b1 LP |
1958 | #ifdef HAVE_VALGRIND_VALGRIND_H |
1959 | /* If we are PID 1 and running under valgrind, then let's exit | |
1960 | * here explicitly. valgrind will only generate nice output on | |
1961 | * exit(), not on exec(), hence let's do the former not the | |
1962 | * latter here. */ | |
1963 | if (getpid() == 1 && RUNNING_ON_VALGRIND) | |
1964 | return 0; | |
1965 | #endif | |
1966 | ||
b9080b03 | 1967 | if (shutdown_verb) { |
b1e90ec5 ZJS |
1968 | char log_level[DECIMAL_STR_MAX(int) + 1]; |
1969 | const char* command_line[9] = { | |
b9080b03 FF |
1970 | SYSTEMD_SHUTDOWN_BINARY_PATH, |
1971 | shutdown_verb, | |
b1e90ec5 ZJS |
1972 | "--log-level", log_level, |
1973 | "--log-target", | |
b9080b03 | 1974 | }; |
b1e90ec5 | 1975 | unsigned pos = 5; |
e3e45d4f | 1976 | _cleanup_strv_free_ char **env_block = NULL; |
15bd5aee LP |
1977 | |
1978 | assert(command_line[pos] == NULL); | |
e3e45d4f | 1979 | env_block = strv_copy(environ); |
b9080b03 | 1980 | |
b1e90ec5 ZJS |
1981 | snprintf(log_level, sizeof(log_level), "%d", log_get_max_level()); |
1982 | ||
1983 | switch (log_get_target()) { | |
1984 | case LOG_TARGET_KMSG: | |
1985 | case LOG_TARGET_JOURNAL_OR_KMSG: | |
1986 | case LOG_TARGET_SYSLOG_OR_KMSG: | |
1987 | command_line[pos++] = "kmsg"; | |
1988 | break; | |
1989 | ||
1990 | case LOG_TARGET_CONSOLE: | |
1991 | default: | |
1992 | command_line[pos++] = "console"; | |
1993 | break; | |
1994 | }; | |
1995 | ||
1996 | if (log_get_show_color()) | |
1997 | command_line[pos++] = "--log-color"; | |
1998 | ||
1999 | if (log_get_show_location()) | |
2000 | command_line[pos++] = "--log-location"; | |
2001 | ||
26abdc73 | 2002 | assert(pos < ELEMENTSOF(command_line)); |
b1e90ec5 | 2003 | |
e96d6be7 | 2004 | if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) { |
e3e45d4f | 2005 | char *e; |
d18f337c | 2006 | |
e96d6be7 LP |
2007 | /* If we reboot let's set the shutdown |
2008 | * watchdog and tell the shutdown binary to | |
2009 | * repeatedly ping it */ | |
2010 | watchdog_set_timeout(&arg_shutdown_watchdog); | |
2011 | watchdog_close(false); | |
2012 | ||
e3e45d4f SP |
2013 | /* Tell the binary how often to ping, ignore failure */ |
2014 | if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0) | |
2015 | strv_push(&env_block, e); | |
2016 | } else | |
e96d6be7 LP |
2017 | watchdog_close(true); |
2018 | ||
66713f77 LP |
2019 | /* Avoid the creation of new processes forked by the |
2020 | * kernel; at this point, we will not listen to the | |
2021 | * signals anyway */ | |
2022 | if (detect_container(NULL) <= 0) | |
2023 | cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER); | |
ad929bcc | 2024 | |
d18f337c | 2025 | execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); |
b1e90ec5 ZJS |
2026 | log_error("Failed to execute shutdown binary, %s: %m", |
2027 | getpid() == 1 ? "freezing" : "quitting"); | |
b9080b03 FF |
2028 | } |
2029 | ||
c3b3c274 LP |
2030 | if (getpid() == 1) |
2031 | freeze(); | |
2032 | ||
60918275 LP |
2033 | return retval; |
2034 | } |