From 41615d59fa09644b5286cbbca05f52d3fbda0236 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 4 Feb 2026 02:31:51 +0100 Subject: [PATCH] various: port manual goto cleanup to CLEANUP_TMPFILE_AT --- src/bootctl/bootctl-random-seed.c | 28 ++++--------- src/libsystemd-network/sd-dhcp-server-lease.c | 16 +++---- src/shared/tar-util.c | 42 +++++++------------ 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/src/bootctl/bootctl-random-seed.c b/src/bootctl/bootctl-random-seed.c index 8abb6c19697..f6f41dc226a 100644 --- a/src/bootctl/bootctl-random-seed.c +++ b/src/bootctl/bootctl-random-seed.c @@ -175,26 +175,22 @@ int install_random_seed(const char *esp) { if (fd < 0) return log_error_errno(fd, "Failed to open random seed file for writing: %m"); + CLEANUP_TMPFILE_AT(loader_dir_fd, tmp); + if (!warned) /* only warn once per seed file */ (void) random_seed_verify_permissions(fd, S_IFREG); r = loop_write(fd, buffer, sizeof(buffer)); - if (r < 0) { - log_error_errno(r, "Failed to write random seed file: %m"); - goto fail; - } + if (r < 0) + return log_error_errno(r, "Failed to write random seed file: %m"); - if (fsync(fd) < 0 || fsync(loader_dir_fd) < 0) { - r = log_error_errno(errno, "Failed to sync random seed file: %m"); - goto fail; - } + if (fsync(fd) < 0 || fsync(loader_dir_fd) < 0) + return log_error_errno(errno, "Failed to sync random seed file: %m"); - if (renameat(loader_dir_fd, tmp, loader_dir_fd, "random-seed") < 0) { - r = log_error_errno(errno, "Failed to move random seed file into place: %m"); - goto fail; - } + if (renameat(loader_dir_fd, tmp, loader_dir_fd, "random-seed") < 0) + return log_error_errno(errno, "Failed to move random seed file into place: %m"); - tmp = mfree(tmp); + tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT() */ if (syncfs(fd) < 0) return log_error_errno(errno, "Failed to sync ESP file system: %m"); @@ -202,12 +198,6 @@ int install_random_seed(const char *esp) { log_info("Random seed file %s/loader/random-seed successfully %s (%zu bytes).", esp, refreshed ? "refreshed" : "written", sizeof(buffer)); return set_system_token(); - -fail: - assert(tmp); - (void) unlinkat(loader_dir_fd, tmp, 0); - - return r; } int verb_random_seed(int argc, char *argv[], void *userdata) { diff --git a/src/libsystemd-network/sd-dhcp-server-lease.c b/src/libsystemd-network/sd-dhcp-server-lease.c index 171d809faab..5c24de4084f 100644 --- a/src/libsystemd-network/sd-dhcp-server-lease.c +++ b/src/libsystemd-network/sd-dhcp-server-lease.c @@ -317,8 +317,6 @@ int dhcp_server_static_leases_append_json(sd_dhcp_server *server, sd_json_varian int dhcp_server_save_leases(sd_dhcp_server *server) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - _cleanup_free_ char *temp_path = NULL; - _cleanup_fclose_ FILE *f = NULL; sd_id128_t boot_id; int r; @@ -355,25 +353,27 @@ int dhcp_server_save_leases(sd_dhcp_server *server) { if (r < 0) return r; + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + r = fopen_temporary_at(server->lease_dir_fd, server->lease_file, &f, &temp_path); if (r < 0) return r; + CLEANUP_TMPFILE_AT(server->lease_dir_fd, temp_path); + (void) fchmod(fileno(f), 0644); r = sd_json_variant_dump(v, SD_JSON_FORMAT_NEWLINE | SD_JSON_FORMAT_FLUSH, f, /* prefix= */ NULL); if (r < 0) - goto failure; + return r; r = conservative_renameat(server->lease_dir_fd, temp_path, server->lease_dir_fd, server->lease_file); if (r < 0) - goto failure; + return r; + temp_path = mfree(temp_path); /* disarm CLEANUP_TMPFILE_AT() */ return 0; - -failure: - (void) unlinkat(server->lease_dir_fd, temp_path, /* flags= */ 0); - return r; } static int json_dispatch_chaddr(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { diff --git a/src/shared/tar-util.c b/src/shared/tar-util.c index 7fe4544c3c8..27f3212fb32 100644 --- a/src/shared/tar-util.c +++ b/src/shared/tar-util.c @@ -240,6 +240,8 @@ static int archive_unpack_regular( if (fd < 0) return log_error_errno(fd, "Failed to create regular file '%s': %m", path); + CLEANUP_TMPFILE_AT(parent_fd, tmp); + if ((fflags & CHATTR_EARLY_FL) != 0) { r = chattr_full(fd, /* path= */ NULL, @@ -250,45 +252,29 @@ static int archive_unpack_regular( CHATTR_FALLBACK_BITWISE); if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) log_warning_errno(r, "Failed to apply chattr of '%s', ignoring: %m", path); - else if (r < 0) { - log_error_errno(r, "Failed to adjust chattr of '%s': %m", path); - goto fail; - } + else if (r < 0) + return log_error_errno(r, "Failed to adjust chattr of '%s': %m", path); } r = sym_archive_read_data_into_fd(a, fd); - if (r != ARCHIVE_OK) { - r = log_error_errno( - SYNTHETIC_ERRNO(ENOTRECOVERABLE), - "Failed to unpack regular file '%s': %s", path, sym_archive_error_string(a)); - goto fail; - } + if (r != ARCHIVE_OK) + return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), + "Failed to unpack regular file '%s': %s", path, sym_archive_error_string(a)); /* If this is a sparse file, then libarchive's archive_read_data_into_fd() won't insert the final * hole. We need to manually truncate. */ off_t l = lseek(fd, 0, SEEK_CUR); - if (l < 0) { - r = log_error_errno(errno, "Failed to determine current file position in '%s': %m", path); - goto fail; - } - if (ftruncate(fd, l) < 0) { - r = log_error_errno(errno, "Failed to truncate regular file '%s' to %" PRIu64 ": %m", path, (uint64_t) l); - goto fail; - } + if (l < 0) + return log_error_errno(errno, "Failed to determine current file position in '%s': %m", path); + if (ftruncate(fd, l) < 0) + return log_error_errno(errno, "Failed to truncate regular file '%s' to %" PRIu64 ": %m", path, (uint64_t) l); r = link_tmpfile_at(fd, parent_fd, tmp, filename, LINK_TMPFILE_REPLACE); - if (r < 0) { - log_error_errno(r, "Failed to install regular file '%s': %m", path); - goto fail; - } + if (r < 0) + return log_error_errno(r, "Failed to install regular file '%s': %m", path); + tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT() */ return TAKE_FD(fd); - -fail: - if (tmp) - (void) unlinkat(parent_fd, tmp, /* flags= */ 0); - - return r; } static int archive_unpack_directory( -- 2.47.3