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