发送文件描述符在UNIX域套接字,并选择() [英] Sending file descriptor over UNIX domain socket, and select()
问题描述
我使用的是UNIX域套接字一个文件描述符传输到另一个进程。这工作得很好,但是当我第一次尝试,看看插座是可写的使用select()时,sendmsg()调用失败,出现错误的文件描述错误。
I'm using a UNIX domain socket to transfer a file descriptor to another process. This works fine, but when I first try to see if socket is writeable using select(), the sendmsg() call fails with a Bad File Descriptor error.
该SENDMSG()函数做工精细与选择()如果我不加文件描述符信息的指向msghdr结构组合,使冲突似乎是选择()和传送文件描述符之间。
The sendmsg() function does work fine in combination with select() if I don't add the file descriptor info to the msghdr struct, so the conflict seems to be between select() and transferring file descriptors.
我找不到在手册页的select(),recvmsg(),或任何其他有这方面的信息。由于这需要成为其手中了文件描述符到多个进程的服务器上,我还是希望能够使用选择()。
I couldn't find any info on this in the man pages for select(), recvmsg(), or any other. Since this needs to become a server which hands out file descriptors to multiple processes, I'd still like to be able to use select().
有什么我可以做,使这项工作,或有没有人知道的替代解决方案?
Is there anything I can do to make this work, or does anyone know of alternative solutions?
平台是Ubuntu的10.4。
Platform is Ubuntu 10.4.
这是code,它初始化结构:
This is the code that initializes the structures:
struct cmsghdr_fd : public cmsghdr
{
int fd;
};
int sendfd(int sock, int fd)
{
struct msghdr hdr;
struct iovec data;
struct cmsghdr_fd msgdata;
char dummy = '*';
data.iov_base = &dummy;
data.iov_len = sizeof(dummy);
hdr.msg_name = NULL;
hdr.msg_namelen = 0;
hdr.msg_iov = &data;
hdr.msg_iovlen = 1;
hdr.msg_flags = 0;
hdr.msg_control = &msgdata;
hdr.msg_controllen = sizeof(msgdata);
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
cmsg->cmsg_len = hdr.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int*)CMSG_DATA(cmsg) = fd;
int n = sendmsg(sock, &hdr, 0);
if(n == -1)
printf("sendmsg() failed: %s (socket fd = %d)\n", strerror(errno), sock);
return n;
}
同样,这个工作,只要我不叫选择()首先检查插座是否准备好写。
Again, this works, as long as I don't call select() first to check whether the socket is ready for writing.
推荐答案
我试过sendfd code。在的此页面,这是好心由号提供,并即使它只是略有不同,它的工作原理,甚至当我在组合中选择使用()。这是code样子现在:
I tried the sendfd code at this page, which was kindly provided by nos, and even though it's only slightly different, it works even when I use it in combination with select(). This is what the code looks like now:
int sendfd(int sock, int fd)
{
struct msghdr hdr;
struct iovec data;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
char dummy = '*';
data.iov_base = &dummy;
data.iov_len = sizeof(dummy);
memset(&hdr, 0, sizeof(hdr));
hdr.msg_name = NULL;
hdr.msg_namelen = 0;
hdr.msg_iov = &data;
hdr.msg_iovlen = 1;
hdr.msg_flags = 0;
hdr.msg_control = cmsgbuf;
hdr.msg_controllen = CMSG_LEN(sizeof(int));
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int*)CMSG_DATA(cmsg) = fd;
int n = sendmsg(sock, &hdr, 0);
if(n == -1)
printf("sendmsg() failed: %s (socket fd = %d)\n", strerror(errno), sock);
return n;
}
这篇关于发送文件描述符在UNIX域套接字,并选择()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!