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 int session_fd;
64 struct gotd_imsgev session_iev;
65 } repo_read;
67 static struct repo_read_client {
68 uint32_t id;
69 int fd;
70 int delta_cache_fd;
71 int report_progress;
72 int pack_pipe;
73 struct gotd_object_id_array want_ids;
74 struct gotd_object_id_array have_ids;
75 } repo_read_client;
77 static volatile sig_atomic_t sigint_received;
78 static volatile sig_atomic_t sigterm_received;
80 static void
81 catch_sigint(int signo)
82 {
83 sigint_received = 1;
84 }
86 static void
87 catch_sigterm(int signo)
88 {
89 sigterm_received = 1;
90 }
92 static const struct got_error *
93 check_cancelled(void *arg)
94 {
95 if (sigint_received || sigterm_received)
96 return got_error(GOT_ERR_CANCELLED);
98 return NULL;
99 }
101 static const struct got_error *
102 send_symref(struct got_reference *symref, struct got_object_id *target_id,
103 struct imsgbuf *ibuf)
105 const struct got_error *err = NULL;
106 struct gotd_imsg_symref isymref;
107 const char *refname = got_ref_get_name(symref);
108 const char *target = got_ref_get_symref_target(symref);
109 size_t len;
110 struct ibuf *wbuf;
112 memset(&isymref, 0, sizeof(isymref));
113 isymref.name_len = strlen(refname);
114 isymref.target_len = strlen(target);
115 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
117 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
118 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
119 err = got_error(GOT_ERR_NO_SPACE);
120 goto done;
123 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
124 if (wbuf == NULL) {
125 err = got_error_from_errno("imsg_create SYMREF");
126 goto done;
129 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
130 err = got_error_from_errno("imsg_add SYMREF");
131 goto done;
133 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
134 err = got_error_from_errno("imsg_add SYMREF");
135 goto done;
137 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
138 err = got_error_from_errno("imsg_add SYMREF");
139 goto done;
142 wbuf->fd = -1;
143 imsg_close(ibuf, wbuf);
144 done:
145 free(target_id);
146 return err;
149 static const struct got_error *
150 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
151 struct imsgbuf *ibuf)
153 const struct got_error *err = NULL;
154 struct got_tag_object *tag;
155 size_t namelen, len;
156 char *peeled_refname = NULL;
157 struct got_object_id *id;
158 struct ibuf *wbuf;
160 err = got_object_tag_open(&tag, repo_read.repo, obj);
161 if (err)
162 return err;
164 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
165 err = got_error_from_errno("asprintf");
166 goto done;
169 id = got_object_tag_get_object_id(tag);
170 namelen = strlen(peeled_refname);
172 len = sizeof(struct gotd_imsg_ref) + namelen;
173 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
174 err = got_error(GOT_ERR_NO_SPACE);
175 goto done;
178 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
179 repo_read.pid, len);
180 if (wbuf == NULL) {
181 err = got_error_from_errno("imsg_create MREF");
182 goto done;
185 /* Keep in sync with struct gotd_imsg_ref definition. */
186 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
187 err = got_error_from_errno("imsg_add REF");
188 goto done;
190 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
191 err = got_error_from_errno("imsg_add REF");
192 goto done;
194 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
195 err = got_error_from_errno("imsg_add REF");
196 goto done;
199 wbuf->fd = -1;
200 imsg_close(ibuf, wbuf);
201 done:
202 got_object_tag_close(tag);
203 return err;
206 static const struct got_error *
207 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
209 const struct got_error *err;
210 const char *refname = got_ref_get_name(ref);
211 size_t namelen;
212 struct got_object_id *id = NULL;
213 struct got_object *obj = NULL;
214 size_t len;
215 struct ibuf *wbuf;
217 namelen = strlen(refname);
219 len = sizeof(struct gotd_imsg_ref) + namelen;
220 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 return got_error(GOT_ERR_NO_SPACE);
223 err = got_ref_resolve(&id, repo_read.repo, ref);
224 if (err)
225 return err;
227 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
228 repo_read.pid, len);
229 if (wbuf == NULL) {
230 err = got_error_from_errno("imsg_create REF");
231 goto done;
234 /* Keep in sync with struct gotd_imsg_ref definition. */
235 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
236 return got_error_from_errno("imsg_add REF");
237 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf, refname, namelen) == -1)
240 return got_error_from_errno("imsg_add REF");
242 wbuf->fd = -1;
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 struct gotd_imsg_list_refs_internal ireq;
265 size_t datalen;
266 struct gotd_imsg_reflist irefs;
267 struct imsgbuf ibuf;
268 int client_fd = imsg->fd;
269 struct got_object_id *head_target_id = NULL;
271 TAILQ_INIT(&refs);
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 != sizeof(ireq))
278 return got_error(GOT_ERR_PRIVSEP_LEN);
279 memcpy(&ireq, imsg->data, sizeof(ireq));
281 if (ireq.client_id == 0)
282 return got_error(GOT_ERR_CLIENT_ID);
283 if (client->id != 0) {
284 return got_error_msg(GOT_ERR_CLIENT_ID,
285 "duplicate list-refs request");
287 client->id = ireq.client_id;
288 client->fd = client_fd;
289 client->delta_cache_fd = -1;
290 client->pack_pipe = -1;
292 imsg_init(&ibuf, client_fd);
294 err = got_ref_list(&refs, repo_read.repo, "",
295 got_ref_cmp_by_name, NULL);
296 if (err)
297 return err;
299 memset(&irefs, 0, sizeof(irefs));
300 TAILQ_FOREACH(re, &refs, entry) {
301 struct got_object_id *id;
302 int obj_type;
304 if (got_ref_is_symbolic(re->ref)) {
305 const char *refname = got_ref_get_name(re->ref);
306 if (strcmp(refname, GOT_REF_HEAD) != 0)
307 continue;
308 err = got_ref_resolve(&head_target_id, repo_read.repo,
309 re->ref);
310 if (err) {
311 if (err->code != GOT_ERR_NOT_REF)
312 return err;
313 /*
314 * HEAD points to a non-existent branch.
315 * Do not advertise it.
316 * Matches git-daemon's behaviour.
317 */
318 head_target_id = NULL;
319 err = NULL;
320 } else
321 irefs.nrefs++;
322 continue;
325 irefs.nrefs++;
327 /* Account for a peeled tag refs. */
328 err = got_ref_resolve(&id, repo_read.repo, re->ref);
329 if (err)
330 goto done;
331 err = got_object_get_type(&obj_type, repo_read.repo, id);
332 free(id);
333 if (err)
334 goto done;
335 if (obj_type == GOT_OBJ_TYPE_TAG)
336 irefs.nrefs++;
339 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
340 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
341 err = got_error_from_errno("imsg_compose REFLIST");
342 goto done;
345 /*
346 * Send the HEAD symref first. In Git-protocol versions < 2
347 * the HEAD symref must be announced on the initial line of
348 * the server's ref advertisement.
349 * For now, we do not advertise symrefs other than HEAD.
350 */
351 TAILQ_FOREACH(re, &refs, entry) {
352 if (!got_ref_is_symbolic(re->ref) ||
353 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
354 head_target_id == NULL)
355 continue;
356 err = send_symref(re->ref, head_target_id, &ibuf);
357 if (err)
358 goto done;
359 break;
361 TAILQ_FOREACH(re, &refs, entry) {
362 if (got_ref_is_symbolic(re->ref))
363 continue;
364 err = send_ref(re->ref, &ibuf);
365 if (err)
366 goto done;
369 err = gotd_imsg_flush(&ibuf);
370 done:
371 got_ref_list_free(&refs);
372 imsg_clear(&ibuf);
373 return err;
376 static const struct got_error *
377 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
379 const size_t alloc_chunksz = 256;
381 if (array->ids == NULL) {
382 array->ids = reallocarray(NULL, alloc_chunksz,
383 sizeof(*array->ids));
384 if (array->ids == NULL)
385 return got_error_from_errno("reallocarray");
386 array->nalloc = alloc_chunksz;
387 array->nids = 0;
388 } else if (array->nalloc <= array->nids) {
389 struct got_object_id **new;
390 new = recallocarray(array->ids, array->nalloc,
391 array->nalloc + alloc_chunksz, sizeof(*new));
392 if (new == NULL)
393 return got_error_from_errno("recallocarray");
394 array->ids = new;
395 array->nalloc += alloc_chunksz;
398 array->ids[array->nids] = got_object_id_dup(id);
399 if (array->ids[array->nids] == NULL)
400 return got_error_from_errno("got_object_id_dup");
401 array->nids++;
402 return NULL;
405 static void
406 free_object_ids(struct gotd_object_id_array *array)
408 size_t i;
410 for (i = 0; i < array->nids; i++)
411 free(array->ids[i]);
412 free(array->ids);
414 array->ids = NULL;
415 array->nalloc = 0;
416 array->nids = 0;
419 static const struct got_error *
420 recv_want(struct imsg *imsg)
422 const struct got_error *err;
423 struct repo_read_client *client = &repo_read_client;
424 struct gotd_imsg_want iwant;
425 size_t datalen;
426 char hex[SHA1_DIGEST_STRING_LENGTH];
427 struct got_object_id id;
428 int obj_type;
429 struct imsgbuf ibuf;
431 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
432 if (datalen != sizeof(iwant))
433 return got_error(GOT_ERR_PRIVSEP_LEN);
434 memcpy(&iwant, imsg->data, sizeof(iwant));
436 memset(&id, 0, sizeof(id));
437 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
439 if (log_getverbose() > 0 &&
440 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
441 log_debug("client wants %s", hex);
443 imsg_init(&ibuf, client->fd);
445 err = got_object_get_type(&obj_type, repo_read.repo, &id);
446 if (err)
447 return err;
449 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
450 obj_type != GOT_OBJ_TYPE_TAG)
451 return got_error(GOT_ERR_OBJ_TYPE);
453 err = record_object_id(&client->want_ids, &id);
454 if (err)
455 return err;
457 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
458 imsg_clear(&ibuf);
459 return err;
462 static const struct got_error *
463 recv_have(struct imsg *imsg)
465 const struct got_error *err;
466 struct repo_read_client *client = &repo_read_client;
467 struct gotd_imsg_have ihave;
468 size_t datalen;
469 char hex[SHA1_DIGEST_STRING_LENGTH];
470 struct got_object_id id;
471 int obj_type;
472 struct imsgbuf ibuf;
474 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
475 if (datalen != sizeof(ihave))
476 return got_error(GOT_ERR_PRIVSEP_LEN);
477 memcpy(&ihave, imsg->data, sizeof(ihave));
479 memset(&id, 0, sizeof(id));
480 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
482 if (log_getverbose() > 0 &&
483 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
484 log_debug("client has %s", hex);
486 imsg_init(&ibuf, client->fd);
488 err = got_object_get_type(&obj_type, repo_read.repo, &id);
489 if (err) {
490 if (err->code == GOT_ERR_NO_OBJ) {
491 gotd_imsg_send_nak(&id, &ibuf,
492 PROC_REPO_READ, repo_read.pid);
493 err = NULL;
495 goto done;
498 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
499 obj_type != GOT_OBJ_TYPE_TAG) {
500 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
501 err = got_error(GOT_ERR_OBJ_TYPE);
502 goto done;
505 err = record_object_id(&client->have_ids, &id);
506 if (err)
507 return err;
509 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
510 done:
511 imsg_clear(&ibuf);
512 return err;
515 struct repo_read_pack_progress_arg {
516 int report_progress;
517 struct imsgbuf *ibuf;
518 int sent_ready;
519 };
521 static const struct got_error *
522 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
523 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
524 int nobj_written)
526 struct repo_read_pack_progress_arg *a = arg;
527 struct gotd_imsg_packfile_progress iprog;
528 int ret;
530 if (!a->report_progress)
531 return NULL;
532 if (packfile_size > 0 && a->sent_ready)
533 return NULL;
535 memset(&iprog, 0, sizeof(iprog));
536 iprog.ncolored = ncolored;
537 iprog.nfound = nfound;
538 iprog.ntrees = ntrees;
539 iprog.packfile_size = packfile_size;
540 iprog.ncommits = ncommits;
541 iprog.nobj_total = nobj_total;
542 iprog.nobj_deltify = nobj_deltify;
543 iprog.nobj_written = nobj_written;
545 /* Using synchronous writes since we are blocking the event loop. */
546 if (packfile_size == 0) {
547 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
548 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
549 if (ret == -1) {
550 return got_error_from_errno("imsg compose "
551 "PACKFILE_PROGRESS");
553 } else {
554 a->sent_ready = 1;
555 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
556 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
557 if (ret == -1) {
558 return got_error_from_errno("imsg compose "
559 "PACKFILE_READY");
563 return gotd_imsg_flush(a->ibuf);
566 static const struct got_error *
567 receive_delta_cache_fd(struct imsg *imsg,
568 struct gotd_imsgev *iev)
570 struct repo_read_client *client = &repo_read_client;
571 struct gotd_imsg_send_packfile ireq;
572 size_t datalen;
574 log_debug("receving delta cache file");
576 if (imsg->fd == -1)
577 return got_error(GOT_ERR_PRIVSEP_NO_FD);
579 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 if (datalen != sizeof(ireq))
581 return got_error(GOT_ERR_PRIVSEP_LEN);
582 memcpy(&ireq, imsg->data, sizeof(ireq));
584 if (client->delta_cache_fd != -1)
585 return got_error(GOT_ERR_PRIVSEP_MSG);
587 client->delta_cache_fd = imsg->fd;
588 client->report_progress = ireq.report_progress;
589 return NULL;
592 static const struct got_error *
593 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
595 struct repo_read_client *client = &repo_read_client;
596 struct gotd_imsg_packfile_pipe ireq;
597 size_t datalen;
599 log_debug("receving pack pipe descriptor");
601 if (imsg->fd == -1)
602 return got_error(GOT_ERR_PRIVSEP_NO_FD);
604 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
605 if (datalen != sizeof(ireq))
606 return got_error(GOT_ERR_PRIVSEP_LEN);
607 memcpy(&ireq, imsg->data, sizeof(ireq));
609 if (client->pack_pipe != -1)
610 return got_error(GOT_ERR_PRIVSEP_MSG);
612 client->pack_pipe = imsg->fd;
613 return NULL;
616 static const struct got_error *
617 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
619 const struct got_error *err = NULL;
620 struct repo_read_client *client = &repo_read_client;
621 struct gotd_imsg_packfile_done idone;
622 uint8_t packsha1[SHA1_DIGEST_LENGTH];
623 char hex[SHA1_DIGEST_STRING_LENGTH];
624 FILE *delta_cache = NULL;
625 struct imsgbuf ibuf;
626 struct repo_read_pack_progress_arg pa;
627 struct got_ratelimit rl;
629 log_debug("packfile request received");
631 got_ratelimit_init(&rl, 2, 0);
633 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
634 return got_error(GOT_ERR_PRIVSEP_NO_FD);
636 imsg_init(&ibuf, client->fd);
638 delta_cache = fdopen(client->delta_cache_fd, "w+");
639 if (delta_cache == NULL) {
640 err = got_error_from_errno("fdopen");
641 goto done;
643 client->delta_cache_fd = -1;
645 memset(&pa, 0, sizeof(pa));
646 pa.ibuf = &ibuf;
647 pa.report_progress = client->report_progress;
649 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
650 client->have_ids.ids, client->have_ids.nids,
651 client->want_ids.ids, client->want_ids.nids,
652 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
653 check_cancelled, NULL);
654 if (err)
655 goto done;
657 if (log_getverbose() > 0 &&
658 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
659 log_debug("sent pack-%s.pack", hex);
661 memset(&idone, 0, sizeof(idone));
662 idone.client_id = client->id;
663 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
664 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
665 err = got_error_from_errno("imsg compose PACKFILE_DONE");
666 done:
667 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
668 err = got_error_from_errno("fclose");
669 imsg_clear(&ibuf);
670 return err;
673 static const struct got_error *
674 recv_disconnect(struct imsg *imsg)
676 const struct got_error *err = NULL;
677 struct gotd_imsg_disconnect idisconnect;
678 size_t datalen;
679 int delta_cache_fd, pack_pipe;
680 struct repo_read_client *client = &repo_read_client;
682 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
683 if (datalen != sizeof(idisconnect))
684 return got_error(GOT_ERR_PRIVSEP_LEN);
685 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
687 log_debug("client disconnecting");
689 free_object_ids(&client->have_ids);
690 free_object_ids(&client->want_ids);
691 if (close(client->fd) == -1)
692 err = got_error_from_errno("close");
693 delta_cache_fd = client->delta_cache_fd;
694 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
695 return got_error_from_errno("close");
696 pack_pipe = client->pack_pipe;
697 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
698 return got_error_from_errno("close");
699 return err;
702 static void
703 repo_read_dispatch_session(int fd, short event, void *arg)
705 const struct got_error *err = NULL;
706 struct gotd_imsgev *iev = arg;
707 struct imsgbuf *ibuf = &iev->ibuf;
708 struct imsg imsg;
709 ssize_t n;
710 int shut = 0;
711 struct repo_read_client *client = &repo_read_client;
713 if (event & EV_READ) {
714 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
715 fatal("imsg_read error");
716 if (n == 0) /* Connection closed. */
717 shut = 1;
720 if (event & EV_WRITE) {
721 n = msgbuf_write(&ibuf->w);
722 if (n == -1 && errno != EAGAIN)
723 fatal("msgbuf_write");
724 if (n == 0) /* Connection closed. */
725 shut = 1;
728 while (err == NULL && check_cancelled(NULL) == NULL) {
729 if ((n = imsg_get(ibuf, &imsg)) == -1)
730 fatal("%s: imsg_get", __func__);
731 if (n == 0) /* No more messages. */
732 break;
734 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
735 client->id == 0) {
736 err = got_error(GOT_ERR_PRIVSEP_MSG);
737 break;
740 switch (imsg.hdr.type) {
741 case GOTD_IMSG_LIST_REFS_INTERNAL:
742 err = list_refs(&imsg);
743 if (err)
744 log_warnx("%s: ls-refs: %s", repo_read.title,
745 err->msg);
746 break;
747 case GOTD_IMSG_WANT:
748 err = recv_want(&imsg);
749 if (err)
750 log_warnx("%s: want-line: %s", repo_read.title,
751 err->msg);
752 break;
753 case GOTD_IMSG_HAVE:
754 err = recv_have(&imsg);
755 if (err)
756 log_warnx("%s: have-line: %s", repo_read.title,
757 err->msg);
758 break;
759 case GOTD_IMSG_SEND_PACKFILE:
760 err = receive_delta_cache_fd(&imsg, iev);
761 if (err)
762 log_warnx("%s: receiving delta cache: %s",
763 repo_read.title, err->msg);
764 break;
765 case GOTD_IMSG_PACKFILE_PIPE:
766 err = receive_pack_pipe(&imsg, iev);
767 if (err) {
768 log_warnx("%s: receiving pack pipe: %s",
769 repo_read.title, err->msg);
770 break;
772 err = send_packfile(&imsg, iev);
773 if (err)
774 log_warnx("%s: sending packfile: %s",
775 repo_read.title, err->msg);
776 break;
777 case GOTD_IMSG_DISCONNECT:
778 err = recv_disconnect(&imsg);
779 if (err)
780 log_warnx("%s: disconnect: %s",
781 repo_read.title, err->msg);
782 shut = 1;
783 break;
784 default:
785 log_debug("%s: unexpected imsg %d", repo_read.title,
786 imsg.hdr.type);
787 break;
790 imsg_free(&imsg);
793 if (!shut && check_cancelled(NULL) == NULL) {
794 if (err &&
795 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
796 client->id, err) == -1) {
797 log_warnx("could not send error to parent: %s",
798 err->msg);
800 gotd_imsg_event_add(iev);
801 } else {
802 /* This pipe is dead. Remove its event handler */
803 event_del(&iev->ev);
804 event_loopexit(NULL);
808 static const struct got_error *
809 recv_connect(struct imsg *imsg)
811 struct gotd_imsgev *iev = &repo_read.session_iev;
812 size_t datalen;
814 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
815 if (datalen != 0)
816 return got_error(GOT_ERR_PRIVSEP_LEN);
817 if (imsg->fd == -1)
818 return got_error(GOT_ERR_PRIVSEP_NO_FD);
820 if (repo_read.session_fd != -1)
821 return got_error(GOT_ERR_PRIVSEP_MSG);
823 repo_read.session_fd = imsg->fd;
825 imsg_init(&iev->ibuf, repo_read.session_fd);
826 iev->handler = repo_read_dispatch_session;
827 iev->events = EV_READ;
828 iev->handler_arg = NULL;
829 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
830 repo_read_dispatch_session, iev);
831 gotd_imsg_event_add(iev);
833 return NULL;
836 static void
837 repo_read_dispatch(int fd, short event, void *arg)
839 const struct got_error *err = NULL;
840 struct gotd_imsgev *iev = arg;
841 struct imsgbuf *ibuf = &iev->ibuf;
842 struct imsg imsg;
843 ssize_t n;
844 int shut = 0;
845 struct repo_read_client *client = &repo_read_client;
847 if (event & EV_READ) {
848 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
849 fatal("imsg_read error");
850 if (n == 0) /* Connection closed. */
851 shut = 1;
854 if (event & EV_WRITE) {
855 n = msgbuf_write(&ibuf->w);
856 if (n == -1 && errno != EAGAIN)
857 fatal("msgbuf_write");
858 if (n == 0) /* Connection closed. */
859 shut = 1;
862 while (err == NULL && check_cancelled(NULL) == NULL) {
863 if ((n = imsg_get(ibuf, &imsg)) == -1)
864 fatal("%s: imsg_get", __func__);
865 if (n == 0) /* No more messages. */
866 break;
868 switch (imsg.hdr.type) {
869 case GOTD_IMSG_CONNECT_REPO_CHILD:
870 err = recv_connect(&imsg);
871 break;
872 default:
873 log_debug("%s: unexpected imsg %d", repo_read.title,
874 imsg.hdr.type);
875 break;
878 imsg_free(&imsg);
881 if (!shut && check_cancelled(NULL) == NULL) {
882 if (err &&
883 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
884 client->id, err) == -1) {
885 log_warnx("could not send error to parent: %s",
886 err->msg);
888 gotd_imsg_event_add(iev);
889 } else {
890 /* This pipe is dead. Remove its event handler */
891 event_del(&iev->ev);
892 event_loopexit(NULL);
896 void
897 repo_read_main(const char *title, const char *repo_path,
898 int *pack_fds, int *temp_fds)
900 const struct got_error *err = NULL;
901 struct gotd_imsgev iev;
903 repo_read.title = title;
904 repo_read.pid = getpid();
905 repo_read.pack_fds = pack_fds;
906 repo_read.temp_fds = temp_fds;
907 repo_read.session_fd = -1;
908 repo_read.session_iev.ibuf.fd = -1;
910 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
911 if (err)
912 goto done;
913 if (!got_repo_is_bare(repo_read.repo)) {
914 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
915 "bare git repository required");
916 goto done;
919 got_repo_temp_fds_set(repo_read.repo, temp_fds);
921 signal(SIGINT, catch_sigint);
922 signal(SIGTERM, catch_sigterm);
923 signal(SIGPIPE, SIG_IGN);
924 signal(SIGHUP, SIG_IGN);
926 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
927 iev.handler = repo_read_dispatch;
928 iev.events = EV_READ;
929 iev.handler_arg = NULL;
930 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
932 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
933 PROC_REPO_READ, -1, NULL, 0) == -1) {
934 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
935 goto done;
938 event_dispatch();
939 done:
940 if (err)
941 log_warnx("%s: %s", title, err->msg);
942 repo_read_shutdown();
945 void
946 repo_read_shutdown(void)
948 log_debug("%s: shutting down", repo_read.title);
949 if (repo_read.repo)
950 got_repo_close(repo_read.repo);
951 got_repo_pack_fds_close(repo_read.pack_fds);
952 got_repo_temp_fds_close(repo_read.temp_fds);
953 if (repo_read.session_fd != -1)
954 close(repo_read.session_fd);
955 exit(0);