1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "bus-locator.h"
5 #include "locale-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"
14 static int normalize_filenames(char **names
) {
18 STRV_FOREACH(u
, names
)
19 if (!path_is_absolute(*u
)) {
20 char* normalized_path
;
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",
28 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
29 "Link argument does contain at least one directory separator: %s",
32 r
= path_make_absolute_cwd(*u
, &normalized_path
);
36 free_and_replace(*u
, normalized_path
);
42 static int normalize_names(char **names
) {
44 bool was_path
= false;
46 STRV_FOREACH(u
, names
) {
52 r
= free_and_strdup(u
, basename(*u
));
54 return log_error_errno(r
, "Failed to normalize unit file path: %m");
60 log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name.");
65 int verb_enable(int argc
, char *argv
[], void *userdata
) {
66 _cleanup_strv_free_
char **names
= NULL
;
67 const char *verb
= argv
[0];
68 UnitFileChange
*changes
= NULL
;
70 int carries_install_info
= -1;
71 bool ignore_carries_install_info
= arg_quiet
;
77 r
= mangle_names("to enable", strv_skip(argv
, 1), &names
);
81 r
= enable_sysv_units(verb
, names
);
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())
90 r
= daemon_reload(ACTION_RELOAD
, /* graceful= */ false);
94 if (streq(verb
, "disable")) {
95 r
= normalize_names(names
);
100 if (streq(verb
, "link")) {
101 r
= normalize_filenames(names
);
106 if (install_client_side()) {
109 flags
= unit_file_flags_from_args();
110 if (streq(verb
, "enable")) {
111 r
= unit_file_enable(arg_scope
, flags
, arg_root
, names
, &changes
, &n_changes
);
112 carries_install_info
= r
;
113 } else if (streq(verb
, "disable"))
114 r
= unit_file_disable(arg_scope
, flags
, arg_root
, names
, &changes
, &n_changes
);
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
);
129 assert_not_reached();
131 unit_file_dump_changes(r
, verb
, changes
, n_changes
, arg_quiet
);
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;
143 if (STR_IN_SET(verb
, "mask", "unmask")) {
145 _cleanup_(lookup_paths_free
) LookupPaths lp
= {};
147 r
= lookup_paths_init(&lp
, arg_scope
, 0, arg_root
);
151 STRV_FOREACH(name
, names
) {
152 r
= unit_exists(&lp
, *name
);
156 log_notice("Unit %s does not exist, proceeding anyway.", *name
);
160 r
= acquire_bus(BUS_MANAGER
, &bus
);
164 polkit_agent_open_maybe();
166 if (streq(verb
, "enable")) {
167 method
= "EnableUnitFiles";
168 expect_carries_install_info
= true;
169 } else if (streq(verb
, "disable")) {
170 method
= "DisableUnitFiles";
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")) {
179 if (arg_preset_mode
!= UNIT_FILE_PRESET_FULL
) {
180 method
= "PresetUnitFilesWithMode";
181 send_preset_mode
= true;
183 method
= "PresetUnitFiles";
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";
192 } else if (streq(verb
, "revert")) {
193 method
= "RevertUnitFiles";
194 send_runtime
= send_force
= false;
196 assert_not_reached();
198 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
200 return bus_log_create_error(r
);
202 r
= sd_bus_message_append_strv(m
, names
);
204 return bus_log_create_error(r
);
206 if (send_preset_mode
) {
207 r
= sd_bus_message_append(m
, "s", unit_file_preset_mode_to_string(arg_preset_mode
));
209 return bus_log_create_error(r
);
213 r
= sd_bus_message_append(m
, "b", arg_runtime
);
215 return bus_log_create_error(r
);
219 r
= sd_bus_message_append(m
, "b", arg_force
);
221 return bus_log_create_error(r
);
224 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
226 return log_error_errno(r
, "Failed to %s unit: %s", verb
, bus_error_message(&error
, r
));
228 if (expect_carries_install_info
) {
229 r
= sd_bus_message_read(reply
, "b", &carries_install_info
);
231 return bus_log_parse_error(r
);
234 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
238 /* Try to reload if enabled */
239 if (!arg_no_reload
) {
240 r
= daemon_reload(ACTION_RELOAD
, /* graceful= */ false);
247 if (carries_install_info
== 0 && !ignore_carries_install_info
)
248 log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n"
249 "Alias= settings in the [Install] section, and DefaultInstance= for template\n"
250 "units). This means they are not meant to be enabled using systemctl.\n"
251 " \n" /* trick: the space is needed so that the line does not get stripped from output */
252 "Possible reasons for having this kind of units are:\n"
253 "%1$s A unit may be statically enabled by being symlinked from another unit's\n"
254 " .wants/ or .requires/ directory.\n"
255 "%1$s A unit's purpose may be to act as a helper for some other unit which has\n"
256 " a requirement dependency on it.\n"
257 "%1$s A unit may be started when needed via activation (socket, path, timer,\n"
258 " D-Bus, udev, scripted systemctl call, ...).\n"
259 "%1$s In case of template units, the unit is meant to be enabled with some\n"
260 " instance name specified.",
261 special_glyph(SPECIAL_GLYPH_BULLET
));
263 if (arg_now
&& STR_IN_SET(argv
[0], "enable", "disable", "mask")) {
267 r
= acquire_bus(BUS_MANAGER
, &bus
);
271 len
= strv_length(names
);
273 char *new_args
[len
+ 2];
275 new_args
[0] = (char*) (streq(argv
[0], "enable") ? "start" : "stop");
276 for (i
= 0; i
< len
; i
++)
277 new_args
[i
+ 1] = basename(names
[i
]);
278 new_args
[i
+ 1] = NULL
;
280 r
= verb_start(len
+ 1, new_args
, userdata
);
285 unit_file_changes_free(changes
, n_changes
);