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