Blame


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