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 struct repo_read_client {
66 STAILQ_ENTRY(repo_read_client) entry;
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 };
75 STAILQ_HEAD(repo_read_clients, repo_read_client);
77 static struct repo_read_clients repo_read_clients[GOTD_CLIENT_TABLE_SIZE];
78 static SIPHASH_KEY clients_hash_key;
80 static uint64_t
81 client_hash(uint32_t client_id)
82 {
83 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
84 }
86 static void
87 add_client(struct repo_read_client *client, uint32_t client_id, int fd)
88 {
89 uint64_t slot;
91 client->id = client_id;
92 client->fd = fd;
93 client->delta_cache_fd = -1;
94 client->pack_pipe = -1;
95 slot = client_hash(client->id) % nitems(repo_read_clients);
96 STAILQ_INSERT_HEAD(&repo_read_clients[slot], client, entry);
97 }
99 static struct repo_read_client *
100 find_client(uint32_t client_id)
102 uint64_t slot;
103 struct repo_read_client *c;
105 slot = client_hash(client_id) % nitems(repo_read_clients);
106 STAILQ_FOREACH(c, &repo_read_clients[slot], entry) {
107 if (c->id == client_id)
108 return c;
111 return NULL;
114 static volatile sig_atomic_t sigint_received;
115 static volatile sig_atomic_t sigterm_received;
117 static void
118 catch_sigint(int signo)
120 sigint_received = 1;
123 static void
124 catch_sigterm(int signo)
126 sigterm_received = 1;
129 static const struct got_error *
130 check_cancelled(void *arg)
132 if (sigint_received || sigterm_received)
133 return got_error(GOT_ERR_CANCELLED);
135 return NULL;
138 static const struct got_error *
139 send_symref(struct got_reference *symref, struct got_object_id *target_id,
140 struct imsgbuf *ibuf)
142 const struct got_error *err = NULL;
143 struct gotd_imsg_symref isymref;
144 const char *refname = got_ref_get_name(symref);
145 const char *target = got_ref_get_symref_target(symref);
146 size_t len;
147 struct ibuf *wbuf;
149 memset(&isymref, 0, sizeof(isymref));
150 isymref.name_len = strlen(refname);
151 isymref.target_len = strlen(target);
152 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
154 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
155 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
156 err = got_error(GOT_ERR_NO_SPACE);
157 goto done;
160 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
161 if (wbuf == NULL) {
162 err = got_error_from_errno("imsg_create SYMREF");
163 goto done;
166 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
167 err = got_error_from_errno("imsg_add SYMREF");
168 goto done;
170 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
171 err = got_error_from_errno("imsg_add SYMREF");
172 goto done;
174 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
175 err = got_error_from_errno("imsg_add SYMREF");
176 goto done;
179 wbuf->fd = -1;
180 imsg_close(ibuf, wbuf);
181 done:
182 free(target_id);
183 return err;
186 static const struct got_error *
187 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
188 struct imsgbuf *ibuf)
190 const struct got_error *err = NULL;
191 struct got_tag_object *tag;
192 size_t namelen, len;
193 char *peeled_refname = NULL;
194 struct got_object_id *id;
195 struct ibuf *wbuf;
197 err = got_object_tag_open(&tag, repo_read.repo, obj);
198 if (err)
199 return err;
201 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
202 err = got_error_from_errno("asprintf");
203 goto done;
206 id = got_object_tag_get_object_id(tag);
207 namelen = strlen(peeled_refname);
209 len = sizeof(struct gotd_imsg_ref) + namelen;
210 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
211 err = got_error(GOT_ERR_NO_SPACE);
212 goto done;
215 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
216 repo_read.pid, len);
217 if (wbuf == NULL) {
218 err = got_error_from_errno("imsg_create MREF");
219 goto done;
222 /* Keep in sync with struct gotd_imsg_ref definition. */
223 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
224 err = got_error_from_errno("imsg_add REF");
225 goto done;
227 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
228 err = got_error_from_errno("imsg_add REF");
229 goto done;
231 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
232 err = got_error_from_errno("imsg_add REF");
233 goto done;
236 wbuf->fd = -1;
237 imsg_close(ibuf, wbuf);
238 done:
239 got_object_tag_close(tag);
240 return err;
243 static const struct got_error *
244 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
246 const struct got_error *err;
247 const char *refname = got_ref_get_name(ref);
248 size_t namelen;
249 struct got_object_id *id = NULL;
250 struct got_object *obj = NULL;
251 size_t len;
252 struct ibuf *wbuf;
254 namelen = strlen(refname);
256 len = sizeof(struct gotd_imsg_ref) + namelen;
257 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
258 return got_error(GOT_ERR_NO_SPACE);
260 err = got_ref_resolve(&id, repo_read.repo, ref);
261 if (err)
262 return err;
264 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
265 repo_read.pid, len);
266 if (wbuf == NULL) {
267 err = got_error_from_errno("imsg_create REF");
268 goto done;
271 /* Keep in sync with struct gotd_imsg_ref definition. */
272 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
273 return got_error_from_errno("imsg_add REF");
274 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
275 return got_error_from_errno("imsg_add REF");
276 if (imsg_add(wbuf, refname, namelen) == -1)
277 return got_error_from_errno("imsg_add REF");
279 wbuf->fd = -1;
280 imsg_close(ibuf, wbuf);
282 err = got_object_open(&obj, repo_read.repo, id);
283 if (err)
284 goto done;
285 if (obj->type == GOT_OBJ_TYPE_TAG)
286 err = send_peeled_tag_ref(ref, obj, ibuf);
287 done:
288 if (obj)
289 got_object_close(obj);
290 free(id);
291 return err;
294 static const struct got_error *
295 list_refs(struct repo_read_client **client, struct imsg *imsg)
297 const struct got_error *err;
298 struct got_reflist_head refs;
299 struct got_reflist_entry *re;
300 struct gotd_imsg_list_refs_internal ireq;
301 size_t datalen;
302 struct gotd_imsg_reflist irefs;
303 struct imsgbuf ibuf;
304 int client_fd = imsg->fd;
305 struct got_object_id *head_target_id = NULL;
307 TAILQ_INIT(&refs);
309 if (client_fd == -1)
310 return got_error(GOT_ERR_PRIVSEP_NO_FD);
312 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
313 if (datalen != sizeof(ireq))
314 return got_error(GOT_ERR_PRIVSEP_LEN);
315 memcpy(&ireq, imsg->data, sizeof(ireq));
317 *client = find_client(ireq.client_id);
318 if (*client)
319 return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
321 *client = calloc(1, sizeof(**client));
322 if (*client == NULL)
323 return got_error_from_errno("calloc");
324 add_client(*client, ireq.client_id, client_fd);
326 imsg_init(&ibuf, client_fd);
328 err = got_ref_list(&refs, repo_read.repo, "",
329 got_ref_cmp_by_name, NULL);
330 if (err)
331 return err;
333 memset(&irefs, 0, sizeof(irefs));
334 TAILQ_FOREACH(re, &refs, entry) {
335 struct got_object_id *id;
336 int obj_type;
338 if (got_ref_is_symbolic(re->ref)) {
339 const char *refname = got_ref_get_name(re->ref);
340 if (strcmp(refname, GOT_REF_HEAD) != 0)
341 continue;
342 err = got_ref_resolve(&head_target_id, repo_read.repo,
343 re->ref);
344 if (err) {
345 if (err->code != GOT_ERR_NOT_REF)
346 return err;
347 /*
348 * HEAD points to a non-existent branch.
349 * Do not advertise it.
350 * Matches git-daemon's behaviour.
351 */
352 head_target_id = NULL;
353 err = NULL;
354 } else
355 irefs.nrefs++;
356 continue;
359 irefs.nrefs++;
361 /* Account for a peeled tag refs. */
362 err = got_ref_resolve(&id, repo_read.repo, re->ref);
363 if (err)
364 goto done;
365 err = got_object_get_type(&obj_type, repo_read.repo, id);
366 free(id);
367 if (err)
368 goto done;
369 if (obj_type == GOT_OBJ_TYPE_TAG)
370 irefs.nrefs++;
373 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
374 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
375 err = got_error_from_errno("imsg_compose REFLIST");
376 goto done;
379 /*
380 * Send the HEAD symref first. In Git-protocol versions < 2
381 * the HEAD symref must be announced on the initial line of
382 * the server's ref advertisement.
383 * For now, we do not advertise symrefs other than HEAD.
384 */
385 TAILQ_FOREACH(re, &refs, entry) {
386 if (!got_ref_is_symbolic(re->ref) ||
387 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
388 head_target_id == NULL)
389 continue;
390 err = send_symref(re->ref, head_target_id, &ibuf);
391 if (err)
392 goto done;
393 break;
395 TAILQ_FOREACH(re, &refs, entry) {
396 if (got_ref_is_symbolic(re->ref))
397 continue;
398 err = send_ref(re->ref, &ibuf);
399 if (err)
400 goto done;
403 err = gotd_imsg_flush(&ibuf);
404 done:
405 got_ref_list_free(&refs);
406 imsg_clear(&ibuf);
407 return err;
410 static const struct got_error *
411 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
413 const size_t alloc_chunksz = 256;
415 if (array->ids == NULL) {
416 array->ids = reallocarray(NULL, alloc_chunksz,
417 sizeof(*array->ids));
418 if (array->ids == NULL)
419 return got_error_from_errno("reallocarray");
420 array->nalloc = alloc_chunksz;
421 array->nids = 0;
422 } else if (array->nalloc <= array->nids) {
423 struct got_object_id **new;
424 new = recallocarray(array->ids, array->nalloc,
425 array->nalloc + alloc_chunksz, sizeof(*new));
426 if (new == NULL)
427 return got_error_from_errno("recallocarray");
428 array->ids = new;
429 array->nalloc += alloc_chunksz;
432 array->ids[array->nids] = got_object_id_dup(id);
433 if (array->ids[array->nids] == NULL)
434 return got_error_from_errno("got_object_id_dup");
435 array->nids++;
436 return NULL;
439 static void
440 free_object_ids(struct gotd_object_id_array *array)
442 size_t i;
444 for (i = 0; i < array->nids; i++)
445 free(array->ids[i]);
446 free(array->ids);
448 array->ids = NULL;
449 array->nalloc = 0;
450 array->nids = 0;
453 static const struct got_error *
454 recv_want(struct repo_read_client **client, struct imsg *imsg)
456 const struct got_error *err;
457 struct gotd_imsg_want iwant;
458 size_t datalen;
459 char hex[SHA1_DIGEST_STRING_LENGTH];
460 struct got_object_id id;
461 int obj_type;
462 struct imsgbuf ibuf;
464 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
465 if (datalen != sizeof(iwant))
466 return got_error(GOT_ERR_PRIVSEP_LEN);
467 memcpy(&iwant, imsg->data, sizeof(iwant));
469 memset(&id, 0, sizeof(id));
470 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
472 if (log_getverbose() > 0 &&
473 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
474 log_debug("client wants %s", hex);
476 *client = find_client(iwant.client_id);
477 if (*client == NULL)
478 return got_error(GOT_ERR_CLIENT_ID);
480 imsg_init(&ibuf, (*client)->fd);
482 err = got_object_get_type(&obj_type, repo_read.repo, &id);
483 if (err)
484 return err;
486 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
487 obj_type != GOT_OBJ_TYPE_TAG)
488 return got_error(GOT_ERR_OBJ_TYPE);
490 err = record_object_id(&(*client)->want_ids, &id);
491 if (err)
492 return err;
494 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
495 imsg_clear(&ibuf);
496 return err;
499 static const struct got_error *
500 recv_have(struct repo_read_client **client, struct imsg *imsg)
502 const struct got_error *err;
503 struct gotd_imsg_have ihave;
504 size_t datalen;
505 char hex[SHA1_DIGEST_STRING_LENGTH];
506 struct got_object_id id;
507 int obj_type;
508 struct imsgbuf ibuf;
510 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
511 if (datalen != sizeof(ihave))
512 return got_error(GOT_ERR_PRIVSEP_LEN);
513 memcpy(&ihave, imsg->data, sizeof(ihave));
515 memset(&id, 0, sizeof(id));
516 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
518 if (log_getverbose() > 0 &&
519 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
520 log_debug("client has %s", hex);
522 *client = find_client(ihave.client_id);
523 if (*client == NULL)
524 return got_error(GOT_ERR_CLIENT_ID);
526 imsg_init(&ibuf, (*client)->fd);
528 err = got_object_get_type(&obj_type, repo_read.repo, &id);
529 if (err) {
530 if (err->code == GOT_ERR_NO_OBJ) {
531 gotd_imsg_send_nak(&id, &ibuf,
532 PROC_REPO_READ, repo_read.pid);
533 err = NULL;
535 goto done;
538 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
539 obj_type != GOT_OBJ_TYPE_TAG) {
540 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
541 err = got_error(GOT_ERR_OBJ_TYPE);
542 goto done;
545 err = record_object_id(&(*client)->have_ids, &id);
546 if (err)
547 return err;
549 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
550 done:
551 imsg_clear(&ibuf);
552 return err;
555 struct repo_read_pack_progress_arg {
556 int report_progress;
557 struct imsgbuf *ibuf;
558 int sent_ready;
559 };
561 static const struct got_error *
562 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
563 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
564 int nobj_written)
566 struct repo_read_pack_progress_arg *a = arg;
567 struct gotd_imsg_packfile_progress iprog;
568 int ret;
570 if (!a->report_progress)
571 return NULL;
572 if (packfile_size > 0 && a->sent_ready)
573 return NULL;
575 memset(&iprog, 0, sizeof(iprog));
576 iprog.ncolored = ncolored;
577 iprog.nfound = nfound;
578 iprog.ntrees = ntrees;
579 iprog.packfile_size = packfile_size;
580 iprog.ncommits = ncommits;
581 iprog.nobj_total = nobj_total;
582 iprog.nobj_deltify = nobj_deltify;
583 iprog.nobj_written = nobj_written;
585 /* Using synchronous writes since we are blocking the event loop. */
586 if (packfile_size == 0) {
587 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
588 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
589 if (ret == -1) {
590 return got_error_from_errno("imsg compose "
591 "PACKFILE_PROGRESS");
593 } else {
594 a->sent_ready = 1;
595 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
596 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
597 if (ret == -1) {
598 return got_error_from_errno("imsg compose "
599 "PACKFILE_READY");
603 return gotd_imsg_flush(a->ibuf);
606 static const struct got_error *
607 receive_delta_cache_fd(struct repo_read_client **client, struct imsg *imsg,
608 struct gotd_imsgev *iev)
610 struct gotd_imsg_send_packfile ireq;
611 size_t datalen;
613 log_debug("receving delta cache file");
615 if (imsg->fd == -1)
616 return got_error(GOT_ERR_PRIVSEP_NO_FD);
618 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
619 if (datalen != sizeof(ireq))
620 return got_error(GOT_ERR_PRIVSEP_LEN);
621 memcpy(&ireq, imsg->data, sizeof(ireq));
623 *client = find_client(ireq.client_id);
624 if (*client == NULL)
625 return got_error(GOT_ERR_CLIENT_ID);
627 if ((*client)->delta_cache_fd != -1)
628 return got_error(GOT_ERR_PRIVSEP_MSG);
630 (*client)->delta_cache_fd = imsg->fd;
631 (*client)->report_progress = ireq.report_progress;
632 return NULL;
635 static const struct got_error *
636 receive_pack_pipe(struct repo_read_client **client, struct imsg *imsg,
637 struct gotd_imsgev *iev)
639 struct gotd_imsg_packfile_pipe ireq;
640 size_t datalen;
642 log_debug("receving pack pipe descriptor");
644 if (imsg->fd == -1)
645 return got_error(GOT_ERR_PRIVSEP_NO_FD);
647 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
648 if (datalen != sizeof(ireq))
649 return got_error(GOT_ERR_PRIVSEP_LEN);
650 memcpy(&ireq, imsg->data, sizeof(ireq));
652 *client = find_client(ireq.client_id);
653 if (*client == NULL)
654 return got_error(GOT_ERR_CLIENT_ID);
655 if ((*client)->pack_pipe != -1)
656 return got_error(GOT_ERR_PRIVSEP_MSG);
658 (*client)->pack_pipe = imsg->fd;
659 return NULL;
662 static const struct got_error *
663 send_packfile(struct repo_read_client *client, struct imsg *imsg,
664 struct gotd_imsgev *iev)
666 const struct got_error *err = NULL;
667 struct gotd_imsg_packfile_done idone;
668 uint8_t packsha1[SHA1_DIGEST_LENGTH];
669 char hex[SHA1_DIGEST_STRING_LENGTH];
670 FILE *delta_cache = NULL;
671 struct imsgbuf ibuf;
672 struct repo_read_pack_progress_arg pa;
673 struct got_ratelimit rl;
675 log_debug("packfile request received");
677 got_ratelimit_init(&rl, 2, 0);
679 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
680 return got_error(GOT_ERR_PRIVSEP_NO_FD);
682 imsg_init(&ibuf, client->fd);
684 delta_cache = fdopen(client->delta_cache_fd, "w+");
685 if (delta_cache == NULL) {
686 err = got_error_from_errno("fdopen");
687 goto done;
689 client->delta_cache_fd = -1;
691 memset(&pa, 0, sizeof(pa));
692 pa.ibuf = &ibuf;
693 pa.report_progress = client->report_progress;
695 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
696 client->have_ids.ids, client->have_ids.nids,
697 client->want_ids.ids, client->want_ids.nids,
698 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
699 check_cancelled, NULL);
700 if (err)
701 goto done;
703 if (log_getverbose() > 0 &&
704 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
705 log_debug("sent pack-%s.pack", hex);
707 memset(&idone, 0, sizeof(idone));
708 idone.client_id = client->id;
709 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
710 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
711 err = got_error_from_errno("imsg compose PACKFILE_DONE");
712 done:
713 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
714 err = got_error_from_errno("fclose");
715 imsg_clear(&ibuf);
716 return err;
719 static const struct got_error *
720 recv_disconnect(struct imsg *imsg)
722 const struct got_error *err = NULL;
723 struct gotd_imsg_disconnect idisconnect;
724 size_t datalen;
725 int client_fd, delta_cache_fd, pack_pipe;
726 struct repo_read_client *client = NULL;
727 uint64_t slot;
729 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
730 if (datalen != sizeof(idisconnect))
731 return got_error(GOT_ERR_PRIVSEP_LEN);
732 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
734 log_debug("client disconnecting");
736 client = find_client(idisconnect.client_id);
737 if (client == NULL)
738 return got_error(GOT_ERR_CLIENT_ID);
740 slot = client_hash(client->id) % nitems(repo_read_clients);
741 STAILQ_REMOVE(&repo_read_clients[slot], client, repo_read_client,
742 entry);
743 free_object_ids(&client->have_ids);
744 free_object_ids(&client->want_ids);
745 client_fd = client->fd;
746 delta_cache_fd = client->delta_cache_fd;
747 pack_pipe = client->pack_pipe;
748 free(client);
749 if (close(client_fd) == -1)
750 err = got_error_from_errno("close");
751 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
752 return got_error_from_errno("close");
753 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
754 return got_error_from_errno("close");
755 return err;
758 static void
759 repo_read_dispatch(int fd, short event, void *arg)
761 const struct got_error *err = NULL;
762 struct gotd_imsgev *iev = arg;
763 struct imsgbuf *ibuf = &iev->ibuf;
764 struct imsg imsg;
765 ssize_t n;
766 int shut = 0;
767 struct repo_read_client *client = NULL;
769 if (event & EV_READ) {
770 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
771 fatal("imsg_read error");
772 if (n == 0) /* Connection closed. */
773 shut = 1;
776 if (event & EV_WRITE) {
777 n = msgbuf_write(&ibuf->w);
778 if (n == -1 && errno != EAGAIN)
779 fatal("msgbuf_write");
780 if (n == 0) /* Connection closed. */
781 shut = 1;
784 while (err == NULL && check_cancelled(NULL) == NULL) {
785 client = NULL;
786 if ((n = imsg_get(ibuf, &imsg)) == -1)
787 fatal("%s: imsg_get", __func__);
788 if (n == 0) /* No more messages. */
789 break;
791 switch (imsg.hdr.type) {
792 case GOTD_IMSG_LIST_REFS_INTERNAL:
793 err = list_refs(&client, &imsg);
794 if (err)
795 log_warnx("%s: ls-refs: %s", repo_read.title,
796 err->msg);
797 break;
798 case GOTD_IMSG_WANT:
799 err = recv_want(&client, &imsg);
800 if (err)
801 log_warnx("%s: want-line: %s", repo_read.title,
802 err->msg);
803 break;
804 case GOTD_IMSG_HAVE:
805 err = recv_have(&client, &imsg);
806 if (err)
807 log_warnx("%s: have-line: %s", repo_read.title,
808 err->msg);
809 break;
810 case GOTD_IMSG_SEND_PACKFILE:
811 err = receive_delta_cache_fd(&client, &imsg, iev);
812 if (err)
813 log_warnx("%s: receiving delta cache: %s",
814 repo_read.title, err->msg);
815 break;
816 case GOTD_IMSG_PACKFILE_PIPE:
817 err = receive_pack_pipe(&client, &imsg, iev);
818 if (err) {
819 log_warnx("%s: receiving pack pipe: %s",
820 repo_read.title, err->msg);
821 break;
823 if (client->pack_pipe == -1)
824 break;
825 err = send_packfile(client, &imsg, iev);
826 if (err)
827 log_warnx("%s: sending packfile: %s",
828 repo_read.title, err->msg);
829 break;
830 case GOTD_IMSG_DISCONNECT:
831 err = recv_disconnect(&imsg);
832 if (err)
833 log_warnx("%s: disconnect: %s",
834 repo_read.title, err->msg);
835 break;
836 default:
837 log_debug("%s: unexpected imsg %d", repo_read.title,
838 imsg.hdr.type);
839 break;
842 imsg_free(&imsg);
845 if (!shut && check_cancelled(NULL) == NULL) {
846 if (err &&
847 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
848 client ? client->id : 0, err) == -1) {
849 log_warnx("could not send error to parent: %s",
850 err->msg);
852 gotd_imsg_event_add(iev);
853 } else {
854 /* This pipe is dead. Remove its event handler */
855 event_del(&iev->ev);
856 event_loopexit(NULL);
860 void
861 repo_read_main(const char *title, const char *repo_path,
862 int *pack_fds, int *temp_fds)
864 const struct got_error *err = NULL;
865 struct gotd_imsgev iev;
867 repo_read.title = title;
868 repo_read.pid = getpid();
869 repo_read.pack_fds = pack_fds;
870 repo_read.temp_fds = temp_fds;
872 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
874 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
875 if (err)
876 goto done;
877 if (!got_repo_is_bare(repo_read.repo)) {
878 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
879 "bare git repository required");
880 goto done;
883 got_repo_temp_fds_set(repo_read.repo, temp_fds);
885 signal(SIGINT, catch_sigint);
886 signal(SIGTERM, catch_sigterm);
887 signal(SIGPIPE, SIG_IGN);
888 signal(SIGHUP, SIG_IGN);
890 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
891 iev.handler = repo_read_dispatch;
892 iev.events = EV_READ;
893 iev.handler_arg = NULL;
894 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
895 if (event_add(&iev.ev, NULL) == -1) {
896 err = got_error_from_errno("event_add");
897 goto done;
900 event_dispatch();
901 done:
902 if (err)
903 log_warnx("%s: %s", title, err->msg);
904 repo_read_shutdown();
907 void
908 repo_read_shutdown(void)
910 log_debug("%s: shutting down", repo_read.title);
911 if (repo_read.repo)
912 got_repo_close(repo_read.repo);
913 got_repo_pack_fds_close(repo_read.pack_fds);
914 got_repo_temp_fds_close(repo_read.temp_fds);
915 exit(0);