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