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/socket-proxy/socket-proxyd.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/socket-proxy/socket-proxyd.c') diff --git a/src/socket-proxy/socket-proxyd.c b/src/socket-proxy/socket-proxyd.c index a42e5ae279..c172bebe8c 100644 --- a/src/socket-proxy/socket-proxyd.c +++ b/src/socket-proxy/socket-proxyd.c @@ -76,10 +76,8 @@ static void connection_free(Connection *c) { sd_event_source_unref(c->server_event_source); sd_event_source_unref(c->client_event_source); - if (c->server_fd >= 0) - close_nointr_nofail(c->server_fd); - if (c->client_fd >= 0) - close_nointr_nofail(c->client_fd); + safe_close(c->server_fd); + safe_close(c->client_fd); close_pipe(c->server_to_client_buffer); close_pipe(c->client_to_server_buffer); @@ -224,8 +222,7 @@ static int connection_shovel( shoveled = true; } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) { *from_source = sd_event_source_unref(*from_source); - close_nointr_nofail(*from); - *from = -1; + *from = safe_close(*from); } else if (errno != EAGAIN && errno != EINTR) { log_error("Failed to splice: %m"); return -errno; @@ -239,8 +236,7 @@ static int connection_shovel( shoveled = true; } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) { *to_source = sd_event_source_unref(*to_source); - close_nointr_nofail(*to); - *to = -1; + *to = safe_close(*to); } else if (errno != EAGAIN && errno != EINTR) { log_error("Failed to splice: %m"); return -errno; @@ -396,7 +392,7 @@ static int add_connection_socket(Context *context, sd_event *event, int fd) { if (set_size(context->connections) > CONNECTIONS_MAX) { log_warning("Hit connection limit, refusing connection."); - close_nointr_nofail(fd); + safe_close(fd); return 0; } @@ -482,7 +478,7 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat r = add_connection_socket(context, sd_event_source_get_event(s), nfd); if (r < 0) { log_error("Failed to accept connection, ignoring: %s", strerror(-r)); - close_nointr_nofail(fd); + safe_close(fd); } } -- cgit v1.2.3-54-g00ecf