daemon-bozo.c revision 1.1 1 1.1 tls /* $eterna: daemon-bozo.c,v 1.8 2006/05/17 08:22:03 mrg Exp $ */
2 1.1 tls
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1997-2006 Matthew R. Green
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * Redistribution and use in source and binary forms, with or without
8 1.1 tls * modification, are permitted provided that the following conditions
9 1.1 tls * are met:
10 1.1 tls * 1. Redistributions of source code must retain the above copyright
11 1.1 tls * notice, this list of conditions and the following disclaimer.
12 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tls * notice, this list of conditions and the following disclaimer and
14 1.1 tls * dedication in the documentation and/or other materials provided
15 1.1 tls * with the distribution.
16 1.1 tls * 3. The name of the author may not be used to endorse or promote products
17 1.1 tls * derived from this software without specific prior written permission.
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.1 tls #include <errno.h>
44 1.1 tls #include <netdb.h>
45 1.1 tls #include <poll.h>
46 1.1 tls #include <stdlib.h>
47 1.1 tls #include <string.h>
48 1.1 tls #include <unistd.h>
49 1.1 tls
50 1.1 tls #include "bozohttpd.h"
51 1.1 tls
52 1.1 tls char *iflag; /* bind address; default INADDR_ANY */
53 1.1 tls static int *sock; /* bound sockets */
54 1.1 tls static int nsock; /* number of above */
55 1.1 tls
56 1.1 tls static void sigchild(int); /* SIGCHLD handler */
57 1.1 tls
58 1.1 tls /* ARGSUSED */
59 1.1 tls static void
60 1.1 tls sigchild(signo)
61 1.1 tls int signo;
62 1.1 tls {
63 1.1 tls while (waitpid(-1, NULL, WNOHANG) > 0)
64 1.1 tls ;
65 1.1 tls }
66 1.1 tls
67 1.1 tls void
68 1.1 tls daemon_init()
69 1.1 tls {
70 1.1 tls struct addrinfo h, *r, *r0;
71 1.1 tls int e, i, on = 1;
72 1.1 tls
73 1.1 tls if (!bflag)
74 1.1 tls return;
75 1.1 tls
76 1.1 tls if (fflag == 0)
77 1.1 tls daemon(1, 0);
78 1.1 tls
79 1.1 tls warning("started in daemon mode as `%s' port `%s' root `%s'",
80 1.1 tls myname, Iflag, slashdir);
81 1.1 tls
82 1.1 tls memset(&h, 0, sizeof h);
83 1.1 tls h.ai_family = PF_UNSPEC;
84 1.1 tls h.ai_socktype = SOCK_STREAM;
85 1.1 tls h.ai_flags = AI_PASSIVE;
86 1.1 tls e = getaddrinfo(iflag, Iflag, &h, &r0);
87 1.1 tls if (e)
88 1.1 tls error(1, "getaddrinfo([%s]:%s): %s",
89 1.1 tls iflag ? iflag : "*", Iflag, gai_strerror(e));
90 1.1 tls for (r = r0; r != NULL; r = r->ai_next)
91 1.1 tls nsock++;
92 1.1 tls sock = bozomalloc(nsock * sizeof *sock);
93 1.1 tls for (i = 0, r = r0; r != NULL; r = r->ai_next) {
94 1.1 tls sock[i] = socket(r->ai_family, SOCK_STREAM, 0);
95 1.1 tls if (sock[i] == -1)
96 1.1 tls continue;
97 1.1 tls if (setsockopt(sock[i], SOL_SOCKET, SO_REUSEADDR, &on,
98 1.1 tls sizeof(on)) == -1)
99 1.1 tls warning("setsockopt SO_REUSEADDR: %s",
100 1.1 tls strerror(errno));
101 1.1 tls if (bind(sock[i], r->ai_addr, r->ai_addrlen) == -1)
102 1.1 tls continue;
103 1.1 tls if (listen(sock[i], SOMAXCONN) == -1)
104 1.1 tls continue;
105 1.1 tls i++;
106 1.1 tls }
107 1.1 tls if (i == 0)
108 1.1 tls error(1, "could not find any addresses to bind");
109 1.1 tls nsock = i;
110 1.1 tls freeaddrinfo(r0);
111 1.1 tls
112 1.1 tls signal(SIGCHLD, sigchild);
113 1.1 tls }
114 1.1 tls
115 1.1 tls /*
116 1.1 tls * the parent never returns from this function, only children that
117 1.1 tls * are ready to run...
118 1.1 tls */
119 1.1 tls void
120 1.1 tls daemon_fork()
121 1.1 tls {
122 1.1 tls struct pollfd *fds = NULL;
123 1.1 tls
124 1.1 tls while (bflag) {
125 1.1 tls struct sockaddr_storage ss;
126 1.1 tls socklen_t slen;
127 1.1 tls int fd;
128 1.1 tls int i;
129 1.1 tls
130 1.1 tls #ifndef POLLRDNORM
131 1.1 tls #define POLLRDNORM 0
132 1.1 tls #endif
133 1.1 tls #ifndef POLLRDBAND
134 1.1 tls #define POLLRDBAND 0
135 1.1 tls #endif
136 1.1 tls #ifndef INFTIM
137 1.1 tls #define INFTIM -1
138 1.1 tls #endif
139 1.1 tls if (fds == NULL) {
140 1.1 tls fds = bozomalloc(nsock * sizeof *fds);
141 1.1 tls for (i = 0; i < nsock; i++) {
142 1.1 tls fds[i].events = POLLIN | POLLPRI | POLLRDNORM |
143 1.1 tls POLLRDBAND | POLLERR;
144 1.1 tls fds[i].fd = sock[i];
145 1.1 tls }
146 1.1 tls }
147 1.1 tls
148 1.1 tls /*
149 1.1 tls * wait for a connection, then fork() and return NULL in
150 1.1 tls * the parent, who will come back here waiting for another
151 1.1 tls * connection. read the request in in the child, and return
152 1.1 tls * it, for processing.
153 1.1 tls */
154 1.1 tls again:
155 1.1 tls if (poll(fds, nsock, INFTIM) == -1) {
156 1.1 tls if (errno != EINTR)
157 1.1 tls error(1, "poll: %s", strerror(errno));
158 1.1 tls goto again;
159 1.1 tls }
160 1.1 tls
161 1.1 tls for (i = 0; i < nsock; i++) {
162 1.1 tls if (fds[i].revents & (POLLNVAL|POLLERR|POLLHUP)) {
163 1.1 tls warning("poll on fd %d: %s", fds[i].fd,
164 1.1 tls strerror(errno));
165 1.1 tls continue;
166 1.1 tls }
167 1.1 tls if (fds[i].revents == 0)
168 1.1 tls continue;
169 1.1 tls
170 1.1 tls fd = accept(sock[i], (struct sockaddr *)&ss, &slen);
171 1.1 tls if (fd == -1) {
172 1.1 tls if (errno != EAGAIN)
173 1.1 tls error(1, "accept: %s", strerror(errno));
174 1.1 tls continue;
175 1.1 tls }
176 1.1 tls switch (fork()) {
177 1.1 tls case -1: /* eep, failure */
178 1.1 tls warning("fork() failed, sleeping for 10 seconds: %s",
179 1.1 tls strerror(errno));
180 1.1 tls close(fd);
181 1.1 tls sleep(10);
182 1.1 tls continue;
183 1.1 tls
184 1.1 tls case 0: /* child */
185 1.1 tls /* setup stdin/stdout/stderr */
186 1.1 tls dup2(fd, 0);
187 1.1 tls dup2(fd, 1);
188 1.1 tls /*dup2(fd, 2);*/
189 1.1 tls close(fd);
190 1.1 tls return;
191 1.1 tls
192 1.1 tls default: /* parent */
193 1.1 tls close(fd);
194 1.1 tls continue;
195 1.1 tls }
196 1.1 tls }
197 1.1 tls }
198 1.1 tls }
199 1.1 tls
200 1.1 tls #endif /* NO_DAEMON_MODE */
201