2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
35 #include "got_error.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 struct gotd_listen_client {
46 STAILQ_ENTRY(gotd_listen_client) entry;
50 STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
52 static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
53 static SIPHASH_KEY clients_hash_key;
54 static volatile int listen_client_cnt;
61 struct gotd_imsgev iev;
62 struct gotd_imsgev pause;
67 static void listen_shutdown(void);
70 listen_sighdlr(int sig, short event, void *arg)
73 * Normal signal handler rules don't apply because libevent
88 fatalx("unexpected signal");
93 client_hash(uint32_t client_id)
95 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
99 add_client(struct gotd_listen_client *client)
101 uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
102 STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
106 static struct gotd_listen_client *
107 find_client(uint32_t client_id)
110 struct gotd_listen_client *c;
112 slot = client_hash(client_id) % nitems(gotd_listen_clients);
113 STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
114 if (c->id == client_id)
129 duplicate = (find_client(id) != NULL);
130 } while (duplicate || id == 0);
135 static const struct got_error *
136 disconnect(struct gotd_listen_client *client)
141 log_debug("client on fd %d disconnecting", client->fd);
143 slot = client_hash(client->id) % nitems(gotd_listen_clients);
144 STAILQ_REMOVE(&gotd_listen_clients[slot], client,
145 gotd_listen_client, entry);
146 client_fd = client->fd;
150 if (close(client_fd) == -1)
151 return got_error_from_errno("close");
157 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
158 int reserve, volatile int *counter)
162 if (getdtablecount() + reserve +
163 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
164 log_debug("inflight fds exceeded");
169 if ((ret = accept4(fd, addr, addrlen,
170 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
178 gotd_accept_paused(int fd, short event, void *arg)
180 event_add(&gotd_listen.iev.ev, NULL);
184 gotd_accept(int fd, short event, void *arg)
186 struct gotd_imsgev *iev = arg;
187 struct sockaddr_storage ss;
188 struct timeval backoff;
191 struct gotd_listen_client *client = NULL;
192 struct gotd_imsg_connect iconn;
199 if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
200 log_warn("event_add");
203 if (event & EV_TIMEOUT)
208 /* Other backoff conditions apart from EMFILE/ENFILE? */
209 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
219 event_del(&gotd_listen.iev.ev);
220 evtimer_add(&gotd_listen.pause.ev, &backoff);
228 if (listen_client_cnt >= GOTD_MAXCLIENTS)
231 if (getpeereid(s, &euid, &egid) == -1) {
232 log_warn("getpeerid");
236 client = calloc(1, sizeof(*client));
237 if (client == NULL) {
238 log_warn("%s: calloc", __func__);
241 client->id = get_client_id();
245 log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
246 client->fd, euid, egid);
248 memset(&iconn, 0, sizeof(iconn));
249 iconn.client_id = client->id;
254 log_warn("%s: dup", __func__);
257 if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
258 &iconn, sizeof(iconn)) == -1) {
259 log_warn("imsg compose CONNECT");
272 static const struct got_error *
273 recv_disconnect(struct imsg *imsg)
275 struct gotd_imsg_disconnect idisconnect;
277 struct gotd_listen_client *client = NULL;
279 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
280 if (datalen != sizeof(idisconnect))
281 return got_error(GOT_ERR_PRIVSEP_LEN);
282 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
284 log_debug("client disconnecting");
286 client = find_client(idisconnect.client_id);
288 return got_error(GOT_ERR_CLIENT_ID);
290 return disconnect(client);
294 listen_dispatch(int fd, short event, void *arg)
296 const struct got_error *err = NULL;
297 struct gotd_imsgev *iev = arg;
298 struct imsgbuf *ibuf = &iev->ibuf;
303 if (event & EV_READ) {
304 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
305 fatal("imsg_read error");
306 if (n == 0) /* Connection closed. */
310 if (event & EV_WRITE) {
311 n = msgbuf_write(&ibuf->w);
312 if (n == -1 && errno != EAGAIN)
313 fatal("msgbuf_write");
314 if (n == 0) /* Connection closed. */
319 if ((n = imsg_get(ibuf, &imsg)) == -1)
320 fatal("%s: imsg_get", __func__);
321 if (n == 0) /* No more messages. */
324 switch (imsg.hdr.type) {
325 case GOTD_IMSG_DISCONNECT:
326 err = recv_disconnect(&imsg);
328 log_warnx("%s: disconnect: %s",
329 gotd_listen.title, err->msg);
332 log_debug("%s: unexpected imsg %d", gotd_listen.title,
341 gotd_imsg_event_add(iev);
343 /* This pipe is dead. Remove its event handler */
345 event_loopexit(NULL);
350 listen_main(const char *title, int gotd_socket)
352 struct gotd_imsgev iev;
353 struct event evsigint, evsigterm, evsighup, evsigusr1;
355 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
357 gotd_listen.title = title;
358 gotd_listen.pid = getpid();
359 gotd_listen.fd = gotd_socket;
361 signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
362 signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
363 signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
364 signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
365 signal(SIGPIPE, SIG_IGN);
367 signal_add(&evsigint, NULL);
368 signal_add(&evsigterm, NULL);
369 signal_add(&evsighup, NULL);
370 signal_add(&evsigusr1, NULL);
372 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
373 iev.handler = listen_dispatch;
374 iev.events = EV_READ;
375 iev.handler_arg = NULL;
376 event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
377 if (event_add(&iev.ev, NULL) == -1)
380 event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
382 if (event_add(&gotd_listen.iev.ev, NULL))
384 evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
392 listen_shutdown(void)
394 log_debug("%s: shutting down", gotd_listen.title);
396 if (gotd_listen.fd != -1)
397 close(gotd_listen.fd);