From c9aded9f1c5c6eea294bf85e5e34efb9f52138c6 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Sat, 19 May 2018 20:01:36 +0100 Subject: [PATCH] server/tripe.c: Formalize the main loop machinery. Organization: Straylight/Edgeware From: Mark Wooding The new `lp_init' function has taken on a number of miscellaneous initialization tasks. But nothing has really changed much. --- server/tripe.c | 74 ++++++++++++++++++++++++++++++++++++-------------- server/tripe.h | 25 +++++++++++++++++ 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/server/tripe.c b/server/tripe.c index e4733bce..b4f4cc97 100644 --- a/server/tripe.c +++ b/server/tripe.c @@ -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); } diff --git a/server/tripe.h b/server/tripe.h index a2fdd006..bd70bb84 100644 --- a/server/tripe.h +++ b/server/tripe.h @@ -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 -- [mdw]