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;
290 client->delta_cache_fd = -1;
291 client->pack_pipe = -1;
293 imsg_init(&ibuf, client_fd);
295 err = got_ref_list(&refs, repo_read.repo, "",
296 got_ref_cmp_by_name, NULL);
297 if (err)
298 return err;
300 memset(&irefs, 0, sizeof(irefs));
301 TAILQ_FOREACH(re, &refs, entry) {
302 struct got_object_id *id;
303 int obj_type;
305 if (got_ref_is_symbolic(re->ref)) {
306 const char *refname = got_ref_get_name(re->ref);
307 if (strcmp(refname, GOT_REF_HEAD) != 0)
308 continue;
309 err = got_ref_resolve(&head_target_id, repo_read.repo,
310 re->ref);
311 if (err) {
312 if (err->code != GOT_ERR_NOT_REF)
313 return err;
314 /*
315 * HEAD points to a non-existent branch.
316 * Do not advertise it.
317 * Matches git-daemon's behaviour.
318 */
319 head_target_id = NULL;
320 err = NULL;
321 } else
322 irefs.nrefs++;
323 continue;
326 irefs.nrefs++;
328 /* Account for a peeled tag refs. */
329 err = got_ref_resolve(&id, repo_read.repo, re->ref);
330 if (err)
331 goto done;
332 err = got_object_get_type(&obj_type, repo_read.repo, id);
333 free(id);
334 if (err)
335 goto done;
336 if (obj_type == GOT_OBJ_TYPE_TAG)
337 irefs.nrefs++;
340 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
341 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
342 err = got_error_from_errno("imsg_compose REFLIST");
343 goto done;
346 /*
347 * Send the HEAD symref first. In Git-protocol versions < 2
348 * the HEAD symref must be announced on the initial line of
349 * the server's ref advertisement.
350 * For now, we do not advertise symrefs other than HEAD.
351 */
352 TAILQ_FOREACH(re, &refs, entry) {
353 if (!got_ref_is_symbolic(re->ref) ||
354 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
355 head_target_id == NULL)
356 continue;
357 err = send_symref(re->ref, head_target_id, &ibuf);
358 if (err)
359 goto done;
360 break;
362 TAILQ_FOREACH(re, &refs, entry) {
363 if (got_ref_is_symbolic(re->ref))
364 continue;
365 err = send_ref(re->ref, &ibuf);
366 if (err)
367 goto done;
370 err = gotd_imsg_flush(&ibuf);
371 done:
372 got_ref_list_free(&refs);
373 imsg_clear(&ibuf);
374 return err;
377 static const struct got_error *
378 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
380 const size_t alloc_chunksz = 256;
382 if (array->ids == NULL) {
383 array->ids = reallocarray(NULL, alloc_chunksz,
384 sizeof(*array->ids));
385 if (array->ids == NULL)
386 return got_error_from_errno("reallocarray");
387 array->nalloc = alloc_chunksz;
388 array->nids = 0;
389 } else if (array->nalloc <= array->nids) {
390 struct got_object_id **new;
391 new = recallocarray(array->ids, array->nalloc,
392 array->nalloc + alloc_chunksz, sizeof(*new));
393 if (new == NULL)
394 return got_error_from_errno("recallocarray");
395 array->ids = new;
396 array->nalloc += alloc_chunksz;
399 array->ids[array->nids] = got_object_id_dup(id);
400 if (array->ids[array->nids] == NULL)
401 return got_error_from_errno("got_object_id_dup");
402 array->nids++;
403 return NULL;
406 static void
407 free_object_ids(struct gotd_object_id_array *array)
409 size_t i;
411 for (i = 0; i < array->nids; i++)
412 free(array->ids[i]);
413 free(array->ids);
415 array->ids = NULL;
416 array->nalloc = 0;
417 array->nids = 0;
420 static const struct got_error *
421 recv_want(struct imsg *imsg)
423 const struct got_error *err;
424 struct repo_read_client *client = &repo_read_client;
425 struct gotd_imsg_want iwant;
426 size_t datalen;
427 char hex[SHA1_DIGEST_STRING_LENGTH];
428 struct got_object_id id;
429 int obj_type;
430 struct imsgbuf ibuf;
432 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
433 if (datalen != sizeof(iwant))
434 return got_error(GOT_ERR_PRIVSEP_LEN);
435 memcpy(&iwant, imsg->data, sizeof(iwant));
437 memset(&id, 0, sizeof(id));
438 memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
440 if (log_getverbose() > 0 &&
441 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
442 log_debug("client wants %s", hex);
444 imsg_init(&ibuf, client->fd);
446 err = got_object_get_type(&obj_type, repo_read.repo, &id);
447 if (err)
448 return err;
450 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
451 obj_type != GOT_OBJ_TYPE_TAG)
452 return got_error(GOT_ERR_OBJ_TYPE);
454 err = record_object_id(&client->want_ids, &id);
455 if (err)
456 return err;
458 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
459 imsg_clear(&ibuf);
460 return err;
463 static const struct got_error *
464 recv_have(struct imsg *imsg)
466 const struct got_error *err;
467 struct repo_read_client *client = &repo_read_client;
468 struct gotd_imsg_have ihave;
469 size_t datalen;
470 char hex[SHA1_DIGEST_STRING_LENGTH];
471 struct got_object_id id;
472 int obj_type;
473 struct imsgbuf ibuf;
475 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
476 if (datalen != sizeof(ihave))
477 return got_error(GOT_ERR_PRIVSEP_LEN);
478 memcpy(&ihave, imsg->data, sizeof(ihave));
480 memset(&id, 0, sizeof(id));
481 memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
483 if (log_getverbose() > 0 &&
484 got_sha1_digest_to_str(id.hash, hex, sizeof(hex)))
485 log_debug("client has %s", hex);
487 imsg_init(&ibuf, client->fd);
489 err = got_object_get_type(&obj_type, repo_read.repo, &id);
490 if (err) {
491 if (err->code == GOT_ERR_NO_OBJ) {
492 gotd_imsg_send_nak(&id, &ibuf,
493 PROC_REPO_READ, repo_read.pid);
494 err = NULL;
496 goto done;
499 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
500 obj_type != GOT_OBJ_TYPE_TAG) {
501 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
502 err = got_error(GOT_ERR_OBJ_TYPE);
503 goto done;
506 err = record_object_id(&client->have_ids, &id);
507 if (err)
508 return err;
510 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
511 done:
512 imsg_clear(&ibuf);
513 return err;
516 struct repo_read_pack_progress_arg {
517 int report_progress;
518 struct imsgbuf *ibuf;
519 int sent_ready;
520 };
522 static const struct got_error *
523 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
524 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
525 int nobj_written)
527 struct repo_read_pack_progress_arg *a = arg;
528 struct gotd_imsg_packfile_progress iprog;
529 int ret;
531 if (!a->report_progress)
532 return NULL;
533 if (packfile_size > 0 && a->sent_ready)
534 return NULL;
536 memset(&iprog, 0, sizeof(iprog));
537 iprog.ncolored = ncolored;
538 iprog.nfound = nfound;
539 iprog.ntrees = ntrees;
540 iprog.packfile_size = packfile_size;
541 iprog.ncommits = ncommits;
542 iprog.nobj_total = nobj_total;
543 iprog.nobj_deltify = nobj_deltify;
544 iprog.nobj_written = nobj_written;
546 /* Using synchronous writes since we are blocking the event loop. */
547 if (packfile_size == 0) {
548 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
549 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
550 if (ret == -1) {
551 return got_error_from_errno("imsg compose "
552 "PACKFILE_PROGRESS");
554 } else {
555 a->sent_ready = 1;
556 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
557 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
558 if (ret == -1) {
559 return got_error_from_errno("imsg compose "
560 "PACKFILE_READY");
564 return gotd_imsg_flush(a->ibuf);
567 static const struct got_error *
568 receive_delta_cache_fd(struct imsg *imsg,
569 struct gotd_imsgev *iev)
571 struct repo_read_client *client = &repo_read_client;
572 struct gotd_imsg_send_packfile ireq;
573 size_t datalen;
575 log_debug("receving delta cache file");
577 if (imsg->fd == -1)
578 return got_error(GOT_ERR_PRIVSEP_NO_FD);
580 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
581 if (datalen != sizeof(ireq))
582 return got_error(GOT_ERR_PRIVSEP_LEN);
583 memcpy(&ireq, imsg->data, sizeof(ireq));
585 if (client->delta_cache_fd != -1)
586 return got_error(GOT_ERR_PRIVSEP_MSG);
588 client->delta_cache_fd = imsg->fd;
589 client->report_progress = ireq.report_progress;
590 return NULL;
593 static const struct got_error *
594 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
596 struct repo_read_client *client = &repo_read_client;
597 struct gotd_imsg_packfile_pipe ireq;
598 size_t datalen;
600 log_debug("receving pack pipe descriptor");
602 if (imsg->fd == -1)
603 return got_error(GOT_ERR_PRIVSEP_NO_FD);
605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 if (datalen != sizeof(ireq))
607 return got_error(GOT_ERR_PRIVSEP_LEN);
608 memcpy(&ireq, imsg->data, sizeof(ireq));
610 if (client->pack_pipe != -1)
611 return got_error(GOT_ERR_PRIVSEP_MSG);
613 client->pack_pipe = imsg->fd;
614 return NULL;
617 static const struct got_error *
618 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
620 const struct got_error *err = NULL;
621 struct repo_read_client *client = &repo_read_client;
622 struct gotd_imsg_packfile_done idone;
623 uint8_t packhash[SHA1_DIGEST_LENGTH];
624 char hex[SHA1_DIGEST_STRING_LENGTH];
625 FILE *delta_cache = NULL;
626 struct imsgbuf ibuf;
627 struct repo_read_pack_progress_arg pa;
628 struct got_ratelimit rl;
630 log_debug("packfile request received");
632 got_ratelimit_init(&rl, 2, 0);
634 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
635 return got_error(GOT_ERR_PRIVSEP_NO_FD);
637 imsg_init(&ibuf, client->fd);
639 delta_cache = fdopen(client->delta_cache_fd, "w+");
640 if (delta_cache == NULL) {
641 err = got_error_from_errno("fdopen");
642 goto done;
644 client->delta_cache_fd = -1;
646 memset(&pa, 0, sizeof(pa));
647 pa.ibuf = &ibuf;
648 pa.report_progress = client->report_progress;
650 err = got_pack_create(packhash, client->pack_pipe, delta_cache,
651 client->have_ids.ids, client->have_ids.nids,
652 client->want_ids.ids, client->want_ids.nids,
653 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
654 check_cancelled, NULL);
655 if (err)
656 goto done;
658 if (log_getverbose() > 0 &&
659 got_sha1_digest_to_str(packhash, hex, sizeof(hex)))
660 log_debug("sent pack-%s.pack", hex);
662 memset(&idone, 0, sizeof(idone));
663 idone.client_id = client->id;
664 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
665 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
666 err = got_error_from_errno("imsg compose PACKFILE_DONE");
667 done:
668 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
669 err = got_error_from_errno("fclose");
670 imsg_clear(&ibuf);
671 return err;
674 static const struct got_error *
675 recv_disconnect(struct imsg *imsg)
677 const struct got_error *err = NULL;
678 struct gotd_imsg_disconnect idisconnect;
679 size_t datalen;
680 int delta_cache_fd, pack_pipe;
681 struct repo_read_client *client = &repo_read_client;
683 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
684 if (datalen != sizeof(idisconnect))
685 return got_error(GOT_ERR_PRIVSEP_LEN);
686 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
688 log_debug("client disconnecting");
690 free_object_ids(&client->have_ids);
691 free_object_ids(&client->want_ids);
692 if (close(client->fd) == -1)
693 err = got_error_from_errno("close");
694 delta_cache_fd = client->delta_cache_fd;
695 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
696 return got_error_from_errno("close");
697 pack_pipe = client->pack_pipe;
698 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
699 return got_error_from_errno("close");
700 return err;
703 static void
704 repo_read_dispatch_session(int fd, short event, void *arg)
706 const struct got_error *err = NULL;
707 struct gotd_imsgev *iev = arg;
708 struct imsgbuf *ibuf = &iev->ibuf;
709 struct imsg imsg;
710 ssize_t n;
711 int shut = 0;
712 struct repo_read_client *client = &repo_read_client;
714 if (event & EV_READ) {
715 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
716 fatal("imsg_read error");
717 if (n == 0) /* Connection closed. */
718 shut = 1;
721 if (event & EV_WRITE) {
722 n = msgbuf_write(&ibuf->w);
723 if (n == -1 && errno != EAGAIN)
724 fatal("msgbuf_write");
725 if (n == 0) /* Connection closed. */
726 shut = 1;
729 while (err == NULL && check_cancelled(NULL) == NULL) {
730 if ((n = imsg_get(ibuf, &imsg)) == -1)
731 fatal("%s: imsg_get", __func__);
732 if (n == 0) /* No more messages. */
733 break;
735 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
736 client->id == 0) {
737 err = got_error(GOT_ERR_PRIVSEP_MSG);
738 break;
741 switch (imsg.hdr.type) {
742 case GOTD_IMSG_LIST_REFS_INTERNAL:
743 err = list_refs(&imsg);
744 if (err)
745 log_warnx("%s: ls-refs: %s", repo_read.title,
746 err->msg);
747 break;
748 case GOTD_IMSG_WANT:
749 err = recv_want(&imsg);
750 if (err)
751 log_warnx("%s: want-line: %s", repo_read.title,
752 err->msg);
753 break;
754 case GOTD_IMSG_HAVE:
755 err = recv_have(&imsg);
756 if (err)
757 log_warnx("%s: have-line: %s", repo_read.title,
758 err->msg);
759 break;
760 case GOTD_IMSG_SEND_PACKFILE:
761 err = receive_delta_cache_fd(&imsg, iev);
762 if (err)
763 log_warnx("%s: receiving delta cache: %s",
764 repo_read.title, err->msg);
765 break;
766 case GOTD_IMSG_PACKFILE_PIPE:
767 err = receive_pack_pipe(&imsg, iev);
768 if (err) {
769 log_warnx("%s: receiving pack pipe: %s",
770 repo_read.title, err->msg);
771 break;
773 err = send_packfile(&imsg, iev);
774 if (err)
775 log_warnx("%s: sending packfile: %s",
776 repo_read.title, err->msg);
777 break;
778 case GOTD_IMSG_DISCONNECT:
779 err = recv_disconnect(&imsg);
780 if (err)
781 log_warnx("%s: disconnect: %s",
782 repo_read.title, err->msg);
783 shut = 1;
784 break;
785 default:
786 log_debug("%s: unexpected imsg %d", repo_read.title,
787 imsg.hdr.type);
788 break;
791 imsg_free(&imsg);
794 if (!shut && check_cancelled(NULL) == NULL) {
795 if (err &&
796 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
797 client->id, err) == -1) {
798 log_warnx("could not send error to parent: %s",
799 err->msg);
801 gotd_imsg_event_add(iev);
802 } else {
803 /* This pipe is dead. Remove its event handler */
804 event_del(&iev->ev);
805 event_loopexit(NULL);
809 static const struct got_error *
810 recv_connect(struct imsg *imsg)
812 struct gotd_imsgev *iev = &repo_read.session_iev;
813 size_t datalen;
815 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
816 if (datalen != 0)
817 return got_error(GOT_ERR_PRIVSEP_LEN);
818 if (imsg->fd == -1)
819 return got_error(GOT_ERR_PRIVSEP_NO_FD);
821 if (repo_read.session_fd != -1)
822 return got_error(GOT_ERR_PRIVSEP_MSG);
824 repo_read.session_fd = imsg->fd;
826 imsg_init(&iev->ibuf, repo_read.session_fd);
827 iev->handler = repo_read_dispatch_session;
828 iev->events = EV_READ;
829 iev->handler_arg = NULL;
830 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
831 repo_read_dispatch_session, iev);
832 gotd_imsg_event_add(iev);
834 return NULL;
837 static void
838 repo_read_dispatch(int fd, short event, void *arg)
840 const struct got_error *err = NULL;
841 struct gotd_imsgev *iev = arg;
842 struct imsgbuf *ibuf = &iev->ibuf;
843 struct imsg imsg;
844 ssize_t n;
845 int shut = 0;
846 struct repo_read_client *client = &repo_read_client;
848 if (event & EV_READ) {
849 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
850 fatal("imsg_read error");
851 if (n == 0) /* Connection closed. */
852 shut = 1;
855 if (event & EV_WRITE) {
856 n = msgbuf_write(&ibuf->w);
857 if (n == -1 && errno != EAGAIN)
858 fatal("msgbuf_write");
859 if (n == 0) /* Connection closed. */
860 shut = 1;
863 while (err == NULL && check_cancelled(NULL) == NULL) {
864 if ((n = imsg_get(ibuf, &imsg)) == -1)
865 fatal("%s: imsg_get", __func__);
866 if (n == 0) /* No more messages. */
867 break;
869 switch (imsg.hdr.type) {
870 case GOTD_IMSG_CONNECT_REPO_CHILD:
871 err = recv_connect(&imsg);
872 break;
873 default:
874 log_debug("%s: unexpected imsg %d", repo_read.title,
875 imsg.hdr.type);
876 break;
879 imsg_free(&imsg);
882 if (!shut && check_cancelled(NULL) == NULL) {
883 if (err &&
884 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
885 client->id, err) == -1) {
886 log_warnx("could not send error to parent: %s",
887 err->msg);
889 gotd_imsg_event_add(iev);
890 } else {
891 /* This pipe is dead. Remove its event handler */
892 event_del(&iev->ev);
893 event_loopexit(NULL);
897 void
898 repo_read_main(const char *title, const char *repo_path,
899 int *pack_fds, int *temp_fds)
901 const struct got_error *err = NULL;
902 struct gotd_imsgev iev;
904 repo_read.title = title;
905 repo_read.pid = getpid();
906 repo_read.pack_fds = pack_fds;
907 repo_read.temp_fds = temp_fds;
908 repo_read.session_fd = -1;
909 repo_read.session_iev.ibuf.fd = -1;
911 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
912 if (err)
913 goto done;
914 if (!got_repo_is_bare(repo_read.repo)) {
915 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
916 "bare git repository required");
917 goto done;
920 got_repo_temp_fds_set(repo_read.repo, temp_fds);
922 signal(SIGINT, catch_sigint);
923 signal(SIGTERM, catch_sigterm);
924 signal(SIGPIPE, SIG_IGN);
925 signal(SIGHUP, SIG_IGN);
927 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
928 iev.handler = repo_read_dispatch;
929 iev.events = EV_READ;
930 iev.handler_arg = NULL;
931 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
933 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
934 PROC_REPO_READ, -1, NULL, 0) == -1) {
935 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
936 goto done;
939 event_dispatch();
940 done:
941 if (err)
942 log_warnx("%s: %s", title, err->msg);
943 repo_read_shutdown();
946 void
947 repo_read_shutdown(void)
949 log_debug("%s: shutting down", repo_read.title);
950 if (repo_read.repo)
951 got_repo_close(repo_read.repo);
952 got_repo_pack_fds_close(repo_read.pack_fds);
953 got_repo_temp_fds_close(repo_read.temp_fds);
954 if (repo_read.session_fd != -1)
955 close(repo_read.session_fd);
956 exit(0);