chiark / gitweb /
747121461d0568414d835db5cbb1515e01bd1501
[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 <dbus/dbus.h>
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <getopt.h>
31 #include <signal.h>
32 #include <sys/wait.h>
33 #include <fcntl.h>
34 #include <sys/prctl.h>
35 #include <sys/mount.h>
36
37 #include "manager.h"
38 #include "log.h"
39 #include "load-fragment.h"
40 #include "fdset.h"
41 #include "special.h"
42 #include "conf-parser.h"
43 #include "bus-errors.h"
44 #include "missing.h"
45 #include "label.h"
46 #include "build.h"
47 #include "strv.h"
48 #include "def.h"
49 #include "virt.h"
50 #include "watchdog.h"
51 #include "path-util.h"
52
53 #include "mount-setup.h"
54 #include "loopback-setup.h"
55 #include "kmod-setup.h"
56 #include "hostname-setup.h"
57 #include "machine-id-setup.h"
58 #include "locale-setup.h"
59 #include "hwclock.h"
60 #include "selinux-setup.h"
61 #include "ima-setup.h"
62
63 static enum {
64         ACTION_RUN,
65         ACTION_HELP,
66         ACTION_TEST,
67         ACTION_DUMP_CONFIGURATION_ITEMS,
68         ACTION_DONE
69 } arg_action = ACTION_RUN;
70
71 static char *arg_default_unit = NULL;
72 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
73
74 static bool arg_dump_core = true;
75 static bool arg_crash_shell = false;
76 static int arg_crash_chvt = -1;
77 static bool arg_confirm_spawn = false;
78 static bool arg_show_status = true;
79 #ifdef HAVE_SYSV_COMPAT
80 static bool arg_sysv_console = true;
81 #endif
82 static char **arg_default_controllers = NULL;
83 static char ***arg_join_controllers = NULL;
84 static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
85 static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
86 static usec_t arg_runtime_watchdog = 0;
87 static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
88
89 static FILE* serialization = NULL;
90
91 static void nop_handler(int sig) {
92 }
93
94 _noreturn_ static void crash(int sig) {
95
96         if (!arg_dump_core)
97                 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
98         else {
99                 struct sigaction sa;
100                 pid_t pid;
101
102                 /* We want to wait for the core process, hence let's enable SIGCHLD */
103                 zero(sa);
104                 sa.sa_handler = nop_handler;
105                 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
106                 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
107
108                 if ((pid = fork()) < 0)
109                         log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
110
111                 else if (pid == 0) {
112                         struct rlimit rl;
113
114                         /* Enable default signal handler for core dump */
115                         zero(sa);
116                         sa.sa_handler = SIG_DFL;
117                         assert_se(sigaction(sig, &sa, NULL) == 0);
118
119                         /* Don't limit the core dump size */
120                         zero(rl);
121                         rl.rlim_cur = RLIM_INFINITY;
122                         rl.rlim_max = RLIM_INFINITY;
123                         setrlimit(RLIMIT_CORE, &rl);
124
125                         /* Just to be sure... */
126                         assert_se(chdir("/") == 0);
127
128                         /* Raise the signal again */
129                         raise(sig);
130
131                         assert_not_reached("We shouldn't be here...");
132                         _exit(1);
133
134                 } else {
135                         siginfo_t status;
136                         int r;
137
138                         /* Order things nicely. */
139                         if ((r = wait_for_terminate(pid, &status)) < 0)
140                                 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
141                         else if (status.si_code != CLD_DUMPED)
142                                 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
143                         else
144                                 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
145                 }
146         }
147
148         if (arg_crash_chvt)
149                 chvt(arg_crash_chvt);
150
151         if (arg_crash_shell) {
152                 struct sigaction sa;
153                 pid_t pid;
154
155                 log_info("Executing crash shell in 10s...");
156                 sleep(10);
157
158                 /* Let the kernel reap children for us */
159                 zero(sa);
160                 sa.sa_handler = SIG_IGN;
161                 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
162                 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
163
164                 if ((pid = fork()) < 0)
165                         log_error("Failed to fork off crash shell: %s", strerror(errno));
166                 else if (pid == 0) {
167                         int fd, r;
168
169                         if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
170                                 log_error("Failed to acquire terminal: %s", strerror(-fd));
171                         else if ((r = make_stdio(fd)) < 0)
172                                 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
173
174                         execl("/bin/sh", "/bin/sh", NULL);
175
176                         log_error("execl() failed: %s", strerror(errno));
177                         _exit(1);
178                 }
179
180                 log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
181         }
182
183         log_info("Freezing execution.");
184         freeze();
185 }
186
187 static void install_crash_handler(void) {
188         struct sigaction sa;
189
190         zero(sa);
191
192         sa.sa_handler = crash;
193         sa.sa_flags = SA_NODEFER;
194
195         sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
196 }
197
198 static int console_setup(bool do_reset) {
199         int tty_fd, r;
200
201         /* If we are init, we connect stdin/stdout/stderr to /dev/null
202          * and make sure we don't have a controlling tty. */
203
204         release_terminal();
205
206         if (!do_reset)
207                 return 0;
208
209         tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
210         if (tty_fd < 0) {
211                 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
212                 return -tty_fd;
213         }
214
215         /* We don't want to force text mode.
216          * plymouth may be showing pictures already from initrd. */
217         r = reset_terminal_fd(tty_fd, false);
218         if (r < 0)
219                 log_error("Failed to reset /dev/console: %s", strerror(-r));
220
221         close_nointr_nofail(tty_fd);
222         return r;
223 }
224
225 static int set_default_unit(const char *u) {
226         char *c;
227
228         assert(u);
229
230         if (!(c = strdup(u)))
231                 return -ENOMEM;
232
233         free(arg_default_unit);
234         arg_default_unit = c;
235         return 0;
236 }
237
238 static int parse_proc_cmdline_word(const char *word) {
239
240         static const char * const rlmap[] = {
241                 "emergency", SPECIAL_EMERGENCY_TARGET,
242                 "-b",        SPECIAL_EMERGENCY_TARGET,
243                 "single",    SPECIAL_RESCUE_TARGET,
244                 "-s",        SPECIAL_RESCUE_TARGET,
245                 "s",         SPECIAL_RESCUE_TARGET,
246                 "S",         SPECIAL_RESCUE_TARGET,
247                 "1",         SPECIAL_RESCUE_TARGET,
248                 "2",         SPECIAL_RUNLEVEL2_TARGET,
249                 "3",         SPECIAL_RUNLEVEL3_TARGET,
250                 "4",         SPECIAL_RUNLEVEL4_TARGET,
251                 "5",         SPECIAL_RUNLEVEL5_TARGET,
252         };
253
254         assert(word);
255
256         if (startswith(word, "systemd.unit="))
257                 return set_default_unit(word + 13);
258
259         else if (startswith(word, "systemd.log_target=")) {
260
261                 if (log_set_target_from_string(word + 19) < 0)
262                         log_warning("Failed to parse log target %s. Ignoring.", word + 19);
263
264         } else if (startswith(word, "systemd.log_level=")) {
265
266                 if (log_set_max_level_from_string(word + 18) < 0)
267                         log_warning("Failed to parse log level %s. Ignoring.", word + 18);
268
269         } else if (startswith(word, "systemd.log_color=")) {
270
271                 if (log_show_color_from_string(word + 18) < 0)
272                         log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
273
274         } else if (startswith(word, "systemd.log_location=")) {
275
276                 if (log_show_location_from_string(word + 21) < 0)
277                         log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
278
279         } else if (startswith(word, "systemd.dump_core=")) {
280                 int r;
281
282                 if ((r = parse_boolean(word + 18)) < 0)
283                         log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
284                 else
285                         arg_dump_core = r;
286
287         } else if (startswith(word, "systemd.crash_shell=")) {
288                 int r;
289
290                 if ((r = parse_boolean(word + 20)) < 0)
291                         log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
292                 else
293                         arg_crash_shell = r;
294
295         } else if (startswith(word, "systemd.confirm_spawn=")) {
296                 int r;
297
298                 if ((r = parse_boolean(word + 22)) < 0)
299                         log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
300                 else
301                         arg_confirm_spawn = r;
302
303         } else if (startswith(word, "systemd.crash_chvt=")) {
304                 int k;
305
306                 if (safe_atoi(word + 19, &k) < 0)
307                         log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
308                 else
309                         arg_crash_chvt = k;
310
311         } else if (startswith(word, "systemd.show_status=")) {
312                 int r;
313
314                 if ((r = parse_boolean(word + 20)) < 0)
315                         log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
316                 else
317                         arg_show_status = r;
318         } else if (startswith(word, "systemd.default_standard_output=")) {
319                 int r;
320
321                 if ((r = exec_output_from_string(word + 32)) < 0)
322                         log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
323                 else
324                         arg_default_std_output = r;
325         } else if (startswith(word, "systemd.default_standard_error=")) {
326                 int r;
327
328                 if ((r = exec_output_from_string(word + 31)) < 0)
329                         log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
330                 else
331                         arg_default_std_error = r;
332         } else if (startswith(word, "systemd.setenv=")) {
333                 char *cenv, *eq;
334                 int r;
335
336                 cenv = strdup(word + 15);
337                 if (!cenv)
338                         return -ENOMEM;
339
340                 eq = strchr(cenv, '=');
341                 if (!eq) {
342                         r = unsetenv(cenv);
343                         if (r < 0)
344                                 log_warning("unsetenv failed %s. Ignoring.", strerror(errno));
345                 } else {
346                         *eq = 0;
347                         r = setenv(cenv, eq + 1, 1);
348                         if (r < 0)
349                                 log_warning("setenv failed %s. Ignoring.", strerror(errno));
350                 }
351                 free(cenv);
352 #ifdef HAVE_SYSV_COMPAT
353         } else if (startswith(word, "systemd.sysv_console=")) {
354                 int r;
355
356                 if ((r = parse_boolean(word + 21)) < 0)
357                         log_warning("Failed to parse SysV console switch %s. Ignoring.", word + 20);
358                 else
359                         arg_sysv_console = r;
360 #endif
361
362         } else if (startswith(word, "systemd.")) {
363
364                 log_warning("Unknown kernel switch %s. Ignoring.", word);
365
366                 log_info("Supported kernel switches:\n"
367                          "systemd.unit=UNIT                        Default unit to start\n"
368                          "systemd.dump_core=0|1                    Dump core on crash\n"
369                          "systemd.crash_shell=0|1                  Run shell on crash\n"
370                          "systemd.crash_chvt=N                     Change to VT #N on crash\n"
371                          "systemd.confirm_spawn=0|1                Confirm every process spawn\n"
372                          "systemd.show_status=0|1                  Show status updates on the console during bootup\n"
373 #ifdef HAVE_SYSV_COMPAT
374                          "systemd.sysv_console=0|1                 Connect output of SysV scripts to console\n"
375 #endif
376                          "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
377                          "                                         Log target\n"
378                          "systemd.log_level=LEVEL                  Log level\n"
379                          "systemd.log_color=0|1                    Highlight important log messages\n"
380                          "systemd.log_location=0|1                 Include code location in log messages\n"
381                          "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
382                          "                                         Set default log output for services\n"
383                          "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
384                          "                                         Set default log error output for services\n");
385
386         } else if (streq(word, "quiet")) {
387                 arg_show_status = false;
388 #ifdef HAVE_SYSV_COMPAT
389                 arg_sysv_console = false;
390 #endif
391         } else {
392                 unsigned i;
393
394                 /* SysV compatibility */
395                 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
396                         if (streq(word, rlmap[i]))
397                                 return set_default_unit(rlmap[i+1]);
398         }
399
400         return 0;
401 }
402
403 static int config_parse_level2(
404                 const char *filename,
405                 unsigned line,
406                 const char *section,
407                 const char *lvalue,
408                 int ltype,
409                 const char *rvalue,
410                 void *data,
411                 void *userdata) {
412
413         assert(filename);
414         assert(lvalue);
415         assert(rvalue);
416
417         log_set_max_level_from_string(rvalue);
418         return 0;
419 }
420
421 static int config_parse_target(
422                 const char *filename,
423                 unsigned line,
424                 const char *section,
425                 const char *lvalue,
426                 int ltype,
427                 const char *rvalue,
428                 void *data,
429                 void *userdata) {
430
431         assert(filename);
432         assert(lvalue);
433         assert(rvalue);
434
435         log_set_target_from_string(rvalue);
436         return 0;
437 }
438
439 static int config_parse_color(
440                 const char *filename,
441                 unsigned line,
442                 const char *section,
443                 const char *lvalue,
444                 int ltype,
445                 const char *rvalue,
446                 void *data,
447                 void *userdata) {
448
449         assert(filename);
450         assert(lvalue);
451         assert(rvalue);
452
453         log_show_color_from_string(rvalue);
454         return 0;
455 }
456
457 static int config_parse_location(
458                 const char *filename,
459                 unsigned line,
460                 const char *section,
461                 const char *lvalue,
462                 int ltype,
463                 const char *rvalue,
464                 void *data,
465                 void *userdata) {
466
467         assert(filename);
468         assert(lvalue);
469         assert(rvalue);
470
471         log_show_location_from_string(rvalue);
472         return 0;
473 }
474
475 static int config_parse_cpu_affinity2(
476                 const char *filename,
477                 unsigned line,
478                 const char *section,
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 -ENOMEM;
502
503                 r = safe_atou(t, &cpu);
504                 free(t);
505
506                 if (!c)
507                         if (!(c = cpu_set_malloc(&ncpus)))
508                                 return -ENOMEM;
509
510                 if (r < 0 || cpu >= ncpus) {
511                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
512                         CPU_FREE(c);
513                         return -EBADMSG;
514                 }
515
516                 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
517         }
518
519         if (c) {
520                 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
521                         log_warning("Failed to set CPU affinity: %m");
522
523                 CPU_FREE(c);
524         }
525
526         return 0;
527 }
528
529 static void strv_free_free(char ***l) {
530         char ***i;
531
532         if (!l)
533                 return;
534
535         for (i = l; *i; i++)
536                 strv_free(*i);
537
538         free(l);
539 }
540
541 static void free_join_controllers(void) {
542         if (!arg_join_controllers)
543                 return;
544
545         strv_free_free(arg_join_controllers);
546         arg_join_controllers = NULL;
547 }
548
549 static int config_parse_join_controllers(
550                 const char *filename,
551                 unsigned line,
552                 const char *section,
553                 const char *lvalue,
554                 int ltype,
555                 const char *rvalue,
556                 void *data,
557                 void *userdata) {
558
559         unsigned n = 0;
560         char *state, *w;
561         size_t length;
562
563         assert(filename);
564         assert(lvalue);
565         assert(rvalue);
566
567         free_join_controllers();
568
569         FOREACH_WORD_QUOTED(w, length, rvalue, state) {
570                 char *s, **l;
571
572                 s = strndup(w, length);
573                 if (!s)
574                         return -ENOMEM;
575
576                 l = strv_split(s, ",");
577                 free(s);
578
579                 strv_uniq(l);
580
581                 if (strv_length(l) <= 1) {
582                         strv_free(l);
583                         continue;
584                 }
585
586                 if (!arg_join_controllers) {
587                         arg_join_controllers = new(char**, 2);
588                         if (!arg_join_controllers) {
589                                 strv_free(l);
590                                 return -ENOMEM;
591                         }
592
593                         arg_join_controllers[0] = l;
594                         arg_join_controllers[1] = NULL;
595
596                         n = 1;
597                 } else {
598                         char ***a;
599                         char ***t;
600
601                         t = new0(char**, n+2);
602                         if (!t) {
603                                 strv_free(l);
604                                 return -ENOMEM;
605                         }
606
607                         n = 0;
608
609                         for (a = arg_join_controllers; *a; a++) {
610
611                                 if (strv_overlap(*a, l)) {
612                                         char **c;
613
614                                         c = strv_merge(*a, l);
615                                         if (!c) {
616                                                 strv_free(l);
617                                                 strv_free_free(t);
618                                                 return -ENOMEM;
619                                         }
620
621                                         strv_free(l);
622                                         l = c;
623                                 } else {
624                                         char **c;
625
626                                         c = strv_copy(*a);
627                                         if (!c) {
628                                                 strv_free(l);
629                                                 strv_free_free(t);
630                                                 return -ENOMEM;
631                                         }
632
633                                         t[n++] = c;
634                                 }
635                         }
636
637                         t[n++] = strv_uniq(l);
638
639                         strv_free_free(arg_join_controllers);
640                         arg_join_controllers = t;
641                 }
642         }
643
644         return 0;
645 }
646
647 static int parse_config_file(void) {
648
649         const ConfigTableItem items[] = {
650                 { "Manager", "LogLevel",              config_parse_level2,       0, NULL                     },
651                 { "Manager", "LogTarget",             config_parse_target,       0, NULL                     },
652                 { "Manager", "LogColor",              config_parse_color,        0, NULL                     },
653                 { "Manager", "LogLocation",           config_parse_location,     0, NULL                     },
654                 { "Manager", "DumpCore",              config_parse_bool,         0, &arg_dump_core           },
655                 { "Manager", "CrashShell",            config_parse_bool,         0, &arg_crash_shell         },
656                 { "Manager", "ShowStatus",            config_parse_bool,         0, &arg_show_status         },
657 #ifdef HAVE_SYSV_COMPAT
658                 { "Manager", "SysVConsole",           config_parse_bool,         0, &arg_sysv_console        },
659 #endif
660                 { "Manager", "CrashChVT",             config_parse_int,          0, &arg_crash_chvt          },
661                 { "Manager", "CPUAffinity",           config_parse_cpu_affinity2, 0, NULL                    },
662                 { "Manager", "DefaultControllers",    config_parse_strv,         0, &arg_default_controllers },
663                 { "Manager", "DefaultStandardOutput", config_parse_output,       0, &arg_default_std_output  },
664                 { "Manager", "DefaultStandardError",  config_parse_output,       0, &arg_default_std_error   },
665                 { "Manager", "JoinControllers",       config_parse_join_controllers, 0, &arg_join_controllers },
666                 { "Manager", "RuntimeWatchdogSec",    config_parse_usec,         0, &arg_runtime_watchdog    },
667                 { "Manager", "ShutdownWatchdogSec",   config_parse_usec,         0, &arg_shutdown_watchdog   },
668                 { NULL, NULL, NULL, 0, NULL }
669         };
670
671         FILE *f;
672         const char *fn;
673         int r;
674
675         fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
676         f = fopen(fn, "re");
677         if (!f) {
678                 if (errno == ENOENT)
679                         return 0;
680
681                 log_warning("Failed to open configuration file '%s': %m", fn);
682                 return 0;
683         }
684
685         r = config_parse(fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, NULL);
686         if (r < 0)
687                 log_warning("Failed to parse configuration file: %s", strerror(-r));
688
689         fclose(f);
690
691         return 0;
692 }
693
694 static int parse_proc_cmdline(void) {
695         char *line, *w, *state;
696         int r;
697         size_t l;
698
699         /* Don't read /proc/cmdline if we are in a container, since
700          * that is only relevant for the host system */
701         if (detect_container(NULL) > 0)
702                 return 0;
703
704         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
705                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
706                 return 0;
707         }
708
709         FOREACH_WORD_QUOTED(w, l, line, state) {
710                 char *word;
711
712                 if (!(word = strndup(w, l))) {
713                         r = -ENOMEM;
714                         goto finish;
715                 }
716
717                 r = parse_proc_cmdline_word(word);
718                 free(word);
719
720                 if (r < 0)
721                         goto finish;
722         }
723
724         r = 0;
725
726 finish:
727         free(line);
728         return r;
729 }
730
731 static int parse_argv(int argc, char *argv[]) {
732
733         enum {
734                 ARG_LOG_LEVEL = 0x100,
735                 ARG_LOG_TARGET,
736                 ARG_LOG_COLOR,
737                 ARG_LOG_LOCATION,
738                 ARG_UNIT,
739                 ARG_SYSTEM,
740                 ARG_USER,
741                 ARG_TEST,
742                 ARG_DUMP_CONFIGURATION_ITEMS,
743                 ARG_DUMP_CORE,
744                 ARG_CRASH_SHELL,
745                 ARG_CONFIRM_SPAWN,
746                 ARG_SHOW_STATUS,
747                 ARG_SYSV_CONSOLE,
748                 ARG_DESERIALIZE,
749                 ARG_SWITCHEDROOT,
750                 ARG_INTROSPECT,
751                 ARG_DEFAULT_STD_OUTPUT,
752                 ARG_DEFAULT_STD_ERROR
753         };
754
755         static const struct option options[] = {
756                 { "log-level",                required_argument, NULL, ARG_LOG_LEVEL                },
757                 { "log-target",               required_argument, NULL, ARG_LOG_TARGET               },
758                 { "log-color",                optional_argument, NULL, ARG_LOG_COLOR                },
759                 { "log-location",             optional_argument, NULL, ARG_LOG_LOCATION             },
760                 { "unit",                     required_argument, NULL, ARG_UNIT                     },
761                 { "system",                   no_argument,       NULL, ARG_SYSTEM                   },
762                 { "user",                     no_argument,       NULL, ARG_USER                     },
763                 { "test",                     no_argument,       NULL, ARG_TEST                     },
764                 { "help",                     no_argument,       NULL, 'h'                          },
765                 { "dump-configuration-items", no_argument,       NULL, ARG_DUMP_CONFIGURATION_ITEMS },
766                 { "dump-core",                optional_argument, NULL, ARG_DUMP_CORE                },
767                 { "crash-shell",              optional_argument, NULL, ARG_CRASH_SHELL              },
768                 { "confirm-spawn",            optional_argument, NULL, ARG_CONFIRM_SPAWN            },
769                 { "show-status",              optional_argument, NULL, ARG_SHOW_STATUS              },
770 #ifdef HAVE_SYSV_COMPAT
771                 { "sysv-console",             optional_argument, NULL, ARG_SYSV_CONSOLE             },
772 #endif
773                 { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
774                 { "switchedroot",             no_argument,       NULL, ARG_SWITCHEDROOT             },
775                 { "introspect",               optional_argument, NULL, ARG_INTROSPECT               },
776                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
777                 { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
778                 { NULL,                       0,                 NULL, 0                            }
779         };
780
781         int c, r;
782
783         assert(argc >= 1);
784         assert(argv);
785
786         if (getpid() == 1)
787                 opterr = 0;
788
789         while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
790
791                 switch (c) {
792
793                 case ARG_LOG_LEVEL:
794                         if ((r = log_set_max_level_from_string(optarg)) < 0) {
795                                 log_error("Failed to parse log level %s.", optarg);
796                                 return r;
797                         }
798
799                         break;
800
801                 case ARG_LOG_TARGET:
802
803                         if ((r = log_set_target_from_string(optarg)) < 0) {
804                                 log_error("Failed to parse log target %s.", optarg);
805                                 return r;
806                         }
807
808                         break;
809
810                 case ARG_LOG_COLOR:
811
812                         if (optarg) {
813                                 if ((r = log_show_color_from_string(optarg)) < 0) {
814                                         log_error("Failed to parse log color setting %s.", optarg);
815                                         return r;
816                                 }
817                         } else
818                                 log_show_color(true);
819
820                         break;
821
822                 case ARG_LOG_LOCATION:
823
824                         if (optarg) {
825                                 if ((r = log_show_location_from_string(optarg)) < 0) {
826                                         log_error("Failed to parse log location setting %s.", optarg);
827                                         return r;
828                                 }
829                         } else
830                                 log_show_location(true);
831
832                         break;
833
834                 case ARG_DEFAULT_STD_OUTPUT:
835
836                         if ((r = exec_output_from_string(optarg)) < 0) {
837                                 log_error("Failed to parse default standard output setting %s.", optarg);
838                                 return r;
839                         } else
840                                 arg_default_std_output = r;
841                         break;
842
843                 case ARG_DEFAULT_STD_ERROR:
844
845                         if ((r = exec_output_from_string(optarg)) < 0) {
846                                 log_error("Failed to parse default standard error output setting %s.", optarg);
847                                 return r;
848                         } else
849                                 arg_default_std_error = r;
850                         break;
851
852                 case ARG_UNIT:
853
854                         if ((r = set_default_unit(optarg)) < 0) {
855                                 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
856                                 return r;
857                         }
858
859                         break;
860
861                 case ARG_SYSTEM:
862                         arg_running_as = MANAGER_SYSTEM;
863                         break;
864
865                 case ARG_USER:
866                         arg_running_as = MANAGER_USER;
867                         break;
868
869                 case ARG_TEST:
870                         arg_action = ACTION_TEST;
871                         break;
872
873                 case ARG_DUMP_CONFIGURATION_ITEMS:
874                         arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
875                         break;
876
877                 case ARG_DUMP_CORE:
878                         r = optarg ? parse_boolean(optarg) : 1;
879                         if (r < 0) {
880                                 log_error("Failed to parse dump core boolean %s.", optarg);
881                                 return r;
882                         }
883                         arg_dump_core = r;
884                         break;
885
886                 case ARG_CRASH_SHELL:
887                         r = optarg ? parse_boolean(optarg) : 1;
888                         if (r < 0) {
889                                 log_error("Failed to parse crash shell boolean %s.", optarg);
890                                 return r;
891                         }
892                         arg_crash_shell = r;
893                         break;
894
895                 case ARG_CONFIRM_SPAWN:
896                         r = optarg ? parse_boolean(optarg) : 1;
897                         if (r < 0) {
898                                 log_error("Failed to parse confirm spawn boolean %s.", optarg);
899                                 return r;
900                         }
901                         arg_confirm_spawn = r;
902                         break;
903
904                 case ARG_SHOW_STATUS:
905                         r = optarg ? parse_boolean(optarg) : 1;
906                         if (r < 0) {
907                                 log_error("Failed to parse show status boolean %s.", optarg);
908                                 return r;
909                         }
910                         arg_show_status = r;
911                         break;
912
913 #ifdef HAVE_SYSV_COMPAT
914                 case ARG_SYSV_CONSOLE:
915                         r = optarg ? parse_boolean(optarg) : 1;
916                         if (r < 0) {
917                                 log_error("Failed to parse SysV console boolean %s.", optarg);
918                                 return r;
919                         }
920                         arg_sysv_console = r;
921                         break;
922 #endif
923
924                 case ARG_DESERIALIZE: {
925                         int fd;
926                         FILE *f;
927
928                         if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
929                                 log_error("Failed to parse deserialize option %s.", optarg);
930                                 return r;
931                         }
932
933                         if (!(f = fdopen(fd, "r"))) {
934                                 log_error("Failed to open serialization fd: %m");
935                                 return r;
936                         }
937
938                         if (serialization)
939                                 fclose(serialization);
940
941                         serialization = f;
942
943                         break;
944                 }
945
946                 case ARG_SWITCHEDROOT:
947                         /* Nothing special yet */
948                         break;
949
950                 case ARG_INTROSPECT: {
951                         const char * const * i = NULL;
952
953                         for (i = bus_interface_table; *i; i += 2)
954                                 if (!optarg || streq(i[0], optarg)) {
955                                         fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
956                                               "<node>\n", stdout);
957                                         fputs(i[1], stdout);
958                                         fputs("</node>\n", stdout);
959
960                                         if (optarg)
961                                                 break;
962                                 }
963
964                         if (!i[0] && optarg)
965                                 log_error("Unknown interface %s.", optarg);
966
967                         arg_action = ACTION_DONE;
968                         break;
969                 }
970
971                 case 'h':
972                         arg_action = ACTION_HELP;
973                         break;
974
975                 case 'D':
976                         log_set_max_level(LOG_DEBUG);
977                         break;
978
979                 case 'b':
980                 case 's':
981                 case 'z':
982                         /* Just to eat away the sysvinit kernel
983                          * cmdline args without getopt() error
984                          * messages that we'll parse in
985                          * parse_proc_cmdline_word() or ignore. */
986
987                 case '?':
988                 default:
989                         if (getpid() != 1) {
990                                 log_error("Unknown option code %c", c);
991                                 return -EINVAL;
992                         }
993
994                         break;
995                 }
996
997         if (optind < argc && getpid() != 1) {
998                 /* Hmm, when we aren't run as init system
999                  * let's complain about excess arguments */
1000
1001                 log_error("Excess arguments.");
1002                 return -EINVAL;
1003         }
1004
1005         if (detect_container(NULL) > 0) {
1006                 char **a;
1007
1008                 /* All /proc/cmdline arguments the kernel didn't
1009                  * understand it passed to us. We're not really
1010                  * interested in that usually since /proc/cmdline is
1011                  * more interesting and complete. With one exception:
1012                  * if we are run in a container /proc/cmdline is not
1013                  * relevant for the container, hence we rely on argv[]
1014                  * instead. */
1015
1016                 for (a = argv; a < argv + argc; a++)
1017                         if ((r = parse_proc_cmdline_word(*a)) < 0)
1018                                 return r;
1019         }
1020
1021         return 0;
1022 }
1023
1024 static int help(void) {
1025
1026         printf("%s [OPTIONS...]\n\n"
1027                "Starts up and maintains the system or user services.\n\n"
1028                "  -h --help                      Show this help\n"
1029                "     --test                      Determine startup sequence, dump it and exit\n"
1030                "     --dump-configuration-items  Dump understood unit configuration items\n"
1031                "     --introspect[=INTERFACE]    Extract D-Bus interface data\n"
1032                "     --unit=UNIT                 Set default unit\n"
1033                "     --system                    Run a system instance, even if PID != 1\n"
1034                "     --user                      Run a user instance\n"
1035                "     --dump-core[=0|1]           Dump core on crash\n"
1036                "     --crash-shell[=0|1]         Run shell on crash\n"
1037                "     --confirm-spawn[=0|1]       Ask for confirmation when spawning processes\n"
1038                "     --show-status[=0|1]         Show status updates on the console during bootup\n"
1039 #ifdef HAVE_SYSV_COMPAT
1040                "     --sysv-console[=0|1]        Connect output of SysV scripts to console\n"
1041 #endif
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 prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
1054         FILE *f = NULL;
1055         FDSet *fds = NULL;
1056         int r;
1057
1058         assert(m);
1059         assert(_f);
1060         assert(_fds);
1061
1062         /* Make sure nothing is really destructed when we shut down */
1063         m->n_reloading ++;
1064
1065         if ((r = manager_open_serialization(m, &f)) < 0) {
1066                 log_error("Failed to create serialization file: %s", strerror(-r));
1067                 goto fail;
1068         }
1069
1070         if (!(fds = fdset_new())) {
1071                 r = -ENOMEM;
1072                 log_error("Failed to allocate fd set: %s", strerror(-r));
1073                 goto fail;
1074         }
1075
1076         if ((r = manager_serialize(m, f, fds)) < 0) {
1077                 log_error("Failed to serialize state: %s", strerror(-r));
1078                 goto fail;
1079         }
1080
1081         if (fseeko(f, 0, SEEK_SET) < 0) {
1082                 log_error("Failed to rewind serialization fd: %m");
1083                 goto fail;
1084         }
1085
1086         if ((r = fd_cloexec(fileno(f), false)) < 0) {
1087                 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
1088                 goto fail;
1089         }
1090
1091         if ((r = fdset_cloexec(fds, false)) < 0) {
1092                 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
1093                 goto fail;
1094         }
1095
1096         *_f = f;
1097         *_fds = fds;
1098
1099         return 0;
1100
1101 fail:
1102         fdset_free(fds);
1103
1104         if (f)
1105                 fclose(f);
1106
1107         return r;
1108 }
1109
1110 static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
1111         const char *e;
1112         unsigned long long a, b;
1113
1114         assert(t);
1115
1116         e = getenv("RD_TIMESTAMP");
1117         if (!e)
1118                 return NULL;
1119
1120         if (sscanf(e, "%llu %llu", &a, &b) != 2)
1121                 return NULL;
1122
1123         t->realtime = (usec_t) a;
1124         t->monotonic = (usec_t) b;
1125
1126         return t;
1127 }
1128
1129 static void test_mtab(void) {
1130         char *p;
1131
1132         /* Check that /etc/mtab is a symlink */
1133
1134         if (readlink_malloc("/etc/mtab", &p) >= 0) {
1135                 bool b;
1136
1137                 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
1138                 free(p);
1139
1140                 if (b)
1141                         return;
1142         }
1143
1144         log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
1145                     "This is not supported anymore. "
1146                     "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
1147 }
1148
1149 static void test_usr(void) {
1150
1151         /* Check that /usr is not a separate fs */
1152
1153         if (dir_is_empty("/usr") <= 0)
1154                 return;
1155
1156         log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
1157                     "Some things will probably break (sometimes even silently) in mysterious ways. "
1158                     "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1159 }
1160
1161 static void test_cgroups(void) {
1162
1163         if (access("/proc/cgroups", F_OK) >= 0)
1164                 return;
1165
1166         log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
1167                     "Systems without control groups are not supported. "
1168                     "We will now sleep for 10s, and then continue boot-up. "
1169                     "Expect breakage and please do not file bugs. "
1170                     "Instead fix your kernel and enable CONFIG_CGROUPS. "
1171                     "Consult http://0pointer.de/blog/projects/cgroups-vs-cgroups.html for more information.");
1172
1173         sleep(10);
1174 }
1175
1176 static int do_switch_root(const char *switch_root) {
1177         int r=0;
1178         /*  Don't try to unmount the old "/", there's no way to do it. */
1179         const char *umounts[] = { "/dev", "/proc", "/sys", "/run", NULL };
1180         int i;
1181         int cfd = -1;
1182         struct stat switch_root_stat, sb;
1183
1184         if (path_equal(switch_root, "/"))
1185                 return 0;
1186
1187         if (stat(switch_root, &switch_root_stat) != 0) {
1188                 r = -errno;
1189                 log_error("failed to stat directory %s", switch_root);
1190                 goto fail;
1191         }
1192
1193         for (i = 0; umounts[i] != NULL; i++) {
1194                 char newmount[PATH_MAX];
1195
1196                 snprintf(newmount, sizeof(newmount), "%s%s", switch_root, umounts[i]);
1197
1198                 if ((stat(newmount, &sb) != 0) || (sb.st_dev != switch_root_stat.st_dev)) {
1199                         /* mount point seems to be mounted already or stat failed */
1200                         umount2(umounts[i], MNT_DETACH);
1201                         continue;
1202                 }
1203
1204                 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
1205                         log_error("failed to mount moving %s to %s",
1206                                   umounts[i], newmount);
1207                         log_error("forcing unmount of %s", umounts[i]);
1208                         umount2(umounts[i], MNT_FORCE);
1209                 }
1210         }
1211
1212         if (chdir(switch_root)) {
1213                 r = -errno;
1214                 log_error("failed to change directory to %s", switch_root);
1215                 goto fail;
1216         }
1217
1218         cfd = open("/", O_RDONLY);
1219
1220         if (mount(switch_root, "/", NULL, MS_MOVE, NULL) < 0) {
1221                 r = -errno;
1222                 log_error("failed to mount moving %s to /", switch_root);
1223                 goto fail;
1224         }
1225
1226         if (chroot(".")) {
1227                 r = -errno;
1228                 log_error("failed to change root");
1229                 goto fail;
1230         }
1231
1232         if (cfd >= 0) {
1233                 rm_rf_children(cfd, false, false);
1234                 close(cfd);
1235                 cfd=-1;
1236         }
1237
1238         return 0;
1239
1240 fail:
1241         if (cfd >= 0)
1242                 close(cfd);
1243
1244         log_error("Failed to switch root, ignoring: %s", strerror(-r));
1245
1246         return r;
1247 }
1248
1249 int main(int argc, char *argv[]) {
1250         Manager *m = NULL;
1251         int r, retval = EXIT_FAILURE;
1252         usec_t before_startup, after_startup;
1253         char timespan[FORMAT_TIMESPAN_MAX];
1254         FDSet *fds = NULL;
1255         bool reexecute = false;
1256         const char *shutdown_verb = NULL;
1257         dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
1258         static char systemd[] = "systemd";
1259         bool is_reexec = false;
1260         int j;
1261         bool loaded_policy = false;
1262         bool arm_reboot_watchdog = false;
1263         char *switch_root = NULL, *switch_root_init = NULL;
1264
1265 #ifdef HAVE_SYSV_COMPAT
1266         if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
1267                 /* This is compatibility support for SysV, where
1268                  * calling init as a user is identical to telinit. */
1269
1270                 errno = -ENOENT;
1271                 execv(SYSTEMCTL_BINARY_PATH, argv);
1272                 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1273                 return 1;
1274         }
1275 #endif
1276
1277         /* Determine if this is a reexecution or normal bootup. We do
1278          * the full command line parsing much later, so let's just
1279          * have a quick peek here. */
1280
1281         for (j = 1; j < argc; j++)
1282                 if (streq(argv[j], "--deserialize")) {
1283                         is_reexec = true;
1284                         break;
1285                 }
1286
1287         /* If we have switched root, do all the special things */
1288         for (j = 1; j < argc; j++)
1289                 if (streq(argv[j], "--switchedroot")) {
1290                         is_reexec = false;
1291                         break;
1292                 }
1293
1294         /* If we get started via the /sbin/init symlink then we are
1295            called 'init'. After a subsequent reexecution we are then
1296            called 'systemd'. That is confusing, hence let's call us
1297            systemd right-away. */
1298         program_invocation_short_name = systemd;
1299         prctl(PR_SET_NAME, systemd);
1300
1301         saved_argv = argv;
1302         saved_argc = argc;
1303
1304         log_show_color(isatty(STDERR_FILENO) > 0);
1305         log_show_location(false);
1306         log_set_max_level(LOG_INFO);
1307
1308         if (getpid() == 1) {
1309                 arg_running_as = MANAGER_SYSTEM;
1310                 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_JOURNAL : LOG_TARGET_JOURNAL_OR_KMSG);
1311
1312                 if (!is_reexec) {
1313                         if (selinux_setup(&loaded_policy) < 0)
1314                                 goto finish;
1315                         if (ima_setup() < 0)
1316                                 goto finish;
1317                 }
1318
1319                 log_open();
1320
1321                 if (label_init(NULL) < 0)
1322                         goto finish;
1323
1324                 if (!is_reexec)
1325                         if (hwclock_is_localtime() > 0) {
1326                                 int min;
1327
1328                                 r = hwclock_apply_localtime_delta(&min);
1329                                 if (r < 0)
1330                                         log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
1331                                 else
1332                                         log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
1333                         }
1334
1335         } else {
1336                 arg_running_as = MANAGER_USER;
1337                 log_set_target(LOG_TARGET_AUTO);
1338                 log_open();
1339         }
1340
1341         /* Initialize default unit */
1342         if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
1343                 goto finish;
1344
1345         /* By default, mount "cpu" and "cpuacct" together */
1346         arg_join_controllers = new(char**, 2);
1347         if (!arg_join_controllers)
1348                 goto finish;
1349
1350         arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
1351         arg_join_controllers[1] = NULL;
1352
1353         if (!arg_join_controllers[0])
1354                 goto finish;
1355
1356         /* Mount /proc, /sys and friends, so that /proc/cmdline and
1357          * /proc/$PID/fd is available. */
1358         if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
1359                 r = mount_setup(loaded_policy);
1360                 if (r < 0)
1361                         goto finish;
1362         }
1363
1364         /* Reset all signal handlers. */
1365         assert_se(reset_all_signal_handlers() == 0);
1366
1367         /* If we are init, we can block sigkill. Yay. */
1368         ignore_signals(SIGNALS_IGNORE, -1);
1369
1370         if (parse_config_file() < 0)
1371                 goto finish;
1372
1373         if (arg_running_as == MANAGER_SYSTEM)
1374                 if (parse_proc_cmdline() < 0)
1375                         goto finish;
1376
1377         log_parse_environment();
1378
1379         if (parse_argv(argc, argv) < 0)
1380                 goto finish;
1381
1382         if (arg_action == ACTION_TEST && geteuid() == 0) {
1383                 log_error("Don't run test mode as root.");
1384                 goto finish;
1385         }
1386
1387         if (arg_running_as == MANAGER_SYSTEM &&
1388             arg_action == ACTION_RUN &&
1389             running_in_chroot() > 0) {
1390                 log_error("Cannot be run in a chroot() environment.");
1391                 goto finish;
1392         }
1393
1394         if (arg_action == ACTION_HELP) {
1395                 retval = help();
1396                 goto finish;
1397         } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
1398                 unit_dump_config_items(stdout);
1399                 retval = EXIT_SUCCESS;
1400                 goto finish;
1401         } else if (arg_action == ACTION_DONE) {
1402                 retval = EXIT_SUCCESS;
1403                 goto finish;
1404         }
1405
1406         assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
1407
1408         /* Close logging fds, in order not to confuse fdset below */
1409         log_close();
1410
1411         /* Remember open file descriptors for later deserialization */
1412         if (serialization) {
1413                 r = fdset_new_fill(&fds);
1414                 if (r < 0) {
1415                         log_error("Failed to allocate fd set: %s", strerror(-r));
1416                         goto finish;
1417                 }
1418
1419                 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
1420         } else
1421                 close_all_fds(NULL, 0);
1422
1423         /* Set up PATH unless it is already set */
1424         setenv("PATH",
1425 #ifdef HAVE_SPLIT_USR
1426                "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
1427 #else
1428                "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
1429 #endif
1430                arg_running_as == MANAGER_SYSTEM);
1431
1432         if (arg_running_as == MANAGER_SYSTEM) {
1433                 /* Parse the data passed to us. We leave this
1434                  * variables set, but the manager later on will not
1435                  * pass them on to our children. */
1436                 parse_initrd_timestamp(&initrd_timestamp);
1437
1438                 /* Unset some environment variables passed in from the
1439                  * kernel that don't really make sense for us. */
1440                 unsetenv("HOME");
1441                 unsetenv("TERM");
1442
1443                 /* When we are invoked by a shell, these might be set,
1444                  * but make little sense to pass on */
1445                 unsetenv("PWD");
1446                 unsetenv("SHLVL");
1447                 unsetenv("_");
1448
1449                 /* When we are invoked by a tool chroot-like such as
1450                  * nspawn, these might be set, but make little sense
1451                  * to pass on */
1452                 unsetenv("USER");
1453                 unsetenv("LOGNAME");
1454
1455                 /* All other variables are left as is, so that clients
1456                  * can still read them via /proc/1/environ */
1457         }
1458
1459         /* Move out of the way, so that we won't block unmounts */
1460         assert_se(chdir("/")  == 0);
1461
1462         if (arg_running_as == MANAGER_SYSTEM) {
1463                 /* Become a session leader if we aren't one yet. */
1464                 setsid();
1465
1466                 /* Disable the umask logic */
1467                 umask(0);
1468         }
1469
1470         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1471         dbus_connection_set_change_sigpipe(FALSE);
1472
1473         /* Reset the console, but only if this is really init and we
1474          * are freshly booted */
1475         if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
1476                 console_setup(getpid() == 1 && !is_reexec);
1477                 make_null_stdio();
1478         }
1479
1480         /* Open the logging devices, if possible and necessary */
1481         log_open();
1482
1483         /* Make sure we leave a core dump without panicing the
1484          * kernel. */
1485         if (getpid() == 1)
1486                 install_crash_handler();
1487
1488         if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
1489                 r = mount_cgroup_controllers(arg_join_controllers);
1490                 if (r < 0)
1491                         goto finish;
1492         }
1493
1494         log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
1495                  PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
1496
1497         if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
1498                 locale_setup();
1499
1500                 if (arg_show_status || plymouth_running())
1501                         status_welcome();
1502
1503                 kmod_setup();
1504                 hostname_setup();
1505                 machine_id_setup();
1506                 loopback_setup();
1507
1508                 test_mtab();
1509                 test_usr();
1510                 test_cgroups();
1511         }
1512
1513         if (arg_running_as == MANAGER_SYSTEM && arg_runtime_watchdog > 0)
1514                 watchdog_set_timeout(&arg_runtime_watchdog);
1515
1516         r = manager_new(arg_running_as, &m);
1517         if (r < 0) {
1518                 log_error("Failed to allocate manager object: %s", strerror(-r));
1519                 goto finish;
1520         }
1521
1522         m->confirm_spawn = arg_confirm_spawn;
1523 #ifdef HAVE_SYSV_COMPAT
1524         m->sysv_console = arg_sysv_console;
1525 #endif
1526         m->default_std_output = arg_default_std_output;
1527         m->default_std_error = arg_default_std_error;
1528         m->runtime_watchdog = arg_runtime_watchdog;
1529         m->shutdown_watchdog = arg_shutdown_watchdog;
1530
1531         if (dual_timestamp_is_set(&initrd_timestamp))
1532                 m->initrd_timestamp = initrd_timestamp;
1533
1534         if (arg_default_controllers)
1535                 manager_set_default_controllers(m, arg_default_controllers);
1536
1537         manager_set_show_status(m, arg_show_status);
1538
1539         before_startup = now(CLOCK_MONOTONIC);
1540
1541         r = manager_startup(m, serialization, fds);
1542         if (r < 0)
1543                 log_error("Failed to fully start up daemon: %s", strerror(-r));
1544
1545         if (fds) {
1546                 /* This will close all file descriptors that were opened, but
1547                  * not claimed by any unit. */
1548
1549                 fdset_free(fds);
1550                 fds = NULL;
1551         }
1552
1553         if (serialization) {
1554                 fclose(serialization);
1555                 serialization = NULL;
1556         } else {
1557                 DBusError error;
1558                 Unit *target = NULL;
1559                 Job *default_unit_job;
1560
1561                 dbus_error_init(&error);
1562
1563                 log_debug("Activating default unit: %s", arg_default_unit);
1564
1565                 r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
1566                 if (r < 0) {
1567                         log_error("Failed to load default target: %s", bus_error(&error, r));
1568                         dbus_error_free(&error);
1569                 } else if (target->load_state == UNIT_ERROR)
1570                         log_error("Failed to load default target: %s", strerror(-target->load_error));
1571                 else if (target->load_state == UNIT_MASKED)
1572                         log_error("Default target masked.");
1573
1574                 if (!target || target->load_state != UNIT_LOADED) {
1575                         log_info("Trying to load rescue target...");
1576
1577                         r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
1578                         if (r < 0) {
1579                                 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1580                                 dbus_error_free(&error);
1581                                 goto finish;
1582                         } else if (target->load_state == UNIT_ERROR) {
1583                                 log_error("Failed to load rescue target: %s", strerror(-target->load_error));
1584                                 goto finish;
1585                         } else if (target->load_state == UNIT_MASKED) {
1586                                 log_error("Rescue target masked.");
1587                                 goto finish;
1588                         }
1589                 }
1590
1591                 assert(target->load_state == UNIT_LOADED);
1592
1593                 if (arg_action == ACTION_TEST) {
1594                         printf("-> By units:\n");
1595                         manager_dump_units(m, stdout, "\t");
1596                 }
1597
1598                 r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &default_unit_job);
1599                 if (r < 0) {
1600                         log_error("Failed to start default target: %s", bus_error(&error, r));
1601                         dbus_error_free(&error);
1602                         goto finish;
1603                 }
1604                 m->default_unit_job_id = default_unit_job->id;
1605
1606                 after_startup = now(CLOCK_MONOTONIC);
1607                 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1608                          "Loaded units and determined initial transaction in %s.",
1609                           format_timespan(timespan, sizeof(timespan), after_startup - before_startup));
1610
1611                 if (arg_action == ACTION_TEST) {
1612                         printf("-> By jobs:\n");
1613                         manager_dump_jobs(m, stdout, "\t");
1614                         retval = EXIT_SUCCESS;
1615                         goto finish;
1616                 }
1617         }
1618
1619         for (;;) {
1620                 r = manager_loop(m);
1621                 if (r < 0) {
1622                         log_error("Failed to run mainloop: %s", strerror(-r));
1623                         goto finish;
1624                 }
1625
1626                 switch (m->exit_code) {
1627
1628                 case MANAGER_EXIT:
1629                         retval = EXIT_SUCCESS;
1630                         log_debug("Exit.");
1631                         goto finish;
1632
1633                 case MANAGER_RELOAD:
1634                         log_info("Reloading.");
1635                         r = manager_reload(m);
1636                         if (r < 0)
1637                                 log_error("Failed to reload: %s", strerror(-r));
1638                         break;
1639
1640                 case MANAGER_REEXECUTE:
1641
1642                         if (prepare_reexecute(m, &serialization, &fds) < 0)
1643                                 goto finish;
1644
1645                         reexecute = true;
1646                         log_notice("Reexecuting.");
1647                         goto finish;
1648
1649                 case MANAGER_SWITCH_ROOT:
1650                         /* Steal the switch root parameters */
1651                         switch_root = m->switch_root;
1652                         switch_root_init = m->switch_root_init;
1653                         m->switch_root = m->switch_root_init = NULL;
1654
1655                         if (!switch_root_init)
1656                                 if (prepare_reexecute(m, &serialization, &fds) < 0)
1657                                         goto finish;
1658
1659                         reexecute = true;
1660                         log_notice("Switching root.");
1661                         goto finish;
1662
1663                 case MANAGER_REBOOT:
1664                 case MANAGER_POWEROFF:
1665                 case MANAGER_HALT:
1666                 case MANAGER_KEXEC: {
1667                         static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1668                                 [MANAGER_REBOOT] = "reboot",
1669                                 [MANAGER_POWEROFF] = "poweroff",
1670                                 [MANAGER_HALT] = "halt",
1671                                 [MANAGER_KEXEC] = "kexec"
1672                         };
1673
1674                         assert_se(shutdown_verb = table[m->exit_code]);
1675                         arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
1676
1677                         log_notice("Shutting down.");
1678                         goto finish;
1679                 }
1680
1681                 default:
1682                         assert_not_reached("Unknown exit code.");
1683                 }
1684         }
1685
1686 finish:
1687         if (m)
1688                 manager_free(m);
1689
1690         free(arg_default_unit);
1691         strv_free(arg_default_controllers);
1692         free_join_controllers();
1693
1694         dbus_shutdown();
1695         label_finish();
1696
1697         if (reexecute) {
1698                 const char **args;
1699                 unsigned i, args_size;
1700
1701                 /* Close and disarm the watchdog, so that the new
1702                  * instance can reinitialize it, but doesn't get
1703                  * rebooted while we do that */
1704                 watchdog_close(true);
1705
1706                 if (switch_root)
1707                         do_switch_root(switch_root);
1708
1709                 args_size = MAX(6, argc+1);
1710                 args = newa(const char*, args_size);
1711
1712                 if (!switch_root_init) {
1713                         char sfd[16];
1714
1715                         /* First try to spawn ourselves with the right
1716                          * path, and with full serialization. We do
1717                          * this only if the user didn't specify an
1718                          * explicit init to spawn. */
1719
1720                         assert(serialization);
1721                         assert(fds);
1722
1723                         snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1724                         char_array_0(sfd);
1725
1726                         i = 0;
1727                         args[i++] = SYSTEMD_BINARY_PATH;
1728                         if (switch_root)
1729                                 args[i++] = "--switchedroot";
1730                         args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
1731                         args[i++] = "--deserialize";
1732                         args[i++] = sfd;
1733                         args[i++] = NULL;
1734
1735                         assert(i <= args_size);
1736                         execv(args[0], (char* const*) args);
1737                 }
1738
1739                 /* Try the fallback, if there is any, without any
1740                  * serialization. We pass the original argv[] and
1741                  * envp[]. (Well, modulo the ordering changes due to
1742                  * getopt() in argv[], and some cleanups in envp[],
1743                  * but let's hope that doesn't matter.) */
1744
1745                 if (serialization) {
1746                         fclose(serialization);
1747                         serialization = NULL;
1748                 }
1749
1750                 if (fds) {
1751                         fdset_free(fds);
1752                         fds = NULL;
1753                 }
1754
1755                 for (j = 1, i = 1; j < argc; j++)
1756                         args[i++] = argv[j];
1757                 args[i++] = NULL;
1758                 assert(i <= args_size);
1759
1760                 if (switch_root_init) {
1761                         args[0] = switch_root_init;
1762                         execv(args[0], (char* const*) args);
1763                         log_warning("Failed to execute configured init, trying fallback: %m");
1764                 }
1765
1766                 args[0] = "/sbin/init";
1767                 execv(args[0], (char* const*) args);
1768
1769                 log_warning("Failed to execute /sbin/init, trying fallback: %m");
1770
1771                 args[0] = "/bin/sh";
1772                 args[1] = NULL;
1773                 execv(args[0], (char* const*) args);
1774                 log_error("Failed to execute /bin/sh, giving up: %m");
1775         }
1776
1777         if (serialization)
1778                 fclose(serialization);
1779
1780         if (fds)
1781                 fdset_free(fds);
1782
1783         if (shutdown_verb) {
1784                 const char * command_line[] = {
1785                         SYSTEMD_SHUTDOWN_BINARY_PATH,
1786                         shutdown_verb,
1787                         NULL
1788                 };
1789                 char **env_block;
1790
1791                 if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
1792                         char e[32];
1793
1794                         /* If we reboot let's set the shutdown
1795                          * watchdog and tell the shutdown binary to
1796                          * repeatedly ping it */
1797                         watchdog_set_timeout(&arg_shutdown_watchdog);
1798                         watchdog_close(false);
1799
1800                         /* Tell the binary how often to ping */
1801                         snprintf(e, sizeof(e), "WATCHDOG_USEC=%llu", (unsigned long long) arg_shutdown_watchdog);
1802                         char_array_0(e);
1803
1804                         env_block = strv_append(environ, e);
1805                 } else {
1806                         env_block = strv_copy(environ);
1807                         watchdog_close(true);
1808                 }
1809
1810                 execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
1811                 free(env_block);
1812                 log_error("Failed to execute shutdown binary, freezing: %m");
1813         }
1814
1815         if (getpid() == 1)
1816                 freeze();
1817
1818         return retval;
1819 }