Debian Codegraph @

src/systemctl/systemctl-is-enabled.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "sd-bus.h"
6
7 #include "bus-error.h"
8 #include "bus-locator.h"
9 #include "bus-util.h"
10 #include "install.h"
11 #include "log.h"
12 #include "strv.h"
13 #include "systemctl.h"
14 #include "systemctl-is-enabled.h"
15 #include "systemctl-util.h"
16
17 static int show_installation_targets_client_side(const char *name) {
18 InstallChange *changes = NULL;
19 size_t n_changes = 0;
20 UnitFileFlags flags;
21 char **p;
22 int r;
23
24 CLEANUP_ARRAY(changes, n_changes, install_changes_free);
25
26 p = STRV_MAKE(name);
27 flags = UNIT_FILE_DRY_RUN |
28 (arg_runtime ? UNIT_FILE_RUNTIME : 0);
29
30 r = unit_file_disable(arg_runtime_scope, flags, NULL, p, &changes, &n_changes);
31 if (r < 0)
32 return log_error_errno(r, "Failed to get file links for %s: %m", name);
33
34 FOREACH_ARRAY(c, changes, n_changes)
35 if (c->type == INSTALL_CHANGE_UNLINK)
36 printf(" %s\n", c->path);
37
38 return 0;
39 }
40
41 static int show_installation_targets(sd_bus *bus, const char *name) {
42 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
43 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
44 const char *link;
45 int r;
46
47 r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileLinks", &error, &reply, "sb", name, arg_runtime);
48 if (r < 0)
49 return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r));
50
51 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
52 if (r < 0)
53 return bus_log_parse_error(r);
54
55 while ((r = sd_bus_message_read(reply, "s", &link)) > 0)
56 printf(" %s\n", link);
57
58 if (r < 0)
59 return bus_log_parse_error(r);
60
61 r = sd_bus_message_exit_container(reply);
62 if (r < 0)
63 return bus_log_parse_error(r);
64
65 return 0;
66 }
67
68 int verb_is_enabled(int argc, char *argv[], uintptr_t _data, void *userdata) {
69 _cleanup_strv_free_ char **names = NULL;
70 bool not_found = true, enabled = false;
71 int r;
72
73 r = mangle_names("to check", strv_skip(argv, 1), &names);
74 if (r < 0)
75 return r;
76
77 if (install_client_side() != INSTALL_CLIENT_SIDE_NO)
78 STRV_FOREACH(name, names) {
79 UnitFileState state;
80
81 r = unit_file_get_state(arg_runtime_scope, arg_root, *name, &state);
82 if (r == -ENOENT) {
83 if (!arg_quiet)
84 puts("not-found");
85 continue;
86 } else if (r < 0)
87 return log_error_errno(r, "Failed to get unit file state for %s: %m", *name);
88 else
89 not_found = false;
90
91 if (IN_SET(state,
92 UNIT_FILE_ENABLED,
93 UNIT_FILE_ENABLED_RUNTIME,
94 UNIT_FILE_STATIC,
95 UNIT_FILE_ALIAS,
96 UNIT_FILE_INDIRECT,
97 UNIT_FILE_GENERATED))
98 enabled = true;
99
100 if (!arg_quiet) {
101 puts(unit_file_state_to_string(state));
102 if (arg_full) {
103 r = show_installation_targets_client_side(*name);
104 if (r < 0)
105 return r;
106 }
107 }
108 }
109 else {
110 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
111 sd_bus *bus;
112
113 r = acquire_bus(BUS_MANAGER, &bus);
114 if (r < 0)
115 return r;
116
117 STRV_FOREACH(name, names) {
118 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
119 const char *s;
120
121 r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", *name);
122 if (r == -ENOENT) {
123 sd_bus_error_free(&error);
124
125 if (!arg_quiet)
126 puts("not-found");
127 continue;
128 } else if (r < 0)
129 return log_error_errno(r,
130 "Failed to get unit file state for %s: %s",
131 *name,
132 bus_error_message(&error, r));
133 else
134 not_found = false;
135
136 r = sd_bus_message_read(reply, "s", &s);
137 if (r < 0)
138 return bus_log_parse_error(r);
139
140 if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "alias", "indirect", "generated"))
141 enabled = true;
142
143 if (!arg_quiet) {
144 puts(s);
145 if (arg_full) {
146 r = show_installation_targets(bus, *name);
147 if (r < 0)
148 return r;
149 }
150 }
151 }
152 }
153
154 return enabled ? EXIT_SUCCESS : not_found ? EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN : EXIT_FAILURE;
155 }