diff options
-rw-r--r-- | src/basic/tmpfile-util.c | 39 | ||||
-rw-r--r-- | src/basic/tmpfile-util.h | 2 | ||||
-rw-r--r-- | src/boot/bootctl.c | 17 |
3 files changed, 45 insertions, 13 deletions
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index cf3bbad1c4..e0a338c163 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -275,6 +275,28 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) { return fd; } +int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file) { + _cleanup_free_ char *path = NULL; + _cleanup_fclose_ FILE *f = NULL; + _cleanup_close_ int fd = -1; + + assert(target); + assert(ret_file); + assert(ret_path); + + fd = open_tmpfile_linkable(target, flags, &path); + if (fd < 0) + return fd; + + f = take_fdopen(&fd, "w"); + if (!f) + return -ENOMEM; + + *ret_path = TAKE_PTR(path); + *ret_file = TAKE_PTR(f); + return 0; +} + int link_tmpfile(int fd, const char *path, const char *target) { assert(fd >= 0); assert(target); @@ -292,6 +314,23 @@ int link_tmpfile(int fd, const char *path, const char *target) { return RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW)); } +int flink_tmpfile(FILE *f, const char *path, const char *target) { + int fd, r; + + assert(f); + assert(target); + + fd = fileno(f); + if (fd < 0) /* Not all FILE* objects encapsulate fds */ + return -EBADF; + + r = fflush_sync_and_check(f); + if (r < 0) + return r; + + return link_tmpfile(fd, path, target); +} + int mkdtemp_malloc(const char *template, char **ret) { _cleanup_free_ char *p = NULL; int r; diff --git a/src/basic/tmpfile-util.h b/src/basic/tmpfile-util.h index 45255fc062..610cbaf87e 100644 --- a/src/basic/tmpfile-util.h +++ b/src/basic/tmpfile-util.h @@ -13,7 +13,9 @@ int tempfn_random_child(const char *p, const char *extra, char **ret); int open_tmpfile_unlinkable(const char *directory, int flags); int open_tmpfile_linkable(const char *target, int flags, char **ret_path); +int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file); int link_tmpfile(int fd, const char *path, const char *target); +int flink_tmpfile(FILE *f, const char *path, const char *target); int mkdtemp_malloc(const char *template, char **ret); diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 54d1630aab..10ed5c6f63 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -1286,7 +1286,6 @@ static int remove_loader_variables(void) { static int install_loader_config(const char *esp_path) { _cleanup_(unlink_and_freep) char *t = NULL; _cleanup_fclose_ FILE *f = NULL; - _cleanup_close_ int fd = -1; const char *p; int r; @@ -1296,13 +1295,9 @@ static int install_loader_config(const char *esp_path) { if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */ return 0; - fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t); - if (fd < 0) - return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p); - - f = take_fdopen(&fd, "w"); - if (!f) - return log_oom(); + r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f); + if (r < 0) + return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p); fprintf(f, "#timeout 3\n" "#console-mode keep\n"); @@ -1312,11 +1307,7 @@ static int install_loader_config(const char *esp_path) { fprintf(f, "default %s-*\n", arg_entry_token); } - r = fflush_sync_and_check(f); - if (r < 0) - return log_error_errno(r, "Failed to write \"%s\": %m", p); - - r = link_tmpfile(fileno(f), t, p); + r = flink_tmpfile(f, t, p); if (r == -EEXIST) return 0; /* Silently skip creation if the file exists now (recheck) */ if (r < 0) |