Commit Diff


commit - 6c98745568f382c8a563faf0213ab1f03189d951
commit + 75d2b8dbff8739eff0d9310409bb002db207d83a
blob - 46b344e7a3bb2a0d5325c95e7b4f63e1aedbb90d
blob + 08d1fa2b4fafe1f44d32463fc6796cc78df54c4f
--- README.md	
+++ README.md	
@@ -41,6 +41,8 @@ Compile and install.
 
 		make
 		make install
+		doas addgroup _thingsd
+		doas useradd -d /var/empty -L daemon -g _thingsd -s /sbin/nologin _thingsd
 		doas cp examples/etc/rc.d/thingsd /etc/rc.d/
 
 Edit thingsd.conf according to `man thingsd.conf`. Your settings will depend
blob - ff61bf4c6d6317f427a08c26920d1842b186ebcc
blob + a79ac44a7d52cd95a9b11d4bcfaf615839d95dae
--- thingsctl/parser.c	
+++ thingsctl/parser.c	
@@ -70,11 +70,6 @@ static const struct token t_list[] = {
 	{ENDTOKEN,	"",		NONE,			NULL}
 };
 
-/* static const struct token t_list[] = { */
-/* 	{NOTOKEN,	"",		NONE,			NULL}, */
-/* 	{ENDTOKEN,	"",		LIST,			NULL} */
-/* }; */
-
 static const struct token *match_token(const char *, const struct token *,
     struct parse_result *);
 static void show_valid_args(const struct token *);
blob - 39b9fcb6a7ea1497aedc11da572c9cc3290ddfbc
blob + c431834a535ed93eceff7ef465bbc08c90e59e91
--- thingsctl/thingsctl.c	
+++ thingsctl/thingsctl.c	
@@ -126,24 +126,24 @@ main(int argc, char *argv[])
 		verbose++;
 		/* FALLTHROUGH */
 	case LOG_BRIEF:
-		imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
+		imsg_compose(ibuf, IMSG_THGS_LOG_VERBOSE, 0, 0, -1,
 		    &verbose, sizeof(verbose));
 		printf("logging request sent\n");
 		done = 1;
 		break;
 	case LIST_CLTS:
-		type = CTL_LIST_CLTS;
-		imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type,
+		type = THGS_LIST_CLTS;
+		imsg_compose(ibuf, IMSG_THGS_LIST, 0, 0, -1, &type,
 		    sizeof(type));
 		break;
 	case LIST_THGS:
-		type = CTL_LIST_THGS;
-		imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type,
+		type = THGS_LIST_THGS;
+		imsg_compose(ibuf, IMSG_THGS_LIST, 0, 0, -1, &type,
 		    sizeof(type));
 		break;
 	case LIST_SOCKS:
-		type = CTL_LIST_SOCKS;
-		imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type,
+		type = THGS_LIST_SOCKS;
+		imsg_compose(ibuf, IMSG_THGS_LIST, 0, 0, -1, &type,
 		    sizeof(type));
 		break;
 	default:
blob - e3f22a628d18b0b02294125a87602076bebddbeb
blob + 13a4d84d6750d6585953128112efa46e79bed093
--- thingsd/Makefile	
+++ thingsd/Makefile	
@@ -2,7 +2,7 @@ PROG=		thingsd
 
 SRCS=		parse.y
 SRCS+=		log.c serial.c sockets.c client.c events.c tls.c control.c
-SRCS+=		ctl.c
+SRCS+=		things.c
 
 SRCS+=		thingsd.c
 
blob - 46fea3c02773e70d25df53948ad60eca18459954
blob + e80488d96a7455a2ec5df9e02ad897cd70ab5a1c
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -133,7 +133,6 @@ clt_add(struct thgsd *pthgsd, struct clt *pclt)
 		event_set(&clt->bev->ev_write, clt->fd, EV_WRITE,
 		    clt_tls_writecb, clt->bev);
 	}
-	bufferevent_base_set(pthgsd->eb, clt->bev);
 	bufferevent_setwatermark(clt->bev, EV_READ, 0, BUFF);
 	bufferevent_enable(clt->bev, EV_READ);
 	start_clt_chk(pthgsd);
blob - d8caa1ca067fd66ff7e9e9a127c0c47fb2e4a337
blob + 2790e9bcd864475de04294d2b0e668b4c5726f57
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -27,7 +27,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "ctl.h"
+#include "things.h"
 #include "thingsd.h"
 #include "control.h"
 
@@ -241,19 +241,19 @@ control_dispatch_imsg(int fd, short event, void *bula)
 		}
 
 		switch (imsg.hdr.type) {
-		case IMSG_CTL_LIST:
+		case IMSG_THGS_LIST:
 			if (IMSG_DATA_SIZE(imsg) !=
-			    sizeof(enum ctl_list_type))
+			    sizeof(enum thgs_list_type))
 				break;
-			ctl_imsg_compose_main(imsg.hdr.type, 0,
+			main_imsg_compose_thgs(imsg.hdr.type, 0,
 			    imsg.data, IMSG_DATA_SIZE(imsg));
 			break;
-		case IMSG_CTL_LOG_VERBOSE:
+		case IMSG_THGS_LOG_VERBOSE:
 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
 			    sizeof(verbose))
 				break;
 			/* forward to other process */
-			ctl_imsg_compose_main(imsg.hdr.type, 0,
+			main_imsg_compose_thgs(imsg.hdr.type, 0,
 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
 			memcpy(&verbose, imsg.data, sizeof(verbose));
 			log_setverbose(verbose);
blob - e34a116d08ee3c6143c31f88c6799c0bb7d19df5 (mode 644)
blob + /dev/null
--- thingsd/ctl.c	
+++ /dev/null	
@@ -1,197 +0,0 @@
-/*
- * Copyright (c) 2016, 2019 Tracey Emery <tracey@traceyemery.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <err.h>
-#include <errno.h>
-#include <event.h>
-#include <pwd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <syslog.h>
-#include <unistd.h>
-
-#include "thingsd.h"
-#include "ctl.h"
-#include "control.h"
-
-extern struct thgsd	*pthgsd;
-extern struct dthgs	*pdthgs;
-struct imsgev		*iev_main;
-
-void
-ctl_sighdlr(int sig, short event, void *bula)
-{
-	struct dthgs		*zdthgs = (struct dthgs *)bula;
-
-	switch (sig) {
-	case SIGINT:
-	case SIGTERM:
-	case SIGQUIT:
-		ctl_shutdown(zdthgs);
-	default:
-		fatalx("unexpected signal");
-	}
-}
-
-void
-thgs_ctl(int debug, int verbose, char *ctl_sock)
-{
-	struct passwd		*pw;
-	int			 ctl_fd;
-
-	log_init(debug, LOG_DAEMON);
-	log_setverbose(verbose);
-
-	thgsd_process = PROC_CTL;
-	setproctitle("%s", log_procnames[thgsd_process]);
-	log_procinit(log_procnames[thgsd_process]);
-
-	if ((ctl_fd = control_init(ctl_sock)) == -1)
-		fatalx("ctl_sock failed");
-	control_state.fd = ctl_fd;
-	if ((pw = getpwnam(TH_USER)) != NULL) {
-		if(setuid(pthgsd->pw->pw_uid) != 0)
-			err(1, "unable to set user id of %s: %s", TH_USER,
-			    strerror(errno));
-		else
-			log_info("running as root. %s user not found", TH_USER);
-	}
-
-	if (pledge("stdio unix recvfd", NULL) == -1)
-		fatal("pledge");
-
-	pthgsd->ctl_eb = event_init();
-
-	signal_set(&pthgsd->ctl_evsigquit, SIGQUIT, ctl_sighdlr, pdthgs);
-	signal_set(&pthgsd->ctl_evsigterm, SIGTERM, ctl_sighdlr, pdthgs);
-	signal_set(&pthgsd->ctl_evsigint, SIGINT, ctl_sighdlr, pdthgs);
-
-	signal_add(&pthgsd->ctl_evsigquit, NULL);
-	signal_add(&pthgsd->ctl_evsigterm, NULL);
-	signal_add(&pthgsd->ctl_evsigint, NULL);
-	signal(SIGPIPE, SIG_IGN);
-	signal(SIGHUP, SIG_IGN);
-
-	/* Setup pipe and event handler to the parent process. */
-	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
-		fatal(NULL);
-	imsg_init(&iev_main->ibuf, 3);
-	iev_main->handler = ctl_dispatch_main;
-	iev_main->events = EV_READ;
-	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
-	    iev_main->handler, iev_main);
-	event_add(&iev_main->ev, NULL);
-
-	TAILQ_INIT(&ctl_conns);
-	control_listen();
-	event_dispatch();
-}
-
-void
-imsg_event_add(struct imsgev *iev)
-{
-	iev->events = EV_READ;
-	if (iev->ibuf.w.queued)
-		iev->events |= EV_WRITE;
-
-	event_del(&iev->ev);
-	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
-	event_add(&iev->ev, NULL);
-}
-
-int
-imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
-    pid_t pid, int fd, void *data, uint16_t datalen)
-{
-	int			ret;
-
-	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
-	    pid, fd, data, datalen)) != -1)
-		imsg_event_add(iev);
-	return (ret);
-}
-
-void
-ctl_dispatch_main(int fd, short event, void *bula)
-{
-	struct imsg		 imsg;
-	struct imsgev		*iev = bula;
-	struct imsgbuf		*ibuf = &iev->ibuf;
-	int			 n, shut = 0, verbose;
-
-	if (event & EV_READ) {
-		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
-			fatal("imsg_read error");
-		if (n == 0)	/* Connection closed. */
-			shut = 1;
-	}
-	if (event & EV_WRITE) {
-		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
-			fatal("msgbuf_write");
-		if (n == 0)	/* Connection closed. */
-			shut = 1;
-	}
-
-	for (;;) {
-		if ((n = imsg_get(ibuf, &imsg)) == -1)
-			fatal("imsg_get error");
-		if (n == 0)	/* No more messages. */
-			break;
-
-		switch (imsg.hdr.type) {
-		case IMSG_LIST_CLTS:
-		case IMSG_LIST_THGS:
-		case IMSG_LIST_SOCKS:
-		case IMSG_CTL_END:
-			control_imsg_relay(&imsg);
-			break;
-		case IMSG_CTL_LOG_VERBOSE:
-			memcpy(&verbose, imsg.data, sizeof(verbose));
-			log_setverbose(verbose);
-			break;
-		default:
-			log_debug("%s: error handling imsg %d", __func__,
-			    imsg.hdr.type);
-			break;
-		}
-		imsg_free(&imsg);
-	}
-	if (!shut)
-		imsg_event_add(iev);
-	else {
-		/* This pipe is dead. Remove its event handler. */
-		event_del(&iev->ev);
-		event_loopexit(NULL);
-	}
-}
-
-void
-ctl_shutdown(struct dthgs *zdthgs)
-{
-	/* Close pipes. */
-	msgbuf_write(&iev_main->ibuf.w);
-	msgbuf_clear(&iev_main->ibuf.w);
-	close(iev_main->ibuf.fd);
-	free(iev_main);
-	log_debug("%s control exiting", getprogname());
-	exit(0);
-}
-
-int
-ctl_imsg_compose_main(int type, pid_t pid, void *data, uint16_t datalen)
-{
-	return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
-}
blob - cce92b3cd1e7ce1ed60f0d811bcec4e1f4497a07 (mode 644)
blob + /dev/null
--- thingsd/ctl.h	
+++ /dev/null	
@@ -1,24 +0,0 @@
-/*
- * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include "thingsd.h"
-
-void			 ctl_sighdlr(int, short, void *);
-void			 ctl_shutdown(struct dthgs *);
-void			 ctl_dispatch_main(int, short, void *);
-
-/* ctl.c */
-int		 	 ctl_imsg_compose_main(int, pid_t, void *, uint16_t);
blob - 330169729184ded77ba2bced0db8a81f5b34da4d
blob + f156a74cc2c84b7c0bf065769e5121787765a16c
--- thingsd/parse.y	
+++ thingsd/parse.y	
@@ -159,7 +159,7 @@ subthgs		: THING '{' STRING ',' STRING '}' optcomma {
 				if (strcmp(clt->name, my_name) == 0) {
 					if (my_fd != clt->fd) {
 						fail = true;
-						log_warn("client exists");
+						log_warnx("client exists");
 					}
 					for (n = 0; n < clt->le; n++)
 						if (strcmp(clt->sub_names[n],
blob - b0f4f1c371d64137a3b13ff10a8178c4e7fbaa34
blob + 53d2f5cba52bd2a31abad4c9fdc074e0544e8c05
--- thingsd/serial.c	
+++ thingsd/serial.c	
@@ -47,9 +47,8 @@ open_thgs(struct thgsd *pthgsd, bool reconn)
 			thg->type = DEV;
 			/*
 			 * Just a reminder to set the ownership of your serial
-			 * devices to _thingsd. Otherwise, a disconnected
-			 * and reconnected thing will not be able to
-			 * successfully open(2) the file descriptor.
+			 * devices to _thingsd. Otherwise, a thing will not be
+			 * able to successfully open(2) the file descriptor.
 			 */
 			fd = open(thg->location, O_RDWR | O_NONBLOCK | O_NOCTTY
 			    | O_NDELAY);
@@ -201,7 +200,6 @@ open_thgs(struct thgsd *pthgsd, bool reconn)
 				}
 				thg->bev = bufferevent_new(thg->fd, sockrd,
 				    sockwr, sock_err, pthgsd);
-				bufferevent_base_set(pthgsd->eb, thg->bev);
 				if (thg->bev == NULL)
 					fatalx("ipaddr bev error");
 				thg->evb = evbuffer_new();
blob - /dev/null
blob + 780e653c01a6fa13fab8b3c6b5a142b7ccb33331 (mode 644)
--- /dev/null	
+++ thingsd/things.c	
@@ -0,0 +1,433 @@
+/*
+ * Copyright (c) 2016, 2019 Tracey Emery <tracey@traceyemery.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <event.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include "thingsd.h"
+#include "things.h"
+
+struct thgsd		*pthgsd;
+struct dthgs		*pdthgs;
+struct imsgev		*iev_main;
+
+void
+thgs_sighdlr(int sig, short event, void *bula)
+{
+	struct dthgs		*zdthgs = (struct dthgs *)bula;
+
+	switch (sig) {
+	case SIGINT:
+	case SIGTERM:
+	case SIGQUIT:
+		thgs_shutdown(zdthgs);
+	default:
+		fatalx("unexpected signal");
+	}
+}
+
+void
+thgs_main(int debug, int verbose, char *thgs_sock)
+{
+	struct timeval		 eb_timeout;
+
+	log_init(debug, LOG_DAEMON);
+	log_setverbose(verbose);
+
+	if ((pthgsd = calloc(1, sizeof(*pthgsd))) == NULL)
+		fatalx("no thgsd calloc");
+	if ((pdthgs = calloc(1, sizeof(*pdthgs))) == NULL)
+		fatalx("no dthgs calloc");
+
+	if (parse_conf(PATH_CONF))
+		fatalx("config parsing failed");
+
+	thgsd_process = PROC_THGS;
+	setproctitle("%s", log_procnames[thgsd_process]);
+	log_procinit(log_procnames[thgsd_process]);
+
+	if ((pthgsd->pw = getpwnam(TH_USER)) != NULL) {
+		if(setuid(pthgsd->pw->pw_uid) != 0)
+			log_warn("unable to set user id of %s: %s", TH_USER,
+			    strerror(errno));
+	}
+
+	pthgsd->thgs_eb = event_init();
+
+	open_thgs(pthgsd, false);
+	create_socks(pthgsd, false);
+
+	pthgsd->verbose = verbose;
+	pthgsd->debug = debug;
+	pdthgs->run = 1;
+
+	if (unveil("/dev", "rw") == -1)
+		errx(1, "unveil");
+	if (pledge("stdio rpath wpath inet proc tty dns", NULL) == -1)
+		errx(1, "pledge");
+
+	signal_set(&pthgsd->thgs_evsigquit, SIGQUIT, thgs_sighdlr, pdthgs);
+	signal_set(&pthgsd->thgs_evsigterm, SIGTERM, thgs_sighdlr, pdthgs);
+	signal_set(&pthgsd->thgs_evsigint, SIGINT, thgs_sighdlr, pdthgs);
+
+	signal_add(&pthgsd->thgs_evsigquit, NULL);
+	signal_add(&pthgsd->thgs_evsigterm, NULL);
+	signal_add(&pthgsd->thgs_evsigint, NULL);
+	signal(SIGPIPE, SIG_IGN);
+	signal(SIGHUP, SIG_IGN);
+
+	/* Setup pipe and event handler to the parent process. */
+	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
+		fatal(NULL);
+	imsg_init(&iev_main->ibuf, PARENT_SOCK_FD);
+	iev_main->handler = thgs_dispatch_main;
+	iev_main->events = EV_READ;
+	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
+	    iev_main->handler, iev_main);
+	event_add(&iev_main->ev, NULL);
+
+	/* begin thing watchdog */
+	eb_timeout.tv_sec = EB_TIMEOUT;
+	eb_timeout.tv_usec = 0;
+
+	while (pdthgs->run) {
+		if (pthgsd->exists) {
+			do_reconn();
+		}
+		event_base_loopexit(pthgsd->thgs_eb, &eb_timeout);
+		event_base_dispatch(pthgsd->thgs_eb);
+	}
+
+	thgs_shutdown(pdthgs);
+}
+
+void
+thgs_dispatch_main(int fd, short event, void *bula)
+{
+	struct imsg		 imsg;
+	struct imsgev		*iev = bula;
+	struct imsgbuf		*ibuf = &iev->ibuf;
+	enum thgs_list_type	 type;
+	int			 n, shut = 0, verbose;
+
+	if (event & EV_READ) {
+		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
+			fatal("imsg_read error");
+		if (n == 0)	/* Connection closed. */
+			shut = 1;
+	}
+	if (event & EV_WRITE) {
+		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
+			fatal("msgbuf_write");
+		if (n == 0)	/* Connection closed. */
+			shut = 1;
+	}
+
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get error");
+		if (n == 0)	/* No more messages. */
+			break;
+
+		switch (imsg.hdr.type) {
+		case IMSG_THGS_LIST:
+			if (IMSG_DATA_SIZE(imsg) != sizeof(type))
+				fatalx("%s: IMSG_THGS_STATUS wrong length: %lu",
+				    __func__, IMSG_DATA_SIZE(imsg));
+			memcpy(&type, imsg.data, sizeof(type));
+			show_list(type, imsg.hdr.pid);
+			break;
+		case IMSG_THGS_LOG_VERBOSE:
+			memcpy(&verbose, imsg.data, sizeof(verbose));
+			log_setverbose(verbose);
+			main_imsg_compose_thgs(imsg.hdr.type, 0,
+			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
+			break;
+		default:
+			log_debug("%s: error handling imsg %d", __func__,
+			    imsg.hdr.type);
+			break;
+		}
+		imsg_free(&imsg);
+	}
+	if (!shut)
+		imsg_event_add(iev);
+	else {
+		/* This pipe is dead. Remove its event handler. */
+		log_warnx("%s: pipe to main dead, event deleted", __func__);
+		event_del(&iev->ev);
+		event_loopexit(NULL);
+	}
+}
+
+void
+thgs_shutdown(struct dthgs *zdthgs)
+{
+	struct thg		*thg, *tthg;
+	struct sock		*sock, *tsock;
+	struct clt		*clt, *tclt;
+	struct dthg		*dthg, *tdthg;
+
+	zdthgs->run = 0;
+
+	/* Close pipes. */
+	msgbuf_write(&iev_main->ibuf.w);
+	msgbuf_clear(&iev_main->ibuf.w);
+	close(iev_main->ibuf.fd);
+	free(iev_main);
+
+	/* clean up things */
+	TAILQ_FOREACH_SAFE(thg, &pthgsd->thgs, entry, tthg) {
+		close(thg->fd);
+		TAILQ_REMOVE(&pthgsd->thgs, thg, entry);
+		free(thg);
+	}
+	/*  clean up dead things */
+	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
+		TAILQ_REMOVE(&pdthgs->zthgs, dthg, entry);
+		free(dthg);
+	}
+	/* clean up sockets */
+	TAILQ_FOREACH_SAFE(sock, &pthgsd->socks, entry, tsock) {
+		if (sock->tls) {
+			tls_config_free(sock->tls_config);
+			tls_free(sock->tls_ctx);
+		}
+		close(sock->fd);
+		free(sock->ev);
+		TAILQ_REMOVE(&pthgsd->socks, sock, entry);
+		free(sock);
+	}
+	/* clean up clts */
+	TAILQ_FOREACH_SAFE(clt, &pthgsd->clts, entry, tclt) {
+		close(clt->fd);
+		free(clt->sub_names);
+		TAILQ_REMOVE(&pthgsd->clts, clt, entry);
+		free(clt);
+	}
+	log_debug("%s child exiting", getprogname());
+	free(pdthgs);
+	free(pthgsd);
+	exit(0);
+}
+
+int
+thgs_imsg_compose_main(int type, pid_t pid, void *data, uint16_t datalen)
+{
+	return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
+}
+
+struct dthg
+*new_dthg(struct thg *pthg)
+{
+	struct dthg		*dthg;
+
+	if ((dthg = calloc(1, sizeof(*dthg))) == NULL)
+		fatalx("no calloc dthg");
+	log_debug("%s: adding detached thing, %s", __func__, pthg->name);
+	dthg->name = pthg->name;
+	dthg->type = pthg->type;
+	dthg->dtime = time(NULL);
+	return dthg;
+}
+
+void
+add_reconn(struct thg *pthg)
+{
+	struct dthg		*dthg;
+
+	dthg = new_dthg(pthg);
+	pthgsd->exists = true;
+	pthgsd->dcount++;
+	TAILQ_INSERT_TAIL(&pdthgs->zthgs, dthg, entry);
+}
+
+void
+do_reconn(void)
+{
+	struct dthg		*dthg, *tdthg;
+	struct thg		*thg;
+
+	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
+		if ((size_t)(time(NULL) - dthg->dtime) >
+		    pthgsd->conn_rtry) {
+			dthg->dtime = time(NULL);
+			log_info("attempting to reconnect %s", dthg->name);
+			switch (dthg->type) {
+			case DEV:
+				open_thgs(pthgsd, true);
+				break;
+			case TCP:
+				create_socks(pthgsd, true);
+				break;
+			}
+		}
+	}
+	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
+		TAILQ_FOREACH(thg, &pthgsd->thgs, entry) {
+			if (strcmp(thg->name, dthg->name) == 0 && thg->exists) {
+				TAILQ_REMOVE(&pdthgs->zthgs, dthg, entry);
+				pthgsd->dcount--;
+				free(dthg);
+				return;
+			}
+		}
+	}
+	if (pthgsd->dcount == 0)
+		pthgsd->exists = false;
+}
+
+void
+show_list(enum thgs_list_type type, pid_t pid)
+{
+	struct clt		*clt;
+	struct thg		*thg;
+	struct sock		*sock;
+	struct clt_imsg		*clt_imsg;
+	struct thg_imsg		*thg_imsg;
+	struct sock_imsg	*sock_imsg;
+
+	switch(type) {
+	case THGS_LIST_CLTS:
+		TAILQ_FOREACH(clt, &pthgsd->clts, entry) {
+			clt_imsg = compose_clts(clt);
+			thgs_imsg_compose_main(IMSG_LIST_CLTS, pid, clt_imsg,
+			    sizeof(*clt_imsg));
+			free(clt_imsg);
+		}
+		break;
+	case THGS_LIST_THGS:
+		TAILQ_FOREACH(thg, &pthgsd->thgs, entry) {
+			thg_imsg = compose_thgs(thg);
+			thgs_imsg_compose_main(IMSG_LIST_THGS, pid, thg_imsg,
+			    sizeof(*thg_imsg));
+			free(thg_imsg);
+		}
+		break;
+	case THGS_LIST_SOCKS:
+		TAILQ_FOREACH(sock, &pthgsd->socks, entry) {
+			sock_imsg = compose_socks(sock);
+			thgs_imsg_compose_main(IMSG_LIST_SOCKS, pid, sock_imsg,
+			    sizeof(*sock_imsg));
+			free(sock_imsg);
+		}
+		break;
+	default:
+		fatalx("unknown resolver type %d", type);
+		break;
+	}
+	thgs_imsg_compose_main(IMSG_CTL_END, pid, NULL, 0);
+}
+
+struct clt_imsg *
+compose_clts(struct clt *pclt)
+{
+	struct clt_imsg		*comp_clt;
+
+	if ((comp_clt = calloc(1, sizeof(*comp_clt))) == NULL)
+		fatalx("no com_clt calloc");
+
+	comp_clt->subscribed = pclt->subscribed;
+
+	if (pclt->name != NULL)
+		strlcpy(comp_clt->name, pclt->name, BUFF);
+
+	comp_clt->fd = pclt->fd;
+	comp_clt->port = pclt->port;
+	comp_clt->subs = pclt->subs;
+	comp_clt->tls = pclt->tls;
+	return(comp_clt);
+};
+
+struct thg_imsg *
+compose_thgs(struct thg *pthg)
+{
+	struct thg_imsg		*comp_thg;
+
+	if ((comp_thg = calloc(1, sizeof(*comp_thg))) == NULL)
+		fatalx("no com_thg calloc");
+	comp_thg->exists = pthg->exists;
+	comp_thg->hw_ctl = pthg->hw_ctl;
+	comp_thg->persist = pthg->persist;
+	comp_thg->sw_ctl = pthg->sw_ctl;
+
+	if (pthg->iface != NULL)
+		strlcpy(comp_thg->iface, pthg->iface, BUFF);
+	if (pthg->ipaddr != NULL)
+		strlcpy(comp_thg->ipaddr, pthg->ipaddr, BUFF);
+	if (pthg->name != NULL)
+		strlcpy(comp_thg->name, pthg->name, BUFF);
+	if (pthg->parity != NULL)
+		strlcpy(comp_thg->parity, pthg->parity, BUFF);
+	if (pthg->password != NULL)
+		strlcpy(comp_thg->password, pthg->password, BUFF);
+	if (pthg->location != NULL)
+		strlcpy(comp_thg->location, pthg->location, BUFF);
+	if (pthg->udp != NULL)
+		strlcpy(comp_thg->udp, pthg->udp, BUFF);
+
+	comp_thg->fd = pthg->fd;
+	comp_thg->baud = pthg->baud;
+	comp_thg->conn_port = pthg->conn_port;
+	comp_thg->data_bits = pthg->data_bits;
+	comp_thg->max_clt = pthg->max_clt;
+	comp_thg->port = pthg->port;
+	comp_thg->stop_bits = pthg->stop_bits;
+	comp_thg->type = pthg->type;
+	comp_thg->clt_cnt = pthg->clt_cnt;
+
+	comp_thg->tls = pthg->tls;
+
+	if (pthg->tls_cert_file != NULL)
+		strlcpy(comp_thg->tls_cert_file, pthg->tls_cert_file, BUFF);
+	if (pthg->tls_key_file != NULL)
+		strlcpy(comp_thg->tls_key_file, pthg->tls_key_file, BUFF);
+	if (pthg->tls_ca_file != NULL)
+		strlcpy(comp_thg->tls_ca_file, pthg->tls_ca_file, BUFF);
+	if (pthg->tls_crl_file != NULL)
+		strlcpy(comp_thg->tls_crl_file, pthg->tls_crl_file, BUFF);
+	if (pthg->tls_ocsp_staple_file != NULL)
+		strlcpy(comp_thg->tls_ocsp_staple_file,
+		    pthg->tls_ocsp_staple_file, BUFF);
+	return(comp_thg);
+};
+
+struct sock_imsg *
+compose_socks(struct sock *psock)
+{
+	struct sock_imsg	*comp_sock;
+
+	if ((comp_sock = calloc(1, sizeof(*comp_sock))) == NULL)
+		fatalx("no com_sock calloc");
+
+	if (psock->name != NULL)
+		strlcpy(comp_sock->name, psock->name, BUFF);
+
+	comp_sock->fd = psock->fd;
+	comp_sock->port = psock->port;
+	comp_sock->clt_cnt = psock->clt_cnt;
+	comp_sock->max_clts = psock->max_clts;
+	comp_sock->tls = psock->tls;
+	return(comp_sock);
+};
blob - /dev/null
blob + bdf1584b24be6c26978a240441402066122e62c8 (mode 644)
--- /dev/null	
+++ thingsd/things.h	
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "thingsd.h"
+
+void			 thgs_sighdlr(int, short, void *);
+void			 thgs_shutdown(struct dthgs *);
+void			 thgs_dispatch_main(int, short, void *);
+void			 do_reconn(void);
+void			 show_list(enum thgs_list_type, pid_t);
+
+/* things.c */
+int		 	 thgs_imsg_compose_main(int, pid_t, void *, uint16_t);
+struct thgsd		*pthgsd;
+struct dthgs		*pdthgs;
+struct thg_imsg		*compose_thgs(struct thg *);
+struct clt_imsg		*compose_clts(struct clt *);
+struct sock_imsg	*compose_socks(struct sock *);
blob - f471d11c2070887dc317ab264b3616306e92479e
blob + 65df4c9ee6d4f3417c530bac83f1e2f71f93ae35
--- thingsd/thingsd.c	
+++ thingsd/thingsd.c	
@@ -34,28 +34,24 @@
 #include <unistd.h>
 
 #include "thingsd.h"
+#include "control.h"
 
 __dead void		 usage(void);
 int			 main(int, char *[]);
-void			 do_reconn(void);
-void			 thingsd_sighdlr(int, short, void *);
-void			 thingsd_shutdown(struct dthgs *);
+void			 thgsd_sighdlr(int, short, void *);
+void			 thgsd_shutdown(void);
 static pid_t		 start_child(int, char *, int, int, int);
-static void		 main_dispatch_ctl(int, short, void *);
-int		 	 main_imsg_compose_ctl(int, pid_t, void *, uint16_t);
-void			 show_list(enum ctl_list_type, pid_t);
+static void		 main_dispatch_thgs(int, short, void *);
 
-struct thgsd		*pthgsd;
-struct dthgs		*pdthgs;
-struct thg_imsg		*compose_thgs(struct thg *);
-struct clt_imsg		*compose_clts(struct clt *);
-struct sock_imsg	*compose_socks(struct sock *);
+bool			 thgs_chld = false;
+struct event		 evsigquit;
+struct event		 evsigterm;
+struct event		 evsigint;
+struct event		 evsighup;
 
-bool			 ctl_chld = false;
+pid_t			 thgs_pid;
+static struct imsgev	*iev_thgs;
 
-pid_t			 ctl_pid;
-static struct imsgev	*iev_ctl;
-
 __dead void
 usage(void)
 {
@@ -64,16 +60,14 @@ usage(void)
 }
 
 void
-thingsd_sighdlr(int sig, short event, void *arg)
+thgsd_sighdlr(int sig, short event, void *arg)
 {
-	struct dthgs		*zdthgs = (struct dthgs *)arg;
-
 	switch (sig) {
 	case SIGQUIT:
 	case SIGTERM:
 	case SIGINT:
 	case SIGHUP:
-		thingsd_shutdown(zdthgs);
+		thgsd_shutdown();
 		break;
 	case SIGPIPE:
 		/* ignore */
@@ -87,21 +81,16 @@ int
 main(int argc, char *argv[])
 {
 	int			 ch;
-	struct timeval		 eb_timeout;
-	char			*ctl_sock, *saved_argv0;
-	int			 pipe_ctl[2];
+	char			*thgs_sock, *saved_argv0;
+	int			 pipe_thgs[2], debug = 0, verbose = 0;
+	int			 control_fd;
 
-	ctl_sock = THINGSD_SOCK;
+	thgs_sock = THINGSD_SOCK;
 
 	/* log to stderr until daemonized */
 	log_init(1, LOG_DAEMON);
 	log_setverbose(1);
 
-	if ((pthgsd = calloc(1, sizeof(*pthgsd))) == NULL)
-		fatalx("no thgsd calloc");
-	if ((pdthgs = calloc(1, sizeof(*pdthgs))) == NULL)
-		fatalx("no dthgs calloc");
-
 	saved_argv0 = argv[0];
 	if (saved_argv0 == NULL)
 		saved_argv0 = "thingsd";
@@ -109,13 +98,13 @@ main(int argc, char *argv[])
 	while ((ch = getopt(argc, argv, "dvC")) != -1) {
 		switch (ch) {
 		case 'C':
-			ctl_chld = true;
+			thgs_chld = true;
 			break;
 		case 'd':
-			pthgsd->debug++;
+			debug++;
 			break;
 		case 'v':
-			pthgsd->verbose++;
+			verbose++;
 			break;
 		default:
 			usage();
@@ -126,199 +115,84 @@ main(int argc, char *argv[])
 	if (argc > 0)
 		usage();
 
-	if (ctl_chld)
-		thgs_ctl(pthgsd->debug, pthgsd->verbose, ctl_sock);
-	if (parse_conf(PATH_CONF))
-		fatalx("config parsing failed");
+	if (thgs_chld)
+		thgs_main(debug, verbose, thgs_sock);
+
 	if (geteuid())
 		fatalx("need root privileges");
 
-	log_init(pthgsd->debug, LOG_DAEMON);
-	log_setverbose(pthgsd->verbose);
+	if ((control_fd = control_init(thgs_sock)) == -1)
+		fatalx("thgs_sock failed");
+	control_state.fd = control_fd;
 
+	log_init(debug, LOG_DAEMON);
+	log_setverbose(verbose);
+
 	/* make parent daemon */
 	thgsd_process = PROC_MAIN;
-	setproctitle("%s", log_procnames[thgsd_process]);
 	log_procinit(log_procnames[thgsd_process]);
 
-	if (!pthgsd->debug && daemon(1, 0) == -1)
+	if (!debug && daemon(1, 0) == -1)
 		fatalx("daemon");
 
 	log_info("%s started", getprogname());
 
 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
-	    PF_UNSPEC, pipe_ctl) == -1)
-		fatalx("ctl socketpair");
+	    PF_UNSPEC, pipe_thgs) == -1)
+		fatalx("thgs socketpair");
 
-	ctl_pid = start_child(PROC_CTL, saved_argv0, pipe_ctl[1], pthgsd->debug,
-	    pthgsd->verbose);
+	thgs_pid = start_child(PROC_THGS, saved_argv0, pipe_thgs[1], debug,
+	    verbose);
 
-	pthgsd->eb = event_init();
-	pdthgs->run = 1;
+	event_init();
 
-	open_thgs(pthgsd, false);
-	create_socks(pthgsd, false);
+	signal_set(&evsigquit, SIGQUIT, thgsd_sighdlr, NULL);
+	signal_set(&evsigterm, SIGTERM, thgsd_sighdlr, NULL);
+	signal_set(&evsigint, SIGINT, thgsd_sighdlr, NULL);
+	signal_set(&evsighup, SIGHUP, thgsd_sighdlr, NULL);
 
-	signal_set(&pthgsd->evsigquit, SIGQUIT, thingsd_sighdlr, pdthgs);
-	signal_set(&pthgsd->evsigterm, SIGTERM, thingsd_sighdlr, pdthgs);
-	signal_set(&pthgsd->evsigint, SIGINT, thingsd_sighdlr, pdthgs);
-	signal_set(&pthgsd->evsighup, SIGHUP, thingsd_sighdlr, pdthgs);
-
-	signal_add(&pthgsd->evsigquit, NULL);
-	signal_add(&pthgsd->evsigterm, NULL);
-	signal_add(&pthgsd->evsigint, NULL);
-	signal_add(&pthgsd->evsighup, NULL);
+	signal_add(&evsigquit, NULL);
+	signal_add(&evsigterm, NULL);
+	signal_add(&evsigint, NULL);
+	signal_add(&evsighup, NULL);
 	signal(SIGPIPE, SIG_IGN);
 
-	/* setup ctl child pipe */
-	if ((iev_ctl = malloc(sizeof(struct imsgev))) == NULL)
-		fatalx("iev_ctl malloc");
-	imsg_init(&iev_ctl->ibuf, pipe_ctl[0]);
-	iev_ctl->handler = main_dispatch_ctl;
+	/* setup thgs child pipe */
+	if ((iev_thgs = malloc(sizeof(struct imsgev))) == NULL)
+		fatalx("iev_thgs malloc");
+	imsg_init(&iev_thgs->ibuf, pipe_thgs[0]);
+	iev_thgs->handler = main_dispatch_thgs;
 
 	/* setup event handler */
-	iev_ctl->events = EV_READ;
-	event_set(&iev_ctl->ev, pipe_ctl[0], iev_ctl->events, iev_ctl->handler,
-	    iev_ctl);
-	event_add(&iev_ctl->ev, NULL);
+	iev_thgs->events = EV_READ;
+	event_set(&iev_thgs->ev, pipe_thgs[0], iev_thgs->events,
+	    iev_thgs->handler, iev_thgs);
+	event_add(&iev_thgs->ev, NULL);
 
-	if ((pthgsd->pw = getpwnam(TH_USER)) != NULL) {
-		if(setuid(pthgsd->pw->pw_uid) != 0)
-			errx(1, "unable to set user id of %s: %s", TH_USER,
-			    strerror(errno));
-	} else
+	if (getpwnam(TH_USER) == NULL)
 		log_info("running as root. %s user not found", TH_USER);
 
-	if (unveil("/dev", "rw") == -1)
-		errx(1, "unveil");
-	if (pledge("stdio rpath wpath inet proc tty dns", NULL) == -1)
-		errx(1, "pledge");
+	if (pledge("stdio unix recvfd", NULL) == -1)
+		fatal("pledge");
 
-	/* begin thing watchdog */
-	eb_timeout.tv_sec = EB_TIMEOUT;
-	eb_timeout.tv_usec = 0;
+	TAILQ_INIT(&ctl_conns);
+	control_listen();
 
-	while (pdthgs->run) {
-		if (pthgsd->exists) {
-			do_reconn();
-		}
-		event_base_loopexit(pthgsd->eb, &eb_timeout);
-		event_base_dispatch(pthgsd->eb);
-	}
+	event_dispatch();
 
-	thingsd_shutdown(pdthgs);
+	thgsd_shutdown();
 	return EXIT_SUCCESS;
 }
 
-struct dthg
-*new_dthg(struct thg *pthg)
-{
-	struct dthg		*dthg;
-
-	if ((dthg = calloc(1, sizeof(*dthg))) == NULL)
-		fatalx("no calloc dthg");
-	log_debug("%s: adding detached thing, %s", __func__, pthg->name);
-	dthg->name = pthg->name;
-	dthg->type = pthg->type;
-	dthg->dtime = time(NULL);
-	return dthg;
-}
-
 void
-add_reconn(struct thg *pthg)
+thgsd_shutdown()
 {
-	struct dthg		*dthg;
-
-	dthg = new_dthg(pthg);
-	pthgsd->exists = true;
-	pthgsd->dcount++;
-	TAILQ_INSERT_TAIL(&pdthgs->zthgs, dthg, entry);
-}
-
-void
-do_reconn(void)
-{
-	struct dthg		*dthg, *tdthg;
-	struct thg		*thg;
-
-	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
-		if ((size_t)(time(NULL) - dthg->dtime) >
-		    pthgsd->conn_rtry) {
-			dthg->dtime = time(NULL);
-			log_info("attempting to reconnect %s", dthg->name);
-			switch (dthg->type) {
-			case DEV:
-				open_thgs(pthgsd, true);
-				break;
-			case TCP:
-				create_socks(pthgsd, true);
-				break;
-			}
-		}
-	}
-	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
-		TAILQ_FOREACH(thg, &pthgsd->thgs, entry) {
-			if (strcmp(thg->name, dthg->name) == 0 && thg->exists) {
-				TAILQ_REMOVE(&pdthgs->zthgs, dthg, entry);
-				pthgsd->dcount--;
-				free(dthg);
-				return;
-			}
-		}
-	}
-	if (pthgsd->dcount == 0)
-		pthgsd->exists = false;
-}
-
-void
-thingsd_shutdown(struct dthgs *zdthgs)
-{
-	struct thg		*thg, *tthg;
-	struct sock		*sock, *tsock;
-	struct clt		*clt, *tclt;
-	struct dthg		*dthg, *tdthg;
-	const char		*progname = getprogname();
-
-	zdthgs->run = 0;
-
 	/* close pipe */
-	msgbuf_write(&iev_ctl->ibuf.w);
-	msgbuf_clear(&iev_ctl->ibuf.w);
-	close(iev_ctl->ibuf.fd);
-	free(iev_ctl);
-	/* clean up things */
-	TAILQ_FOREACH_SAFE(thg, &pthgsd->thgs, entry, tthg) {
-		close(thg->fd);
-		TAILQ_REMOVE(&pthgsd->thgs, thg, entry);
-		free(thg);
-	}
-	/*  clean up dead things */
-	TAILQ_FOREACH_SAFE(dthg, &pdthgs->zthgs, entry, tdthg) {
-		TAILQ_REMOVE(&pdthgs->zthgs, dthg, entry);
-		free(dthg);
-	}
-	/* clean up sockets */
-	TAILQ_FOREACH_SAFE(sock, &pthgsd->socks, entry, tsock) {
-		if (sock->tls) {
-			tls_config_free(sock->tls_config);
-			tls_free(sock->tls_ctx);
-		}
-		close(sock->fd);
-		free(sock->ev);
-		TAILQ_REMOVE(&pthgsd->socks, sock, entry);
-		free(sock);
-	}
-	/* clean up clts */
-	TAILQ_FOREACH_SAFE(clt, &pthgsd->clts, entry, tclt) {
-		close(clt->fd);
-		free(clt->sub_names);
-		TAILQ_REMOVE(&pthgsd->clts, clt, entry);
-		free(clt);
-	}
-	log_info("%s terminated", progname);
-	/* event_base_free(pthgsd->eb); */
-	free(pdthgs);
-	free(pthgsd);
+	msgbuf_write(&iev_thgs->ibuf.w);
+	msgbuf_clear(&iev_thgs->ibuf.w);
+	close(iev_thgs->ibuf.fd);
+	free(iev_thgs);
+	log_info("%s terminated", getprogname());
 	exit(0);
 }
 
@@ -340,8 +214,8 @@ start_child(int p, char *argv0, int fd, int debug, int
 		return (pid);
 	}
 
-	if (fd != 3) {
-		if (dup2(fd, 3) == -1)
+	if (fd != PARENT_SOCK_FD) {
+		if (dup2(fd, PARENT_SOCK_FD) == -1)
 			fatal("cannot setup imsg fd");
 	} else if (fcntl(fd, F_SETFD, 0) == -1)
 		fatal("cannot setup imsg fd");
@@ -350,7 +224,7 @@ start_child(int p, char *argv0, int fd, int debug, int
 	switch (p) {
 	case PROC_MAIN:
 		fatalx("Can not start main process");
-	case PROC_CTL:
+	case PROC_THGS:
 		argv[argc++] = "-C";
 		break;
 	}
@@ -380,13 +254,36 @@ start_child(int p, char *argv0, int fd, int debug, int
 	fatal("execvp");
 }
 
+void
+imsg_event_add(struct imsgev *iev)
+{
+	iev->events = EV_READ;
+	if (iev->ibuf.w.queued)
+		iev->events |= EV_WRITE;
+
+	event_del(&iev->ev);
+	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
+	event_add(&iev->ev, NULL);
+}
+
+int
+imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
+    pid_t pid, int fd, void *data, uint16_t datalen)
+{
+	int			ret;
+
+	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
+	    pid, fd, data, datalen)) != -1)
+		imsg_event_add(iev);
+	return (ret);
+}
+
 static void
-main_dispatch_ctl(int fd, short event, void *bula)
+main_dispatch_thgs(int fd, short event, void *bula)
 {
 	struct imsgev		*iev = bula;
 	struct imsgbuf		*ibuf = &iev->ibuf;
 	struct imsg		 imsg;
-	enum ctl_list_type	 type;
 	ssize_t			 n;
 	int			 shut = 0, verbose;
 
@@ -411,18 +308,15 @@ main_dispatch_ctl(int fd, short event, void *bula)
 			break;
 
 		switch (imsg.hdr.type) {
-		case IMSG_CTL_LIST:
-			if (IMSG_DATA_SIZE(imsg) != sizeof(type))
-				fatalx("%s: IMSG_CTL_STATUS wrong length: %lu",
-				    __func__, IMSG_DATA_SIZE(imsg));
-			memcpy(&type, imsg.data, sizeof(type));
-			show_list(type, imsg.hdr.pid);
+		case IMSG_LIST_CLTS:
+		case IMSG_LIST_THGS:
+		case IMSG_LIST_SOCKS:
+		case IMSG_CTL_END:
+			control_imsg_relay(&imsg);
 			break;
-		case IMSG_CTL_LOG_VERBOSE:
+		case IMSG_THGS_LOG_VERBOSE:
 			memcpy(&verbose, imsg.data, sizeof(verbose));
 			log_setverbose(verbose);
-			main_imsg_compose_ctl(imsg.hdr.type, 0,
-			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
 			break;
 		default:
 			log_debug("%s: error handling imsg %d", __func__,
@@ -435,148 +329,14 @@ main_dispatch_ctl(int fd, short event, void *bula)
 		imsg_event_add(iev);
 	else {
 		/* this pipe is dead, so remove the event handler */
-		log_warnx("%s: ctl pipe dead, event deleted", __func__);
+		log_warnx("%s: pipe to thgs dead, event deleted", __func__);
 		event_del(&iev->ev);
 		event_loopexit(NULL);
 	}
 }
 
 int
-main_imsg_compose_ctl(int type, pid_t pid, void *data, uint16_t datalen)
+main_imsg_compose_thgs(int type, pid_t pid, void *data, uint16_t datalen)
 {
-	return (imsg_compose_event(iev_ctl, type, 0, pid, -1, data, datalen));
+	return (imsg_compose_event(iev_thgs, type, 0, pid, -1, data, datalen));
 }
-
-void
-show_list(enum ctl_list_type type, pid_t pid)
-{
-	struct clt		*clt;
-	struct thg		*thg;
-	struct sock		*sock;
-	struct clt_imsg		*clt_imsg;
-	struct thg_imsg		*thg_imsg;
-	struct sock_imsg	*sock_imsg;
-
-	switch(type) {
-	case CTL_LIST_CLTS:
-		TAILQ_FOREACH(clt, &pthgsd->clts, entry) {
-			clt_imsg = compose_clts(clt);
-			main_imsg_compose_ctl(IMSG_LIST_CLTS, pid, clt_imsg,
-			    sizeof(*clt_imsg));
-			free(clt_imsg);
-		}
-		break;
-	case CTL_LIST_THGS:
-		TAILQ_FOREACH(thg, &pthgsd->thgs, entry) {
-			thg_imsg = compose_thgs(thg);
-			main_imsg_compose_ctl(IMSG_LIST_THGS, pid, thg_imsg,
-			    sizeof(*thg_imsg));
-			free(thg_imsg);
-		}
-		break;
-	case CTL_LIST_SOCKS:
-		TAILQ_FOREACH(sock, &pthgsd->socks, entry) {
-			sock_imsg = compose_socks(sock);
-			main_imsg_compose_ctl(IMSG_LIST_SOCKS, pid, sock_imsg,
-			    sizeof(*sock_imsg));
-			free(sock_imsg);
-		}
-		break;
-	default:
-		fatalx("unknown resolver type %d", type);
-		break;
-	}
-	main_imsg_compose_ctl(IMSG_CTL_END, pid, NULL, 0);
-}
-
-struct clt_imsg *
-compose_clts(struct clt *pclt)
-{
-	struct clt_imsg		*comp_clt;
-
-	if ((comp_clt = calloc(1, sizeof(*comp_clt))) == NULL)
-		fatalx("no com_clt calloc");
-
-	comp_clt->subscribed = pclt->subscribed;
-
-	if (pclt->name != NULL)
-		strlcpy(comp_clt->name, pclt->name, BUFF);
-
-	comp_clt->fd = pclt->fd;
-	comp_clt->port = pclt->port;
-	comp_clt->subs = pclt->subs;
-	comp_clt->tls = pclt->tls;
-	return(comp_clt);
-};
-
-struct thg_imsg *
-compose_thgs(struct thg *pthg)
-{
-	struct thg_imsg		*comp_thg;
-
-	if ((comp_thg = calloc(1, sizeof(*comp_thg))) == NULL)
-		fatalx("no com_thg calloc");
-	comp_thg->exists = pthg->exists;
-	comp_thg->hw_ctl = pthg->hw_ctl;
-	comp_thg->persist = pthg->persist;
-	comp_thg->sw_ctl = pthg->sw_ctl;
-
-	if (pthg->iface != NULL)
-		strlcpy(comp_thg->iface, pthg->iface, BUFF);
-	if (pthg->ipaddr != NULL)
-		strlcpy(comp_thg->ipaddr, pthg->ipaddr, BUFF);
-	if (pthg->name != NULL)
-		strlcpy(comp_thg->name, pthg->name, BUFF);
-	if (pthg->parity != NULL)
-		strlcpy(comp_thg->parity, pthg->parity, BUFF);
-	if (pthg->password != NULL)
-		strlcpy(comp_thg->password, pthg->password, BUFF);
-	if (pthg->location != NULL)
-		strlcpy(comp_thg->location, pthg->location, BUFF);
-	if (pthg->udp != NULL)
-		strlcpy(comp_thg->udp, pthg->udp, BUFF);
-
-	comp_thg->fd = pthg->fd;
-	comp_thg->baud = pthg->baud;
-	comp_thg->conn_port = pthg->conn_port;
-	comp_thg->data_bits = pthg->data_bits;
-	comp_thg->max_clt = pthg->max_clt;
-	comp_thg->port = pthg->port;
-	comp_thg->stop_bits = pthg->stop_bits;
-	comp_thg->type = pthg->type;
-	comp_thg->clt_cnt = pthg->clt_cnt;
-
-	comp_thg->tls = pthg->tls;
-
-	if (pthg->tls_cert_file != NULL)
-		strlcpy(comp_thg->tls_cert_file, pthg->tls_cert_file, BUFF);
-	if (pthg->tls_key_file != NULL)
-		strlcpy(comp_thg->tls_key_file, pthg->tls_key_file, BUFF);
-	if (pthg->tls_ca_file != NULL)
-		strlcpy(comp_thg->tls_ca_file, pthg->tls_ca_file, BUFF);
-	if (pthg->tls_crl_file != NULL)
-		strlcpy(comp_thg->tls_crl_file, pthg->tls_crl_file, BUFF);
-	if (pthg->tls_ocsp_staple_file != NULL)
-		strlcpy(comp_thg->tls_ocsp_staple_file,
-		    pthg->tls_ocsp_staple_file, BUFF);
-	return(comp_thg);
-};
-
-struct sock_imsg *
-compose_socks(struct sock *psock)
-{
-	struct sock_imsg	*comp_sock;
-
-	if ((comp_sock = calloc(1, sizeof(*comp_sock))) == NULL)
-		fatalx("no com_sock calloc");
-
-	if (psock->name != NULL)
-		strlcpy(comp_sock->name, psock->name, BUFF);
-
-	comp_sock->fd = psock->fd;
-	comp_sock->port = psock->port;
-	comp_sock->clt_cnt = psock->clt_cnt;
-	comp_sock->max_clts = psock->max_clts;
-	comp_sock->tls = psock->tls;
-	return(comp_sock);
-};
blob - 63747f669a140eaca6999267b35fc5b80ba1152c
blob + 7fe1bf5176623be21a7cf5851a201a07a478e237
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -52,6 +52,8 @@
 #define TLSFLAG_OPTIONAL	0x04
 #define TLSFLAG_BITS		"\10\01CA\02CRL\03OPTIONAL"
 
+#define PARENT_SOCK_FD		 (STDERR_FILENO + 1)
+
 enum socktypes {
 	TCP,
 	UDP,
@@ -224,36 +226,34 @@ struct thgsd {
 	bool			 exists;
 	int			 dcount;
 
-	/* signal handlers */
-	struct event		 evsigquit;
-	struct event		 evsigterm;
-	struct event		 evsigint;
-	struct event		 evsighup;
-
-	/* ctl */
-	struct event_base	*ctl_eb;
-	struct event		 ctl_evsigquit;
-	struct event		 ctl_evsigterm;
-	struct event		 ctl_evsigint;
+	/* thgs */
+	struct event_base	*thgs_eb;
+	struct event		 thgs_evsigquit;
+	struct event		 thgs_evsigterm;
+	struct event		 thgs_evsigint;
 };
 
+/* thingsd.c */
+int			 	 main_imsg_compose_thgs(int, pid_t, void *,
+				    uint16_t);
+
 /* control.c */
 #define IMSG_DATA_SIZE(imsg)    ((imsg).hdr.len - IMSG_HEADER_SIZE)
 
 enum {
 	PROC_MAIN,
-	PROC_CTL,
+	PROC_THGS,
 } thgsd_process;
 
 static const char * const log_procnames[] = {
-	"thingsd main",
-	"thingsd control",
+	"main",
+	"things",
 };
 
-enum ctl_list_type {
-	CTL_LIST_CLTS,
-	CTL_LIST_THGS,
-	CTL_LIST_SOCKS,
+enum thgs_list_type {
+	THGS_LIST_CLTS,
+	THGS_LIST_THGS,
+	THGS_LIST_SOCKS,
 };
 
 struct imsgev {
@@ -273,9 +273,9 @@ TAILQ_HEAD(ctl_conns, ctl_conn)	ctl_conns;
 
 enum imsg_type {
 	IMSG_NONE,
-	IMSG_CTL_LOG_VERBOSE,
-	IMSG_CTL_LIST,
 	IMSG_CTL_END,
+	IMSG_THGS_LOG_VERBOSE,
+	IMSG_THGS_LIST,
 	IMSG_LIST_CLTS,
 	IMSG_LIST_THGS,
 	IMSG_LIST_SOCKS,
@@ -289,12 +289,10 @@ void				 imsg_event_add(struct imsgev *);
 int				 imsg_compose_event(struct imsgev *, uint16_t,
 				    uint32_t, pid_t, int, void *, uint16_t);
 
-/* ctl.c */
-void             		 thgs_ctl(int, int, char*);
-
-/* thingsd.c */
-struct dthg			*new_dthg(struct thg *);
+/* thgs.c */
+void             		 thgs_main(int, int, char*);
 void				 add_reconn(struct thg *);
+struct dthg			*new_dthg(struct thg *);
 
 /* parse.y */
 struct thg			*new_thg(char *);
@@ -315,9 +313,6 @@ void				 sock_err(struct bufferevent *, short, void *)
 int				 create_sock(int, char *, int);
 int				 open_clt_sock(char *, int);
 
-/* things.c */
-pid_t				 thgs_main(struct thgsd *, const char *);
-
 /* events.c */
 void				 udp_evt(int, short, void *);