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 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 imsg_close(ibuf, wbuf);
201 done:
202 got_object_tag_close(tag);
203 return err;
206 static const struct got_error *
207 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
209 const struct got_error *err;
210 const char *refname = got_ref_get_name(ref);
211 size_t namelen;
212 struct got_object_id *id = NULL;
213 struct got_object *obj = NULL;
214 size_t len;
215 struct ibuf *wbuf;
217 namelen = strlen(refname);
219 len = sizeof(struct gotd_imsg_ref) + namelen;
220 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 return got_error(GOT_ERR_NO_SPACE);
223 err = got_ref_resolve(&id, repo_read.repo, ref);
224 if (err)
225 return err;
227 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
228 repo_read.pid, len);
229 if (wbuf == NULL) {
230 err = got_error_from_errno("imsg_create REF");
231 goto done;
234 /* Keep in sync with struct gotd_imsg_ref definition. */
235 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
236 return got_error_from_errno("imsg_add REF");
237 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf, refname, namelen) == -1)
240 return got_error_from_errno("imsg_add REF");
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;
268 struct got_object_id *head_target_id = NULL;
270 TAILQ_INIT(&refs);
272 client_fd = imsg_get_fd(imsg);
273 if (client_fd == -1)
274 return got_error(GOT_ERR_PRIVSEP_NO_FD);
276 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
277 if (datalen != sizeof(ireq))
278 return got_error(GOT_ERR_PRIVSEP_LEN);
279 memcpy(&ireq, imsg->data, sizeof(ireq));
281 if (ireq.client_id == 0)
282 return got_error(GOT_ERR_CLIENT_ID);
283 if (client->id != 0) {
284 return got_error_msg(GOT_ERR_CLIENT_ID,
285 "duplicate list-refs request");
287 client->id = ireq.client_id;
288 client->fd = client_fd;
290 imsg_init(&ibuf, client_fd);
292 err = got_ref_list(&refs, repo_read.repo, "",
293 got_ref_cmp_by_name, NULL);
294 if (err)
295 return err;
297 memset(&irefs, 0, sizeof(irefs));
298 TAILQ_FOREACH(re, &refs, entry) {
299 struct got_object_id *id;
300 int obj_type;
302 if (got_ref_is_symbolic(re->ref)) {
303 const char *refname = got_ref_get_name(re->ref);
304 if (strcmp(refname, GOT_REF_HEAD) != 0)
305 continue;
306 err = got_ref_resolve(&head_target_id, repo_read.repo,
307 re->ref);
308 if (err) {
309 if (err->code != GOT_ERR_NOT_REF)
310 return err;
311 /*
312 * HEAD points to a non-existent branch.
313 * Do not advertise it.
314 * Matches git-daemon's behaviour.
315 */
316 head_target_id = NULL;
317 err = NULL;
318 } else
319 irefs.nrefs++;
320 continue;
323 irefs.nrefs++;
325 /* Account for a peeled tag refs. */
326 err = got_ref_resolve(&id, repo_read.repo, re->ref);
327 if (err)
328 goto done;
329 err = got_object_get_type(&obj_type, repo_read.repo, id);
330 free(id);
331 if (err)
332 goto done;
333 if (obj_type == GOT_OBJ_TYPE_TAG)
334 irefs.nrefs++;
337 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
338 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
339 err = got_error_from_errno("imsg_compose REFLIST");
340 goto done;
343 /*
344 * Send the HEAD symref first. In Git-protocol versions < 2
345 * the HEAD symref must be announced on the initial line of
346 * the server's ref advertisement.
347 * For now, we do not advertise symrefs other than HEAD.
348 */
349 TAILQ_FOREACH(re, &refs, entry) {
350 if (!got_ref_is_symbolic(re->ref) ||
351 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
352 head_target_id == NULL)
353 continue;
354 err = send_symref(re->ref, head_target_id, &ibuf);
355 if (err)
356 goto done;
357 break;
359 TAILQ_FOREACH(re, &refs, entry) {
360 if (got_ref_is_symbolic(re->ref))
361 continue;
362 err = send_ref(re->ref, &ibuf);
363 if (err)
364 goto done;
367 err = gotd_imsg_flush(&ibuf);
368 done:
369 got_ref_list_free(&refs);
370 imsg_clear(&ibuf);
371 return err;
374 static const struct got_error *
375 append_object_id(struct got_object_id *id, void *data, void *arg)
377 struct gotd_object_id_array *array = arg;
378 const size_t alloc_chunksz = 256;
380 if (array->ids == NULL) {
381 array->ids = reallocarray(NULL, alloc_chunksz,
382 sizeof(*array->ids));
383 if (array->ids == NULL)
384 return got_error_from_errno("reallocarray");
385 array->nalloc = alloc_chunksz;
386 array->nids = 0;
387 } else if (array->nalloc <= array->nids) {
388 struct got_object_id **new;
389 new = recallocarray(array->ids, array->nalloc,
390 array->nalloc + alloc_chunksz, sizeof(*new));
391 if (new == NULL)
392 return got_error_from_errno("recallocarray");
393 array->ids = new;
394 array->nalloc += alloc_chunksz;
397 array->ids[array->nids] = id;
398 array->nids++;
399 return NULL;
402 static const struct got_error *
403 recv_want(struct imsg *imsg)
405 const struct got_error *err;
406 struct repo_read_client *client = &repo_read_client;
407 struct gotd_imsg_want iwant;
408 size_t datalen;
409 char hex[SHA1_DIGEST_STRING_LENGTH];
410 struct got_object_id id;
411 int obj_type;
412 struct imsgbuf ibuf;
414 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
415 if (datalen != sizeof(iwant))
416 return got_error(GOT_ERR_PRIVSEP_LEN);
417 memcpy(&iwant, imsg->data, sizeof(iwant));
419 memset(&id, 0, sizeof(id));
420 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
422 if (log_getverbose() > 0 &&
423 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
424 log_debug("client wants %s", hex);
426 imsg_init(&ibuf, client->fd);
428 err = got_object_get_type(&obj_type, repo_read.repo, &id);
429 if (err)
430 return err;
432 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
433 obj_type != GOT_OBJ_TYPE_TAG)
434 return got_error(GOT_ERR_OBJ_TYPE);
436 if (!got_object_idset_contains(client->want_ids, &id)) {
437 err = got_object_idset_add(client->want_ids, &id, NULL);
438 if (err)
439 return err;
442 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
443 imsg_clear(&ibuf);
444 return err;
447 static const struct got_error *
448 recv_have(struct imsg *imsg)
450 const struct got_error *err;
451 struct repo_read_client *client = &repo_read_client;
452 struct gotd_imsg_have ihave;
453 size_t datalen;
454 char hex[SHA1_DIGEST_STRING_LENGTH];
455 struct got_object_id id;
456 int obj_type;
457 struct imsgbuf ibuf;
459 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
460 if (datalen != sizeof(ihave))
461 return got_error(GOT_ERR_PRIVSEP_LEN);
462 memcpy(&ihave, imsg->data, sizeof(ihave));
464 memset(&id, 0, sizeof(id));
465 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
467 if (log_getverbose() > 0 &&
468 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
469 log_debug("client has %s", hex);
471 imsg_init(&ibuf, client->fd);
473 err = got_object_get_type(&obj_type, repo_read.repo, &id);
474 if (err) {
475 if (err->code == GOT_ERR_NO_OBJ) {
476 gotd_imsg_send_nak(&id, &ibuf,
477 PROC_REPO_READ, repo_read.pid);
478 err = NULL;
480 goto done;
483 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
484 obj_type != GOT_OBJ_TYPE_TAG) {
485 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
486 err = got_error(GOT_ERR_OBJ_TYPE);
487 goto done;
490 if (!got_object_idset_contains(client->have_ids, &id)) {
491 err = got_object_idset_add(client->have_ids, &id, NULL);
492 if (err)
493 goto done;
496 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
497 done:
498 imsg_clear(&ibuf);
499 return err;
502 struct repo_read_pack_progress_arg {
503 int report_progress;
504 struct imsgbuf *ibuf;
505 int sent_ready;
506 };
508 static const struct got_error *
509 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
510 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
511 int nobj_written)
513 struct repo_read_pack_progress_arg *a = arg;
514 struct gotd_imsg_packfile_progress iprog;
515 int ret;
517 if (!a->report_progress)
518 return NULL;
519 if (packfile_size > 0 && a->sent_ready)
520 return NULL;
522 memset(&iprog, 0, sizeof(iprog));
523 iprog.ncolored = ncolored;
524 iprog.nfound = nfound;
525 iprog.ntrees = ntrees;
526 iprog.packfile_size = packfile_size;
527 iprog.ncommits = ncommits;
528 iprog.nobj_total = nobj_total;
529 iprog.nobj_deltify = nobj_deltify;
530 iprog.nobj_written = nobj_written;
532 /* Using synchronous writes since we are blocking the event loop. */
533 if (packfile_size == 0) {
534 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
535 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
536 if (ret == -1) {
537 return got_error_from_errno("imsg compose "
538 "PACKFILE_PROGRESS");
540 } else {
541 a->sent_ready = 1;
542 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
543 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
544 if (ret == -1) {
545 return got_error_from_errno("imsg compose "
546 "PACKFILE_READY");
550 return gotd_imsg_flush(a->ibuf);
553 static const struct got_error *
554 receive_delta_cache_fd(struct imsg *imsg,
555 struct gotd_imsgev *iev)
557 struct repo_read_client *client = &repo_read_client;
558 struct gotd_imsg_send_packfile ireq;
559 size_t datalen;
561 log_debug("receiving delta cache file");
563 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
564 if (datalen != sizeof(ireq))
565 return got_error(GOT_ERR_PRIVSEP_LEN);
566 memcpy(&ireq, imsg->data, sizeof(ireq));
568 if (client->delta_cache_fd != -1)
569 return got_error(GOT_ERR_PRIVSEP_MSG);
571 client->delta_cache_fd = imsg_get_fd(imsg);
572 if (client->delta_cache_fd == -1)
573 return got_error(GOT_ERR_PRIVSEP_NO_FD);
575 client->report_progress = ireq.report_progress;
576 return NULL;
579 static const struct got_error *
580 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
582 struct repo_read_client *client = &repo_read_client;
583 struct gotd_imsg_packfile_pipe ireq;
584 size_t datalen;
586 log_debug("receiving pack pipe descriptor");
588 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
589 if (datalen != sizeof(ireq))
590 return got_error(GOT_ERR_PRIVSEP_LEN);
591 memcpy(&ireq, imsg->data, sizeof(ireq));
593 if (client->pack_pipe != -1)
594 return got_error(GOT_ERR_PRIVSEP_MSG);
596 client->pack_pipe = imsg_get_fd(imsg);
597 if (client->pack_pipe == -1)
598 return got_error(GOT_ERR_PRIVSEP_NO_FD);
600 return NULL;
603 static const struct got_error *
604 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
606 const struct got_error *err = NULL;
607 struct repo_read_client *client = &repo_read_client;
608 struct gotd_imsg_packfile_done idone;
609 uint8_t packsha1[SHA1_DIGEST_LENGTH];
610 char hex[SHA1_DIGEST_STRING_LENGTH];
611 FILE *delta_cache = NULL;
612 struct imsgbuf ibuf;
613 struct repo_read_pack_progress_arg pa;
614 struct got_ratelimit rl;
615 struct gotd_object_id_array want_ids;
616 struct gotd_object_id_array have_ids;
618 log_debug("packfile request received");
620 memset(&want_ids, 0, sizeof(want_ids));
621 memset(&have_ids, 0, sizeof(have_ids));
623 got_ratelimit_init(&rl, 2, 0);
625 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
626 return got_error(GOT_ERR_PRIVSEP_NO_FD);
628 imsg_init(&ibuf, client->fd);
630 delta_cache = fdopen(client->delta_cache_fd, "w+");
631 if (delta_cache == NULL) {
632 err = got_error_from_errno("fdopen");
633 goto done;
635 client->delta_cache_fd = -1;
637 memset(&pa, 0, sizeof(pa));
638 pa.ibuf = &ibuf;
639 pa.report_progress = client->report_progress;
641 err = got_object_idset_for_each(client->want_ids,
642 append_object_id, &want_ids);
643 if (err)
644 goto done;
645 err = got_object_idset_for_each(client->have_ids,
646 append_object_id, &have_ids);
647 if (err)
648 goto done;
650 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
651 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
652 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
653 check_cancelled, NULL);
654 if (err)
655 goto done;
657 if (log_getverbose() > 0 &&
658 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
659 log_debug("sent pack-%s.pack", hex);
661 memset(&idone, 0, sizeof(idone));
662 idone.client_id = client->id;
663 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
664 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
665 err = got_error_from_errno("imsg compose PACKFILE_DONE");
666 done:
667 if (client->delta_cache_fd != -1 &&
668 close(client->delta_cache_fd) == -1 && err == NULL)
669 err = got_error_from_errno("close");
670 client->delta_cache_fd = -1;
671 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
672 err = got_error_from_errno("fclose");
673 imsg_clear(&ibuf);
674 free(want_ids.ids);
675 free(have_ids.ids);
676 return err;
679 static void
680 repo_read_dispatch_session(int fd, short event, void *arg)
682 const struct got_error *err = NULL;
683 struct gotd_imsgev *iev = arg;
684 struct imsgbuf *ibuf = &iev->ibuf;
685 struct imsg imsg;
686 ssize_t n;
687 int shut = 0;
688 struct repo_read_client *client = &repo_read_client;
690 if (event & EV_READ) {
691 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
692 fatal("imsg_read error");
693 if (n == 0) /* Connection closed. */
694 shut = 1;
697 if (event & EV_WRITE) {
698 n = msgbuf_write(&ibuf->w);
699 if (n == -1 && errno != EAGAIN)
700 fatal("msgbuf_write");
701 if (n == 0) /* Connection closed. */
702 shut = 1;
705 while (err == NULL && check_cancelled(NULL) == NULL) {
706 if ((n = imsg_get(ibuf, &imsg)) == -1)
707 fatal("%s: imsg_get", __func__);
708 if (n == 0) /* No more messages. */
709 break;
711 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
712 client->id == 0) {
713 err = got_error(GOT_ERR_PRIVSEP_MSG);
714 break;
717 switch (imsg.hdr.type) {
718 case GOTD_IMSG_LIST_REFS_INTERNAL:
719 err = list_refs(&imsg);
720 if (err)
721 log_warnx("ls-refs: %s", err->msg);
722 break;
723 case GOTD_IMSG_WANT:
724 err = recv_want(&imsg);
725 if (err)
726 log_warnx("want-line: %s", err->msg);
727 break;
728 case GOTD_IMSG_HAVE:
729 err = recv_have(&imsg);
730 if (err)
731 log_warnx("have-line: %s", err->msg);
732 break;
733 case GOTD_IMSG_SEND_PACKFILE:
734 err = receive_delta_cache_fd(&imsg, iev);
735 if (err)
736 log_warnx("receiving delta cache: %s",
737 err->msg);
738 break;
739 case GOTD_IMSG_PACKFILE_PIPE:
740 err = receive_pack_pipe(&imsg, iev);
741 if (err) {
742 log_warnx("receiving pack pipe: %s", err->msg);
743 break;
745 err = send_packfile(&imsg, iev);
746 if (err)
747 log_warnx("sending packfile: %s", err->msg);
748 break;
749 default:
750 log_debug("unexpected imsg %d", imsg.hdr.type);
751 break;
754 imsg_free(&imsg);
757 if (!shut && check_cancelled(NULL) == NULL) {
758 if (err &&
759 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
760 client->id, err) == -1) {
761 log_warnx("could not send error to parent: %s",
762 err->msg);
764 gotd_imsg_event_add(iev);
765 } else {
766 /* This pipe is dead. Remove its event handler */
767 event_del(&iev->ev);
768 event_loopexit(NULL);
772 static const struct got_error *
773 recv_connect(struct imsg *imsg)
775 struct gotd_imsgev *iev = &repo_read.session_iev;
776 size_t datalen;
778 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
779 if (datalen != 0)
780 return got_error(GOT_ERR_PRIVSEP_LEN);
782 if (repo_read.session_fd != -1)
783 return got_error(GOT_ERR_PRIVSEP_MSG);
785 repo_read.session_fd = imsg_get_fd(imsg);
786 if (repo_read.session_fd == -1)
787 return got_error(GOT_ERR_PRIVSEP_NO_FD);
789 imsg_init(&iev->ibuf, repo_read.session_fd);
790 iev->handler = repo_read_dispatch_session;
791 iev->events = EV_READ;
792 iev->handler_arg = NULL;
793 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
794 repo_read_dispatch_session, iev);
795 gotd_imsg_event_add(iev);
797 return NULL;
800 static void
801 repo_read_dispatch(int fd, short event, void *arg)
803 const struct got_error *err = NULL;
804 struct gotd_imsgev *iev = arg;
805 struct imsgbuf *ibuf = &iev->ibuf;
806 struct imsg imsg;
807 ssize_t n;
808 int shut = 0;
809 struct repo_read_client *client = &repo_read_client;
811 if (event & EV_READ) {
812 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
813 fatal("imsg_read error");
814 if (n == 0) /* Connection closed. */
815 shut = 1;
818 if (event & EV_WRITE) {
819 n = msgbuf_write(&ibuf->w);
820 if (n == -1 && errno != EAGAIN)
821 fatal("msgbuf_write");
822 if (n == 0) /* Connection closed. */
823 shut = 1;
826 while (err == NULL && check_cancelled(NULL) == NULL) {
827 if ((n = imsg_get(ibuf, &imsg)) == -1)
828 fatal("%s: imsg_get", __func__);
829 if (n == 0) /* No more messages. */
830 break;
832 switch (imsg.hdr.type) {
833 case GOTD_IMSG_CONNECT_REPO_CHILD:
834 err = recv_connect(&imsg);
835 break;
836 default:
837 log_debug("unexpected imsg %d", imsg.hdr.type);
838 break;
841 imsg_free(&imsg);
844 if (!shut && check_cancelled(NULL) == NULL) {
845 if (err &&
846 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
847 client->id, err) == -1) {
848 log_warnx("could not send error to parent: %s",
849 err->msg);
851 gotd_imsg_event_add(iev);
852 } else {
853 /* This pipe is dead. Remove its event handler */
854 event_del(&iev->ev);
855 event_loopexit(NULL);
859 void
860 repo_read_main(const char *title, const char *repo_path,
861 int *pack_fds, int *temp_fds)
863 const struct got_error *err = NULL;
864 struct repo_read_client *client = &repo_read_client;
865 struct gotd_imsgev iev;
867 client->fd = -1;
868 client->delta_cache_fd = -1;
869 client->pack_pipe = -1;
870 client->have_ids = got_object_idset_alloc();
871 if (client->have_ids == NULL) {
872 err = got_error_from_errno("got_object_idset_alloc");
873 goto done;
875 client->want_ids = got_object_idset_alloc();
876 if (client->want_ids == NULL) {
877 err = got_error_from_errno("got_object_idset_alloc");
878 goto done;
881 repo_read.title = title;
882 repo_read.pid = getpid();
883 repo_read.pack_fds = pack_fds;
884 repo_read.temp_fds = temp_fds;
885 repo_read.session_fd = -1;
886 repo_read.session_iev.ibuf.fd = -1;
888 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
889 if (err)
890 goto done;
891 if (!got_repo_is_bare(repo_read.repo)) {
892 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
893 "bare git repository required");
894 goto done;
897 got_repo_temp_fds_set(repo_read.repo, temp_fds);
899 signal(SIGINT, catch_sigint);
900 signal(SIGTERM, catch_sigterm);
901 signal(SIGPIPE, SIG_IGN);
902 signal(SIGHUP, SIG_IGN);
904 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
905 iev.handler = repo_read_dispatch;
906 iev.events = EV_READ;
907 iev.handler_arg = NULL;
908 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
910 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
911 PROC_REPO_READ, -1, NULL, 0) == -1) {
912 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
913 goto done;
916 event_dispatch();
917 done:
918 if (err)
919 log_warnx("%s: %s", title, err->msg);
920 repo_read_shutdown();
923 void
924 repo_read_shutdown(void)
926 struct repo_read_client *client = &repo_read_client;
928 log_debug("shutting down");
930 if (client->have_ids)
931 got_object_idset_free(client->have_ids);
932 if (client->want_ids)
933 got_object_idset_free(client->want_ids);
934 if (client->fd != -1)
935 close(client->fd);
936 if (client->delta_cache_fd != -1)
937 close(client->delta_cache_fd);
938 if (client->pack_pipe != -1)
939 close(client->pack_pipe);
941 if (repo_read.repo)
942 got_repo_close(repo_read.repo);
943 got_repo_pack_fds_close(repo_read.pack_fds);
944 got_repo_temp_fds_close(repo_read.temp_fds);
945 if (repo_read.session_fd != -1)
946 close(repo_read.session_fd);
947 exit(0);