通过 UNIX 域套接字发送文件描述符和 select() [英] 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.
如果我不将文件描述符信息添加到 msghdr 结构中,则 sendmsg() 函数与 select() 结合使用确实可以正常工作,因此冲突似乎在 select() 和传输文件描述符之间.
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() 或任何其他的手册页中找不到任何关于此的信息.由于这需要成为将文件描述符分发给多个进程的服务器,我仍然希望能够使用 select().
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.
这是初始化结构的代码:
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)
", strerror(errno), sock);
return n;
}
同样,只要我不先调用 select() 来检查套接字是否准备好写入,这也有效.
Again, this works, as long as I don't call select() first to check whether the socket is ready for writing.
推荐答案
我在 此页面,由 nos 友情提供,即使它只是略有不同,即使我将它与 select() 结合使用,它也能工作.这就是代码现在的样子:
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)
", strerror(errno), sock);
return n;
}
这篇关于通过 UNIX 域套接字发送文件描述符和 select()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!