From: Mike Yuan Date: Fri, 19 Dec 2025 22:58:56 +0000 (+0100) Subject: core/socket: modernize listen/accept_in_cgroup X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65d173a02d4eeca9ad28c8d7d0c4f6a47d465503;p=thirdparty%2Fsystemd.git core/socket: modernize listen/accept_in_cgroup --- diff --git a/src/core/socket.c b/src/core/socket.c index b2b21a8a6d4..86e76745284 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1557,9 +1557,7 @@ static int socket_address_listen_in_cgroup( const SocketAddress *address, const char *label) { - _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; - _cleanup_close_pair_ int pair[2] = EBADF_PAIR; - int fd, r; + int r; assert(s); assert(address); @@ -1571,11 +1569,11 @@ static int socket_address_listen_in_cgroup( if (!fork_needed(address, s)) { /* Shortcut things... */ - fd = socket_address_listen_do(s, address, label); - if (fd < 0) - return log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m"); + r = socket_address_listen_do(s, address, label); + if (r < 0) + return log_address_error_errno(UNIT(s), address, r, "Failed to create listening socket (%s): %m"); - return fd; + return r; } r = unit_setup_exec_runtime(UNIT(s)); @@ -1605,6 +1603,10 @@ static int socket_address_listen_in_cgroup( } } + _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; + _cleanup_close_pair_ int pair[2] = EBADF_PAIR; + _cleanup_close_ int fd = -EBADF; + if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); @@ -1654,15 +1656,13 @@ static int socket_address_listen_in_cgroup( /* We synchronously wait for the helper, as it shouldn't be slow */ r = pidref_wait_for_terminate_and_check("(sd-listen)", &pid, WAIT_LOG_ABNORMAL); - if (r < 0) { - safe_close(fd); + if (r < 0) return r; - } if (fd < 0) return log_address_error_errno(UNIT(s), address, fd, "Failed to receive listening socket (%s): %m"); - return fd; + return TAKE_FD(fd); } static int socket_open_fds(Socket *orig_s) { @@ -3189,7 +3189,7 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) { /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS * this is a spurious accept(), let's convert that back to EAGAIN here. */ - if (cfd == -EIO) + if (cfd == -EIO && r == EXIT_SUCCESS) return -EAGAIN; if (cfd < 0) return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m");