2 This file is part of systemd.
4 Copyright 2017 Felipe Sateler
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.
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.
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/>.
21 #include <sys/prctl.h>
24 #include "bus-error.h"
26 #include "process-util.h"
28 #include "signal-util.h"
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
;
35 r
= bus_connect_system_systemd(&bus
);
37 log_error_errno(r
, "Failed to get D-Bus connection: %m");
41 log_info("Starting default target");
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",
51 "ss", "default.target", "isolate");
54 log_error("Failed to start default target: %s", bus_error_message(&error
, r
));
59 static void fork_wait(const char* const cmdline
[]) {
64 log_error_errno(errno
, "fork(): %m");
71 (void) reset_all_signal_handlers();
72 (void) reset_signal_mask();
73 assert_se(prctl(PR_SET_PDEATHSIG
, SIGTERM
) == 0);
75 execv(cmdline
[0], (char**) cmdline
);
76 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
77 _exit(EXIT_FAILURE
); /* Operational error */
80 wait_for_terminate_and_warn(cmdline
[0], pid
, false);
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
);
90 int main(int argc
, char *argv
[]) {
91 static const char* const sulogin_cmdline
[] = {SULOGIN
, NULL
};
94 log_set_target(LOG_TARGET_AUTO
);
95 log_parse_environment();
98 print_mode(argc
> 1 ? argv
[1] : "");
100 fork_wait(sulogin_cmdline
);
102 r
= start_default_target();
104 return r
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;