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/journald-kmsg.c | |
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/journald-kmsg.c')
-rw-r--r-- | src/journal/journald-kmsg.c | 14 |
1 files changed, 3 insertions, 11 deletions
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; |