From 5cf09553c30f94e874727fc2200dbce8571d40da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 2 Sep 2020 18:17:14 +0200 Subject: [PATCH] core/socket: use _cleanup_ to close the connection fd Removing the gotos would lead to a lot of duplicated code, so I left them as they were. --- src/core/socket.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/core/socket.c b/src/core/socket.c index de4c9ab3f67..20c09b2a3f9 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -2307,13 +2307,14 @@ static void flush_ports(Socket *s) { } } -static void socket_enter_running(Socket *s, int cfd) { +static void socket_enter_running(Socket *s, int cfd_in) { + /* Note that this call takes possession of the connection fd passed. It either has to assign it + * somewhere or close it. */ + _cleanup_close_ int cfd = cfd_in; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int r; - /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or - * close it. */ - assert(s); /* We don't take connections anymore if we are supposed to shut down anyway */ @@ -2323,9 +2324,8 @@ static void socket_enter_running(Socket *s, int cfd) { if (cfd >= 0) goto refuse; - else - flush_ports(s); + flush_ports(s); return; } @@ -2375,7 +2375,7 @@ static void socket_enter_running(Socket *s, int cfd) { if (s->max_connections_per_source > 0) { r = socket_acquire_peer(s, cfd, &p); if (ERRNO_IS_DISCONNECT(r)) - goto notconn; + return; if (r < 0) /* We didn't have enough resources to acquire peer information, let's fail. */ goto fail; if (r > 0 && p->n_ref > s->max_connections_per_source) { @@ -2392,7 +2392,7 @@ static void socket_enter_running(Socket *s, int cfd) { r = socket_load_service_unit(s, cfd, &service); if (ERRNO_IS_DISCONNECT(r)) - goto notconn; + return; if (r < 0) goto fail; @@ -2405,7 +2405,7 @@ static void socket_enter_running(Socket *s, int cfd) { r = service_set_socket_fd(SERVICE(service), cfd, s, s->selinux_context_from_net); if (ERRNO_IS_DISCONNECT(r)) - goto notconn; + return; if (r < 0) goto fail; @@ -2426,12 +2426,11 @@ static void socket_enter_running(Socket *s, int cfd) { unit_add_to_dbus_queue(UNIT(s)); } + TAKE_FD(cfd); return; refuse: s->n_refused++; -notconn: - safe_close(cfd); return; fail: @@ -2444,7 +2443,6 @@ fail: bus_error_message(&error, r)); socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); - safe_close(cfd); } static void socket_run_next(Socket *s) { -- 2.47.3