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/param.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
31 int buf_realloc(struct buf
*, size_t);
32 void buf_enqueue(struct msgbuf
*, struct buf
*);
33 void buf_dequeue(struct msgbuf
*, struct buf
*);
40 if ((buf
= calloc(1, sizeof(struct buf
))) == NULL
)
42 if ((buf
->buf
= malloc(len
)) == NULL
) {
46 buf
->size
= buf
->max
= len
;
53 buf_dynamic(size_t len
, size_t max
)
60 if ((buf
= buf_open(len
)) == NULL
)
70 buf_realloc(struct buf
*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
= realloc(buf
->buf
, buf
->wpos
+ len
);
84 buf
->size
= buf
->wpos
+ len
;
90 buf_add(struct buf
*buf
, const void *data
, size_t len
)
92 if (buf
->wpos
+ len
> buf
->size
)
93 if (buf_realloc(buf
, len
) == -1)
96 memcpy(buf
->buf
+ buf
->wpos
, data
, len
);
102 buf_reserve(struct buf
*buf
, size_t len
)
106 if (buf
->wpos
+ len
> buf
->size
)
107 if (buf_realloc(buf
, len
) == -1)
110 b
= buf
->buf
+ buf
->wpos
;
116 buf_seek(struct buf
*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 buf_size(struct buf
*buf
)
132 buf_left(struct buf
*buf
)
134 return (buf
->max
- buf
->wpos
);
138 buf_close(struct msgbuf
*msgbuf
, struct buf
*buf
)
140 buf_enqueue(msgbuf
, buf
);
144 buf_write(struct msgbuf
*msgbuf
)
146 struct iovec iov
[IOV_MAX
];
151 bzero(&iov
, 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
;
160 if ((n
= writev(msgbuf
->fd
, iov
, i
)) == -1) {
161 if (errno
== EAGAIN
|| errno
== ENOBUFS
||
162 errno
== EINTR
) /* try later */
168 if (n
== 0) { /* connection closed */
173 msgbuf_drain(msgbuf
, n
);
179 buf_free(struct buf
*buf
)
186 msgbuf_init(struct msgbuf
*msgbuf
)
190 TAILQ_INIT(&msgbuf
->bufs
);
194 msgbuf_drain(struct msgbuf
*msgbuf
, size_t n
)
196 struct buf
*buf
, *next
;
198 for (buf
= TAILQ_FIRST(&msgbuf
->bufs
); buf
!= NULL
&& n
> 0;
200 next
= TAILQ_NEXT(buf
, entry
);
201 if (buf
->rpos
+ n
>= buf
->wpos
) {
202 n
-= buf
->wpos
- buf
->rpos
;
203 buf_dequeue(msgbuf
, buf
);
212 msgbuf_clear(struct msgbuf
*msgbuf
)
216 while ((buf
= TAILQ_FIRST(&msgbuf
->bufs
)) != NULL
)
217 buf_dequeue(msgbuf
, buf
);
221 msgbuf_write(struct msgbuf
*msgbuf
)
223 struct iovec iov
[IOV_MAX
];
228 struct cmsghdr
*cmsg
;
231 char buf
[CMSG_SPACE(sizeof(int))];
234 bzero(&iov
, sizeof(iov
));
235 bzero(&msg
, sizeof(msg
));
236 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
239 iov
[i
].iov_base
= buf
->buf
+ buf
->rpos
;
240 iov
[i
].iov_len
= buf
->wpos
- buf
->rpos
;
249 if (buf
!= NULL
&& buf
->fd
!= -1) {
250 msg
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
251 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
252 cmsg
= CMSG_FIRSTHDR(&msg
);
253 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
254 cmsg
->cmsg_level
= SOL_SOCKET
;
255 cmsg
->cmsg_type
= SCM_RIGHTS
;
256 *(int *)CMSG_DATA(cmsg
) = buf
->fd
;
259 if ((n
= sendmsg(msgbuf
->fd
, &msg
, 0)) == -1) {
260 if (errno
== EAGAIN
|| errno
== ENOBUFS
||
261 errno
== EINTR
) /* try later */
267 if (n
== 0) { /* connection closed */
273 * assumption: fd got sent if sendmsg sent anything
274 * this works because fds are passed one at a time
276 if (buf
!= NULL
&& buf
->fd
!= -1) {
281 msgbuf_drain(msgbuf
, n
);
287 buf_enqueue(struct msgbuf
*msgbuf
, struct buf
*buf
)
289 TAILQ_INSERT_TAIL(&msgbuf
->bufs
, buf
, entry
);
294 buf_dequeue(struct msgbuf
*msgbuf
, struct buf
*buf
)
296 TAILQ_REMOVE(&msgbuf
->bufs
, buf
, entry
);