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