1 |
|
|
/* $OpenBSD: imsg.c,v 1.15 2017/04/11 09:57:19 reyk Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/queue.h> |
21 |
|
|
#include <sys/socket.h> |
22 |
|
|
#include <sys/uio.h> |
23 |
|
|
|
24 |
|
|
#include <errno.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <string.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
|
29 |
|
|
#include "imsg.h" |
30 |
|
|
|
31 |
|
|
int imsg_fd_overhead = 0; |
32 |
|
|
|
33 |
|
|
int imsg_get_fd(struct imsgbuf *); |
34 |
|
|
|
35 |
|
|
void |
36 |
|
|
imsg_init(struct imsgbuf *ibuf, int fd) |
37 |
|
|
{ |
38 |
|
2408 |
msgbuf_init(&ibuf->w); |
39 |
|
1204 |
memset(&ibuf->r, 0, sizeof(ibuf->r)); |
40 |
|
1204 |
ibuf->fd = fd; |
41 |
|
1204 |
ibuf->w.fd = fd; |
42 |
|
1204 |
ibuf->pid = getpid(); |
43 |
|
1204 |
TAILQ_INIT(&ibuf->fds); |
44 |
|
1204 |
} |
45 |
|
|
|
46 |
|
|
ssize_t |
47 |
|
|
imsg_read(struct imsgbuf *ibuf) |
48 |
|
|
{ |
49 |
|
2380 |
struct msghdr msg; |
50 |
|
|
struct cmsghdr *cmsg; |
51 |
|
1190 |
union { |
52 |
|
|
struct cmsghdr hdr; |
53 |
|
|
char buf[CMSG_SPACE(sizeof(int) * 1)]; |
54 |
|
|
} cmsgbuf; |
55 |
|
1190 |
struct iovec iov; |
56 |
|
|
ssize_t n = -1; |
57 |
|
|
int fd; |
58 |
|
|
struct imsg_fd *ifd; |
59 |
|
|
|
60 |
|
1190 |
memset(&msg, 0, sizeof(msg)); |
61 |
|
1190 |
memset(&cmsgbuf, 0, sizeof(cmsgbuf)); |
62 |
|
|
|
63 |
|
1190 |
iov.iov_base = ibuf->r.buf + ibuf->r.wpos; |
64 |
|
1190 |
iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos; |
65 |
|
1190 |
msg.msg_iov = &iov; |
66 |
|
1190 |
msg.msg_iovlen = 1; |
67 |
|
1190 |
msg.msg_control = &cmsgbuf.buf; |
68 |
|
1190 |
msg.msg_controllen = sizeof(cmsgbuf.buf); |
69 |
|
|
|
70 |
✗✓ |
1190 |
if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL) |
71 |
|
|
return (-1); |
72 |
|
|
|
73 |
|
|
again: |
74 |
✗✓ |
2380 |
if (getdtablecount() + imsg_fd_overhead + |
75 |
|
|
(int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int)) |
76 |
|
1190 |
>= getdtablesize()) { |
77 |
|
|
errno = EAGAIN; |
78 |
|
|
free(ifd); |
79 |
|
|
return (-1); |
80 |
|
|
} |
81 |
|
|
|
82 |
✗✓ |
1190 |
if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) { |
83 |
|
|
if (errno == EINTR) |
84 |
|
|
goto again; |
85 |
|
|
goto fail; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
1190 |
ibuf->r.wpos += n; |
89 |
|
|
|
90 |
✗✓ |
2380 |
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; |
91 |
|
|
cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
92 |
|
|
if (cmsg->cmsg_level == SOL_SOCKET && |
93 |
|
|
cmsg->cmsg_type == SCM_RIGHTS) { |
94 |
|
|
int i; |
95 |
|
|
int j; |
96 |
|
|
|
97 |
|
|
/* |
98 |
|
|
* We only accept one file descriptor. Due to C |
99 |
|
|
* padding rules, our control buffer might contain |
100 |
|
|
* more than one fd, and we must close them. |
101 |
|
|
*/ |
102 |
|
|
j = ((char *)cmsg + cmsg->cmsg_len - |
103 |
|
|
(char *)CMSG_DATA(cmsg)) / sizeof(int); |
104 |
|
|
for (i = 0; i < j; i++) { |
105 |
|
|
fd = ((int *)CMSG_DATA(cmsg))[i]; |
106 |
|
|
if (ifd != NULL) { |
107 |
|
|
ifd->fd = fd; |
108 |
|
|
TAILQ_INSERT_TAIL(&ibuf->fds, ifd, |
109 |
|
|
entry); |
110 |
|
|
ifd = NULL; |
111 |
|
|
} else |
112 |
|
|
close(fd); |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
/* we do not handle other ctl data level */ |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
fail: |
119 |
|
1190 |
free(ifd); |
120 |
|
1190 |
return (n); |
121 |
|
1190 |
} |
122 |
|
|
|
123 |
|
|
ssize_t |
124 |
|
|
imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) |
125 |
|
|
{ |
126 |
|
|
size_t av, left, datalen; |
127 |
|
|
|
128 |
|
9704 |
av = ibuf->r.wpos; |
129 |
|
|
|
130 |
✓✓ |
4852 |
if (IMSG_HEADER_SIZE > av) |
131 |
|
3646 |
return (0); |
132 |
|
|
|
133 |
|
1206 |
memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr)); |
134 |
✓✗✗✓
|
2412 |
if (imsg->hdr.len < IMSG_HEADER_SIZE || |
135 |
|
1206 |
imsg->hdr.len > MAX_IMSGSIZE) { |
136 |
|
|
errno = ERANGE; |
137 |
|
|
return (-1); |
138 |
|
|
} |
139 |
✗✓ |
1206 |
if (imsg->hdr.len > av) |
140 |
|
|
return (0); |
141 |
|
1206 |
datalen = imsg->hdr.len - IMSG_HEADER_SIZE; |
142 |
|
1206 |
ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; |
143 |
✓✓ |
1206 |
if (datalen == 0) |
144 |
|
1154 |
imsg->data = NULL; |
145 |
✗✓ |
52 |
else if ((imsg->data = malloc(datalen)) == NULL) |
146 |
|
|
return (-1); |
147 |
|
|
|
148 |
✗✓ |
1206 |
if (imsg->hdr.flags & IMSGF_HASFD) |
149 |
|
|
imsg->fd = imsg_get_fd(ibuf); |
150 |
|
|
else |
151 |
|
|
imsg->fd = -1; |
152 |
|
|
|
153 |
|
1206 |
memcpy(imsg->data, ibuf->r.rptr, datalen); |
154 |
|
|
|
155 |
✓✓ |
1206 |
if (imsg->hdr.len < av) { |
156 |
|
24 |
left = av - imsg->hdr.len; |
157 |
|
24 |
memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left); |
158 |
|
|
ibuf->r.wpos = left; |
159 |
|
24 |
} else |
160 |
|
|
ibuf->r.wpos = 0; |
161 |
|
|
|
162 |
|
1206 |
return (datalen + IMSG_HEADER_SIZE); |
163 |
|
4852 |
} |
164 |
|
|
|
165 |
|
|
int |
166 |
|
|
imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, |
167 |
|
|
int fd, const void *data, uint16_t datalen) |
168 |
|
|
{ |
169 |
|
|
struct ibuf *wbuf; |
170 |
|
|
|
171 |
✗✓ |
12568 |
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) |
172 |
|
|
return (-1); |
173 |
|
|
|
174 |
✗✓ |
6284 |
if (imsg_add(wbuf, data, datalen) == -1) |
175 |
|
|
return (-1); |
176 |
|
|
|
177 |
|
6284 |
wbuf->fd = fd; |
178 |
|
|
|
179 |
|
6284 |
imsg_close(ibuf, wbuf); |
180 |
|
|
|
181 |
|
6284 |
return (1); |
182 |
|
6284 |
} |
183 |
|
|
|
184 |
|
|
int |
185 |
|
|
imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, |
186 |
|
|
int fd, const struct iovec *iov, int iovcnt) |
187 |
|
|
{ |
188 |
|
|
struct ibuf *wbuf; |
189 |
|
|
int i, datalen = 0; |
190 |
|
|
|
191 |
✓✓ |
10080 |
for (i = 0; i < iovcnt; i++) |
192 |
|
2652 |
datalen += iov[i].iov_len; |
193 |
|
|
|
194 |
✗✓ |
1592 |
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) |
195 |
|
|
return (-1); |
196 |
|
|
|
197 |
✓✓ |
8488 |
for (i = 0; i < iovcnt; i++) |
198 |
✗✓ |
2652 |
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1) |
199 |
|
|
return (-1); |
200 |
|
|
|
201 |
|
1592 |
wbuf->fd = fd; |
202 |
|
|
|
203 |
|
1592 |
imsg_close(ibuf, wbuf); |
204 |
|
|
|
205 |
|
1592 |
return (1); |
206 |
|
1592 |
} |
207 |
|
|
|
208 |
|
|
/* ARGSUSED */ |
209 |
|
|
struct ibuf * |
210 |
|
|
imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, |
211 |
|
|
uint16_t datalen) |
212 |
|
|
{ |
213 |
|
|
struct ibuf *wbuf; |
214 |
|
15752 |
struct imsg_hdr hdr; |
215 |
|
|
|
216 |
|
7876 |
datalen += IMSG_HEADER_SIZE; |
217 |
✗✓ |
7876 |
if (datalen > MAX_IMSGSIZE) { |
218 |
|
|
errno = ERANGE; |
219 |
|
|
return (NULL); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
7876 |
hdr.type = type; |
223 |
|
7876 |
hdr.flags = 0; |
224 |
|
7876 |
hdr.peerid = peerid; |
225 |
✓✓ |
7876 |
if ((hdr.pid = pid) == 0) |
226 |
|
14 |
hdr.pid = ibuf->pid; |
227 |
✗✓ |
7876 |
if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) { |
228 |
|
|
return (NULL); |
229 |
|
|
} |
230 |
✗✓ |
7876 |
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1) |
231 |
|
|
return (NULL); |
232 |
|
|
|
233 |
|
7876 |
return (wbuf); |
234 |
|
7876 |
} |
235 |
|
|
|
236 |
|
|
int |
237 |
|
|
imsg_add(struct ibuf *msg, const void *data, uint16_t datalen) |
238 |
|
|
{ |
239 |
✓✓ |
33624 |
if (datalen) |
240 |
✗✓ |
15646 |
if (ibuf_add(msg, data, datalen) == -1) { |
241 |
|
|
ibuf_free(msg); |
242 |
|
|
return (-1); |
243 |
|
|
} |
244 |
|
16812 |
return (datalen); |
245 |
|
16812 |
} |
246 |
|
|
|
247 |
|
|
void |
248 |
|
|
imsg_close(struct imsgbuf *ibuf, struct ibuf *msg) |
249 |
|
|
{ |
250 |
|
|
struct imsg_hdr *hdr; |
251 |
|
|
|
252 |
|
15752 |
hdr = (struct imsg_hdr *)msg->buf; |
253 |
|
|
|
254 |
|
7876 |
hdr->flags &= ~IMSGF_HASFD; |
255 |
✓✓ |
7876 |
if (msg->fd != -1) |
256 |
|
3770 |
hdr->flags |= IMSGF_HASFD; |
257 |
|
|
|
258 |
|
7876 |
hdr->len = (uint16_t)msg->wpos; |
259 |
|
|
|
260 |
|
7876 |
ibuf_close(&ibuf->w, msg); |
261 |
|
7876 |
} |
262 |
|
|
|
263 |
|
|
void |
264 |
|
|
imsg_free(struct imsg *imsg) |
265 |
|
|
{ |
266 |
|
2412 |
freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE); |
267 |
|
1206 |
} |
268 |
|
|
|
269 |
|
|
int |
270 |
|
|
imsg_get_fd(struct imsgbuf *ibuf) |
271 |
|
|
{ |
272 |
|
|
int fd; |
273 |
|
|
struct imsg_fd *ifd; |
274 |
|
|
|
275 |
✓✗ |
2340 |
if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL) |
276 |
|
1170 |
return (-1); |
277 |
|
|
|
278 |
|
|
fd = ifd->fd; |
279 |
|
|
TAILQ_REMOVE(&ibuf->fds, ifd, entry); |
280 |
|
|
free(ifd); |
281 |
|
|
|
282 |
|
|
return (fd); |
283 |
|
1170 |
} |
284 |
|
|
|
285 |
|
|
int |
286 |
|
|
imsg_flush(struct imsgbuf *ibuf) |
287 |
|
|
{ |
288 |
✓✓ |
75964465 |
while (ibuf->w.queued) |
289 |
✓✓ |
25320647 |
if (msgbuf_write(&ibuf->w) <= 0) |
290 |
|
25316335 |
return (-1); |
291 |
|
3716 |
return (0); |
292 |
|
25320051 |
} |
293 |
|
|
|
294 |
|
|
void |
295 |
|
|
imsg_clear(struct imsgbuf *ibuf) |
296 |
|
|
{ |
297 |
|
|
int fd; |
298 |
|
|
|
299 |
|
2340 |
msgbuf_clear(&ibuf->w); |
300 |
✗✓ |
2340 |
while ((fd = imsg_get_fd(ibuf)) != -1) |
301 |
|
|
close(fd); |
302 |
|
1170 |
} |