diff options
author | Luca Boccassi <bluca@debian.org> | 2024-08-30 18:55:18 +0200 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2024-09-02 16:33:29 +0200 |
commit | 1e2d1a7202400e08a00782f32804fdc503259806 (patch) | |
tree | dd7418cd40d1f62e5c51a183fd2fd224ddfba0f3 /src/portable | |
parent | network/route: fix typo (diff) | |
download | systemd-1e2d1a7202400e08a00782f32804fdc503259806.tar.xz systemd-1e2d1a7202400e08a00782f32804fdc503259806.zip |
portable: ensure PORTABLE_FORCE_ATTACH works even when there is a leftover unit
Force means force, we skip checks with PID1 for existing units, but
then bail out with EEXIST if the files are actually there. Overwrite
everything instead.
Diffstat (limited to 'src/portable')
-rw-r--r-- | src/portable/portable.c | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/src/portable/portable.c b/src/portable/portable.c index 08e9bc0113..af7d9552da 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1255,8 +1255,12 @@ static int install_profile_dropin( return -ENOMEM; if (flags & PORTABLE_PREFER_COPY) { + CopyFlags copy_flags = COPY_REFLINK|COPY_FSYNC; - r = copy_file_atomic(from, dropin, 0644, COPY_REFLINK|COPY_FSYNC); + if (flags & PORTABLE_FORCE_ATTACH) + copy_flags |= COPY_REPLACE; + + r = copy_file_atomic(from, dropin, 0644, copy_flags); if (r < 0) return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin); @@ -1264,8 +1268,12 @@ static int install_profile_dropin( } else { - if (symlink(from, dropin) < 0) - return log_debug_errno(errno, "Failed to link %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin); + if (flags & PORTABLE_FORCE_ATTACH) + r = symlink_atomic(from, dropin); + else + r = RET_NERRNO(symlink(from, dropin)); + if (r < 0) + return log_debug_errno(r, "Failed to link %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin); (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from); } @@ -1351,15 +1359,23 @@ static int attach_unit_file( if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) { - if (symlink(m->source, path) < 0) - return log_debug_errno(errno, "Failed to symlink unit file '%s': %m", path); + if (flags & PORTABLE_FORCE_ATTACH) + r = symlink_atomic(m->source, path); + else + r = RET_NERRNO(symlink(m->source, path)); + if (r < 0) + return log_debug_errno(r, "Failed to symlink unit file '%s': %m", path); (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source); } else { + LinkTmpfileFlags link_flags = LINK_TMPFILE_SYNC; _cleanup_(unlink_and_freep) char *tmp = NULL; _cleanup_close_ int fd = -EBADF; + if (flags & PORTABLE_FORCE_ATTACH) + link_flags |= LINK_TMPFILE_REPLACE; + (void) mac_selinux_create_file_prepare_label(path, m->selinux_label); fd = open_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &tmp); @@ -1374,7 +1390,7 @@ static int attach_unit_file( if (fchmod(fd, 0644) < 0) return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path); - r = link_tmpfile(fd, tmp, path, LINK_TMPFILE_SYNC); + r = link_tmpfile(fd, tmp, path, link_flags); if (r < 0) return log_debug_errno(r, "Failed to install unit file '%s': %m", path); |