Provide a `--pidfile' option in `fw'.
authormdw <mdw>
Thu, 5 May 2005 22:57:16 +0000 (22:57 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 9 Mar 2006 20:13:57 +0000 (20:13 +0000)
debian/changelog
fw.1
fw.c

index 87c21a9..b4a34cf 100644 (file)
@@ -1,13 +1,10 @@
 fw (1.2.7) experimental; urgency=low
 
+  * Add --pidfile option.
   * Debianization!
-
   * Fix data corruption in chan.c.
-
   * Support binding to specific addresses for inet sources and targets.
-
   * Require (or prevent) privileged incoming connections in ACLs.
-
   * Privileged outgoing connections, if started as root, with privilege
     separation if requested to drop privileges after initialization.
 
diff --git a/fw.1 b/fw.1
index d6634f3..ebb1b66 100644 (file)
--- a/fw.1
+++ b/fw.1
@@ -99,7 +99,7 @@ fw \- port forwarder
 .SH SYNOPSIS
 .
 .B fw
-.RB [ \-dlq ]
+.RB [ \-dlpq ]
 .RB [ \-f
 .IR file ]
 .RB [ \-s
@@ -175,6 +175,15 @@ initializing properly.
 .B "\-l, \-\-syslog, \-\-log"
 Emit logging information to the system log, rather than standard error.
 .TP
+.B "\-p, \-\-pidfile=" file
+Write
+.BR fw 's
+process-id to
+.I file
+during start-up.  If
+.B \-d
+is given too, then the process-id is written after forking (obviously).
+.TP
 .B "\-q, \-\-quiet"
 Don't output any logging information.  This option is not recommended
 for normal use, although it can make system call traces clearer so I use
diff --git a/fw.c b/fw.c
index bf3e477..85e6573 100644 (file)
--- a/fw.c
+++ b/fw.c
@@ -577,6 +577,7 @@ int main(int argc, char *argv[])
   scanner sc;
   uid_t drop = -1;
   gid_t dropg = -1;
+  const char *pidfile = 0;
   conffile *cf, **cff = &conffiles;
 
 #define f_bogus 1u
@@ -620,6 +621,7 @@ int main(int argc, char *argv[])
       { "file",                OPTF_ARGREQ,    0,      'f' },
       { "fork",                0,              0,      'd' },
       { "daemon",      0,              0,      'd' },
+      { "pidfile",     OPTF_ARGREQ,    0,      'p' },
       { "syslog",      0,              0,      'l' },
       { "log",         0,              0,      'l' },
       { "quiet",       0,              0,      'q' },
@@ -632,7 +634,7 @@ int main(int argc, char *argv[])
 
       { 0,             0,              0,      0 }
     };
-    int i = mdwopt(argc, argv, "+hvu GO f:dls:g:", opts, 0, 0, 0);
+    int i = mdwopt(argc, argv, "+hvu" "GO" "f:dp:ls:g:", opts, 0, 0, 0);
 
     if (i < 0)
       break;
@@ -675,6 +677,9 @@ int main(int argc, char *argv[])
       case 'd':
        f |= f_fork;
        break;
+      case 'p':
+       pidfile = optarg;
+       break;
       case 'l':
        f |= f_syslog;
        break;
@@ -786,6 +791,18 @@ int main(int argc, char *argv[])
     if (kid != 0)
       _exit(0);
   }
+  if (pidfile) {
+    FILE *fp = fopen(pidfile, "w");
+    if (!fp) {
+      die(EXIT_FAILURE, "couldn't create pidfile `%s': %s",
+         pidfile, strerror(errno));
+    }
+    fprintf(fp, "%lu\n", (unsigned long)getpid());
+    if (fflush(fp) || ferror(fp) || fclose(fp)) {
+      die(EXIT_FAILURE, "couldn't write pidfile `%s': %s",
+         pidfile, strerror(errno));
+    }
+  }
 
   if (f & f_syslog) {
     flags |= FW_SYSLOG;