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