main.c 66.5 KB
Newer Older
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Lennart Poettering's avatar
Lennart Poettering committed
2

3 4 5 6 7 8
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
9 10
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
11 12 13 14 15
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  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 20 21
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

22 23
#include <dbus/dbus.h>

Lennart Poettering's avatar
Lennart Poettering committed
24 25 26
#include <stdio.h>
#include <errno.h>
#include <string.h>
27
#include <unistd.h>
28 29
#include <sys/types.h>
#include <sys/stat.h>
30
#include <getopt.h>
31
#include <signal.h>
32
#include <sys/wait.h>
Lennart Poettering's avatar
Lennart Poettering committed
33
#include <fcntl.h>
34
#include <sys/prctl.h>
35
#include <sys/mount.h>
Lennart Poettering's avatar
Lennart Poettering committed
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"
Lennart Poettering's avatar
Lennart Poettering committed
54

55 56 57 58 59 60
#include "mount-setup.h"
#include "loopback-setup.h"
#include "kmod-setup.h"
#include "hostname-setup.h"
#include "machine-id-setup.h"
#include "locale-setup.h"
Kay Sievers's avatar
Kay Sievers committed
61
#include "hwclock.h"
62 63 64
#include "selinux-setup.h"
#include "ima-setup.h"

65 66
static enum {
        ACTION_RUN,
Lennart Poettering's avatar
Lennart Poettering committed
67
        ACTION_HELP,
68
        ACTION_TEST,
69 70
        ACTION_DUMP_CONFIGURATION_ITEMS,
        ACTION_DONE
71
} arg_action = ACTION_RUN;
72

73 74 75 76 77 78 79
static char *arg_default_unit = NULL;
static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;

static bool arg_dump_core = true;
static bool arg_crash_shell = false;
static int arg_crash_chvt = -1;
static bool arg_confirm_spawn = false;
80
static bool arg_show_status = true;
81
static bool arg_switched_root = false;
82
#ifdef HAVE_SYSV_COMPAT
83
static bool arg_sysv_console = true;
84
#endif
85
static char **arg_default_controllers = NULL;
86
static char ***arg_join_controllers = NULL;
87
static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
88
static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
89 90
static usec_t arg_runtime_watchdog = 0;
static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
91
static struct rlimit *arg_default_rlimit[RLIMIT_NLIMITS] = {};
92
static uint64_t arg_capability_bounding_set_drop = 0;
93
static nsec_t arg_timer_slack_nsec = (nsec_t) -1;
94

95
static FILE* serialization = NULL;
Lennart Poettering's avatar
Lennart Poettering committed
96

97 98 99
static void nop_handler(int sig) {
}

100
_noreturn_ static void crash(int sig) {
101

102
        if (!arg_dump_core)
103
                log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
104
        else {
105
                struct sigaction sa;
106 107
                pid_t pid;

108 109 110 111 112 113
                /* We want to wait for the core process, hence let's enable SIGCHLD */
                zero(sa);
                sa.sa_handler = nop_handler;
                sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
                assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);

114
                if ((pid = fork()) < 0)
115
                        log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

                else if (pid == 0) {
                        struct rlimit rl;

                        /* Enable default signal handler for core dump */
                        zero(sa);
                        sa.sa_handler = SIG_DFL;
                        assert_se(sigaction(sig, &sa, NULL) == 0);

                        /* Don't limit the core dump size */
                        zero(rl);
                        rl.rlim_cur = RLIM_INFINITY;
                        rl.rlim_max = RLIM_INFINITY;
                        setrlimit(RLIMIT_CORE, &rl);

                        /* Just to be sure... */
                        assert_se(chdir("/") == 0);

                        /* Raise the signal again */
                        raise(sig);

                        assert_not_reached("We shouldn't be here...");
                        _exit(1);
139 140

                } else {
141 142
                        siginfo_t status;
                        int r;
143 144

                        /* Order things nicely. */
145 146 147
                        if ((r = wait_for_terminate(pid, &status)) < 0)
                                log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
                        else if (status.si_code != CLD_DUMPED)
148
                                log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
149
                        else
150
                                log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
151 152 153
                }
        }

154 155
        if (arg_crash_chvt)
                chvt(arg_crash_chvt);
156

157
        if (arg_crash_shell) {
158 159
                struct sigaction sa;
                pid_t pid;
160

161 162 163
                log_info("Executing crash shell in 10s...");
                sleep(10);

164 165 166 167 168
                /* Let the kernel reap children for us */
                zero(sa);
                sa.sa_handler = SIG_IGN;
                sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
                assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
169

170 171 172
                if ((pid = fork()) < 0)
                        log_error("Failed to fork off crash shell: %s", strerror(errno));
                else if (pid == 0) {
173
                        int fd, r;
174

175
                        if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
176
                                log_error("Failed to acquire terminal: %s", strerror(-fd));
177
                        else if ((r = make_stdio(fd)) < 0)
178
                                log_error("Failed to duplicate terminal fd: %s", strerror(-r));
179

180 181 182 183 184
                        execl("/bin/sh", "/bin/sh", NULL);

                        log_error("execl() failed: %s", strerror(errno));
                        _exit(1);
                }
185

186
                log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
187 188 189
        }

        log_info("Freezing execution.");
190 191 192 193 194 195 196 197 198 199 200
        freeze();
}

static void install_crash_handler(void) {
        struct sigaction sa;

        zero(sa);

        sa.sa_handler = crash;
        sa.sa_flags = SA_NODEFER;

Lennart Poettering's avatar
Lennart Poettering committed
201
        sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
202
}
203

204 205
static int console_setup(bool do_reset) {
        int tty_fd, r;
Lennart Poettering's avatar
Lennart Poettering committed
206

207 208
        /* If we are init, we connect stdin/stdout/stderr to /dev/null
         * and make sure we don't have a controlling tty. */
Lennart Poettering's avatar
Lennart Poettering committed
209

210 211 212 213
        release_terminal();

        if (!do_reset)
                return 0;
Lennart Poettering's avatar
Lennart Poettering committed
214

215 216
        tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
        if (tty_fd < 0) {
217 218 219
                log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
                return -tty_fd;
        }
Lennart Poettering's avatar
Lennart Poettering committed
220

221 222 223 224
        /* We don't want to force text mode.
         * plymouth may be showing pictures already from initrd. */
        r = reset_terminal_fd(tty_fd, false);
        if (r < 0)
225 226 227
                log_error("Failed to reset /dev/console: %s", strerror(-r));

        close_nointr_nofail(tty_fd);
Lennart Poettering's avatar
Lennart Poettering committed
228 229 230
        return r;
}

231 232 233 234 235
static int set_default_unit(const char *u) {
        char *c;

        assert(u);

236 237
        c = strdup(u);
        if (!c)
238 239
                return -ENOMEM;

240 241
        free(arg_default_unit);
        arg_default_unit = c;
242

243 244 245 246 247 248
        return 0;
}

static int parse_proc_cmdline_word(const char *word) {

        static const char * const rlmap[] = {
249
                "emergency", SPECIAL_EMERGENCY_TARGET,
250
                "-b",        SPECIAL_EMERGENCY_TARGET,
251 252 253 254 255 256 257 258 259
                "single",    SPECIAL_RESCUE_TARGET,
                "-s",        SPECIAL_RESCUE_TARGET,
                "s",         SPECIAL_RESCUE_TARGET,
                "S",         SPECIAL_RESCUE_TARGET,
                "1",         SPECIAL_RESCUE_TARGET,
                "2",         SPECIAL_RUNLEVEL2_TARGET,
                "3",         SPECIAL_RUNLEVEL3_TARGET,
                "4",         SPECIAL_RUNLEVEL4_TARGET,
                "5",         SPECIAL_RUNLEVEL5_TARGET,
260 261
        };

262 263
        assert(word);

264 265 266 267 268 269 270 271 272
        if (startswith(word, "systemd.unit=")) {

                if (!in_initrd())
                        return set_default_unit(word + 13);

        } else if (startswith(word, "rd.systemd.unit=")) {

                if (in_initrd())
                        return set_default_unit(word + 16);
273

274
        } else if (startswith(word, "systemd.log_target=")) {
275 276 277 278 279 280 281 282 283

                if (log_set_target_from_string(word + 19) < 0)
                        log_warning("Failed to parse log target %s. Ignoring.", word + 19);

        } else if (startswith(word, "systemd.log_level=")) {

                if (log_set_max_level_from_string(word + 18) < 0)
                        log_warning("Failed to parse log level %s. Ignoring.", word + 18);

284 285 286 287 288 289 290 291 292 293
        } else if (startswith(word, "systemd.log_color=")) {

                if (log_show_color_from_string(word + 18) < 0)
                        log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);

        } else if (startswith(word, "systemd.log_location=")) {

                if (log_show_location_from_string(word + 21) < 0)
                        log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);

294 295 296 297
        } else if (startswith(word, "systemd.dump_core=")) {
                int r;

                if ((r = parse_boolean(word + 18)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
298
                        log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
299
                else
300
                        arg_dump_core = r;
301 302 303 304 305

        } else if (startswith(word, "systemd.crash_shell=")) {
                int r;

                if ((r = parse_boolean(word + 20)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
306
                        log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
307
                else
308
                        arg_crash_shell = r;
309 310 311 312 313

        } else if (startswith(word, "systemd.confirm_spawn=")) {
                int r;

                if ((r = parse_boolean(word + 22)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
314
                        log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
315
                else
316
                        arg_confirm_spawn = r;
317

318 319 320 321
        } else if (startswith(word, "systemd.crash_chvt=")) {
                int k;

                if (safe_atoi(word + 19, &k) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
322
                        log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
323
                else
324
                        arg_crash_chvt = k;
325

326 327 328 329
        } else if (startswith(word, "systemd.show_status=")) {
                int r;

                if ((r = parse_boolean(word + 20)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
330
                        log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
331
                else
332
                        arg_show_status = r;
333 334 335 336
        } else if (startswith(word, "systemd.default_standard_output=")) {
                int r;

                if ((r = exec_output_from_string(word + 32)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
337
                        log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
338 339 340 341 342 343
                else
                        arg_default_std_output = r;
        } else if (startswith(word, "systemd.default_standard_error=")) {
                int r;

                if ((r = exec_output_from_string(word + 31)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
344
                        log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
345 346
                else
                        arg_default_std_error = r;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        } else if (startswith(word, "systemd.setenv=")) {
                char *cenv, *eq;
                int r;

                cenv = strdup(word + 15);
                if (!cenv)
                        return -ENOMEM;

                eq = strchr(cenv, '=');
                if (!eq) {
                        r = unsetenv(cenv);
                        if (r < 0)
                                log_warning("unsetenv failed %s. Ignoring.", strerror(errno));
                } else {
                        *eq = 0;
                        r = setenv(cenv, eq + 1, 1);
                        if (r < 0)
                                log_warning("setenv failed %s. Ignoring.", strerror(errno));
                }
                free(cenv);
367
#ifdef HAVE_SYSV_COMPAT
368 369 370 371
        } else if (startswith(word, "systemd.sysv_console=")) {
                int r;

                if ((r = parse_boolean(word + 21)) < 0)
Lennart Poettering's avatar
Lennart Poettering committed
372
                        log_warning("Failed to parse SysV console switch %s. Ignoring.", word + 20);
373 374
                else
                        arg_sysv_console = r;
375
#endif
376

377 378 379 380
        } else if (startswith(word, "systemd.")) {

                log_warning("Unknown kernel switch %s. Ignoring.", word);

381 382
                log_info("Supported kernel switches:\n"
                         "systemd.unit=UNIT                        Default unit to start\n"
383
                         "rd.systemd.unit=UNIT                     Default unit to start when run in initrd\n"
384
                         "systemd.dump_core=0|1                    Dump core on crash\n"
385
                         "systemd.crash_shell=0|1                  Run shell on crash\n"
386
                         "systemd.crash_chvt=N                     Change to VT #N on crash\n"
387
                         "systemd.confirm_spawn=0|1                Confirm every process spawn\n"
388
                         "systemd.show_status=0|1                  Show status updates on the console during bootup\n"
389
#ifdef HAVE_SYSV_COMPAT
390
                         "systemd.sysv_console=0|1                 Connect output of SysV scripts to console\n"
391
#endif
392
                         "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
393 394 395
                         "                                         Log target\n"
                         "systemd.log_level=LEVEL                  Log level\n"
                         "systemd.log_color=0|1                    Highlight important log messages\n"
396
                         "systemd.log_location=0|1                 Include code location in log messages\n"
397
                         "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
398
                         "                                         Set default log output for services\n"
399
                         "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
400
                         "                                         Set default log error output for services\n");
401

402
        } else if (streq(word, "quiet")) {
403
                arg_show_status = false;
404
#ifdef HAVE_SYSV_COMPAT
405
                arg_sysv_console = false;
406
#endif
407
        } else {
408 409 410 411 412 413 414 415 416 417 418
                unsigned i;

                /* SysV compatibility */
                for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
                        if (streq(word, rlmap[i]))
                                return set_default_unit(rlmap[i+1]);
        }

        return 0;
}

419
static int config_parse_level2(
420 421 422 423
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
424
                int ltype,
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                const char *rvalue,
                void *data,
                void *userdata) {

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        log_set_max_level_from_string(rvalue);
        return 0;
}

static int config_parse_target(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
442
                int ltype,
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
                const char *rvalue,
                void *data,
                void *userdata) {

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        log_set_target_from_string(rvalue);
        return 0;
}

static int config_parse_color(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
460
                int ltype,
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                const char *rvalue,
                void *data,
                void *userdata) {

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        log_show_color_from_string(rvalue);
        return 0;
}

static int config_parse_location(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
478
                int ltype,
479 480 481 482 483 484 485 486 487 488 489 490
                const char *rvalue,
                void *data,
                void *userdata) {

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        log_show_location_from_string(rvalue);
        return 0;
}

491
static int config_parse_cpu_affinity2(
492 493 494 495
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
496
                int ltype,
497 498 499 500 501 502 503 504 505 506 507 508 509 510
                const char *rvalue,
                void *data,
                void *userdata) {

        char *w;
        size_t l;
        char *state;
        cpu_set_t *c = NULL;
        unsigned ncpus = 0;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

511
        FOREACH_WORD_QUOTED(w, l, rvalue, state) {
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
                char *t;
                int r;
                unsigned cpu;

                if (!(t = strndup(w, l)))
                        return -ENOMEM;

                r = safe_atou(t, &cpu);
                free(t);

                if (!c)
                        if (!(c = cpu_set_malloc(&ncpus)))
                                return -ENOMEM;

                if (r < 0 || cpu >= ncpus) {
                        log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
                        CPU_FREE(c);
                        return -EBADMSG;
                }

                CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
        }

        if (c) {
                if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
                        log_warning("Failed to set CPU affinity: %m");

                CPU_FREE(c);
        }

        return 0;
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
static void strv_free_free(char ***l) {
        char ***i;

        if (!l)
                return;

        for (i = l; *i; i++)
                strv_free(*i);

        free(l);
}

static void free_join_controllers(void) {
        if (!arg_join_controllers)
                return;

        strv_free_free(arg_join_controllers);
        arg_join_controllers = NULL;
}

static int config_parse_join_controllers(
                const char *filename,
                unsigned line,
                const char *section,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        unsigned n = 0;
        char *state, *w;
        size_t length;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        free_join_controllers();

        FOREACH_WORD_QUOTED(w, length, rvalue, state) {
                char *s, **l;

                s = strndup(w, length);
                if (!s)
                        return -ENOMEM;

                l = strv_split(s, ",");
                free(s);

                strv_uniq(l);

                if (strv_length(l) <= 1) {
                        strv_free(l);
                        continue;
                }

                if (!arg_join_controllers) {
                        arg_join_controllers = new(char**, 2);
                        if (!arg_join_controllers) {
                                strv_free(l);
                                return -ENOMEM;
                        }

                        arg_join_controllers[0] = l;
                        arg_join_controllers[1] = NULL;

                        n = 1;
                } else {
                        char ***a;
                        char ***t;

                        t = new0(char**, n+2);
                        if (!t) {
                                strv_free(l);
                                return -ENOMEM;
                        }

                        n = 0;

                        for (a = arg_join_controllers; *a; a++) {

                                if (strv_overlap(*a, l)) {
                                        char **c;

                                        c = strv_merge(*a, l);
                                        if (!c) {
                                                strv_free(l);
                                                strv_free_free(t);
                                                return -ENOMEM;
                                        }

                                        strv_free(l);
                                        l = c;
                                } else {
                                        char **c;

                                        c = strv_copy(*a);
                                        if (!c) {
                                                strv_free(l);
                                                strv_free_free(t);
                                                return -ENOMEM;
                                        }

                                        t[n++] = c;
                                }
                        }

                        t[n++] = strv_uniq(l);

                        strv_free_free(arg_join_controllers);
                        arg_join_controllers = t;
                }
        }

        return 0;
}

663 664
static int parse_config_file(void) {

665 666 667 668 669 670 671 672
        const ConfigTableItem items[] = {
                { "Manager", "LogLevel",              config_parse_level2,       0, NULL                     },
                { "Manager", "LogTarget",             config_parse_target,       0, NULL                     },
                { "Manager", "LogColor",              config_parse_color,        0, NULL                     },
                { "Manager", "LogLocation",           config_parse_location,     0, NULL                     },
                { "Manager", "DumpCore",              config_parse_bool,         0, &arg_dump_core           },
                { "Manager", "CrashShell",            config_parse_bool,         0, &arg_crash_shell         },
                { "Manager", "ShowStatus",            config_parse_bool,         0, &arg_show_status         },
673
#ifdef HAVE_SYSV_COMPAT
674
                { "Manager", "SysVConsole",           config_parse_bool,         0, &arg_sysv_console        },
675
#endif
676 677 678 679 680
                { "Manager", "CrashChVT",             config_parse_int,          0, &arg_crash_chvt          },
                { "Manager", "CPUAffinity",           config_parse_cpu_affinity2, 0, NULL                    },
                { "Manager", "DefaultControllers",    config_parse_strv,         0, &arg_default_controllers },
                { "Manager", "DefaultStandardOutput", config_parse_output,       0, &arg_default_std_output  },
                { "Manager", "DefaultStandardError",  config_parse_output,       0, &arg_default_std_error   },
681
                { "Manager", "JoinControllers",       config_parse_join_controllers, 0, &arg_join_controllers },
682 683
                { "Manager", "RuntimeWatchdogSec",    config_parse_usec,         0, &arg_runtime_watchdog    },
                { "Manager", "ShutdownWatchdogSec",   config_parse_usec,         0, &arg_shutdown_watchdog   },
684
                { "Manager", "CapabilityBoundingSet", config_parse_bounding_set, 0, &arg_capability_bounding_set_drop },
685
                { "Manager", "TimerSlackNSec",        config_parse_nsec,         0, &arg_timer_slack_nsec    },
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
                { "Manager", "DefaultLimitCPU",       config_parse_limit,        0, &arg_default_rlimit[RLIMIT_CPU]},
                { "Manager", "DefaultLimitFSIZE",     config_parse_limit,        0, &arg_default_rlimit[RLIMIT_FSIZE]},
                { "Manager", "DefaultLimitDATA",      config_parse_limit,        0, &arg_default_rlimit[RLIMIT_DATA]},
                { "Manager", "DefaultLimitSTACK",     config_parse_limit,        0, &arg_default_rlimit[RLIMIT_STACK]},
                { "Manager", "DefaultLimitCORE",      config_parse_limit,        0, &arg_default_rlimit[RLIMIT_CORE]},
                { "Manager", "DefaultLimitRSS",       config_parse_limit,        0, &arg_default_rlimit[RLIMIT_RSS]},
                { "Manager", "DefaultLimitNOFILE",    config_parse_limit,        0, &arg_default_rlimit[RLIMIT_NOFILE]},
                { "Manager", "DefaultLimitAS",        config_parse_limit,        0, &arg_default_rlimit[RLIMIT_AS]},
                { "Manager", "DefaultLimitNPROC",     config_parse_limit,        0, &arg_default_rlimit[RLIMIT_NPROC]},
                { "Manager", "DefaultLimitMEMLOCK",   config_parse_limit,        0, &arg_default_rlimit[RLIMIT_MEMLOCK]},
                { "Manager", "DefaultLimitLOCKS",     config_parse_limit,        0, &arg_default_rlimit[RLIMIT_LOCKS]},
                { "Manager", "DefaultLimitSIGPENDING",config_parse_limit,        0, &arg_default_rlimit[RLIMIT_SIGPENDING]},
                { "Manager", "DefaultLimitMSGQUEUE",  config_parse_limit,        0, &arg_default_rlimit[RLIMIT_MSGQUEUE]},
                { "Manager", "DefaultLimitNICE",      config_parse_limit,        0, &arg_default_rlimit[RLIMIT_NICE]},
                { "Manager", "DefaultLimitRTPRIO",    config_parse_limit,        0, &arg_default_rlimit[RLIMIT_RTPRIO]},
                { "Manager", "DefaultLimitRTTIME",    config_parse_limit,        0, &arg_default_rlimit[RLIMIT_RTTIME]},
702
                { NULL, NULL, NULL, 0, NULL }
703 704 705 706 707 708
        };

        FILE *f;
        const char *fn;
        int r;

709
        fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
710 711
        f = fopen(fn, "re");
        if (!f) {
712 713 714 715 716 717 718
                if (errno == ENOENT)
                        return 0;

                log_warning("Failed to open configuration file '%s': %m", fn);
                return 0;
        }

719 720
        r = config_parse(fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, NULL);
        if (r < 0)
721 722 723 724 725 726 727
                log_warning("Failed to parse configuration file: %s", strerror(-r));

        fclose(f);

        return 0;
}

728
static int parse_proc_cmdline(void) {
729
        char *line, *w, *state;
730 731 732
        int r;
        size_t l;

733 734 735 736 737
        /* Don't read /proc/cmdline if we are in a container, since
         * that is only relevant for the host system */
        if (detect_container(NULL) > 0)
                return 0;

738
        if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
739
                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
                return 0;
        }

        FOREACH_WORD_QUOTED(w, l, line, state) {
                char *word;

                if (!(word = strndup(w, l))) {
                        r = -ENOMEM;
                        goto finish;
                }

                r = parse_proc_cmdline_word(word);
                free(word);

                if (r < 0)
                        goto finish;
        }

        r = 0;

finish:
        free(line);
        return r;
}

static int parse_argv(int argc, char *argv[]) {

        enum {
                ARG_LOG_LEVEL = 0x100,
                ARG_LOG_TARGET,
770 771
                ARG_LOG_COLOR,
                ARG_LOG_LOCATION,
772
                ARG_UNIT,
773
                ARG_SYSTEM,
774
                ARG_USER,
775
                ARG_TEST,
Lennart Poettering's avatar
Lennart Poettering committed
776
                ARG_DUMP_CONFIGURATION_ITEMS,
777 778
                ARG_DUMP_CORE,
                ARG_CRASH_SHELL,
779
                ARG_CONFIRM_SPAWN,
780
                ARG_SHOW_STATUS,
781
                ARG_SYSV_CONSOLE,
782
                ARG_DESERIALIZE,
783
                ARG_SWITCHED_ROOT,
784 785 786
                ARG_INTROSPECT,
                ARG_DEFAULT_STD_OUTPUT,
                ARG_DEFAULT_STD_ERROR
787 788 789
        };

        static const struct option options[] = {
790 791
                { "log-level",                required_argument, NULL, ARG_LOG_LEVEL                },
                { "log-target",               required_argument, NULL, ARG_LOG_TARGET               },
792 793
                { "log-color",                optional_argument, NULL, ARG_LOG_COLOR                },
                { "log-location",             optional_argument, NULL, ARG_LOG_LOCATION             },
794
                { "unit",                     required_argument, NULL, ARG_UNIT                     },
795
                { "system",                   no_argument,       NULL, ARG_SYSTEM                   },
796
                { "user",                     no_argument,       NULL, ARG_USER                     },
797 798 799
                { "test",                     no_argument,       NULL, ARG_TEST                     },
                { "help",                     no_argument,       NULL, 'h'                          },
                { "dump-configuration-items", no_argument,       NULL, ARG_DUMP_CONFIGURATION_ITEMS },
800 801 802
                { "dump-core",                optional_argument, NULL, ARG_DUMP_CORE                },
                { "crash-shell",              optional_argument, NULL, ARG_CRASH_SHELL              },
                { "confirm-spawn",            optional_argument, NULL, ARG_CONFIRM_SPAWN            },
803
                { "show-status",              optional_argument, NULL, ARG_SHOW_STATUS              },
804
#ifdef HAVE_SYSV_COMPAT
805
                { "sysv-console",             optional_argument, NULL, ARG_SYSV_CONSOLE             },
806
#endif
807
                { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
808
                { "switched-root",            no_argument,       NULL, ARG_SWITCHED_ROOT            },
809
                { "introspect",               optional_argument, NULL, ARG_INTROSPECT               },
810 811
                { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
                { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
812
                { NULL,                       0,                 NULL, 0                            }
813 814 815 816 817 818 819
        };

        int c, r;

        assert(argc >= 1);
        assert(argv);

820 821 822
        if (getpid() == 1)
                opterr = 0;

823
        while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

                switch (c) {

                case ARG_LOG_LEVEL:
                        if ((r = log_set_max_level_from_string(optarg)) < 0) {
                                log_error("Failed to parse log level %s.", optarg);
                                return r;
                        }

                        break;

                case ARG_LOG_TARGET:

                        if ((r = log_set_target_from_string(optarg)) < 0) {
                                log_error("Failed to parse log target %s.", optarg);
                                return r;
                        }

                        break;

844 845
                case ARG_LOG_COLOR:

846 847 848 849 850 851 852
                        if (optarg) {
                                if ((r = log_show_color_from_string(optarg)) < 0) {
                                        log_error("Failed to parse log color setting %s.", optarg);
                                        return r;
                                }
                        } else
                                log_show_color(true);
853 854 855 856 857

                        break;

                case ARG_LOG_LOCATION:

858 859 860 861 862 863 864
                        if (optarg) {
                                if ((r = log_show_location_from_string(optarg)) < 0) {
                                        log_error("Failed to parse log location setting %s.", optarg);
                                        return r;
                                }
                        } else
                                log_show_location(true);
865 866 867

                        break;

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
                case ARG_DEFAULT_STD_OUTPUT:

                        if ((r = exec_output_from_string(optarg)) < 0) {
                                log_error("Failed to parse default standard output setting %s.", optarg);
                                return r;
                        } else
                                arg_default_std_output = r;
                        break;

                case ARG_DEFAULT_STD_ERROR:

                        if ((r = exec_output_from_string(optarg)) < 0) {
                                log_error("Failed to parse default standard error output setting %s.", optarg);
                                return r;
                        } else
                                arg_default_std_error = r;
                        break;

886
                case ARG_UNIT:
887 888 889 890 891 892 893 894

                        if ((r = set_default_unit(optarg)) < 0) {
                                log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
                                return r;
                        }

                        break;

895 896 897
                case ARG_SYSTEM:
                        arg_running_as = MANAGER_SYSTEM;
                        break;
898

899 900
                case ARG_USER:
                        arg_running_as = MANAGER_USER;
901 902
                        break;

Lennart Poettering's avatar
Lennart Poettering committed
903
                case ARG_TEST:
904
                        arg_action = ACTION_TEST;
Lennart Poettering's avatar
Lennart Poettering committed
905 906
                        break;

907
                case ARG_DUMP_CONFIGURATION_ITEMS:
908
                        arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
909 910
                        break;

911
                case ARG_DUMP_CORE:
912 913 914 915 916 917
                        r = optarg ? parse_boolean(optarg) : 1;
                        if (r < 0) {
                                log_error("Failed to parse dump core boolean %s.", optarg);
                                return r;
                        }
                        arg_dump_core = r;
918 919 920
                        break;

                case ARG_CRASH_SHELL:
921 922 923 924 925 926
                        r = optarg ? parse_boolean(optarg) : 1;
                        if (r < 0) {
                                log_error("Failed to parse crash shell boolean %s.", optarg);
                                return r;
                        }
                        arg_crash_shell = r;
927 928
                        break;

Lennart Poettering's avatar
Lennart Poettering committed
929
                case ARG_CONFIRM_SPAWN:
930 931 932 933 934 935
                        r = optarg ? parse_boolean(optarg) : 1;
                        if (r < 0) {
                                log_error("Failed to parse confirm spawn boolean %s.", optarg);
                                return r;
                        }
                        arg_confirm_spawn = r;
Lennart Poettering's avatar
Lennart Poettering committed
936 937
                        break;

938
                case ARG_SHOW_STATUS:
939 940 941 942 943 944
                        r = optarg ? parse_boolean(optarg) : 1;
                        if (r < 0) {
                                log_error("Failed to parse show status boolean %s.", optarg);
                                return r;
                        }
                        arg_show_status = r;
945
                        break;
946

947
#ifdef HAVE_SYSV_COMPAT
948
                case ARG_SYSV_CONSOLE:
949 950 951 952 953 954
                        r = optarg ? parse_boolean(optarg) : 1;
                        if (r < 0) {
                                log_error("Failed to parse SysV console boolean %s.", optarg);
                                return r;
                        }
                        arg_sysv_console = r;
955
                        break;
956
#endif
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
                case ARG_DESERIALIZE: {
                        int fd;
                        FILE *f;

                        if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
                                log_error("Failed to parse deserialize option %s.", optarg);
                                return r;
                        }

                        if (!(f = fdopen(fd, "r"))) {
                                log_error("Failed to open serialization fd: %m");
                                return r;
                        }

                        if (serialization)
                                fclose(serialization);

                        serialization = f;

                        break;
                }

980
                case ARG_SWITCHED_ROOT:
981
                        arg_switched_root = true;
982 983
                        break;

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
                case ARG_INTROSPECT: {
                        const char * const * i = NULL;

                        for (i = bus_interface_table; *i; i += 2)
                                if (!optarg || streq(i[0], optarg)) {
                                        fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
                                              "<node>\n", stdout);
                                        fputs(i[1], stdout);
                                        fputs("</node>\n", stdout);

                                        if (optarg)
                                                break;
                                }

                        if (!i[0] && optarg)
                                log_error("Unknown interface %s.", optarg);

1001
                        arg_action = ACTION_DONE;
1002 1003 1004
                        break;
                }

1005
                case 'h':
1006
                        arg_action = ACTION_HELP;
1007 1008
                        break;

1009 1010 1011 1012
                case 'D':
                        log_set_max_level(LOG_DEBUG);
                        break;

1013 1014 1015 1016 1017 1018 1019
                case 'b':
                case 's':
                case 'z':
                        /* Just to eat away the sysvinit kernel
                         * cmdline args without getopt() error
                         * messages that we'll parse in
                         * parse_proc_cmdline_word() or ignore. */
1020

1021
                case '?':
1022
                default:
1023 1024 1025 1026 1027 1028
                        if (getpid() != 1) {
                                log_error("Unknown option code %c", c);
                                return -EINVAL;
                        }

                        break;
1029 1030
                }

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
        if (optind < argc && getpid() != 1) {
                /* Hmm, when we aren't run as init system
                 * let's complain about excess arguments */

                log_error("Excess arguments.");
                return -EINVAL;
        }

        if (detect_container(NULL) > 0) {
                char **a;

                /* All /proc/cmdline arguments the kernel didn't
                 * understand it passed to us. We're not really
                 * interested in that usually since /proc/cmdline is
                 * more interesting and complete. With one exception:
                 * if we are run in a container /proc/cmdline is not
                 * relevant for the container, hence we rely on argv[]
                 * instead. */

                for (a = argv; a < argv + argc; a++)
                        if ((r = parse_proc_cmdline_word(*a)) < 0)
                                return r;
1053 1054
        }

1055 1056 1057 1058 1059
        return 0;
}

static int help(void) {

Lennart Poettering's avatar
Lennart Poettering committed
1060
        printf("%s [OPTIONS...]\n\n"
1061
               "Starts up and maintains the system or user services.\n\n"
1062 1063
               "  -h --help                      Show this help\n"
               "     --test                      Determine startup sequence, dump it and exit\n"
Lennart Poettering's avatar
Lennart Poettering committed
1064
               "     --dump-configuration-items  Dump understood unit configuration items\n"
1065
               "     --introspect[=INTERFACE]    Extract D-Bus interface data\n"
1066
               "     --unit=UNIT                 Set default unit\n"
1067
               "     --system                    Run a system instance, even if PID != 1\n"
1068
               "     --user                      Run a user instance\n"
1069 1070 1071
               "     --dump-core[=0|1]           Dump core on crash\n"
               "     --crash-shell[=0|1]         Run shell on crash\n"
               "     --confirm-spawn[=0|1]       Ask for confirmation when spawning processes\n"
1072
               "     --show-status[=0|1]         Show status updates on the console during bootup\n"
1073
#ifdef HAVE_SYSV_COMPAT
1074
               "     --sysv-console[=0|1]        Connect output of SysV scripts to console\n"
1075
#endif
1076
               "     --log-target=TARGET         Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
1077
               "     --log-level=LEVEL           Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
1078
               "     --log-color[=0|1]           Highlight important log messages\n"
1079 1080 1081
               "     --log-location[=0|1]        Include code location in log messages\n"
               "     --default-standard-output=  Set default standard output for services\n"
               "     --default-standard-error=   Set default standard error output for services\n",
1082
               program_invocation_short_name);
1083 1084 1085 1086

        return 0;
}

1087 1088 1089 1090 1091 1092 1093 1094 1095
static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
        FILE *f = NULL;
        FDSet *fds = NULL;
        int r;

        assert(m);
        assert(_f);
        assert(_fds);

1096 1097 1098
        /* Make sure nothing is really destructed when we shut down */
        m->n_reloading ++;

1099
        if ((r = manager_open_serialization(m, &f)) < 0) {
Harald Hoyer's avatar
Harald Hoyer committed
1100
                log_error("Failed to create serialization file: %s", strerror(-r));
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
                goto fail;
        }

        if (!(fds = fdset_new())) {
                r = -ENOMEM;
                log_error("Failed to allocate fd set: %s", strerror(-r));
                goto fail;
        }

        if ((r = manager_serialize(m, f, fds)) < 0) {
                log_error("Failed to serialize state: %s", strerror(-r));
                goto fail;
        }

        if (fseeko(f, 0, SEEK_SET) < 0) {
                log_error("Failed to rewind serialization fd: %m");
                goto fail;
        }

        if ((r = fd_cloexec(fileno(f), false)) < 0) {
                log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
                goto fail;
        }

        if ((r = fdset_cloexec(fds, false)) < 0) {
                log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
                goto fail;
        }

        *_f = f;
        *_fds = fds;

        return 0;

fail:
        fdset_free(fds);

        if (f)
                fclose(f);

        return r;
}

1144 1145 1146 1147 1148 1149
static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
        const char *e;
        unsigned long long a, b;

        assert(t);

1150 1151
        e = getenv("RD_TIMESTAMP");
        if (!e)
1152 1153 1154 1155