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/core/automount.c | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'src/core/automount.c') diff --git a/src/core/automount.c b/src/core/automount.c index 889b20e25d..77d80b2789 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -90,9 +90,7 @@ static void unmount_autofs(Automount *a) { automount_send_ready(a, -EHOSTDOWN); a->pipe_event_source = sd_event_source_unref(a->pipe_event_source); - - close_nointr_nofail(a->pipe_fd); - a->pipe_fd = -1; + a->pipe_fd = safe_close(a->pipe_fd); /* If we reload/reexecute things we keep the mount point * around */ @@ -310,8 +308,7 @@ static int open_dev_autofs(Manager *m) { init_autofs_dev_ioctl(¶m); if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) { - close_nointr_nofail(m->dev_autofs_fd); - m->dev_autofs_fd = -1; + m->dev_autofs_fd = safe_close(m->dev_autofs_fd); return -errno; } @@ -411,8 +408,9 @@ static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, in } int automount_send_ready(Automount *a, int status) { - int ioctl_fd, r; + _cleanup_close_ int ioctl_fd = -1; unsigned token; + int r; assert(a); assert(status <= 0); @@ -421,10 +419,8 @@ int automount_send_ready(Automount *a, int status) { return 0; ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id); - if (ioctl_fd < 0) { - r = ioctl_fd; - goto fail; - } + if (ioctl_fd < 0) + return ioctl_fd; if (status) log_debug_unit(UNIT(a)->id, "Sending failure: %s", strerror(-status)); @@ -450,18 +446,15 @@ int automount_send_ready(Automount *a, int status) { r = k; } -fail: - if (ioctl_fd >= 0) - close_nointr_nofail(ioctl_fd); - return r; } static void automount_enter_waiting(Automount *a) { + _cleanup_close_ int ioctl_fd = -1; int p[2] = { -1, -1 }; char name[32], options[128]; bool mounted = false; - int r, ioctl_fd = -1, dev_autofs_fd; + int r, dev_autofs_fd; struct stat st; assert(a); @@ -500,8 +493,7 @@ static void automount_enter_waiting(Automount *a) { mounted = true; - close_nointr_nofail(p[1]); - p[1] = -1; + p[1] = safe_close(p[1]); if (stat(a->where, &st) < 0) { r = -errno; @@ -528,9 +520,6 @@ static void automount_enter_waiting(Automount *a) { * the direct mount will not receive events from the * kernel. */ - close_nointr_nofail(ioctl_fd); - ioctl_fd = -1; - r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, p[0], EPOLLIN, automount_dispatch_io, a); if (r < 0) goto fail; @@ -543,10 +532,7 @@ static void automount_enter_waiting(Automount *a) { return; fail: - assert_se(close_pipe(p) == 0); - - if (ioctl_fd >= 0) - close_nointr_nofail(ioctl_fd); + close_pipe(p); if (mounted) repeat_unmount(a->where); @@ -713,9 +699,7 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) log_debug_unit(u->id, "Failed to parse pipe-fd value %s", value); else { - if (a->pipe_fd >= 0) - close_nointr_nofail(a->pipe_fd); - + safe_close(a->pipe_fd); a->pipe_fd = fdset_remove(fds, fd); } } else @@ -809,8 +793,7 @@ fail: static void automount_shutdown(Manager *m) { assert(m); - if (m->dev_autofs_fd >= 0) - close_nointr_nofail(m->dev_autofs_fd); + m->dev_autofs_fd = safe_close(m->dev_autofs_fd); } static void automount_reset_failed(Unit *u) { -- cgit v1.2.3-54-g00ecf