]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
912a0f21 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2017 Felipe Sateler |
912a0f21 FS |
4 | ***/ |
5 | ||
6 | #include <errno.h> | |
7 | #include <sys/prctl.h> | |
8 | ||
6e86b24d ZJS |
9 | #include "sd-bus.h" |
10 | ||
912a0f21 FS |
11 | #include "bus-util.h" |
12 | #include "bus-error.h" | |
375c3f6a | 13 | #include "def.h" |
33eb44fe | 14 | #include "env-util.h" |
912a0f21 FS |
15 | #include "log.h" |
16 | #include "process-util.h" | |
912a0f21 | 17 | #include "signal-util.h" |
fe7a6da8 | 18 | #include "special.h" |
912a0f21 | 19 | |
375c3f6a | 20 | static int reload_manager(sd_bus *bus) { |
912a0f21 | 21 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
375c3f6a | 22 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
912a0f21 FS |
23 | int r; |
24 | ||
375c3f6a ZJS |
25 | log_info("Reloading system manager configuration"); |
26 | ||
27 | r = sd_bus_message_new_method_call( | |
28 | bus, | |
29 | &m, | |
30 | "org.freedesktop.systemd1", | |
31 | "/org/freedesktop/systemd1", | |
32 | "org.freedesktop.systemd1.Manager", | |
33 | "Reload"); | |
34 | if (r < 0) | |
35 | return bus_log_create_error(r); | |
36 | ||
37 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
38 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
39 | * their timeout, and for everything else there's the same time budget in place. */ | |
40 | ||
41 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
42 | if (r < 0) | |
43 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
44 | ||
45 | return 0; | |
46 | } | |
47 | ||
48 | static int start_default_target(sd_bus *bus) { | |
49 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
50 | int r; | |
912a0f21 FS |
51 | |
52 | log_info("Starting default target"); | |
53 | ||
54 | /* Start these units only if we can replace base.target with it */ | |
55 | r = sd_bus_call_method(bus, | |
56 | "org.freedesktop.systemd1", | |
57 | "/org/freedesktop/systemd1", | |
58 | "org.freedesktop.systemd1.Manager", | |
59 | "StartUnit", | |
60 | &error, | |
61 | NULL, | |
fe7a6da8 | 62 | "ss", SPECIAL_DEFAULT_TARGET, "isolate"); |
912a0f21 FS |
63 | |
64 | if (r < 0) | |
4ae25393 | 65 | return log_error_errno(r, "Failed to start default target: %s", bus_error_message(&error, r)); |
912a0f21 | 66 | |
4ae25393 | 67 | return 0; |
912a0f21 FS |
68 | } |
69 | ||
cccb78f0 | 70 | static int fork_wait(const char* const cmdline[]) { |
912a0f21 | 71 | pid_t pid; |
4c253ed1 | 72 | int r; |
912a0f21 | 73 | |
0672e2c6 | 74 | r = safe_fork("(sulogin)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 75 | if (r < 0) |
b6e1fff1 | 76 | return r; |
4c253ed1 | 77 | if (r == 0) { |
912a0f21 | 78 | /* Child */ |
912a0f21 FS |
79 | execv(cmdline[0], (char**) cmdline); |
80 | log_error_errno(errno, "Failed to execute %s: %m", cmdline[0]); | |
81 | _exit(EXIT_FAILURE); /* Operational error */ | |
82 | } | |
83 | ||
7d4904fe | 84 | return wait_for_terminate_and_check(cmdline[0], pid, WAIT_LOG_ABNORMAL); |
912a0f21 FS |
85 | } |
86 | ||
87 | static void print_mode(const char* mode) { | |
88 | printf("You are in %s mode. After logging in, type \"journalctl -xb\" to view\n" | |
9db82fe3 ZJS |
89 | "system logs, \"systemctl reboot\" to reboot, \"systemctl default\" or \"exit\"\n" |
90 | "to boot into default mode.\n", mode); | |
912a0f21 FS |
91 | fflush(stdout); |
92 | } | |
93 | ||
94 | int main(int argc, char *argv[]) { | |
33eb44fe AH |
95 | const char* sulogin_cmdline[] = { |
96 | SULOGIN, | |
97 | NULL, /* --force */ | |
98 | NULL | |
99 | }; | |
375c3f6a | 100 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
912a0f21 FS |
101 | int r; |
102 | ||
6bf3c61c | 103 | log_setup_service(); |
912a0f21 FS |
104 | |
105 | print_mode(argc > 1 ? argv[1] : ""); | |
106 | ||
33eb44fe AH |
107 | if (getenv_bool("SYSTEMD_SULOGIN_FORCE") > 0) |
108 | /* allows passwordless logins if root account is locked. */ | |
109 | sulogin_cmdline[1] = "--force"; | |
110 | ||
cccb78f0 | 111 | (void) fork_wait(sulogin_cmdline); |
912a0f21 | 112 | |
375c3f6a ZJS |
113 | r = bus_connect_system_systemd(&bus); |
114 | if (r < 0) { | |
115 | log_warning_errno(r, "Failed to get D-Bus connection: %m"); | |
116 | r = 0; | |
117 | } else { | |
118 | (void) reload_manager(bus); | |
119 | ||
120 | r = start_default_target(bus); | |
121 | } | |
912a0f21 FS |
122 | |
123 | return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
124 | } |