3 * tailing reliable realtime streaming feeder for inn
5 * Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * (I believe that when you compile and link this as part of the inn2
21 * build, with the Makefile runes I have provided, all the libraries
22 * and files which end up included in innduct are licence-compatible
23 * with GPLv3. If not then please let me know. -Ian Jackson.)
38 #include "inn/innconf.h"
39 #include "inn/messages.h"
42 #include <sys/types.h>
45 #include <sys/socket.h>
66 /*----- general definitions, probably best not changed -----*/
68 #define CONNCHILD_ESTATUS_STREAM 24
69 #define CONNCHILD_ESTATUS_NOSTREAM 25
71 #define INNDCOMMCHILD_ESTATUS_FAIL 26
72 #define INNDCOMMCHILD_ESTATUS_NONESUCH 27
74 #define MAX_LINE_FEEDFILE (NNTP_MSGID_MAXLEN + sizeof(TOKEN)*2 + 10)
75 #define MAX_CLI_COMMAND 1000
77 #define VA va_list al; va_start(al,fmt)
78 #define PRINTF(f,a) __attribute__((__format__(printf,f,a)))
79 #define NORET_PRINTF(f,a) __attribute__((__noreturn__,__format__(printf,f,a)))
80 #define NORET __attribute__((__noreturn__))
82 #define NEW(ptr) ((ptr)= zxmalloc(sizeof(*(ptr))))
83 #define NEW_DECL(type,ptr) type ptr = zxmalloc(sizeof(*(ptr)))
85 #define DUMPV(fmt,pfx,v) fprintf(f, " " #v "=" fmt, pfx v);
87 #define FOR_CONN(conn) \
88 for ((conn)=LIST_HEAD(conns); (conn); (conn)=LIST_NEXT((conn)))
90 /*----- doubly linked lists -----*/
92 #define ISNODE(T) struct node list_node
95 union { struct list li; T *for_type; } u; \
99 #define NODE(n) (assert((void*)&(n)->list_node == (n)), &(n)->list_node)
101 #define LIST_CHECKCANHAVENODE(l,n) \
102 ((void)((n) == ((l).u.for_type))) /* just for the type check */
104 #define LIST_ADDSOMEHOW(l,n,list_addsomehow) \
105 ( LIST_CHECKCANHAVENODE(l,n), \
106 list_addsomehow(&(l).u.li, NODE((n))), \
110 #define LIST_REMSOMEHOW(l,list_remsomehow) \
111 ( (typeof((l).u.for_type)) \
114 list_remsomehow(&(l).u.li) ) \
120 #define LIST_ADDHEAD(l,n) LIST_ADDSOMEHOW((l),(n),list_addhead)
121 #define LIST_ADDTAIL(l,n) LIST_ADDSOMEHOW((l),(n),list_addtail)
122 #define LIST_REMHEAD(l) LIST_REMSOMEHOW((l),list_remhead)
123 #define LIST_REMTAIL(l) LIST_REMSOMEHOW((l),list_remtail)
125 #define LIST_INIT(l) ((l).count=0, list_new(&(l).u.li))
126 #define LIST_HEAD(l) ((typeof((l).u.for_type))(list_head((struct list*)&(l))))
127 #define LIST_NEXT(n) ((typeof(n))list_succ(NODE((n))))
128 #define LIST_BACK(n) ((typeof(n))list_pred(NODE((n))))
130 #define LIST_REMOVE(l,n) \
131 ( LIST_CHECKCANHAVENODE(l,n), \
132 list_remove(NODE((n))), \
136 #define LIST_INSERT(l,n,pred) \
137 ( LIST_CHECKCANHAVENODE(l,n), \
138 LIST_CHECKCANHAVENODE(l,pred), \
139 list_insert((struct list*)&(l), NODE((n)), NODE((pred))), \
143 /*----- type predeclarations -----*/
145 typedef struct Conn Conn;
146 typedef struct Article Article;
147 typedef struct InputFile InputFile;
148 typedef struct XmitDetails XmitDetails;
149 typedef struct Filemon_Perfile Filemon_Perfile;
150 typedef enum StateMachineState StateMachineState;
151 typedef struct CliCommand CliCommand;
157 /*----- configuration options -----*/
159 extern const char *sitename, *remote_host;
160 extern const char *feedfile, *path_run, *path_cli, *path_cli_dir;
161 extern int quiet_multiple, interactive, try_filemon, try_stream, port;
162 extern const char *inndconffile;
164 extern int max_connections, max_queue_per_conn, target_max_feedfile_size;
165 extern int period_seconds, filepoll_seconds, max_queue_per_ipf;
166 extern int connection_setup_timeout, inndcomm_flush_timeout;
168 extern double nocheck_thresh;
169 extern double nocheck_decay;
171 /* all these are initialised to seconds, and converted to periods in main */
172 extern int reconnect_delay_periods;
173 extern int flushfail_retry_periods;
174 extern int backlog_retry_minperiods;
175 extern int backlog_spontrescan_periods;
176 extern int spontaneous_flush_periods;
177 extern int max_separated_periods;
178 extern int need_activity_periods;
179 extern int stats_log_periods;
180 extern int lowvol_thresh;
181 extern int lowvol_periods;
183 extern double max_bad_data_ratio;
184 extern int max_bad_data_initial;
187 /*----- statistics -----*/
189 typedef enum { /* in queue in conn->sent */
190 art_Unchecked, /* not checked, not sent checking */
191 art_Wanted, /* checked, wanted sent body as requested */
192 art_Unsolicited, /* - sent body without check */
195 extern const char *const artstate_names[]; /* xmit.c */
197 #define RESULT_COUNTS(RCS,RCN) \
206 #define RCI_TRIPLE_FMT_BASE "%d (id=%d,bod=%d,nc=%d)"
207 #define RCI_TRIPLE_VALS_BASE(counts,x) \
208 counts[art_Unchecked] x \
209 + counts[art_Wanted] x \
210 + counts[art_Unsolicited] x, \
211 counts[art_Unchecked] x \
212 , counts[art_Wanted] x \
213 , counts[art_Unsolicited] x
216 #define RC_INDEX(x) RC_##x,
217 RESULT_COUNTS(RC_INDEX, RC_INDEX)
222 read_ok, read_blank, read_err, nooffer_missing,
226 /*----- transmission buffers -----*/
242 /*----- core operational data structure types -----*/
245 int results[art_MaxState][RCI_max];
250 /* This is also an instance of struct oop_readable */
251 struct oop_readable readable; /* first */
252 oop_readable_call *readable_callback;
253 void *readable_callback_user;
256 Filemon_Perfile *filemon;
258 oop_read *rd; /* non-0: reading; 0: constructing, or had EOF */
260 int skippinglong, paused, fake_readable;
263 long inprogress; /* includes queue.count and also articles in conns */
264 long autodefer; /* -1 means not doing autodefer */
283 int fd; /* may be 0, meaning closed (during construction/destruction) */
284 oop_read *rd; /* likewise */
285 int oopwriting; /* since on_fd is not idempotent */
286 int max_queue, stream;
287 const char *quitting;
288 int since_activity; /* periods */
289 ArticleList waiting; /* not yet told peer */
290 ArticleList priority; /* peer says send it now */
291 ArticleList sent; /* offered/transmitted - in xmit or waiting reply */
292 struct iovec xmit[CONNIOVS];
293 XmitDetails xmitd[CONNIOVS];
297 #define SMS_LIST(X) \
305 enum StateMachineState {
306 #define SMS_DEF_ENUM(s) sm_##s,
307 SMS_LIST(SMS_DEF_ENUM)
310 extern const char *sms_names[];
312 /*========== function declarations ==========*/
314 /*----- help.c -----*/
316 void syscrash(const char *fmt, ...) NORET_PRINTF(1,2);
317 void crash(const char *fmt, ...) NORET_PRINTF(1,2);
318 void info(const char *fmt, ...) PRINTF(1,2);
319 void dbg(const char *fmt, ...) PRINTF(1,2);
321 void logv(int sysloglevel, const char *pfx, int errnoval,
322 const char *fmt, va_list al) PRINTF(5,0);
324 char *xvasprintf(const char *fmt, va_list al) PRINTF(1,0);
325 char *xasprintf(const char *fmt, ...) PRINTF(1,2);
327 int close_perhaps(int *fd);
328 void xclose(int fd, const char *what, const char *what2);
329 void xclose_perhaps(int *fd, const char *what, const char *what2);
330 pid_t xfork(const char *what); /* also runs postfork in child */
331 pid_t xfork_bare(const char *what);
333 void on_fd_read_except(int fd, oop_call_fd callback);
334 void cancel_fd_read_except(int fd);
336 void report_child_status(const char *what, int status);
337 int xwaitpid(pid_t *pid, const char *what);
339 void *zxmalloc(size_t sz);
340 void xunlink(const char *path, const char *what);
342 void xsigaction(int signo, const struct sigaction *sa);
343 void xsigsetdefault(int signo);
344 void raise_default(int signo) NORET;
346 void xgettimeofday(struct timeval *tv_r);
347 void xsetnonblock(int fd, int nonb);
349 void check_isreg(const struct stat *stab, const char *path,
351 void xfstat(int fd, struct stat *stab_r, const char *what);
352 void xfstat_isreg(int fd, struct stat *stab_r,
353 const char *path, const char *what);
354 void xlstat_isreg(const char *path, struct stat *stab,
355 int *enoent_r /* 0 means ENOENT is fatal */,
357 int samefile(const struct stat *a, const struct stat *b);
359 char *sanitise(const char *input, int len);
361 static inline int isewouldblock(int errnoval) {
362 return errnoval==EWOULDBLOCK || errnoval==EAGAIN;
365 #define INNLOGSETS(INNLOGSET) \
366 INNLOGSET(die, "fatal", LOG_ERR) \
367 INNLOGSET(warn, "warning", LOG_WARNING) \
368 INNLOGSET(notice, "notice", LOG_NOTICE) \
369 INNLOGSET(trace, "trace", LOG_NOTICE)
370 #define INNLOGSET_DECLARE(fn, pfx, sysloglevel) \
371 void duct_log_##fn(int l, const char *fmt, va_list al, int errval) \
373 INNLOGSETS(INNLOGSET_DECLARE)
375 /*----- duct.c -----*/
380 /*----- cli.c -----*/
383 void cli_stdio(void);
385 /*----- conn.c -----*/
387 void conn_closefd(Conn *conn, const char *msgprefix);
388 void check_idle_conns(void);
389 int conn_busy(Conn *conn);
390 void conn_dispose(Conn *conn);
392 void vconnfail(Conn *conn, const char *fmt, va_list al) PRINTF(2,0);
393 void connfail(Conn *conn, const char *fmt, ...) PRINTF(2,3);
395 void notice_conns_more(const char *new_kind);
396 void notice_conns_fewer(void);
397 void notice_conns_stats(void);
399 int allow_connect_start(void);
400 void connect_start(void);
402 /*----- defer.c -----*/
404 void poll_backlog_file(void);
405 void search_backlog_file(void);
406 void open_defer(void);
407 void close_defer(void);
409 /*----- filemon.c -----*/
411 int filemon_method_init(void);
412 void filemon_method_dump_info(FILE *f);
414 void filemon_start(InputFile *ipf);
415 void filemon_stop(InputFile *ipf);
417 /*----- infile.c -----*/
421 void inputfile_reading_start(InputFile *ipf);
422 void inputfile_reading_stop(InputFile *ipf);
423 void inputfile_reading_pause(InputFile *ipf);
424 void inputfile_reading_resume(InputFile *ipf);
425 /* pause and resume are idempotent, and no-op if not done _reading_start */
427 InputFile *open_input_file(const char *path);
428 void close_input_file(InputFile *ipf); /* does not free */
429 char *dbg_report_ipf(InputFile *ipf);
431 void tailing_make_readable(InputFile *ipf);
433 /*----- recv.c -----*/
435 extern const oop_rd_style peer_rd_style;
436 oop_rd_call peer_rd_ok, peer_rd_err;
438 void article_done(Article *art, int whichcount);
440 /*----- statemc.c -----*/
442 void statemc_check_flushing_done(void);
443 void statemc_check_backlog_done(void);
445 extern sig_atomic_t terminate_sig_flag;
446 void statemc_period_poll(void);
447 void statemc_lock(void);
448 void init_signals(void);
449 void statemc_init(void);
450 void showstats(void);
452 #define SMS(newstate, periods, why) \
453 (statemc_setstate(sm_##newstate,(periods),#newstate,(why)))
454 void statemc_setstate(StateMachineState newsms, int periods,
455 const char *forlog, const char *why);
457 void statemc_start_flush(const char *why); /* Normal => Flushing */
458 void spawn_inndcomm_flush(const char *why); /* Moved => Flushing */
459 int trigger_flush_ok(const char *why /* 0 means timeout */);
460 /* => Flushing,FLUSHING, ret 1; or ret 0 */
462 void preterminate(void);
464 /*----- xmit.c -----*/
466 void inputfile_queue_check_expired(InputFile *ipf);
467 void check_assign_articles(void);
468 void queue_check_input_done(void);
469 void check_reading_pause_resume(InputFile *ipf);
471 void conn_maybe_write(Conn *conn);
472 void conn_make_some_xmits(Conn *conn);
473 void *conn_write_some_xmits(Conn *conn);
475 void xmit_free(XmitDetails *d);
477 int article_check_expired(Article *art /* must be queued, not conn */);
478 void article_autodefer(InputFile *ipf, Article *art);
479 void article_defer(Article *art /* not on a queue */, int whichcount);
480 void autodefer_input_file(InputFile *ipf);
482 /*----- external linkage for debug/dump only -----*/
484 extern pid_t connecting_child;
485 extern int connecting_fdpass_sock;
486 extern pid_t inndcomm_child;
488 /*========== general operational variables ==========*/
491 extern oop_source *loop;
492 extern ConnList conns;
493 extern char *path_lock, *path_flushing, *path_defer, *path_dump;
494 extern char *globpat_backlog;
495 extern pid_t self_pid;
496 extern int *lowvol_perperiod;
497 extern int lowvol_circptr;
498 extern int lowvol_total; /* does not include current period */
499 extern int until_stats_log;
502 extern StateMachineState sms;
503 extern int until_flush;
504 extern InputFile *main_input_file, *flushing_input_file, *backlog_input_file;
505 extern Counts backlog_counts;
506 extern int backlog_counts_report;
508 extern int until_connect, until_backlog_nextscan;
509 extern double accept_proportion;
510 extern int nocheck, nocheck_reported, in_child;
513 extern int simulate_flush;
514 extern int logv_use_syslog;
515 extern const char *logv_prefix;