Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
60 #include "gotd.h"
61 #include "log.h"
62 #include "repo_read.h"
63 #include "repo_write.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct gotd_client {
70 STAILQ_ENTRY(gotd_client) entry;
71 enum gotd_client_state state;
72 struct gotd_client_capability *capabilities;
73 size_t ncapa_alloc;
74 size_t ncapabilities;
75 uint32_t id;
76 int fd;
77 int delta_cache_fd;
78 struct gotd_imsgev iev;
79 struct event tmo;
80 uid_t euid;
81 gid_t egid;
82 struct gotd_child_proc *repo_read;
83 struct gotd_child_proc *repo_write;
84 char *packfile_path;
85 char *packidx_path;
86 int nref_updates;
87 };
88 STAILQ_HEAD(gotd_clients, gotd_client);
90 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
91 static SIPHASH_KEY clients_hash_key;
92 volatile int client_cnt;
93 static struct timeval timeout = { 3600, 0 };
94 static int inflight;
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
100 __dead static void
101 usage()
103 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
104 exit(1);
107 static int
108 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
110 struct sockaddr_un sun;
111 int fd = -1;
112 mode_t old_umask, mode;
114 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
115 if (fd == -1) {
116 log_warn("socket");
117 return -1;
120 sun.sun_family = AF_UNIX;
121 if (strlcpy(sun.sun_path, unix_socket_path,
122 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
123 log_warnx("%s: name too long", unix_socket_path);
124 close(fd);
125 return -1;
128 if (unlink(unix_socket_path) == -1) {
129 if (errno != ENOENT) {
130 log_warn("unlink %s", unix_socket_path);
131 close(fd);
132 return -1;
136 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
137 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
139 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
140 log_warn("bind: %s", unix_socket_path);
141 close(fd);
142 umask(old_umask);
143 return -1;
146 umask(old_umask);
148 if (chmod(unix_socket_path, mode) == -1) {
149 log_warn("chmod %o %s", mode, unix_socket_path);
150 close(fd);
151 unlink(unix_socket_path);
152 return -1;
155 if (chown(unix_socket_path, uid, gid) == -1) {
156 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
157 close(fd);
158 unlink(unix_socket_path);
159 return -1;
162 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
163 log_warn("listen");
164 close(fd);
165 unlink(unix_socket_path);
166 return -1;
169 return fd;
172 static struct group *
173 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
175 struct group *gr;
176 int i;
178 for (i = 0; i < ngroups; i++) {
179 gr = getgrgid(groups[i]);
180 if (gr == NULL) {
181 log_warn("getgrgid %d", groups[i]);
182 continue;
184 if (strcmp(gr->gr_name, unix_group_name) == 0)
185 return gr;
188 return NULL;
191 static int
192 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
193 int reserve, volatile int *counter)
195 int ret;
197 if (getdtablecount() + reserve +
198 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
199 log_debug("inflight fds exceeded");
200 errno = EMFILE;
201 return -1;
204 if ((ret = accept4(fd, addr, addrlen,
205 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
206 (*counter)++;
209 return ret;
212 static uint64_t
213 client_hash(uint32_t client_id)
215 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
218 static void
219 add_client(struct gotd_client *client)
221 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
222 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
223 client_cnt++;
226 static struct gotd_client *
227 find_client(uint32_t client_id)
229 uint64_t slot;
230 struct gotd_client *c;
232 slot = client_hash(client_id) % nitems(gotd_clients);
233 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
234 if (c->id == client_id)
235 return c;
238 return NULL;
241 static uint32_t
242 get_client_id(void)
244 int duplicate = 0;
245 uint32_t id;
247 do {
248 id = arc4random();
249 duplicate = (find_client(id) != NULL);
250 } while (duplicate || id == 0);
252 return id;
255 static struct gotd_child_proc *
256 get_client_proc(struct gotd_client *client)
258 if (client->repo_read && client->repo_write) {
259 fatalx("uid %d is reading and writing in the same session",
260 client->euid);
261 /* NOTREACHED */
264 if (client->repo_read)
265 return client->repo_read;
266 else if (client->repo_write)
267 return client->repo_write;
269 return NULL;
272 static int
273 client_is_reading(struct gotd_client *client)
275 return client->repo_read != NULL;
278 static int
279 client_is_writing(struct gotd_client *client)
281 return client->repo_write != NULL;
284 static const struct got_error *
285 ensure_client_is_reading(struct gotd_client *client)
287 if (!client_is_reading(client)) {
288 return got_error_fmt(GOT_ERR_BAD_PACKET,
289 "uid %d made a read-request but is not reading from "
290 "a repository", client->euid);
293 return NULL;
296 static const struct got_error *
297 ensure_client_is_writing(struct gotd_client *client)
299 if (!client_is_writing(client)) {
300 return got_error_fmt(GOT_ERR_BAD_PACKET,
301 "uid %d made a write-request but is not writing to "
302 "a repository", client->euid);
305 return NULL;
308 static const struct got_error *
309 ensure_client_is_not_writing(struct gotd_client *client)
311 if (client_is_writing(client)) {
312 return got_error_fmt(GOT_ERR_BAD_PACKET,
313 "uid %d made a read-request but is writing to "
314 "a repository", client->euid);
317 return NULL;
320 static const struct got_error *
321 ensure_client_is_not_reading(struct gotd_client *client)
323 if (client_is_reading(client)) {
324 return got_error_fmt(GOT_ERR_BAD_PACKET,
325 "uid %d made a write-request but is reading from "
326 "a repository", client->euid);
329 return NULL;
332 static void
333 disconnect(struct gotd_client *client)
335 struct gotd_imsg_disconnect idisconnect;
336 struct gotd_child_proc *proc = get_client_proc(client);
337 uint64_t slot;
339 log_debug("uid %d: disconnecting", client->euid);
341 idisconnect.client_id = client->id;
342 if (proc) {
343 if (gotd_imsg_compose_event(&proc->iev,
344 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
345 &idisconnect, sizeof(idisconnect)) == -1)
346 log_warn("imsg compose DISCONNECT");
348 slot = client_hash(client->id) % nitems(gotd_clients);
349 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
350 imsg_clear(&client->iev.ibuf);
351 event_del(&client->iev.ev);
352 evtimer_del(&client->tmo);
353 close(client->fd);
354 if (client->delta_cache_fd != -1)
355 close(client->delta_cache_fd);
356 if (client->packfile_path) {
357 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
358 log_warn("unlink %s: ", client->packfile_path);
359 free(client->packfile_path);
361 if (client->packidx_path) {
362 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
363 log_warn("unlink %s: ", client->packidx_path);
364 free(client->packidx_path);
366 free(client->capabilities);
367 free(client);
368 inflight--;
369 client_cnt--;
372 static void
373 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
375 struct imsgbuf ibuf;
377 log_warnx("uid %d: %s", client->euid, err->msg);
378 if (err->code != GOT_ERR_EOF) {
379 imsg_init(&ibuf, client->fd);
380 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
381 imsg_clear(&ibuf);
383 disconnect(client);
386 static const struct got_error *
387 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
389 const struct got_error *err = NULL;
390 struct gotd_imsg_info_repo irepo;
392 memset(&irepo, 0, sizeof(irepo));
394 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
395 >= sizeof(irepo.repo_name))
396 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
397 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
398 >= sizeof(irepo.repo_path))
399 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
401 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
402 &irepo, sizeof(irepo)) == -1) {
403 err = got_error_from_errno("imsg compose INFO_REPO");
404 if (err)
405 return err;
408 return NULL;
411 static const struct got_error *
412 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
414 const struct got_error *err = NULL;
415 struct gotd_imsg_capability icapa;
416 size_t len;
417 struct ibuf *wbuf;
419 memset(&icapa, 0, sizeof(icapa));
421 icapa.key_len = strlen(capa->key);
422 len = sizeof(icapa) + icapa.key_len;
423 if (capa->value) {
424 icapa.value_len = strlen(capa->value);
425 len += icapa.value_len;
428 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
429 if (wbuf == NULL) {
430 err = got_error_from_errno("imsg_create CAPABILITY");
431 return err;
434 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
435 return got_error_from_errno("imsg_add CAPABILITY");
436 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
437 return got_error_from_errno("imsg_add CAPABILITY");
438 if (capa->value) {
439 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
440 return got_error_from_errno("imsg_add CAPABILITY");
443 wbuf->fd = -1;
444 imsg_close(&iev->ibuf, wbuf);
446 gotd_imsg_event_add(iev);
448 return NULL;
451 static const struct got_error *
452 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
454 const struct got_error *err = NULL;
455 struct gotd_imsg_info_client iclient;
456 struct gotd_child_proc *proc;
457 size_t i;
459 memset(&iclient, 0, sizeof(iclient));
460 iclient.euid = client->euid;
461 iclient.egid = client->egid;
463 proc = get_client_proc(client);
464 if (proc) {
465 if (strlcpy(iclient.repo_name, proc->chroot_path,
466 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
467 return got_error_msg(GOT_ERR_NO_SPACE,
468 "repo name too long");
470 if (client_is_writing(client))
471 iclient.is_writing = 1;
474 iclient.state = client->state;
475 iclient.ncapabilities = client->ncapabilities;
477 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
478 &iclient, sizeof(iclient)) == -1) {
479 err = got_error_from_errno("imsg compose INFO_CLIENT");
480 if (err)
481 return err;
484 for (i = 0; i < client->ncapabilities; i++) {
485 struct gotd_client_capability *capa;
486 capa = &client->capabilities[i];
487 err = send_capability(capa, iev);
488 if (err)
489 return err;
492 return NULL;
495 static const struct got_error *
496 send_info(struct gotd_client *client)
498 const struct got_error *err = NULL;
499 struct gotd_imsg_info info;
500 uint64_t slot;
501 struct gotd_repo *repo;
503 info.pid = gotd.pid;
504 info.verbosity = gotd.verbosity;
505 info.nrepos = gotd.nrepos;
506 info.nclients = client_cnt - 1;
508 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
509 &info, sizeof(info)) == -1) {
510 err = got_error_from_errno("imsg compose INFO");
511 if (err)
512 return err;
515 TAILQ_FOREACH(repo, &gotd.repos, entry) {
516 err = send_repo_info(&client->iev, repo);
517 if (err)
518 return err;
521 for (slot = 0; slot < nitems(gotd_clients); slot++) {
522 struct gotd_client *c;
523 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
524 if (c->id == client->id)
525 continue;
526 err = send_client_info(&client->iev, c);
527 if (err)
528 return err;
532 return NULL;
535 static const struct got_error *
536 stop_gotd(struct gotd_client *client)
539 if (client->euid != 0)
540 return got_error_set_errno(EPERM, "stop");
542 gotd_shutdown();
543 /* NOTREACHED */
544 return NULL;
547 static struct gotd_child_proc *
548 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
550 struct gotd_child_proc *proc;
551 int i;
552 size_t namelen;
554 for (i = 0; i < gotd.nprocs; i++) {
555 proc = &gotd.procs[i];
556 if (proc->type != proc_id)
557 continue;
558 namelen = strlen(proc->repo_name);
559 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
560 continue;
561 if (repo_name[namelen] == '\0' ||
562 strcmp(&repo_name[namelen], ".git") == 0)
563 return proc;
566 return NULL;
569 static struct gotd_child_proc *
570 find_proc_by_fd(int fd)
572 struct gotd_child_proc *proc;
573 int i;
575 for (i = 0; i < gotd.nprocs; i++) {
576 proc = &gotd.procs[i];
577 if (proc->iev.ibuf.fd == fd)
578 return proc;
581 return NULL;
584 static const struct got_error *
585 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
587 const struct got_error *err;
588 struct gotd_imsg_list_refs ireq;
589 struct gotd_imsg_list_refs_internal ilref;
590 struct gotd_child_proc *proc = NULL;
591 size_t datalen;
592 int fd = -1;
594 log_debug("list-refs request from uid %d", client->euid);
596 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
597 if (datalen != sizeof(ireq))
598 return got_error(GOT_ERR_PRIVSEP_LEN);
600 memcpy(&ireq, imsg->data, datalen);
602 memset(&ilref, 0, sizeof(ilref));
603 ilref.client_id = client->id;
605 if (ireq.client_is_reading) {
606 err = ensure_client_is_not_writing(client);
607 if (err)
608 return err;
609 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
610 ireq.repo_name);
611 if (client->repo_read == NULL)
612 return got_error(GOT_ERR_NOT_GIT_REPO);
613 } else {
614 err = ensure_client_is_not_reading(client);
615 if (err)
616 return err;
617 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
618 ireq.repo_name);
619 if (client->repo_write == NULL)
620 return got_error(GOT_ERR_NOT_GIT_REPO);
623 fd = dup(client->fd);
624 if (fd == -1)
625 return got_error_from_errno("dup");
627 proc = get_client_proc(client);
628 if (proc == NULL)
629 fatalx("no process found for uid %d", client->euid);
630 if (gotd_imsg_compose_event(&proc->iev,
631 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
632 &ilref, sizeof(ilref)) == -1) {
633 err = got_error_from_errno("imsg compose WANT");
634 close(fd);
635 return err;
638 return NULL;
641 static const struct got_error *
642 forward_want(struct gotd_client *client, struct imsg *imsg)
644 struct gotd_imsg_want ireq;
645 struct gotd_imsg_want iwant;
646 size_t datalen;
648 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
649 if (datalen != sizeof(ireq))
650 return got_error(GOT_ERR_PRIVSEP_LEN);
652 memcpy(&ireq, imsg->data, datalen);
654 memset(&iwant, 0, sizeof(iwant));
655 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
656 iwant.client_id = client->id;
658 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
659 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
660 return got_error_from_errno("imsg compose WANT");
662 return NULL;
665 static const struct got_error *
666 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
668 const struct got_error *err = NULL;
669 struct gotd_imsg_ref_update ireq;
670 struct gotd_imsg_ref_update *iref = NULL;
671 size_t datalen;
673 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
674 if (datalen < sizeof(ireq))
675 return got_error(GOT_ERR_PRIVSEP_LEN);
676 memcpy(&ireq, imsg->data, sizeof(ireq));
677 if (datalen != sizeof(ireq) + ireq.name_len)
678 return got_error(GOT_ERR_PRIVSEP_LEN);
680 iref = malloc(datalen);
681 if (iref == NULL)
682 return got_error_from_errno("malloc");
683 memcpy(iref, imsg->data, datalen);
685 iref->client_id = client->id;
686 if (gotd_imsg_compose_event(&client->repo_write->iev,
687 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
688 err = got_error_from_errno("imsg compose REF_UPDATE");
689 free(iref);
690 return err;
693 static const struct got_error *
694 forward_have(struct gotd_client *client, struct imsg *imsg)
696 struct gotd_imsg_have ireq;
697 struct gotd_imsg_have ihave;
698 size_t datalen;
700 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
701 if (datalen != sizeof(ireq))
702 return got_error(GOT_ERR_PRIVSEP_LEN);
704 memcpy(&ireq, imsg->data, datalen);
706 memset(&ihave, 0, sizeof(ihave));
707 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
708 ihave.client_id = client->id;
710 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
711 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
712 return got_error_from_errno("imsg compose HAVE");
714 return NULL;
717 static int
718 client_has_capability(struct gotd_client *client, const char *capastr)
720 struct gotd_client_capability *capa;
721 size_t i;
723 if (client->ncapabilities == 0)
724 return 0;
726 for (i = 0; i < client->ncapabilities; i++) {
727 capa = &client->capabilities[i];
728 if (strcmp(capa->key, capastr) == 0)
729 return 1;
732 return 0;
735 static const struct got_error *
736 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
738 struct gotd_imsg_capabilities icapas;
739 size_t datalen;
741 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
742 if (datalen != sizeof(icapas))
743 return got_error(GOT_ERR_PRIVSEP_LEN);
744 memcpy(&icapas, imsg->data, sizeof(icapas));
746 client->ncapa_alloc = icapas.ncapabilities;
747 client->capabilities = calloc(client->ncapa_alloc,
748 sizeof(*client->capabilities));
749 if (client->capabilities == NULL) {
750 client->ncapa_alloc = 0;
751 return got_error_from_errno("calloc");
754 log_debug("expecting %zu capabilities from uid %d",
755 client->ncapa_alloc, client->euid);
756 return NULL;
759 static const struct got_error *
760 recv_capability(struct gotd_client *client, struct imsg *imsg)
762 struct gotd_imsg_capability icapa;
763 struct gotd_client_capability *capa;
764 size_t datalen;
765 char *key, *value = NULL;
767 if (client->capabilities == NULL ||
768 client->ncapabilities >= client->ncapa_alloc) {
769 return got_error_msg(GOT_ERR_BAD_REQUEST,
770 "unexpected capability received");
773 memset(&icapa, 0, sizeof(icapa));
775 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
776 if (datalen < sizeof(icapa))
777 return got_error(GOT_ERR_PRIVSEP_LEN);
778 memcpy(&icapa, imsg->data, sizeof(icapa));
780 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
781 return got_error(GOT_ERR_PRIVSEP_LEN);
783 key = malloc(icapa.key_len + 1);
784 if (key == NULL)
785 return got_error_from_errno("malloc");
786 if (icapa.value_len > 0) {
787 value = malloc(icapa.value_len + 1);
788 if (value == NULL) {
789 free(key);
790 return got_error_from_errno("malloc");
794 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
795 key[icapa.key_len] = '\0';
796 if (value) {
797 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
798 icapa.value_len);
799 value[icapa.value_len] = '\0';
802 capa = &client->capabilities[client->ncapabilities++];
803 capa->key = key;
804 capa->value = value;
806 if (value)
807 log_debug("uid %d: capability %s=%s", client->euid, key, value);
808 else
809 log_debug("uid %d: capability %s", client->euid, key);
811 return NULL;
814 static const struct got_error *
815 send_packfile(struct gotd_client *client)
817 const struct got_error *err = NULL;
818 struct gotd_imsg_send_packfile ipack;
819 struct gotd_imsg_packfile_pipe ipipe;
820 int pipe[2];
822 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
823 return got_error_from_errno("socketpair");
825 memset(&ipack, 0, sizeof(ipack));
826 memset(&ipipe, 0, sizeof(ipipe));
828 ipack.client_id = client->id;
829 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
830 ipack.report_progress = 1;
832 client->delta_cache_fd = got_opentempfd();
833 if (client->delta_cache_fd == -1)
834 return got_error_from_errno("got_opentempfd");
836 if (gotd_imsg_compose_event(&client->repo_read->iev,
837 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
838 &ipack, sizeof(ipack)) == -1) {
839 err = got_error_from_errno("imsg compose SEND_PACKFILE");
840 close(pipe[0]);
841 close(pipe[1]);
842 return err;
845 ipipe.client_id = client->id;
847 /* Send pack pipe end 0 to repo_read. */
848 if (gotd_imsg_compose_event(&client->repo_read->iev,
849 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
850 &ipipe, sizeof(ipipe)) == -1) {
851 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
852 close(pipe[1]);
853 return err;
856 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
857 if (gotd_imsg_compose_event(&client->iev,
858 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
859 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
861 return err;
864 static const struct got_error *
865 recv_packfile(struct gotd_client *client)
867 const struct got_error *err = NULL;
868 struct gotd_imsg_recv_packfile ipack;
869 struct gotd_imsg_packfile_pipe ipipe;
870 struct gotd_imsg_packidx_file ifile;
871 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
872 int packfd = -1, idxfd = -1;
873 int pipe[2] = { -1, -1 };
875 if (client->packfile_path) {
876 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
877 "uid %d already has a pack file", client->euid);
880 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
881 return got_error_from_errno("socketpair");
883 memset(&ipipe, 0, sizeof(ipipe));
884 ipipe.client_id = client->id;
886 /* Send pack pipe end 0 to repo_write. */
887 if (gotd_imsg_compose_event(&client->repo_write->iev,
888 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
889 &ipipe, sizeof(ipipe)) == -1) {
890 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
891 pipe[0] = -1;
892 goto done;
894 pipe[0] = -1;
896 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
897 if (gotd_imsg_compose_event(&client->iev,
898 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
899 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
900 pipe[1] = -1;
902 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
903 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
904 client->euid) == -1) {
905 err = got_error_from_errno("asprintf");
906 goto done;
909 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
910 if (err)
911 goto done;
913 free(basepath);
914 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
915 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
916 client->euid) == -1) {
917 err = got_error_from_errno("asprintf");
918 basepath = NULL;
919 goto done;
921 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
922 if (err)
923 goto done;
925 memset(&ifile, 0, sizeof(ifile));
926 ifile.client_id = client->id;
927 if (gotd_imsg_compose_event(&client->repo_write->iev,
928 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
929 &ifile, sizeof(ifile)) == -1) {
930 err = got_error_from_errno("imsg compose PACKIDX_FILE");
931 idxfd = -1;
932 goto done;
934 idxfd = -1;
936 memset(&ipack, 0, sizeof(ipack));
937 ipack.client_id = client->id;
938 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
939 ipack.report_status = 1;
941 if (gotd_imsg_compose_event(&client->repo_write->iev,
942 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
943 &ipack, sizeof(ipack)) == -1) {
944 err = got_error_from_errno("imsg compose RECV_PACKFILE");
945 packfd = -1;
946 goto done;
948 packfd = -1;
950 done:
951 free(basepath);
952 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
953 err = got_error_from_errno("close");
954 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
955 err = got_error_from_errno("close");
956 if (packfd != -1 && close(packfd) == -1 && err == NULL)
957 err = got_error_from_errno("close");
958 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
959 err = got_error_from_errno("close");
960 if (err) {
961 free(pack_path);
962 free(idx_path);
963 } else {
964 client->packfile_path = pack_path;
965 client->packidx_path = idx_path;
967 return err;
970 static void
971 gotd_request(int fd, short events, void *arg)
973 struct gotd_imsgev *iev = arg;
974 struct imsgbuf *ibuf = &iev->ibuf;
975 struct gotd_client *client = iev->handler_arg;
976 const struct got_error *err = NULL;
977 struct imsg imsg;
978 ssize_t n;
980 if (events & EV_WRITE) {
981 while (ibuf->w.queued) {
982 n = msgbuf_write(&ibuf->w);
983 if (n == -1 && errno == EPIPE) {
984 /*
985 * The client has closed its socket.
986 * This can happen when Git clients are
987 * done sending pack file data.
988 */
989 msgbuf_clear(&ibuf->w);
990 continue;
991 } else if (n == -1 && errno != EAGAIN) {
992 err = got_error_from_errno("imsg_flush");
993 disconnect_on_error(client, err);
994 return;
996 if (n == 0) {
997 /* Connection closed. */
998 err = got_error(GOT_ERR_EOF);
999 disconnect_on_error(client, err);
1000 return;
1004 /* Disconnect gotctl(8) now that messages have been sent. */
1005 if (!client_is_reading(client) && !client_is_writing(client)) {
1006 disconnect(client);
1007 return;
1011 if ((events & EV_READ) == 0)
1012 return;
1014 memset(&imsg, 0, sizeof(imsg));
1016 while (err == NULL) {
1017 err = gotd_imsg_recv(&imsg, ibuf, 0);
1018 if (err) {
1019 if (err->code == GOT_ERR_PRIVSEP_READ)
1020 err = NULL;
1021 break;
1024 evtimer_del(&client->tmo);
1026 switch (imsg.hdr.type) {
1027 case GOTD_IMSG_INFO:
1028 err = send_info(client);
1029 break;
1030 case GOTD_IMSG_STOP:
1031 err = stop_gotd(client);
1032 break;
1033 case GOTD_IMSG_LIST_REFS:
1034 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1035 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1036 "unexpected list-refs request received");
1037 break;
1039 err = forward_list_refs_request(client, &imsg);
1040 if (err)
1041 break;
1042 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1043 log_debug("uid %d: expecting capabilities",
1044 client->euid);
1045 break;
1046 case GOTD_IMSG_CAPABILITIES:
1047 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1048 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1049 "unexpected capabilities received");
1050 break;
1052 log_debug("receiving capabilities from uid %d",
1053 client->euid);
1054 err = recv_capabilities(client, &imsg);
1055 break;
1056 case GOTD_IMSG_CAPABILITY:
1057 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1058 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1059 "unexpected capability received");
1060 break;
1062 err = recv_capability(client, &imsg);
1063 if (err || client->ncapabilities < client->ncapa_alloc)
1064 break;
1065 if (client_is_reading(client)) {
1066 client->state = GOTD_STATE_EXPECT_WANT;
1067 log_debug("uid %d: expecting want-lines",
1068 client->euid);
1069 } else if (client_is_writing(client)) {
1070 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1071 log_debug("uid %d: expecting ref-update-lines",
1072 client->euid);
1073 } else
1074 fatalx("client %d is both reading and writing",
1075 client->euid);
1076 break;
1077 case GOTD_IMSG_WANT:
1078 if (client->state != GOTD_STATE_EXPECT_WANT) {
1079 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1080 "unexpected want-line received");
1081 break;
1083 log_debug("received want-line from uid %d",
1084 client->euid);
1085 err = ensure_client_is_reading(client);
1086 if (err)
1087 break;
1088 err = forward_want(client, &imsg);
1089 break;
1090 case GOTD_IMSG_REF_UPDATE:
1091 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1092 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1093 "unexpected ref-update-line received");
1094 break;
1096 log_debug("received ref-update-line from uid %d",
1097 client->euid);
1098 err = ensure_client_is_writing(client);
1099 if (err)
1100 break;
1101 err = forward_ref_update(client, &imsg);
1102 if (err)
1103 break;
1104 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1105 break;
1106 case GOTD_IMSG_HAVE:
1107 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1108 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1109 "unexpected have-line received");
1110 break;
1112 log_debug("received have-line from uid %d",
1113 client->euid);
1114 err = ensure_client_is_reading(client);
1115 if (err)
1116 break;
1117 err = forward_have(client, &imsg);
1118 if (err)
1119 break;
1120 break;
1121 case GOTD_IMSG_FLUSH:
1122 if (client->state == GOTD_STATE_EXPECT_WANT ||
1123 client->state == GOTD_STATE_EXPECT_HAVE) {
1124 err = ensure_client_is_reading(client);
1125 if (err)
1126 break;
1127 } else if (client->state ==
1128 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1129 err = ensure_client_is_writing(client);
1130 if (err)
1131 break;
1132 } else {
1133 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1134 "unexpected flush-pkt received");
1135 break;
1137 log_debug("received flush-pkt from uid %d",
1138 client->euid);
1139 if (client->state == GOTD_STATE_EXPECT_WANT) {
1140 client->state = GOTD_STATE_EXPECT_HAVE;
1141 log_debug("uid %d: expecting have-lines",
1142 client->euid);
1143 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1144 client->state = GOTD_STATE_EXPECT_DONE;
1145 log_debug("uid %d: expecting 'done'",
1146 client->euid);
1147 } else if (client->state ==
1148 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1149 client->state = GOTD_STATE_EXPECT_PACKFILE;
1150 log_debug("uid %d: expecting packfile",
1151 client->euid);
1152 err = recv_packfile(client);
1153 } else {
1154 /* should not happen, see above */
1155 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1156 "unexpected client state");
1157 break;
1159 break;
1160 case GOTD_IMSG_DONE:
1161 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1162 client->state != GOTD_STATE_EXPECT_DONE) {
1163 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1164 "unexpected flush-pkt received");
1165 break;
1167 log_debug("received 'done' from uid %d", client->euid);
1168 err = ensure_client_is_reading(client);
1169 if (err)
1170 break;
1171 client->state = GOTD_STATE_DONE;
1172 err = send_packfile(client);
1173 break;
1174 default:
1175 err = got_error(GOT_ERR_PRIVSEP_MSG);
1176 break;
1179 imsg_free(&imsg);
1182 if (err) {
1183 if (err->code != GOT_ERR_EOF ||
1184 client->state != GOTD_STATE_EXPECT_PACKFILE)
1185 disconnect_on_error(client, err);
1186 } else {
1187 gotd_imsg_event_add(&client->iev);
1188 evtimer_add(&client->tmo, &timeout);
1192 static void
1193 gotd_request_timeout(int fd, short events, void *arg)
1195 struct gotd_client *client = arg;
1197 log_debug("disconnecting uid %d due to timeout", client->euid);
1198 disconnect(client);
1201 static void
1202 gotd_accept(int fd, short event, void *arg)
1204 struct sockaddr_storage ss;
1205 struct timeval backoff;
1206 socklen_t len;
1207 int s = -1;
1208 struct gotd_client *client = NULL;
1209 uid_t euid;
1210 gid_t egid;
1212 backoff.tv_sec = 1;
1213 backoff.tv_usec = 0;
1215 if (event_add(&gotd.ev, NULL) == -1) {
1216 log_warn("event_add");
1217 return;
1219 if (event & EV_TIMEOUT)
1220 return;
1222 len = sizeof(ss);
1224 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1225 &inflight);
1227 if (s == -1) {
1228 switch (errno) {
1229 case EINTR:
1230 case EWOULDBLOCK:
1231 case ECONNABORTED:
1232 return;
1233 case EMFILE:
1234 case ENFILE:
1235 event_del(&gotd.ev);
1236 evtimer_add(&gotd.pause, &backoff);
1237 return;
1238 default:
1239 log_warn("%s: accept", __func__);
1240 return;
1244 if (client_cnt >= GOTD_MAXCLIENTS)
1245 goto err;
1247 if (getpeereid(s, &euid, &egid) == -1) {
1248 log_warn("%s: getpeereid", __func__);
1249 goto err;
1252 client = calloc(1, sizeof(*client));
1253 if (client == NULL) {
1254 log_warn("%s: calloc", __func__);
1255 goto err;
1258 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1259 client->id = get_client_id();
1260 client->fd = s;
1261 s = -1;
1262 client->delta_cache_fd = -1;
1263 client->euid = euid;
1264 client->egid = egid;
1265 client->nref_updates = -1;
1267 imsg_init(&client->iev.ibuf, client->fd);
1268 client->iev.handler = gotd_request;
1269 client->iev.events = EV_READ;
1270 client->iev.handler_arg = client;
1272 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1273 &client->iev);
1274 gotd_imsg_event_add(&client->iev);
1276 evtimer_set(&client->tmo, gotd_request_timeout, client);
1278 add_client(client);
1279 log_debug("%s: new client uid %d connected on fd %d", __func__,
1280 client->euid, client->fd);
1281 return;
1282 err:
1283 inflight--;
1284 if (s != -1)
1285 close(s);
1286 free(client);
1289 static void
1290 gotd_accept_paused(int fd, short event, void *arg)
1292 event_add(&gotd.ev, NULL);
1295 static const char *gotd_proc_names[PROC_MAX] = {
1296 "parent",
1297 "repo_read",
1298 "repo_write"
1301 static struct gotd_child_proc *
1302 get_proc_for_pid(pid_t pid)
1304 struct gotd_child_proc *proc;
1305 int i;
1307 for (i = 0; i < gotd.nprocs; i++) {
1308 proc = &gotd.procs[i];
1309 if (proc->pid == pid)
1310 return proc;
1313 return NULL;
1316 static void
1317 kill_proc(struct gotd_child_proc *proc, int fatal)
1319 if (fatal) {
1320 log_warnx("sending SIGKILL to PID %d", proc->pid);
1321 kill(proc->pid, SIGKILL);
1322 } else
1323 kill(proc->pid, SIGTERM);
1326 static void
1327 gotd_shutdown(void)
1329 pid_t pid;
1330 int status, i;
1331 struct gotd_child_proc *proc;
1333 for (i = 0; i < gotd.nprocs; i++) {
1334 proc = &gotd.procs[i];
1335 msgbuf_clear(&proc->iev.ibuf.w);
1336 close(proc->iev.ibuf.fd);
1337 kill_proc(proc, 0);
1340 log_debug("waiting for children to terminate");
1341 do {
1342 pid = wait(&status);
1343 if (pid == -1) {
1344 if (errno != EINTR && errno != ECHILD)
1345 fatal("wait");
1346 } else if (WIFSIGNALED(status)) {
1347 proc = get_proc_for_pid(pid);
1348 log_warnx("%s %s child process terminated; signal %d",
1349 proc ? gotd_proc_names[proc->type] : "",
1350 proc ? proc->chroot_path : "", WTERMSIG(status));
1352 } while (pid != -1 || (pid == -1 && errno == EINTR));
1354 log_info("terminating");
1355 exit(0);
1358 void
1359 gotd_sighdlr(int sig, short event, void *arg)
1362 * Normal signal handler rules don't apply because libevent
1363 * decouples for us.
1366 switch (sig) {
1367 case SIGHUP:
1368 log_info("%s: ignoring SIGHUP", __func__);
1369 break;
1370 case SIGUSR1:
1371 log_info("%s: ignoring SIGUSR1", __func__);
1372 break;
1373 case SIGTERM:
1374 case SIGINT:
1375 gotd_shutdown();
1376 log_warnx("gotd terminating");
1377 exit(0);
1378 break;
1379 default:
1380 fatalx("unexpected signal");
1384 static const struct got_error *
1385 ensure_proc_is_reading(struct gotd_client *client,
1386 struct gotd_child_proc *proc)
1388 if (!client_is_reading(client)) {
1389 kill_proc(proc, 1);
1390 return got_error_fmt(GOT_ERR_BAD_PACKET,
1391 "PID %d handled a read-request for uid %d but this "
1392 "user is not reading from a repository", proc->pid,
1393 client->euid);
1396 return NULL;
1399 static const struct got_error *
1400 ensure_proc_is_writing(struct gotd_client *client,
1401 struct gotd_child_proc *proc)
1403 if (!client_is_writing(client)) {
1404 kill_proc(proc, 1);
1405 return got_error_fmt(GOT_ERR_BAD_PACKET,
1406 "PID %d handled a write-request for uid %d but this "
1407 "user is not writing to a repository", proc->pid,
1408 client->euid);
1411 return NULL;
1414 static int
1415 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1416 struct imsg *imsg)
1418 const struct got_error *err;
1419 struct gotd_child_proc *client_proc;
1420 int ret = 0;
1422 client_proc = get_client_proc(client);
1423 if (client_proc == NULL)
1424 fatalx("no process found for uid %d", client->euid);
1426 if (proc->pid != client_proc->pid) {
1427 kill_proc(proc, 1);
1428 log_warnx("received message from PID %d for uid %d, while "
1429 "PID %d is the process serving this user",
1430 proc->pid, client->euid, client_proc->pid);
1431 return 0;
1434 switch (imsg->hdr.type) {
1435 case GOTD_IMSG_ERROR:
1436 ret = 1;
1437 break;
1438 case GOTD_IMSG_PACKFILE_DONE:
1439 err = ensure_proc_is_reading(client, proc);
1440 if (err)
1441 log_warnx("uid %d: %s", client->euid, err->msg);
1442 else
1443 ret = 1;
1444 break;
1445 case GOTD_IMSG_PACKFILE_INSTALL:
1446 case GOTD_IMSG_REF_UPDATES_START:
1447 case GOTD_IMSG_REF_UPDATE:
1448 err = ensure_proc_is_writing(client, proc);
1449 if (err)
1450 log_warnx("uid %d: %s", client->euid, err->msg);
1451 else
1452 ret = 1;
1453 break;
1454 default:
1455 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1456 break;
1459 return ret;
1462 static const struct got_error *
1463 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1465 struct gotd_imsg_packfile_done idone;
1466 size_t datalen;
1468 log_debug("packfile-done received");
1470 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1471 if (datalen != sizeof(idone))
1472 return got_error(GOT_ERR_PRIVSEP_LEN);
1473 memcpy(&idone, imsg->data, sizeof(idone));
1475 *client_id = idone.client_id;
1476 return NULL;
1479 static const struct got_error *
1480 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1482 struct gotd_imsg_packfile_install inst;
1483 size_t datalen;
1485 log_debug("packfile-install received");
1487 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1488 if (datalen != sizeof(inst))
1489 return got_error(GOT_ERR_PRIVSEP_LEN);
1490 memcpy(&inst, imsg->data, sizeof(inst));
1492 *client_id = inst.client_id;
1493 return NULL;
1496 static const struct got_error *
1497 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1499 struct gotd_imsg_ref_updates_start istart;
1500 size_t datalen;
1502 log_debug("ref-updates-start received");
1504 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1505 if (datalen != sizeof(istart))
1506 return got_error(GOT_ERR_PRIVSEP_LEN);
1507 memcpy(&istart, imsg->data, sizeof(istart));
1509 *client_id = istart.client_id;
1510 return NULL;
1513 static const struct got_error *
1514 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1516 struct gotd_imsg_ref_update iref;
1517 size_t datalen;
1519 log_debug("ref-update received");
1521 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1522 if (datalen < sizeof(iref))
1523 return got_error(GOT_ERR_PRIVSEP_LEN);
1524 memcpy(&iref, imsg->data, sizeof(iref));
1526 *client_id = iref.client_id;
1527 return NULL;
1530 static const struct got_error *
1531 send_ref_update_ok(struct gotd_client *client,
1532 struct gotd_imsg_ref_update *iref, const char *refname)
1534 struct gotd_imsg_ref_update_ok iok;
1535 struct ibuf *wbuf;
1536 size_t len;
1538 memset(&iok, 0, sizeof(iok));
1539 iok.client_id = client->id;
1540 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1541 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1542 iok.name_len = strlen(refname);
1544 len = sizeof(iok) + iok.name_len;
1545 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1546 PROC_GOTD, gotd.pid, len);
1547 if (wbuf == NULL)
1548 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1550 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1551 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1552 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1553 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1555 wbuf->fd = -1;
1556 imsg_close(&client->iev.ibuf, wbuf);
1557 gotd_imsg_event_add(&client->iev);
1558 return NULL;
1561 static void
1562 send_refs_updated(struct gotd_client *client)
1564 if (gotd_imsg_compose_event(&client->iev,
1565 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1566 log_warn("imsg compose REFS_UPDATED");
1569 static const struct got_error *
1570 send_ref_update_ng(struct gotd_client *client,
1571 struct gotd_imsg_ref_update *iref, const char *refname,
1572 const char *reason)
1574 const struct got_error *ng_err;
1575 struct gotd_imsg_ref_update_ng ing;
1576 struct ibuf *wbuf;
1577 size_t len;
1579 memset(&ing, 0, sizeof(ing));
1580 ing.client_id = client->id;
1581 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1582 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1583 ing.name_len = strlen(refname);
1585 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1586 ing.reason_len = strlen(ng_err->msg);
1588 len = sizeof(ing) + ing.name_len + ing.reason_len;
1589 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1590 PROC_GOTD, gotd.pid, len);
1591 if (wbuf == NULL)
1592 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1594 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1595 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1596 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1597 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1598 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1599 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1601 wbuf->fd = -1;
1602 imsg_close(&client->iev.ibuf, wbuf);
1603 gotd_imsg_event_add(&client->iev);
1604 return NULL;
1607 static const struct got_error *
1608 install_pack(struct gotd_client *client, const char *repo_path,
1609 struct imsg *imsg)
1611 const struct got_error *err = NULL;
1612 struct gotd_imsg_packfile_install inst;
1613 char hex[SHA1_DIGEST_STRING_LENGTH];
1614 size_t datalen;
1615 char *packfile_path = NULL, *packidx_path = NULL;
1617 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1618 if (datalen != sizeof(inst))
1619 return got_error(GOT_ERR_PRIVSEP_LEN);
1620 memcpy(&inst, imsg->data, sizeof(inst));
1622 if (client->packfile_path == NULL)
1623 return got_error_msg(GOT_ERR_BAD_REQUEST,
1624 "client has no pack file");
1625 if (client->packidx_path == NULL)
1626 return got_error_msg(GOT_ERR_BAD_REQUEST,
1627 "client has no pack file index");
1629 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1630 return got_error_msg(GOT_ERR_NO_SPACE,
1631 "could not convert pack file SHA1 to hex");
1633 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1634 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1635 err = got_error_from_errno("asprintf");
1636 goto done;
1639 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1640 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1641 err = got_error_from_errno("asprintf");
1642 goto done;
1645 if (rename(client->packfile_path, packfile_path) == -1) {
1646 err = got_error_from_errno3("rename", client->packfile_path,
1647 packfile_path);
1648 goto done;
1651 free(client->packfile_path);
1652 client->packfile_path = NULL;
1654 if (rename(client->packidx_path, packidx_path) == -1) {
1655 err = got_error_from_errno3("rename", client->packidx_path,
1656 packidx_path);
1657 goto done;
1660 free(client->packidx_path);
1661 client->packidx_path = NULL;
1662 done:
1663 free(packfile_path);
1664 free(packidx_path);
1665 return err;
1668 static const struct got_error *
1669 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1671 struct gotd_imsg_ref_updates_start istart;
1672 size_t datalen;
1674 if (client->nref_updates != -1)
1675 return got_error(GOT_ERR_PRIVSEP_MSG);
1677 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1678 if (datalen != sizeof(istart))
1679 return got_error(GOT_ERR_PRIVSEP_LEN);
1680 memcpy(&istart, imsg->data, sizeof(istart));
1682 if (istart.nref_updates <= 0)
1683 return got_error(GOT_ERR_PRIVSEP_MSG);
1685 client->nref_updates = istart.nref_updates;
1686 return NULL;
1689 static const struct got_error *
1690 update_ref(struct gotd_client *client, const char *repo_path,
1691 struct imsg *imsg)
1693 const struct got_error *err = NULL;
1694 struct got_repository *repo = NULL;
1695 struct got_reference *ref = NULL;
1696 struct gotd_imsg_ref_update iref;
1697 struct got_object_id old_id, new_id;
1698 struct got_object_id *id = NULL;
1699 struct got_object *obj = NULL;
1700 char *refname = NULL;
1701 size_t datalen;
1702 int locked = 0;
1704 log_debug("update-ref from uid %d", client->euid);
1706 if (client->nref_updates <= 0)
1707 return got_error(GOT_ERR_PRIVSEP_MSG);
1709 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1710 if (datalen < sizeof(iref))
1711 return got_error(GOT_ERR_PRIVSEP_LEN);
1712 memcpy(&iref, imsg->data, sizeof(iref));
1713 if (datalen != sizeof(iref) + iref.name_len)
1714 return got_error(GOT_ERR_PRIVSEP_LEN);
1715 refname = malloc(iref.name_len + 1);
1716 if (refname == NULL)
1717 return got_error_from_errno("malloc");
1718 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1719 refname[iref.name_len] = '\0';
1721 log_debug("updating ref %s for uid %d", refname, client->euid);
1723 err = got_repo_open(&repo, repo_path, NULL, NULL);
1724 if (err)
1725 goto done;
1727 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1728 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1729 err = got_object_open(&obj, repo, &new_id);
1730 if (err)
1731 goto done;
1733 if (iref.ref_is_new) {
1734 err = got_ref_open(&ref, repo, refname, 0);
1735 if (err) {
1736 if (err->code != GOT_ERR_NOT_REF)
1737 goto done;
1738 err = got_ref_alloc(&ref, refname, &new_id);
1739 if (err)
1740 goto done;
1741 err = got_ref_write(ref, repo); /* will lock/unlock */
1742 if (err)
1743 goto done;
1744 } else {
1745 err = got_error_fmt(GOT_ERR_REF_BUSY,
1746 "%s has been created by someone else "
1747 "while transaction was in progress",
1748 got_ref_get_name(ref));
1749 goto done;
1751 } else {
1752 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1753 if (err)
1754 goto done;
1755 locked = 1;
1757 err = got_ref_resolve(&id, repo, ref);
1758 if (err)
1759 goto done;
1761 if (got_object_id_cmp(id, &old_id) != 0) {
1762 err = got_error_fmt(GOT_ERR_REF_BUSY,
1763 "%s has been modified by someone else "
1764 "while transaction was in progress",
1765 got_ref_get_name(ref));
1766 goto done;
1769 err = got_ref_change_ref(ref, &new_id);
1770 if (err)
1771 goto done;
1773 err = got_ref_write(ref, repo);
1774 if (err)
1775 goto done;
1777 free(id);
1778 id = NULL;
1780 done:
1781 if (err) {
1782 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1783 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1784 "could not acquire exclusive file lock for %s",
1785 refname);
1787 send_ref_update_ng(client, &iref, refname, err->msg);
1788 } else
1789 send_ref_update_ok(client, &iref, refname);
1791 if (client->nref_updates > 0) {
1792 client->nref_updates--;
1793 if (client->nref_updates == 0)
1794 send_refs_updated(client);
1797 if (locked) {
1798 const struct got_error *unlock_err;
1799 unlock_err = got_ref_unlock(ref);
1800 if (unlock_err && err == NULL)
1801 err = unlock_err;
1803 if (ref)
1804 got_ref_close(ref);
1805 if (obj)
1806 got_object_close(obj);
1807 if (repo)
1808 got_repo_close(repo);
1809 free(refname);
1810 free(id);
1811 return err;
1814 static void
1815 gotd_dispatch(int fd, short event, void *arg)
1817 struct gotd_imsgev *iev = arg;
1818 struct imsgbuf *ibuf = &iev->ibuf;
1819 struct gotd_child_proc *proc = NULL;
1820 ssize_t n;
1821 int shut = 0;
1822 struct imsg imsg;
1824 if (event & EV_READ) {
1825 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1826 fatal("imsg_read error");
1827 if (n == 0) {
1828 /* Connection closed. */
1829 shut = 1;
1830 goto done;
1834 if (event & EV_WRITE) {
1835 n = msgbuf_write(&ibuf->w);
1836 if (n == -1 && errno != EAGAIN)
1837 fatal("msgbuf_write");
1838 if (n == 0) {
1839 /* Connection closed. */
1840 shut = 1;
1841 goto done;
1845 proc = find_proc_by_fd(fd);
1846 if (proc == NULL)
1847 fatalx("cannot find child process for fd %d", fd);
1849 for (;;) {
1850 const struct got_error *err = NULL;
1851 struct gotd_client *client = NULL;
1852 uint32_t client_id = 0;
1853 int do_disconnect = 0;
1854 int do_ref_updates = 0, do_ref_update = 0;
1855 int do_packfile_install = 0;
1857 if ((n = imsg_get(ibuf, &imsg)) == -1)
1858 fatal("%s: imsg_get error", __func__);
1859 if (n == 0) /* No more messages. */
1860 break;
1862 switch (imsg.hdr.type) {
1863 case GOTD_IMSG_ERROR:
1864 do_disconnect = 1;
1865 err = gotd_imsg_recv_error(&client_id, &imsg);
1866 break;
1867 case GOTD_IMSG_PACKFILE_DONE:
1868 do_disconnect = 1;
1869 err = recv_packfile_done(&client_id, &imsg);
1870 break;
1871 case GOTD_IMSG_PACKFILE_INSTALL:
1872 err = recv_packfile_install(&client_id, &imsg);
1873 if (err == NULL)
1874 do_packfile_install = 1;
1875 break;
1876 case GOTD_IMSG_REF_UPDATES_START:
1877 err = recv_ref_updates_start(&client_id, &imsg);
1878 if (err == NULL)
1879 do_ref_updates = 1;
1880 break;
1881 case GOTD_IMSG_REF_UPDATE:
1882 err = recv_ref_update(&client_id, &imsg);
1883 if (err == NULL)
1884 do_ref_update = 1;
1885 break;
1886 default:
1887 log_debug("unexpected imsg %d", imsg.hdr.type);
1888 break;
1891 client = find_client(client_id);
1892 if (client == NULL) {
1893 log_warnx("%s: client not found", __func__);
1894 imsg_free(&imsg);
1895 continue;
1898 if (!verify_imsg_src(client, proc, &imsg)) {
1899 log_debug("dropping imsg type %d from PID %d",
1900 imsg.hdr.type, proc->pid);
1901 imsg_free(&imsg);
1902 continue;
1904 if (err)
1905 log_warnx("uid %d: %s", client->euid, err->msg);
1907 if (do_disconnect) {
1908 if (err)
1909 disconnect_on_error(client, err);
1910 else
1911 disconnect(client);
1912 } else if (do_packfile_install)
1913 err = install_pack(client, proc->chroot_path, &imsg);
1914 else if (do_ref_updates)
1915 err = begin_ref_updates(client, &imsg);
1916 else if (do_ref_update)
1917 err = update_ref(client, proc->chroot_path, &imsg);
1919 if (err)
1920 log_warnx("uid %d: %s", client->euid, err->msg);
1921 imsg_free(&imsg);
1923 done:
1924 if (!shut) {
1925 gotd_imsg_event_add(iev);
1926 } else {
1927 /* This pipe is dead. Remove its event handler */
1928 event_del(&iev->ev);
1929 event_loopexit(NULL);
1933 static pid_t
1934 start_child(enum gotd_procid proc_id, const char *chroot_path,
1935 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1937 char *argv[11];
1938 int argc = 0;
1939 pid_t pid;
1941 switch (pid = fork()) {
1942 case -1:
1943 fatal("cannot fork");
1944 case 0:
1945 break;
1946 default:
1947 close(fd);
1948 return pid;
1951 if (fd != GOTD_SOCK_FILENO) {
1952 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1953 fatal("cannot setup imsg fd");
1954 } else if (fcntl(fd, F_SETFD, 0) == -1)
1955 fatal("cannot setup imsg fd");
1957 argv[argc++] = argv0;
1958 switch (proc_id) {
1959 case PROC_REPO_READ:
1960 argv[argc++] = (char *)"-R";
1961 break;
1962 case PROC_REPO_WRITE:
1963 argv[argc++] = (char *)"-W";
1964 break;
1965 default:
1966 fatalx("invalid process id %d", proc_id);
1969 argv[argc++] = (char *)"-f";
1970 argv[argc++] = (char *)confpath;
1972 argv[argc++] = (char *)"-P";
1973 argv[argc++] = (char *)chroot_path;
1975 if (!daemonize)
1976 argv[argc++] = (char *)"-d";
1977 if (verbosity > 0)
1978 argv[argc++] = (char *)"-v";
1979 if (verbosity > 1)
1980 argv[argc++] = (char *)"-v";
1981 argv[argc++] = NULL;
1983 execvp(argv0, argv);
1984 fatal("execvp");
1987 static void
1988 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
1989 int daemonize, int verbosity)
1991 struct gotd_repo *repo = NULL;
1992 struct gotd_child_proc *proc;
1993 int i;
1996 * XXX For now, use one reader and one writer per repository.
1997 * This should be changed to N readers + M writers.
1999 gotd->nprocs = gotd->nrepos * 2;
2000 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2001 if (gotd->procs == NULL)
2002 fatal("calloc");
2003 for (i = 0; i < gotd->nprocs; i++) {
2004 if (repo == NULL)
2005 repo = TAILQ_FIRST(&gotd->repos);
2006 proc = &gotd->procs[i];
2007 if (i < gotd->nrepos)
2008 proc->type = PROC_REPO_READ;
2009 else
2010 proc->type = PROC_REPO_WRITE;
2011 if (strlcpy(proc->repo_name, repo->name,
2012 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2013 fatalx("repository name too long: %s", repo->name);
2014 log_debug("adding repository %s", repo->name);
2015 if (realpath(repo->path, proc->chroot_path) == NULL)
2016 fatal("%s", repo->path);
2017 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2018 PF_UNSPEC, proc->pipe) == -1)
2019 fatal("socketpair");
2020 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2021 confpath, proc->pipe[1], daemonize, verbosity);
2022 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2023 log_debug("proc %s %s is on fd %d",
2024 gotd_proc_names[proc->type], proc->chroot_path,
2025 proc->pipe[0]);
2026 proc->iev.handler = gotd_dispatch;
2027 proc->iev.events = EV_READ;
2028 proc->iev.handler_arg = NULL;
2029 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2030 gotd_dispatch, &proc->iev);
2032 repo = TAILQ_NEXT(repo, entry);
2036 static void
2037 apply_unveil(void)
2039 struct gotd_repo *repo;
2041 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2042 if (unveil(repo->path, "rwc") == -1)
2043 fatal("unveil %s", repo->path);
2046 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2047 fatal("unveil %s", GOT_TMPDIR_STR);
2049 if (unveil(NULL, NULL) == -1)
2050 fatal("unveil");
2053 int
2054 main(int argc, char **argv)
2056 const struct got_error *error = NULL;
2057 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2058 const char *confpath = GOTD_CONF_PATH;
2059 char *argv0 = argv[0];
2060 char title[2048];
2061 gid_t groups[NGROUPS_MAX + 1];
2062 int ngroups = NGROUPS_MAX + 1;
2063 struct passwd *pw = NULL;
2064 struct group *gr = NULL;
2065 char *repo_path = NULL;
2066 enum gotd_procid proc_id = PROC_GOTD;
2067 struct event evsigint, evsigterm, evsighup, evsigusr1;
2068 int *pack_fds = NULL, *temp_fds = NULL;
2070 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2072 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2073 switch (ch) {
2074 case 'd':
2075 daemonize = 0;
2076 break;
2077 case 'f':
2078 confpath = optarg;
2079 break;
2080 case 'n':
2081 noaction = 1;
2082 break;
2083 case 'P':
2084 repo_path = realpath(optarg, NULL);
2085 if (repo_path == NULL)
2086 fatal("realpath '%s'", optarg);
2087 break;
2088 case 'R':
2089 proc_id = PROC_REPO_READ;
2090 break;
2091 case 'v':
2092 if (verbosity < 3)
2093 verbosity++;
2094 break;
2095 case 'W':
2096 proc_id = PROC_REPO_WRITE;
2097 break;
2098 default:
2099 usage();
2103 argc -= optind;
2104 argv += optind;
2106 if (argc != 0)
2107 usage();
2109 if (geteuid())
2110 fatalx("need root privileges");
2112 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2113 log_setverbose(verbosity);
2115 if (parse_config(confpath, proc_id, &gotd) != 0)
2116 return 1;
2118 if (proc_id == PROC_GOTD &&
2119 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2120 fatalx("no repository defined in configuration file");
2122 pw = getpwnam(gotd.user_name);
2123 if (pw == NULL)
2124 fatal("getpwuid: user %s not found", gotd.user_name);
2126 if (pw->pw_uid == 0) {
2127 fatalx("cannot run %s as %s: the user running %s "
2128 "must not be the superuser",
2129 getprogname(), pw->pw_name, getprogname());
2132 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2133 log_warnx("group membership list truncated");
2135 gr = match_group(groups, ngroups, gotd.unix_group_name);
2136 if (gr == NULL) {
2137 fatalx("cannot start %s: the user running %s "
2138 "must be a secondary member of group %s",
2139 getprogname(), getprogname(), gotd.unix_group_name);
2141 if (gr->gr_gid == pw->pw_gid) {
2142 fatalx("cannot start %s: the user running %s "
2143 "must be a secondary member of group %s, but "
2144 "%s is the user's primary group",
2145 getprogname(), getprogname(), gotd.unix_group_name,
2146 gotd.unix_group_name);
2149 if (proc_id == PROC_GOTD &&
2150 !got_path_is_absolute(gotd.unix_socket_path))
2151 fatalx("bad unix socket path \"%s\": must be an absolute path",
2152 gotd.unix_socket_path);
2154 if (noaction)
2155 return 0;
2157 if (proc_id == PROC_GOTD && verbosity) {
2158 log_info("socket: %s", gotd.unix_socket_path);
2159 log_info("user: %s", pw->pw_name);
2160 log_info("secondary group: %s", gr->gr_name);
2162 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2163 gr->gr_gid);
2164 if (fd == -1) {
2165 fatal("cannot listen on unix socket %s",
2166 gotd.unix_socket_path);
2170 if (proc_id == PROC_GOTD) {
2171 gotd.pid = getpid();
2172 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2173 start_repo_children(&gotd, argv0, confpath, daemonize,
2174 verbosity);
2175 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2176 if (daemonize && daemon(0, 0) == -1)
2177 fatal("daemon");
2178 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2179 error = got_repo_pack_fds_open(&pack_fds);
2180 if (error != NULL)
2181 fatalx("cannot open pack tempfiles: %s", error->msg);
2182 error = got_repo_temp_fds_open(&temp_fds);
2183 if (error != NULL)
2184 fatalx("cannot open pack tempfiles: %s", error->msg);
2185 if (repo_path == NULL)
2186 fatalx("repository path not specified");
2187 snprintf(title, sizeof(title), "%s %s",
2188 gotd_proc_names[proc_id], repo_path);
2189 if (chroot(repo_path) == -1)
2190 fatal("chroot");
2191 if (chdir("/") == -1)
2192 fatal("chdir(\"/\")");
2193 if (daemonize && daemon(1, 0) == -1)
2194 fatal("daemon");
2195 } else
2196 fatal("invalid process id %d", proc_id);
2198 setproctitle("%s", title);
2199 log_procinit(title);
2201 /* Drop root privileges. */
2202 if (setgid(pw->pw_gid) == -1)
2203 fatal("setgid %d failed", pw->pw_gid);
2204 if (setuid(pw->pw_uid) == -1)
2205 fatal("setuid %d failed", pw->pw_uid);
2207 event_init();
2209 switch (proc_id) {
2210 case PROC_GOTD:
2211 #ifndef PROFILE
2212 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2213 "fattr flock unix unveil", NULL) == -1)
2214 err(1, "pledge");
2215 #endif
2216 break;
2217 case PROC_REPO_READ:
2218 #ifndef PROFILE
2219 if (pledge("stdio rpath recvfd", NULL) == -1)
2220 err(1, "pledge");
2221 #endif
2222 repo_read_main(title, pack_fds, temp_fds);
2223 /* NOTREACHED */
2224 exit(0);
2225 case PROC_REPO_WRITE:
2226 #ifndef PROFILE
2227 if (pledge("stdio rpath recvfd", NULL) == -1)
2228 err(1, "pledge");
2229 #endif
2230 repo_write_main(title, pack_fds, temp_fds);
2231 /* NOTREACHED */
2232 exit(0);
2233 default:
2234 fatal("invalid process id %d", proc_id);
2237 if (proc_id != PROC_GOTD)
2238 fatal("invalid process id %d", proc_id);
2240 apply_unveil();
2242 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2243 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2244 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2245 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2246 signal(SIGPIPE, SIG_IGN);
2248 signal_add(&evsigint, NULL);
2249 signal_add(&evsigterm, NULL);
2250 signal_add(&evsighup, NULL);
2251 signal_add(&evsigusr1, NULL);
2253 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2254 if (event_add(&gotd.ev, NULL))
2255 fatalx("event add");
2256 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2258 event_dispatch();
2260 if (fd != -1)
2261 close(fd);
2262 if (pack_fds)
2263 got_repo_pack_fds_close(pack_fds);
2264 free(repo_path);
2265 return 0;