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/journal | |
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/journal')
-rw-r--r-- | src/journal/cat.c | 9 | ||||
-rw-r--r-- | src/journal/catalog.c | 8 | ||||
-rw-r--r-- | src/journal/coredumpctl.c | 3 | ||||
-rw-r--r-- | src/journal/journal-authenticate.c | 5 | ||||
-rw-r--r-- | src/journal/journal-file.c | 4 | ||||
-rw-r--r-- | src/journal/journal-send.c | 14 | ||||
-rw-r--r-- | src/journal/journal-verify.c | 12 | ||||
-rw-r--r-- | src/journal/journalctl.c | 5 | ||||
-rw-r--r-- | src/journal/journald-console.c | 2 | ||||
-rw-r--r-- | src/journal/journald-kmsg.c | 14 | ||||
-rw-r--r-- | src/journal/journald-server.c | 22 | ||||
-rw-r--r-- | src/journal/journald-stream.c | 7 | ||||
-rw-r--r-- | src/journal/sd-journal.c | 3 | ||||
-rw-r--r-- | src/journal/test-journal-verify.c | 2 | ||||
-rw-r--r-- | src/journal/test-mmap-cache.c | 6 |
15 files changed, 44 insertions, 72 deletions
diff --git a/src/journal/cat.c b/src/journal/cat.c index 02b75642a3..60625cb6dd 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -152,7 +152,7 @@ int main(int argc, char *argv[]) { } if (fd >= 3) - close_nointr_nofail(fd); + safe_close(fd); fd = -1; @@ -170,11 +170,8 @@ int main(int argc, char *argv[]) { log_error("Failed to execute process: %s", strerror(-r)); finish: - if (fd >= 0) - close_nointr_nofail(fd); - - if (saved_stderr >= 0) - close_nointr_nofail(saved_stderr); + safe_close(fd); + safe_close(saved_stderr); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } diff --git a/src/journal/catalog.c b/src/journal/catalog.c index 2823232cbf..3ed0b7ee81 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -469,18 +469,18 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p return -errno; if (fstat(fd, &st) < 0) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } if (st.st_size < (off_t) sizeof(CatalogHeader)) { - close_nointr_nofail(fd); + safe_close(fd); return -EINVAL; } p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } @@ -491,7 +491,7 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p h->incompatible_flags != 0 || le64toh(h->n_items) <= 0 || st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) { - close_nointr_nofail(fd); + safe_close(fd); munmap(p, st.st_size); return -EBADMSG; } diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index 3bceb48ff7..bf943bc032 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -490,8 +490,7 @@ static int run_gdb(sd_journal *j) { goto finish; } - close_nointr_nofail(fd); - fd = -1; + fd = safe_close(fd); pid = fork(); if (pid < 0) { diff --git a/src/journal/journal-authenticate.c b/src/journal/journal-authenticate.c index f416b79a34..5ab1982bf0 100644 --- a/src/journal/journal-authenticate.c +++ b/src/journal/journal-authenticate.c @@ -418,10 +418,9 @@ finish: if (m) munmap(m, PAGE_ALIGN(sizeof(FSSHeader))); - if (fd >= 0) - close_nointr_nofail(fd); - + safe_close(fd); free(p); + return r; } diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 0e1fc7f7bc..f2f1f35fc3 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -133,9 +133,7 @@ void journal_file_close(JournalFile *f) { if (f->header) munmap(f->header, PAGE_ALIGN(sizeof(Header))); - if (f->fd >= 0) - close_nointr_nofail(f->fd); - + safe_close(f->fd); free(f->path); if (f->mmap) diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c index ca9199f718..d92e84baab 100644 --- a/src/journal/journal-send.c +++ b/src/journal/journal-send.c @@ -66,7 +66,7 @@ retry: fd_inc_sndbuf(fd, SNDBUF_SIZE); if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) { - close_nointr_nofail(fd); + safe_close(fd); goto retry; } @@ -316,7 +316,7 @@ _public_ int sd_journal_sendv(const struct iovec *iov, int n) { n = writev(buffer_fd, w, j); if (n < 0) { - close_nointr_nofail(buffer_fd); + safe_close(buffer_fd); return -errno; } @@ -336,7 +336,7 @@ _public_ int sd_journal_sendv(const struct iovec *iov, int n) { mh.msg_controllen = cmsg->cmsg_len; k = sendmsg(fd, &mh, MSG_NOSIGNAL); - close_nointr_nofail(buffer_fd); + safe_close(buffer_fd); if (k < 0) return -errno; @@ -412,12 +412,12 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path)); if (r < 0) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } if (shutdown(fd, SHUT_RD) < 0) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } @@ -445,12 +445,12 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve r = loop_write(fd, header, l, false); if (r < 0) { - close_nointr_nofail(fd); + safe_close(fd); return (int) r; } if ((size_t) r != l) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c index 9434cc925c..31bae5a8f8 100644 --- a/src/journal/journal-verify.c +++ b/src/journal/journal-verify.c @@ -1222,9 +1222,9 @@ int journal_file_verify( mmap_cache_close_fd(f->mmap, entry_fd); mmap_cache_close_fd(f->mmap, entry_array_fd); - close_nointr_nofail(data_fd); - close_nointr_nofail(entry_fd); - close_nointr_nofail(entry_array_fd); + safe_close(data_fd); + safe_close(entry_fd); + safe_close(entry_array_fd); if (first_contained) *first_contained = le64toh(f->header->head_entry_realtime); @@ -1247,17 +1247,17 @@ fail: if (data_fd >= 0) { mmap_cache_close_fd(f->mmap, data_fd); - close_nointr_nofail(data_fd); + safe_close(data_fd); } if (entry_fd >= 0) { mmap_cache_close_fd(f->mmap, entry_fd); - close_nointr_nofail(entry_fd); + safe_close(entry_fd); } if (entry_array_fd >= 0) { mmap_cache_close_fd(f->mmap, entry_array_fd); - close_nointr_nofail(entry_array_fd); + safe_close(entry_array_fd); } return r; diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 0619b256b9..019629047b 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1290,7 +1290,7 @@ static int setup_keys(void) { n = now(CLOCK_REALTIME); n /= arg_interval; - close_nointr_nofail(fd); + safe_close(fd); fd = mkostemp_safe(k, O_WRONLY|O_CLOEXEC); if (fd < 0) { log_error("Failed to open %s: %m", k); @@ -1389,8 +1389,7 @@ static int setup_keys(void) { r = 0; finish: - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); if (k) { unlink(k); diff --git a/src/journal/journald-console.c b/src/journal/journald-console.c index 35da52af2a..3db5fc50a1 100644 --- a/src/journal/journald-console.c +++ b/src/journal/journald-console.c @@ -107,5 +107,5 @@ void server_forward_console( if (writev(fd, iovec, n) < 0) log_debug("Failed to write to %s for logging: %m", tty); - close_nointr_nofail(fd); + safe_close(fd); } diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c index 05b128f843..35948ea754 100644 --- a/src/journal/journald-kmsg.c +++ b/src/journal/journald-kmsg.c @@ -428,19 +428,14 @@ int server_open_dev_kmsg(Server *s) { return 0; fail: - if (s->dev_kmsg_event_source) - s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); - - if (s->dev_kmsg_fd >= 0) { - close_nointr_nofail(s->dev_kmsg_fd); - s->dev_kmsg_fd = -1; - } + s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); + s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd); return r; } int server_open_kernel_seqnum(Server *s) { - int fd; + _cleanup_close_ int fd; uint64_t *p; assert(s); @@ -457,18 +452,15 @@ int server_open_kernel_seqnum(Server *s) { if (posix_fallocate(fd, 0, sizeof(uint64_t)) < 0) { log_error("Failed to allocate sequential number file, ignoring: %m"); - close_nointr_nofail(fd); return 0; } p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { log_error("Failed to map sequential number file, ignoring: %m"); - close_nointr_nofail(fd); return 0; } - close_nointr_nofail(fd); s->kernel_seqnum = p; return 0; diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index ef39d0a602..c2a60d5e83 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -1432,8 +1432,7 @@ static int server_open_hostname(Server *s) { if (r == -EPERM) { log_warning("Failed to register hostname fd in event loop: %s. Ignoring.", strerror(-r)); - close_nointr_nofail(s->hostname_fd); - s->hostname_fd = -1; + s->hostname_fd = safe_close(s->hostname_fd); return 0; } @@ -1643,20 +1642,11 @@ void server_done(Server *s) { sd_event_source_unref(s->hostname_event_source); sd_event_unref(s->event); - if (s->syslog_fd >= 0) - close_nointr_nofail(s->syslog_fd); - - if (s->native_fd >= 0) - close_nointr_nofail(s->native_fd); - - if (s->stdout_fd >= 0) - close_nointr_nofail(s->stdout_fd); - - if (s->dev_kmsg_fd >= 0) - close_nointr_nofail(s->dev_kmsg_fd); - - if (s->hostname_fd >= 0) - close_nointr_nofail(s->hostname_fd); + safe_close(s->syslog_fd); + safe_close(s->native_fd); + safe_close(s->stdout_fd); + safe_close(s->dev_kmsg_fd); + safe_close(s->hostname_fd); if (s->rate_limit) journal_rate_limit_free(s->rate_limit); diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index c46ffe5d45..89da150a60 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -341,8 +341,7 @@ void stdout_stream_free(StdoutStream *s) { s->event_source = sd_event_source_unref(s->event_source); } - if (s->fd >= 0) - close_nointr_nofail(s->fd); + safe_close(s->fd); #ifdef HAVE_SELINUX if (s->security_context) @@ -377,13 +376,13 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) { log_warning("Too many stdout streams, refusing connection."); - close_nointr_nofail(fd); + safe_close(fd); return 0; } stream = new0(StdoutStream, 1); if (!stream) { - close_nointr_nofail(fd); + safe_close(fd); return log_oom(); } diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index b54bc21090..7587211506 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -1850,8 +1850,7 @@ _public_ void sd_journal_close(sd_journal *j) { hashmap_free(j->directories_by_path); hashmap_free(j->directories_by_wd); - if (j->inotify_fd >= 0) - close_nointr_nofail(j->inotify_fd); + safe_close(j->inotify_fd); if (j->mmap) { log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap)); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index 0540074207..3b181c6794 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -48,7 +48,7 @@ static void bit_toggle(const char *fn, uint64_t p) { r = pwrite(fd, &b, 1, p/8); assert(r == 1); - close_nointr_nofail(fd); + safe_close(fd); } static int raw_verify(const char *fn, const char *verification_key) { diff --git a/src/journal/test-mmap-cache.c b/src/journal/test-mmap-cache.c index 7d03bfe9d1..b7bb260fcf 100644 --- a/src/journal/test-mmap-cache.c +++ b/src/journal/test-mmap-cache.c @@ -72,9 +72,9 @@ int main(int argc, char *argv[]) { mmap_cache_unref(m); - close_nointr_nofail(x); - close_nointr_nofail(y); - close_nointr_nofail(z); + safe_close(x); + safe_close(y); + safe_close(z); return 0; } |