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/busname.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'src/core/busname.c') diff --git a/src/core/busname.c b/src/core/busname.c index bd7d02d73b..9d7a796538 100644 --- a/src/core/busname.c +++ b/src/core/busname.c @@ -56,11 +56,7 @@ static void busname_done(Unit *u) { unit_ref_unset(&n->service); n->event_source = sd_event_source_unref(n->event_source); - - if (n->starter_fd >= 0) { - close_nointr_nofail(n->starter_fd); - n->starter_fd = -1; - } + n->starter_fd = safe_close(n->starter_fd); } static int busname_add_default_default_dependencies(BusName *n) { @@ -122,8 +118,6 @@ static int busname_add_extras(BusName *n) { return 0; } - - static int busname_verify(BusName *n) { char *e; @@ -202,8 +196,7 @@ static void busname_close_fd(BusName *n) { if (n->starter_fd <= 0) return; - close_nointr_nofail(n->starter_fd); - n->starter_fd = -1; + n->starter_fd = safe_close(n->starter_fd); } static int busname_watch_fd(BusName *n) { @@ -454,8 +447,7 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value, if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) log_debug_unit(u->id, "Failed to parse starter fd value %s", value); else { - if (n->starter_fd >= 0) - close_nointr_nofail(n->starter_fd); + safe_close(n->starter_fd); n->starter_fd = fdset_remove(fds, fd); } } else -- cgit v1.2.3-54-g00ecf