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->sha1, 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->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 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 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.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
420 if (log_getverbose() > 0 &&
421 got_sha1_digest_to_str(id.sha1, 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.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
465 if (log_getverbose() > 0 &&
466 got_sha1_digest_to_str(id.sha1, 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 uint8_t packsha1[SHA1_DIGEST_LENGTH];
605 char hex[SHA1_DIGEST_STRING_LENGTH];
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(packsha1, 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_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
654 log_debug("sent pack-%s.pack", hex);
656 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
657 PROC_REPO_READ, -1, NULL, 0) == -1)
658 err = got_error_from_errno("imsg compose PACKFILE_DONE");
659 done:
660 if (client->delta_cache_fd != -1 &&
661 close(client->delta_cache_fd) == -1 && err == NULL)
662 err = got_error_from_errno("close");
663 client->delta_cache_fd = -1;
664 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
665 err = got_error_from_errno("fclose");
666 imsg_clear(&ibuf);
667 free(want_ids.ids);
668 free(have_ids.ids);
669 return err;
672 static void
673 repo_read_dispatch_session(int fd, short event, void *arg)
675 const struct got_error *err = NULL;
676 struct gotd_imsgev *iev = arg;
677 struct imsgbuf *ibuf = &iev->ibuf;
678 struct imsg imsg;
679 ssize_t n;
680 int shut = 0;
681 struct repo_read_client *client = &repo_read_client;
683 if (event & EV_READ) {
684 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
685 fatal("imsg_read error");
686 if (n == 0) /* Connection closed. */
687 shut = 1;
690 if (event & EV_WRITE) {
691 n = msgbuf_write(&ibuf->w);
692 if (n == -1 && errno != EAGAIN)
693 fatal("msgbuf_write");
694 if (n == 0) /* Connection closed. */
695 shut = 1;
698 while (err == NULL && check_cancelled(NULL) == NULL) {
699 if ((n = imsg_get(ibuf, &imsg)) == -1)
700 fatal("%s: imsg_get", __func__);
701 if (n == 0) /* No more messages. */
702 break;
704 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
705 !repo_read.refs_listed) {
706 err = got_error(GOT_ERR_PRIVSEP_MSG);
707 break;
710 switch (imsg.hdr.type) {
711 case GOTD_IMSG_LIST_REFS_INTERNAL:
712 err = list_refs(&imsg);
713 if (err)
714 log_warnx("ls-refs: %s", err->msg);
715 break;
716 case GOTD_IMSG_WANT:
717 err = recv_want(&imsg);
718 if (err)
719 log_warnx("want-line: %s", err->msg);
720 break;
721 case GOTD_IMSG_HAVE:
722 err = recv_have(&imsg);
723 if (err)
724 log_warnx("have-line: %s", err->msg);
725 break;
726 case GOTD_IMSG_SEND_PACKFILE:
727 err = receive_delta_cache_fd(&imsg, iev);
728 if (err)
729 log_warnx("receiving delta cache: %s",
730 err->msg);
731 break;
732 case GOTD_IMSG_PACKFILE_PIPE:
733 err = receive_pack_pipe(&imsg, iev);
734 if (err) {
735 log_warnx("receiving pack pipe: %s", err->msg);
736 break;
738 err = send_packfile(&imsg, iev);
739 if (err)
740 log_warnx("sending packfile: %s", err->msg);
741 break;
742 default:
743 log_debug("unexpected imsg %d", imsg.hdr.type);
744 break;
747 imsg_free(&imsg);
750 if (!shut && check_cancelled(NULL) == NULL) {
751 if (err &&
752 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
753 client->id, err) == -1) {
754 log_warnx("could not send error to parent: %s",
755 err->msg);
757 gotd_imsg_event_add(iev);
758 } else {
759 /* This pipe is dead. Remove its event handler */
760 event_del(&iev->ev);
761 event_loopexit(NULL);
765 static const struct got_error *
766 recv_connect(struct imsg *imsg)
768 struct gotd_imsgev *iev = &repo_read.session_iev;
769 size_t datalen;
771 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
772 if (datalen != 0)
773 return got_error(GOT_ERR_PRIVSEP_LEN);
775 if (repo_read.session_fd != -1)
776 return got_error(GOT_ERR_PRIVSEP_MSG);
778 repo_read.session_fd = imsg_get_fd(imsg);
779 if (repo_read.session_fd == -1)
780 return got_error(GOT_ERR_PRIVSEP_NO_FD);
782 imsg_init(&iev->ibuf, repo_read.session_fd);
783 iev->handler = repo_read_dispatch_session;
784 iev->events = EV_READ;
785 iev->handler_arg = NULL;
786 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
787 repo_read_dispatch_session, iev);
788 gotd_imsg_event_add(iev);
790 return NULL;
793 static void
794 repo_read_dispatch(int fd, short event, void *arg)
796 const struct got_error *err = NULL;
797 struct gotd_imsgev *iev = arg;
798 struct imsgbuf *ibuf = &iev->ibuf;
799 struct imsg imsg;
800 ssize_t n;
801 int shut = 0;
802 struct repo_read_client *client = &repo_read_client;
804 if (event & EV_READ) {
805 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
806 fatal("imsg_read error");
807 if (n == 0) /* Connection closed. */
808 shut = 1;
811 if (event & EV_WRITE) {
812 n = msgbuf_write(&ibuf->w);
813 if (n == -1 && errno != EAGAIN)
814 fatal("msgbuf_write");
815 if (n == 0) /* Connection closed. */
816 shut = 1;
819 while (err == NULL && check_cancelled(NULL) == NULL) {
820 if ((n = imsg_get(ibuf, &imsg)) == -1)
821 fatal("%s: imsg_get", __func__);
822 if (n == 0) /* No more messages. */
823 break;
825 switch (imsg.hdr.type) {
826 case GOTD_IMSG_CONNECT_REPO_CHILD:
827 err = recv_connect(&imsg);
828 break;
829 default:
830 log_debug("unexpected imsg %d", imsg.hdr.type);
831 break;
834 imsg_free(&imsg);
837 if (!shut && check_cancelled(NULL) == NULL) {
838 if (err &&
839 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
840 client->id, err) == -1) {
841 log_warnx("could not send error to parent: %s",
842 err->msg);
844 gotd_imsg_event_add(iev);
845 } else {
846 /* This pipe is dead. Remove its event handler */
847 event_del(&iev->ev);
848 event_loopexit(NULL);
852 void
853 repo_read_main(const char *title, const char *repo_path,
854 int *pack_fds, int *temp_fds)
856 const struct got_error *err = NULL;
857 struct repo_read_client *client = &repo_read_client;
858 struct gotd_imsgev iev;
860 client->fd = -1;
861 client->delta_cache_fd = -1;
862 client->pack_pipe = -1;
863 client->have_ids = got_object_idset_alloc();
864 if (client->have_ids == NULL) {
865 err = got_error_from_errno("got_object_idset_alloc");
866 goto done;
868 client->want_ids = got_object_idset_alloc();
869 if (client->want_ids == NULL) {
870 err = got_error_from_errno("got_object_idset_alloc");
871 goto done;
874 repo_read.title = title;
875 repo_read.pid = getpid();
876 repo_read.pack_fds = pack_fds;
877 repo_read.temp_fds = temp_fds;
878 repo_read.session_fd = -1;
879 repo_read.session_iev.ibuf.fd = -1;
881 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
882 if (err)
883 goto done;
884 if (!got_repo_is_bare(repo_read.repo)) {
885 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
886 "bare git repository required");
887 goto done;
890 got_repo_temp_fds_set(repo_read.repo, temp_fds);
892 signal(SIGINT, catch_sigint);
893 signal(SIGTERM, catch_sigterm);
894 signal(SIGPIPE, SIG_IGN);
895 signal(SIGHUP, SIG_IGN);
897 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
898 iev.handler = repo_read_dispatch;
899 iev.events = EV_READ;
900 iev.handler_arg = NULL;
901 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
903 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
904 PROC_REPO_READ, -1, NULL, 0) == -1) {
905 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
906 goto done;
909 event_dispatch();
910 done:
911 if (err)
912 log_warnx("%s: %s", title, err->msg);
913 repo_read_shutdown();
916 void
917 repo_read_shutdown(void)
919 struct repo_read_client *client = &repo_read_client;
921 log_debug("%s: shutting down", repo_read.title);
923 if (client->have_ids)
924 got_object_idset_free(client->have_ids);
925 if (client->want_ids)
926 got_object_idset_free(client->want_ids);
927 if (client->fd != -1)
928 close(client->fd);
929 if (client->delta_cache_fd != -1)
930 close(client->delta_cache_fd);
931 if (client->pack_pipe != -1)
932 close(client->pack_pipe);
934 if (repo_read.repo)
935 got_repo_close(repo_read.repo);
936 got_repo_pack_fds_close(repo_read.pack_fds);
937 got_repo_temp_fds_close(repo_read.temp_fds);
938 if (repo_read.session_fd != -1)
939 close(repo_read.session_fd);
940 exit(0);