portable: release 0.90
[got-portable.git] / gotd / session.c
blob6bb52da0072acc8ca769bbe69c71259b848ccbaf
1 /*
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>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <imsg.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_repository.h"
36 #include "got_object.h"
37 #include "got_path.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_repository.h"
47 #include "got_lib_gitproto.h"
49 #include "gotd.h"
50 #include "log.h"
51 #include "session.h"
54 static struct gotd_session {
55 pid_t pid;
56 const char *title;
57 struct got_repository *repo;
58 int *pack_fds;
59 int *temp_fds;
60 struct gotd_imsgev parent_iev;
61 struct timeval request_timeout;
62 enum gotd_procid proc_id;
63 } gotd_session;
65 static struct gotd_session_client {
66 enum gotd_session_state state;
67 int is_writing;
68 struct gotd_client_capability *capabilities;
69 size_t ncapa_alloc;
70 size_t ncapabilities;
71 uint32_t id;
72 int fd;
73 int delta_cache_fd;
74 struct gotd_imsgev iev;
75 struct gotd_imsgev repo_child_iev;
76 struct event tmo;
77 uid_t euid;
78 gid_t egid;
79 char *packfile_path;
80 char *packidx_path;
81 int nref_updates;
82 int accept_flush_pkt;
83 } gotd_session_client;
85 void gotd_session_sighdlr(int sig, short event, void *arg);
86 static void gotd_session_shutdown(void);
88 static 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, gotd_session.proc_id, -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);
100 close(client->fd);
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();
118 static void
119 disconnect_on_error(struct gotd_session_client *client,
120 const struct got_error *err)
122 struct imsgbuf ibuf;
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, gotd_session.proc_id, err);
128 imsg_clear(&ibuf);
131 disconnect(client);
134 static void
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);
140 disconnect(client);
143 void
144 gotd_session_sighdlr(int sig, short event, void *arg)
147 * Normal signal handler rules don't apply because libevent
148 * decouples for us.
151 switch (sig) {
152 case SIGHUP:
153 log_info("%s: ignoring SIGHUP", __func__);
154 break;
155 case SIGUSR1:
156 log_info("%s: ignoring SIGUSR1", __func__);
157 break;
158 case SIGTERM:
159 case SIGINT:
160 gotd_session_shutdown();
161 /* NOTREACHED */
162 break;
163 default:
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;
172 size_t datalen;
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;
182 return NULL;
185 static const struct got_error *
186 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
188 struct gotd_imsg_packfile_install inst;
189 size_t datalen;
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;
199 return NULL;
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;
206 size_t datalen;
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;
216 return NULL;
219 static const struct got_error *
220 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
222 struct gotd_imsg_ref_update iref;
223 size_t datalen;
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;
233 return NULL;
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;
242 struct ibuf *wbuf;
243 size_t len;
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 gotd_session.proc_id, gotd_session.pid, len);
254 if (wbuf == NULL)
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");
262 wbuf->fd = -1;
263 imsg_close(&iev->ibuf, wbuf);
264 gotd_imsg_event_add(iev);
265 return NULL;
268 static void
269 send_refs_updated(struct gotd_session_client *client)
271 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 gotd_session.proc_id, -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,
279 const char *reason)
281 const struct got_error *ng_err;
282 struct gotd_imsg_ref_update_ng ing;
283 struct gotd_imsgev *iev = &client->iev;
284 struct ibuf *wbuf;
285 size_t len;
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 gotd_session.proc_id, gotd_session.pid, len);
299 if (wbuf == NULL)
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");
309 wbuf->fd = -1;
310 imsg_close(&iev->ibuf, wbuf);
311 gotd_imsg_event_add(iev);
312 return NULL;
315 static const struct got_error *
316 install_pack(struct gotd_session_client *client, const char *repo_path,
317 struct imsg *imsg)
319 const struct got_error *err = NULL;
320 struct gotd_imsg_packfile_install inst;
321 char hex[SHA1_DIGEST_STRING_LENGTH];
322 size_t datalen;
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");
344 goto done;
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");
350 goto done;
353 if (rename(client->packfile_path, packfile_path) == -1) {
354 err = got_error_from_errno3("rename", client->packfile_path,
355 packfile_path);
356 goto done;
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,
364 packidx_path);
365 goto done;
368 free(client->packidx_path);
369 client->packidx_path = NULL;
370 done:
371 free(packfile_path);
372 free(packidx_path);
373 return err;
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;
380 size_t datalen;
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;
394 return NULL;
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;
409 size_t datalen;
410 int locked = 0;
411 char hex1[SHA1_DIGEST_STRING_LENGTH];
412 char hex2[SHA1_DIGEST_STRING_LENGTH];
414 log_debug("update-ref from uid %d", client->euid);
416 if (client->nref_updates <= 0)
417 return got_error(GOT_ERR_PRIVSEP_MSG);
419 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
420 if (datalen < sizeof(iref))
421 return got_error(GOT_ERR_PRIVSEP_LEN);
422 memcpy(&iref, imsg->data, sizeof(iref));
423 if (datalen != sizeof(iref) + iref.name_len)
424 return got_error(GOT_ERR_PRIVSEP_LEN);
425 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
426 if (refname == NULL)
427 return got_error_from_errno("strndup");
429 log_debug("updating ref %s for uid %d", refname, client->euid);
431 err = got_repo_open(&repo, repo_path, NULL, NULL);
432 if (err)
433 goto done;
435 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
436 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
437 err = got_object_open(&obj, repo,
438 iref.delete_ref ? &old_id : &new_id);
439 if (err)
440 goto done;
442 if (iref.ref_is_new) {
443 err = got_ref_open(&ref, repo, refname, 0);
444 if (err) {
445 if (err->code != GOT_ERR_NOT_REF)
446 goto done;
447 err = got_ref_alloc(&ref, refname, &new_id);
448 if (err)
449 goto done;
450 err = got_ref_write(ref, repo); /* will lock/unlock */
451 if (err)
452 goto done;
453 } else {
454 err = got_ref_resolve(&id, repo, ref);
455 if (err)
456 goto done;
457 got_object_id_hex(&new_id, hex1, sizeof(hex1));
458 got_object_id_hex(id, hex2, sizeof(hex2));
459 err = got_error_fmt(GOT_ERR_REF_BUSY,
460 "Addition %s: %s failed; %s: %s has been "
461 "created by someone else while transaction "
462 "was in progress",
463 got_ref_get_name(ref), hex1,
464 got_ref_get_name(ref), hex2);
465 goto done;
467 } else if (iref.delete_ref) {
468 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
469 if (err)
470 goto done;
471 locked = 1;
473 err = got_ref_resolve(&id, repo, ref);
474 if (err)
475 goto done;
477 if (got_object_id_cmp(id, &old_id) != 0) {
478 got_object_id_hex(&old_id, hex1, sizeof(hex1));
479 got_object_id_hex(id, hex2, sizeof(hex2));
480 err = got_error_fmt(GOT_ERR_REF_BUSY,
481 "Deletion %s: %s failed; %s: %s has been "
482 "created by someone else while transaction "
483 "was in progress",
484 got_ref_get_name(ref), hex1,
485 got_ref_get_name(ref), hex2);
486 goto done;
489 err = got_ref_delete(ref, repo);
490 if (err)
491 goto done;
493 free(id);
494 id = NULL;
495 } else {
496 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
497 if (err)
498 goto done;
499 locked = 1;
501 err = got_ref_resolve(&id, repo, ref);
502 if (err)
503 goto done;
505 if (got_object_id_cmp(id, &old_id) != 0) {
506 got_object_id_hex(&old_id, hex1, sizeof(hex1));
507 got_object_id_hex(id, hex2, sizeof(hex2));
508 err = got_error_fmt(GOT_ERR_REF_BUSY,
509 "Update %s: %s failed; %s: %s has been "
510 "created by someone else while transaction "
511 "was in progress",
512 got_ref_get_name(ref), hex1,
513 got_ref_get_name(ref), hex2);
514 goto done;
517 if (got_object_id_cmp(&new_id, &old_id) != 0) {
518 err = got_ref_change_ref(ref, &new_id);
519 if (err)
520 goto done;
522 err = got_ref_write(ref, repo);
523 if (err)
524 goto done;
527 free(id);
528 id = NULL;
530 done:
531 if (err) {
532 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
533 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
534 "could not acquire exclusive file lock for %s",
535 refname);
537 send_ref_update_ng(client, &iref, refname, err->msg);
538 } else
539 send_ref_update_ok(client, &iref, refname);
541 if (client->nref_updates > 0) {
542 client->nref_updates--;
543 if (client->nref_updates == 0) {
544 send_refs_updated(client);
545 *shut = 1;
549 if (locked) {
550 const struct got_error *unlock_err;
551 unlock_err = got_ref_unlock(ref);
552 if (unlock_err && err == NULL)
553 err = unlock_err;
555 if (ref)
556 got_ref_close(ref);
557 if (obj)
558 got_object_close(obj);
559 if (repo)
560 got_repo_close(repo);
561 free(refname);
562 free(id);
563 return err;
566 static void
567 session_dispatch_repo_child(int fd, short event, void *arg)
569 struct gotd_imsgev *iev = arg;
570 struct imsgbuf *ibuf = &iev->ibuf;
571 struct gotd_session_client *client = &gotd_session_client;
572 ssize_t n;
573 int shut = 0;
574 struct imsg imsg;
576 if (event & EV_READ) {
577 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
578 fatal("imsg_read error");
579 if (n == 0) {
580 /* Connection closed. */
581 shut = 1;
582 goto done;
586 if (event & EV_WRITE) {
587 n = msgbuf_write(&ibuf->w);
588 if (n == -1 && errno != EAGAIN)
589 fatal("msgbuf_write");
590 if (n == 0) {
591 /* Connection closed. */
592 shut = 1;
593 goto done;
597 for (;;) {
598 const struct got_error *err = NULL;
599 uint32_t client_id = 0;
600 int do_disconnect = 0;
601 int do_ref_updates = 0, do_ref_update = 0;
602 int do_packfile_install = 0;
604 if ((n = imsg_get(ibuf, &imsg)) == -1)
605 fatal("%s: imsg_get error", __func__);
606 if (n == 0) /* No more messages. */
607 break;
609 switch (imsg.hdr.type) {
610 case GOTD_IMSG_ERROR:
611 do_disconnect = 1;
612 err = gotd_imsg_recv_error(&client_id, &imsg);
613 break;
614 case GOTD_IMSG_PACKFILE_DONE:
615 do_disconnect = 1;
616 err = recv_packfile_done(&client_id, &imsg);
617 break;
618 case GOTD_IMSG_PACKFILE_INSTALL:
619 err = recv_packfile_install(&client_id, &imsg);
620 if (err == NULL)
621 do_packfile_install = 1;
622 break;
623 case GOTD_IMSG_REF_UPDATES_START:
624 err = recv_ref_updates_start(&client_id, &imsg);
625 if (err == NULL)
626 do_ref_updates = 1;
627 break;
628 case GOTD_IMSG_REF_UPDATE:
629 err = recv_ref_update(&client_id, &imsg);
630 if (err == NULL)
631 do_ref_update = 1;
632 break;
633 default:
634 log_debug("unexpected imsg %d", imsg.hdr.type);
635 break;
638 if (do_disconnect) {
639 if (err)
640 disconnect_on_error(client, err);
641 else
642 disconnect(client);
643 } else {
644 if (do_packfile_install)
645 err = install_pack(client,
646 gotd_session.repo->path, &imsg);
647 else if (do_ref_updates)
648 err = begin_ref_updates(client, &imsg);
649 else if (do_ref_update)
650 err = update_ref(&shut, client,
651 gotd_session.repo->path, &imsg);
652 if (err)
653 log_warnx("uid %d: %s", client->euid, err->msg);
655 imsg_free(&imsg);
657 done:
658 if (!shut) {
659 gotd_imsg_event_add(iev);
660 } else {
661 /* This pipe is dead. Remove its event handler */
662 event_del(&iev->ev);
663 event_loopexit(NULL);
667 static const struct got_error *
668 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
670 struct gotd_imsg_capabilities icapas;
671 size_t datalen;
673 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
674 if (datalen != sizeof(icapas))
675 return got_error(GOT_ERR_PRIVSEP_LEN);
676 memcpy(&icapas, imsg->data, sizeof(icapas));
678 client->ncapa_alloc = icapas.ncapabilities;
679 client->capabilities = calloc(client->ncapa_alloc,
680 sizeof(*client->capabilities));
681 if (client->capabilities == NULL) {
682 client->ncapa_alloc = 0;
683 return got_error_from_errno("calloc");
686 log_debug("expecting %zu capabilities from uid %d",
687 client->ncapa_alloc, client->euid);
688 return NULL;
691 static const struct got_error *
692 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
694 struct gotd_imsg_capability icapa;
695 struct gotd_client_capability *capa;
696 size_t datalen;
697 char *key, *value = NULL;
699 if (client->capabilities == NULL ||
700 client->ncapabilities >= client->ncapa_alloc) {
701 return got_error_msg(GOT_ERR_BAD_REQUEST,
702 "unexpected capability received");
705 memset(&icapa, 0, sizeof(icapa));
707 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
708 if (datalen < sizeof(icapa))
709 return got_error(GOT_ERR_PRIVSEP_LEN);
710 memcpy(&icapa, imsg->data, sizeof(icapa));
712 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
713 return got_error(GOT_ERR_PRIVSEP_LEN);
715 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
716 if (key == NULL)
717 return got_error_from_errno("strndup");
718 if (icapa.value_len > 0) {
719 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
720 icapa.value_len);
721 if (value == NULL) {
722 free(key);
723 return got_error_from_errno("strndup");
727 capa = &client->capabilities[client->ncapabilities++];
728 capa->key = key;
729 capa->value = value;
731 if (value)
732 log_debug("uid %d: capability %s=%s", client->euid, key, value);
733 else
734 log_debug("uid %d: capability %s", client->euid, key);
736 return NULL;
739 static const struct got_error *
740 ensure_client_is_reading(struct gotd_session_client *client)
742 if (client->is_writing) {
743 return got_error_fmt(GOT_ERR_BAD_PACKET,
744 "uid %d made a read-request but is not reading from "
745 "a repository", client->euid);
748 return NULL;
751 static const struct got_error *
752 ensure_client_is_writing(struct gotd_session_client *client)
754 if (!client->is_writing) {
755 return got_error_fmt(GOT_ERR_BAD_PACKET,
756 "uid %d made a write-request but is not writing to "
757 "a repository", client->euid);
760 return NULL;
763 static const struct got_error *
764 forward_want(struct gotd_session_client *client, struct imsg *imsg)
766 struct gotd_imsg_want ireq;
767 struct gotd_imsg_want iwant;
768 size_t datalen;
770 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
771 if (datalen != sizeof(ireq))
772 return got_error(GOT_ERR_PRIVSEP_LEN);
774 memcpy(&ireq, imsg->data, datalen);
776 memset(&iwant, 0, sizeof(iwant));
777 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
778 iwant.client_id = client->id;
780 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
781 gotd_session.proc_id, -1, &iwant, sizeof(iwant)) == -1)
782 return got_error_from_errno("imsg compose WANT");
784 return NULL;
787 static const struct got_error *
788 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
790 const struct got_error *err = NULL;
791 struct gotd_imsg_ref_update ireq;
792 struct gotd_imsg_ref_update *iref = NULL;
793 size_t datalen;
795 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
796 if (datalen < sizeof(ireq))
797 return got_error(GOT_ERR_PRIVSEP_LEN);
798 memcpy(&ireq, imsg->data, sizeof(ireq));
799 if (datalen != sizeof(ireq) + ireq.name_len)
800 return got_error(GOT_ERR_PRIVSEP_LEN);
802 iref = malloc(datalen);
803 if (iref == NULL)
804 return got_error_from_errno("malloc");
805 memcpy(iref, imsg->data, datalen);
807 iref->client_id = client->id;
808 if (gotd_imsg_compose_event(&client->repo_child_iev,
809 GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
810 iref, datalen) == -1)
811 err = got_error_from_errno("imsg compose REF_UPDATE");
812 free(iref);
813 return err;
816 static const struct got_error *
817 forward_have(struct gotd_session_client *client, struct imsg *imsg)
819 struct gotd_imsg_have ireq;
820 struct gotd_imsg_have ihave;
821 size_t datalen;
823 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
824 if (datalen != sizeof(ireq))
825 return got_error(GOT_ERR_PRIVSEP_LEN);
827 memcpy(&ireq, imsg->data, datalen);
829 memset(&ihave, 0, sizeof(ihave));
830 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
831 ihave.client_id = client->id;
833 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
834 gotd_session.proc_id, -1, &ihave, sizeof(ihave)) == -1)
835 return got_error_from_errno("imsg compose HAVE");
837 return NULL;
840 static int
841 client_has_capability(struct gotd_session_client *client, const char *capastr)
843 struct gotd_client_capability *capa;
844 size_t i;
846 if (client->ncapabilities == 0)
847 return 0;
849 for (i = 0; i < client->ncapabilities; i++) {
850 capa = &client->capabilities[i];
851 if (strcmp(capa->key, capastr) == 0)
852 return 1;
855 return 0;
858 static const struct got_error *
859 recv_packfile(struct gotd_session_client *client)
861 const struct got_error *err = NULL;
862 struct gotd_imsg_recv_packfile ipack;
863 struct gotd_imsg_packfile_pipe ipipe;
864 struct gotd_imsg_packidx_file ifile;
865 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
866 int packfd = -1, idxfd = -1;
867 int pipe[2] = { -1, -1 };
869 if (client->packfile_path) {
870 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
871 "uid %d already has a pack file", client->euid);
874 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
875 return got_error_from_errno("socketpair");
877 memset(&ipipe, 0, sizeof(ipipe));
878 ipipe.client_id = client->id;
880 /* Send pack pipe end 0 to repo child process. */
881 if (gotd_imsg_compose_event(&client->repo_child_iev,
882 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
883 &ipipe, sizeof(ipipe)) == -1) {
884 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
885 pipe[0] = -1;
886 goto done;
888 pipe[0] = -1;
890 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
891 if (gotd_imsg_compose_event(&client->iev,
892 GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
893 NULL, 0) == -1)
894 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
895 pipe[1] = -1;
897 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
898 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
899 client->euid) == -1) {
900 err = got_error_from_errno("asprintf");
901 goto done;
904 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
905 if (err)
906 goto done;
907 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
908 err = got_error_from_errno2("fchmod", pack_path);
909 goto done;
912 free(basepath);
913 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
914 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
915 client->euid) == -1) {
916 err = got_error_from_errno("asprintf");
917 basepath = NULL;
918 goto done;
920 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
921 if (err)
922 goto done;
923 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
924 err = got_error_from_errno2("fchmod", idx_path);
925 goto done;
928 memset(&ifile, 0, sizeof(ifile));
929 ifile.client_id = client->id;
930 if (gotd_imsg_compose_event(&client->repo_child_iev,
931 GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
932 idxfd, &ifile, sizeof(ifile)) == -1) {
933 err = got_error_from_errno("imsg compose PACKIDX_FILE");
934 idxfd = -1;
935 goto done;
937 idxfd = -1;
939 memset(&ipack, 0, sizeof(ipack));
940 ipack.client_id = client->id;
941 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
942 ipack.report_status = 1;
944 if (gotd_imsg_compose_event(&client->repo_child_iev,
945 GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
946 &ipack, sizeof(ipack)) == -1) {
947 err = got_error_from_errno("imsg compose RECV_PACKFILE");
948 packfd = -1;
949 goto done;
951 packfd = -1;
953 done:
954 free(basepath);
955 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
956 err = got_error_from_errno("close");
957 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
958 err = got_error_from_errno("close");
959 if (packfd != -1 && close(packfd) == -1 && err == NULL)
960 err = got_error_from_errno("close");
961 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
962 err = got_error_from_errno("close");
963 if (err) {
964 free(pack_path);
965 free(idx_path);
966 } else {
967 client->packfile_path = pack_path;
968 client->packidx_path = idx_path;
970 return err;
973 static const struct got_error *
974 send_packfile(struct gotd_session_client *client)
976 const struct got_error *err = NULL;
977 struct gotd_imsg_send_packfile ipack;
978 struct gotd_imsg_packfile_pipe ipipe;
979 int pipe[2];
981 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
982 return got_error_from_errno("socketpair");
984 memset(&ipack, 0, sizeof(ipack));
985 memset(&ipipe, 0, sizeof(ipipe));
987 ipack.client_id = client->id;
988 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
989 ipack.report_progress = 1;
991 client->delta_cache_fd = got_opentempfd();
992 if (client->delta_cache_fd == -1)
993 return got_error_from_errno("got_opentempfd");
995 if (gotd_imsg_compose_event(&client->repo_child_iev,
996 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
997 &ipack, sizeof(ipack)) == -1) {
998 err = got_error_from_errno("imsg compose SEND_PACKFILE");
999 close(pipe[0]);
1000 close(pipe[1]);
1001 return err;
1004 ipipe.client_id = client->id;
1006 /* Send pack pipe end 0 to repo child process. */
1007 if (gotd_imsg_compose_event(&client->repo_child_iev,
1008 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1009 pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1010 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1011 close(pipe[1]);
1012 return err;
1015 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1016 if (gotd_imsg_compose_event(&client->iev,
1017 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1018 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1020 return err;
1023 static void
1024 session_dispatch_client(int fd, short events, void *arg)
1026 struct gotd_imsgev *iev = arg;
1027 struct imsgbuf *ibuf = &iev->ibuf;
1028 struct gotd_session_client *client = &gotd_session_client;
1029 const struct got_error *err = NULL;
1030 struct imsg imsg;
1031 ssize_t n;
1033 if (events & EV_WRITE) {
1034 while (ibuf->w.queued) {
1035 n = msgbuf_write(&ibuf->w);
1036 if (n == -1 && errno == EPIPE) {
1038 * The client has closed its socket.
1039 * This can happen when Git clients are
1040 * done sending pack file data.
1042 msgbuf_clear(&ibuf->w);
1043 continue;
1044 } else if (n == -1 && errno != EAGAIN) {
1045 err = got_error_from_errno("imsg_flush");
1046 disconnect_on_error(client, err);
1047 return;
1049 if (n == 0) {
1050 /* Connection closed. */
1051 err = got_error(GOT_ERR_EOF);
1052 disconnect_on_error(client, err);
1053 return;
1058 if ((events & EV_READ) == 0)
1059 return;
1061 memset(&imsg, 0, sizeof(imsg));
1063 while (err == NULL) {
1064 err = gotd_imsg_recv(&imsg, ibuf, 0);
1065 if (err) {
1066 if (err->code == GOT_ERR_PRIVSEP_READ)
1067 err = NULL;
1068 else if (err->code == GOT_ERR_EOF &&
1069 client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1071 * The client has closed its socket before
1072 * sending its capability announcement.
1073 * This can happen when Git clients have
1074 * no ref-updates to send.
1076 disconnect_on_error(client, err);
1077 return;
1079 break;
1082 evtimer_del(&client->tmo);
1084 switch (imsg.hdr.type) {
1085 case GOTD_IMSG_CAPABILITIES:
1086 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1087 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1088 "unexpected capabilities received");
1089 break;
1091 log_debug("receiving capabilities from uid %d",
1092 client->euid);
1093 err = recv_capabilities(client, &imsg);
1094 break;
1095 case GOTD_IMSG_CAPABILITY:
1096 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1097 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1098 "unexpected capability received");
1099 break;
1101 err = recv_capability(client, &imsg);
1102 if (err || client->ncapabilities < client->ncapa_alloc)
1103 break;
1104 if (!client->is_writing) {
1105 client->state = GOTD_STATE_EXPECT_WANT;
1106 client->accept_flush_pkt = 1;
1107 log_debug("uid %d: expecting want-lines",
1108 client->euid);
1109 } else if (client->is_writing) {
1110 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1111 client->accept_flush_pkt = 1;
1112 log_debug("uid %d: expecting ref-update-lines",
1113 client->euid);
1114 } else
1115 fatalx("client %d is both reading and writing",
1116 client->euid);
1117 break;
1118 case GOTD_IMSG_WANT:
1119 if (client->state != GOTD_STATE_EXPECT_WANT) {
1120 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1121 "unexpected want-line received");
1122 break;
1124 log_debug("received want-line from uid %d",
1125 client->euid);
1126 err = ensure_client_is_reading(client);
1127 if (err)
1128 break;
1129 client->accept_flush_pkt = 1;
1130 err = forward_want(client, &imsg);
1131 break;
1132 case GOTD_IMSG_REF_UPDATE:
1133 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1134 client->state !=
1135 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1136 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1137 "unexpected ref-update-line received");
1138 break;
1140 log_debug("received ref-update-line from uid %d",
1141 client->euid);
1142 err = ensure_client_is_writing(client);
1143 if (err)
1144 break;
1145 err = forward_ref_update(client, &imsg);
1146 if (err)
1147 break;
1148 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1149 client->accept_flush_pkt = 1;
1150 break;
1151 case GOTD_IMSG_HAVE:
1152 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1153 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1154 "unexpected have-line received");
1155 break;
1157 log_debug("received have-line from uid %d",
1158 client->euid);
1159 err = ensure_client_is_reading(client);
1160 if (err)
1161 break;
1162 err = forward_have(client, &imsg);
1163 if (err)
1164 break;
1165 client->accept_flush_pkt = 1;
1166 break;
1167 case GOTD_IMSG_FLUSH:
1168 if (client->state == GOTD_STATE_EXPECT_WANT ||
1169 client->state == GOTD_STATE_EXPECT_HAVE) {
1170 err = ensure_client_is_reading(client);
1171 if (err)
1172 break;
1173 } else if (client->state ==
1174 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1175 err = ensure_client_is_writing(client);
1176 if (err)
1177 break;
1178 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1179 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1180 "unexpected flush-pkt received");
1181 break;
1183 if (!client->accept_flush_pkt) {
1184 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1185 "unexpected flush-pkt received");
1186 break;
1190 * Accept just one flush packet at a time.
1191 * Future client state transitions will set this flag
1192 * again if another flush packet is expected.
1194 client->accept_flush_pkt = 0;
1196 log_debug("received flush-pkt from uid %d",
1197 client->euid);
1198 if (client->state == GOTD_STATE_EXPECT_WANT) {
1199 client->state = GOTD_STATE_EXPECT_HAVE;
1200 log_debug("uid %d: expecting have-lines",
1201 client->euid);
1202 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1203 client->state = GOTD_STATE_EXPECT_DONE;
1204 client->accept_flush_pkt = 1;
1205 log_debug("uid %d: expecting 'done'",
1206 client->euid);
1207 } else if (client->state ==
1208 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1209 client->state = GOTD_STATE_EXPECT_PACKFILE;
1210 log_debug("uid %d: expecting packfile",
1211 client->euid);
1212 err = recv_packfile(client);
1213 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1214 /* should not happen, see above */
1215 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1216 "unexpected client state");
1217 break;
1219 break;
1220 case GOTD_IMSG_DONE:
1221 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1222 client->state != GOTD_STATE_EXPECT_DONE) {
1223 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1224 "unexpected flush-pkt received");
1225 break;
1227 log_debug("received 'done' from uid %d", client->euid);
1228 err = ensure_client_is_reading(client);
1229 if (err)
1230 break;
1231 client->state = GOTD_STATE_DONE;
1232 client->accept_flush_pkt = 1;
1233 err = send_packfile(client);
1234 break;
1235 default:
1236 log_debug("unexpected imsg %d", imsg.hdr.type);
1237 err = got_error(GOT_ERR_PRIVSEP_MSG);
1238 break;
1241 imsg_free(&imsg);
1244 if (err) {
1245 if (err->code != GOT_ERR_EOF ||
1246 client->state != GOTD_STATE_EXPECT_PACKFILE)
1247 disconnect_on_error(client, err);
1248 } else {
1249 gotd_imsg_event_add(iev);
1250 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1254 static const struct got_error *
1255 list_refs_request(void)
1257 static const struct got_error *err;
1258 struct gotd_session_client *client = &gotd_session_client;
1259 struct gotd_imsgev *iev = &client->repo_child_iev;
1260 struct gotd_imsg_list_refs_internal ilref;
1261 int fd;
1263 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1264 return got_error(GOT_ERR_PRIVSEP_MSG);
1266 memset(&ilref, 0, sizeof(ilref));
1267 ilref.client_id = client->id;
1269 fd = dup(client->fd);
1270 if (fd == -1)
1271 return got_error_from_errno("dup");
1273 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1274 gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1275 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1276 close(fd);
1277 return err;
1280 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1281 log_debug("uid %d: expecting capabilities", client->euid);
1282 return NULL;
1285 static const struct got_error *
1286 recv_connect(struct imsg *imsg)
1288 struct gotd_session_client *client = &gotd_session_client;
1289 struct gotd_imsg_connect iconnect;
1290 size_t datalen;
1292 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1293 return got_error(GOT_ERR_PRIVSEP_MSG);
1295 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1296 if (datalen != sizeof(iconnect))
1297 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1300 if (imsg->fd == -1)
1301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1303 client->fd = imsg->fd;
1304 client->euid = iconnect.euid;
1305 client->egid = iconnect.egid;
1307 imsg_init(&client->iev.ibuf, client->fd);
1308 client->iev.handler = session_dispatch_client;
1309 client->iev.events = EV_READ;
1310 client->iev.handler_arg = NULL;
1311 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1312 session_dispatch_client, &client->iev);
1313 gotd_imsg_event_add(&client->iev);
1314 evtimer_set(&client->tmo, gotd_request_timeout, client);
1316 return NULL;
1319 static const struct got_error *
1320 recv_repo_child(struct imsg *imsg)
1322 struct gotd_imsg_connect_repo_child ichild;
1323 struct gotd_session_client *client = &gotd_session_client;
1324 size_t datalen;
1326 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1327 return got_error(GOT_ERR_PRIVSEP_MSG);
1329 /* We should already have received a pipe to the listener. */
1330 if (client->fd == -1)
1331 return got_error(GOT_ERR_PRIVSEP_MSG);
1333 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1334 if (datalen != sizeof(ichild))
1335 return got_error(GOT_ERR_PRIVSEP_LEN);
1337 memcpy(&ichild, imsg->data, sizeof(ichild));
1339 client->id = ichild.client_id;
1340 if (ichild.proc_id == PROC_REPO_WRITE)
1341 client->is_writing = 1;
1342 else if (ichild.proc_id == PROC_REPO_READ)
1343 client->is_writing = 0;
1344 else
1345 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1346 "bad child process type");
1348 if (imsg->fd == -1)
1349 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1351 imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1352 client->repo_child_iev.handler = session_dispatch_repo_child;
1353 client->repo_child_iev.events = EV_READ;
1354 client->repo_child_iev.handler_arg = NULL;
1355 event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1356 EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1357 gotd_imsg_event_add(&client->repo_child_iev);
1359 /* The "recvfd" pledge promise is no longer needed. */
1360 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1361 fatal("pledge");
1363 return NULL;
1366 static void
1367 session_dispatch(int fd, short event, void *arg)
1369 struct gotd_imsgev *iev = arg;
1370 struct imsgbuf *ibuf = &iev->ibuf;
1371 struct gotd_session_client *client = &gotd_session_client;
1372 ssize_t n;
1373 int shut = 0;
1374 struct imsg imsg;
1376 if (event & EV_READ) {
1377 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1378 fatal("imsg_read error");
1379 if (n == 0) {
1380 /* Connection closed. */
1381 shut = 1;
1382 goto done;
1386 if (event & EV_WRITE) {
1387 n = msgbuf_write(&ibuf->w);
1388 if (n == -1 && errno != EAGAIN)
1389 fatal("msgbuf_write");
1390 if (n == 0) {
1391 /* Connection closed. */
1392 shut = 1;
1393 goto done;
1397 for (;;) {
1398 const struct got_error *err = NULL;
1399 uint32_t client_id = 0;
1400 int do_disconnect = 0, do_list_refs = 0;
1402 if ((n = imsg_get(ibuf, &imsg)) == -1)
1403 fatal("%s: imsg_get error", __func__);
1404 if (n == 0) /* No more messages. */
1405 break;
1407 switch (imsg.hdr.type) {
1408 case GOTD_IMSG_ERROR:
1409 do_disconnect = 1;
1410 err = gotd_imsg_recv_error(&client_id, &imsg);
1411 break;
1412 case GOTD_IMSG_CONNECT:
1413 err = recv_connect(&imsg);
1414 break;
1415 case GOTD_IMSG_DISCONNECT:
1416 do_disconnect = 1;
1417 break;
1418 case GOTD_IMSG_CONNECT_REPO_CHILD:
1419 err = recv_repo_child(&imsg);
1420 if (err)
1421 break;
1422 do_list_refs = 1;
1423 break;
1424 default:
1425 log_debug("unexpected imsg %d", imsg.hdr.type);
1426 break;
1428 imsg_free(&imsg);
1430 if (do_disconnect) {
1431 if (err)
1432 disconnect_on_error(client, err);
1433 else
1434 disconnect(client);
1435 } else if (do_list_refs)
1436 err = list_refs_request();
1438 if (err)
1439 log_warnx("uid %d: %s", client->euid, err->msg);
1441 done:
1442 if (!shut) {
1443 gotd_imsg_event_add(iev);
1444 } else {
1445 /* This pipe is dead. Remove its event handler */
1446 event_del(&iev->ev);
1447 event_loopexit(NULL);
1451 void
1452 session_main(const char *title, const char *repo_path,
1453 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1454 enum gotd_procid proc_id)
1456 const struct got_error *err = NULL;
1457 struct event evsigint, evsigterm, evsighup, evsigusr1;
1459 gotd_session.title = title;
1460 gotd_session.pid = getpid();
1461 gotd_session.pack_fds = pack_fds;
1462 gotd_session.temp_fds = temp_fds;
1463 memcpy(&gotd_session.request_timeout, request_timeout,
1464 sizeof(gotd_session.request_timeout));
1465 gotd_session.proc_id = proc_id;
1467 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1468 if (err)
1469 goto done;
1470 if (!got_repo_is_bare(gotd_session.repo)) {
1471 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1472 "bare git repository required");
1473 goto done;
1476 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1478 signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1479 signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1480 signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1481 signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1482 signal(SIGPIPE, SIG_IGN);
1484 signal_add(&evsigint, NULL);
1485 signal_add(&evsigterm, NULL);
1486 signal_add(&evsighup, NULL);
1487 signal_add(&evsigusr1, NULL);
1489 gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1490 gotd_session_client.fd = -1;
1491 gotd_session_client.nref_updates = -1;
1492 gotd_session_client.delta_cache_fd = -1;
1493 gotd_session_client.accept_flush_pkt = 1;
1495 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1496 gotd_session.parent_iev.handler = session_dispatch;
1497 gotd_session.parent_iev.events = EV_READ;
1498 gotd_session.parent_iev.handler_arg = NULL;
1499 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1500 EV_READ, session_dispatch, &gotd_session.parent_iev);
1501 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1502 GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1503 -1, NULL, 0) == -1) {
1504 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1505 goto done;
1508 event_dispatch();
1509 done:
1510 if (err)
1511 log_warnx("%s: %s", title, err->msg);
1512 gotd_session_shutdown();
1515 void
1516 gotd_session_shutdown(void)
1518 log_debug("shutting down");
1519 if (gotd_session.repo)
1520 got_repo_close(gotd_session.repo);
1521 got_repo_pack_fds_close(gotd_session.pack_fds);
1522 got_repo_temp_fds_close(gotd_session.temp_fds);
1523 exit(0);