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