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 <siphash.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_ratelimit.h"
46 #include "got_lib_pack_create.h"
47 #include "got_lib_poll.h"
49 #include "log.h"
50 #include "gotd.h"
51 #include "repo_read.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static struct repo_read {
58 pid_t pid;
59 const char *title;
60 struct got_repository *repo;
61 int *pack_fds;
62 int *temp_fds;
63 } repo_read;
65 static struct repo_read_client {
66 uint32_t id;
67 int fd;
68 int delta_cache_fd;
69 int report_progress;
70 int pack_pipe;
71 struct gotd_object_id_array want_ids;
72 struct gotd_object_id_array have_ids;
73 } repo_read_client;
75 static volatile sig_atomic_t sigint_received;
76 static volatile sig_atomic_t sigterm_received;
78 static void
79 catch_sigint(int signo)
80 {
81 sigint_received = 1;
82 }
84 static void
85 catch_sigterm(int signo)
86 {
87 sigterm_received = 1;
88 }
90 static const struct got_error *
91 check_cancelled(void *arg)
92 {
93 if (sigint_received || sigterm_received)
94 return got_error(GOT_ERR_CANCELLED);
96 return NULL;
97 }
99 static const struct got_error *
100 send_symref(struct got_reference *symref, struct got_object_id *target_id,
101 struct imsgbuf *ibuf)
103 const struct got_error *err = NULL;
104 struct gotd_imsg_symref isymref;
105 const char *refname = got_ref_get_name(symref);
106 const char *target = got_ref_get_symref_target(symref);
107 size_t len;
108 struct ibuf *wbuf;
110 memset(&isymref, 0, sizeof(isymref));
111 isymref.name_len = strlen(refname);
112 isymref.target_len = strlen(target);
113 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
115 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
116 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
117 err = got_error(GOT_ERR_NO_SPACE);
118 goto done;
121 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
122 if (wbuf == NULL) {
123 err = got_error_from_errno("imsg_create SYMREF");
124 goto done;
127 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
128 err = got_error_from_errno("imsg_add SYMREF");
129 goto done;
131 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
132 err = got_error_from_errno("imsg_add SYMREF");
133 goto done;
135 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
136 err = got_error_from_errno("imsg_add SYMREF");
137 goto done;
140 wbuf->fd = -1;
141 imsg_close(ibuf, wbuf);
142 done:
143 free(target_id);
144 return err;
147 static const struct got_error *
148 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
149 struct imsgbuf *ibuf)
151 const struct got_error *err = NULL;
152 struct got_tag_object *tag;
153 size_t namelen, len;
154 char *peeled_refname = NULL;
155 struct got_object_id *id;
156 struct ibuf *wbuf;
158 err = got_object_tag_open(&tag, repo_read.repo, obj);
159 if (err)
160 return err;
162 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
163 err = got_error_from_errno("asprintf");
164 goto done;
167 id = got_object_tag_get_object_id(tag);
168 namelen = strlen(peeled_refname);
170 len = sizeof(struct gotd_imsg_ref) + namelen;
171 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
172 err = got_error(GOT_ERR_NO_SPACE);
173 goto done;
176 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
177 repo_read.pid, len);
178 if (wbuf == NULL) {
179 err = got_error_from_errno("imsg_create MREF");
180 goto done;
183 /* Keep in sync with struct gotd_imsg_ref definition. */
184 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
185 err = got_error_from_errno("imsg_add REF");
186 goto done;
188 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
189 err = got_error_from_errno("imsg_add REF");
190 goto done;
192 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
193 err = got_error_from_errno("imsg_add REF");
194 goto done;
197 wbuf->fd = -1;
198 imsg_close(ibuf, wbuf);
199 done:
200 got_object_tag_close(tag);
201 return err;
204 static const struct got_error *
205 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
207 const struct got_error *err;
208 const char *refname = got_ref_get_name(ref);
209 size_t namelen;
210 struct got_object_id *id = NULL;
211 struct got_object *obj = NULL;
212 size_t len;
213 struct ibuf *wbuf;
215 namelen = strlen(refname);
217 len = sizeof(struct gotd_imsg_ref) + namelen;
218 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
219 return got_error(GOT_ERR_NO_SPACE);
221 err = got_ref_resolve(&id, repo_read.repo, ref);
222 if (err)
223 return err;
225 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
226 repo_read.pid, len);
227 if (wbuf == NULL) {
228 err = got_error_from_errno("imsg_create REF");
229 goto done;
232 /* Keep in sync with struct gotd_imsg_ref definition. */
233 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
234 return got_error_from_errno("imsg_add REF");
235 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
236 return got_error_from_errno("imsg_add REF");
237 if (imsg_add(wbuf, refname, namelen) == -1)
238 return got_error_from_errno("imsg_add REF");
240 wbuf->fd = -1;
241 imsg_close(ibuf, wbuf);
243 err = got_object_open(&obj, repo_read.repo, id);
244 if (err)
245 goto done;
246 if (obj->type == GOT_OBJ_TYPE_TAG)
247 err = send_peeled_tag_ref(ref, obj, ibuf);
248 done:
249 if (obj)
250 got_object_close(obj);
251 free(id);
252 return err;
255 static const struct got_error *
256 list_refs(struct imsg *imsg)
258 const struct got_error *err;
259 struct repo_read_client *client = &repo_read_client;
260 struct got_reflist_head refs;
261 struct got_reflist_entry *re;
262 struct gotd_imsg_list_refs_internal ireq;
263 size_t datalen;
264 struct gotd_imsg_reflist irefs;
265 struct imsgbuf ibuf;
266 int client_fd = imsg->fd;
267 struct got_object_id *head_target_id = NULL;
269 TAILQ_INIT(&refs);
271 if (client_fd == -1)
272 return got_error(GOT_ERR_PRIVSEP_NO_FD);
274 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
275 if (datalen != sizeof(ireq))
276 return got_error(GOT_ERR_PRIVSEP_LEN);
277 memcpy(&ireq, imsg->data, sizeof(ireq));
279 if (ireq.client_id == 0)
280 return got_error(GOT_ERR_CLIENT_ID);
281 if (client->id != 0) {
282 return got_error_msg(GOT_ERR_CLIENT_ID,
283 "duplicate list-refs request");
285 client->id = ireq.client_id;
286 client->fd = client_fd;
287 client->delta_cache_fd = -1;
288 client->pack_pipe = -1;
290 imsg_init(&ibuf, client_fd);
292 err = got_ref_list(&refs, repo_read.repo, "",
293 got_ref_cmp_by_name, NULL);
294 if (err)
295 return err;
297 memset(&irefs, 0, sizeof(irefs));
298 TAILQ_FOREACH(re, &refs, entry) {
299 struct got_object_id *id;
300 int obj_type;
302 if (got_ref_is_symbolic(re->ref)) {
303 const char *refname = got_ref_get_name(re->ref);
304 if (strcmp(refname, GOT_REF_HEAD) != 0)
305 continue;
306 err = got_ref_resolve(&head_target_id, repo_read.repo,
307 re->ref);
308 if (err) {
309 if (err->code != GOT_ERR_NOT_REF)
310 return err;
311 /*
312 * HEAD points to a non-existent branch.
313 * Do not advertise it.
314 * Matches git-daemon's behaviour.
315 */
316 head_target_id = NULL;
317 err = NULL;
318 } else
319 irefs.nrefs++;
320 continue;
323 irefs.nrefs++;
325 /* Account for a peeled tag refs. */
326 err = got_ref_resolve(&id, repo_read.repo, re->ref);
327 if (err)
328 goto done;
329 err = got_object_get_type(&obj_type, repo_read.repo, id);
330 free(id);
331 if (err)
332 goto done;
333 if (obj_type == GOT_OBJ_TYPE_TAG)
334 irefs.nrefs++;
337 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
338 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
339 err = got_error_from_errno("imsg_compose REFLIST");
340 goto done;
343 /*
344 * Send the HEAD symref first. In Git-protocol versions < 2
345 * the HEAD symref must be announced on the initial line of
346 * the server's ref advertisement.
347 * For now, we do not advertise symrefs other than HEAD.
348 */
349 TAILQ_FOREACH(re, &refs, entry) {
350 if (!got_ref_is_symbolic(re->ref) ||
351 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
352 head_target_id == NULL)
353 continue;
354 err = send_symref(re->ref, head_target_id, &ibuf);
355 if (err)
356 goto done;
357 break;
359 TAILQ_FOREACH(re, &refs, entry) {
360 if (got_ref_is_symbolic(re->ref))
361 continue;
362 err = send_ref(re->ref, &ibuf);
363 if (err)
364 goto done;
367 err = gotd_imsg_flush(&ibuf);
368 done:
369 got_ref_list_free(&refs);
370 imsg_clear(&ibuf);
371 return err;
374 static const struct got_error *
375 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
377 const size_t alloc_chunksz = 256;
379 if (array->ids == NULL) {
380 array->ids = reallocarray(NULL, alloc_chunksz,
381 sizeof(*array->ids));
382 if (array->ids == NULL)
383 return got_error_from_errno("reallocarray");
384 array->nalloc = alloc_chunksz;
385 array->nids = 0;
386 } else if (array->nalloc <= array->nids) {
387 struct got_object_id **new;
388 new = recallocarray(array->ids, array->nalloc,
389 array->nalloc + alloc_chunksz, sizeof(*new));
390 if (new == NULL)
391 return got_error_from_errno("recallocarray");
392 array->ids = new;
393 array->nalloc += alloc_chunksz;
396 array->ids[array->nids] = got_object_id_dup(id);
397 if (array->ids[array->nids] == NULL)
398 return got_error_from_errno("got_object_id_dup");
399 array->nids++;
400 return NULL;
403 static void
404 free_object_ids(struct gotd_object_id_array *array)
406 size_t i;
408 for (i = 0; i < array->nids; i++)
409 free(array->ids[i]);
410 free(array->ids);
412 array->ids = NULL;
413 array->nalloc = 0;
414 array->nids = 0;
417 static const struct got_error *
418 recv_want(struct imsg *imsg)
420 const struct got_error *err;
421 struct repo_read_client *client = &repo_read_client;
422 struct gotd_imsg_want iwant;
423 size_t datalen;
424 char hex[SHA1_DIGEST_STRING_LENGTH];
425 struct got_object_id id;
426 int obj_type;
427 struct imsgbuf ibuf;
429 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
430 if (datalen != sizeof(iwant))
431 return got_error(GOT_ERR_PRIVSEP_LEN);
432 memcpy(&iwant, imsg->data, sizeof(iwant));
434 memset(&id, 0, sizeof(id));
435 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
437 if (log_getverbose() > 0 &&
438 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
439 log_debug("client wants %s", hex);
441 imsg_init(&ibuf, client->fd);
443 err = got_object_get_type(&obj_type, repo_read.repo, &id);
444 if (err)
445 return err;
447 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
448 obj_type != GOT_OBJ_TYPE_TAG)
449 return got_error(GOT_ERR_OBJ_TYPE);
451 err = record_object_id(&client->want_ids, &id);
452 if (err)
453 return err;
455 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
456 imsg_clear(&ibuf);
457 return err;
460 static const struct got_error *
461 recv_have(struct imsg *imsg)
463 const struct got_error *err;
464 struct repo_read_client *client = &repo_read_client;
465 struct gotd_imsg_have ihave;
466 size_t datalen;
467 char hex[SHA1_DIGEST_STRING_LENGTH];
468 struct got_object_id id;
469 int obj_type;
470 struct imsgbuf ibuf;
472 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
473 if (datalen != sizeof(ihave))
474 return got_error(GOT_ERR_PRIVSEP_LEN);
475 memcpy(&ihave, imsg->data, sizeof(ihave));
477 memset(&id, 0, sizeof(id));
478 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
480 if (log_getverbose() > 0 &&
481 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
482 log_debug("client has %s", hex);
484 imsg_init(&ibuf, client->fd);
486 err = got_object_get_type(&obj_type, repo_read.repo, &id);
487 if (err) {
488 if (err->code == GOT_ERR_NO_OBJ) {
489 gotd_imsg_send_nak(&id, &ibuf,
490 PROC_REPO_READ, repo_read.pid);
491 err = NULL;
493 goto done;
496 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
497 obj_type != GOT_OBJ_TYPE_TAG) {
498 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
499 err = got_error(GOT_ERR_OBJ_TYPE);
500 goto done;
503 err = record_object_id(&client->have_ids, &id);
504 if (err)
505 return err;
507 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
508 done:
509 imsg_clear(&ibuf);
510 return err;
513 struct repo_read_pack_progress_arg {
514 int report_progress;
515 struct imsgbuf *ibuf;
516 int sent_ready;
517 };
519 static const struct got_error *
520 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
521 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
522 int nobj_written)
524 struct repo_read_pack_progress_arg *a = arg;
525 struct gotd_imsg_packfile_progress iprog;
526 int ret;
528 if (!a->report_progress)
529 return NULL;
530 if (packfile_size > 0 && a->sent_ready)
531 return NULL;
533 memset(&iprog, 0, sizeof(iprog));
534 iprog.ncolored = ncolored;
535 iprog.nfound = nfound;
536 iprog.ntrees = ntrees;
537 iprog.packfile_size = packfile_size;
538 iprog.ncommits = ncommits;
539 iprog.nobj_total = nobj_total;
540 iprog.nobj_deltify = nobj_deltify;
541 iprog.nobj_written = nobj_written;
543 /* Using synchronous writes since we are blocking the event loop. */
544 if (packfile_size == 0) {
545 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
546 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
547 if (ret == -1) {
548 return got_error_from_errno("imsg compose "
549 "PACKFILE_PROGRESS");
551 } else {
552 a->sent_ready = 1;
553 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
554 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
555 if (ret == -1) {
556 return got_error_from_errno("imsg compose "
557 "PACKFILE_READY");
561 return gotd_imsg_flush(a->ibuf);
564 static const struct got_error *
565 receive_delta_cache_fd(struct imsg *imsg,
566 struct gotd_imsgev *iev)
568 struct repo_read_client *client = &repo_read_client;
569 struct gotd_imsg_send_packfile ireq;
570 size_t datalen;
572 log_debug("receving delta cache file");
574 if (imsg->fd == -1)
575 return got_error(GOT_ERR_PRIVSEP_NO_FD);
577 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
578 if (datalen != sizeof(ireq))
579 return got_error(GOT_ERR_PRIVSEP_LEN);
580 memcpy(&ireq, imsg->data, sizeof(ireq));
582 if (client->delta_cache_fd != -1)
583 return got_error(GOT_ERR_PRIVSEP_MSG);
585 client->delta_cache_fd = imsg->fd;
586 client->report_progress = ireq.report_progress;
587 return NULL;
590 static const struct got_error *
591 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
593 struct repo_read_client *client = &repo_read_client;
594 struct gotd_imsg_packfile_pipe ireq;
595 size_t datalen;
597 log_debug("receving pack pipe descriptor");
599 if (imsg->fd == -1)
600 return got_error(GOT_ERR_PRIVSEP_NO_FD);
602 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
603 if (datalen != sizeof(ireq))
604 return got_error(GOT_ERR_PRIVSEP_LEN);
605 memcpy(&ireq, imsg->data, sizeof(ireq));
607 if (client->pack_pipe != -1)
608 return got_error(GOT_ERR_PRIVSEP_MSG);
610 client->pack_pipe = imsg->fd;
611 return NULL;
614 static const struct got_error *
615 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
617 const struct got_error *err = NULL;
618 struct repo_read_client *client = &repo_read_client;
619 struct gotd_imsg_packfile_done idone;
620 uint8_t packsha1[SHA1_DIGEST_LENGTH];
621 char hex[SHA1_DIGEST_STRING_LENGTH];
622 FILE *delta_cache = NULL;
623 struct imsgbuf ibuf;
624 struct repo_read_pack_progress_arg pa;
625 struct got_ratelimit rl;
627 log_debug("packfile request received");
629 got_ratelimit_init(&rl, 2, 0);
631 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
632 return got_error(GOT_ERR_PRIVSEP_NO_FD);
634 imsg_init(&ibuf, client->fd);
636 delta_cache = fdopen(client->delta_cache_fd, "w+");
637 if (delta_cache == NULL) {
638 err = got_error_from_errno("fdopen");
639 goto done;
641 client->delta_cache_fd = -1;
643 memset(&pa, 0, sizeof(pa));
644 pa.ibuf = &ibuf;
645 pa.report_progress = client->report_progress;
647 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
648 client->have_ids.ids, client->have_ids.nids,
649 client->want_ids.ids, client->want_ids.nids,
650 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
651 check_cancelled, NULL);
652 if (err)
653 goto done;
655 if (log_getverbose() > 0 &&
656 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
657 log_debug("sent pack-%s.pack", hex);
659 memset(&idone, 0, sizeof(idone));
660 idone.client_id = client->id;
661 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
662 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
663 err = got_error_from_errno("imsg compose PACKFILE_DONE");
664 done:
665 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
666 err = got_error_from_errno("fclose");
667 imsg_clear(&ibuf);
668 return err;
671 static const struct got_error *
672 recv_disconnect(struct imsg *imsg)
674 const struct got_error *err = NULL;
675 struct gotd_imsg_disconnect idisconnect;
676 size_t datalen;
677 int delta_cache_fd, pack_pipe;
678 struct repo_read_client *client = &repo_read_client;
680 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
681 if (datalen != sizeof(idisconnect))
682 return got_error(GOT_ERR_PRIVSEP_LEN);
683 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
685 log_debug("client disconnecting");
687 free_object_ids(&client->have_ids);
688 free_object_ids(&client->want_ids);
689 if (close(client->fd) == -1)
690 err = got_error_from_errno("close");
691 delta_cache_fd = client->delta_cache_fd;
692 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
693 return got_error_from_errno("close");
694 pack_pipe = client->pack_pipe;
695 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
696 return got_error_from_errno("close");
697 return err;
700 static void
701 repo_read_dispatch(int fd, short event, void *arg)
703 const struct got_error *err = NULL;
704 struct gotd_imsgev *iev = arg;
705 struct imsgbuf *ibuf = &iev->ibuf;
706 struct imsg imsg;
707 ssize_t n;
708 int shut = 0;
709 struct repo_read_client *client = &repo_read_client;
711 if (event & EV_READ) {
712 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
713 fatal("imsg_read error");
714 if (n == 0) /* Connection closed. */
715 shut = 1;
718 if (event & EV_WRITE) {
719 n = msgbuf_write(&ibuf->w);
720 if (n == -1 && errno != EAGAIN)
721 fatal("msgbuf_write");
722 if (n == 0) /* Connection closed. */
723 shut = 1;
726 while (err == NULL && check_cancelled(NULL) == NULL) {
727 if ((n = imsg_get(ibuf, &imsg)) == -1)
728 fatal("%s: imsg_get", __func__);
729 if (n == 0) /* No more messages. */
730 break;
732 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
733 client->id == 0) {
734 err = got_error(GOT_ERR_PRIVSEP_MSG);
735 break;
738 switch (imsg.hdr.type) {
739 case GOTD_IMSG_LIST_REFS_INTERNAL:
740 err = list_refs(&imsg);
741 if (err)
742 log_warnx("%s: ls-refs: %s", repo_read.title,
743 err->msg);
744 break;
745 case GOTD_IMSG_WANT:
746 err = recv_want(&imsg);
747 if (err)
748 log_warnx("%s: want-line: %s", repo_read.title,
749 err->msg);
750 break;
751 case GOTD_IMSG_HAVE:
752 err = recv_have(&imsg);
753 if (err)
754 log_warnx("%s: have-line: %s", repo_read.title,
755 err->msg);
756 break;
757 case GOTD_IMSG_SEND_PACKFILE:
758 err = receive_delta_cache_fd(&imsg, iev);
759 if (err)
760 log_warnx("%s: receiving delta cache: %s",
761 repo_read.title, err->msg);
762 break;
763 case GOTD_IMSG_PACKFILE_PIPE:
764 err = receive_pack_pipe(&imsg, iev);
765 if (err) {
766 log_warnx("%s: receiving pack pipe: %s",
767 repo_read.title, err->msg);
768 break;
770 err = send_packfile(&imsg, iev);
771 if (err)
772 log_warnx("%s: sending packfile: %s",
773 repo_read.title, err->msg);
774 break;
775 case GOTD_IMSG_DISCONNECT:
776 err = recv_disconnect(&imsg);
777 if (err)
778 log_warnx("%s: disconnect: %s",
779 repo_read.title, err->msg);
780 shut = 1;
781 break;
782 default:
783 log_debug("%s: unexpected imsg %d", repo_read.title,
784 imsg.hdr.type);
785 break;
788 imsg_free(&imsg);
791 if (!shut && check_cancelled(NULL) == NULL) {
792 if (err &&
793 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
794 client->id, err) == -1) {
795 log_warnx("could not send error to parent: %s",
796 err->msg);
798 gotd_imsg_event_add(iev);
799 } else {
800 /* This pipe is dead. Remove its event handler */
801 event_del(&iev->ev);
802 event_loopexit(NULL);
806 void
807 repo_read_main(const char *title, const char *repo_path,
808 int *pack_fds, int *temp_fds)
810 const struct got_error *err = NULL;
811 struct gotd_imsgev iev;
813 repo_read.title = title;
814 repo_read.pid = getpid();
815 repo_read.pack_fds = pack_fds;
816 repo_read.temp_fds = temp_fds;
818 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
819 if (err)
820 goto done;
821 if (!got_repo_is_bare(repo_read.repo)) {
822 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
823 "bare git repository required");
824 goto done;
827 got_repo_temp_fds_set(repo_read.repo, temp_fds);
829 signal(SIGINT, catch_sigint);
830 signal(SIGTERM, catch_sigterm);
831 signal(SIGPIPE, SIG_IGN);
832 signal(SIGHUP, SIG_IGN);
834 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
835 iev.handler = repo_read_dispatch;
836 iev.events = EV_READ;
837 iev.handler_arg = NULL;
838 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
840 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
841 PROC_REPO_READ, -1, NULL, 0) == -1) {
842 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
843 goto done;
846 event_dispatch();
847 done:
848 if (err)
849 log_warnx("%s: %s", title, err->msg);
850 repo_read_shutdown();
853 void
854 repo_read_shutdown(void)
856 log_debug("%s: shutting down", repo_read.title);
857 if (repo_read.repo)
858 got_repo_close(repo_read.repo);
859 got_repo_pack_fds_close(repo_read.pack_fds);
860 got_repo_temp_fds_close(repo_read.temp_fds);
861 exit(0);