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"
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 wbuf->fd = -1;
144 imsg_close(ibuf, wbuf);
145 done:
146 free(target_id);
147 return err;
150 static const struct got_error *
151 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
152 struct imsgbuf *ibuf)
154 const struct got_error *err = NULL;
155 struct got_tag_object *tag;
156 size_t namelen, len;
157 char *peeled_refname = NULL;
158 struct got_object_id *id;
159 struct ibuf *wbuf;
161 err = got_object_tag_open(&tag, repo_read.repo, obj);
162 if (err)
163 return err;
165 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
166 err = got_error_from_errno("asprintf");
167 goto done;
170 id = got_object_tag_get_object_id(tag);
171 namelen = strlen(peeled_refname);
173 len = sizeof(struct gotd_imsg_ref) + namelen;
174 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
175 err = got_error(GOT_ERR_NO_SPACE);
176 goto done;
179 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
180 repo_read.pid, len);
181 if (wbuf == NULL) {
182 err = got_error_from_errno("imsg_create MREF");
183 goto done;
186 /* Keep in sync with struct gotd_imsg_ref definition. */
187 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
188 err = got_error_from_errno("imsg_add REF");
189 goto done;
191 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
192 err = got_error_from_errno("imsg_add REF");
193 goto done;
195 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
196 err = got_error_from_errno("imsg_add REF");
197 goto done;
200 wbuf->fd = -1;
201 imsg_close(ibuf, wbuf);
202 done:
203 got_object_tag_close(tag);
204 return err;
207 static const struct got_error *
208 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
210 const struct got_error *err;
211 const char *refname = got_ref_get_name(ref);
212 size_t namelen;
213 struct got_object_id *id = NULL;
214 struct got_object *obj = NULL;
215 size_t len;
216 struct ibuf *wbuf;
218 namelen = strlen(refname);
220 len = sizeof(struct gotd_imsg_ref) + namelen;
221 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 err = got_ref_resolve(&id, repo_read.repo, ref);
225 if (err)
226 return err;
228 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
229 repo_read.pid, len);
230 if (wbuf == NULL) {
231 err = got_error_from_errno("imsg_create REF");
232 goto done;
235 /* Keep in sync with struct gotd_imsg_ref definition. */
236 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
237 return got_error_from_errno("imsg_add REF");
238 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
239 return got_error_from_errno("imsg_add REF");
240 if (imsg_add(wbuf, refname, namelen) == -1)
241 return got_error_from_errno("imsg_add REF");
243 wbuf->fd = -1;
244 imsg_close(ibuf, wbuf);
246 err = got_object_open(&obj, repo_read.repo, id);
247 if (err)
248 goto done;
249 if (obj->type == GOT_OBJ_TYPE_TAG)
250 err = send_peeled_tag_ref(ref, obj, ibuf);
251 done:
252 if (obj)
253 got_object_close(obj);
254 free(id);
255 return err;
258 static const struct got_error *
259 list_refs(struct imsg *imsg)
261 const struct got_error *err;
262 struct repo_read_client *client = &repo_read_client;
263 struct got_reflist_head refs;
264 struct got_reflist_entry *re;
265 struct gotd_imsg_list_refs_internal ireq;
266 size_t datalen;
267 struct gotd_imsg_reflist irefs;
268 struct imsgbuf ibuf;
269 int client_fd = imsg->fd;
270 struct got_object_id *head_target_id = NULL;
272 TAILQ_INIT(&refs);
274 if (client_fd == -1)
275 return got_error(GOT_ERR_PRIVSEP_NO_FD);
277 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
278 if (datalen != sizeof(ireq))
279 return got_error(GOT_ERR_PRIVSEP_LEN);
280 memcpy(&ireq, imsg->data, sizeof(ireq));
282 if (ireq.client_id == 0)
283 return got_error(GOT_ERR_CLIENT_ID);
284 if (client->id != 0) {
285 return got_error_msg(GOT_ERR_CLIENT_ID,
286 "duplicate list-refs request");
288 client->id = ireq.client_id;
289 client->fd = client_fd;
291 imsg_init(&ibuf, client_fd);
293 err = got_ref_list(&refs, repo_read.repo, "",
294 got_ref_cmp_by_name, NULL);
295 if (err)
296 return err;
298 memset(&irefs, 0, sizeof(irefs));
299 TAILQ_FOREACH(re, &refs, entry) {
300 struct got_object_id *id;
301 int obj_type;
303 if (got_ref_is_symbolic(re->ref)) {
304 const char *refname = got_ref_get_name(re->ref);
305 if (strcmp(refname, GOT_REF_HEAD) != 0)
306 continue;
307 err = got_ref_resolve(&head_target_id, repo_read.repo,
308 re->ref);
309 if (err) {
310 if (err->code != GOT_ERR_NOT_REF)
311 return err;
312 /*
313 * HEAD points to a non-existent branch.
314 * Do not advertise it.
315 * Matches git-daemon's behaviour.
316 */
317 head_target_id = NULL;
318 err = NULL;
319 } else
320 irefs.nrefs++;
321 continue;
324 irefs.nrefs++;
326 /* Account for a peeled tag refs. */
327 err = got_ref_resolve(&id, repo_read.repo, re->ref);
328 if (err)
329 goto done;
330 err = got_object_get_type(&obj_type, repo_read.repo, id);
331 free(id);
332 if (err)
333 goto done;
334 if (obj_type == GOT_OBJ_TYPE_TAG)
335 irefs.nrefs++;
338 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
339 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
340 err = got_error_from_errno("imsg_compose REFLIST");
341 goto done;
344 /*
345 * Send the HEAD symref first. In Git-protocol versions < 2
346 * the HEAD symref must be announced on the initial line of
347 * the server's ref advertisement.
348 * For now, we do not advertise symrefs other than HEAD.
349 */
350 TAILQ_FOREACH(re, &refs, entry) {
351 if (!got_ref_is_symbolic(re->ref) ||
352 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
353 head_target_id == NULL)
354 continue;
355 err = send_symref(re->ref, head_target_id, &ibuf);
356 if (err)
357 goto done;
358 break;
360 TAILQ_FOREACH(re, &refs, entry) {
361 if (got_ref_is_symbolic(re->ref))
362 continue;
363 err = send_ref(re->ref, &ibuf);
364 if (err)
365 goto done;
368 err = gotd_imsg_flush(&ibuf);
369 done:
370 got_ref_list_free(&refs);
371 imsg_clear(&ibuf);
372 return err;
375 static const struct got_error *
376 append_object_id(struct got_object_id *id, void *data, void *arg)
378 struct gotd_object_id_array *array = arg;
379 const size_t alloc_chunksz = 256;
381 if (array->ids == NULL) {
382 array->ids = reallocarray(NULL, alloc_chunksz,
383 sizeof(*array->ids));
384 if (array->ids == NULL)
385 return got_error_from_errno("reallocarray");
386 array->nalloc = alloc_chunksz;
387 array->nids = 0;
388 } else if (array->nalloc <= array->nids) {
389 struct got_object_id **new;
390 new = recallocarray(array->ids, array->nalloc,
391 array->nalloc + alloc_chunksz, sizeof(*new));
392 if (new == NULL)
393 return got_error_from_errno("recallocarray");
394 array->ids = new;
395 array->nalloc += alloc_chunksz;
398 array->ids[array->nids] = id;
399 array->nids++;
400 return NULL;
403 static const struct got_error *
404 recv_want(struct imsg *imsg)
406 const struct got_error *err;
407 struct repo_read_client *client = &repo_read_client;
408 struct gotd_imsg_want iwant;
409 size_t datalen;
410 char hex[SHA1_DIGEST_STRING_LENGTH];
411 struct got_object_id id;
412 int obj_type;
413 struct imsgbuf ibuf;
415 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
416 if (datalen != sizeof(iwant))
417 return got_error(GOT_ERR_PRIVSEP_LEN);
418 memcpy(&iwant, imsg->data, sizeof(iwant));
420 memset(&id, 0, sizeof(id));
421 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
423 if (log_getverbose() > 0 &&
424 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
425 log_debug("client wants %s", hex);
427 imsg_init(&ibuf, client->fd);
429 err = got_object_get_type(&obj_type, repo_read.repo, &id);
430 if (err)
431 return err;
433 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
434 obj_type != GOT_OBJ_TYPE_TAG)
435 return got_error(GOT_ERR_OBJ_TYPE);
437 if (!got_object_idset_contains(client->want_ids, &id)) {
438 err = got_object_idset_add(client->want_ids, &id, NULL);
439 if (err)
440 return err;
443 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
444 imsg_clear(&ibuf);
445 return err;
448 static const struct got_error *
449 recv_have(struct imsg *imsg)
451 const struct got_error *err;
452 struct repo_read_client *client = &repo_read_client;
453 struct gotd_imsg_have ihave;
454 size_t datalen;
455 char hex[SHA1_DIGEST_STRING_LENGTH];
456 struct got_object_id id;
457 int obj_type;
458 struct imsgbuf ibuf;
460 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
461 if (datalen != sizeof(ihave))
462 return got_error(GOT_ERR_PRIVSEP_LEN);
463 memcpy(&ihave, imsg->data, sizeof(ihave));
465 memset(&id, 0, sizeof(id));
466 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
468 if (log_getverbose() > 0 &&
469 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
470 log_debug("client has %s", hex);
472 imsg_init(&ibuf, client->fd);
474 err = got_object_get_type(&obj_type, repo_read.repo, &id);
475 if (err) {
476 if (err->code == GOT_ERR_NO_OBJ) {
477 gotd_imsg_send_nak(&id, &ibuf,
478 PROC_REPO_READ, repo_read.pid);
479 err = NULL;
481 goto done;
484 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
485 obj_type != GOT_OBJ_TYPE_TAG) {
486 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
487 err = got_error(GOT_ERR_OBJ_TYPE);
488 goto done;
491 if (!got_object_idset_contains(client->have_ids, &id)) {
492 err = got_object_idset_add(client->have_ids, &id, NULL);
493 if (err)
494 goto done;
497 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
498 done:
499 imsg_clear(&ibuf);
500 return err;
503 struct repo_read_pack_progress_arg {
504 int report_progress;
505 struct imsgbuf *ibuf;
506 int sent_ready;
507 };
509 static const struct got_error *
510 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
511 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
512 int nobj_written)
514 struct repo_read_pack_progress_arg *a = arg;
515 struct gotd_imsg_packfile_progress iprog;
516 int ret;
518 if (!a->report_progress)
519 return NULL;
520 if (packfile_size > 0 && a->sent_ready)
521 return NULL;
523 memset(&iprog, 0, sizeof(iprog));
524 iprog.ncolored = ncolored;
525 iprog.nfound = nfound;
526 iprog.ntrees = ntrees;
527 iprog.packfile_size = packfile_size;
528 iprog.ncommits = ncommits;
529 iprog.nobj_total = nobj_total;
530 iprog.nobj_deltify = nobj_deltify;
531 iprog.nobj_written = nobj_written;
533 /* Using synchronous writes since we are blocking the event loop. */
534 if (packfile_size == 0) {
535 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
536 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
537 if (ret == -1) {
538 return got_error_from_errno("imsg compose "
539 "PACKFILE_PROGRESS");
541 } else {
542 a->sent_ready = 1;
543 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
544 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
545 if (ret == -1) {
546 return got_error_from_errno("imsg compose "
547 "PACKFILE_READY");
551 return gotd_imsg_flush(a->ibuf);
554 static const struct got_error *
555 receive_delta_cache_fd(struct imsg *imsg,
556 struct gotd_imsgev *iev)
558 struct repo_read_client *client = &repo_read_client;
559 struct gotd_imsg_send_packfile ireq;
560 size_t datalen;
562 log_debug("receving delta cache file");
564 if (imsg->fd == -1)
565 return got_error(GOT_ERR_PRIVSEP_NO_FD);
567 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
568 if (datalen != sizeof(ireq))
569 return got_error(GOT_ERR_PRIVSEP_LEN);
570 memcpy(&ireq, imsg->data, sizeof(ireq));
572 if (client->delta_cache_fd != -1)
573 return got_error(GOT_ERR_PRIVSEP_MSG);
575 client->delta_cache_fd = imsg->fd;
576 client->report_progress = ireq.report_progress;
577 return NULL;
580 static const struct got_error *
581 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
583 struct repo_read_client *client = &repo_read_client;
584 struct gotd_imsg_packfile_pipe ireq;
585 size_t datalen;
587 log_debug("receving pack pipe descriptor");
589 if (imsg->fd == -1)
590 return got_error(GOT_ERR_PRIVSEP_NO_FD);
592 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
593 if (datalen != sizeof(ireq))
594 return got_error(GOT_ERR_PRIVSEP_LEN);
595 memcpy(&ireq, imsg->data, sizeof(ireq));
597 if (client->pack_pipe != -1)
598 return got_error(GOT_ERR_PRIVSEP_MSG);
600 client->pack_pipe = imsg->fd;
601 return NULL;
604 static const struct got_error *
605 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
607 const struct got_error *err = NULL;
608 struct repo_read_client *client = &repo_read_client;
609 struct gotd_imsg_packfile_done idone;
610 uint8_t packsha1[SHA1_DIGEST_LENGTH];
611 char hex[SHA1_DIGEST_STRING_LENGTH];
612 FILE *delta_cache = NULL;
613 struct imsgbuf ibuf;
614 struct repo_read_pack_progress_arg pa;
615 struct got_ratelimit rl;
616 struct gotd_object_id_array want_ids;
617 struct gotd_object_id_array have_ids;
619 log_debug("packfile request received");
621 memset(&want_ids, 0, sizeof(want_ids));
622 memset(&have_ids, 0, sizeof(have_ids));
624 got_ratelimit_init(&rl, 2, 0);
626 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
627 return got_error(GOT_ERR_PRIVSEP_NO_FD);
629 imsg_init(&ibuf, client->fd);
631 delta_cache = fdopen(client->delta_cache_fd, "w+");
632 if (delta_cache == NULL) {
633 err = got_error_from_errno("fdopen");
634 goto done;
636 client->delta_cache_fd = -1;
638 memset(&pa, 0, sizeof(pa));
639 pa.ibuf = &ibuf;
640 pa.report_progress = client->report_progress;
642 err = got_object_idset_for_each(client->want_ids,
643 append_object_id, &want_ids);
644 if (err)
645 goto done;
646 err = got_object_idset_for_each(client->have_ids,
647 append_object_id, &have_ids);
648 if (err)
649 goto done;
651 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
652 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
653 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
654 check_cancelled, NULL);
655 if (err)
656 goto done;
658 if (log_getverbose() > 0 &&
659 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
660 log_debug("sent pack-%s.pack", hex);
662 memset(&idone, 0, sizeof(idone));
663 idone.client_id = client->id;
664 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
665 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
666 err = got_error_from_errno("imsg compose PACKFILE_DONE");
667 done:
668 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
669 err = got_error_from_errno("fclose");
670 imsg_clear(&ibuf);
671 free(want_ids.ids);
672 free(have_ids.ids);
673 return err;
676 static void
677 repo_read_dispatch_session(int fd, short event, void *arg)
679 const struct got_error *err = NULL;
680 struct gotd_imsgev *iev = arg;
681 struct imsgbuf *ibuf = &iev->ibuf;
682 struct imsg imsg;
683 ssize_t n;
684 int shut = 0;
685 struct repo_read_client *client = &repo_read_client;
687 if (event & EV_READ) {
688 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
689 fatal("imsg_read error");
690 if (n == 0) /* Connection closed. */
691 shut = 1;
694 if (event & EV_WRITE) {
695 n = msgbuf_write(&ibuf->w);
696 if (n == -1 && errno != EAGAIN)
697 fatal("msgbuf_write");
698 if (n == 0) /* Connection closed. */
699 shut = 1;
702 while (err == NULL && check_cancelled(NULL) == NULL) {
703 if ((n = imsg_get(ibuf, &imsg)) == -1)
704 fatal("%s: imsg_get", __func__);
705 if (n == 0) /* No more messages. */
706 break;
708 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
709 client->id == 0) {
710 err = got_error(GOT_ERR_PRIVSEP_MSG);
711 break;
714 switch (imsg.hdr.type) {
715 case GOTD_IMSG_LIST_REFS_INTERNAL:
716 err = list_refs(&imsg);
717 if (err)
718 log_warnx("%s: ls-refs: %s", repo_read.title,
719 err->msg);
720 break;
721 case GOTD_IMSG_WANT:
722 err = recv_want(&imsg);
723 if (err)
724 log_warnx("%s: want-line: %s", repo_read.title,
725 err->msg);
726 break;
727 case GOTD_IMSG_HAVE:
728 err = recv_have(&imsg);
729 if (err)
730 log_warnx("%s: have-line: %s", repo_read.title,
731 err->msg);
732 break;
733 case GOTD_IMSG_SEND_PACKFILE:
734 err = receive_delta_cache_fd(&imsg, iev);
735 if (err)
736 log_warnx("%s: receiving delta cache: %s",
737 repo_read.title, err->msg);
738 break;
739 case GOTD_IMSG_PACKFILE_PIPE:
740 err = receive_pack_pipe(&imsg, iev);
741 if (err) {
742 log_warnx("%s: receiving pack pipe: %s",
743 repo_read.title, err->msg);
744 break;
746 err = send_packfile(&imsg, iev);
747 if (err)
748 log_warnx("%s: sending packfile: %s",
749 repo_read.title, err->msg);
750 break;
751 default:
752 log_debug("%s: unexpected imsg %d", repo_read.title,
753 imsg.hdr.type);
754 break;
757 imsg_free(&imsg);
760 if (!shut && check_cancelled(NULL) == NULL) {
761 if (err &&
762 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
763 client->id, err) == -1) {
764 log_warnx("could not send error to parent: %s",
765 err->msg);
767 gotd_imsg_event_add(iev);
768 } else {
769 /* This pipe is dead. Remove its event handler */
770 event_del(&iev->ev);
771 event_loopexit(NULL);
775 static const struct got_error *
776 recv_connect(struct imsg *imsg)
778 struct gotd_imsgev *iev = &repo_read.session_iev;
779 size_t datalen;
781 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
782 if (datalen != 0)
783 return got_error(GOT_ERR_PRIVSEP_LEN);
784 if (imsg->fd == -1)
785 return got_error(GOT_ERR_PRIVSEP_NO_FD);
787 if (repo_read.session_fd != -1)
788 return got_error(GOT_ERR_PRIVSEP_MSG);
790 repo_read.session_fd = imsg->fd;
792 imsg_init(&iev->ibuf, repo_read.session_fd);
793 iev->handler = repo_read_dispatch_session;
794 iev->events = EV_READ;
795 iev->handler_arg = NULL;
796 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
797 repo_read_dispatch_session, iev);
798 gotd_imsg_event_add(iev);
800 return NULL;
803 static void
804 repo_read_dispatch(int fd, short event, void *arg)
806 const struct got_error *err = NULL;
807 struct gotd_imsgev *iev = arg;
808 struct imsgbuf *ibuf = &iev->ibuf;
809 struct imsg imsg;
810 ssize_t n;
811 int shut = 0;
812 struct repo_read_client *client = &repo_read_client;
814 if (event & EV_READ) {
815 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
816 fatal("imsg_read error");
817 if (n == 0) /* Connection closed. */
818 shut = 1;
821 if (event & EV_WRITE) {
822 n = msgbuf_write(&ibuf->w);
823 if (n == -1 && errno != EAGAIN)
824 fatal("msgbuf_write");
825 if (n == 0) /* Connection closed. */
826 shut = 1;
829 while (err == NULL && check_cancelled(NULL) == NULL) {
830 if ((n = imsg_get(ibuf, &imsg)) == -1)
831 fatal("%s: imsg_get", __func__);
832 if (n == 0) /* No more messages. */
833 break;
835 switch (imsg.hdr.type) {
836 case GOTD_IMSG_CONNECT_REPO_CHILD:
837 err = recv_connect(&imsg);
838 break;
839 default:
840 log_debug("%s: unexpected imsg %d", repo_read.title,
841 imsg.hdr.type);
842 break;
845 imsg_free(&imsg);
848 if (!shut && check_cancelled(NULL) == NULL) {
849 if (err &&
850 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
851 client->id, err) == -1) {
852 log_warnx("could not send error to parent: %s",
853 err->msg);
855 gotd_imsg_event_add(iev);
856 } else {
857 /* This pipe is dead. Remove its event handler */
858 event_del(&iev->ev);
859 event_loopexit(NULL);
863 void
864 repo_read_main(const char *title, const char *repo_path,
865 int *pack_fds, int *temp_fds)
867 const struct got_error *err = NULL;
868 struct repo_read_client *client = &repo_read_client;
869 struct gotd_imsgev iev;
871 client->fd = -1;
872 client->delta_cache_fd = -1;
873 client->pack_pipe = -1;
874 client->have_ids = got_object_idset_alloc();
875 if (client->have_ids == NULL) {
876 err = got_error_from_errno("got_object_idset_alloc");
877 goto done;
879 client->want_ids = got_object_idset_alloc();
880 if (client->want_ids == NULL) {
881 err = got_error_from_errno("got_object_idset_alloc");
882 goto done;
885 repo_read.title = title;
886 repo_read.pid = getpid();
887 repo_read.pack_fds = pack_fds;
888 repo_read.temp_fds = temp_fds;
889 repo_read.session_fd = -1;
890 repo_read.session_iev.ibuf.fd = -1;
892 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
893 if (err)
894 goto done;
895 if (!got_repo_is_bare(repo_read.repo)) {
896 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
897 "bare git repository required");
898 goto done;
901 got_repo_temp_fds_set(repo_read.repo, temp_fds);
903 signal(SIGINT, catch_sigint);
904 signal(SIGTERM, catch_sigterm);
905 signal(SIGPIPE, SIG_IGN);
906 signal(SIGHUP, SIG_IGN);
908 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
909 iev.handler = repo_read_dispatch;
910 iev.events = EV_READ;
911 iev.handler_arg = NULL;
912 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
914 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
915 PROC_REPO_READ, -1, NULL, 0) == -1) {
916 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
917 goto done;
920 event_dispatch();
921 done:
922 if (err)
923 log_warnx("%s: %s", title, err->msg);
924 repo_read_shutdown();
927 void
928 repo_read_shutdown(void)
930 struct repo_read_client *client = &repo_read_client;
932 log_debug("%s: shutting down", repo_read.title);
934 if (client->have_ids)
935 got_object_idset_free(client->have_ids);
936 if (client->want_ids)
937 got_object_idset_free(client->want_ids);
938 if (client->fd != -1)
939 close(client->fd);
940 if (client->delta_cache_fd != -1)
941 close(client->delta_cache_fd);
942 if (client->pack_pipe != -1)
943 close(client->pack_pipe);
945 if (repo_read.repo)
946 got_repo_close(repo_read.repo);
947 got_repo_pack_fds_close(repo_read.pack_fds);
948 got_repo_temp_fds_close(repo_read.temp_fds);
949 if (repo_read.session_fd != -1)
950 close(repo_read.session_fd);
951 exit(0);