commit 170f38f3d5543009bccd5ef497d629a129eedf0f from: Tracey Emery date: Fri Apr 19 15:38:07 2019 UTC Implement final controls commit - ea947d481662dcd3c4643733d59dffbfc32ddf44 commit + 170f38f3d5543009bccd5ef497d629a129eedf0f blob - a79ac44a7d52cd95a9b11d4bcfaf615839d95dae blob + afb2b1e21518293011762618652d76614be113ef --- thingsctl/parser.c +++ thingsctl/parser.c @@ -36,7 +36,9 @@ enum token_type { NOTOKEN, ENDTOKEN, - KEYWORD + KEYWORD, + CLTNAME, + THGNAME, }; struct token { @@ -49,10 +51,14 @@ struct token { static const struct token t_main[]; static const struct token t_log[]; static const struct token t_list[]; +static const struct token t_ctl_name[]; +static const struct token t_thg_name[]; static const struct token t_main[] = { {KEYWORD, "list", NONE, t_list}, {KEYWORD, "log", NONE, t_log}, + {KEYWORD, "kill", KILL_CLT, t_ctl_name}, + {KEYWORD, "show", SHOW_PKTS, t_thg_name}, {ENDTOKEN, "", NONE, NULL} }; @@ -70,6 +76,16 @@ static const struct token t_list[] = { {ENDTOKEN, "", NONE, NULL} }; +static const struct token t_ctl_name[] = { + {CLTNAME, "", NONE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_thg_name[] = { + {THGNAME, "", NONE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + static const struct token *match_token(const char *, const struct token *, struct parse_result *); static void show_valid_args(const struct token *); @@ -133,6 +149,20 @@ match_token(const char *word, const struct token *tabl res->action = t->value; } break; + case CLTNAME: + if (!match && word != NULL && strlen(word) > 0) { + res->clt_name = strdup(word); + match++; + t = &table[i]; + } + break; + case THGNAME: + if (!match && word != NULL && strlen(word) > 0) { + res->thg_name = strdup(word); + match++; + t = &table[i]; + } + break; case ENDTOKEN: break; } @@ -163,7 +193,12 @@ show_valid_args(const struct token *table) break; case KEYWORD: fprintf(stderr, " %s\n", table[i].keyword); + case CLTNAME: + fprintf(stderr, " \n"); break; + case THGNAME: + fprintf(stderr, " \n"); + break; case ENDTOKEN: break; } blob - ba5487449ddaec55930a96cb6532c9f582a13111 blob + fb5db2d2af3a2db8d1c598d0a671d12b145a93f3 --- thingsctl/parser.h +++ thingsctl/parser.h @@ -23,10 +23,14 @@ enum actions { LIST_CLTS, LIST_THGS, LIST_SOCKS, + KILL_CLT, + SHOW_PKTS, }; struct parse_result { - enum actions action; + char *clt_name; + char *thg_name; + enum actions action; }; struct parse_result *parse(int, char *[]); blob - ef75cf29e7967d8b471a6f9f09ebe872199ab5e9 blob + 51a96299d621588c7c0c4137dc7334d014915cac --- thingsctl/thingsctl.c +++ thingsctl/thingsctl.c @@ -110,6 +110,8 @@ main(int argc, char *argv[]) case LOG_DEBUG: case LOG_VERBOSE: case LOG_BRIEF: + case KILL_CLT: + case SHOW_PKTS: if (geteuid() != 0) errx(1, "need root privileges"); break; @@ -119,6 +121,16 @@ main(int argc, char *argv[]) /* Process user request. */ switch (res->action) { + case SHOW_PKTS: + imsg_compose(ibuf, IMSG_SHOW_PKTS, 0, 0, -1, + res->thg_name, sizeof(res->thg_name)); + break; + case KILL_CLT: + imsg_compose(ibuf, IMSG_KILL_CLT, 0, 0, -1, + res->clt_name, sizeof(res->clt_name)); + printf("kill request for client \"%s\" sent\n", res->clt_name); + done = 1; + break; case LOG_DEBUG: verbose++; /* FALLTHROUGH */ @@ -166,6 +178,9 @@ main(int argc, char *argv[]) break; switch (res->action) { + case SHOW_PKTS: + printf("%s\n", (char *)imsg.data); + break; case LIST_CLTS: case LIST_THGS: case LIST_SOCKS: blob - 68a3c22218de96ddc1232215fbd9bfc1da197ccf blob + 8fefa3e034998cd78fb52075468b08a02692b1db --- thingsd/control.c +++ thingsd/control.c @@ -238,6 +238,8 @@ control_dispatch_imsg(int fd, short event, void *bula) switch (imsg.hdr.type) { case IMSG_THGS_LOG_VERBOSE: + case IMSG_KILL_CLT: + case IMSG_SHOW_PKTS: if (euid != 0) { imsg_free(&imsg); control_close(fd); @@ -249,6 +251,14 @@ control_dispatch_imsg(int fd, short event, void *bula) } switch (imsg.hdr.type) { + case IMSG_SHOW_PKTS: + main_imsg_compose_thgs(imsg.hdr.type, imsg.hdr.pid, + imsg.data, IMSG_DATA_SIZE(imsg)); + break; + case IMSG_KILL_CLT: + main_imsg_compose_thgs(imsg.hdr.type, 0, imsg.data, + IMSG_DATA_SIZE(imsg)); + break; case IMSG_THGS_LIST: if (IMSG_DATA_SIZE(imsg) != sizeof(enum thgs_list_type)) blob - ab31098b815794d980c086fc095cb74a37a4e3d0 blob + 645043028cad19390ca5169654f129174c216cde --- thingsd/sockets.c +++ thingsd/sockets.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include "thingsd.h" extern struct dthgs *pdthgs; +extern struct ctl_pkt *ctl_pkt; void create_socks(struct thgsd *pthgsd, bool reconn) @@ -331,7 +333,8 @@ sock_rd(struct bufferevent *bev, void *arg) struct thg *thg = NULL, *tthg; struct clt *clt; size_t len; - int fd = bev->ev_read.ev_fd; + int fd = bev->ev_read.ev_fd, pid_chk; + bool ctl = false; size_t n; char *pkt; @@ -345,10 +348,24 @@ sock_rd(struct bufferevent *bev, void *arg) len = EVBUFFER_LENGTH(thg->evb); if ((pkt = calloc(len, sizeof(*pkt))) == NULL) return; + /* send pkt to control socket */ + if (ctl_pkt->exists) { + pid_chk = kill(ctl_pkt->pid, 0); + if (pid_chk == -1 && errno > 1) { + ctl_pkt->exists = false; + ctl_pkt->pid = -1; + ctl = false; + } else { + ctl = true; + evbuffer_remove(thg->evb, pkt, len); + thgs_imsg_compose_main(IMSG_SHOW_PKTS, ctl_pkt->pid, + pkt, len); + } + } if (thg->clt_cnt == 0) { evbuffer_drain(thg->evb, len); return; - } else + } else if (ctl == false) evbuffer_remove(thg->evb, pkt, len); /* write to clients */ TAILQ_FOREACH(clt, &pthgsd->clts, entry) { blob - 3062721987a298ed7d0b6c366c88592c9a34e529 blob + a9b00ef119f46bba1642067a5e82a089dbcd16b1 --- thingsd/things.c +++ thingsd/things.c @@ -31,6 +31,7 @@ struct thgsd *pthgsd; struct dthgs *pdthgs; struct imsgev *iev_main; +struct ctl_pkt *ctl_pkt; void thgs_sighdlr(int sig, short event, void *bula) @@ -59,7 +60,8 @@ thgs_main(int debug, int verbose, char *thgs_sock) fatalx("no thgsd calloc"); if ((pdthgs = calloc(1, sizeof(*pdthgs))) == NULL) fatalx("no dthgs calloc"); - + if ((ctl_pkt = calloc(1, sizeof(*ctl_pkt))) == NULL) + fatalx("no ctl_pkt calloc"); if (parse_conf(PATH_CONF)) fatalx("config parsing failed"); @@ -73,6 +75,9 @@ thgs_main(int debug, int verbose, char *thgs_sock) strerror(errno)); } + ctl_pkt->exists = false; + ctl_pkt->pid = -1; + pthgsd->thgs_eb = event_init(); open_thgs(pthgsd, false); @@ -125,6 +130,7 @@ thgs_main(int debug, int verbose, char *thgs_sock) void thgs_dispatch_main(int fd, short event, void *bula) { + struct clt *clt; struct imsg imsg; struct imsgev *iev = bula; struct imsgbuf *ibuf = &iev->ibuf; @@ -151,6 +157,22 @@ thgs_dispatch_main(int fd, short event, void *bula) break; switch (imsg.hdr.type) { + case IMSG_SHOW_PKTS: + ctl_pkt->exists = true; + ctl_pkt->pid = imsg.hdr.pid; + break; + case IMSG_KILL_CLT: + TAILQ_FOREACH(clt, &pthgsd->clts, entry) { + if (imsg.data == NULL) + break; + if (strncmp(clt->name, imsg.data, BUFF) == 0) { + log_debug("Control killed client: %s", + imsg.data); + clt_del(pthgsd, clt); + break; + } + } + break; case IMSG_THGS_LIST: if (IMSG_DATA_SIZE(imsg) != sizeof(type)) fatalx("%s: IMSG_THGS_STATUS wrong length: %lu", @@ -227,6 +249,7 @@ thgs_shutdown(struct dthgs *zdthgs) log_debug("%s child exiting", getprogname()); free(pdthgs); free(pthgsd); + free(ctl_pkt); exit(0); } blob - 2708644bbf923a39a05dd6b14ebd0b59dc76ad38 blob + fea5e2b82e3c78632bffe9c78e0a4090db141221 --- thingsd/things.h +++ thingsd/things.h @@ -23,7 +23,6 @@ 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 *, int); blob - 65df4c9ee6d4f3417c530bac83f1e2f71f93ae35 blob + eb3834703637466f36b38f03cc92875f96aacfae --- thingsd/thingsd.c +++ thingsd/thingsd.c @@ -308,6 +308,7 @@ main_dispatch_thgs(int fd, short event, void *bula) break; switch (imsg.hdr.type) { + case IMSG_SHOW_PKTS: case IMSG_LIST_CLTS: case IMSG_LIST_THGS: case IMSG_LIST_SOCKS: blob - 1edebe9a75099e6eeb36b99e3cdb306860d30f5f blob + c6e88142fe474ba243cdb66177f4fddb10bed162 --- thingsd/thingsd.h +++ thingsd/thingsd.h @@ -264,6 +264,11 @@ struct imsgev { short events; }; +struct ctl_pkt { + bool exists; + uint32_t pid; +}; + int control_init(char *); int control_listen(void); void control_accept(int, short, void *); @@ -281,9 +286,7 @@ enum imsg_type { IMSG_LIST_THGS, IMSG_LIST_SOCKS, IMSG_KILL_CLT, - IMSG_ADD_THG, - IMSG_DEL_THG, - IMSG_SHOW_PKTS + IMSG_SHOW_PKTS, }; void imsg_event_add(struct imsgev *); @@ -294,6 +297,8 @@ int imsg_compose_event(struct imsgev *, uint16_t, void thgs_main(int, int, char*); void add_reconn(struct thg *); struct dthg *new_dthg(struct thg *); +int thgs_imsg_compose_main(int, pid_t, void *, + uint16_t); /* parse.y */ struct thg *new_thg(char *);