From 46dde0806919904d146851bf8bd93fac9915730b Mon Sep 17 00:00:00 2001 Message-Id: <46dde0806919904d146851bf8bd93fac9915730b.1715095533.git.mdw@distorted.org.uk> From: Mark Wooding Date: Mon, 8 Dec 2008 12:11:28 +0000 Subject: [PATCH 1/1] server: Option `-F' to run the server in the `foreground'. Organization: Straylight/Edgeware From: Mark Wooding This means that the server will quit when it sees EOF on standard input. Useful for testing, since it means that your processes aren't going to continue running after you let them off the leash. Also make tripectl pass `-F' to a tripe that it's starting up so that it won't continue without (say) logging. --- client/tripectl.1.in | 28 ++++++++++++++++++++++++---- client/tripectl.c | 1 + server/admin.c | 9 +++++++++ server/tripe-admin.5.in | 5 +++++ server/tripe.8.in | 9 ++++++++- server/tripe.c | 21 ++++++++++++++++----- server/tripe.h | 1 + 7 files changed, 64 insertions(+), 10 deletions(-) diff --git a/client/tripectl.1.in b/client/tripectl.1.in index edf93a90..11fa6c94 100644 --- a/client/tripectl.1.in +++ b/client/tripectl.1.in @@ -62,7 +62,7 @@ Options: .br \& .RB [ \-p -.IR program ] +.IR path ] .RB [ \-S .IB arg , arg ,\fR...] . @@ -245,9 +245,29 @@ are given, .B tripectl will start a new .B tripe -server, rather than connecting to an existing one. This is most useful -if you want to collect logging information from the server, and want to -avoid losing any. For example, the command +server, rather than connecting to an existing one. +.PP +The command line for the new server is +.IP +.I path +.B \-F +.B \-d.\& +.B \-a +.I socket +.I dash-S-options +.PP +where +.I dash-S-options +is the concatenation of the arguments of +.B \-S +options, split at commas. +.PP +Starting a +.B tripe +server through +.B tripectl +is most useful if you want to collect logging information from the +server, and want to avoid losing any. For example, the command .VS tripectl -Ds -f tripe.log .VE diff --git a/client/tripectl.c b/client/tripectl.c index d9f5dd81..e171b0da 100644 --- a/client/tripectl.c +++ b/client/tripectl.c @@ -450,6 +450,7 @@ int main(int argc, char *argv[]) DA_UNSHIFT(&spawnopts, (char *)sock); DA_UNSHIFT(&spawnopts, "-a"); DA_UNSHIFT(&spawnopts, "-d."); + DA_UNSHIFT(&spawnopts, "-F"); DA_UNSHIFT(&spawnopts, (char *)spawnpath); DA_PUSH(&spawnopts, 0); if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd)) diff --git a/server/admin.c b/server/admin.c index 288a64ff..382baca9 100644 --- a/server/admin.c +++ b/server/admin.c @@ -70,6 +70,7 @@ static sig s_term, s_int, s_hup; #define F_DAEMON 1u #define F_INIT 2u +#define F_FOREGROUND 4u #define T_RESOLVE SEC(30) #define T_PING SEC(5) @@ -1927,6 +1928,14 @@ static void a_destroypending(void) T( trace(T_ADMIN, "admin: completing destruction of connection %u", a->seq); ) + /* --- If this is the foreground client then shut down --- */ + + if (a->f & AF_FOREGROUND) { + T( trace(T_ADMIN, "admin: foreground client quit: shutting down"); ) + a_warn("SERVER", "quit", "foreground-eof", A_END); + a_quit(); + } + /* --- Abort any background jobs in progress --- */ for (bg = a->bg; bg; bg = bbg) { diff --git a/server/tripe-admin.5.in b/server/tripe-admin.5.in index 63cfb635..3442b023 100644 --- a/server/tripe-admin.5.in +++ b/server/tripe-admin.5.in @@ -1282,6 +1282,11 @@ A client of the administration interface issued a .B QUIT command. .SP +.BI "SERVER quit foreground-eof" +The server is running in foreground mode (the +.B \-F +option), and encountered end-of-file on standard input. +.SP .BI "SERVER select-error " ecode " " message An error occurred in the server's main event loop. This is bad: if it happens too many times, the server will abort. diff --git a/server/tripe.8.in b/server/tripe.8.in index 2135c4e0..edf0ea58 100644 --- a/server/tripe.8.in +++ b/server/tripe.8.in @@ -38,7 +38,7 @@ tripe \- a simple VPN daemon .SH "SYNOPSIS" . .B tripe -.RB [ \-D ] +.RB [ \-DF ] .RB [ \-d .IR dir ] .RB [ \-b @@ -173,6 +173,13 @@ standard output. A better way to start in the background is with .BR tripectl (1). .TP +.B "\-F, \-\-foreground" +Runs the server in the `foreground'; i.e., +.B tripe +will quit if it sees end-of-file on its standard input. This is +incompatible with +.BR \-D . +.TP .BI "\-d, \-\-directory=" dir Makes .I dir diff --git a/server/tripe.c b/server/tripe.c index 522fe7f1..ebd8efc7 100644 --- a/server/tripe.c +++ b/server/tripe.c @@ -72,7 +72,7 @@ static void interval(struct timeval *tv, void *v) static void usage(FILE *fp) { - pquis(fp, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\ + pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\ [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\ [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n"); } @@ -93,6 +93,7 @@ Options:\n\ --tunnels Display IP tunnel drivers and exit.\n\ \n\ -D, --daemon Run in the background.\n\ +-F, --foreground Quit when stdin reports end-of-file.\n\ -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\ -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\ -p, --port=PORT Select UDP port to listen to " @@ -122,12 +123,14 @@ int main(int argc, char *argv[]) unsigned f = 0; int i; int selerr = 0; + unsigned af; struct timeval tv; uid_t u = -1; gid_t g = -1; #define f_bogus 1u #define f_daemon 2u +#define f_foreground 4u ego(argv[0]); T( trace_on(stderr, 0); ) @@ -146,6 +149,7 @@ int main(int argc, char *argv[]) { "tunnels", 0, 0, '0' }, { "daemon", 0, 0, 'D' }, + { "foreground", 0, 0, 'F' }, { "uid", OPTF_ARGREQ, 0, 'U' }, { "setuid", OPTF_ARGREQ, 0, 'U' }, { "gid", OPTF_ARGREQ, 0, 'G' }, @@ -165,7 +169,7 @@ int main(int argc, char *argv[]) { 0, 0, 0, 0 } }; - i = mdwopt(argc, argv, "hvuDU:G:b:n:p:d:k:K:t:a:" T("T:"), + i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:" T("T:"), opts, 0, 0, 0); if (i < 0) break; @@ -189,6 +193,9 @@ int main(int argc, char *argv[]) case 'G': g = u_getgroup(optarg); break; + case 'F': + f |= f_foreground; + break; case 'b': { struct hostent *h = gethostbyname(optarg); @@ -256,6 +263,8 @@ int main(int argc, char *argv[]) usage(stderr); exit(EXIT_FAILURE); } + if (!(~f & (f_daemon | f_foreground))) + die(EXIT_FAILURE, "foreground operation for a daemon is silly"); if (chdir(dir)) { die(EXIT_FAILURE, "can't set current directory to `%s': %s", @@ -271,11 +280,13 @@ int main(int argc, char *argv[]) tunnels[i]->init(); p_init(baddr, port); if (!(f & f_daemon)) { + af = AF_WARN; #ifndef NTRACE - a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN); -#else - a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN); + af |= AF_TRACE; #endif + if (f & f_foreground) + af |= AF_FOREGROUND; + a_create(STDIN_FILENO, STDOUT_FILENO, af); } u_setugid(u, g); km_init(kr_priv, kr_pub, tag_priv); diff --git a/server/tripe.h b/server/tripe.h index 483880a4..448f9991 100644 --- a/server/tripe.h +++ b/server/tripe.h @@ -477,6 +477,7 @@ typedef struct admin { #ifndef NTRACE #define AF_TRACE 16u /* Catch tracing */ #endif +#define AF_FOREGROUND 32u /* Quit server when client closes */ #ifndef NTRACE # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN) -- [mdw]