1 /* $OpenBSD: imsg-buffer.c,v 1.11 2017/12/14 09:27:44 kettenis Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
29 #include "got_compat.h"
31 static int ibuf_realloc(struct ibuf
*, size_t);
32 static void ibuf_enqueue(struct msgbuf
*, struct ibuf
*);
33 static void ibuf_dequeue(struct msgbuf
*, struct ibuf
*);
40 if ((buf
= calloc(1, sizeof(struct ibuf
))) == NULL
)
42 if ((buf
->buf
= malloc(len
)) == NULL
) {
46 buf
->size
= buf
->max
= len
;
53 ibuf_dynamic(size_t len
, size_t max
)
60 if ((buf
= ibuf_open(len
)) == NULL
)
70 ibuf_realloc(struct ibuf
*buf
, size_t len
)
74 /* on static buffers max is eq size and so the following fails */
75 if (buf
->wpos
+ len
> buf
->max
) {
80 b
= recallocarray(buf
->buf
, buf
->size
, buf
->wpos
+ len
, 1);
84 buf
->size
= buf
->wpos
+ len
;
90 ibuf_add(struct ibuf
*buf
, const void *data
, size_t len
)
92 if (buf
->wpos
+ len
> buf
->size
)
93 if (ibuf_realloc(buf
, len
) == -1)
96 memcpy(buf
->buf
+ buf
->wpos
, data
, len
);
102 ibuf_reserve(struct ibuf
*buf
, size_t len
)
106 if (buf
->wpos
+ len
> buf
->size
)
107 if (ibuf_realloc(buf
, len
) == -1)
110 b
= buf
->buf
+ buf
->wpos
;
116 ibuf_seek(struct ibuf
*buf
, size_t pos
, size_t len
)
118 /* only allowed to seek in already written parts */
119 if (pos
+ len
> buf
->wpos
)
122 return (buf
->buf
+ pos
);
126 ibuf_size(struct ibuf
*buf
)
132 ibuf_left(struct ibuf
*buf
)
134 return (buf
->max
- buf
->wpos
);
138 ibuf_close(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
140 ibuf_enqueue(msgbuf
, buf
);
144 ibuf_write(struct msgbuf
*msgbuf
)
146 struct iovec iov
[IOV_MAX
];
151 memset(&iov
, 0, sizeof(iov
));
152 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
155 iov
[i
].iov_base
= buf
->buf
+ buf
->rpos
;
156 iov
[i
].iov_len
= buf
->wpos
- buf
->rpos
;
161 if ((n
= writev(msgbuf
->fd
, iov
, i
)) == -1) {
164 if (errno
== ENOBUFS
)
169 if (n
== 0) { /* connection closed */
174 msgbuf_drain(msgbuf
, n
);
180 ibuf_free(struct ibuf
*buf
)
184 freezero(buf
->buf
, buf
->size
);
189 msgbuf_init(struct msgbuf
*msgbuf
)
193 TAILQ_INIT(&msgbuf
->bufs
);
197 msgbuf_drain(struct msgbuf
*msgbuf
, size_t n
)
199 struct ibuf
*buf
, *next
;
201 for (buf
= TAILQ_FIRST(&msgbuf
->bufs
); buf
!= NULL
&& n
> 0;
203 next
= TAILQ_NEXT(buf
, entry
);
204 if (buf
->rpos
+ n
>= buf
->wpos
) {
205 n
-= buf
->wpos
- buf
->rpos
;
206 ibuf_dequeue(msgbuf
, buf
);
215 msgbuf_clear(struct msgbuf
*msgbuf
)
219 while ((buf
= TAILQ_FIRST(&msgbuf
->bufs
)) != NULL
)
220 ibuf_dequeue(msgbuf
, buf
);
224 msgbuf_write(struct msgbuf
*msgbuf
)
226 struct iovec iov
[IOV_MAX
];
231 struct cmsghdr
*cmsg
;
234 char buf
[CMSG_SPACE(sizeof(int))];
237 memset(&iov
, 0, sizeof(iov
));
238 memset(&msg
, 0, sizeof(msg
));
239 memset(&cmsgbuf
, 0, sizeof(cmsgbuf
));
240 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
243 iov
[i
].iov_base
= buf
->buf
+ buf
->rpos
;
244 iov
[i
].iov_len
= buf
->wpos
- buf
->rpos
;
253 if (buf
!= NULL
&& buf
->fd
!= -1) {
254 msg
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
255 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
256 cmsg
= CMSG_FIRSTHDR(&msg
);
257 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
258 cmsg
->cmsg_level
= SOL_SOCKET
;
259 cmsg
->cmsg_type
= SCM_RIGHTS
;
260 *(int *)CMSG_DATA(cmsg
) = buf
->fd
;
264 if ((n
= sendmsg(msgbuf
->fd
, &msg
, 0)) == -1) {
267 if (errno
== ENOBUFS
)
272 if (n
== 0) { /* connection closed */
278 * assumption: fd got sent if sendmsg sent anything
279 * this works because fds are passed one at a time
281 if (buf
!= NULL
&& buf
->fd
!= -1) {
286 msgbuf_drain(msgbuf
, n
);
292 ibuf_enqueue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
294 TAILQ_INSERT_TAIL(&msgbuf
->bufs
, buf
, entry
);
299 ibuf_dequeue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
301 TAILQ_REMOVE(&msgbuf
->bufs
, buf
, entry
);