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 imsgbuf *ibuf)
141 const struct got_error *err = NULL;
142 struct gotd_imsg_symref isymref;
143 const char *refname = got_ref_get_name(symref);
144 const char *target = got_ref_get_symref_target(symref);
145 size_t len;
146 struct ibuf *wbuf;
147 struct got_object_id *target_id;
149 err = got_ref_resolve(&target_id, repo_read.repo, symref);
150 if (err)
151 return err;
153 memset(&isymref, 0, sizeof(isymref));
154 isymref.name_len = strlen(refname);
155 isymref.target_len = strlen(target);
156 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
158 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
159 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
160 err = got_error(GOT_ERR_NO_SPACE);
161 goto done;
164 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
165 if (wbuf == NULL) {
166 err = got_error_from_errno("imsg_create SYMREF");
167 goto done;
170 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
171 err = got_error_from_errno("imsg_add SYMREF");
172 goto done;
174 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
175 err = got_error_from_errno("imsg_add SYMREF");
176 goto done;
178 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
179 err = got_error_from_errno("imsg_add SYMREF");
180 goto done;
183 wbuf->fd = -1;
184 imsg_close(ibuf, wbuf);
185 done:
186 free(target_id);
187 return err;
190 static const struct got_error *
191 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
192 struct imsgbuf *ibuf)
194 const struct got_error *err = NULL;
195 struct got_tag_object *tag;
196 size_t namelen, len;
197 char *peeled_refname = NULL;
198 struct got_object_id *id;
199 struct ibuf *wbuf;
201 err = got_object_tag_open(&tag, repo_read.repo, obj);
202 if (err)
203 return err;
205 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
206 err = got_error_from_errno("asprintf");
207 goto done;
210 id = got_object_tag_get_object_id(tag);
211 namelen = strlen(peeled_refname);
213 len = sizeof(struct gotd_imsg_ref) + namelen;
214 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
215 err = got_error(GOT_ERR_NO_SPACE);
216 goto done;
219 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
220 repo_read.pid, len);
221 if (wbuf == NULL) {
222 err = got_error_from_errno("imsg_create MREF");
223 goto done;
226 /* Keep in sync with struct gotd_imsg_ref definition. */
227 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
228 err = got_error_from_errno("imsg_add REF");
229 goto done;
231 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
232 err = got_error_from_errno("imsg_add REF");
233 goto done;
235 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
236 err = got_error_from_errno("imsg_add REF");
237 goto done;
240 wbuf->fd = -1;
241 imsg_close(ibuf, wbuf);
242 done:
243 got_object_tag_close(tag);
244 return err;
247 static const struct got_error *
248 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
250 const struct got_error *err;
251 const char *refname = got_ref_get_name(ref);
252 size_t namelen;
253 struct got_object_id *id = NULL;
254 struct got_object *obj = NULL;
255 size_t len;
256 struct ibuf *wbuf;
258 namelen = strlen(refname);
260 len = sizeof(struct gotd_imsg_ref) + namelen;
261 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
262 return got_error(GOT_ERR_NO_SPACE);
264 err = got_ref_resolve(&id, repo_read.repo, ref);
265 if (err)
266 return err;
268 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
269 repo_read.pid, len);
270 if (wbuf == NULL) {
271 err = got_error_from_errno("imsg_create REF");
272 goto done;
275 /* Keep in sync with struct gotd_imsg_ref definition. */
276 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
277 return got_error_from_errno("imsg_add REF");
278 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
279 return got_error_from_errno("imsg_add REF");
280 if (imsg_add(wbuf, refname, namelen) == -1)
281 return got_error_from_errno("imsg_add REF");
283 wbuf->fd = -1;
284 imsg_close(ibuf, wbuf);
286 err = got_object_open(&obj, repo_read.repo, id);
287 if (err)
288 goto done;
289 if (obj->type == GOT_OBJ_TYPE_TAG)
290 err = send_peeled_tag_ref(ref, obj, ibuf);
291 done:
292 if (obj)
293 got_object_close(obj);
294 free(id);
295 return err;
298 static const struct got_error *
299 list_refs(struct repo_read_client **client, struct imsg *imsg)
301 const struct got_error *err;
302 struct got_reflist_head refs;
303 struct got_reflist_entry *re;
304 struct gotd_imsg_list_refs_internal ireq;
305 size_t datalen;
306 struct gotd_imsg_reflist irefs;
307 struct imsgbuf ibuf;
308 int client_fd = imsg->fd;
310 TAILQ_INIT(&refs);
312 if (client_fd == -1)
313 return got_error(GOT_ERR_PRIVSEP_NO_FD);
315 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
316 if (datalen != sizeof(ireq))
317 return got_error(GOT_ERR_PRIVSEP_LEN);
318 memcpy(&ireq, imsg->data, sizeof(ireq));
320 *client = find_client(ireq.client_id);
321 if (*client)
322 return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
324 *client = calloc(1, sizeof(**client));
325 if (*client == NULL)
326 return got_error_from_errno("calloc");
327 add_client(*client, ireq.client_id, client_fd);
329 imsg_init(&ibuf, client_fd);
331 err = got_ref_list(&refs, repo_read.repo, "",
332 got_ref_cmp_by_name, NULL);
333 if (err)
334 return err;
336 memset(&irefs, 0, sizeof(irefs));
337 TAILQ_FOREACH(re, &refs, entry) {
338 struct got_object_id *id;
339 int obj_type;
341 if (got_ref_is_symbolic(re->ref)) {
342 const char *refname = got_ref_get_name(re->ref);
343 if (strcmp(refname, GOT_REF_HEAD) == 0)
344 irefs.nrefs++;
345 continue;
348 irefs.nrefs++;
350 /* Account for a peeled tag refs. */
351 err = got_ref_resolve(&id, repo_read.repo, re->ref);
352 if (err)
353 goto done;
354 err = got_object_get_type(&obj_type, repo_read.repo, id);
355 free(id);
356 if (err)
357 goto done;
358 if (obj_type == GOT_OBJ_TYPE_TAG)
359 irefs.nrefs++;
362 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
363 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
364 err = got_error_from_errno("imsg_compose REFLIST");
365 goto done;
368 /*
369 * Send the HEAD symref first. In Git-protocol versions < 2
370 * the HEAD symref must be announced on the initial line of
371 * the server's ref advertisement.
372 * For now, we do not advertise symrefs other than HEAD.
373 */
374 TAILQ_FOREACH(re, &refs, entry) {
375 if (!got_ref_is_symbolic(re->ref) ||
376 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0)
377 continue;
378 err = send_symref(re->ref, &ibuf);
379 if (err)
380 goto done;
381 break;
383 TAILQ_FOREACH(re, &refs, entry) {
384 if (got_ref_is_symbolic(re->ref))
385 continue;
386 err = send_ref(re->ref, &ibuf);
387 if (err)
388 goto done;
391 err = gotd_imsg_flush(&ibuf);
392 done:
393 got_ref_list_free(&refs);
394 imsg_clear(&ibuf);
395 return err;
398 static const struct got_error *
399 record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
401 const size_t alloc_chunksz = 256;
403 if (array->ids == NULL) {
404 array->ids = reallocarray(NULL, alloc_chunksz,
405 sizeof(*array->ids));
406 if (array->ids == NULL)
407 return got_error_from_errno("reallocarray");
408 array->nalloc = alloc_chunksz;
409 array->nids = 0;
410 } else if (array->nalloc <= array->nids) {
411 struct got_object_id **new;
412 new = recallocarray(array->ids, array->nalloc,
413 array->nalloc + alloc_chunksz, sizeof(*new));
414 if (new == NULL)
415 return got_error_from_errno("recallocarray");
416 array->ids = new;
417 array->nalloc += alloc_chunksz;
420 array->ids[array->nids] = got_object_id_dup(id);
421 if (array->ids[array->nids] == NULL)
422 return got_error_from_errno("got_object_id_dup");
423 array->nids++;
424 return NULL;
427 static void
428 free_object_ids(struct gotd_object_id_array *array)
430 size_t i;
432 for (i = 0; i < array->nids; i++)
433 free(array->ids[i]);
434 free(array->ids);
436 array->ids = NULL;
437 array->nalloc = 0;
438 array->nids = 0;
441 static const struct got_error *
442 recv_want(struct repo_read_client **client, struct imsg *imsg)
444 const struct got_error *err;
445 struct gotd_imsg_want iwant;
446 size_t datalen;
447 char hex[SHA1_DIGEST_STRING_LENGTH];
448 struct got_object_id id;
449 int obj_type;
450 struct imsgbuf ibuf;
452 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
453 if (datalen != sizeof(iwant))
454 return got_error(GOT_ERR_PRIVSEP_LEN);
455 memcpy(&iwant, imsg->data, sizeof(iwant));
457 memset(&id, 0, sizeof(id));
458 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
460 if (log_getverbose() > 0 &&
461 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
462 log_debug("client wants %s", hex);
464 *client = find_client(iwant.client_id);
465 if (*client == NULL)
466 return got_error(GOT_ERR_CLIENT_ID);
468 imsg_init(&ibuf, (*client)->fd);
470 err = got_object_get_type(&obj_type, repo_read.repo, &id);
471 if (err)
472 return err;
474 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
475 obj_type != GOT_OBJ_TYPE_TAG)
476 return got_error(GOT_ERR_OBJ_TYPE);
478 err = record_object_id(&(*client)->want_ids, &id);
479 if (err)
480 return err;
482 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
483 imsg_clear(&ibuf);
484 return err;
487 static const struct got_error *
488 recv_have(struct repo_read_client **client, struct imsg *imsg)
490 const struct got_error *err;
491 struct gotd_imsg_have ihave;
492 size_t datalen;
493 char hex[SHA1_DIGEST_STRING_LENGTH];
494 struct got_object_id id;
495 int obj_type;
496 struct imsgbuf ibuf;
498 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
499 if (datalen != sizeof(ihave))
500 return got_error(GOT_ERR_PRIVSEP_LEN);
501 memcpy(&ihave, imsg->data, sizeof(ihave));
503 memset(&id, 0, sizeof(id));
504 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
506 if (log_getverbose() > 0 &&
507 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
508 log_debug("client has %s", hex);
510 *client = find_client(ihave.client_id);
511 if (*client == NULL)
512 return got_error(GOT_ERR_CLIENT_ID);
514 imsg_init(&ibuf, (*client)->fd);
516 err = got_object_get_type(&obj_type, repo_read.repo, &id);
517 if (err) {
518 if (err->code == GOT_ERR_NO_OBJ) {
519 gotd_imsg_send_nak(&id, &ibuf,
520 PROC_REPO_READ, repo_read.pid);
521 err = NULL;
523 goto done;
526 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
527 obj_type != GOT_OBJ_TYPE_TAG) {
528 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
529 err = got_error(GOT_ERR_OBJ_TYPE);
530 goto done;
533 err = record_object_id(&(*client)->have_ids, &id);
534 if (err)
535 return err;
537 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
538 done:
539 imsg_clear(&ibuf);
540 return err;
543 struct repo_read_pack_progress_arg {
544 int report_progress;
545 struct imsgbuf *ibuf;
546 int sent_ready;
547 };
549 static const struct got_error *
550 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
551 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
552 int nobj_written)
554 struct repo_read_pack_progress_arg *a = arg;
555 struct gotd_imsg_packfile_progress iprog;
556 int ret;
558 if (!a->report_progress)
559 return NULL;
560 if (packfile_size > 0 && a->sent_ready)
561 return NULL;
563 memset(&iprog, 0, sizeof(iprog));
564 iprog.ncolored = ncolored;
565 iprog.nfound = nfound;
566 iprog.ntrees = ntrees;
567 iprog.packfile_size = packfile_size;
568 iprog.ncommits = ncommits;
569 iprog.nobj_total = nobj_total;
570 iprog.nobj_deltify = nobj_deltify;
571 iprog.nobj_written = nobj_written;
573 /* Using synchronous writes since we are blocking the event loop. */
574 if (packfile_size == 0) {
575 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
576 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
577 if (ret == -1) {
578 return got_error_from_errno("imsg compose "
579 "PACKFILE_PROGRESS");
581 } else {
582 a->sent_ready = 1;
583 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
584 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
585 if (ret == -1) {
586 return got_error_from_errno("imsg compose "
587 "PACKFILE_READY");
591 return gotd_imsg_flush(a->ibuf);
594 static const struct got_error *
595 receive_delta_cache_fd(struct repo_read_client **client, struct imsg *imsg,
596 struct gotd_imsgev *iev)
598 struct gotd_imsg_send_packfile ireq;
599 size_t datalen;
601 log_debug("receving delta cache file");
603 if (imsg->fd == -1)
604 return got_error(GOT_ERR_PRIVSEP_NO_FD);
606 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
607 if (datalen != sizeof(ireq))
608 return got_error(GOT_ERR_PRIVSEP_LEN);
609 memcpy(&ireq, imsg->data, sizeof(ireq));
611 *client = find_client(ireq.client_id);
612 if (*client == NULL)
613 return got_error(GOT_ERR_CLIENT_ID);
615 if ((*client)->delta_cache_fd != -1)
616 return got_error(GOT_ERR_PRIVSEP_MSG);
618 (*client)->delta_cache_fd = imsg->fd;
619 (*client)->report_progress = ireq.report_progress;
620 return NULL;
623 static const struct got_error *
624 receive_pack_pipe(struct repo_read_client **client, struct imsg *imsg,
625 struct gotd_imsgev *iev)
627 struct gotd_imsg_packfile_pipe ireq;
628 size_t datalen;
630 log_debug("receving pack pipe descriptor");
632 if (imsg->fd == -1)
633 return got_error(GOT_ERR_PRIVSEP_NO_FD);
635 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
636 if (datalen != sizeof(ireq))
637 return got_error(GOT_ERR_PRIVSEP_LEN);
638 memcpy(&ireq, imsg->data, sizeof(ireq));
640 *client = find_client(ireq.client_id);
641 if (*client == NULL)
642 return got_error(GOT_ERR_CLIENT_ID);
643 if ((*client)->pack_pipe != -1)
644 return got_error(GOT_ERR_PRIVSEP_MSG);
646 (*client)->pack_pipe = imsg->fd;
647 return NULL;
650 static const struct got_error *
651 send_packfile(struct repo_read_client *client, struct imsg *imsg,
652 struct gotd_imsgev *iev)
654 const struct got_error *err = NULL;
655 struct gotd_imsg_packfile_done idone;
656 uint8_t packsha1[SHA1_DIGEST_LENGTH];
657 char hex[SHA1_DIGEST_STRING_LENGTH];
658 FILE *delta_cache = NULL;
659 struct imsgbuf ibuf;
660 struct repo_read_pack_progress_arg pa;
661 struct got_ratelimit rl;
663 log_debug("packfile request received");
665 got_ratelimit_init(&rl, 2, 0);
667 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
668 return got_error(GOT_ERR_PRIVSEP_NO_FD);
670 imsg_init(&ibuf, client->fd);
672 delta_cache = fdopen(client->delta_cache_fd, "w+");
673 if (delta_cache == NULL) {
674 err = got_error_from_errno("fdopen");
675 goto done;
677 client->delta_cache_fd = -1;
679 memset(&pa, 0, sizeof(pa));
680 pa.ibuf = &ibuf;
681 pa.report_progress = client->report_progress;
683 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
684 client->have_ids.ids, client->have_ids.nids,
685 client->want_ids.ids, client->want_ids.nids,
686 repo_read.repo, 0, 1, pack_progress, &pa, &rl,
687 check_cancelled, NULL);
688 if (err)
689 goto done;
691 if (log_getverbose() > 0 &&
692 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
693 log_debug("sent pack-%s.pack", hex);
695 memset(&idone, 0, sizeof(idone));
696 idone.client_id = client->id;
697 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
698 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
699 err = got_error_from_errno("imsg compose PACKFILE_DONE");
700 done:
701 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
702 err = got_error_from_errno("fclose");
703 imsg_clear(&ibuf);
704 return err;
707 static const struct got_error *
708 recv_disconnect(struct imsg *imsg)
710 const struct got_error *err = NULL;
711 struct gotd_imsg_disconnect idisconnect;
712 size_t datalen;
713 int client_fd, delta_cache_fd, pack_pipe;
714 struct repo_read_client *client = NULL;
715 uint64_t slot;
717 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
718 if (datalen != sizeof(idisconnect))
719 return got_error(GOT_ERR_PRIVSEP_LEN);
720 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
722 log_debug("client disconnecting");
724 client = find_client(idisconnect.client_id);
725 if (client == NULL)
726 return got_error(GOT_ERR_CLIENT_ID);
728 slot = client_hash(client->id) % nitems(repo_read_clients);
729 STAILQ_REMOVE(&repo_read_clients[slot], client, repo_read_client,
730 entry);
731 free_object_ids(&client->have_ids);
732 free_object_ids(&client->want_ids);
733 client_fd = client->fd;
734 delta_cache_fd = client->delta_cache_fd;
735 pack_pipe = client->pack_pipe;
736 free(client);
737 if (close(client_fd) == -1)
738 err = got_error_from_errno("close");
739 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
740 return got_error_from_errno("close");
741 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
742 return got_error_from_errno("close");
743 return err;
746 static void
747 repo_read_dispatch(int fd, short event, void *arg)
749 const struct got_error *err = NULL;
750 struct gotd_imsgev *iev = arg;
751 struct imsgbuf *ibuf = &iev->ibuf;
752 struct imsg imsg;
753 ssize_t n;
754 int shut = 0;
755 struct repo_read_client *client = NULL;
757 if (event & EV_READ) {
758 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
759 fatal("imsg_read error");
760 if (n == 0) /* Connection closed. */
761 shut = 1;
764 if (event & EV_WRITE) {
765 n = msgbuf_write(&ibuf->w);
766 if (n == -1 && errno != EAGAIN)
767 fatal("msgbuf_write");
768 if (n == 0) /* Connection closed. */
769 shut = 1;
772 while (err == NULL && check_cancelled(NULL) == NULL) {
773 client = NULL;
774 if ((n = imsg_get(ibuf, &imsg)) == -1)
775 fatal("%s: imsg_get", __func__);
776 if (n == 0) /* No more messages. */
777 break;
779 switch (imsg.hdr.type) {
780 case GOTD_IMSG_LIST_REFS_INTERNAL:
781 err = list_refs(&client, &imsg);
782 if (err)
783 log_warnx("%s: ls-refs: %s", repo_read.title,
784 err->msg);
785 break;
786 case GOTD_IMSG_WANT:
787 err = recv_want(&client, &imsg);
788 if (err)
789 log_warnx("%s: want-line: %s", repo_read.title,
790 err->msg);
791 break;
792 case GOTD_IMSG_HAVE:
793 err = recv_have(&client, &imsg);
794 if (err)
795 log_warnx("%s: have-line: %s", repo_read.title,
796 err->msg);
797 break;
798 case GOTD_IMSG_SEND_PACKFILE:
799 err = receive_delta_cache_fd(&client, &imsg, iev);
800 if (err)
801 log_warnx("%s: receiving delta cache: %s",
802 repo_read.title, err->msg);
803 break;
804 case GOTD_IMSG_PACKFILE_PIPE:
805 err = receive_pack_pipe(&client, &imsg, iev);
806 if (err) {
807 log_warnx("%s: receiving pack pipe: %s",
808 repo_read.title, err->msg);
809 break;
811 if (client->pack_pipe == -1)
812 break;
813 err = send_packfile(client, &imsg, iev);
814 if (err)
815 log_warnx("%s: sending packfile: %s",
816 repo_read.title, err->msg);
817 break;
818 case GOTD_IMSG_DISCONNECT:
819 err = recv_disconnect(&imsg);
820 if (err)
821 log_warnx("%s: disconnect: %s",
822 repo_read.title, err->msg);
823 break;
824 default:
825 log_debug("%s: unexpected imsg %d", repo_read.title,
826 imsg.hdr.type);
827 break;
830 imsg_free(&imsg);
833 if (!shut && check_cancelled(NULL) == NULL) {
834 if (err &&
835 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
836 client ? client->id : 0, err) == -1) {
837 log_warnx("could not send error to parent: %s",
838 err->msg);
840 gotd_imsg_event_add(iev);
841 } else {
842 /* This pipe is dead. Remove its event handler */
843 event_del(&iev->ev);
844 event_loopexit(NULL);
848 void
849 repo_read_main(const char *title, int *pack_fds, int *temp_fds)
851 const struct got_error *err = NULL;
852 struct gotd_imsgev iev;
854 repo_read.title = title;
855 repo_read.pid = getpid();
856 repo_read.pack_fds = pack_fds;
857 repo_read.temp_fds = temp_fds;
859 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
861 /*
862 * Open a repository in the root directory.
863 * We are already in chroot at this point.
864 */
865 err = got_repo_open(&repo_read.repo, "/", NULL, pack_fds);
866 if (err)
867 goto done;
868 if (!got_repo_is_bare(repo_read.repo)) {
869 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
870 "bare git repository required");
871 goto done;
874 got_repo_temp_fds_set(repo_read.repo, temp_fds);
876 signal(SIGINT, catch_sigint);
877 signal(SIGTERM, catch_sigterm);
878 signal(SIGPIPE, SIG_IGN);
879 signal(SIGHUP, SIG_IGN);
881 imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
882 iev.handler = repo_read_dispatch;
883 iev.events = EV_READ;
884 iev.handler_arg = NULL;
885 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
886 if (event_add(&iev.ev, NULL) == -1) {
887 err = got_error_from_errno("event_add");
888 goto done;
891 event_dispatch();
892 done:
893 if (err)
894 log_warnx("%s: %s", title, err->msg);
895 repo_read_shutdown();
898 void
899 repo_read_shutdown(void)
901 log_debug("%s: shutting down", repo_read.title);
902 if (repo_read.repo)
903 got_repo_close(repo_read.repo);
904 got_repo_pack_fds_close(repo_read.pack_fds);
905 got_repo_temp_fds_close(repo_read.temp_fds);
906 exit(0);