Home | History | Annotate | Line # | Download | only in httpd
daemon-bozo.c revision 1.14
      1  1.14     joerg /*	$NetBSD: daemon-bozo.c,v 1.14 2011/08/27 15:33:59 joerg Exp $	*/
      2   1.2       tls 
      3  1.11       mrg /*	$eterna: daemon-bozo.c,v 1.22 2010/06/21 06:45:45 mrg Exp $	*/
      4   1.1       tls 
      5   1.1       tls /*
      6   1.8       mrg  * Copyright (c) 1997-2010 Matthew R. Green
      7   1.1       tls  * All rights reserved.
      8   1.1       tls  *
      9   1.1       tls  * Redistribution and use in source and binary forms, with or without
     10   1.1       tls  * modification, are permitted provided that the following conditions
     11   1.1       tls  * are met:
     12   1.1       tls  * 1. Redistributions of source code must retain the above copyright
     13   1.1       tls  *    notice, this list of conditions and the following disclaimer.
     14   1.1       tls  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1       tls  *    notice, this list of conditions and the following disclaimer and
     16   1.1       tls  *    dedication in the documentation and/or other materials provided
     17   1.1       tls  *    with the distribution.
     18   1.1       tls  *
     19   1.1       tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1       tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21   1.1       tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22   1.1       tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23   1.1       tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24   1.1       tls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25   1.1       tls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26   1.1       tls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27   1.1       tls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       tls  * SUCH DAMAGE.
     30   1.1       tls  *
     31   1.1       tls  */
     32   1.1       tls 
     33   1.1       tls /* this code implements daemon mode for bozohttpd */
     34   1.1       tls 
     35   1.1       tls #ifndef NO_DAEMON_MODE
     36   1.1       tls 
     37   1.1       tls #include <sys/param.h>
     38   1.1       tls #include <sys/socket.h>
     39   1.1       tls #include <sys/wait.h>
     40   1.1       tls 
     41   1.1       tls #include <netinet/in.h>
     42   1.1       tls 
     43  1.13      jmmv #include <assert.h>
     44   1.1       tls #include <errno.h>
     45   1.1       tls #include <netdb.h>
     46   1.1       tls #include <poll.h>
     47  1.13      jmmv #include <stdio.h>
     48   1.1       tls #include <stdlib.h>
     49   1.1       tls #include <string.h>
     50   1.1       tls #include <unistd.h>
     51   1.1       tls 
     52   1.1       tls #include "bozohttpd.h"
     53   1.1       tls 
     54   1.1       tls static	void	sigchild(int);	/* SIGCHLD handler */
     55   1.1       tls 
     56   1.7       mrg #ifndef POLLRDNORM
     57   1.7       mrg #define POLLRDNORM 0
     58   1.7       mrg #endif
     59   1.7       mrg #ifndef POLLRDBAND
     60   1.7       mrg #define POLLRDBAND 0
     61   1.7       mrg #endif
     62   1.7       mrg #ifndef INFTIM
     63   1.7       mrg #define INFTIM -1
     64   1.7       mrg #endif
     65   1.7       mrg 
     66  1.13      jmmv static const char* pidfile_path = NULL;
     67  1.13      jmmv static pid_t pidfile_pid = 0;
     68  1.13      jmmv 
     69   1.1       tls /* ARGSUSED */
     70   1.1       tls static void
     71   1.8       mrg sigchild(int signo)
     72   1.1       tls {
     73   1.8       mrg 	while (waitpid(-1, NULL, WNOHANG) > 0) {
     74   1.8       mrg 	}
     75   1.1       tls }
     76   1.1       tls 
     77  1.13      jmmv /* Signal handler to exit in a controlled manner.  This ensures that
     78  1.13      jmmv  * any atexit(3) handlers are properly executed. */
     79  1.13      jmmv /* ARGSUSED */
     80  1.14     joerg BOZO_DEAD static void
     81  1.13      jmmv controlled_exit(int signo)
     82  1.13      jmmv {
     83  1.13      jmmv 
     84  1.13      jmmv 	exit(EXIT_SUCCESS);
     85  1.13      jmmv }
     86  1.13      jmmv 
     87  1.13      jmmv static void
     88  1.13      jmmv remove_pidfile(void)
     89  1.13      jmmv {
     90  1.13      jmmv 
     91  1.13      jmmv 	if (pidfile_path != NULL && pidfile_pid == getpid()) {
     92  1.13      jmmv 		(void)unlink(pidfile_path);
     93  1.13      jmmv 		pidfile_path = NULL;
     94  1.13      jmmv 	}
     95  1.13      jmmv }
     96  1.13      jmmv 
     97  1.13      jmmv static void
     98  1.13      jmmv create_pidfile(bozohttpd_t *httpd)
     99  1.13      jmmv {
    100  1.13      jmmv 	FILE *file;
    101  1.13      jmmv 
    102  1.13      jmmv 	assert(pidfile_path == NULL);
    103  1.13      jmmv 
    104  1.13      jmmv 	if (httpd->pidfile == NULL)
    105  1.13      jmmv 		return;
    106  1.13      jmmv 
    107  1.13      jmmv 	if (atexit(remove_pidfile) == -1)
    108  1.13      jmmv 		bozo_err(httpd, 1, "Failed to install pidfile handler");
    109  1.13      jmmv 
    110  1.13      jmmv 	if ((file = fopen(httpd->pidfile, "w")) == NULL)
    111  1.13      jmmv 		bozo_err(httpd, 1, "Failed to create pidfile '%s'",
    112  1.13      jmmv 		    httpd->pidfile);
    113  1.13      jmmv 	(void)fprintf(file, "%d\n", getpid());
    114  1.13      jmmv 	(void)fclose(file);
    115  1.13      jmmv 
    116  1.13      jmmv 	pidfile_path = httpd->pidfile;
    117  1.13      jmmv 	pidfile_pid = getpid();
    118  1.13      jmmv 
    119  1.13      jmmv 	debug((httpd, DEBUG_FAT, "Created pid file '%s' for pid %d",
    120  1.13      jmmv 	    pidfile_path, pidfile_pid));
    121  1.13      jmmv }
    122  1.13      jmmv 
    123   1.1       tls void
    124   1.8       mrg bozo_daemon_init(bozohttpd_t *httpd)
    125   1.1       tls {
    126   1.1       tls 	struct addrinfo h, *r, *r0;
    127   1.8       mrg 	const char	*portnum;
    128   1.1       tls 	int e, i, on = 1;
    129   1.1       tls 
    130   1.8       mrg 	if (!httpd->background)
    131   1.1       tls 		return;
    132   1.1       tls 
    133   1.8       mrg 	portnum = (httpd->bindport) ? httpd->bindport : "http";
    134   1.1       tls 
    135   1.8       mrg 	memset(&h, 0, sizeof(h));
    136   1.1       tls 	h.ai_family = PF_UNSPEC;
    137   1.1       tls 	h.ai_socktype = SOCK_STREAM;
    138   1.1       tls 	h.ai_flags = AI_PASSIVE;
    139   1.8       mrg 	e = getaddrinfo(httpd->bindaddress, portnum, &h, &r0);
    140   1.1       tls 	if (e)
    141   1.8       mrg 		bozo_err(httpd, 1, "getaddrinfo([%s]:%s): %s",
    142   1.8       mrg 		    httpd->bindaddress ? httpd->bindaddress : "*",
    143   1.8       mrg 		    portnum, gai_strerror(e));
    144   1.1       tls 	for (r = r0; r != NULL; r = r->ai_next)
    145   1.8       mrg 		httpd->nsock++;
    146   1.8       mrg 	httpd->sock = bozomalloc(httpd, httpd->nsock * sizeof(*httpd->sock));
    147   1.8       mrg 	httpd->fds = bozomalloc(httpd, httpd->nsock * sizeof(*httpd->fds));
    148   1.1       tls 	for (i = 0, r = r0; r != NULL; r = r->ai_next) {
    149   1.8       mrg 		httpd->sock[i] = socket(r->ai_family, SOCK_STREAM, 0);
    150   1.8       mrg 		if (httpd->sock[i] == -1)
    151   1.1       tls 			continue;
    152   1.8       mrg 		if (setsockopt(httpd->sock[i], SOL_SOCKET, SO_REUSEADDR, &on,
    153   1.1       tls 		    sizeof(on)) == -1)
    154   1.8       mrg 			bozo_warn(httpd, "setsockopt SO_REUSEADDR: %s",
    155   1.1       tls 			    strerror(errno));
    156   1.8       mrg 		if (bind(httpd->sock[i], r->ai_addr, r->ai_addrlen) == -1)
    157   1.1       tls 			continue;
    158   1.8       mrg 		if (listen(httpd->sock[i], SOMAXCONN) == -1)
    159   1.1       tls 			continue;
    160   1.8       mrg 		httpd->fds[i].events = POLLIN | POLLPRI | POLLRDNORM |
    161   1.7       mrg 				POLLRDBAND | POLLERR;
    162   1.8       mrg 		httpd->fds[i].fd = httpd->sock[i];
    163   1.1       tls 		i++;
    164   1.1       tls 	}
    165   1.1       tls 	if (i == 0)
    166   1.8       mrg 		bozo_err(httpd, 1, "could not find any addresses to bind");
    167   1.8       mrg 	httpd->nsock = i;
    168   1.1       tls 	freeaddrinfo(r0);
    169   1.1       tls 
    170  1.12     pooka 	if (httpd->foreground == 0)
    171  1.12     pooka 		daemon(1, 0);
    172  1.12     pooka 
    173  1.13      jmmv 	create_pidfile(httpd);
    174  1.13      jmmv 
    175  1.12     pooka 	bozo_warn(httpd, "started in daemon mode as `%s' port `%s' root `%s'",
    176  1.12     pooka 	    httpd->virthostname, portnum, httpd->slashdir);
    177  1.12     pooka 
    178  1.13      jmmv 	signal(SIGHUP, controlled_exit);
    179  1.13      jmmv 	signal(SIGINT, controlled_exit);
    180  1.13      jmmv 	signal(SIGTERM, controlled_exit);
    181  1.13      jmmv 
    182   1.1       tls 	signal(SIGCHLD, sigchild);
    183   1.1       tls }
    184   1.1       tls 
    185   1.7       mrg void
    186   1.8       mrg bozo_daemon_closefds(bozohttpd_t *httpd)
    187   1.7       mrg {
    188   1.7       mrg 	int i;
    189   1.7       mrg 
    190   1.8       mrg 	for (i = 0; i < httpd->nsock; i++)
    191   1.8       mrg 		close(httpd->sock[i]);
    192   1.7       mrg }
    193   1.7       mrg 
    194   1.7       mrg static void
    195   1.8       mrg daemon_runchild(bozohttpd_t *httpd, int fd)
    196   1.7       mrg {
    197   1.8       mrg 	httpd->request_times++;
    198   1.7       mrg 
    199   1.7       mrg 	/* setup stdin/stdout/stderr */
    200   1.7       mrg 	dup2(fd, 0);
    201   1.7       mrg 	dup2(fd, 1);
    202   1.7       mrg 	/*dup2(fd, 2);*/
    203   1.7       mrg 	close(fd);
    204   1.7       mrg }
    205   1.7       mrg 
    206   1.9       mrg static int
    207   1.9       mrg daemon_poll_err(bozohttpd_t *httpd, int fd, int idx)
    208   1.9       mrg {
    209   1.9       mrg 	if ((httpd->fds[idx].revents & (POLLNVAL|POLLERR|POLLHUP)) == 0)
    210   1.9       mrg 		return 0;
    211   1.9       mrg 
    212   1.9       mrg 	bozo_warn(httpd, "poll on fd %d pid %d revents %d: %s",
    213   1.9       mrg 	    httpd->fds[idx].fd, getpid(), httpd->fds[idx].revents,
    214   1.9       mrg 	    strerror(errno));
    215   1.9       mrg 	bozo_warn(httpd, "nsock = %d", httpd->nsock);
    216   1.9       mrg 	close(httpd->sock[idx]);
    217   1.9       mrg 	httpd->nsock--;
    218   1.9       mrg 	bozo_warn(httpd, "nsock now = %d", httpd->nsock);
    219   1.9       mrg 	/* no sockets left */
    220   1.9       mrg 	if (httpd->nsock == 0)
    221   1.9       mrg 		exit(0);
    222   1.9       mrg 	/* last socket closed is the easy case */
    223   1.9       mrg 	if (httpd->nsock != idx) {
    224   1.9       mrg 		memmove(&httpd->fds[idx], &httpd->fds[idx+1],
    225   1.9       mrg 			(httpd->nsock - idx) * sizeof(*httpd->fds));
    226   1.9       mrg 		memmove(&httpd->sock[idx], &httpd->sock[idx+1],
    227   1.9       mrg 			(httpd->nsock - idx) * sizeof(*httpd->sock));
    228   1.9       mrg 	}
    229   1.9       mrg 
    230   1.9       mrg 	return 1;
    231   1.9       mrg }
    232   1.9       mrg 
    233   1.1       tls /*
    234   1.1       tls  * the parent never returns from this function, only children that
    235   1.5       mrg  * are ready to run... XXXMRG - still true in fork-lesser bozo?
    236   1.1       tls  */
    237  1.10       mrg int
    238   1.8       mrg bozo_daemon_fork(bozohttpd_t *httpd)
    239   1.1       tls {
    240   1.7       mrg 	int i;
    241   1.1       tls 
    242   1.8       mrg 	debug((httpd, DEBUG_FAT, "%s: pid %u request_times %d",
    243   1.8       mrg 		__func__, getpid(),
    244   1.8       mrg 		httpd->request_times));
    245   1.7       mrg 	/* if we've handled 5 files, exit and let someone else work */
    246   1.8       mrg 	if (httpd->request_times > 5 ||
    247   1.8       mrg 	    (httpd->background == 2 && httpd->request_times > 0))
    248  1.11       mrg 		_exit(0);
    249  1.11       mrg 
    250  1.11       mrg #if 1
    251  1.11       mrg 	if (httpd->request_times > 0)
    252  1.11       mrg 		_exit(0);
    253  1.11       mrg #endif
    254   1.6       mrg 
    255   1.8       mrg 	while (httpd->background) {
    256   1.6       mrg 		struct	sockaddr_storage ss;
    257   1.6       mrg 		socklen_t slen;
    258   1.6       mrg 		int fd;
    259   1.6       mrg 
    260   1.8       mrg 		if (httpd->nsock == 0)
    261   1.6       mrg 			exit(0);
    262   1.1       tls 
    263   1.1       tls 		/*
    264   1.1       tls 		 * wait for a connection, then fork() and return NULL in
    265   1.1       tls 		 * the parent, who will come back here waiting for another
    266   1.1       tls 		 * connection.  read the request in in the child, and return
    267   1.1       tls 		 * it, for processing.
    268   1.1       tls 		 */
    269   1.1       tls again:
    270   1.8       mrg 		if (poll(httpd->fds, (unsigned)httpd->nsock, INFTIM) == -1) {
    271   1.6       mrg 			/* fail on programmer errors */
    272   1.6       mrg 			if (errno == EFAULT ||
    273   1.6       mrg 			    errno == EINVAL)
    274   1.8       mrg 				bozo_err(httpd, 1, "poll: %s",
    275   1.8       mrg 					strerror(errno));
    276   1.6       mrg 
    277   1.6       mrg 			/* sleep on some temporary kernel failures */
    278   1.6       mrg 			if (errno == ENOMEM ||
    279   1.6       mrg 			    errno == EAGAIN)
    280   1.6       mrg 				sleep(1);
    281   1.6       mrg 
    282   1.1       tls 			goto again;
    283   1.1       tls 		}
    284   1.1       tls 
    285   1.8       mrg 		for (i = 0; i < httpd->nsock; i++) {
    286   1.9       mrg 			if (daemon_poll_err(httpd, fd, i))
    287   1.6       mrg 				break;
    288   1.8       mrg 			if (httpd->fds[i].revents == 0)
    289   1.1       tls 				continue;
    290   1.1       tls 
    291   1.4  degroote 			slen = sizeof(ss);
    292   1.8       mrg 			fd = accept(httpd->fds[i].fd,
    293   1.8       mrg 					(struct sockaddr *)(void *)&ss, &slen);
    294   1.1       tls 			if (fd == -1) {
    295   1.6       mrg 				if (errno == EFAULT ||
    296   1.6       mrg 				    errno == EINVAL)
    297   1.8       mrg 					bozo_err(httpd, 1, "accept: %s",
    298   1.8       mrg 						strerror(errno));
    299   1.6       mrg 
    300   1.6       mrg 				if (errno == ENOMEM ||
    301   1.6       mrg 				    errno == EAGAIN)
    302   1.6       mrg 					sleep(1);
    303   1.6       mrg 
    304   1.1       tls 				continue;
    305   1.1       tls 			}
    306   1.7       mrg 
    307  1.10       mrg #if 0
    308  1.10       mrg 			/*
    309  1.10       mrg 			 * This code doesn't work.  It interacts very poorly
    310  1.10       mrg 			 * with ~user translation and needs to be fixed.
    311  1.10       mrg 			 */
    312   1.8       mrg 			if (httpd->request_times > 0) {
    313   1.8       mrg 				daemon_runchild(httpd, fd);
    314  1.10       mrg 				return 0;
    315   1.7       mrg 			}
    316  1.10       mrg #endif
    317   1.7       mrg 
    318   1.1       tls 			switch (fork()) {
    319   1.1       tls 			case -1: /* eep, failure */
    320   1.8       mrg 				bozo_warn(httpd, "fork() failed, sleeping for "
    321   1.7       mrg 					"10 seconds: %s", strerror(errno));
    322   1.1       tls 				close(fd);
    323   1.1       tls 				sleep(10);
    324   1.7       mrg 				break;
    325   1.1       tls 
    326   1.1       tls 			case 0: /* child */
    327   1.8       mrg 				daemon_runchild(httpd, fd);
    328  1.10       mrg 				return 0;
    329   1.1       tls 
    330   1.1       tls 			default: /* parent */
    331   1.1       tls 				close(fd);
    332   1.7       mrg 				break;
    333   1.1       tls 			}
    334   1.1       tls 		}
    335   1.1       tls 	}
    336  1.10       mrg 	return 0;
    337   1.1       tls }
    338   1.1       tls 
    339   1.1       tls #endif /* NO_DAEMON_MODE */
    340