Debian Codegraph @

src/systemctl/systemctl-main.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4
5 #include "dissect-image.h"
6 #include "glyph-util.h"
7 #include "log.h"
8 #include "logs-show.h"
9 #include "loop-util.h"
10 #include "main-func.h"
11 #include "mount-util.h"
12 #include "stat-util.h"
13 #include "strv.h"
14 #include "systemctl-compat-halt.h"
15 #include "systemctl-logind.h"
16 #include "systemctl-util.h"
17 #include "systemctl.h"
18 #include "virt.h"
19
20 static int run(int argc, char *argv[]) {
21 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
22 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
23 char **args = STRV_EMPTY;
24 int r;
25
26 setlocale(LC_ALL, "");
27 log_setup();
28
29 r = systemctl_dispatch_parse_argv(argc, argv, /* log_level_shift= */ 0, &args);
30 if (r <= 0)
31 goto finish;
32
33 journal_browse_prepare();
34
35 if (proc_mounted() == 0)
36 log_full(arg_no_warn ? LOG_DEBUG : LOG_WARNING,
37 "%s%s/proc/ is not mounted. This is not a supported mode of operation. Please fix\n"
38 "your invocation environment to mount /proc/ and /sys/ properly. Proceeding anyway.\n"
39 "Your mileage may vary.",
40 optional_glyph(GLYPH_WARNING_SIGN),
41 optional_glyph(GLYPH_SPACE));
42
43 if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
44 if (!arg_quiet)
45 log_info("Running in chroot, ignoring request.");
46 r = 0;
47 goto finish;
48 }
49
50 /* systemctl_main() will print an error message for the bus connection, but only if it needs to */
51
52 if (arg_image) {
53 assert(!arg_root);
54
55 r = mount_image_privately_interactively(
56 arg_image,
57 arg_image_policy,
58 DISSECT_IMAGE_GENERIC_ROOT |
59 DISSECT_IMAGE_REQUIRE_ROOT |
60 DISSECT_IMAGE_RELAX_VAR_CHECK |
61 DISSECT_IMAGE_VALIDATE_OS |
62 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
63 &mounted_dir,
64 /* ret_dir_fd= */ NULL,
65 &loop_device);
66 if (r < 0)
67 return r;
68
69 arg_root = strdup(mounted_dir);
70 if (!arg_root)
71 return log_oom();
72 }
73
74 switch (arg_action) {
75
76 case ACTION_SYSTEMCTL:
77 r = systemctl_main(args);
78 break;
79
80 /* Legacy command aliases set arg_action. They provide some fallbacks, e.g. to tell sysvinit to
81 * reboot after you have installed systemd binaries. */
82
83 case ACTION_HALT:
84 case ACTION_POWEROFF:
85 case ACTION_REBOOT:
86 case ACTION_KEXEC:
87 r = halt_main();
88 break;
89
90 case ACTION_CANCEL_SHUTDOWN:
91 r = logind_cancel_shutdown();
92 break;
93
94 case ACTION_SHOW_SHUTDOWN:
95 case ACTION_SYSTEMCTL_SHOW_SHUTDOWN:
96 r = logind_show_shutdown();
97 break;
98
99 case ACTION_RESCUE:
100 case ACTION_RELOAD:
101 case ACTION_REEXEC:
102 case ACTION_EXIT:
103 case ACTION_SLEEP:
104 case ACTION_SUSPEND:
105 case ACTION_HIBERNATE:
106 case ACTION_HYBRID_SLEEP:
107 case ACTION_SUSPEND_THEN_HIBERNATE:
108 case ACTION_EMERGENCY:
109 case ACTION_DEFAULT:
110 /* systemctl verbs with no equivalent in the legacy commands. These cannot appear in
111 * arg_action. Fall through. */
112
113 case _ACTION_INVALID:
114 default:
115 assert_not_reached();
116 }
117
118 finish:
119 release_busses();
120
121 /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */
122 return r;
123 }
124
125 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);