Featured post
c - Sending file descriptor over UNIX domain socket, and select() -
i'm using unix domain socket transfer file descriptor process. works fine, when first try see if socket writeable using select(), sendmsg() call fails bad file descriptor error.
the sendmsg() function work fine in combination select() if don't add file descriptor info msghdr struct, conflict seems between select() , transferring file descriptors.
i couldn't find info on in man pages select(), recvmsg(), or other. since needs become server hands out file descriptors multiple processes, i'd still able use select().
is there can make work, or know of alternative solutions?
platform ubuntu 10.4.
this code initializes 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, works, long don't call select() first check whether socket ready writing.
i tried sendfd code @ this page, kindly provided nos, , though it's different, works when use in combination select(). code looks 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; }
- Get link
- X
- Other Apps
Comments
Post a Comment