chiark / gitweb /
more work on stats report
[innduct.git] / innduct.h
1 /*
2  *  innduct
3  *  tailing reliable realtime streaming feeder for inn
4  *
5  *  Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * 
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.
11  * 
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.
16  * 
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/>.
19  *
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.)
24  */
25
26 #ifndef INNDUCT_H
27 #define INNDUCT_H
28
29 #define _GNU_SOURCE 1
30
31 #include "config.h"
32 #include "storage.h"
33 #include "nntp.h"
34 #include "libinn.h"
35 #include "inndcomm.h"
36
37 #include "inn/list.h"
38 #include "inn/innconf.h"
39 #include "inn/messages.h"
40
41 #include <sys/uio.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <sys/stat.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <syslog.h>
53 #include <fcntl.h>
54 #include <stdarg.h>
55 #include <assert.h>
56 #include <stdlib.h>
57 #include <stddef.h>
58 #include <glob.h>
59 #include <time.h>
60 #include <math.h>
61 #include <ctype.h>
62
63 #include <oop.h>
64 #include <oop-read.h>
65
66 /*----- general definitions, probably best not changed -----*/
67
68 #define CONNCHILD_ESTATUS_STREAM   24
69 #define CONNCHILD_ESTATUS_NOSTREAM 25
70
71 #define INNDCOMMCHILD_ESTATUS_FAIL     26
72 #define INNDCOMMCHILD_ESTATUS_NONESUCH 27
73
74 #define MAX_LINE_FEEDFILE (NNTP_MSGID_MAXLEN + sizeof(TOKEN)*2 + 10)
75 #define MAX_CLI_COMMAND 1000
76
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__))
81
82 #define NEW(ptr)              ((ptr)= zxmalloc(sizeof(*(ptr))))
83 #define NEW_DECL(type,ptr) type ptr = zxmalloc(sizeof(*(ptr)))
84
85 #define DUMPV(fmt,pfx,v) fprintf(f, " " #v "=" fmt, pfx v);
86
87 #define FOR_CONN(conn) \
88   for ((conn)=LIST_HEAD(conns); (conn); (conn)=LIST_NEXT((conn)))
89
90 /*----- doubly linked lists -----*/
91
92 #define ISNODE(T)   struct node list_node
93 #define DEFLIST(T)                              \
94    typedef struct {                             \
95      union { struct list li; T *for_type; } u;  \
96      int count;                                 \
97    } T##List
98
99 #define NODE(n) (assert((void*)&(n)->list_node == (n)), &(n)->list_node)
100
101 #define LIST_CHECKCANHAVENODE(l,n) \
102   ((void)((n) == ((l).u.for_type))) /* just for the type check */
103
104 #define LIST_ADDSOMEHOW(l,n,list_addsomehow)    \
105  ( LIST_CHECKCANHAVENODE(l,n),                  \
106    list_addsomehow(&(l).u.li, NODE((n))),       \
107    (void)(l).count++                            \
108    )
109
110 #define LIST_REMSOMEHOW(l,list_remsomehow)      \
111  ( (typeof((l).u.for_type))                     \
112    ( (l).count                                  \
113      ? ( (l).count--,                           \
114          list_remsomehow(&(l).u.li) )           \
115      : 0                                        \
116      )                                          \
117    )
118
119
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)
124
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))))
129
130 #define LIST_REMOVE(l,n)                        \
131  ( LIST_CHECKCANHAVENODE(l,n),                  \
132    list_remove(NODE((n))),                      \
133    (void)(l).count--                            \
134    )
135
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))),    \
140    (void)(l).count++                                            \
141    )
142
143 /*----- type predeclarations -----*/
144
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;
152
153 DEFLIST(Conn);
154 DEFLIST(Article);
155
156
157 /*----- configuration options -----*/
158
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;
163
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;
167
168 extern double nocheck_thresh;
169 extern double nocheck_decay;
170
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;
182
183 extern double max_bad_data_ratio;
184 extern int max_bad_data_initial;
185
186
187 /*----- statistics -----*/
188
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 */
193   art_MaxState,
194 } ArtState;
195 extern const char *const artstate_names[]; /* xmit.c */
196
197 #define RESULT_COUNTS(RCS,RCN)                  \
198   RCS(sent)                                     \
199   RCS(accepted)                                 \
200   RCN(unwanted)                                 \
201   RCN(rejected)                                 \
202   RCN(deferred)                                 \
203   RCN(missing)                                  \
204   RCN(connretry)
205
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
214
215 typedef enum {
216 #define RC_INDEX(x) RC_##x,
217   RESULT_COUNTS(RC_INDEX, RC_INDEX)
218   RCI_max
219 } ResultCountIndex;
220
221
222 /*----- transmission buffers -----*/
223
224 #define CONNIOVS 128
225
226 typedef enum {
227   xk_Const, xk_Artdata
228 } XmitKind;
229
230 struct XmitDetails {
231   XmitKind kind;
232   union {
233     ARTHANDLE *sm_art;
234   } info;
235 };
236
237
238 /*----- core operational data structure types -----*/
239
240 struct InputFile {
241   /* This is also an instance of struct oop_readable */
242   struct oop_readable readable; /* first */
243   oop_readable_call *readable_callback;
244   void *readable_callback_user;
245
246   int fd;
247   Filemon_Perfile *filemon;
248
249   oop_read *rd; /* non-0: reading; 0: constructing, or had EOF */
250   off_t offset;
251   int skippinglong, paused, fake_readable;
252
253   ArticleList queue;
254   long inprogress; /* includes queue.count and also articles in conns */
255   long autodefer; /* -1 means not doing autodefer */
256
257   int counts[art_MaxState][RCI_max];
258   int readcount_ok, readcount_blank, readcount_err, count_nooffer_missing;
259   char path[];
260 };
261
262 struct Article {
263   ISNODE(Article);
264   ArtState state;
265   int midlen, missing;
266   InputFile *ipf;
267   TOKEN token;
268   off_t offset;
269   int blanklen;
270   char messageid[1];
271 };
272
273 struct Conn {
274   ISNODE(Conn);
275   int fd; /* may be 0, meaning closed (during construction/destruction) */
276   oop_read *rd; /* likewise */
277   int oopwriting; /* since on_fd is not idempotent */
278   int max_queue, stream;
279   const char *quitting;
280   int since_activity; /* periods */
281   ArticleList waiting; /* not yet told peer */
282   ArticleList priority; /* peer says send it now */
283   ArticleList sent; /* offered/transmitted - in xmit or waiting reply */
284   struct iovec xmit[CONNIOVS];
285   XmitDetails xmitd[CONNIOVS];
286   int xmitu;
287 };
288
289 #define SMS_LIST(X)                             \
290   X(NORMAL)                                     \
291   X(FLUSHING)                                   \
292   X(FLUSHFAILED)                                \
293   X(SEPARATED)                                  \
294   X(DROPPING)                                   \
295   X(DROPPED)
296
297 enum StateMachineState {
298 #define SMS_DEF_ENUM(s) sm_##s,
299   SMS_LIST(SMS_DEF_ENUM)
300 };
301
302 extern const char *sms_names[];
303
304 /*========== function declarations ==========*/
305
306 /*----- help.c -----*/
307
308 void syscrash(const char *fmt, ...) NORET_PRINTF(1,2);
309 void crash(const char *fmt, ...) NORET_PRINTF(1,2);
310 void info(const char *fmt, ...) PRINTF(1,2);
311 void dbg(const char *fmt, ...) PRINTF(1,2);
312
313 void logv(int sysloglevel, const char *pfx, int errnoval,
314           const char *fmt, va_list al) PRINTF(5,0);
315
316 char *xvasprintf(const char *fmt, va_list al) PRINTF(1,0);
317 char *xasprintf(const char *fmt, ...) PRINTF(1,2);
318
319 int close_perhaps(int *fd);
320 void xclose(int fd, const char *what, const char *what2);
321 void xclose_perhaps(int *fd, const char *what, const char *what2);
322 pid_t xfork(const char *what);
323
324 void on_fd_read_except(int fd, oop_call_fd callback);
325 void cancel_fd_read_except(int fd);
326
327 void report_child_status(const char *what, int status);
328 int xwaitpid(pid_t *pid, const char *what);
329
330 void *zxmalloc(size_t sz);
331 void xunlink(const char *path, const char *what);
332 time_t xtime(void);
333 void xsigaction(int signo, const struct sigaction *sa);
334 void xsigsetdefault(int signo);
335 void raise_default(int signo) NORET;
336
337 void xgettimeofday(struct timeval *tv_r);
338 void xsetnonblock(int fd, int nonb);
339
340 void check_isreg(const struct stat *stab, const char *path,
341                         const char *what);
342 void xfstat(int fd, struct stat *stab_r, const char *what);
343 void xfstat_isreg(int fd, struct stat *stab_r,
344                   const char *path, const char *what);
345 void xlstat_isreg(const char *path, struct stat *stab,
346                   int *enoent_r /* 0 means ENOENT is fatal */,
347                   const char *what);
348 int samefile(const struct stat *a, const struct stat *b);
349
350 char *sanitise(const char *input, int len);
351
352 static inline int isewouldblock(int errnoval) {
353   return errnoval==EWOULDBLOCK || errnoval==EAGAIN;
354 }
355
356 #define INNLOGSETS(INNLOGSET)                   \
357   INNLOGSET(die,      "fatal",    LOG_ERR)      \
358   INNLOGSET(warn,     "warning",  LOG_WARNING)  \
359   INNLOGSET(notice,   "notice",   LOG_NOTICE)   \
360   INNLOGSET(trace,    "trace",    LOG_NOTICE)
361 #define INNLOGSET_DECLARE(fn, pfx, sysloglevel)                         \
362   void duct_log_##fn(int l, const char *fmt, va_list al, int errval)    \
363     PRINTF(3,0);
364 INNLOGSETS(INNLOGSET_DECLARE)
365
366 /*----- duct.c -----*/
367
368 void postfork(void);
369 void period(void);
370
371 /*----- cli.c -----*/
372
373 void cli_init(void);
374 void cli_stdio(void);
375
376 /*----- conn.c -----*/
377
378 void conn_closefd(Conn *conn, const char *msgprefix);
379 void check_idle_conns(void);
380 int conn_busy(Conn *conn);
381 void conn_dispose(Conn *conn);
382
383 void vconnfail(Conn *conn, const char *fmt, va_list al) PRINTF(2,0);
384 void connfail(Conn *conn, const char *fmt, ...)         PRINTF(2,3);
385
386 int allow_connect_start(void);
387 void connect_start(void);
388
389 /*----- defer.c -----*/
390
391 void poll_backlog_file(void);
392 void search_backlog_file(void);
393 void open_defer(void);
394 void close_defer(void);
395
396 /*----- filemon.c -----*/
397
398 int filemon_method_init(void);
399 void filemon_method_dump_info(FILE *f);
400
401 void filemon_start(InputFile *ipf);
402 void filemon_stop(InputFile *ipf);
403
404 /*----- infile.c -----*/
405
406 void filepoll(void);
407
408 void inputfile_reading_start(InputFile *ipf);
409 void inputfile_reading_stop(InputFile *ipf);
410 void inputfile_reading_pause(InputFile *ipf);
411 void inputfile_reading_resume(InputFile *ipf);
412   /* pause and resume are idempotent, and no-op if not done _reading_start */
413
414 InputFile *open_input_file(const char *path);
415 void close_input_file(InputFile *ipf); /* does not free */
416 char *dbg_report_ipf(InputFile *ipf);
417
418 void tailing_make_readable(InputFile *ipf);
419
420 /*----- recv.c -----*/
421
422 extern const oop_rd_style peer_rd_style;
423 oop_rd_call peer_rd_ok, peer_rd_err;
424
425 void article_done(Article *art, int whichcount);
426
427 /*----- statemc.c -----*/
428
429 void statemc_check_flushing_done(void);
430 void statemc_check_backlog_done(void);
431
432 extern sig_atomic_t terminate_sig_flag;
433 void statemc_period_poll(void);
434 void statemc_lock(void);
435 void init_signals(void);
436 void statemc_init(void);
437 void showstats(void);
438
439 #define SMS(newstate, periods, why) \
440    (statemc_setstate(sm_##newstate,(periods),#newstate,(why)))
441 void statemc_setstate(StateMachineState newsms, int periods,
442                              const char *forlog, const char *why);
443
444 void statemc_start_flush(const char *why); /* Normal => Flushing */
445 void spawn_inndcomm_flush(const char *why); /* Moved => Flushing */
446 int trigger_flush_ok(const char *why /* 0 means timeout */);
447                                   /* => Flushing,FLUSHING, ret 1; or ret 0 */
448
449 void preterminate(void);
450
451 /*----- xmit.c -----*/
452
453 void inputfile_queue_check_expired(InputFile *ipf);
454 void check_assign_articles(void);
455 void queue_check_input_done(void);
456 void check_reading_pause_resume(InputFile *ipf);
457
458 void conn_maybe_write(Conn *conn);
459 void conn_make_some_xmits(Conn *conn);
460 void *conn_write_some_xmits(Conn *conn);
461
462 void xmit_free(XmitDetails *d);
463
464 int article_check_expired(Article *art /* must be queued, not conn */);
465 void article_autodefer(InputFile *ipf, Article *art);
466 void article_defer(Article *art /* not on a queue */, int whichcount);
467 void autodefer_input_file(InputFile *ipf);
468
469 /*----- external linkage for debug/dump only -----*/
470
471 extern pid_t connecting_child;
472 extern int connecting_fdpass_sock;
473 extern pid_t inndcomm_child;
474
475 /*========== general operational variables ==========*/
476
477 /* duct.c */
478 extern oop_source *loop;
479 extern ConnList conns;
480 extern char *path_lock, *path_flushing, *path_defer, *path_dump;
481 extern char *globpat_backlog;
482 extern pid_t self_pid;
483 extern int *lowvol_perperiod;
484 extern int lowvol_circptr;
485 extern int lowvol_total; /* does not include current period */
486 extern int until_stats_log;
487
488 /* statemc.c */
489 extern StateMachineState sms;
490 extern int until_flush;
491 extern InputFile *main_input_file, *flushing_input_file, *backlog_input_file;
492 extern FILE *defer;
493 extern int until_connect, until_backlog_nextscan;
494 extern double accept_proportion;
495 extern int nocheck, nocheck_reported, in_child;
496
497 /* help.c */
498 extern int simulate_flush;
499 extern int logv_use_syslog;
500 extern const char *logv_prefix;
501
502
503 #endif /*INNDUCT_H*/