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 gotd_object_id_array want_ids;
75 struct gotd_object_id_array 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->hash, 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->hash, 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->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 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 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
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] = got_object_id_dup(id);
398 if (array->ids[array->nids] == NULL)
399 return got_error_from_errno("got_object_id_dup");
400 array->nids++;
401 return NULL;
404 static void
405 free_object_ids(struct gotd_object_id_array *array)
407 size_t i;
409 for (i = 0; i < array->nids; i++)
410 free(array->ids[i]);
411 free(array->ids);
413 array->ids = NULL;
414 array->nalloc = 0;
415 array->nids = 0;
418 static const struct got_error *
419 recv_want(struct imsg *imsg)
421 const struct got_error *err;
422 struct repo_read_client *client = &repo_read_client;
423 struct gotd_imsg_want iwant;
424 size_t datalen;
425 char hex[SHA1_DIGEST_STRING_LENGTH];
426 struct got_object_id id;
427 int obj_type;
428 struct imsgbuf ibuf;
430 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
431 if (datalen != sizeof(iwant))
432 return got_error(GOT_ERR_PRIVSEP_LEN);
433 memcpy(&iwant, imsg->data, sizeof(iwant));
435 memset(&id, 0, sizeof(id));
436 memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
438 if (log_getverbose() > 0 &&
439 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
440 log_debug("client wants %s", hex);
442 imsg_init(&ibuf, client->fd);
444 err = got_object_get_type(&obj_type, repo_read.repo, &id);
445 if (err)
446 return err;
448 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
449 obj_type != GOT_OBJ_TYPE_TAG)
450 return got_error(GOT_ERR_OBJ_TYPE);
452 err = record_object_id(&client->want_ids, &id);
453 if (err)
454 return err;
456 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
457 imsg_clear(&ibuf);
458 return err;
461 static const struct got_error *
462 recv_have(struct imsg *imsg)
464 const struct got_error *err;
465 struct repo_read_client *client = &repo_read_client;
466 struct gotd_imsg_have ihave;
467 size_t datalen;
468 char hex[SHA1_DIGEST_STRING_LENGTH];
469 struct got_object_id id;
470 int obj_type;
471 struct imsgbuf ibuf;
473 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
474 if (datalen != sizeof(ihave))
475 return got_error(GOT_ERR_PRIVSEP_LEN);
476 memcpy(&ihave, imsg->data, sizeof(ihave));
478 memset(&id, 0, sizeof(id));
479 memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
481 if (log_getverbose() > 0 &&
482 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
483 log_debug("client has %s", hex);
485 imsg_init(&ibuf, client->fd);
487 err = got_object_get_type(&obj_type, repo_read.repo, &id);
488 if (err) {
489 if (err->code == GOT_ERR_NO_OBJ) {
490 gotd_imsg_send_nak(&id, &ibuf,
491 PROC_REPO_READ, repo_read.pid);
492 err = NULL;
494 goto done;
497 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
498 obj_type != GOT_OBJ_TYPE_TAG) {
499 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
500 err = got_error(GOT_ERR_OBJ_TYPE);
501 goto done;
504 err = record_object_id(&client->have_ids, &id);
505 if (err)
506 return err;
508 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
509 done:
510 imsg_clear(&ibuf);
511 return err;
514 struct repo_read_pack_progress_arg {
515 int report_progress;
516 struct imsgbuf *ibuf;
517 int sent_ready;
518 };
520 static const struct got_error *
521 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
522 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
523 int nobj_written)
525 struct repo_read_pack_progress_arg *a = arg;
526 struct gotd_imsg_packfile_progress iprog;
527 int ret;
529 if (!a->report_progress)
530 return NULL;
531 if (packfile_size > 0 && a->sent_ready)
532 return NULL;
534 memset(&iprog, 0, sizeof(iprog));
535 iprog.ncolored = ncolored;
536 iprog.nfound = nfound;
537 iprog.ntrees = ntrees;
538 iprog.packfile_size = packfile_size;
539 iprog.ncommits = ncommits;
540 iprog.nobj_total = nobj_total;
541 iprog.nobj_deltify = nobj_deltify;
542 iprog.nobj_written = nobj_written;
544 /* Using synchronous writes since we are blocking the event loop. */
545 if (packfile_size == 0) {
546 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
547 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
548 if (ret == -1) {
549 return got_error_from_errno("imsg compose "
550 "PACKFILE_PROGRESS");
552 } else {
553 a->sent_ready = 1;
554 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
555 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
556 if (ret == -1) {
557 return got_error_from_errno("imsg compose "
558 "PACKFILE_READY");
562 return gotd_imsg_flush(a->ibuf);
565 static const struct got_error *
566 receive_delta_cache_fd(struct imsg *imsg,
567 struct gotd_imsgev *iev)
569 struct repo_read_client *client = &repo_read_client;
570 struct gotd_imsg_send_packfile ireq;
571 size_t datalen;
573 log_debug("receving delta cache file");
575 if (imsg->fd == -1)
576 return got_error(GOT_ERR_PRIVSEP_NO_FD);
578 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
579 if (datalen != sizeof(ireq))
580 return got_error(GOT_ERR_PRIVSEP_LEN);
581 memcpy(&ireq, imsg->data, sizeof(ireq));
583 if (client->delta_cache_fd != -1)
584 return got_error(GOT_ERR_PRIVSEP_MSG);
586 client->delta_cache_fd = imsg->fd;
587 client->report_progress = ireq.report_progress;
588 return NULL;
591 static const struct got_error *
592 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
594 struct repo_read_client *client = &repo_read_client;
595 struct gotd_imsg_packfile_pipe ireq;
596 size_t datalen;
598 log_debug("receving pack pipe descriptor");
600 if (imsg->fd == -1)
601 return got_error(GOT_ERR_PRIVSEP_NO_FD);
603 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
604 if (datalen != sizeof(ireq))
605 return got_error(GOT_ERR_PRIVSEP_LEN);
606 memcpy(&ireq, imsg->data, sizeof(ireq));
608 if (client->pack_pipe != -1)
609 return got_error(GOT_ERR_PRIVSEP_MSG);
611 client->pack_pipe = imsg->fd;
612 return NULL;
615 static const struct got_error *
616 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
618 const struct got_error *err = NULL;
619 struct repo_read_client *client = &repo_read_client;
620 struct gotd_imsg_packfile_done idone;
621 uint8_t packsha1[SHA1_DIGEST_LENGTH];
622 char hex[SHA1_DIGEST_STRING_LENGTH];
623 FILE *delta_cache = NULL;
624 struct imsgbuf ibuf;
625 struct repo_read_pack_progress_arg pa;
626 struct got_ratelimit rl;
628 log_debug("packfile request received");
630 got_ratelimit_init(&rl, 2, 0);
632 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
633 return got_error(GOT_ERR_PRIVSEP_NO_FD);
635 imsg_init(&ibuf, client->fd);
637 delta_cache = fdopen(client->delta_cache_fd, "w+");
638 if (delta_cache == NULL) {
639 err = got_error_from_errno("fdopen");
640 goto done;
642 client->delta_cache_fd = -1;
644 memset(&pa, 0, sizeof(pa));
645 pa.ibuf = &ibuf;
646 pa.report_progress = client->report_progress;
648 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
649 client->have_ids.ids, client->have_ids.nids,
650 client->want_ids.ids, client->want_ids.nids,
651 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
652 check_cancelled, NULL);
653 if (err)
654 goto done;
656 if (log_getverbose() > 0 &&
657 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
658 log_debug("sent pack-%s.pack", hex);
660 memset(&idone, 0, sizeof(idone));
661 idone.client_id = client->id;
662 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
663 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
664 err = got_error_from_errno("imsg compose PACKFILE_DONE");
665 done:
666 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
667 err = got_error_from_errno("fclose");
668 imsg_clear(&ibuf);
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 client->id == 0) {
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("%s: ls-refs: %s", repo_read.title,
715 err->msg);
716 break;
717 case GOTD_IMSG_WANT:
718 err = recv_want(&imsg);
719 if (err)
720 log_warnx("%s: want-line: %s", repo_read.title,
721 err->msg);
722 break;
723 case GOTD_IMSG_HAVE:
724 err = recv_have(&imsg);
725 if (err)
726 log_warnx("%s: have-line: %s", repo_read.title,
727 err->msg);
728 break;
729 case GOTD_IMSG_SEND_PACKFILE:
730 err = receive_delta_cache_fd(&imsg, iev);
731 if (err)
732 log_warnx("%s: receiving delta cache: %s",
733 repo_read.title, err->msg);
734 break;
735 case GOTD_IMSG_PACKFILE_PIPE:
736 err = receive_pack_pipe(&imsg, iev);
737 if (err) {
738 log_warnx("%s: receiving pack pipe: %s",
739 repo_read.title, err->msg);
740 break;
742 err = send_packfile(&imsg, iev);
743 if (err)
744 log_warnx("%s: sending packfile: %s",
745 repo_read.title, err->msg);
746 break;
747 default:
748 log_debug("%s: unexpected imsg %d", repo_read.title,
749 imsg.hdr.type);
750 break;
753 imsg_free(&imsg);
756 if (!shut && check_cancelled(NULL) == NULL) {
757 if (err &&
758 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
759 client->id, err) == -1) {
760 log_warnx("could not send error to parent: %s",
761 err->msg);
763 gotd_imsg_event_add(iev);
764 } else {
765 /* This pipe is dead. Remove its event handler */
766 event_del(&iev->ev);
767 event_loopexit(NULL);
771 static const struct got_error *
772 recv_connect(struct imsg *imsg)
774 struct gotd_imsgev *iev = &repo_read.session_iev;
775 size_t datalen;
777 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
778 if (datalen != 0)
779 return got_error(GOT_ERR_PRIVSEP_LEN);
780 if (imsg->fd == -1)
781 return got_error(GOT_ERR_PRIVSEP_NO_FD);
783 if (repo_read.session_fd != -1)
784 return got_error(GOT_ERR_PRIVSEP_MSG);
786 repo_read.session_fd = imsg->fd;
788 imsg_init(&iev->ibuf, repo_read.session_fd);
789 iev->handler = repo_read_dispatch_session;
790 iev->events = EV_READ;
791 iev->handler_arg = NULL;
792 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
793 repo_read_dispatch_session, iev);
794 gotd_imsg_event_add(iev);
796 return NULL;
799 static void
800 repo_read_dispatch(int fd, short event, void *arg)
802 const struct got_error *err = NULL;
803 struct gotd_imsgev *iev = arg;
804 struct imsgbuf *ibuf = &iev->ibuf;
805 struct imsg imsg;
806 ssize_t n;
807 int shut = 0;
808 struct repo_read_client *client = &repo_read_client;
810 if (event & EV_READ) {
811 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
812 fatal("imsg_read error");
813 if (n == 0) /* Connection closed. */
814 shut = 1;
817 if (event & EV_WRITE) {
818 n = msgbuf_write(&ibuf->w);
819 if (n == -1 && errno != EAGAIN)
820 fatal("msgbuf_write");
821 if (n == 0) /* Connection closed. */
822 shut = 1;
825 while (err == NULL && check_cancelled(NULL) == NULL) {
826 if ((n = imsg_get(ibuf, &imsg)) == -1)
827 fatal("%s: imsg_get", __func__);
828 if (n == 0) /* No more messages. */
829 break;
831 switch (imsg.hdr.type) {
832 case GOTD_IMSG_CONNECT_REPO_CHILD:
833 err = recv_connect(&imsg);
834 break;
835 default:
836 log_debug("%s: unexpected imsg %d", repo_read.title,
837 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;
871 repo_read.title = title;
872 repo_read.pid = getpid();
873 repo_read.pack_fds = pack_fds;
874 repo_read.temp_fds = temp_fds;
875 repo_read.session_fd = -1;
876 repo_read.session_iev.ibuf.fd = -1;
878 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
879 if (err)
880 goto done;
881 if (!got_repo_is_bare(repo_read.repo)) {
882 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
883 "bare git repository required");
884 goto done;
887 got_repo_temp_fds_set(repo_read.repo, temp_fds);
889 signal(SIGINT, catch_sigint);
890 signal(SIGTERM, catch_sigterm);
891 signal(SIGPIPE, SIG_IGN);
892 signal(SIGHUP, SIG_IGN);
894 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
895 iev.handler = repo_read_dispatch;
896 iev.events = EV_READ;
897 iev.handler_arg = NULL;
898 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
900 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
901 PROC_REPO_READ, -1, NULL, 0) == -1) {
902 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
903 goto done;
906 event_dispatch();
907 done:
908 if (err)
909 log_warnx("%s: %s", title, err->msg);
910 repo_read_shutdown();
913 void
914 repo_read_shutdown(void)
916 struct repo_read_client *client = &repo_read_client;
918 log_debug("%s: shutting down", repo_read.title);
920 free_object_ids(&client->have_ids);
921 free_object_ids(&client->want_ids);
922 if (client->fd != -1)
923 close(client->fd);
924 if (client->delta_cache_fd != -1)
925 close(client->delta_cache_fd);
926 if (client->pack_pipe != -1)
927 close(client->pack_pipe);
929 if (repo_read.repo)
930 got_repo_close(repo_read.repo);
931 got_repo_pack_fds_close(repo_read.pack_fds);
932 got_repo_temp_fds_close(repo_read.temp_fds);
933 if (repo_read.session_fd != -1)
934 close(repo_read.session_fd);
935 exit(0);