]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sulogin-shell/sulogin-shell.c
Merge pull request #6924 from andir/vrf-dhcpv4
[thirdparty/systemd.git] / src / sulogin-shell / sulogin-shell.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2017 Felipe Sateler
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/prctl.h>
22
23 #include "bus-util.h"
24 #include "bus-error.h"
25 #include "log.h"
26 #include "process-util.h"
27 #include "sd-bus.h"
28 #include "signal-util.h"
29
30 static int start_default_target(void) {
31 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
32 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
33 int r;
34
35 r = bus_connect_system_systemd(&bus);
36 if (r < 0) {
37 log_error_errno(r, "Failed to get D-Bus connection: %m");
38 return false;
39 }
40
41 log_info("Starting default target");
42
43 /* Start these units only if we can replace base.target with it */
44 r = sd_bus_call_method(bus,
45 "org.freedesktop.systemd1",
46 "/org/freedesktop/systemd1",
47 "org.freedesktop.systemd1.Manager",
48 "StartUnit",
49 &error,
50 NULL,
51 "ss", "default.target", "isolate");
52
53 if (r < 0)
54 log_error("Failed to start default target: %s", bus_error_message(&error, r));
55
56 return r;
57 }
58
59 static void fork_wait(const char* const cmdline[]) {
60 pid_t pid;
61
62 pid = fork();
63 if (pid < 0) {
64 log_error_errno(errno, "fork(): %m");
65 return;
66 }
67 if (pid == 0) {
68
69 /* Child */
70
71 (void) reset_all_signal_handlers();
72 (void) reset_signal_mask();
73 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
74
75 execv(cmdline[0], (char**) cmdline);
76 log_error_errno(errno, "Failed to execute %s: %m", cmdline[0]);
77 _exit(EXIT_FAILURE); /* Operational error */
78 }
79
80 wait_for_terminate_and_warn(cmdline[0], pid, false);
81 }
82
83 static void print_mode(const char* mode) {
84 printf("You are in %s mode. After logging in, type \"journalctl -xb\" to view\n"
85 "system logs, \"systemctl reboot\" to reboot, \"systemctl default\" or ^D to boot\n"
86 "into default mode.\n", mode);
87 fflush(stdout);
88 }
89
90 int main(int argc, char *argv[]) {
91 static const char* const sulogin_cmdline[] = {SULOGIN, NULL};
92 int r;
93
94 log_set_target(LOG_TARGET_AUTO);
95 log_parse_environment();
96 log_open();
97
98 print_mode(argc > 1 ? argv[1] : "");
99
100 fork_wait(sulogin_cmdline);
101
102 r = start_default_target();
103
104 return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
105 }