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 } repo_read;
68 static struct repo_read_client {
69 uint32_t id;
70 int fd;
71 int delta_cache_fd;
72 int report_progress;
73 int pack_pipe;
74 struct got_object_idset *want_ids;
75 struct got_object_idset *have_ids;
76 } repo_read_client;
78 static volatile sig_atomic_t sigint_received;
79 static volatile sig_atomic_t sigterm_received;
81 static void
82 catch_sigint(int signo)
83 {
84 sigint_received = 1;
85 }
87 static void
88 catch_sigterm(int signo)
89 {
90 sigterm_received = 1;
91 }
93 static const struct got_error *
94 check_cancelled(void *arg)
95 {
96 if (sigint_received || sigterm_received)
97 return got_error(GOT_ERR_CANCELLED);
99 return NULL;
102 static const struct got_error *
103 send_symref(struct got_reference *symref, struct got_object_id *target_id,
104 struct imsgbuf *ibuf)
106 const struct got_error *err = NULL;
107 struct gotd_imsg_symref isymref;
108 const char *refname = got_ref_get_name(symref);
109 const char *target = got_ref_get_symref_target(symref);
110 size_t len;
111 struct ibuf *wbuf;
113 memset(&isymref, 0, sizeof(isymref));
114 isymref.name_len = strlen(refname);
115 isymref.target_len = strlen(target);
116 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
118 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
119 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
120 err = got_error(GOT_ERR_NO_SPACE);
121 goto done;
124 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
125 if (wbuf == NULL) {
126 err = got_error_from_errno("imsg_create SYMREF");
127 goto done;
130 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
131 err = got_error_from_errno("imsg_add SYMREF");
132 goto done;
134 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
135 err = got_error_from_errno("imsg_add SYMREF");
136 goto done;
138 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
139 err = got_error_from_errno("imsg_add SYMREF");
140 goto done;
143 imsg_close(ibuf, wbuf);
144 done:
145 free(target_id);
146 return err;
149 static const struct got_error *
150 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
151 struct imsgbuf *ibuf)
153 const struct got_error *err = NULL;
154 struct got_tag_object *tag;
155 size_t namelen, len;
156 char *peeled_refname = NULL;
157 struct got_object_id *id;
158 struct ibuf *wbuf;
160 err = got_object_tag_open(&tag, repo_read.repo, obj);
161 if (err)
162 return err;
164 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
165 err = got_error_from_errno("asprintf");
166 goto done;
169 id = got_object_tag_get_object_id(tag);
170 namelen = strlen(peeled_refname);
172 len = sizeof(struct gotd_imsg_ref) + namelen;
173 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
174 err = got_error(GOT_ERR_NO_SPACE);
175 goto done;
178 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
179 repo_read.pid, len);
180 if (wbuf == NULL) {
181 err = got_error_from_errno("imsg_create MREF");
182 goto done;
185 /* Keep in sync with struct gotd_imsg_ref definition. */
186 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
187 err = got_error_from_errno("imsg_add REF");
188 goto done;
190 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
191 err = got_error_from_errno("imsg_add REF");
192 goto done;
194 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
195 err = got_error_from_errno("imsg_add REF");
196 goto done;
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 imsg_close(ibuf, wbuf);
243 err = got_object_open(&obj, repo_read.repo, id);
244 if (err)
245 goto done;
246 if (obj->type == GOT_OBJ_TYPE_TAG)
247 err = send_peeled_tag_ref(ref, obj, ibuf);
248 done:
249 if (obj)
250 got_object_close(obj);
251 free(id);
252 return err;
255 static const struct got_error *
256 list_refs(struct imsg *imsg)
258 const struct got_error *err;
259 struct repo_read_client *client = &repo_read_client;
260 struct got_reflist_head refs;
261 struct got_reflist_entry *re;
262 struct gotd_imsg_list_refs_internal ireq;
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 != 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("receiving delta cache file");
562 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
563 if (datalen != sizeof(ireq))
564 return got_error(GOT_ERR_PRIVSEP_LEN);
565 memcpy(&ireq, imsg->data, sizeof(ireq));
567 if (client->delta_cache_fd != -1)
568 return got_error(GOT_ERR_PRIVSEP_MSG);
570 client->delta_cache_fd = imsg_get_fd(imsg);
571 if (client->delta_cache_fd == -1)
572 return got_error(GOT_ERR_PRIVSEP_NO_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("receiving pack pipe descriptor");
587 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 if (datalen != sizeof(ireq))
589 return got_error(GOT_ERR_PRIVSEP_LEN);
590 memcpy(&ireq, imsg->data, sizeof(ireq));
592 if (client->pack_pipe != -1)
593 return got_error(GOT_ERR_PRIVSEP_MSG);
595 client->pack_pipe = imsg_get_fd(imsg);
596 if (client->pack_pipe == -1)
597 return got_error(GOT_ERR_PRIVSEP_NO_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 (client->delta_cache_fd != -1 &&
667 close(client->delta_cache_fd) == -1 && err == NULL)
668 err = got_error_from_errno("close");
669 client->delta_cache_fd = -1;
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);