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-buffer.c,v 1.3 2013/11/13 20:40:24 benno 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 ibuf_realloc(struct ibuf *, size_t);
032
2017-10-04
pjp
void ibuf_enqueue(struct msgbuf *, struct ibuf *);
033
2017-10-04
pjp
void ibuf_dequeue(struct msgbuf *, struct ibuf *);
034
2017-10-04
pjp
035
2017-10-04
pjp
struct ibuf *
036
2017-10-04
pjp
ibuf_open(size_t len)
037
2017-10-04
pjp
{
038
2017-10-04
pjp
struct ibuf *buf;
039
2017-10-04
pjp
040
2017-10-04
pjp
if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
041
2017-10-04
pjp
return (NULL);
042
2017-10-04
pjp
if ((buf->buf = malloc(len)) == NULL) {
043
2017-10-04
pjp
free(buf);
044
2017-10-04
pjp
return (NULL);
045
2017-10-04
pjp
}
046
2017-10-04
pjp
buf->size = buf->max = len;
047
2017-10-04
pjp
buf->fd = -1;
048
2017-10-04
pjp
049
2017-10-04
pjp
return (buf);
050
2017-10-04
pjp
}
051
2017-10-04
pjp
052
2017-10-04
pjp
struct ibuf *
053
2017-10-04
pjp
ibuf_dynamic(size_t len, size_t max)
054
2017-10-04
pjp
{
055
2017-10-04
pjp
struct ibuf *buf;
056
2017-10-04
pjp
057
2017-10-04
pjp
if (max < len)
058
2017-10-04
pjp
return (NULL);
059
2017-10-04
pjp
060
2017-10-04
pjp
if ((buf = ibuf_open(len)) == NULL)
061
2017-10-04
pjp
return (NULL);
062
2017-10-04
pjp
063
2017-10-04
pjp
if (max > 0)
064
2017-10-04
pjp
buf->max = max;
065
2017-10-04
pjp
066
2017-10-04
pjp
return (buf);
067
2017-10-04
pjp
}
068
2017-10-04
pjp
069
2017-10-04
pjp
int
070
2017-10-04
pjp
ibuf_realloc(struct ibuf *buf, size_t len)
071
2017-10-04
pjp
{
072
2017-10-04
pjp
u_char *b;
073
2017-10-04
pjp
074
2017-10-04
pjp
/* on static buffers max is eq size and so the following fails */
075
2017-10-04
pjp
if (buf->wpos + len > buf->max) {
076
2017-10-04
pjp
errno = ENOMEM;
077
2017-10-04
pjp
return (-1);
078
2017-10-04
pjp
}
079
2017-10-04
pjp
080
2017-10-04
pjp
b = realloc(buf->buf, buf->wpos + len);
081
2017-10-04
pjp
if (b == NULL)
082
2017-10-04
pjp
return (-1);
083
2017-10-04
pjp
buf->buf = b;
084
2017-10-04
pjp
buf->size = buf->wpos + len;
085
2017-10-04
pjp
086
2017-10-04
pjp
return (0);
087
2017-10-04
pjp
}
088
2017-10-04
pjp
089
2017-10-04
pjp
int
090
2017-10-04
pjp
ibuf_add(struct ibuf *buf, const void *data, size_t len)
091
2017-10-04
pjp
{
092
2017-10-04
pjp
if (buf->wpos + len > buf->size)
093
2017-10-04
pjp
if (ibuf_realloc(buf, len) == -1)
094
2017-10-04
pjp
return (-1);
095
2017-10-04
pjp
096
2017-10-04
pjp
memcpy(buf->buf + buf->wpos, data, len);
097
2017-10-04
pjp
buf->wpos += len;
098
2017-10-04
pjp
return (0);
099
2017-10-04
pjp
}
100
2017-10-04
pjp
101
2017-10-04
pjp
void *
102
2017-10-04
pjp
ibuf_reserve(struct ibuf *buf, size_t len)
103
2017-10-04
pjp
{
104
2017-10-04
pjp
void *b;
105
2017-10-04
pjp
106
2017-10-04
pjp
if (buf->wpos + len > buf->size)
107
2017-10-04
pjp
if (ibuf_realloc(buf, len) == -1)
108
2017-10-04
pjp
return (NULL);
109
2017-10-04
pjp
110
2017-10-04
pjp
b = buf->buf + buf->wpos;
111
2017-10-04
pjp
buf->wpos += len;
112
2017-10-04
pjp
return (b);
113
2017-10-04
pjp
}
114
2017-10-04
pjp
115
2017-10-04
pjp
void *
116
2017-10-04
pjp
ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
117
2017-10-04
pjp
{
118
2017-10-04
pjp
/* only allowed to seek in already written parts */
119
2017-10-04
pjp
if (pos + len > buf->wpos)
120
2017-10-04
pjp
return (NULL);
121
2017-10-04
pjp
122
2017-10-04
pjp
return (buf->buf + pos);
123
2017-10-04
pjp
}
124
2017-10-04
pjp
125
2017-10-04
pjp
size_t
126
2017-10-04
pjp
ibuf_size(struct ibuf *buf)
127
2017-10-04
pjp
{
128
2017-10-04
pjp
return (buf->wpos);
129
2017-10-04
pjp
}
130
2017-10-04
pjp
131
2017-10-04
pjp
size_t
132
2017-10-04
pjp
ibuf_left(struct ibuf *buf)
133
2017-10-04
pjp
{
134
2017-10-04
pjp
return (buf->max - buf->wpos);
135
2017-10-04
pjp
}
136
2017-10-04
pjp
137
2017-10-04
pjp
void
138
2017-10-04
pjp
ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
139
2017-10-04
pjp
{
140
2017-10-04
pjp
ibuf_enqueue(msgbuf, buf);
141
2017-10-04
pjp
}
142
2017-10-04
pjp
143
2017-10-04
pjp
int
144
2017-10-04
pjp
ibuf_write(struct msgbuf *msgbuf)
145
2017-10-04
pjp
{
146
2017-10-04
pjp
struct iovec iov[IOV_MAX];
147
2017-10-04
pjp
struct ibuf *buf;
148
2017-10-04
pjp
unsigned int i = 0;
149
2017-10-04
pjp
ssize_t n;
150
2017-10-04
pjp
151
2017-10-04
pjp
bzero(&iov, sizeof(iov));
152
2017-10-04
pjp
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
153
2017-10-04
pjp
if (i >= IOV_MAX)
154
2017-10-04
pjp
break;
155
2017-10-04
pjp
iov[i].iov_base = buf->buf + buf->rpos;
156
2017-10-04
pjp
iov[i].iov_len = buf->wpos - buf->rpos;
157
2017-10-04
pjp
i++;
158
2017-10-04
pjp
}
159
2017-10-04
pjp
160
2017-10-04
pjp
again:
161
2017-10-04
pjp
if ((n = writev(msgbuf->fd, iov, i)) == -1) {
162
2017-10-04
pjp
if (errno == EINTR)
163
2017-10-04
pjp
goto again;
164
2017-10-04
pjp
if (errno == ENOBUFS)
165
2017-10-04
pjp
errno = EAGAIN;
166
2017-10-04
pjp
return (-1);
167
2017-10-04
pjp
}
168
2017-10-04
pjp
169
2017-10-04
pjp
if (n == 0) { /* connection closed */
170
2017-10-04
pjp
errno = 0;
171
2017-10-04
pjp
return (0);
172
2017-10-04
pjp
}
173
2017-10-04
pjp
174
2017-10-04
pjp
msgbuf_drain(msgbuf, n);
175
2017-10-04
pjp
176
2017-10-04
pjp
return (1);
177
2017-10-04
pjp
}
178
2017-10-04
pjp
179
2017-10-04
pjp
void
180
2017-10-04
pjp
ibuf_free(struct ibuf *buf)
181
2017-10-04
pjp
{
182
2017-10-04
pjp
free(buf->buf);
183
2017-10-04
pjp
free(buf);
184
2017-10-04
pjp
}
185
2017-10-04
pjp
186
2017-10-04
pjp
void
187
2017-10-04
pjp
msgbuf_init(struct msgbuf *msgbuf)
188
2017-10-04
pjp
{
189
2017-10-04
pjp
msgbuf->queued = 0;
190
2017-10-04
pjp
msgbuf->fd = -1;
191
2017-10-04
pjp
TAILQ_INIT(&msgbuf->bufs);
192
2017-10-04
pjp
}
193
2017-10-04
pjp
194
2017-10-04
pjp
void
195
2017-10-04
pjp
msgbuf_drain(struct msgbuf *msgbuf, size_t n)
196
2017-10-04
pjp
{
197
2017-10-04
pjp
struct ibuf *buf, *next;
198
2017-10-04
pjp
199
2017-10-04
pjp
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
200
2017-10-04
pjp
buf = next) {
201
2017-10-04
pjp
next = TAILQ_NEXT(buf, entry);
202
2017-10-04
pjp
if (buf->rpos + n >= buf->wpos) {
203
2017-10-04
pjp
n -= buf->wpos - buf->rpos;
204
2017-10-04
pjp
ibuf_dequeue(msgbuf, buf);
205
2017-10-04
pjp
} else {
206
2017-10-04
pjp
buf->rpos += n;
207
2017-10-04
pjp
n = 0;
208
2017-10-04
pjp
}
209
2017-10-04
pjp
}
210
2017-10-04
pjp
}
211
2017-10-04
pjp
212
2017-10-04
pjp
void
213
2017-10-04
pjp
msgbuf_clear(struct msgbuf *msgbuf)
214
2017-10-04
pjp
{
215
2017-10-04
pjp
struct ibuf *buf;
216
2017-10-04
pjp
217
2017-10-04
pjp
while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
218
2017-10-04
pjp
ibuf_dequeue(msgbuf, buf);
219
2017-10-04
pjp
}
220
2017-10-04
pjp
221
2017-10-04
pjp
int
222
2017-10-04
pjp
msgbuf_write(struct msgbuf *msgbuf)
223
2017-10-04
pjp
{
224
2017-10-04
pjp
struct iovec iov[IOV_MAX];
225
2017-10-04
pjp
struct ibuf *buf;
226
2017-10-04
pjp
unsigned int i = 0;
227
2017-10-04
pjp
ssize_t n;
228
2017-10-04
pjp
struct msghdr msg;
229
2017-10-04
pjp
struct cmsghdr *cmsg;
230
2017-10-04
pjp
union {
231
2017-10-04
pjp
struct cmsghdr hdr;
232
2017-10-04
pjp
char buf[CMSG_SPACE(sizeof(int))];
233
2017-10-04
pjp
} cmsgbuf;
234
2017-10-04
pjp
235
2017-10-04
pjp
bzero(&iov, sizeof(iov));
236
2017-10-04
pjp
bzero(&msg, sizeof(msg));
237
2017-10-04
pjp
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
238
2017-10-04
pjp
if (i >= IOV_MAX)
239
2017-10-04
pjp
break;
240
2017-10-04
pjp
iov[i].iov_base = buf->buf + buf->rpos;
241
2017-10-04
pjp
iov[i].iov_len = buf->wpos - buf->rpos;
242
2017-10-04
pjp
i++;
243
2017-10-04
pjp
if (buf->fd != -1)
244
2017-10-04
pjp
break;
245
2017-10-04
pjp
}
246
2017-10-04
pjp
247
2017-10-04
pjp
msg.msg_iov = iov;
248
2017-10-04
pjp
msg.msg_iovlen = i;
249
2017-10-04
pjp
250
2017-10-04
pjp
if (buf != NULL && buf->fd != -1) {
251
2017-10-04
pjp
msg.msg_control = (caddr_t)&cmsgbuf.buf;
252
2017-10-04
pjp
msg.msg_controllen = sizeof(cmsgbuf.buf);
253
2017-10-04
pjp
cmsg = CMSG_FIRSTHDR(&msg);
254
2017-10-04
pjp
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
255
2017-10-04
pjp
cmsg->cmsg_level = SOL_SOCKET;
256
2017-10-04
pjp
cmsg->cmsg_type = SCM_RIGHTS;
257
2017-10-04
pjp
*(int *)CMSG_DATA(cmsg) = buf->fd;
258
2017-10-04
pjp
}
259
2017-10-04
pjp
260
2017-10-04
pjp
again:
261
2017-10-04
pjp
if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
262
2017-10-04
pjp
if (errno == EINTR)
263
2017-10-04
pjp
goto again;
264
2017-10-04
pjp
if (errno == ENOBUFS)
265
2017-10-04
pjp
errno = EAGAIN;
266
2017-10-04
pjp
return (-1);
267
2017-10-04
pjp
}
268
2017-10-04
pjp
269
2017-10-04
pjp
if (n == 0) { /* connection closed */
270
2017-10-04
pjp
errno = 0;
271
2017-10-04
pjp
return (0);
272
2017-10-04
pjp
}
273
2017-10-04
pjp
274
2017-10-04
pjp
/*
275
2017-10-04
pjp
* assumption: fd got sent if sendmsg sent anything
276
2017-10-04
pjp
* this works because fds are passed one at a time
277
2017-10-04
pjp
*/
278
2017-10-04
pjp
if (buf != NULL && buf->fd != -1) {
279
2017-10-04
pjp
close(buf->fd);
280
2017-10-04
pjp
buf->fd = -1;
281
2017-10-04
pjp
}
282
2017-10-04
pjp
283
2017-10-04
pjp
msgbuf_drain(msgbuf, n);
284
2017-10-04
pjp
285
2017-10-04
pjp
return (1);
286
2017-10-04
pjp
}
287
2017-10-04
pjp
288
2017-10-04
pjp
void
289
2017-10-04
pjp
ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
290
2017-10-04
pjp
{
291
2017-10-04
pjp
TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
292
2017-10-04
pjp
msgbuf->queued++;
293
2017-10-04
pjp
}
294
2017-10-04
pjp
295
2017-10-04
pjp
void
296
2017-10-04
pjp
ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
297
2017-10-04
pjp
{
298
2017-10-04
pjp
TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
299
2017-10-04
pjp
300
2017-10-04
pjp
if (buf->fd != -1)
301
2017-10-04
pjp
close(buf->fd);
302
2017-10-04
pjp
303
2017-10-04
pjp
msgbuf->queued--;
304
2017-10-04
pjp
ibuf_free(buf);
305
2017-10-04
pjp
}
repomaster@centroid.eu