2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
18 #include <sys/types.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_repository_admin.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_idset.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_ratelimit.h"
47 #include "got_lib_pack_create.h"
48 #include "got_lib_poll.h"
52 #include "repo_read.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static struct repo_read {
61 struct got_repository *repo;
65 struct gotd_imsgev session_iev;
68 static struct repo_read_client {
74 struct gotd_object_id_array want_ids;
75 struct gotd_object_id_array have_ids;
78 static volatile sig_atomic_t sigint_received;
79 static volatile sig_atomic_t sigterm_received;
82 catch_sigint(int signo)
88 catch_sigterm(int signo)
93 static const struct got_error *
94 check_cancelled(void *arg)
96 if (sigint_received || sigterm_received)
97 return got_error(GOT_ERR_CANCELLED);
102 static const struct got_error *
103 send_symref(struct got_reference *symref, struct got_object_id *target_id,
104 struct imsgbuf *ibuf)
106 const struct got_error *err = NULL;
107 struct gotd_imsg_symref isymref;
108 const char *refname = got_ref_get_name(symref);
109 const char *target = got_ref_get_symref_target(symref);
113 memset(&isymref, 0, sizeof(isymref));
114 isymref.name_len = strlen(refname);
115 isymref.target_len = strlen(target);
116 memcpy(isymref.target_id, target_id->hash, sizeof(isymref.target_id));
118 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
119 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
120 err = got_error(GOT_ERR_NO_SPACE);
124 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
126 err = got_error_from_errno("imsg_create SYMREF");
130 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
131 err = got_error_from_errno("imsg_add SYMREF");
134 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
135 err = got_error_from_errno("imsg_add SYMREF");
138 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
139 err = got_error_from_errno("imsg_add SYMREF");
144 imsg_close(ibuf, wbuf);
150 static const struct got_error *
151 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
152 struct imsgbuf *ibuf)
154 const struct got_error *err = NULL;
155 struct got_tag_object *tag;
157 char *peeled_refname = NULL;
158 struct got_object_id *id;
161 err = got_object_tag_open(&tag, repo_read.repo, obj);
165 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
166 err = got_error_from_errno("asprintf");
170 id = got_object_tag_get_object_id(tag);
171 namelen = strlen(peeled_refname);
173 len = sizeof(struct gotd_imsg_ref) + namelen;
174 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
175 err = got_error(GOT_ERR_NO_SPACE);
179 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
182 err = got_error_from_errno("imsg_create MREF");
186 /* Keep in sync with struct gotd_imsg_ref definition. */
187 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
188 err = got_error_from_errno("imsg_add REF");
191 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
192 err = got_error_from_errno("imsg_add REF");
195 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
196 err = got_error_from_errno("imsg_add REF");
201 imsg_close(ibuf, wbuf);
203 got_object_tag_close(tag);
207 static const struct got_error *
208 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
210 const struct got_error *err;
211 const char *refname = got_ref_get_name(ref);
213 struct got_object_id *id = NULL;
214 struct got_object *obj = NULL;
218 namelen = strlen(refname);
220 len = sizeof(struct gotd_imsg_ref) + namelen;
221 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 err = got_ref_resolve(&id, repo_read.repo, ref);
228 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
231 err = got_error_from_errno("imsg_create REF");
235 /* Keep in sync with struct gotd_imsg_ref definition. */
236 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
237 return got_error_from_errno("imsg_add REF");
238 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
239 return got_error_from_errno("imsg_add REF");
240 if (imsg_add(wbuf, refname, namelen) == -1)
241 return got_error_from_errno("imsg_add REF");
244 imsg_close(ibuf, wbuf);
246 err = got_object_open(&obj, repo_read.repo, id);
249 if (obj->type == GOT_OBJ_TYPE_TAG)
250 err = send_peeled_tag_ref(ref, obj, ibuf);
253 got_object_close(obj);
258 static const struct got_error *
259 list_refs(struct imsg *imsg)
261 const struct got_error *err;
262 struct repo_read_client *client = &repo_read_client;
263 struct got_reflist_head refs;
264 struct got_reflist_entry *re;
265 struct gotd_imsg_list_refs_internal ireq;
267 struct gotd_imsg_reflist irefs;
269 int client_fd = imsg->fd;
270 struct got_object_id *head_target_id = NULL;
275 return got_error(GOT_ERR_PRIVSEP_NO_FD);
277 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
278 if (datalen != sizeof(ireq))
279 return got_error(GOT_ERR_PRIVSEP_LEN);
280 memcpy(&ireq, imsg->data, sizeof(ireq));
282 if (ireq.client_id == 0)
283 return got_error(GOT_ERR_CLIENT_ID);
284 if (client->id != 0) {
285 return got_error_msg(GOT_ERR_CLIENT_ID,
286 "duplicate list-refs request");
288 client->id = ireq.client_id;
289 client->fd = client_fd;
290 client->delta_cache_fd = -1;
291 client->pack_pipe = -1;
293 imsg_init(&ibuf, client_fd);
295 err = got_ref_list(&refs, repo_read.repo, "",
296 got_ref_cmp_by_name, NULL);
300 memset(&irefs, 0, sizeof(irefs));
301 TAILQ_FOREACH(re, &refs, entry) {
302 struct got_object_id *id;
305 if (got_ref_is_symbolic(re->ref)) {
306 const char *refname = got_ref_get_name(re->ref);
307 if (strcmp(refname, GOT_REF_HEAD) != 0)
309 err = got_ref_resolve(&head_target_id, repo_read.repo,
312 if (err->code != GOT_ERR_NOT_REF)
315 * HEAD points to a non-existent branch.
316 * Do not advertise it.
317 * Matches git-daemon's behaviour.
319 head_target_id = NULL;
328 /* Account for a peeled tag refs. */
329 err = got_ref_resolve(&id, repo_read.repo, re->ref);
332 err = got_object_get_type(&obj_type, repo_read.repo, id);
336 if (obj_type == GOT_OBJ_TYPE_TAG)
340 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
341 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
342 err = got_error_from_errno("imsg_compose REFLIST");
347 * Send the HEAD symref first. In Git-protocol versions < 2
348 * the HEAD symref must be announced on the initial line of
349 * the server's ref advertisement.
350 * For now, we do not advertise symrefs other than HEAD.
352 TAILQ_FOREACH(re, &refs, entry) {
353 if (!got_ref_is_symbolic(re->ref) ||
354 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
355 head_target_id == NULL)
357 err = send_symref(re->ref, head_target_id, &ibuf);
362 TAILQ_FOREACH(re, &refs, entry) {
363 if (got_ref_is_symbolic(re->ref))
365 err = send_ref(re->ref, &ibuf);
370 err = gotd_imsg_flush(&ibuf);
372 got_ref_list_free(&refs);
377 static const struct got_error *
378 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
380 const size_t alloc_chunksz = 256;
382 if (array->ids == NULL) {
383 array->ids = reallocarray(NULL, alloc_chunksz,
384 sizeof(*array->ids));
385 if (array->ids == NULL)
386 return got_error_from_errno("reallocarray");
387 array->nalloc = alloc_chunksz;
389 } else if (array->nalloc <= array->nids) {
390 struct got_object_id **new;
391 new = recallocarray(array->ids, array->nalloc,
392 array->nalloc + alloc_chunksz, sizeof(*new));
394 return got_error_from_errno("recallocarray");
396 array->nalloc += alloc_chunksz;
399 array->ids[array->nids] = got_object_id_dup(id);
400 if (array->ids[array->nids] == NULL)
401 return got_error_from_errno("got_object_id_dup");
407 free_object_ids(struct gotd_object_id_array *array)
411 for (i = 0; i < array->nids; i++)
420 static const struct got_error *
421 recv_want(struct imsg *imsg)
423 const struct got_error *err;
424 struct repo_read_client *client = &repo_read_client;
425 struct gotd_imsg_want iwant;
427 char hex[SHA1_DIGEST_STRING_LENGTH];
428 struct got_object_id id;
432 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
433 if (datalen != sizeof(iwant))
434 return got_error(GOT_ERR_PRIVSEP_LEN);
435 memcpy(&iwant, imsg->data, sizeof(iwant));
437 memset(&id, 0, sizeof(id));
438 memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
440 if (log_getverbose() > 0 &&
441 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
442 log_debug("client wants %s", hex);
444 imsg_init(&ibuf, client->fd);
446 err = got_object_get_type(&obj_type, repo_read.repo, &id);
450 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
451 obj_type != GOT_OBJ_TYPE_TAG)
452 return got_error(GOT_ERR_OBJ_TYPE);
454 err = record_object_id(&client->want_ids, &id);
458 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
463 static const struct got_error *
464 recv_have(struct imsg *imsg)
466 const struct got_error *err;
467 struct repo_read_client *client = &repo_read_client;
468 struct gotd_imsg_have ihave;
470 char hex[SHA1_DIGEST_STRING_LENGTH];
471 struct got_object_id id;
475 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
476 if (datalen != sizeof(ihave))
477 return got_error(GOT_ERR_PRIVSEP_LEN);
478 memcpy(&ihave, imsg->data, sizeof(ihave));
480 memset(&id, 0, sizeof(id));
481 memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
483 if (log_getverbose() > 0 &&
484 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
485 log_debug("client has %s", hex);
487 imsg_init(&ibuf, client->fd);
489 err = got_object_get_type(&obj_type, repo_read.repo, &id);
491 if (err->code == GOT_ERR_NO_OBJ) {
492 gotd_imsg_send_nak(&id, &ibuf,
493 PROC_REPO_READ, repo_read.pid);
499 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
500 obj_type != GOT_OBJ_TYPE_TAG) {
501 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
502 err = got_error(GOT_ERR_OBJ_TYPE);
506 err = record_object_id(&client->have_ids, &id);
510 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
516 struct repo_read_pack_progress_arg {
518 struct imsgbuf *ibuf;
522 static const struct got_error *
523 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
524 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
527 struct repo_read_pack_progress_arg *a = arg;
528 struct gotd_imsg_packfile_progress iprog;
531 if (!a->report_progress)
533 if (packfile_size > 0 && a->sent_ready)
536 memset(&iprog, 0, sizeof(iprog));
537 iprog.ncolored = ncolored;
538 iprog.nfound = nfound;
539 iprog.ntrees = ntrees;
540 iprog.packfile_size = packfile_size;
541 iprog.ncommits = ncommits;
542 iprog.nobj_total = nobj_total;
543 iprog.nobj_deltify = nobj_deltify;
544 iprog.nobj_written = nobj_written;
546 /* Using synchronous writes since we are blocking the event loop. */
547 if (packfile_size == 0) {
548 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
549 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
551 return got_error_from_errno("imsg compose "
552 "PACKFILE_PROGRESS");
556 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
557 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
559 return got_error_from_errno("imsg compose "
564 return gotd_imsg_flush(a->ibuf);
567 static const struct got_error *
568 receive_delta_cache_fd(struct imsg *imsg,
569 struct gotd_imsgev *iev)
571 struct repo_read_client *client = &repo_read_client;
572 struct gotd_imsg_send_packfile ireq;
575 log_debug("receving delta cache file");
578 return got_error(GOT_ERR_PRIVSEP_NO_FD);
580 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
581 if (datalen != sizeof(ireq))
582 return got_error(GOT_ERR_PRIVSEP_LEN);
583 memcpy(&ireq, imsg->data, sizeof(ireq));
585 if (client->delta_cache_fd != -1)
586 return got_error(GOT_ERR_PRIVSEP_MSG);
588 client->delta_cache_fd = imsg->fd;
589 client->report_progress = ireq.report_progress;
593 static const struct got_error *
594 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
596 struct repo_read_client *client = &repo_read_client;
597 struct gotd_imsg_packfile_pipe ireq;
600 log_debug("receving pack pipe descriptor");
603 return got_error(GOT_ERR_PRIVSEP_NO_FD);
605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 if (datalen != sizeof(ireq))
607 return got_error(GOT_ERR_PRIVSEP_LEN);
608 memcpy(&ireq, imsg->data, sizeof(ireq));
610 if (client->pack_pipe != -1)
611 return got_error(GOT_ERR_PRIVSEP_MSG);
613 client->pack_pipe = imsg->fd;
617 static const struct got_error *
618 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
620 const struct got_error *err = NULL;
621 struct repo_read_client *client = &repo_read_client;
622 struct gotd_imsg_packfile_done idone;
623 uint8_t packhash[SHA1_DIGEST_LENGTH];
624 char hex[SHA1_DIGEST_STRING_LENGTH];
625 FILE *delta_cache = NULL;
627 struct repo_read_pack_progress_arg pa;
628 struct got_ratelimit rl;
630 log_debug("packfile request received");
632 got_ratelimit_init(&rl, 2, 0);
634 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
635 return got_error(GOT_ERR_PRIVSEP_NO_FD);
637 imsg_init(&ibuf, client->fd);
639 delta_cache = fdopen(client->delta_cache_fd, "w+");
640 if (delta_cache == NULL) {
641 err = got_error_from_errno("fdopen");
644 client->delta_cache_fd = -1;
646 memset(&pa, 0, sizeof(pa));
648 pa.report_progress = client->report_progress;
650 err = got_pack_create(packhash, client->pack_pipe, delta_cache,
651 client->have_ids.ids, client->have_ids.nids,
652 client->want_ids.ids, client->want_ids.nids,
653 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
654 check_cancelled, NULL);
658 if (log_getverbose() > 0 &&
659 got_sha1_digest_to_str(packhash, hex, sizeof(hex)))
660 log_debug("sent pack-%s.pack", hex);
662 memset(&idone, 0, sizeof(idone));
663 idone.client_id = client->id;
664 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
665 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
666 err = got_error_from_errno("imsg compose PACKFILE_DONE");
668 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
669 err = got_error_from_errno("fclose");
674 static const struct got_error *
675 recv_disconnect(struct imsg *imsg)
677 const struct got_error *err = NULL;
678 struct gotd_imsg_disconnect idisconnect;
680 int delta_cache_fd, pack_pipe;
681 struct repo_read_client *client = &repo_read_client;
683 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
684 if (datalen != sizeof(idisconnect))
685 return got_error(GOT_ERR_PRIVSEP_LEN);
686 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
688 log_debug("client disconnecting");
690 free_object_ids(&client->have_ids);
691 free_object_ids(&client->want_ids);
692 if (close(client->fd) == -1)
693 err = got_error_from_errno("close");
694 delta_cache_fd = client->delta_cache_fd;
695 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
696 return got_error_from_errno("close");
697 pack_pipe = client->pack_pipe;
698 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
699 return got_error_from_errno("close");
704 repo_read_dispatch_session(int fd, short event, void *arg)
706 const struct got_error *err = NULL;
707 struct gotd_imsgev *iev = arg;
708 struct imsgbuf *ibuf = &iev->ibuf;
712 struct repo_read_client *client = &repo_read_client;
714 if (event & EV_READ) {
715 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
716 fatal("imsg_read error");
717 if (n == 0) /* Connection closed. */
721 if (event & EV_WRITE) {
722 n = msgbuf_write(&ibuf->w);
723 if (n == -1 && errno != EAGAIN)
724 fatal("msgbuf_write");
725 if (n == 0) /* Connection closed. */
729 while (err == NULL && check_cancelled(NULL) == NULL) {
730 if ((n = imsg_get(ibuf, &imsg)) == -1)
731 fatal("%s: imsg_get", __func__);
732 if (n == 0) /* No more messages. */
735 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
737 err = got_error(GOT_ERR_PRIVSEP_MSG);
741 switch (imsg.hdr.type) {
742 case GOTD_IMSG_LIST_REFS_INTERNAL:
743 err = list_refs(&imsg);
745 log_warnx("%s: ls-refs: %s", repo_read.title,
749 err = recv_want(&imsg);
751 log_warnx("%s: want-line: %s", repo_read.title,
755 err = recv_have(&imsg);
757 log_warnx("%s: have-line: %s", repo_read.title,
760 case GOTD_IMSG_SEND_PACKFILE:
761 err = receive_delta_cache_fd(&imsg, iev);
763 log_warnx("%s: receiving delta cache: %s",
764 repo_read.title, err->msg);
766 case GOTD_IMSG_PACKFILE_PIPE:
767 err = receive_pack_pipe(&imsg, iev);
769 log_warnx("%s: receiving pack pipe: %s",
770 repo_read.title, err->msg);
773 err = send_packfile(&imsg, iev);
775 log_warnx("%s: sending packfile: %s",
776 repo_read.title, err->msg);
778 case GOTD_IMSG_DISCONNECT:
779 err = recv_disconnect(&imsg);
781 log_warnx("%s: disconnect: %s",
782 repo_read.title, err->msg);
786 log_debug("%s: unexpected imsg %d", repo_read.title,
794 if (!shut && check_cancelled(NULL) == NULL) {
796 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
797 client->id, err) == -1) {
798 log_warnx("could not send error to parent: %s",
801 gotd_imsg_event_add(iev);
803 /* This pipe is dead. Remove its event handler */
805 event_loopexit(NULL);
809 static const struct got_error *
810 recv_connect(struct imsg *imsg)
812 struct gotd_imsgev *iev = &repo_read.session_iev;
815 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
817 return got_error(GOT_ERR_PRIVSEP_LEN);
819 return got_error(GOT_ERR_PRIVSEP_NO_FD);
821 if (repo_read.session_fd != -1)
822 return got_error(GOT_ERR_PRIVSEP_MSG);
824 repo_read.session_fd = imsg->fd;
826 imsg_init(&iev->ibuf, repo_read.session_fd);
827 iev->handler = repo_read_dispatch_session;
828 iev->events = EV_READ;
829 iev->handler_arg = NULL;
830 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
831 repo_read_dispatch_session, iev);
832 gotd_imsg_event_add(iev);
838 repo_read_dispatch(int fd, short event, void *arg)
840 const struct got_error *err = NULL;
841 struct gotd_imsgev *iev = arg;
842 struct imsgbuf *ibuf = &iev->ibuf;
846 struct repo_read_client *client = &repo_read_client;
848 if (event & EV_READ) {
849 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
850 fatal("imsg_read error");
851 if (n == 0) /* Connection closed. */
855 if (event & EV_WRITE) {
856 n = msgbuf_write(&ibuf->w);
857 if (n == -1 && errno != EAGAIN)
858 fatal("msgbuf_write");
859 if (n == 0) /* Connection closed. */
863 while (err == NULL && check_cancelled(NULL) == NULL) {
864 if ((n = imsg_get(ibuf, &imsg)) == -1)
865 fatal("%s: imsg_get", __func__);
866 if (n == 0) /* No more messages. */
869 switch (imsg.hdr.type) {
870 case GOTD_IMSG_CONNECT_REPO_CHILD:
871 err = recv_connect(&imsg);
874 log_debug("%s: unexpected imsg %d", repo_read.title,
882 if (!shut && check_cancelled(NULL) == NULL) {
884 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
885 client->id, err) == -1) {
886 log_warnx("could not send error to parent: %s",
889 gotd_imsg_event_add(iev);
891 /* This pipe is dead. Remove its event handler */
893 event_loopexit(NULL);
898 repo_read_main(const char *title, const char *repo_path,
899 int *pack_fds, int *temp_fds)
901 const struct got_error *err = NULL;
902 struct gotd_imsgev iev;
904 repo_read.title = title;
905 repo_read.pid = getpid();
906 repo_read.pack_fds = pack_fds;
907 repo_read.temp_fds = temp_fds;
908 repo_read.session_fd = -1;
909 repo_read.session_iev.ibuf.fd = -1;
911 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
914 if (!got_repo_is_bare(repo_read.repo)) {
915 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
916 "bare git repository required");
920 got_repo_temp_fds_set(repo_read.repo, temp_fds);
922 signal(SIGINT, catch_sigint);
923 signal(SIGTERM, catch_sigterm);
924 signal(SIGPIPE, SIG_IGN);
925 signal(SIGHUP, SIG_IGN);
927 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
928 iev.handler = repo_read_dispatch;
929 iev.events = EV_READ;
930 iev.handler_arg = NULL;
931 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
933 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
934 PROC_REPO_READ, -1, NULL, 0) == -1) {
935 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
942 log_warnx("%s: %s", title, err->msg);
943 repo_read_shutdown();
947 repo_read_shutdown(void)
949 log_debug("%s: shutting down", repo_read.title);
951 got_repo_close(repo_read.repo);
952 got_repo_pack_fds_close(repo_read.pack_fds);
953 got_repo_temp_fds_close(repo_read.temp_fds);
954 if (repo_read.session_fd != -1)
955 close(repo_read.session_fd);