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