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 if (gotd_imsg_compose_event(&client->repo_write->iev,
887 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
888 &ipipe, sizeof(ipipe)) == -1) {
889 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
890 pipe[0] = -1;
891 goto done;
893 pipe[0] = -1;
895 if (gotd_imsg_compose_event(&client->repo_write->iev,
896 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
897 &ipipe, sizeof(ipipe)) == -1)
898 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
899 pipe[1] = -1;
901 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
902 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
903 client->euid) == -1) {
904 err = got_error_from_errno("asprintf");
905 goto done;
908 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
909 if (err)
910 goto done;
912 free(basepath);
913 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
914 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
915 client->euid) == -1) {
916 err = got_error_from_errno("asprintf");
917 basepath = NULL;
918 goto done;
920 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
921 if (err)
922 goto done;
924 memset(&ifile, 0, sizeof(ifile));
925 ifile.client_id = client->id;
926 if (gotd_imsg_compose_event(&client->repo_write->iev,
927 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
928 &ifile, sizeof(ifile)) == -1) {
929 err = got_error_from_errno("imsg compose PACKIDX_FILE");
930 idxfd = -1;
931 goto done;
933 idxfd = -1;
935 memset(&ipack, 0, sizeof(ipack));
936 ipack.client_id = client->id;
937 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
938 ipack.report_status = 1;
940 if (gotd_imsg_compose_event(&client->repo_write->iev,
941 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
942 &ipack, sizeof(ipack)) == -1) {
943 err = got_error_from_errno("imsg compose RECV_PACKFILE");
944 packfd = -1;
945 goto done;
947 packfd = -1;
949 done:
950 free(basepath);
951 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
952 err = got_error_from_errno("close");
953 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
954 err = got_error_from_errno("close");
955 if (packfd != -1 && close(packfd) == -1 && err == NULL)
956 err = got_error_from_errno("close");
957 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
958 err = got_error_from_errno("close");
959 if (err) {
960 free(pack_path);
961 free(idx_path);
962 } else {
963 client->packfile_path = pack_path;
964 client->packidx_path = idx_path;
966 return err;
969 static void
970 gotd_request(int fd, short events, void *arg)
972 struct gotd_imsgev *iev = arg;
973 struct imsgbuf *ibuf = &iev->ibuf;
974 struct gotd_client *client = iev->handler_arg;
975 const struct got_error *err = NULL;
976 struct imsg imsg;
977 ssize_t n;
979 if (events & EV_WRITE) {
980 while (ibuf->w.queued) {
981 n = msgbuf_write(&ibuf->w);
982 if (n == -1 && errno == EPIPE) {
983 /*
984 * The client has closed its socket.
985 * This can happen when Git clients are
986 * done sending pack file data.
987 */
988 msgbuf_clear(&ibuf->w);
989 continue;
990 } else if (n == -1 && errno != EAGAIN) {
991 err = got_error_from_errno("imsg_flush");
992 disconnect_on_error(client, err);
993 return;
995 if (n == 0) {
996 /* Connection closed. */
997 err = got_error(GOT_ERR_EOF);
998 disconnect_on_error(client, err);
999 return;
1003 /* Disconnect gotctl(8) now that messages have been sent. */
1004 if (!client_is_reading(client) && !client_is_writing(client)) {
1005 disconnect(client);
1006 return;
1010 if ((events & EV_READ) == 0)
1011 return;
1013 memset(&imsg, 0, sizeof(imsg));
1015 while (err == NULL) {
1016 err = gotd_imsg_recv(&imsg, ibuf, 0);
1017 if (err) {
1018 if (err->code == GOT_ERR_PRIVSEP_READ)
1019 err = NULL;
1020 break;
1023 evtimer_del(&client->tmo);
1025 switch (imsg.hdr.type) {
1026 case GOTD_IMSG_INFO:
1027 err = send_info(client);
1028 break;
1029 case GOTD_IMSG_STOP:
1030 err = stop_gotd(client);
1031 break;
1032 case GOTD_IMSG_LIST_REFS:
1033 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1034 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1035 "unexpected list-refs request received");
1036 break;
1038 err = forward_list_refs_request(client, &imsg);
1039 if (err)
1040 break;
1041 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1042 log_debug("uid %d: expecting capabilities",
1043 client->euid);
1044 break;
1045 case GOTD_IMSG_CAPABILITIES:
1046 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1047 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1048 "unexpected capabilities received");
1049 break;
1051 log_debug("receiving capabilities from uid %d",
1052 client->euid);
1053 err = recv_capabilities(client, &imsg);
1054 break;
1055 case GOTD_IMSG_CAPABILITY:
1056 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1057 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1058 "unexpected capability received");
1059 break;
1061 err = recv_capability(client, &imsg);
1062 if (err || client->ncapabilities < client->ncapa_alloc)
1063 break;
1064 if (client_is_reading(client)) {
1065 client->state = GOTD_STATE_EXPECT_WANT;
1066 log_debug("uid %d: expecting want-lines",
1067 client->euid);
1068 } else if (client_is_writing(client)) {
1069 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1070 log_debug("uid %d: expecting ref-update-lines",
1071 client->euid);
1072 } else
1073 fatalx("client %d is both reading and writing",
1074 client->euid);
1075 break;
1076 case GOTD_IMSG_WANT:
1077 if (client->state != GOTD_STATE_EXPECT_WANT) {
1078 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1079 "unexpected want-line received");
1080 break;
1082 log_debug("received want-line from uid %d",
1083 client->euid);
1084 err = ensure_client_is_reading(client);
1085 if (err)
1086 break;
1087 err = forward_want(client, &imsg);
1088 break;
1089 case GOTD_IMSG_REF_UPDATE:
1090 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1091 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1092 "unexpected ref-update-line received");
1093 break;
1095 log_debug("received ref-update-line from uid %d",
1096 client->euid);
1097 err = ensure_client_is_writing(client);
1098 if (err)
1099 break;
1100 err = forward_ref_update(client, &imsg);
1101 if (err)
1102 break;
1103 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1104 break;
1105 case GOTD_IMSG_HAVE:
1106 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1107 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 "unexpected have-line received");
1109 break;
1111 log_debug("received have-line from uid %d",
1112 client->euid);
1113 err = ensure_client_is_reading(client);
1114 if (err)
1115 break;
1116 err = forward_have(client, &imsg);
1117 if (err)
1118 break;
1119 break;
1120 case GOTD_IMSG_FLUSH:
1121 if (client->state == GOTD_STATE_EXPECT_WANT ||
1122 client->state == GOTD_STATE_EXPECT_HAVE) {
1123 err = ensure_client_is_reading(client);
1124 if (err)
1125 break;
1126 } else if (client->state ==
1127 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1128 err = ensure_client_is_writing(client);
1129 if (err)
1130 break;
1131 } else {
1132 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1133 "unexpected flush-pkt received");
1134 break;
1136 log_debug("received flush-pkt from uid %d",
1137 client->euid);
1138 if (client->state == GOTD_STATE_EXPECT_WANT) {
1139 client->state = GOTD_STATE_EXPECT_HAVE;
1140 log_debug("uid %d: expecting have-lines",
1141 client->euid);
1142 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1143 client->state = GOTD_STATE_EXPECT_DONE;
1144 log_debug("uid %d: expecting 'done'",
1145 client->euid);
1146 } else if (client->state ==
1147 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1148 client->state = GOTD_STATE_EXPECT_PACKFILE;
1149 log_debug("uid %d: expecting packfile",
1150 client->euid);
1151 err = recv_packfile(client);
1152 } else {
1153 /* should not happen, see above */
1154 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1155 "unexpected client state");
1156 break;
1158 break;
1159 case GOTD_IMSG_DONE:
1160 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1161 client->state != GOTD_STATE_EXPECT_DONE) {
1162 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1163 "unexpected flush-pkt received");
1164 break;
1166 log_debug("received 'done' from uid %d", client->euid);
1167 err = ensure_client_is_reading(client);
1168 if (err)
1169 break;
1170 client->state = GOTD_STATE_DONE;
1171 err = send_packfile(client);
1172 break;
1173 default:
1174 err = got_error(GOT_ERR_PRIVSEP_MSG);
1175 break;
1178 imsg_free(&imsg);
1181 if (err) {
1182 if (err->code != GOT_ERR_EOF ||
1183 client->state != GOTD_STATE_EXPECT_PACKFILE)
1184 disconnect_on_error(client, err);
1185 } else {
1186 gotd_imsg_event_add(&client->iev);
1187 evtimer_add(&client->tmo, &timeout);
1191 static void
1192 gotd_request_timeout(int fd, short events, void *arg)
1194 struct gotd_client *client = arg;
1196 log_debug("disconnecting uid %d due to timeout", client->euid);
1197 disconnect(client);
1200 static void
1201 gotd_accept(int fd, short event, void *arg)
1203 struct sockaddr_storage ss;
1204 struct timeval backoff;
1205 socklen_t len;
1206 int s = -1;
1207 struct gotd_client *client = NULL;
1208 uid_t euid;
1209 gid_t egid;
1211 backoff.tv_sec = 1;
1212 backoff.tv_usec = 0;
1214 if (event_add(&gotd.ev, NULL) == -1) {
1215 log_warn("event_add");
1216 return;
1218 if (event & EV_TIMEOUT)
1219 return;
1221 len = sizeof(ss);
1223 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1224 &inflight);
1226 if (s == -1) {
1227 switch (errno) {
1228 case EINTR:
1229 case EWOULDBLOCK:
1230 case ECONNABORTED:
1231 return;
1232 case EMFILE:
1233 case ENFILE:
1234 event_del(&gotd.ev);
1235 evtimer_add(&gotd.pause, &backoff);
1236 return;
1237 default:
1238 log_warn("%s: accept", __func__);
1239 return;
1243 if (client_cnt >= GOTD_MAXCLIENTS)
1244 goto err;
1246 if (getpeereid(s, &euid, &egid) == -1) {
1247 log_warn("%s: getpeereid", __func__);
1248 goto err;
1251 client = calloc(1, sizeof(*client));
1252 if (client == NULL) {
1253 log_warn("%s: calloc", __func__);
1254 goto err;
1257 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1258 client->id = get_client_id();
1259 client->fd = s;
1260 s = -1;
1261 client->delta_cache_fd = -1;
1262 client->euid = euid;
1263 client->egid = egid;
1264 client->nref_updates = -1;
1266 imsg_init(&client->iev.ibuf, client->fd);
1267 client->iev.handler = gotd_request;
1268 client->iev.events = EV_READ;
1269 client->iev.handler_arg = client;
1271 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1272 &client->iev);
1273 gotd_imsg_event_add(&client->iev);
1275 evtimer_set(&client->tmo, gotd_request_timeout, client);
1277 add_client(client);
1278 log_debug("%s: new client uid %d connected on fd %d", __func__,
1279 client->euid, client->fd);
1280 return;
1281 err:
1282 inflight--;
1283 if (s != -1)
1284 close(s);
1285 free(client);
1288 static void
1289 gotd_accept_paused(int fd, short event, void *arg)
1291 event_add(&gotd.ev, NULL);
1294 static const char *gotd_proc_names[PROC_MAX] = {
1295 "parent",
1296 "repo_read",
1297 "repo_write"
1300 static struct gotd_child_proc *
1301 get_proc_for_pid(pid_t pid)
1303 struct gotd_child_proc *proc;
1304 int i;
1306 for (i = 0; i < gotd.nprocs; i++) {
1307 proc = &gotd.procs[i];
1308 if (proc->pid == pid)
1309 return proc;
1312 return NULL;
1315 static void
1316 kill_proc(struct gotd_child_proc *proc, int fatal)
1318 if (fatal) {
1319 log_warnx("sending SIGKILL to PID %d", proc->pid);
1320 kill(proc->pid, SIGKILL);
1321 } else
1322 kill(proc->pid, SIGTERM);
1325 static void
1326 gotd_shutdown(void)
1328 pid_t pid;
1329 int status, i;
1330 struct gotd_child_proc *proc;
1332 for (i = 0; i < gotd.nprocs; i++) {
1333 proc = &gotd.procs[i];
1334 msgbuf_clear(&proc->iev.ibuf.w);
1335 close(proc->iev.ibuf.fd);
1336 kill_proc(proc, 0);
1339 log_debug("waiting for children to terminate");
1340 do {
1341 pid = wait(&status);
1342 if (pid == -1) {
1343 if (errno != EINTR && errno != ECHILD)
1344 fatal("wait");
1345 } else if (WIFSIGNALED(status)) {
1346 proc = get_proc_for_pid(pid);
1347 log_warnx("%s %s child process terminated; signal %d",
1348 proc ? gotd_proc_names[proc->type] : "",
1349 proc ? proc->chroot_path : "", WTERMSIG(status));
1351 } while (pid != -1 || (pid == -1 && errno == EINTR));
1353 log_info("terminating");
1354 exit(0);
1357 void
1358 gotd_sighdlr(int sig, short event, void *arg)
1361 * Normal signal handler rules don't apply because libevent
1362 * decouples for us.
1365 switch (sig) {
1366 case SIGHUP:
1367 log_info("%s: ignoring SIGHUP", __func__);
1368 break;
1369 case SIGUSR1:
1370 log_info("%s: ignoring SIGUSR1", __func__);
1371 break;
1372 case SIGTERM:
1373 case SIGINT:
1374 gotd_shutdown();
1375 log_warnx("gotd terminating");
1376 exit(0);
1377 break;
1378 default:
1379 fatalx("unexpected signal");
1383 static const struct got_error *
1384 ensure_proc_is_reading(struct gotd_client *client,
1385 struct gotd_child_proc *proc)
1387 if (!client_is_reading(client)) {
1388 kill_proc(proc, 1);
1389 return got_error_fmt(GOT_ERR_BAD_PACKET,
1390 "PID %d handled a read-request for uid %d but this "
1391 "user is not reading from a repository", proc->pid,
1392 client->euid);
1395 return NULL;
1398 static const struct got_error *
1399 ensure_proc_is_writing(struct gotd_client *client,
1400 struct gotd_child_proc *proc)
1402 if (!client_is_writing(client)) {
1403 kill_proc(proc, 1);
1404 return got_error_fmt(GOT_ERR_BAD_PACKET,
1405 "PID %d handled a write-request for uid %d but this "
1406 "user is not writing to a repository", proc->pid,
1407 client->euid);
1410 return NULL;
1413 static int
1414 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1415 struct imsg *imsg)
1417 const struct got_error *err;
1418 struct gotd_child_proc *client_proc;
1419 int ret = 0;
1421 client_proc = get_client_proc(client);
1422 if (client_proc == NULL)
1423 fatalx("no process found for uid %d", client->euid);
1425 if (proc->pid != client_proc->pid) {
1426 kill_proc(proc, 1);
1427 log_warnx("received message from PID %d for uid %d, while "
1428 "PID %d is the process serving this user",
1429 proc->pid, client->euid, client_proc->pid);
1430 return 0;
1433 switch (imsg->hdr.type) {
1434 case GOTD_IMSG_ERROR:
1435 ret = 1;
1436 break;
1437 case GOTD_IMSG_PACKFILE_DONE:
1438 err = ensure_proc_is_reading(client, proc);
1439 if (err)
1440 log_warnx("uid %d: %s", client->euid, err->msg);
1441 else
1442 ret = 1;
1443 break;
1444 case GOTD_IMSG_PACKFILE_INSTALL:
1445 case GOTD_IMSG_REF_UPDATES_START:
1446 case GOTD_IMSG_REF_UPDATE:
1447 err = ensure_proc_is_writing(client, proc);
1448 if (err)
1449 log_warnx("uid %d: %s", client->euid, err->msg);
1450 else
1451 ret = 1;
1452 break;
1453 default:
1454 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1455 break;
1458 return ret;
1461 static const struct got_error *
1462 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1464 struct gotd_imsg_packfile_done idone;
1465 size_t datalen;
1467 log_debug("packfile-done received");
1469 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1470 if (datalen != sizeof(idone))
1471 return got_error(GOT_ERR_PRIVSEP_LEN);
1472 memcpy(&idone, imsg->data, sizeof(idone));
1474 *client_id = idone.client_id;
1475 return NULL;
1478 static const struct got_error *
1479 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1481 struct gotd_imsg_packfile_install inst;
1482 size_t datalen;
1484 log_debug("packfile-install received");
1486 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1487 if (datalen != sizeof(inst))
1488 return got_error(GOT_ERR_PRIVSEP_LEN);
1489 memcpy(&inst, imsg->data, sizeof(inst));
1491 *client_id = inst.client_id;
1492 return NULL;
1495 static const struct got_error *
1496 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1498 struct gotd_imsg_ref_updates_start istart;
1499 size_t datalen;
1501 log_debug("ref-updates-start received");
1503 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1504 if (datalen != sizeof(istart))
1505 return got_error(GOT_ERR_PRIVSEP_LEN);
1506 memcpy(&istart, imsg->data, sizeof(istart));
1508 *client_id = istart.client_id;
1509 return NULL;
1512 static const struct got_error *
1513 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1515 struct gotd_imsg_ref_update iref;
1516 size_t datalen;
1518 log_debug("ref-update received");
1520 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1521 if (datalen < sizeof(iref))
1522 return got_error(GOT_ERR_PRIVSEP_LEN);
1523 memcpy(&iref, imsg->data, sizeof(iref));
1525 *client_id = iref.client_id;
1526 return NULL;
1529 static const struct got_error *
1530 send_ref_update_ok(struct gotd_client *client,
1531 struct gotd_imsg_ref_update *iref, const char *refname)
1533 struct gotd_imsg_ref_update_ok iok;
1534 struct ibuf *wbuf;
1535 size_t len;
1537 memset(&iok, 0, sizeof(iok));
1538 iok.client_id = client->id;
1539 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1540 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1541 iok.name_len = strlen(refname);
1543 len = sizeof(iok) + iok.name_len;
1544 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1545 PROC_GOTD, gotd.pid, len);
1546 if (wbuf == NULL)
1547 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1549 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1550 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1551 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1552 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1554 wbuf->fd = -1;
1555 imsg_close(&client->iev.ibuf, wbuf);
1556 gotd_imsg_event_add(&client->iev);
1557 return NULL;
1560 static void
1561 send_refs_updated(struct gotd_client *client)
1563 if (gotd_imsg_compose_event(&client->iev,
1564 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1565 log_warn("imsg compose REFS_UPDATED");
1568 static const struct got_error *
1569 send_ref_update_ng(struct gotd_client *client,
1570 struct gotd_imsg_ref_update *iref, const char *refname,
1571 const char *reason)
1573 const struct got_error *ng_err;
1574 struct gotd_imsg_ref_update_ng ing;
1575 struct ibuf *wbuf;
1576 size_t len;
1578 memset(&ing, 0, sizeof(ing));
1579 ing.client_id = client->id;
1580 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1581 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1582 ing.name_len = strlen(refname);
1584 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1585 ing.reason_len = strlen(ng_err->msg);
1587 len = sizeof(ing) + ing.name_len + ing.reason_len;
1588 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1589 PROC_GOTD, gotd.pid, len);
1590 if (wbuf == NULL)
1591 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1593 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1594 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1595 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1596 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1597 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1598 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1600 wbuf->fd = -1;
1601 imsg_close(&client->iev.ibuf, wbuf);
1602 gotd_imsg_event_add(&client->iev);
1603 return NULL;
1606 static const struct got_error *
1607 install_pack(struct gotd_client *client, const char *repo_path,
1608 struct imsg *imsg)
1610 const struct got_error *err = NULL;
1611 struct gotd_imsg_packfile_install inst;
1612 char hex[SHA1_DIGEST_STRING_LENGTH];
1613 size_t datalen;
1614 char *packfile_path = NULL, *packidx_path = NULL;
1616 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1617 if (datalen != sizeof(inst))
1618 return got_error(GOT_ERR_PRIVSEP_LEN);
1619 memcpy(&inst, imsg->data, sizeof(inst));
1621 if (client->packfile_path == NULL)
1622 return got_error_msg(GOT_ERR_BAD_REQUEST,
1623 "client has no pack file");
1624 if (client->packidx_path == NULL)
1625 return got_error_msg(GOT_ERR_BAD_REQUEST,
1626 "client has no pack file index");
1628 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1629 return got_error_msg(GOT_ERR_NO_SPACE,
1630 "could not convert pack file SHA1 to hex");
1632 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1633 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1634 err = got_error_from_errno("asprintf");
1635 goto done;
1638 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1639 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1640 err = got_error_from_errno("asprintf");
1641 goto done;
1644 if (rename(client->packfile_path, packfile_path) == -1) {
1645 err = got_error_from_errno3("rename", client->packfile_path,
1646 packfile_path);
1647 goto done;
1650 free(client->packfile_path);
1651 client->packfile_path = NULL;
1653 if (rename(client->packidx_path, packidx_path) == -1) {
1654 err = got_error_from_errno3("rename", client->packidx_path,
1655 packidx_path);
1656 goto done;
1659 free(client->packidx_path);
1660 client->packidx_path = NULL;
1661 done:
1662 free(packfile_path);
1663 free(packidx_path);
1664 return err;
1667 static const struct got_error *
1668 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1670 struct gotd_imsg_ref_updates_start istart;
1671 size_t datalen;
1673 if (client->nref_updates != -1)
1674 return got_error(GOT_ERR_PRIVSEP_MSG);
1676 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1677 if (datalen != sizeof(istart))
1678 return got_error(GOT_ERR_PRIVSEP_LEN);
1679 memcpy(&istart, imsg->data, sizeof(istart));
1681 if (istart.nref_updates <= 0)
1682 return got_error(GOT_ERR_PRIVSEP_MSG);
1684 client->nref_updates = istart.nref_updates;
1685 return NULL;
1688 static const struct got_error *
1689 update_ref(struct gotd_client *client, const char *repo_path,
1690 struct imsg *imsg)
1692 const struct got_error *err = NULL;
1693 struct got_repository *repo = NULL;
1694 struct got_reference *ref = NULL;
1695 struct gotd_imsg_ref_update iref;
1696 struct got_object_id old_id, new_id;
1697 struct got_object_id *id = NULL;
1698 struct got_object *obj = NULL;
1699 char *refname = NULL;
1700 size_t datalen;
1701 int locked = 0;
1703 log_debug("update-ref from uid %d", client->euid);
1705 if (client->nref_updates <= 0)
1706 return got_error(GOT_ERR_PRIVSEP_MSG);
1708 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1709 if (datalen < sizeof(iref))
1710 return got_error(GOT_ERR_PRIVSEP_LEN);
1711 memcpy(&iref, imsg->data, sizeof(iref));
1712 if (datalen != sizeof(iref) + iref.name_len)
1713 return got_error(GOT_ERR_PRIVSEP_LEN);
1714 refname = malloc(iref.name_len + 1);
1715 if (refname == NULL)
1716 return got_error_from_errno("malloc");
1717 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1718 refname[iref.name_len] = '\0';
1720 log_debug("updating ref %s for uid %d", refname, client->euid);
1722 err = got_repo_open(&repo, repo_path, NULL, NULL);
1723 if (err)
1724 goto done;
1726 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1727 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1728 err = got_object_open(&obj, repo, &new_id);
1729 if (err)
1730 goto done;
1732 if (iref.ref_is_new) {
1733 err = got_ref_open(&ref, repo, refname, 0);
1734 if (err) {
1735 if (err->code != GOT_ERR_NOT_REF)
1736 goto done;
1737 err = got_ref_alloc(&ref, refname, &new_id);
1738 if (err)
1739 goto done;
1740 err = got_ref_write(ref, repo); /* will lock/unlock */
1741 if (err)
1742 goto done;
1743 } else {
1744 err = got_error_fmt(GOT_ERR_REF_BUSY,
1745 "%s has been created by someone else "
1746 "while transaction was in progress",
1747 got_ref_get_name(ref));
1748 goto done;
1750 } else {
1751 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1752 if (err)
1753 goto done;
1754 locked = 1;
1756 err = got_ref_resolve(&id, repo, ref);
1757 if (err)
1758 goto done;
1760 if (got_object_id_cmp(id, &old_id) != 0) {
1761 err = got_error_fmt(GOT_ERR_REF_BUSY,
1762 "%s has been modified by someone else "
1763 "while transaction was in progress",
1764 got_ref_get_name(ref));
1765 goto done;
1768 err = got_ref_change_ref(ref, &new_id);
1769 if (err)
1770 goto done;
1772 err = got_ref_write(ref, repo);
1773 if (err)
1774 goto done;
1776 free(id);
1777 id = NULL;
1779 done:
1780 if (err) {
1781 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1782 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1783 "could not acquire exclusive file lock for %s",
1784 refname);
1786 send_ref_update_ng(client, &iref, refname, err->msg);
1787 } else
1788 send_ref_update_ok(client, &iref, refname);
1790 if (client->nref_updates > 0) {
1791 client->nref_updates--;
1792 if (client->nref_updates == 0)
1793 send_refs_updated(client);
1796 if (locked) {
1797 const struct got_error *unlock_err;
1798 unlock_err = got_ref_unlock(ref);
1799 if (unlock_err && err == NULL)
1800 err = unlock_err;
1802 if (ref)
1803 got_ref_close(ref);
1804 if (obj)
1805 got_object_close(obj);
1806 if (repo)
1807 got_repo_close(repo);
1808 free(refname);
1809 free(id);
1810 return err;
1813 static void
1814 gotd_dispatch(int fd, short event, void *arg)
1816 struct gotd_imsgev *iev = arg;
1817 struct imsgbuf *ibuf = &iev->ibuf;
1818 struct gotd_child_proc *proc = NULL;
1819 ssize_t n;
1820 int shut = 0;
1821 struct imsg imsg;
1823 if (event & EV_READ) {
1824 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1825 fatal("imsg_read error");
1826 if (n == 0) {
1827 /* Connection closed. */
1828 shut = 1;
1829 goto done;
1833 if (event & EV_WRITE) {
1834 n = msgbuf_write(&ibuf->w);
1835 if (n == -1 && errno != EAGAIN)
1836 fatal("msgbuf_write");
1837 if (n == 0) {
1838 /* Connection closed. */
1839 shut = 1;
1840 goto done;
1844 proc = find_proc_by_fd(fd);
1845 if (proc == NULL)
1846 fatalx("cannot find child process for fd %d", fd);
1848 for (;;) {
1849 const struct got_error *err = NULL;
1850 struct gotd_client *client = NULL;
1851 uint32_t client_id = 0;
1852 int do_disconnect = 0;
1853 int do_ref_updates = 0, do_ref_update = 0;
1854 int do_packfile_install = 0;
1856 if ((n = imsg_get(ibuf, &imsg)) == -1)
1857 fatal("%s: imsg_get error", __func__);
1858 if (n == 0) /* No more messages. */
1859 break;
1861 switch (imsg.hdr.type) {
1862 case GOTD_IMSG_ERROR:
1863 do_disconnect = 1;
1864 err = gotd_imsg_recv_error(&client_id, &imsg);
1865 break;
1866 case GOTD_IMSG_PACKFILE_DONE:
1867 do_disconnect = 1;
1868 err = recv_packfile_done(&client_id, &imsg);
1869 break;
1870 case GOTD_IMSG_PACKFILE_INSTALL:
1871 err = recv_packfile_install(&client_id, &imsg);
1872 if (err == NULL)
1873 do_packfile_install = 1;
1874 break;
1875 case GOTD_IMSG_REF_UPDATES_START:
1876 err = recv_ref_updates_start(&client_id, &imsg);
1877 if (err == NULL)
1878 do_ref_updates = 1;
1879 break;
1880 case GOTD_IMSG_REF_UPDATE:
1881 err = recv_ref_update(&client_id, &imsg);
1882 if (err == NULL)
1883 do_ref_update = 1;
1884 break;
1885 default:
1886 log_debug("unexpected imsg %d", imsg.hdr.type);
1887 break;
1890 client = find_client(client_id);
1891 if (client == NULL) {
1892 log_warnx("%s: client not found", __func__);
1893 imsg_free(&imsg);
1894 continue;
1897 if (!verify_imsg_src(client, proc, &imsg)) {
1898 log_debug("dropping imsg type %d from PID %d",
1899 imsg.hdr.type, proc->pid);
1900 imsg_free(&imsg);
1901 continue;
1903 if (err)
1904 log_warnx("uid %d: %s", client->euid, err->msg);
1906 if (do_disconnect) {
1907 if (err)
1908 disconnect_on_error(client, err);
1909 else
1910 disconnect(client);
1911 } else if (do_packfile_install)
1912 err = install_pack(client, proc->chroot_path, &imsg);
1913 else if (do_ref_updates)
1914 err = begin_ref_updates(client, &imsg);
1915 else if (do_ref_update)
1916 err = update_ref(client, proc->chroot_path, &imsg);
1918 if (err)
1919 log_warnx("uid %d: %s", client->euid, err->msg);
1920 imsg_free(&imsg);
1922 done:
1923 if (!shut) {
1924 gotd_imsg_event_add(iev);
1925 } else {
1926 /* This pipe is dead. Remove its event handler */
1927 event_del(&iev->ev);
1928 event_loopexit(NULL);
1932 static pid_t
1933 start_child(enum gotd_procid proc_id, const char *chroot_path,
1934 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1936 char *argv[11];
1937 int argc = 0;
1938 pid_t pid;
1940 switch (pid = fork()) {
1941 case -1:
1942 fatal("cannot fork");
1943 case 0:
1944 break;
1945 default:
1946 close(fd);
1947 return pid;
1950 if (fd != GOTD_SOCK_FILENO) {
1951 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1952 fatal("cannot setup imsg fd");
1953 } else if (fcntl(fd, F_SETFD, 0) == -1)
1954 fatal("cannot setup imsg fd");
1956 argv[argc++] = argv0;
1957 switch (proc_id) {
1958 case PROC_REPO_READ:
1959 argv[argc++] = (char *)"-R";
1960 break;
1961 case PROC_REPO_WRITE:
1962 argv[argc++] = (char *)"-W";
1963 break;
1964 default:
1965 fatalx("invalid process id %d", proc_id);
1968 argv[argc++] = (char *)"-f";
1969 argv[argc++] = (char *)confpath;
1971 argv[argc++] = (char *)"-P";
1972 argv[argc++] = (char *)chroot_path;
1974 if (!daemonize)
1975 argv[argc++] = (char *)"-d";
1976 if (verbosity > 0)
1977 argv[argc++] = (char *)"-v";
1978 if (verbosity > 1)
1979 argv[argc++] = (char *)"-v";
1980 argv[argc++] = NULL;
1982 execvp(argv0, argv);
1983 fatal("execvp");
1986 static void
1987 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
1988 int daemonize, int verbosity)
1990 struct gotd_repo *repo = NULL;
1991 struct gotd_child_proc *proc;
1992 int i;
1995 * XXX For now, use one reader and one writer per repository.
1996 * This should be changed to N readers + M writers.
1998 gotd->nprocs = gotd->nrepos * 2;
1999 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2000 if (gotd->procs == NULL)
2001 fatal("calloc");
2002 for (i = 0; i < gotd->nprocs; i++) {
2003 if (repo == NULL)
2004 repo = TAILQ_FIRST(&gotd->repos);
2005 proc = &gotd->procs[i];
2006 if (i < gotd->nrepos)
2007 proc->type = PROC_REPO_READ;
2008 else
2009 proc->type = PROC_REPO_WRITE;
2010 if (strlcpy(proc->repo_name, repo->name,
2011 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2012 fatalx("repository name too long: %s", repo->name);
2013 log_debug("adding repository %s", repo->name);
2014 if (realpath(repo->path, proc->chroot_path) == NULL)
2015 fatal("%s", repo->path);
2016 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2017 PF_UNSPEC, proc->pipe) == -1)
2018 fatal("socketpair");
2019 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2020 confpath, proc->pipe[1], daemonize, verbosity);
2021 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2022 log_debug("proc %s %s is on fd %d",
2023 gotd_proc_names[proc->type], proc->chroot_path,
2024 proc->pipe[0]);
2025 proc->iev.handler = gotd_dispatch;
2026 proc->iev.events = EV_READ;
2027 proc->iev.handler_arg = NULL;
2028 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2029 gotd_dispatch, &proc->iev);
2031 repo = TAILQ_NEXT(repo, entry);
2035 static void
2036 apply_unveil(void)
2038 struct gotd_repo *repo;
2040 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2041 if (unveil(repo->path, "rwc") == -1)
2042 fatal("unveil %s", repo->path);
2045 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2046 fatal("unveil %s", GOT_TMPDIR_STR);
2048 if (unveil(NULL, NULL) == -1)
2049 fatal("unveil");
2052 int
2053 main(int argc, char **argv)
2055 const struct got_error *error = NULL;
2056 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2057 const char *confpath = GOTD_CONF_PATH;
2058 char *argv0 = argv[0];
2059 char title[2048];
2060 gid_t groups[NGROUPS_MAX + 1];
2061 int ngroups = NGROUPS_MAX + 1;
2062 struct passwd *pw = NULL;
2063 struct group *gr = NULL;
2064 char *repo_path = NULL;
2065 enum gotd_procid proc_id = PROC_GOTD;
2066 struct event evsigint, evsigterm, evsighup, evsigusr1;
2067 int *pack_fds = NULL, *temp_fds = NULL;
2069 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2071 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2072 switch (ch) {
2073 case 'd':
2074 daemonize = 0;
2075 break;
2076 case 'f':
2077 confpath = optarg;
2078 break;
2079 case 'n':
2080 noaction = 1;
2081 break;
2082 case 'P':
2083 repo_path = realpath(optarg, NULL);
2084 if (repo_path == NULL)
2085 fatal("realpath '%s'", optarg);
2086 break;
2087 case 'R':
2088 proc_id = PROC_REPO_READ;
2089 break;
2090 case 'v':
2091 if (verbosity < 3)
2092 verbosity++;
2093 break;
2094 case 'W':
2095 proc_id = PROC_REPO_WRITE;
2096 break;
2097 default:
2098 usage();
2102 argc -= optind;
2103 argv += optind;
2105 if (argc != 0)
2106 usage();
2108 if (geteuid())
2109 fatalx("need root privileges");
2111 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2112 log_setverbose(verbosity);
2114 if (parse_config(confpath, proc_id, &gotd) != 0)
2115 return 1;
2117 if (proc_id == PROC_GOTD &&
2118 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2119 fatalx("no repository defined in configuration file");
2121 pw = getpwnam(gotd.user_name);
2122 if (pw == NULL)
2123 fatal("getpwuid: user %s not found", gotd.user_name);
2125 if (pw->pw_uid == 0) {
2126 fatalx("cannot run %s as %s: the user running %s "
2127 "must not be the superuser",
2128 getprogname(), pw->pw_name, getprogname());
2131 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2132 log_warnx("group membership list truncated");
2134 gr = match_group(groups, ngroups, gotd.unix_group_name);
2135 if (gr == NULL) {
2136 fatalx("cannot start %s: the user running %s "
2137 "must be a secondary member of group %s",
2138 getprogname(), getprogname(), gotd.unix_group_name);
2140 if (gr->gr_gid == pw->pw_gid) {
2141 fatalx("cannot start %s: the user running %s "
2142 "must be a secondary member of group %s, but "
2143 "%s is the user's primary group",
2144 getprogname(), getprogname(), gotd.unix_group_name,
2145 gotd.unix_group_name);
2148 if (proc_id == PROC_GOTD &&
2149 !got_path_is_absolute(gotd.unix_socket_path))
2150 fatalx("bad unix socket path \"%s\": must be an absolute path",
2151 gotd.unix_socket_path);
2153 if (noaction)
2154 return 0;
2156 if (proc_id == PROC_GOTD && verbosity) {
2157 log_info("socket: %s", gotd.unix_socket_path);
2158 log_info("user: %s", pw->pw_name);
2159 log_info("secondary group: %s", gr->gr_name);
2161 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2162 gr->gr_gid);
2163 if (fd == -1) {
2164 fatal("cannot listen on unix socket %s",
2165 gotd.unix_socket_path);
2169 if (proc_id == PROC_GOTD) {
2170 gotd.pid = getpid();
2171 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2172 start_repo_children(&gotd, argv0, confpath, daemonize,
2173 verbosity);
2174 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2175 if (daemonize && daemon(0, 0) == -1)
2176 fatal("daemon");
2177 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2178 error = got_repo_pack_fds_open(&pack_fds);
2179 if (error != NULL)
2180 fatalx("cannot open pack tempfiles: %s", error->msg);
2181 error = got_repo_temp_fds_open(&temp_fds);
2182 if (error != NULL)
2183 fatalx("cannot open pack tempfiles: %s", error->msg);
2184 if (repo_path == NULL)
2185 fatalx("repository path not specified");
2186 snprintf(title, sizeof(title), "%s %s",
2187 gotd_proc_names[proc_id], repo_path);
2188 if (chroot(repo_path) == -1)
2189 fatal("chroot");
2190 if (chdir("/") == -1)
2191 fatal("chdir(\"/\")");
2192 if (daemonize && daemon(1, 0) == -1)
2193 fatal("daemon");
2194 } else
2195 fatal("invalid process id %d", proc_id);
2197 setproctitle("%s", title);
2198 log_procinit(title);
2200 /* Drop root privileges. */
2201 if (setgid(pw->pw_gid) == -1)
2202 fatal("setgid %d failed", pw->pw_gid);
2203 if (setuid(pw->pw_uid) == -1)
2204 fatal("setuid %d failed", pw->pw_uid);
2206 event_init();
2208 switch (proc_id) {
2209 case PROC_GOTD:
2210 #ifndef PROFILE
2211 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2212 "fattr flock unix unveil", NULL) == -1)
2213 err(1, "pledge");
2214 #endif
2215 break;
2216 case PROC_REPO_READ:
2217 #ifndef PROFILE
2218 if (pledge("stdio rpath recvfd", NULL) == -1)
2219 err(1, "pledge");
2220 #endif
2221 repo_read_main(title, pack_fds, temp_fds);
2222 /* NOTREACHED */
2223 exit(0);
2224 case PROC_REPO_WRITE:
2225 #ifndef PROFILE
2226 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2227 err(1, "pledge");
2228 #endif
2229 repo_write_main(title, pack_fds, temp_fds);
2230 /* NOTREACHED */
2231 exit(0);
2232 default:
2233 fatal("invalid process id %d", proc_id);
2236 if (proc_id != PROC_GOTD)
2237 fatal("invalid process id %d", proc_id);
2239 apply_unveil();
2241 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2242 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2243 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2244 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2245 signal(SIGPIPE, SIG_IGN);
2247 signal_add(&evsigint, NULL);
2248 signal_add(&evsigterm, NULL);
2249 signal_add(&evsighup, NULL);
2250 signal_add(&evsigusr1, NULL);
2252 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2253 if (event_add(&gotd.ev, NULL))
2254 fatalx("event add");
2255 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2257 event_dispatch();
2259 if (fd != -1)
2260 close(fd);
2261 if (pack_fds)
2262 got_repo_pack_fds_close(pack_fds);
2263 free(repo_path);
2264 return 0;