*: use _cleanup_close_ with fdopen() where trivial

Also convert these to use take_fdopen().
This commit is contained in:
Vito Caputo 2020-03-31 02:29:37 -07:00
parent 9f81a592c1
commit b46c3e4913
5 changed files with 16 additions and 25 deletions

View File

@ -78,18 +78,16 @@ int mkostemp_safe(char *pattern) {
}
int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
int fd;
_cleanup_close_ int fd = -1;
FILE *f;
fd = mkostemp_safe(pattern);
if (fd < 0)
return fd;
f = fdopen(fd, mode);
if (!f) {
safe_close(fd);
f = take_fdopen(&fd, mode);
if (!f)
return -errno;
}
*ret_f = f;
return 0;

View File

@ -982,8 +982,9 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
char machine_string[SD_ID128_STRING_MAX];
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_close_ int fd = -1;
const char *p;
int r, fd;
int r;
p = prefix_roota(esp_path, "/loader/loader.conf");
if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
@ -993,11 +994,9 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
if (fd < 0)
return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
f = fdopen(fd, "w");
if (!f) {
safe_close(fd);
f = take_fdopen(&fd, "w");
if (!f)
return log_oom();
}
fprintf(f, "#timeout 3\n"
"#console-mode keep\n"

View File

@ -3107,7 +3107,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
}
int manager_open_serialization(Manager *m, FILE **_f) {
int fd;
_cleanup_close_ int fd = -1;
FILE *f;
assert(_f);
@ -3116,11 +3116,9 @@ int manager_open_serialization(Manager *m, FILE **_f) {
if (fd < 0)
return fd;
f = fdopen(fd, "w+");
if (!f) {
safe_close(fd);
f = take_fdopen(&fd, "w+");
if (!f)
return -errno;
}
*_f = f;
return 0;

View File

@ -560,7 +560,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
_cleanup_fclose_ FILE *fdinfo = NULL;
_cleanup_free_ char *fdname = NULL;
int fd;
_cleanup_close_ int fd = -1;
r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
if (r < 0)
@ -574,11 +574,9 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
if (fd < 0)
continue;
fdinfo = fdopen(fd, "r");
if (!fdinfo) {
safe_close(fd);
fdinfo = take_fdopen(&fd, "r");
if (!fdinfo)
continue;
}
for (;;) {
_cleanup_free_ char *line = NULL;

View File

@ -123,17 +123,15 @@ static int request_meta_ensure_tmp(RequestMeta *m) {
if (m->tmp)
rewind(m->tmp);
else {
int fd;
_cleanup_close_ int fd = -1;
fd = open_tmpfile_unlinkable("/tmp", O_RDWR|O_CLOEXEC);
if (fd < 0)
return fd;
m->tmp = fdopen(fd, "w+");
if (!m->tmp) {
safe_close(fd);
m->tmp = take_fdopen(&fd, "w+");
if (!m->tmp)
return -errno;
}
}
return 0;