2 * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_repository.h"
48 #include "got_lib_gitproto.h"
55 static struct gotd_session {
58 struct got_repository *repo;
61 struct gotd_imsgev parent_iev;
62 struct timeval request_timeout;
65 static struct gotd_session_client {
66 enum gotd_session_state state;
68 struct gotd_client_capability *capabilities;
74 struct gotd_imsgev iev;
75 struct gotd_imsgev repo_child_iev;
83 } gotd_session_client;
85 void gotd_session_sighdlr(int sig, short event, void *arg);
86 static void gotd_session_shutdown(void);
89 disconnect(struct gotd_session_client *client)
91 log_debug("uid %d: disconnecting", client->euid);
93 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
94 GOTD_IMSG_DISCONNECT, PROC_SESSION, -1, NULL, 0) == -1)
95 log_warn("imsg compose DISCONNECT");
97 imsg_clear(&client->repo_child_iev.ibuf);
98 event_del(&client->repo_child_iev.ev);
99 evtimer_del(&client->tmo);
101 if (client->delta_cache_fd != -1)
102 close(client->delta_cache_fd);
103 if (client->packfile_path) {
104 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
105 log_warn("unlink %s: ", client->packfile_path);
106 free(client->packfile_path);
108 if (client->packidx_path) {
109 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
110 log_warn("unlink %s: ", client->packidx_path);
111 free(client->packidx_path);
113 free(client->capabilities);
115 gotd_session_shutdown();
119 disconnect_on_error(struct gotd_session_client *client,
120 const struct got_error *err)
124 log_warnx("uid %d: %s", client->euid, err->msg);
125 if (err->code != GOT_ERR_EOF) {
126 imsg_init(&ibuf, client->fd);
127 gotd_imsg_send_error(&ibuf, 0, PROC_SESSION, err);
135 gotd_request_timeout(int fd, short events, void *arg)
137 struct gotd_session_client *client = arg;
139 log_debug("disconnecting uid %d due to timeout", client->euid);
144 gotd_session_sighdlr(int sig, short event, void *arg)
147 * Normal signal handler rules don't apply because libevent
153 log_info("%s: ignoring SIGHUP", __func__);
156 log_info("%s: ignoring SIGUSR1", __func__);
160 gotd_session_shutdown();
164 fatalx("unexpected signal");
168 static const struct got_error *
169 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
171 struct gotd_imsg_packfile_done idone;
174 log_debug("packfile-done received");
176 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
177 if (datalen != sizeof(idone))
178 return got_error(GOT_ERR_PRIVSEP_LEN);
179 memcpy(&idone, imsg->data, sizeof(idone));
181 *client_id = idone.client_id;
185 static const struct got_error *
186 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
188 struct gotd_imsg_packfile_install inst;
191 log_debug("packfile-install received");
193 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
194 if (datalen != sizeof(inst))
195 return got_error(GOT_ERR_PRIVSEP_LEN);
196 memcpy(&inst, imsg->data, sizeof(inst));
198 *client_id = inst.client_id;
202 static const struct got_error *
203 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
205 struct gotd_imsg_ref_updates_start istart;
208 log_debug("ref-updates-start received");
210 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
211 if (datalen != sizeof(istart))
212 return got_error(GOT_ERR_PRIVSEP_LEN);
213 memcpy(&istart, imsg->data, sizeof(istart));
215 *client_id = istart.client_id;
219 static const struct got_error *
220 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
222 struct gotd_imsg_ref_update iref;
225 log_debug("ref-update received");
227 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
228 if (datalen < sizeof(iref))
229 return got_error(GOT_ERR_PRIVSEP_LEN);
230 memcpy(&iref, imsg->data, sizeof(iref));
232 *client_id = iref.client_id;
236 static const struct got_error *
237 send_ref_update_ok(struct gotd_session_client *client,
238 struct gotd_imsg_ref_update *iref, const char *refname)
240 struct gotd_imsg_ref_update_ok iok;
241 struct gotd_imsgev *iev = &client->iev;
245 memset(&iok, 0, sizeof(iok));
246 iok.client_id = client->id;
247 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
248 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
249 iok.name_len = strlen(refname);
251 len = sizeof(iok) + iok.name_len;
252 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
253 PROC_SESSION, gotd_session.pid, len);
255 return got_error_from_errno("imsg_create REF_UPDATE_OK");
257 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
258 return got_error_from_errno("imsg_add REF_UPDATE_OK");
259 if (imsg_add(wbuf, refname, iok.name_len) == -1)
260 return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 imsg_close(&iev->ibuf, wbuf);
264 gotd_imsg_event_add(iev);
269 send_refs_updated(struct gotd_session_client *client)
271 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 PROC_SESSION, -1, NULL, 0) == -1)
273 log_warn("imsg compose REFS_UPDATED");
276 static const struct got_error *
277 send_ref_update_ng(struct gotd_session_client *client,
278 struct gotd_imsg_ref_update *iref, const char *refname,
281 const struct got_error *ng_err;
282 struct gotd_imsg_ref_update_ng ing;
283 struct gotd_imsgev *iev = &client->iev;
287 memset(&ing, 0, sizeof(ing));
288 ing.client_id = client->id;
289 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
290 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
291 ing.name_len = strlen(refname);
293 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
294 ing.reason_len = strlen(ng_err->msg);
296 len = sizeof(ing) + ing.name_len + ing.reason_len;
297 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
298 PROC_SESSION, gotd_session.pid, len);
300 return got_error_from_errno("imsg_create REF_UPDATE_NG");
302 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
303 return got_error_from_errno("imsg_add REF_UPDATE_NG");
304 if (imsg_add(wbuf, refname, ing.name_len) == -1)
305 return got_error_from_errno("imsg_add REF_UPDATE_NG");
306 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
307 return got_error_from_errno("imsg_add REF_UPDATE_NG");
310 imsg_close(&iev->ibuf, wbuf);
311 gotd_imsg_event_add(iev);
315 static const struct got_error *
316 install_pack(struct gotd_session_client *client, const char *repo_path,
319 const struct got_error *err = NULL;
320 struct gotd_imsg_packfile_install inst;
321 char hex[SHA1_DIGEST_STRING_LENGTH];
323 char *packfile_path = NULL, *packidx_path = NULL;
325 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 if (datalen != sizeof(inst))
327 return got_error(GOT_ERR_PRIVSEP_LEN);
328 memcpy(&inst, imsg->data, sizeof(inst));
330 if (client->packfile_path == NULL)
331 return got_error_msg(GOT_ERR_BAD_REQUEST,
332 "client has no pack file");
333 if (client->packidx_path == NULL)
334 return got_error_msg(GOT_ERR_BAD_REQUEST,
335 "client has no pack file index");
337 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
338 return got_error_msg(GOT_ERR_NO_SPACE,
339 "could not convert pack file SHA1 to hex");
341 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
342 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
343 err = got_error_from_errno("asprintf");
347 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
348 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
349 err = got_error_from_errno("asprintf");
353 if (rename(client->packfile_path, packfile_path) == -1) {
354 err = got_error_from_errno3("rename", client->packfile_path,
359 free(client->packfile_path);
360 client->packfile_path = NULL;
362 if (rename(client->packidx_path, packidx_path) == -1) {
363 err = got_error_from_errno3("rename", client->packidx_path,
368 free(client->packidx_path);
369 client->packidx_path = NULL;
376 static const struct got_error *
377 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
379 struct gotd_imsg_ref_updates_start istart;
382 if (client->nref_updates != -1)
383 return got_error(GOT_ERR_PRIVSEP_MSG);
385 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
386 if (datalen != sizeof(istart))
387 return got_error(GOT_ERR_PRIVSEP_LEN);
388 memcpy(&istart, imsg->data, sizeof(istart));
390 if (istart.nref_updates <= 0)
391 return got_error(GOT_ERR_PRIVSEP_MSG);
393 client->nref_updates = istart.nref_updates;
397 static const struct got_error *
398 update_ref(int *shut, struct gotd_session_client *client,
399 const char *repo_path, struct imsg *imsg)
401 const struct got_error *err = NULL;
402 struct got_repository *repo = NULL;
403 struct got_reference *ref = NULL;
404 struct gotd_imsg_ref_update iref;
405 struct got_object_id old_id, new_id;
406 struct got_object_id *id = NULL;
407 struct got_object *obj = NULL;
408 char *refname = NULL;
412 log_debug("update-ref from uid %d", client->euid);
414 if (client->nref_updates <= 0)
415 return got_error(GOT_ERR_PRIVSEP_MSG);
417 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
418 if (datalen < sizeof(iref))
419 return got_error(GOT_ERR_PRIVSEP_LEN);
420 memcpy(&iref, imsg->data, sizeof(iref));
421 if (datalen != sizeof(iref) + iref.name_len)
422 return got_error(GOT_ERR_PRIVSEP_LEN);
423 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
425 return got_error_from_errno("strndup");
427 log_debug("updating ref %s for uid %d", refname, client->euid);
429 err = got_repo_open(&repo, repo_path, NULL, NULL);
433 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
434 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
435 err = got_object_open(&obj, repo, &new_id);
439 if (iref.ref_is_new) {
440 err = got_ref_open(&ref, repo, refname, 0);
442 if (err->code != GOT_ERR_NOT_REF)
444 err = got_ref_alloc(&ref, refname, &new_id);
447 err = got_ref_write(ref, repo); /* will lock/unlock */
451 err = got_error_fmt(GOT_ERR_REF_BUSY,
452 "%s has been created by someone else "
453 "while transaction was in progress",
454 got_ref_get_name(ref));
458 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
463 err = got_ref_resolve(&id, repo, ref);
467 if (got_object_id_cmp(id, &old_id) != 0) {
468 err = got_error_fmt(GOT_ERR_REF_BUSY,
469 "%s has been modified by someone else "
470 "while transaction was in progress",
471 got_ref_get_name(ref));
475 err = got_ref_change_ref(ref, &new_id);
479 err = got_ref_write(ref, repo);
488 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
489 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
490 "could not acquire exclusive file lock for %s",
493 send_ref_update_ng(client, &iref, refname, err->msg);
495 send_ref_update_ok(client, &iref, refname);
497 if (client->nref_updates > 0) {
498 client->nref_updates--;
499 if (client->nref_updates == 0) {
500 send_refs_updated(client);
506 const struct got_error *unlock_err;
507 unlock_err = got_ref_unlock(ref);
508 if (unlock_err && err == NULL)
514 got_object_close(obj);
516 got_repo_close(repo);
523 session_dispatch_repo_child(int fd, short event, void *arg)
525 struct gotd_imsgev *iev = arg;
526 struct imsgbuf *ibuf = &iev->ibuf;
527 struct gotd_session_client *client = &gotd_session_client;
532 if (event & EV_READ) {
533 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
534 fatal("imsg_read error");
536 /* Connection closed. */
542 if (event & EV_WRITE) {
543 n = msgbuf_write(&ibuf->w);
544 if (n == -1 && errno != EAGAIN)
545 fatal("msgbuf_write");
547 /* Connection closed. */
554 const struct got_error *err = NULL;
555 uint32_t client_id = 0;
556 int do_disconnect = 0;
557 int do_ref_updates = 0, do_ref_update = 0;
558 int do_packfile_install = 0;
560 if ((n = imsg_get(ibuf, &imsg)) == -1)
561 fatal("%s: imsg_get error", __func__);
562 if (n == 0) /* No more messages. */
565 switch (imsg.hdr.type) {
566 case GOTD_IMSG_ERROR:
568 err = gotd_imsg_recv_error(&client_id, &imsg);
570 case GOTD_IMSG_PACKFILE_DONE:
572 err = recv_packfile_done(&client_id, &imsg);
574 case GOTD_IMSG_PACKFILE_INSTALL:
575 err = recv_packfile_install(&client_id, &imsg);
577 do_packfile_install = 1;
579 case GOTD_IMSG_REF_UPDATES_START:
580 err = recv_ref_updates_start(&client_id, &imsg);
584 case GOTD_IMSG_REF_UPDATE:
585 err = recv_ref_update(&client_id, &imsg);
590 log_debug("unexpected imsg %d", imsg.hdr.type);
596 disconnect_on_error(client, err);
600 if (do_packfile_install)
601 err = install_pack(client,
602 gotd_session.repo->path, &imsg);
603 else if (do_ref_updates)
604 err = begin_ref_updates(client, &imsg);
605 else if (do_ref_update)
606 err = update_ref(&shut, client,
607 gotd_session.repo->path, &imsg);
609 log_warnx("uid %d: %s", client->euid, err->msg);
615 gotd_imsg_event_add(iev);
617 /* This pipe is dead. Remove its event handler */
619 event_loopexit(NULL);
623 static const struct got_error *
624 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
626 struct gotd_imsg_capabilities icapas;
629 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
630 if (datalen != sizeof(icapas))
631 return got_error(GOT_ERR_PRIVSEP_LEN);
632 memcpy(&icapas, imsg->data, sizeof(icapas));
634 client->ncapa_alloc = icapas.ncapabilities;
635 client->capabilities = calloc(client->ncapa_alloc,
636 sizeof(*client->capabilities));
637 if (client->capabilities == NULL) {
638 client->ncapa_alloc = 0;
639 return got_error_from_errno("calloc");
642 log_debug("expecting %zu capabilities from uid %d",
643 client->ncapa_alloc, client->euid);
647 static const struct got_error *
648 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
650 struct gotd_imsg_capability icapa;
651 struct gotd_client_capability *capa;
653 char *key, *value = NULL;
655 if (client->capabilities == NULL ||
656 client->ncapabilities >= client->ncapa_alloc) {
657 return got_error_msg(GOT_ERR_BAD_REQUEST,
658 "unexpected capability received");
661 memset(&icapa, 0, sizeof(icapa));
663 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
664 if (datalen < sizeof(icapa))
665 return got_error(GOT_ERR_PRIVSEP_LEN);
666 memcpy(&icapa, imsg->data, sizeof(icapa));
668 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
669 return got_error(GOT_ERR_PRIVSEP_LEN);
671 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
673 return got_error_from_errno("strndup");
674 if (icapa.value_len > 0) {
675 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
679 return got_error_from_errno("strndup");
683 capa = &client->capabilities[client->ncapabilities++];
688 log_debug("uid %d: capability %s=%s", client->euid, key, value);
690 log_debug("uid %d: capability %s", client->euid, key);
695 static const struct got_error *
696 ensure_client_is_reading(struct gotd_session_client *client)
698 if (client->is_writing) {
699 return got_error_fmt(GOT_ERR_BAD_PACKET,
700 "uid %d made a read-request but is not reading from "
701 "a repository", client->euid);
707 static const struct got_error *
708 ensure_client_is_writing(struct gotd_session_client *client)
710 if (!client->is_writing) {
711 return got_error_fmt(GOT_ERR_BAD_PACKET,
712 "uid %d made a write-request but is not writing to "
713 "a repository", client->euid);
719 static const struct got_error *
720 forward_want(struct gotd_session_client *client, struct imsg *imsg)
722 struct gotd_imsg_want ireq;
723 struct gotd_imsg_want iwant;
726 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
727 if (datalen != sizeof(ireq))
728 return got_error(GOT_ERR_PRIVSEP_LEN);
730 memcpy(&ireq, imsg->data, datalen);
732 memset(&iwant, 0, sizeof(iwant));
733 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
734 iwant.client_id = client->id;
736 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
737 PROC_SESSION, -1, &iwant, sizeof(iwant)) == -1)
738 return got_error_from_errno("imsg compose WANT");
743 static const struct got_error *
744 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
746 const struct got_error *err = NULL;
747 struct gotd_imsg_ref_update ireq;
748 struct gotd_imsg_ref_update *iref = NULL;
751 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
752 if (datalen < sizeof(ireq))
753 return got_error(GOT_ERR_PRIVSEP_LEN);
754 memcpy(&ireq, imsg->data, sizeof(ireq));
755 if (datalen != sizeof(ireq) + ireq.name_len)
756 return got_error(GOT_ERR_PRIVSEP_LEN);
758 iref = malloc(datalen);
760 return got_error_from_errno("malloc");
761 memcpy(iref, imsg->data, datalen);
763 iref->client_id = client->id;
764 if (gotd_imsg_compose_event(&client->repo_child_iev,
765 GOTD_IMSG_REF_UPDATE, PROC_SESSION, -1, iref, datalen) == -1)
766 err = got_error_from_errno("imsg compose REF_UPDATE");
771 static const struct got_error *
772 forward_have(struct gotd_session_client *client, struct imsg *imsg)
774 struct gotd_imsg_have ireq;
775 struct gotd_imsg_have ihave;
778 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
779 if (datalen != sizeof(ireq))
780 return got_error(GOT_ERR_PRIVSEP_LEN);
782 memcpy(&ireq, imsg->data, datalen);
784 memset(&ihave, 0, sizeof(ihave));
785 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
786 ihave.client_id = client->id;
788 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
789 PROC_SESSION, -1, &ihave, sizeof(ihave)) == -1)
790 return got_error_from_errno("imsg compose HAVE");
796 client_has_capability(struct gotd_session_client *client, const char *capastr)
798 struct gotd_client_capability *capa;
801 if (client->ncapabilities == 0)
804 for (i = 0; i < client->ncapabilities; i++) {
805 capa = &client->capabilities[i];
806 if (strcmp(capa->key, capastr) == 0)
813 static const struct got_error *
814 recv_packfile(struct gotd_session_client *client)
816 const struct got_error *err = NULL;
817 struct gotd_imsg_recv_packfile ipack;
818 struct gotd_imsg_packfile_pipe ipipe;
819 struct gotd_imsg_packidx_file ifile;
820 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
821 int packfd = -1, idxfd = -1;
822 int pipe[2] = { -1, -1 };
824 if (client->packfile_path) {
825 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
826 "uid %d already has a pack file", client->euid);
829 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
830 return got_error_from_errno("socketpair");
832 memset(&ipipe, 0, sizeof(ipipe));
833 ipipe.client_id = client->id;
835 /* Send pack pipe end 0 to repo child process. */
836 if (gotd_imsg_compose_event(&client->repo_child_iev,
837 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[0],
838 &ipipe, sizeof(ipipe)) == -1) {
839 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
845 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
846 if (gotd_imsg_compose_event(&client->iev,
847 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[1], NULL, 0) == -1)
848 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
851 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
852 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
853 client->euid) == -1) {
854 err = got_error_from_errno("asprintf");
858 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
863 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
864 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
865 client->euid) == -1) {
866 err = got_error_from_errno("asprintf");
870 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
874 memset(&ifile, 0, sizeof(ifile));
875 ifile.client_id = client->id;
876 if (gotd_imsg_compose_event(&client->repo_child_iev,
877 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION,
878 idxfd, &ifile, sizeof(ifile)) == -1) {
879 err = got_error_from_errno("imsg compose PACKIDX_FILE");
885 memset(&ipack, 0, sizeof(ipack));
886 ipack.client_id = client->id;
887 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
888 ipack.report_status = 1;
890 if (gotd_imsg_compose_event(&client->repo_child_iev,
891 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION, packfd,
892 &ipack, sizeof(ipack)) == -1) {
893 err = got_error_from_errno("imsg compose RECV_PACKFILE");
901 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
902 err = got_error_from_errno("close");
903 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
904 err = got_error_from_errno("close");
905 if (packfd != -1 && close(packfd) == -1 && err == NULL)
906 err = got_error_from_errno("close");
907 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
908 err = got_error_from_errno("close");
913 client->packfile_path = pack_path;
914 client->packidx_path = idx_path;
919 static const struct got_error *
920 send_packfile(struct gotd_session_client *client)
922 const struct got_error *err = NULL;
923 struct gotd_imsg_send_packfile ipack;
924 struct gotd_imsg_packfile_pipe ipipe;
927 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
928 return got_error_from_errno("socketpair");
930 memset(&ipack, 0, sizeof(ipack));
931 memset(&ipipe, 0, sizeof(ipipe));
933 ipack.client_id = client->id;
934 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
935 ipack.report_progress = 1;
937 client->delta_cache_fd = got_opentempfd();
938 if (client->delta_cache_fd == -1)
939 return got_error_from_errno("got_opentempfd");
941 if (gotd_imsg_compose_event(&client->repo_child_iev,
942 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
943 &ipack, sizeof(ipack)) == -1) {
944 err = got_error_from_errno("imsg compose SEND_PACKFILE");
950 ipipe.client_id = client->id;
952 /* Send pack pipe end 0 to repo child process. */
953 if (gotd_imsg_compose_event(&client->repo_child_iev,
954 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
955 pipe[0], &ipipe, sizeof(ipipe)) == -1) {
956 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
961 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
962 if (gotd_imsg_compose_event(&client->iev,
963 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
964 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
970 session_dispatch_listener(int fd, short events, void *arg)
972 struct gotd_imsgev *iev = arg;
973 struct imsgbuf *ibuf = &iev->ibuf;
974 struct gotd_session_client *client = &gotd_session_client;
975 const struct got_error *err = NULL;
979 if (events & EV_WRITE) {
980 while (ibuf->w.queued) {
981 n = msgbuf_write(&ibuf->w);
982 if (n == -1 && errno == EPIPE) {
984 * The client has closed its socket.
985 * This can happen when Git clients are
986 * done sending pack file data.
988 msgbuf_clear(&ibuf->w);
990 } else if (n == -1 && errno != EAGAIN) {
991 err = got_error_from_errno("imsg_flush");
992 disconnect_on_error(client, err);
996 /* Connection closed. */
997 err = got_error(GOT_ERR_EOF);
998 disconnect_on_error(client, err);
1004 if ((events & EV_READ) == 0)
1007 memset(&imsg, 0, sizeof(imsg));
1009 while (err == NULL) {
1010 err = gotd_imsg_recv(&imsg, ibuf, 0);
1012 if (err->code == GOT_ERR_PRIVSEP_READ)
1017 evtimer_del(&client->tmo);
1019 switch (imsg.hdr.type) {
1020 case GOTD_IMSG_CAPABILITIES:
1021 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1022 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1023 "unexpected capabilities received");
1026 log_debug("receiving capabilities from uid %d",
1028 err = recv_capabilities(client, &imsg);
1030 case GOTD_IMSG_CAPABILITY:
1031 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1032 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1033 "unexpected capability received");
1036 err = recv_capability(client, &imsg);
1037 if (err || client->ncapabilities < client->ncapa_alloc)
1039 if (!client->is_writing) {
1040 client->state = GOTD_STATE_EXPECT_WANT;
1041 client->accept_flush_pkt = 1;
1042 log_debug("uid %d: expecting want-lines",
1044 } else if (client->is_writing) {
1045 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1046 client->accept_flush_pkt = 1;
1047 log_debug("uid %d: expecting ref-update-lines",
1050 fatalx("client %d is both reading and writing",
1053 case GOTD_IMSG_WANT:
1054 if (client->state != GOTD_STATE_EXPECT_WANT) {
1055 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1056 "unexpected want-line received");
1059 log_debug("received want-line from uid %d",
1061 err = ensure_client_is_reading(client);
1064 client->accept_flush_pkt = 1;
1065 err = forward_want(client, &imsg);
1067 case GOTD_IMSG_REF_UPDATE:
1068 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1070 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1071 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1072 "unexpected ref-update-line received");
1075 log_debug("received ref-update-line from uid %d",
1077 err = ensure_client_is_writing(client);
1080 err = forward_ref_update(client, &imsg);
1083 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1084 client->accept_flush_pkt = 1;
1086 case GOTD_IMSG_HAVE:
1087 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1088 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1089 "unexpected have-line received");
1092 log_debug("received have-line from uid %d",
1094 err = ensure_client_is_reading(client);
1097 err = forward_have(client, &imsg);
1100 client->accept_flush_pkt = 1;
1102 case GOTD_IMSG_FLUSH:
1103 if (client->state == GOTD_STATE_EXPECT_WANT ||
1104 client->state == GOTD_STATE_EXPECT_HAVE) {
1105 err = ensure_client_is_reading(client);
1108 } else if (client->state ==
1109 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1110 err = ensure_client_is_writing(client);
1113 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1114 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1115 "unexpected flush-pkt received");
1118 if (!client->accept_flush_pkt) {
1119 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1120 "unexpected flush-pkt received");
1125 * Accept just one flush packet at a time.
1126 * Future client state transitions will set this flag
1127 * again if another flush packet is expected.
1129 client->accept_flush_pkt = 0;
1131 log_debug("received flush-pkt from uid %d",
1133 if (client->state == GOTD_STATE_EXPECT_WANT) {
1134 client->state = GOTD_STATE_EXPECT_HAVE;
1135 log_debug("uid %d: expecting have-lines",
1137 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1138 client->state = GOTD_STATE_EXPECT_DONE;
1139 client->accept_flush_pkt = 1;
1140 log_debug("uid %d: expecting 'done'",
1142 } else if (client->state ==
1143 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1144 client->state = GOTD_STATE_EXPECT_PACKFILE;
1145 log_debug("uid %d: expecting packfile",
1147 err = recv_packfile(client);
1148 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1149 /* should not happen, see above */
1150 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1151 "unexpected client state");
1155 case GOTD_IMSG_DONE:
1156 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1157 client->state != GOTD_STATE_EXPECT_DONE) {
1158 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1159 "unexpected flush-pkt received");
1162 log_debug("received 'done' from uid %d", client->euid);
1163 err = ensure_client_is_reading(client);
1166 client->state = GOTD_STATE_DONE;
1167 client->accept_flush_pkt = 1;
1168 err = send_packfile(client);
1171 log_debug("unexpected imsg %d", imsg.hdr.type);
1172 err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 if (err->code != GOT_ERR_EOF ||
1181 client->state != GOTD_STATE_EXPECT_PACKFILE)
1182 disconnect_on_error(client, err);
1184 gotd_imsg_event_add(iev);
1185 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1189 static const struct got_error *
1190 list_refs_request(void)
1192 static const struct got_error *err;
1193 struct gotd_session_client *client = &gotd_session_client;
1194 struct gotd_imsgev *iev = &client->repo_child_iev;
1195 struct gotd_imsg_list_refs_internal ilref;
1198 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1199 return got_error(GOT_ERR_PRIVSEP_MSG);
1201 memset(&ilref, 0, sizeof(ilref));
1202 ilref.client_id = client->id;
1204 fd = dup(client->fd);
1206 return got_error_from_errno("dup");
1208 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1209 PROC_SESSION, fd, &ilref, sizeof(ilref)) == -1) {
1210 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1215 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1216 log_debug("uid %d: expecting capabilities", client->euid);
1220 static const struct got_error *
1221 recv_connect(struct imsg *imsg)
1223 struct gotd_session_client *client = &gotd_session_client;
1224 struct gotd_imsg_connect iconnect;
1227 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1228 return got_error(GOT_ERR_PRIVSEP_MSG);
1230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1231 if (datalen != sizeof(iconnect))
1232 return got_error(GOT_ERR_PRIVSEP_LEN);
1233 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1236 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1238 client->fd = imsg->fd;
1239 client->euid = iconnect.euid;
1240 client->egid = iconnect.egid;
1242 imsg_init(&client->iev.ibuf, client->fd);
1243 client->iev.handler = session_dispatch_listener;
1244 client->iev.events = EV_READ;
1245 client->iev.handler_arg = NULL;
1246 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1247 session_dispatch_listener, &client->iev);
1248 gotd_imsg_event_add(&client->iev);
1249 evtimer_set(&client->tmo, gotd_request_timeout, client);
1254 static const struct got_error *
1255 recv_repo_child(struct imsg *imsg)
1257 struct gotd_imsg_connect_repo_child ichild;
1258 struct gotd_session_client *client = &gotd_session_client;
1261 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1262 return got_error(GOT_ERR_PRIVSEP_MSG);
1264 /* We should already have received a pipe to the listener. */
1265 if (client->fd == -1)
1266 return got_error(GOT_ERR_PRIVSEP_MSG);
1268 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1269 if (datalen != sizeof(ichild))
1270 return got_error(GOT_ERR_PRIVSEP_LEN);
1272 memcpy(&ichild, imsg->data, sizeof(ichild));
1274 client->id = ichild.client_id;
1275 if (ichild.proc_id == PROC_REPO_WRITE)
1276 client->is_writing = 1;
1277 else if (ichild.proc_id == PROC_REPO_READ)
1278 client->is_writing = 0;
1280 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1281 "bad child process type");
1284 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1286 imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1287 client->repo_child_iev.handler = session_dispatch_repo_child;
1288 client->repo_child_iev.events = EV_READ;
1289 client->repo_child_iev.handler_arg = NULL;
1290 event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1291 EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1292 gotd_imsg_event_add(&client->repo_child_iev);
1294 /* The "recvfd" pledge promise is no longer needed. */
1295 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1302 session_dispatch(int fd, short event, void *arg)
1304 struct gotd_imsgev *iev = arg;
1305 struct imsgbuf *ibuf = &iev->ibuf;
1306 struct gotd_session_client *client = &gotd_session_client;
1311 if (event & EV_READ) {
1312 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1313 fatal("imsg_read error");
1315 /* Connection closed. */
1321 if (event & EV_WRITE) {
1322 n = msgbuf_write(&ibuf->w);
1323 if (n == -1 && errno != EAGAIN)
1324 fatal("msgbuf_write");
1326 /* Connection closed. */
1333 const struct got_error *err = NULL;
1334 uint32_t client_id = 0;
1335 int do_disconnect = 0, do_list_refs = 0;
1337 if ((n = imsg_get(ibuf, &imsg)) == -1)
1338 fatal("%s: imsg_get error", __func__);
1339 if (n == 0) /* No more messages. */
1342 switch (imsg.hdr.type) {
1343 case GOTD_IMSG_ERROR:
1345 err = gotd_imsg_recv_error(&client_id, &imsg);
1347 case GOTD_IMSG_CONNECT:
1348 err = recv_connect(&imsg);
1350 case GOTD_IMSG_DISCONNECT:
1353 case GOTD_IMSG_CONNECT_REPO_CHILD:
1354 err = recv_repo_child(&imsg);
1360 log_debug("unexpected imsg %d", imsg.hdr.type);
1365 if (do_disconnect) {
1367 disconnect_on_error(client, err);
1370 } else if (do_list_refs)
1371 err = list_refs_request();
1374 log_warnx("uid %d: %s", client->euid, err->msg);
1378 gotd_imsg_event_add(iev);
1380 /* This pipe is dead. Remove its event handler */
1381 event_del(&iev->ev);
1382 event_loopexit(NULL);
1387 session_main(const char *title, const char *repo_path,
1388 int *pack_fds, int *temp_fds, struct timeval *request_timeout)
1390 const struct got_error *err = NULL;
1391 struct event evsigint, evsigterm, evsighup, evsigusr1;
1393 gotd_session.title = title;
1394 gotd_session.pid = getpid();
1395 gotd_session.pack_fds = pack_fds;
1396 gotd_session.temp_fds = temp_fds;
1397 memcpy(&gotd_session.request_timeout, request_timeout,
1398 sizeof(gotd_session.request_timeout));
1400 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1403 if (!got_repo_is_bare(gotd_session.repo)) {
1404 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1405 "bare git repository required");
1409 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1411 signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1412 signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1413 signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1414 signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1415 signal(SIGPIPE, SIG_IGN);
1417 signal_add(&evsigint, NULL);
1418 signal_add(&evsigterm, NULL);
1419 signal_add(&evsighup, NULL);
1420 signal_add(&evsigusr1, NULL);
1422 gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1423 gotd_session_client.fd = -1;
1424 gotd_session_client.nref_updates = -1;
1425 gotd_session_client.delta_cache_fd = -1;
1426 gotd_session_client.accept_flush_pkt = 1;
1428 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1429 gotd_session.parent_iev.handler = session_dispatch;
1430 gotd_session.parent_iev.events = EV_READ;
1431 gotd_session.parent_iev.handler_arg = NULL;
1432 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1433 EV_READ, session_dispatch, &gotd_session.parent_iev);
1434 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1435 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION, -1, NULL, 0) == -1) {
1436 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1443 log_warnx("%s: %s", title, err->msg);
1444 gotd_session_shutdown();
1448 gotd_session_shutdown(void)
1450 log_debug("%s: shutting down", gotd_session.title);
1451 if (gotd_session.repo)
1452 got_repo_close(gotd_session.repo);
1453 got_repo_pack_fds_close(gotd_session.pack_fds);
1454 got_repo_temp_fds_close(gotd_session.temp_fds);