diff --git a/libexec/pppoed/pppoed.8 b/libexec/pppoed/pppoed.8 index da8038b947ab..808c3dde85a8 100644 --- a/libexec/pppoed/pppoed.8 +++ b/libexec/pppoed/pppoed.8 @@ -34,6 +34,7 @@ .Sh SYNOPSIS .Nm pppoed .Op Fl Fd +.Op Fl P Ar pidfile .Op Fl a Ar name .Op Fl e Ar exec .Op Fl p Ar provider @@ -120,6 +121,12 @@ If the flag is given, additional diagnostics are provided (see the .Sx DIAGNOSTICS section below). +.Pp +If +.Ar pidfile +is given, +.Nm +will write its process ID to this file on startup. .Sh DIAGNOSTICS After creating the necessary .Xr netgraph 4 diff --git a/libexec/pppoed/pppoed.c b/libexec/pppoed/pppoed.c index 31abf161cc55..86063099bc8b 100644 --- a/libexec/pppoed/pppoed.c +++ b/libexec/pppoed/pppoed.c @@ -65,11 +65,13 @@ static int usage(const char *prog) { - fprintf(stderr, "Usage: %s [-Fd] [-a name] [-e exec] [-p provider]" - " interface\n", prog); + fprintf(stderr, "Usage: %s [-Fd] [-P pidfile] [-a name] [-e exec]" + " [-p provider] interface\n", prog); return EX_USAGE; } +const char *pidfile; + static void Fairwell(int sig) { @@ -81,6 +83,9 @@ Fairwell(int sig) syslog(LOG_INFO, buf); + if (pidfile) + remove(pidfile); + signal(sig, SIG_DFL); raise(sig); } @@ -422,17 +427,22 @@ main(int argc, char **argv) prog = strrchr(argv[0], '/'); prog = prog ? prog + 1 : argv[0]; + pidfile = NULL; exec = NULL; acname = NULL; provider = ""; optF = optd = 0; - while ((ch = getopt(argc, argv, "a:Fde:p:")) != -1) { + while ((ch = getopt(argc, argv, "FP:a:de:p:")) != -1) { switch (ch) { case 'F': optF = 1; break; + case 'P': + pidfile = optarg; + break; + case 'a': acname = optarg; break; @@ -512,9 +522,26 @@ main(int argc, char **argv) if (!optF && daemon(1, 0) == -1) { perror("daemon()"); + close(cs); + close(ds); return EX_OSERR; } + + if (pidfile != NULL) { + FILE *fp; + + if ((fp = fopen(pidfile, "w")) == NULL) { + perror(pidfile); + close(cs); + close(ds); + return EX_CANTCREAT; + } else { + fprintf(fp, "%d\n", (int)getpid()); + fclose(fp); + } + } + openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON); signal(SIGHUP, Fairwell);