Blame
Date:
Thu Oct 15 07:11:48 2020 UTC
Message:
up the debug a bit further while we're chasing a bug.
001
2017-10-04
pjp
/* $OpenBSD: imsg.c,v 1.5 2013/12/26 17:32:33 eric Exp $ */
002
2017-10-04
pjp
003
2017-10-04
pjp
/*
004
2017-10-04
pjp
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
005
2017-10-04
pjp
*
006
2017-10-04
pjp
* Permission to use, copy, modify, and distribute this software for any
007
2017-10-04
pjp
* purpose with or without fee is hereby granted, provided that the above
008
2017-10-04
pjp
* copyright notice and this permission notice appear in all copies.
009
2017-10-04
pjp
*
010
2017-10-04
pjp
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
011
2017-10-04
pjp
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
012
2017-10-04
pjp
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
013
2017-10-04
pjp
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
014
2017-10-04
pjp
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
015
2017-10-04
pjp
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
016
2017-10-04
pjp
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2017-10-04
pjp
*/
018
2017-10-04
pjp
019
2017-10-26
pjp
/*
020
2020-04-11
pjp
* $Id: imsg.c,v 1.3 2020/04/11 06:00:07 pjp Exp $
021
2017-10-26
pjp
*/
022
2017-10-04
pjp
023
2020-04-11
pjp
#include <sys/types.h>
024
2017-10-04
pjp
#include <sys/queue.h>
025
2017-10-04
pjp
#include <sys/socket.h>
026
2017-10-04
pjp
#include <sys/uio.h>
027
2017-10-04
pjp
028
2017-10-04
pjp
#include <errno.h>
029
2017-10-04
pjp
#include <stdlib.h>
030
2017-10-04
pjp
#include <string.h>
031
2017-10-04
pjp
#include <unistd.h>
032
2017-10-04
pjp
033
2017-10-04
pjp
#include "imsg.h"
034
2017-10-04
pjp
035
2017-10-04
pjp
int imsg_fd_overhead = 0;
036
2017-10-04
pjp
037
2017-10-04
pjp
int imsg_get_fd(struct imsgbuf *);
038
2017-10-04
pjp
039
2017-10-04
pjp
int
040
2017-10-04
pjp
available_fds(unsigned int n)
041
2017-10-04
pjp
{
042
2017-10-04
pjp
unsigned int i;
043
2017-10-04
pjp
int ret, fds[256];
044
2017-10-04
pjp
045
2017-10-04
pjp
if (n > (sizeof(fds)/sizeof(fds[0])))
046
2017-10-04
pjp
return (1);
047
2017-10-04
pjp
048
2017-10-04
pjp
ret = 0;
049
2017-10-04
pjp
for (i = 0; i < n; i++) {
050
2017-10-04
pjp
fds[i] = -1;
051
2017-10-04
pjp
if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
052
2017-10-04
pjp
ret = 1;
053
2017-10-04
pjp
break;
054
2017-10-04
pjp
}
055
2017-10-04
pjp
}
056
2017-10-04
pjp
057
2017-10-04
pjp
for (i = 0; i < n && fds[i] >= 0; i++)
058
2017-10-04
pjp
close(fds[i]);
059
2017-10-04
pjp
060
2017-10-04
pjp
return (ret);
061
2017-10-04
pjp
}
062
2017-10-04
pjp
063
2017-10-04
pjp
void
064
2017-10-04
pjp
imsg_init(struct imsgbuf *ibuf, int fd)
065
2017-10-04
pjp
{
066
2017-10-04
pjp
msgbuf_init(&ibuf->w);
067
2017-10-04
pjp
bzero(&ibuf->r, sizeof(ibuf->r));
068
2017-10-04
pjp
ibuf->fd = fd;
069
2017-10-04
pjp
ibuf->w.fd = fd;
070
2017-10-04
pjp
ibuf->pid = getpid();
071
2017-10-04
pjp
TAILQ_INIT(&ibuf->fds);
072
2017-10-04
pjp
}
073
2017-10-04
pjp
074
2017-10-04
pjp
ssize_t
075
2017-10-04
pjp
imsg_read(struct imsgbuf *ibuf)
076
2017-10-04
pjp
{
077
2017-10-04
pjp
struct msghdr msg;
078
2017-10-04
pjp
struct cmsghdr *cmsg;
079
2017-10-04
pjp
union {
080
2017-10-04
pjp
struct cmsghdr hdr;
081
2017-10-04
pjp
char buf[CMSG_SPACE(sizeof(int) * 1)];
082
2017-10-04
pjp
} cmsgbuf;
083
2017-10-04
pjp
struct iovec iov;
084
2017-10-04
pjp
ssize_t n = -1;
085
2017-10-04
pjp
int fd;
086
2017-10-04
pjp
struct imsg_fd *ifd;
087
2017-10-04
pjp
088
2017-10-04
pjp
bzero(&msg, sizeof(msg));
089
2017-10-04
pjp
090
2017-10-04
pjp
iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
091
2017-10-04
pjp
iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
092
2017-10-04
pjp
msg.msg_iov = &iov;
093
2017-10-04
pjp
msg.msg_iovlen = 1;
094
2017-10-04
pjp
msg.msg_control = &cmsgbuf.buf;
095
2017-10-04
pjp
msg.msg_controllen = sizeof(cmsgbuf.buf);
096
2017-10-04
pjp
097
2017-10-04
pjp
if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
098
2017-10-04
pjp
return (-1);
099
2017-10-04
pjp
100
2017-10-04
pjp
again:
101
2017-10-04
pjp
if (available_fds(imsg_fd_overhead +
102
2017-10-04
pjp
(CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))) {
103
2017-10-04
pjp
errno = EAGAIN;
104
2017-10-04
pjp
free(ifd);
105
2017-10-04
pjp
return (-1);
106
2017-10-04
pjp
}
107
2017-10-04
pjp
108
2017-10-04
pjp
if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
109
2017-10-04
pjp
if (errno == EMSGSIZE)
110
2017-10-04
pjp
goto fail;
111
2017-10-04
pjp
if (errno != EINTR && errno != EAGAIN)
112
2017-10-04
pjp
goto fail;
113
2017-10-04
pjp
goto again;
114
2017-10-04
pjp
}
115
2017-10-04
pjp
116
2017-10-04
pjp
ibuf->r.wpos += n;
117
2017-10-04
pjp
118
2017-10-04
pjp
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
119
2017-10-04
pjp
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
120
2017-10-04
pjp
if (cmsg->cmsg_level == SOL_SOCKET &&
121
2017-10-04
pjp
cmsg->cmsg_type == SCM_RIGHTS) {
122
2017-10-04
pjp
int i;
123
2017-10-04
pjp
int j;
124
2017-10-04
pjp
125
2017-10-04
pjp
/*
126
2017-10-04
pjp
* We only accept one file descriptor. Due to C
127
2017-10-04
pjp
* padding rules, our control buffer might contain
128
2017-10-04
pjp
* more than one fd, and we must close them.
129
2017-10-04
pjp
*/
130
2017-10-04
pjp
j = ((char *)cmsg + cmsg->cmsg_len -
131
2017-10-04
pjp
(char *)CMSG_DATA(cmsg)) / sizeof(int);
132
2017-10-04
pjp
for (i = 0; i < j; i++) {
133
2017-10-04
pjp
fd = ((int *)CMSG_DATA(cmsg))[i];
134
2017-10-04
pjp
if (ifd != NULL) {
135
2017-10-04
pjp
ifd->fd = fd;
136
2017-10-04
pjp
TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
137
2017-10-04
pjp
entry);
138
2017-10-04
pjp
ifd = NULL;
139
2017-10-04
pjp
} else
140
2017-10-04
pjp
close(fd);
141
2017-10-04
pjp
}
142
2017-10-04
pjp
}
143
2017-10-04
pjp
/* we do not handle other ctl data level */
144
2017-10-04
pjp
}
145
2017-10-04
pjp
146
2017-10-04
pjp
fail:
147
2017-10-04
pjp
if (ifd)
148
2017-10-04
pjp
free(ifd);
149
2017-10-04
pjp
return (n);
150
2017-10-04
pjp
}
151
2017-10-04
pjp
152
2017-10-04
pjp
ssize_t
153
2017-10-04
pjp
imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
154
2017-10-04
pjp
{
155
2017-10-04
pjp
size_t av, left, datalen;
156
2017-10-04
pjp
157
2017-10-04
pjp
av = ibuf->r.wpos;
158
2017-10-04
pjp
159
2017-10-04
pjp
if (IMSG_HEADER_SIZE > av)
160
2017-10-04
pjp
return (0);
161
2017-10-04
pjp
162
2017-10-04
pjp
memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
163
2017-10-04
pjp
if (imsg->hdr.len < IMSG_HEADER_SIZE ||
164
2017-10-04
pjp
imsg->hdr.len > MAX_IMSGSIZE) {
165
2017-10-04
pjp
errno = ERANGE;
166
2017-10-04
pjp
return (-1);
167
2017-10-04
pjp
}
168
2017-10-04
pjp
if (imsg->hdr.len > av)
169
2017-10-04
pjp
return (0);
170
2017-10-04
pjp
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
171
2017-10-04
pjp
ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
172
2017-10-04
pjp
if ((imsg->data = malloc(datalen)) == NULL)
173
2017-10-04
pjp
return (-1);
174
2017-10-04
pjp
175
2017-10-04
pjp
if (imsg->hdr.flags & IMSGF_HASFD)
176
2017-10-04
pjp
imsg->fd = imsg_get_fd(ibuf);
177
2017-10-04
pjp
else
178
2017-10-04
pjp
imsg->fd = -1;
179
2017-10-04
pjp
180
2017-10-04
pjp
memcpy(imsg->data, ibuf->r.rptr, datalen);
181
2017-10-04
pjp
182
2017-10-04
pjp
if (imsg->hdr.len < av) {
183
2017-10-04
pjp
left = av - imsg->hdr.len;
184
2017-10-04
pjp
memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
185
2017-10-04
pjp
ibuf->r.wpos = left;
186
2017-10-04
pjp
} else
187
2017-10-04
pjp
ibuf->r.wpos = 0;
188
2017-10-04
pjp
189
2017-10-04
pjp
return (datalen + IMSG_HEADER_SIZE);
190
2017-10-04
pjp
}
191
2017-10-04
pjp
192
2017-10-04
pjp
int
193
2017-10-04
pjp
imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
194
2017-10-04
pjp
pid_t pid, int fd, const void *data, u_int16_t datalen)
195
2017-10-04
pjp
{
196
2017-10-04
pjp
struct ibuf *wbuf;
197
2017-10-04
pjp
198
2017-10-04
pjp
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
199
2017-10-04
pjp
return (-1);
200
2017-10-04
pjp
201
2017-10-04
pjp
if (imsg_add(wbuf, data, datalen) == -1)
202
2017-10-04
pjp
return (-1);
203
2017-10-04
pjp
204
2017-10-04
pjp
wbuf->fd = fd;
205
2017-10-04
pjp
206
2017-10-04
pjp
imsg_close(ibuf, wbuf);
207
2017-10-04
pjp
208
2017-10-04
pjp
return (1);
209
2017-10-04
pjp
}
210
2017-10-04
pjp
211
2017-10-04
pjp
int
212
2017-10-04
pjp
imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
213
2017-10-04
pjp
pid_t pid, int fd, const struct iovec *iov, int iovcnt)
214
2017-10-04
pjp
{
215
2017-10-04
pjp
struct ibuf *wbuf;
216
2017-10-04
pjp
int i, datalen = 0;
217
2017-10-04
pjp
218
2017-10-04
pjp
for (i = 0; i < iovcnt; i++)
219
2017-10-04
pjp
datalen += iov[i].iov_len;
220
2017-10-04
pjp
221
2017-10-04
pjp
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
222
2017-10-04
pjp
return (-1);
223
2017-10-04
pjp
224
2017-10-04
pjp
for (i = 0; i < iovcnt; i++)
225
2017-10-04
pjp
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
226
2017-10-04
pjp
return (-1);
227
2017-10-04
pjp
228
2017-10-04
pjp
wbuf->fd = fd;
229
2017-10-04
pjp
230
2017-10-04
pjp
imsg_close(ibuf, wbuf);
231
2017-10-04
pjp
232
2017-10-04
pjp
return (1);
233
2017-10-04
pjp
}
234
2017-10-04
pjp
235
2017-10-04
pjp
/* ARGSUSED */
236
2017-10-04
pjp
struct ibuf *
237
2017-10-04
pjp
imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
238
2017-10-04
pjp
pid_t pid, u_int16_t datalen)
239
2017-10-04
pjp
{
240
2017-10-04
pjp
struct ibuf *wbuf;
241
2017-10-04
pjp
struct imsg_hdr hdr;
242
2017-10-04
pjp
243
2017-10-04
pjp
datalen += IMSG_HEADER_SIZE;
244
2017-10-04
pjp
if (datalen > MAX_IMSGSIZE) {
245
2017-10-04
pjp
errno = ERANGE;
246
2017-10-04
pjp
return (NULL);
247
2017-10-04
pjp
}
248
2017-10-04
pjp
249
2017-10-04
pjp
hdr.type = type;
250
2017-10-04
pjp
hdr.flags = 0;
251
2017-10-04
pjp
hdr.peerid = peerid;
252
2017-10-04
pjp
if ((hdr.pid = pid) == 0)
253
2017-10-04
pjp
hdr.pid = ibuf->pid;
254
2017-10-04
pjp
if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
255
2017-10-04
pjp
return (NULL);
256
2017-10-04
pjp
}
257
2017-10-04
pjp
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
258
2017-10-04
pjp
return (NULL);
259
2017-10-04
pjp
260
2017-10-04
pjp
return (wbuf);
261
2017-10-04
pjp
}
262
2017-10-04
pjp
263
2017-10-04
pjp
int
264
2017-10-04
pjp
imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
265
2017-10-04
pjp
{
266
2017-10-04
pjp
if (datalen)
267
2017-10-04
pjp
if (ibuf_add(msg, data, datalen) == -1) {
268
2017-10-04
pjp
ibuf_free(msg);
269
2017-10-04
pjp
return (-1);
270
2017-10-04
pjp
}
271
2017-10-04
pjp
return (datalen);
272
2017-10-04
pjp
}
273
2017-10-04
pjp
274
2017-10-04
pjp
void
275
2017-10-04
pjp
imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
276
2017-10-04
pjp
{
277
2017-10-04
pjp
struct imsg_hdr *hdr;
278
2017-10-04
pjp
279
2017-10-04
pjp
hdr = (struct imsg_hdr *)msg->buf;
280
2017-10-04
pjp
281
2017-10-04
pjp
hdr->flags &= ~IMSGF_HASFD;
282
2017-10-04
pjp
if (msg->fd != -1)
283
2017-10-04
pjp
hdr->flags |= IMSGF_HASFD;
284
2017-10-04
pjp
285
2017-10-04
pjp
hdr->len = (u_int16_t)msg->wpos;
286
2017-10-04
pjp
287
2017-10-04
pjp
ibuf_close(&ibuf->w, msg);
288
2017-10-04
pjp
}
289
2017-10-04
pjp
290
2017-10-04
pjp
void
291
2017-10-04
pjp
imsg_free(struct imsg *imsg)
292
2017-10-04
pjp
{
293
2017-10-04
pjp
free(imsg->data);
294
2017-10-04
pjp
}
295
2017-10-04
pjp
296
2017-10-04
pjp
int
297
2017-10-04
pjp
imsg_get_fd(struct imsgbuf *ibuf)
298
2017-10-04
pjp
{
299
2017-10-04
pjp
int fd;
300
2017-10-04
pjp
struct imsg_fd *ifd;
301
2017-10-04
pjp
302
2017-10-04
pjp
if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
303
2017-10-04
pjp
return (-1);
304
2017-10-04
pjp
305
2017-10-04
pjp
fd = ifd->fd;
306
2017-10-04
pjp
TAILQ_REMOVE(&ibuf->fds, ifd, entry);
307
2017-10-04
pjp
free(ifd);
308
2017-10-04
pjp
309
2017-10-04
pjp
return (fd);
310
2017-10-04
pjp
}
311
2017-10-04
pjp
312
2017-10-04
pjp
int
313
2017-10-04
pjp
imsg_flush(struct imsgbuf *ibuf)
314
2017-10-04
pjp
{
315
2017-10-04
pjp
while (ibuf->w.queued)
316
2017-10-04
pjp
if (msgbuf_write(&ibuf->w) <= 0)
317
2017-10-04
pjp
return (-1);
318
2017-10-04
pjp
return (0);
319
2017-10-04
pjp
}
320
2017-10-04
pjp
321
2017-10-04
pjp
void
322
2017-10-04
pjp
imsg_clear(struct imsgbuf *ibuf)
323
2017-10-04
pjp
{
324
2017-10-04
pjp
int fd;
325
2017-10-04
pjp
326
2017-10-04
pjp
msgbuf_clear(&ibuf->w);
327
2017-10-04
pjp
while ((fd = imsg_get_fd(ibuf)) != -1)
328
2017-10-04
pjp
close(fd);
329
2017-10-04
pjp
}
repomaster@centroid.eu