3 * tailing reliable realtime streaming feeder for inn
4 * cli.c - command and control connections
6 * Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * (I believe that when you compile and link this as part of the inn2
22 * build, with the Makefile runes I have provided, all the libraries
23 * and files which end up included in innduct are licence-compatible
24 * with GPLv3. If not then please let me know. -Ian Jackson.)
29 /*========== command and control (CLI) connections ==========*/
31 static int cli_master;
33 typedef struct CliConn CliConn;
35 void (*destroy)(CliConn*);
41 struct sockaddr_un un;
46 static const oop_rd_style cli_rd_style= {
47 OOP_RD_DELIM_STRIP, '\n',
49 OOP_RD_SHORTREC_FORBID
52 static void cli_destroy(CliConn *cc) {
56 static void cli_checkouterr(CliConn *cc /* may destroy*/) {
57 if (ferror(cc->out) | fflush(cc->out)) {
58 info("CTRL%d write error %s", cc->fd, strerror(errno));
63 static void cli_prompt(CliConn *cc /* may destroy*/) {
64 fprintf(cc->out, "%s| ", sitename);
70 void (*f)(CliConn *cc, const CliCommand *ccmd,
71 const char *arg, size_t argsz);
76 static const CliCommand cli_commands[];
79 static void ccmd_##wh(CliConn *cc, const CliCommand *c, \
80 const char *arg, size_t argsz)
83 fputs("commands:\n", cc->out);
84 const CliCommand *ccmd;
85 for (ccmd=cli_commands; ccmd->cmd; ccmd++)
86 fprintf(cc->out, " %s\n", ccmd->cmd);
87 fputs("NB: permissible arguments are not shown above."
88 " Not all commands listed are safe. See innduct(8).\n", cc->out);
92 int ok= trigger_flush_ok("manual request");
93 if (!ok) fprintf(cc->out,"already flushing (state is %s)\n", sms_names[sms]);
98 notice("terminating (CTRL%d)",cc->fd);
99 raise_default(SIGTERM);
105 /* messing with our head: */
106 CCMD(period) { period(); }
107 CCMD(setintarg) { *(int*)c->xdata= atoi(arg); }
108 CCMD(setint) { *(int*)c->xdata= c->xval; }
109 CCMD(setint_period) { *(int*)c->xdata= c->xval; period(); }
111 static const CliCommand cli_commands[]= {
113 { "flush", ccmd_flush },
114 { "stop", ccmd_stop },
115 { "dump q", ccmd_dump, 0,0 },
116 { "dump a", ccmd_dump, 0,1 },
118 { "p", ccmd_period },
120 #define POKES(cmd,func) \
121 { cmd "flush", func, &until_flush, 1 }, \
122 { cmd "conn", func, &until_connect, 0 }, \
123 { cmd "blscan", func, &until_backlog_nextscan, 0 },
124 POKES("next ", ccmd_setint)
125 POKES("prod ", ccmd_setint_period)
127 { "pretend flush", ccmd_setintarg, &simulate_flush },
128 { "wedge blscan", ccmd_setint, &until_backlog_nextscan, -1 },
132 static void *cli_rd_ok(oop_source *lp, oop_read *oread, oop_rd_event ev,
133 const char *errmsg, int errnoval,
134 const char *data, size_t recszu, void *cc_v) {
138 info("CTRL%d closed", cc->fd);
143 if (recszu == 0) goto prompt;
144 assert(recszu <= INT_MAX);
147 const CliCommand *ccmd;
148 for (ccmd=cli_commands; ccmd->cmd; ccmd++) {
149 int l= strlen(ccmd->cmd);
150 if (recsz < l) continue;
151 if (recsz > l && data[l] != ' ') continue;
152 if (memcmp(data, ccmd->cmd, l)) continue;
154 int argl= (int)recsz - (l+1);
155 ccmd->f(cc, ccmd, argl>=0 ? data+l+1 : 0, argl);
159 fputs("unknown command; h for help\n", cc->out);
166 static void *cli_rd_err(oop_source *lp, oop_read *oread, oop_rd_event ev,
167 const char *errmsg, int errnoval,
168 const char *data, size_t recsz, void *cc_v) {
171 info("CTRL%d read error %s", cc->fd, errmsg);
176 static int cli_conn_startup(CliConn *cc /* may destroy*/,
178 cc->rd= oop_rd_new_fd(loop, cc->fd, 0,0);
179 if (!cc->rd) { warn("oop_rd_new_fd cli failed"); return -1; }
181 int er= oop_rd_read(cc->rd, &cli_rd_style, MAX_CLI_COMMAND,
184 if (er) { errno= er; syswarn("oop_rd_read cli failed"); return -1; }
186 info("CTRL%d %s ready", cc->fd, how);
191 static void cli_stdio_destroy(CliConn *cc) {
193 oop_rd_cancel(cc->rd);
194 errno= oop_rd_delete_tidy(cc->rd);
195 if (errno) syswarn("oop_rd_delete tidy failed (no-nonblock stdin?)");
200 void cli_stdio(void) {
201 NEW_DECL(CliConn *,cc);
202 cc->destroy= cli_stdio_destroy;
206 int r= cli_conn_startup(cc,"stdio");
207 if (r) cc->destroy(cc);
210 static void cli_accepted_destroy(CliConn *cc) {
212 oop_rd_cancel(cc->rd);
213 oop_rd_delete_kill(cc->rd);
215 if (cc->out) { fclose(cc->out); cc->fd=0; }
216 close_perhaps(&cc->fd);
220 static void *cli_master_readable(oop_source *lp, int master,
221 oop_event ev, void *u) {
222 NEW_DECL(CliConn *,cc);
223 cc->destroy= cli_accepted_destroy;
225 cc->salen= sizeof(cc->sa);
226 cc->fd= accept(master, &cc->sa.sa, &cc->salen);
227 if (cc->fd<0) { syswarn("error accepting cli connection"); goto x; }
229 cc->out= fdopen(cc->fd, "w");
230 if (!cc->out) { syswarn("error fdopening accepted cli connection"); goto x; }
232 int r= cli_conn_startup(cc, "accepted");
242 #define NOCLI(...) do{ \
243 syswarn("no cli listener, because failed to " __VA_ARGS__); \
247 void cli_init(void) {
250 struct sockaddr_un un;
253 memset(&sa,0,sizeof(sa));
254 int maxlen= sizeof(sa.un.sun_path);
257 info("control command line disabled");
261 int pathlen= strlen(path_cli);
262 if (pathlen > maxlen) {
263 warn("no cli listener, because cli socket path %s too long (%d>%d)",
264 path_cli, pathlen, maxlen);
269 int r= mkdir(path_cli_dir, 0700);
270 if (r && errno!=EEXIST)
271 NOCLI("create cli socket directory %s", path_cli_dir);
274 int r= unlink(path_cli);
275 if (r && errno!=ENOENT)
276 NOCLI("remove old cli socket %s", path_cli);
278 cli_master= socket(PF_UNIX, SOCK_STREAM, 0);
279 if (cli_master<0) NOCLI("create new cli master socket");
281 int sl= pathlen + offsetof(struct sockaddr_un, sun_path);
282 sa.un.sun_family= AF_UNIX;
283 memcpy(sa.un.sun_path, path_cli, pathlen);
285 r= bind(cli_master, &sa.sa, sl);
286 if (r) NOCLI("bind to cli socket path %s", sa.un.sun_path);
288 r= listen(cli_master, 5);
289 if (r) NOCLI("listen to cli master socket");
291 xsetnonblock(cli_master, 1);
293 loop->on_fd(loop, cli_master, OOP_READ, cli_master_readable, 0);
294 info("cli ready, listening on %s", path_cli);
299 xclose_perhaps(&cli_master, "cli master",0);
303 /*========== dumping state ==========*/
305 static void dump_article_list(FILE *f, const CliCommand *c,
306 const ArticleList *al) {
307 fprintf(f, " count=%d\n", al->count);
308 if (!c->xval) return;
311 for (i=0, art=LIST_HEAD(*al); art; i++, art=LIST_NEXT(art)) {
312 fprintf(f," #%05d %-11s", i, artstate_names[art->state]);
313 DUMPV("%p", art->,ipf);
314 DUMPV("%d", art->,missing);
315 DUMPV("%lu", (unsigned long)art->,offset);
316 DUMPV("%d", art->,blanklen);
317 DUMPV("%d", art->,midlen);
318 fprintf(f, " %s %s\n", TokenToText(art->token), art->messageid);
322 static void dump_input_file(FILE *f, const CliCommand *c,
323 InputFile *ipf, const char *wh) {
324 char *dipf= dbg_report_ipf(ipf);
325 fprintf(f,"input %s %s", wh, dipf);
329 DUMPV("%d", ipf->,readcount_ok);
330 DUMPV("%d", ipf->,readcount_blank);
331 DUMPV("%d", ipf->,readcount_err);
332 DUMPV("%d", ipf->,count_nooffer_missing);
336 ArtState state; const char *const *statename;
337 for (state=0, statename=artstate_names; *statename; state++,statename++) {
338 #define RC_DUMP_FMT(x) " " #x "=%d"
339 #define RC_DUMP_VAL(x) ,ipf->counts[state][RC_##x]
340 fprintf(f,"input %s counts %-11s"
341 RESULT_COUNTS(RC_DUMP_FMT,RC_DUMP_FMT) "\n",
343 RESULT_COUNTS(RC_DUMP_VAL,RC_DUMP_VAL));
345 fprintf(f,"input %s queue", wh);
346 dump_article_list(f,c,&ipf->queue);
352 fprintf(cc->out, "dumping state to %s\n", path_dump);
353 FILE *f= fopen(path_dump, "w");
354 if (!f) { fprintf(cc->out, "failed: open: %s\n", strerror(errno)); return; }
356 fprintf(f,"general");
357 DUMPV("%s", sms_names,[sms]);
358 DUMPV("%d", ,until_flush);
359 DUMPV("%ld", (long),self_pid);
360 DUMPV("%p", , defer);
361 DUMPV("%d", , until_connect);
362 DUMPV("%d", , until_backlog_nextscan);
363 DUMPV("%d", , simulate_flush);
364 fprintf(f,"\nnocheck");
365 DUMPV("%#.10f", , accept_proportion);
366 DUMPV("%d", , nocheck);
367 DUMPV("%d", , nocheck_reported);
370 fprintf(f,"special");
371 DUMPV("%ld", (long),connecting_child);
372 DUMPV("%d", , connecting_fdpass_sock);
373 DUMPV("%d", , cli_master);
377 DUMPV("%d", , lowvol_circptr);
378 DUMPV("%d", , lowvol_total);
380 for (i=0; i<lowvol_periods; i++) {
382 if (i==lowvol_circptr) fprintf(f,"*");
383 fprintf(f,"%d",lowvol_perperiod[i]);
387 fprintf(f,"filemon ");
388 filemon_method_dump_info(f);
390 dump_input_file(f,c, main_input_file, "main" );
391 dump_input_file(f,c, flushing_input_file, "flushing");
392 dump_input_file(f,c, backlog_input_file, "backlog" );
394 fprintf(f,"conns count=%d\n", conns.count);
399 fprintf(f,"C%d",conn->fd);
400 DUMPV("%p",conn->,rd); DUMPV("%d",conn->,max_queue);
401 DUMPV("%d",conn->,stream); DUMPV("\"%s\"",conn->,quitting);
402 DUMPV("%d",conn->,since_activity);
405 fprintf(f,"C%d waiting", conn->fd); dump_article_list(f,c,&conn->waiting);
406 fprintf(f,"C%d priority",conn->fd); dump_article_list(f,c,&conn->priority);
407 fprintf(f,"C%d sent", conn->fd); dump_article_list(f,c,&conn->sent);
409 fprintf(f,"C%d xmit xmitu=%d\n", conn->fd, conn->xmitu);
410 for (i=0; i<conn->xmitu; i++) {
411 const struct iovec *iv= &conn->xmit[i];
412 const XmitDetails *xd= &conn->xmitd[i];
415 case xk_Const: dinfo= xasprintf("Const"); break;
416 case xk_Artdata: dinfo= xasprintf("A%p", xd->info.sm_art); break;
420 fprintf(f," #%03d %-11s l=%d %s\n", i, dinfo, iv->iov_len,
421 sanitise(iv->iov_base, iv->iov_len));
427 DUMPV("%s", , feedfile);
428 DUMPV("%s", , path_cli);
429 DUMPV("%s", , path_lock);
430 DUMPV("%s", , path_flushing);
431 DUMPV("%s", , path_defer);
432 DUMPV("%s", , path_dump);
433 DUMPV("%s", , globpat_backlog);
436 if (!!ferror(f) + !!fclose(f)) {
437 fprintf(cc->out, "failed: write: %s\n", strerror(errno));