1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include "me.h"
int CONTROL_LEN = CMSG_LEN(sizeof(int));
void send_fd(int fd,int fd_to_send) { struct iovec iov[1]; struct msghdr msg; char buf[2];
iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = 1;
struct cmsghdr cm; cm.cmsg_len = CONTROL_LEN; cm.cmsg_level = SOL_SOCKET; cm.cmsg_type = SCM_RIGHTS; *(int*)CMSG_DATA(&cm) = fd_to_send; msg.msg_control = &cm; msg.msg_controllen = CONTROL_LEN;
sendmsg(fd,&msg,0); }
int recv_fd(int fd) { struct iovec iov[1]; struct msghdr msg; char buf[2];
iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = 1;
struct cmsghdr cm; msg.msg_control = &cm; msg.msg_controllen = CONTROL_LEN; printf("[recv_fd CONTROL_LEN] = %d\n",CONTROL_LEN);
recvmsg(fd,&msg,0); int fd_to_read = *(int*)CMSG_DATA(&cm); return fd_to_read; }
int main() { printf("[parent pid]: %d\n",getpid()); int pipefd[2]; int fd_to_pass = 0; int ret = socketpair(AF_UNIX,SOCK_DGRAM,0,pipefd); assert(ret != -1);
pid_t pid = fork(); assert(pid >= 0); if (pid == 0) { printf("[child pid]: %d\n",getpid()); close(pipefd[0]); fd_to_pass = open("1.in",O_RDWR,0666); printf("[child process]: fd_to_pass = %d\n",fd_to_pass); send_fd(pipefd[1],(fd_to_pass > 0) ? fd_to_pass : 0); sleep(400); close(fd_to_pass); exit(0); } close(pipefd[1]); fd_to_pass = recv_fd(pipefd[0]); char buf[1024]; memset(buf,'\0',1024); read(fd_to_pass,buf,1024); printf("[parent process]: fd_to_pass = %d\n",fd_to_pass); printf("I got fd %d and data %s \n",fd_to_pass,buf); sleep(500); close(fd_to_pass); exit(0); }
|