diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-03-18 19:22:43 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-03-18 19:31:34 +0100 |
commit | 03e334a1c7dc8c20c38902aa039440763acc9b17 (patch) | |
tree | bc30b522de8ef9c251bf3ff2fe2d52c92dd8b1ea /src/readahead | |
parent | 9459781ee66eb57709c8b8701701365ba60a9f1c (diff) |
util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:
fd = safe_close(fd);
Which will close an fd if it is open, and reset the fd variable
correctly.
By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
Diffstat (limited to 'src/readahead')
-rw-r--r-- | src/readahead/readahead-collect.c | 22 | ||||
-rw-r--r-- | src/readahead/readahead-common.c | 2 | ||||
-rw-r--r-- | src/readahead/readahead-replay.c | 18 |
3 files changed, 13 insertions, 29 deletions
diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index be920062dc..c1afd0d605 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -176,8 +176,7 @@ finish: if (start != MAP_FAILED) munmap(start, l); - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); return r; } @@ -493,16 +492,12 @@ static int collect(const char *root) { log_warning("readlink(%s) failed: %s", fn, strerror(-k)); next_iteration: - if (m->fd >= 0) - close_nointr_nofail(m->fd); + safe_close(m->fd); } } done: - if (fanotify_fd >= 0) { - close_nointr_nofail(fanotify_fd); - fanotify_fd = -1; - } + fanotify_fd = safe_close(fanotify_fd); log_debug("Writing Pack File..."); @@ -592,14 +587,9 @@ done: log_debug("Done."); finish: - if (fanotify_fd >= 0) - close_nointr_nofail(fanotify_fd); - - if (signal_fd >= 0) - close_nointr_nofail(signal_fd); - - if (inotify_fd >= 0) - close_nointr_nofail(inotify_fd); + safe_close(fanotify_fd); + safe_close(signal_fd); + safe_close(inotify_fd); if (pack) { fclose(pack); diff --git a/src/readahead/readahead-common.c b/src/readahead/readahead-common.c index aea1fbeea4..5ffa88b78a 100644 --- a/src/readahead/readahead-common.c +++ b/src/readahead/readahead-common.c @@ -220,7 +220,7 @@ int open_inotify(void) { if (inotify_add_watch(fd, "/run/systemd/readahead", IN_CREATE) < 0) { log_error("Failed to watch /run/systemd/readahead: %m"); - close_nointr_nofail(fd); + safe_close(fd); return -errno; } diff --git a/src/readahead/readahead-replay.c b/src/readahead/readahead-replay.c index cb04e5f9cd..8dc194257f 100644 --- a/src/readahead/readahead-replay.c +++ b/src/readahead/readahead-replay.c @@ -67,10 +67,8 @@ static int unpack_file(FILE *pack) { if (errno != ENOENT && errno != EPERM && errno != EACCES && errno != ELOOP) log_warning("open(%s) failed: %m", fn); - } else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) { - close_nointr_nofail(fd); - fd = -1; - } + } else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) + fd = safe_close(fd); if (fread(&inode, sizeof(inode), 1, pack) != 1) { log_error("Premature end of pack file."); @@ -81,10 +79,8 @@ static int unpack_file(FILE *pack) { if (fd >= 0) { /* If the inode changed the file got deleted, so just * ignore this entry */ - if (st.st_ino != (uint64_t) inode) { - close_nointr_nofail(fd); - fd = -1; - } + if (st.st_ino != (uint64_t) inode) + fd = safe_close(fd); } for (;;) { @@ -129,8 +125,7 @@ static int unpack_file(FILE *pack) { } finish: - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); return r; } @@ -279,8 +274,7 @@ finish: if (pack) fclose(pack); - if (inotify_fd >= 0) - close_nointr_nofail(inotify_fd); + safe_close(inotify_fd); free(pack_fn); |