[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain so
From: |
Eric Blake |
Subject: |
Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets |
Date: |
Thu, 03 Mar 2011 14:33:21 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.7 |
On 03/01/2011 07:04 AM, Bastien ROUCARIES wrote:
> Recvfd and sendfd can be used to pass an open file descriptor over a Unix
> domain socket from one process to another.
Have you considered porting this to Windows yet? I like the idea of
this module (especially since I want to use it in libvirt), but it's not
ready for prime-time yet.
> +#ifdef HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44
> + struct cmsghdr *cmsg;
> + char buf[CMSG_SPACE (sizeof (fd))];
> +
> + msg.msg_control = buf;
> + msg.msg_controllen = sizeof (buf);
> + cmsg = CMSG_FIRSTHDR (&msg);
> + cmsg->cmsg_level = SOL_SOCKET;
> + cmsg->cmsg_type = SCM_RIGHTS;
> + cmsg->cmsg_len = CMSG_LEN (sizeof (int));
> + /* Initialize the payload: */
> + (*(int *) CMSG_DATA (cmsg)) = fd;
You need to use memcpy here, as CMSG_DATA does not have type alignment
guarantees, and your type-punning may result in SIGBUS on some
architectures.
> +#elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43
This #define name is awkward to read because it is platform-based rather
than feature based. Why not use AC_CHECK_MEMBER's paradigm of
HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
> +recvfd (int sock)
> +
> + if (recvmsg (sock, &msg, 0) < 0)
> + return -1;
> +
> + cmsg = CMSG_FIRSTHDR (&msg);
> + /* be paranoiac */
> + if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int))
> + || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
> + {
> + /* fake errno: at end the file is not available */
> + errno = EACCES;
No TABS, please.
> + return -1;
> + }
> +
> + fd = *(int *) CMSG_DATA (cmsg);
Again, memcpy, not type-punning.
I just created http://sourceware.org/bugzilla/show_bug.cgi?id=12539 - it
would be nice if the kernel and glibc would give us a way to atomically
set the FD_CLOEXEC flag on fd's created by recvfd. But even without
atomic support from the kernel, it would be nice for gnulib to provide
provide something like recvfd2(int sock, int flags), where flags can be
O_CLOEXEC (or maybe SOCK_CLOEXEC), as a way to set it as soon as
possible after receiving the fd, in anticipation of the kernel catching
up and providing such an interface.
> +Maintainer:
> +Bastien ROUCARI�S
Are you consistently using UTF-8 in your files? This came through email
as something rather butchered.
--
Eric Blake address@hidden +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- [V3] Passfd, Bastien ROUCARIES, 2011/03/01
- [PATCH 3/3] Add test for passfd, Bastien ROUCARIES, 2011/03/01
- [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets, Bastien ROUCARIES, 2011/03/01
- Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets,
Eric Blake <=
- Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets, Paolo Bonzini, 2011/03/04
- Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets, Bastien ROUCARIES, 2011/03/06
- Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets, Bastien ROUCARIES, 2011/03/06
- Re: [PATCH 2/3] sendfd, recvf pass file descriptors along Unix domain sockets, Eric Blake, 2011/03/07
- [PATCH 1/3] Add AF_UNIX test of socket familly, Bastien ROUCARIES, 2011/03/01