1 /*========== command and control (CLI) connections ==========*/
5 typedef struct CliConn CliConn;
7 void (*destroy)(CliConn*);
13 struct sockaddr_un un;
18 static const oop_rd_style cli_rd_style= {
19 OOP_RD_DELIM_STRIP, '\n',
21 OOP_RD_SHORTREC_FORBID
24 static void cli_destroy(CliConn *cc) {
28 static void cli_checkouterr(CliConn *cc /* may destroy*/) {
29 if (ferror(cc->out) | fflush(cc->out)) {
30 info("CTRL%d write error %s", cc->fd, strerror(errno));
35 static void cli_prompt(CliConn *cc /* may destroy*/) {
36 fprintf(cc->out, "%s| ", sitename);
42 void (*f)(CliConn *cc, const CliCommand *ccmd,
43 const char *arg, size_t argsz);
48 static const CliCommand cli_commands[];
51 static void ccmd_##wh(CliConn *cc, const CliCommand *c, \
52 const char *arg, size_t argsz)
55 fputs("commands:\n", cc->out);
56 const CliCommand *ccmd;
57 for (ccmd=cli_commands; ccmd->cmd; ccmd++)
58 fprintf(cc->out, " %s\n", ccmd->cmd);
59 fputs("NB: permissible arguments are not shown above."
60 " Not all commands listed are safe. See innduct(8).\n", cc->out);
64 int ok= trigger_flush_ok("manual request");
65 if (!ok) fprintf(cc->out,"already flushing (state is %s)\n", sms_names[sms]);
70 notice("terminating (CTRL%d)",cc->fd);
71 raise_default(SIGTERM);
77 /* messing with our head: */
78 CCMD(period) { period(); }
79 CCMD(setintarg) { *(int*)c->xdata= atoi(arg); }
80 CCMD(setint) { *(int*)c->xdata= c->xval; }
81 CCMD(setint_period) { *(int*)c->xdata= c->xval; period(); }
83 static const CliCommand cli_commands[]= {
85 { "flush", ccmd_flush },
86 { "stop", ccmd_stop },
87 { "dump q", ccmd_dump, 0,0 },
88 { "dump a", ccmd_dump, 0,1 },
92 #define POKES(cmd,func) \
93 { cmd "flush", func, &until_flush, 1 }, \
94 { cmd "conn", func, &until_connect, 0 }, \
95 { cmd "blscan", func, &until_backlog_nextscan, 0 },
96 POKES("next ", ccmd_setint)
97 POKES("prod ", ccmd_setint_period)
99 { "pretend flush", ccmd_setintarg, &simulate_flush },
100 { "wedge blscan", ccmd_setint, &until_backlog_nextscan, -1 },
104 static void *cli_rd_ok(oop_source *lp, oop_read *oread, oop_rd_event ev,
105 const char *errmsg, int errnoval,
106 const char *data, size_t recszu, void *cc_v) {
110 info("CTRL%d closed", cc->fd);
115 if (recszu == 0) goto prompt;
116 assert(recszu <= INT_MAX);
119 const CliCommand *ccmd;
120 for (ccmd=cli_commands; ccmd->cmd; ccmd++) {
121 int l= strlen(ccmd->cmd);
122 if (recsz < l) continue;
123 if (recsz > l && data[l] != ' ') continue;
124 if (memcmp(data, ccmd->cmd, l)) continue;
126 int argl= (int)recsz - (l+1);
127 ccmd->f(cc, ccmd, argl>=0 ? data+l+1 : 0, argl);
131 fputs("unknown command; h for help\n", cc->out);
138 static void *cli_rd_err(oop_source *lp, oop_read *oread, oop_rd_event ev,
139 const char *errmsg, int errnoval,
140 const char *data, size_t recsz, void *cc_v) {
143 info("CTRL%d read error %s", cc->fd, errmsg);
148 static int cli_conn_startup(CliConn *cc /* may destroy*/,
150 cc->rd= oop_rd_new_fd(loop, cc->fd, 0,0);
151 if (!cc->rd) { warn("oop_rd_new_fd cli failed"); return -1; }
153 int er= oop_rd_read(cc->rd, &cli_rd_style, MAX_CLI_COMMAND,
156 if (er) { errno= er; syswarn("oop_rd_read cli failed"); return -1; }
158 info("CTRL%d %s ready", cc->fd, how);
163 static void cli_stdio_destroy(CliConn *cc) {
165 oop_rd_cancel(cc->rd);
166 errno= oop_rd_delete_tidy(cc->rd);
167 if (errno) syswarn("oop_rd_delete tidy failed (no-nonblock stdin?)");
172 static void cli_stdio(void) {
173 NEW_DECL(CliConn *,cc);
174 cc->destroy= cli_stdio_destroy;
178 int r= cli_conn_startup(cc,"stdio");
179 if (r) cc->destroy(cc);
182 static void cli_accepted_destroy(CliConn *cc) {
184 oop_rd_cancel(cc->rd);
185 oop_rd_delete_kill(cc->rd);
187 if (cc->out) { fclose(cc->out); cc->fd=0; }
188 close_perhaps(&cc->fd);
192 static void *cli_master_readable(oop_source *lp, int master,
193 oop_event ev, void *u) {
194 NEW_DECL(CliConn *,cc);
195 cc->destroy= cli_accepted_destroy;
197 cc->salen= sizeof(cc->sa);
198 cc->fd= accept(master, &cc->sa.sa, &cc->salen);
199 if (cc->fd<0) { syswarn("error accepting cli connection"); goto x; }
201 cc->out= fdopen(cc->fd, "w");
202 if (!cc->out) { syswarn("error fdopening accepted cli connection"); goto x; }
204 int r= cli_conn_startup(cc, "accepted");
214 #define NOCLI(...) do{ \
215 syswarn("no cli listener, because failed to " __VA_ARGS__); \
219 static void cli_init(void) {
222 struct sockaddr_un un;
225 memset(&sa,0,sizeof(sa));
226 int maxlen= sizeof(sa.un.sun_path);
229 info("control command line disabled");
233 int pathlen= strlen(path_cli);
234 if (pathlen > maxlen) {
235 warn("no cli listener, because cli socket path %s too long (%d>%d)",
236 path_cli, pathlen, maxlen);
241 int r= mkdir(path_cli_dir, 0700);
242 if (r && errno!=EEXIST)
243 NOCLI("create cli socket directory %s", path_cli_dir);
246 int r= unlink(path_cli);
247 if (r && errno!=ENOENT)
248 NOCLI("remove old cli socket %s", path_cli);
250 cli_master= socket(PF_UNIX, SOCK_STREAM, 0);
251 if (cli_master<0) NOCLI("create new cli master socket");
253 int sl= pathlen + offsetof(struct sockaddr_un, sun_path);
254 sa.un.sun_family= AF_UNIX;
255 memcpy(sa.un.sun_path, path_cli, pathlen);
257 r= bind(cli_master, &sa.sa, sl);
258 if (r) NOCLI("bind to cli socket path %s", sa.un.sun_path);
260 r= listen(cli_master, 5);
261 if (r) NOCLI("listen to cli master socket");
263 xsetnonblock(cli_master, 1);
265 loop->on_fd(loop, cli_master, OOP_READ, cli_master_readable, 0);
266 info("cli ready, listening on %s", path_cli);
271 xclose_perhaps(&cli_master, "cli master",0);
275 /*========== dumping state ==========*/
277 static void dump_article_list(FILE *f, const CliCommand *c,
278 const ArticleList *al) {
279 fprintf(f, " count=%d\n", al->count);
280 if (!c->xval) return;
283 for (i=0, art=LIST_HEAD(*al); art; i++, art=LIST_NEXT(art)) {
284 fprintf(f," #%05d %-11s", i, artstate_names[art->state]);
285 DUMPV("%p", art->,ipf);
286 DUMPV("%d", art->,missing);
287 DUMPV("%lu", (unsigned long)art->,offset);
288 DUMPV("%d", art->,blanklen);
289 DUMPV("%d", art->,midlen);
290 fprintf(f, " %s %s\n", TokenToText(art->token), art->messageid);
294 static void dump_input_file(FILE *f, const CliCommand *c,
295 InputFile *ipf, const char *wh) {
296 char *dipf= dbg_report_ipf(ipf);
297 fprintf(f,"input %s %s", wh, dipf);
301 DUMPV("%d", ipf->,readcount_ok);
302 DUMPV("%d", ipf->,readcount_blank);
303 DUMPV("%d", ipf->,readcount_err);
304 DUMPV("%d", ipf->,count_nooffer_missing);
308 ArtState state; const char *const *statename;
309 for (state=0, statename=artstate_names; *statename; state++,statename++) {
310 #define RC_DUMP_FMT(x) " " #x "=%d"
311 #define RC_DUMP_VAL(x) ,ipf->counts[state][RC_##x]
312 fprintf(f,"input %s counts %-11s"
313 RESULT_COUNTS(RC_DUMP_FMT,RC_DUMP_FMT) "\n",
315 RESULT_COUNTS(RC_DUMP_VAL,RC_DUMP_VAL));
317 fprintf(f,"input %s queue", wh);
318 dump_article_list(f,c,&ipf->queue);
324 fprintf(cc->out, "dumping state to %s\n", path_dump);
325 FILE *f= fopen(path_dump, "w");
326 if (!f) { fprintf(cc->out, "failed: open: %s\n", strerror(errno)); return; }
328 fprintf(f,"general");
329 DUMPV("%s", sms_names,[sms]);
330 DUMPV("%d", ,until_flush);
331 DUMPV("%ld", (long),self_pid);
332 DUMPV("%p", , defer);
333 DUMPV("%d", , until_connect);
334 DUMPV("%d", , until_backlog_nextscan);
335 DUMPV("%d", , simulate_flush);
336 fprintf(f,"\nnocheck");
337 DUMPV("%#.10f", , accept_proportion);
338 DUMPV("%d", , nocheck);
339 DUMPV("%d", , nocheck_reported);
342 fprintf(f,"special");
343 DUMPV("%ld", (long),connecting_child);
344 DUMPV("%d", , connecting_fdpass_sock);
345 DUMPV("%d", , cli_master);
349 DUMPV("%d", , lowvol_circptr);
350 DUMPV("%d", , lowvol_total);
352 for (i=0; i<lowvol_periods; i++) {
354 if (i==lowvol_circptr) fprintf(f,"*");
355 fprintf(f,"%d",lowvol_perperiod[i]);
359 fprintf(f,"filemon ");
360 filemon_method_dump_info(f);
362 dump_input_file(f,c, main_input_file, "main" );
363 dump_input_file(f,c, flushing_input_file, "flushing");
364 dump_input_file(f,c, backlog_input_file, "backlog" );
366 fprintf(f,"conns count=%d\n", conns.count);
371 fprintf(f,"C%d",conn->fd);
372 DUMPV("%p",conn->,rd); DUMPV("%d",conn->,max_queue);
373 DUMPV("%d",conn->,stream); DUMPV("\"%s\"",conn->,quitting);
374 DUMPV("%d",conn->,since_activity);
377 fprintf(f,"C%d waiting", conn->fd); dump_article_list(f,c,&conn->waiting);
378 fprintf(f,"C%d priority",conn->fd); dump_article_list(f,c,&conn->priority);
379 fprintf(f,"C%d sent", conn->fd); dump_article_list(f,c,&conn->sent);
381 fprintf(f,"C%d xmit xmitu=%d\n", conn->fd, conn->xmitu);
382 for (i=0; i<conn->xmitu; i++) {
383 const struct iovec *iv= &conn->xmit[i];
384 const XmitDetails *xd= &conn->xmitd[i];
387 case xk_Const: dinfo= xasprintf("Const"); break;
388 case xk_Artdata: dinfo= xasprintf("A%p", xd->info.sm_art); break;
392 fprintf(f," #%03d %-11s l=%d %s\n", i, dinfo, iv->iov_len,
393 sanitise(iv->iov_base, iv->iov_len));
399 DUMPV("%s", , feedfile);
400 DUMPV("%s", , path_cli);
401 DUMPV("%s", , path_lock);
402 DUMPV("%s", , path_flushing);
403 DUMPV("%s", , path_defer);
404 DUMPV("%s", , path_dump);
405 DUMPV("%s", , globpat_backlog);
408 if (!!ferror(f) + !!fclose(f)) {
409 fprintf(cc->out, "failed: write: %s\n", strerror(errno));