Skip to content

Commit 8c9fcb5

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) (cherry picked from commit 44c08e6)
1 parent 8f28021 commit 8c9fcb5

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
@@ -2241,7 +2241,9 @@ static int install_context_mark_for_removal(
22412241
else {
22422242
log_debug_errno(r, "Unit %s not found, removing name.", i->name);
22432243
r = install_changes_add(changes, n_changes, r, i->path ?: i->name, NULL);
2244-
if (r < 0)
2244+
/* In case there's no unit, we still want to remove any leftover symlink, even if
2245+
* the unit might have been removed already, hence treating ENOENT as non-fatal. */
2246+
if (r != -ENOENT)
22452247
return r;
22462248
}
22472249
} else if (r < 0) {
@@ -2839,9 +2841,13 @@ static int do_unit_file_disable(
28392841
r = install_info_add(&ctx, *name, NULL, lp->root_dir, /* auxiliary= */ false, &info);
28402842
if (r >= 0)
28412843
r = install_info_traverse(&ctx, lp, info, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, NULL);
2842-
2843-
if (r < 0)
2844-
return install_changes_add(changes, n_changes, r, *name, NULL);
2844+
if (r < 0) {
2845+
r = install_changes_add(changes, n_changes, r, *name, NULL);
2846+
/* In case there's no unit, we still want to remove any leftover symlink, even if
2847+
* the unit might have been removed already, hence treating ENOENT as non-fatal. */
2848+
if (r != -ENOENT)
2849+
return r;
2850+
}
28452851

28462852
/* If we enable multiple units, some with install info and others without,
28472853
* 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
@@ -309,6 +309,12 @@ systemctl cat "$UNIT_NAME"
309309
systemctl help "$UNIT_NAME"
310310
systemctl service-watchdogs
311311
systemctl service-watchdogs "$(systemctl service-watchdogs)"
312+
# Ensure that the enablement symlinks can still be removed after the user is gone, to avoid having leftovers
313+
systemctl enable "$UNIT_NAME"
314+
systemctl stop "$UNIT_NAME"
315+
rm -f "/usr/lib/systemd/system/$UNIT_NAME"
316+
systemctl daemon-reload
317+
systemctl disable "$UNIT_NAME"
312318

313319
# show/set-environment
314320
# Make sure PATH is set

0 commit comments

Comments
 (0)