[英]Sending file descriptor over UNIX domain socket, and select()
I'm using a UNIX domain socket to transfer a file descriptor to another process. 我正在使用UNIX域套接字将文件描述符传输到另一个进程。 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. 这工作正常,但是当我第一次尝试使用select()查看套接字是否可写时,sendmsg()调用失败并显示错误的文件描述符错误。
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. 如果我不将文件描述符信息添加到msghdr结构中,sendmsg()函数与select()一起工作正常,因此冲突似乎在select()和传输文件描述符之间。
I couldn't find any info on this in the man pages for select(), recvmsg(), or any other. 我在select(),recvmsg()或任何其他的手册页中找不到任何关于此的信息。 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(). 由于这需要成为将文件描述符分发给多个进程的服务器,我仍然希望能够使用select()。
Is there anything I can do to make this work, or does anyone know of alternative solutions? 有什么我可以做的工作,或者有人知道替代解决方案吗?
Platform is Ubuntu 10.4. 平台是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)\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. 再次,这是有效的,只要我不首先调用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(). 我在这个页面上尝试了sendfd代码,这是由nos友情提供的,即使它只是略有不同,它甚至在我与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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.