Blame


1 56324ebe 2022-07-14 thomas /*
2 56324ebe 2022-07-14 thomas * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 56324ebe 2022-07-14 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 56324ebe 2022-07-14 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 56324ebe 2022-07-14 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 56324ebe 2022-07-14 thomas *
7 56324ebe 2022-07-14 thomas * Permission to use, copy, modify, and distribute this software for any
8 56324ebe 2022-07-14 thomas * purpose with or without fee is hereby granted, provided that the above
9 56324ebe 2022-07-14 thomas * copyright notice and this permission notice appear in all copies.
10 56324ebe 2022-07-14 thomas *
11 56324ebe 2022-07-14 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 56324ebe 2022-07-14 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 56324ebe 2022-07-14 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 56324ebe 2022-07-14 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 56324ebe 2022-07-14 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 56324ebe 2022-07-14 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 56324ebe 2022-07-14 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 56324ebe 2022-07-14 thomas */
19 56324ebe 2022-07-14 thomas
20 56324ebe 2022-07-14 thomas #include <sys/param.h>
21 56324ebe 2022-07-14 thomas #include <sys/ioctl.h>
22 56324ebe 2022-07-14 thomas #include <sys/wait.h>
23 56324ebe 2022-07-14 thomas #include <sys/uio.h>
24 56324ebe 2022-07-14 thomas #include <sys/resource.h>
25 56324ebe 2022-07-14 thomas #include <sys/socket.h>
26 56324ebe 2022-07-14 thomas #include <sys/stat.h>
27 56324ebe 2022-07-14 thomas #include <sys/time.h>
28 56324ebe 2022-07-14 thomas #include <sys/types.h>
29 56324ebe 2022-07-14 thomas #include <sys/mman.h>
30 56324ebe 2022-07-14 thomas #include <sys/un.h>
31 56324ebe 2022-07-14 thomas
32 56324ebe 2022-07-14 thomas #include <net/if.h>
33 56324ebe 2022-07-14 thomas #include <netinet/in.h>
34 56324ebe 2022-07-14 thomas
35 56324ebe 2022-07-14 thomas #include <errno.h>
36 56324ebe 2022-07-14 thomas #include <event.h>
37 56324ebe 2022-07-14 thomas #include <fcntl.h>
38 56324ebe 2022-07-14 thomas #include <ifaddrs.h>
39 56324ebe 2022-07-14 thomas #include <limits.h>
40 56324ebe 2022-07-14 thomas #include <netdb.h>
41 56324ebe 2022-07-14 thomas #include <poll.h>
42 56324ebe 2022-07-14 thomas #include <pwd.h>
43 56324ebe 2022-07-14 thomas #include <stddef.h>
44 56324ebe 2022-07-14 thomas #include <stdio.h>
45 56324ebe 2022-07-14 thomas #include <stdlib.h>
46 56324ebe 2022-07-14 thomas #include <string.h>
47 56324ebe 2022-07-14 thomas #include <unistd.h>
48 56324ebe 2022-07-14 thomas
49 56324ebe 2022-07-14 thomas #include "got_error.h"
50 56324ebe 2022-07-14 thomas #include "got_opentemp.h"
51 56324ebe 2022-07-14 thomas
52 56324ebe 2022-07-14 thomas #include "proc.h"
53 56324ebe 2022-07-14 thomas #include "gotwebd.h"
54 d7cad54e 2022-07-14 thomas
55 d7cad54e 2022-07-14 thomas #include "got_compat.h"
56 56324ebe 2022-07-14 thomas
57 56324ebe 2022-07-14 thomas #define SOCKS_BACKLOG 5
58 56324ebe 2022-07-14 thomas #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
59 56324ebe 2022-07-14 thomas
60 56324ebe 2022-07-14 thomas
61 56324ebe 2022-07-14 thomas volatile int client_cnt;
62 56324ebe 2022-07-14 thomas
63 56324ebe 2022-07-14 thomas struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
64 56324ebe 2022-07-14 thomas
65 56324ebe 2022-07-14 thomas void sockets_sighdlr(int, short, void *);
66 56324ebe 2022-07-14 thomas void sockets_run(struct privsep *, struct privsep_proc *, void *);
67 56324ebe 2022-07-14 thomas void sockets_launch(void);
68 56324ebe 2022-07-14 thomas void sockets_purge(struct gotwebd *);
69 56324ebe 2022-07-14 thomas void sockets_accept_paused(int, short, void *);
70 56324ebe 2022-07-14 thomas void sockets_dup_new_socket(struct socket *, struct socket *);
71 56324ebe 2022-07-14 thomas void sockets_rlimit(int);
72 56324ebe 2022-07-14 thomas
73 56324ebe 2022-07-14 thomas
74 56324ebe 2022-07-14 thomas int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
75 56324ebe 2022-07-14 thomas int sockets_unix_socket_listen(struct privsep *, struct socket *);
76 56324ebe 2022-07-14 thomas int sockets_create_socket(struct addresslist *, in_port_t);
77 56324ebe 2022-07-14 thomas int sockets_socket_af(struct sockaddr_storage *, in_port_t);
78 56324ebe 2022-07-14 thomas int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
79 56324ebe 2022-07-14 thomas volatile int *);
80 56324ebe 2022-07-14 thomas
81 56324ebe 2022-07-14 thomas struct socket
82 56324ebe 2022-07-14 thomas *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
83 56324ebe 2022-07-14 thomas int);
84 56324ebe 2022-07-14 thomas
85 56324ebe 2022-07-14 thomas int cgi_inflight = 0;
86 56324ebe 2022-07-14 thomas
87 56324ebe 2022-07-14 thomas static struct privsep_proc procs[] = {
88 56324ebe 2022-07-14 thomas { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
89 56324ebe 2022-07-14 thomas };
90 56324ebe 2022-07-14 thomas
91 56324ebe 2022-07-14 thomas void
92 56324ebe 2022-07-14 thomas sockets(struct privsep *ps, struct privsep_proc *p)
93 56324ebe 2022-07-14 thomas {
94 56324ebe 2022-07-14 thomas proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
95 56324ebe 2022-07-14 thomas }
96 56324ebe 2022-07-14 thomas
97 56324ebe 2022-07-14 thomas void
98 56324ebe 2022-07-14 thomas sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
99 56324ebe 2022-07-14 thomas {
100 56324ebe 2022-07-14 thomas if (config_init(ps->ps_env) == -1)
101 56324ebe 2022-07-14 thomas fatal("failed to initialize configuration");
102 56324ebe 2022-07-14 thomas
103 56324ebe 2022-07-14 thomas p->p_shutdown = sockets_shutdown;
104 56324ebe 2022-07-14 thomas
105 56324ebe 2022-07-14 thomas sockets_rlimit(-1);
106 56324ebe 2022-07-14 thomas
107 56324ebe 2022-07-14 thomas signal_del(&ps->ps_evsigchld);
108 56324ebe 2022-07-14 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
109 56324ebe 2022-07-14 thomas signal_add(&ps->ps_evsigchld, NULL);
110 56324ebe 2022-07-14 thomas
111 56324ebe 2022-07-14 thomas #ifndef PROFILE
112 56324ebe 2022-07-14 thomas if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
113 56324ebe 2022-07-14 thomas NULL) == -1)
114 56324ebe 2022-07-14 thomas fatal("pledge");
115 56324ebe 2022-07-14 thomas #endif
116 56324ebe 2022-07-14 thomas }
117 56324ebe 2022-07-14 thomas
118 56324ebe 2022-07-14 thomas void
119 56324ebe 2022-07-14 thomas sockets_parse_sockets(struct gotwebd *env)
120 56324ebe 2022-07-14 thomas {
121 56324ebe 2022-07-14 thomas struct server *srv;
122 56324ebe 2022-07-14 thomas struct socket *sock, *new_sock = NULL;
123 56324ebe 2022-07-14 thomas struct address *a;
124 56324ebe 2022-07-14 thomas int sock_id = 0, ipv4 = 0, ipv6 = 0;
125 56324ebe 2022-07-14 thomas
126 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, env->servers, entry) {
127 56324ebe 2022-07-14 thomas if (srv->unix_socket) {
128 56324ebe 2022-07-14 thomas sock_id++;
129 56324ebe 2022-07-14 thomas new_sock = sockets_conf_new_socket(env, srv,
130 56324ebe 2022-07-14 thomas sock_id, UNIX, 0);
131 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
132 56324ebe 2022-07-14 thomas }
133 56324ebe 2022-07-14 thomas
134 56324ebe 2022-07-14 thomas if (srv->fcgi_socket) {
135 56324ebe 2022-07-14 thomas sock_id++;
136 56324ebe 2022-07-14 thomas new_sock = sockets_conf_new_socket(env, srv,
137 56324ebe 2022-07-14 thomas sock_id, FCGI, 0);
138 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
139 56324ebe 2022-07-14 thomas
140 56324ebe 2022-07-14 thomas /* add ipv6 children */
141 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sock, env->sockets, entry) {
142 56324ebe 2022-07-14 thomas ipv4 = ipv6 = 0;
143 56324ebe 2022-07-14 thomas
144 56324ebe 2022-07-14 thomas TAILQ_FOREACH(a, sock->conf.al, entry) {
145 56324ebe 2022-07-14 thomas if (a->ss.ss_family == AF_INET)
146 56324ebe 2022-07-14 thomas ipv4 = 1;
147 56324ebe 2022-07-14 thomas if (a->ss.ss_family == AF_INET6)
148 56324ebe 2022-07-14 thomas ipv6 = 1;
149 56324ebe 2022-07-14 thomas }
150 56324ebe 2022-07-14 thomas
151 56324ebe 2022-07-14 thomas /* create ipv6 sock */
152 56324ebe 2022-07-14 thomas if (ipv4 == 1 && ipv6 == 1) {
153 56324ebe 2022-07-14 thomas sock_id++;
154 56324ebe 2022-07-14 thomas sock->conf.child_id = sock_id;
155 56324ebe 2022-07-14 thomas new_sock = sockets_conf_new_socket(env,
156 56324ebe 2022-07-14 thomas srv, sock_id, FCGI, 1);
157 56324ebe 2022-07-14 thomas sockets_dup_new_socket(sock, new_sock);
158 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(env->sockets,
159 56324ebe 2022-07-14 thomas new_sock, entry);
160 56324ebe 2022-07-14 thomas continue;
161 56324ebe 2022-07-14 thomas }
162 56324ebe 2022-07-14 thomas }
163 56324ebe 2022-07-14 thomas }
164 56324ebe 2022-07-14 thomas }
165 56324ebe 2022-07-14 thomas }
166 56324ebe 2022-07-14 thomas
167 56324ebe 2022-07-14 thomas void
168 56324ebe 2022-07-14 thomas sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
169 56324ebe 2022-07-14 thomas {
170 56324ebe 2022-07-14 thomas struct address *a, *acp;
171 56324ebe 2022-07-14 thomas int n;
172 56324ebe 2022-07-14 thomas
173 56324ebe 2022-07-14 thomas sock->conf.parent_id = p_sock->conf.id;
174 56324ebe 2022-07-14 thomas sock->conf.ipv4 = 0;
175 56324ebe 2022-07-14 thomas sock->conf.ipv6 = 1;
176 56324ebe 2022-07-14 thomas
177 56324ebe 2022-07-14 thomas memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
178 56324ebe 2022-07-14 thomas sizeof(sock->conf.srv_name));
179 56324ebe 2022-07-14 thomas
180 56324ebe 2022-07-14 thomas n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
181 56324ebe 2022-07-14 thomas p_sock->conf.srv_name);
182 56324ebe 2022-07-14 thomas if (n < 0) {
183 56324ebe 2022-07-14 thomas free(p_sock->conf.al);
184 56324ebe 2022-07-14 thomas free(p_sock);
185 56324ebe 2022-07-14 thomas free(sock->conf.al);
186 56324ebe 2022-07-14 thomas free(sock);
187 56324ebe 2022-07-14 thomas fatalx("%s: snprintf", __func__);
188 56324ebe 2022-07-14 thomas }
189 56324ebe 2022-07-14 thomas
190 56324ebe 2022-07-14 thomas TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 56324ebe 2022-07-14 thomas if (a->ss.ss_family == AF_INET)
192 56324ebe 2022-07-14 thomas continue;
193 56324ebe 2022-07-14 thomas
194 56324ebe 2022-07-14 thomas if ((acp = calloc(1, sizeof(*acp))) == NULL)
195 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
196 56324ebe 2022-07-14 thomas memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
197 56324ebe 2022-07-14 thomas acp->ipproto = a->ipproto;
198 56324ebe 2022-07-14 thomas acp->prefixlen = a->prefixlen;
199 56324ebe 2022-07-14 thomas acp->port = a->port;
200 56324ebe 2022-07-14 thomas if (strlen(a->ifname) != 0) {
201 56324ebe 2022-07-14 thomas if (strlcpy(acp->ifname, a->ifname,
202 56324ebe 2022-07-14 thomas sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
203 56324ebe 2022-07-14 thomas fatalx("%s: interface name truncated",
204 56324ebe 2022-07-14 thomas __func__);
205 56324ebe 2022-07-14 thomas }
206 56324ebe 2022-07-14 thomas }
207 56324ebe 2022-07-14 thomas
208 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
209 56324ebe 2022-07-14 thomas }
210 56324ebe 2022-07-14 thomas }
211 56324ebe 2022-07-14 thomas
212 56324ebe 2022-07-14 thomas struct socket *
213 56324ebe 2022-07-14 thomas sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 56324ebe 2022-07-14 thomas int type, int is_dup)
215 56324ebe 2022-07-14 thomas {
216 56324ebe 2022-07-14 thomas struct socket *sock;
217 56324ebe 2022-07-14 thomas struct address *a, *acp;
218 56324ebe 2022-07-14 thomas int n;
219 56324ebe 2022-07-14 thomas
220 56324ebe 2022-07-14 thomas if ((sock = calloc(1, sizeof(*sock))) == NULL)
221 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
222 56324ebe 2022-07-14 thomas
223 56324ebe 2022-07-14 thomas if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
224 56324ebe 2022-07-14 thomas free(sock);
225 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
226 56324ebe 2022-07-14 thomas }
227 56324ebe 2022-07-14 thomas TAILQ_INIT(sock->conf.al);
228 56324ebe 2022-07-14 thomas
229 56324ebe 2022-07-14 thomas sock->conf.parent_id = 0;
230 56324ebe 2022-07-14 thomas sock->conf.id = id;
231 56324ebe 2022-07-14 thomas
232 56324ebe 2022-07-14 thomas sock->fd = -1;
233 56324ebe 2022-07-14 thomas
234 56324ebe 2022-07-14 thomas sock->conf.type = type;
235 56324ebe 2022-07-14 thomas
236 56324ebe 2022-07-14 thomas if (type == UNIX) {
237 56324ebe 2022-07-14 thomas if (strlcpy(sock->conf.unix_socket_name,
238 56324ebe 2022-07-14 thomas srv->unix_socket_name,
239 56324ebe 2022-07-14 thomas sizeof(sock->conf.unix_socket_name)) >=
240 56324ebe 2022-07-14 thomas sizeof(sock->conf.unix_socket_name)) {
241 56324ebe 2022-07-14 thomas free(sock->conf.al);
242 56324ebe 2022-07-14 thomas free(sock);
243 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
244 56324ebe 2022-07-14 thomas }
245 56324ebe 2022-07-14 thomas } else
246 56324ebe 2022-07-14 thomas sock->conf.ipv4 = 1;
247 56324ebe 2022-07-14 thomas
248 56324ebe 2022-07-14 thomas sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
249 56324ebe 2022-07-14 thomas
250 56324ebe 2022-07-14 thomas if (is_dup)
251 56324ebe 2022-07-14 thomas goto done;
252 56324ebe 2022-07-14 thomas
253 56324ebe 2022-07-14 thomas n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
254 56324ebe 2022-07-14 thomas srv->name);
255 56324ebe 2022-07-14 thomas if (n < 0) {
256 56324ebe 2022-07-14 thomas free(sock->conf.al);
257 56324ebe 2022-07-14 thomas free(sock);
258 56324ebe 2022-07-14 thomas fatalx("%s: snprintf", __func__);
259 56324ebe 2022-07-14 thomas }
260 56324ebe 2022-07-14 thomas
261 56324ebe 2022-07-14 thomas if (strlcpy(sock->conf.srv_name, srv->name,
262 56324ebe 2022-07-14 thomas sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
263 56324ebe 2022-07-14 thomas free(sock->conf.al);
264 56324ebe 2022-07-14 thomas free(sock);
265 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
266 56324ebe 2022-07-14 thomas }
267 56324ebe 2022-07-14 thomas
268 56324ebe 2022-07-14 thomas TAILQ_FOREACH(a, srv->al, entry) {
269 56324ebe 2022-07-14 thomas if ((acp = calloc(1, sizeof(*acp))) == NULL) {
270 56324ebe 2022-07-14 thomas free(sock->conf.al);
271 56324ebe 2022-07-14 thomas free(sock);
272 56324ebe 2022-07-14 thomas fatal("%s: calloc", __func__);
273 56324ebe 2022-07-14 thomas }
274 56324ebe 2022-07-14 thomas memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 56324ebe 2022-07-14 thomas acp->ipproto = a->ipproto;
276 56324ebe 2022-07-14 thomas acp->prefixlen = a->prefixlen;
277 56324ebe 2022-07-14 thomas acp->port = a->port;
278 56324ebe 2022-07-14 thomas if (strlen(a->ifname) != 0) {
279 56324ebe 2022-07-14 thomas if (strlcpy(acp->ifname, a->ifname,
280 56324ebe 2022-07-14 thomas sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
281 56324ebe 2022-07-14 thomas fatalx("%s: interface name truncated",
282 56324ebe 2022-07-14 thomas __func__);
283 56324ebe 2022-07-14 thomas }
284 56324ebe 2022-07-14 thomas }
285 56324ebe 2022-07-14 thomas
286 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
287 56324ebe 2022-07-14 thomas }
288 56324ebe 2022-07-14 thomas done:
289 56324ebe 2022-07-14 thomas return (sock);
290 56324ebe 2022-07-14 thomas }
291 56324ebe 2022-07-14 thomas
292 56324ebe 2022-07-14 thomas int
293 56324ebe 2022-07-14 thomas sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
294 56324ebe 2022-07-14 thomas {
295 56324ebe 2022-07-14 thomas switch (ss->ss_family) {
296 56324ebe 2022-07-14 thomas case AF_INET:
297 56324ebe 2022-07-14 thomas ((struct sockaddr_in *)ss)->sin_port = port;
298 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
299 d7cad54e 2022-07-14 thomas #ifndef __linux__
300 56324ebe 2022-07-14 thomas ((struct sockaddr_in *)ss)->sin_len =
301 56324ebe 2022-07-14 thomas sizeof(struct sockaddr_in);
302 d7cad54e 2022-07-14 thomas #endif
303 56324ebe 2022-07-14 thomas break;
304 56324ebe 2022-07-14 thomas case AF_INET6:
305 56324ebe 2022-07-14 thomas ((struct sockaddr_in6 *)ss)->sin6_port = port;
306 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
307 d7cad54e 2022-07-14 thomas #ifndef __linux__
308 56324ebe 2022-07-14 thomas ((struct sockaddr_in6 *)ss)->sin6_len =
309 56324ebe 2022-07-14 thomas sizeof(struct sockaddr_in6);
310 d7cad54e 2022-07-14 thomas #endif
311 56324ebe 2022-07-14 thomas break;
312 56324ebe 2022-07-14 thomas default:
313 56324ebe 2022-07-14 thomas return -1;
314 56324ebe 2022-07-14 thomas }
315 56324ebe 2022-07-14 thomas
316 56324ebe 2022-07-14 thomas return 0;
317 56324ebe 2022-07-14 thomas }
318 56324ebe 2022-07-14 thomas
319 56324ebe 2022-07-14 thomas void
320 56324ebe 2022-07-14 thomas sockets_launch(void)
321 56324ebe 2022-07-14 thomas {
322 56324ebe 2022-07-14 thomas struct socket *sock;
323 56324ebe 2022-07-14 thomas
324 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
325 56324ebe 2022-07-14 thomas log_debug("%s: configuring socket %d (%d)", __func__,
326 56324ebe 2022-07-14 thomas sock->conf.id, sock->fd);
327 56324ebe 2022-07-14 thomas
328 56324ebe 2022-07-14 thomas event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
329 56324ebe 2022-07-14 thomas sockets_socket_accept, sock);
330 56324ebe 2022-07-14 thomas
331 56324ebe 2022-07-14 thomas if (event_add(&sock->ev, NULL))
332 56324ebe 2022-07-14 thomas fatalx("event add sock");
333 56324ebe 2022-07-14 thomas
334 56324ebe 2022-07-14 thomas evtimer_set(&sock->pause, sockets_accept_paused, sock);
335 56324ebe 2022-07-14 thomas
336 56324ebe 2022-07-14 thomas log_debug("%s: running socket listener %d", __func__,
337 56324ebe 2022-07-14 thomas sock->conf.id);
338 56324ebe 2022-07-14 thomas }
339 56324ebe 2022-07-14 thomas }
340 56324ebe 2022-07-14 thomas
341 56324ebe 2022-07-14 thomas void
342 56324ebe 2022-07-14 thomas sockets_purge(struct gotwebd *env)
343 56324ebe 2022-07-14 thomas {
344 56324ebe 2022-07-14 thomas struct socket *sock, *tsock;
345 56324ebe 2022-07-14 thomas
346 56324ebe 2022-07-14 thomas /* shutdown and remove sockets */
347 56324ebe 2022-07-14 thomas TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
348 56324ebe 2022-07-14 thomas if (event_initialized(&sock->ev))
349 56324ebe 2022-07-14 thomas event_del(&sock->ev);
350 56324ebe 2022-07-14 thomas if (evtimer_initialized(&sock->evt))
351 56324ebe 2022-07-14 thomas evtimer_del(&sock->evt);
352 56324ebe 2022-07-14 thomas if (evtimer_initialized(&sock->pause))
353 56324ebe 2022-07-14 thomas evtimer_del(&sock->pause);
354 56324ebe 2022-07-14 thomas if (sock->fd != -1)
355 56324ebe 2022-07-14 thomas close(sock->fd);
356 56324ebe 2022-07-14 thomas TAILQ_REMOVE(env->sockets, sock, entry);
357 56324ebe 2022-07-14 thomas }
358 56324ebe 2022-07-14 thomas }
359 56324ebe 2022-07-14 thomas
360 56324ebe 2022-07-14 thomas int
361 56324ebe 2022-07-14 thomas sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
362 56324ebe 2022-07-14 thomas {
363 56324ebe 2022-07-14 thomas struct privsep *ps = p->p_ps;
364 56324ebe 2022-07-14 thomas int res = 0, cmd = 0, verbose;
365 56324ebe 2022-07-14 thomas
366 56324ebe 2022-07-14 thomas switch (imsg->hdr.type) {
367 56324ebe 2022-07-14 thomas case IMSG_CFG_SRV:
368 56324ebe 2022-07-14 thomas config_getserver(gotwebd_env, imsg);
369 56324ebe 2022-07-14 thomas break;
370 56324ebe 2022-07-14 thomas case IMSG_CFG_SOCK:
371 56324ebe 2022-07-14 thomas config_getsock(gotwebd_env, imsg);
372 56324ebe 2022-07-14 thomas break;
373 56324ebe 2022-07-14 thomas case IMSG_CFG_FD:
374 56324ebe 2022-07-14 thomas config_getfd(gotwebd_env, imsg);
375 56324ebe 2022-07-14 thomas break;
376 56324ebe 2022-07-14 thomas case IMSG_CFG_DONE:
377 56324ebe 2022-07-14 thomas config_getcfg(gotwebd_env, imsg);
378 56324ebe 2022-07-14 thomas break;
379 56324ebe 2022-07-14 thomas case IMSG_CTL_START:
380 56324ebe 2022-07-14 thomas sockets_launch();
381 56324ebe 2022-07-14 thomas break;
382 56324ebe 2022-07-14 thomas case IMSG_CTL_VERBOSE:
383 56324ebe 2022-07-14 thomas IMSG_SIZE_CHECK(imsg, &verbose);
384 56324ebe 2022-07-14 thomas memcpy(&verbose, imsg->data, sizeof(verbose));
385 56324ebe 2022-07-14 thomas log_setverbose(verbose);
386 56324ebe 2022-07-14 thomas break;
387 56324ebe 2022-07-14 thomas default:
388 56324ebe 2022-07-14 thomas return -1;
389 56324ebe 2022-07-14 thomas }
390 56324ebe 2022-07-14 thomas
391 56324ebe 2022-07-14 thomas switch (cmd) {
392 56324ebe 2022-07-14 thomas case 0:
393 56324ebe 2022-07-14 thomas break;
394 56324ebe 2022-07-14 thomas default:
395 56324ebe 2022-07-14 thomas if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
396 56324ebe 2022-07-14 thomas imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
397 56324ebe 2022-07-14 thomas return -1;
398 56324ebe 2022-07-14 thomas break;
399 56324ebe 2022-07-14 thomas }
400 56324ebe 2022-07-14 thomas
401 56324ebe 2022-07-14 thomas return 0;
402 56324ebe 2022-07-14 thomas }
403 56324ebe 2022-07-14 thomas
404 56324ebe 2022-07-14 thomas void
405 56324ebe 2022-07-14 thomas sockets_sighdlr(int sig, short event, void *arg)
406 56324ebe 2022-07-14 thomas {
407 56324ebe 2022-07-14 thomas switch (sig) {
408 56324ebe 2022-07-14 thomas case SIGHUP:
409 56324ebe 2022-07-14 thomas log_info("%s: ignoring SIGHUP", __func__);
410 56324ebe 2022-07-14 thomas break;
411 56324ebe 2022-07-14 thomas case SIGPIPE:
412 56324ebe 2022-07-14 thomas log_info("%s: ignoring SIGPIPE", __func__);
413 56324ebe 2022-07-14 thomas break;
414 56324ebe 2022-07-14 thomas case SIGUSR1:
415 56324ebe 2022-07-14 thomas log_info("%s: ignoring SIGUSR1", __func__);
416 56324ebe 2022-07-14 thomas break;
417 56324ebe 2022-07-14 thomas case SIGCHLD:
418 56324ebe 2022-07-14 thomas break;
419 56324ebe 2022-07-14 thomas default:
420 56324ebe 2022-07-14 thomas log_info("SIGNAL: %d", sig);
421 56324ebe 2022-07-14 thomas fatalx("unexpected signal");
422 56324ebe 2022-07-14 thomas }
423 56324ebe 2022-07-14 thomas }
424 56324ebe 2022-07-14 thomas
425 56324ebe 2022-07-14 thomas void
426 56324ebe 2022-07-14 thomas sockets_shutdown(void)
427 56324ebe 2022-07-14 thomas {
428 56324ebe 2022-07-14 thomas struct server *srv, *tsrv;
429 56324ebe 2022-07-14 thomas struct socket *sock, *tsock;
430 56324ebe 2022-07-14 thomas
431 56324ebe 2022-07-14 thomas sockets_purge(gotwebd_env);
432 56324ebe 2022-07-14 thomas
433 56324ebe 2022-07-14 thomas /* clean sockets */
434 56324ebe 2022-07-14 thomas TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
435 56324ebe 2022-07-14 thomas TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
436 56324ebe 2022-07-14 thomas close(sock->fd);
437 56324ebe 2022-07-14 thomas free(sock);
438 56324ebe 2022-07-14 thomas }
439 56324ebe 2022-07-14 thomas
440 56324ebe 2022-07-14 thomas /* clean servers */
441 56324ebe 2022-07-14 thomas TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
442 56324ebe 2022-07-14 thomas free(srv);
443 56324ebe 2022-07-14 thomas
444 56324ebe 2022-07-14 thomas free(gotwebd_env->sockets);
445 56324ebe 2022-07-14 thomas free(gotwebd_env->servers);
446 56324ebe 2022-07-14 thomas free(gotwebd_env);
447 56324ebe 2022-07-14 thomas }
448 56324ebe 2022-07-14 thomas
449 56324ebe 2022-07-14 thomas int
450 56324ebe 2022-07-14 thomas sockets_privinit(struct gotwebd *env, struct socket *sock)
451 56324ebe 2022-07-14 thomas {
452 56324ebe 2022-07-14 thomas struct privsep *ps = env->gotwebd_ps;
453 56324ebe 2022-07-14 thomas
454 56324ebe 2022-07-14 thomas if (sock->conf.type == UNIX) {
455 56324ebe 2022-07-14 thomas log_debug("%s: initializing unix socket %s", __func__,
456 56324ebe 2022-07-14 thomas sock->conf.unix_socket_name);
457 56324ebe 2022-07-14 thomas sock->fd = sockets_unix_socket_listen(ps, sock);
458 56324ebe 2022-07-14 thomas if (sock->fd == -1) {
459 56324ebe 2022-07-14 thomas log_warnx("%s: create unix socket failed", __func__);
460 56324ebe 2022-07-14 thomas return -1;
461 56324ebe 2022-07-14 thomas }
462 56324ebe 2022-07-14 thomas }
463 56324ebe 2022-07-14 thomas
464 56324ebe 2022-07-14 thomas if (sock->conf.type == FCGI) {
465 56324ebe 2022-07-14 thomas log_debug("%s: initializing fcgi socket for %s", __func__,
466 56324ebe 2022-07-14 thomas sock->conf.name);
467 56324ebe 2022-07-14 thomas sock->fd = sockets_create_socket(sock->conf.al,
468 56324ebe 2022-07-14 thomas sock->conf.fcgi_socket_port);
469 56324ebe 2022-07-14 thomas if (sock->fd == -1) {
470 56324ebe 2022-07-14 thomas log_warnx("%s: create unix socket failed", __func__);
471 56324ebe 2022-07-14 thomas return -1;
472 56324ebe 2022-07-14 thomas }
473 56324ebe 2022-07-14 thomas }
474 56324ebe 2022-07-14 thomas
475 56324ebe 2022-07-14 thomas return 0;
476 56324ebe 2022-07-14 thomas }
477 56324ebe 2022-07-14 thomas
478 56324ebe 2022-07-14 thomas int
479 56324ebe 2022-07-14 thomas sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
480 56324ebe 2022-07-14 thomas {
481 56324ebe 2022-07-14 thomas struct gotwebd *env = ps->ps_env;
482 56324ebe 2022-07-14 thomas struct sockaddr_un sun;
483 56324ebe 2022-07-14 thomas struct socket *tsock;
484 56324ebe 2022-07-14 thomas int u_fd = -1;
485 56324ebe 2022-07-14 thomas mode_t old_umask, mode;
486 56324ebe 2022-07-14 thomas
487 56324ebe 2022-07-14 thomas TAILQ_FOREACH(tsock, env->sockets, entry) {
488 56324ebe 2022-07-14 thomas if (strcmp(tsock->conf.unix_socket_name,
489 56324ebe 2022-07-14 thomas sock->conf.unix_socket_name) == 0 &&
490 56324ebe 2022-07-14 thomas tsock->fd != -1)
491 56324ebe 2022-07-14 thomas return (tsock->fd);
492 56324ebe 2022-07-14 thomas }
493 56324ebe 2022-07-14 thomas
494 56324ebe 2022-07-14 thomas u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
495 56324ebe 2022-07-14 thomas if (u_fd == -1) {
496 56324ebe 2022-07-14 thomas log_warn("%s: socket", __func__);
497 56324ebe 2022-07-14 thomas return -1;
498 56324ebe 2022-07-14 thomas }
499 56324ebe 2022-07-14 thomas
500 56324ebe 2022-07-14 thomas sun.sun_family = AF_UNIX;
501 56324ebe 2022-07-14 thomas if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
502 56324ebe 2022-07-14 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
503 56324ebe 2022-07-14 thomas log_warn("%s: %s name too long", __func__,
504 56324ebe 2022-07-14 thomas sock->conf.unix_socket_name);
505 56324ebe 2022-07-14 thomas close(u_fd);
506 56324ebe 2022-07-14 thomas return -1;
507 56324ebe 2022-07-14 thomas }
508 56324ebe 2022-07-14 thomas
509 56324ebe 2022-07-14 thomas if (unlink(sock->conf.unix_socket_name) == -1) {
510 56324ebe 2022-07-14 thomas if (errno != ENOENT) {
511 56324ebe 2022-07-14 thomas log_warn("%s: unlink %s", __func__,
512 56324ebe 2022-07-14 thomas sock->conf.unix_socket_name);
513 56324ebe 2022-07-14 thomas close(u_fd);
514 56324ebe 2022-07-14 thomas return -1;
515 56324ebe 2022-07-14 thomas }
516 56324ebe 2022-07-14 thomas }
517 56324ebe 2022-07-14 thomas
518 56324ebe 2022-07-14 thomas old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
519 56324ebe 2022-07-14 thomas mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
520 56324ebe 2022-07-14 thomas
521 56324ebe 2022-07-14 thomas if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
522 56324ebe 2022-07-14 thomas log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
523 56324ebe 2022-07-14 thomas close(u_fd);
524 56324ebe 2022-07-14 thomas (void)umask(old_umask);
525 56324ebe 2022-07-14 thomas return -1;
526 56324ebe 2022-07-14 thomas }
527 56324ebe 2022-07-14 thomas
528 56324ebe 2022-07-14 thomas (void)umask(old_umask);
529 56324ebe 2022-07-14 thomas
530 56324ebe 2022-07-14 thomas if (chmod(sock->conf.unix_socket_name, mode) == -1) {
531 56324ebe 2022-07-14 thomas log_warn("%s: chmod", __func__);
532 56324ebe 2022-07-14 thomas close(u_fd);
533 56324ebe 2022-07-14 thomas (void)unlink(sock->conf.unix_socket_name);
534 56324ebe 2022-07-14 thomas return -1;
535 56324ebe 2022-07-14 thomas }
536 56324ebe 2022-07-14 thomas
537 56324ebe 2022-07-14 thomas if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
538 56324ebe 2022-07-14 thomas ps->ps_pw->pw_gid) == -1) {
539 56324ebe 2022-07-14 thomas log_warn("%s: chown", __func__);
540 56324ebe 2022-07-14 thomas close(u_fd);
541 56324ebe 2022-07-14 thomas (void)unlink(sock->conf.unix_socket_name);
542 56324ebe 2022-07-14 thomas return -1;
543 56324ebe 2022-07-14 thomas }
544 56324ebe 2022-07-14 thomas
545 56324ebe 2022-07-14 thomas if (listen(u_fd, SOCKS_BACKLOG) == -1) {
546 56324ebe 2022-07-14 thomas log_warn("%s: listen", __func__);
547 56324ebe 2022-07-14 thomas return -1;
548 56324ebe 2022-07-14 thomas }
549 56324ebe 2022-07-14 thomas
550 56324ebe 2022-07-14 thomas return u_fd;
551 56324ebe 2022-07-14 thomas }
552 56324ebe 2022-07-14 thomas
553 56324ebe 2022-07-14 thomas int
554 56324ebe 2022-07-14 thomas sockets_create_socket(struct addresslist *al, in_port_t port)
555 56324ebe 2022-07-14 thomas {
556 56324ebe 2022-07-14 thomas struct addrinfo hints;
557 56324ebe 2022-07-14 thomas struct address *a;
558 56324ebe 2022-07-14 thomas int fd = -1, o_val = 1, flags;
559 56324ebe 2022-07-14 thomas
560 56324ebe 2022-07-14 thomas memset(&hints, 0, sizeof(hints));
561 56324ebe 2022-07-14 thomas hints.ai_family = AF_UNSPEC;
562 56324ebe 2022-07-14 thomas hints.ai_socktype = SOCK_STREAM;
563 56324ebe 2022-07-14 thomas hints.ai_flags |= AI_PASSIVE;
564 56324ebe 2022-07-14 thomas
565 56324ebe 2022-07-14 thomas TAILQ_FOREACH(a, al, entry) {
566 56324ebe 2022-07-14 thomas if (sockets_socket_af(&a->ss, port) == -1) {
567 56324ebe 2022-07-14 thomas log_warnx("%s: sockets_socket_af", __func__);
568 56324ebe 2022-07-14 thomas goto fail;
569 56324ebe 2022-07-14 thomas }
570 56324ebe 2022-07-14 thomas
571 56324ebe 2022-07-14 thomas fd = socket(a->ss.ss_family, hints.ai_socktype,
572 56324ebe 2022-07-14 thomas a->ipproto);
573 56324ebe 2022-07-14 thomas log_debug("%s: opening socket (%d) for %s", __func__,
574 56324ebe 2022-07-14 thomas fd, a->ifname);
575 56324ebe 2022-07-14 thomas
576 56324ebe 2022-07-14 thomas if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
577 56324ebe 2022-07-14 thomas sizeof(int)) == -1) {
578 56324ebe 2022-07-14 thomas log_warn("%s: setsockopt error", __func__);
579 56324ebe 2022-07-14 thomas return -1;
580 56324ebe 2022-07-14 thomas }
581 56324ebe 2022-07-14 thomas
582 56324ebe 2022-07-14 thomas /* non-blocking */
583 56324ebe 2022-07-14 thomas flags = fcntl(fd, F_GETFL);
584 56324ebe 2022-07-14 thomas flags |= O_NONBLOCK;
585 56324ebe 2022-07-14 thomas fcntl(fd, F_SETFL, flags);
586 56324ebe 2022-07-14 thomas
587 d7cad54e 2022-07-14 thomas /* TA: 'sizeof a' vs a->ss.len */
588 d7cad54e 2022-07-14 thomas if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
589 56324ebe 2022-07-14 thomas close(fd);
590 56324ebe 2022-07-14 thomas log_info("%s: can't bind to port %d", __func__,
591 56324ebe 2022-07-14 thomas ntohs(port));
592 56324ebe 2022-07-14 thomas goto fail;
593 56324ebe 2022-07-14 thomas }
594 56324ebe 2022-07-14 thomas
595 56324ebe 2022-07-14 thomas if (listen(fd, SOMAXCONN) == -1) {
596 56324ebe 2022-07-14 thomas log_warn("%s, unable to listen on socket", __func__);
597 56324ebe 2022-07-14 thomas goto fail;
598 56324ebe 2022-07-14 thomas }
599 56324ebe 2022-07-14 thomas }
600 56324ebe 2022-07-14 thomas
601 56324ebe 2022-07-14 thomas free(a);
602 56324ebe 2022-07-14 thomas return (fd);
603 56324ebe 2022-07-14 thomas fail:
604 56324ebe 2022-07-14 thomas free(a);
605 56324ebe 2022-07-14 thomas return -1;
606 56324ebe 2022-07-14 thomas }
607 56324ebe 2022-07-14 thomas
608 56324ebe 2022-07-14 thomas int
609 56324ebe 2022-07-14 thomas sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
610 56324ebe 2022-07-14 thomas int reserve, volatile int *counter)
611 56324ebe 2022-07-14 thomas {
612 56324ebe 2022-07-14 thomas int ret;
613 56324ebe 2022-07-14 thomas
614 56324ebe 2022-07-14 thomas if (getdtablecount() + reserve +
615 56324ebe 2022-07-14 thomas ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
616 56324ebe 2022-07-14 thomas log_debug("inflight fds exceeded");
617 56324ebe 2022-07-14 thomas errno = EMFILE;
618 56324ebe 2022-07-14 thomas return -1;
619 56324ebe 2022-07-14 thomas }
620 56324ebe 2022-07-14 thomas
621 56324ebe 2022-07-14 thomas if ((ret = accept4(sockfd, addr, addrlen,
622 56324ebe 2022-07-14 thomas SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
623 56324ebe 2022-07-14 thomas (*counter)++;
624 56324ebe 2022-07-14 thomas log_debug("inflight incremented, now %d", *counter);
625 56324ebe 2022-07-14 thomas }
626 56324ebe 2022-07-14 thomas
627 56324ebe 2022-07-14 thomas return ret;
628 56324ebe 2022-07-14 thomas }
629 56324ebe 2022-07-14 thomas
630 56324ebe 2022-07-14 thomas void
631 56324ebe 2022-07-14 thomas sockets_accept_paused(int fd, short events, void *arg)
632 56324ebe 2022-07-14 thomas {
633 56324ebe 2022-07-14 thomas struct socket *sock = (struct socket *)arg;
634 56324ebe 2022-07-14 thomas
635 56324ebe 2022-07-14 thomas event_add(&sock->ev, NULL);
636 56324ebe 2022-07-14 thomas }
637 56324ebe 2022-07-14 thomas
638 56324ebe 2022-07-14 thomas void
639 56324ebe 2022-07-14 thomas sockets_socket_accept(int fd, short event, void *arg)
640 56324ebe 2022-07-14 thomas {
641 56324ebe 2022-07-14 thomas struct socket *sock = (struct socket *)arg;
642 56324ebe 2022-07-14 thomas struct sockaddr_storage ss;
643 56324ebe 2022-07-14 thomas struct timeval backoff;
644 56324ebe 2022-07-14 thomas struct request *c = NULL;
645 56324ebe 2022-07-14 thomas socklen_t len;
646 56324ebe 2022-07-14 thomas int s;
647 56324ebe 2022-07-14 thomas
648 56324ebe 2022-07-14 thomas backoff.tv_sec = 1;
649 56324ebe 2022-07-14 thomas backoff.tv_usec = 0;
650 56324ebe 2022-07-14 thomas
651 56324ebe 2022-07-14 thomas event_add(&sock->ev, NULL);
652 56324ebe 2022-07-14 thomas if (event & EV_TIMEOUT)
653 56324ebe 2022-07-14 thomas return;
654 56324ebe 2022-07-14 thomas
655 56324ebe 2022-07-14 thomas len = sizeof(ss);
656 56324ebe 2022-07-14 thomas
657 56324ebe 2022-07-14 thomas s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
658 56324ebe 2022-07-14 thomas FD_RESERVE, &cgi_inflight);
659 56324ebe 2022-07-14 thomas
660 56324ebe 2022-07-14 thomas if (s == -1) {
661 56324ebe 2022-07-14 thomas switch (errno) {
662 56324ebe 2022-07-14 thomas case EINTR:
663 56324ebe 2022-07-14 thomas case EWOULDBLOCK:
664 56324ebe 2022-07-14 thomas case ECONNABORTED:
665 56324ebe 2022-07-14 thomas return;
666 56324ebe 2022-07-14 thomas case EMFILE:
667 56324ebe 2022-07-14 thomas case ENFILE:
668 56324ebe 2022-07-14 thomas event_del(&sock->ev);
669 56324ebe 2022-07-14 thomas evtimer_add(&sock->pause, &backoff);
670 56324ebe 2022-07-14 thomas return;
671 56324ebe 2022-07-14 thomas default:
672 56324ebe 2022-07-14 thomas log_warn("%s: accept", __func__);
673 56324ebe 2022-07-14 thomas }
674 56324ebe 2022-07-14 thomas }
675 56324ebe 2022-07-14 thomas
676 56324ebe 2022-07-14 thomas if (client_cnt > GOTWEBD_MAXCLIENTS)
677 56324ebe 2022-07-14 thomas goto err;
678 56324ebe 2022-07-14 thomas
679 56324ebe 2022-07-14 thomas c = calloc(1, sizeof(struct request));
680 56324ebe 2022-07-14 thomas if (c == NULL) {
681 56324ebe 2022-07-14 thomas log_warn("%s", __func__);
682 56324ebe 2022-07-14 thomas close(s);
683 56324ebe 2022-07-14 thomas cgi_inflight--;
684 56324ebe 2022-07-14 thomas return;
685 56324ebe 2022-07-14 thomas }
686 56324ebe 2022-07-14 thomas
687 56324ebe 2022-07-14 thomas c->fd = s;
688 56324ebe 2022-07-14 thomas c->sock = sock;
689 56324ebe 2022-07-14 thomas memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
690 56324ebe 2022-07-14 thomas c->buf_pos = 0;
691 56324ebe 2022-07-14 thomas c->buf_len = 0;
692 56324ebe 2022-07-14 thomas c->request_started = 0;
693 56324ebe 2022-07-14 thomas c->sock->client_status = CLIENT_CONNECT;
694 56324ebe 2022-07-14 thomas
695 56324ebe 2022-07-14 thomas event_set(&c->ev, s, EV_READ, fcgi_request, c);
696 56324ebe 2022-07-14 thomas event_add(&c->ev, NULL);
697 56324ebe 2022-07-14 thomas
698 56324ebe 2022-07-14 thomas evtimer_set(&c->tmo, fcgi_timeout, c);
699 56324ebe 2022-07-14 thomas evtimer_add(&c->tmo, &timeout);
700 56324ebe 2022-07-14 thomas
701 56324ebe 2022-07-14 thomas client_cnt++;
702 56324ebe 2022-07-14 thomas
703 56324ebe 2022-07-14 thomas return;
704 56324ebe 2022-07-14 thomas err:
705 56324ebe 2022-07-14 thomas cgi_inflight--;
706 56324ebe 2022-07-14 thomas close(s);
707 56324ebe 2022-07-14 thomas if (c != NULL)
708 56324ebe 2022-07-14 thomas free(c);
709 56324ebe 2022-07-14 thomas }
710 56324ebe 2022-07-14 thomas
711 56324ebe 2022-07-14 thomas void
712 56324ebe 2022-07-14 thomas sockets_rlimit(int maxfd)
713 56324ebe 2022-07-14 thomas {
714 56324ebe 2022-07-14 thomas struct rlimit rl;
715 56324ebe 2022-07-14 thomas
716 56324ebe 2022-07-14 thomas if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
717 56324ebe 2022-07-14 thomas fatal("%s: failed to get resource limit", __func__);
718 56324ebe 2022-07-14 thomas log_debug("%s: max open files %llu", __func__, rl.rlim_max);
719 56324ebe 2022-07-14 thomas
720 56324ebe 2022-07-14 thomas /*
721 56324ebe 2022-07-14 thomas * Allow the maximum number of open file descriptors for this
722 56324ebe 2022-07-14 thomas * login class (which should be the class "daemon" by default).
723 56324ebe 2022-07-14 thomas */
724 56324ebe 2022-07-14 thomas if (maxfd == -1)
725 56324ebe 2022-07-14 thomas rl.rlim_cur = rl.rlim_max;
726 56324ebe 2022-07-14 thomas else
727 56324ebe 2022-07-14 thomas rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
728 56324ebe 2022-07-14 thomas if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
729 56324ebe 2022-07-14 thomas fatal("%s: failed to set resource limit", __func__);
730 d7cad54e 2022-07-14 thomas }