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 } gotd_session;
67 static struct gotd_session_client {
68 enum gotd_session_state state;
69 int is_writing;
70 struct gotd_client_capability *capabilities;
71 size_t ncapa_alloc;
72 size_t ncapabilities;
73 uint32_t id;
74 int fd;
75 int delta_cache_fd;
76 struct gotd_imsgev iev;
77 struct gotd_imsgev repo_child_iev;
78 struct event tmo;
79 uid_t euid;
80 gid_t egid;
81 char *packfile_path;
82 char *packidx_path;
83 int nref_updates;
84 int accept_flush_pkt;
85 } gotd_session_client;
87 void gotd_session_sighdlr(int sig, short event, void *arg);
88 static void gotd_session_shutdown(void);
90 static void
91 disconnect(struct gotd_session_client *client)
92 {
93 log_debug("uid %d: disconnecting", client->euid);
95 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
96 GOTD_IMSG_DISCONNECT, PROC_SESSION, -1, NULL, 0) == -1)
97 log_warn("imsg compose DISCONNECT");
99 imsg_clear(&client->repo_child_iev.ibuf);
100 event_del(&client->repo_child_iev.ev);
101 evtimer_del(&client->tmo);
102 close(client->fd);
103 if (client->delta_cache_fd != -1)
104 close(client->delta_cache_fd);
105 if (client->packfile_path) {
106 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
107 log_warn("unlink %s: ", client->packfile_path);
108 free(client->packfile_path);
110 if (client->packidx_path) {
111 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
112 log_warn("unlink %s: ", client->packidx_path);
113 free(client->packidx_path);
115 free(client->capabilities);
117 gotd_session_shutdown();
120 static void
121 disconnect_on_error(struct gotd_session_client *client,
122 const struct got_error *err)
124 struct imsgbuf ibuf;
126 log_warnx("uid %d: %s", client->euid, err->msg);
127 if (err->code != GOT_ERR_EOF) {
128 imsg_init(&ibuf, client->fd);
129 gotd_imsg_send_error(&ibuf, 0, PROC_SESSION, err);
130 imsg_clear(&ibuf);
133 disconnect(client);
136 static void
137 gotd_request_timeout(int fd, short events, void *arg)
139 struct gotd_session_client *client = arg;
141 log_debug("disconnecting uid %d due to timeout", client->euid);
142 disconnect(client);
145 void
146 gotd_session_sighdlr(int sig, short event, void *arg)
148 /*
149 * Normal signal handler rules don't apply because libevent
150 * decouples for us.
151 */
153 switch (sig) {
154 case SIGHUP:
155 log_info("%s: ignoring SIGHUP", __func__);
156 break;
157 case SIGUSR1:
158 log_info("%s: ignoring SIGUSR1", __func__);
159 break;
160 case SIGTERM:
161 case SIGINT:
162 gotd_session_shutdown();
163 /* NOTREACHED */
164 break;
165 default:
166 fatalx("unexpected signal");
170 static const struct got_error *
171 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
173 struct gotd_imsg_packfile_done idone;
174 size_t datalen;
176 log_debug("packfile-done received");
178 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
179 if (datalen != sizeof(idone))
180 return got_error(GOT_ERR_PRIVSEP_LEN);
181 memcpy(&idone, imsg->data, sizeof(idone));
183 *client_id = idone.client_id;
184 return NULL;
187 static const struct got_error *
188 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
190 struct gotd_imsg_packfile_install inst;
191 size_t datalen;
193 log_debug("packfile-install received");
195 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
196 if (datalen != sizeof(inst))
197 return got_error(GOT_ERR_PRIVSEP_LEN);
198 memcpy(&inst, imsg->data, sizeof(inst));
200 *client_id = inst.client_id;
201 return NULL;
204 static const struct got_error *
205 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
207 struct gotd_imsg_ref_updates_start istart;
208 size_t datalen;
210 log_debug("ref-updates-start received");
212 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
213 if (datalen != sizeof(istart))
214 return got_error(GOT_ERR_PRIVSEP_LEN);
215 memcpy(&istart, imsg->data, sizeof(istart));
217 *client_id = istart.client_id;
218 return NULL;
221 static const struct got_error *
222 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
224 struct gotd_imsg_ref_update iref;
225 size_t datalen;
227 log_debug("ref-update received");
229 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 if (datalen < sizeof(iref))
231 return got_error(GOT_ERR_PRIVSEP_LEN);
232 memcpy(&iref, imsg->data, sizeof(iref));
234 *client_id = iref.client_id;
235 return NULL;
238 static const struct got_error *
239 send_ref_update_ok(struct gotd_session_client *client,
240 struct gotd_imsg_ref_update *iref, const char *refname)
242 struct gotd_imsg_ref_update_ok iok;
243 struct gotd_imsgev *iev = &client->iev;
244 struct ibuf *wbuf;
245 size_t len;
247 memset(&iok, 0, sizeof(iok));
248 iok.client_id = client->id;
249 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
250 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
251 iok.name_len = strlen(refname);
253 len = sizeof(iok) + iok.name_len;
254 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
255 PROC_SESSION, gotd_session.pid, len);
256 if (wbuf == NULL)
257 return got_error_from_errno("imsg_create REF_UPDATE_OK");
259 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
260 return got_error_from_errno("imsg_add REF_UPDATE_OK");
261 if (imsg_add(wbuf, refname, iok.name_len) == -1)
262 return got_error_from_errno("imsg_add REF_UPDATE_OK");
264 wbuf->fd = -1;
265 imsg_close(&iev->ibuf, wbuf);
266 gotd_imsg_event_add(iev);
267 return NULL;
270 static void
271 send_refs_updated(struct gotd_session_client *client)
273 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
274 PROC_SESSION, -1, NULL, 0) == -1)
275 log_warn("imsg compose REFS_UPDATED");
278 static const struct got_error *
279 send_ref_update_ng(struct gotd_session_client *client,
280 struct gotd_imsg_ref_update *iref, const char *refname,
281 const char *reason)
283 const struct got_error *ng_err;
284 struct gotd_imsg_ref_update_ng ing;
285 struct gotd_imsgev *iev = &client->iev;
286 struct ibuf *wbuf;
287 size_t len;
289 memset(&ing, 0, sizeof(ing));
290 ing.client_id = client->id;
291 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
292 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
293 ing.name_len = strlen(refname);
295 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
296 ing.reason_len = strlen(ng_err->msg);
298 len = sizeof(ing) + ing.name_len + ing.reason_len;
299 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
300 PROC_SESSION, gotd_session.pid, len);
301 if (wbuf == NULL)
302 return got_error_from_errno("imsg_create REF_UPDATE_NG");
304 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
305 return got_error_from_errno("imsg_add REF_UPDATE_NG");
306 if (imsg_add(wbuf, refname, ing.name_len) == -1)
307 return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
309 return got_error_from_errno("imsg_add REF_UPDATE_NG");
311 wbuf->fd = -1;
312 imsg_close(&iev->ibuf, wbuf);
313 gotd_imsg_event_add(iev);
314 return NULL;
317 static const struct got_error *
318 install_pack(struct gotd_session_client *client, const char *repo_path,
319 struct imsg *imsg)
321 const struct got_error *err = NULL;
322 struct gotd_imsg_packfile_install inst;
323 char hex[SHA1_DIGEST_STRING_LENGTH];
324 size_t datalen;
325 char *packfile_path = NULL, *packidx_path = NULL;
327 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
328 if (datalen != sizeof(inst))
329 return got_error(GOT_ERR_PRIVSEP_LEN);
330 memcpy(&inst, imsg->data, sizeof(inst));
332 if (client->packfile_path == NULL)
333 return got_error_msg(GOT_ERR_BAD_REQUEST,
334 "client has no pack file");
335 if (client->packidx_path == NULL)
336 return got_error_msg(GOT_ERR_BAD_REQUEST,
337 "client has no pack file index");
339 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
340 return got_error_msg(GOT_ERR_NO_SPACE,
341 "could not convert pack file SHA1 to hex");
343 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
344 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
345 err = got_error_from_errno("asprintf");
346 goto done;
349 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
350 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
351 err = got_error_from_errno("asprintf");
352 goto done;
355 if (rename(client->packfile_path, packfile_path) == -1) {
356 err = got_error_from_errno3("rename", client->packfile_path,
357 packfile_path);
358 goto done;
361 free(client->packfile_path);
362 client->packfile_path = NULL;
364 if (rename(client->packidx_path, packidx_path) == -1) {
365 err = got_error_from_errno3("rename", client->packidx_path,
366 packidx_path);
367 goto done;
370 free(client->packidx_path);
371 client->packidx_path = NULL;
372 done:
373 free(packfile_path);
374 free(packidx_path);
375 return err;
378 static const struct got_error *
379 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
381 struct gotd_imsg_ref_updates_start istart;
382 size_t datalen;
384 if (client->nref_updates != -1)
385 return got_error(GOT_ERR_PRIVSEP_MSG);
387 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 if (datalen != sizeof(istart))
389 return got_error(GOT_ERR_PRIVSEP_LEN);
390 memcpy(&istart, imsg->data, sizeof(istart));
392 if (istart.nref_updates <= 0)
393 return got_error(GOT_ERR_PRIVSEP_MSG);
395 client->nref_updates = istart.nref_updates;
396 return NULL;
399 static const struct got_error *
400 update_ref(int *shut, struct gotd_session_client *client,
401 const char *repo_path, struct imsg *imsg)
403 const struct got_error *err = NULL;
404 struct got_repository *repo = NULL;
405 struct got_reference *ref = NULL;
406 struct gotd_imsg_ref_update iref;
407 struct got_object_id old_id, new_id;
408 struct got_object_id *id = NULL;
409 struct got_object *obj = NULL;
410 char *refname = NULL;
411 size_t datalen;
412 int locked = 0;
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_error_fmt(GOT_ERR_REF_BUSY,
455 "%s has been created by someone else "
456 "while transaction was in progress",
457 got_ref_get_name(ref));
458 goto done;
460 } else if (iref.delete_ref) {
461 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
462 if (err)
463 goto done;
464 locked = 1;
466 err = got_ref_resolve(&id, repo, ref);
467 if (err)
468 goto done;
470 if (got_object_id_cmp(id, &old_id) != 0) {
471 err = got_error_fmt(GOT_ERR_REF_BUSY,
472 "%s has been modified by someone else "
473 "while transaction was in progress",
474 got_ref_get_name(ref));
475 goto done;
478 err = got_ref_delete(ref, repo);
479 if (err)
480 goto done;
482 free(id);
483 id = NULL;
484 } else {
485 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
486 if (err)
487 goto done;
488 locked = 1;
490 err = got_ref_resolve(&id, repo, ref);
491 if (err)
492 goto done;
494 if (got_object_id_cmp(id, &old_id) != 0) {
495 err = got_error_fmt(GOT_ERR_REF_BUSY,
496 "%s has been modified by someone else "
497 "while transaction was in progress",
498 got_ref_get_name(ref));
499 goto done;
502 err = got_ref_change_ref(ref, &new_id);
503 if (err)
504 goto done;
506 err = got_ref_write(ref, repo);
507 if (err)
508 goto done;
510 free(id);
511 id = NULL;
513 done:
514 if (err) {
515 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
516 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
517 "could not acquire exclusive file lock for %s",
518 refname);
520 send_ref_update_ng(client, &iref, refname, err->msg);
521 } else
522 send_ref_update_ok(client, &iref, refname);
524 if (client->nref_updates > 0) {
525 client->nref_updates--;
526 if (client->nref_updates == 0) {
527 send_refs_updated(client);
528 *shut = 1;
532 if (locked) {
533 const struct got_error *unlock_err;
534 unlock_err = got_ref_unlock(ref);
535 if (unlock_err && err == NULL)
536 err = unlock_err;
538 if (ref)
539 got_ref_close(ref);
540 if (obj)
541 got_object_close(obj);
542 if (repo)
543 got_repo_close(repo);
544 free(refname);
545 free(id);
546 return err;
549 static void
550 session_dispatch_repo_child(int fd, short event, void *arg)
552 struct gotd_imsgev *iev = arg;
553 struct imsgbuf *ibuf = &iev->ibuf;
554 struct gotd_session_client *client = &gotd_session_client;
555 ssize_t n;
556 int shut = 0;
557 struct imsg imsg;
559 if (event & EV_READ) {
560 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
561 fatal("imsg_read error");
562 if (n == 0) {
563 /* Connection closed. */
564 shut = 1;
565 goto done;
569 if (event & EV_WRITE) {
570 n = msgbuf_write(&ibuf->w);
571 if (n == -1 && errno != EAGAIN)
572 fatal("msgbuf_write");
573 if (n == 0) {
574 /* Connection closed. */
575 shut = 1;
576 goto done;
580 for (;;) {
581 const struct got_error *err = NULL;
582 uint32_t client_id = 0;
583 int do_disconnect = 0;
584 int do_ref_updates = 0, do_ref_update = 0;
585 int do_packfile_install = 0;
587 if ((n = imsg_get(ibuf, &imsg)) == -1)
588 fatal("%s: imsg_get error", __func__);
589 if (n == 0) /* No more messages. */
590 break;
592 switch (imsg.hdr.type) {
593 case GOTD_IMSG_ERROR:
594 do_disconnect = 1;
595 err = gotd_imsg_recv_error(&client_id, &imsg);
596 break;
597 case GOTD_IMSG_PACKFILE_DONE:
598 do_disconnect = 1;
599 err = recv_packfile_done(&client_id, &imsg);
600 break;
601 case GOTD_IMSG_PACKFILE_INSTALL:
602 err = recv_packfile_install(&client_id, &imsg);
603 if (err == NULL)
604 do_packfile_install = 1;
605 break;
606 case GOTD_IMSG_REF_UPDATES_START:
607 err = recv_ref_updates_start(&client_id, &imsg);
608 if (err == NULL)
609 do_ref_updates = 1;
610 break;
611 case GOTD_IMSG_REF_UPDATE:
612 err = recv_ref_update(&client_id, &imsg);
613 if (err == NULL)
614 do_ref_update = 1;
615 break;
616 default:
617 log_debug("unexpected imsg %d", imsg.hdr.type);
618 break;
621 if (do_disconnect) {
622 if (err)
623 disconnect_on_error(client, err);
624 else
625 disconnect(client);
626 } else {
627 if (do_packfile_install)
628 err = install_pack(client,
629 gotd_session.repo->path, &imsg);
630 else if (do_ref_updates)
631 err = begin_ref_updates(client, &imsg);
632 else if (do_ref_update)
633 err = update_ref(&shut, client,
634 gotd_session.repo->path, &imsg);
635 if (err)
636 log_warnx("uid %d: %s", client->euid, err->msg);
638 imsg_free(&imsg);
640 done:
641 if (!shut) {
642 gotd_imsg_event_add(iev);
643 } else {
644 /* This pipe is dead. Remove its event handler */
645 event_del(&iev->ev);
646 event_loopexit(NULL);
650 static const struct got_error *
651 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
653 struct gotd_imsg_capabilities icapas;
654 size_t datalen;
656 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
657 if (datalen != sizeof(icapas))
658 return got_error(GOT_ERR_PRIVSEP_LEN);
659 memcpy(&icapas, imsg->data, sizeof(icapas));
661 client->ncapa_alloc = icapas.ncapabilities;
662 client->capabilities = calloc(client->ncapa_alloc,
663 sizeof(*client->capabilities));
664 if (client->capabilities == NULL) {
665 client->ncapa_alloc = 0;
666 return got_error_from_errno("calloc");
669 log_debug("expecting %zu capabilities from uid %d",
670 client->ncapa_alloc, client->euid);
671 return NULL;
674 static const struct got_error *
675 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
677 struct gotd_imsg_capability icapa;
678 struct gotd_client_capability *capa;
679 size_t datalen;
680 char *key, *value = NULL;
682 if (client->capabilities == NULL ||
683 client->ncapabilities >= client->ncapa_alloc) {
684 return got_error_msg(GOT_ERR_BAD_REQUEST,
685 "unexpected capability received");
688 memset(&icapa, 0, sizeof(icapa));
690 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
691 if (datalen < sizeof(icapa))
692 return got_error(GOT_ERR_PRIVSEP_LEN);
693 memcpy(&icapa, imsg->data, sizeof(icapa));
695 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
696 return got_error(GOT_ERR_PRIVSEP_LEN);
698 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
699 if (key == NULL)
700 return got_error_from_errno("strndup");
701 if (icapa.value_len > 0) {
702 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
703 icapa.value_len);
704 if (value == NULL) {
705 free(key);
706 return got_error_from_errno("strndup");
710 capa = &client->capabilities[client->ncapabilities++];
711 capa->key = key;
712 capa->value = value;
714 if (value)
715 log_debug("uid %d: capability %s=%s", client->euid, key, value);
716 else
717 log_debug("uid %d: capability %s", client->euid, key);
719 return NULL;
722 static const struct got_error *
723 ensure_client_is_reading(struct gotd_session_client *client)
725 if (client->is_writing) {
726 return got_error_fmt(GOT_ERR_BAD_PACKET,
727 "uid %d made a read-request but is not reading from "
728 "a repository", client->euid);
731 return NULL;
734 static const struct got_error *
735 ensure_client_is_writing(struct gotd_session_client *client)
737 if (!client->is_writing) {
738 return got_error_fmt(GOT_ERR_BAD_PACKET,
739 "uid %d made a write-request but is not writing to "
740 "a repository", client->euid);
743 return NULL;
746 static const struct got_error *
747 forward_want(struct gotd_session_client *client, struct imsg *imsg)
749 struct gotd_imsg_want ireq;
750 struct gotd_imsg_want iwant;
751 size_t datalen;
753 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
754 if (datalen != sizeof(ireq))
755 return got_error(GOT_ERR_PRIVSEP_LEN);
757 memcpy(&ireq, imsg->data, datalen);
759 memset(&iwant, 0, sizeof(iwant));
760 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
761 iwant.client_id = client->id;
763 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
764 PROC_SESSION, -1, &iwant, sizeof(iwant)) == -1)
765 return got_error_from_errno("imsg compose WANT");
767 return NULL;
770 static const struct got_error *
771 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
773 const struct got_error *err = NULL;
774 struct gotd_imsg_ref_update ireq;
775 struct gotd_imsg_ref_update *iref = NULL;
776 size_t datalen;
778 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
779 if (datalen < sizeof(ireq))
780 return got_error(GOT_ERR_PRIVSEP_LEN);
781 memcpy(&ireq, imsg->data, sizeof(ireq));
782 if (datalen != sizeof(ireq) + ireq.name_len)
783 return got_error(GOT_ERR_PRIVSEP_LEN);
785 iref = malloc(datalen);
786 if (iref == NULL)
787 return got_error_from_errno("malloc");
788 memcpy(iref, imsg->data, datalen);
790 iref->client_id = client->id;
791 if (gotd_imsg_compose_event(&client->repo_child_iev,
792 GOTD_IMSG_REF_UPDATE, PROC_SESSION, -1, iref, datalen) == -1)
793 err = got_error_from_errno("imsg compose REF_UPDATE");
794 free(iref);
795 return err;
798 static const struct got_error *
799 forward_have(struct gotd_session_client *client, struct imsg *imsg)
801 struct gotd_imsg_have ireq;
802 struct gotd_imsg_have ihave;
803 size_t datalen;
805 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
806 if (datalen != sizeof(ireq))
807 return got_error(GOT_ERR_PRIVSEP_LEN);
809 memcpy(&ireq, imsg->data, datalen);
811 memset(&ihave, 0, sizeof(ihave));
812 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
813 ihave.client_id = client->id;
815 if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
816 PROC_SESSION, -1, &ihave, sizeof(ihave)) == -1)
817 return got_error_from_errno("imsg compose HAVE");
819 return NULL;
822 static int
823 client_has_capability(struct gotd_session_client *client, const char *capastr)
825 struct gotd_client_capability *capa;
826 size_t i;
828 if (client->ncapabilities == 0)
829 return 0;
831 for (i = 0; i < client->ncapabilities; i++) {
832 capa = &client->capabilities[i];
833 if (strcmp(capa->key, capastr) == 0)
834 return 1;
837 return 0;
840 static const struct got_error *
841 recv_packfile(struct gotd_session_client *client)
843 const struct got_error *err = NULL;
844 struct gotd_imsg_recv_packfile ipack;
845 struct gotd_imsg_packfile_pipe ipipe;
846 struct gotd_imsg_packidx_file ifile;
847 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
848 int packfd = -1, idxfd = -1;
849 int pipe[2] = { -1, -1 };
851 if (client->packfile_path) {
852 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
853 "uid %d already has a pack file", client->euid);
856 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
857 return got_error_from_errno("socketpair");
859 memset(&ipipe, 0, sizeof(ipipe));
860 ipipe.client_id = client->id;
862 /* Send pack pipe end 0 to repo child process. */
863 if (gotd_imsg_compose_event(&client->repo_child_iev,
864 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[0],
865 &ipipe, sizeof(ipipe)) == -1) {
866 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
867 pipe[0] = -1;
868 goto done;
870 pipe[0] = -1;
872 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
873 if (gotd_imsg_compose_event(&client->iev,
874 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[1], NULL, 0) == -1)
875 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
876 pipe[1] = -1;
878 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
879 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
880 client->euid) == -1) {
881 err = got_error_from_errno("asprintf");
882 goto done;
885 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
886 if (err)
887 goto done;
888 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
889 err = got_error_from_errno2("fchmod", pack_path);
890 goto done;
893 free(basepath);
894 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
895 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
896 client->euid) == -1) {
897 err = got_error_from_errno("asprintf");
898 basepath = NULL;
899 goto done;
901 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
902 if (err)
903 goto done;
904 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
905 err = got_error_from_errno2("fchmod", idx_path);
906 goto done;
909 memset(&ifile, 0, sizeof(ifile));
910 ifile.client_id = client->id;
911 if (gotd_imsg_compose_event(&client->repo_child_iev,
912 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION,
913 idxfd, &ifile, sizeof(ifile)) == -1) {
914 err = got_error_from_errno("imsg compose PACKIDX_FILE");
915 idxfd = -1;
916 goto done;
918 idxfd = -1;
920 memset(&ipack, 0, sizeof(ipack));
921 ipack.client_id = client->id;
922 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
923 ipack.report_status = 1;
925 if (gotd_imsg_compose_event(&client->repo_child_iev,
926 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION, packfd,
927 &ipack, sizeof(ipack)) == -1) {
928 err = got_error_from_errno("imsg compose RECV_PACKFILE");
929 packfd = -1;
930 goto done;
932 packfd = -1;
934 done:
935 free(basepath);
936 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
937 err = got_error_from_errno("close");
938 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
939 err = got_error_from_errno("close");
940 if (packfd != -1 && close(packfd) == -1 && err == NULL)
941 err = got_error_from_errno("close");
942 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
943 err = got_error_from_errno("close");
944 if (err) {
945 free(pack_path);
946 free(idx_path);
947 } else {
948 client->packfile_path = pack_path;
949 client->packidx_path = idx_path;
951 return err;
954 static const struct got_error *
955 send_packfile(struct gotd_session_client *client)
957 const struct got_error *err = NULL;
958 struct gotd_imsg_send_packfile ipack;
959 struct gotd_imsg_packfile_pipe ipipe;
960 int pipe[2];
962 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
963 return got_error_from_errno("socketpair");
965 memset(&ipack, 0, sizeof(ipack));
966 memset(&ipipe, 0, sizeof(ipipe));
968 ipack.client_id = client->id;
969 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
970 ipack.report_progress = 1;
972 client->delta_cache_fd = got_opentempfd();
973 if (client->delta_cache_fd == -1)
974 return got_error_from_errno("got_opentempfd");
976 if (gotd_imsg_compose_event(&client->repo_child_iev,
977 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
978 &ipack, sizeof(ipack)) == -1) {
979 err = got_error_from_errno("imsg compose SEND_PACKFILE");
980 close(pipe[0]);
981 close(pipe[1]);
982 return err;
985 ipipe.client_id = client->id;
987 /* Send pack pipe end 0 to repo child process. */
988 if (gotd_imsg_compose_event(&client->repo_child_iev,
989 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
990 pipe[0], &ipipe, sizeof(ipipe)) == -1) {
991 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
992 close(pipe[1]);
993 return err;
996 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
997 if (gotd_imsg_compose_event(&client->iev,
998 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
999 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1001 return err;
1004 static void
1005 session_dispatch_client(int fd, short events, void *arg)
1007 struct gotd_imsgev *iev = arg;
1008 struct imsgbuf *ibuf = &iev->ibuf;
1009 struct gotd_session_client *client = &gotd_session_client;
1010 const struct got_error *err = NULL;
1011 struct imsg imsg;
1012 ssize_t n;
1014 if (events & EV_WRITE) {
1015 while (ibuf->w.queued) {
1016 n = msgbuf_write(&ibuf->w);
1017 if (n == -1 && errno == EPIPE) {
1019 * The client has closed its socket.
1020 * This can happen when Git clients are
1021 * done sending pack file data.
1023 msgbuf_clear(&ibuf->w);
1024 continue;
1025 } else if (n == -1 && errno != EAGAIN) {
1026 err = got_error_from_errno("imsg_flush");
1027 disconnect_on_error(client, err);
1028 return;
1030 if (n == 0) {
1031 /* Connection closed. */
1032 err = got_error(GOT_ERR_EOF);
1033 disconnect_on_error(client, err);
1034 return;
1039 if ((events & EV_READ) == 0)
1040 return;
1042 memset(&imsg, 0, sizeof(imsg));
1044 while (err == NULL) {
1045 err = gotd_imsg_recv(&imsg, ibuf, 0);
1046 if (err) {
1047 if (err->code == GOT_ERR_PRIVSEP_READ)
1048 err = NULL;
1049 break;
1052 evtimer_del(&client->tmo);
1054 switch (imsg.hdr.type) {
1055 case GOTD_IMSG_CAPABILITIES:
1056 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1057 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1058 "unexpected capabilities received");
1059 break;
1061 log_debug("receiving capabilities from uid %d",
1062 client->euid);
1063 err = recv_capabilities(client, &imsg);
1064 break;
1065 case GOTD_IMSG_CAPABILITY:
1066 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1067 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1068 "unexpected capability received");
1069 break;
1071 err = recv_capability(client, &imsg);
1072 if (err || client->ncapabilities < client->ncapa_alloc)
1073 break;
1074 if (!client->is_writing) {
1075 client->state = GOTD_STATE_EXPECT_WANT;
1076 client->accept_flush_pkt = 1;
1077 log_debug("uid %d: expecting want-lines",
1078 client->euid);
1079 } else if (client->is_writing) {
1080 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1081 client->accept_flush_pkt = 1;
1082 log_debug("uid %d: expecting ref-update-lines",
1083 client->euid);
1084 } else
1085 fatalx("client %d is both reading and writing",
1086 client->euid);
1087 break;
1088 case GOTD_IMSG_WANT:
1089 if (client->state != GOTD_STATE_EXPECT_WANT) {
1090 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1091 "unexpected want-line received");
1092 break;
1094 log_debug("received want-line from uid %d",
1095 client->euid);
1096 err = ensure_client_is_reading(client);
1097 if (err)
1098 break;
1099 client->accept_flush_pkt = 1;
1100 err = forward_want(client, &imsg);
1101 break;
1102 case GOTD_IMSG_REF_UPDATE:
1103 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1104 client->state !=
1105 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1106 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1107 "unexpected ref-update-line received");
1108 break;
1110 log_debug("received ref-update-line from uid %d",
1111 client->euid);
1112 err = ensure_client_is_writing(client);
1113 if (err)
1114 break;
1115 err = forward_ref_update(client, &imsg);
1116 if (err)
1117 break;
1118 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1119 client->accept_flush_pkt = 1;
1120 break;
1121 case GOTD_IMSG_HAVE:
1122 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1123 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1124 "unexpected have-line received");
1125 break;
1127 log_debug("received have-line from uid %d",
1128 client->euid);
1129 err = ensure_client_is_reading(client);
1130 if (err)
1131 break;
1132 err = forward_have(client, &imsg);
1133 if (err)
1134 break;
1135 client->accept_flush_pkt = 1;
1136 break;
1137 case GOTD_IMSG_FLUSH:
1138 if (client->state == GOTD_STATE_EXPECT_WANT ||
1139 client->state == GOTD_STATE_EXPECT_HAVE) {
1140 err = ensure_client_is_reading(client);
1141 if (err)
1142 break;
1143 } else if (client->state ==
1144 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1145 err = ensure_client_is_writing(client);
1146 if (err)
1147 break;
1148 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1149 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1150 "unexpected flush-pkt received");
1151 break;
1153 if (!client->accept_flush_pkt) {
1154 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1155 "unexpected flush-pkt received");
1156 break;
1160 * Accept just one flush packet at a time.
1161 * Future client state transitions will set this flag
1162 * again if another flush packet is expected.
1164 client->accept_flush_pkt = 0;
1166 log_debug("received flush-pkt from uid %d",
1167 client->euid);
1168 if (client->state == GOTD_STATE_EXPECT_WANT) {
1169 client->state = GOTD_STATE_EXPECT_HAVE;
1170 log_debug("uid %d: expecting have-lines",
1171 client->euid);
1172 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1173 client->state = GOTD_STATE_EXPECT_DONE;
1174 client->accept_flush_pkt = 1;
1175 log_debug("uid %d: expecting 'done'",
1176 client->euid);
1177 } else if (client->state ==
1178 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1179 client->state = GOTD_STATE_EXPECT_PACKFILE;
1180 log_debug("uid %d: expecting packfile",
1181 client->euid);
1182 err = recv_packfile(client);
1183 } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1184 /* should not happen, see above */
1185 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1186 "unexpected client state");
1187 break;
1189 break;
1190 case GOTD_IMSG_DONE:
1191 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1192 client->state != GOTD_STATE_EXPECT_DONE) {
1193 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1194 "unexpected flush-pkt received");
1195 break;
1197 log_debug("received 'done' from uid %d", client->euid);
1198 err = ensure_client_is_reading(client);
1199 if (err)
1200 break;
1201 client->state = GOTD_STATE_DONE;
1202 client->accept_flush_pkt = 1;
1203 err = send_packfile(client);
1204 break;
1205 default:
1206 log_debug("unexpected imsg %d", imsg.hdr.type);
1207 err = got_error(GOT_ERR_PRIVSEP_MSG);
1208 break;
1211 imsg_free(&imsg);
1214 if (err) {
1215 if (err->code != GOT_ERR_EOF ||
1216 client->state != GOTD_STATE_EXPECT_PACKFILE)
1217 disconnect_on_error(client, err);
1218 } else {
1219 gotd_imsg_event_add(iev);
1220 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1224 static const struct got_error *
1225 list_refs_request(void)
1227 static const struct got_error *err;
1228 struct gotd_session_client *client = &gotd_session_client;
1229 struct gotd_imsgev *iev = &client->repo_child_iev;
1230 struct gotd_imsg_list_refs_internal ilref;
1231 int fd;
1233 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1234 return got_error(GOT_ERR_PRIVSEP_MSG);
1236 memset(&ilref, 0, sizeof(ilref));
1237 ilref.client_id = client->id;
1239 fd = dup(client->fd);
1240 if (fd == -1)
1241 return got_error_from_errno("dup");
1243 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1244 PROC_SESSION, fd, &ilref, sizeof(ilref)) == -1) {
1245 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1246 close(fd);
1247 return err;
1250 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1251 log_debug("uid %d: expecting capabilities", client->euid);
1252 return NULL;
1255 static const struct got_error *
1256 recv_connect(struct imsg *imsg)
1258 struct gotd_session_client *client = &gotd_session_client;
1259 struct gotd_imsg_connect iconnect;
1260 size_t datalen;
1262 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1263 return got_error(GOT_ERR_PRIVSEP_MSG);
1265 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1266 if (datalen != sizeof(iconnect))
1267 return got_error(GOT_ERR_PRIVSEP_LEN);
1268 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1270 if (imsg->fd == -1)
1271 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1273 client->fd = imsg->fd;
1274 client->euid = iconnect.euid;
1275 client->egid = iconnect.egid;
1277 imsg_init(&client->iev.ibuf, client->fd);
1278 client->iev.handler = session_dispatch_client;
1279 client->iev.events = EV_READ;
1280 client->iev.handler_arg = NULL;
1281 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1282 session_dispatch_client, &client->iev);
1283 gotd_imsg_event_add(&client->iev);
1284 evtimer_set(&client->tmo, gotd_request_timeout, client);
1286 return NULL;
1289 static const struct got_error *
1290 recv_repo_child(struct imsg *imsg)
1292 struct gotd_imsg_connect_repo_child ichild;
1293 struct gotd_session_client *client = &gotd_session_client;
1294 size_t datalen;
1296 if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1297 return got_error(GOT_ERR_PRIVSEP_MSG);
1299 /* We should already have received a pipe to the listener. */
1300 if (client->fd == -1)
1301 return got_error(GOT_ERR_PRIVSEP_MSG);
1303 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1304 if (datalen != sizeof(ichild))
1305 return got_error(GOT_ERR_PRIVSEP_LEN);
1307 memcpy(&ichild, imsg->data, sizeof(ichild));
1309 client->id = ichild.client_id;
1310 if (ichild.proc_id == PROC_REPO_WRITE)
1311 client->is_writing = 1;
1312 else if (ichild.proc_id == PROC_REPO_READ)
1313 client->is_writing = 0;
1314 else
1315 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1316 "bad child process type");
1318 if (imsg->fd == -1)
1319 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1321 imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1322 client->repo_child_iev.handler = session_dispatch_repo_child;
1323 client->repo_child_iev.events = EV_READ;
1324 client->repo_child_iev.handler_arg = NULL;
1325 event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1326 EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1327 gotd_imsg_event_add(&client->repo_child_iev);
1329 /* The "recvfd" pledge promise is no longer needed. */
1330 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1331 fatal("pledge");
1333 return NULL;
1336 static void
1337 session_dispatch(int fd, short event, void *arg)
1339 struct gotd_imsgev *iev = arg;
1340 struct imsgbuf *ibuf = &iev->ibuf;
1341 struct gotd_session_client *client = &gotd_session_client;
1342 ssize_t n;
1343 int shut = 0;
1344 struct imsg imsg;
1346 if (event & EV_READ) {
1347 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1348 fatal("imsg_read error");
1349 if (n == 0) {
1350 /* Connection closed. */
1351 shut = 1;
1352 goto done;
1356 if (event & EV_WRITE) {
1357 n = msgbuf_write(&ibuf->w);
1358 if (n == -1 && errno != EAGAIN)
1359 fatal("msgbuf_write");
1360 if (n == 0) {
1361 /* Connection closed. */
1362 shut = 1;
1363 goto done;
1367 for (;;) {
1368 const struct got_error *err = NULL;
1369 uint32_t client_id = 0;
1370 int do_disconnect = 0, do_list_refs = 0;
1372 if ((n = imsg_get(ibuf, &imsg)) == -1)
1373 fatal("%s: imsg_get error", __func__);
1374 if (n == 0) /* No more messages. */
1375 break;
1377 switch (imsg.hdr.type) {
1378 case GOTD_IMSG_ERROR:
1379 do_disconnect = 1;
1380 err = gotd_imsg_recv_error(&client_id, &imsg);
1381 break;
1382 case GOTD_IMSG_CONNECT:
1383 err = recv_connect(&imsg);
1384 break;
1385 case GOTD_IMSG_DISCONNECT:
1386 do_disconnect = 1;
1387 break;
1388 case GOTD_IMSG_CONNECT_REPO_CHILD:
1389 err = recv_repo_child(&imsg);
1390 if (err)
1391 break;
1392 do_list_refs = 1;
1393 break;
1394 default:
1395 log_debug("unexpected imsg %d", imsg.hdr.type);
1396 break;
1398 imsg_free(&imsg);
1400 if (do_disconnect) {
1401 if (err)
1402 disconnect_on_error(client, err);
1403 else
1404 disconnect(client);
1405 } else if (do_list_refs)
1406 err = list_refs_request();
1408 if (err)
1409 log_warnx("uid %d: %s", client->euid, err->msg);
1411 done:
1412 if (!shut) {
1413 gotd_imsg_event_add(iev);
1414 } else {
1415 /* This pipe is dead. Remove its event handler */
1416 event_del(&iev->ev);
1417 event_loopexit(NULL);
1421 void
1422 session_main(const char *title, const char *repo_path,
1423 int *pack_fds, int *temp_fds, struct timeval *request_timeout)
1425 const struct got_error *err = NULL;
1426 struct event evsigint, evsigterm, evsighup, evsigusr1;
1428 gotd_session.title = title;
1429 gotd_session.pid = getpid();
1430 gotd_session.pack_fds = pack_fds;
1431 gotd_session.temp_fds = temp_fds;
1432 memcpy(&gotd_session.request_timeout, request_timeout,
1433 sizeof(gotd_session.request_timeout));
1435 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1436 if (err)
1437 goto done;
1438 if (!got_repo_is_bare(gotd_session.repo)) {
1439 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1440 "bare git repository required");
1441 goto done;
1444 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1446 signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1447 signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1448 signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1449 signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1450 signal(SIGPIPE, SIG_IGN);
1452 signal_add(&evsigint, NULL);
1453 signal_add(&evsigterm, NULL);
1454 signal_add(&evsighup, NULL);
1455 signal_add(&evsigusr1, NULL);
1457 gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1458 gotd_session_client.fd = -1;
1459 gotd_session_client.nref_updates = -1;
1460 gotd_session_client.delta_cache_fd = -1;
1461 gotd_session_client.accept_flush_pkt = 1;
1463 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1464 gotd_session.parent_iev.handler = session_dispatch;
1465 gotd_session.parent_iev.events = EV_READ;
1466 gotd_session.parent_iev.handler_arg = NULL;
1467 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1468 EV_READ, session_dispatch, &gotd_session.parent_iev);
1469 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1470 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION, -1, NULL, 0) == -1) {
1471 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1472 goto done;
1475 event_dispatch();
1476 done:
1477 if (err)
1478 log_warnx("%s: %s", title, err->msg);
1479 gotd_session_shutdown();
1482 void
1483 gotd_session_shutdown(void)
1485 log_debug("shutting down");
1486 if (gotd_session.repo)
1487 got_repo_close(gotd_session.repo);
1488 got_repo_pack_fds_close(gotd_session.pack_fds);
1489 got_repo_temp_fds_close(gotd_session.temp_fds);
1490 exit(0);