Module Name:    src
Committed By:   jmmv
Date:           Tue Mar 29 07:22:31 UTC 2011

Modified Files:
        src/libexec/httpd: bozohttpd.8 bozohttpd.c bozohttpd.h daemon-bozo.c
            main.c

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

OKed by mrg@.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/libexec/httpd/bozohttpd.8
cvs rdiff -u -r1.26 -r1.27 src/libexec/httpd/bozohttpd.c
cvs rdiff -u -r1.17 -r1.18 src/libexec/httpd/bozohttpd.h
cvs rdiff -u -r1.12 -r1.13 src/libexec/httpd/daemon-bozo.c
cvs rdiff -u -r1.1.1.2 -r1.2 src/libexec/httpd/main.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/libexec/httpd/bozohttpd.8
diff -u src/libexec/httpd/bozohttpd.8:1.25 src/libexec/httpd/bozohttpd.8:1.26
--- src/libexec/httpd/bozohttpd.8:1.25	Thu Mar 10 14:37:16 2011
+++ src/libexec/httpd/bozohttpd.8	Tue Mar 29 07:22:31 2011
@@ -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/

Index: src/libexec/httpd/bozohttpd.c
diff -u src/libexec/httpd/bozohttpd.c:1.26 src/libexec/httpd/bozohttpd.c:1.27
--- src/libexec/httpd/bozohttpd.c:1.26	Wed Jan 12 19:29:24 2011
+++ src/libexec/httpd/bozohttpd.c	Tue Mar 29 07:22:31 2011
@@ -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;

Index: src/libexec/httpd/bozohttpd.h
diff -u src/libexec/httpd/bozohttpd.h:1.17 src/libexec/httpd/bozohttpd.h:1.18
--- src/libexec/httpd/bozohttpd.h:1.17	Mon Sep 20 23:11:38 2010
+++ src/libexec/httpd/bozohttpd.h	Tue Mar 29 07:22:31 2011
@@ -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" */

Index: src/libexec/httpd/daemon-bozo.c
diff -u src/libexec/httpd/daemon-bozo.c:1.12 src/libexec/httpd/daemon-bozo.c:1.13
--- src/libexec/httpd/daemon-bozo.c:1.12	Sun Feb  6 19:00:53 2011
+++ src/libexec/httpd/daemon-bozo.c	Tue Mar 29 07:22:31 2011
@@ -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);
 }
 

Index: src/libexec/httpd/main.c
diff -u src/libexec/httpd/main.c:1.1.1.2 src/libexec/httpd/main.c:1.2
--- src/libexec/httpd/main.c:1.1.1.2	Mon Sep 20 23:07:21 2010
+++ src/libexec/httpd/main.c	Tue Mar 29 07:22:31 2011
@@ -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

Reply via email to