]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
hurd: Implement MSG_CMSG_CLOEXEC
authorSergey Bugaev <bugaevc@gmail.com>
Sun, 23 Apr 2023 16:05:46 +0000 (19:05 +0300)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Mon, 24 Apr 2023 21:09:50 +0000 (23:09 +0200)
This is a new flag that can be passed to recvmsg () to make it
atomically set the CLOEXEC flag on all the file descriptors received
using the SCM_RIGHTS mechanism. This is useful for all the same reasons
that the other XXX_CLOEXEC flags are useful: namely, it provides
atomicity with respect to another thread of the same process calling
(fork and then) exec at the same time.

This flag is already supported on Linux and FreeBSD. The flag's value,
0x40000, is choosen to match FreeBSD's.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
Message-Id: <20230423160548.126576-2-bugaevc@gmail.com>

sysdeps/mach/hurd/bits/socket.h
sysdeps/mach/hurd/recvmsg.c

index 2d78a9167c9171dc6e0d50a31e27e7d96af40236..c2392bed80d3addca0798bb514d0cde2a2118e56 100644 (file)
@@ -197,8 +197,11 @@ enum
 #define MSG_WAITALL MSG_WAITALL
     MSG_DONTWAIT       = 0x80, /* This message should be nonblocking.  */
 #define MSG_DONTWAIT MSG_DONTWAIT
-    MSG_NOSIGNAL       = 0x0400        /* Do not generate SIGPIPE on EPIPE.  */
+    MSG_NOSIGNAL       = 0x0400,       /* Do not generate SIGPIPE on EPIPE.  */
 #define MSG_NOSIGNAL MSG_NOSIGNAL
+    MSG_CMSG_CLOEXEC   = 0x40000       /* Atomically set close-on-exec flag
+                                          for file descriptors in SCM_RIGHTS.  */
+#define MSG_CMSG_CLOEXEC MSG_CMSG_CLOEXEC
   };
 
 
index 90fd2698c028b815f27c7c112a7e42f5942037a0..e06b0fe3ba3a3e64cd92d3644ff2cba8377c2859 100644 (file)
@@ -197,11 +197,13 @@ __libc_recvmsg (int fd, struct msghdr *message, int flags)
 
        for (j = 0; j < nfds; j++)
          {
+           int fd_flags = (flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
            err = reauthenticate (ports[i], &newports[newfds]);
            if (err)
              goto cleanup;
            fds[j] = opened_fds[newfds] = _hurd_intern_fd (newports[newfds],
-                                                          fds[j], 0);
+                                                          fds[j] | fd_flags,
+                                                          0);
            if (fds[j] == -1)
              {
                err = errno;