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 <siphash.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_repository_admin.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_hash.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_ratelimit.h"
45 #include "got_lib_pack_create.h"
46 #include "got_lib_poll.h"
48 #include "log.h"
49 #include "gotd.h"
50 #include "repo_read.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static struct repo_read {
57 pid_t pid;
58 const char *title;
59 struct got_repository *repo;
60 int *pack_fds;
61 int *temp_fds;
62 int session_fd;
63 struct gotd_imsgev session_iev;
64 } repo_read;
66 static struct repo_read_client {
67 uint32_t id;
68 int fd;
69 int delta_cache_fd;
70 int report_progress;
71 int pack_pipe;
72 struct gotd_object_id_array want_ids;
73 struct gotd_object_id_array have_ids;
74 } repo_read_client;
76 static volatile sig_atomic_t sigint_received;
77 static volatile sig_atomic_t sigterm_received;
79 static void
80 catch_sigint(int signo)
81 {
82 sigint_received = 1;
83 }
85 static void
86 catch_sigterm(int signo)
87 {
88 sigterm_received = 1;
89 }
91 static const struct got_error *
92 check_cancelled(void *arg)
93 {
94 if (sigint_received || sigterm_received)
95 return got_error(GOT_ERR_CANCELLED);
97 return NULL;
98 }
100 static const struct got_error *
101 send_symref(struct got_reference *symref, struct got_object_id *target_id,
102 struct imsgbuf *ibuf)
104 const struct got_error *err = NULL;
105 struct gotd_imsg_symref isymref;
106 const char *refname = got_ref_get_name(symref);
107 const char *target = got_ref_get_symref_target(symref);
108 size_t len;
109 struct ibuf *wbuf;
111 memset(&isymref, 0, sizeof(isymref));
112 isymref.name_len = strlen(refname);
113 isymref.target_len = strlen(target);
114 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
116 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
117 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
118 err = got_error(GOT_ERR_NO_SPACE);
119 goto done;
122 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
123 if (wbuf == NULL) {
124 err = got_error_from_errno("imsg_create SYMREF");
125 goto done;
128 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
129 err = got_error_from_errno("imsg_add SYMREF");
130 goto done;
132 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
134 goto done;
136 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
138 goto done;
141 wbuf->fd = -1;
142 imsg_close(ibuf, wbuf);
143 done:
144 free(target_id);
145 return err;
148 static const struct got_error *
149 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
150 struct imsgbuf *ibuf)
152 const struct got_error *err = NULL;
153 struct got_tag_object *tag;
154 size_t namelen, len;
155 char *peeled_refname = NULL;
156 struct got_object_id *id;
157 struct ibuf *wbuf;
159 err = got_object_tag_open(&tag, repo_read.repo, obj);
160 if (err)
161 return err;
163 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
164 err = got_error_from_errno("asprintf");
165 goto done;
168 id = got_object_tag_get_object_id(tag);
169 namelen = strlen(peeled_refname);
171 len = sizeof(struct gotd_imsg_ref) + namelen;
172 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
173 err = got_error(GOT_ERR_NO_SPACE);
174 goto done;
177 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
178 repo_read.pid, len);
179 if (wbuf == NULL) {
180 err = got_error_from_errno("imsg_create MREF");
181 goto done;
184 /* Keep in sync with struct gotd_imsg_ref definition. */
185 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
186 err = got_error_from_errno("imsg_add REF");
187 goto done;
189 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
190 err = got_error_from_errno("imsg_add REF");
191 goto done;
193 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
194 err = got_error_from_errno("imsg_add REF");
195 goto done;
198 wbuf->fd = -1;
199 imsg_close(ibuf, wbuf);
200 done:
201 got_object_tag_close(tag);
202 return err;
205 static const struct got_error *
206 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
208 const struct got_error *err;
209 const char *refname = got_ref_get_name(ref);
210 size_t namelen;
211 struct got_object_id *id = NULL;
212 struct got_object *obj = NULL;
213 size_t len;
214 struct ibuf *wbuf;
216 namelen = strlen(refname);
218 len = sizeof(struct gotd_imsg_ref) + namelen;
219 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
220 return got_error(GOT_ERR_NO_SPACE);
222 err = got_ref_resolve(&id, repo_read.repo, ref);
223 if (err)
224 return err;
226 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
227 repo_read.pid, len);
228 if (wbuf == NULL) {
229 err = got_error_from_errno("imsg_create REF");
230 goto done;
233 /* Keep in sync with struct gotd_imsg_ref definition. */
234 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
235 return got_error_from_errno("imsg_add REF");
236 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
237 return got_error_from_errno("imsg_add REF");
238 if (imsg_add(wbuf, refname, namelen) == -1)
239 return got_error_from_errno("imsg_add REF");
241 wbuf->fd = -1;
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 = imsg->fd;
268 struct got_object_id *head_target_id = NULL;
270 TAILQ_INIT(&refs);
272 if (client_fd == -1)
273 return got_error(GOT_ERR_PRIVSEP_NO_FD);
275 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 if (datalen != sizeof(ireq))
277 return got_error(GOT_ERR_PRIVSEP_LEN);
278 memcpy(&ireq, imsg->data, sizeof(ireq));
280 if (ireq.client_id == 0)
281 return got_error(GOT_ERR_CLIENT_ID);
282 if (client->id != 0) {
283 return got_error_msg(GOT_ERR_CLIENT_ID,
284 "duplicate list-refs request");
286 client->id = ireq.client_id;
287 client->fd = client_fd;
289 imsg_init(&ibuf, client_fd);
291 err = got_ref_list(&refs, repo_read.repo, "",
292 got_ref_cmp_by_name, NULL);
293 if (err)
294 return err;
296 memset(&irefs, 0, sizeof(irefs));
297 TAILQ_FOREACH(re, &refs, entry) {
298 struct got_object_id *id;
299 int obj_type;
301 if (got_ref_is_symbolic(re->ref)) {
302 const char *refname = got_ref_get_name(re->ref);
303 if (strcmp(refname, GOT_REF_HEAD) != 0)
304 continue;
305 err = got_ref_resolve(&head_target_id, repo_read.repo,
306 re->ref);
307 if (err) {
308 if (err->code != GOT_ERR_NOT_REF)
309 return err;
310 /*
311 * HEAD points to a non-existent branch.
312 * Do not advertise it.
313 * Matches git-daemon's behaviour.
314 */
315 head_target_id = NULL;
316 err = NULL;
317 } else
318 irefs.nrefs++;
319 continue;
322 irefs.nrefs++;
324 /* Account for a peeled tag refs. */
325 err = got_ref_resolve(&id, repo_read.repo, re->ref);
326 if (err)
327 goto done;
328 err = got_object_get_type(&obj_type, repo_read.repo, id);
329 free(id);
330 if (err)
331 goto done;
332 if (obj_type == GOT_OBJ_TYPE_TAG)
333 irefs.nrefs++;
336 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
337 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
338 err = got_error_from_errno("imsg_compose REFLIST");
339 goto done;
342 /*
343 * Send the HEAD symref first. In Git-protocol versions < 2
344 * the HEAD symref must be announced on the initial line of
345 * the server's ref advertisement.
346 * For now, we do not advertise symrefs other than HEAD.
347 */
348 TAILQ_FOREACH(re, &refs, entry) {
349 if (!got_ref_is_symbolic(re->ref) ||
350 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
351 head_target_id == NULL)
352 continue;
353 err = send_symref(re->ref, head_target_id, &ibuf);
354 if (err)
355 goto done;
356 break;
358 TAILQ_FOREACH(re, &refs, entry) {
359 if (got_ref_is_symbolic(re->ref))
360 continue;
361 err = send_ref(re->ref, &ibuf);
362 if (err)
363 goto done;
366 err = gotd_imsg_flush(&ibuf);
367 done:
368 got_ref_list_free(&refs);
369 imsg_clear(&ibuf);
370 return err;
373 static const struct got_error *
374 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
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] = got_object_id_dup(id);
396 if (array->ids[array->nids] == NULL)
397 return got_error_from_errno("got_object_id_dup");
398 array->nids++;
399 return NULL;
402 static void
403 free_object_ids(struct gotd_object_id_array *array)
405 size_t i;
407 for (i = 0; i < array->nids; i++)
408 free(array->ids[i]);
409 free(array->ids);
411 array->ids = NULL;
412 array->nalloc = 0;
413 array->nids = 0;
416 static const struct got_error *
417 recv_want(struct imsg *imsg)
419 const struct got_error *err;
420 struct repo_read_client *client = &repo_read_client;
421 struct gotd_imsg_want iwant;
422 size_t datalen;
423 char hex[SHA1_DIGEST_STRING_LENGTH];
424 struct got_object_id id;
425 int obj_type;
426 struct imsgbuf ibuf;
428 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
429 if (datalen != sizeof(iwant))
430 return got_error(GOT_ERR_PRIVSEP_LEN);
431 memcpy(&iwant, imsg->data, sizeof(iwant));
433 memset(&id, 0, sizeof(id));
434 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
436 if (log_getverbose() > 0 &&
437 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
438 log_debug("client wants %s", hex);
440 imsg_init(&ibuf, client->fd);
442 err = got_object_get_type(&obj_type, repo_read.repo, &id);
443 if (err)
444 return err;
446 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
447 obj_type != GOT_OBJ_TYPE_TAG)
448 return got_error(GOT_ERR_OBJ_TYPE);
450 err = record_object_id(&client->want_ids, &id);
451 if (err)
452 return err;
454 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
455 imsg_clear(&ibuf);
456 return err;
459 static const struct got_error *
460 recv_have(struct imsg *imsg)
462 const struct got_error *err;
463 struct repo_read_client *client = &repo_read_client;
464 struct gotd_imsg_have ihave;
465 size_t datalen;
466 char hex[SHA1_DIGEST_STRING_LENGTH];
467 struct got_object_id id;
468 int obj_type;
469 struct imsgbuf ibuf;
471 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
472 if (datalen != sizeof(ihave))
473 return got_error(GOT_ERR_PRIVSEP_LEN);
474 memcpy(&ihave, imsg->data, sizeof(ihave));
476 memset(&id, 0, sizeof(id));
477 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
479 if (log_getverbose() > 0 &&
480 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
481 log_debug("client has %s", hex);
483 imsg_init(&ibuf, client->fd);
485 err = got_object_get_type(&obj_type, repo_read.repo, &id);
486 if (err) {
487 if (err->code == GOT_ERR_NO_OBJ) {
488 gotd_imsg_send_nak(&id, &ibuf,
489 PROC_REPO_READ, repo_read.pid);
490 err = NULL;
492 goto done;
495 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
496 obj_type != GOT_OBJ_TYPE_TAG) {
497 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
498 err = got_error(GOT_ERR_OBJ_TYPE);
499 goto done;
502 err = record_object_id(&client->have_ids, &id);
503 if (err)
504 return err;
506 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
507 done:
508 imsg_clear(&ibuf);
509 return err;
512 struct repo_read_pack_progress_arg {
513 int report_progress;
514 struct imsgbuf *ibuf;
515 int sent_ready;
516 };
518 static const struct got_error *
519 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
520 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
521 int nobj_written)
523 struct repo_read_pack_progress_arg *a = arg;
524 struct gotd_imsg_packfile_progress iprog;
525 int ret;
527 if (!a->report_progress)
528 return NULL;
529 if (packfile_size > 0 && a->sent_ready)
530 return NULL;
532 memset(&iprog, 0, sizeof(iprog));
533 iprog.ncolored = ncolored;
534 iprog.nfound = nfound;
535 iprog.ntrees = ntrees;
536 iprog.packfile_size = packfile_size;
537 iprog.ncommits = ncommits;
538 iprog.nobj_total = nobj_total;
539 iprog.nobj_deltify = nobj_deltify;
540 iprog.nobj_written = nobj_written;
542 /* Using synchronous writes since we are blocking the event loop. */
543 if (packfile_size == 0) {
544 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
545 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
546 if (ret == -1) {
547 return got_error_from_errno("imsg compose "
548 "PACKFILE_PROGRESS");
550 } else {
551 a->sent_ready = 1;
552 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
553 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
554 if (ret == -1) {
555 return got_error_from_errno("imsg compose "
556 "PACKFILE_READY");
560 return gotd_imsg_flush(a->ibuf);
563 static const struct got_error *
564 receive_delta_cache_fd(struct imsg *imsg,
565 struct gotd_imsgev *iev)
567 struct repo_read_client *client = &repo_read_client;
568 struct gotd_imsg_send_packfile ireq;
569 size_t datalen;
571 log_debug("receving delta cache file");
573 if (imsg->fd == -1)
574 return got_error(GOT_ERR_PRIVSEP_NO_FD);
576 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
577 if (datalen != sizeof(ireq))
578 return got_error(GOT_ERR_PRIVSEP_LEN);
579 memcpy(&ireq, imsg->data, sizeof(ireq));
581 if (client->delta_cache_fd != -1)
582 return got_error(GOT_ERR_PRIVSEP_MSG);
584 client->delta_cache_fd = imsg->fd;
585 client->report_progress = ireq.report_progress;
586 return NULL;
589 static const struct got_error *
590 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
592 struct repo_read_client *client = &repo_read_client;
593 struct gotd_imsg_packfile_pipe ireq;
594 size_t datalen;
596 log_debug("receving pack pipe descriptor");
598 if (imsg->fd == -1)
599 return got_error(GOT_ERR_PRIVSEP_NO_FD);
601 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
602 if (datalen != sizeof(ireq))
603 return got_error(GOT_ERR_PRIVSEP_LEN);
604 memcpy(&ireq, imsg->data, sizeof(ireq));
606 if (client->pack_pipe != -1)
607 return got_error(GOT_ERR_PRIVSEP_MSG);
609 client->pack_pipe = imsg->fd;
610 return NULL;
613 static const struct got_error *
614 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
616 const struct got_error *err = NULL;
617 struct repo_read_client *client = &repo_read_client;
618 struct gotd_imsg_packfile_done idone;
619 uint8_t packsha1[SHA1_DIGEST_LENGTH];
620 char hex[SHA1_DIGEST_STRING_LENGTH];
621 FILE *delta_cache = NULL;
622 struct imsgbuf ibuf;
623 struct repo_read_pack_progress_arg pa;
624 struct got_ratelimit rl;
626 log_debug("packfile request received");
628 got_ratelimit_init(&rl, 2, 0);
630 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
631 return got_error(GOT_ERR_PRIVSEP_NO_FD);
633 imsg_init(&ibuf, client->fd);
635 delta_cache = fdopen(client->delta_cache_fd, "w+");
636 if (delta_cache == NULL) {
637 err = got_error_from_errno("fdopen");
638 goto done;
640 client->delta_cache_fd = -1;
642 memset(&pa, 0, sizeof(pa));
643 pa.ibuf = &ibuf;
644 pa.report_progress = client->report_progress;
646 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
647 client->have_ids.ids, client->have_ids.nids,
648 client->want_ids.ids, client->want_ids.nids,
649 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
650 check_cancelled, NULL);
651 if (err)
652 goto done;
654 if (log_getverbose() > 0 &&
655 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
656 log_debug("sent pack-%s.pack", hex);
658 memset(&idone, 0, sizeof(idone));
659 idone.client_id = client->id;
660 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
661 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
662 err = got_error_from_errno("imsg compose PACKFILE_DONE");
663 done:
664 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
665 err = got_error_from_errno("fclose");
666 imsg_clear(&ibuf);
667 return err;
670 static void
671 repo_read_dispatch_session(int fd, short event, void *arg)
673 const struct got_error *err = NULL;
674 struct gotd_imsgev *iev = arg;
675 struct imsgbuf *ibuf = &iev->ibuf;
676 struct imsg imsg;
677 ssize_t n;
678 int shut = 0;
679 struct repo_read_client *client = &repo_read_client;
681 if (event & EV_READ) {
682 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
683 fatal("imsg_read error");
684 if (n == 0) /* Connection closed. */
685 shut = 1;
688 if (event & EV_WRITE) {
689 n = msgbuf_write(&ibuf->w);
690 if (n == -1 && errno != EAGAIN)
691 fatal("msgbuf_write");
692 if (n == 0) /* Connection closed. */
693 shut = 1;
696 while (err == NULL && check_cancelled(NULL) == NULL) {
697 if ((n = imsg_get(ibuf, &imsg)) == -1)
698 fatal("%s: imsg_get", __func__);
699 if (n == 0) /* No more messages. */
700 break;
702 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
703 client->id == 0) {
704 err = got_error(GOT_ERR_PRIVSEP_MSG);
705 break;
708 switch (imsg.hdr.type) {
709 case GOTD_IMSG_LIST_REFS_INTERNAL:
710 err = list_refs(&imsg);
711 if (err)
712 log_warnx("%s: ls-refs: %s", repo_read.title,
713 err->msg);
714 break;
715 case GOTD_IMSG_WANT:
716 err = recv_want(&imsg);
717 if (err)
718 log_warnx("%s: want-line: %s", repo_read.title,
719 err->msg);
720 break;
721 case GOTD_IMSG_HAVE:
722 err = recv_have(&imsg);
723 if (err)
724 log_warnx("%s: have-line: %s", repo_read.title,
725 err->msg);
726 break;
727 case GOTD_IMSG_SEND_PACKFILE:
728 err = receive_delta_cache_fd(&imsg, iev);
729 if (err)
730 log_warnx("%s: receiving delta cache: %s",
731 repo_read.title, err->msg);
732 break;
733 case GOTD_IMSG_PACKFILE_PIPE:
734 err = receive_pack_pipe(&imsg, iev);
735 if (err) {
736 log_warnx("%s: receiving pack pipe: %s",
737 repo_read.title, err->msg);
738 break;
740 err = send_packfile(&imsg, iev);
741 if (err)
742 log_warnx("%s: sending packfile: %s",
743 repo_read.title, err->msg);
744 break;
745 default:
746 log_debug("%s: unexpected imsg %d", repo_read.title,
747 imsg.hdr.type);
748 break;
751 imsg_free(&imsg);
754 if (!shut && check_cancelled(NULL) == NULL) {
755 if (err &&
756 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
757 client->id, err) == -1) {
758 log_warnx("could not send error to parent: %s",
759 err->msg);
761 gotd_imsg_event_add(iev);
762 } else {
763 /* This pipe is dead. Remove its event handler */
764 event_del(&iev->ev);
765 event_loopexit(NULL);
769 static const struct got_error *
770 recv_connect(struct imsg *imsg)
772 struct gotd_imsgev *iev = &repo_read.session_iev;
773 size_t datalen;
775 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
776 if (datalen != 0)
777 return got_error(GOT_ERR_PRIVSEP_LEN);
778 if (imsg->fd == -1)
779 return got_error(GOT_ERR_PRIVSEP_NO_FD);
781 if (repo_read.session_fd != -1)
782 return got_error(GOT_ERR_PRIVSEP_MSG);
784 repo_read.session_fd = imsg->fd;
786 imsg_init(&iev->ibuf, repo_read.session_fd);
787 iev->handler = repo_read_dispatch_session;
788 iev->events = EV_READ;
789 iev->handler_arg = NULL;
790 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
791 repo_read_dispatch_session, iev);
792 gotd_imsg_event_add(iev);
794 return NULL;
797 static void
798 repo_read_dispatch(int fd, short event, void *arg)
800 const struct got_error *err = NULL;
801 struct gotd_imsgev *iev = arg;
802 struct imsgbuf *ibuf = &iev->ibuf;
803 struct imsg imsg;
804 ssize_t n;
805 int shut = 0;
806 struct repo_read_client *client = &repo_read_client;
808 if (event & EV_READ) {
809 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
810 fatal("imsg_read error");
811 if (n == 0) /* Connection closed. */
812 shut = 1;
815 if (event & EV_WRITE) {
816 n = msgbuf_write(&ibuf->w);
817 if (n == -1 && errno != EAGAIN)
818 fatal("msgbuf_write");
819 if (n == 0) /* Connection closed. */
820 shut = 1;
823 while (err == NULL && check_cancelled(NULL) == NULL) {
824 if ((n = imsg_get(ibuf, &imsg)) == -1)
825 fatal("%s: imsg_get", __func__);
826 if (n == 0) /* No more messages. */
827 break;
829 switch (imsg.hdr.type) {
830 case GOTD_IMSG_CONNECT_REPO_CHILD:
831 err = recv_connect(&imsg);
832 break;
833 default:
834 log_debug("%s: unexpected imsg %d", repo_read.title,
835 imsg.hdr.type);
836 break;
839 imsg_free(&imsg);
842 if (!shut && check_cancelled(NULL) == NULL) {
843 if (err &&
844 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
845 client->id, err) == -1) {
846 log_warnx("could not send error to parent: %s",
847 err->msg);
849 gotd_imsg_event_add(iev);
850 } else {
851 /* This pipe is dead. Remove its event handler */
852 event_del(&iev->ev);
853 event_loopexit(NULL);
857 void
858 repo_read_main(const char *title, const char *repo_path,
859 int *pack_fds, int *temp_fds)
861 const struct got_error *err = NULL;
862 struct repo_read_client *client = &repo_read_client;
863 struct gotd_imsgev iev;
865 client->fd = -1;
866 client->delta_cache_fd = -1;
867 client->pack_pipe = -1;
869 repo_read.title = title;
870 repo_read.pid = getpid();
871 repo_read.pack_fds = pack_fds;
872 repo_read.temp_fds = temp_fds;
873 repo_read.session_fd = -1;
874 repo_read.session_iev.ibuf.fd = -1;
876 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
877 if (err)
878 goto done;
879 if (!got_repo_is_bare(repo_read.repo)) {
880 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
881 "bare git repository required");
882 goto done;
885 got_repo_temp_fds_set(repo_read.repo, temp_fds);
887 signal(SIGINT, catch_sigint);
888 signal(SIGTERM, catch_sigterm);
889 signal(SIGPIPE, SIG_IGN);
890 signal(SIGHUP, SIG_IGN);
892 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
893 iev.handler = repo_read_dispatch;
894 iev.events = EV_READ;
895 iev.handler_arg = NULL;
896 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
898 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
899 PROC_REPO_READ, -1, NULL, 0) == -1) {
900 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
901 goto done;
904 event_dispatch();
905 done:
906 if (err)
907 log_warnx("%s: %s", title, err->msg);
908 repo_read_shutdown();
911 void
912 repo_read_shutdown(void)
914 struct repo_read_client *client = &repo_read_client;
916 log_debug("%s: shutting down", repo_read.title);
918 free_object_ids(&client->have_ids);
919 free_object_ids(&client->want_ids);
920 if (client->fd != -1)
921 close(client->fd);
922 if (client->delta_cache_fd != -1)
923 close(client->delta_cache_fd);
924 if (client->pack_pipe != -1)
925 close(client->pack_pipe);
927 if (repo_read.repo)
928 got_repo_close(repo_read.repo);
929 got_repo_pack_fds_close(repo_read.pack_fds);
930 got_repo_temp_fds_close(repo_read.temp_fds);
931 if (repo_read.session_fd != -1)
932 close(repo_read.session_fd);
933 exit(0);