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 <siphash.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.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"
48 #include "log.h"
49 #include "gotd.h"
50 #include "repo_read.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static struct repo_read {
57 pid_t pid;
58 const char *title;
59 struct got_repository *repo;
60 int *pack_fds;
61 int *temp_fds;
62 int session_fd;
63 struct gotd_imsgev session_iev;
64 } repo_read;
66 static struct repo_read_client {
67 uint32_t id;
68 int fd;
69 int delta_cache_fd;
70 int report_progress;
71 int pack_pipe;
72 struct got_object_idset *want_ids;
73 struct got_object_idset *have_ids;
74 } repo_read_client;
76 static volatile sig_atomic_t sigint_received;
77 static volatile sig_atomic_t sigterm_received;
79 static void
80 catch_sigint(int signo)
81 {
82 sigint_received = 1;
83 }
85 static void
86 catch_sigterm(int signo)
87 {
88 sigterm_received = 1;
89 }
91 static const struct got_error *
92 check_cancelled(void *arg)
93 {
94 if (sigint_received || sigterm_received)
95 return got_error(GOT_ERR_CANCELLED);
97 return NULL;
98 }
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);
108 size_t len;
109 struct ibuf *wbuf;
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);
119 goto done;
122 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
123 if (wbuf == NULL) {
124 err = got_error_from_errno("imsg_create SYMREF");
125 goto done;
128 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
129 err = got_error_from_errno("imsg_add SYMREF");
130 goto done;
132 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
134 goto done;
136 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
138 goto done;
141 wbuf->fd = -1;
142 imsg_close(ibuf, wbuf);
143 done:
144 free(target_id);
145 return err;
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;
154 size_t namelen, len;
155 char *peeled_refname = NULL;
156 struct got_object_id *id;
157 struct ibuf *wbuf;
159 err = got_object_tag_open(&tag, repo_read.repo, obj);
160 if (err)
161 return err;
163 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
164 err = got_error_from_errno("asprintf");
165 goto done;
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);
174 goto done;
177 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
178 repo_read.pid, len);
179 if (wbuf == NULL) {
180 err = got_error_from_errno("imsg_create MREF");
181 goto done;
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");
187 goto done;
189 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
190 err = got_error_from_errno("imsg_add REF");
191 goto done;
193 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
194 err = got_error_from_errno("imsg_add REF");
195 goto done;
198 wbuf->fd = -1;
199 imsg_close(ibuf, wbuf);
200 done:
201 got_object_tag_close(tag);
202 return err;
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);
210 size_t namelen;
211 struct got_object_id *id = NULL;
212 struct got_object *obj = NULL;
213 size_t len;
214 struct ibuf *wbuf;
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);
223 if (err)
224 return err;
226 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
227 repo_read.pid, len);
228 if (wbuf == NULL) {
229 err = got_error_from_errno("imsg_create REF");
230 goto done;
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");
241 wbuf->fd = -1;
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 struct gotd_imsg_list_refs_internal ireq;
264 size_t datalen;
265 struct gotd_imsg_reflist irefs;
266 struct imsgbuf ibuf;
267 int client_fd = imsg->fd;
268 struct got_object_id *head_target_id = NULL;
270 TAILQ_INIT(&refs);
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 != 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);
293 if (err)
294 return err;
296 memset(&irefs, 0, sizeof(irefs));
297 TAILQ_FOREACH(re, &refs, entry) {
298 struct got_object_id *id;
299 int obj_type;
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)
304 continue;
305 err = got_ref_resolve(&head_target_id, repo_read.repo,
306 re->ref);
307 if (err) {
308 if (err->code != GOT_ERR_NOT_REF)
309 return err;
310 /*
311 * HEAD points to a non-existent branch.
312 * Do not advertise it.
313 * Matches git-daemon's behaviour.
314 */
315 head_target_id = NULL;
316 err = NULL;
317 } else
318 irefs.nrefs++;
319 continue;
322 irefs.nrefs++;
324 /* Account for a peeled tag refs. */
325 err = got_ref_resolve(&id, repo_read.repo, re->ref);
326 if (err)
327 goto done;
328 err = got_object_get_type(&obj_type, repo_read.repo, id);
329 free(id);
330 if (err)
331 goto done;
332 if (obj_type == GOT_OBJ_TYPE_TAG)
333 irefs.nrefs++;
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");
339 goto done;
342 /*
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.
347 */
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)
352 continue;
353 err = send_symref(re->ref, head_target_id, &ibuf);
354 if (err)
355 goto done;
356 break;
358 TAILQ_FOREACH(re, &refs, entry) {
359 if (got_ref_is_symbolic(re->ref))
360 continue;
361 err = send_ref(re->ref, &ibuf);
362 if (err)
363 goto done;
366 err = gotd_imsg_flush(&ibuf);
367 done:
368 got_ref_list_free(&refs);
369 imsg_clear(&ibuf);
370 return err;
373 static const struct got_error *
374 append_object_id(struct got_object_id *id, void *data, void *arg)
376 struct gotd_object_id_array *array = arg;
377 const size_t alloc_chunksz = 256;
379 if (array->ids == NULL) {
380 array->ids = reallocarray(NULL, alloc_chunksz,
381 sizeof(*array->ids));
382 if (array->ids == NULL)
383 return got_error_from_errno("reallocarray");
384 array->nalloc = alloc_chunksz;
385 array->nids = 0;
386 } else if (array->nalloc <= array->nids) {
387 struct got_object_id **new;
388 new = recallocarray(array->ids, array->nalloc,
389 array->nalloc + alloc_chunksz, sizeof(*new));
390 if (new == NULL)
391 return got_error_from_errno("recallocarray");
392 array->ids = new;
393 array->nalloc += alloc_chunksz;
396 array->ids[array->nids] = id;
397 array->nids++;
398 return NULL;
401 static const struct got_error *
402 recv_want(struct imsg *imsg)
404 const struct got_error *err;
405 struct repo_read_client *client = &repo_read_client;
406 struct gotd_imsg_want iwant;
407 size_t datalen;
408 char hex[SHA1_DIGEST_STRING_LENGTH];
409 struct got_object_id id;
410 int obj_type;
411 struct imsgbuf ibuf;
413 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
414 if (datalen != sizeof(iwant))
415 return got_error(GOT_ERR_PRIVSEP_LEN);
416 memcpy(&iwant, imsg->data, sizeof(iwant));
418 memset(&id, 0, sizeof(id));
419 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
421 if (log_getverbose() > 0 &&
422 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
423 log_debug("client wants %s", hex);
425 imsg_init(&ibuf, client->fd);
427 err = got_object_get_type(&obj_type, repo_read.repo, &id);
428 if (err)
429 return err;
431 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
432 obj_type != GOT_OBJ_TYPE_TAG)
433 return got_error(GOT_ERR_OBJ_TYPE);
435 if (!got_object_idset_contains(client->want_ids, &id)) {
436 err = got_object_idset_add(client->want_ids, &id, NULL);
437 if (err)
438 return err;
441 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
442 imsg_clear(&ibuf);
443 return err;
446 static const struct got_error *
447 recv_have(struct imsg *imsg)
449 const struct got_error *err;
450 struct repo_read_client *client = &repo_read_client;
451 struct gotd_imsg_have ihave;
452 size_t datalen;
453 char hex[SHA1_DIGEST_STRING_LENGTH];
454 struct got_object_id id;
455 int obj_type;
456 struct imsgbuf ibuf;
458 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
459 if (datalen != sizeof(ihave))
460 return got_error(GOT_ERR_PRIVSEP_LEN);
461 memcpy(&ihave, imsg->data, sizeof(ihave));
463 memset(&id, 0, sizeof(id));
464 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
466 if (log_getverbose() > 0 &&
467 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
468 log_debug("client has %s", hex);
470 imsg_init(&ibuf, client->fd);
472 err = got_object_get_type(&obj_type, repo_read.repo, &id);
473 if (err) {
474 if (err->code == GOT_ERR_NO_OBJ) {
475 gotd_imsg_send_nak(&id, &ibuf,
476 PROC_REPO_READ, repo_read.pid);
477 err = NULL;
479 goto done;
482 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
483 obj_type != GOT_OBJ_TYPE_TAG) {
484 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
485 err = got_error(GOT_ERR_OBJ_TYPE);
486 goto done;
489 if (!got_object_idset_contains(client->have_ids, &id)) {
490 err = got_object_idset_add(client->have_ids, &id, NULL);
491 if (err)
492 goto done;
495 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
496 done:
497 imsg_clear(&ibuf);
498 return err;
501 struct repo_read_pack_progress_arg {
502 int report_progress;
503 struct imsgbuf *ibuf;
504 int sent_ready;
505 };
507 static const struct got_error *
508 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
509 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
510 int nobj_written)
512 struct repo_read_pack_progress_arg *a = arg;
513 struct gotd_imsg_packfile_progress iprog;
514 int ret;
516 if (!a->report_progress)
517 return NULL;
518 if (packfile_size > 0 && a->sent_ready)
519 return NULL;
521 memset(&iprog, 0, sizeof(iprog));
522 iprog.ncolored = ncolored;
523 iprog.nfound = nfound;
524 iprog.ntrees = ntrees;
525 iprog.packfile_size = packfile_size;
526 iprog.ncommits = ncommits;
527 iprog.nobj_total = nobj_total;
528 iprog.nobj_deltify = nobj_deltify;
529 iprog.nobj_written = nobj_written;
531 /* Using synchronous writes since we are blocking the event loop. */
532 if (packfile_size == 0) {
533 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
534 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
535 if (ret == -1) {
536 return got_error_from_errno("imsg compose "
537 "PACKFILE_PROGRESS");
539 } else {
540 a->sent_ready = 1;
541 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
542 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
543 if (ret == -1) {
544 return got_error_from_errno("imsg compose "
545 "PACKFILE_READY");
549 return gotd_imsg_flush(a->ibuf);
552 static const struct got_error *
553 receive_delta_cache_fd(struct imsg *imsg,
554 struct gotd_imsgev *iev)
556 struct repo_read_client *client = &repo_read_client;
557 struct gotd_imsg_send_packfile ireq;
558 size_t datalen;
560 log_debug("receving delta cache file");
562 if (imsg->fd == -1)
563 return got_error(GOT_ERR_PRIVSEP_NO_FD);
565 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
566 if (datalen != sizeof(ireq))
567 return got_error(GOT_ERR_PRIVSEP_LEN);
568 memcpy(&ireq, imsg->data, sizeof(ireq));
570 if (client->delta_cache_fd != -1)
571 return got_error(GOT_ERR_PRIVSEP_MSG);
573 client->delta_cache_fd = imsg->fd;
574 client->report_progress = ireq.report_progress;
575 return NULL;
578 static const struct got_error *
579 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
581 struct repo_read_client *client = &repo_read_client;
582 struct gotd_imsg_packfile_pipe ireq;
583 size_t datalen;
585 log_debug("receving pack pipe descriptor");
587 if (imsg->fd == -1)
588 return got_error(GOT_ERR_PRIVSEP_NO_FD);
590 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
591 if (datalen != sizeof(ireq))
592 return got_error(GOT_ERR_PRIVSEP_LEN);
593 memcpy(&ireq, imsg->data, sizeof(ireq));
595 if (client->pack_pipe != -1)
596 return got_error(GOT_ERR_PRIVSEP_MSG);
598 client->pack_pipe = imsg->fd;
599 return NULL;
602 static const struct got_error *
603 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
605 const struct got_error *err = NULL;
606 struct repo_read_client *client = &repo_read_client;
607 struct gotd_imsg_packfile_done idone;
608 uint8_t packsha1[SHA1_DIGEST_LENGTH];
609 char hex[SHA1_DIGEST_STRING_LENGTH];
610 FILE *delta_cache = NULL;
611 struct imsgbuf ibuf;
612 struct repo_read_pack_progress_arg pa;
613 struct got_ratelimit rl;
614 struct gotd_object_id_array want_ids;
615 struct gotd_object_id_array have_ids;
617 log_debug("packfile request received");
619 memset(&want_ids, 0, sizeof(want_ids));
620 memset(&have_ids, 0, sizeof(have_ids));
622 got_ratelimit_init(&rl, 2, 0);
624 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
625 return got_error(GOT_ERR_PRIVSEP_NO_FD);
627 imsg_init(&ibuf, client->fd);
629 delta_cache = fdopen(client->delta_cache_fd, "w+");
630 if (delta_cache == NULL) {
631 err = got_error_from_errno("fdopen");
632 goto done;
634 client->delta_cache_fd = -1;
636 memset(&pa, 0, sizeof(pa));
637 pa.ibuf = &ibuf;
638 pa.report_progress = client->report_progress;
640 err = got_object_idset_for_each(client->want_ids,
641 append_object_id, &want_ids);
642 if (err)
643 goto done;
644 err = got_object_idset_for_each(client->have_ids,
645 append_object_id, &have_ids);
646 if (err)
647 goto done;
649 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
650 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
651 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
652 check_cancelled, NULL);
653 if (err)
654 goto done;
656 if (log_getverbose() > 0 &&
657 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
658 log_debug("sent pack-%s.pack", hex);
660 memset(&idone, 0, sizeof(idone));
661 idone.client_id = client->id;
662 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
663 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
664 err = got_error_from_errno("imsg compose PACKFILE_DONE");
665 done:
666 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
667 err = got_error_from_errno("fclose");
668 imsg_clear(&ibuf);
669 free(want_ids.ids);
670 free(have_ids.ids);
671 return err;
674 static void
675 repo_read_dispatch_session(int fd, short event, void *arg)
677 const struct got_error *err = NULL;
678 struct gotd_imsgev *iev = arg;
679 struct imsgbuf *ibuf = &iev->ibuf;
680 struct imsg imsg;
681 ssize_t n;
682 int shut = 0;
683 struct repo_read_client *client = &repo_read_client;
685 if (event & EV_READ) {
686 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
687 fatal("imsg_read error");
688 if (n == 0) /* Connection closed. */
689 shut = 1;
692 if (event & EV_WRITE) {
693 n = msgbuf_write(&ibuf->w);
694 if (n == -1 && errno != EAGAIN)
695 fatal("msgbuf_write");
696 if (n == 0) /* Connection closed. */
697 shut = 1;
700 while (err == NULL && check_cancelled(NULL) == NULL) {
701 if ((n = imsg_get(ibuf, &imsg)) == -1)
702 fatal("%s: imsg_get", __func__);
703 if (n == 0) /* No more messages. */
704 break;
706 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
707 client->id == 0) {
708 err = got_error(GOT_ERR_PRIVSEP_MSG);
709 break;
712 switch (imsg.hdr.type) {
713 case GOTD_IMSG_LIST_REFS_INTERNAL:
714 err = list_refs(&imsg);
715 if (err)
716 log_warnx("ls-refs: %s", err->msg);
717 break;
718 case GOTD_IMSG_WANT:
719 err = recv_want(&imsg);
720 if (err)
721 log_warnx("want-line: %s", err->msg);
722 break;
723 case GOTD_IMSG_HAVE:
724 err = recv_have(&imsg);
725 if (err)
726 log_warnx("have-line: %s", err->msg);
727 break;
728 case GOTD_IMSG_SEND_PACKFILE:
729 err = receive_delta_cache_fd(&imsg, iev);
730 if (err)
731 log_warnx("receiving delta cache: %s",
732 err->msg);
733 break;
734 case GOTD_IMSG_PACKFILE_PIPE:
735 err = receive_pack_pipe(&imsg, iev);
736 if (err) {
737 log_warnx("receiving pack pipe: %s", err->msg);
738 break;
740 err = send_packfile(&imsg, iev);
741 if (err)
742 log_warnx("sending packfile: %s", err->msg);
743 break;
744 default:
745 log_debug("unexpected imsg %d", imsg.hdr.type);
746 break;
749 imsg_free(&imsg);
752 if (!shut && check_cancelled(NULL) == NULL) {
753 if (err &&
754 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
755 client->id, err) == -1) {
756 log_warnx("could not send error to parent: %s",
757 err->msg);
759 gotd_imsg_event_add(iev);
760 } else {
761 /* This pipe is dead. Remove its event handler */
762 event_del(&iev->ev);
763 event_loopexit(NULL);
767 static const struct got_error *
768 recv_connect(struct imsg *imsg)
770 struct gotd_imsgev *iev = &repo_read.session_iev;
771 size_t datalen;
773 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
774 if (datalen != 0)
775 return got_error(GOT_ERR_PRIVSEP_LEN);
776 if (imsg->fd == -1)
777 return got_error(GOT_ERR_PRIVSEP_NO_FD);
779 if (repo_read.session_fd != -1)
780 return got_error(GOT_ERR_PRIVSEP_MSG);
782 repo_read.session_fd = imsg->fd;
784 imsg_init(&iev->ibuf, repo_read.session_fd);
785 iev->handler = repo_read_dispatch_session;
786 iev->events = EV_READ;
787 iev->handler_arg = NULL;
788 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
789 repo_read_dispatch_session, iev);
790 gotd_imsg_event_add(iev);
792 return NULL;
795 static void
796 repo_read_dispatch(int fd, short event, void *arg)
798 const struct got_error *err = NULL;
799 struct gotd_imsgev *iev = arg;
800 struct imsgbuf *ibuf = &iev->ibuf;
801 struct imsg imsg;
802 ssize_t n;
803 int shut = 0;
804 struct repo_read_client *client = &repo_read_client;
806 if (event & EV_READ) {
807 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
808 fatal("imsg_read error");
809 if (n == 0) /* Connection closed. */
810 shut = 1;
813 if (event & EV_WRITE) {
814 n = msgbuf_write(&ibuf->w);
815 if (n == -1 && errno != EAGAIN)
816 fatal("msgbuf_write");
817 if (n == 0) /* Connection closed. */
818 shut = 1;
821 while (err == NULL && check_cancelled(NULL) == NULL) {
822 if ((n = imsg_get(ibuf, &imsg)) == -1)
823 fatal("%s: imsg_get", __func__);
824 if (n == 0) /* No more messages. */
825 break;
827 switch (imsg.hdr.type) {
828 case GOTD_IMSG_CONNECT_REPO_CHILD:
829 err = recv_connect(&imsg);
830 break;
831 default:
832 log_debug("unexpected imsg %d", imsg.hdr.type);
833 break;
836 imsg_free(&imsg);
839 if (!shut && check_cancelled(NULL) == NULL) {
840 if (err &&
841 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
842 client->id, err) == -1) {
843 log_warnx("could not send error to parent: %s",
844 err->msg);
846 gotd_imsg_event_add(iev);
847 } else {
848 /* This pipe is dead. Remove its event handler */
849 event_del(&iev->ev);
850 event_loopexit(NULL);
854 void
855 repo_read_main(const char *title, const char *repo_path,
856 int *pack_fds, int *temp_fds)
858 const struct got_error *err = NULL;
859 struct repo_read_client *client = &repo_read_client;
860 struct gotd_imsgev iev;
862 client->fd = -1;
863 client->delta_cache_fd = -1;
864 client->pack_pipe = -1;
865 client->have_ids = got_object_idset_alloc();
866 if (client->have_ids == NULL) {
867 err = got_error_from_errno("got_object_idset_alloc");
868 goto done;
870 client->want_ids = got_object_idset_alloc();
871 if (client->want_ids == NULL) {
872 err = got_error_from_errno("got_object_idset_alloc");
873 goto done;
876 repo_read.title = title;
877 repo_read.pid = getpid();
878 repo_read.pack_fds = pack_fds;
879 repo_read.temp_fds = temp_fds;
880 repo_read.session_fd = -1;
881 repo_read.session_iev.ibuf.fd = -1;
883 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
884 if (err)
885 goto done;
886 if (!got_repo_is_bare(repo_read.repo)) {
887 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
888 "bare git repository required");
889 goto done;
892 got_repo_temp_fds_set(repo_read.repo, temp_fds);
894 signal(SIGINT, catch_sigint);
895 signal(SIGTERM, catch_sigterm);
896 signal(SIGPIPE, SIG_IGN);
897 signal(SIGHUP, SIG_IGN);
899 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
900 iev.handler = repo_read_dispatch;
901 iev.events = EV_READ;
902 iev.handler_arg = NULL;
903 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
905 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
906 PROC_REPO_READ, -1, NULL, 0) == -1) {
907 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
908 goto done;
911 event_dispatch();
912 done:
913 if (err)
914 log_warnx("%s: %s", title, err->msg);
915 repo_read_shutdown();
918 void
919 repo_read_shutdown(void)
921 struct repo_read_client *client = &repo_read_client;
923 log_debug("shutting down");
925 if (client->have_ids)
926 got_object_idset_free(client->have_ids);
927 if (client->want_ids)
928 got_object_idset_free(client->want_ids);
929 if (client->fd != -1)
930 close(client->fd);
931 if (client->delta_cache_fd != -1)
932 close(client->delta_cache_fd);
933 if (client->pack_pipe != -1)
934 close(client->pack_pipe);
936 if (repo_read.repo)
937 got_repo_close(repo_read.repo);
938 got_repo_pack_fds_close(repo_read.pack_fds);
939 got_repo_temp_fds_close(repo_read.temp_fds);
940 if (repo_read.session_fd != -1)
941 close(repo_read.session_fd);
942 exit(0);