]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/socket: modernize listen/accept_in_cgroup
authorMike Yuan <me@yhndnzj.com>
Fri, 19 Dec 2025 22:58:56 +0000 (23:58 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 20 Dec 2025 21:56:33 +0000 (22:56 +0100)
src/core/socket.c

index b2b21a8a6d4d4431c4d56f25ec80012615e68bfc..86e767452843a8b15f75b3d2288c183dbabd9a7f 100644 (file)
@@ -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");