1 /* $OpenBSD: imsg.c,v 1.5 2013/12/26 17:32:33 eric Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/queue.h> 21 #include <sys/socket.h> 22 #include <sys/uio.h> 23 24 #include <errno.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include "imsg.h" 30 31 int imsg_fd_overhead = 0; 32 33 int imsg_get_fd(struct imsgbuf *); 34 35 void 36 imsg_init(struct imsgbuf *ibuf, int fd) 37 { 38 msgbuf_init(&ibuf->w); 39 bzero(&ibuf->r, sizeof(ibuf->r)); 40 ibuf->fd = fd; 41 ibuf->w.fd = fd; 42 ibuf->pid = getpid(); 43 TAILQ_INIT(&ibuf->fds); 44 } 45 46 ssize_t 47 imsg_read(struct imsgbuf *ibuf) 48 { 49 struct msghdr msg; 50 struct cmsghdr *cmsg; 51 union { 52 struct cmsghdr hdr; 53 char buf[CMSG_SPACE(sizeof(int) * 1)]; 54 } cmsgbuf; 55 struct iovec iov; 56 ssize_t n = -1; 57 int fd; 58 struct imsg_fd *ifd; 59 60 bzero(&msg, sizeof(msg)); 61 62 iov.iov_base = ibuf->r.buf + ibuf->r.wpos; 63 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos; 64 msg.msg_iov = &iov; 65 msg.msg_iovlen = 1; 66 msg.msg_control = &cmsgbuf.buf; 67 msg.msg_controllen = sizeof(cmsgbuf.buf); 68 69 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL) 70 return (-1); 71 72 again: 73 if (getdtablecount() + imsg_fd_overhead + 74 (CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int) 75 >= getdtablesize()) { 76 errno = EAGAIN; 77 free(ifd); 78 return (-1); 79 } 80 81 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) { 82 if (errno == EMSGSIZE) 83 goto fail; 84 if (errno != EINTR && errno != EAGAIN) 85 goto fail; 86 goto again; 87 } 88 89 ibuf->r.wpos += n; 90 91 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 92 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 93 if (cmsg->cmsg_level == SOL_SOCKET && 94 cmsg->cmsg_type == SCM_RIGHTS) { 95 int i; 96 int j; 97 98 /* 99 * We only accept one file descriptor. Due to C 100 * padding rules, our control buffer might contain 101 * more than one fd, and we must close them. 102 */ 103 j = ((char *)cmsg + cmsg->cmsg_len - 104 (char *)CMSG_DATA(cmsg)) / sizeof(int); 105 for (i = 0; i < j; i++) { 106 fd = ((int *)CMSG_DATA(cmsg))[i]; 107 if (ifd != NULL) { 108 ifd->fd = fd; 109 TAILQ_INSERT_TAIL(&ibuf->fds, ifd, 110 entry); 111 ifd = NULL; 112 } else 113 close(fd); 114 } 115 } 116 /* we do not handle other ctl data level */ 117 } 118 119 fail: 120 if (ifd) 121 free(ifd); 122 return (n); 123 } 124 125 ssize_t 126 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) 127 { 128 size_t av, left, datalen; 129 130 av = ibuf->r.wpos; 131 132 if (IMSG_HEADER_SIZE > av) 133 return (0); 134 135 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr)); 136 if (imsg->hdr.len < IMSG_HEADER_SIZE || 137 imsg->hdr.len > MAX_IMSGSIZE) { 138 errno = ERANGE; 139 return (-1); 140 } 141 if (imsg->hdr.len > av) 142 return (0); 143 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 144 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; 145 if ((imsg->data = malloc(datalen)) == NULL) 146 return (-1); 147 148 if (imsg->hdr.flags & IMSGF_HASFD) 149 imsg->fd = imsg_get_fd(ibuf); 150 else 151 imsg->fd = -1; 152 153 memcpy(imsg->data, ibuf->r.rptr, datalen); 154 155 if (imsg->hdr.len < av) { 156 left = av - imsg->hdr.len; 157 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left); 158 ibuf->r.wpos = left; 159 } else 160 ibuf->r.wpos = 0; 161 162 return (datalen + IMSG_HEADER_SIZE); 163 } 164 165 int 166 imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid, 167 pid_t pid, int fd, const void *data, u_int16_t datalen) 168 { 169 struct ibuf *wbuf; 170 171 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) 172 return (-1); 173 174 if (imsg_add(wbuf, data, datalen) == -1) 175 return (-1); 176 177 wbuf->fd = fd; 178 179 imsg_close(ibuf, wbuf); 180 181 return (1); 182 } 183 184 int 185 imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid, 186 pid_t pid, int fd, const struct iovec *iov, int iovcnt) 187 { 188 struct ibuf *wbuf; 189 int i, datalen = 0; 190 191 for (i = 0; i < iovcnt; i++) 192 datalen += iov[i].iov_len; 193 194 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) 195 return (-1); 196 197 for (i = 0; i < iovcnt; i++) 198 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1) 199 return (-1); 200 201 wbuf->fd = fd; 202 203 imsg_close(ibuf, wbuf); 204 205 return (1); 206 } 207 208 /* ARGSUSED */ 209 struct ibuf * 210 imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid, 211 pid_t pid, u_int16_t datalen) 212 { 213 struct ibuf *wbuf; 214 struct imsg_hdr hdr; 215 216 datalen += IMSG_HEADER_SIZE; 217 if (datalen > MAX_IMSGSIZE) { 218 errno = ERANGE; 219 return (NULL); 220 } 221 222 hdr.type = type; 223 hdr.flags = 0; 224 hdr.peerid = peerid; 225 if ((hdr.pid = pid) == 0) 226 hdr.pid = ibuf->pid; 227 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) { 228 return (NULL); 229 } 230 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1) 231 return (NULL); 232 233 return (wbuf); 234 } 235 236 int 237 imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen) 238 { 239 if (datalen) 240 if (ibuf_add(msg, data, datalen) == -1) { 241 ibuf_free(msg); 242 return (-1); 243 } 244 return (datalen); 245 } 246 247 void 248 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg) 249 { 250 struct imsg_hdr *hdr; 251 252 hdr = (struct imsg_hdr *)msg->buf; 253 254 hdr->flags &= ~IMSGF_HASFD; 255 if (msg->fd != -1) 256 hdr->flags |= IMSGF_HASFD; 257 258 hdr->len = (u_int16_t)msg->wpos; 259 260 ibuf_close(&ibuf->w, msg); 261 } 262 263 void 264 imsg_free(struct imsg *imsg) 265 { 266 free(imsg->data); 267 } 268 269 int 270 imsg_get_fd(struct imsgbuf *ibuf) 271 { 272 int fd; 273 struct imsg_fd *ifd; 274 275 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL) 276 return (-1); 277 278 fd = ifd->fd; 279 TAILQ_REMOVE(&ibuf->fds, ifd, entry); 280 free(ifd); 281 282 return (fd); 283 } 284 285 int 286 imsg_flush(struct imsgbuf *ibuf) 287 { 288 while (ibuf->w.queued) 289 if (msgbuf_write(&ibuf->w) < 0) 290 return (-1); 291 return (0); 292 } 293 294 void 295 imsg_clear(struct imsgbuf *ibuf) 296 { 297 int fd; 298 299 msgbuf_clear(&ibuf->w); 300 while ((fd = imsg_get_fd(ibuf)) != -1) 301 close(fd); 302 } 303