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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <event.h>
23 #include <errno.h>
24 #include <imsg.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <poll.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
39 #include "got_path.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"
50 #include "log.h"
51 #include "gotd.h"
52 #include "repo_read.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 static struct repo_read {
59 pid_t pid;
60 const char *title;
61 struct got_repository *repo;
62 int *pack_fds;
63 int *temp_fds;
64 int session_fd;
65 struct gotd_imsgev session_iev;
66 int refs_listed;
67 } repo_read;
69 static struct repo_read_client {
70 uint32_t id;
71 int fd;
72 int delta_cache_fd;
73 int report_progress;
74 int pack_pipe;
75 struct got_object_idset *want_ids;
76 struct got_object_idset *have_ids;
77 } repo_read_client;
79 static volatile sig_atomic_t sigint_received;
80 static volatile sig_atomic_t sigterm_received;
82 static void
83 catch_sigint(int signo)
84 {
85 sigint_received = 1;
86 }
88 static void
89 catch_sigterm(int signo)
90 {
91 sigterm_received = 1;
92 }
94 static const struct got_error *
95 check_cancelled(void *arg)
96 {
97 if (sigint_received || sigterm_received)
98 return got_error(GOT_ERR_CANCELLED);
100 return NULL;
103 static const struct got_error *
104 send_symref(struct got_reference *symref, struct got_object_id *target_id,
105 struct imsgbuf *ibuf)
107 const struct got_error *err = NULL;
108 struct gotd_imsg_symref isymref;
109 const char *refname = got_ref_get_name(symref);
110 const char *target = got_ref_get_symref_target(symref);
111 size_t len;
112 struct ibuf *wbuf;
114 memset(&isymref, 0, sizeof(isymref));
115 isymref.name_len = strlen(refname);
116 isymref.target_len = strlen(target);
117 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
119 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
120 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
121 err = got_error(GOT_ERR_NO_SPACE);
122 goto done;
125 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
126 if (wbuf == NULL) {
127 err = got_error_from_errno("imsg_create SYMREF");
128 goto done;
131 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
132 err = got_error_from_errno("imsg_add SYMREF");
133 goto done;
135 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
136 err = got_error_from_errno("imsg_add SYMREF");
137 goto done;
139 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
140 err = got_error_from_errno("imsg_add SYMREF");
141 goto done;
144 imsg_close(ibuf, wbuf);
145 done:
146 free(target_id);
147 return err;
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;
156 size_t namelen, len;
157 char *peeled_refname = NULL;
158 struct got_object_id *id;
159 struct ibuf *wbuf;
161 err = got_object_tag_open(&tag, repo_read.repo, obj);
162 if (err)
163 return err;
165 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
166 err = got_error_from_errno("asprintf");
167 goto done;
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);
176 goto done;
179 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
180 repo_read.pid, len);
181 if (wbuf == NULL) {
182 err = got_error_from_errno("imsg_create MREF");
183 goto done;
186 /* Keep in sync with struct gotd_imsg_ref definition. */
187 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
188 err = got_error_from_errno("imsg_add REF");
189 goto done;
191 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
192 err = got_error_from_errno("imsg_add REF");
193 goto done;
195 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
196 err = got_error_from_errno("imsg_add REF");
197 goto done;
200 imsg_close(ibuf, wbuf);
201 done:
202 got_object_tag_close(tag);
203 return err;
206 static const struct got_error *
207 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
209 const struct got_error *err;
210 const char *refname = got_ref_get_name(ref);
211 size_t namelen;
212 struct got_object_id *id = NULL;
213 struct got_object *obj = NULL;
214 size_t len;
215 struct ibuf *wbuf;
217 namelen = strlen(refname);
219 len = sizeof(struct gotd_imsg_ref) + namelen;
220 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 return got_error(GOT_ERR_NO_SPACE);
223 err = got_ref_resolve(&id, repo_read.repo, ref);
224 if (err)
225 return err;
227 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
228 repo_read.pid, len);
229 if (wbuf == NULL) {
230 err = got_error_from_errno("imsg_create REF");
231 goto done;
234 /* Keep in sync with struct gotd_imsg_ref definition. */
235 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
236 return got_error_from_errno("imsg_add REF");
237 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf, refname, namelen) == -1)
240 return got_error_from_errno("imsg_add REF");
242 imsg_close(ibuf, wbuf);
244 err = got_object_open(&obj, repo_read.repo, id);
245 if (err)
246 goto done;
247 if (obj->type == GOT_OBJ_TYPE_TAG)
248 err = send_peeled_tag_ref(ref, obj, ibuf);
249 done:
250 if (obj)
251 got_object_close(obj);
252 free(id);
253 return err;
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 size_t datalen;
264 struct gotd_imsg_reflist irefs;
265 struct imsgbuf ibuf;
266 int client_fd;
267 struct got_object_id *head_target_id = NULL;
269 TAILQ_INIT(&refs);
271 client_fd = imsg_get_fd(imsg);
272 if (client_fd == -1)
273 return got_error(GOT_ERR_PRIVSEP_NO_FD);
275 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 if (datalen != 0)
277 return got_error(GOT_ERR_PRIVSEP_LEN);
279 if (repo_read.refs_listed) {
280 return got_error_msg(GOT_ERR_CLIENT_ID,
281 "duplicate list-refs request");
283 repo_read.refs_listed = 1;
285 client->fd = client_fd;
287 imsg_init(&ibuf, client_fd);
289 err = got_ref_list(&refs, repo_read.repo, "",
290 got_ref_cmp_by_name, NULL);
291 if (err)
292 return err;
294 memset(&irefs, 0, sizeof(irefs));
295 TAILQ_FOREACH(re, &refs, entry) {
296 struct got_object_id *id;
297 int obj_type;
299 if (got_ref_is_symbolic(re->ref)) {
300 const char *refname = got_ref_get_name(re->ref);
301 if (strcmp(refname, GOT_REF_HEAD) != 0)
302 continue;
303 err = got_ref_resolve(&head_target_id, repo_read.repo,
304 re->ref);
305 if (err) {
306 if (err->code != GOT_ERR_NOT_REF)
307 return err;
308 /*
309 * HEAD points to a non-existent branch.
310 * Do not advertise it.
311 * Matches git-daemon's behaviour.
312 */
313 head_target_id = NULL;
314 err = NULL;
315 } else
316 irefs.nrefs++;
317 continue;
320 irefs.nrefs++;
322 /* Account for a peeled tag refs. */
323 err = got_ref_resolve(&id, repo_read.repo, re->ref);
324 if (err)
325 goto done;
326 err = got_object_get_type(&obj_type, repo_read.repo, id);
327 free(id);
328 if (err)
329 goto done;
330 if (obj_type == GOT_OBJ_TYPE_TAG)
331 irefs.nrefs++;
334 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
335 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
336 err = got_error_from_errno("imsg_compose REFLIST");
337 goto done;
340 /*
341 * Send the HEAD symref first. In Git-protocol versions < 2
342 * the HEAD symref must be announced on the initial line of
343 * the server's ref advertisement.
344 * For now, we do not advertise symrefs other than HEAD.
345 */
346 TAILQ_FOREACH(re, &refs, entry) {
347 if (!got_ref_is_symbolic(re->ref) ||
348 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
349 head_target_id == NULL)
350 continue;
351 err = send_symref(re->ref, head_target_id, &ibuf);
352 if (err)
353 goto done;
354 break;
356 TAILQ_FOREACH(re, &refs, entry) {
357 if (got_ref_is_symbolic(re->ref))
358 continue;
359 err = send_ref(re->ref, &ibuf);
360 if (err)
361 goto done;
364 err = gotd_imsg_flush(&ibuf);
365 done:
366 got_ref_list_free(&refs);
367 imsg_clear(&ibuf);
368 return err;
371 static const struct got_error *
372 append_object_id(struct got_object_id *id, void *data, void *arg)
374 struct gotd_object_id_array *array = arg;
375 const size_t alloc_chunksz = 256;
377 if (array->ids == NULL) {
378 array->ids = reallocarray(NULL, alloc_chunksz,
379 sizeof(*array->ids));
380 if (array->ids == NULL)
381 return got_error_from_errno("reallocarray");
382 array->nalloc = alloc_chunksz;
383 array->nids = 0;
384 } else if (array->nalloc <= array->nids) {
385 struct got_object_id **new;
386 new = recallocarray(array->ids, array->nalloc,
387 array->nalloc + alloc_chunksz, sizeof(*new));
388 if (new == NULL)
389 return got_error_from_errno("recallocarray");
390 array->ids = new;
391 array->nalloc += alloc_chunksz;
394 array->ids[array->nids] = id;
395 array->nids++;
396 return NULL;
399 static const struct got_error *
400 recv_want(struct imsg *imsg)
402 const struct got_error *err;
403 struct repo_read_client *client = &repo_read_client;
404 struct gotd_imsg_want iwant;
405 size_t datalen;
406 char hex[SHA1_DIGEST_STRING_LENGTH];
407 struct got_object_id id;
408 int obj_type;
409 struct imsgbuf ibuf;
411 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
412 if (datalen != sizeof(iwant))
413 return got_error(GOT_ERR_PRIVSEP_LEN);
414 memcpy(&iwant, imsg->data, sizeof(iwant));
416 memset(&id, 0, sizeof(id));
417 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
419 if (log_getverbose() > 0 &&
420 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
421 log_debug("client wants %s", hex);
423 imsg_init(&ibuf, client->fd);
425 err = got_object_get_type(&obj_type, repo_read.repo, &id);
426 if (err)
427 return err;
429 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
430 obj_type != GOT_OBJ_TYPE_TAG)
431 return got_error(GOT_ERR_OBJ_TYPE);
433 if (!got_object_idset_contains(client->want_ids, &id)) {
434 err = got_object_idset_add(client->want_ids, &id, NULL);
435 if (err)
436 return err;
439 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
440 imsg_clear(&ibuf);
441 return err;
444 static const struct got_error *
445 recv_have(struct imsg *imsg)
447 const struct got_error *err;
448 struct repo_read_client *client = &repo_read_client;
449 struct gotd_imsg_have ihave;
450 size_t datalen;
451 char hex[SHA1_DIGEST_STRING_LENGTH];
452 struct got_object_id id;
453 int obj_type;
454 struct imsgbuf ibuf;
456 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
457 if (datalen != sizeof(ihave))
458 return got_error(GOT_ERR_PRIVSEP_LEN);
459 memcpy(&ihave, imsg->data, sizeof(ihave));
461 memset(&id, 0, sizeof(id));
462 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
464 if (log_getverbose() > 0 &&
465 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
466 log_debug("client has %s", hex);
468 imsg_init(&ibuf, client->fd);
470 err = got_object_get_type(&obj_type, repo_read.repo, &id);
471 if (err) {
472 if (err->code == GOT_ERR_NO_OBJ) {
473 gotd_imsg_send_nak(&id, &ibuf,
474 PROC_REPO_READ, repo_read.pid);
475 err = NULL;
477 goto done;
480 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
481 obj_type != GOT_OBJ_TYPE_TAG) {
482 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
483 err = got_error(GOT_ERR_OBJ_TYPE);
484 goto done;
487 if (!got_object_idset_contains(client->have_ids, &id)) {
488 err = got_object_idset_add(client->have_ids, &id, NULL);
489 if (err)
490 goto done;
493 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
494 done:
495 imsg_clear(&ibuf);
496 return err;
499 struct repo_read_pack_progress_arg {
500 int report_progress;
501 struct imsgbuf *ibuf;
502 int sent_ready;
503 };
505 static const struct got_error *
506 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
507 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
508 int nobj_written)
510 struct repo_read_pack_progress_arg *a = arg;
511 struct gotd_imsg_packfile_progress iprog;
512 int ret;
514 if (!a->report_progress)
515 return NULL;
516 if (packfile_size > 0 && a->sent_ready)
517 return NULL;
519 memset(&iprog, 0, sizeof(iprog));
520 iprog.ncolored = ncolored;
521 iprog.nfound = nfound;
522 iprog.ntrees = ntrees;
523 iprog.packfile_size = packfile_size;
524 iprog.ncommits = ncommits;
525 iprog.nobj_total = nobj_total;
526 iprog.nobj_deltify = nobj_deltify;
527 iprog.nobj_written = nobj_written;
529 /* Using synchronous writes since we are blocking the event loop. */
530 if (packfile_size == 0) {
531 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
532 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
533 if (ret == -1) {
534 return got_error_from_errno("imsg compose "
535 "PACKFILE_PROGRESS");
537 } else {
538 a->sent_ready = 1;
539 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
540 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
541 if (ret == -1) {
542 return got_error_from_errno("imsg compose "
543 "PACKFILE_READY");
547 return gotd_imsg_flush(a->ibuf);
550 static const struct got_error *
551 receive_delta_cache_fd(struct imsg *imsg,
552 struct gotd_imsgev *iev)
554 struct repo_read_client *client = &repo_read_client;
555 struct gotd_imsg_send_packfile ireq;
556 size_t datalen;
558 log_debug("receiving delta cache file");
560 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
561 if (datalen != sizeof(ireq))
562 return got_error(GOT_ERR_PRIVSEP_LEN);
563 memcpy(&ireq, imsg->data, sizeof(ireq));
565 if (client->delta_cache_fd != -1)
566 return got_error(GOT_ERR_PRIVSEP_MSG);
568 client->delta_cache_fd = imsg_get_fd(imsg);
569 if (client->delta_cache_fd == -1)
570 return got_error(GOT_ERR_PRIVSEP_NO_FD);
572 client->report_progress = ireq.report_progress;
573 return NULL;
576 static const struct got_error *
577 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
579 struct repo_read_client *client = &repo_read_client;
580 size_t datalen;
582 log_debug("receiving pack pipe descriptor");
584 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
585 if (datalen != 0)
586 return got_error(GOT_ERR_PRIVSEP_LEN);
588 if (client->pack_pipe != -1)
589 return got_error(GOT_ERR_PRIVSEP_MSG);
591 client->pack_pipe = imsg_get_fd(imsg);
592 if (client->pack_pipe == -1)
593 return got_error(GOT_ERR_PRIVSEP_NO_FD);
595 return NULL;
598 static const struct got_error *
599 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
601 const struct got_error *err = NULL;
602 struct repo_read_client *client = &repo_read_client;
603 uint8_t packsha1[SHA1_DIGEST_LENGTH];
604 char hex[SHA1_DIGEST_STRING_LENGTH];
605 FILE *delta_cache = NULL;
606 struct imsgbuf ibuf;
607 struct repo_read_pack_progress_arg pa;
608 struct got_ratelimit rl;
609 struct gotd_object_id_array want_ids;
610 struct gotd_object_id_array have_ids;
612 log_debug("packfile request received");
614 memset(&want_ids, 0, sizeof(want_ids));
615 memset(&have_ids, 0, sizeof(have_ids));
617 got_ratelimit_init(&rl, 2, 0);
619 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
620 return got_error(GOT_ERR_PRIVSEP_NO_FD);
622 imsg_init(&ibuf, client->fd);
624 delta_cache = fdopen(client->delta_cache_fd, "w+");
625 if (delta_cache == NULL) {
626 err = got_error_from_errno("fdopen");
627 goto done;
629 client->delta_cache_fd = -1;
631 memset(&pa, 0, sizeof(pa));
632 pa.ibuf = &ibuf;
633 pa.report_progress = client->report_progress;
635 err = got_object_idset_for_each(client->want_ids,
636 append_object_id, &want_ids);
637 if (err)
638 goto done;
639 err = got_object_idset_for_each(client->have_ids,
640 append_object_id, &have_ids);
641 if (err)
642 goto done;
644 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
645 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
646 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
647 check_cancelled, NULL);
648 if (err)
649 goto done;
651 if (log_getverbose() > 0 &&
652 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
653 log_debug("sent pack-%s.pack", hex);
655 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
656 PROC_REPO_READ, -1, NULL, 0) == -1)
657 err = got_error_from_errno("imsg compose PACKFILE_DONE");
658 done:
659 if (client->delta_cache_fd != -1 &&
660 close(client->delta_cache_fd) == -1 && err == NULL)
661 err = got_error_from_errno("close");
662 client->delta_cache_fd = -1;
663 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
664 err = got_error_from_errno("fclose");
665 imsg_clear(&ibuf);
666 free(want_ids.ids);
667 free(have_ids.ids);
668 return err;
671 static void
672 repo_read_dispatch_session(int fd, short event, void *arg)
674 const struct got_error *err = NULL;
675 struct gotd_imsgev *iev = arg;
676 struct imsgbuf *ibuf = &iev->ibuf;
677 struct imsg imsg;
678 ssize_t n;
679 int shut = 0;
680 struct repo_read_client *client = &repo_read_client;
682 if (event & EV_READ) {
683 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
684 fatal("imsg_read error");
685 if (n == 0) /* Connection closed. */
686 shut = 1;
689 if (event & EV_WRITE) {
690 n = msgbuf_write(&ibuf->w);
691 if (n == -1 && errno != EAGAIN)
692 fatal("msgbuf_write");
693 if (n == 0) /* Connection closed. */
694 shut = 1;
697 while (err == NULL && check_cancelled(NULL) == NULL) {
698 if ((n = imsg_get(ibuf, &imsg)) == -1)
699 fatal("%s: imsg_get", __func__);
700 if (n == 0) /* No more messages. */
701 break;
703 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
704 !repo_read.refs_listed) {
705 err = got_error(GOT_ERR_PRIVSEP_MSG);
706 break;
709 switch (imsg.hdr.type) {
710 case GOTD_IMSG_LIST_REFS_INTERNAL:
711 err = list_refs(&imsg);
712 if (err)
713 log_warnx("ls-refs: %s", err->msg);
714 break;
715 case GOTD_IMSG_WANT:
716 err = recv_want(&imsg);
717 if (err)
718 log_warnx("want-line: %s", err->msg);
719 break;
720 case GOTD_IMSG_HAVE:
721 err = recv_have(&imsg);
722 if (err)
723 log_warnx("have-line: %s", err->msg);
724 break;
725 case GOTD_IMSG_SEND_PACKFILE:
726 err = receive_delta_cache_fd(&imsg, iev);
727 if (err)
728 log_warnx("receiving delta cache: %s",
729 err->msg);
730 break;
731 case GOTD_IMSG_PACKFILE_PIPE:
732 err = receive_pack_pipe(&imsg, iev);
733 if (err) {
734 log_warnx("receiving pack pipe: %s", err->msg);
735 break;
737 err = send_packfile(&imsg, iev);
738 if (err)
739 log_warnx("sending packfile: %s", err->msg);
740 break;
741 default:
742 log_debug("unexpected imsg %d", imsg.hdr.type);
743 break;
746 imsg_free(&imsg);
749 if (!shut && check_cancelled(NULL) == NULL) {
750 if (err &&
751 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
752 client->id, err) == -1) {
753 log_warnx("could not send error to parent: %s",
754 err->msg);
756 gotd_imsg_event_add(iev);
757 } else {
758 /* This pipe is dead. Remove its event handler */
759 event_del(&iev->ev);
760 event_loopexit(NULL);
764 static const struct got_error *
765 recv_connect(struct imsg *imsg)
767 struct gotd_imsgev *iev = &repo_read.session_iev;
768 size_t datalen;
770 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
771 if (datalen != 0)
772 return got_error(GOT_ERR_PRIVSEP_LEN);
774 if (repo_read.session_fd != -1)
775 return got_error(GOT_ERR_PRIVSEP_MSG);
777 repo_read.session_fd = imsg_get_fd(imsg);
778 if (repo_read.session_fd == -1)
779 return got_error(GOT_ERR_PRIVSEP_NO_FD);
781 imsg_init(&iev->ibuf, repo_read.session_fd);
782 iev->handler = repo_read_dispatch_session;
783 iev->events = EV_READ;
784 iev->handler_arg = NULL;
785 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
786 repo_read_dispatch_session, iev);
787 gotd_imsg_event_add(iev);
789 return NULL;
792 static void
793 repo_read_dispatch(int fd, short event, void *arg)
795 const struct got_error *err = NULL;
796 struct gotd_imsgev *iev = arg;
797 struct imsgbuf *ibuf = &iev->ibuf;
798 struct imsg imsg;
799 ssize_t n;
800 int shut = 0;
801 struct repo_read_client *client = &repo_read_client;
803 if (event & EV_READ) {
804 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
805 fatal("imsg_read error");
806 if (n == 0) /* Connection closed. */
807 shut = 1;
810 if (event & EV_WRITE) {
811 n = msgbuf_write(&ibuf->w);
812 if (n == -1 && errno != EAGAIN)
813 fatal("msgbuf_write");
814 if (n == 0) /* Connection closed. */
815 shut = 1;
818 while (err == NULL && check_cancelled(NULL) == NULL) {
819 if ((n = imsg_get(ibuf, &imsg)) == -1)
820 fatal("%s: imsg_get", __func__);
821 if (n == 0) /* No more messages. */
822 break;
824 switch (imsg.hdr.type) {
825 case GOTD_IMSG_CONNECT_REPO_CHILD:
826 err = recv_connect(&imsg);
827 break;
828 default:
829 log_debug("unexpected imsg %d", imsg.hdr.type);
830 break;
833 imsg_free(&imsg);
836 if (!shut && check_cancelled(NULL) == NULL) {
837 if (err &&
838 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
839 client->id, err) == -1) {
840 log_warnx("could not send error to parent: %s",
841 err->msg);
843 gotd_imsg_event_add(iev);
844 } else {
845 /* This pipe is dead. Remove its event handler */
846 event_del(&iev->ev);
847 event_loopexit(NULL);
851 void
852 repo_read_main(const char *title, const char *repo_path,
853 int *pack_fds, int *temp_fds)
855 const struct got_error *err = NULL;
856 struct repo_read_client *client = &repo_read_client;
857 struct gotd_imsgev iev;
859 client->fd = -1;
860 client->delta_cache_fd = -1;
861 client->pack_pipe = -1;
862 client->have_ids = got_object_idset_alloc();
863 if (client->have_ids == NULL) {
864 err = got_error_from_errno("got_object_idset_alloc");
865 goto done;
867 client->want_ids = got_object_idset_alloc();
868 if (client->want_ids == NULL) {
869 err = got_error_from_errno("got_object_idset_alloc");
870 goto done;
873 repo_read.title = title;
874 repo_read.pid = getpid();
875 repo_read.pack_fds = pack_fds;
876 repo_read.temp_fds = temp_fds;
877 repo_read.session_fd = -1;
878 repo_read.session_iev.ibuf.fd = -1;
880 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
881 if (err)
882 goto done;
883 if (!got_repo_is_bare(repo_read.repo)) {
884 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
885 "bare git repository required");
886 goto done;
889 got_repo_temp_fds_set(repo_read.repo, temp_fds);
891 signal(SIGINT, catch_sigint);
892 signal(SIGTERM, catch_sigterm);
893 signal(SIGPIPE, SIG_IGN);
894 signal(SIGHUP, SIG_IGN);
896 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
897 iev.handler = repo_read_dispatch;
898 iev.events = EV_READ;
899 iev.handler_arg = NULL;
900 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
902 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
903 PROC_REPO_READ, -1, NULL, 0) == -1) {
904 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
905 goto done;
908 event_dispatch();
909 done:
910 if (err)
911 log_warnx("%s: %s", title, err->msg);
912 repo_read_shutdown();
915 void
916 repo_read_shutdown(void)
918 struct repo_read_client *client = &repo_read_client;
920 log_debug("%s: shutting down", repo_read.title);
922 if (client->have_ids)
923 got_object_idset_free(client->have_ids);
924 if (client->want_ids)
925 got_object_idset_free(client->want_ids);
926 if (client->fd != -1)
927 close(client->fd);
928 if (client->delta_cache_fd != -1)
929 close(client->delta_cache_fd);
930 if (client->pack_pipe != -1)
931 close(client->pack_pipe);
933 if (repo_read.repo)
934 got_repo_close(repo_read.repo);
935 got_repo_pack_fds_close(repo_read.pack_fds);
936 got_repo_temp_fds_close(repo_read.temp_fds);
937 if (repo_read.session_fd != -1)
938 close(repo_read.session_fd);
939 exit(0);