commit - ea947d481662dcd3c4643733d59dffbfc32ddf44
commit + 170f38f3d5543009bccd5ef497d629a129eedf0f
blob - a79ac44a7d52cd95a9b11d4bcfaf615839d95dae
blob + afb2b1e21518293011762618652d76614be113ef
--- thingsctl/parser.c
+++ thingsctl/parser.c
enum token_type {
NOTOKEN,
ENDTOKEN,
- KEYWORD
+ KEYWORD,
+ CLTNAME,
+ THGNAME,
};
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}
};
{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 *);
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;
}
break;
case KEYWORD:
fprintf(stderr, " %s\n", table[i].keyword);
+ case CLTNAME:
+ fprintf(stderr, " <client name>\n");
break;
+ case THGNAME:
+ fprintf(stderr, " <thing name>\n");
+ break;
case ENDTOKEN:
break;
}
blob - ba5487449ddaec55930a96cb6532c9f582a13111
blob + fb5db2d2af3a2db8d1c598d0a671d12b145a93f3
--- thingsctl/parser.h
+++ thingsctl/parser.h
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
case LOG_DEBUG:
case LOG_VERBOSE:
case LOG_BRIEF:
+ case KILL_CLT:
+ case SHOW_PKTS:
if (geteuid() != 0)
errx(1, "need root privileges");
break;
/* 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 */
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
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);
}
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
#include <sys/time.h>
#include <sys/socket.h>
+#include <errno.h>
#include <event.h>
#include <ifaddrs.h>
#include <fcntl.h>
#include "thingsd.h"
extern struct dthgs *pdthgs;
+extern struct ctl_pkt *ctl_pkt;
void
create_socks(struct thgsd *pthgsd, bool reconn)
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;
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
struct thgsd *pthgsd;
struct dthgs *pdthgs;
struct imsgev *iev_main;
+struct ctl_pkt *ctl_pkt;
void
thgs_sighdlr(int sig, short event, void *bula)
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");
strerror(errno));
}
+ ctl_pkt->exists = false;
+ ctl_pkt->pid = -1;
+
pthgsd->thgs_eb = event_init();
open_thgs(pthgsd, false);
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;
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",
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
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
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
short events;
};
+struct ctl_pkt {
+ bool exists;
+ uint32_t pid;
+};
+
int control_init(char *);
int control_listen(void);
void control_accept(int, short, void *);
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 *);
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 *);