Commit | Line | Data |
---|---|---|
759ef658 | 1 | /* -*-c-*- |
2 | * | |
759ef658 | 3 | * File descriptor passing |
4 | * | |
5 | * (c) 2003 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
759ef658 | 9 | * |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
d4efbcd9 | 16 | * |
759ef658 | 17 | * mLib is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
d4efbcd9 | 21 | * |
759ef658 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with mLib; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
759ef658 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
b3a0ac5e MW |
30 | #include "config.h" |
31 | ||
759ef658 | 32 | #include <errno.h> |
33 | #include <stdio.h> | |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | ||
37 | #include <sys/types.h> | |
38 | #include <sys/time.h> | |
39 | #include <unistd.h> | |
40 | #include <sys/socket.h> | |
41 | #include <sys/uio.h> | |
42 | #include <sys/un.h> | |
43 | ||
44 | #include "fdpass.h" | |
45 | ||
46 | /*----- Main code ---------------------------------------------------------*/ | |
47 | ||
48 | /* --- @fdpass_send@ --- * | |
49 | * | |
50 | * Arguments: @int sock@ = socket to send over | |
51 | * @int fd@ = file descriptor to send | |
52 | * @const void *p@ = pointer to data to send | |
53 | * @size_t sz@ = size of buffer to send | |
54 | * | |
55 | * Returns: On error, @-1@, otherwise number of bytes transferred from | |
56 | * @p@. | |
57 | * | |
58 | * Use: Sends a copy of file descriptor @fd@ to the other end of | |
59 | * @sock@. | |
60 | */ | |
61 | ||
62 | ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz) | |
63 | { | |
64 | struct iovec iov; | |
65 | struct msghdr msg; | |
66 | #ifndef HAVE_MSG_ACCRIGHTS | |
67 | char buf[CMSG_SPACE(sizeof(fd))]; | |
68 | struct cmsghdr *cmsg; | |
69 | #endif | |
70 | ||
71 | iov.iov_base = (/*unconst*/ void *)p; | |
72 | iov.iov_len = sz; | |
73 | msg.msg_name = 0; | |
74 | msg.msg_namelen = 0; | |
75 | msg.msg_iov = &iov; | |
76 | msg.msg_iovlen = 1; | |
759ef658 | 77 | #ifdef HAVE_MSG_ACCRIGHTS |
78 | msg.msg_accrights = &fd; | |
79 | msg.msg_accrightslen = sizeof(fd); | |
d4efbcd9 | 80 | #else |
1a6043f9 | 81 | msg.msg_flags = 0; |
759ef658 | 82 | msg.msg_control = buf; |
83 | msg.msg_controllen = sizeof(buf); | |
84 | cmsg = CMSG_FIRSTHDR(&msg); | |
85 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); | |
86 | cmsg->cmsg_level = SOL_SOCKET; | |
87 | cmsg->cmsg_type = SCM_RIGHTS; | |
88 | memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); | |
89 | #endif | |
90 | return (sendmsg(sock, &msg, 0)); | |
91 | } | |
92 | ||
93 | /* --- @fdpass_recv@ --- * | |
94 | * | |
95 | * Arguments: @int sock@ = socket to send over | |
96 | * @int *fd@ = where to put received descriptor | |
97 | * @void *p@ = pointer to where to put data | |
98 | * @size_t sz@ = size of buffer | |
99 | * | |
100 | * Returns: On error, @-1@, otherwise number of bytes transferred. | |
101 | * | |
102 | * Use: Receives a file descriptor. If the call succeeds, and there | |
103 | * was a file descriptor, then @fd@ won't be @-1@ on exit; | |
104 | * otherwise it will. At most one descriptor will be collected. | |
105 | */ | |
106 | ||
dff095f3 MW |
107 | /* Qemu 2.8.1 clobbers 12 bytes beyond the end of the control-message |
108 | * buffer. This is fixed in 2.12, but I'll bodge it for the sake of Debian | |
109 | * stable. | |
110 | */ | |
111 | #define QEMU_SCRATCHSZ 16 | |
112 | ||
759ef658 | 113 | ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz) |
114 | { | |
115 | struct iovec iov; | |
116 | struct msghdr msg; | |
117 | ssize_t rc; | |
118 | #ifndef HAVE_MSG_ACCRIGHTS | |
dff095f3 | 119 | char buf[CMSG_SPACE(sizeof(fd)) + QEMU_SCRATCHSZ]; |
759ef658 | 120 | struct cmsghdr *cmsg; |
121 | int fdtmp; | |
122 | #endif | |
123 | ||
124 | *fd = -1; | |
125 | iov.iov_base = p; | |
126 | iov.iov_len = sz; | |
127 | msg.msg_name = 0; | |
128 | msg.msg_namelen = 0; | |
129 | msg.msg_iov = &iov; | |
130 | msg.msg_iovlen = 1; | |
759ef658 | 131 | #ifdef HAVE_MSG_ACCRIGHTS |
132 | msg.msg_accrights = fd; | |
133 | msg.msg_accrightslen = sizeof(*fd); | |
134 | #else | |
1a6043f9 | 135 | msg.msg_flags = 0; |
759ef658 | 136 | msg.msg_control = buf; |
dff095f3 | 137 | msg.msg_controllen = sizeof(buf) - QEMU_SCRATCHSZ; |
759ef658 | 138 | #endif |
139 | if ((rc = recvmsg(sock, &msg, 0)) < 0) | |
140 | return (rc); | |
141 | #ifdef HAVE_MSG_ACCRIGHTS | |
142 | if (msg.msg_accrightslen < sizeof(*fd)) | |
143 | *fd = -1; | |
144 | #else | |
145 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
146 | if (cmsg->cmsg_level == SOL_SOCKET && | |
147 | cmsg->cmsg_type == SCM_RIGHTS && | |
3c9c17a1 | 148 | cmsg->cmsg_len >= CMSG_LEN(sizeof(*fd))) { |
759ef658 | 149 | memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp)); |
150 | if (*fd == -1) | |
151 | *fd = fdtmp; | |
152 | else | |
153 | close(fdtmp); | |
154 | } | |
155 | } | |
156 | #endif | |
157 | return (rc); | |
158 | } | |
159 | ||
dff095f3 MW |
160 | #undef QEMU_SCRATCHSZ |
161 | ||
759ef658 | 162 | /*----- That's all, folks -------------------------------------------------*/ |