Commit Diff


commit - 0816b8e449ec4d3f96b82be1ee8f52f6d96889cf
commit + 398fc78f4423d68f4d01eee672753c98b1cb75c8
blob - 2192cdbe338a9f1a568a28f49826acc4f6173fa9
blob + 05681d2c7271a63392dc82236d9b5a1aacae7696
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -31,31 +31,84 @@
 #include "proc.h"
 #include "thingsd.h"
 
+#define FD_RESERVE		5
+#define FD_NEEDED		6
+
+int cgi_inflight = 0;
+
+int		 accept_reserve(int, struct sockaddr *, socklen_t *, int,
+		    volatile int *);
 void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *);
 
+int
+accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+    int reserve, volatile int *counter)
+{
+	int ret;
+	if (getdtablecount() + reserve +
+	    ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
+		log_debug("inflight fds exceeded");
+		errno = EMFILE;
+		return -1;
+	}
+
+	if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC))
+	    > -1) {
+		(*counter)++;
+		log_debug("inflight incremented, now %d", *counter);
+	}
+	return ret;
+}
+
 void
+client_paused(int fd, short events, void *arg)
+{
+	struct thingsd		*env = arg;
+	event_add(env->ev, NULL);
+}
+
+void
 client_conn(int fd, short event, void *arg)
 {
 	struct sockaddr_storage	 ss;
-	struct client		*client;
+	struct timeval		 backoff;
+	struct client		*client = NULL;
 	struct thingsd		*env = (struct thingsd *)arg;
 	struct socket		*sock = NULL;
 	int			 client_fd;
 	socklen_t		 len = sizeof(ss);
 
-	if ((client_fd = accept4(fd, (struct sockaddr *)&ss, &len,
-	    SOCK_NONBLOCK)) == -1) {
-		log_warnx("client accept failed");
-		return;
+	backoff.tv_sec = 1;
+	backoff.tv_usec = 0;
+
+	client_fd = accept_reserve(fd, (struct sockaddr *)&ss, &len, FD_RESERVE,
+	    &cgi_inflight);
+	if (client_fd == -1) {
+		switch (errno) {
+		case EINTR:
+		case EWOULDBLOCK:
+		case ECONNABORTED:
+			return;
+		case EMFILE:
+		case ENFILE:
+			event_del(env->ev);
+			evtimer_add(&env->pause, &backoff);
+			return;
+		default:
+			log_warnx("client accept failed");
+		}
 	}
 
-	if ((client = calloc(1, sizeof(*client))) == NULL)
+	client = calloc(1, sizeof(*client));
+	if (client == NULL)
 		goto err;
 
-	if ((client->ev = calloc(1, sizeof(*client->ev))) == NULL)
+	client->ev = calloc(1, sizeof(*client->ev));
+	if (client->ev == NULL)
 		goto err;
 
-	if ((sock = get_socket(env, fd)) == NULL)
+	sock = get_socket(env, fd);
+	if (sock == NULL)
 		goto err;
 
 	if (sock->tls) {
@@ -116,6 +169,7 @@ client_conn(int fd, short event, void *arg)
 	return;
 err:
 	log_debug("%s: client error", __func__);
+	cgi_inflight--;
 	if (client_fd != -1)
 		close(client_fd);
 	if (sock->tls)
@@ -227,10 +281,12 @@ client_rd(struct bufferevent *bev, void *arg)
 
 	if (client->subscribed == false) {
 		/* allow one shot at subscription so we don't get hammered */
-		if ((pkt = calloc(len, sizeof(*pkt))) == NULL)
+		pkt = calloc(len, sizeof(*pkt));
+		if (pkt == NULL)
 			return;
 
-		if ((npkt = calloc(len, sizeof(*npkt))) == NULL)
+		npkt = calloc(len, sizeof(*npkt));
+		if (npkt == NULL)
 			return;
 
 		bufferevent_disable(client->bev, EV_READ);
@@ -334,15 +390,17 @@ client_wr_things(struct client *client, struct thing *
 {
 	char			*pkt;
 
-	if ((pkt = calloc(len, sizeof(*pkt))) == NULL)
+	pkt = calloc(len, sizeof(*pkt));
+	if (pkt == NULL)
 		return;
 
 	switch (thing->type) {
 	case TCP:
 	case DEV:
 		if (thing->persist == false) {
-			if ((thing->fd = open_client_socket(thing->ipaddr,
-			    thing->conn_port)) == -1) {
+			thing->fd = open_client_socket(thing->ipaddr,
+			    thing->conn_port);
+			if (thing->fd == -1) {
 				log_warnx("%s: temporary ipaddr connection"
 				    " failed", __func__);
 				return;
blob - 3290eeff0e201ca3f0dd6301e6b458bd83b5b821
blob + 9a2b42662d22f6d6f621ed64b320f6f3a474f1e3
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -90,7 +90,8 @@ control_dispatch_parent(int fd, struct privsep_proc *p
 	case IMSG_GET_INFO_CLIENTS_END_DATA:
 	case IMSG_GET_INFO_SOCKETS_DATA:
 	case IMSG_GET_INFO_SOCKETS_END_DATA:
-		if ((c = control_connbyfd(imsg->hdr.peerid)) == NULL) {
+		c = control_connbyfd(imsg->hdr.peerid);
+		if (c == NULL) {
 			log_warnx("%s: fd %d: not found",
 			    __func__, imsg->hdr.peerid);
 			return (-1);
@@ -123,7 +124,8 @@ control_init(struct privsep *ps, struct control_sock *
 	if (cs->cs_name == NULL)
 		return (0);
 
-	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
+	fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+	if (fd == -1) {
 		log_warn("%s: socket", __func__);
 		return (-1);
 	}
@@ -213,12 +215,13 @@ control_accept(int listenfd, short event, void *arg)
 	struct ctl_conn		*c;
 
 	event_add(&cs->cs_ev, NULL);
-	if ((event & EV_TIMEOUT))
+	if (event & EV_TIMEOUT)
 		return;
 
 	len = sizeof(sun);
-	if ((connfd = accept4(listenfd,
-	    (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) {
+	connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
+	    SOCK_NONBLOCK);
+	if (connfd == -1) {
 		/*
 		 * Pause accept if we are out of file descriptors, or
 		 * libevent will haunt us here too.
@@ -234,7 +237,8 @@ control_accept(int listenfd, short event, void *arg)
 		return;
 	}
 
-	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
+	c = calloc(1, sizeof(struct ctl_conn));
+	if (c == NULL) {
 		log_warn("%s", __func__);
 		close(connfd);
 		return;
@@ -280,7 +284,8 @@ control_close(struct privsep *ps, struct imsg *imsg, i
 {
 	struct ctl_conn	*c;
 
-	if ((c = control_connbyfd(fd)) == NULL) {
+	c = control_connbyfd(fd);
+	if (c == NULL) {
 		log_warn("%s: fd %d: not found", __func__, fd);
 		return;
 	}
@@ -322,14 +327,15 @@ control_dispatch_imsg(int fd, short event, void *arg)
 	if (getpeereid(fd, &euid, &egid) == -1)
 		return;
 
-	if ((c = control_connbyfd(fd)) == NULL) {
+	c = control_connbyfd(fd);
+	if (c == NULL) {
 		log_warn("%s: fd %d: not found", __func__, fd);
 		return;
 	}
 
 	if (event & EV_READ) {
-		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
-		    n == 0) {
+		n = imsg_read(&c->iev.ibuf);
+		if ((n == -1 && errno != EAGAIN) || n == 0) {
 			control_close(ps, &imsg, fd, cs);
 			return;
 		}
@@ -343,7 +349,8 @@ control_dispatch_imsg(int fd, short event, void *arg)
 	}
 
 	for (;;) {
-		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
+		n = imsg_get(&c->iev.ibuf, &imsg);
+		if (n == -1) {
 			control_close(ps, &imsg, fd, cs);
 			return;
 		}
blob - 61a694204bd6e8867f54b4e916d78849b14ae2e1
blob + 0f7784f5e9374d911a44d26414999c6ba3dc4042
--- thingsd/parse.y	
+++ thingsd/parse.y	
@@ -179,7 +179,8 @@ dosub		: SUBSCRIBE '{' optnl subopts '}'
 include		: INCLUDE STRING		{
 			struct file	*nfile;
 
-			if ((nfile = pushfile($2, 1)) == NULL) {
+			nfile = pushfile($2, 1);
+			if (nfile == NULL) {
 				yyerror("failed to include file %s", $2);
 				free($2);
 				YYERROR;
@@ -846,7 +847,8 @@ lgetc(int quotec)
 			"quoted string");
 			return (-1);
 		}
-		if ((c = getc(file->stream)) == EOF) {
+		c = getc(file->stream);
+		if (c == EOF) {
 			yyerror("reached end of file while parsing "
 			    "quoted string");
 			if (file == topfile || popfile() == EOF)
@@ -936,7 +938,8 @@ top:
 			; /* nothing */
 	if (c == '$' && parsebuf == NULL) {
 		while (1) {
-			if ((c = lgetc(0)) == EOF)
+			c = lgetc(0);
+			if (c == EOF)
 				return (0);
 
 			if (p + 1 >= buf + sizeof(buf) - 1) {
@@ -966,13 +969,15 @@ top:
 	case '"':
 		quotec = c;
 		while (1) {
-			if ((c = lgetc(quotec)) == EOF)
+			c = lgetc(quotec);
+			if (c == EOF)
 				return (0);
 			if (c == '\n') {
 				file->lineno++;
 				continue;
 			} else if (c == '\\') {
-				if ((next = lgetc(quotec)) == EOF)
+				next = lgetc(quotec);
+				if (next == EOF)
 					return (0);
 				if (next == quotec || c == ' ' || c == '\t')
 					c = next;
@@ -1052,9 +1057,12 @@ nodigits:
 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
 		lungetc(c);
 		*p = '\0';
-		if ((token = lookup(buf)) == STRING)
-			if ((yylval.v.string = strdup(buf)) == NULL)
+		token = lookup(buf);
+		if (token == STRING) {
+			yylval.v.string = strdup(buf);
+			if (yylval.v.string == NULL)
 				err(1, "yylex: strdup");
+		}
 		return (token);
 	}
 	if (c == '\n') {
@@ -1091,16 +1099,19 @@ pushfile(const char *name, int secret)
 {
 	struct file	*nfile;
 
-	if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
+	nfile = calloc(1, sizeof(struct file));
+	if (nfile == NULL) {
 		log_warn("calloc");
 		return (NULL);
 	}
-	if ((nfile->name = strdup(name)) == NULL) {
+	nfile->name = strdup(name);
+	if (nfile->name == NULL) {
 		log_warn("strdup");
 		free(nfile);
 		return (NULL);
 	}
-	if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
+	nfile->stream = fopen(nfile->name, "r");
+	if (nfile->stream == NULL) {
 		log_warn("%s", nfile->name);
 		free(nfile->name);
 		free(nfile);
@@ -1122,16 +1133,19 @@ pushbuff(u_char *pkt)
 {
 	struct file	*bfile;
 
-	if ((bfile = calloc(1, sizeof(struct file))) == NULL) {
+	bfile = calloc(1, sizeof(struct file));
+	if (bfile == NULL) {
 		log_warn("calloc");
 		return (NULL);
 	}
-	if ((bfile->name = strdup("subscribe buffer")) == NULL) {
+	bfile->name = strdup("subscribe buffer");
+	if (bfile->name == NULL) {
 		log_warn("strdup");
 		free(bfile);
 		return (NULL);
 	}
-	if ((parsebuf = pkt) == NULL) {
+	parsebuf = pkt;
+	if (parsebuf == NULL) {
 		log_warn("%s", bfile->name);
 		free(bfile->name);
 		free(bfile);
@@ -1147,7 +1161,8 @@ popfile(void)
 {
 	struct file	*prev;
 
-	if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
+	prev = TAILQ_PREV(file, files, entry);
+	if (prev != NULL)
 		prev->errors += file->errors;
 
 	TAILQ_REMOVE(&files, file, entry);
@@ -1163,7 +1178,8 @@ popbuff(void)
 {
 	struct file	*prev;
 
-	if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
+	prev = TAILQ_PREV(file, files, entry);
+	if (prev != NULL)
 		prev->errors += file->errors;
 	TAILQ_REMOVE(&files, file, entry);
 	free(file->name);
@@ -1232,7 +1248,8 @@ symset(const char *nam, const char *val, int persist)
 			free(sym);
 		}
 	}
-	if ((sym = calloc(1, sizeof(*sym))) == NULL)
+	sym = calloc(1, sizeof(*sym));
+	if (sym == NULL)
 		return (-1);
 
 	sym->nam = strdup(nam);
@@ -1259,11 +1276,13 @@ cmdline_symset(char *s)
 	int	 ret;
 	size_t	 len;
 
-	if ((val = strrchr(s, '=')) == NULL)
+	val = strrchr(s, '=');
+	if (val == NULL)
 		return (-1);
 
 	len = strlen(s) - strlen(val) + 1;
-	if ((sym = malloc(len)) == NULL)
+	sym = malloc(len);
+	if (sym == NULL)
 		fatal("%s: malloc", __func__);
 
 	strlcpy(sym, s, len);
@@ -1295,7 +1314,8 @@ parse_buf(struct client *pclient, u_char *pkt, int len
 	tclient = pclient;
 	my_fd = tclient->fd;
 
-	if ((file = pushbuff(pkt)) == NULL)
+	file = pushbuff(pkt);
+	if (file == NULL)
 		return (-1);
 
 	topfile = file;
blob - fc65e060564fdddd45bc820eebc9784288adab06
blob + 7198fd4583a422df5879316f7ed86a8f4f315d0f
--- thingsd/proc.c	
+++ thingsd/proc.c	
@@ -303,8 +303,9 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 
 		id = procs[src].p_id;
 		ps->ps_title[id] = procs[src].p_title;
-		if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
-		    sizeof(struct imsgev))) == NULL)
+		ps->ps_ievs[id] = calloc(ps->ps_instances[id],
+		    sizeof(struct imsgev));
+		if (ps->ps_ievs[id] == NULL)
 			fatal("%s: calloc", __func__);
 
 		/* With this set up, we are ready to call imsg_init(). */
@@ -331,8 +332,9 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 	 */
 	for (src = 0; src < PROC_MAX; src++) {
 		/* Allocate destination array for each process */
-		if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
-		    sizeof(struct privsep_pipes))) == NULL)
+		ps->ps_pipes[src] = calloc(ps->ps_instances[src],
+		    sizeof(struct privsep_pipes));
+		if (ps->ps_pipes[src] == NULL)
 			fatal("%s: calloc", __func__);
 
 		for (i = 0; i < ps->ps_instances[src]; i++) {
@@ -340,9 +342,10 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 
 			for (dst = 0; dst < PROC_MAX; dst++) {
 				/* Allocate maximum fd integers */
-				if ((pp->pp_pipes[dst] =
+				pp->pp_pipes[dst] =
 				    calloc(ps->ps_instances[dst],
-				    sizeof(int))) == NULL)
+				    sizeof(int));
+				if (pp->pp_pipes[dst] == NULL)
 					fatal("%s: calloc", __func__);
 
 				/* Mark fd as unused */
@@ -613,7 +616,8 @@ proc_dispatch(int fd, short event, void *arg)
 	ibuf = &iev->ibuf;
 
 	if (event & EV_READ) {
-		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
+		n = imsg_read(ibuf);
+		if (n == -1 && errno != EAGAIN)
 			fatal("%s: imsg_read", __func__);
 		if (n == 0) {
 			/* this pipe is dead, so remove the event handler */
@@ -624,7 +628,8 @@ proc_dispatch(int fd, short event, void *arg)
 	}
 
 	if (event & EV_WRITE) {
-		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
+		n = msgbuf_write(&ibuf->w);
+		if (n == -1 && errno != EAGAIN)
 			fatal("%s: msgbuf_write", __func__);
 		if (n == 0) {
 			/* this pipe is dead, so remove the event handler */
@@ -635,7 +640,8 @@ proc_dispatch(int fd, short event, void *arg)
 	}
 
 	for (;;) {
-		if ((n = imsg_get(ibuf, &imsg)) == -1)
+		n = imsg_get(ibuf, &imsg);
+		if (n == -1)
 			fatal("%s: imsg_get", __func__);
 		if (n == 0)
 			break;
@@ -716,8 +722,8 @@ imsg_compose_event(struct imsgev *iev, uint16_t type, 
 {
 	int	ret;
 
-	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
-	    pid, fd, data, datalen)) == -1)
+	ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
+	if (ret == -1)
 		return (ret);
 	imsg_event_add(iev);
 	return (ret);
@@ -729,8 +735,8 @@ imsg_composev_event(struct imsgev *iev, uint16_t type,
 {
 	int	ret;
 
-	if ((ret = imsg_composev(&iev->ibuf, type, peerid,
-	    pid, fd, iov, iovcnt)) == -1)
+	ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
+	if (ret == -1)
 		return (ret);
 	imsg_event_add(iev);
 	return (ret);
@@ -829,7 +835,8 @@ proc_flush_imsg(struct privsep *ps, enum privsep_proci
 
 	proc_range(ps, id, &n, &m);
 	for (; n < m; n++) {
-		if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
+		ibuf = proc_ibuf(ps, id, n);
+		if (ibuf == NULL)
 			return (-1);
 		do {
 			ret = imsg_flush(ibuf);
blob - e8b8b663c4ea05736d3d9ae6b53745fcb41199a9
blob + 953b34a7ef182e2d9616a934f411d2dc3c378104
--- thingsd/serial.c	
+++ thingsd/serial.c	
@@ -213,7 +213,8 @@ open_things(struct thingsd *env, bool reconn)
 				/* Set the new options for the port */
 				tcsetattr(fd, TCSANOW, &s_opts);
 
-				if ((thing->fd = fd) == '\0') {
+				thing->fd = fd;
+				if (thing->fd == '\0') {
 					log_warnx("serial device not opened");
 					if (reconn)
 						return;
blob - c92c01472eb1709c6a30f6094074404ff37e8e9a
blob + f13e277731f08cfb512f6e7c8f64febfe52582dc
--- thingsd/sockets.c	
+++ thingsd/sockets.c	
@@ -90,15 +90,16 @@ create_sockets(struct thingsd *env, bool reconn)
 			thing->type = UDP;
 			conn_socket = new_socket(thing->rcv_port);
 
-			if ((csock = sock->fd = create_socket(thing->port,
-			    iface, TCP)) == -1)
+			csock = sock->fd = create_socket(thing->port,
+			    iface, TCP);
+			if (csock == -1)
 				log_warnx("udp create socket failed");
 			else
 				thing->exists = true;
 
-			if ((conn_csock = conn_socket->fd =
-			    create_socket(thing->rcv_port, iface,
-		 	    thing->type)) == -1)
+			conn_csock = conn_socket->fd =
+			    create_socket(thing->rcv_port, iface, thing->type);
+			if (conn_csock == -1)
 				log_warnx("udp create socket failed");
 			else
 				thing->exists = true;
@@ -110,9 +111,9 @@ recreate:
 			thing->type = TCP;
 
 			if (thing->persist == 1) {
-				if ((thing->fd =
-				    open_client_socket(thing->ipaddr,
-				    thing->conn_port)) == -1) {
+				thing->fd = open_client_socket(thing->ipaddr,
+				    thing->conn_port);
+				if (thing->fd == -1) {
 					log_warnx("ipaddr connection failed");
 					if (reconn)
 						continue;
@@ -141,9 +142,9 @@ recreate:
 				thing->fd = -1;
 
 			if (reconn == false) {
-				if ((csock = sock->fd =
-				    create_socket(thing->port, iface,
-				    TCP)) == -1) {
+				csock = sock->fd = create_socket(thing->port,
+				    iface, TCP);
+				if (csock == -1) {
 					log_warnx("tcp create socket failed");
 					thing->exists = false;
 					add_reconn(thing);
@@ -191,6 +192,7 @@ recreate:
 			    client_conn, env);
 			if (event_add(sock->ev, NULL))
 				fatalx("event add sock");
+			evtimer_set(&env->pause, client_paused, env);
 		}
 
 		if (conn_socket != NULL && conn_socket->fd > 0) {
@@ -226,13 +228,15 @@ create_socket(int iport, char *iface, int type)
 		addr_hints.ai_flags |= AI_PASSIVE;
 		break;
 	}
-	if ((gai = getaddrinfo(iface, port, &addr_hints, &addr_res)) != 0)
+	gai = getaddrinfo(iface, port, &addr_hints, &addr_res);
+	if (gai != 0)
 		fatalx("getaddrinfo failed on %s: %s:%s", gai_strerror(gai),
 		    iface, port);
 	for (loop_res = addr_res; loop_res != NULL;
 	    loop_res = loop_res->ai_next) {
-		if ((socket_fd = socket(loop_res->ai_family,
-		    loop_res->ai_socktype, loop_res->ai_protocol)) == -1)
+		socket_fd = socket(loop_res->ai_family, loop_res->ai_socktype,
+		    loop_res->ai_protocol);
+		if (socket_fd == -1)
 			fatalx("unable to create socket");
 
 		if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &o_val,
@@ -383,10 +387,12 @@ new_socket(int port)
 {
 	struct socket		*sock;
 
-	if ((sock = calloc(1, sizeof(*sock))) == NULL)
+	sock = calloc(1, sizeof(*sock));
+	if (sock == NULL)
 		fatal("%s: calloc", __func__);
 
-	if ((sock->ev = calloc(1, sizeof(*sock->ev))) == NULL)
+	sock->ev = calloc(1, sizeof(*sock->ev));
+	if (sock->ev == NULL)
 		fatal("%s: calloc", __func__);
 
 	sock->port = port;
@@ -426,7 +432,8 @@ socket_rd(struct bufferevent *bev, void *arg)
 
 			len = EVBUFFER_LENGTH(thing->evb);
 
-			if ((pkt = calloc(len, sizeof(*pkt))) == NULL)
+			pkt = calloc(len, sizeof(*pkt));
+			if (pkt == NULL)
 				return;
 
 			evbuffer_remove(thing->evb, pkt, len);
blob - 2b106528c37ad2b020d2fd97d4b3acca7ad1bdef
blob + 60451fa49b6f1788316925cc8e4c40f771d91804
--- thingsd/things.c	
+++ thingsd/things.c	
@@ -559,12 +559,14 @@ send_to_packet_client(struct thingsd *env, char *name,
 	int			 snm;
 
 	TAILQ_FOREACH(packet_client, env->packet_clients, entry) {
-		if (strlen(packet_client->name) != 0)
-			if ((snm = strcmp(name, packet_client->name)) == 0)
+		if (strlen(packet_client->name) != 0) {
+			snm = strcmp(name, packet_client->name);
+			if (snm == 0)
 				if (proc_compose_imsg(&packet_client->ps,
 				    PROC_CONTROL, -1, IMSG_SHOW_PACKETS_DATA,
 				    packet_client->imsg.hdr.peerid, -1,
 				    pkt, len) == -1)
 				return;
+		}
 	}
 }
\ No newline at end of file
blob - 113111a6377399b66886c38e177bd6e03694d1bc
blob + ea9b40bf2f9f2bea439ce1701818c3f638cef42e
--- thingsd/thingsd.c	
+++ thingsd/thingsd.c	
@@ -232,7 +232,8 @@ main(int argc, char **argv)
 	/* log to stderr until daemonized */
 	log_init(1, LOG_DAEMON);
 
-	if ((env = calloc(1, sizeof(*env))) == NULL)
+	env = calloc(1, sizeof(*env));
+	if (env == NULL)
 		fatal("%s: calloc", __func__);
 
 	thingsd_env = env;
@@ -292,7 +293,8 @@ main(int argc, char **argv)
 	if (config_init(env) == -1)
 		fatal("failed to initialize configuration");
 
-	if ((ps->ps_pw = getpwnam(THINGSD_USER)) == NULL)
+	ps->ps_pw = getpwnam(THINGSD_USER);
+	if (ps->ps_pw == NULL)
 		fatal("unknown user %s", THINGSD_USER);
 
 	env->thingsd_conffile = conffile;
blob - ba18c57d38933af62a1cd50a04e2e147368704b8
blob + 55e7ccb4360bb4bd9dac562a9a09c4d4e1530f79
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -102,8 +102,8 @@ struct thingsd_parent_info {
 
 struct client {
 	TAILQ_ENTRY(client)	 entry;
-	struct event		*ev;
 	struct evbuffer		*evb;
+	struct event		*ev;
 	struct bufferevent	*bev;
 	struct socket		*socket;
 	bool			 subscribed;
@@ -250,6 +250,10 @@ struct thingsd {
 	/* control packets */
 	struct packetclientlist	*packet_clients;
 	int			 packet_client_count;
+
+	/* client accept */
+	struct event		*ev;
+	struct event		 pause;
 };
 
 extern struct thingsd	*thingsd_env;
@@ -271,6 +275,7 @@ void	 client_wr_things(struct client *, struct thing *
 void	 client_tls_readcb(int, short, void *);
 void	 client_tls_writecb(int, short, void *);
 void	 clients_show_info(struct privsep *, struct imsg *);
+void	 client_paused(int, short, void *);
 
 /* serial.c */
 void	 open_things(struct thingsd *, bool);
blob - 4c9d1b6da7a5e7a4ea1439f0bcf242d8a244282c
blob + f8d113e659d0be189908917f0112f0acdb2cc313
--- thingsd/tls.c	
+++ thingsd/tls.c	
@@ -44,15 +44,17 @@ tls_load_keypair(struct thing *thing)
 	if (thing->tls == false)
 		return (0);
 
-	if ((thing->tls_cert = tls_load_file(thing->tls_cert_file,
-	    &thing->tls_cert_len, NULL)) == NULL)
+	thing->tls_cert = tls_load_file(thing->tls_cert_file,
+	    &thing->tls_cert_len, NULL);
+	if (thing->tls_cert == NULL)
 		return (-1);
 
 	log_debug("%s: using certificate %s", __func__, thing->tls_cert_file);
 
 	/* XXX allow to specify password for encrypted key */
-	if ((thing->tls_key = tls_load_file(thing->tls_key_file,
-	    &thing->tls_key_len, NULL)) == NULL)
+	thing->tls_key = tls_load_file(thing->tls_key_file,
+	    &thing->tls_key_len, NULL);
+	if (thing->tls_key == NULL)
 		return (-1);
 
 	log_debug("%s: using private key %s", __func__, thing->tls_key_file);
@@ -67,8 +69,9 @@ tls_load_ca(struct thing *thing)
 	    strlen(thing->tls_ca_file) == 0)
 		return (0);
 
-	if ((thing->tls_ca = tls_load_file(thing->tls_ca_file,
-	    &thing->tls_ca_len, NULL)) == NULL)
+	thing->tls_ca = tls_load_file(thing->tls_ca_file,
+	    &thing->tls_ca_len, NULL);
+	if (thing->tls_ca == NULL)
 		return (-1);
 
 	log_debug("%s: using ca cert(s) from %s", __func__, thing->tls_ca_file);
@@ -85,9 +88,10 @@ tls_load_ocsp(struct thing *thing)
 	if (strlen(thing->tls_ocsp_staple_file) == 0)
 		return (0);
 
-	if ((thing->tls_ocsp_staple = tls_load_file(
+	thing->tls_ocsp_staple = tls_load_file(
 	    thing->tls_ocsp_staple_file,
-	    &thing->tls_ocsp_staple_len, NULL)) == NULL) {
+	    &thing->tls_ocsp_staple_len, NULL);
+	if (thing->tls_ocsp_staple == NULL) {
 		log_warnx("%s: Failed to load ocsp staple from %s", __func__,
 		    thing->tls_ocsp_staple_file);
 		return (-1);
@@ -112,8 +116,9 @@ tls_load_crl(struct thing *thing)
 	    strlen(thing->tls_crl_file) == 0)
 		return (0);
 
-	if ((thing->tls_crl = tls_load_file(thing->tls_crl_file,
-	    &thing->tls_crl_len, NULL)) == NULL)
+	thing->tls_crl = tls_load_file(thing->tls_crl_file,
+	    &thing->tls_crl_len, NULL);
+	if (thing->tls_crl == NULL)
 		return (-1);
 
 	log_debug("%s: using crl(s) from %s", __func__, thing->tls_crl_file);
@@ -141,12 +146,14 @@ socket_tls_init(struct socket *socket, struct thing *t
 		return (-1);
 	}
 
-	if ((socket->tls_config = tls_config_new()) == NULL) {
+	socket->tls_config = tls_config_new();
+	if (socket->tls_config == NULL) {
 		log_warnx("%s: failed to get tls config", __func__);
 		return (-1);
 	}
 
-	if ((socket->tls_ctx = tls_server()) == NULL) {
+	socket->tls_ctx = tls_server();
+	if (socket->tls_ctx == NULL) {
 		log_warnx("%s: failed to get tls server", __func__);
 		return (-1);
 	}