From 03e334a1c7dc8c20c38902aa039440763acc9b17 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 18 Mar 2014 19:22:43 +0100 Subject: 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. --- src/libsystemd/sd-event/sd-event.c | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) (limited to 'src/libsystemd/sd-event') diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index db7643347f..3bda7f31ad 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -331,20 +331,11 @@ static void event_free(sd_event *e) { if (e->default_event_ptr) *(e->default_event_ptr) = NULL; - if (e->epoll_fd >= 0) - close_nointr_nofail(e->epoll_fd); - - if (e->signal_fd >= 0) - close_nointr_nofail(e->signal_fd); - - if (e->realtime_fd >= 0) - close_nointr_nofail(e->realtime_fd); - - if (e->monotonic_fd >= 0) - close_nointr_nofail(e->monotonic_fd); - - if (e->watchdog_fd >= 0) - close_nointr_nofail(e->watchdog_fd); + safe_close(e->epoll_fd); + safe_close(e->signal_fd); + safe_close(e->realtime_fd); + safe_close(e->monotonic_fd); + safe_close(e->watchdog_fd); prioq_free(e->pending); prioq_free(e->prepare); @@ -673,7 +664,7 @@ static int event_setup_timer_fd( r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev); if (r < 0) { - close_nointr_nofail(fd); + safe_close(fd); return -errno; } @@ -809,9 +800,7 @@ static int event_update_signal_fd(sd_event *e) { r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev); if (r < 0) { - close_nointr_nofail(e->signal_fd); - e->signal_fd = -1; - + e->signal_fd = safe_close(e->signal_fd); return -errno; } @@ -2272,8 +2261,7 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) { } else { if (e->watchdog_fd >= 0) { epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL); - close_nointr_nofail(e->watchdog_fd); - e->watchdog_fd = -1; + e->watchdog_fd = safe_close(e->watchdog_fd); } } @@ -2281,8 +2269,7 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) { return e->watchdog; fail: - close_nointr_nofail(e->watchdog_fd); - e->watchdog_fd = -1; + e->watchdog_fd = safe_close(e->watchdog_fd); return r; } -- cgit v1.2.3-54-g00ecf