2 2b3d32a1 2022-12-30 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 2b3d32a1 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 2b3d32a1 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 2b3d32a1 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
8 2b3d32a1 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2b3d32a1 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2b3d32a1 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2b3d32a1 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2b3d32a1 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2b3d32a1 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2b3d32a1 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
19 2b3d32a1 2022-12-30 thomas #include <sys/types.h>
20 2b3d32a1 2022-12-30 thomas #include <sys/queue.h>
21 2b3d32a1 2022-12-30 thomas #include <sys/socket.h>
22 2b3d32a1 2022-12-30 thomas #include <sys/uio.h>
24 2b3d32a1 2022-12-30 thomas #include <errno.h>
25 2b3d32a1 2022-12-30 thomas #include <event.h>
26 2b3d32a1 2022-12-30 thomas #include <stdint.h>
27 2b3d32a1 2022-12-30 thomas #include <stdio.h>
28 2b3d32a1 2022-12-30 thomas #include <stdlib.h>
29 2b3d32a1 2022-12-30 thomas #include <string.h>
30 2b3d32a1 2022-12-30 thomas #include <imsg.h>
31 2b3d32a1 2022-12-30 thomas #include <limits.h>
32 2b3d32a1 2022-12-30 thomas #include <signal.h>
33 2b3d32a1 2022-12-30 thomas #include <unistd.h>
35 2b3d32a1 2022-12-30 thomas #include "got_error.h"
36 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
38 febe25b7 2023-08-29 thomas #include "got_compat.h"
40 2b3d32a1 2022-12-30 thomas #include "gotd.h"
41 2b3d32a1 2022-12-30 thomas #include "log.h"
42 2b3d32a1 2022-12-30 thomas #include "listen.h"
44 2b3d32a1 2022-12-30 thomas #ifndef nitems
45 2b3d32a1 2022-12-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 2b3d32a1 2022-12-30 thomas struct gotd_listen_client {
49 2b3d32a1 2022-12-30 thomas STAILQ_ENTRY(gotd_listen_client) entry;
50 2b3d32a1 2022-12-30 thomas uint32_t id;
52 ba63ab46 2023-01-02 thomas uid_t euid;
54 2b3d32a1 2022-12-30 thomas STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
56 2b3d32a1 2022-12-30 thomas static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
57 2b3d32a1 2022-12-30 thomas static SIPHASH_KEY clients_hash_key;
58 2b3d32a1 2022-12-30 thomas static volatile int listen_client_cnt;
59 2b3d32a1 2022-12-30 thomas static int inflight;
61 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter {
62 ba63ab46 2023-01-02 thomas STAILQ_ENTRY(gotd_uid_connection_counter) entry;
63 ba63ab46 2023-01-02 thomas uid_t euid;
64 ba63ab46 2023-01-02 thomas int nconnections;
66 ba63ab46 2023-01-02 thomas STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
67 ba63ab46 2023-01-02 thomas static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
68 ba63ab46 2023-01-02 thomas static SIPHASH_KEY uid_hash_key;
70 2b3d32a1 2022-12-30 thomas static struct {
71 2b3d32a1 2022-12-30 thomas pid_t pid;
72 2b3d32a1 2022-12-30 thomas const char *title;
74 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
75 2b3d32a1 2022-12-30 thomas struct gotd_imsgev pause;
76 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *connection_limits;
77 0781db0e 2023-01-06 thomas size_t nconnection_limits;
78 2b3d32a1 2022-12-30 thomas } gotd_listen;
80 2b3d32a1 2022-12-30 thomas static int inflight;
82 2b3d32a1 2022-12-30 thomas static void listen_shutdown(void);
84 2b3d32a1 2022-12-30 thomas static void
85 2b3d32a1 2022-12-30 thomas listen_sighdlr(int sig, short event, void *arg)
88 2b3d32a1 2022-12-30 thomas * Normal signal handler rules don't apply because libevent
89 2b3d32a1 2022-12-30 thomas * decouples for us.
92 2b3d32a1 2022-12-30 thomas switch (sig) {
93 2b3d32a1 2022-12-30 thomas case SIGHUP:
95 2b3d32a1 2022-12-30 thomas case SIGUSR1:
97 2b3d32a1 2022-12-30 thomas case SIGTERM:
98 2b3d32a1 2022-12-30 thomas case SIGINT:
99 2b3d32a1 2022-12-30 thomas listen_shutdown();
100 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
103 2b3d32a1 2022-12-30 thomas fatalx("unexpected signal");
107 2b3d32a1 2022-12-30 thomas static uint64_t
108 2b3d32a1 2022-12-30 thomas client_hash(uint32_t client_id)
110 2b3d32a1 2022-12-30 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
113 2b3d32a1 2022-12-30 thomas static void
114 2b3d32a1 2022-12-30 thomas add_client(struct gotd_listen_client *client)
116 2b3d32a1 2022-12-30 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
117 2b3d32a1 2022-12-30 thomas STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
118 2b3d32a1 2022-12-30 thomas listen_client_cnt++;
121 2b3d32a1 2022-12-30 thomas static struct gotd_listen_client *
122 2b3d32a1 2022-12-30 thomas find_client(uint32_t client_id)
124 2b3d32a1 2022-12-30 thomas uint64_t slot;
125 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *c;
127 2b3d32a1 2022-12-30 thomas slot = client_hash(client_id) % nitems(gotd_listen_clients);
128 2b3d32a1 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
129 2b3d32a1 2022-12-30 thomas if (c->id == client_id)
130 2b3d32a1 2022-12-30 thomas return c;
133 2b3d32a1 2022-12-30 thomas return NULL;
136 2b3d32a1 2022-12-30 thomas static uint32_t
137 2b3d32a1 2022-12-30 thomas get_client_id(void)
139 2b3d32a1 2022-12-30 thomas int duplicate = 0;
140 2b3d32a1 2022-12-30 thomas uint32_t id;
143 2b3d32a1 2022-12-30 thomas id = arc4random();
144 2b3d32a1 2022-12-30 thomas duplicate = (find_client(id) != NULL);
145 2b3d32a1 2022-12-30 thomas } while (duplicate || id == 0);
147 2b3d32a1 2022-12-30 thomas return id;
150 ba63ab46 2023-01-02 thomas static uint64_t
151 ba63ab46 2023-01-02 thomas uid_hash(uid_t euid)
153 ba63ab46 2023-01-02 thomas return SipHash24(&uid_hash_key, &euid, sizeof(euid));
156 ba63ab46 2023-01-02 thomas static void
157 ba63ab46 2023-01-02 thomas add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
159 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
160 ba63ab46 2023-01-02 thomas STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
163 ba63ab46 2023-01-02 thomas static void
164 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
166 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
167 ba63ab46 2023-01-02 thomas STAILQ_REMOVE(&gotd_client_uids[slot], counter,
168 ba63ab46 2023-01-02 thomas gotd_uid_connection_counter, entry);
171 ba63ab46 2023-01-02 thomas static struct gotd_uid_connection_counter *
172 ba63ab46 2023-01-02 thomas find_uid_connection_counter(uid_t euid)
174 ba63ab46 2023-01-02 thomas uint64_t slot;
175 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *c;
177 ba63ab46 2023-01-02 thomas slot = uid_hash(euid) % nitems(gotd_client_uids);
178 ba63ab46 2023-01-02 thomas STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
179 ba63ab46 2023-01-02 thomas if (c->euid == euid)
180 ba63ab46 2023-01-02 thomas return c;
183 ba63ab46 2023-01-02 thomas return NULL;
186 2b3d32a1 2022-12-30 thomas static const struct got_error *
187 2b3d32a1 2022-12-30 thomas disconnect(struct gotd_listen_client *client)
189 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter;
190 2b3d32a1 2022-12-30 thomas uint64_t slot;
191 2b3d32a1 2022-12-30 thomas int client_fd;
193 2b3d32a1 2022-12-30 thomas log_debug("client on fd %d disconnecting", client->fd);
195 2b3d32a1 2022-12-30 thomas slot = client_hash(client->id) % nitems(gotd_listen_clients);
196 2b3d32a1 2022-12-30 thomas STAILQ_REMOVE(&gotd_listen_clients[slot], client,
197 2b3d32a1 2022-12-30 thomas gotd_listen_client, entry);
199 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(client->euid);
200 ba63ab46 2023-01-02 thomas if (counter) {
201 ba63ab46 2023-01-02 thomas if (counter->nconnections > 0)
202 ba63ab46 2023-01-02 thomas counter->nconnections--;
203 ba63ab46 2023-01-02 thomas if (counter->nconnections == 0) {
204 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(counter);
205 ba63ab46 2023-01-02 thomas free(counter);
209 2b3d32a1 2022-12-30 thomas client_fd = client->fd;
210 2b3d32a1 2022-12-30 thomas free(client);
211 2b3d32a1 2022-12-30 thomas inflight--;
212 2b3d32a1 2022-12-30 thomas listen_client_cnt--;
213 2b3d32a1 2022-12-30 thomas if (close(client_fd) == -1)
214 2b3d32a1 2022-12-30 thomas return got_error_from_errno("close");
216 2b3d32a1 2022-12-30 thomas return NULL;
219 2b3d32a1 2022-12-30 thomas static int
220 2b3d32a1 2022-12-30 thomas accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
221 2b3d32a1 2022-12-30 thomas int reserve, volatile int *counter)
224 febe25b7 2023-08-29 thomas int sock_flags = SOCK_NONBLOCK;
226 febe25b7 2023-08-29 thomas #ifdef SOCK_CLOEXEC
227 febe25b7 2023-08-29 thomas sock_flags |= SOCK_CLOEXEC;
230 2b3d32a1 2022-12-30 thomas if (getdtablecount() + reserve +
231 2b3d32a1 2022-12-30 thomas ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
232 2b3d32a1 2022-12-30 thomas log_debug("inflight fds exceeded");
233 2b3d32a1 2022-12-30 thomas errno = EMFILE;
234 2b3d32a1 2022-12-30 thomas return -1;
236 febe25b7 2023-08-29 thomas #ifdef __APPLE__
237 febe25b7 2023-08-29 thomas /* TA: silence warning from GCC. */
238 febe25b7 2023-08-29 thomas (void)sock_flags;
239 febe25b7 2023-08-29 thomas ret = accept(fd, addr, addrlen);
241 febe25b7 2023-08-29 thomas ret = accept4(fd, addr, addrlen, sock_flags);
244 febe25b7 2023-08-29 thomas if (ret > -1) {
245 2b3d32a1 2022-12-30 thomas (*counter)++;
248 2b3d32a1 2022-12-30 thomas return ret;
251 2b3d32a1 2022-12-30 thomas static void
252 2b3d32a1 2022-12-30 thomas gotd_accept_paused(int fd, short event, void *arg)
254 2b3d32a1 2022-12-30 thomas event_add(&gotd_listen.iev.ev, NULL);
257 2b3d32a1 2022-12-30 thomas static void
258 2b3d32a1 2022-12-30 thomas gotd_accept(int fd, short event, void *arg)
260 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
261 2b3d32a1 2022-12-30 thomas struct sockaddr_storage ss;
262 2b3d32a1 2022-12-30 thomas struct timeval backoff;
263 2b3d32a1 2022-12-30 thomas socklen_t len;
264 2b3d32a1 2022-12-30 thomas int s = -1;
265 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
266 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter = NULL;
267 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconn;
268 0bcde4c8 2022-12-30 thomas uid_t euid;
269 0bcde4c8 2022-12-30 thomas gid_t egid;
271 2b3d32a1 2022-12-30 thomas backoff.tv_sec = 1;
272 2b3d32a1 2022-12-30 thomas backoff.tv_usec = 0;
274 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
275 2b3d32a1 2022-12-30 thomas log_warn("event_add");
278 2b3d32a1 2022-12-30 thomas if (event & EV_TIMEOUT)
281 2b3d32a1 2022-12-30 thomas len = sizeof(ss);
283 2b3d32a1 2022-12-30 thomas /* Other backoff conditions apart from EMFILE/ENFILE? */
284 2b3d32a1 2022-12-30 thomas s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
285 2b3d32a1 2022-12-30 thomas &inflight);
286 2b3d32a1 2022-12-30 thomas if (s == -1) {
287 2b3d32a1 2022-12-30 thomas switch (errno) {
288 2b3d32a1 2022-12-30 thomas case EINTR:
289 2b3d32a1 2022-12-30 thomas case EWOULDBLOCK:
290 2b3d32a1 2022-12-30 thomas case ECONNABORTED:
292 2b3d32a1 2022-12-30 thomas case EMFILE:
293 2b3d32a1 2022-12-30 thomas case ENFILE:
294 2b3d32a1 2022-12-30 thomas event_del(&gotd_listen.iev.ev);
295 2b3d32a1 2022-12-30 thomas evtimer_add(&gotd_listen.pause.ev, &backoff);
298 2b3d32a1 2022-12-30 thomas log_warn("accept");
303 2b3d32a1 2022-12-30 thomas if (listen_client_cnt >= GOTD_MAXCLIENTS)
304 0bcde4c8 2022-12-30 thomas goto err;
306 0bcde4c8 2022-12-30 thomas if (getpeereid(s, &euid, &egid) == -1) {
307 0bcde4c8 2022-12-30 thomas log_warn("getpeerid");
308 2b3d32a1 2022-12-30 thomas goto err;
311 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(euid);
312 ba63ab46 2023-01-02 thomas if (counter == NULL) {
313 ba63ab46 2023-01-02 thomas counter = calloc(1, sizeof(*counter));
314 ba63ab46 2023-01-02 thomas if (counter == NULL) {
315 ba63ab46 2023-01-02 thomas log_warn("%s: calloc", __func__);
316 ba63ab46 2023-01-02 thomas goto err;
318 ba63ab46 2023-01-02 thomas counter->euid = euid;
319 ba63ab46 2023-01-02 thomas counter->nconnections = 1;
320 ba63ab46 2023-01-02 thomas add_uid_connection_counter(counter);
322 0781db0e 2023-01-06 thomas int max_connections = GOTD_MAX_CONN_PER_UID;
323 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
325 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(
326 0781db0e 2023-01-06 thomas gotd_listen.connection_limits,
327 0781db0e 2023-01-06 thomas gotd_listen.nconnection_limits, euid);
328 0781db0e 2023-01-06 thomas if (limit)
329 0781db0e 2023-01-06 thomas max_connections = limit->max_connections;
331 0781db0e 2023-01-06 thomas if (counter->nconnections >= max_connections) {
332 ba63ab46 2023-01-02 thomas log_warnx("maximum connections exceeded for uid %d",
334 ba63ab46 2023-01-02 thomas goto err;
336 ba63ab46 2023-01-02 thomas counter->nconnections++;
339 2b3d32a1 2022-12-30 thomas client = calloc(1, sizeof(*client));
340 2b3d32a1 2022-12-30 thomas if (client == NULL) {
341 2b3d32a1 2022-12-30 thomas log_warn("%s: calloc", __func__);
342 2b3d32a1 2022-12-30 thomas goto err;
344 2b3d32a1 2022-12-30 thomas client->id = get_client_id();
345 2b3d32a1 2022-12-30 thomas client->fd = s;
346 ba63ab46 2023-01-02 thomas client->euid = euid;
348 2b3d32a1 2022-12-30 thomas add_client(client);
349 0bcde4c8 2022-12-30 thomas log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
350 0bcde4c8 2022-12-30 thomas client->fd, euid, egid);
352 2b3d32a1 2022-12-30 thomas memset(&iconn, 0, sizeof(iconn));
353 2b3d32a1 2022-12-30 thomas iconn.client_id = client->id;
354 0bcde4c8 2022-12-30 thomas iconn.euid = euid;
355 0bcde4c8 2022-12-30 thomas iconn.egid = egid;
356 2b3d32a1 2022-12-30 thomas s = dup(client->fd);
357 2b3d32a1 2022-12-30 thomas if (s == -1) {
358 2b3d32a1 2022-12-30 thomas log_warn("%s: dup", __func__);
359 2b3d32a1 2022-12-30 thomas goto err;
361 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
362 2b3d32a1 2022-12-30 thomas &iconn, sizeof(iconn)) == -1) {
363 2b3d32a1 2022-12-30 thomas log_warn("imsg compose CONNECT");
364 2b3d32a1 2022-12-30 thomas goto err;
369 2b3d32a1 2022-12-30 thomas inflight--;
370 2b3d32a1 2022-12-30 thomas if (client)
371 2b3d32a1 2022-12-30 thomas disconnect(client);
372 2b3d32a1 2022-12-30 thomas if (s != -1)
373 2b3d32a1 2022-12-30 thomas close(s);
376 2b3d32a1 2022-12-30 thomas static const struct got_error *
377 2b3d32a1 2022-12-30 thomas recv_disconnect(struct imsg *imsg)
379 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
380 2b3d32a1 2022-12-30 thomas size_t datalen;
381 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
383 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
384 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(idisconnect))
385 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
386 2b3d32a1 2022-12-30 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
388 2b3d32a1 2022-12-30 thomas log_debug("client disconnecting");
390 2b3d32a1 2022-12-30 thomas client = find_client(idisconnect.client_id);
391 2b3d32a1 2022-12-30 thomas if (client == NULL)
392 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_CLIENT_ID);
394 2b3d32a1 2022-12-30 thomas return disconnect(client);
397 2b3d32a1 2022-12-30 thomas static void
398 2b3d32a1 2022-12-30 thomas listen_dispatch(int fd, short event, void *arg)
400 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
401 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
402 2b3d32a1 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
403 2b3d32a1 2022-12-30 thomas struct imsg imsg;
404 2b3d32a1 2022-12-30 thomas ssize_t n;
405 2b3d32a1 2022-12-30 thomas int shut = 0;
407 2b3d32a1 2022-12-30 thomas if (event & EV_READ) {
408 2b3d32a1 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
409 2b3d32a1 2022-12-30 thomas fatal("imsg_read error");
410 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
411 2b3d32a1 2022-12-30 thomas shut = 1;
414 2b3d32a1 2022-12-30 thomas if (event & EV_WRITE) {
415 2b3d32a1 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
416 2b3d32a1 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
417 2b3d32a1 2022-12-30 thomas fatal("msgbuf_write");
418 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
419 2b3d32a1 2022-12-30 thomas shut = 1;
422 2b3d32a1 2022-12-30 thomas for (;;) {
423 2b3d32a1 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
424 2b3d32a1 2022-12-30 thomas fatal("%s: imsg_get", __func__);
425 2b3d32a1 2022-12-30 thomas if (n == 0) /* No more messages. */
428 2b3d32a1 2022-12-30 thomas switch (imsg.hdr.type) {
429 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_DISCONNECT:
430 2b3d32a1 2022-12-30 thomas err = recv_disconnect(&imsg);
432 5330ab76 2023-02-17 thomas log_warnx("disconnect: %s", err->msg);
435 5330ab76 2023-02-17 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
439 2b3d32a1 2022-12-30 thomas imsg_free(&imsg);
442 2b3d32a1 2022-12-30 thomas if (!shut) {
443 2b3d32a1 2022-12-30 thomas gotd_imsg_event_add(iev);
445 2b3d32a1 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
446 2b3d32a1 2022-12-30 thomas event_del(&iev->ev);
447 2b3d32a1 2022-12-30 thomas event_loopexit(NULL);
452 0781db0e 2023-01-06 thomas listen_main(const char *title, int gotd_socket,
453 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *connection_limits,
454 0781db0e 2023-01-06 thomas size_t nconnection_limits)
456 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
457 2b3d32a1 2022-12-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
459 56409302 2023-01-02 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
460 ba63ab46 2023-01-02 thomas arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
462 2b3d32a1 2022-12-30 thomas gotd_listen.title = title;
463 2b3d32a1 2022-12-30 thomas gotd_listen.pid = getpid();
464 2b3d32a1 2022-12-30 thomas gotd_listen.fd = gotd_socket;
465 0781db0e 2023-01-06 thomas gotd_listen.connection_limits = connection_limits;
466 0781db0e 2023-01-06 thomas gotd_listen.nconnection_limits = nconnection_limits;
468 2b3d32a1 2022-12-30 thomas signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
469 2b3d32a1 2022-12-30 thomas signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
470 2b3d32a1 2022-12-30 thomas signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
471 2b3d32a1 2022-12-30 thomas signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
472 2b3d32a1 2022-12-30 thomas signal(SIGPIPE, SIG_IGN);
474 2b3d32a1 2022-12-30 thomas signal_add(&evsigint, NULL);
475 2b3d32a1 2022-12-30 thomas signal_add(&evsigterm, NULL);
476 2b3d32a1 2022-12-30 thomas signal_add(&evsighup, NULL);
477 2b3d32a1 2022-12-30 thomas signal_add(&evsigusr1, NULL);
479 2b3d32a1 2022-12-30 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
480 2b3d32a1 2022-12-30 thomas iev.handler = listen_dispatch;
481 2b3d32a1 2022-12-30 thomas iev.events = EV_READ;
482 2b3d32a1 2022-12-30 thomas iev.handler_arg = NULL;
483 2b3d32a1 2022-12-30 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
484 2b3d32a1 2022-12-30 thomas if (event_add(&iev.ev, NULL) == -1)
485 2b3d32a1 2022-12-30 thomas fatalx("event add");
487 2b3d32a1 2022-12-30 thomas event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
488 2b3d32a1 2022-12-30 thomas gotd_accept, &iev);
489 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL))
490 2b3d32a1 2022-12-30 thomas fatalx("event add");
491 2b3d32a1 2022-12-30 thomas evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
493 2b3d32a1 2022-12-30 thomas event_dispatch();
495 2b3d32a1 2022-12-30 thomas listen_shutdown();
498 2b3d32a1 2022-12-30 thomas static void
499 2b3d32a1 2022-12-30 thomas listen_shutdown(void)
501 5330ab76 2023-02-17 thomas log_debug("shutting down");
503 0781db0e 2023-01-06 thomas free(gotd_listen.connection_limits);
504 2b3d32a1 2022-12-30 thomas if (gotd_listen.fd != -1)
505 2b3d32a1 2022-12-30 thomas close(gotd_listen.fd);