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 2b3d32a1 2022-12-30 thomas #include <sys/types.h>
18 2b3d32a1 2022-12-30 thomas #include <sys/queue.h>
19 2b3d32a1 2022-12-30 thomas #include <sys/socket.h>
20 2b3d32a1 2022-12-30 thomas #include <sys/uio.h>
22 2b3d32a1 2022-12-30 thomas #include <errno.h>
23 2b3d32a1 2022-12-30 thomas #include <event.h>
24 2b3d32a1 2022-12-30 thomas #include <siphash.h>
25 2b3d32a1 2022-12-30 thomas #include <stdint.h>
26 2b3d32a1 2022-12-30 thomas #include <stdio.h>
27 2b3d32a1 2022-12-30 thomas #include <stdlib.h>
28 2b3d32a1 2022-12-30 thomas #include <string.h>
29 2b3d32a1 2022-12-30 thomas #include <imsg.h>
30 2b3d32a1 2022-12-30 thomas #include <limits.h>
31 2b3d32a1 2022-12-30 thomas #include <sha1.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"
37 2b3d32a1 2022-12-30 thomas #include "gotd.h"
38 2b3d32a1 2022-12-30 thomas #include "log.h"
39 2b3d32a1 2022-12-30 thomas #include "listen.h"
41 2b3d32a1 2022-12-30 thomas #ifndef nitems
42 2b3d32a1 2022-12-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 2b3d32a1 2022-12-30 thomas struct gotd_listen_client {
46 2b3d32a1 2022-12-30 thomas STAILQ_ENTRY(gotd_listen_client) entry;
47 2b3d32a1 2022-12-30 thomas uint32_t id;
49 ba63ab46 2023-01-02 thomas uid_t euid;
51 2b3d32a1 2022-12-30 thomas STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
53 2b3d32a1 2022-12-30 thomas static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
54 2b3d32a1 2022-12-30 thomas static SIPHASH_KEY clients_hash_key;
55 2b3d32a1 2022-12-30 thomas static volatile int listen_client_cnt;
56 2b3d32a1 2022-12-30 thomas static int inflight;
58 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter {
59 ba63ab46 2023-01-02 thomas STAILQ_ENTRY(gotd_uid_connection_counter) entry;
60 ba63ab46 2023-01-02 thomas uid_t euid;
61 ba63ab46 2023-01-02 thomas int nconnections;
63 ba63ab46 2023-01-02 thomas STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
64 ba63ab46 2023-01-02 thomas static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
65 ba63ab46 2023-01-02 thomas static SIPHASH_KEY uid_hash_key;
67 2b3d32a1 2022-12-30 thomas static struct {
68 2b3d32a1 2022-12-30 thomas pid_t pid;
69 2b3d32a1 2022-12-30 thomas const char *title;
71 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
72 2b3d32a1 2022-12-30 thomas struct gotd_imsgev pause;
73 2b3d32a1 2022-12-30 thomas } gotd_listen;
75 2b3d32a1 2022-12-30 thomas static int inflight;
77 2b3d32a1 2022-12-30 thomas static void listen_shutdown(void);
79 2b3d32a1 2022-12-30 thomas static void
80 2b3d32a1 2022-12-30 thomas listen_sighdlr(int sig, short event, void *arg)
83 2b3d32a1 2022-12-30 thomas * Normal signal handler rules don't apply because libevent
84 2b3d32a1 2022-12-30 thomas * decouples for us.
87 2b3d32a1 2022-12-30 thomas switch (sig) {
88 2b3d32a1 2022-12-30 thomas case SIGHUP:
90 2b3d32a1 2022-12-30 thomas case SIGUSR1:
92 2b3d32a1 2022-12-30 thomas case SIGTERM:
93 2b3d32a1 2022-12-30 thomas case SIGINT:
94 2b3d32a1 2022-12-30 thomas listen_shutdown();
95 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
98 2b3d32a1 2022-12-30 thomas fatalx("unexpected signal");
102 2b3d32a1 2022-12-30 thomas static uint64_t
103 2b3d32a1 2022-12-30 thomas client_hash(uint32_t client_id)
105 2b3d32a1 2022-12-30 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
108 2b3d32a1 2022-12-30 thomas static void
109 2b3d32a1 2022-12-30 thomas add_client(struct gotd_listen_client *client)
111 2b3d32a1 2022-12-30 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
112 2b3d32a1 2022-12-30 thomas STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
113 2b3d32a1 2022-12-30 thomas listen_client_cnt++;
116 2b3d32a1 2022-12-30 thomas static struct gotd_listen_client *
117 2b3d32a1 2022-12-30 thomas find_client(uint32_t client_id)
119 2b3d32a1 2022-12-30 thomas uint64_t slot;
120 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *c;
122 2b3d32a1 2022-12-30 thomas slot = client_hash(client_id) % nitems(gotd_listen_clients);
123 2b3d32a1 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
124 2b3d32a1 2022-12-30 thomas if (c->id == client_id)
125 2b3d32a1 2022-12-30 thomas return c;
128 2b3d32a1 2022-12-30 thomas return NULL;
131 2b3d32a1 2022-12-30 thomas static uint32_t
132 2b3d32a1 2022-12-30 thomas get_client_id(void)
134 2b3d32a1 2022-12-30 thomas int duplicate = 0;
135 2b3d32a1 2022-12-30 thomas uint32_t id;
138 2b3d32a1 2022-12-30 thomas id = arc4random();
139 2b3d32a1 2022-12-30 thomas duplicate = (find_client(id) != NULL);
140 2b3d32a1 2022-12-30 thomas } while (duplicate || id == 0);
142 2b3d32a1 2022-12-30 thomas return id;
145 ba63ab46 2023-01-02 thomas static uint64_t
146 ba63ab46 2023-01-02 thomas uid_hash(uid_t euid)
148 ba63ab46 2023-01-02 thomas return SipHash24(&uid_hash_key, &euid, sizeof(euid));
151 ba63ab46 2023-01-02 thomas static void
152 ba63ab46 2023-01-02 thomas add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
154 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
155 ba63ab46 2023-01-02 thomas STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
158 ba63ab46 2023-01-02 thomas static void
159 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
161 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
162 ba63ab46 2023-01-02 thomas STAILQ_REMOVE(&gotd_client_uids[slot], counter,
163 ba63ab46 2023-01-02 thomas gotd_uid_connection_counter, entry);
166 ba63ab46 2023-01-02 thomas static struct gotd_uid_connection_counter *
167 ba63ab46 2023-01-02 thomas find_uid_connection_counter(uid_t euid)
169 ba63ab46 2023-01-02 thomas uint64_t slot;
170 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *c;
172 ba63ab46 2023-01-02 thomas slot = uid_hash(euid) % nitems(gotd_client_uids);
173 ba63ab46 2023-01-02 thomas STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
174 ba63ab46 2023-01-02 thomas if (c->euid == euid)
175 ba63ab46 2023-01-02 thomas return c;
178 ba63ab46 2023-01-02 thomas return NULL;
181 2b3d32a1 2022-12-30 thomas static const struct got_error *
182 2b3d32a1 2022-12-30 thomas disconnect(struct gotd_listen_client *client)
184 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter;
185 2b3d32a1 2022-12-30 thomas uint64_t slot;
186 2b3d32a1 2022-12-30 thomas int client_fd;
188 2b3d32a1 2022-12-30 thomas log_debug("client on fd %d disconnecting", client->fd);
190 2b3d32a1 2022-12-30 thomas slot = client_hash(client->id) % nitems(gotd_listen_clients);
191 2b3d32a1 2022-12-30 thomas STAILQ_REMOVE(&gotd_listen_clients[slot], client,
192 2b3d32a1 2022-12-30 thomas gotd_listen_client, entry);
194 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(client->euid);
195 ba63ab46 2023-01-02 thomas if (counter) {
196 ba63ab46 2023-01-02 thomas if (counter->nconnections > 0)
197 ba63ab46 2023-01-02 thomas counter->nconnections--;
198 ba63ab46 2023-01-02 thomas if (counter->nconnections == 0) {
199 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(counter);
200 ba63ab46 2023-01-02 thomas free(counter);
204 2b3d32a1 2022-12-30 thomas client_fd = client->fd;
205 2b3d32a1 2022-12-30 thomas free(client);
206 2b3d32a1 2022-12-30 thomas inflight--;
207 2b3d32a1 2022-12-30 thomas listen_client_cnt--;
208 2b3d32a1 2022-12-30 thomas if (close(client_fd) == -1)
209 2b3d32a1 2022-12-30 thomas return got_error_from_errno("close");
211 2b3d32a1 2022-12-30 thomas return NULL;
214 2b3d32a1 2022-12-30 thomas static int
215 2b3d32a1 2022-12-30 thomas accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
216 2b3d32a1 2022-12-30 thomas int reserve, volatile int *counter)
220 2b3d32a1 2022-12-30 thomas if (getdtablecount() + reserve +
221 2b3d32a1 2022-12-30 thomas ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
222 2b3d32a1 2022-12-30 thomas log_debug("inflight fds exceeded");
223 2b3d32a1 2022-12-30 thomas errno = EMFILE;
224 2b3d32a1 2022-12-30 thomas return -1;
227 2b3d32a1 2022-12-30 thomas if ((ret = accept4(fd, addr, addrlen,
228 2b3d32a1 2022-12-30 thomas SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
229 2b3d32a1 2022-12-30 thomas (*counter)++;
232 2b3d32a1 2022-12-30 thomas return ret;
235 2b3d32a1 2022-12-30 thomas static void
236 2b3d32a1 2022-12-30 thomas gotd_accept_paused(int fd, short event, void *arg)
238 2b3d32a1 2022-12-30 thomas event_add(&gotd_listen.iev.ev, NULL);
241 2b3d32a1 2022-12-30 thomas static void
242 2b3d32a1 2022-12-30 thomas gotd_accept(int fd, short event, void *arg)
244 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
245 2b3d32a1 2022-12-30 thomas struct sockaddr_storage ss;
246 2b3d32a1 2022-12-30 thomas struct timeval backoff;
247 2b3d32a1 2022-12-30 thomas socklen_t len;
248 2b3d32a1 2022-12-30 thomas int s = -1;
249 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
250 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter = NULL;
251 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconn;
252 0bcde4c8 2022-12-30 thomas uid_t euid;
253 0bcde4c8 2022-12-30 thomas gid_t egid;
255 2b3d32a1 2022-12-30 thomas backoff.tv_sec = 1;
256 2b3d32a1 2022-12-30 thomas backoff.tv_usec = 0;
258 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
259 2b3d32a1 2022-12-30 thomas log_warn("event_add");
262 2b3d32a1 2022-12-30 thomas if (event & EV_TIMEOUT)
265 2b3d32a1 2022-12-30 thomas len = sizeof(ss);
267 2b3d32a1 2022-12-30 thomas /* Other backoff conditions apart from EMFILE/ENFILE? */
268 2b3d32a1 2022-12-30 thomas s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
269 2b3d32a1 2022-12-30 thomas &inflight);
270 2b3d32a1 2022-12-30 thomas if (s == -1) {
271 2b3d32a1 2022-12-30 thomas switch (errno) {
272 2b3d32a1 2022-12-30 thomas case EINTR:
273 2b3d32a1 2022-12-30 thomas case EWOULDBLOCK:
274 2b3d32a1 2022-12-30 thomas case ECONNABORTED:
276 2b3d32a1 2022-12-30 thomas case EMFILE:
277 2b3d32a1 2022-12-30 thomas case ENFILE:
278 2b3d32a1 2022-12-30 thomas event_del(&gotd_listen.iev.ev);
279 2b3d32a1 2022-12-30 thomas evtimer_add(&gotd_listen.pause.ev, &backoff);
282 2b3d32a1 2022-12-30 thomas log_warn("accept");
287 2b3d32a1 2022-12-30 thomas if (listen_client_cnt >= GOTD_MAXCLIENTS)
288 0bcde4c8 2022-12-30 thomas goto err;
290 0bcde4c8 2022-12-30 thomas if (getpeereid(s, &euid, &egid) == -1) {
291 0bcde4c8 2022-12-30 thomas log_warn("getpeerid");
292 2b3d32a1 2022-12-30 thomas goto err;
295 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(euid);
296 ba63ab46 2023-01-02 thomas if (counter == NULL) {
297 ba63ab46 2023-01-02 thomas counter = calloc(1, sizeof(*counter));
298 ba63ab46 2023-01-02 thomas if (counter == NULL) {
299 ba63ab46 2023-01-02 thomas log_warn("%s: calloc", __func__);
300 ba63ab46 2023-01-02 thomas goto err;
302 ba63ab46 2023-01-02 thomas counter->euid = euid;
303 ba63ab46 2023-01-02 thomas counter->nconnections = 1;
304 ba63ab46 2023-01-02 thomas add_uid_connection_counter(counter);
306 ba63ab46 2023-01-02 thomas if (counter->nconnections >= GOTD_MAX_CONN_PER_UID) {
307 ba63ab46 2023-01-02 thomas log_warnx("maximum connections exceeded for uid %d",
309 ba63ab46 2023-01-02 thomas goto err;
311 ba63ab46 2023-01-02 thomas counter->nconnections++;
314 2b3d32a1 2022-12-30 thomas client = calloc(1, sizeof(*client));
315 2b3d32a1 2022-12-30 thomas if (client == NULL) {
316 2b3d32a1 2022-12-30 thomas log_warn("%s: calloc", __func__);
317 2b3d32a1 2022-12-30 thomas goto err;
319 2b3d32a1 2022-12-30 thomas client->id = get_client_id();
320 2b3d32a1 2022-12-30 thomas client->fd = s;
321 ba63ab46 2023-01-02 thomas client->euid = euid;
323 2b3d32a1 2022-12-30 thomas add_client(client);
324 0bcde4c8 2022-12-30 thomas log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
325 0bcde4c8 2022-12-30 thomas client->fd, euid, egid);
327 2b3d32a1 2022-12-30 thomas memset(&iconn, 0, sizeof(iconn));
328 2b3d32a1 2022-12-30 thomas iconn.client_id = client->id;
329 0bcde4c8 2022-12-30 thomas iconn.euid = euid;
330 0bcde4c8 2022-12-30 thomas iconn.egid = egid;
331 2b3d32a1 2022-12-30 thomas s = dup(client->fd);
332 2b3d32a1 2022-12-30 thomas if (s == -1) {
333 2b3d32a1 2022-12-30 thomas log_warn("%s: dup", __func__);
334 2b3d32a1 2022-12-30 thomas goto err;
336 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
337 2b3d32a1 2022-12-30 thomas &iconn, sizeof(iconn)) == -1) {
338 2b3d32a1 2022-12-30 thomas log_warn("imsg compose CONNECT");
339 2b3d32a1 2022-12-30 thomas goto err;
344 2b3d32a1 2022-12-30 thomas inflight--;
345 2b3d32a1 2022-12-30 thomas if (client)
346 2b3d32a1 2022-12-30 thomas disconnect(client);
347 2b3d32a1 2022-12-30 thomas if (s != -1)
348 2b3d32a1 2022-12-30 thomas close(s);
351 2b3d32a1 2022-12-30 thomas static const struct got_error *
352 2b3d32a1 2022-12-30 thomas recv_disconnect(struct imsg *imsg)
354 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
355 2b3d32a1 2022-12-30 thomas size_t datalen;
356 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
358 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
359 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(idisconnect))
360 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
361 2b3d32a1 2022-12-30 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
363 2b3d32a1 2022-12-30 thomas log_debug("client disconnecting");
365 2b3d32a1 2022-12-30 thomas client = find_client(idisconnect.client_id);
366 2b3d32a1 2022-12-30 thomas if (client == NULL)
367 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_CLIENT_ID);
369 2b3d32a1 2022-12-30 thomas return disconnect(client);
372 2b3d32a1 2022-12-30 thomas static void
373 2b3d32a1 2022-12-30 thomas listen_dispatch(int fd, short event, void *arg)
375 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
376 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
377 2b3d32a1 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
378 2b3d32a1 2022-12-30 thomas struct imsg imsg;
379 2b3d32a1 2022-12-30 thomas ssize_t n;
380 2b3d32a1 2022-12-30 thomas int shut = 0;
382 2b3d32a1 2022-12-30 thomas if (event & EV_READ) {
383 2b3d32a1 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
384 2b3d32a1 2022-12-30 thomas fatal("imsg_read error");
385 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
386 2b3d32a1 2022-12-30 thomas shut = 1;
389 2b3d32a1 2022-12-30 thomas if (event & EV_WRITE) {
390 2b3d32a1 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
391 2b3d32a1 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
392 2b3d32a1 2022-12-30 thomas fatal("msgbuf_write");
393 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
394 2b3d32a1 2022-12-30 thomas shut = 1;
397 2b3d32a1 2022-12-30 thomas for (;;) {
398 2b3d32a1 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
399 2b3d32a1 2022-12-30 thomas fatal("%s: imsg_get", __func__);
400 2b3d32a1 2022-12-30 thomas if (n == 0) /* No more messages. */
403 2b3d32a1 2022-12-30 thomas switch (imsg.hdr.type) {
404 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_DISCONNECT:
405 2b3d32a1 2022-12-30 thomas err = recv_disconnect(&imsg);
407 2b3d32a1 2022-12-30 thomas log_warnx("%s: disconnect: %s",
408 2b3d32a1 2022-12-30 thomas gotd_listen.title, err->msg);
411 2b3d32a1 2022-12-30 thomas log_debug("%s: unexpected imsg %d", gotd_listen.title,
412 2b3d32a1 2022-12-30 thomas imsg.hdr.type);
416 2b3d32a1 2022-12-30 thomas imsg_free(&imsg);
419 2b3d32a1 2022-12-30 thomas if (!shut) {
420 2b3d32a1 2022-12-30 thomas gotd_imsg_event_add(iev);
422 2b3d32a1 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
423 2b3d32a1 2022-12-30 thomas event_del(&iev->ev);
424 2b3d32a1 2022-12-30 thomas event_loopexit(NULL);
429 2b3d32a1 2022-12-30 thomas listen_main(const char *title, int gotd_socket)
431 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
432 2b3d32a1 2022-12-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
434 56409302 2023-01-02 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
435 ba63ab46 2023-01-02 thomas arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
437 2b3d32a1 2022-12-30 thomas gotd_listen.title = title;
438 2b3d32a1 2022-12-30 thomas gotd_listen.pid = getpid();
439 2b3d32a1 2022-12-30 thomas gotd_listen.fd = gotd_socket;
441 2b3d32a1 2022-12-30 thomas signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
442 2b3d32a1 2022-12-30 thomas signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
443 2b3d32a1 2022-12-30 thomas signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
444 2b3d32a1 2022-12-30 thomas signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
445 2b3d32a1 2022-12-30 thomas signal(SIGPIPE, SIG_IGN);
447 2b3d32a1 2022-12-30 thomas signal_add(&evsigint, NULL);
448 2b3d32a1 2022-12-30 thomas signal_add(&evsigterm, NULL);
449 2b3d32a1 2022-12-30 thomas signal_add(&evsighup, NULL);
450 2b3d32a1 2022-12-30 thomas signal_add(&evsigusr1, NULL);
452 2b3d32a1 2022-12-30 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
453 2b3d32a1 2022-12-30 thomas iev.handler = listen_dispatch;
454 2b3d32a1 2022-12-30 thomas iev.events = EV_READ;
455 2b3d32a1 2022-12-30 thomas iev.handler_arg = NULL;
456 2b3d32a1 2022-12-30 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
457 2b3d32a1 2022-12-30 thomas if (event_add(&iev.ev, NULL) == -1)
458 2b3d32a1 2022-12-30 thomas fatalx("event add");
460 2b3d32a1 2022-12-30 thomas event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
461 2b3d32a1 2022-12-30 thomas gotd_accept, &iev);
462 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL))
463 2b3d32a1 2022-12-30 thomas fatalx("event add");
464 2b3d32a1 2022-12-30 thomas evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
466 2b3d32a1 2022-12-30 thomas event_dispatch();
468 2b3d32a1 2022-12-30 thomas listen_shutdown();
471 2b3d32a1 2022-12-30 thomas static void
472 2b3d32a1 2022-12-30 thomas listen_shutdown(void)
474 2b3d32a1 2022-12-30 thomas log_debug("%s: shutting down", gotd_listen.title);
476 2b3d32a1 2022-12-30 thomas if (gotd_listen.fd != -1)
477 2b3d32a1 2022-12-30 thomas close(gotd_listen.fd);