]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl-enable.c
86d9f602fa0c5493d9f9a3c402fec727020dff94
[thirdparty/systemd.git] / src / systemctl / systemctl-enable.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bus-error.h"
4 #include "bus-locator.h"
5 #include "locale-util.h"
6 #include "path-util.h"
7 #include "systemctl-daemon-reload.h"
8 #include "systemctl-enable.h"
9 #include "systemctl-start-unit.h"
10 #include "systemctl-sysv-compat.h"
11 #include "systemctl-util.h"
12 #include "systemctl.h"
13
14 static int normalize_filenames(char **names) {
15 int r;
16
17 STRV_FOREACH(u, names)
18 if (!path_is_absolute(*u)) {
19 char* normalized_path;
20
21 if (!isempty(arg_root))
22 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
23 "Non-absolute paths are not allowed when --root is used: %s",
24 *u);
25
26 if (!strchr(*u, '/'))
27 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
28 "Link argument must contain at least one directory separator.\n"
29 "If you intended to link a file in the current directory, try ./%s instead.",
30 *u);
31
32 r = path_make_absolute_cwd(*u, &normalized_path);
33 if (r < 0)
34 return r;
35
36 free_and_replace(*u, normalized_path);
37 }
38
39 return 0;
40 }
41
42 static int normalize_names(char **names) {
43 bool was_path = false;
44
45 STRV_FOREACH(u, names) {
46 int r;
47
48 if (!is_path(*u))
49 continue;
50
51 r = free_and_strdup(u, basename(*u));
52 if (r < 0)
53 return log_error_errno(r, "Failed to normalize unit file path: %m");
54
55 was_path = true;
56 }
57
58 if (was_path)
59 log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name.");
60
61 return 0;
62 }
63
64 int verb_enable(int argc, char *argv[], void *userdata) {
65 _cleanup_strv_free_ char **names = NULL;
66 const char *verb = argv[0];
67 InstallChange *changes = NULL;
68 size_t n_changes = 0;
69 int carries_install_info = -1;
70 bool ignore_carries_install_info = arg_quiet || arg_no_warn;
71 int r;
72
73 if (!argv[1])
74 return 0;
75
76 r = mangle_names("to enable", strv_skip(argv, 1), &names);
77 if (r < 0)
78 return r;
79
80 r = enable_sysv_units(verb, names);
81 if (r < 0)
82 return r;
83
84 /* If the operation was fully executed by the SysV compat, let's finish early */
85 if (strv_isempty(names)) {
86 if (arg_no_reload || install_client_side())
87 return 0;
88
89 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
90 return r > 0 ? 0 : r;
91 }
92
93 if (streq(verb, "disable")) {
94 r = normalize_names(names);
95 if (r < 0)
96 return r;
97 }
98
99 if (streq(verb, "link")) {
100 r = normalize_filenames(names);
101 if (r < 0)
102 return r;
103 }
104
105 if (install_client_side()) {
106 UnitFileFlags flags;
107
108 flags = unit_file_flags_from_args();
109 if (streq(verb, "enable")) {
110 r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes);
111 carries_install_info = r;
112 } else if (streq(verb, "disable")) {
113 r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes);
114 carries_install_info = r;
115 } else if (streq(verb, "reenable")) {
116 r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes);
117 carries_install_info = r;
118 } else if (streq(verb, "link"))
119 r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes);
120 else if (streq(verb, "preset"))
121 r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes);
122 else if (streq(verb, "mask"))
123 r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes);
124 else if (streq(verb, "unmask"))
125 r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes);
126 else if (streq(verb, "revert"))
127 r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes);
128 else
129 assert_not_reached();
130
131 install_changes_dump(r, verb, changes, n_changes, arg_quiet);
132 if (r < 0)
133 goto finish;
134 r = 0;
135 } else {
136 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
138 bool expect_carries_install_info = false;
139 bool send_runtime = true, send_force = true, send_preset_mode = false;
140 const char *method;
141 sd_bus *bus;
142
143 if (STR_IN_SET(verb, "mask", "unmask")) {
144 _cleanup_(lookup_paths_free) LookupPaths lp = {};
145
146 r = lookup_paths_init_or_warn(&lp, arg_scope, 0, arg_root);
147 if (r < 0)
148 return r;
149
150 STRV_FOREACH(name, names) {
151 r = unit_exists(&lp, *name);
152 if (r < 0)
153 return r;
154 if (r == 0)
155 log_notice("Unit %s does not exist, proceeding anyway.", *name);
156 }
157 }
158
159 r = acquire_bus(BUS_MANAGER, &bus);
160 if (r < 0)
161 return r;
162
163 polkit_agent_open_maybe();
164
165 if (streq(verb, "enable")) {
166 method = "EnableUnitFiles";
167 expect_carries_install_info = true;
168 } else if (streq(verb, "disable")) {
169 method = "DisableUnitFilesWithFlagsAndInstallInfo";
170 expect_carries_install_info = true;
171 send_force = false;
172 } else if (streq(verb, "reenable")) {
173 method = "ReenableUnitFiles";
174 expect_carries_install_info = true;
175 } else if (streq(verb, "link"))
176 method = "LinkUnitFiles";
177 else if (streq(verb, "preset")) {
178
179 if (arg_preset_mode != UNIT_FILE_PRESET_FULL) {
180 method = "PresetUnitFilesWithMode";
181 send_preset_mode = true;
182 } else
183 method = "PresetUnitFiles";
184
185 expect_carries_install_info = true;
186 ignore_carries_install_info = true;
187 } else if (streq(verb, "mask"))
188 method = "MaskUnitFiles";
189 else if (streq(verb, "unmask")) {
190 method = "UnmaskUnitFiles";
191 send_force = false;
192 } else if (streq(verb, "revert")) {
193 method = "RevertUnitFiles";
194 send_runtime = send_force = false;
195 } else
196 assert_not_reached();
197
198 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method);
199 if (r < 0)
200 return bus_log_create_error(r);
201
202 r = sd_bus_message_append_strv(m, names);
203 if (r < 0)
204 return bus_log_create_error(r);
205
206 if (send_preset_mode) {
207 r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode));
208 if (r < 0)
209 return bus_log_create_error(r);
210 }
211
212 if (send_runtime) {
213 if (streq(method, "DisableUnitFilesWithFlagsAndInstallInfo"))
214 r = sd_bus_message_append(m, "t", arg_runtime ? UNIT_FILE_RUNTIME : 0);
215 else
216 r = sd_bus_message_append(m, "b", arg_runtime);
217 if (r < 0)
218 return bus_log_create_error(r);
219 }
220
221 if (send_force) {
222 r = sd_bus_message_append(m, "b", arg_force);
223 if (r < 0)
224 return bus_log_create_error(r);
225 }
226
227 r = sd_bus_call(bus, m, 0, &error, &reply);
228 if (r < 0)
229 return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r));
230
231 if (expect_carries_install_info) {
232 r = sd_bus_message_read(reply, "b", &carries_install_info);
233 if (r < 0)
234 return bus_log_parse_error(r);
235 }
236
237 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
238 if (r < 0)
239 goto finish;
240
241 /* Try to reload if enabled */
242 if (!arg_no_reload) {
243 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
244 if (r > 0)
245 r = 0;
246 } else
247 r = 0;
248 }
249
250 if (carries_install_info == 0 && !ignore_carries_install_info)
251 log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n"
252 "Alias= settings in the [Install] section, and DefaultInstance= for template\n"
253 "units). This means they are not meant to be enabled or disabled using systemctl.\n"
254 " \n" /* trick: the space is needed so that the line does not get stripped from output */
255 "Possible reasons for having this kind of units are:\n"
256 "%1$s A unit may be statically enabled by being symlinked from another unit's\n"
257 " .wants/ or .requires/ directory.\n"
258 "%1$s A unit's purpose may be to act as a helper for some other unit which has\n"
259 " a requirement dependency on it.\n"
260 "%1$s A unit may be started when needed via activation (socket, path, timer,\n"
261 " D-Bus, udev, scripted systemctl call, ...).\n"
262 "%1$s In case of template units, the unit is meant to be enabled with some\n"
263 " instance name specified.",
264 special_glyph(SPECIAL_GLYPH_BULLET));
265
266 if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) {
267 sd_bus *bus;
268 size_t len, i;
269
270 r = acquire_bus(BUS_MANAGER, &bus);
271 if (r < 0)
272 goto finish;
273
274 len = strv_length(names);
275 {
276 char *new_args[len + 2];
277
278 new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop");
279 for (i = 0; i < len; i++)
280 new_args[i + 1] = basename(names[i]);
281 new_args[i + 1] = NULL;
282
283 r = verb_start(len + 1, new_args, userdata);
284 }
285 }
286
287 finish:
288 install_changes_free(changes, n_changes);
289
290 return r;
291 }