+/*========== command and control (CLI) connections ==========*/
+
+static int cli_master;
+
+typedef struct CliConn CliConn;
+struct CliConn {
+ void (*destroy)(CliConn*);
+ int fd;
+ oop_read *rd;
+ FILE *out;
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+ socklen_t salen;
+};
+
+static const oop_rd_style cli_rd_style= {
+ OOP_RD_DELIM_STRIP, '\n',
+ OOP_RD_NUL_FORBID,
+ OOP_RD_SHORTREC_FORBID
+};
+
+static void cli_destroy(CliConn *cc) {
+ cc->destroy(cc);
+}
+
+static void cli_checkouterr(CliConn *cc /* may destroy*/) {
+ if (ferror(cc->out) | fflush(cc->out)) {
+ info("CTRL%d write error %s", cc->fd, strerror(errno));
+ cli_destroy(cc);
+ }
+}
+
+static void cli_prompt(CliConn *cc /* may destroy*/) {
+ fprintf(cc->out, "%s| ", sitename);
+ cli_checkouterr(cc);
+}
+
+struct CliCommand {
+ const char *cmd;
+ void (*f)(CliConn *cc, const CliCommand *ccmd,
+ const char *arg, size_t argsz);
+ void *xdata;
+ int xval;
+};
+
+static const CliCommand cli_commands[];
+
+#define CCMD(wh) \
+ static void ccmd_##wh(CliConn *cc, const CliCommand *c, \
+ const char *arg, size_t argsz)
+
+CCMD(help) {
+ fputs("commands:\n", cc->out);
+ const CliCommand *ccmd;
+ for (ccmd=cli_commands; ccmd->cmd; ccmd++)
+ fprintf(cc->out, " %s\n", ccmd->cmd);
+ fputs("NB: permissible arguments are not shown above."
+ " Not all commands listed are safe. See innduct(8).\n", cc->out);
+}
+
+CCMD(flush) {
+ int ok= trigger_flush_ok("manual request");
+ if (!ok) fprintf(cc->out,"already flushing (state is %s)\n", sms_names[sms]);
+}
+
+CCMD(stop) {
+ preterminate();
+ notice("terminating (CTRL%d)",cc->fd);
+ raise_default(SIGTERM);
+ abort();
+}
+
+CCMD(dump);
+
+/* messing with our head: */
+CCMD(period) { period(); }
+CCMD(setintarg) { *(int*)c->xdata= atoi(arg); }
+CCMD(setint) { *(int*)c->xdata= c->xval; }
+CCMD(setint_period) { *(int*)c->xdata= c->xval; period(); }
+
+static const CliCommand cli_commands[]= {
+ { "h", ccmd_help },
+ { "flush", ccmd_flush },
+ { "stop", ccmd_stop },
+ { "dump q", ccmd_dump, 0,0 },
+ { "dump a", ccmd_dump, 0,1 },
+
+ { "p", ccmd_period },
+
+#define POKES(cmd,func) \
+ { cmd "flush", func, &until_flush, 1 }, \
+ { cmd "conn", func, &until_connect, 0 }, \
+ { cmd "blscan", func, &until_backlog_nextscan, 0 },
+POKES("next ", ccmd_setint)
+POKES("prod ", ccmd_setint_period)
+
+ { "pretend flush", ccmd_setintarg, &simulate_flush },
+ { "wedge blscan", ccmd_setint, &until_backlog_nextscan, -1 },
+ { 0 }
+};
+
+static void *cli_rd_ok(oop_source *lp, oop_read *oread, oop_rd_event ev,
+ const char *errmsg, int errnoval,
+ const char *data, size_t recsz, void *cc_v) {
+ CliConn *cc= cc_v;
+
+ if (!data) {
+ info("CTRL%d closed", cc->fd);
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+ }
+
+ if (recsz == 0) goto prompt;
+
+ const CliCommand *ccmd;
+ for (ccmd=cli_commands; ccmd->cmd; ccmd++) {
+ int l= strlen(ccmd->cmd);
+ if (recsz < l) continue;
+ if (recsz > l && data[l] != ' ') continue;
+ if (memcmp(data, ccmd->cmd, l)) continue;
+
+ int argl= (int)recsz - (l+1);
+ ccmd->f(cc, ccmd, argl>=0 ? data+l+1 : 0, argl);
+ goto prompt;
+ }
+
+ fputs("unknown command; h for help\n", cc->out);
+
+ prompt:
+ cli_prompt(cc);
+ return OOP_CONTINUE;
+}
+
+static void *cli_rd_err(oop_source *lp, oop_read *oread, oop_rd_event ev,
+ const char *errmsg, int errnoval,
+ const char *data, size_t recsz, void *cc_v) {
+ CliConn *cc= cc_v;
+
+ info("CTRL%d read error %s", cc->fd, errmsg);
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+}
+
+static int cli_conn_startup(CliConn *cc /* may destroy*/,
+ const char *how) {
+ cc->rd= oop_rd_new_fd(loop, cc->fd, 0,0);
+ if (!cc->rd) { warn("oop_rd_new_fd cli failed"); return -1; }
+
+ int er= oop_rd_read(cc->rd, &cli_rd_style, MAX_CLI_COMMAND,
+ cli_rd_ok, cc,
+ cli_rd_err, cc);
+ if (er) { errno= er; syswarn("oop_rd_read cli failed"); return -1; }
+
+ info("CTRL%d %s ready", cc->fd, how);
+ cli_prompt(cc);
+ return 0;
+}
+
+static void cli_stdio_destroy(CliConn *cc) {
+ if (cc->rd) {
+ oop_rd_cancel(cc->rd);
+ errno= oop_rd_delete_tidy(cc->rd);
+ if (errno) syswarn("oop_rd_delete tidy failed (no-nonblock stdin?)");
+ }
+ free(cc);
+}
+
+static void cli_stdio(void) {
+ NEW_DECL(CliConn *,cc);
+ cc->destroy= cli_stdio_destroy;
+
+ cc->fd= 0;
+ cc->out= stdout;
+ int r= cli_conn_startup(cc,"stdio");
+ if (r) cc->destroy(cc);
+}
+
+static void cli_accepted_destroy(CliConn *cc) {
+ if (cc->rd) {
+ oop_rd_cancel(cc->rd);
+ oop_rd_delete_kill(cc->rd);
+ }
+ if (cc->out) { fclose(cc->out); cc->fd=0; }
+ close_perhaps(&cc->fd);
+ free(cc);
+}
+
+static void *cli_master_readable(oop_source *lp, int master,
+ oop_event ev, void *u) {
+ NEW_DECL(CliConn *,cc);
+ cc->destroy= cli_accepted_destroy;
+
+ cc->salen= sizeof(cc->sa);
+ cc->fd= accept(master, &cc->sa.sa, &cc->salen);
+ if (cc->fd<0) { syswarn("error accepting cli connection"); goto x; }
+
+ cc->out= fdopen(cc->fd, "w");
+ if (!cc->out) { syswarn("error fdopening accepted cli connection"); goto x; }
+
+ int r= cli_conn_startup(cc, "accepted");
+ if (r) goto x;
+
+ return OOP_CONTINUE;
+
+ x:
+ cc->destroy(cc);
+ return OOP_CONTINUE;
+}
+
+#define NOCLI(...) do{ \
+ syswarn("no cli listener, because failed to " __VA_ARGS__); \
+ goto nocli; \
+ }while(0)
+
+static void cli_init(void) {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+
+ memset(&sa,0,sizeof(sa));
+ int maxlen= sizeof(sa.un.sun_path);
+
+ if (!path_cli) {
+ info("control command line disabled");
+ return;
+ }
+
+ int pathlen= strlen(path_cli);
+ if (pathlen > maxlen) {
+ warn("no cli listener, because cli socket path %s too long (%d>%d)",
+ path_cli, pathlen, maxlen);
+ return;
+ }
+
+ if (path_cli_dir) {
+ int r= mkdir(path_cli_dir, 0700);
+ if (r && errno!=EEXIST)
+ NOCLI("create cli socket directory %s", path_cli_dir);
+ }
+
+ int r= unlink(path_cli);
+ if (r && errno!=ENOENT)
+ NOCLI("remove old cli socket %s", path_cli);
+
+ cli_master= socket(PF_UNIX, SOCK_STREAM, 0);
+ if (cli_master<0) NOCLI("create new cli master socket");
+
+ int sl= pathlen + offsetof(struct sockaddr_un, sun_path);
+ sa.un.sun_family= AF_UNIX;
+ memcpy(sa.un.sun_path, path_cli, pathlen);
+
+ r= bind(cli_master, &sa.sa, sl);
+ if (r) NOCLI("bind to cli socket path %s", sa.un.sun_path);
+
+ r= listen(cli_master, 5);
+ if (r) NOCLI("listen to cli master socket");
+
+ xsetnonblock(cli_master, 1);
+
+ loop->on_fd(loop, cli_master, OOP_READ, cli_master_readable, 0);
+ info("cli ready, listening on %s", path_cli);
+
+ return;
+
+ nocli:
+ xclose_perhaps(&cli_master, "cli master",0);
+ return;
+}
+
+/*========== management of connections ==========*/
+
+static void conn_closefd(Conn *conn, const char *msgprefix) {
+ int r= close_perhaps(&conn->fd);
+ if (r) info("C%d %serror closing socket: %s",
+ conn->fd, msgprefix, strerror(errno));
+}