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 int refs_listed;
68 } repo_read;
70 static struct repo_read_client {
71 uint32_t id;
72 int fd;
73 int delta_cache_fd;
74 int report_progress;
75 int pack_pipe;
76 struct got_object_idset *want_ids;
77 struct got_object_idset *have_ids;
78 } repo_read_client;
80 static volatile sig_atomic_t sigint_received;
81 static volatile sig_atomic_t sigterm_received;
83 static void
84 catch_sigint(int signo)
85 {
86 sigint_received = 1;
87 }
89 static void
90 catch_sigterm(int signo)
91 {
92 sigterm_received = 1;
93 }
95 static const struct got_error *
96 check_cancelled(void *arg)
97 {
98 if (sigint_received || sigterm_received)
99 return got_error(GOT_ERR_CANCELLED);
101 return NULL;
104 static const struct got_error *
105 send_symref(struct got_reference *symref, struct got_object_id *target_id,
106 struct imsgbuf *ibuf)
108 const struct got_error *err = NULL;
109 struct gotd_imsg_symref isymref;
110 const char *refname = got_ref_get_name(symref);
111 const char *target = got_ref_get_symref_target(symref);
112 size_t len;
113 struct ibuf *wbuf;
115 memset(&isymref, 0, sizeof(isymref));
116 isymref.name_len = strlen(refname);
117 isymref.target_len = strlen(target);
118 memcpy(isymref.target_id, target_id->hash, sizeof(isymref.target_id));
120 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
121 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
122 err = got_error(GOT_ERR_NO_SPACE);
123 goto done;
126 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
127 if (wbuf == NULL) {
128 err = got_error_from_errno("imsg_create SYMREF");
129 goto done;
132 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
134 goto done;
136 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
138 goto done;
140 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
141 err = got_error_from_errno("imsg_add SYMREF");
142 goto done;
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->hash, 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 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->hash, 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 imsg_close(ibuf, wbuf);
245 err = got_object_open(&obj, repo_read.repo, id);
246 if (err)
247 goto done;
248 if (obj->type == GOT_OBJ_TYPE_TAG)
249 err = send_peeled_tag_ref(ref, obj, ibuf);
250 done:
251 if (obj)
252 got_object_close(obj);
253 free(id);
254 return err;
257 static const struct got_error *
258 list_refs(struct imsg *imsg)
260 const struct got_error *err;
261 struct repo_read_client *client = &repo_read_client;
262 struct got_reflist_head refs;
263 struct got_reflist_entry *re;
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 != 0)
278 return got_error(GOT_ERR_PRIVSEP_LEN);
280 if (repo_read.refs_listed) {
281 return got_error_msg(GOT_ERR_CLIENT_ID,
282 "duplicate list-refs request");
284 repo_read.refs_listed = 1;
286 client->fd = client_fd;
288 imsg_init(&ibuf, client_fd);
290 err = got_ref_list(&refs, repo_read.repo, "",
291 got_ref_cmp_by_name, NULL);
292 if (err)
293 return err;
295 memset(&irefs, 0, sizeof(irefs));
296 TAILQ_FOREACH(re, &refs, entry) {
297 struct got_object_id *id;
298 int obj_type;
300 if (got_ref_is_symbolic(re->ref)) {
301 const char *refname = got_ref_get_name(re->ref);
302 if (strcmp(refname, GOT_REF_HEAD) != 0)
303 continue;
304 err = got_ref_resolve(&head_target_id, repo_read.repo,
305 re->ref);
306 if (err) {
307 if (err->code != GOT_ERR_NOT_REF)
308 return err;
309 /*
310 * HEAD points to a non-existent branch.
311 * Do not advertise it.
312 * Matches git-daemon's behaviour.
313 */
314 head_target_id = NULL;
315 err = NULL;
316 } else
317 irefs.nrefs++;
318 continue;
321 irefs.nrefs++;
323 /* Account for a peeled tag refs. */
324 err = got_ref_resolve(&id, repo_read.repo, re->ref);
325 if (err)
326 goto done;
327 err = got_object_get_type(&obj_type, repo_read.repo, id);
328 free(id);
329 if (err)
330 goto done;
331 if (obj_type == GOT_OBJ_TYPE_TAG)
332 irefs.nrefs++;
335 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
336 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
337 err = got_error_from_errno("imsg_compose REFLIST");
338 goto done;
341 /*
342 * Send the HEAD symref first. In Git-protocol versions < 2
343 * the HEAD symref must be announced on the initial line of
344 * the server's ref advertisement.
345 * For now, we do not advertise symrefs other than HEAD.
346 */
347 TAILQ_FOREACH(re, &refs, entry) {
348 if (!got_ref_is_symbolic(re->ref) ||
349 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
350 head_target_id == NULL)
351 continue;
352 err = send_symref(re->ref, head_target_id, &ibuf);
353 if (err)
354 goto done;
355 break;
357 TAILQ_FOREACH(re, &refs, entry) {
358 if (got_ref_is_symbolic(re->ref))
359 continue;
360 err = send_ref(re->ref, &ibuf);
361 if (err)
362 goto done;
365 err = gotd_imsg_flush(&ibuf);
366 done:
367 got_ref_list_free(&refs);
368 imsg_clear(&ibuf);
369 return err;
372 static const struct got_error *
373 append_object_id(struct got_object_id *id, void *data, void *arg)
375 struct gotd_object_id_array *array = arg;
376 const size_t alloc_chunksz = 256;
378 if (array->ids == NULL) {
379 array->ids = reallocarray(NULL, alloc_chunksz,
380 sizeof(*array->ids));
381 if (array->ids == NULL)
382 return got_error_from_errno("reallocarray");
383 array->nalloc = alloc_chunksz;
384 array->nids = 0;
385 } else if (array->nalloc <= array->nids) {
386 struct got_object_id **new;
387 new = recallocarray(array->ids, array->nalloc,
388 array->nalloc + alloc_chunksz, sizeof(*new));
389 if (new == NULL)
390 return got_error_from_errno("recallocarray");
391 array->ids = new;
392 array->nalloc += alloc_chunksz;
395 array->ids[array->nids] = id;
396 array->nids++;
397 return NULL;
400 static const struct got_error *
401 recv_want(struct imsg *imsg)
403 const struct got_error *err;
404 struct repo_read_client *client = &repo_read_client;
405 struct gotd_imsg_want iwant;
406 size_t datalen;
407 char hex[SHA1_DIGEST_STRING_LENGTH];
408 struct got_object_id id;
409 int obj_type;
410 struct imsgbuf ibuf;
412 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
413 if (datalen != sizeof(iwant))
414 return got_error(GOT_ERR_PRIVSEP_LEN);
415 memcpy(&iwant, imsg->data, sizeof(iwant));
417 memset(&id, 0, sizeof(id));
418 memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
420 if (log_getverbose() > 0 &&
421 got_object_id_hex(&id, hex, sizeof(hex)))
422 log_debug("client wants %s", hex);
424 imsg_init(&ibuf, client->fd);
426 err = got_object_get_type(&obj_type, repo_read.repo, &id);
427 if (err)
428 return err;
430 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
431 obj_type != GOT_OBJ_TYPE_TAG)
432 return got_error(GOT_ERR_OBJ_TYPE);
434 if (!got_object_idset_contains(client->want_ids, &id)) {
435 err = got_object_idset_add(client->want_ids, &id, NULL);
436 if (err)
437 return err;
440 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
441 imsg_clear(&ibuf);
442 return err;
445 static const struct got_error *
446 recv_have(struct imsg *imsg)
448 const struct got_error *err;
449 struct repo_read_client *client = &repo_read_client;
450 struct gotd_imsg_have ihave;
451 size_t datalen;
452 char hex[SHA1_DIGEST_STRING_LENGTH];
453 struct got_object_id id;
454 int obj_type;
455 struct imsgbuf ibuf;
457 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
458 if (datalen != sizeof(ihave))
459 return got_error(GOT_ERR_PRIVSEP_LEN);
460 memcpy(&ihave, imsg->data, sizeof(ihave));
462 memset(&id, 0, sizeof(id));
463 memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
465 if (log_getverbose() > 0 &&
466 got_object_id_hex(&id, hex, sizeof(hex)))
467 log_debug("client has %s", hex);
469 imsg_init(&ibuf, client->fd);
471 err = got_object_get_type(&obj_type, repo_read.repo, &id);
472 if (err) {
473 if (err->code == GOT_ERR_NO_OBJ) {
474 gotd_imsg_send_nak(&id, &ibuf,
475 PROC_REPO_READ, repo_read.pid);
476 err = NULL;
478 goto done;
481 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
482 obj_type != GOT_OBJ_TYPE_TAG) {
483 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
484 err = got_error(GOT_ERR_OBJ_TYPE);
485 goto done;
488 if (!got_object_idset_contains(client->have_ids, &id)) {
489 err = got_object_idset_add(client->have_ids, &id, NULL);
490 if (err)
491 goto done;
494 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
495 done:
496 imsg_clear(&ibuf);
497 return err;
500 struct repo_read_pack_progress_arg {
501 int report_progress;
502 struct imsgbuf *ibuf;
503 int sent_ready;
504 };
506 static const struct got_error *
507 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
508 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
509 int nobj_written)
511 struct repo_read_pack_progress_arg *a = arg;
512 struct gotd_imsg_packfile_progress iprog;
513 int ret;
515 if (!a->report_progress)
516 return NULL;
517 if (packfile_size > 0 && a->sent_ready)
518 return NULL;
520 memset(&iprog, 0, sizeof(iprog));
521 iprog.ncolored = ncolored;
522 iprog.nfound = nfound;
523 iprog.ntrees = ntrees;
524 iprog.packfile_size = packfile_size;
525 iprog.ncommits = ncommits;
526 iprog.nobj_total = nobj_total;
527 iprog.nobj_deltify = nobj_deltify;
528 iprog.nobj_written = nobj_written;
530 /* Using synchronous writes since we are blocking the event loop. */
531 if (packfile_size == 0) {
532 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
533 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
534 if (ret == -1) {
535 return got_error_from_errno("imsg compose "
536 "PACKFILE_PROGRESS");
538 } else {
539 a->sent_ready = 1;
540 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
541 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
542 if (ret == -1) {
543 return got_error_from_errno("imsg compose "
544 "PACKFILE_READY");
548 return gotd_imsg_flush(a->ibuf);
551 static const struct got_error *
552 receive_delta_cache_fd(struct imsg *imsg,
553 struct gotd_imsgev *iev)
555 struct repo_read_client *client = &repo_read_client;
556 struct gotd_imsg_send_packfile ireq;
557 size_t datalen;
559 log_debug("receiving delta cache file");
561 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
562 if (datalen != sizeof(ireq))
563 return got_error(GOT_ERR_PRIVSEP_LEN);
564 memcpy(&ireq, imsg->data, sizeof(ireq));
566 if (client->delta_cache_fd != -1)
567 return got_error(GOT_ERR_PRIVSEP_MSG);
569 client->delta_cache_fd = imsg_get_fd(imsg);
570 if (client->delta_cache_fd == -1)
571 return got_error(GOT_ERR_PRIVSEP_NO_FD);
573 client->report_progress = ireq.report_progress;
574 return NULL;
577 static const struct got_error *
578 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
580 struct repo_read_client *client = &repo_read_client;
581 size_t datalen;
583 log_debug("receiving pack pipe descriptor");
585 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
586 if (datalen != 0)
587 return got_error(GOT_ERR_PRIVSEP_LEN);
589 if (client->pack_pipe != -1)
590 return got_error(GOT_ERR_PRIVSEP_MSG);
592 client->pack_pipe = imsg_get_fd(imsg);
593 if (client->pack_pipe == -1)
594 return got_error(GOT_ERR_PRIVSEP_NO_FD);
596 return NULL;
599 static const struct got_error *
600 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
602 const struct got_error *err = NULL;
603 struct repo_read_client *client = &repo_read_client;
604 struct got_object_id packhash;
605 char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
606 FILE *delta_cache = NULL;
607 struct imsgbuf ibuf;
608 struct repo_read_pack_progress_arg pa;
609 struct got_ratelimit rl;
610 struct gotd_object_id_array want_ids;
611 struct gotd_object_id_array have_ids;
613 log_debug("packfile request received");
615 memset(&want_ids, 0, sizeof(want_ids));
616 memset(&have_ids, 0, sizeof(have_ids));
618 got_ratelimit_init(&rl, 2, 0);
620 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
621 return got_error(GOT_ERR_PRIVSEP_NO_FD);
623 imsg_init(&ibuf, client->fd);
625 delta_cache = fdopen(client->delta_cache_fd, "w+");
626 if (delta_cache == NULL) {
627 err = got_error_from_errno("fdopen");
628 goto done;
630 client->delta_cache_fd = -1;
632 memset(&pa, 0, sizeof(pa));
633 pa.ibuf = &ibuf;
634 pa.report_progress = client->report_progress;
636 err = got_object_idset_for_each(client->want_ids,
637 append_object_id, &want_ids);
638 if (err)
639 goto done;
640 err = got_object_idset_for_each(client->have_ids,
641 append_object_id, &have_ids);
642 if (err)
643 goto done;
645 err = got_pack_create(&packhash, client->pack_pipe, delta_cache,
646 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
647 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
648 check_cancelled, NULL);
649 if (err)
650 goto done;
652 if (log_getverbose() > 0 &&
653 got_hash_digest_to_str(packhash.hash, hex, sizeof(hex),
654 packhash.algo))
655 log_debug("sent pack-%s.pack", hex);
657 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
658 PROC_REPO_READ, -1, NULL, 0) == -1)
659 err = got_error_from_errno("imsg compose PACKFILE_DONE");
660 done:
661 if (client->delta_cache_fd != -1 &&
662 close(client->delta_cache_fd) == -1 && err == NULL)
663 err = got_error_from_errno("close");
664 client->delta_cache_fd = -1;
665 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
666 err = got_error_from_errno("fclose");
667 imsg_clear(&ibuf);
668 free(want_ids.ids);
669 free(have_ids.ids);
670 return err;
673 static void
674 repo_read_dispatch_session(int fd, short event, void *arg)
676 const struct got_error *err = NULL;
677 struct gotd_imsgev *iev = arg;
678 struct imsgbuf *ibuf = &iev->ibuf;
679 struct imsg imsg;
680 ssize_t n;
681 int shut = 0;
682 struct repo_read_client *client = &repo_read_client;
684 if (event & EV_READ) {
685 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
686 fatal("imsg_read error");
687 if (n == 0) /* Connection closed. */
688 shut = 1;
691 if (event & EV_WRITE) {
692 n = msgbuf_write(&ibuf->w);
693 if (n == -1 && errno != EAGAIN)
694 fatal("msgbuf_write");
695 if (n == 0) /* Connection closed. */
696 shut = 1;
699 while (err == NULL && check_cancelled(NULL) == NULL) {
700 if ((n = imsg_get(ibuf, &imsg)) == -1)
701 fatal("%s: imsg_get", __func__);
702 if (n == 0) /* No more messages. */
703 break;
705 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
706 !repo_read.refs_listed) {
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
708 break;
711 switch (imsg.hdr.type) {
712 case GOTD_IMSG_LIST_REFS_INTERNAL:
713 err = list_refs(&imsg);
714 if (err)
715 log_warnx("ls-refs: %s", err->msg);
716 break;
717 case GOTD_IMSG_WANT:
718 err = recv_want(&imsg);
719 if (err)
720 log_warnx("want-line: %s", err->msg);
721 break;
722 case GOTD_IMSG_HAVE:
723 err = recv_have(&imsg);
724 if (err)
725 log_warnx("have-line: %s", err->msg);
726 break;
727 case GOTD_IMSG_SEND_PACKFILE:
728 err = receive_delta_cache_fd(&imsg, iev);
729 if (err)
730 log_warnx("receiving delta cache: %s",
731 err->msg);
732 break;
733 case GOTD_IMSG_PACKFILE_PIPE:
734 err = receive_pack_pipe(&imsg, iev);
735 if (err) {
736 log_warnx("receiving pack pipe: %s", err->msg);
737 break;
739 err = send_packfile(&imsg, iev);
740 if (err)
741 log_warnx("sending packfile: %s", err->msg);
742 break;
743 default:
744 log_debug("unexpected imsg %d", imsg.hdr.type);
745 break;
748 imsg_free(&imsg);
751 if (!shut && check_cancelled(NULL) == NULL) {
752 if (err &&
753 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
754 client->id, err) == -1) {
755 log_warnx("could not send error to parent: %s",
756 err->msg);
758 gotd_imsg_event_add(iev);
759 } else {
760 /* This pipe is dead. Remove its event handler */
761 event_del(&iev->ev);
762 event_loopexit(NULL);
766 static const struct got_error *
767 recv_connect(struct imsg *imsg)
769 struct gotd_imsgev *iev = &repo_read.session_iev;
770 size_t datalen;
772 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
773 if (datalen != 0)
774 return got_error(GOT_ERR_PRIVSEP_LEN);
776 if (repo_read.session_fd != -1)
777 return got_error(GOT_ERR_PRIVSEP_MSG);
779 repo_read.session_fd = imsg_get_fd(imsg);
780 if (repo_read.session_fd == -1)
781 return got_error(GOT_ERR_PRIVSEP_NO_FD);
783 imsg_init(&iev->ibuf, repo_read.session_fd);
784 iev->handler = repo_read_dispatch_session;
785 iev->events = EV_READ;
786 iev->handler_arg = NULL;
787 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
788 repo_read_dispatch_session, iev);
789 gotd_imsg_event_add(iev);
791 return NULL;
794 static void
795 repo_read_dispatch(int fd, short event, void *arg)
797 const struct got_error *err = NULL;
798 struct gotd_imsgev *iev = arg;
799 struct imsgbuf *ibuf = &iev->ibuf;
800 struct imsg imsg;
801 ssize_t n;
802 int shut = 0;
803 struct repo_read_client *client = &repo_read_client;
805 if (event & EV_READ) {
806 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
807 fatal("imsg_read error");
808 if (n == 0) /* Connection closed. */
809 shut = 1;
812 if (event & EV_WRITE) {
813 n = msgbuf_write(&ibuf->w);
814 if (n == -1 && errno != EAGAIN)
815 fatal("msgbuf_write");
816 if (n == 0) /* Connection closed. */
817 shut = 1;
820 while (err == NULL && check_cancelled(NULL) == NULL) {
821 if ((n = imsg_get(ibuf, &imsg)) == -1)
822 fatal("%s: imsg_get", __func__);
823 if (n == 0) /* No more messages. */
824 break;
826 switch (imsg.hdr.type) {
827 case GOTD_IMSG_CONNECT_REPO_CHILD:
828 err = recv_connect(&imsg);
829 break;
830 default:
831 log_debug("unexpected imsg %d", imsg.hdr.type);
832 break;
835 imsg_free(&imsg);
838 if (!shut && check_cancelled(NULL) == NULL) {
839 if (err &&
840 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
841 client->id, err) == -1) {
842 log_warnx("could not send error to parent: %s",
843 err->msg);
845 gotd_imsg_event_add(iev);
846 } else {
847 /* This pipe is dead. Remove its event handler */
848 event_del(&iev->ev);
849 event_loopexit(NULL);
853 void
854 repo_read_main(const char *title, const char *repo_path,
855 int *pack_fds, int *temp_fds)
857 const struct got_error *err = NULL;
858 struct repo_read_client *client = &repo_read_client;
859 struct gotd_imsgev iev;
861 client->fd = -1;
862 client->delta_cache_fd = -1;
863 client->pack_pipe = -1;
864 client->have_ids = got_object_idset_alloc();
865 if (client->have_ids == NULL) {
866 err = got_error_from_errno("got_object_idset_alloc");
867 goto done;
869 client->want_ids = got_object_idset_alloc();
870 if (client->want_ids == NULL) {
871 err = got_error_from_errno("got_object_idset_alloc");
872 goto done;
875 repo_read.title = title;
876 repo_read.pid = getpid();
877 repo_read.pack_fds = pack_fds;
878 repo_read.temp_fds = temp_fds;
879 repo_read.session_fd = -1;
880 repo_read.session_iev.ibuf.fd = -1;
882 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
883 if (err)
884 goto done;
885 if (!got_repo_is_bare(repo_read.repo)) {
886 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
887 "bare git repository required");
888 goto done;
890 if (got_repo_get_object_format(repo_read.repo) != GOT_HASH_SHA1) {
891 err = got_error_msg(GOT_ERR_NOT_IMPL,
892 "sha256 object IDs unsupported in network protocol");
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("%s: shutting down", repo_read.title);
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);