| 1 |
|
| /* code lifted from Stevens' APUE */ |
| 2 |
|
| |
| 3 |
|
| #include <errno.h> /* for definition of errno */ |
| 4 |
|
| #include <stdarg.h> /* ANSI C header file */ |
| 5 |
|
| #include <sys/types.h> |
| 6 |
|
| #include <sys/socket.h> |
| 7 |
|
| #include <sys/uio.h> |
| 8 |
|
| #include <string.h> |
| 9 |
|
| #include <unistd.h> |
| 10 |
|
| #include "pass_fd.h" |
| 11 |
|
| |
| 12 |
|
| #if VARIANT_SVR4 |
| 13 |
|
| |
| 14 |
|
| int |
| 15 |
|
| s_pipe(int fd[2]) |
| 16 |
|
| { |
| 17 |
|
| return( pipe(fd) ); |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
|
| #elif defined(VARIANT_43BSD) || defined(VARIANT_44BSD) |
| 21 |
|
| |
| 22 |
|
| int |
| 23 |
0 |
| s_pipe(int fd[2]) |
| 24 |
|
| { |
| 25 |
0 |
| return( socketpair(AF_UNIX, SOCK_STREAM, 0, fd) ); |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
|
| #else |
| 29 |
|
| |
| 30 |
|
| #error "Couldn't guess variant" |
| 31 |
|
| |
| 32 |
|
| #endif |
| 33 |
|
| |
| 34 |
|
| |
| 35 |
|
| #if VARIANT_43BSD |
| 36 |
|
| |
| 37 |
|
| int |
| 38 |
|
| send_fd(int clifd, int fd) |
| 39 |
|
| { |
| 40 |
|
| struct iovec iov[1]; |
| 41 |
|
| struct msghdr msg; |
| 42 |
|
| char buf[2]; |
| 43 |
|
| |
| 44 |
|
| iov[0].iov_base = buf; |
| 45 |
|
| iov[0].iov_len = 2; |
| 46 |
|
| msg.msg_iov = iov; |
| 47 |
|
| msg.msg_iovlen = 1; |
| 48 |
|
| msg.msg_name = NULL; |
| 49 |
|
| msg.msg_namelen = 0; |
| 50 |
|
| |
| 51 |
|
| if (fd < 0) { |
| 52 |
|
| msg.msg_accrights = NULL; |
| 53 |
|
| msg.msg_accrightslen = 0; |
| 54 |
|
| buf[1] = -fd; |
| 55 |
|
| if (buf[1] == 0) |
| 56 |
|
| buf[1] = 1; |
| 57 |
|
| } |
| 58 |
|
| else { |
| 59 |
|
| msg.msg_accrights = (caddr_t) &fd; |
| 60 |
|
| msg.msg_accrightslen = sizeof(int); |
| 61 |
|
| buf[1] = 0; |
| 62 |
|
| } |
| 63 |
|
| buf[0] = 0; |
| 64 |
|
| |
| 65 |
|
| if (sendmsg(clifd, &msg, 0) != 2) |
| 66 |
|
| return(-1); |
| 67 |
|
| |
| 68 |
|
| return(0); |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| int |
| 72 |
|
| recv_fd(int servfd) |
| 73 |
|
| { |
| 74 |
|
| int newfd, nread, status; |
| 75 |
|
| char *ptr, buf[2]; |
| 76 |
|
| struct iovec iov[1]; |
| 77 |
|
| struct msghdr msg; |
| 78 |
|
| |
| 79 |
|
| iov[0].iov_base = buf; |
| 80 |
|
| iov[0].iov_len = 2; |
| 81 |
|
| msg.msg_iov = iov; |
| 82 |
|
| msg.msg_iovlen = 1; |
| 83 |
|
| msg.msg_name = NULL; |
| 84 |
|
| msg.msg_namelen = 0; |
| 85 |
|
| msg.msg_accrights = (caddr_t) &newfd; |
| 86 |
|
| msg.msg_accrightslen = sizeof(int); |
| 87 |
|
| |
| 88 |
|
| if ( (nread = recvmsg(servfd, &msg, 0)) <= 0) |
| 89 |
|
| return(-1); |
| 90 |
|
| |
| 91 |
|
| return(newfd);/* descriptor, or -status */ |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| #else |
| 95 |
|
| |
| 96 |
|
| struct cmessage { |
| 97 |
|
| struct cmsghdr cmsg; |
| 98 |
|
| int fd; |
| 99 |
|
| }; |
| 100 |
|
| |
| 101 |
|
| int |
| 102 |
0 |
| send_fd(int over, int this) |
| 103 |
|
| { |
| 104 |
|
| struct iovec iov[1]; |
| 105 |
|
| struct msghdr msg; |
| 106 |
|
| struct cmessage cm; |
| 107 |
0 |
| char sendbuf[] = ""; |
| 108 |
|
| |
| 109 |
0 |
| iov[0].iov_base = (char *)&sendbuf; |
| 110 |
0 |
| iov[0].iov_len = sizeof(sendbuf); |
| 111 |
|
| |
| 112 |
0 |
| cm.cmsg.cmsg_type = SCM_RIGHTS; |
| 113 |
0 |
| cm.cmsg.cmsg_level = SOL_SOCKET; |
| 114 |
0 |
| cm.cmsg.cmsg_len = sizeof(struct cmessage); |
| 115 |
0 |
| cm.fd = this; |
| 116 |
|
| |
| 117 |
0 |
| msg.msg_iov = iov; |
| 118 |
0 |
| msg.msg_iovlen = 1; |
| 119 |
0 |
| msg.msg_name = NULL; |
| 120 |
0 |
| msg.msg_namelen = 0; |
| 121 |
0 |
| msg.msg_control = (caddr_t)&cm; |
| 122 |
0 |
| msg.msg_controllen = sizeof(struct cmessage); |
| 123 |
0 |
| msg.msg_flags = 0; |
| 124 |
|
| |
| 125 |
0 |
▶ | if (sendmsg(over, &msg, 0) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sendmsg(over, &msg, 0) < 0) |
0 |
0 |
0 |
|
| 126 |
0 |
| return -1; |
| 127 |
0 |
| return 0; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| int |
| 131 |
0 |
| recv_fd(int over) |
| 132 |
|
| { |
| 133 |
|
| struct iovec iov[1]; |
| 134 |
|
| struct msghdr msg; |
| 135 |
|
| struct cmessage cm; |
| 136 |
|
| ssize_t got; |
| 137 |
|
| char recbuf; |
| 138 |
|
| |
| 139 |
|
| /* in examples this was >1 but this causes too much to be read, |
| 140 |
|
| * causing sync issues */ |
| 141 |
|
| |
| 142 |
0 |
| iov[0].iov_base = &recbuf; |
| 143 |
0 |
| iov[0].iov_len = 1; |
| 144 |
|
| |
| 145 |
0 |
| bzero((char *)&cm, sizeof(cm)); |
| 146 |
0 |
| bzero((char *)&msg, sizeof(msg)); |
| 147 |
|
| |
| 148 |
0 |
| msg.msg_iov = iov; |
| 149 |
0 |
| msg.msg_iovlen = 1; |
| 150 |
0 |
| msg.msg_name = NULL; |
| 151 |
0 |
| msg.msg_namelen = 0; |
| 152 |
0 |
| msg.msg_control = (caddr_t)&cm; |
| 153 |
0 |
| msg.msg_controllen = sizeof(struct cmessage); |
| 154 |
0 |
| msg.msg_flags = 0; |
| 155 |
|
| |
| 156 |
0 |
▶ | if ((got = recvmsg(over, &msg, 0)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((got = recvmsg(over, &msg, 0)) < 0) |
0 |
0 |
0 |
|
| 157 |
0 |
| return -1; |
| 158 |
|
| |
| 159 |
0 |
▶ | if (cm.cmsg.cmsg_type != SCM_RIGHTS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cm.cmsg.cmsg_type != SCM_RIGHTS) |
0 |
0 |
0 |
|
| 160 |
0 |
| return -1; |
| 161 |
|
| |
| 162 |
0 |
| return cm.fd; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| #endif |