chiark / gitweb /
Some macros replace some boilerplate formulae
[inn-innduct.git] / backends / innduct.c
index 8e92f08cb192d09e4bae7d442a1ef64192b1b23a..f7ef3acddbd990896415674212923224a97358c5 100644 (file)
@@ -1,8 +1,5 @@
 /*
  * todo
- *  - rename defraise to raise_default
- *  - xmalloc + memset -> xcalloc
- *  - macro for conn iteration
  *  - skipping_long offset calculation is wrong
  *  - reset signals TERM and INT (and HUP) in children
  *
@@ -10,7 +7,7 @@
  *  - admin-initiated flush
  *
  * debugging rune:
- *  build-lfs/backends/innduct --no-daemon -C ../inn.conf -f `pwd`/fee sit localhost
+ *  build-lfs/backends/innduct --connection-timeout=30 --no-daemon -C ../inn.conf -f `pwd`/fee sit localhost
  */
 
 /*
@@ -213,8 +210,14 @@ perl -ne 'print if m/-8\<-/..m/-\>8-/; print "\f" if m/-\^L-/' backends/innduct.
 #define PRINTF(f,a)       __attribute__((__format__(printf,f,a)))
 #define NORET_PRINTF(f,a) __attribute__((__noreturn__,__format__(printf,f,a)))
 
+#define NEW(ptr)              ((ptr)= zxmalloc(sizeof(*(ptr))))
+#define NEW_DECL(type,ptr) type ptr = zxmalloc(sizeof(*(ptr)))
+
 #define DUMPV(fmt,pfx,v) fprintf(f, " " #v "=" fmt, pfx v);
 
+#define FOR_CONN(conn) \
+  for ((conn)=LIST_HEAD(conns); (conn); (conn)=LIST_NEXT((conn)))
+
 /*----- doubly linked lists -----*/
 
 #define ISNODE(T)   struct node list_node
@@ -312,7 +315,7 @@ static void open_defer(void);
 static void close_defer(void);
 static void search_backlog_file(void);
 static void preterminate(void);
-static void defraise(int signo);
+static void raise_default(int signo);
 static char *debug_report_ipf(InputFile *ipf);
 
 static void inputfile_reading_start(InputFile *ipf);
@@ -659,6 +662,12 @@ static int xwaitpid(pid_t *pid, const char *what) {
   return status;
 }
 
+static void *zxmalloc(size_t sz) {
+  void *p= xmalloc(sz);
+  memset(p,0,sz);
+  return p;
+}
+
 static void xunlink(const char *path, const char *what) {
   int r= unlink(path);
   if (r) sysdie("can't unlink %s %s", path, what);
@@ -813,7 +822,7 @@ CCMD(dump);
 CCMD(stop) {
   preterminate();
   notice("terminating (CTRL%d)",cc->fd);
-  defraise(SIGTERM);
+  raise_default(SIGTERM);
   abort();
 }
 
@@ -903,8 +912,7 @@ static void control_stdio_destroy(ControlConn *cc) {
 }
 
 static void control_stdio(void) {
-  ControlConn *cc= xmalloc(sizeof(*cc));
-  memset(cc,0,sizeof(*cc));
+  NEW_DECL(ControlConn *,cc);
   cc->destroy= control_stdio_destroy;
 
   cc->fd= 0;
@@ -925,8 +933,7 @@ static void control_accepted_destroy(ControlConn *cc) {
 
 static void *control_master_readable(oop_source *lp, int master,
                                     oop_event ev, void *u) {
-  ControlConn *cc= xmalloc(sizeof(*cc));
-  memset(cc,0,sizeof(*cc));
+  NEW_DECL(ControlConn *,cc);
   cc->destroy= control_accepted_destroy;
 
   cc->salen= sizeof(cc->sa);
@@ -1113,10 +1120,10 @@ static void connfail(Conn *conn, const char *fmt, ...) {
 
 static void check_idle_conns(void) {
   Conn *conn;
-  for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn))
+  FOR_CONN(conn)
     conn->since_activity++;
  search_again:
-  for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn)) {
+  FOR_CONN(conn) {
     if (conn->since_activity <= need_activity_periods) continue;
 
     /* We need to shut this down */
@@ -1202,8 +1209,7 @@ static void *connchild_event(oop_source *lp, int fd, oop_event e, void *u) {
     goto x;
   }
 
-  conn= xmalloc(sizeof(*conn));
-  memset(conn,0,sizeof(*conn));
+  NEW(conn);
   LIST_INIT(conn->waiting);
   LIST_INIT(conn->priority);
   LIST_INIT(conn->sent);
@@ -1411,7 +1417,7 @@ static void check_assign_articles(void) {
      * connections in order.  That way if we have too many
      * connections, the spare ones will go away eventually.
      */
-    for (walk=LIST_HEAD(conns); walk; walk=LIST_NEXT(walk)) {
+    FOR_CONN(walk) {
       if (walk->quitting) continue;
       inqueue= walk->sent.count + walk->priority.count
             + walk->waiting.count;
@@ -2146,8 +2152,7 @@ static void filemon_method_dump_info(FILE *f) { fprintf(f,"dummy\n"); }
 static void filemon_start(InputFile *ipf) {
   assert(!ipf->filemon);
 
-  ipf->filemon= xmalloc(sizeof(*ipf->filemon));
-  memset(ipf->filemon, 0, sizeof(*ipf->filemon));
+  NEW(ipf->filemon);
   filemon_method_startfile(ipf, ipf->filemon);
 }
 
@@ -2781,7 +2786,7 @@ static void preterminate(void) {
 static int signal_self_pipe[2];
 static sig_atomic_t terminate_sig_flag;
 
-static void defraise(int signo) {
+static void raise_default(int signo) {
   struct sigaction sa;
   memset(&sa,0,sizeof(sa));
   sa.sa_handler= SIG_DFL;
@@ -2798,7 +2803,7 @@ static void *sigarrived_event(oop_source *lp, int fd, oop_event e, void *u) {
   if (terminate_sig_flag) {
     preterminate();
     notice("terminating (%s)", strsignal(terminate_sig_flag));
-    defraise(terminate_sig_flag);
+    raise_default(terminate_sig_flag);
     abort();
   }
   return OOP_CONTINUE;
@@ -2986,7 +2991,7 @@ static void postfork(void) {
   postfork_inputfile(flushing_input_file);
 
   Conn *conn;
-  for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn))
+  FOR_CONN(conn)
     conn_closefd(conn,"(in child) ");
 
   postfork_stdio(defer, "defer file ", path_defer);
@@ -3016,7 +3021,7 @@ static void every_schedule(Every *e, struct timeval base) {
 }
 
 static void every(int interval, int fixed_rate, void (*f)(void)) {
-  Every *e= xmalloc(sizeof(*e));
+  NEW_DECL(Every *,e);
   e->interval.tv_sec= interval;
   e->interval.tv_usec= 0;
   e->fixed_rate= fixed_rate;
@@ -3154,7 +3159,7 @@ CCMD(dump) {
   fprintf(f,"conns count=%d\n", conns.count);
 
   Conn *conn;
-  for (conn=LIST_HEAD(conns); conn; conn=LIST_NEXT(conn)) {
+  FOR_CONN(conn) {
 
     fprintf(f,"C%d",conn->fd);
     DUMPV("%p",conn->,rd);             DUMPV("%d",conn->,max_queue);