Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/httpd Add pid file support: if the new -P option is ...



details:   https://anonhg.NetBSD.org/src/rev/26cac75a4ecc
branches:  trunk
changeset: 763638:26cac75a4ecc
user:      jmmv <jmmv%NetBSD.org@localhost>
date:      Tue Mar 29 07:22:31 2011 +0000

description:
Add pid file support: if the new -P option is provided, it specifies the
location of the pid file to create.

OKed by mrg@.

diffstat:

 libexec/httpd/bozohttpd.8   |  13 ++++++++-
 libexec/httpd/bozohttpd.c   |   5 +++-
 libexec/httpd/bozohttpd.h   |   3 +-
 libexec/httpd/daemon-bozo.c |  59 ++++++++++++++++++++++++++++++++++++++++++++-
 libexec/httpd/main.c        |   9 +++++-
 5 files changed, 82 insertions(+), 7 deletions(-)

diffs (221 lines):

diff -r 0ae7a1ee3efc -r 26cac75a4ecc libexec/httpd/bozohttpd.8
--- a/libexec/httpd/bozohttpd.8 Mon Mar 28 22:23:39 2011 +0000
+++ b/libexec/httpd/bozohttpd.8 Tue Mar 29 07:22:31 2011 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: bozohttpd.8,v 1.25 2011/03/10 14:37:16 reed Exp $
+.\"    $NetBSD: bozohttpd.8,v 1.26 2011/03/29 07:22:31 jmmv Exp $
 .\"
 .\"    $eterna: bozohttpd.8,v 1.99 2010/09/20 22:26:28 mrg Exp $
 .\"
@@ -26,7 +26,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd September 20, 2010
+.Dd March 25, 2011
 .Dt HTTPD 8
 .Os
 .Sh NAME
@@ -40,6 +40,7 @@
 .Op Fl I Ar port
 .Op Fl i Ar address
 .Op Fl M Ar suffix type encoding encoding11
+.Op Fl P Ar pidfile
 .Op Fl p Ar pubdir
 .Op Fl S Ar server_software
 .Op Fl t Ar chrootdir
@@ -181,6 +182,14 @@
 This option has no effect without the
 .Fl c
 option.
+.It Fl P Ar pidfile
+This option causes
+.Nm
+to create a pid file in
+.Ar pidfile
+when run in daemon mode with the
+.Fl b
+option.
 .It Fl p Ar pubdir
 This option changes the default user directory for
 .Em /~user/
diff -r 0ae7a1ee3efc -r 26cac75a4ecc libexec/httpd/bozohttpd.c
--- a/libexec/httpd/bozohttpd.c Mon Mar 28 22:23:39 2011 +0000
+++ b/libexec/httpd/bozohttpd.c Tue Mar 29 07:22:31 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.c,v 1.26 2011/01/12 19:29:24 pooka Exp $     */
+/*     $NetBSD: bozohttpd.c,v 1.27 2011/03/29 07:22:31 jmmv Exp $      */
 
 /*     $eterna: bozohttpd.c,v 1.176 2010/09/20 22:26:28 mrg Exp $      */
 
@@ -1997,6 +1997,9 @@
            strcmp(cp, "true") == 0) {
                httpd->foreground = 1;
        }
+       if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) {
+               httpd->pidfile = strdup(cp);
+       }
        if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
            strcmp(cp, "true") == 0) {
                httpd->unknown_slash = 1;
diff -r 0ae7a1ee3efc -r 26cac75a4ecc libexec/httpd/bozohttpd.h
--- a/libexec/httpd/bozohttpd.h Mon Mar 28 22:23:39 2011 +0000
+++ b/libexec/httpd/bozohttpd.h Tue Mar 29 07:22:31 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.h,v 1.17 2010/09/20 23:11:38 mrg Exp $       */
+/*     $NetBSD: bozohttpd.h,v 1.18 2011/03/29 07:22:31 jmmv Exp $      */
 
 /*     $eterna: bozohttpd.h,v 1.37 2010/09/20 22:26:28 mrg Exp $       */
 
@@ -76,6 +76,7 @@
        int              logstderr;     /* log to stderr (even if not tty) */
        int              background;    /* drop into daemon mode */
        int              foreground;    /* keep daemon mode in foreground */
+       char            *pidfile;       /* path to the pid file, if any */
        size_t           page_size;     /* page size */
        char            *slashdir;      /* www slash directory */
        char            *bindport;      /* bind port; default "http" */
diff -r 0ae7a1ee3efc -r 26cac75a4ecc libexec/httpd/daemon-bozo.c
--- a/libexec/httpd/daemon-bozo.c       Mon Mar 28 22:23:39 2011 +0000
+++ b/libexec/httpd/daemon-bozo.c       Tue Mar 29 07:22:31 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: daemon-bozo.c,v 1.12 2011/02/06 19:00:53 pooka Exp $   */
+/*     $NetBSD: daemon-bozo.c,v 1.13 2011/03/29 07:22:31 jmmv Exp $    */
 
 /*     $eterna: daemon-bozo.c,v 1.22 2010/06/21 06:45:45 mrg Exp $     */
 
@@ -40,9 +40,11 @@
 
 #include <netinet/in.h>
 
+#include <assert.h>
 #include <errno.h>
 #include <netdb.h>
 #include <poll.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -61,6 +63,9 @@
 #define INFTIM -1
 #endif
 
+static const char* pidfile_path = NULL;
+static pid_t pidfile_pid = 0;
+
 /* ARGSUSED */
 static void
 sigchild(int signo)
@@ -69,6 +74,52 @@
        }
 }
 
+/* Signal handler to exit in a controlled manner.  This ensures that
+ * any atexit(3) handlers are properly executed. */
+/* ARGSUSED */
+static void
+controlled_exit(int signo)
+{
+
+       exit(EXIT_SUCCESS);
+}
+
+static void
+remove_pidfile(void)
+{
+
+       if (pidfile_path != NULL && pidfile_pid == getpid()) {
+               (void)unlink(pidfile_path);
+               pidfile_path = NULL;
+       }
+}
+
+static void
+create_pidfile(bozohttpd_t *httpd)
+{
+       FILE *file;
+
+       assert(pidfile_path == NULL);
+
+       if (httpd->pidfile == NULL)
+               return;
+
+       if (atexit(remove_pidfile) == -1)
+               bozo_err(httpd, 1, "Failed to install pidfile handler");
+
+       if ((file = fopen(httpd->pidfile, "w")) == NULL)
+               bozo_err(httpd, 1, "Failed to create pidfile '%s'",
+                   httpd->pidfile);
+       (void)fprintf(file, "%d\n", getpid());
+       (void)fclose(file);
+
+       pidfile_path = httpd->pidfile;
+       pidfile_pid = getpid();
+
+       debug((httpd, DEBUG_FAT, "Created pid file '%s' for pid %d",
+           pidfile_path, pidfile_pid));
+}
+
 void
 bozo_daemon_init(bozohttpd_t *httpd)
 {
@@ -119,9 +170,15 @@
        if (httpd->foreground == 0)
                daemon(1, 0);
 
+       create_pidfile(httpd);
+
        bozo_warn(httpd, "started in daemon mode as `%s' port `%s' root `%s'",
            httpd->virthostname, portnum, httpd->slashdir);
 
+       signal(SIGHUP, controlled_exit);
+       signal(SIGINT, controlled_exit);
+       signal(SIGTERM, controlled_exit);
+
        signal(SIGCHLD, sigchild);
 }
 
diff -r 0ae7a1ee3efc -r 26cac75a4ecc libexec/httpd/main.c
--- a/libexec/httpd/main.c      Mon Mar 28 22:23:39 2011 +0000
+++ b/libexec/httpd/main.c      Tue Mar 29 07:22:31 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.1.1.2 2010/09/20 23:07:21 mrg Exp $ */
+/*     $NetBSD: main.c,v 1.2 2011/03/29 07:22:31 jmmv Exp $    */
 
 /*     $eterna: main.c,v 1.4 2010/07/11 00:34:28 mrg Exp $     */
 /* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp       */
@@ -89,6 +89,7 @@
        bozo_warn(httpd,
                "   -i address\t\tbind on this address (daemon mode only)");
        bozo_warn(httpd, "   -I port\t\tbind on this port (daemon mode only)");
+       bozo_warn(httpd, "   -P pidfile\t\tpath to the pid file to create");
 #endif
        bozo_warn(httpd, "   -S version\t\tset server version string");
        bozo_warn(httpd, "   -t dir\t\tchroot to `dir'");
@@ -141,7 +142,7 @@
        bozo_set_defaults(&httpd, &prefs);
 
        while ((c = getopt(argc, argv,
-                          "C:HI:M:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) {
+                          "C:HI:M:P:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) {
                switch(c) {
 
                case 'M':
@@ -207,6 +208,7 @@
                case 'f':
                case 'i':
                case 'I':
+               case 'P':
                        bozo_err(&httpd, 1, "Daemon mode is not enabled");
                        /* NOTREACHED */
 #else
@@ -238,6 +240,9 @@
                case 'I':
                        bozo_set_pref(&prefs, "port number", optarg);
                        break;
+               case 'P':
+                       bozo_set_pref(&prefs, "pid file", optarg);
+                       break;
 #endif /* NO_DAEMON_MODE */
 
 #ifdef NO_CGIBIN_SUPPORT



Home | Main Index | Thread Index | Old Index