Add pid file support: if the new -P option is provided, it specifies the location of the pid file to create. OKed by mrg@.diff -r1.25 -r1.26 src/libexec/httpd/bozohttpd.8
(jmmv)
--- src/libexec/httpd/bozohttpd.8 2011/03/10 14:37:16 1.25
+++ src/libexec/httpd/bozohttpd.8 2011/03/29 07:22:31 1.26
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | .\" $NetBSD: bozohttpd.8,v 1.25 2011/03/10 14:37:16 reed Exp $ | 1 | .\" $NetBSD: bozohttpd.8,v 1.26 2011/03/29 07:22:31 jmmv Exp $ | |
2 | .\" | 2 | .\" | |
3 | .\" $eterna: bozohttpd.8,v 1.99 2010/09/20 22:26:28 mrg Exp $ | 3 | .\" $eterna: bozohttpd.8,v 1.99 2010/09/20 22:26:28 mrg Exp $ | |
4 | .\" | 4 | .\" | |
5 | .\" Copyright (c) 1997-2010 Matthew R. Green | 5 | .\" Copyright (c) 1997-2010 Matthew R. Green | |
6 | .\" All rights reserved. | 6 | .\" All rights reserved. | |
7 | .\" | 7 | .\" | |
8 | .\" Redistribution and use in source and binary forms, with or without | 8 | .\" Redistribution and use in source and binary forms, with or without | |
9 | .\" modification, are permitted provided that the following conditions | 9 | .\" modification, are permitted provided that the following conditions | |
10 | .\" are met: | 10 | .\" are met: | |
11 | .\" 1. Redistributions of source code must retain the above copyright | 11 | .\" 1. Redistributions of source code must retain the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer. | 12 | .\" notice, this list of conditions and the following disclaimer. | |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the | |
@@ -16,40 +16,41 @@ | @@ -16,40 +16,41 @@ | |||
16 | .\" | 16 | .\" | |
17 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 17 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 18 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
19 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 19 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
20 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 20 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
21 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 21 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
22 | .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 22 | .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
23 | .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 23 | .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
24 | .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 24 | .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
25 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 25 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 26 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | .\" SUCH DAMAGE. | 27 | .\" SUCH DAMAGE. | |
28 | .\" | 28 | .\" | |
29 | .Dd September 20, 2010 | 29 | .Dd March 25, 2011 | |
30 | .Dt HTTPD 8 | 30 | .Dt HTTPD 8 | |
31 | .Os | 31 | .Os | |
32 | .Sh NAME | 32 | .Sh NAME | |
33 | .Nm httpd | 33 | .Nm httpd | |
34 | .Nd hyper text transfer protocol version 1.1 daemon | 34 | .Nd hyper text transfer protocol version 1.1 daemon | |
35 | .Sh SYNOPSIS | 35 | .Sh SYNOPSIS | |
36 | .Nm | 36 | .Nm | |
37 | .Op Fl befHnrsuVX | 37 | .Op Fl befHnrsuVX | |
38 | .Op Fl C Ar suffix cgihandler | 38 | .Op Fl C Ar suffix cgihandler | |
39 | .Op Fl c Ar cgibin | 39 | .Op Fl c Ar cgibin | |
40 | .Op Fl I Ar port | 40 | .Op Fl I Ar port | |
41 | .Op Fl i Ar address | 41 | .Op Fl i Ar address | |
42 | .Op Fl M Ar suffix type encoding encoding11 | 42 | .Op Fl M Ar suffix type encoding encoding11 | |
43 | .Op Fl P Ar pidfile | |||
43 | .Op Fl p Ar pubdir | 44 | .Op Fl p Ar pubdir | |
44 | .Op Fl S Ar server_software | 45 | .Op Fl S Ar server_software | |
45 | .Op Fl t Ar chrootdir | 46 | .Op Fl t Ar chrootdir | |
46 | .Op Fl v Ar virtualroot | 47 | .Op Fl v Ar virtualroot | |
47 | .Op Fl x Ar index | 48 | .Op Fl x Ar index | |
48 | .Op Fl Z Ar cert privkey | 49 | .Op Fl Z Ar cert privkey | |
49 | .Ar slashdir | 50 | .Ar slashdir | |
50 | .Op Ar myname | 51 | .Op Ar myname | |
51 | .Sh DESCRIPTION | 52 | .Sh DESCRIPTION | |
52 | The | 53 | The | |
53 | .Nm | 54 | .Nm | |
54 | program reads a | 55 | program reads a | |
55 | .Em HTTP | 56 | .Em HTTP | |
@@ -171,26 +172,34 @@ If any of these are a single dash | @@ -171,26 +172,34 @@ If any of these are a single dash | |||
171 | the empty string is used instead. | 172 | the empty string is used instead. | |
172 | Multiple | 173 | Multiple | |
173 | .Fl M | 174 | .Fl M | |
174 | options may be passed. | 175 | options may be passed. | |
175 | .It Fl n | 176 | .It Fl n | |
176 | This option stops | 177 | This option stops | |
177 | .Nm | 178 | .Nm | |
178 | from doing IP address to name resolution of hosts for setting the | 179 | from doing IP address to name resolution of hosts for setting the | |
179 | .Ev REMOTE_HOST | 180 | .Ev REMOTE_HOST | |
180 | variable before running a CGI program. | 181 | variable before running a CGI program. | |
181 | This option has no effect without the | 182 | This option has no effect without the | |
182 | .Fl c | 183 | .Fl c | |
183 | option. | 184 | option. | |
185 | .It Fl P Ar pidfile | |||
186 | This option causes | |||
187 | .Nm | |||
188 | to create a pid file in | |||
189 | .Ar pidfile | |||
190 | when run in daemon mode with the | |||
191 | .Fl b | |||
192 | option. | |||
184 | .It Fl p Ar pubdir | 193 | .It Fl p Ar pubdir | |
185 | This option changes the default user directory for | 194 | This option changes the default user directory for | |
186 | .Em /~user/ | 195 | .Em /~user/ | |
187 | translations from | 196 | translations from | |
188 | .Dq public_html | 197 | .Dq public_html | |
189 | to | 198 | to | |
190 | .Ar pubdir . | 199 | .Ar pubdir . | |
191 | .It Fl r | 200 | .It Fl r | |
192 | This option forces pages besides the | 201 | This option forces pages besides the | |
193 | .Dq index.html | 202 | .Dq index.html | |
194 | (see the | 203 | (see the | |
195 | .Fl X | 204 | .Fl X | |
196 | option) page to require that the Referrer: header be present and | 205 | option) page to require that the Referrer: header be present and |
--- src/libexec/httpd/bozohttpd.c 2011/01/12 19:29:24 1.26
+++ src/libexec/httpd/bozohttpd.c 2011/03/29 07:22:31 1.27
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: bozohttpd.c,v 1.26 2011/01/12 19:29:24 pooka Exp $ */ | 1 | /* $NetBSD: bozohttpd.c,v 1.27 2011/03/29 07:22:31 jmmv Exp $ */ | |
2 | 2 | |||
3 | /* $eterna: bozohttpd.c,v 1.176 2010/09/20 22:26:28 mrg Exp $ */ | 3 | /* $eterna: bozohttpd.c,v 1.176 2010/09/20 22:26:28 mrg Exp $ */ | |
4 | 4 | |||
5 | /* | 5 | /* | |
6 | * Copyright (c) 1997-2010 Matthew R. Green | 6 | * Copyright (c) 1997-2010 Matthew R. Green | |
7 | * All rights reserved. | 7 | * All rights reserved. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -1987,26 +1987,29 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs | @@ -1987,26 +1987,29 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs | |||
1987 | strcmp(cp, "true") == 0) { | 1987 | strcmp(cp, "true") == 0) { | |
1988 | httpd->logstderr = 1; | 1988 | httpd->logstderr = 1; | |
1989 | } | 1989 | } | |
1990 | if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) { | 1990 | if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) { | |
1991 | httpd->bindaddress = strdup(cp); | 1991 | httpd->bindaddress = strdup(cp); | |
1992 | } | 1992 | } | |
1993 | if ((cp = bozo_get_pref(prefs, "background")) != NULL) { | 1993 | if ((cp = bozo_get_pref(prefs, "background")) != NULL) { | |
1994 | httpd->background = atoi(cp); | 1994 | httpd->background = atoi(cp); | |
1995 | } | 1995 | } | |
1996 | if ((cp = bozo_get_pref(prefs, "foreground")) != NULL && | 1996 | if ((cp = bozo_get_pref(prefs, "foreground")) != NULL && | |
1997 | strcmp(cp, "true") == 0) { | 1997 | strcmp(cp, "true") == 0) { | |
1998 | httpd->foreground = 1; | 1998 | httpd->foreground = 1; | |
1999 | } | 1999 | } | |
2000 | if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) { | |||
2001 | httpd->pidfile = strdup(cp); | |||
2002 | } | |||
2000 | if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL && | 2003 | if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL && | |
2001 | strcmp(cp, "true") == 0) { | 2004 | strcmp(cp, "true") == 0) { | |
2002 | httpd->unknown_slash = 1; | 2005 | httpd->unknown_slash = 1; | |
2003 | } | 2006 | } | |
2004 | if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) { | 2007 | if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) { | |
2005 | httpd->virtbase = strdup(cp); | 2008 | httpd->virtbase = strdup(cp); | |
2006 | } | 2009 | } | |
2007 | if ((cp = bozo_get_pref(prefs, "enable users")) != NULL && | 2010 | if ((cp = bozo_get_pref(prefs, "enable users")) != NULL && | |
2008 | strcmp(cp, "true") == 0) { | 2011 | strcmp(cp, "true") == 0) { | |
2009 | httpd->enable_users = 1; | 2012 | httpd->enable_users = 1; | |
2010 | } | 2013 | } | |
2011 | if ((cp = bozo_get_pref(prefs, "dirty environment")) != NULL && | 2014 | if ((cp = bozo_get_pref(prefs, "dirty environment")) != NULL && | |
2012 | strcmp(cp, "true") == 0) { | 2015 | strcmp(cp, "true") == 0) { |
--- src/libexec/httpd/bozohttpd.h 2010/09/20 23:11:38 1.17
+++ src/libexec/httpd/bozohttpd.h 2011/03/29 07:22:31 1.18
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: bozohttpd.h,v 1.17 2010/09/20 23:11:38 mrg Exp $ */ | 1 | /* $NetBSD: bozohttpd.h,v 1.18 2011/03/29 07:22:31 jmmv Exp $ */ | |
2 | 2 | |||
3 | /* $eterna: bozohttpd.h,v 1.37 2010/09/20 22:26:28 mrg Exp $ */ | 3 | /* $eterna: bozohttpd.h,v 1.37 2010/09/20 22:26:28 mrg Exp $ */ | |
4 | 4 | |||
5 | /* | 5 | /* | |
6 | * Copyright (c) 1997-2010 Matthew R. Green | 6 | * Copyright (c) 1997-2010 Matthew R. Green | |
7 | * All rights reserved. | 7 | * All rights reserved. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -66,26 +66,27 @@ typedef struct bozo_consts_t { | @@ -66,26 +66,27 @@ typedef struct bozo_consts_t { | |||
66 | } bozo_consts_t; | 66 | } bozo_consts_t; | |
67 | 67 | |||
68 | /* this structure encapsulates all the bozo flags and control vars */ | 68 | /* this structure encapsulates all the bozo flags and control vars */ | |
69 | typedef struct bozohttpd_t { | 69 | typedef struct bozohttpd_t { | |
70 | char *rootdir; /* root directory */ | 70 | char *rootdir; /* root directory */ | |
71 | char *username; /* username to switch to */ | 71 | char *username; /* username to switch to */ | |
72 | int numeric; /* avoid gethostby*() */ | 72 | int numeric; /* avoid gethostby*() */ | |
73 | char *virtbase; /* virtual directory base */ | 73 | char *virtbase; /* virtual directory base */ | |
74 | int unknown_slash; /* unknown vhosts go to normal slashdir */ | 74 | int unknown_slash; /* unknown vhosts go to normal slashdir */ | |
75 | int untrustedref; /* make sure referrer = me unless url = / */ | 75 | int untrustedref; /* make sure referrer = me unless url = / */ | |
76 | int logstderr; /* log to stderr (even if not tty) */ | 76 | int logstderr; /* log to stderr (even if not tty) */ | |
77 | int background; /* drop into daemon mode */ | 77 | int background; /* drop into daemon mode */ | |
78 | int foreground; /* keep daemon mode in foreground */ | 78 | int foreground; /* keep daemon mode in foreground */ | |
79 | char *pidfile; /* path to the pid file, if any */ | |||
79 | size_t page_size; /* page size */ | 80 | size_t page_size; /* page size */ | |
80 | char *slashdir; /* www slash directory */ | 81 | char *slashdir; /* www slash directory */ | |
81 | char *bindport; /* bind port; default "http" */ | 82 | char *bindport; /* bind port; default "http" */ | |
82 | char *bindaddress; /* address for binding - INADDR_ANY */ | 83 | char *bindaddress; /* address for binding - INADDR_ANY */ | |
83 | int debug; /* debugging level */ | 84 | int debug; /* debugging level */ | |
84 | char *virthostname; /* my name */ | 85 | char *virthostname; /* my name */ | |
85 | const char *server_software;/* our brand :-) */ | 86 | const char *server_software;/* our brand :-) */ | |
86 | const char *index_html; /* our home page */ | 87 | const char *index_html; /* our home page */ | |
87 | const char *public_html; /* ~user/public_html page */ | 88 | const char *public_html; /* ~user/public_html page */ | |
88 | int enable_users; /* enable public_html */ | 89 | int enable_users; /* enable public_html */ | |
89 | int *sock; /* bound sockets */ | 90 | int *sock; /* bound sockets */ | |
90 | int nsock; /* number of above */ | 91 | int nsock; /* number of above */ | |
91 | struct pollfd *fds; /* current poll fd set */ | 92 | struct pollfd *fds; /* current poll fd set */ |
--- src/libexec/httpd/daemon-bozo.c 2011/02/06 19:00:53 1.12
+++ src/libexec/httpd/daemon-bozo.c 2011/03/29 07:22:31 1.13
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: daemon-bozo.c,v 1.12 2011/02/06 19:00:53 pooka Exp $ */ | 1 | /* $NetBSD: daemon-bozo.c,v 1.13 2011/03/29 07:22:31 jmmv Exp $ */ | |
2 | 2 | |||
3 | /* $eterna: daemon-bozo.c,v 1.22 2010/06/21 06:45:45 mrg Exp $ */ | 3 | /* $eterna: daemon-bozo.c,v 1.22 2010/06/21 06:45:45 mrg Exp $ */ | |
4 | 4 | |||
5 | /* | 5 | /* | |
6 | * Copyright (c) 1997-2010 Matthew R. Green | 6 | * Copyright (c) 1997-2010 Matthew R. Green | |
7 | * All rights reserved. | 7 | * All rights reserved. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -30,55 +30,106 @@ | @@ -30,55 +30,106 @@ | |||
30 | * | 30 | * | |
31 | */ | 31 | */ | |
32 | 32 | |||
33 | /* this code implements daemon mode for bozohttpd */ | 33 | /* this code implements daemon mode for bozohttpd */ | |
34 | 34 | |||
35 | #ifndef NO_DAEMON_MODE | 35 | #ifndef NO_DAEMON_MODE | |
36 | 36 | |||
37 | #include <sys/param.h> | 37 | #include <sys/param.h> | |
38 | #include <sys/socket.h> | 38 | #include <sys/socket.h> | |
39 | #include <sys/wait.h> | 39 | #include <sys/wait.h> | |
40 | 40 | |||
41 | #include <netinet/in.h> | 41 | #include <netinet/in.h> | |
42 | 42 | |||
43 | #include <assert.h> | |||
43 | #include <errno.h> | 44 | #include <errno.h> | |
44 | #include <netdb.h> | 45 | #include <netdb.h> | |
45 | #include <poll.h> | 46 | #include <poll.h> | |
47 | #include <stdio.h> | |||
46 | #include <stdlib.h> | 48 | #include <stdlib.h> | |
47 | #include <string.h> | 49 | #include <string.h> | |
48 | #include <unistd.h> | 50 | #include <unistd.h> | |
49 | 51 | |||
50 | #include "bozohttpd.h" | 52 | #include "bozohttpd.h" | |
51 | 53 | |||
52 | static void sigchild(int); /* SIGCHLD handler */ | 54 | static void sigchild(int); /* SIGCHLD handler */ | |
53 | 55 | |||
54 | #ifndef POLLRDNORM | 56 | #ifndef POLLRDNORM | |
55 | #define POLLRDNORM 0 | 57 | #define POLLRDNORM 0 | |
56 | #endif | 58 | #endif | |
57 | #ifndef POLLRDBAND | 59 | #ifndef POLLRDBAND | |
58 | #define POLLRDBAND 0 | 60 | #define POLLRDBAND 0 | |
59 | #endif | 61 | #endif | |
60 | #ifndef INFTIM | 62 | #ifndef INFTIM | |
61 | #define INFTIM -1 | 63 | #define INFTIM -1 | |
62 | #endif | 64 | #endif | |
63 | 65 | |||
66 | static const char* pidfile_path = NULL; | |||
67 | static pid_t pidfile_pid = 0; | |||
68 | ||||
64 | /* ARGSUSED */ | 69 | /* ARGSUSED */ | |
65 | static void | 70 | static void | |
66 | sigchild(int signo) | 71 | sigchild(int signo) | |
67 | { | 72 | { | |
68 | while (waitpid(-1, NULL, WNOHANG) > 0) { | 73 | while (waitpid(-1, NULL, WNOHANG) > 0) { | |
69 | } | 74 | } | |
70 | } | 75 | } | |
71 | 76 | |||
77 | /* Signal handler to exit in a controlled manner. This ensures that | |||
78 | * any atexit(3) handlers are properly executed. */ | |||
79 | /* ARGSUSED */ | |||
80 | static void | |||
81 | controlled_exit(int signo) | |||
82 | { | |||
83 | ||||
84 | exit(EXIT_SUCCESS); | |||
85 | } | |||
86 | ||||
87 | static void | |||
88 | remove_pidfile(void) | |||
89 | { | |||
90 | ||||
91 | if (pidfile_path != NULL && pidfile_pid == getpid()) { | |||
92 | (void)unlink(pidfile_path); | |||
93 | pidfile_path = NULL; | |||
94 | } | |||
95 | } | |||
96 | ||||
97 | static void | |||
98 | create_pidfile(bozohttpd_t *httpd) | |||
99 | { | |||
100 | FILE *file; | |||
101 | ||||
102 | assert(pidfile_path == NULL); | |||
103 | ||||
104 | if (httpd->pidfile == NULL) | |||
105 | return; | |||
106 | ||||
107 | if (atexit(remove_pidfile) == -1) | |||
108 | bozo_err(httpd, 1, "Failed to install pidfile handler"); | |||
109 | ||||
110 | if ((file = fopen(httpd->pidfile, "w")) == NULL) | |||
111 | bozo_err(httpd, 1, "Failed to create pidfile '%s'", | |||
112 | httpd->pidfile); | |||
113 | (void)fprintf(file, "%d\n", getpid()); | |||
114 | (void)fclose(file); | |||
115 | ||||
116 | pidfile_path = httpd->pidfile; | |||
117 | pidfile_pid = getpid(); | |||
118 | ||||
119 | debug((httpd, DEBUG_FAT, "Created pid file '%s' for pid %d", | |||
120 | pidfile_path, pidfile_pid)); | |||
121 | } | |||
122 | ||||
72 | void | 123 | void | |
73 | bozo_daemon_init(bozohttpd_t *httpd) | 124 | bozo_daemon_init(bozohttpd_t *httpd) | |
74 | { | 125 | { | |
75 | struct addrinfo h, *r, *r0; | 126 | struct addrinfo h, *r, *r0; | |
76 | const char *portnum; | 127 | const char *portnum; | |
77 | int e, i, on = 1; | 128 | int e, i, on = 1; | |
78 | 129 | |||
79 | if (!httpd->background) | 130 | if (!httpd->background) | |
80 | return; | 131 | return; | |
81 | 132 | |||
82 | portnum = (httpd->bindport) ? httpd->bindport : "http"; | 133 | portnum = (httpd->bindport) ? httpd->bindport : "http"; | |
83 | 134 | |||
84 | memset(&h, 0, sizeof(h)); | 135 | memset(&h, 0, sizeof(h)); | |
@@ -109,29 +160,35 @@ bozo_daemon_init(bozohttpd_t *httpd) | @@ -109,29 +160,35 @@ bozo_daemon_init(bozohttpd_t *httpd) | |||
109 | httpd->fds[i].events = POLLIN | POLLPRI | POLLRDNORM | | 160 | httpd->fds[i].events = POLLIN | POLLPRI | POLLRDNORM | | |
110 | POLLRDBAND | POLLERR; | 161 | POLLRDBAND | POLLERR; | |
111 | httpd->fds[i].fd = httpd->sock[i]; | 162 | httpd->fds[i].fd = httpd->sock[i]; | |
112 | i++; | 163 | i++; | |
113 | } | 164 | } | |
114 | if (i == 0) | 165 | if (i == 0) | |
115 | bozo_err(httpd, 1, "could not find any addresses to bind"); | 166 | bozo_err(httpd, 1, "could not find any addresses to bind"); | |
116 | httpd->nsock = i; | 167 | httpd->nsock = i; | |
117 | freeaddrinfo(r0); | 168 | freeaddrinfo(r0); | |
118 | 169 | |||
119 | if (httpd->foreground == 0) | 170 | if (httpd->foreground == 0) | |
120 | daemon(1, 0); | 171 | daemon(1, 0); | |
121 | 172 | |||
173 | create_pidfile(httpd); | |||
174 | ||||
122 | bozo_warn(httpd, "started in daemon mode as `%s' port `%s' root `%s'", | 175 | bozo_warn(httpd, "started in daemon mode as `%s' port `%s' root `%s'", | |
123 | httpd->virthostname, portnum, httpd->slashdir); | 176 | httpd->virthostname, portnum, httpd->slashdir); | |
124 | 177 | |||
178 | signal(SIGHUP, controlled_exit); | |||
179 | signal(SIGINT, controlled_exit); | |||
180 | signal(SIGTERM, controlled_exit); | |||
181 | ||||
125 | signal(SIGCHLD, sigchild); | 182 | signal(SIGCHLD, sigchild); | |
126 | } | 183 | } | |
127 | 184 | |||
128 | void | 185 | void | |
129 | bozo_daemon_closefds(bozohttpd_t *httpd) | 186 | bozo_daemon_closefds(bozohttpd_t *httpd) | |
130 | { | 187 | { | |
131 | int i; | 188 | int i; | |
132 | 189 | |||
133 | for (i = 0; i < httpd->nsock; i++) | 190 | for (i = 0; i < httpd->nsock; i++) | |
134 | close(httpd->sock[i]); | 191 | close(httpd->sock[i]); | |
135 | } | 192 | } | |
136 | 193 | |||
137 | static void | 194 | static void |
--- src/libexec/httpd/main.c 2010/09/20 23:07:21 1.1.1.2
+++ src/libexec/httpd/main.c 2011/03/29 07:22:31 1.2
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: main.c,v 1.1.1.2 2010/09/20 23:07:21 mrg Exp $ */ | 1 | /* $NetBSD: main.c,v 1.2 2011/03/29 07:22:31 jmmv Exp $ */ | |
2 | 2 | |||
3 | /* $eterna: main.c,v 1.4 2010/07/11 00:34:28 mrg Exp $ */ | 3 | /* $eterna: main.c,v 1.4 2010/07/11 00:34:28 mrg Exp $ */ | |
4 | /* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp */ | 4 | /* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp */ | |
5 | 5 | |||
6 | /* | 6 | /* | |
7 | * Copyright (c) 1997-2010 Matthew R. Green | 7 | * Copyright (c) 1997-2010 Matthew R. Green | |
8 | * All rights reserved. | 8 | * All rights reserved. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -79,26 +79,27 @@ usage(bozohttpd_t *httpd, char *progname | @@ -79,26 +79,27 @@ usage(bozohttpd_t *httpd, char *progname | |||
79 | #ifndef NO_CGIBIN_SUPPORT | 79 | #ifndef NO_CGIBIN_SUPPORT | |
80 | #ifndef NO_DYNAMIC_CONTENT | 80 | #ifndef NO_DYNAMIC_CONTENT | |
81 | bozo_warn(httpd, " -C arg prog\t\tadd this CGI handler"); | 81 | bozo_warn(httpd, " -C arg prog\t\tadd this CGI handler"); | |
82 | #endif | 82 | #endif | |
83 | bozo_warn(httpd, | 83 | bozo_warn(httpd, | |
84 | " -c cgibin\t\tenable cgi-bin support in this directory"); | 84 | " -c cgibin\t\tenable cgi-bin support in this directory"); | |
85 | #endif | 85 | #endif | |
86 | #ifndef NO_DAEMON_MODE | 86 | #ifndef NO_DAEMON_MODE | |
87 | bozo_warn(httpd, " -b\t\t\tbackground and go into daemon mode"); | 87 | bozo_warn(httpd, " -b\t\t\tbackground and go into daemon mode"); | |
88 | bozo_warn(httpd, " -f\t\t\tkeep daemon mode in the foreground"); | 88 | bozo_warn(httpd, " -f\t\t\tkeep daemon mode in the foreground"); | |
89 | bozo_warn(httpd, | 89 | bozo_warn(httpd, | |
90 | " -i address\t\tbind on this address (daemon mode only)"); | 90 | " -i address\t\tbind on this address (daemon mode only)"); | |
91 | bozo_warn(httpd, " -I port\t\tbind on this port (daemon mode only)"); | 91 | bozo_warn(httpd, " -I port\t\tbind on this port (daemon mode only)"); | |
92 | bozo_warn(httpd, " -P pidfile\t\tpath to the pid file to create"); | |||
92 | #endif | 93 | #endif | |
93 | bozo_warn(httpd, " -S version\t\tset server version string"); | 94 | bozo_warn(httpd, " -S version\t\tset server version string"); | |
94 | bozo_warn(httpd, " -t dir\t\tchroot to `dir'"); | 95 | bozo_warn(httpd, " -t dir\t\tchroot to `dir'"); | |
95 | bozo_warn(httpd, " -U username\t\tchange user to `user'"); | 96 | bozo_warn(httpd, " -U username\t\tchange user to `user'"); | |
96 | bozo_warn(httpd, | 97 | bozo_warn(httpd, | |
97 | " -e\t\t\tdon't clean the environment (-t and -U only)"); | 98 | " -e\t\t\tdon't clean the environment (-t and -U only)"); | |
98 | bozo_warn(httpd, | 99 | bozo_warn(httpd, | |
99 | " -v virtualroot\tenable virtual host support " | 100 | " -v virtualroot\tenable virtual host support " | |
100 | "in this directory"); | 101 | "in this directory"); | |
101 | bozo_warn(httpd, | 102 | bozo_warn(httpd, | |
102 | " -r\t\t\tmake sure sub-pages come from " | 103 | " -r\t\t\tmake sure sub-pages come from " | |
103 | "this host via referrer"); | 104 | "this host via referrer"); | |
104 | #ifndef NO_DIRINDEX_SUPPORT | 105 | #ifndef NO_DIRINDEX_SUPPORT | |
@@ -131,27 +132,27 @@ main(int argc, char **argv) | @@ -131,27 +132,27 @@ main(int argc, char **argv) | |||
131 | (void) memset(&httpd, 0x0, sizeof(httpd)); | 132 | (void) memset(&httpd, 0x0, sizeof(httpd)); | |
132 | (void) memset(&prefs, 0x0, sizeof(prefs)); | 133 | (void) memset(&prefs, 0x0, sizeof(prefs)); | |
133 | 134 | |||
134 | if ((progname = strrchr(argv[0], '/')) == NULL) | 135 | if ((progname = strrchr(argv[0], '/')) == NULL) | |
135 | progname = argv[0]; | 136 | progname = argv[0]; | |
136 | else | 137 | else | |
137 | progname++; | 138 | progname++; | |
138 | 139 | |||
139 | openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP); | 140 | openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP); | |
140 | 141 | |||
141 | bozo_set_defaults(&httpd, &prefs); | 142 | bozo_set_defaults(&httpd, &prefs); | |
142 | 143 | |||
143 | while ((c = getopt(argc, argv, | 144 | while ((c = getopt(argc, argv, | |
144 | "C:HI:M:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) { | 145 | "C:HI:M:P:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) { | |
145 | switch(c) { | 146 | switch(c) { | |
146 | 147 | |||
147 | case 'M': | 148 | case 'M': | |
148 | #ifdef NO_DYNAMIC_CONTENT | 149 | #ifdef NO_DYNAMIC_CONTENT | |
149 | bozo_err(&httpd, 1, | 150 | bozo_err(&httpd, 1, | |
150 | "dynamic mime content support is not enabled"); | 151 | "dynamic mime content support is not enabled"); | |
151 | /* NOTREACHED */ | 152 | /* NOTREACHED */ | |
152 | #else | 153 | #else | |
153 | /* make sure there's four arguments */ | 154 | /* make sure there's four arguments */ | |
154 | if (argc - optind < 3) | 155 | if (argc - optind < 3) | |
155 | usage(&httpd, progname); | 156 | usage(&httpd, progname); | |
156 | bozo_add_content_map_mime(&httpd, optarg, argv[optind], | 157 | bozo_add_content_map_mime(&httpd, optarg, argv[optind], | |
157 | argv[optind+1], argv[optind+2]); | 158 | argv[optind+1], argv[optind+2]); | |
@@ -197,26 +198,27 @@ main(int argc, char **argv) | @@ -197,26 +198,27 @@ main(int argc, char **argv) | |||
197 | bozo_set_pref(&prefs, "virtual base", optarg); | 198 | bozo_set_pref(&prefs, "virtual base", optarg); | |
198 | break; | 199 | break; | |
199 | 200 | |||
200 | case 'x': | 201 | case 'x': | |
201 | bozo_set_pref(&prefs, "index.html", optarg); | 202 | bozo_set_pref(&prefs, "index.html", optarg); | |
202 | break; | 203 | break; | |
203 | 204 | |||
204 | #ifdef NO_DAEMON_MODE | 205 | #ifdef NO_DAEMON_MODE | |
205 | case 'b': | 206 | case 'b': | |
206 | case 'e': | 207 | case 'e': | |
207 | case 'f': | 208 | case 'f': | |
208 | case 'i': | 209 | case 'i': | |
209 | case 'I': | 210 | case 'I': | |
211 | case 'P': | |||
210 | bozo_err(&httpd, 1, "Daemon mode is not enabled"); | 212 | bozo_err(&httpd, 1, "Daemon mode is not enabled"); | |
211 | /* NOTREACHED */ | 213 | /* NOTREACHED */ | |
212 | #else | 214 | #else | |
213 | case 'b': | 215 | case 'b': | |
214 | /* | 216 | /* | |
215 | * test suite support - undocumented | 217 | * test suite support - undocumented | |
216 | * background == 2 (aka, -b -b) means to | 218 | * background == 2 (aka, -b -b) means to | |
217 | * only process 1 per kid | 219 | * only process 1 per kid | |
218 | */ | 220 | */ | |
219 | if (bozo_get_pref(&prefs, "background") == NULL) { | 221 | if (bozo_get_pref(&prefs, "background") == NULL) { | |
220 | bozo_set_pref(&prefs, "background", "1"); | 222 | bozo_set_pref(&prefs, "background", "1"); | |
221 | } else { | 223 | } else { | |
222 | bozo_set_pref(&prefs, "background", "2"); | 224 | bozo_set_pref(&prefs, "background", "2"); | |
@@ -228,26 +230,29 @@ main(int argc, char **argv) | @@ -228,26 +230,29 @@ main(int argc, char **argv) | |||
228 | break; | 230 | break; | |
229 | 231 | |||
230 | case 'f': | 232 | case 'f': | |
231 | bozo_set_pref(&prefs, "foreground", "true"); | 233 | bozo_set_pref(&prefs, "foreground", "true"); | |
232 | break; | 234 | break; | |
233 | 235 | |||
234 | case 'i': | 236 | case 'i': | |
235 | bozo_set_pref(&prefs, "bind address", optarg); | 237 | bozo_set_pref(&prefs, "bind address", optarg); | |
236 | break; | 238 | break; | |
237 | 239 | |||
238 | case 'I': | 240 | case 'I': | |
239 | bozo_set_pref(&prefs, "port number", optarg); | 241 | bozo_set_pref(&prefs, "port number", optarg); | |
240 | break; | 242 | break; | |
243 | case 'P': | |||
244 | bozo_set_pref(&prefs, "pid file", optarg); | |||
245 | break; | |||
241 | #endif /* NO_DAEMON_MODE */ | 246 | #endif /* NO_DAEMON_MODE */ | |
242 | 247 | |||
243 | #ifdef NO_CGIBIN_SUPPORT | 248 | #ifdef NO_CGIBIN_SUPPORT | |
244 | case 'c': | 249 | case 'c': | |
245 | case 'C': | 250 | case 'C': | |
246 | bozo_err(&httpd, 1, "CGI is not enabled"); | 251 | bozo_err(&httpd, 1, "CGI is not enabled"); | |
247 | /* NOTREACHED */ | 252 | /* NOTREACHED */ | |
248 | #else | 253 | #else | |
249 | case 'c': | 254 | case 'c': | |
250 | bozo_cgi_setbin(&httpd, optarg); | 255 | bozo_cgi_setbin(&httpd, optarg); | |
251 | break; | 256 | break; | |
252 | 257 | |||
253 | case 'C': | 258 | case 'C': |