chiark / gitweb /
Comment out some excessive debugging
[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 typedef enum {
222   read_ok, read_blank, read_err, nooffer_missing,
223   ECI_max
224 } EventCountIndex;
225
226 /*----- transmission buffers -----*/
227
228 #define CONNIOVS 128
229
230 typedef enum {
231   xk_Const, xk_Artdata
232 } XmitKind;
233
234 struct XmitDetails {
235   XmitKind kind;
236   union {
237     ARTHANDLE *sm_art;
238   } info;
239 };
240
241
242 /*----- core operational data structure types -----*/
243
244 typedef struct {
245   int results[art_MaxState][RCI_max];
246   int events[ECI_max];
247 } Counts;
248
249 struct InputFile {
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;
254
255   int fd;
256   Filemon_Perfile *filemon;
257
258   oop_read *rd; /* non-0: reading; 0: constructing, or had EOF */
259   off_t offset;
260   int skippinglong, paused, fake_readable;
261
262   ArticleList queue;
263   long inprogress; /* includes queue.count and also articles in conns */
264   long autodefer; /* -1 means not doing autodefer */
265
266   Counts counts;
267   char path[];
268 };
269
270 struct Article {
271   ISNODE(Article);
272   ArtState state;
273   int midlen, missing;
274   InputFile *ipf;
275   TOKEN token;
276   off_t offset;
277   int blanklen;
278   char messageid[1];
279 };
280
281 struct Conn {
282   ISNODE(Conn);
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];
294   int xmitu;
295 };
296
297 #define SMS_LIST(X)                             \
298   X(NORMAL)                                     \
299   X(FLUSHING)                                   \
300   X(FLUSHFAILED)                                \
301   X(SEPARATED)                                  \
302   X(DROPPING)                                   \
303   X(DROPPED)
304
305 enum StateMachineState {
306 #define SMS_DEF_ENUM(s) sm_##s,
307   SMS_LIST(SMS_DEF_ENUM)
308 };
309
310 extern const char *sms_names[];
311
312 /*========== function declarations ==========*/
313
314 /*----- help.c -----*/
315
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);
320
321 void logv(int sysloglevel, const char *pfx, int errnoval,
322           const char *fmt, va_list al) PRINTF(5,0);
323
324 char *xvasprintf(const char *fmt, va_list al) PRINTF(1,0);
325 char *xasprintf(const char *fmt, ...) PRINTF(1,2);
326
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);
332
333 void on_fd_read_except(int fd, oop_call_fd callback);
334 void cancel_fd_read_except(int fd);
335
336 void report_child_status(const char *what, int status);
337 int xwaitpid(pid_t *pid, const char *what);
338
339 void *zxmalloc(size_t sz);
340 void xunlink(const char *path, const char *what);
341 time_t xtime(void);
342 void xsigaction(int signo, const struct sigaction *sa);
343 void xsigsetdefault(int signo);
344 void raise_default(int signo) NORET;
345
346 void xgettimeofday(struct timeval *tv_r);
347 void xsetnonblock(int fd, int nonb);
348
349 void check_isreg(const struct stat *stab, const char *path,
350                         const char *what);
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 */,
356                   const char *what);
357 int samefile(const struct stat *a, const struct stat *b);
358
359 char *sanitise(const char *input, int len);
360
361 static inline int isewouldblock(int errnoval) {
362   return errnoval==EWOULDBLOCK || errnoval==EAGAIN;
363 }
364
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)    \
372     PRINTF(3,0);
373 INNLOGSETS(INNLOGSET_DECLARE)
374
375 /*----- duct.c -----*/
376
377 void postfork(void);
378 void period(void);
379
380 /*----- cli.c -----*/
381
382 void cli_init(void);
383 void cli_stdio(void);
384
385 /*----- conn.c -----*/
386
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);
391
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);
394
395 void notice_conns_more(const char *new_kind);
396 void notice_conns_fewer(void);
397 void notice_conns_stats(void);
398
399 int allow_connect_start(void);
400 void connect_start(void);
401
402 /*----- defer.c -----*/
403
404 void poll_backlog_file(void);
405 void search_backlog_file(void);
406 void open_defer(void);
407 void close_defer(void);
408
409 /*----- filemon.c -----*/
410
411 int filemon_method_init(void);
412 void filemon_method_dump_info(FILE *f);
413
414 void filemon_start(InputFile *ipf);
415 void filemon_stop(InputFile *ipf);
416
417 /*----- infile.c -----*/
418
419 void filepoll(void);
420
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 */
426
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);
430
431 void tailing_make_readable(InputFile *ipf);
432
433 /*----- recv.c -----*/
434
435 extern const oop_rd_style peer_rd_style;
436 oop_rd_call peer_rd_ok, peer_rd_err;
437
438 void article_done(Article *art, int whichcount);
439
440 /*----- statemc.c -----*/
441
442 void statemc_check_flushing_done(void);
443 void statemc_check_backlog_done(void);
444
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);
451
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);
456
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 */
461
462 void preterminate(void);
463
464 /*----- xmit.c -----*/
465
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);
470
471 void conn_maybe_write(Conn *conn);
472 void conn_make_some_xmits(Conn *conn);
473 void *conn_write_some_xmits(Conn *conn);
474
475 void xmit_free(XmitDetails *d);
476
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);
481
482 /*----- external linkage for debug/dump only -----*/
483
484 extern pid_t connecting_child;
485 extern int connecting_fdpass_sock;
486 extern pid_t inndcomm_child;
487
488 /*========== general operational variables ==========*/
489
490 /* duct.c */
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;
500
501 /* statemc.c */
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;
507 extern FILE *defer;
508 extern int until_connect, until_backlog_nextscan;
509 extern double accept_proportion;
510 extern int nocheck, nocheck_reported, in_child;
511
512 /* help.c */
513 extern int simulate_flush;
514 extern int logv_use_syslog;
515 extern const char *logv_prefix;
516
517
518 #endif /*INNDUCT_H*/