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/socket-proxy/socket-proxyd.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/socket-proxy/socket-proxyd.c')
-rw-r--r-- | src/socket-proxy/socket-proxyd.c | 16 |
1 files changed, 6 insertions, 10 deletions
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); } } |