| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-error.h" |
| 7 |
#include "bus-locator.h" |
| 8 |
#include "bus-unit-util.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "glyph-util.h" |
| 11 |
#include "install.h" |
| 12 |
#include "log.h" |
| 13 |
#include "path-lookup.h" |
| 14 |
#include "path-util.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "systemctl.h" |
| 18 |
#include "systemctl-daemon-reload.h" |
| 19 |
#include "systemctl-enable.h" |
| 20 |
#include "systemctl-start-unit.h" |
| 21 |
#include "systemctl-util.h" |
| 22 |
#include "unit-name.h" |
| 23 |
#include "verbs.h" |
| 24 |
|
| 25 |
static int normalize_link_paths(char **paths) { |
| 26 |
int r; |
| 27 |
|
| 28 |
STRV_FOREACH(u, paths) { |
| 29 |
if (path_is_absolute(*u)) |
| 30 |
continue; |
| 31 |
|
| 32 |
if (!isempty(arg_root)) |
| 33 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 34 |
"Non-absolute paths are not allowed when --root= is used: %s", |
| 35 |
*u); |
| 36 |
|
| 37 |
if (!is_path(*u)) |
| 38 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 39 |
"Link argument must contain at least one directory separator.\n" |
| 40 |
"If you intended to link a file in the current directory, try './%s' instead.", |
| 41 |
*u); |
| 42 |
|
| 43 |
char *normalized_path; |
| 44 |
|
| 45 |
r = path_make_absolute_cwd(*u, &normalized_path); |
| 46 |
if (r < 0) |
| 47 |
return log_error_errno(r, "Failed to normalize path '%s': %m", *u); |
| 48 |
|
| 49 |
path_simplify(normalized_path); |
| 50 |
|
| 51 |
free_and_replace(*u, normalized_path); |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static int normalize_names(char **names) { |
| 58 |
bool was_path = false; |
| 59 |
int r; |
| 60 |
|
| 61 |
STRV_FOREACH(u, names) { |
| 62 |
if (!is_path(*u)) |
| 63 |
continue; |
| 64 |
|
| 65 |
char *fn; |
| 66 |
|
| 67 |
r = path_extract_filename(*u, &fn); |
| 68 |
if (r < 0) |
| 69 |
return log_error_errno(r, "Failed to extract file name from '%s': %m", *u); |
| 70 |
|
| 71 |
free_and_replace(*u, fn); |
| 72 |
|
| 73 |
was_path = true; |
| 74 |
} |
| 75 |
|
| 76 |
if (was_path) |
| 77 |
log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int verb_enable(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 83 |
const char *verb = ASSERT_PTR(argv[0]); |
| 84 |
_cleanup_strv_free_ char **names = NULL; |
| 85 |
int carries_install_info = -1; |
| 86 |
bool ignore_carries_install_info = arg_quiet || arg_no_warn; |
| 87 |
sd_bus *bus = NULL; |
| 88 |
int r; |
| 89 |
|
| 90 |
if (streq(verb, "preset") && should_bypass("SYSTEMD_PRESET")) |
| 91 |
return 0; |
| 92 |
|
| 93 |
const char *operation = strjoina("to ", verb); |
| 94 |
r = mangle_names(operation, ASSERT_PTR(strv_skip(argv, 1)), &names); |
| 95 |
if (r < 0) |
| 96 |
return r; |
| 97 |
|
| 98 |
if (streq(verb, "disable")) |
| 99 |
r = normalize_names(names); |
| 100 |
else if (streq(verb, "link")) |
| 101 |
r = normalize_link_paths(names); |
| 102 |
else |
| 103 |
r = 0; |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
|
| 107 |
if (install_client_side() == INSTALL_CLIENT_SIDE_NO) { |
| 108 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
| 109 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 110 |
bool expect_carries_install_info = false; |
| 111 |
bool send_runtime = true, send_force = true, send_preset_mode = false; |
| 112 |
const char *method, *warn_trigger_operation = NULL; |
| 113 |
bool warn_trigger_ignore_masked = true; /* suppress "used uninitialized" warning */ |
| 114 |
|
| 115 |
if (STR_IN_SET(verb, "mask", "unmask")) { |
| 116 |
_cleanup_(lookup_paths_done) LookupPaths lp = {}; |
| 117 |
|
| 118 |
r = lookup_paths_init_or_warn(&lp, arg_runtime_scope, 0, arg_root); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
|
| 122 |
STRV_FOREACH(name, names) { |
| 123 |
r = unit_exists(&lp, *name); |
| 124 |
if (r < 0) |
| 125 |
return r; |
| 126 |
if (r == 0) |
| 127 |
log_notice("Unit %s does not exist, proceeding anyway.", *name); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
|
| 135 |
polkit_agent_open_maybe(); |
| 136 |
|
| 137 |
if (streq(verb, "enable")) { |
| 138 |
method = "EnableUnitFiles"; |
| 139 |
expect_carries_install_info = true; |
| 140 |
} else if (streq(verb, "disable")) { |
| 141 |
method = "DisableUnitFilesWithFlagsAndInstallInfo"; |
| 142 |
expect_carries_install_info = true; |
| 143 |
send_force = false; |
| 144 |
|
| 145 |
warn_trigger_operation = "Disabling"; |
| 146 |
warn_trigger_ignore_masked = true; |
| 147 |
} else if (streq(verb, "reenable")) { |
| 148 |
method = "ReenableUnitFiles"; |
| 149 |
expect_carries_install_info = true; |
| 150 |
} else if (streq(verb, "link")) |
| 151 |
method = "LinkUnitFiles"; |
| 152 |
else if (streq(verb, "preset")) { |
| 153 |
|
| 154 |
if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { |
| 155 |
method = "PresetUnitFilesWithMode"; |
| 156 |
send_preset_mode = true; |
| 157 |
} else |
| 158 |
method = "PresetUnitFiles"; |
| 159 |
|
| 160 |
expect_carries_install_info = true; |
| 161 |
ignore_carries_install_info = true; |
| 162 |
} else if (streq(verb, "mask")) { |
| 163 |
method = "MaskUnitFiles"; |
| 164 |
|
| 165 |
warn_trigger_operation = "Masking"; |
| 166 |
warn_trigger_ignore_masked = false; |
| 167 |
} else if (streq(verb, "unmask")) { |
| 168 |
method = "UnmaskUnitFiles"; |
| 169 |
send_force = false; |
| 170 |
} else if (streq(verb, "revert")) { |
| 171 |
method = "RevertUnitFiles"; |
| 172 |
send_runtime = send_force = false; |
| 173 |
} else |
| 174 |
assert_not_reached(); |
| 175 |
|
| 176 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
| 177 |
if (r < 0) |
| 178 |
return bus_log_create_error(r); |
| 179 |
|
| 180 |
r = sd_bus_message_append_strv(m, names); |
| 181 |
if (r < 0) |
| 182 |
return bus_log_create_error(r); |
| 183 |
|
| 184 |
if (send_preset_mode) { |
| 185 |
r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); |
| 186 |
if (r < 0) |
| 187 |
return bus_log_create_error(r); |
| 188 |
} |
| 189 |
|
| 190 |
if (send_runtime) { |
| 191 |
if (streq(method, "DisableUnitFilesWithFlagsAndInstallInfo")) |
| 192 |
r = sd_bus_message_append(m, "t", arg_runtime ? (uint64_t) UNIT_FILE_RUNTIME : UINT64_C(0)); |
| 193 |
else |
| 194 |
r = sd_bus_message_append(m, "b", arg_runtime); |
| 195 |
if (r < 0) |
| 196 |
return bus_log_create_error(r); |
| 197 |
} |
| 198 |
|
| 199 |
if (send_force) { |
| 200 |
r = sd_bus_message_append(m, "b", arg_force); |
| 201 |
if (r < 0) |
| 202 |
return bus_log_create_error(r); |
| 203 |
} |
| 204 |
|
| 205 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 206 |
if (r < 0) |
| 207 |
return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
| 208 |
|
| 209 |
if (expect_carries_install_info) { |
| 210 |
r = sd_bus_message_read(reply, "b", &carries_install_info); |
| 211 |
if (r < 0) |
| 212 |
return bus_log_parse_error(r); |
| 213 |
} |
| 214 |
|
| 215 |
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet); |
| 216 |
if (r < 0) |
| 217 |
return r; |
| 218 |
|
| 219 |
/* Try to reload if enabled */ |
| 220 |
if (!arg_no_reload) { |
| 221 |
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false); |
| 222 |
if (r < 0) |
| 223 |
return r; |
| 224 |
} |
| 225 |
|
| 226 |
if (warn_trigger_operation && !arg_quiet && !arg_no_warn) |
| 227 |
STRV_FOREACH(unit, names) |
| 228 |
warn_triggering_units(bus, *unit, warn_trigger_operation, warn_trigger_ignore_masked); |
| 229 |
} else { |
| 230 |
UnitFileFlags flags; |
| 231 |
InstallChange *changes = NULL; |
| 232 |
size_t n_changes = 0; |
| 233 |
|
| 234 |
CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
| 235 |
|
| 236 |
flags = unit_file_flags_from_args(); |
| 237 |
|
| 238 |
if (streq(verb, "enable")) { |
| 239 |
r = unit_file_enable(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 240 |
carries_install_info = r; |
| 241 |
} else if (streq(verb, "disable")) { |
| 242 |
r = unit_file_disable(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 243 |
carries_install_info = r; |
| 244 |
} else if (streq(verb, "reenable")) { |
| 245 |
r = unit_file_reenable(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 246 |
carries_install_info = r; |
| 247 |
} else if (streq(verb, "link")) |
| 248 |
r = unit_file_link(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 249 |
else if (streq(verb, "preset")) |
| 250 |
r = unit_file_preset(arg_runtime_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
| 251 |
else if (streq(verb, "mask")) |
| 252 |
r = unit_file_mask(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 253 |
else if (streq(verb, "unmask")) |
| 254 |
r = unit_file_unmask(arg_runtime_scope, flags, arg_root, names, &changes, &n_changes); |
| 255 |
else if (streq(verb, "revert")) |
| 256 |
r = unit_file_revert(arg_runtime_scope, arg_root, names, &changes, &n_changes); |
| 257 |
else |
| 258 |
assert_not_reached(); |
| 259 |
|
| 260 |
r = install_changes_dump(r, verb, changes, n_changes, arg_quiet); |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
} |
| 264 |
|
| 265 |
if (carries_install_info == 0 && !ignore_carries_install_info) |
| 266 |
log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, UpheldBy=,\n" |
| 267 |
"Also=, or Alias= settings in the [Install] section, and DefaultInstance= for\n" |
| 268 |
"template units). This means they are not meant to be enabled or disabled using systemctl.\n" |
| 269 |
" \n" /* trick: the space is needed so that the line does not get stripped from output */ |
| 270 |
"Possible reasons for having these kinds of units are:\n" |
| 271 |
"%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
| 272 |
" .wants/, .requires/, or .upholds/ directory.\n" |
| 273 |
"%1$s A unit's purpose may be to act as a helper for some other unit which has\n" |
| 274 |
" a requirement dependency on it.\n" |
| 275 |
"%1$s A unit may be started when needed via activation (socket, path, timer,\n" |
| 276 |
" D-Bus, udev, scripted systemctl call, ...).\n" |
| 277 |
"%1$s In case of template units, the unit is meant to be enabled with some\n" |
| 278 |
" instance name specified.", |
| 279 |
glyph(GLYPH_BULLET)); |
| 280 |
|
| 281 |
if (streq(verb, "disable") && arg_runtime_scope == RUNTIME_SCOPE_USER && !arg_quiet && !arg_no_warn) { |
| 282 |
/* If some of the units are disabled in user scope but still enabled in global scope, |
| 283 |
* we emit a warning for that. */ |
| 284 |
|
| 285 |
/* No strv_free here, strings are owned by 'names' */ |
| 286 |
_cleanup_free_ char **enabled_in_global_scope = NULL; |
| 287 |
|
| 288 |
STRV_FOREACH(name, names) { |
| 289 |
UnitFileState state; |
| 290 |
|
| 291 |
r = unit_file_get_state(RUNTIME_SCOPE_GLOBAL, arg_root, *name, &state); |
| 292 |
if (r == -ENOENT) |
| 293 |
continue; |
| 294 |
if (r < 0) |
| 295 |
return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
| 296 |
|
| 297 |
if (IN_SET(state, UNIT_FILE_ENABLED, UNIT_FILE_ENABLED_RUNTIME)) { |
| 298 |
r = strv_push(&enabled_in_global_scope, *name); |
| 299 |
if (r < 0) |
| 300 |
return log_oom(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if (!strv_isempty(enabled_in_global_scope)) { |
| 305 |
_cleanup_free_ char *joined = NULL; |
| 306 |
|
| 307 |
joined = strv_join(enabled_in_global_scope, ", "); |
| 308 |
if (!joined) |
| 309 |
return log_oom(); |
| 310 |
|
| 311 |
log_notice("The following unit files have been enabled in global scope. This means\n" |
| 312 |
"they will still be started automatically after a successful disablement\n" |
| 313 |
"in user scope:\n" |
| 314 |
"%s", |
| 315 |
joined); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (arg_now) { |
| 320 |
_cleanup_strv_free_ char **new_args = NULL; |
| 321 |
const char *start_verb; |
| 322 |
bool accept_path, prohibit_templates, dead_ok = false; |
| 323 |
|
| 324 |
if (streq(verb, "enable")) { |
| 325 |
start_verb = "start"; |
| 326 |
accept_path = true; |
| 327 |
prohibit_templates = true; |
| 328 |
} else if (STR_IN_SET(verb, "disable", "mask")) { |
| 329 |
start_verb = "stop"; |
| 330 |
accept_path = false; |
| 331 |
prohibit_templates = false; |
| 332 |
dead_ok = true; /* If the service is not running anyway, no need to stop it. */ |
| 333 |
} else if (streq(verb, "reenable")) { |
| 334 |
/* Note that we use try-restart here. This matches the semantics of reenable better, |
| 335 |
* and allows us to glob template units. */ |
| 336 |
start_verb = "try-restart"; |
| 337 |
accept_path = true; |
| 338 |
prohibit_templates = false; |
| 339 |
} else |
| 340 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 341 |
"--now can only be used with verb enable, disable, reenable, or mask."); |
| 342 |
|
| 343 |
switch (install_client_side()) { |
| 344 |
case INSTALL_CLIENT_SIDE_NO: |
| 345 |
break; |
| 346 |
case INSTALL_CLIENT_SIDE_OVERRIDE: |
| 347 |
case INSTALL_CLIENT_SIDE_OFFLINE: |
| 348 |
case INSTALL_CLIENT_SIDE_NOT_BOOTED: |
| 349 |
if (!dead_ok) |
| 350 |
log_warning("Cannot %s unit with --now when systemd is not running, ignoring.", start_verb); |
| 351 |
return 0; |
| 352 |
case INSTALL_CLIENT_SIDE_ARG_ROOT: |
| 353 |
return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "--now cannot be used with --root=."); |
| 354 |
case INSTALL_CLIENT_SIDE_GLOBAL_SCOPE: |
| 355 |
return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "--now cannot be used with --global."); |
| 356 |
} |
| 357 |
|
| 358 |
assert(bus); |
| 359 |
|
| 360 |
if (strv_extend(&new_args, start_verb) < 0) |
| 361 |
return log_oom(); |
| 362 |
|
| 363 |
STRV_FOREACH(name, names) { |
| 364 |
_cleanup_free_ char *fn = NULL; |
| 365 |
const char *unit_name; |
| 366 |
|
| 367 |
if (accept_path) { |
| 368 |
/* 'enable' and 'reenable' accept path to unit files, so extract it first. */ |
| 369 |
|
| 370 |
r = path_extract_filename(*name, &fn); |
| 371 |
if (r < 0) |
| 372 |
return log_error_errno(r, "Failed to extract filename of '%s': %m", *name); |
| 373 |
|
| 374 |
unit_name = fn; |
| 375 |
} else |
| 376 |
unit_name = *name; |
| 377 |
|
| 378 |
if (unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { |
| 379 |
char *globbed; |
| 380 |
|
| 381 |
if (prohibit_templates) { |
| 382 |
/* Skip template units when enabling. Globbing doesn't make sense |
| 383 |
* since the semantics would be altered (we're operating on |
| 384 |
* DefaultInstance= when enabling), and starting template unit |
| 385 |
* is not supported anyway. */ |
| 386 |
log_warning("Template unit is not supported by %s --now, skipping: %s", |
| 387 |
verb, unit_name); |
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
assert(!STR_IN_SET(start_verb, "start", "restart")); |
| 392 |
|
| 393 |
r = unit_name_replace_instance_full(unit_name, "*", /* accept_glob= */ true, &globbed); |
| 394 |
if (r < 0) |
| 395 |
return log_error_errno(r, "Failed to glob unit name '%s': %m", unit_name); |
| 396 |
|
| 397 |
r = strv_consume(&new_args, globbed); |
| 398 |
} else |
| 399 |
r = strv_extend(&new_args, unit_name); |
| 400 |
if (r < 0) |
| 401 |
return log_oom(); |
| 402 |
} |
| 403 |
|
| 404 |
return verb_start(strv_length(new_args), new_args, data, userdata); |
| 405 |
} |
| 406 |
|
| 407 |
return 0; |
| 408 |
} |