1 /* $OpenBSD: imsg.c,v 1.13 2015/12/09 11:54:12 tb 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.
21 #include <sys/types.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
33 int imsg_fd_overhead = 0;
35 int imsg_get_fd(struct imsgbuf *);
38 imsg_init(struct imsgbuf *ibuf, int fd)
40 msgbuf_init(&ibuf->w);
41 memset(&ibuf->r, 0, sizeof(ibuf->r));
45 TAILQ_INIT(&ibuf->fds);
49 imsg_read(struct imsgbuf *ibuf)
55 char buf[CMSG_SPACE(sizeof(int) * 1)];
62 memset(&msg, 0, sizeof(msg));
63 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
65 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
66 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
69 msg.msg_control = &cmsgbuf.buf;
70 msg.msg_controllen = sizeof(cmsgbuf.buf);
72 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
76 if (getdtablecount() + imsg_fd_overhead +
77 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
84 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
92 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
93 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
94 if (cmsg->cmsg_level == SOL_SOCKET &&
95 cmsg->cmsg_type == SCM_RIGHTS) {
100 * We only accept one file descriptor. Due to C
101 * padding rules, our control buffer might contain
102 * more than one fd, and we must close them.
104 j = ((char *)cmsg + cmsg->cmsg_len -
105 (char *)CMSG_DATA(cmsg)) / sizeof(int);
106 for (i = 0; i < j; i++) {
107 fd = ((int *)CMSG_DATA(cmsg))[i];
110 TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
117 /* we do not handle other ctl data level */
126 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
128 size_t av, left, datalen;
132 if (IMSG_HEADER_SIZE > av)
135 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
136 if (imsg->hdr.len < IMSG_HEADER_SIZE ||
137 imsg->hdr.len > MAX_IMSGSIZE) {
141 if (imsg->hdr.len > av)
143 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
144 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
147 else if ((imsg->data = malloc(datalen)) == NULL)
150 if (imsg->hdr.flags & IMSGF_HASFD)
151 imsg->fd = imsg_get_fd(ibuf);
155 memcpy(imsg->data, ibuf->r.rptr, datalen);
157 if (imsg->hdr.len < av) {
158 left = av - imsg->hdr.len;
159 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
164 return (datalen + IMSG_HEADER_SIZE);
168 imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
169 pid_t pid, int fd, const void *data, u_int16_t datalen)
173 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
176 if (imsg_add(wbuf, data, datalen) == -1)
181 imsg_close(ibuf, wbuf);
187 imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
188 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
193 for (i = 0; i < iovcnt; i++)
194 datalen += iov[i].iov_len;
196 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
199 for (i = 0; i < iovcnt; i++)
200 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
205 imsg_close(ibuf, wbuf);
212 imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
213 pid_t pid, u_int16_t datalen)
218 datalen += IMSG_HEADER_SIZE;
219 if (datalen > MAX_IMSGSIZE) {
227 if ((hdr.pid = pid) == 0)
229 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
232 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
239 imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
242 if (ibuf_add(msg, data, datalen) == -1) {
250 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
252 struct imsg_hdr *hdr;
254 hdr = (struct imsg_hdr *)msg->buf;
256 hdr->flags &= ~IMSGF_HASFD;
258 hdr->flags |= IMSGF_HASFD;
260 hdr->len = (u_int16_t)msg->wpos;
262 ibuf_close(&ibuf->w, msg);
266 imsg_free(struct imsg *imsg)
272 imsg_get_fd(struct imsgbuf *ibuf)
277 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
281 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
288 imsg_flush(struct imsgbuf *ibuf)
290 while (ibuf->w.queued)
291 if (msgbuf_write(&ibuf->w) <= 0)
297 imsg_clear(struct imsgbuf *ibuf)
301 msgbuf_clear(&ibuf->w);
302 while ((fd = imsg_get_fd(ibuf)) != -1)