2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
8 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
9 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
11 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 a596b957 2022-07-14 tracey #include <sys/param.h>
21 a596b957 2022-07-14 tracey #include <sys/ioctl.h>
22 a596b957 2022-07-14 tracey #include <sys/queue.h>
23 a596b957 2022-07-14 tracey #include <sys/wait.h>
24 a596b957 2022-07-14 tracey #include <sys/uio.h>
25 a596b957 2022-07-14 tracey #include <sys/resource.h>
26 a596b957 2022-07-14 tracey #include <sys/socket.h>
27 a596b957 2022-07-14 tracey #include <sys/stat.h>
28 a596b957 2022-07-14 tracey #include <sys/time.h>
29 a596b957 2022-07-14 tracey #include <sys/types.h>
30 a596b957 2022-07-14 tracey #include <sys/mman.h>
31 a596b957 2022-07-14 tracey #include <sys/un.h>
33 a596b957 2022-07-14 tracey #include <net/if.h>
34 a596b957 2022-07-14 tracey #include <netinet/in.h>
36 a596b957 2022-07-14 tracey #include <errno.h>
37 a596b957 2022-07-14 tracey #include <event.h>
38 a596b957 2022-07-14 tracey #include <fcntl.h>
39 a596b957 2022-07-14 tracey #include <ifaddrs.h>
40 a596b957 2022-07-14 tracey #include <imsg.h>
41 a596b957 2022-07-14 tracey #include <limits.h>
42 a596b957 2022-07-14 tracey #include <netdb.h>
43 a596b957 2022-07-14 tracey #include <poll.h>
44 a596b957 2022-07-14 tracey #include <pwd.h>
45 a596b957 2022-07-14 tracey #include <stddef.h>
46 a596b957 2022-07-14 tracey #include <stdio.h>
47 a596b957 2022-07-14 tracey #include <stdlib.h>
48 a596b957 2022-07-14 tracey #include <string.h>
49 a596b957 2022-07-14 tracey #include <unistd.h>
50 a596b957 2022-07-14 tracey #include <util.h>
52 a596b957 2022-07-14 tracey #include "got_error.h"
53 a596b957 2022-07-14 tracey #include "got_opentemp.h"
54 df2d3cd2 2023-03-11 op #include "got_reference.h"
55 b5c757f5 2022-09-01 stsp #include "got_repository.h"
56 6fe3b58a 2023-11-14 stsp #include "got_privsep.h"
58 a596b957 2022-07-14 tracey #include "gotwebd.h"
59 1220d7ea 2024-05-21 op #include "log.h"
60 ed619ca0 2022-12-14 op #include "tmpl.h"
62 a596b957 2022-07-14 tracey #define SOCKS_BACKLOG 5
63 a596b957 2022-07-14 tracey #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
65 a596b957 2022-07-14 tracey volatile int client_cnt;
67 026ac2c4 2022-08-20 stsp static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
69 026ac2c4 2022-08-20 stsp static void sockets_sighdlr(int, short, void *);
70 26678add 2023-11-16 op static void sockets_shutdown(void);
71 026ac2c4 2022-08-20 stsp static void sockets_launch(void);
72 026ac2c4 2022-08-20 stsp static void sockets_purge(struct gotwebd *);
73 026ac2c4 2022-08-20 stsp static void sockets_accept_paused(int, short, void *);
74 026ac2c4 2022-08-20 stsp static void sockets_rlimit(int);
76 26678add 2023-11-16 op static void sockets_dispatch_main(int, short, void *);
77 26678add 2023-11-16 op static int sockets_unix_socket_listen(struct gotwebd *, struct socket *);
78 0c64c2f8 2023-11-15 op static int sockets_create_socket(struct address *);
79 026ac2c4 2022-08-20 stsp static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
80 026ac2c4 2022-08-20 stsp int, volatile int *);
82 36079424 2024-05-16 op static struct socket *sockets_conf_new_socket(struct gotwebd *,
83 36079424 2024-05-16 op int, struct address *);
85 a596b957 2022-07-14 tracey int cgi_inflight = 0;
88 26678add 2023-11-16 op sockets(struct gotwebd *env, int fd)
90 736fac90 2024-02-10 op struct event sighup, sigint, sigusr1, sigchld, sigterm;
94 a596b957 2022-07-14 tracey sockets_rlimit(-1);
96 26678add 2023-11-16 op if (config_init(env) == -1)
97 26678add 2023-11-16 op fatal("failed to initialize configuration");
99 26678add 2023-11-16 op if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
100 26678add 2023-11-16 op fatal("malloc");
101 26678add 2023-11-16 op imsg_init(&env->iev_parent->ibuf, fd);
102 26678add 2023-11-16 op env->iev_parent->handler = sockets_dispatch_main;
103 26678add 2023-11-16 op env->iev_parent->data = env->iev_parent;
104 26678add 2023-11-16 op event_set(&env->iev_parent->ev, fd, EV_READ, sockets_dispatch_main,
105 26678add 2023-11-16 op env->iev_parent);
106 26678add 2023-11-16 op event_add(&env->iev_parent->ev, NULL);
108 8def9425 2023-11-16 op signal(SIGPIPE, SIG_IGN);
110 56a9d315 2023-11-18 op signal_set(&sighup, SIGHUP, sockets_sighdlr, env);
111 26678add 2023-11-16 op signal_add(&sighup, NULL);
112 736fac90 2024-02-10 op signal_set(&sigint, SIGINT, sockets_sighdlr, env);
113 736fac90 2024-02-10 op signal_add(&sigint, NULL);
114 56a9d315 2023-11-18 op signal_set(&sigusr1, SIGUSR1, sockets_sighdlr, env);
115 26678add 2023-11-16 op signal_add(&sigusr1, NULL);
116 26678add 2023-11-16 op signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
117 26678add 2023-11-16 op signal_add(&sigchld, NULL);
118 736fac90 2024-02-10 op signal_set(&sigterm, SIGTERM, sockets_sighdlr, env);
119 736fac90 2024-02-10 op signal_add(&sigterm, NULL);
121 a596b957 2022-07-14 tracey #ifndef PROFILE
122 63c6b10f 2023-11-14 stsp if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
123 63c6b10f 2023-11-14 stsp NULL) == -1)
124 a596b957 2022-07-14 tracey fatal("pledge");
127 26678add 2023-11-16 op event_dispatch();
128 26678add 2023-11-16 op sockets_shutdown();
132 a596b957 2022-07-14 tracey sockets_parse_sockets(struct gotwebd *env)
134 610dd8c9 2022-08-19 stsp struct address *a;
135 4fcc9f74 2022-08-16 stsp struct socket *new_sock = NULL;
136 610dd8c9 2022-08-19 stsp int sock_id = 1;
138 36079424 2024-05-16 op TAILQ_FOREACH(a, &env->addresses, entry) {
139 36079424 2024-05-16 op new_sock = sockets_conf_new_socket(env, sock_id, a);
140 36079424 2024-05-16 op if (new_sock) {
142 36079424 2024-05-16 op TAILQ_INSERT_TAIL(&env->sockets,
143 36079424 2024-05-16 op new_sock, entry);
148 026ac2c4 2022-08-20 stsp static struct socket *
149 36079424 2024-05-16 op sockets_conf_new_socket(struct gotwebd *env, int id, struct address *a)
151 610dd8c9 2022-08-19 stsp struct socket *sock;
152 610dd8c9 2022-08-19 stsp struct address *acp;
154 610dd8c9 2022-08-19 stsp if ((sock = calloc(1, sizeof(*sock))) == NULL)
155 610dd8c9 2022-08-19 stsp fatalx("%s: calloc", __func__);
157 610dd8c9 2022-08-19 stsp sock->conf.id = id;
158 610dd8c9 2022-08-19 stsp sock->fd = -1;
159 610dd8c9 2022-08-19 stsp sock->conf.af_type = a->ss.ss_family;
161 36079424 2024-05-16 op if (a->ss.ss_family == AF_UNIX) {
162 36079424 2024-05-16 op struct sockaddr_un *sun;
164 36079424 2024-05-16 op sun = (struct sockaddr_un *)&a->ss;
165 36079424 2024-05-16 op if (strlcpy(sock->conf.unix_socket_name, sun->sun_path,
166 36079424 2024-05-16 op sizeof(sock->conf.unix_socket_name)) >=
167 36079424 2024-05-16 op sizeof(sock->conf.unix_socket_name))
168 36079424 2024-05-16 op fatalx("unix socket path too long: %s", sun->sun_path);
171 36079424 2024-05-16 op sock->conf.fcgi_socket_port = a->port;
173 610dd8c9 2022-08-19 stsp acp = &sock->conf.addr;
175 610dd8c9 2022-08-19 stsp memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
176 cdfd248a 2023-11-15 op acp->slen = a->slen;
177 0c64c2f8 2023-11-15 op acp->ai_family = a->ai_family;
178 0c64c2f8 2023-11-15 op acp->ai_socktype = a->ai_socktype;
179 0c64c2f8 2023-11-15 op acp->ai_protocol = a->ai_protocol;
180 610dd8c9 2022-08-19 stsp acp->port = a->port;
181 4448825a 2023-06-16 op if (*a->ifname != '\0') {
182 610dd8c9 2022-08-19 stsp if (strlcpy(acp->ifname, a->ifname,
183 610dd8c9 2022-08-19 stsp sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
184 610dd8c9 2022-08-19 stsp fatalx("%s: interface name truncated",
189 a596b957 2022-07-14 tracey return (sock);
192 026ac2c4 2022-08-20 stsp static void
193 a596b957 2022-07-14 tracey sockets_launch(void)
195 a596b957 2022-07-14 tracey struct socket *sock;
196 6fe3b58a 2023-11-14 stsp struct server *srv;
197 6fe3b58a 2023-11-14 stsp const struct got_error *error;
199 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
200 a596b957 2022-07-14 tracey log_debug("%s: configuring socket %d (%d)", __func__,
201 a596b957 2022-07-14 tracey sock->conf.id, sock->fd);
203 a596b957 2022-07-14 tracey event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
204 a596b957 2022-07-14 tracey sockets_socket_accept, sock);
206 a596b957 2022-07-14 tracey if (event_add(&sock->ev, NULL))
207 a596b957 2022-07-14 tracey fatalx("event add sock");
209 a596b957 2022-07-14 tracey evtimer_set(&sock->pause, sockets_accept_paused, sock);
211 a596b957 2022-07-14 tracey log_debug("%s: running socket listener %d", __func__,
212 a596b957 2022-07-14 tracey sock->conf.id);
215 6fe3b58a 2023-11-14 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
216 6fe3b58a 2023-11-14 stsp if (unveil(srv->repos_path, "r") == -1)
217 6fe3b58a 2023-11-14 stsp fatal("unveil %s", srv->repos_path);
220 6fe3b58a 2023-11-14 stsp error = got_privsep_unveil_exec_helpers();
222 6fe3b58a 2023-11-14 stsp fatal("%s", error->msg);
224 6fe3b58a 2023-11-14 stsp if (unveil(NULL, NULL) == -1)
225 6fe3b58a 2023-11-14 stsp fatal("unveil");
228 026ac2c4 2022-08-20 stsp static void
229 a596b957 2022-07-14 tracey sockets_purge(struct gotwebd *env)
231 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
233 a596b957 2022-07-14 tracey /* shutdown and remove sockets */
234 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
235 a596b957 2022-07-14 tracey if (event_initialized(&sock->ev))
236 a596b957 2022-07-14 tracey event_del(&sock->ev);
237 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->evt))
238 a596b957 2022-07-14 tracey evtimer_del(&sock->evt);
239 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->pause))
240 a596b957 2022-07-14 tracey evtimer_del(&sock->pause);
241 a596b957 2022-07-14 tracey if (sock->fd != -1)
242 a596b957 2022-07-14 tracey close(sock->fd);
243 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&env->sockets, sock, entry);
248 26678add 2023-11-16 op sockets_dispatch_main(int fd, short event, void *arg)
250 26678add 2023-11-16 op struct imsgev *iev = arg;
251 26678add 2023-11-16 op struct imsgbuf *ibuf;
252 26678add 2023-11-16 op struct imsg imsg;
253 26678add 2023-11-16 op struct gotwebd *env = gotwebd_env;
255 26678add 2023-11-16 op int shut = 0;
257 26678add 2023-11-16 op ibuf = &iev->ibuf;
259 26678add 2023-11-16 op if (event & EV_READ) {
260 26678add 2023-11-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
261 26678add 2023-11-16 op fatal("imsg_read error");
262 26678add 2023-11-16 op if (n == 0) /* Connection closed */
265 26678add 2023-11-16 op if (event & EV_WRITE) {
266 26678add 2023-11-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
267 26678add 2023-11-16 op fatal("msgbuf_write");
268 26678add 2023-11-16 op if (n == 0) /* Connection closed */
273 26678add 2023-11-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
274 26678add 2023-11-16 op fatal("imsg_get");
275 26678add 2023-11-16 op if (n == 0) /* No more messages. */
278 26678add 2023-11-16 op switch (imsg.hdr.type) {
279 26678add 2023-11-16 op case IMSG_CFG_SRV:
280 26678add 2023-11-16 op config_getserver(env, &imsg);
282 26678add 2023-11-16 op case IMSG_CFG_SOCK:
283 26678add 2023-11-16 op config_getsock(env, &imsg);
285 26678add 2023-11-16 op case IMSG_CFG_FD:
286 26678add 2023-11-16 op config_getfd(env, &imsg);
288 26678add 2023-11-16 op case IMSG_CFG_DONE:
289 26678add 2023-11-16 op config_getcfg(env, &imsg);
291 26678add 2023-11-16 op case IMSG_CTL_START:
292 26678add 2023-11-16 op sockets_launch();
295 26678add 2023-11-16 op fatalx("%s: unknown imsg type %d", __func__,
296 26678add 2023-11-16 op imsg.hdr.type);
299 26678add 2023-11-16 op imsg_free(&imsg);
303 26678add 2023-11-16 op imsg_event_add(iev);
305 26678add 2023-11-16 op /* This pipe is dead. Remove its event handler */
306 26678add 2023-11-16 op event_del(&iev->ev);
307 26678add 2023-11-16 op event_loopexit(NULL);
311 026ac2c4 2022-08-20 stsp static void
312 a596b957 2022-07-14 tracey sockets_sighdlr(int sig, short event, void *arg)
314 a596b957 2022-07-14 tracey switch (sig) {
315 a596b957 2022-07-14 tracey case SIGHUP:
316 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGHUP", __func__);
318 a596b957 2022-07-14 tracey case SIGPIPE:
319 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGPIPE", __func__);
321 a596b957 2022-07-14 tracey case SIGUSR1:
322 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGUSR1", __func__);
324 a596b957 2022-07-14 tracey case SIGCHLD:
327 736fac90 2024-02-10 op case SIGTERM:
328 736fac90 2024-02-10 op sockets_shutdown();
331 a596b957 2022-07-14 tracey log_info("SIGNAL: %d", sig);
332 a596b957 2022-07-14 tracey fatalx("unexpected signal");
337 a596b957 2022-07-14 tracey sockets_shutdown(void)
339 a596b957 2022-07-14 tracey struct server *srv, *tsrv;
340 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
342 a596b957 2022-07-14 tracey sockets_purge(gotwebd_env);
344 a596b957 2022-07-14 tracey /* clean sockets */
345 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
346 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
347 a596b957 2022-07-14 tracey close(sock->fd);
348 a596b957 2022-07-14 tracey free(sock);
351 a596b957 2022-07-14 tracey /* clean servers */
352 1632f50a 2023-11-17 stsp TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
353 a596b957 2022-07-14 tracey free(srv);
355 a596b957 2022-07-14 tracey free(gotwebd_env);
361 a596b957 2022-07-14 tracey sockets_privinit(struct gotwebd *env, struct socket *sock)
363 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_UNIX) {
364 a596b957 2022-07-14 tracey log_debug("%s: initializing unix socket %s", __func__,
365 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
366 26678add 2023-11-16 op sock->fd = sockets_unix_socket_listen(env, sock);
367 a596b957 2022-07-14 tracey if (sock->fd == -1) {
368 a596b957 2022-07-14 tracey log_warnx("%s: create unix socket failed", __func__);
369 a596b957 2022-07-14 tracey return -1;
373 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
374 36079424 2024-05-16 op log_debug("%s: initializing %s FCGI socket on port %d",
375 4fcc9f74 2022-08-16 stsp __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
376 36079424 2024-05-16 op sock->conf.fcgi_socket_port);
377 0c64c2f8 2023-11-15 op sock->fd = sockets_create_socket(&sock->conf.addr);
378 a596b957 2022-07-14 tracey if (sock->fd == -1) {
379 610dd8c9 2022-08-19 stsp log_warnx("%s: create FCGI socket failed", __func__);
380 a596b957 2022-07-14 tracey return -1;
384 a596b957 2022-07-14 tracey return 0;
388 26678add 2023-11-16 op sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
390 a596b957 2022-07-14 tracey int u_fd = -1;
391 a596b957 2022-07-14 tracey mode_t old_umask, mode;
393 a596b957 2022-07-14 tracey u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
394 a596b957 2022-07-14 tracey if (u_fd == -1) {
395 a596b957 2022-07-14 tracey log_warn("%s: socket", __func__);
396 a596b957 2022-07-14 tracey return -1;
399 a596b957 2022-07-14 tracey if (unlink(sock->conf.unix_socket_name) == -1) {
400 a596b957 2022-07-14 tracey if (errno != ENOENT) {
401 a596b957 2022-07-14 tracey log_warn("%s: unlink %s", __func__,
402 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
403 a596b957 2022-07-14 tracey close(u_fd);
404 a596b957 2022-07-14 tracey return -1;
408 a596b957 2022-07-14 tracey old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
409 a596b957 2022-07-14 tracey mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
411 36079424 2024-05-16 op if (bind(u_fd, (struct sockaddr *)&sock->conf.addr.ss,
412 36079424 2024-05-16 op sock->conf.addr.slen) == -1) {
413 a596b957 2022-07-14 tracey log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
414 a596b957 2022-07-14 tracey close(u_fd);
415 a596b957 2022-07-14 tracey (void)umask(old_umask);
416 a596b957 2022-07-14 tracey return -1;
419 a596b957 2022-07-14 tracey (void)umask(old_umask);
421 a596b957 2022-07-14 tracey if (chmod(sock->conf.unix_socket_name, mode) == -1) {
422 a596b957 2022-07-14 tracey log_warn("%s: chmod", __func__);
423 a596b957 2022-07-14 tracey close(u_fd);
424 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
425 a596b957 2022-07-14 tracey return -1;
428 26678add 2023-11-16 op if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
429 26678add 2023-11-16 op env->pw->pw_gid) == -1) {
430 a596b957 2022-07-14 tracey log_warn("%s: chown", __func__);
431 a596b957 2022-07-14 tracey close(u_fd);
432 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
433 a596b957 2022-07-14 tracey return -1;
436 a596b957 2022-07-14 tracey if (listen(u_fd, SOCKS_BACKLOG) == -1) {
437 a596b957 2022-07-14 tracey log_warn("%s: listen", __func__);
438 a596b957 2022-07-14 tracey return -1;
441 a596b957 2022-07-14 tracey return u_fd;
445 0c64c2f8 2023-11-15 op sockets_create_socket(struct address *a)
447 a596b957 2022-07-14 tracey int fd = -1, o_val = 1, flags;
449 0c64c2f8 2023-11-15 op fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
450 610dd8c9 2022-08-19 stsp if (fd == -1)
453 610dd8c9 2022-08-19 stsp log_debug("%s: opened socket (%d) for %s", __func__,
454 610dd8c9 2022-08-19 stsp fd, a->ifname);
456 610dd8c9 2022-08-19 stsp if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
457 610dd8c9 2022-08-19 stsp sizeof(int)) == -1) {
458 610dd8c9 2022-08-19 stsp log_warn("%s: setsockopt error", __func__);
463 610dd8c9 2022-08-19 stsp /* non-blocking */
464 610dd8c9 2022-08-19 stsp flags = fcntl(fd, F_GETFL);
465 610dd8c9 2022-08-19 stsp flags |= O_NONBLOCK;
466 610dd8c9 2022-08-19 stsp if (fcntl(fd, F_SETFL, flags) == -1) {
467 610dd8c9 2022-08-19 stsp log_info("%s: could not enable non-blocking I/O", __func__);
472 cdfd248a 2023-11-15 op if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
474 0c64c2f8 2023-11-15 op log_info("%s: can't bind to port %d", __func__, a->port);
478 610dd8c9 2022-08-19 stsp if (listen(fd, SOMAXCONN) == -1) {
479 610dd8c9 2022-08-19 stsp log_warn("%s, unable to listen on socket", __func__);
484 a596b957 2022-07-14 tracey return (fd);
488 a596b957 2022-07-14 tracey sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
489 a596b957 2022-07-14 tracey int reserve, volatile int *counter)
493 a596b957 2022-07-14 tracey if (getdtablecount() + reserve +
494 a596b957 2022-07-14 tracey ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
495 a596b957 2022-07-14 tracey log_debug("inflight fds exceeded");
496 a596b957 2022-07-14 tracey errno = EMFILE;
497 a596b957 2022-07-14 tracey return -1;
500 a596b957 2022-07-14 tracey if ((ret = accept4(sockfd, addr, addrlen,
501 a596b957 2022-07-14 tracey SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
502 a596b957 2022-07-14 tracey (*counter)++;
503 a596b957 2022-07-14 tracey log_debug("inflight incremented, now %d", *counter);
506 a596b957 2022-07-14 tracey return ret;
509 026ac2c4 2022-08-20 stsp static void
510 a596b957 2022-07-14 tracey sockets_accept_paused(int fd, short events, void *arg)
512 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
514 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
518 a596b957 2022-07-14 tracey sockets_socket_accept(int fd, short event, void *arg)
520 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
521 a596b957 2022-07-14 tracey struct sockaddr_storage ss;
522 a596b957 2022-07-14 tracey struct timeval backoff;
523 a596b957 2022-07-14 tracey struct request *c = NULL;
524 a596b957 2022-07-14 tracey socklen_t len;
527 a596b957 2022-07-14 tracey backoff.tv_sec = 1;
528 a596b957 2022-07-14 tracey backoff.tv_usec = 0;
530 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
531 a596b957 2022-07-14 tracey if (event & EV_TIMEOUT)
534 a596b957 2022-07-14 tracey len = sizeof(ss);
536 a596b957 2022-07-14 tracey s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
537 a596b957 2022-07-14 tracey FD_RESERVE, &cgi_inflight);
539 a596b957 2022-07-14 tracey if (s == -1) {
540 a596b957 2022-07-14 tracey switch (errno) {
541 a596b957 2022-07-14 tracey case EINTR:
542 a596b957 2022-07-14 tracey case EWOULDBLOCK:
543 a596b957 2022-07-14 tracey case ECONNABORTED:
545 a596b957 2022-07-14 tracey case EMFILE:
546 a596b957 2022-07-14 tracey case ENFILE:
547 a596b957 2022-07-14 tracey event_del(&sock->ev);
548 a596b957 2022-07-14 tracey evtimer_add(&sock->pause, &backoff);
551 a596b957 2022-07-14 tracey log_warn("%s: accept", __func__);
555 a596b957 2022-07-14 tracey if (client_cnt > GOTWEBD_MAXCLIENTS)
556 a596b957 2022-07-14 tracey goto err;
558 a596b957 2022-07-14 tracey c = calloc(1, sizeof(struct request));
559 a596b957 2022-07-14 tracey if (c == NULL) {
560 ed619ca0 2022-12-14 op log_warn("%s", __func__);
562 ed619ca0 2022-12-14 op cgi_inflight--;
566 62eab86e 2023-09-13 op c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
567 ed619ca0 2022-12-14 op if (c->tp == NULL) {
568 a596b957 2022-07-14 tracey log_warn("%s", __func__);
569 a596b957 2022-07-14 tracey close(s);
570 a596b957 2022-07-14 tracey cgi_inflight--;
575 a596b957 2022-07-14 tracey c->fd = s;
576 a596b957 2022-07-14 tracey c->sock = sock;
577 2a83fef7 2024-05-27 op memcpy(c->priv_fd, gotwebd_env->priv_fd, sizeof(c->priv_fd));
578 a596b957 2022-07-14 tracey c->buf_pos = 0;
579 a596b957 2022-07-14 tracey c->buf_len = 0;
580 a596b957 2022-07-14 tracey c->request_started = 0;
581 a596b957 2022-07-14 tracey c->sock->client_status = CLIENT_CONNECT;
583 5add7f42 2023-03-10 op event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
584 a596b957 2022-07-14 tracey event_add(&c->ev, NULL);
586 a596b957 2022-07-14 tracey evtimer_set(&c->tmo, fcgi_timeout, c);
587 a596b957 2022-07-14 tracey evtimer_add(&c->tmo, &timeout);
589 a596b957 2022-07-14 tracey client_cnt++;
593 a596b957 2022-07-14 tracey cgi_inflight--;
594 a596b957 2022-07-14 tracey close(s);
595 a596b957 2022-07-14 tracey if (c != NULL)
599 026ac2c4 2022-08-20 stsp static void
600 a596b957 2022-07-14 tracey sockets_rlimit(int maxfd)
602 a596b957 2022-07-14 tracey struct rlimit rl;
604 a596b957 2022-07-14 tracey if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
605 a596b957 2022-07-14 tracey fatal("%s: failed to get resource limit", __func__);
606 d6267a62 2022-07-17 naddy log_debug("%s: max open files %llu", __func__,
607 d6267a62 2022-07-17 naddy (unsigned long long)rl.rlim_max);
610 a596b957 2022-07-14 tracey * Allow the maximum number of open file descriptors for this
611 a596b957 2022-07-14 tracey * login class (which should be the class "daemon" by default).
613 a596b957 2022-07-14 tracey if (maxfd == -1)
614 a596b957 2022-07-14 tracey rl.rlim_cur = rl.rlim_max;
616 a596b957 2022-07-14 tracey rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
617 a596b957 2022-07-14 tracey if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
618 a596b957 2022-07-14 tracey fatal("%s: failed to set resource limit", __func__);