Blob


1 /*
2 * Copyright (c) 2022, 2023 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/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/uio.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <limits.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <imsg.h>
35 #include <unistd.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_opentemp.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_gitproto.h"
52 #include "gotd.h"
53 #include "log.h"
54 #include "session.h"
57 static struct gotd_session {
58 pid_t pid;
59 const char *title;
60 struct got_repository *repo;
61 int *pack_fds;
62 int *temp_fds;
63 struct gotd_imsgev parent_iev;
64 struct timeval request_timeout;
65 enum gotd_procid proc_id;
66 } gotd_session;
68 static struct gotd_session_client {
69 enum gotd_session_state state;
70 int is_writing;
71 struct gotd_client_capability *capabilities;
72 size_t ncapa_alloc;
73 size_t ncapabilities;
74 uint32_t id;
75 int fd;
76 int delta_cache_fd;
77 struct gotd_imsgev iev;
78 struct gotd_imsgev repo_child_iev;
79 struct event tmo;
80 uid_t euid;
81 gid_t egid;
82 char *packfile_path;
83 char *packidx_path;
84 int nref_updates;
85 int accept_flush_pkt;
86 int flush_disconnect;
87 } gotd_session_client;
89 void gotd_session_sighdlr(int sig, short event, void *arg);
90 static void gotd_session_shutdown(void);
92 static void
93 disconnect(struct gotd_session_client *client)
94 {
95 log_debug("uid %d: disconnecting", client->euid);
97 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
98 GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
99 log_warn("imsg compose DISCONNECT");
101 imsg_clear(&client->repo_child_iev.ibuf);
102 event_del(&client->repo_child_iev.ev);
103 evtimer_del(&client->tmo);
104 close(client->fd);
105 if (client->delta_cache_fd != -1)
106 close(client->delta_cache_fd);
107 if (client->packfile_path) {
108 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
109 log_warn("unlink %s: ", client->packfile_path);
110 free(client->packfile_path);
112 if (client->packidx_path) {
113 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
114 log_warn("unlink %s: ", client->packidx_path);
115 free(client->packidx_path);
117 free(client->capabilities);
119 gotd_session_shutdown();
122 static void
123 disconnect_on_error(struct gotd_session_client *client,
124 const struct got_error *err)
126 struct imsgbuf ibuf;
128 if (err->code != GOT_ERR_EOF) {
129 log_warnx("uid %d: %s", client->euid, err->msg);
130 imsg_init(&ibuf, client->fd);
131 gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
132 imsg_clear(&ibuf);
135 disconnect(client);
138 static void
139 gotd_request_timeout(int fd, short events, void *arg)
141 struct gotd_session_client *client = arg;
143 log_debug("disconnecting uid %d due to timeout", client->euid);
144 disconnect(client);
147 void
148 gotd_session_sighdlr(int sig, short event, void *arg)
150 /*
151 * Normal signal handler rules don't apply because libevent
152 * decouples for us.
153 */
155 switch (sig) {
156 case SIGHUP:
157 log_info("%s: ignoring SIGHUP", __func__);
158 break;
159 case SIGUSR1:
160 log_info("%s: ignoring SIGUSR1", __func__);
161 break;
162 case SIGTERM:
163 case SIGINT:
164 gotd_session_shutdown();
165 /* NOTREACHED */
166 break;
167 default:
168 fatalx("unexpected signal");
172 static const struct got_error *
173 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
175 struct gotd_imsg_packfile_done idone;
176 size_t datalen;
178 log_debug("packfile-done received");
180 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
181 if (datalen != sizeof(idone))
182 return got_error(GOT_ERR_PRIVSEP_LEN);
183 memcpy(&idone, imsg->data, sizeof(idone));
185 *client_id = idone.client_id;
186 return NULL;
189 static const struct got_error *
190 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
192 struct gotd_imsg_packfile_install inst;
193 size_t datalen;
195 log_debug("packfile-install received");
197 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
198 if (datalen != sizeof(inst))
199 return got_error(GOT_ERR_PRIVSEP_LEN);
200 memcpy(&inst, imsg->data, sizeof(inst));
202 *client_id = inst.client_id;
203 return NULL;
206 static const struct got_error *
207 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
209 struct gotd_imsg_ref_updates_start istart;
210 size_t datalen;
212 log_debug("ref-updates-start received");
214 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 if (datalen != sizeof(istart))
216 return got_error(GOT_ERR_PRIVSEP_LEN);
217 memcpy(&istart, imsg->data, sizeof(istart));
219 *client_id = istart.client_id;
220 return NULL;
223 static const struct got_error *
224 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
226 struct gotd_imsg_ref_update iref;
227 size_t datalen;
229 log_debug("ref-update received");
231 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 if (datalen < sizeof(iref))
233 return got_error(GOT_ERR_PRIVSEP_LEN);
234 memcpy(&iref, imsg->data, sizeof(iref));
236 *client_id = iref.client_id;
237 return NULL;
240 static const struct got_error *
241 send_ref_update_ok(struct gotd_session_client *client,
242 struct gotd_imsg_ref_update *iref, const char *refname)
244 struct gotd_imsg_ref_update_ok iok;
245 struct gotd_imsgev *iev = &client->iev;
246 struct ibuf *wbuf;
247 size_t len;
249 memset(&iok, 0, sizeof(iok));
250 iok.client_id = client->id;
251 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
252 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
253 iok.name_len = strlen(refname);
255 len = sizeof(iok) + iok.name_len;
256 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
257 gotd_session.proc_id, gotd_session.pid, len);
258 if (wbuf == NULL)
259 return got_error_from_errno("imsg_create REF_UPDATE_OK");
261 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
262 return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 if (imsg_add(wbuf, refname, iok.name_len) == -1)
264 return got_error_from_errno("imsg_add REF_UPDATE_OK");
266 wbuf->fd = -1;
267 imsg_close(&iev->ibuf, wbuf);
268 gotd_imsg_event_add(iev);
269 return NULL;
272 static void
273 send_refs_updated(struct gotd_session_client *client)
275 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
276 gotd_session.proc_id, -1, NULL, 0) == -1)
277 log_warn("imsg compose REFS_UPDATED");
280 static const struct got_error *
281 send_ref_update_ng(struct gotd_session_client *client,
282 struct gotd_imsg_ref_update *iref, const char *refname,
283 const char *reason)
285 const struct got_error *ng_err;
286 struct gotd_imsg_ref_update_ng ing;
287 struct gotd_imsgev *iev = &client->iev;
288 struct ibuf *wbuf;
289 size_t len;
291 memset(&ing, 0, sizeof(ing));
292 ing.client_id = client->id;
293 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
294 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
295 ing.name_len = strlen(refname);
297 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
298 ing.reason_len = strlen(ng_err->msg);
300 len = sizeof(ing) + ing.name_len + ing.reason_len;
301 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
302 gotd_session.proc_id, gotd_session.pid, len);
303 if (wbuf == NULL)
304 return got_error_from_errno("imsg_create REF_UPDATE_NG");
306 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
307 return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 if (imsg_add(wbuf, refname, ing.name_len) == -1)
309 return got_error_from_errno("imsg_add REF_UPDATE_NG");
310 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
311 return got_error_from_errno("imsg_add REF_UPDATE_NG");
313 wbuf->fd = -1;
314 imsg_close(&iev->ibuf, wbuf);
315 gotd_imsg_event_add(iev);
316 return NULL;
319 static const struct got_error *
320 install_pack(struct gotd_session_client *client, const char *repo_path,
321 struct imsg *imsg)
323 const struct got_error *err = NULL;
324 struct gotd_imsg_packfile_install inst;
325 char hex[SHA1_DIGEST_STRING_LENGTH];
326 size_t datalen;
327 char *packfile_path = NULL, *packidx_path = NULL;
329 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
330 if (datalen != sizeof(inst))
331 return got_error(GOT_ERR_PRIVSEP_LEN);
332 memcpy(&inst, imsg->data, sizeof(inst));
334 if (client->packfile_path == NULL)
335 return got_error_msg(GOT_ERR_BAD_REQUEST,
336 "client has no pack file");
337 if (client->packidx_path == NULL)
338 return got_error_msg(GOT_ERR_BAD_REQUEST,
339 "client has no pack file index");
341 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
342 return got_error_msg(GOT_ERR_NO_SPACE,
343 "could not convert pack file SHA1 to hex");
345 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
346 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 err = got_error_from_errno("asprintf");
348 goto done;
351 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
352 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
353 err = got_error_from_errno("asprintf");
354 goto done;
357 if (rename(client->packfile_path, packfile_path) == -1) {
358 err = got_error_from_errno3("rename", client->packfile_path,
359 packfile_path);
360 goto done;
363 free(client->packfile_path);
364 client->packfile_path = NULL;
366 if (rename(client->packidx_path, packidx_path) == -1) {
367 err = got_error_from_errno3("rename", client->packidx_path,
368 packidx_path);
369 goto done;
372 free(client->packidx_path);
373 client->packidx_path = NULL;
374 done:
375 free(packfile_path);
376 free(packidx_path);
377 return err;
380 static const struct got_error *
381 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
383 struct gotd_imsg_ref_updates_start istart;
384 size_t datalen;
386 if (client->nref_updates != -1)
387 return got_error(GOT_ERR_PRIVSEP_MSG);
389 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
390 if (datalen != sizeof(istart))
391 return got_error(GOT_ERR_PRIVSEP_LEN);
392 memcpy(&istart, imsg->data, sizeof(istart));
394 if (istart.nref_updates <= 0)
395 return got_error(GOT_ERR_PRIVSEP_MSG);
397 client->nref_updates = istart.nref_updates;
398 return NULL;
401 static const struct got_error *
402 update_ref(int *shut, struct gotd_session_client *client,
403 const char *repo_path, struct imsg *imsg)
405 const struct got_error *err = NULL;
406 struct got_repository *repo = NULL;
407 struct got_reference *ref = NULL;
408 struct gotd_imsg_ref_update iref;
409 struct got_object_id old_id, new_id;
410 struct got_object_id *id = NULL;
411 char *refname = NULL;
412 size_t datalen;
413 int locked = 0;
414 char hex1[SHA1_DIGEST_STRING_LENGTH];
415 char hex2[SHA1_DIGEST_STRING_LENGTH];
417 log_debug("update-ref from uid %d", client->euid);
419 if (client->nref_updates <= 0)
420 return got_error(GOT_ERR_PRIVSEP_MSG);
422 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
423 if (datalen < sizeof(iref))
424 return got_error(GOT_ERR_PRIVSEP_LEN);
425 memcpy(&iref, imsg->data, sizeof(iref));
426 if (datalen != sizeof(iref) + iref.name_len)
427 return got_error(GOT_ERR_PRIVSEP_LEN);
428 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
429 if (refname == NULL)
430 return got_error_from_errno("strndup");
432 log_debug("updating ref %s for uid %d", refname, client->euid);
434 err = got_repo_open(&repo, repo_path, NULL, NULL);
435 if (err)
436 goto done;
438 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
439 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
440 err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
441 repo);
442 if (err)
443 goto done;
445 if (iref.ref_is_new) {
446 err = got_ref_open(&ref, repo, refname, 0);
447 if (err) {
448 if (err->code != GOT_ERR_NOT_REF)
449 goto done;
450 err = got_ref_alloc(&ref, refname, &new_id);
451 if (err)
452 goto done;
453 err = got_ref_write(ref, repo); /* will lock/unlock */
454 if (err)
455 goto done;
456 } else {
457 err = got_ref_resolve(&id, repo, ref);
458 if (err)
459 goto done;
460 got_object_id_hex(&new_id, hex1, sizeof(hex1));
461 got_object_id_hex(id, hex2, sizeof(hex2));
462 err = got_error_fmt(GOT_ERR_REF_BUSY,
463 "Addition %s: %s failed; %s: %s has been "
464 "created by someone else while transaction "
465 "was in progress",
466 got_ref_get_name(ref), hex1,
467 got_ref_get_name(ref), hex2);
468 goto done;
470 } else if (iref.delete_ref) {
471 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
472 if (err)
473 goto done;
474 locked = 1;
476 err = got_ref_resolve(&id, repo, ref);
477 if (err)
478 goto done;
480 if (got_object_id_cmp(id, &old_id) != 0) {
481 got_object_id_hex(&old_id, hex1, sizeof(hex1));
482 got_object_id_hex(id, hex2, sizeof(hex2));
483 err = got_error_fmt(GOT_ERR_REF_BUSY,
484 "Deletion %s: %s failed; %s: %s has been "
485 "created by someone else while transaction "
486 "was in progress",
487 got_ref_get_name(ref), hex1,
488 got_ref_get_name(ref), hex2);
489 goto done;
492 err = got_ref_delete(ref, repo);
493 if (err)
494 goto done;
496 free(id);
497 id = NULL;
498 } else {
499 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
500 if (err)
501 goto done;
502 locked = 1;
504 err = got_ref_resolve(&id, repo, ref);
505 if (err)
506 goto done;
508 if (got_object_id_cmp(id, &old_id) != 0) {
509 got_object_id_hex(&old_id, hex1, sizeof(hex1));
510 got_object_id_hex(id, hex2, sizeof(hex2));
511 err = got_error_fmt(GOT_ERR_REF_BUSY,
512 "Update %s: %s failed; %s: %s has been "
513 "created by someone else while transaction "
514 "was in progress",
515 got_ref_get_name(ref), hex1,
516 got_ref_get_name(ref), hex2);
517 goto done;
520 if (got_object_id_cmp(&new_id, &old_id) != 0) {
521 err = got_ref_change_ref(ref, &new_id);
522 if (err)
523 goto done;
525 err = got_ref_write(ref, repo);
526 if (err)
527 goto done;
530 free(id);
531 id = NULL;
533 done:
534 if (err) {
535 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
536 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
537 "could not acquire exclusive file lock for %s",
538 refname);
540 send_ref_update_ng(client, &iref, refname, err->msg);
541 } else
542 send_ref_update_ok(client, &iref, refname);
544 if (client->nref_updates > 0) {
545 client->nref_updates--;
546 if (client->nref_updates == 0) {
547 send_refs_updated(client);
548 client->flush_disconnect = 1;
552 if (locked) {
553 const struct got_error *unlock_err;
554 unlock_err = got_ref_unlock(ref);
555 if (unlock_err && err == NULL)
556 err = unlock_err;
558 if (ref)
559 got_ref_close(ref);
560 if (repo)
561 got_repo_close(repo);
562 free(refname);
563 free(id);
564 return err;
567 static void
568 session_dispatch_repo_child(int fd, short event, void *arg)
570 struct gotd_imsgev *iev = arg;
571 struct imsgbuf *ibuf = &iev->ibuf;
572 struct gotd_session_client *client = &gotd_session_client;
573 ssize_t n;
574 int shut = 0;
575 struct imsg imsg;
577 if (event & EV_READ) {
578 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
579 fatal("imsg_read error");
580 if (n == 0) {
581 /* Connection closed. */
582 shut = 1;
583 goto done;
587 if (event & EV_WRITE) {
588 n = msgbuf_write(&ibuf->w);
589 if (n == -1 && errno != EAGAIN)
590 fatal("msgbuf_write");
591 if (n == 0) {
592 /* Connection closed. */
593 shut = 1;
594 goto done;
598 for (;;) {
599 const struct got_error *err = NULL;
600 uint32_t client_id = 0;
601 int do_disconnect = 0;
602 int do_ref_updates = 0, do_ref_update = 0;
603 int do_packfile_install = 0;
605 if ((n = imsg_get(ibuf, &imsg)) == -1)
606 fatal("%s: imsg_get error", __func__);
607 if (n == 0) /* No more messages. */
608 break;
610 switch (imsg.hdr.type) {
611 case GOTD_IMSG_ERROR:
612 do_disconnect = 1;
613 err = gotd_imsg_recv_error(&client_id, &imsg);
614 break;
615 case GOTD_IMSG_PACKFILE_DONE:
616 do_disconnect = 1;
617 err = recv_packfile_done(&client_id, &imsg);
618 break;
619 case GOTD_IMSG_PACKFILE_INSTALL:
620 err = recv_packfile_install(&client_id, &imsg);
621 if (err == NULL)
622 do_packfile_install = 1;
623 break;
624 case GOTD_IMSG_REF_UPDATES_START:
625 err = recv_ref_updates_start(&client_id, &imsg);
626 if (err == NULL)
627 do_ref_updates = 1;
628 break;
629 case GOTD_IMSG_REF_UPDATE:
630 err = recv_ref_update(&client_id, &imsg);
631 if (err == NULL)
632 do_ref_update = 1;
633 break;
634 default:
635 log_debug("unexpected imsg %d", imsg.hdr.type);
636 break;
639 if (do_disconnect) {
640 if (err)
641 disconnect_on_error(client, err);
642 else
643 disconnect(client);
644 } else {
645 if (do_packfile_install)
646 err = install_pack(client,
647 gotd_session.repo->path, &imsg);
648 else if (do_ref_updates)
649 err = begin_ref_updates(client, &imsg);
650 else if (do_ref_update)
651 err = update_ref(&shut, client,
652 gotd_session.repo->path, &imsg);
653 if (err)
654 log_warnx("uid %d: %s", client->euid, err->msg);
656 imsg_free(&imsg);
658 done:
659 if (!shut) {
660 gotd_imsg_event_add(iev);
661 } else {
662 /* This pipe is dead. Remove its event handler */
663 event_del(&iev->ev);
664 event_loopexit(NULL);
668 static const struct got_error *
669 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
671 struct gotd_imsg_capabilities icapas;
672 size_t datalen;
674 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
675 if (datalen != sizeof(icapas))
676 return got_error(GOT_ERR_PRIVSEP_LEN);
677 memcpy(&icapas, imsg->data, sizeof(icapas));
679 client->ncapa_alloc = icapas.ncapabilities;
680 client->capabilities = calloc(client->ncapa_alloc,
681 sizeof(*client->capabilities));
682 if (client->capabilities == NULL) {
683 client->ncapa_alloc = 0;
684 return got_error_from_errno("calloc");
687 log_debug("expecting %zu capabilities from uid %d",
688 client->ncapa_alloc, client->euid);
689 return NULL;
692 static const struct got_error *
693 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
695 struct gotd_imsg_capability icapa;
696 struct gotd_client_capability *capa;
697 size_t datalen;
698 char *key, *value = NULL;
700 if (client->capabilities == NULL ||
701 client->ncapabilities >= client->ncapa_alloc) {
702 return got_error_msg(GOT_ERR_BAD_REQUEST,
703 "unexpected capability received");
706 memset(&icapa, 0, sizeof(icapa));
708 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
709 if (datalen < sizeof(icapa))
710 return got_error(GOT_ERR_PRIVSEP_LEN);
711 memcpy(&icapa, imsg->data, sizeof(icapa));
713 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
714 return got_error(GOT_ERR_PRIVSEP_LEN);
716 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
717 if (key == NULL)
718 return got_error_from_errno("strndup");
719 if (icapa.value_len > 0) {
720 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
721 icapa.value_len);
722 if (value == NULL) {
723 free(key);
724 return got_error_from_errno("strndup");
728 capa = &client->capabilities[client->ncapabilities++];
729 capa->key = key;
730 capa->value = value;
732 if (value)
733 log_debug("uid %d: capability %s=%s", client->euid, key, value);
734 else
735 log_debug("uid %d: capability %s", client->euid, key);
737 return NULL;
740 static const struct got_error *
741 ensure_client_is_reading(struct gotd_session_client *client)
743 if (client->is_writing) {
744 return got_error_fmt(GOT_ERR_BAD_PACKET,
745 "uid %d made a read-request but is not reading from "
746 "a repository", client->euid);
749 return NULL;
752 static const struct got_error *
753 ensure_client_is_writing(struct gotd_session_client *client)
755 if (!client->is_writing) {
756 return got_error_fmt(GOT_ERR_BAD_PACKET,
757 "uid %d made a write-request but is not writing to "
758 "a repository", client->euid);
761 return NULL;
764 static const struct got_error *
765 forward_want(struct gotd_session_client *client, struct imsg *imsg)
767 struct gotd_imsg_want ireq;
768 struct gotd_imsg_want iwant;
769 size_t datalen;
771 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
772 if (datalen != sizeof(ireq))
773 return got_error(GOT_ERR_PRIVSEP_LEN);
775 memcpy(&ireq, imsg->data, datalen);
777 memset(&iwant, 0, sizeof(iwant));
778 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
779 iwant.client_id = client->id;
781 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
782 gotd_session.proc_id, -1, &iwant, sizeof(iwant)) == -1)
783 return got_error_from_errno("imsg compose WANT");
785 return NULL;
788 static const struct got_error *
789 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
791 const struct got_error *err = NULL;
792 struct gotd_imsg_ref_update ireq;
793 struct gotd_imsg_ref_update *iref = NULL;
794 size_t datalen;
796 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
797 if (datalen < sizeof(ireq))
798 return got_error(GOT_ERR_PRIVSEP_LEN);
799 memcpy(&ireq, imsg->data, sizeof(ireq));
800 if (datalen != sizeof(ireq) + ireq.name_len)
801 return got_error(GOT_ERR_PRIVSEP_LEN);
803 iref = malloc(datalen);
804 if (iref == NULL)
805 return got_error_from_errno("malloc");
806 memcpy(iref, imsg->data, datalen);
808 iref->client_id = client->id;
809 if (gotd_imsg_compose_event(&client->repo_child_iev,
810 GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
811 iref, datalen) == -1)
812 err = got_error_from_errno("imsg compose REF_UPDATE");
813 free(iref);
814 return err;
817 static const struct got_error *
818 forward_have(struct gotd_session_client *client, struct imsg *imsg)
820 struct gotd_imsg_have ireq;
821 struct gotd_imsg_have ihave;
822 size_t datalen;
824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
825 if (datalen != sizeof(ireq))
826 return got_error(GOT_ERR_PRIVSEP_LEN);
828 memcpy(&ireq, imsg->data, datalen);
830 memset(&ihave, 0, sizeof(ihave));
831 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
832 ihave.client_id = client->id;
834 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
835 gotd_session.proc_id, -1, &ihave, sizeof(ihave)) == -1)
836 return got_error_from_errno("imsg compose HAVE");
838 return NULL;
841 static int
842 client_has_capability(struct gotd_session_client *client, const char *capastr)
844 struct gotd_client_capability *capa;
845 size_t i;
847 if (client->ncapabilities == 0)
848 return 0;
850 for (i = 0; i < client->ncapabilities; i++) {
851 capa = &client->capabilities[i];
852 if (strcmp(capa->key, capastr) == 0)
853 return 1;
856 return 0;
859 static const struct got_error *
860 recv_packfile(struct gotd_session_client *client)
862 const struct got_error *err = NULL;
863 struct gotd_imsg_recv_packfile ipack;
864 struct gotd_imsg_packfile_pipe ipipe;
865 struct gotd_imsg_packidx_file ifile;
866 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
867 int packfd = -1, idxfd = -1;
868 int pipe[2] = { -1, -1 };
870 if (client->packfile_path) {
871 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
872 "uid %d already has a pack file", client->euid);
875 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
876 return got_error_from_errno("socketpair");
878 memset(&ipipe, 0, sizeof(ipipe));
879 ipipe.client_id = client->id;
881 /* Send pack pipe end 0 to repo child process. */
882 if (gotd_imsg_compose_event(&client->repo_child_iev,
883 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
884 &ipipe, sizeof(ipipe)) == -1) {
885 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
886 pipe[0] = -1;
887 goto done;
889 pipe[0] = -1;
891 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
892 if (gotd_imsg_compose_event(&client->iev,
893 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
894 NULL, 0) == -1)
895 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
896 pipe[1] = -1;
898 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
899 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
900 client->euid) == -1) {
901 err = got_error_from_errno("asprintf");
902 goto done;
905 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
906 if (err)
907 goto done;
908 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
909 err = got_error_from_errno2("fchmod", pack_path);
910 goto done;
913 free(basepath);
914 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
915 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
916 client->euid) == -1) {
917 err = got_error_from_errno("asprintf");
918 basepath = NULL;
919 goto done;
921 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
922 if (err)
923 goto done;
924 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
925 err = got_error_from_errno2("fchmod", idx_path);
926 goto done;
929 memset(&ifile, 0, sizeof(ifile));
930 ifile.client_id = client->id;
931 if (gotd_imsg_compose_event(&client->repo_child_iev,
932 GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
933 idxfd, &ifile, sizeof(ifile)) == -1) {
934 err = got_error_from_errno("imsg compose PACKIDX_FILE");
935 idxfd = -1;
936 goto done;
938 idxfd = -1;
940 memset(&ipack, 0, sizeof(ipack));
941 ipack.client_id = client->id;
942 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
943 ipack.report_status = 1;
945 if (gotd_imsg_compose_event(&client->repo_child_iev,
946 GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
947 &ipack, sizeof(ipack)) == -1) {
948 err = got_error_from_errno("imsg compose RECV_PACKFILE");
949 packfd = -1;
950 goto done;
952 packfd = -1;
954 done:
955 free(basepath);
956 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
957 err = got_error_from_errno("close");
958 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
959 err = got_error_from_errno("close");
960 if (packfd != -1 && close(packfd) == -1 && err == NULL)
961 err = got_error_from_errno("close");
962 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
963 err = got_error_from_errno("close");
964 if (err) {
965 free(pack_path);
966 free(idx_path);
967 } else {
968 client->packfile_path = pack_path;
969 client->packidx_path = idx_path;
971 return err;
974 static const struct got_error *
975 send_packfile(struct gotd_session_client *client)
977 const struct got_error *err = NULL;
978 struct gotd_imsg_send_packfile ipack;
979 struct gotd_imsg_packfile_pipe ipipe;
980 int pipe[2];
982 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
983 return got_error_from_errno("socketpair");
985 memset(&ipack, 0, sizeof(ipack));
986 memset(&ipipe, 0, sizeof(ipipe));
988 ipack.client_id = client->id;
989 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
990 ipack.report_progress = 1;
992 client->delta_cache_fd = got_opentempfd();
993 if (client->delta_cache_fd == -1)
994 return got_error_from_errno("got_opentempfd");
996 if (gotd_imsg_compose_event(&client->repo_child_iev,
997 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
998 &ipack, sizeof(ipack)) == -1) {
999 err = got_error_from_errno("imsg compose SEND_PACKFILE");
1000 close(pipe[0]);
1001 close(pipe[1]);
1002 return err;
1005 ipipe.client_id = client->id;
1007 /* Send pack pipe end 0 to repo child process. */
1008 if (gotd_imsg_compose_event(&client->repo_child_iev,
1009 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1010 pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1011 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1012 close(pipe[1]);
1013 return err;
1016 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1017 if (gotd_imsg_compose_event(&client->iev,
1018 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1019 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1021 return err;
1024 static void
1025 session_dispatch_client(int fd, short events, void *arg)
1027 struct gotd_imsgev *iev = arg;
1028 struct imsgbuf *ibuf = &iev->ibuf;
1029 struct gotd_session_client *client = &gotd_session_client;
1030 const struct got_error *err = NULL;
1031 struct imsg imsg;
1032 ssize_t n;
1034 if (events & EV_WRITE) {
1035 while (ibuf->w.queued) {
1036 n = msgbuf_write(&ibuf->w);
1037 if (n == -1 && errno == EPIPE) {
1039 * The client has closed its socket.
1040 * This can happen when Git clients are
1041 * done sending pack file data.
1043 msgbuf_clear(&ibuf->w);
1044 continue;
1045 } else if (n == -1 && errno != EAGAIN) {
1046 err = got_error_from_errno("imsg_flush");
1047 disconnect_on_error(client, err);
1048 return;
1050 if (n == 0) {
1051 /* Connection closed. */
1052 err = got_error(GOT_ERR_EOF);
1053 disconnect_on_error(client, err);
1054 return;
1058 if (client->flush_disconnect) {
1059 disconnect(client);
1060 return;
1064 if ((events & EV_READ) == 0)
1065 return;
1067 memset(&imsg, 0, sizeof(imsg));
1069 while (err == NULL) {
1070 err = gotd_imsg_recv(&imsg, ibuf, 0);
1071 if (err) {
1072 if (err->code == GOT_ERR_PRIVSEP_READ)
1073 err = NULL;
1074 else if (err->code == GOT_ERR_EOF &&
1075 client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1077 * The client has closed its socket before
1078 * sending its capability announcement.
1079 * This can happen when Git clients have
1080 * no ref-updates to send.
1082 disconnect_on_error(client, err);
1083 return;
1085 break;
1088 evtimer_del(&client->tmo);
1090 switch (imsg.hdr.type) {
1091 case GOTD_IMSG_CAPABILITIES:
1092 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1093 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1094 "unexpected capabilities received");
1095 break;
1097 log_debug("receiving capabilities from uid %d",
1098 client->euid);
1099 err = recv_capabilities(client, &imsg);
1100 break;
1101 case GOTD_IMSG_CAPABILITY:
1102 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1103 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1104 "unexpected capability received");
1105 break;
1107 err = recv_capability(client, &imsg);
1108 if (err || client->ncapabilities < client->ncapa_alloc)
1109 break;
1110 if (!client->is_writing) {
1111 client->state = GOTD_STATE_EXPECT_WANT;
1112 client->accept_flush_pkt = 1;
1113 log_debug("uid %d: expecting want-lines",
1114 client->euid);
1115 } else if (client->is_writing) {
1116 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1117 client->accept_flush_pkt = 1;
1118 log_debug("uid %d: expecting ref-update-lines",
1119 client->euid);
1120 } else
1121 fatalx("client %d is both reading and writing",
1122 client->euid);
1123 break;
1124 case GOTD_IMSG_WANT:
1125 if (client->state != GOTD_STATE_EXPECT_WANT) {
1126 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1127 "unexpected want-line received");
1128 break;
1130 log_debug("received want-line from uid %d",
1131 client->euid);
1132 err = ensure_client_is_reading(client);
1133 if (err)
1134 break;
1135 client->accept_flush_pkt = 1;
1136 err = forward_want(client, &imsg);
1137 break;
1138 case GOTD_IMSG_REF_UPDATE:
1139 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1140 client->state !=
1141 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1142 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1143 "unexpected ref-update-line received");
1144 break;
1146 log_debug("received ref-update-line from uid %d",
1147 client->euid);
1148 err = ensure_client_is_writing(client);
1149 if (err)
1150 break;
1151 err = forward_ref_update(client, &imsg);
1152 if (err)
1153 break;
1154 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1155 client->accept_flush_pkt = 1;
1156 break;
1157 case GOTD_IMSG_HAVE:
1158 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1159 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1160 "unexpected have-line received");
1161 break;
1163 log_debug("received have-line from uid %d",
1164 client->euid);
1165 err = ensure_client_is_reading(client);
1166 if (err)
1167 break;
1168 err = forward_have(client, &imsg);
1169 if (err)
1170 break;
1171 client->accept_flush_pkt = 1;
1172 break;
1173 case GOTD_IMSG_FLUSH:
1174 if (client->state == GOTD_STATE_EXPECT_WANT ||
1175 client->state == GOTD_STATE_EXPECT_HAVE) {
1176 err = ensure_client_is_reading(client);
1177 if (err)
1178 break;
1179 } else if (client->state ==
1180 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1181 err = ensure_client_is_writing(client);
1182 if (err)
1183 break;
1184 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1185 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1186 "unexpected flush-pkt received");
1187 break;
1189 if (!client->accept_flush_pkt) {
1190 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1191 "unexpected flush-pkt received");
1192 break;
1196 * Accept just one flush packet at a time.
1197 * Future client state transitions will set this flag
1198 * again if another flush packet is expected.
1200 client->accept_flush_pkt = 0;
1202 log_debug("received flush-pkt from uid %d",
1203 client->euid);
1204 if (client->state == GOTD_STATE_EXPECT_WANT) {
1205 client->state = GOTD_STATE_EXPECT_HAVE;
1206 log_debug("uid %d: expecting have-lines",
1207 client->euid);
1208 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1209 client->state = GOTD_STATE_EXPECT_DONE;
1210 client->accept_flush_pkt = 1;
1211 log_debug("uid %d: expecting 'done'",
1212 client->euid);
1213 } else if (client->state ==
1214 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1215 client->state = GOTD_STATE_EXPECT_PACKFILE;
1216 log_debug("uid %d: expecting packfile",
1217 client->euid);
1218 err = recv_packfile(client);
1219 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1220 /* should not happen, see above */
1221 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1222 "unexpected client state");
1223 break;
1225 break;
1226 case GOTD_IMSG_DONE:
1227 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1228 client->state != GOTD_STATE_EXPECT_DONE) {
1229 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1230 "unexpected flush-pkt received");
1231 break;
1233 log_debug("received 'done' from uid %d", client->euid);
1234 err = ensure_client_is_reading(client);
1235 if (err)
1236 break;
1237 client->state = GOTD_STATE_DONE;
1238 client->accept_flush_pkt = 1;
1239 err = send_packfile(client);
1240 break;
1241 default:
1242 log_debug("unexpected imsg %d", imsg.hdr.type);
1243 err = got_error(GOT_ERR_PRIVSEP_MSG);
1244 break;
1247 imsg_free(&imsg);
1250 if (err) {
1251 if (err->code != GOT_ERR_EOF ||
1252 client->state != GOTD_STATE_EXPECT_PACKFILE)
1253 disconnect_on_error(client, err);
1254 } else {
1255 gotd_imsg_event_add(iev);
1256 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1260 static const struct got_error *
1261 list_refs_request(void)
1263 static const struct got_error *err;
1264 struct gotd_session_client *client = &gotd_session_client;
1265 struct gotd_imsgev *iev = &client->repo_child_iev;
1266 struct gotd_imsg_list_refs_internal ilref;
1267 int fd;
1269 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1270 return got_error(GOT_ERR_PRIVSEP_MSG);
1272 memset(&ilref, 0, sizeof(ilref));
1273 ilref.client_id = client->id;
1275 fd = dup(client->fd);
1276 if (fd == -1)
1277 return got_error_from_errno("dup");
1279 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1280 gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1281 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1282 close(fd);
1283 return err;
1286 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1287 log_debug("uid %d: expecting capabilities", client->euid);
1288 return NULL;
1291 static const struct got_error *
1292 recv_connect(struct imsg *imsg)
1294 struct gotd_session_client *client = &gotd_session_client;
1295 struct gotd_imsg_connect iconnect;
1296 size_t datalen;
1298 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1299 return got_error(GOT_ERR_PRIVSEP_MSG);
1301 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1302 if (datalen != sizeof(iconnect))
1303 return got_error(GOT_ERR_PRIVSEP_LEN);
1304 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1306 if (imsg->fd == -1)
1307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1309 client->fd = imsg->fd;
1310 client->euid = iconnect.euid;
1311 client->egid = iconnect.egid;
1313 imsg_init(&client->iev.ibuf, client->fd);
1314 client->iev.handler = session_dispatch_client;
1315 client->iev.events = EV_READ;
1316 client->iev.handler_arg = NULL;
1317 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1318 session_dispatch_client, &client->iev);
1319 gotd_imsg_event_add(&client->iev);
1320 evtimer_set(&client->tmo, gotd_request_timeout, client);
1322 return NULL;
1325 static const struct got_error *
1326 recv_repo_child(struct imsg *imsg)
1328 struct gotd_imsg_connect_repo_child ichild;
1329 struct gotd_session_client *client = &gotd_session_client;
1330 size_t datalen;
1332 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1333 return got_error(GOT_ERR_PRIVSEP_MSG);
1335 /* We should already have received a pipe to the listener. */
1336 if (client->fd == -1)
1337 return got_error(GOT_ERR_PRIVSEP_MSG);
1339 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1340 if (datalen != sizeof(ichild))
1341 return got_error(GOT_ERR_PRIVSEP_LEN);
1343 memcpy(&ichild, imsg->data, sizeof(ichild));
1345 client->id = ichild.client_id;
1346 if (ichild.proc_id == PROC_REPO_WRITE)
1347 client->is_writing = 1;
1348 else if (ichild.proc_id == PROC_REPO_READ)
1349 client->is_writing = 0;
1350 else
1351 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1352 "bad child process type");
1354 if (imsg->fd == -1)
1355 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1357 imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1358 client->repo_child_iev.handler = session_dispatch_repo_child;
1359 client->repo_child_iev.events = EV_READ;
1360 client->repo_child_iev.handler_arg = NULL;
1361 event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1362 EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1363 gotd_imsg_event_add(&client->repo_child_iev);
1365 /* The "recvfd" pledge promise is no longer needed. */
1366 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1367 fatal("pledge");
1369 return NULL;
1372 static void
1373 session_dispatch(int fd, short event, void *arg)
1375 struct gotd_imsgev *iev = arg;
1376 struct imsgbuf *ibuf = &iev->ibuf;
1377 struct gotd_session_client *client = &gotd_session_client;
1378 ssize_t n;
1379 int shut = 0;
1380 struct imsg imsg;
1382 if (event & EV_READ) {
1383 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1384 fatal("imsg_read error");
1385 if (n == 0) {
1386 /* Connection closed. */
1387 shut = 1;
1388 goto done;
1392 if (event & EV_WRITE) {
1393 n = msgbuf_write(&ibuf->w);
1394 if (n == -1 && errno != EAGAIN)
1395 fatal("msgbuf_write");
1396 if (n == 0) {
1397 /* Connection closed. */
1398 shut = 1;
1399 goto done;
1403 for (;;) {
1404 const struct got_error *err = NULL;
1405 uint32_t client_id = 0;
1406 int do_disconnect = 0, do_list_refs = 0;
1408 if ((n = imsg_get(ibuf, &imsg)) == -1)
1409 fatal("%s: imsg_get error", __func__);
1410 if (n == 0) /* No more messages. */
1411 break;
1413 switch (imsg.hdr.type) {
1414 case GOTD_IMSG_ERROR:
1415 do_disconnect = 1;
1416 err = gotd_imsg_recv_error(&client_id, &imsg);
1417 break;
1418 case GOTD_IMSG_CONNECT:
1419 err = recv_connect(&imsg);
1420 break;
1421 case GOTD_IMSG_DISCONNECT:
1422 do_disconnect = 1;
1423 break;
1424 case GOTD_IMSG_CONNECT_REPO_CHILD:
1425 err = recv_repo_child(&imsg);
1426 if (err)
1427 break;
1428 do_list_refs = 1;
1429 break;
1430 default:
1431 log_debug("unexpected imsg %d", imsg.hdr.type);
1432 break;
1434 imsg_free(&imsg);
1436 if (do_disconnect) {
1437 if (err)
1438 disconnect_on_error(client, err);
1439 else
1440 disconnect(client);
1441 } else if (do_list_refs)
1442 err = list_refs_request();
1444 if (err)
1445 log_warnx("uid %d: %s", client->euid, err->msg);
1447 done:
1448 if (!shut) {
1449 gotd_imsg_event_add(iev);
1450 } else {
1451 /* This pipe is dead. Remove its event handler */
1452 event_del(&iev->ev);
1453 event_loopexit(NULL);
1457 void
1458 session_main(const char *title, const char *repo_path,
1459 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1460 enum gotd_procid proc_id)
1462 const struct got_error *err = NULL;
1463 struct event evsigint, evsigterm, evsighup, evsigusr1;
1465 gotd_session.title = title;
1466 gotd_session.pid = getpid();
1467 gotd_session.pack_fds = pack_fds;
1468 gotd_session.temp_fds = temp_fds;
1469 memcpy(&gotd_session.request_timeout, request_timeout,
1470 sizeof(gotd_session.request_timeout));
1471 gotd_session.proc_id = proc_id;
1473 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1474 if (err)
1475 goto done;
1476 if (!got_repo_is_bare(gotd_session.repo)) {
1477 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1478 "bare git repository required");
1479 goto done;
1482 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1484 signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1485 signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1486 signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1487 signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1488 signal(SIGPIPE, SIG_IGN);
1490 signal_add(&evsigint, NULL);
1491 signal_add(&evsigterm, NULL);
1492 signal_add(&evsighup, NULL);
1493 signal_add(&evsigusr1, NULL);
1495 gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1496 gotd_session_client.fd = -1;
1497 gotd_session_client.nref_updates = -1;
1498 gotd_session_client.delta_cache_fd = -1;
1499 gotd_session_client.accept_flush_pkt = 1;
1501 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1502 gotd_session.parent_iev.handler = session_dispatch;
1503 gotd_session.parent_iev.events = EV_READ;
1504 gotd_session.parent_iev.handler_arg = NULL;
1505 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1506 EV_READ, session_dispatch, &gotd_session.parent_iev);
1507 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1508 GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1509 -1, NULL, 0) == -1) {
1510 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1511 goto done;
1514 event_dispatch();
1515 done:
1516 if (err)
1517 log_warnx("%s: %s", title, err->msg);
1518 gotd_session_shutdown();
1521 void
1522 gotd_session_shutdown(void)
1524 log_debug("shutting down");
1525 if (gotd_session.repo)
1526 got_repo_close(gotd_session.repo);
1527 got_repo_pack_fds_close(gotd_session.pack_fds);
1528 got_repo_temp_fds_close(gotd_session.temp_fds);
1529 exit(0);