server/tripe.c: Formalize the main loop machinery.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 19 May 2018 19:01:36 +0000 (20:01 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 25 Jan 2019 12:10:32 +0000 (12:10 +0000)
The new `lp_init' function has taken on a number of miscellaneous
initialization tasks.  But nothing has really changed much.

server/tripe.c
server/tripe.h

index e4733bc..b4f4cc9 100644 (file)
@@ -99,6 +99,56 @@ void iv_rmreason(void)
   if (!iv_nreasons) sel_rmtimer(&it);
 }
 
+/*----- The main loop -----------------------------------------------------*/
+
+/* --- @lp_init@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes the main loop.  Most importantly, this sets up
+ *             the select multiplexor that everything else hooks onto.
+ */
+
+void lp_init(void)
+{
+  rand_noisesrc(RAND_GLOBAL, &noise_source);
+  rand_seed(RAND_GLOBAL, MAXHASHSZ);
+  gettimeofday(&iv_next, 0); iv_next.tv_sec += T_INTERVAL;
+  signal(SIGPIPE, SIG_IGN);
+  sel_init(&sel);
+  sig_init(&sel);
+}
+
+/* --- @lp_run@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    Zero on successful termination; @-1@ if things went wrong.
+ *
+ * Use:                Cranks the main loop until it should be cranked no more.
+ */
+
+int lp_run(void)
+{
+  int nerr = 0;
+
+  for (;;) {
+    a_preselect();
+    if (!sel_select(&sel)) nerr = 0;
+    else if (errno != EINTR && errno != EAGAIN) {
+      a_warn("SERVER", "select-error", "?ERRNO", A_END);
+      nerr++;
+      if (nerr > 8) {
+       a_warn("ABORT", "repeated-select-errors", A_END);
+       abort();
+      }
+    }
+  }
+  return (0);
+}
+
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @main@ --- *
@@ -167,7 +217,7 @@ int main(int argc, char *argv[])
   struct addrinfo aihint = { 0 }, *ailist;
   unsigned f = 0;
   int i;
-  int err, selerr = 0;
+  int err;
   unsigned af;
   uid_t u = -1;
   gid_t g = -1;
@@ -328,11 +378,7 @@ int main(int argc, char *argv[])
        dir, strerror(errno));
   }
 
-  sel_init(&sel);
-  sig_init(&sel);
-  rand_noisesrc(RAND_GLOBAL, &noise_source);
-  rand_seed(RAND_GLOBAL, MAXHASHSZ);
-  signal(SIGPIPE, SIG_IGN);
+  lp_init();
 
   if (!(f & f_daemon)) {
     af = AF_WARN;
@@ -372,23 +418,9 @@ int main(int argc, char *argv[])
     a_switcherr();
   }
 
-  gettimeofday(&iv_next, 0); iv_next.tv_sec += T_INTERVAL;
   iv_addreason();
 
-  for (;;) {
-    a_preselect();
-    if (!sel_select(&sel))
-      selerr = 0;
-    else if (errno != EINTR && errno != EAGAIN) {
-      a_warn("SERVER", "select-error", "?ERRNO", A_END);
-      selerr++;
-      if (selerr > 8) {
-       a_warn("ABORT", "repeated-select-errors", A_END);
-       abort();
-      }
-    }
-  }
-
+  lp_run();
   return (0);
 }
 
index a2fdd00..bd70bb8 100644 (file)
@@ -1855,6 +1855,31 @@ extern void iv_addreason(void);
 
 extern void iv_rmreason(void);
 
+/*----- The main loop -----------------------------------------------------*/
+
+/* --- @lp_init@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes the main loop.  Most importantly, this sets up
+ *             the select multiplexor that everything else hooks onto.
+ */
+
+extern void lp_init(void);
+
+/* --- @lp_run@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    Zero on successful termination; @-1@ if things went wrong.
+ *
+ * Use:                Cranks the main loop until it should be cranked no more.
+ */
+
+extern int lp_run(void);
+
 /*----- Tunnel drivers ----------------------------------------------------*/
 
 #ifdef TUN_LINUX