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 = imsg->fd;
271 struct got_object_id *head_target_id = NULL;
273 TAILQ_INIT(&refs);
275 if (client_fd == -1)
276 return got_error(GOT_ERR_PRIVSEP_NO_FD);
278 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
279 if (datalen != sizeof(ireq))
280 return got_error(GOT_ERR_PRIVSEP_LEN);
281 memcpy(&ireq, imsg->data, sizeof(ireq));
283 if (ireq.client_id == 0)
284 return got_error(GOT_ERR_CLIENT_ID);
285 if (client->id != 0) {
286 return got_error_msg(GOT_ERR_CLIENT_ID,
287 "duplicate list-refs request");
289 client->id = ireq.client_id;
290 client->fd = client_fd;
292 imsg_init(&ibuf, client_fd);
294 err = got_ref_list(&refs, repo_read.repo, "",
295 got_ref_cmp_by_name, NULL);
296 if (err)
297 return err;
299 memset(&irefs, 0, sizeof(irefs));
300 TAILQ_FOREACH(re, &refs, entry) {
301 struct got_object_id *id;
302 int obj_type;
304 if (got_ref_is_symbolic(re->ref)) {
305 const char *refname = got_ref_get_name(re->ref);
306 if (strcmp(refname, GOT_REF_HEAD) != 0)
307 continue;
308 err = got_ref_resolve(&head_target_id, repo_read.repo,
309 re->ref);
310 if (err) {
311 if (err->code != GOT_ERR_NOT_REF)
312 return err;
313 /*
314 * HEAD points to a non-existent branch.
315 * Do not advertise it.
316 * Matches git-daemon's behaviour.
317 */
318 head_target_id = NULL;
319 err = NULL;
320 } else
321 irefs.nrefs++;
322 continue;
325 irefs.nrefs++;
327 /* Account for a peeled tag refs. */
328 err = got_ref_resolve(&id, repo_read.repo, re->ref);
329 if (err)
330 goto done;
331 err = got_object_get_type(&obj_type, repo_read.repo, id);
332 free(id);
333 if (err)
334 goto done;
335 if (obj_type == GOT_OBJ_TYPE_TAG)
336 irefs.nrefs++;
339 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
340 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
341 err = got_error_from_errno("imsg_compose REFLIST");
342 goto done;
345 /*
346 * Send the HEAD symref first. In Git-protocol versions < 2
347 * the HEAD symref must be announced on the initial line of
348 * the server's ref advertisement.
349 * For now, we do not advertise symrefs other than HEAD.
350 */
351 TAILQ_FOREACH(re, &refs, entry) {
352 if (!got_ref_is_symbolic(re->ref) ||
353 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
354 head_target_id == NULL)
355 continue;
356 err = send_symref(re->ref, head_target_id, &ibuf);
357 if (err)
358 goto done;
359 break;
361 TAILQ_FOREACH(re, &refs, entry) {
362 if (got_ref_is_symbolic(re->ref))
363 continue;
364 err = send_ref(re->ref, &ibuf);
365 if (err)
366 goto done;
369 err = gotd_imsg_flush(&ibuf);
370 done:
371 got_ref_list_free(&refs);
372 imsg_clear(&ibuf);
373 return err;
376 static const struct got_error *
377 append_object_id(struct got_object_id *id, void *data, void *arg)
379 struct gotd_object_id_array *array = arg;
380 const size_t alloc_chunksz = 256;
382 if (array->ids == NULL) {
383 array->ids = reallocarray(NULL, alloc_chunksz,
384 sizeof(*array->ids));
385 if (array->ids == NULL)
386 return got_error_from_errno("reallocarray");
387 array->nalloc = alloc_chunksz;
388 array->nids = 0;
389 } else if (array->nalloc <= array->nids) {
390 struct got_object_id **new;
391 new = recallocarray(array->ids, array->nalloc,
392 array->nalloc + alloc_chunksz, sizeof(*new));
393 if (new == NULL)
394 return got_error_from_errno("recallocarray");
395 array->ids = new;
396 array->nalloc += alloc_chunksz;
399 array->ids[array->nids] = id;
400 array->nids++;
401 return NULL;
404 static const struct got_error *
405 recv_want(struct imsg *imsg)
407 const struct got_error *err;
408 struct repo_read_client *client = &repo_read_client;
409 struct gotd_imsg_want iwant;
410 size_t datalen;
411 char hex[SHA1_DIGEST_STRING_LENGTH];
412 struct got_object_id id;
413 int obj_type;
414 struct imsgbuf ibuf;
416 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
417 if (datalen != sizeof(iwant))
418 return got_error(GOT_ERR_PRIVSEP_LEN);
419 memcpy(&iwant, imsg->data, sizeof(iwant));
421 memset(&id, 0, sizeof(id));
422 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
424 if (log_getverbose() > 0 &&
425 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
426 log_debug("client wants %s", hex);
428 imsg_init(&ibuf, client->fd);
430 err = got_object_get_type(&obj_type, repo_read.repo, &id);
431 if (err)
432 return err;
434 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
435 obj_type != GOT_OBJ_TYPE_TAG)
436 return got_error(GOT_ERR_OBJ_TYPE);
438 if (!got_object_idset_contains(client->want_ids, &id)) {
439 err = got_object_idset_add(client->want_ids, &id, NULL);
440 if (err)
441 return err;
444 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
445 imsg_clear(&ibuf);
446 return err;
449 static const struct got_error *
450 recv_have(struct imsg *imsg)
452 const struct got_error *err;
453 struct repo_read_client *client = &repo_read_client;
454 struct gotd_imsg_have ihave;
455 size_t datalen;
456 char hex[SHA1_DIGEST_STRING_LENGTH];
457 struct got_object_id id;
458 int obj_type;
459 struct imsgbuf ibuf;
461 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
462 if (datalen != sizeof(ihave))
463 return got_error(GOT_ERR_PRIVSEP_LEN);
464 memcpy(&ihave, imsg->data, sizeof(ihave));
466 memset(&id, 0, sizeof(id));
467 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
469 if (log_getverbose() > 0 &&
470 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
471 log_debug("client has %s", hex);
473 imsg_init(&ibuf, client->fd);
475 err = got_object_get_type(&obj_type, repo_read.repo, &id);
476 if (err) {
477 if (err->code == GOT_ERR_NO_OBJ) {
478 gotd_imsg_send_nak(&id, &ibuf,
479 PROC_REPO_READ, repo_read.pid);
480 err = NULL;
482 goto done;
485 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
486 obj_type != GOT_OBJ_TYPE_TAG) {
487 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
488 err = got_error(GOT_ERR_OBJ_TYPE);
489 goto done;
492 if (!got_object_idset_contains(client->have_ids, &id)) {
493 err = got_object_idset_add(client->have_ids, &id, NULL);
494 if (err)
495 goto done;
498 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
499 done:
500 imsg_clear(&ibuf);
501 return err;
504 struct repo_read_pack_progress_arg {
505 int report_progress;
506 struct imsgbuf *ibuf;
507 int sent_ready;
508 };
510 static const struct got_error *
511 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
512 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
513 int nobj_written)
515 struct repo_read_pack_progress_arg *a = arg;
516 struct gotd_imsg_packfile_progress iprog;
517 int ret;
519 if (!a->report_progress)
520 return NULL;
521 if (packfile_size > 0 && a->sent_ready)
522 return NULL;
524 memset(&iprog, 0, sizeof(iprog));
525 iprog.ncolored = ncolored;
526 iprog.nfound = nfound;
527 iprog.ntrees = ntrees;
528 iprog.packfile_size = packfile_size;
529 iprog.ncommits = ncommits;
530 iprog.nobj_total = nobj_total;
531 iprog.nobj_deltify = nobj_deltify;
532 iprog.nobj_written = nobj_written;
534 /* Using synchronous writes since we are blocking the event loop. */
535 if (packfile_size == 0) {
536 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
537 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
538 if (ret == -1) {
539 return got_error_from_errno("imsg compose "
540 "PACKFILE_PROGRESS");
542 } else {
543 a->sent_ready = 1;
544 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
545 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
546 if (ret == -1) {
547 return got_error_from_errno("imsg compose "
548 "PACKFILE_READY");
552 return gotd_imsg_flush(a->ibuf);
555 static const struct got_error *
556 receive_delta_cache_fd(struct imsg *imsg,
557 struct gotd_imsgev *iev)
559 struct repo_read_client *client = &repo_read_client;
560 struct gotd_imsg_send_packfile ireq;
561 size_t datalen;
563 log_debug("receiving delta cache file");
565 if (imsg->fd == -1)
566 return got_error(GOT_ERR_PRIVSEP_NO_FD);
568 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
569 if (datalen != sizeof(ireq))
570 return got_error(GOT_ERR_PRIVSEP_LEN);
571 memcpy(&ireq, imsg->data, sizeof(ireq));
573 if (client->delta_cache_fd != -1)
574 return got_error(GOT_ERR_PRIVSEP_MSG);
576 client->delta_cache_fd = imsg->fd;
577 client->report_progress = ireq.report_progress;
578 return NULL;
581 static const struct got_error *
582 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
584 struct repo_read_client *client = &repo_read_client;
585 struct gotd_imsg_packfile_pipe ireq;
586 size_t datalen;
588 log_debug("receiving pack pipe descriptor");
590 if (imsg->fd == -1)
591 return got_error(GOT_ERR_PRIVSEP_NO_FD);
593 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
594 if (datalen != sizeof(ireq))
595 return got_error(GOT_ERR_PRIVSEP_LEN);
596 memcpy(&ireq, imsg->data, sizeof(ireq));
598 if (client->pack_pipe != -1)
599 return got_error(GOT_ERR_PRIVSEP_MSG);
601 client->pack_pipe = imsg->fd;
602 return NULL;
605 static const struct got_error *
606 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
608 const struct got_error *err = NULL;
609 struct repo_read_client *client = &repo_read_client;
610 struct gotd_imsg_packfile_done idone;
611 uint8_t packsha1[SHA1_DIGEST_LENGTH];
612 char hex[SHA1_DIGEST_STRING_LENGTH];
613 FILE *delta_cache = NULL;
614 struct imsgbuf ibuf;
615 struct repo_read_pack_progress_arg pa;
616 struct got_ratelimit rl;
617 struct gotd_object_id_array want_ids;
618 struct gotd_object_id_array have_ids;
620 log_debug("packfile request received");
622 memset(&want_ids, 0, sizeof(want_ids));
623 memset(&have_ids, 0, sizeof(have_ids));
625 got_ratelimit_init(&rl, 2, 0);
627 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
628 return got_error(GOT_ERR_PRIVSEP_NO_FD);
630 imsg_init(&ibuf, client->fd);
632 delta_cache = fdopen(client->delta_cache_fd, "w+");
633 if (delta_cache == NULL) {
634 err = got_error_from_errno("fdopen");
635 goto done;
637 client->delta_cache_fd = -1;
639 memset(&pa, 0, sizeof(pa));
640 pa.ibuf = &ibuf;
641 pa.report_progress = client->report_progress;
643 err = got_object_idset_for_each(client->want_ids,
644 append_object_id, &want_ids);
645 if (err)
646 goto done;
647 err = got_object_idset_for_each(client->have_ids,
648 append_object_id, &have_ids);
649 if (err)
650 goto done;
652 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
653 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
654 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
655 check_cancelled, NULL);
656 if (err)
657 goto done;
659 if (log_getverbose() > 0 &&
660 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
661 log_debug("sent pack-%s.pack", hex);
663 memset(&idone, 0, sizeof(idone));
664 idone.client_id = client->id;
665 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
666 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
667 err = got_error_from_errno("imsg compose PACKFILE_DONE");
668 done:
669 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
670 err = got_error_from_errno("fclose");
671 imsg_clear(&ibuf);
672 free(want_ids.ids);
673 free(have_ids.ids);
674 return err;
677 static void
678 repo_read_dispatch_session(int fd, short event, void *arg)
680 const struct got_error *err = NULL;
681 struct gotd_imsgev *iev = arg;
682 struct imsgbuf *ibuf = &iev->ibuf;
683 struct imsg imsg;
684 ssize_t n;
685 int shut = 0;
686 struct repo_read_client *client = &repo_read_client;
688 if (event & EV_READ) {
689 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
690 fatal("imsg_read error");
691 if (n == 0) /* Connection closed. */
692 shut = 1;
695 if (event & EV_WRITE) {
696 n = msgbuf_write(&ibuf->w);
697 if (n == -1 && errno != EAGAIN)
698 fatal("msgbuf_write");
699 if (n == 0) /* Connection closed. */
700 shut = 1;
703 while (err == NULL && check_cancelled(NULL) == NULL) {
704 if ((n = imsg_get(ibuf, &imsg)) == -1)
705 fatal("%s: imsg_get", __func__);
706 if (n == 0) /* No more messages. */
707 break;
709 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
710 client->id == 0) {
711 err = got_error(GOT_ERR_PRIVSEP_MSG);
712 break;
715 switch (imsg.hdr.type) {
716 case GOTD_IMSG_LIST_REFS_INTERNAL:
717 err = list_refs(&imsg);
718 if (err)
719 log_warnx("ls-refs: %s", err->msg);
720 break;
721 case GOTD_IMSG_WANT:
722 err = recv_want(&imsg);
723 if (err)
724 log_warnx("want-line: %s", err->msg);
725 break;
726 case GOTD_IMSG_HAVE:
727 err = recv_have(&imsg);
728 if (err)
729 log_warnx("have-line: %s", err->msg);
730 break;
731 case GOTD_IMSG_SEND_PACKFILE:
732 err = receive_delta_cache_fd(&imsg, iev);
733 if (err)
734 log_warnx("receiving delta cache: %s",
735 err->msg);
736 break;
737 case GOTD_IMSG_PACKFILE_PIPE:
738 err = receive_pack_pipe(&imsg, iev);
739 if (err) {
740 log_warnx("receiving pack pipe: %s", err->msg);
741 break;
743 err = send_packfile(&imsg, iev);
744 if (err)
745 log_warnx("sending packfile: %s", err->msg);
746 break;
747 default:
748 log_debug("unexpected imsg %d", imsg.hdr.type);
749 break;
752 imsg_free(&imsg);
755 if (!shut && check_cancelled(NULL) == NULL) {
756 if (err &&
757 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
758 client->id, err) == -1) {
759 log_warnx("could not send error to parent: %s",
760 err->msg);
762 gotd_imsg_event_add(iev);
763 } else {
764 /* This pipe is dead. Remove its event handler */
765 event_del(&iev->ev);
766 event_loopexit(NULL);
770 static const struct got_error *
771 recv_connect(struct imsg *imsg)
773 struct gotd_imsgev *iev = &repo_read.session_iev;
774 size_t datalen;
776 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 if (datalen != 0)
778 return got_error(GOT_ERR_PRIVSEP_LEN);
779 if (imsg->fd == -1)
780 return got_error(GOT_ERR_PRIVSEP_NO_FD);
782 if (repo_read.session_fd != -1)
783 return got_error(GOT_ERR_PRIVSEP_MSG);
785 repo_read.session_fd = imsg->fd;
787 imsg_init(&iev->ibuf, repo_read.session_fd);
788 iev->handler = repo_read_dispatch_session;
789 iev->events = EV_READ;
790 iev->handler_arg = NULL;
791 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
792 repo_read_dispatch_session, iev);
793 gotd_imsg_event_add(iev);
795 return NULL;
798 static void
799 repo_read_dispatch(int fd, short event, void *arg)
801 const struct got_error *err = NULL;
802 struct gotd_imsgev *iev = arg;
803 struct imsgbuf *ibuf = &iev->ibuf;
804 struct imsg imsg;
805 ssize_t n;
806 int shut = 0;
807 struct repo_read_client *client = &repo_read_client;
809 if (event & EV_READ) {
810 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
811 fatal("imsg_read error");
812 if (n == 0) /* Connection closed. */
813 shut = 1;
816 if (event & EV_WRITE) {
817 n = msgbuf_write(&ibuf->w);
818 if (n == -1 && errno != EAGAIN)
819 fatal("msgbuf_write");
820 if (n == 0) /* Connection closed. */
821 shut = 1;
824 while (err == NULL && check_cancelled(NULL) == NULL) {
825 if ((n = imsg_get(ibuf, &imsg)) == -1)
826 fatal("%s: imsg_get", __func__);
827 if (n == 0) /* No more messages. */
828 break;
830 switch (imsg.hdr.type) {
831 case GOTD_IMSG_CONNECT_REPO_CHILD:
832 err = recv_connect(&imsg);
833 break;
834 default:
835 log_debug("unexpected imsg %d", imsg.hdr.type);
836 break;
839 imsg_free(&imsg);
842 if (!shut && check_cancelled(NULL) == NULL) {
843 if (err &&
844 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
845 client->id, err) == -1) {
846 log_warnx("could not send error to parent: %s",
847 err->msg);
849 gotd_imsg_event_add(iev);
850 } else {
851 /* This pipe is dead. Remove its event handler */
852 event_del(&iev->ev);
853 event_loopexit(NULL);
857 void
858 repo_read_main(const char *title, const char *repo_path,
859 int *pack_fds, int *temp_fds)
861 const struct got_error *err = NULL;
862 struct repo_read_client *client = &repo_read_client;
863 struct gotd_imsgev iev;
865 client->fd = -1;
866 client->delta_cache_fd = -1;
867 client->pack_pipe = -1;
868 client->have_ids = got_object_idset_alloc();
869 if (client->have_ids == NULL) {
870 err = got_error_from_errno("got_object_idset_alloc");
871 goto done;
873 client->want_ids = got_object_idset_alloc();
874 if (client->want_ids == NULL) {
875 err = got_error_from_errno("got_object_idset_alloc");
876 goto done;
879 repo_read.title = title;
880 repo_read.pid = getpid();
881 repo_read.pack_fds = pack_fds;
882 repo_read.temp_fds = temp_fds;
883 repo_read.session_fd = -1;
884 repo_read.session_iev.ibuf.fd = -1;
886 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
887 if (err)
888 goto done;
889 if (!got_repo_is_bare(repo_read.repo)) {
890 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
891 "bare git repository required");
892 goto done;
895 got_repo_temp_fds_set(repo_read.repo, temp_fds);
897 signal(SIGINT, catch_sigint);
898 signal(SIGTERM, catch_sigterm);
899 signal(SIGPIPE, SIG_IGN);
900 signal(SIGHUP, SIG_IGN);
902 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
903 iev.handler = repo_read_dispatch;
904 iev.events = EV_READ;
905 iev.handler_arg = NULL;
906 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
908 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
909 PROC_REPO_READ, -1, NULL, 0) == -1) {
910 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
911 goto done;
914 event_dispatch();
915 done:
916 if (err)
917 log_warnx("%s: %s", title, err->msg);
918 repo_read_shutdown();
921 void
922 repo_read_shutdown(void)
924 struct repo_read_client *client = &repo_read_client;
926 log_debug("shutting down");
928 if (client->have_ids)
929 got_object_idset_free(client->have_ids);
930 if (client->want_ids)
931 got_object_idset_free(client->want_ids);
932 if (client->fd != -1)
933 close(client->fd);
934 if (client->delta_cache_fd != -1)
935 close(client->delta_cache_fd);
936 if (client->pack_pipe != -1)
937 close(client->pack_pipe);
939 if (repo_read.repo)
940 got_repo_close(repo_read.repo);
941 got_repo_pack_fds_close(repo_read.pack_fds);
942 got_repo_temp_fds_close(repo_read.temp_fds);
943 if (repo_read.session_fd != -1)
944 close(repo_read.session_fd);
945 exit(0);