chiark / gitweb /
1ff61849c8cd5a0d3fbbf25a1b6981cd9cc4ae88
[elogind.git] / src / 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 General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
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
36 #include "manager.h"
37 #include "log.h"
38 #include "mount-setup.h"
39 #include "hostname-setup.h"
40 #include "loopback-setup.h"
41 #include "kmod-setup.h"
42 #include "locale-setup.h"
43 #include "selinux-setup.h"
44 #include "machine-id-setup.h"
45 #include "load-fragment.h"
46 #include "fdset.h"
47 #include "special.h"
48 #include "conf-parser.h"
49 #include "bus-errors.h"
50 #include "missing.h"
51 #include "label.h"
52 #include "build.h"
53 #include "strv.h"
54 #include "def.h"
55
56 static enum {
57         ACTION_RUN,
58         ACTION_HELP,
59         ACTION_TEST,
60         ACTION_DUMP_CONFIGURATION_ITEMS,
61         ACTION_DONE
62 } arg_action = ACTION_RUN;
63
64 static char *arg_default_unit = NULL;
65 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
66
67 static bool arg_dump_core = true;
68 static bool arg_crash_shell = false;
69 static int arg_crash_chvt = -1;
70 static bool arg_confirm_spawn = false;
71 static bool arg_show_status = true;
72 #ifdef HAVE_SYSV_COMPAT
73 static bool arg_sysv_console = true;
74 #endif
75 static bool arg_mount_auto = true;
76 static bool arg_swap_auto = true;
77 static char **arg_default_controllers = NULL;
78 static ExecOutput arg_default_std_output = EXEC_OUTPUT_INHERIT;
79 static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
80
81 static FILE* serialization = NULL;
82
83 static void nop_handler(int sig) {
84 }
85
86 _noreturn_ static void crash(int sig) {
87
88         if (!arg_dump_core)
89                 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
90         else {
91                 struct sigaction sa;
92                 pid_t pid;
93
94                 /* We want to wait for the core process, hence let's enable SIGCHLD */
95                 zero(sa);
96                 sa.sa_handler = nop_handler;
97                 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
98                 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
99
100                 if ((pid = fork()) < 0)
101                         log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
102
103                 else if (pid == 0) {
104                         struct rlimit rl;
105
106                         /* Enable default signal handler for core dump */
107                         zero(sa);
108                         sa.sa_handler = SIG_DFL;
109                         assert_se(sigaction(sig, &sa, NULL) == 0);
110
111                         /* Don't limit the core dump size */
112                         zero(rl);
113                         rl.rlim_cur = RLIM_INFINITY;
114                         rl.rlim_max = RLIM_INFINITY;
115                         setrlimit(RLIMIT_CORE, &rl);
116
117                         /* Just to be sure... */
118                         assert_se(chdir("/") == 0);
119
120                         /* Raise the signal again */
121                         raise(sig);
122
123                         assert_not_reached("We shouldn't be here...");
124                         _exit(1);
125
126                 } else {
127                         siginfo_t status;
128                         int r;
129
130                         /* Order things nicely. */
131                         if ((r = wait_for_terminate(pid, &status)) < 0)
132                                 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
133                         else if (status.si_code != CLD_DUMPED)
134                                 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
135                         else
136                                 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
137                 }
138         }
139
140         if (arg_crash_chvt)
141                 chvt(arg_crash_chvt);
142
143         if (arg_crash_shell) {
144                 struct sigaction sa;
145                 pid_t pid;
146
147                 log_info("Executing crash shell in 10s...");
148                 sleep(10);
149
150                 /* Let the kernel reap children for us */
151                 zero(sa);
152                 sa.sa_handler = SIG_IGN;
153                 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
154                 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
155
156                 if ((pid = fork()) < 0)
157                         log_error("Failed to fork off crash shell: %s", strerror(errno));
158                 else if (pid == 0) {
159                         int fd, r;
160
161                         if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
162                                 log_error("Failed to acquire terminal: %s", strerror(-fd));
163                         else if ((r = make_stdio(fd)) < 0)
164                                 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
165
166                         execl("/bin/sh", "/bin/sh", NULL);
167
168                         log_error("execl() failed: %s", strerror(errno));
169                         _exit(1);
170                 }
171
172                 log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
173         }
174
175         log_info("Freezing execution.");
176         freeze();
177 }
178
179 static void install_crash_handler(void) {
180         struct sigaction sa;
181
182         zero(sa);
183
184         sa.sa_handler = crash;
185         sa.sa_flags = SA_NODEFER;
186
187         sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
188 }
189
190 static int console_setup(bool do_reset) {
191         int tty_fd, r;
192
193         /* If we are init, we connect stdin/stdout/stderr to /dev/null
194          * and make sure we don't have a controlling tty. */
195
196         release_terminal();
197
198         if (!do_reset)
199                 return 0;
200
201         if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
202                 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
203                 return -tty_fd;
204         }
205
206         if ((r = reset_terminal_fd(tty_fd)) < 0)
207                 log_error("Failed to reset /dev/console: %s", strerror(-r));
208
209         close_nointr_nofail(tty_fd);
210         return r;
211 }
212
213 static int set_default_unit(const char *u) {
214         char *c;
215
216         assert(u);
217
218         if (!(c = strdup(u)))
219                 return -ENOMEM;
220
221         free(arg_default_unit);
222         arg_default_unit = c;
223         return 0;
224 }
225
226 static int parse_proc_cmdline_word(const char *word) {
227
228         static const char * const rlmap[] = {
229                 "emergency", SPECIAL_EMERGENCY_TARGET,
230                 "-b",        SPECIAL_EMERGENCY_TARGET,
231                 "single",    SPECIAL_RESCUE_TARGET,
232                 "-s",        SPECIAL_RESCUE_TARGET,
233                 "s",         SPECIAL_RESCUE_TARGET,
234                 "S",         SPECIAL_RESCUE_TARGET,
235                 "1",         SPECIAL_RESCUE_TARGET,
236                 "2",         SPECIAL_RUNLEVEL2_TARGET,
237                 "3",         SPECIAL_RUNLEVEL3_TARGET,
238                 "4",         SPECIAL_RUNLEVEL4_TARGET,
239                 "5",         SPECIAL_RUNLEVEL5_TARGET,
240         };
241
242         assert(word);
243
244         if (startswith(word, "systemd.unit="))
245                 return set_default_unit(word + 13);
246
247         else if (startswith(word, "systemd.log_target=")) {
248
249                 if (log_set_target_from_string(word + 19) < 0)
250                         log_warning("Failed to parse log target %s. Ignoring.", word + 19);
251
252         } else if (startswith(word, "systemd.log_level=")) {
253
254                 if (log_set_max_level_from_string(word + 18) < 0)
255                         log_warning("Failed to parse log level %s. Ignoring.", word + 18);
256
257         } else if (startswith(word, "systemd.log_color=")) {
258
259                 if (log_show_color_from_string(word + 18) < 0)
260                         log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
261
262         } else if (startswith(word, "systemd.log_location=")) {
263
264                 if (log_show_location_from_string(word + 21) < 0)
265                         log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
266
267         } else if (startswith(word, "systemd.dump_core=")) {
268                 int r;
269
270                 if ((r = parse_boolean(word + 18)) < 0)
271                         log_warning("Failed to parse dump core switch %s, Ignoring.", word + 18);
272                 else
273                         arg_dump_core = r;
274
275         } else if (startswith(word, "systemd.crash_shell=")) {
276                 int r;
277
278                 if ((r = parse_boolean(word + 20)) < 0)
279                         log_warning("Failed to parse crash shell switch %s, Ignoring.", word + 20);
280                 else
281                         arg_crash_shell = r;
282
283         } else if (startswith(word, "systemd.confirm_spawn=")) {
284                 int r;
285
286                 if ((r = parse_boolean(word + 22)) < 0)
287                         log_warning("Failed to parse confirm spawn switch %s, Ignoring.", word + 22);
288                 else
289                         arg_confirm_spawn = r;
290
291         } else if (startswith(word, "systemd.crash_chvt=")) {
292                 int k;
293
294                 if (safe_atoi(word + 19, &k) < 0)
295                         log_warning("Failed to parse crash chvt switch %s, Ignoring.", word + 19);
296                 else
297                         arg_crash_chvt = k;
298
299         } else if (startswith(word, "systemd.show_status=")) {
300                 int r;
301
302                 if ((r = parse_boolean(word + 20)) < 0)
303                         log_warning("Failed to parse show status switch %s, Ignoring.", word + 20);
304                 else
305                         arg_show_status = r;
306         } else if (startswith(word, "systemd.default_standard_output=")) {
307                 int r;
308
309                 if ((r = exec_output_from_string(word + 32)) < 0)
310                         log_warning("Failed to parse default standard output switch %s, Ignoring.", word + 32);
311                 else
312                         arg_default_std_output = r;
313         } else if (startswith(word, "systemd.default_standard_error=")) {
314                 int r;
315
316                 if ((r = exec_output_from_string(word + 31)) < 0)
317                         log_warning("Failed to parse default standard error switch %s, Ignoring.", word + 31);
318                 else
319                         arg_default_std_error = r;
320 #ifdef HAVE_SYSV_COMPAT
321         } else if (startswith(word, "systemd.sysv_console=")) {
322                 int r;
323
324                 if ((r = parse_boolean(word + 21)) < 0)
325                         log_warning("Failed to parse SysV console switch %s, Ignoring.", word + 20);
326                 else
327                         arg_sysv_console = r;
328 #endif
329
330         } else if (startswith(word, "systemd.")) {
331
332                 log_warning("Unknown kernel switch %s. Ignoring.", word);
333
334                 log_info("Supported kernel switches:\n"
335                          "systemd.unit=UNIT                        Default unit to start\n"
336                          "systemd.dump_core=0|1                    Dump core on crash\n"
337                          "systemd.crash_shell=0|1                  Run shell on crash\n"
338                          "systemd.crash_chvt=N                     Change to VT #N on crash\n"
339                          "systemd.confirm_spawn=0|1                Confirm every process spawn\n"
340                          "systemd.show_status=0|1                  Show status updates on the console during bootup\n"
341 #ifdef HAVE_SYSV_COMPAT
342                          "systemd.sysv_console=0|1                 Connect output of SysV scripts to console\n"
343 #endif
344                          "systemd.log_target=console|kmsg|syslog|syslog-or-kmsg|null\n"
345                          "                                         Log target\n"
346                          "systemd.log_level=LEVEL                  Log level\n"
347                          "systemd.log_color=0|1                    Highlight important log messages\n"
348                          "systemd.log_location=0|1                 Include code location in log messages\n"
349                          "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
350                          "                                         Set default log output for services\n"
351                          "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console\n"
352                          "                                         Set default log error output for services\n");
353
354         } else if (streq(word, "quiet")) {
355                 arg_show_status = false;
356 #ifdef HAVE_SYSV_COMPAT
357                 arg_sysv_console = false;
358 #endif
359         } else {
360                 unsigned i;
361
362                 /* SysV compatibility */
363                 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
364                         if (streq(word, rlmap[i]))
365                                 return set_default_unit(rlmap[i+1]);
366         }
367
368         return 0;
369 }
370
371 static int config_parse_level2(
372                 const char *filename,
373                 unsigned line,
374                 const char *section,
375                 const char *lvalue,
376                 int ltype,
377                 const char *rvalue,
378                 void *data,
379                 void *userdata) {
380
381         assert(filename);
382         assert(lvalue);
383         assert(rvalue);
384
385         log_set_max_level_from_string(rvalue);
386         return 0;
387 }
388
389 static int config_parse_target(
390                 const char *filename,
391                 unsigned line,
392                 const char *section,
393                 const char *lvalue,
394                 int ltype,
395                 const char *rvalue,
396                 void *data,
397                 void *userdata) {
398
399         assert(filename);
400         assert(lvalue);
401         assert(rvalue);
402
403         log_set_target_from_string(rvalue);
404         return 0;
405 }
406
407 static int config_parse_color(
408                 const char *filename,
409                 unsigned line,
410                 const char *section,
411                 const char *lvalue,
412                 int ltype,
413                 const char *rvalue,
414                 void *data,
415                 void *userdata) {
416
417         assert(filename);
418         assert(lvalue);
419         assert(rvalue);
420
421         log_show_color_from_string(rvalue);
422         return 0;
423 }
424
425 static int config_parse_location(
426                 const char *filename,
427                 unsigned line,
428                 const char *section,
429                 const char *lvalue,
430                 int ltype,
431                 const char *rvalue,
432                 void *data,
433                 void *userdata) {
434
435         assert(filename);
436         assert(lvalue);
437         assert(rvalue);
438
439         log_show_location_from_string(rvalue);
440         return 0;
441 }
442
443 static int config_parse_cpu_affinity2(
444                 const char *filename,
445                 unsigned line,
446                 const char *section,
447                 const char *lvalue,
448                 int ltype,
449                 const char *rvalue,
450                 void *data,
451                 void *userdata) {
452
453         char *w;
454         size_t l;
455         char *state;
456         cpu_set_t *c = NULL;
457         unsigned ncpus = 0;
458
459         assert(filename);
460         assert(lvalue);
461         assert(rvalue);
462
463         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
464                 char *t;
465                 int r;
466                 unsigned cpu;
467
468                 if (!(t = strndup(w, l)))
469                         return -ENOMEM;
470
471                 r = safe_atou(t, &cpu);
472                 free(t);
473
474                 if (!c)
475                         if (!(c = cpu_set_malloc(&ncpus)))
476                                 return -ENOMEM;
477
478                 if (r < 0 || cpu >= ncpus) {
479                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
480                         CPU_FREE(c);
481                         return -EBADMSG;
482                 }
483
484                 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
485         }
486
487         if (c) {
488                 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
489                         log_warning("Failed to set CPU affinity: %m");
490
491                 CPU_FREE(c);
492         }
493
494         return 0;
495 }
496
497 static int parse_config_file(void) {
498
499         const ConfigTableItem items[] = {
500                 { "Manager", "LogLevel",              config_parse_level2,       0, NULL                     },
501                 { "Manager", "LogTarget",             config_parse_target,       0, NULL                     },
502                 { "Manager", "LogColor",              config_parse_color,        0, NULL                     },
503                 { "Manager", "LogLocation",           config_parse_location,     0, NULL                     },
504                 { "Manager", "DumpCore",              config_parse_bool,         0, &arg_dump_core           },
505                 { "Manager", "CrashShell",            config_parse_bool,         0, &arg_crash_shell         },
506                 { "Manager", "ShowStatus",            config_parse_bool,         0, &arg_show_status         },
507 #ifdef HAVE_SYSV_COMPAT
508                 { "Manager", "SysVConsole",           config_parse_bool,         0, &arg_sysv_console        },
509 #endif
510                 { "Manager", "CrashChVT",             config_parse_int,          0, &arg_crash_chvt          },
511                 { "Manager", "CPUAffinity",           config_parse_cpu_affinity2, 0, NULL                    },
512                 { "Manager", "MountAuto",             config_parse_bool,         0, &arg_mount_auto          },
513                 { "Manager", "SwapAuto",              config_parse_bool,         0, &arg_swap_auto           },
514                 { "Manager", "DefaultControllers",    config_parse_strv,         0, &arg_default_controllers },
515                 { "Manager", "DefaultStandardOutput", config_parse_output,       0, &arg_default_std_output  },
516                 { "Manager", "DefaultStandardError",  config_parse_output,       0, &arg_default_std_error   },
517                 { NULL, NULL, NULL, 0, NULL }
518         };
519
520         FILE *f;
521         const char *fn;
522         int r;
523
524         fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
525         f = fopen(fn, "re");
526         if (!f) {
527                 if (errno == ENOENT)
528                         return 0;
529
530                 log_warning("Failed to open configuration file '%s': %m", fn);
531                 return 0;
532         }
533
534         r = config_parse(fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, NULL);
535         if (r < 0)
536                 log_warning("Failed to parse configuration file: %s", strerror(-r));
537
538         fclose(f);
539
540         return 0;
541 }
542
543 static int parse_proc_cmdline(void) {
544         char *line, *w, *state;
545         int r;
546         size_t l;
547
548         /* Don't read /proc/cmdline if we are in a container, since
549          * that is only relevant for the host system */
550         if (detect_container(NULL) > 0)
551                 return 0;
552
553         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
554                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
555                 return 0;
556         }
557
558         FOREACH_WORD_QUOTED(w, l, line, state) {
559                 char *word;
560
561                 if (!(word = strndup(w, l))) {
562                         r = -ENOMEM;
563                         goto finish;
564                 }
565
566                 r = parse_proc_cmdline_word(word);
567                 free(word);
568
569                 if (r < 0)
570                         goto finish;
571         }
572
573         r = 0;
574
575 finish:
576         free(line);
577         return r;
578 }
579
580 static int parse_argv(int argc, char *argv[]) {
581
582         enum {
583                 ARG_LOG_LEVEL = 0x100,
584                 ARG_LOG_TARGET,
585                 ARG_LOG_COLOR,
586                 ARG_LOG_LOCATION,
587                 ARG_UNIT,
588                 ARG_SYSTEM,
589                 ARG_USER,
590                 ARG_TEST,
591                 ARG_DUMP_CONFIGURATION_ITEMS,
592                 ARG_DUMP_CORE,
593                 ARG_CRASH_SHELL,
594                 ARG_CONFIRM_SPAWN,
595                 ARG_SHOW_STATUS,
596                 ARG_SYSV_CONSOLE,
597                 ARG_DESERIALIZE,
598                 ARG_INTROSPECT,
599                 ARG_DEFAULT_STD_OUTPUT,
600                 ARG_DEFAULT_STD_ERROR
601         };
602
603         static const struct option options[] = {
604                 { "log-level",                required_argument, NULL, ARG_LOG_LEVEL                },
605                 { "log-target",               required_argument, NULL, ARG_LOG_TARGET               },
606                 { "log-color",                optional_argument, NULL, ARG_LOG_COLOR                },
607                 { "log-location",             optional_argument, NULL, ARG_LOG_LOCATION             },
608                 { "unit",                     required_argument, NULL, ARG_UNIT                     },
609                 { "system",                   no_argument,       NULL, ARG_SYSTEM                   },
610                 { "user",                     no_argument,       NULL, ARG_USER                     },
611                 { "test",                     no_argument,       NULL, ARG_TEST                     },
612                 { "help",                     no_argument,       NULL, 'h'                          },
613                 { "dump-configuration-items", no_argument,       NULL, ARG_DUMP_CONFIGURATION_ITEMS },
614                 { "dump-core",                no_argument,       NULL, ARG_DUMP_CORE                },
615                 { "crash-shell",              no_argument,       NULL, ARG_CRASH_SHELL              },
616                 { "confirm-spawn",            no_argument,       NULL, ARG_CONFIRM_SPAWN            },
617                 { "show-status",              optional_argument, NULL, ARG_SHOW_STATUS              },
618 #ifdef HAVE_SYSV_COMPAT
619                 { "sysv-console",             optional_argument, NULL, ARG_SYSV_CONSOLE             },
620 #endif
621                 { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
622                 { "introspect",               optional_argument, NULL, ARG_INTROSPECT               },
623                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
624                 { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
625                 { NULL,                       0,                 NULL, 0                            }
626         };
627
628         int c, r;
629
630         assert(argc >= 1);
631         assert(argv);
632
633         if (getpid() == 1)
634                 opterr = 0;
635
636         while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
637
638                 switch (c) {
639
640                 case ARG_LOG_LEVEL:
641                         if ((r = log_set_max_level_from_string(optarg)) < 0) {
642                                 log_error("Failed to parse log level %s.", optarg);
643                                 return r;
644                         }
645
646                         break;
647
648                 case ARG_LOG_TARGET:
649
650                         if ((r = log_set_target_from_string(optarg)) < 0) {
651                                 log_error("Failed to parse log target %s.", optarg);
652                                 return r;
653                         }
654
655                         break;
656
657                 case ARG_LOG_COLOR:
658
659                         if (optarg) {
660                                 if ((r = log_show_color_from_string(optarg)) < 0) {
661                                         log_error("Failed to parse log color setting %s.", optarg);
662                                         return r;
663                                 }
664                         } else
665                                 log_show_color(true);
666
667                         break;
668
669                 case ARG_LOG_LOCATION:
670
671                         if (optarg) {
672                                 if ((r = log_show_location_from_string(optarg)) < 0) {
673                                         log_error("Failed to parse log location setting %s.", optarg);
674                                         return r;
675                                 }
676                         } else
677                                 log_show_location(true);
678
679                         break;
680
681                 case ARG_DEFAULT_STD_OUTPUT:
682
683                         if ((r = exec_output_from_string(optarg)) < 0) {
684                                 log_error("Failed to parse default standard output setting %s.", optarg);
685                                 return r;
686                         } else
687                                 arg_default_std_output = r;
688                         break;
689
690                 case ARG_DEFAULT_STD_ERROR:
691
692                         if ((r = exec_output_from_string(optarg)) < 0) {
693                                 log_error("Failed to parse default standard error output setting %s.", optarg);
694                                 return r;
695                         } else
696                                 arg_default_std_error = r;
697                         break;
698
699                 case ARG_UNIT:
700
701                         if ((r = set_default_unit(optarg)) < 0) {
702                                 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
703                                 return r;
704                         }
705
706                         break;
707
708                 case ARG_SYSTEM:
709                         arg_running_as = MANAGER_SYSTEM;
710                         break;
711
712                 case ARG_USER:
713                         arg_running_as = MANAGER_USER;
714                         break;
715
716                 case ARG_TEST:
717                         arg_action = ACTION_TEST;
718                         break;
719
720                 case ARG_DUMP_CONFIGURATION_ITEMS:
721                         arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
722                         break;
723
724                 case ARG_DUMP_CORE:
725                         arg_dump_core = true;
726                         break;
727
728                 case ARG_CRASH_SHELL:
729                         arg_crash_shell = true;
730                         break;
731
732                 case ARG_CONFIRM_SPAWN:
733                         arg_confirm_spawn = true;
734                         break;
735
736                 case ARG_SHOW_STATUS:
737
738                         if (optarg) {
739                                 if ((r = parse_boolean(optarg)) < 0) {
740                                         log_error("Failed to show status boolean %s.", optarg);
741                                         return r;
742                                 }
743                                 arg_show_status = r;
744                         } else
745                                 arg_show_status = true;
746                         break;
747 #ifdef HAVE_SYSV_COMPAT
748                 case ARG_SYSV_CONSOLE:
749
750                         if (optarg) {
751                                 if ((r = parse_boolean(optarg)) < 0) {
752                                         log_error("Failed to SysV console boolean %s.", optarg);
753                                         return r;
754                                 }
755                                 arg_sysv_console = r;
756                         } else
757                                 arg_sysv_console = true;
758                         break;
759 #endif
760
761                 case ARG_DESERIALIZE: {
762                         int fd;
763                         FILE *f;
764
765                         if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
766                                 log_error("Failed to parse deserialize option %s.", optarg);
767                                 return r;
768                         }
769
770                         if (!(f = fdopen(fd, "r"))) {
771                                 log_error("Failed to open serialization fd: %m");
772                                 return r;
773                         }
774
775                         if (serialization)
776                                 fclose(serialization);
777
778                         serialization = f;
779
780                         break;
781                 }
782
783                 case ARG_INTROSPECT: {
784                         const char * const * i = NULL;
785
786                         for (i = bus_interface_table; *i; i += 2)
787                                 if (!optarg || streq(i[0], optarg)) {
788                                         fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
789                                               "<node>\n", stdout);
790                                         fputs(i[1], stdout);
791                                         fputs("</node>\n", stdout);
792
793                                         if (optarg)
794                                                 break;
795                                 }
796
797                         if (!i[0] && optarg)
798                                 log_error("Unknown interface %s.", optarg);
799
800                         arg_action = ACTION_DONE;
801                         break;
802                 }
803
804                 case 'h':
805                         arg_action = ACTION_HELP;
806                         break;
807
808                 case 'D':
809                         log_set_max_level(LOG_DEBUG);
810                         break;
811
812                 case 'b':
813                 case 's':
814                 case 'z':
815                         /* Just to eat away the sysvinit kernel
816                          * cmdline args without getopt() error
817                          * messages that we'll parse in
818                          * parse_proc_cmdline_word() or ignore. */
819
820                 case '?':
821                 default:
822                         if (getpid() != 1) {
823                                 log_error("Unknown option code %c", c);
824                                 return -EINVAL;
825                         }
826
827                         break;
828                 }
829
830         if (optind < argc && getpid() != 1) {
831                 /* Hmm, when we aren't run as init system
832                  * let's complain about excess arguments */
833
834                 log_error("Excess arguments.");
835                 return -EINVAL;
836         }
837
838         if (detect_container(NULL) > 0) {
839                 char **a;
840
841                 /* All /proc/cmdline arguments the kernel didn't
842                  * understand it passed to us. We're not really
843                  * interested in that usually since /proc/cmdline is
844                  * more interesting and complete. With one exception:
845                  * if we are run in a container /proc/cmdline is not
846                  * relevant for the container, hence we rely on argv[]
847                  * instead. */
848
849                 for (a = argv; a < argv + argc; a++)
850                         if ((r = parse_proc_cmdline_word(*a)) < 0)
851                                 return r;
852         }
853
854         return 0;
855 }
856
857 static int help(void) {
858
859         printf("%s [OPTIONS...]\n\n"
860                "Starts up and maintains the system or user services.\n\n"
861                "  -h --help                      Show this help\n"
862                "     --test                      Determine startup sequence, dump it and exit\n"
863                "     --dump-configuration-items  Dump understood unit configuration items\n"
864                "     --introspect[=INTERFACE]    Extract D-Bus interface data\n"
865                "     --unit=UNIT                 Set default unit\n"
866                "     --system                    Run a system instance, even if PID != 1\n"
867                "     --user                      Run a user instance\n"
868                "     --dump-core                 Dump core on crash\n"
869                "     --crash-shell               Run shell on crash\n"
870                "     --confirm-spawn             Ask for confirmation when spawning processes\n"
871                "     --show-status[=0|1]         Show status updates on the console during bootup\n"
872 #ifdef HAVE_SYSV_COMPAT
873                "     --sysv-console[=0|1]        Connect output of SysV scripts to console\n"
874 #endif
875                "     --log-target=TARGET         Set log target (console, syslog, kmsg, syslog-or-kmsg, null)\n"
876                "     --log-level=LEVEL           Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
877                "     --log-color[=0|1]           Highlight important log messages\n"
878                "     --log-location[=0|1]        Include code location in log messages\n"
879                "     --default-standard-output=  Set default standard output for services\n"
880                "     --default-standard-error=   Set default standard error output for services\n",
881                program_invocation_short_name);
882
883         return 0;
884 }
885
886 static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
887         FILE *f = NULL;
888         FDSet *fds = NULL;
889         int r;
890
891         assert(m);
892         assert(_f);
893         assert(_fds);
894
895         /* Make sure nothing is really destructed when we shut down */
896         m->n_reloading ++;
897
898         if ((r = manager_open_serialization(m, &f)) < 0) {
899                 log_error("Failed to create serialization file: %s", strerror(-r));
900                 goto fail;
901         }
902
903         if (!(fds = fdset_new())) {
904                 r = -ENOMEM;
905                 log_error("Failed to allocate fd set: %s", strerror(-r));
906                 goto fail;
907         }
908
909         if ((r = manager_serialize(m, f, fds)) < 0) {
910                 log_error("Failed to serialize state: %s", strerror(-r));
911                 goto fail;
912         }
913
914         if (fseeko(f, 0, SEEK_SET) < 0) {
915                 log_error("Failed to rewind serialization fd: %m");
916                 goto fail;
917         }
918
919         if ((r = fd_cloexec(fileno(f), false)) < 0) {
920                 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
921                 goto fail;
922         }
923
924         if ((r = fdset_cloexec(fds, false)) < 0) {
925                 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
926                 goto fail;
927         }
928
929         *_f = f;
930         *_fds = fds;
931
932         return 0;
933
934 fail:
935         fdset_free(fds);
936
937         if (f)
938                 fclose(f);
939
940         return r;
941 }
942
943 static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
944         const char *e;
945         unsigned long long a, b;
946
947         assert(t);
948
949         if (!(e = getenv("RD_TIMESTAMP")))
950                 return NULL;
951
952         if (sscanf(e, "%llu %llu", &a, &b) != 2)
953                 return NULL;
954
955         t->realtime = (usec_t) a;
956         t->monotonic = (usec_t) b;
957
958         return t;
959 }
960
961 static void test_mtab(void) {
962         char *p;
963
964         /* Check that /etc/mtab is a symlink */
965
966         if (readlink_malloc("/etc/mtab", &p) >= 0) {
967                 bool b;
968
969                 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
970                 free(p);
971
972                 if (b)
973                         return;
974         }
975
976         log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
977                     "This is not supported anymore. "
978                     "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
979 }
980
981 static void test_usr(void) {
982
983         /* Check that /usr is not a separate fs */
984
985         if (dir_is_empty("/usr") <= 0)
986                 return;
987
988         log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
989                     "Some things will probably break (sometimes even silently) in mysterious ways. "
990                     "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
991 }
992
993 static void test_cgroups(void) {
994
995         if (access("/proc/cgroups", F_OK) >= 0)
996                 return;
997
998         log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
999                     "Systems without control groups are not supported. "
1000                     "We will now sleep for 10s, and then continue boot-up. "
1001                     "Expect breakage and please do not file bugs. "
1002                     "Instead fix your kernel and enable CONFIG_CGROUPS." );
1003
1004         sleep(10);
1005 }
1006
1007 int main(int argc, char *argv[]) {
1008         Manager *m = NULL;
1009         int r, retval = EXIT_FAILURE;
1010         usec_t before_startup, after_startup;
1011         char timespan[FORMAT_TIMESPAN_MAX];
1012         FDSet *fds = NULL;
1013         bool reexecute = false;
1014         const char *shutdown_verb = NULL;
1015         dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
1016         char systemd[] = "systemd";
1017         bool is_reexec = false;
1018         int j;
1019         bool loaded_policy = false;
1020
1021 #ifdef HAVE_SYSV_COMPAT
1022         if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
1023                 /* This is compatibility support for SysV, where
1024                  * calling init as a user is identical to telinit. */
1025
1026                 errno = -ENOENT;
1027                 execv(SYSTEMCTL_BINARY_PATH, argv);
1028                 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1029                 return 1;
1030         }
1031 #endif
1032
1033         /* Determine if this is a reexecution or normal bootup. We do
1034          * the full command line parsing much later, so let's just
1035          * have a quick peek here. */
1036
1037         for (j = 1; j < argc; j++)
1038                 if (streq(argv[j], "--deserialize")) {
1039                         is_reexec = true;
1040                         break;
1041                 }
1042
1043         /* If we get started via the /sbin/init symlink then we are
1044            called 'init'. After a subsequent reexecution we are then
1045            called 'systemd'. That is confusing, hence let's call us
1046            systemd right-away. */
1047
1048         program_invocation_short_name = systemd;
1049         prctl(PR_SET_NAME, systemd);
1050         saved_argv = argv;
1051         saved_argc = argc;
1052
1053         log_show_color(isatty(STDERR_FILENO) > 0);
1054         log_show_location(false);
1055         log_set_max_level(LOG_INFO);
1056
1057         if (getpid() == 1) {
1058                 arg_running_as = MANAGER_SYSTEM;
1059                 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_SYSLOG_OR_KMSG);
1060
1061                 if (!is_reexec)
1062                         if (selinux_setup(&loaded_policy) < 0)
1063                                 goto finish;
1064
1065                 log_open();
1066
1067                 if (label_init() < 0)
1068                         goto finish;
1069
1070                 if (!is_reexec)
1071                         if (hwclock_is_localtime() > 0) {
1072                                 int min;
1073
1074                                 r = hwclock_apply_localtime_delta(&min);
1075                                 if (r < 0)
1076                                         log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
1077                                 else
1078                                         log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
1079                         }
1080
1081         } else {
1082                 arg_running_as = MANAGER_USER;
1083                 log_set_target(LOG_TARGET_AUTO);
1084                 log_open();
1085         }
1086
1087         if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
1088                 goto finish;
1089
1090         /* Mount /proc, /sys and friends, so that /proc/cmdline and
1091          * /proc/$PID/fd is available. */
1092         if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS"))
1093                 if (mount_setup(loaded_policy) < 0)
1094                         goto finish;
1095
1096         /* Reset all signal handlers. */
1097         assert_se(reset_all_signal_handlers() == 0);
1098
1099         /* If we are init, we can block sigkill. Yay. */
1100         ignore_signals(SIGNALS_IGNORE, -1);
1101
1102         if (parse_config_file() < 0)
1103                 goto finish;
1104
1105         if (arg_running_as == MANAGER_SYSTEM)
1106                 if (parse_proc_cmdline() < 0)
1107                         goto finish;
1108
1109         log_parse_environment();
1110
1111         if (parse_argv(argc, argv) < 0)
1112                 goto finish;
1113
1114         if (arg_action == ACTION_TEST && geteuid() == 0) {
1115                 log_error("Don't run test mode as root.");
1116                 goto finish;
1117         }
1118
1119         if (arg_running_as == MANAGER_SYSTEM &&
1120             arg_action == ACTION_RUN &&
1121             running_in_chroot() > 0) {
1122                 log_error("Cannot be run in a chroot() environment.");
1123                 goto finish;
1124         }
1125
1126         if (arg_action == ACTION_HELP) {
1127                 retval = help();
1128                 goto finish;
1129         } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
1130                 unit_dump_config_items(stdout);
1131                 retval = EXIT_SUCCESS;
1132                 goto finish;
1133         } else if (arg_action == ACTION_DONE) {
1134                 retval = EXIT_SUCCESS;
1135                 goto finish;
1136         }
1137
1138         assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
1139
1140         /* Close logging fds, in order not to confuse fdset below */
1141         log_close();
1142
1143         /* Remember open file descriptors for later deserialization */
1144         if (serialization) {
1145                 if ((r = fdset_new_fill(&fds)) < 0) {
1146                         log_error("Failed to allocate fd set: %s", strerror(-r));
1147                         goto finish;
1148                 }
1149
1150                 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
1151         } else
1152                 close_all_fds(NULL, 0);
1153
1154         /* Set up PATH unless it is already set */
1155         setenv("PATH",
1156                "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
1157                arg_running_as == MANAGER_SYSTEM);
1158
1159         if (arg_running_as == MANAGER_SYSTEM) {
1160                 /* Parse the data passed to us by the initrd and unset it */
1161                 parse_initrd_timestamp(&initrd_timestamp);
1162                 filter_environ("RD_");
1163
1164                 /* Unset some environment variables passed in from the
1165                  * kernel that don't really make sense for us. */
1166                 unsetenv("HOME");
1167                 unsetenv("TERM");
1168
1169                 /* All other variables are left as is, so that clients
1170                  * can still read them via /proc/1/environ */
1171         }
1172
1173         /* Move out of the way, so that we won't block unmounts */
1174         assert_se(chdir("/")  == 0);
1175
1176         if (arg_running_as == MANAGER_SYSTEM) {
1177                 /* Become a session leader if we aren't one yet. */
1178                 setsid();
1179
1180                 /* Disable the umask logic */
1181                 umask(0);
1182         }
1183
1184         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1185         dbus_connection_set_change_sigpipe(FALSE);
1186
1187         /* Reset the console, but only if this is really init and we
1188          * are freshly booted */
1189         if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
1190                 console_setup(getpid() == 1 && !is_reexec);
1191                 make_null_stdio();
1192         }
1193
1194         /* Open the logging devices, if possible and necessary */
1195         log_open();
1196
1197         /* Make sure we leave a core dump without panicing the
1198          * kernel. */
1199         if (getpid() == 1)
1200                 install_crash_handler();
1201
1202         log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
1203                  PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
1204
1205         if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
1206                 locale_setup();
1207
1208                 if (arg_show_status || plymouth_running())
1209                         status_welcome();
1210
1211                 kmod_setup();
1212                 hostname_setup();
1213                 machine_id_setup();
1214                 loopback_setup();
1215
1216                 test_mtab();
1217                 test_usr();
1218                 test_cgroups();
1219         }
1220
1221         if ((r = manager_new(arg_running_as, &m)) < 0) {
1222                 log_error("Failed to allocate manager object: %s", strerror(-r));
1223                 goto finish;
1224         }
1225
1226         m->confirm_spawn = arg_confirm_spawn;
1227         m->show_status = arg_show_status;
1228 #ifdef HAVE_SYSV_COMPAT
1229         m->sysv_console = arg_sysv_console;
1230 #endif
1231         m->mount_auto = arg_mount_auto;
1232         m->swap_auto = arg_swap_auto;
1233         m->default_std_output = arg_default_std_output;
1234         m->default_std_error = arg_default_std_error;
1235
1236         if (dual_timestamp_is_set(&initrd_timestamp))
1237                 m->initrd_timestamp = initrd_timestamp;
1238
1239         if (arg_default_controllers)
1240                 manager_set_default_controllers(m, arg_default_controllers);
1241
1242         before_startup = now(CLOCK_MONOTONIC);
1243
1244         if ((r = manager_startup(m, serialization, fds)) < 0)
1245                 log_error("Failed to fully start up daemon: %s", strerror(-r));
1246
1247         if (fds) {
1248                 /* This will close all file descriptors that were opened, but
1249                  * not claimed by any unit. */
1250
1251                 fdset_free(fds);
1252                 fds = NULL;
1253         }
1254
1255         if (serialization) {
1256                 fclose(serialization);
1257                 serialization = NULL;
1258         } else {
1259                 DBusError error;
1260                 Unit *target = NULL;
1261
1262                 dbus_error_init(&error);
1263
1264                 log_debug("Activating default unit: %s", arg_default_unit);
1265
1266                 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1267                         log_error("Failed to load default target: %s", bus_error(&error, r));
1268                         dbus_error_free(&error);
1269                 } else if (target->meta.load_state == UNIT_ERROR)
1270                         log_error("Failed to load default target: %s", strerror(-target->meta.load_error));
1271                 else if (target->meta.load_state == UNIT_MASKED)
1272                         log_error("Default target masked.");
1273
1274                 if (!target || target->meta.load_state != UNIT_LOADED) {
1275                         log_info("Trying to load rescue target...");
1276
1277                         if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1278                                 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1279                                 dbus_error_free(&error);
1280                                 goto finish;
1281                         } else if (target->meta.load_state == UNIT_ERROR) {
1282                                 log_error("Failed to load rescue target: %s", strerror(-target->meta.load_error));
1283                                 goto finish;
1284                         } else if (target->meta.load_state == UNIT_MASKED) {
1285                                 log_error("Rescue target masked.");
1286                                 goto finish;
1287                         }
1288                 }
1289
1290                 assert(target->meta.load_state == UNIT_LOADED);
1291
1292                 if (arg_action == ACTION_TEST) {
1293                         printf("-> By units:\n");
1294                         manager_dump_units(m, stdout, "\t");
1295                 }
1296
1297                 if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
1298                         log_error("Failed to start default target: %s", bus_error(&error, r));
1299                         dbus_error_free(&error);
1300                         goto finish;
1301                 }
1302
1303                 after_startup = now(CLOCK_MONOTONIC);
1304                 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1305                          "Loaded units and determined initial transaction in %s.",
1306                           format_timespan(timespan, sizeof(timespan), after_startup - before_startup));
1307
1308                 if (arg_action == ACTION_TEST) {
1309                         printf("-> By jobs:\n");
1310                         manager_dump_jobs(m, stdout, "\t");
1311                         retval = EXIT_SUCCESS;
1312                         goto finish;
1313                 }
1314         }
1315
1316         for (;;) {
1317                 if ((r = manager_loop(m)) < 0) {
1318                         log_error("Failed to run mainloop: %s", strerror(-r));
1319                         goto finish;
1320                 }
1321
1322                 switch (m->exit_code) {
1323
1324                 case MANAGER_EXIT:
1325                         retval = EXIT_SUCCESS;
1326                         log_debug("Exit.");
1327                         goto finish;
1328
1329                 case MANAGER_RELOAD:
1330                         log_info("Reloading.");
1331                         if ((r = manager_reload(m)) < 0)
1332                                 log_error("Failed to reload: %s", strerror(-r));
1333                         break;
1334
1335                 case MANAGER_REEXECUTE:
1336                         if (prepare_reexecute(m, &serialization, &fds) < 0)
1337                                 goto finish;
1338
1339                         reexecute = true;
1340                         log_notice("Reexecuting.");
1341                         goto finish;
1342
1343                 case MANAGER_REBOOT:
1344                 case MANAGER_POWEROFF:
1345                 case MANAGER_HALT:
1346                 case MANAGER_KEXEC: {
1347                         static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1348                                 [MANAGER_REBOOT] = "reboot",
1349                                 [MANAGER_POWEROFF] = "poweroff",
1350                                 [MANAGER_HALT] = "halt",
1351                                 [MANAGER_KEXEC] = "kexec"
1352                         };
1353
1354                         assert_se(shutdown_verb = table[m->exit_code]);
1355
1356                         log_notice("Shutting down.");
1357                         goto finish;
1358                 }
1359
1360                 default:
1361                         assert_not_reached("Unknown exit code.");
1362                 }
1363         }
1364
1365 finish:
1366         if (m)
1367                 manager_free(m);
1368
1369         free(arg_default_unit);
1370         strv_free(arg_default_controllers);
1371
1372         dbus_shutdown();
1373
1374         label_finish();
1375
1376         if (reexecute) {
1377                 const char *args[15];
1378                 unsigned i = 0;
1379                 char sfd[16];
1380
1381                 assert(serialization);
1382                 assert(fds);
1383
1384                 args[i++] = SYSTEMD_BINARY_PATH;
1385
1386                 args[i++] = "--log-level";
1387                 args[i++] = log_level_to_string(log_get_max_level());
1388
1389                 args[i++] = "--log-target";
1390                 args[i++] = log_target_to_string(log_get_target());
1391
1392                 if (arg_running_as == MANAGER_SYSTEM)
1393                         args[i++] = "--system";
1394                 else
1395                         args[i++] = "--user";
1396
1397                 if (arg_dump_core)
1398                         args[i++] = "--dump-core";
1399
1400                 if (arg_crash_shell)
1401                         args[i++] = "--crash-shell";
1402
1403                 if (arg_confirm_spawn)
1404                         args[i++] = "--confirm-spawn";
1405
1406                 if (arg_show_status)
1407                         args[i++] = "--show-status=1";
1408                 else
1409                         args[i++] = "--show-status=0";
1410
1411 #ifdef HAVE_SYSV_COMPAT
1412                 if (arg_sysv_console)
1413                         args[i++] = "--sysv-console=1";
1414                 else
1415                         args[i++] = "--sysv-console=0";
1416 #endif
1417
1418                 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1419                 char_array_0(sfd);
1420
1421                 args[i++] = "--deserialize";
1422                 args[i++] = sfd;
1423
1424                 args[i++] = NULL;
1425
1426                 assert(i <= ELEMENTSOF(args));
1427
1428                 execv(args[0], (char* const*) args);
1429
1430                 log_error("Failed to reexecute: %m");
1431         }
1432
1433         if (serialization)
1434                 fclose(serialization);
1435
1436         if (fds)
1437                 fdset_free(fds);
1438
1439         if (shutdown_verb) {
1440                 const char * command_line[] = {
1441                         SYSTEMD_SHUTDOWN_BINARY_PATH,
1442                         shutdown_verb,
1443                         NULL
1444                 };
1445
1446                 execv(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line);
1447                 log_error("Failed to execute shutdown binary, freezing: %m");
1448         }
1449
1450         if (getpid() == 1)
1451                 freeze();
1452
1453         return retval;
1454 }