commit 8b0186ed01081d1606f79a5b4b5f7a91f0a77a4a from: Tracey Emery date: Mon Jul 27 17:18:21 2020 UTC error handling clean-up kill control conn on bad thing name when echo packets are requested commit - a999abe24e04a394cc07482a75408689d8f0fc1c commit + 8b0186ed01081d1606f79a5b4b5f7a91f0a77a4a blob - 655da0ab34ce892b14d8c19f9560e1c200d4110a blob + 5737a353f2f5ddaa7de964b75fb073451712c01e --- thingsctl/thingsctl.c +++ thingsctl/thingsctl.c @@ -103,7 +103,7 @@ main(int argc, char *argv[]) err(1, "pledge"); if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) - err(1, NULL); + err(1, "%s: malloc", __func__); imsg_init(ibuf, ctl_sock); done = 0; @@ -166,8 +166,10 @@ main(int argc, char *argv[]) printf("Waiting for incoming packets\n"); imsg_compose(ibuf, IMSG_SHOW_PACKETS_REQUEST, 0, 0, -1, res->name, sizeof(res->name)); - } else + } else { printf("Echo packets ignored\n"); + done = 1; + } printf("\n"); break; case SHOW_PARENT: @@ -200,8 +202,13 @@ main(int argc, char *argv[]) while (!done) { if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) errx(1, "imsg_read error"); - if (n == 0) - errx(1, "pipe closed"); + if (n == 0) { + if (res->action == SHOW_PACKETS) + errx(1, "Bad thing name request, or thingsd " \ + "shutdown.\n"); + else + errx(1, "pipe closed"); + } while (!done) { if ((n = imsg_get(ibuf, &imsg)) == -1) @@ -230,7 +237,7 @@ main(int argc, char *argv[]) } if ((ctl_pkt = calloc(IMSG_DATA_SIZE(&imsg), sizeof(*ctl_pkt))) == NULL) - errx(1, "calloc ctl_pkt"); + err(1, "%s: calloc", __func__); if ((ctl_pkt = strndup(imsg.data, IMSG_DATA_SIZE(&imsg))) == NULL) { free(ctl_pkt); blob - 34e8ddde5256b86ed58c4dd6a93b1ca67615e142 blob + 60f663314972098c99b48d29a5581d8537be4d54 --- thingsd/client.c +++ thingsd/client.c @@ -67,7 +67,7 @@ client_conn(int fd, short event, void *arg) *client->sub_names = (char *) calloc(env->max_subs, sizeof(client->sub_names)); if (*client->sub_names == NULL) - fatalx("calloc client->sub_names"); + goto err; /* check for unlimited clients */ sock->client_cnt++; blob - 6e0725ae6c10055e095a96cd200120a61bb8993e blob + 3290eeff0e201ca3f0dd6301e6b458bd83b5b821 --- thingsd/control.c +++ thingsd/control.c @@ -73,8 +73,13 @@ int control_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) { struct ctl_conn *c; + struct privsep *ps = p->p_ps; + struct control_sock cs = ps->ps_csock; + int bad = 0; switch (imsg->hdr.type) { + case IMSG_BAD_THING: + bad = 1; case IMSG_SHOW_PACKETS_DATA: case IMSG_SHOW_PACKETS_END_DATA: case IMSG_GET_INFO_PARENT_DATA: @@ -90,6 +95,12 @@ control_dispatch_parent(int fd, struct privsep_proc *p __func__, imsg->hdr.peerid); return (-1); } + + if (bad) { + control_close(ps, imsg, imsg->hdr.peerid, &cs); + break; + } + imsg_compose_event(&c->iev, imsg->hdr.type, 0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg)); break; blob - 2e5a116be9387d32a0c4dddbe0e796619cce0392 blob + 1c1b56351fc4705118174ded4de12fc1208b8e91 --- thingsd/parse.y +++ thingsd/parse.y @@ -1124,11 +1124,11 @@ pushbuff(u_char *pkt) struct file *bfile; if ((bfile = calloc(1, sizeof(struct file))) == NULL) { - log_warn("malloc"); + log_warn("calloc"); return (NULL); } if ((bfile->name = strdup("subscribe buffer")) == NULL) { - log_warn("malloc"); + log_warn("strdup"); free(bfile); return (NULL); } @@ -1265,7 +1265,7 @@ cmdline_symset(char *s) len = strlen(s) - strlen(val) + 1; if ((sym = malloc(len)) == NULL) - errx(1, "cmdline_symset: malloc"); + fatal("%s: malloc", __func__); strlcpy(sym, s, len); @@ -1322,7 +1322,7 @@ conf_new_thing(char *name) thing = calloc(1, sizeof(*thing)); if (thing == NULL) - errx(1, "get_thing: calloc"); + fatal("%s: calloc", __func__); n = strlcpy(thing->name, name, sizeof(thing->name)); if (n >= sizeof(thing->name)) fatalx("%s: thing->name too long", __func__); blob - b0ee8071857615961b89dcf7bb778ca7af857273 blob + c92c01472eb1709c6a30f6094074404ff37e8e9a --- thingsd/sockets.c +++ thingsd/sockets.c @@ -384,10 +384,10 @@ new_socket(int port) struct socket *sock; if ((sock = calloc(1, sizeof(*sock))) == NULL) - fatalx("no sock calloc"); + fatal("%s: calloc", __func__); if ((sock->ev = calloc(1, sizeof(*sock->ev))) == NULL) - fatalx("no sock->ev calloc"); + fatal("%s: calloc", __func__); sock->port = port; blob - 81a2845aee4c29fc99fa93c2226f1a47d9ebd26d blob + 31042a597bc4430e041b5cfe287df2250a394316 --- thingsd/things.c +++ thingsd/things.c @@ -90,6 +90,8 @@ things_dispatch_parent(int fd, struct privsep_proc *p, memcpy(&nti, imsg->data, sizeof(nti)); thing = calloc(1, sizeof(*thing)); + if (thing == NULL) + fatal("%s: calloc", __func__); thing->exists = nti.exists; thing->hw_ctl = nti.hw_ctl; @@ -305,9 +307,8 @@ things_echo_pkt(struct privsep *ps, struct imsg *imsg) struct packet_client *packet_client; packet_client = calloc(1, sizeof(*packet_client)); - if (packet_client == NULL) - fatalx("no calloc packet_client"); + fatal("%s: calloc", __func__); thingsd_env->packet_client_count++; @@ -489,7 +490,7 @@ struct dead_thing dead_thing = calloc(1, sizeof(*dead_thing)); if (dead_thing == NULL) - fatalx("no calloc dead_thing"); + fatal("%s: calloc", __func__); log_debug("%s: adding detached thing, %s", __func__, thing->name); blob - 4672c5eac1d4554526a05d9bf7dde89b891c3bf8 blob + 271fd1f4b6f7cc44c5b14665d00a1838bd9ef351 --- thingsd/thingsd.c +++ thingsd/thingsd.c @@ -62,7 +62,7 @@ struct thingsd *thingsd_env; static struct privsep_proc procs[] = { { "control", PROC_CONTROL, thingsd_dispatch_control, control }, { "things", PROC_THINGS, thingsd_dispatch_things, things, - things_shutdown }, + things_shutdown }, }; /* For the privileged process */ @@ -73,10 +73,12 @@ int thingsd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) { struct privsep *ps = p->p_ps; - int res = 0, cmd = 0, verbose; + int res = 0, cmd = 0, verbose, exists = 0; unsigned int v = 0; char client_name[THINGSD_MAXNAME]; + char thing_name[THINGSD_MAXNAME]; struct client *client; + struct thing *thing; switch (imsg->hdr.type) { case IMSG_SHOW_PACKETS_END_DATA: @@ -89,7 +91,19 @@ thingsd_dispatch_control(int fd, struct privsep_proc * IMSG_SIZE_CHECK(imsg, &v); if (imsg->data == NULL) break; - things_echo_pkt(ps, imsg); + memcpy(thing_name, imsg->data, sizeof(thing_name)); + TAILQ_FOREACH(thing, thingsd_env->things, entry) { + if (strcmp(thing->name, thing_name) == 0) { + things_echo_pkt(ps, imsg); + exists = 1; + break; + } + } + if (!exists) + if (proc_compose_imsg(ps, PROC_CONTROL, -1, + IMSG_BAD_THING, + imsg->hdr.peerid, -1, &res, sizeof(res)) == -1) + return (-1); break; case IMSG_KILL_CLIENT: IMSG_SIZE_CHECK(imsg, &v); @@ -219,7 +233,7 @@ main(int argc, char **argv) log_init(1, LOG_DAEMON); if ((env = calloc(1, sizeof(*env))) == NULL) - fatal("calloc: env"); + fatal("%s: calloc", __func__); thingsd_env = env; blob - a8da26e3d21a2f780fb4d3113e8f2e8e30db4109 blob + 915cfd554603f023034d2accdb8d04e648834dc2 --- thingsd/thingsd.h +++ thingsd/thingsd.h @@ -79,6 +79,7 @@ enum imsg_type { IMSG_SHOW_PACKETS_REQUEST, IMSG_SHOW_PACKETS_DATA, IMSG_SHOW_PACKETS_END_DATA, + IMSG_BAD_THING, IMSG_ADD_THING, IMSG_KILL_CLIENT,