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