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>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_repository_admin.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_hash.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_ratelimit.h"
45 #include "got_lib_pack_create.h"
46 #include "got_lib_poll.h"
50 #include "repo_read.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static struct repo_read {
59 struct got_repository *repo;
63 struct gotd_imsgev session_iev;
66 static struct repo_read_client {
72 struct gotd_object_id_array want_ids;
73 struct gotd_object_id_array have_ids;
76 static volatile sig_atomic_t sigint_received;
77 static volatile sig_atomic_t sigterm_received;
80 catch_sigint(int signo)
86 catch_sigterm(int signo)
91 static const struct got_error *
92 check_cancelled(void *arg)
94 if (sigint_received || sigterm_received)
95 return got_error(GOT_ERR_CANCELLED);
100 static const struct got_error *
101 send_symref(struct got_reference *symref, struct got_object_id *target_id,
102 struct imsgbuf *ibuf)
104 const struct got_error *err = NULL;
105 struct gotd_imsg_symref isymref;
106 const char *refname = got_ref_get_name(symref);
107 const char *target = got_ref_get_symref_target(symref);
111 memset(&isymref, 0, sizeof(isymref));
112 isymref.name_len = strlen(refname);
113 isymref.target_len = strlen(target);
114 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
116 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
117 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
118 err = got_error(GOT_ERR_NO_SPACE);
122 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
124 err = got_error_from_errno("imsg_create SYMREF");
128 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
129 err = got_error_from_errno("imsg_add SYMREF");
132 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
136 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
142 imsg_close(ibuf, wbuf);
148 static const struct got_error *
149 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
150 struct imsgbuf *ibuf)
152 const struct got_error *err = NULL;
153 struct got_tag_object *tag;
155 char *peeled_refname = NULL;
156 struct got_object_id *id;
159 err = got_object_tag_open(&tag, repo_read.repo, obj);
163 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
164 err = got_error_from_errno("asprintf");
168 id = got_object_tag_get_object_id(tag);
169 namelen = strlen(peeled_refname);
171 len = sizeof(struct gotd_imsg_ref) + namelen;
172 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
173 err = got_error(GOT_ERR_NO_SPACE);
177 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
180 err = got_error_from_errno("imsg_create MREF");
184 /* Keep in sync with struct gotd_imsg_ref definition. */
185 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
186 err = got_error_from_errno("imsg_add REF");
189 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
190 err = got_error_from_errno("imsg_add REF");
193 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
194 err = got_error_from_errno("imsg_add REF");
199 imsg_close(ibuf, wbuf);
201 got_object_tag_close(tag);
205 static const struct got_error *
206 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
208 const struct got_error *err;
209 const char *refname = got_ref_get_name(ref);
211 struct got_object_id *id = NULL;
212 struct got_object *obj = NULL;
216 namelen = strlen(refname);
218 len = sizeof(struct gotd_imsg_ref) + namelen;
219 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
220 return got_error(GOT_ERR_NO_SPACE);
222 err = got_ref_resolve(&id, repo_read.repo, ref);
226 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
229 err = got_error_from_errno("imsg_create REF");
233 /* Keep in sync with struct gotd_imsg_ref definition. */
234 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
235 return got_error_from_errno("imsg_add REF");
236 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
237 return got_error_from_errno("imsg_add REF");
238 if (imsg_add(wbuf, refname, namelen) == -1)
239 return got_error_from_errno("imsg_add REF");
242 imsg_close(ibuf, wbuf);
244 err = got_object_open(&obj, repo_read.repo, id);
247 if (obj->type == GOT_OBJ_TYPE_TAG)
248 err = send_peeled_tag_ref(ref, obj, ibuf);
251 got_object_close(obj);
256 static const struct got_error *
257 list_refs(struct imsg *imsg)
259 const struct got_error *err;
260 struct repo_read_client *client = &repo_read_client;
261 struct got_reflist_head refs;
262 struct got_reflist_entry *re;
263 struct gotd_imsg_list_refs_internal ireq;
265 struct gotd_imsg_reflist irefs;
267 int client_fd = imsg->fd;
268 struct got_object_id *head_target_id = NULL;
273 return got_error(GOT_ERR_PRIVSEP_NO_FD);
275 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 if (datalen != sizeof(ireq))
277 return got_error(GOT_ERR_PRIVSEP_LEN);
278 memcpy(&ireq, imsg->data, sizeof(ireq));
280 if (ireq.client_id == 0)
281 return got_error(GOT_ERR_CLIENT_ID);
282 if (client->id != 0) {
283 return got_error_msg(GOT_ERR_CLIENT_ID,
284 "duplicate list-refs request");
286 client->id = ireq.client_id;
287 client->fd = client_fd;
289 imsg_init(&ibuf, client_fd);
291 err = got_ref_list(&refs, repo_read.repo, "",
292 got_ref_cmp_by_name, NULL);
296 memset(&irefs, 0, sizeof(irefs));
297 TAILQ_FOREACH(re, &refs, entry) {
298 struct got_object_id *id;
301 if (got_ref_is_symbolic(re->ref)) {
302 const char *refname = got_ref_get_name(re->ref);
303 if (strcmp(refname, GOT_REF_HEAD) != 0)
305 err = got_ref_resolve(&head_target_id, repo_read.repo,
308 if (err->code != GOT_ERR_NOT_REF)
311 * HEAD points to a non-existent branch.
312 * Do not advertise it.
313 * Matches git-daemon's behaviour.
315 head_target_id = NULL;
324 /* Account for a peeled tag refs. */
325 err = got_ref_resolve(&id, repo_read.repo, re->ref);
328 err = got_object_get_type(&obj_type, repo_read.repo, id);
332 if (obj_type == GOT_OBJ_TYPE_TAG)
336 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
337 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
338 err = got_error_from_errno("imsg_compose REFLIST");
343 * Send the HEAD symref first. In Git-protocol versions < 2
344 * the HEAD symref must be announced on the initial line of
345 * the server's ref advertisement.
346 * For now, we do not advertise symrefs other than HEAD.
348 TAILQ_FOREACH(re, &refs, entry) {
349 if (!got_ref_is_symbolic(re->ref) ||
350 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
351 head_target_id == NULL)
353 err = send_symref(re->ref, head_target_id, &ibuf);
358 TAILQ_FOREACH(re, &refs, entry) {
359 if (got_ref_is_symbolic(re->ref))
361 err = send_ref(re->ref, &ibuf);
366 err = gotd_imsg_flush(&ibuf);
368 got_ref_list_free(&refs);
373 static const struct got_error *
374 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
376 const size_t alloc_chunksz = 256;
378 if (array->ids == NULL) {
379 array->ids = reallocarray(NULL, alloc_chunksz,
380 sizeof(*array->ids));
381 if (array->ids == NULL)
382 return got_error_from_errno("reallocarray");
383 array->nalloc = alloc_chunksz;
385 } else if (array->nalloc <= array->nids) {
386 struct got_object_id **new;
387 new = recallocarray(array->ids, array->nalloc,
388 array->nalloc + alloc_chunksz, sizeof(*new));
390 return got_error_from_errno("recallocarray");
392 array->nalloc += alloc_chunksz;
395 array->ids[array->nids] = got_object_id_dup(id);
396 if (array->ids[array->nids] == NULL)
397 return got_error_from_errno("got_object_id_dup");
403 free_object_ids(struct gotd_object_id_array *array)
407 for (i = 0; i < array->nids; i++)
416 static const struct got_error *
417 recv_want(struct imsg *imsg)
419 const struct got_error *err;
420 struct repo_read_client *client = &repo_read_client;
421 struct gotd_imsg_want iwant;
423 char hex[SHA1_DIGEST_STRING_LENGTH];
424 struct got_object_id id;
428 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
429 if (datalen != sizeof(iwant))
430 return got_error(GOT_ERR_PRIVSEP_LEN);
431 memcpy(&iwant, imsg->data, sizeof(iwant));
433 memset(&id, 0, sizeof(id));
434 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
436 if (log_getverbose() > 0 &&
437 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
438 log_debug("client wants %s", hex);
440 imsg_init(&ibuf, client->fd);
442 err = got_object_get_type(&obj_type, repo_read.repo, &id);
446 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
447 obj_type != GOT_OBJ_TYPE_TAG)
448 return got_error(GOT_ERR_OBJ_TYPE);
450 err = record_object_id(&client->want_ids, &id);
454 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
459 static const struct got_error *
460 recv_have(struct imsg *imsg)
462 const struct got_error *err;
463 struct repo_read_client *client = &repo_read_client;
464 struct gotd_imsg_have ihave;
466 char hex[SHA1_DIGEST_STRING_LENGTH];
467 struct got_object_id id;
471 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
472 if (datalen != sizeof(ihave))
473 return got_error(GOT_ERR_PRIVSEP_LEN);
474 memcpy(&ihave, imsg->data, sizeof(ihave));
476 memset(&id, 0, sizeof(id));
477 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
479 if (log_getverbose() > 0 &&
480 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
481 log_debug("client has %s", hex);
483 imsg_init(&ibuf, client->fd);
485 err = got_object_get_type(&obj_type, repo_read.repo, &id);
487 if (err->code == GOT_ERR_NO_OBJ) {
488 gotd_imsg_send_nak(&id, &ibuf,
489 PROC_REPO_READ, repo_read.pid);
495 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
496 obj_type != GOT_OBJ_TYPE_TAG) {
497 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
498 err = got_error(GOT_ERR_OBJ_TYPE);
502 err = record_object_id(&client->have_ids, &id);
506 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
512 struct repo_read_pack_progress_arg {
514 struct imsgbuf *ibuf;
518 static const struct got_error *
519 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
520 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
523 struct repo_read_pack_progress_arg *a = arg;
524 struct gotd_imsg_packfile_progress iprog;
527 if (!a->report_progress)
529 if (packfile_size > 0 && a->sent_ready)
532 memset(&iprog, 0, sizeof(iprog));
533 iprog.ncolored = ncolored;
534 iprog.nfound = nfound;
535 iprog.ntrees = ntrees;
536 iprog.packfile_size = packfile_size;
537 iprog.ncommits = ncommits;
538 iprog.nobj_total = nobj_total;
539 iprog.nobj_deltify = nobj_deltify;
540 iprog.nobj_written = nobj_written;
542 /* Using synchronous writes since we are blocking the event loop. */
543 if (packfile_size == 0) {
544 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
545 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
547 return got_error_from_errno("imsg compose "
548 "PACKFILE_PROGRESS");
552 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
553 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
555 return got_error_from_errno("imsg compose "
560 return gotd_imsg_flush(a->ibuf);
563 static const struct got_error *
564 receive_delta_cache_fd(struct imsg *imsg,
565 struct gotd_imsgev *iev)
567 struct repo_read_client *client = &repo_read_client;
568 struct gotd_imsg_send_packfile ireq;
571 log_debug("receving delta cache file");
574 return got_error(GOT_ERR_PRIVSEP_NO_FD);
576 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
577 if (datalen != sizeof(ireq))
578 return got_error(GOT_ERR_PRIVSEP_LEN);
579 memcpy(&ireq, imsg->data, sizeof(ireq));
581 if (client->delta_cache_fd != -1)
582 return got_error(GOT_ERR_PRIVSEP_MSG);
584 client->delta_cache_fd = imsg->fd;
585 client->report_progress = ireq.report_progress;
589 static const struct got_error *
590 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
592 struct repo_read_client *client = &repo_read_client;
593 struct gotd_imsg_packfile_pipe ireq;
596 log_debug("receving pack pipe descriptor");
599 return got_error(GOT_ERR_PRIVSEP_NO_FD);
601 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
602 if (datalen != sizeof(ireq))
603 return got_error(GOT_ERR_PRIVSEP_LEN);
604 memcpy(&ireq, imsg->data, sizeof(ireq));
606 if (client->pack_pipe != -1)
607 return got_error(GOT_ERR_PRIVSEP_MSG);
609 client->pack_pipe = imsg->fd;
613 static const struct got_error *
614 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
616 const struct got_error *err = NULL;
617 struct repo_read_client *client = &repo_read_client;
618 struct gotd_imsg_packfile_done idone;
619 uint8_t packsha1[SHA1_DIGEST_LENGTH];
620 char hex[SHA1_DIGEST_STRING_LENGTH];
621 FILE *delta_cache = NULL;
623 struct repo_read_pack_progress_arg pa;
624 struct got_ratelimit rl;
626 log_debug("packfile request received");
628 got_ratelimit_init(&rl, 2, 0);
630 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
631 return got_error(GOT_ERR_PRIVSEP_NO_FD);
633 imsg_init(&ibuf, client->fd);
635 delta_cache = fdopen(client->delta_cache_fd, "w+");
636 if (delta_cache == NULL) {
637 err = got_error_from_errno("fdopen");
640 client->delta_cache_fd = -1;
642 memset(&pa, 0, sizeof(pa));
644 pa.report_progress = client->report_progress;
646 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
647 client->have_ids.ids, client->have_ids.nids,
648 client->want_ids.ids, client->want_ids.nids,
649 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
650 check_cancelled, NULL);
654 if (log_getverbose() > 0 &&
655 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
656 log_debug("sent pack-%s.pack", hex);
658 memset(&idone, 0, sizeof(idone));
659 idone.client_id = client->id;
660 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
661 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
662 err = got_error_from_errno("imsg compose PACKFILE_DONE");
664 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
665 err = got_error_from_errno("fclose");
671 repo_read_dispatch_session(int fd, short event, void *arg)
673 const struct got_error *err = NULL;
674 struct gotd_imsgev *iev = arg;
675 struct imsgbuf *ibuf = &iev->ibuf;
679 struct repo_read_client *client = &repo_read_client;
681 if (event & EV_READ) {
682 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
683 fatal("imsg_read error");
684 if (n == 0) /* Connection closed. */
688 if (event & EV_WRITE) {
689 n = msgbuf_write(&ibuf->w);
690 if (n == -1 && errno != EAGAIN)
691 fatal("msgbuf_write");
692 if (n == 0) /* Connection closed. */
696 while (err == NULL && check_cancelled(NULL) == NULL) {
697 if ((n = imsg_get(ibuf, &imsg)) == -1)
698 fatal("%s: imsg_get", __func__);
699 if (n == 0) /* No more messages. */
702 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
704 err = got_error(GOT_ERR_PRIVSEP_MSG);
708 switch (imsg.hdr.type) {
709 case GOTD_IMSG_LIST_REFS_INTERNAL:
710 err = list_refs(&imsg);
712 log_warnx("%s: ls-refs: %s", repo_read.title,
716 err = recv_want(&imsg);
718 log_warnx("%s: want-line: %s", repo_read.title,
722 err = recv_have(&imsg);
724 log_warnx("%s: have-line: %s", repo_read.title,
727 case GOTD_IMSG_SEND_PACKFILE:
728 err = receive_delta_cache_fd(&imsg, iev);
730 log_warnx("%s: receiving delta cache: %s",
731 repo_read.title, err->msg);
733 case GOTD_IMSG_PACKFILE_PIPE:
734 err = receive_pack_pipe(&imsg, iev);
736 log_warnx("%s: receiving pack pipe: %s",
737 repo_read.title, err->msg);
740 err = send_packfile(&imsg, iev);
742 log_warnx("%s: sending packfile: %s",
743 repo_read.title, err->msg);
746 log_debug("%s: unexpected imsg %d", repo_read.title,
754 if (!shut && check_cancelled(NULL) == NULL) {
756 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
757 client->id, err) == -1) {
758 log_warnx("could not send error to parent: %s",
761 gotd_imsg_event_add(iev);
763 /* This pipe is dead. Remove its event handler */
765 event_loopexit(NULL);
769 static const struct got_error *
770 recv_connect(struct imsg *imsg)
772 struct gotd_imsgev *iev = &repo_read.session_iev;
775 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 return got_error(GOT_ERR_PRIVSEP_LEN);
779 return got_error(GOT_ERR_PRIVSEP_NO_FD);
781 if (repo_read.session_fd != -1)
782 return got_error(GOT_ERR_PRIVSEP_MSG);
784 repo_read.session_fd = imsg->fd;
786 imsg_init(&iev->ibuf, repo_read.session_fd);
787 iev->handler = repo_read_dispatch_session;
788 iev->events = EV_READ;
789 iev->handler_arg = NULL;
790 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
791 repo_read_dispatch_session, iev);
792 gotd_imsg_event_add(iev);
798 repo_read_dispatch(int fd, short event, void *arg)
800 const struct got_error *err = NULL;
801 struct gotd_imsgev *iev = arg;
802 struct imsgbuf *ibuf = &iev->ibuf;
806 struct repo_read_client *client = &repo_read_client;
808 if (event & EV_READ) {
809 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
810 fatal("imsg_read error");
811 if (n == 0) /* Connection closed. */
815 if (event & EV_WRITE) {
816 n = msgbuf_write(&ibuf->w);
817 if (n == -1 && errno != EAGAIN)
818 fatal("msgbuf_write");
819 if (n == 0) /* Connection closed. */
823 while (err == NULL && check_cancelled(NULL) == NULL) {
824 if ((n = imsg_get(ibuf, &imsg)) == -1)
825 fatal("%s: imsg_get", __func__);
826 if (n == 0) /* No more messages. */
829 switch (imsg.hdr.type) {
830 case GOTD_IMSG_CONNECT_REPO_CHILD:
831 err = recv_connect(&imsg);
834 log_debug("%s: unexpected imsg %d", repo_read.title,
842 if (!shut && check_cancelled(NULL) == NULL) {
844 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
845 client->id, err) == -1) {
846 log_warnx("could not send error to parent: %s",
849 gotd_imsg_event_add(iev);
851 /* This pipe is dead. Remove its event handler */
853 event_loopexit(NULL);
858 repo_read_main(const char *title, const char *repo_path,
859 int *pack_fds, int *temp_fds)
861 const struct got_error *err = NULL;
862 struct repo_read_client *client = &repo_read_client;
863 struct gotd_imsgev iev;
866 client->delta_cache_fd = -1;
867 client->pack_pipe = -1;
869 repo_read.title = title;
870 repo_read.pid = getpid();
871 repo_read.pack_fds = pack_fds;
872 repo_read.temp_fds = temp_fds;
873 repo_read.session_fd = -1;
874 repo_read.session_iev.ibuf.fd = -1;
876 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
879 if (!got_repo_is_bare(repo_read.repo)) {
880 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
881 "bare git repository required");
885 got_repo_temp_fds_set(repo_read.repo, temp_fds);
887 signal(SIGINT, catch_sigint);
888 signal(SIGTERM, catch_sigterm);
889 signal(SIGPIPE, SIG_IGN);
890 signal(SIGHUP, SIG_IGN);
892 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
893 iev.handler = repo_read_dispatch;
894 iev.events = EV_READ;
895 iev.handler_arg = NULL;
896 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
898 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
899 PROC_REPO_READ, -1, NULL, 0) == -1) {
900 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
907 log_warnx("%s: %s", title, err->msg);
908 repo_read_shutdown();
912 repo_read_shutdown(void)
914 struct repo_read_client *client = &repo_read_client;
916 log_debug("%s: shutting down", repo_read.title);
918 free_object_ids(&client->have_ids);
919 free_object_ids(&client->want_ids);
920 if (client->fd != -1)
922 if (client->delta_cache_fd != -1)
923 close(client->delta_cache_fd);
924 if (client->pack_pipe != -1)
925 close(client->pack_pipe);
928 got_repo_close(repo_read.repo);
929 got_repo_pack_fds_close(repo_read.pack_fds);
930 got_repo_temp_fds_close(repo_read.temp_fds);
931 if (repo_read.session_fd != -1)
932 close(repo_read.session_fd);