From: Mark Wooding Date: Sat, 12 May 2018 19:12:22 +0000 (+0100) Subject: server/: Split peer and admin initialization into smaller pieces. X-Git-Tag: 1.5.0~33 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/commitdiff_plain/7737eb87d283d46b7a90caba20ac75c3214451d3 server/: Split peer and admin initialization into smaller pieces. Split admin initialization into: * setting up internal data structures, which is `a_init' as before; * establishing the daemonic signal handlers, which is now `a_signals'; * creating the admin socket and listening for incoming connections, which is now `a_listen'; and * switching alert reporting over to admin clients rather than stderr, which is now `a_switcherr'. Given that `a_listen' now need not actually be called, only unlink the socket object if we actually created it. Split peer initialization into: * setting up the data structures, which is `p_init' as before; and * creating the UDP sockets for communicating with other peers, which is now `p_bind'. This will make sense in context. squash! server/: Split peer and admin initialization into smaller pieces. server/admin.c: Split out signal-handler establishment. --- diff --git a/server/admin.c b/server/admin.c index e5306206..acdb973a 100644 --- a/server/admin.c +++ b/server/admin.c @@ -68,7 +68,7 @@ static const trace_opt w_opts[] = { static admin *admins; static admin *a_dead; static sel_file sock; -static const char *sockname; +static const char *sockname = 0; static sym_table a_svcs; static unsigned flags = 0; static admin *a_stdin = 0; @@ -559,7 +559,7 @@ void a_notify(const char *fmt, ...) void a_quit(void) { close(sock.fd); - unlink(sockname); + if (sockname) unlink(sockname); FOREACH_PEER(p, { p_destroy(p, 1); }); ps_quit(); exit(0); @@ -2405,7 +2405,9 @@ static void a_line(char *p, size_t len, void *vp) * Returns: --- * * Use: Creates a new admin connection. It's safe to call this - * before @a_init@. + * before @a_init@ -- and, indeed, this makes sense if you also + * call @a_switcherr@ to report initialization errors through + * the administration machinery. */ void a_create(int fd_in, int fd_out, unsigned f) @@ -2483,7 +2485,7 @@ void a_preselect(void) { if (a_dead) a_destroypending(); } void a_daemon(void) { flags |= F_DAEMON; } -/* --- @a_init@ --- * +/* --- @a_listen@ --- * * * Arguments: @const char *name@ = socket name to create * @uid_t u@ = user to own the socket @@ -2495,21 +2497,13 @@ void a_daemon(void) { flags |= F_DAEMON; } * Use: Creates the admin listening socket. */ -void a_init(const char *name, uid_t u, gid_t g, mode_t m) +void a_listen(const char *name, uid_t u, gid_t g, mode_t m) { int fd; int n = 5; struct sockaddr_un sun; - struct sigaction sa; size_t sz; mode_t omask; -#ifdef HAVE_LIBADNS - int err; -#endif - - /* --- Create services table --- */ - - sym_create(&a_svcs); /* --- Set up the socket address --- */ @@ -2572,6 +2566,72 @@ again: sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0); sel_addfile(&sock); sockname = name; +} + +/* --- @a_switcherr@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Arrange to report warnings, trace messages, etc. to + * administration clients rather than the standard-error stream. + * + * Obviously this makes no sense unless there is at least one + * client established. Calling @a_listen@ won't help with this, + * because the earliest a new client can connect is during the + * first select-loop iteration, which is too late: some initial + * client must have been added manually using @a_create@. + */ + +void a_switcherr(void) +{ + T( trace_custom(a_trace, 0); + trace(T_ADMIN, "admin: enabled custom tracing"); ) + flags |= F_INIT; +} + +/* --- @a_signals@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Establishes handlers for the obvious signals. + */ + +void a_signals(void) +{ + struct sigaction sa; + + sig_add(&s_term, SIGTERM, a_sigdie, 0); + sig_add(&s_hup, SIGHUP, a_sighup, 0); + sigaction(SIGINT, 0, &sa); + if (sa.sa_handler != SIG_IGN) + sig_add(&s_int, SIGINT, a_sigdie, 0); +} + +/* --- @a_init@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Creates the admin listening socket. + */ + +void a_init(void) +{ +#ifdef HAVE_LIBADNS + int err; +#endif + + /* --- Create services table --- */ + + sym_create(&a_svcs); + + /* --- Prepare the background name resolver --- */ + #ifdef HAVE_LIBADNS if ((err = adns_init(&ads, (adns_if_permit_ipv4 | adns_if_permit_ipv6 | @@ -2583,17 +2643,6 @@ again: #else bres_init(&sel); #endif - T( trace_custom(a_trace, 0); - trace(T_ADMIN, "admin: enabled custom tracing"); ) - flags |= F_INIT; - - /* --- Set up signal handlers --- */ - - sig_add(&s_term, SIGTERM, a_sigdie, 0); - sig_add(&s_hup, SIGHUP, a_sighup, 0); - sigaction(SIGINT, 0, &sa); - if (sa.sa_handler != SIG_IGN) - sig_add(&s_int, SIGINT, a_sigdie, 0); } /*----- That's all, folks -------------------------------------------------*/ diff --git a/server/peer.c b/server/peer.c index 377dcc59..27ac4a78 100644 --- a/server/peer.c +++ b/server/peer.c @@ -816,7 +816,7 @@ void p_setifname(peer *p, const char *name) const addr *p_addr(peer *p) { return (&p->spec.sa); } -/* --- @p_init@ --- * +/* --- @p_bind@ --- * * * Arguments: @struct addrinfo *ailist@ = addresses to bind to * @@ -825,7 +825,7 @@ const addr *p_addr(peer *p) { return (&p->spec.sa); } * Use: Initializes the peer system; creates the socket. */ -void p_init(struct addrinfo *ailist) +void p_bind(struct addrinfo *ailist) { int fd; int len = PKBUFSZ; @@ -883,6 +883,19 @@ void p_init(struct addrinfo *ailist) } } +} + +/* --- @p_init@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Initializes the peer system. + */ + +void p_init(void) +{ sym_create(&byname); am_create(&byaddr); } diff --git a/server/tripe.c b/server/tripe.c index ed54c5ed..60eaf8f9 100644 --- a/server/tripe.c +++ b/server/tripe.c @@ -294,7 +294,8 @@ int main(int argc, char *argv[]) signal(SIGPIPE, SIG_IGN); for (i = 0; tunnels[i]; i++) tunnels[i]->init(); - p_init(ailist); freeaddrinfo(ailist); + p_init(); + p_bind(ailist); freeaddrinfo(ailist); if (!(f & f_daemon)) { af = AF_WARN; #ifndef NTRACE @@ -312,7 +313,10 @@ int main(int argc, char *argv[]) } } - a_init(csock, u, g, csockmode); + a_init(); + a_signals(); + a_listen(csock, u, g, csockmode); + a_switcherr(); u_setugid(u, g); km_init(kr_priv, kr_pub, tag_priv); kx_init(); diff --git a/server/tripe.h b/server/tripe.h index 9549ec37..05956826 100644 --- a/server/tripe.h +++ b/server/tripe.h @@ -1298,7 +1298,9 @@ extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...); * Returns: --- * * Use: Creates a new admin connection. It's safe to call this - * before @a_init@. + * before @a_init@ -- and, indeed, this makes sense if you also + * call @a_switcherr@ to report initialization errors through + * the administration machinery. */ extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/); @@ -1338,6 +1340,50 @@ extern void a_preselect(void); extern void a_daemon(void); +/* --- @a_listen@ --- * + * + * Arguments: @const char *name@ = socket name to create + * @uid_t u@ = user to own the socket + * @gid_t g@ = group to own the socket + * @mode_t m@ = permissions to set on the socket + * + * Returns: --- + * + * Use: Creates the admin listening socket. + */ + +extern void a_listen(const char */*sock*/, + uid_t /*u*/, gid_t /*g*/, mode_t /*m*/); + +/* --- @a_switcherr@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Arrange to report warnings, trace messages, etc. to + * administration clients rather than the standard-error stream. + * + * Obviously this makes no sense unless there is at least one + * client established. Calling @a_listen@ won't help with this, + * because the earliest a new client can connect is during the + * first select-loop iteration, which is too late: some initial + * client must have been added manually using @a_create@. + */ + +extern void a_switcherr(void); + +/* --- @a_signals@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Establishes handlers for the obvious signals. + */ + +extern void a_signals(void); + /* --- @a_init@ --- * * * Arguments: @const char *sock@ = socket name to create @@ -1350,8 +1396,7 @@ extern void a_daemon(void); * Use: Creates the admin listening socket. */ -extern void a_init(const char */*sock*/, - uid_t /*u*/, gid_t /*g*/, mode_t /*m*/); +extern void a_init(void); /*----- Mapping with addresses as keys ------------------------------------*/ @@ -1640,16 +1685,27 @@ extern void p_setifname(peer */*p*/, const char */*name*/); extern const addr *p_addr(peer */*p*/); -/* --- @p_init@ --- * +/* --- @p_bind@ --- * * * Arguments: @struct addrinfo *ailist@ = addresses to bind to * * Returns: --- * - * Use: Initializes the peer system; creates the socket. + * Use: Binds to the main UDP sockets. + */ + +extern void p_bind(struct addrinfo */*ailist*/); + +/* --- @p_init@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Initializes the peer system. */ -extern void p_init(struct addrinfo */*ailist*/); +extern void p_init(void); /* --- @p_create@ --- * *