Commit Diff


commit - f13e3a2b334c49b82283aa98d7b0dcc3f9b990d6
commit + 70b4b935ca57bbd9ba5ea767d759579b251a6b67
blob - 74265491115e86d1cbdc5c274d91c6011ba93d38
blob + fc82ae778f297bd56c6fea165bbf6365cb2bc28b
--- thingsctl/thingsctl.c	
+++ thingsctl/thingsctl.c	
@@ -104,7 +104,8 @@ main(int argc, char *argv[])
 
 	if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
 		err(1, "%s: malloc", __func__);
-	imsg_init(ibuf, ctl_sock);
+	if (imsgbuf_init(ibuf, ctl_sock) == -1)
+		err(1, "%s: imsgbuf_init", __func__);
 	done = 0;
 
 	/* Check for root only actions */
@@ -186,12 +187,11 @@ main(int argc, char *argv[])
 		usage();
 	}
 
-	while (ibuf->w.queued)
-		if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN)
-			err(1, "write error");
+	if (imsgbuf_flush(ibuf) == -1)
+		err(1, "write error");
 
 	while (!done) {
-		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
+		if ((n = imsgbuf_read(ibuf)) == -1)
 			errx(1, "imsg_read error");
 		if (n == 0) {
 			if (res->action == SHOW_PACKETS)
@@ -202,8 +202,8 @@ main(int argc, char *argv[])
 		}
 
 		while (!done) {
-			if ((n = imsg_get(ibuf, &imsg)) == -1)
-				errx(1, "imsg_get error");
+			if ((n = imsgbuf_get(ibuf, &imsg)) == -1)
+				errx(1, "imsgbuf_get error");
 			if (n == 0)
 				break;
 
@@ -226,7 +226,7 @@ main(int argc, char *argv[])
 					done = 1;
 					break;
 				}
-				printf("%s\n", imsg.data);
+				printf("%s\n", (char *)imsg.data);
 				break;
 			case SHOW_THINGSD:
 				done = show_parent_msg(&imsg);
blob - f62feecd9602215ff84c048994a6f14d472d75ee
blob + 19cc2b7944e2e1ab2aa381ca7646b44f698218ae
--- thingsd/config.c	
+++ thingsd/config.c	
@@ -115,14 +115,11 @@ config_getsocks(struct thingsd *env, struct imsg *imsg
 	}
 
 	/* create a new socket */
-	if ((sock = calloc(1, sizeof(*sock))) == NULL) {
-		if (imsg->fd != -1)
-			close(imsg->fd);
+	if ((sock = calloc(1, sizeof(*sock))) == NULL)
 		return (-1);
-	}
 
 	memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
-	sock->fd = imsg->fd;
+	sock->fd = imsg_get_fd(imsg);
 
 	TAILQ_INSERT_TAIL(env->sockets, sock, entry);
 
blob - 0262a2e9c400f007defc806b1a8107c19cc28e49
blob + d7fa4f99c86b4ee296c19214dc9ec022480622b5
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -315,7 +315,12 @@ control_accept(int listenfd, short event, void *arg)
 		return;
 	}
 
-	imsg_init(&c->iev.ibuf, connfd);
+	if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) {
+		log_warn("%s: failed to init imsgbuf", __func__);
+		close(connfd);
+		free(c);
+		return;
+	}
 
 	c->iev.handler = control_dispatch_imsg;
 	c->iev.events = EV_READ;
@@ -358,7 +363,7 @@ control_close(struct privsep *ps, struct imsg *imsg, i
 	    &fd, sizeof(fd)) == -1)
 		log_warn("fd not sent to thingsd to close");
 
-	msgbuf_clear(&c->iev.ibuf.w);
+	imsgbuf_clear(&c->iev.ibuf);
 	TAILQ_REMOVE(&ctl_conns, c, entry);
 
 	event_del(&c->iev.ev);
@@ -397,22 +402,22 @@ control_dispatch_imsg(int fd, short event, void *arg)
 	}
 
 	if (event & EV_READ) {
-		n = imsg_read(&c->iev.ibuf);
-		if ((n == -1 && errno != EAGAIN) || n == 0) {
+		n = imsgbuf_read(&c->iev.ibuf);
+		if (n == -1 || n == 0) {
 			control_close(ps, &imsg, fd, cs);
 			return;
 		}
 	}
 
 	if (event & EV_WRITE) {
-		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
+		if (imsgbuf_write(&c->iev.ibuf) == -1) {
 			control_close(ps, &imsg, fd, cs);
 			return;
 		}
 	}
 
 	for (;;) {
-		n = imsg_get(&c->iev.ibuf, &imsg);
+		n = imsgbuf_get(&c->iev.ibuf, &imsg);
 		if (n == -1) {
 			control_close(ps, &imsg, fd, cs);
 			return;
@@ -505,7 +510,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
 fail:
 	imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
 	    0, 0, -1, &ret, sizeof(ret));
-	imsg_flush(&c->iev.ibuf);
+	imsgbuf_flush(&c->iev.ibuf);
 	control_close(ps, &imsg, fd, cs);
 }
 
blob - 9dac64172ce767e32c067439f0dd48911dc4cd8e
blob + 4417dd0e55b3c89efebb76345cd94e981c439a68
--- thingsd/proc.c	
+++ thingsd/proc.c	
@@ -183,7 +183,10 @@ proc_connect(struct privsep *ps)
 
 		for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
 			iev = &ps->ps_ievs[dst][inst];
-			imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
+			if (imsgbuf_init(&iev->ibuf,
+			    ps->ps_pp->pp_pipes[dst][inst]) == -1)
+				fatal(NULL);
+			imsgbuf_allow_fdpass(&iev->ibuf);
 			event_set(&iev->ev, iev->ibuf.fd, iev->events,
 			    iev->handler, iev->data);
 			event_add(&iev->ev, NULL);
@@ -289,7 +292,9 @@ proc_accept(struct privsep *ps, int fd, enum privsep_p
 		pp->pp_pipes[dst][n] = fd;
 
 	iev = &ps->ps_ievs[dst][n];
-	imsg_init(&iev->ibuf, fd);
+	if (imsgbuf_init(&iev->ibuf, fd) == -1)
+		fatal(NULL);
+	imsgbuf_allow_fdpass(&iev->ibuf);
 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
 	event_add(&iev->ev, NULL);
 }
@@ -474,7 +479,7 @@ proc_close(struct privsep *ps)
 
 			/* Cancel the fd, close and invalidate the fd */
 			event_del(&(ps->ps_ievs[dst][n].ev));
-			imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
+			imsgbuf_clear(&(ps->ps_ievs[dst][n].ibuf));
 			close(pp->pp_pipes[dst][n]);
 			pp->pp_pipes[dst][n] = -1;
 		}
@@ -616,8 +621,7 @@ proc_dispatch(int fd, short event, void *arg)
 	struct privsep		*ps = p->p_ps;
 	struct imsgbuf		*ibuf;
 	struct imsg		 imsg;
-	ssize_t			 n;
-	int			 verbose;
+	int			 n, verbose;
 	const char		*title;
 	struct privsep_fd	 pf;
 
@@ -625,7 +629,7 @@ proc_dispatch(int fd, short event, void *arg)
 	ibuf = &iev->ibuf;
 
 	if (event & EV_READ) {
-		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
+		if ((n = imsgbuf_read(ibuf)) == -1)
 			fatal("%s: imsg_read", __func__);
 		if (n == 0) {
 			/* this pipe is dead, so remove the event handler */
@@ -636,19 +640,20 @@ proc_dispatch(int fd, short event, void *arg)
 	}
 
 	if (event & EV_WRITE) {
-		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
+		if ((n = imsgbuf_write(ibuf)) == -1) {
+			if (errno == EPIPE) {
+				/* this pipe is dead */
+				event_del(&iev->ev);
+				event_loopexit(NULL);
+				return;
+			}
 			fatal("%s: msgbuf_write", __func__);
-		if (n == 0) {
-			/* this pipe is dead, so remove the event handler */
-			event_del(&iev->ev);
-			event_loopexit(NULL);
-			return;
 		}
 	}
 
 	for (;;) {
-		if ((n = imsg_get(ibuf, &imsg)) == -1)
-			fatal("%s: imsg_get", __func__);
+		if ((n = imsgbuf_get(ibuf, &imsg)) == -1)
+			fatal("%s: imsgbuf_get", __func__);
 		if (n == 0)
 			break;
 
@@ -679,7 +684,7 @@ proc_dispatch(int fd, short event, void *arg)
 		case IMSG_CTL_PROCFD:
 			IMSG_SIZE_CHECK(&imsg, &pf);
 			memcpy(&pf, imsg.data, sizeof(pf));
-			proc_accept(ps, imsg.fd, pf.pf_procid,
+			proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
 			    pf.pf_instance);
 			break;
 		default:
@@ -712,12 +717,12 @@ void
 imsg_event_add(struct imsgev *iev)
 {
 	if (iev->handler == NULL) {
-		imsg_flush(&iev->ibuf);
+		imsgbuf_flush(&iev->ibuf);
 		return;
 	}
 
 	iev->events = EV_READ;
-	if (iev->ibuf.w.queued)
+	if (imsgbuf_queuelen(&iev->ibuf) > 0)
 		iev->events |= EV_WRITE;
 
 	event_del(&iev->ev);
@@ -814,7 +819,8 @@ proc_forward_imsg(struct privsep *ps, struct imsg *ims
     enum privsep_procid id, int n)
 {
 	return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
-	    imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
+	    imsg->hdr.peerid, imsg_get_fd(imsg), imsg->data,
+	    IMSG_DATA_SIZE(imsg)));
 }
 
 struct imsgbuf *
@@ -847,7 +853,7 @@ proc_flush_imsg(struct privsep *ps, enum privsep_proci
 		if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
 			return (-1);
 		do {
-			ret = imsg_flush(ibuf);
+			ret = imsgbuf_flush(ibuf);
 		} while (ret == -1 && errno == EAGAIN);
 		if (ret == -1)
 			break;