server: Option `-F' to run the server in the `foreground'.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 8 Dec 2008 12:11:28 +0000 (12:11 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 8 Dec 2008 20:11:32 +0000 (20:11 +0000)
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
client/tripectl.c
server/admin.c
server/tripe-admin.5.in
server/tripe.8.in
server/tripe.c
server/tripe.h

index edf93a9..11fa6c9 100644 (file)
@@ -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
index d9f5dd8..e171b0d 100644 (file)
@@ -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))
index 288a64f..382baca 100644 (file)
@@ -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) {
index 63cfb63..3442b02 100644 (file)
@@ -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.
index 2135c4e..edf0ea5 100644 (file)
@@ -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
index 522fe7f..ebd8efc 100644 (file)
@@ -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);
index 483880a..448f999 100644 (file)
@@ -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)