Skip to content

Commit 44c08e6

Browse files
committed
install: allow removing symlinks even for units that are gone
If a symlink is leftover, still allow cleaning it up via 'disable'. This happens when a unit is stopped and removed, but not disabled, and a reload has already happened. At that point, cleaning up the old symlinks becomes impossible through the APIs, and needs to be done manually. Always allow cleaning up symlinks, if they exist, by only erroring out if there is an OOM. Follow-up for f31f10a (cherry picked from commit 5163c9b) (cherry picked from commit c26e56d)
1 parent 8ead254 commit 44c08e6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/shared/install.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,9 @@ static int install_context_mark_for_removal(
22322232
else {
22332233
log_debug_errno(r, "Unit %s not found, removing name.", i->name);
22342234
r = install_changes_add(changes, n_changes, r, i->path ?: i->name, NULL);
2235-
if (r < 0)
2235+
/* In case there's no unit, we still want to remove any leftover symlink, even if
2236+
* the unit might have been removed already, hence treating ENOENT as non-fatal. */
2237+
if (r != -ENOENT)
22362238
return r;
22372239
}
22382240
} else if (r < 0) {
@@ -2830,9 +2832,13 @@ static int do_unit_file_disable(
28302832
r = install_info_add(&ctx, *name, NULL, lp->root_dir, /* auxiliary= */ false, &info);
28312833
if (r >= 0)
28322834
r = install_info_traverse(&ctx, lp, info, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL);
2833-
2834-
if (r < 0)
2835-
return install_changes_add(changes, n_changes, r, *name, NULL);
2835+
if (r < 0) {
2836+
r = install_changes_add(changes, n_changes, r, *name, NULL);
2837+
/* In case there's no unit, we still want to remove any leftover symlink, even if
2838+
* the unit might have been removed already, hence treating ENOENT as non-fatal. */
2839+
if (r != -ENOENT)
2840+
return r;
2841+
}
28362842

28372843
/* If we enable multiple units, some with install info and others without,
28382844
* the "empty [Install] section" warning is not shown. Let's make the behavior

test/units/testsuite-26.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ systemctl cat "$UNIT_NAME"
311311
systemctl help "$UNIT_NAME"
312312
systemctl service-watchdogs
313313
systemctl service-watchdogs "$(systemctl service-watchdogs)"
314+
# Ensure that the enablement symlinks can still be removed after the user is gone, to avoid having leftovers
315+
systemctl enable "$UNIT_NAME"
316+
systemctl stop "$UNIT_NAME"
317+
rm -f "/usr/lib/systemd/system/$UNIT_NAME"
318+
systemctl daemon-reload
319+
systemctl disable "$UNIT_NAME"
314320

315321
# show/set-environment
316322
# Make sure PATH is set

0 commit comments

Comments
 (0)