Blame
Date:
Mon Jan 11 09:00:47 2021 UTC
Message:
a usleep makes it unnecessary slow, I guess it forces a context switch...
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
2020-04-11
pjp
#include <sys/types.h>
020
2017-10-04
pjp
#include <sys/queue.h>
021
2017-10-04
pjp
#include <sys/socket.h>
022
2017-10-04
pjp
#include <sys/uio.h>
023
2017-10-04
pjp
024
2017-10-04
pjp
#include <errno.h>
025
2017-10-04
pjp
#include <stdlib.h>
026
2017-10-04
pjp
#include <string.h>
027
2017-10-04
pjp
#include <unistd.h>
028
2017-10-04
pjp
029
2017-10-04
pjp
#include "imsg.h"
030
2017-10-04
pjp
031
2017-10-04
pjp
int imsg_fd_overhead = 0;
032
2017-10-04
pjp
033
2017-10-04
pjp
int imsg_get_fd(struct imsgbuf *);
034
2017-10-04
pjp
035
2017-10-04
pjp
int
036
2017-10-04
pjp
available_fds(unsigned int n)
037
2017-10-04
pjp
{
038
2017-10-04
pjp
unsigned int i;
039
2017-10-04
pjp
int ret, fds[256];
040
2017-10-04
pjp
041
2017-10-04
pjp
if (n > (sizeof(fds)/sizeof(fds[0])))
042
2017-10-04
pjp
return (1);
043
2017-10-04
pjp
044
2017-10-04
pjp
ret = 0;
045
2017-10-04
pjp
for (i = 0; i < n; i++) {
046
2017-10-04
pjp
fds[i] = -1;
047
2017-10-04
pjp
if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
048
2017-10-04
pjp
ret = 1;
049
2017-10-04
pjp
break;
050
2017-10-04
pjp
}
051
2017-10-04
pjp
}
052
2017-10-04
pjp
053
2017-10-04
pjp
for (i = 0; i < n && fds[i] >= 0; i++)
054
2017-10-04
pjp
close(fds[i]);
055
2017-10-04
pjp
056
2017-10-04
pjp
return (ret);
057
2017-10-04
pjp
}
058
2017-10-04
pjp
059
2017-10-04
pjp
void
060
2017-10-04
pjp
imsg_init(struct imsgbuf *ibuf, int fd)
061
2017-10-04
pjp
{
062
2017-10-04
pjp
msgbuf_init(&ibuf->w);
063
2017-10-04
pjp
bzero(&ibuf->r, sizeof(ibuf->r));
064
2017-10-04
pjp
ibuf->fd = fd;
065
2017-10-04
pjp
ibuf->w.fd = fd;
066
2017-10-04
pjp
ibuf->pid = getpid();
067
2017-10-04
pjp
TAILQ_INIT(&ibuf->fds);
068
2017-10-04
pjp
}
069
2017-10-04
pjp
070
2017-10-04
pjp
ssize_t
071
2017-10-04
pjp
imsg_read(struct imsgbuf *ibuf)
072
2017-10-04
pjp
{
073
2017-10-04
pjp
struct msghdr msg;
074
2017-10-04
pjp
struct cmsghdr *cmsg;
075
2017-10-04
pjp
union {
076
2017-10-04
pjp
struct cmsghdr hdr;
077
2017-10-04
pjp
char buf[CMSG_SPACE(sizeof(int) * 1)];
078
2017-10-04
pjp
} cmsgbuf;
079
2017-10-04
pjp
struct iovec iov;
080
2017-10-04
pjp
ssize_t n = -1;
081
2017-10-04
pjp
int fd;
082
2017-10-04
pjp
struct imsg_fd *ifd;
083
2017-10-04
pjp
084
2017-10-04
pjp
bzero(&msg, sizeof(msg));
085
2017-10-04
pjp
086
2017-10-04
pjp
iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
087
2017-10-04
pjp
iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
088
2017-10-04
pjp
msg.msg_iov = &iov;
089
2017-10-04
pjp
msg.msg_iovlen = 1;
090
2017-10-04
pjp
msg.msg_control = &cmsgbuf.buf;
091
2017-10-04
pjp
msg.msg_controllen = sizeof(cmsgbuf.buf);
092
2017-10-04
pjp
093
2017-10-04
pjp
if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
094
2017-10-04
pjp
return (-1);
095
2017-10-04
pjp
096
2017-10-04
pjp
again:
097
2017-10-04
pjp
if (available_fds(imsg_fd_overhead +
098
2017-10-04
pjp
(CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))) {
099
2017-10-04
pjp
errno = EAGAIN;
100
2017-10-04
pjp
free(ifd);
101
2017-10-04
pjp
return (-1);
102
2017-10-04
pjp
}
103
2017-10-04
pjp
104
2017-10-04
pjp
if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
105
2017-10-04
pjp
if (errno == EMSGSIZE)
106
2017-10-04
pjp
goto fail;
107
2017-10-04
pjp
if (errno != EINTR && errno != EAGAIN)
108
2017-10-04
pjp
goto fail;
109
2017-10-04
pjp
goto again;
110
2017-10-04
pjp
}
111
2017-10-04
pjp
112
2017-10-04
pjp
ibuf->r.wpos += n;
113
2017-10-04
pjp
114
2017-10-04
pjp
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
115
2017-10-04
pjp
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
116
2017-10-04
pjp
if (cmsg->cmsg_level == SOL_SOCKET &&
117
2017-10-04
pjp
cmsg->cmsg_type == SCM_RIGHTS) {
118
2017-10-04
pjp
int i;
119
2017-10-04
pjp
int j;
120
2017-10-04
pjp
121
2017-10-04
pjp
/*
122
2017-10-04
pjp
* We only accept one file descriptor. Due to C
123
2017-10-04
pjp
* padding rules, our control buffer might contain
124
2017-10-04
pjp
* more than one fd, and we must close them.
125
2017-10-04
pjp
*/
126
2017-10-04
pjp
j = ((char *)cmsg + cmsg->cmsg_len -
127
2017-10-04
pjp
(char *)CMSG_DATA(cmsg)) / sizeof(int);
128
2017-10-04
pjp
for (i = 0; i < j; i++) {
129
2017-10-04
pjp
fd = ((int *)CMSG_DATA(cmsg))[i];
130
2017-10-04
pjp
if (ifd != NULL) {
131
2017-10-04
pjp
ifd->fd = fd;
132
2017-10-04
pjp
TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
133
2017-10-04
pjp
entry);
134
2017-10-04
pjp
ifd = NULL;
135
2017-10-04
pjp
} else
136
2017-10-04
pjp
close(fd);
137
2017-10-04
pjp
}
138
2017-10-04
pjp
}
139
2017-10-04
pjp
/* we do not handle other ctl data level */
140
2017-10-04
pjp
}
141
2017-10-04
pjp
142
2017-10-04
pjp
fail:
143
2017-10-04
pjp
if (ifd)
144
2017-10-04
pjp
free(ifd);
145
2017-10-04
pjp
return (n);
146
2017-10-04
pjp
}
147
2017-10-04
pjp
148
2017-10-04
pjp
ssize_t
149
2017-10-04
pjp
imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
150
2017-10-04
pjp
{
151
2017-10-04
pjp
size_t av, left, datalen;
152
2017-10-04
pjp
153
2017-10-04
pjp
av = ibuf->r.wpos;
154
2017-10-04
pjp
155
2017-10-04
pjp
if (IMSG_HEADER_SIZE > av)
156
2017-10-04
pjp
return (0);
157
2017-10-04
pjp
158
2017-10-04
pjp
memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
159
2017-10-04
pjp
if (imsg->hdr.len < IMSG_HEADER_SIZE ||
160
2017-10-04
pjp
imsg->hdr.len > MAX_IMSGSIZE) {
161
2017-10-04
pjp
errno = ERANGE;
162
2017-10-04
pjp
return (-1);
163
2017-10-04
pjp
}
164
2017-10-04
pjp
if (imsg->hdr.len > av)
165
2017-10-04
pjp
return (0);
166
2017-10-04
pjp
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
167
2017-10-04
pjp
ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
168
2017-10-04
pjp
if ((imsg->data = malloc(datalen)) == NULL)
169
2017-10-04
pjp
return (-1);
170
2017-10-04
pjp
171
2017-10-04
pjp
if (imsg->hdr.flags & IMSGF_HASFD)
172
2017-10-04
pjp
imsg->fd = imsg_get_fd(ibuf);
173
2017-10-04
pjp
else
174
2017-10-04
pjp
imsg->fd = -1;
175
2017-10-04
pjp
176
2017-10-04
pjp
memcpy(imsg->data, ibuf->r.rptr, datalen);
177
2017-10-04
pjp
178
2017-10-04
pjp
if (imsg->hdr.len < av) {
179
2017-10-04
pjp
left = av - imsg->hdr.len;
180
2017-10-04
pjp
memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
181
2017-10-04
pjp
ibuf->r.wpos = left;
182
2017-10-04
pjp
} else
183
2017-10-04
pjp
ibuf->r.wpos = 0;
184
2017-10-04
pjp
185
2017-10-04
pjp
return (datalen + IMSG_HEADER_SIZE);
186
2017-10-04
pjp
}
187
2017-10-04
pjp
188
2017-10-04
pjp
int
189
2017-10-04
pjp
imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
190
2017-10-04
pjp
pid_t pid, int fd, const void *data, u_int16_t datalen)
191
2017-10-04
pjp
{
192
2017-10-04
pjp
struct ibuf *wbuf;
193
2017-10-04
pjp
194
2017-10-04
pjp
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
195
2017-10-04
pjp
return (-1);
196
2017-10-04
pjp
197
2017-10-04
pjp
if (imsg_add(wbuf, data, datalen) == -1)
198
2017-10-04
pjp
return (-1);
199
2017-10-04
pjp
200
2017-10-04
pjp
wbuf->fd = fd;
201
2017-10-04
pjp
202
2017-10-04
pjp
imsg_close(ibuf, wbuf);
203
2017-10-04
pjp
204
2017-10-04
pjp
return (1);
205
2017-10-04
pjp
}
206
2017-10-04
pjp
207
2017-10-04
pjp
int
208
2017-10-04
pjp
imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
209
2017-10-04
pjp
pid_t pid, int fd, const struct iovec *iov, int iovcnt)
210
2017-10-04
pjp
{
211
2017-10-04
pjp
struct ibuf *wbuf;
212
2017-10-04
pjp
int i, datalen = 0;
213
2017-10-04
pjp
214
2017-10-04
pjp
for (i = 0; i < iovcnt; i++)
215
2017-10-04
pjp
datalen += iov[i].iov_len;
216
2017-10-04
pjp
217
2017-10-04
pjp
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
218
2017-10-04
pjp
return (-1);
219
2017-10-04
pjp
220
2017-10-04
pjp
for (i = 0; i < iovcnt; i++)
221
2017-10-04
pjp
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
222
2017-10-04
pjp
return (-1);
223
2017-10-04
pjp
224
2017-10-04
pjp
wbuf->fd = fd;
225
2017-10-04
pjp
226
2017-10-04
pjp
imsg_close(ibuf, wbuf);
227
2017-10-04
pjp
228
2017-10-04
pjp
return (1);
229
2017-10-04
pjp
}
230
2017-10-04
pjp
231
2017-10-04
pjp
/* ARGSUSED */
232
2017-10-04
pjp
struct ibuf *
233
2017-10-04
pjp
imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
234
2017-10-04
pjp
pid_t pid, u_int16_t datalen)
235
2017-10-04
pjp
{
236
2017-10-04
pjp
struct ibuf *wbuf;
237
2017-10-04
pjp
struct imsg_hdr hdr;
238
2017-10-04
pjp
239
2017-10-04
pjp
datalen += IMSG_HEADER_SIZE;
240
2017-10-04
pjp
if (datalen > MAX_IMSGSIZE) {
241
2017-10-04
pjp
errno = ERANGE;
242
2017-10-04
pjp
return (NULL);
243
2017-10-04
pjp
}
244
2017-10-04
pjp
245
2017-10-04
pjp
hdr.type = type;
246
2017-10-04
pjp
hdr.flags = 0;
247
2017-10-04
pjp
hdr.peerid = peerid;
248
2017-10-04
pjp
if ((hdr.pid = pid) == 0)
249
2017-10-04
pjp
hdr.pid = ibuf->pid;
250
2017-10-04
pjp
if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
251
2017-10-04
pjp
return (NULL);
252
2017-10-04
pjp
}
253
2017-10-04
pjp
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
254
2017-10-04
pjp
return (NULL);
255
2017-10-04
pjp
256
2017-10-04
pjp
return (wbuf);
257
2017-10-04
pjp
}
258
2017-10-04
pjp
259
2017-10-04
pjp
int
260
2017-10-04
pjp
imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
261
2017-10-04
pjp
{
262
2017-10-04
pjp
if (datalen)
263
2017-10-04
pjp
if (ibuf_add(msg, data, datalen) == -1) {
264
2017-10-04
pjp
ibuf_free(msg);
265
2017-10-04
pjp
return (-1);
266
2017-10-04
pjp
}
267
2017-10-04
pjp
return (datalen);
268
2017-10-04
pjp
}
269
2017-10-04
pjp
270
2017-10-04
pjp
void
271
2017-10-04
pjp
imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
272
2017-10-04
pjp
{
273
2017-10-04
pjp
struct imsg_hdr *hdr;
274
2017-10-04
pjp
275
2017-10-04
pjp
hdr = (struct imsg_hdr *)msg->buf;
276
2017-10-04
pjp
277
2017-10-04
pjp
hdr->flags &= ~IMSGF_HASFD;
278
2017-10-04
pjp
if (msg->fd != -1)
279
2017-10-04
pjp
hdr->flags |= IMSGF_HASFD;
280
2017-10-04
pjp
281
2017-10-04
pjp
hdr->len = (u_int16_t)msg->wpos;
282
2017-10-04
pjp
283
2017-10-04
pjp
ibuf_close(&ibuf->w, msg);
284
2017-10-04
pjp
}
285
2017-10-04
pjp
286
2017-10-04
pjp
void
287
2017-10-04
pjp
imsg_free(struct imsg *imsg)
288
2017-10-04
pjp
{
289
2017-10-04
pjp
free(imsg->data);
290
2017-10-04
pjp
}
291
2017-10-04
pjp
292
2017-10-04
pjp
int
293
2017-10-04
pjp
imsg_get_fd(struct imsgbuf *ibuf)
294
2017-10-04
pjp
{
295
2017-10-04
pjp
int fd;
296
2017-10-04
pjp
struct imsg_fd *ifd;
297
2017-10-04
pjp
298
2017-10-04
pjp
if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
299
2017-10-04
pjp
return (-1);
300
2017-10-04
pjp
301
2017-10-04
pjp
fd = ifd->fd;
302
2017-10-04
pjp
TAILQ_REMOVE(&ibuf->fds, ifd, entry);
303
2017-10-04
pjp
free(ifd);
304
2017-10-04
pjp
305
2017-10-04
pjp
return (fd);
306
2017-10-04
pjp
}
307
2017-10-04
pjp
308
2017-10-04
pjp
int
309
2017-10-04
pjp
imsg_flush(struct imsgbuf *ibuf)
310
2017-10-04
pjp
{
311
2017-10-04
pjp
while (ibuf->w.queued)
312
2017-10-04
pjp
if (msgbuf_write(&ibuf->w) <= 0)
313
2017-10-04
pjp
return (-1);
314
2017-10-04
pjp
return (0);
315
2017-10-04
pjp
}
316
2017-10-04
pjp
317
2017-10-04
pjp
void
318
2017-10-04
pjp
imsg_clear(struct imsgbuf *ibuf)
319
2017-10-04
pjp
{
320
2017-10-04
pjp
int fd;
321
2017-10-04
pjp
322
2017-10-04
pjp
msgbuf_clear(&ibuf->w);
323
2017-10-04
pjp
while ((fd = imsg_get_fd(ibuf)) != -1)
324
2017-10-04
pjp
close(fd);
325
2017-10-04
pjp
}
repomaster@centroid.eu