]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sulogin-shell/sulogin-shell.c
tree-wide: normalize includes of public headers
[thirdparty/systemd.git] / src / sulogin-shell / sulogin-shell.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2017 Felipe Sateler
4 ***/
5
6 #include <errno.h>
7 #include <sys/prctl.h>
8
9 #include "sd-bus.h"
10
11 #include "bus-util.h"
12 #include "bus-error.h"
13 #include "def.h"
14 #include "env-util.h"
15 #include "log.h"
16 #include "process-util.h"
17 #include "signal-util.h"
18 #include "special.h"
19
20 static int reload_manager(sd_bus *bus) {
21 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
22 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
23 int r;
24
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;
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,
62 "ss", SPECIAL_DEFAULT_TARGET, "isolate");
63
64 if (r < 0)
65 return log_error_errno(r, "Failed to start default target: %s", bus_error_message(&error, r));
66
67 return 0;
68 }
69
70 static int fork_wait(const char* const cmdline[]) {
71 pid_t pid;
72 int r;
73
74 r = safe_fork("(sulogin)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
75 if (r < 0)
76 return r;
77 if (r == 0) {
78 /* Child */
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
84 return wait_for_terminate_and_check(cmdline[0], pid, WAIT_LOG_ABNORMAL);
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"
89 "system logs, \"systemctl reboot\" to reboot, \"systemctl default\" or \"exit\"\n"
90 "to boot into default mode.\n", mode);
91 fflush(stdout);
92 }
93
94 int main(int argc, char *argv[]) {
95 const char* sulogin_cmdline[] = {
96 SULOGIN,
97 NULL, /* --force */
98 NULL
99 };
100 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
101 int r;
102
103 log_setup_service();
104
105 print_mode(argc > 1 ? argv[1] : "");
106
107 if (getenv_bool("SYSTEMD_SULOGIN_FORCE") > 0)
108 /* allows passwordless logins if root account is locked. */
109 sulogin_cmdline[1] = "--force";
110
111 (void) fork_wait(sulogin_cmdline);
112
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 }
122
123 return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
124 }