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/types.h>
20 #include <event.h>
21 #include <errno.h>
22 #include <imsg.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <poll.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <siphash.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.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"
40 #include "got_path.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_ratelimit.h"
48 #include "got_lib_pack_create.h"
49 #include "got_lib_poll.h"
51 #include "log.h"
52 #include "gotd.h"
53 #include "repo_read.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static struct repo_read {
60 pid_t pid;
61 const char *title;
62 struct got_repository *repo;
63 int *pack_fds;
64 int *temp_fds;
65 int session_fd;
66 struct gotd_imsgev session_iev;
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 wbuf->fd = -1;
145 imsg_close(ibuf, wbuf);
146 done:
147 free(target_id);
148 return err;
151 static const struct got_error *
152 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
153 struct imsgbuf *ibuf)
155 const struct got_error *err = NULL;
156 struct got_tag_object *tag;
157 size_t namelen, len;
158 char *peeled_refname = NULL;
159 struct got_object_id *id;
160 struct ibuf *wbuf;
162 err = got_object_tag_open(&tag, repo_read.repo, obj);
163 if (err)
164 return err;
166 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
167 err = got_error_from_errno("asprintf");
168 goto done;
171 id = got_object_tag_get_object_id(tag);
172 namelen = strlen(peeled_refname);
174 len = sizeof(struct gotd_imsg_ref) + namelen;
175 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
176 err = got_error(GOT_ERR_NO_SPACE);
177 goto done;
180 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
181 repo_read.pid, len);
182 if (wbuf == NULL) {
183 err = got_error_from_errno("imsg_create MREF");
184 goto done;
187 /* Keep in sync with struct gotd_imsg_ref definition. */
188 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
189 err = got_error_from_errno("imsg_add REF");
190 goto done;
192 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
193 err = got_error_from_errno("imsg_add REF");
194 goto done;
196 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
197 err = got_error_from_errno("imsg_add REF");
198 goto done;
201 wbuf->fd = -1;
202 imsg_close(ibuf, wbuf);
203 done:
204 got_object_tag_close(tag);
205 return err;
208 static const struct got_error *
209 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
211 const struct got_error *err;
212 const char *refname = got_ref_get_name(ref);
213 size_t namelen;
214 struct got_object_id *id = NULL;
215 struct got_object *obj = NULL;
216 size_t len;
217 struct ibuf *wbuf;
219 namelen = strlen(refname);
221 len = sizeof(struct gotd_imsg_ref) + namelen;
222 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
223 return got_error(GOT_ERR_NO_SPACE);
225 err = got_ref_resolve(&id, repo_read.repo, ref);
226 if (err)
227 return err;
229 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
230 repo_read.pid, len);
231 if (wbuf == NULL) {
232 err = got_error_from_errno("imsg_create REF");
233 goto done;
236 /* Keep in sync with struct gotd_imsg_ref definition. */
237 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
240 return got_error_from_errno("imsg_add REF");
241 if (imsg_add(wbuf, refname, namelen) == -1)
242 return got_error_from_errno("imsg_add REF");
244 wbuf->fd = -1;
245 imsg_close(ibuf, wbuf);
247 err = got_object_open(&obj, repo_read.repo, id);
248 if (err)
249 goto done;
250 if (obj->type == GOT_OBJ_TYPE_TAG)
251 err = send_peeled_tag_ref(ref, obj, ibuf);
252 done:
253 if (obj)
254 got_object_close(obj);
255 free(id);
256 return err;
259 static const struct got_error *
260 list_refs(struct imsg *imsg)
262 const struct got_error *err;
263 struct repo_read_client *client = &repo_read_client;
264 struct got_reflist_head refs;
265 struct got_reflist_entry *re;
266 struct gotd_imsg_list_refs_internal ireq;
267 size_t datalen;
268 struct gotd_imsg_reflist irefs;
269 struct imsgbuf ibuf;
270 int client_fd;
271 struct got_object_id *head_target_id = NULL;
273 TAILQ_INIT(&refs);
275 client_fd = imsg_get_fd(imsg);
276 if (client_fd == -1)
277 return got_error(GOT_ERR_PRIVSEP_NO_FD);
279 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
280 if (datalen != sizeof(ireq))
281 return got_error(GOT_ERR_PRIVSEP_LEN);
282 memcpy(&ireq, imsg->data, sizeof(ireq));
284 if (ireq.client_id == 0)
285 return got_error(GOT_ERR_CLIENT_ID);
286 if (client->id != 0) {
287 return got_error_msg(GOT_ERR_CLIENT_ID,
288 "duplicate list-refs request");
290 client->id = ireq.client_id;
291 client->fd = client_fd;
293 imsg_init(&ibuf, client_fd);
295 err = got_ref_list(&refs, repo_read.repo, "",
296 got_ref_cmp_by_name, NULL);
297 if (err)
298 return err;
300 memset(&irefs, 0, sizeof(irefs));
301 TAILQ_FOREACH(re, &refs, entry) {
302 struct got_object_id *id;
303 int obj_type;
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)
308 continue;
309 err = got_ref_resolve(&head_target_id, repo_read.repo,
310 re->ref);
311 if (err) {
312 if (err->code != GOT_ERR_NOT_REF)
313 return err;
314 /*
315 * HEAD points to a non-existent branch.
316 * Do not advertise it.
317 * Matches git-daemon's behaviour.
318 */
319 head_target_id = NULL;
320 err = NULL;
321 } else
322 irefs.nrefs++;
323 continue;
326 irefs.nrefs++;
328 /* Account for a peeled tag refs. */
329 err = got_ref_resolve(&id, repo_read.repo, re->ref);
330 if (err)
331 goto done;
332 err = got_object_get_type(&obj_type, repo_read.repo, id);
333 free(id);
334 if (err)
335 goto done;
336 if (obj_type == GOT_OBJ_TYPE_TAG)
337 irefs.nrefs++;
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");
343 goto done;
346 /*
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.
351 */
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)
356 continue;
357 err = send_symref(re->ref, head_target_id, &ibuf);
358 if (err)
359 goto done;
360 break;
362 TAILQ_FOREACH(re, &refs, entry) {
363 if (got_ref_is_symbolic(re->ref))
364 continue;
365 err = send_ref(re->ref, &ibuf);
366 if (err)
367 goto done;
370 err = gotd_imsg_flush(&ibuf);
371 done:
372 got_ref_list_free(&refs);
373 imsg_clear(&ibuf);
374 return err;
377 static const struct got_error *
378 append_object_id(struct got_object_id *id, void *data, void *arg)
380 struct gotd_object_id_array *array = arg;
381 const size_t alloc_chunksz = 256;
383 if (array->ids == NULL) {
384 array->ids = reallocarray(NULL, alloc_chunksz,
385 sizeof(*array->ids));
386 if (array->ids == NULL)
387 return got_error_from_errno("reallocarray");
388 array->nalloc = alloc_chunksz;
389 array->nids = 0;
390 } else if (array->nalloc <= array->nids) {
391 struct got_object_id **new;
392 new = recallocarray(array->ids, array->nalloc,
393 array->nalloc + alloc_chunksz, sizeof(*new));
394 if (new == NULL)
395 return got_error_from_errno("recallocarray");
396 array->ids = new;
397 array->nalloc += alloc_chunksz;
400 array->ids[array->nids] = id;
401 array->nids++;
402 return NULL;
405 static const struct got_error *
406 recv_want(struct imsg *imsg)
408 const struct got_error *err;
409 struct repo_read_client *client = &repo_read_client;
410 struct gotd_imsg_want iwant;
411 size_t datalen;
412 char hex[SHA1_DIGEST_STRING_LENGTH];
413 struct got_object_id id;
414 int obj_type;
415 struct imsgbuf ibuf;
417 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
418 if (datalen != sizeof(iwant))
419 return got_error(GOT_ERR_PRIVSEP_LEN);
420 memcpy(&iwant, imsg->data, sizeof(iwant));
422 memset(&id, 0, sizeof(id));
423 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
425 if (log_getverbose() > 0 &&
426 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
427 log_debug("client wants %s", hex);
429 imsg_init(&ibuf, client->fd);
431 err = got_object_get_type(&obj_type, repo_read.repo, &id);
432 if (err)
433 return err;
435 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
436 obj_type != GOT_OBJ_TYPE_TAG)
437 return got_error(GOT_ERR_OBJ_TYPE);
439 if (!got_object_idset_contains(client->want_ids, &id)) {
440 err = got_object_idset_add(client->want_ids, &id, NULL);
441 if (err)
442 return err;
445 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
446 imsg_clear(&ibuf);
447 return err;
450 static const struct got_error *
451 recv_have(struct imsg *imsg)
453 const struct got_error *err;
454 struct repo_read_client *client = &repo_read_client;
455 struct gotd_imsg_have ihave;
456 size_t datalen;
457 char hex[SHA1_DIGEST_STRING_LENGTH];
458 struct got_object_id id;
459 int obj_type;
460 struct imsgbuf ibuf;
462 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
463 if (datalen != sizeof(ihave))
464 return got_error(GOT_ERR_PRIVSEP_LEN);
465 memcpy(&ihave, imsg->data, sizeof(ihave));
467 memset(&id, 0, sizeof(id));
468 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
470 if (log_getverbose() > 0 &&
471 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
472 log_debug("client has %s", hex);
474 imsg_init(&ibuf, client->fd);
476 err = got_object_get_type(&obj_type, repo_read.repo, &id);
477 if (err) {
478 if (err->code == GOT_ERR_NO_OBJ) {
479 gotd_imsg_send_nak(&id, &ibuf,
480 PROC_REPO_READ, repo_read.pid);
481 err = NULL;
483 goto done;
486 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
487 obj_type != GOT_OBJ_TYPE_TAG) {
488 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
489 err = got_error(GOT_ERR_OBJ_TYPE);
490 goto done;
493 if (!got_object_idset_contains(client->have_ids, &id)) {
494 err = got_object_idset_add(client->have_ids, &id, NULL);
495 if (err)
496 goto done;
499 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
500 done:
501 imsg_clear(&ibuf);
502 return err;
505 struct repo_read_pack_progress_arg {
506 int report_progress;
507 struct imsgbuf *ibuf;
508 int sent_ready;
509 };
511 static const struct got_error *
512 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
513 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
514 int nobj_written)
516 struct repo_read_pack_progress_arg *a = arg;
517 struct gotd_imsg_packfile_progress iprog;
518 int ret;
520 if (!a->report_progress)
521 return NULL;
522 if (packfile_size > 0 && a->sent_ready)
523 return NULL;
525 memset(&iprog, 0, sizeof(iprog));
526 iprog.ncolored = ncolored;
527 iprog.nfound = nfound;
528 iprog.ntrees = ntrees;
529 iprog.packfile_size = packfile_size;
530 iprog.ncommits = ncommits;
531 iprog.nobj_total = nobj_total;
532 iprog.nobj_deltify = nobj_deltify;
533 iprog.nobj_written = nobj_written;
535 /* Using synchronous writes since we are blocking the event loop. */
536 if (packfile_size == 0) {
537 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
538 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
539 if (ret == -1) {
540 return got_error_from_errno("imsg compose "
541 "PACKFILE_PROGRESS");
543 } else {
544 a->sent_ready = 1;
545 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
546 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
547 if (ret == -1) {
548 return got_error_from_errno("imsg compose "
549 "PACKFILE_READY");
553 return gotd_imsg_flush(a->ibuf);
556 static const struct got_error *
557 receive_delta_cache_fd(struct imsg *imsg,
558 struct gotd_imsgev *iev)
560 struct repo_read_client *client = &repo_read_client;
561 struct gotd_imsg_send_packfile ireq;
562 size_t datalen;
564 log_debug("receiving delta cache file");
566 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
567 if (datalen != sizeof(ireq))
568 return got_error(GOT_ERR_PRIVSEP_LEN);
569 memcpy(&ireq, imsg->data, sizeof(ireq));
571 if (client->delta_cache_fd != -1)
572 return got_error(GOT_ERR_PRIVSEP_MSG);
574 client->delta_cache_fd = imsg_get_fd(imsg);
575 if (client->delta_cache_fd == -1)
576 return got_error(GOT_ERR_PRIVSEP_NO_FD);
578 client->report_progress = ireq.report_progress;
579 return NULL;
582 static const struct got_error *
583 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
585 struct repo_read_client *client = &repo_read_client;
586 struct gotd_imsg_packfile_pipe ireq;
587 size_t datalen;
589 log_debug("receiving pack pipe descriptor");
591 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
592 if (datalen != sizeof(ireq))
593 return got_error(GOT_ERR_PRIVSEP_LEN);
594 memcpy(&ireq, imsg->data, sizeof(ireq));
596 if (client->pack_pipe != -1)
597 return got_error(GOT_ERR_PRIVSEP_MSG);
599 client->pack_pipe = imsg_get_fd(imsg);
600 if (client->pack_pipe == -1)
601 return got_error(GOT_ERR_PRIVSEP_NO_FD);
603 return NULL;
606 static const struct got_error *
607 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
609 const struct got_error *err = NULL;
610 struct repo_read_client *client = &repo_read_client;
611 struct gotd_imsg_packfile_done idone;
612 uint8_t packsha1[SHA1_DIGEST_LENGTH];
613 char hex[SHA1_DIGEST_STRING_LENGTH];
614 FILE *delta_cache = NULL;
615 struct imsgbuf ibuf;
616 struct repo_read_pack_progress_arg pa;
617 struct got_ratelimit rl;
618 struct gotd_object_id_array want_ids;
619 struct gotd_object_id_array have_ids;
621 log_debug("packfile request received");
623 memset(&want_ids, 0, sizeof(want_ids));
624 memset(&have_ids, 0, sizeof(have_ids));
626 got_ratelimit_init(&rl, 2, 0);
628 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
629 return got_error(GOT_ERR_PRIVSEP_NO_FD);
631 imsg_init(&ibuf, client->fd);
633 delta_cache = fdopen(client->delta_cache_fd, "w+");
634 if (delta_cache == NULL) {
635 err = got_error_from_errno("fdopen");
636 goto done;
638 client->delta_cache_fd = -1;
640 memset(&pa, 0, sizeof(pa));
641 pa.ibuf = &ibuf;
642 pa.report_progress = client->report_progress;
644 err = got_object_idset_for_each(client->want_ids,
645 append_object_id, &want_ids);
646 if (err)
647 goto done;
648 err = got_object_idset_for_each(client->have_ids,
649 append_object_id, &have_ids);
650 if (err)
651 goto done;
653 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
654 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
655 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
656 check_cancelled, NULL);
657 if (err)
658 goto done;
660 if (log_getverbose() > 0 &&
661 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
662 log_debug("sent pack-%s.pack", hex);
664 memset(&idone, 0, sizeof(idone));
665 idone.client_id = client->id;
666 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
667 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
668 err = got_error_from_errno("imsg compose PACKFILE_DONE");
669 done:
670 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
672 imsg_clear(&ibuf);
673 free(want_ids.ids);
674 free(have_ids.ids);
675 return err;
678 static void
679 repo_read_dispatch_session(int fd, short event, void *arg)
681 const struct got_error *err = NULL;
682 struct gotd_imsgev *iev = arg;
683 struct imsgbuf *ibuf = &iev->ibuf;
684 struct imsg imsg;
685 ssize_t n;
686 int shut = 0;
687 struct repo_read_client *client = &repo_read_client;
689 if (event & EV_READ) {
690 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
691 fatal("imsg_read error");
692 if (n == 0) /* Connection closed. */
693 shut = 1;
696 if (event & EV_WRITE) {
697 n = msgbuf_write(&ibuf->w);
698 if (n == -1 && errno != EAGAIN)
699 fatal("msgbuf_write");
700 if (n == 0) /* Connection closed. */
701 shut = 1;
704 while (err == NULL && check_cancelled(NULL) == NULL) {
705 if ((n = imsg_get(ibuf, &imsg)) == -1)
706 fatal("%s: imsg_get", __func__);
707 if (n == 0) /* No more messages. */
708 break;
710 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
711 client->id == 0) {
712 err = got_error(GOT_ERR_PRIVSEP_MSG);
713 break;
716 switch (imsg.hdr.type) {
717 case GOTD_IMSG_LIST_REFS_INTERNAL:
718 err = list_refs(&imsg);
719 if (err)
720 log_warnx("ls-refs: %s", err->msg);
721 break;
722 case GOTD_IMSG_WANT:
723 err = recv_want(&imsg);
724 if (err)
725 log_warnx("want-line: %s", err->msg);
726 break;
727 case GOTD_IMSG_HAVE:
728 err = recv_have(&imsg);
729 if (err)
730 log_warnx("have-line: %s", err->msg);
731 break;
732 case GOTD_IMSG_SEND_PACKFILE:
733 err = receive_delta_cache_fd(&imsg, iev);
734 if (err)
735 log_warnx("receiving delta cache: %s",
736 err->msg);
737 break;
738 case GOTD_IMSG_PACKFILE_PIPE:
739 err = receive_pack_pipe(&imsg, iev);
740 if (err) {
741 log_warnx("receiving pack pipe: %s", err->msg);
742 break;
744 err = send_packfile(&imsg, iev);
745 if (err)
746 log_warnx("sending packfile: %s", err->msg);
747 break;
748 default:
749 log_debug("unexpected imsg %d", imsg.hdr.type);
750 break;
753 imsg_free(&imsg);
756 if (!shut && check_cancelled(NULL) == NULL) {
757 if (err &&
758 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
759 client->id, err) == -1) {
760 log_warnx("could not send error to parent: %s",
761 err->msg);
763 gotd_imsg_event_add(iev);
764 } else {
765 /* This pipe is dead. Remove its event handler */
766 event_del(&iev->ev);
767 event_loopexit(NULL);
771 static const struct got_error *
772 recv_connect(struct imsg *imsg)
774 struct gotd_imsgev *iev = &repo_read.session_iev;
775 size_t datalen;
777 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
778 if (datalen != 0)
779 return got_error(GOT_ERR_PRIVSEP_LEN);
781 if (repo_read.session_fd != -1)
782 return got_error(GOT_ERR_PRIVSEP_MSG);
784 repo_read.session_fd = imsg_get_fd(imsg);
785 if (repo_read.session_fd == -1)
786 return got_error(GOT_ERR_PRIVSEP_NO_FD);
788 imsg_init(&iev->ibuf, repo_read.session_fd);
789 iev->handler = repo_read_dispatch_session;
790 iev->events = EV_READ;
791 iev->handler_arg = NULL;
792 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
793 repo_read_dispatch_session, iev);
794 gotd_imsg_event_add(iev);
796 return NULL;
799 static void
800 repo_read_dispatch(int fd, short event, void *arg)
802 const struct got_error *err = NULL;
803 struct gotd_imsgev *iev = arg;
804 struct imsgbuf *ibuf = &iev->ibuf;
805 struct imsg imsg;
806 ssize_t n;
807 int shut = 0;
808 struct repo_read_client *client = &repo_read_client;
810 if (event & EV_READ) {
811 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
812 fatal("imsg_read error");
813 if (n == 0) /* Connection closed. */
814 shut = 1;
817 if (event & EV_WRITE) {
818 n = msgbuf_write(&ibuf->w);
819 if (n == -1 && errno != EAGAIN)
820 fatal("msgbuf_write");
821 if (n == 0) /* Connection closed. */
822 shut = 1;
825 while (err == NULL && check_cancelled(NULL) == NULL) {
826 if ((n = imsg_get(ibuf, &imsg)) == -1)
827 fatal("%s: imsg_get", __func__);
828 if (n == 0) /* No more messages. */
829 break;
831 switch (imsg.hdr.type) {
832 case GOTD_IMSG_CONNECT_REPO_CHILD:
833 err = recv_connect(&imsg);
834 break;
835 default:
836 log_debug("unexpected imsg %d", imsg.hdr.type);
837 break;
840 imsg_free(&imsg);
843 if (!shut && check_cancelled(NULL) == NULL) {
844 if (err &&
845 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
846 client->id, err) == -1) {
847 log_warnx("could not send error to parent: %s",
848 err->msg);
850 gotd_imsg_event_add(iev);
851 } else {
852 /* This pipe is dead. Remove its event handler */
853 event_del(&iev->ev);
854 event_loopexit(NULL);
858 void
859 repo_read_main(const char *title, const char *repo_path,
860 int *pack_fds, int *temp_fds)
862 const struct got_error *err = NULL;
863 struct repo_read_client *client = &repo_read_client;
864 struct gotd_imsgev iev;
866 client->fd = -1;
867 client->delta_cache_fd = -1;
868 client->pack_pipe = -1;
869 client->have_ids = got_object_idset_alloc();
870 if (client->have_ids == NULL) {
871 err = got_error_from_errno("got_object_idset_alloc");
872 goto done;
874 client->want_ids = got_object_idset_alloc();
875 if (client->want_ids == NULL) {
876 err = got_error_from_errno("got_object_idset_alloc");
877 goto done;
880 repo_read.title = title;
881 repo_read.pid = getpid();
882 repo_read.pack_fds = pack_fds;
883 repo_read.temp_fds = temp_fds;
884 repo_read.session_fd = -1;
885 repo_read.session_iev.ibuf.fd = -1;
887 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
888 if (err)
889 goto done;
890 if (!got_repo_is_bare(repo_read.repo)) {
891 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
892 "bare git repository required");
893 goto done;
896 got_repo_temp_fds_set(repo_read.repo, temp_fds);
898 signal(SIGINT, catch_sigint);
899 signal(SIGTERM, catch_sigterm);
900 signal(SIGPIPE, SIG_IGN);
901 signal(SIGHUP, SIG_IGN);
903 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
904 iev.handler = repo_read_dispatch;
905 iev.events = EV_READ;
906 iev.handler_arg = NULL;
907 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
909 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
910 PROC_REPO_READ, -1, NULL, 0) == -1) {
911 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
912 goto done;
915 event_dispatch();
916 done:
917 if (err)
918 log_warnx("%s: %s", title, err->msg);
919 repo_read_shutdown();
922 void
923 repo_read_shutdown(void)
925 struct repo_read_client *client = &repo_read_client;
927 log_debug("shutting down");
929 if (client->have_ids)
930 got_object_idset_free(client->have_ids);
931 if (client->want_ids)
932 got_object_idset_free(client->want_ids);
933 if (client->fd != -1)
934 close(client->fd);
935 if (client->delta_cache_fd != -1)
936 close(client->delta_cache_fd);
937 if (client->pack_pipe != -1)
938 close(client->pack_pipe);
940 if (repo_read.repo)
941 got_repo_close(repo_read.repo);
942 got_repo_pack_fds_close(repo_read.pack_fds);
943 got_repo_temp_fds_close(repo_read.temp_fds);
944 if (repo_read.session_fd != -1)
945 close(repo_read.session_fd);
946 exit(0);