2 d93ecf7d 2022-12-14 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 d93ecf7d 2022-12-14 stsp * Permission to use, copy, modify, and distribute this software for any
5 d93ecf7d 2022-12-14 stsp * purpose with or without fee is hereby granted, provided that the above
6 d93ecf7d 2022-12-14 stsp * copyright notice and this permission notice appear in all copies.
8 d93ecf7d 2022-12-14 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d93ecf7d 2022-12-14 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d93ecf7d 2022-12-14 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d93ecf7d 2022-12-14 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d93ecf7d 2022-12-14 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d93ecf7d 2022-12-14 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d93ecf7d 2022-12-14 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d93ecf7d 2022-12-14 stsp #include <sys/types.h>
18 d93ecf7d 2022-12-14 stsp #include <sys/queue.h>
19 d93ecf7d 2022-12-14 stsp #include <sys/socket.h>
20 d93ecf7d 2022-12-14 stsp #include <sys/uio.h>
22 d93ecf7d 2022-12-14 stsp #include <errno.h>
23 d93ecf7d 2022-12-14 stsp #include <event.h>
24 d93ecf7d 2022-12-14 stsp #include <siphash.h>
25 d93ecf7d 2022-12-14 stsp #include <stdint.h>
26 d93ecf7d 2022-12-14 stsp #include <stdio.h>
27 d93ecf7d 2022-12-14 stsp #include <stdlib.h>
28 d93ecf7d 2022-12-14 stsp #include <string.h>
29 d93ecf7d 2022-12-14 stsp #include <imsg.h>
30 d93ecf7d 2022-12-14 stsp #include <limits.h>
31 d93ecf7d 2022-12-14 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 d93ecf7d 2022-12-14 stsp #include <signal.h>
34 d93ecf7d 2022-12-14 stsp #include <unistd.h>
36 d93ecf7d 2022-12-14 stsp #include "got_error.h"
37 1b1a386d 2024-07-09 op #include "got_object.h"
38 9afa3de2 2023-04-04 stsp #include "got_path.h"
40 d93ecf7d 2022-12-14 stsp #include "gotd.h"
41 d93ecf7d 2022-12-14 stsp #include "log.h"
42 d93ecf7d 2022-12-14 stsp #include "listen.h"
44 d93ecf7d 2022-12-14 stsp #ifndef nitems
45 d93ecf7d 2022-12-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 d93ecf7d 2022-12-14 stsp struct gotd_listen_client {
49 d93ecf7d 2022-12-14 stsp STAILQ_ENTRY(gotd_listen_client) entry;
50 d93ecf7d 2022-12-14 stsp uint32_t id;
54 d93ecf7d 2022-12-14 stsp STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
56 d93ecf7d 2022-12-14 stsp static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
57 d93ecf7d 2022-12-14 stsp static SIPHASH_KEY clients_hash_key;
58 d93ecf7d 2022-12-14 stsp static volatile int listen_client_cnt;
59 d93ecf7d 2022-12-14 stsp static int inflight;
61 7a0564e3 2022-12-30 stsp struct gotd_uid_connection_counter {
62 7a0564e3 2022-12-30 stsp STAILQ_ENTRY(gotd_uid_connection_counter) entry;
64 7a0564e3 2022-12-30 stsp int nconnections;
66 7a0564e3 2022-12-30 stsp STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
67 7a0564e3 2022-12-30 stsp static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
68 7a0564e3 2022-12-30 stsp static SIPHASH_KEY uid_hash_key;
70 d93ecf7d 2022-12-14 stsp static struct {
72 d93ecf7d 2022-12-14 stsp const char *title;
74 d93ecf7d 2022-12-14 stsp struct gotd_imsgev iev;
75 d93ecf7d 2022-12-14 stsp struct gotd_imsgev pause;
76 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *connection_limits;
77 40b85cca 2023-01-03 stsp size_t nconnection_limits;
78 d93ecf7d 2022-12-14 stsp } gotd_listen;
80 d93ecf7d 2022-12-14 stsp static int inflight;
82 d93ecf7d 2022-12-14 stsp static void listen_shutdown(void);
85 d93ecf7d 2022-12-14 stsp listen_sighdlr(int sig, short event, void *arg)
88 d93ecf7d 2022-12-14 stsp * Normal signal handler rules don't apply because libevent
89 d93ecf7d 2022-12-14 stsp * decouples for us.
92 d93ecf7d 2022-12-14 stsp switch (sig) {
93 d93ecf7d 2022-12-14 stsp case SIGHUP:
95 d93ecf7d 2022-12-14 stsp case SIGUSR1:
97 d93ecf7d 2022-12-14 stsp case SIGTERM:
98 d93ecf7d 2022-12-14 stsp case SIGINT:
99 d93ecf7d 2022-12-14 stsp listen_shutdown();
100 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
103 d93ecf7d 2022-12-14 stsp fatalx("unexpected signal");
107 d93ecf7d 2022-12-14 stsp static uint64_t
108 d93ecf7d 2022-12-14 stsp client_hash(uint32_t client_id)
110 d93ecf7d 2022-12-14 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
113 d93ecf7d 2022-12-14 stsp static void
114 d93ecf7d 2022-12-14 stsp add_client(struct gotd_listen_client *client)
116 d93ecf7d 2022-12-14 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
117 d93ecf7d 2022-12-14 stsp STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
118 d93ecf7d 2022-12-14 stsp listen_client_cnt++;
121 d93ecf7d 2022-12-14 stsp static struct gotd_listen_client *
122 d93ecf7d 2022-12-14 stsp find_client(uint32_t client_id)
124 d93ecf7d 2022-12-14 stsp uint64_t slot;
125 d93ecf7d 2022-12-14 stsp struct gotd_listen_client *c;
127 d93ecf7d 2022-12-14 stsp slot = client_hash(client_id) % nitems(gotd_listen_clients);
128 d93ecf7d 2022-12-14 stsp STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
129 d93ecf7d 2022-12-14 stsp if (c->id == client_id)
133 d93ecf7d 2022-12-14 stsp return NULL;
136 d93ecf7d 2022-12-14 stsp static uint32_t
137 d93ecf7d 2022-12-14 stsp get_client_id(void)
139 d93ecf7d 2022-12-14 stsp int duplicate = 0;
140 d93ecf7d 2022-12-14 stsp uint32_t id;
143 d93ecf7d 2022-12-14 stsp id = arc4random();
144 d93ecf7d 2022-12-14 stsp duplicate = (find_client(id) != NULL);
145 d93ecf7d 2022-12-14 stsp } while (duplicate || id == 0);
150 7a0564e3 2022-12-30 stsp static uint64_t
151 7a0564e3 2022-12-30 stsp uid_hash(uid_t euid)
153 7a0564e3 2022-12-30 stsp return SipHash24(&uid_hash_key, &euid, sizeof(euid));
156 7a0564e3 2022-12-30 stsp static void
157 7a0564e3 2022-12-30 stsp add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
159 7a0564e3 2022-12-30 stsp uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
160 7a0564e3 2022-12-30 stsp STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
163 7a0564e3 2022-12-30 stsp static void
164 7a0564e3 2022-12-30 stsp remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
166 7a0564e3 2022-12-30 stsp uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
167 7a0564e3 2022-12-30 stsp STAILQ_REMOVE(&gotd_client_uids[slot], counter,
168 7a0564e3 2022-12-30 stsp gotd_uid_connection_counter, entry);
171 7a0564e3 2022-12-30 stsp static struct gotd_uid_connection_counter *
172 7a0564e3 2022-12-30 stsp find_uid_connection_counter(uid_t euid)
174 7a0564e3 2022-12-30 stsp uint64_t slot;
175 7a0564e3 2022-12-30 stsp struct gotd_uid_connection_counter *c;
177 7a0564e3 2022-12-30 stsp slot = uid_hash(euid) % nitems(gotd_client_uids);
178 7a0564e3 2022-12-30 stsp STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
179 7a0564e3 2022-12-30 stsp if (c->euid == euid)
183 7a0564e3 2022-12-30 stsp return NULL;
186 d93ecf7d 2022-12-14 stsp static const struct got_error *
187 d93ecf7d 2022-12-14 stsp disconnect(struct gotd_listen_client *client)
189 7a0564e3 2022-12-30 stsp struct gotd_uid_connection_counter *counter;
190 d93ecf7d 2022-12-14 stsp uint64_t slot;
191 d93ecf7d 2022-12-14 stsp int client_fd;
193 d93ecf7d 2022-12-14 stsp log_debug("client on fd %d disconnecting", client->fd);
195 d93ecf7d 2022-12-14 stsp slot = client_hash(client->id) % nitems(gotd_listen_clients);
196 d93ecf7d 2022-12-14 stsp STAILQ_REMOVE(&gotd_listen_clients[slot], client,
197 d93ecf7d 2022-12-14 stsp gotd_listen_client, entry);
199 7a0564e3 2022-12-30 stsp counter = find_uid_connection_counter(client->euid);
200 7a0564e3 2022-12-30 stsp if (counter) {
201 7a0564e3 2022-12-30 stsp if (counter->nconnections > 0)
202 7a0564e3 2022-12-30 stsp counter->nconnections--;
203 7a0564e3 2022-12-30 stsp if (counter->nconnections == 0) {
204 7a0564e3 2022-12-30 stsp remove_uid_connection_counter(counter);
205 7a0564e3 2022-12-30 stsp free(counter);
209 d93ecf7d 2022-12-14 stsp client_fd = client->fd;
210 d93ecf7d 2022-12-14 stsp free(client);
211 d93ecf7d 2022-12-14 stsp inflight--;
212 d93ecf7d 2022-12-14 stsp listen_client_cnt--;
213 d93ecf7d 2022-12-14 stsp if (close(client_fd) == -1)
214 d93ecf7d 2022-12-14 stsp return got_error_from_errno("close");
216 d93ecf7d 2022-12-14 stsp return NULL;
220 d93ecf7d 2022-12-14 stsp accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
221 d93ecf7d 2022-12-14 stsp int reserve, volatile int *counter)
225 d93ecf7d 2022-12-14 stsp if (getdtablecount() + reserve +
226 d93ecf7d 2022-12-14 stsp ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
227 d93ecf7d 2022-12-14 stsp log_debug("inflight fds exceeded");
228 d93ecf7d 2022-12-14 stsp errno = EMFILE;
232 d93ecf7d 2022-12-14 stsp if ((ret = accept4(fd, addr, addrlen,
233 d93ecf7d 2022-12-14 stsp SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
234 d93ecf7d 2022-12-14 stsp (*counter)++;
237 d93ecf7d 2022-12-14 stsp return ret;
240 d93ecf7d 2022-12-14 stsp static void
241 d93ecf7d 2022-12-14 stsp gotd_accept_paused(int fd, short event, void *arg)
243 d93ecf7d 2022-12-14 stsp event_add(&gotd_listen.iev.ev, NULL);
246 d93ecf7d 2022-12-14 stsp static void
247 d93ecf7d 2022-12-14 stsp gotd_accept(int fd, short event, void *arg)
249 d93ecf7d 2022-12-14 stsp struct gotd_imsgev *iev = arg;
250 d93ecf7d 2022-12-14 stsp struct sockaddr_storage ss;
251 d93ecf7d 2022-12-14 stsp struct timeval backoff;
252 d93ecf7d 2022-12-14 stsp socklen_t len;
253 d93ecf7d 2022-12-14 stsp int s = -1;
254 d93ecf7d 2022-12-14 stsp struct gotd_listen_client *client = NULL;
255 7a0564e3 2022-12-30 stsp struct gotd_uid_connection_counter *counter = NULL;
256 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconn;
257 365cf0f3 2022-12-29 stsp uid_t euid;
258 365cf0f3 2022-12-29 stsp gid_t egid;
260 d93ecf7d 2022-12-14 stsp backoff.tv_sec = 1;
261 d93ecf7d 2022-12-14 stsp backoff.tv_usec = 0;
263 d93ecf7d 2022-12-14 stsp if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
264 d93ecf7d 2022-12-14 stsp log_warn("event_add");
267 d93ecf7d 2022-12-14 stsp if (event & EV_TIMEOUT)
270 d93ecf7d 2022-12-14 stsp len = sizeof(ss);
272 d93ecf7d 2022-12-14 stsp /* Other backoff conditions apart from EMFILE/ENFILE? */
273 d93ecf7d 2022-12-14 stsp s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
274 d93ecf7d 2022-12-14 stsp &inflight);
275 d93ecf7d 2022-12-14 stsp if (s == -1) {
276 d93ecf7d 2022-12-14 stsp switch (errno) {
277 d93ecf7d 2022-12-14 stsp case EINTR:
278 d93ecf7d 2022-12-14 stsp case EWOULDBLOCK:
279 d93ecf7d 2022-12-14 stsp case ECONNABORTED:
281 d93ecf7d 2022-12-14 stsp case EMFILE:
282 d93ecf7d 2022-12-14 stsp case ENFILE:
283 d93ecf7d 2022-12-14 stsp event_del(&gotd_listen.iev.ev);
284 d93ecf7d 2022-12-14 stsp evtimer_add(&gotd_listen.pause.ev, &backoff);
287 d93ecf7d 2022-12-14 stsp log_warn("accept");
292 d93ecf7d 2022-12-14 stsp if (listen_client_cnt >= GOTD_MAXCLIENTS)
295 365cf0f3 2022-12-29 stsp if (getpeereid(s, &euid, &egid) == -1) {
296 365cf0f3 2022-12-29 stsp log_warn("getpeerid");
300 7a0564e3 2022-12-30 stsp counter = find_uid_connection_counter(euid);
301 7a0564e3 2022-12-30 stsp if (counter == NULL) {
302 7a0564e3 2022-12-30 stsp counter = calloc(1, sizeof(*counter));
303 7a0564e3 2022-12-30 stsp if (counter == NULL) {
304 7a0564e3 2022-12-30 stsp log_warn("%s: calloc", __func__);
307 7a0564e3 2022-12-30 stsp counter->euid = euid;
308 7a0564e3 2022-12-30 stsp counter->nconnections = 1;
309 7a0564e3 2022-12-30 stsp add_uid_connection_counter(counter);
311 40b85cca 2023-01-03 stsp int max_connections = GOTD_MAX_CONN_PER_UID;
312 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
314 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(
315 40b85cca 2023-01-03 stsp gotd_listen.connection_limits,
316 40b85cca 2023-01-03 stsp gotd_listen.nconnection_limits, euid);
318 40b85cca 2023-01-03 stsp max_connections = limit->max_connections;
320 40b85cca 2023-01-03 stsp if (counter->nconnections >= max_connections) {
321 7a0564e3 2022-12-30 stsp log_warnx("maximum connections exceeded for uid %d",
325 7a0564e3 2022-12-30 stsp counter->nconnections++;
328 d93ecf7d 2022-12-14 stsp client = calloc(1, sizeof(*client));
329 d93ecf7d 2022-12-14 stsp if (client == NULL) {
330 d93ecf7d 2022-12-14 stsp log_warn("%s: calloc", __func__);
333 d93ecf7d 2022-12-14 stsp client->id = get_client_id();
334 d93ecf7d 2022-12-14 stsp client->fd = s;
335 7a0564e3 2022-12-30 stsp client->euid = euid;
337 d93ecf7d 2022-12-14 stsp add_client(client);
338 365cf0f3 2022-12-29 stsp log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
339 365cf0f3 2022-12-29 stsp client->fd, euid, egid);
341 d93ecf7d 2022-12-14 stsp memset(&iconn, 0, sizeof(iconn));
342 d93ecf7d 2022-12-14 stsp iconn.client_id = client->id;
343 365cf0f3 2022-12-29 stsp iconn.euid = euid;
344 365cf0f3 2022-12-29 stsp iconn.egid = egid;
345 d93ecf7d 2022-12-14 stsp s = dup(client->fd);
346 d93ecf7d 2022-12-14 stsp if (s == -1) {
347 d93ecf7d 2022-12-14 stsp log_warn("%s: dup", __func__);
350 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
351 d93ecf7d 2022-12-14 stsp &iconn, sizeof(iconn)) == -1) {
352 d93ecf7d 2022-12-14 stsp log_warn("imsg compose CONNECT");
358 d93ecf7d 2022-12-14 stsp inflight--;
359 d93ecf7d 2022-12-14 stsp if (client)
360 d93ecf7d 2022-12-14 stsp disconnect(client);
361 d93ecf7d 2022-12-14 stsp if (s != -1)
365 d93ecf7d 2022-12-14 stsp static const struct got_error *
366 d93ecf7d 2022-12-14 stsp recv_disconnect(struct imsg *imsg)
368 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
369 d93ecf7d 2022-12-14 stsp size_t datalen;
370 d93ecf7d 2022-12-14 stsp struct gotd_listen_client *client = NULL;
372 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
373 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(idisconnect))
374 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
375 d93ecf7d 2022-12-14 stsp memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
377 d93ecf7d 2022-12-14 stsp log_debug("client disconnecting");
379 d93ecf7d 2022-12-14 stsp client = find_client(idisconnect.client_id);
380 d93ecf7d 2022-12-14 stsp if (client == NULL)
381 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_CLIENT_ID);
383 d93ecf7d 2022-12-14 stsp return disconnect(client);
386 d93ecf7d 2022-12-14 stsp static void
387 d93ecf7d 2022-12-14 stsp listen_dispatch(int fd, short event, void *arg)
389 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
390 d93ecf7d 2022-12-14 stsp struct gotd_imsgev *iev = arg;
391 d93ecf7d 2022-12-14 stsp struct imsgbuf *ibuf = &iev->ibuf;
392 d93ecf7d 2022-12-14 stsp struct imsg imsg;
394 d93ecf7d 2022-12-14 stsp int shut = 0;
396 d93ecf7d 2022-12-14 stsp if (event & EV_READ) {
397 d93ecf7d 2022-12-14 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
398 d93ecf7d 2022-12-14 stsp fatal("imsg_read error");
399 d93ecf7d 2022-12-14 stsp if (n == 0) /* Connection closed. */
403 d93ecf7d 2022-12-14 stsp if (event & EV_WRITE) {
404 d93ecf7d 2022-12-14 stsp n = msgbuf_write(&ibuf->w);
405 d93ecf7d 2022-12-14 stsp if (n == -1 && errno != EAGAIN)
406 d93ecf7d 2022-12-14 stsp fatal("msgbuf_write");
407 d93ecf7d 2022-12-14 stsp if (n == 0) /* Connection closed. */
412 d93ecf7d 2022-12-14 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
413 d93ecf7d 2022-12-14 stsp fatal("%s: imsg_get", __func__);
414 d93ecf7d 2022-12-14 stsp if (n == 0) /* No more messages. */
417 d93ecf7d 2022-12-14 stsp switch (imsg.hdr.type) {
418 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_DISCONNECT:
419 d93ecf7d 2022-12-14 stsp err = recv_disconnect(&imsg);
421 2ec74a9e 2023-02-08 op log_warnx("disconnect: %s", err->msg);
424 2ec74a9e 2023-02-08 op log_debug("unexpected imsg %d", imsg.hdr.type);
428 d93ecf7d 2022-12-14 stsp imsg_free(&imsg);
431 d93ecf7d 2022-12-14 stsp if (!shut) {
432 d93ecf7d 2022-12-14 stsp gotd_imsg_event_add(iev);
434 d93ecf7d 2022-12-14 stsp /* This pipe is dead. Remove its event handler */
435 d93ecf7d 2022-12-14 stsp event_del(&iev->ev);
436 d93ecf7d 2022-12-14 stsp event_loopexit(NULL);
441 40b85cca 2023-01-03 stsp listen_main(const char *title, int gotd_socket,
442 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *connection_limits,
443 40b85cca 2023-01-03 stsp size_t nconnection_limits)
445 d93ecf7d 2022-12-14 stsp struct gotd_imsgev iev;
446 d93ecf7d 2022-12-14 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
448 c602198a 2022-12-30 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
449 7a0564e3 2022-12-30 stsp arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
451 d93ecf7d 2022-12-14 stsp gotd_listen.title = title;
452 d93ecf7d 2022-12-14 stsp gotd_listen.pid = getpid();
453 d93ecf7d 2022-12-14 stsp gotd_listen.fd = gotd_socket;
454 40b85cca 2023-01-03 stsp gotd_listen.connection_limits = connection_limits;
455 40b85cca 2023-01-03 stsp gotd_listen.nconnection_limits = nconnection_limits;
457 d93ecf7d 2022-12-14 stsp signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
458 d93ecf7d 2022-12-14 stsp signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
459 d93ecf7d 2022-12-14 stsp signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
460 d93ecf7d 2022-12-14 stsp signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
461 d93ecf7d 2022-12-14 stsp signal(SIGPIPE, SIG_IGN);
463 d93ecf7d 2022-12-14 stsp signal_add(&evsigint, NULL);
464 d93ecf7d 2022-12-14 stsp signal_add(&evsigterm, NULL);
465 d93ecf7d 2022-12-14 stsp signal_add(&evsighup, NULL);
466 d93ecf7d 2022-12-14 stsp signal_add(&evsigusr1, NULL);
468 d93ecf7d 2022-12-14 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
469 d93ecf7d 2022-12-14 stsp iev.handler = listen_dispatch;
470 d93ecf7d 2022-12-14 stsp iev.events = EV_READ;
471 d93ecf7d 2022-12-14 stsp iev.handler_arg = NULL;
472 d93ecf7d 2022-12-14 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
473 d93ecf7d 2022-12-14 stsp if (event_add(&iev.ev, NULL) == -1)
474 d93ecf7d 2022-12-14 stsp fatalx("event add");
476 d93ecf7d 2022-12-14 stsp event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
477 d93ecf7d 2022-12-14 stsp gotd_accept, &iev);
478 d93ecf7d 2022-12-14 stsp if (event_add(&gotd_listen.iev.ev, NULL))
479 d93ecf7d 2022-12-14 stsp fatalx("event add");
480 d93ecf7d 2022-12-14 stsp evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
482 d93ecf7d 2022-12-14 stsp event_dispatch();
484 d93ecf7d 2022-12-14 stsp listen_shutdown();
487 d93ecf7d 2022-12-14 stsp static void
488 d93ecf7d 2022-12-14 stsp listen_shutdown(void)
490 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_listen.title);
492 40b85cca 2023-01-03 stsp free(gotd_listen.connection_limits);
493 d93ecf7d 2022-12-14 stsp if (gotd_listen.fd != -1)
494 d93ecf7d 2022-12-14 stsp close(gotd_listen.fd);