daemon-bozo.c revision 1.2 1 1.2 tls /* $NetBSD: daemon-bozo.c,v 1.2 2007/10/17 18:48:01 tls Exp $ */
2 1.2 tls
3 1.1 tls /* $eterna: daemon-bozo.c,v 1.8 2006/05/17 08:22:03 mrg Exp $ */
4 1.1 tls
5 1.1 tls /*
6 1.1 tls * Copyright (c) 1997-2006 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 * 3. The name of the author may not be used to endorse or promote products
19 1.1 tls * derived from this software without specific prior written permission.
20 1.1 tls *
21 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 tls * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 tls * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 tls * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 tls * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 tls * SUCH DAMAGE.
32 1.1 tls *
33 1.1 tls */
34 1.1 tls
35 1.1 tls /* this code implements daemon mode for bozohttpd */
36 1.1 tls
37 1.1 tls #ifndef NO_DAEMON_MODE
38 1.1 tls
39 1.1 tls #include <sys/param.h>
40 1.1 tls #include <sys/socket.h>
41 1.1 tls #include <sys/wait.h>
42 1.1 tls
43 1.1 tls #include <netinet/in.h>
44 1.1 tls
45 1.1 tls #include <errno.h>
46 1.1 tls #include <netdb.h>
47 1.1 tls #include <poll.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 char *iflag; /* bind address; default INADDR_ANY */
55 1.1 tls static int *sock; /* bound sockets */
56 1.1 tls static int nsock; /* number of above */
57 1.1 tls
58 1.1 tls static void sigchild(int); /* SIGCHLD handler */
59 1.1 tls
60 1.1 tls /* ARGSUSED */
61 1.1 tls static void
62 1.1 tls sigchild(signo)
63 1.1 tls int signo;
64 1.1 tls {
65 1.1 tls while (waitpid(-1, NULL, WNOHANG) > 0)
66 1.1 tls ;
67 1.1 tls }
68 1.1 tls
69 1.1 tls void
70 1.1 tls daemon_init()
71 1.1 tls {
72 1.1 tls struct addrinfo h, *r, *r0;
73 1.1 tls int e, i, on = 1;
74 1.1 tls
75 1.1 tls if (!bflag)
76 1.1 tls return;
77 1.1 tls
78 1.1 tls if (fflag == 0)
79 1.1 tls daemon(1, 0);
80 1.1 tls
81 1.1 tls warning("started in daemon mode as `%s' port `%s' root `%s'",
82 1.1 tls myname, Iflag, slashdir);
83 1.1 tls
84 1.1 tls memset(&h, 0, sizeof h);
85 1.1 tls h.ai_family = PF_UNSPEC;
86 1.1 tls h.ai_socktype = SOCK_STREAM;
87 1.1 tls h.ai_flags = AI_PASSIVE;
88 1.1 tls e = getaddrinfo(iflag, Iflag, &h, &r0);
89 1.1 tls if (e)
90 1.1 tls error(1, "getaddrinfo([%s]:%s): %s",
91 1.1 tls iflag ? iflag : "*", Iflag, gai_strerror(e));
92 1.1 tls for (r = r0; r != NULL; r = r->ai_next)
93 1.1 tls nsock++;
94 1.1 tls sock = bozomalloc(nsock * sizeof *sock);
95 1.1 tls for (i = 0, r = r0; r != NULL; r = r->ai_next) {
96 1.1 tls sock[i] = socket(r->ai_family, SOCK_STREAM, 0);
97 1.1 tls if (sock[i] == -1)
98 1.1 tls continue;
99 1.1 tls if (setsockopt(sock[i], SOL_SOCKET, SO_REUSEADDR, &on,
100 1.1 tls sizeof(on)) == -1)
101 1.1 tls warning("setsockopt SO_REUSEADDR: %s",
102 1.1 tls strerror(errno));
103 1.1 tls if (bind(sock[i], r->ai_addr, r->ai_addrlen) == -1)
104 1.1 tls continue;
105 1.1 tls if (listen(sock[i], SOMAXCONN) == -1)
106 1.1 tls continue;
107 1.1 tls i++;
108 1.1 tls }
109 1.1 tls if (i == 0)
110 1.1 tls error(1, "could not find any addresses to bind");
111 1.1 tls nsock = i;
112 1.1 tls freeaddrinfo(r0);
113 1.1 tls
114 1.1 tls signal(SIGCHLD, sigchild);
115 1.1 tls }
116 1.1 tls
117 1.1 tls /*
118 1.1 tls * the parent never returns from this function, only children that
119 1.1 tls * are ready to run...
120 1.1 tls */
121 1.1 tls void
122 1.1 tls daemon_fork()
123 1.1 tls {
124 1.1 tls struct pollfd *fds = NULL;
125 1.1 tls
126 1.1 tls while (bflag) {
127 1.1 tls struct sockaddr_storage ss;
128 1.1 tls socklen_t slen;
129 1.1 tls int fd;
130 1.1 tls int i;
131 1.1 tls
132 1.1 tls #ifndef POLLRDNORM
133 1.1 tls #define POLLRDNORM 0
134 1.1 tls #endif
135 1.1 tls #ifndef POLLRDBAND
136 1.1 tls #define POLLRDBAND 0
137 1.1 tls #endif
138 1.1 tls #ifndef INFTIM
139 1.1 tls #define INFTIM -1
140 1.1 tls #endif
141 1.1 tls if (fds == NULL) {
142 1.1 tls fds = bozomalloc(nsock * sizeof *fds);
143 1.1 tls for (i = 0; i < nsock; i++) {
144 1.1 tls fds[i].events = POLLIN | POLLPRI | POLLRDNORM |
145 1.1 tls POLLRDBAND | POLLERR;
146 1.1 tls fds[i].fd = sock[i];
147 1.1 tls }
148 1.1 tls }
149 1.1 tls
150 1.1 tls /*
151 1.1 tls * wait for a connection, then fork() and return NULL in
152 1.1 tls * the parent, who will come back here waiting for another
153 1.1 tls * connection. read the request in in the child, and return
154 1.1 tls * it, for processing.
155 1.1 tls */
156 1.1 tls again:
157 1.1 tls if (poll(fds, nsock, INFTIM) == -1) {
158 1.1 tls if (errno != EINTR)
159 1.1 tls error(1, "poll: %s", strerror(errno));
160 1.1 tls goto again;
161 1.1 tls }
162 1.1 tls
163 1.1 tls for (i = 0; i < nsock; i++) {
164 1.1 tls if (fds[i].revents & (POLLNVAL|POLLERR|POLLHUP)) {
165 1.1 tls warning("poll on fd %d: %s", fds[i].fd,
166 1.1 tls strerror(errno));
167 1.1 tls continue;
168 1.1 tls }
169 1.1 tls if (fds[i].revents == 0)
170 1.1 tls continue;
171 1.1 tls
172 1.1 tls fd = accept(sock[i], (struct sockaddr *)&ss, &slen);
173 1.1 tls if (fd == -1) {
174 1.1 tls if (errno != EAGAIN)
175 1.1 tls error(1, "accept: %s", strerror(errno));
176 1.1 tls continue;
177 1.1 tls }
178 1.1 tls switch (fork()) {
179 1.1 tls case -1: /* eep, failure */
180 1.1 tls warning("fork() failed, sleeping for 10 seconds: %s",
181 1.1 tls strerror(errno));
182 1.1 tls close(fd);
183 1.1 tls sleep(10);
184 1.1 tls continue;
185 1.1 tls
186 1.1 tls case 0: /* child */
187 1.1 tls /* setup stdin/stdout/stderr */
188 1.1 tls dup2(fd, 0);
189 1.1 tls dup2(fd, 1);
190 1.1 tls /*dup2(fd, 2);*/
191 1.1 tls close(fd);
192 1.1 tls return;
193 1.1 tls
194 1.1 tls default: /* parent */
195 1.1 tls close(fd);
196 1.1 tls continue;
197 1.1 tls }
198 1.1 tls }
199 1.1 tls }
200 1.1 tls }
201 1.1 tls
202 1.1 tls #endif /* NO_DAEMON_MODE */
203