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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
36 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_opentemp.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_repository.h"
51 #include "got_lib_gitproto.h"
55 #include "session_write.h"
57 struct gotd_session_notif {
58 STAILQ_ENTRY(gotd_session_notif) entry;
60 enum gotd_notification_action action;
62 struct got_object_id old_id;
63 struct got_object_id new_id;
65 STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
67 enum gotd_session_write_state {
68 GOTD_STATE_EXPECT_LIST_REFS,
69 GOTD_STATE_EXPECT_CAPABILITIES,
70 GOTD_STATE_EXPECT_REF_UPDATE,
71 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
72 GOTD_STATE_EXPECT_PACKFILE,
76 static struct gotd_session_write {
79 struct got_repository *repo;
80 struct gotd_repo *repo_cfg;
83 struct gotd_imsgev parent_iev;
84 struct gotd_imsgev notifier_iev;
85 struct timeval request_timeout;
86 enum gotd_session_write_state state;
87 struct gotd_imsgev repo_child_iev;
90 static struct gotd_session_client {
91 struct gotd_client_capability *capabilities;
97 struct gotd_imsgev iev;
105 int accept_flush_pkt;
106 int flush_disconnect;
107 } gotd_session_client;
109 static void session_write_shutdown(void);
112 disconnect(struct gotd_session_client *client)
114 log_debug("uid %d: disconnecting", client->euid);
116 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
117 GOTD_IMSG_DISCONNECT, PROC_SESSION_WRITE, -1, NULL, 0) == -1)
118 log_warn("imsg compose DISCONNECT");
120 imsg_clear(&gotd_session.repo_child_iev.ibuf);
121 event_del(&gotd_session.repo_child_iev.ev);
122 evtimer_del(&client->tmo);
124 if (client->delta_cache_fd != -1)
125 close(client->delta_cache_fd);
126 if (client->packfile_path) {
127 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
128 log_warn("unlink %s: ", client->packfile_path);
129 free(client->packfile_path);
131 if (client->packidx_path) {
132 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
133 log_warn("unlink %s: ", client->packidx_path);
134 free(client->packidx_path);
136 free(client->capabilities);
138 session_write_shutdown();
142 disconnect_on_error(struct gotd_session_client *client,
143 const struct got_error *err)
147 if (err->code != GOT_ERR_EOF) {
148 log_warnx("uid %d: %s", client->euid, err->msg);
149 imsg_init(&ibuf, client->fd);
150 gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_WRITE, err);
158 gotd_request_timeout(int fd, short events, void *arg)
160 struct gotd_session_client *client = arg;
162 log_debug("disconnecting uid %d due to timeout", client->euid);
167 session_write_sighdlr(int sig, short event, void *arg)
170 * Normal signal handler rules don't apply because libevent
176 log_info("%s: ignoring SIGHUP", __func__);
179 log_info("%s: ignoring SIGUSR1", __func__);
183 session_write_shutdown();
187 fatalx("unexpected signal");
191 static const struct got_error *
192 recv_packfile_install(struct imsg *imsg)
194 struct gotd_imsg_packfile_install inst;
197 log_debug("packfile-install received");
199 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
200 if (datalen != sizeof(inst))
201 return got_error(GOT_ERR_PRIVSEP_LEN);
202 memcpy(&inst, imsg->data, sizeof(inst));
207 static const struct got_error *
208 recv_ref_updates_start(struct imsg *imsg)
210 struct gotd_imsg_ref_updates_start istart;
213 log_debug("ref-updates-start received");
215 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
216 if (datalen != sizeof(istart))
217 return got_error(GOT_ERR_PRIVSEP_LEN);
218 memcpy(&istart, imsg->data, sizeof(istart));
223 static const struct got_error *
224 recv_ref_update(struct imsg *imsg)
226 struct gotd_imsg_ref_update iref;
229 log_debug("ref-update received");
231 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 if (datalen < sizeof(iref))
233 return got_error(GOT_ERR_PRIVSEP_LEN);
234 memcpy(&iref, imsg->data, sizeof(iref));
239 static const struct got_error *
240 send_ref_update_ok(struct gotd_session_client *client,
241 struct gotd_imsg_ref_update *iref, const char *refname)
243 struct gotd_imsg_ref_update_ok iok;
244 struct gotd_imsgev *iev = &client->iev;
248 memset(&iok, 0, sizeof(iok));
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_WRITE, gotd_session.pid, len);
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 imsg_close(&iev->ibuf, wbuf);
265 gotd_imsg_event_add(iev);
270 send_refs_updated(struct gotd_session_client *client)
272 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
273 PROC_SESSION_WRITE, -1, NULL, 0) == -1)
274 log_warn("imsg compose REFS_UPDATED");
277 static const struct got_error *
278 send_ref_update_ng(struct gotd_session_client *client,
279 struct gotd_imsg_ref_update *iref, const char *refname,
282 const struct got_error *ng_err;
283 struct gotd_imsg_ref_update_ng ing;
284 struct gotd_imsgev *iev = &client->iev;
288 memset(&ing, 0, sizeof(ing));
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_WRITE, 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");
309 imsg_close(&iev->ibuf, wbuf);
310 gotd_imsg_event_add(iev);
314 static const struct got_error *
315 install_pack(struct gotd_session_client *client, const char *repo_path,
318 const struct got_error *err = NULL;
319 struct gotd_imsg_packfile_install inst;
320 char hex[SHA1_DIGEST_STRING_LENGTH];
322 char *packfile_path = NULL, *packidx_path = NULL;
324 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
325 if (datalen != sizeof(inst))
326 return got_error(GOT_ERR_PRIVSEP_LEN);
327 memcpy(&inst, imsg->data, sizeof(inst));
329 if (client->packfile_path == NULL)
330 return got_error_msg(GOT_ERR_BAD_REQUEST,
331 "client has no pack file");
332 if (client->packidx_path == NULL)
333 return got_error_msg(GOT_ERR_BAD_REQUEST,
334 "client has no pack file index");
336 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
337 return got_error_msg(GOT_ERR_NO_SPACE,
338 "could not convert pack file SHA1 to hex");
340 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
341 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
342 err = got_error_from_errno("asprintf");
346 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
347 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
348 err = got_error_from_errno("asprintf");
352 if (rename(client->packfile_path, packfile_path) == -1) {
353 err = got_error_from_errno3("rename", client->packfile_path,
358 free(client->packfile_path);
359 client->packfile_path = NULL;
361 if (rename(client->packidx_path, packidx_path) == -1) {
362 err = got_error_from_errno3("rename", client->packidx_path,
367 /* Ensure we re-read the pack index list upon next access. */
368 gotd_session.repo->pack_path_mtime.tv_sec = 0;
369 gotd_session.repo->pack_path_mtime.tv_nsec = 0;
371 free(client->packidx_path);
372 client->packidx_path = NULL;
379 static const struct got_error *
380 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
382 struct gotd_imsg_ref_updates_start istart;
385 if (client->nref_updates != -1)
386 return got_error(GOT_ERR_PRIVSEP_MSG);
388 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
389 if (datalen != sizeof(istart))
390 return got_error(GOT_ERR_PRIVSEP_LEN);
391 memcpy(&istart, imsg->data, sizeof(istart));
393 if (istart.nref_updates <= 0)
394 return got_error(GOT_ERR_PRIVSEP_MSG);
396 client->nref_updates = istart.nref_updates;
400 static const struct got_error *
401 validate_namespace(const char *namespace)
403 size_t len = strlen(namespace);
405 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
406 namespace[len - 1] != '/') {
407 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
408 "reference namespace '%s'", namespace);
414 static const struct got_error *
415 queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
416 struct got_repository *repo, struct got_reference *ref)
418 const struct got_error *err = NULL;
419 struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
420 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
421 struct got_pathlist_entry *pe;
422 struct gotd_session_notif *notif;
424 if (iev->ibuf.fd == -1 ||
425 STAILQ_EMPTY(&repo_cfg->notification_targets))
426 return NULL; /* notifications unused */
428 TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
429 const char *refname = pe->path;
430 if (strcmp(got_ref_get_name(ref), refname) == 0)
434 TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
436 const char *namespace = pe->path;
438 err = validate_namespace(namespace);
441 if (strncmp(namespace, got_ref_get_name(ref),
442 strlen(namespace)) == 0)
448 * If a branch or a reference namespace was specified in the
449 * configuration file then only send notifications if a match
452 if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
453 !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
456 notif = calloc(1, sizeof(*notif));
458 return got_error_from_errno("calloc");
463 notif->action = GOTD_NOTIF_ACTION_CREATED;
464 else if (new_id == NULL)
465 notif->action = GOTD_NOTIF_ACTION_REMOVED;
467 notif->action = GOTD_NOTIF_ACTION_CHANGED;
470 memcpy(¬if->old_id, old_id, sizeof(notif->old_id));
472 memcpy(¬if->new_id, new_id, sizeof(notif->new_id));
474 notif->refname = strdup(got_ref_get_name(ref));
475 if (notif->refname == NULL) {
476 err = got_error_from_errno("strdup");
480 STAILQ_INSERT_TAIL(¬ifications, notif, entry);
483 free(notif->refname);
489 /* Forward notification content to the NOTIFY process. */
490 static const struct got_error *
491 forward_notification(struct gotd_session_client *client, struct imsg *imsg)
493 const struct got_error *err = NULL;
494 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
495 struct gotd_session_notif *notif;
496 struct gotd_imsg_notification_content icontent;
497 char *refname = NULL;
499 struct gotd_imsg_notify inotify;
502 memset(&inotify, 0, sizeof(inotify));
504 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
505 if (datalen < sizeof(icontent))
506 return got_error(GOT_ERR_PRIVSEP_LEN);
507 memcpy(&icontent, imsg->data, sizeof(icontent));
508 if (datalen != sizeof(icontent) + icontent.refname_len)
509 return got_error(GOT_ERR_PRIVSEP_LEN);
510 refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
512 return got_error_from_errno("strndup");
514 notif = STAILQ_FIRST(¬ifications);
516 return got_error(GOT_ERR_PRIVSEP_MSG);
518 STAILQ_REMOVE(¬ifications, notif, gotd_session_notif, entry);
520 if (notif->action != icontent.action || notif->fd == -1 ||
521 strcmp(notif->refname, refname) != 0) {
522 err = got_error(GOT_ERR_PRIVSEP_MSG);
525 if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
526 if (memcmp(notif->new_id.sha1, icontent.new_id,
527 SHA1_DIGEST_LENGTH) != 0) {
528 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
529 "received notification content for unknown event");
532 } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
533 if (memcmp(notif->old_id.sha1, icontent.old_id,
534 SHA1_DIGEST_LENGTH) != 0) {
535 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
536 "received notification content for unknown event");
539 } else if (memcmp(notif->old_id.sha1, icontent.old_id,
540 SHA1_DIGEST_LENGTH) != 0 ||
541 memcmp(notif->new_id.sha1, icontent.new_id,
542 SHA1_DIGEST_LENGTH) != 0) {
543 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
544 "received notification content for unknown event");
548 switch (notif->action) {
549 case GOTD_NOTIF_ACTION_CREATED:
552 case GOTD_NOTIF_ACTION_REMOVED:
555 case GOTD_NOTIF_ACTION_CHANGED:
559 err = got_error(GOT_ERR_PRIVSEP_MSG);
563 strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
564 sizeof(inotify.repo_name));
566 snprintf(inotify.subject_line, sizeof(inotify.subject_line),
567 "%s: %s %s %s", gotd_session.repo_cfg->name,
568 client->username, action, notif->refname);
570 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFY,
571 PROC_SESSION_WRITE, notif->fd, &inotify, sizeof(inotify))
573 err = got_error_from_errno("imsg compose NOTIFY");
585 /* Request notification content from REPO_WRITE process. */
586 static const struct got_error *
587 request_notification(struct gotd_session_notif *notif)
589 const struct got_error *err = NULL;
590 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
591 struct gotd_imsg_notification_content icontent;
596 fd = got_opentempfd();
598 return got_error_from_errno("got_opentemp");
600 memset(&icontent, 0, sizeof(icontent));
602 icontent.action = notif->action;
603 memcpy(&icontent.old_id, ¬if->old_id, sizeof(notif->old_id));
604 memcpy(&icontent.new_id, ¬if->new_id, sizeof(notif->new_id));
605 icontent.refname_len = strlen(notif->refname);
607 len = sizeof(icontent) + icontent.refname_len;
608 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
609 PROC_SESSION_WRITE, gotd_session.pid, len);
611 err = got_error_from_errno("imsg_create NOTIFY");
614 if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
615 err = got_error_from_errno("imsg_add NOTIFY");
618 if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
619 err = got_error_from_errno("imsg_add NOTIFY");
624 if (notif->fd == -1) {
625 err = got_error_from_errno("dup");
629 ibuf_fd_set(wbuf, fd);
632 imsg_close(&iev->ibuf, wbuf);
633 gotd_imsg_event_add(iev);
640 static const struct got_error *
641 update_ref(int *shut, struct gotd_session_client *client,
642 const char *repo_path, struct imsg *imsg)
644 const struct got_error *err = NULL;
645 struct got_repository *repo = gotd_session.repo;
646 struct got_reference *ref = NULL;
647 struct gotd_imsg_ref_update iref;
648 struct got_object_id old_id, new_id;
649 struct gotd_session_notif *notif;
650 struct got_object_id *id = NULL;
651 char *refname = NULL;
654 char hex1[SHA1_DIGEST_STRING_LENGTH];
655 char hex2[SHA1_DIGEST_STRING_LENGTH];
657 log_debug("update-ref from uid %d", client->euid);
659 if (client->nref_updates <= 0)
660 return got_error(GOT_ERR_PRIVSEP_MSG);
662 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
663 if (datalen < sizeof(iref))
664 return got_error(GOT_ERR_PRIVSEP_LEN);
665 memcpy(&iref, imsg->data, sizeof(iref));
666 if (datalen != sizeof(iref) + iref.name_len)
667 return got_error(GOT_ERR_PRIVSEP_LEN);
668 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
670 return got_error_from_errno("strndup");
672 log_debug("updating ref %s for uid %d", refname, client->euid);
674 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
675 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
676 err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
681 if (iref.ref_is_new) {
682 err = got_ref_open(&ref, repo, refname, 0);
684 if (err->code != GOT_ERR_NOT_REF)
686 err = got_ref_alloc(&ref, refname, &new_id);
689 err = got_ref_write(ref, repo); /* will lock/unlock */
692 err = queue_notification(NULL, &new_id, repo, ref);
696 err = got_ref_resolve(&id, repo, ref);
699 got_object_id_hex(&new_id, hex1, sizeof(hex1));
700 got_object_id_hex(id, hex2, sizeof(hex2));
701 err = got_error_fmt(GOT_ERR_REF_BUSY,
702 "Addition %s: %s failed; %s: %s has been "
703 "created by someone else while transaction "
705 got_ref_get_name(ref), hex1,
706 got_ref_get_name(ref), hex2);
709 } else if (iref.delete_ref) {
710 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
715 err = got_ref_resolve(&id, repo, ref);
719 if (got_object_id_cmp(id, &old_id) != 0) {
720 got_object_id_hex(&old_id, hex1, sizeof(hex1));
721 got_object_id_hex(id, hex2, sizeof(hex2));
722 err = got_error_fmt(GOT_ERR_REF_BUSY,
723 "Deletion %s: %s failed; %s: %s has been "
724 "created by someone else while transaction "
726 got_ref_get_name(ref), hex1,
727 got_ref_get_name(ref), hex2);
731 err = got_ref_delete(ref, repo);
734 err = queue_notification(&old_id, NULL, repo, ref);
740 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
745 err = got_ref_resolve(&id, repo, ref);
749 if (got_object_id_cmp(id, &old_id) != 0) {
750 got_object_id_hex(&old_id, hex1, sizeof(hex1));
751 got_object_id_hex(id, hex2, sizeof(hex2));
752 err = got_error_fmt(GOT_ERR_REF_BUSY,
753 "Update %s: %s failed; %s: %s has been "
754 "created by someone else while transaction "
756 got_ref_get_name(ref), hex1,
757 got_ref_get_name(ref), hex2);
761 if (got_object_id_cmp(&new_id, &old_id) != 0) {
762 err = got_ref_change_ref(ref, &new_id);
765 err = got_ref_write(ref, repo);
768 err = queue_notification(&old_id, &new_id, repo, ref);
778 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
779 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
780 "could not acquire exclusive file lock for %s",
783 send_ref_update_ng(client, &iref, refname, err->msg);
785 send_ref_update_ok(client, &iref, refname);
787 if (client->nref_updates > 0) {
788 client->nref_updates--;
789 if (client->nref_updates == 0) {
790 send_refs_updated(client);
791 notif = STAILQ_FIRST(¬ifications);
793 gotd_session.state = GOTD_STATE_NOTIFY;
794 err = request_notification(notif);
796 log_warn("could not send notification: "
798 client->flush_disconnect = 1;
801 client->flush_disconnect = 1;
806 const struct got_error *unlock_err;
807 unlock_err = got_ref_unlock(ref);
808 if (unlock_err && err == NULL)
818 static const struct got_error *
819 recv_notification_content(struct imsg *imsg)
821 struct gotd_imsg_notification_content inotif;
824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
825 if (datalen < sizeof(inotif))
826 return got_error(GOT_ERR_PRIVSEP_LEN);
827 memcpy(&inotif, imsg->data, sizeof(inotif));
833 session_dispatch_repo_child(int fd, short event, void *arg)
835 struct gotd_imsgev *iev = arg;
836 struct imsgbuf *ibuf = &iev->ibuf;
837 struct gotd_session_client *client = &gotd_session_client;
842 if (event & EV_READ) {
843 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
844 fatal("imsg_read error");
846 /* Connection closed. */
852 if (event & EV_WRITE) {
853 n = msgbuf_write(&ibuf->w);
854 if (n == -1 && errno != EAGAIN)
855 fatal("msgbuf_write");
857 /* Connection closed. */
864 const struct got_error *err = NULL;
865 uint32_t client_id = 0;
866 int do_disconnect = 0;
867 int do_ref_updates = 0, do_ref_update = 0;
868 int do_packfile_install = 0, do_notify = 0;
870 if ((n = imsg_get(ibuf, &imsg)) == -1)
871 fatal("%s: imsg_get error", __func__);
872 if (n == 0) /* No more messages. */
875 switch (imsg.hdr.type) {
876 case GOTD_IMSG_ERROR:
878 err = gotd_imsg_recv_error(&client_id, &imsg);
880 case GOTD_IMSG_PACKFILE_INSTALL:
881 err = recv_packfile_install(&imsg);
883 do_packfile_install = 1;
885 case GOTD_IMSG_REF_UPDATES_START:
886 err = recv_ref_updates_start(&imsg);
890 case GOTD_IMSG_REF_UPDATE:
891 err = recv_ref_update(&imsg);
895 case GOTD_IMSG_NOTIFY:
896 err = recv_notification_content(&imsg);
901 log_debug("unexpected imsg %d", imsg.hdr.type);
907 disconnect_on_error(client, err);
911 struct gotd_session_notif *notif;
913 if (do_packfile_install)
914 err = install_pack(client,
915 gotd_session.repo->path, &imsg);
916 else if (do_ref_updates)
917 err = begin_ref_updates(client, &imsg);
918 else if (do_ref_update)
919 err = update_ref(&shut, client,
920 gotd_session.repo->path, &imsg);
922 err = forward_notification(client, &imsg);
924 log_warnx("uid %d: %s", client->euid, err->msg);
926 notif = STAILQ_FIRST(¬ifications);
927 if (notif && do_notify) {
928 /* Request content for next notification. */
929 err = request_notification(notif);
931 log_warn("could not send notification: "
941 gotd_imsg_event_add(iev);
943 /* This pipe is dead. Remove its event handler */
945 event_loopexit(NULL);
949 static const struct got_error *
950 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
952 struct gotd_imsg_capabilities icapas;
955 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
956 if (datalen != sizeof(icapas))
957 return got_error(GOT_ERR_PRIVSEP_LEN);
958 memcpy(&icapas, imsg->data, sizeof(icapas));
960 client->ncapa_alloc = icapas.ncapabilities;
961 client->capabilities = calloc(client->ncapa_alloc,
962 sizeof(*client->capabilities));
963 if (client->capabilities == NULL) {
964 client->ncapa_alloc = 0;
965 return got_error_from_errno("calloc");
968 log_debug("expecting %zu capabilities from uid %d",
969 client->ncapa_alloc, client->euid);
973 static const struct got_error *
974 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
976 struct gotd_imsg_capability icapa;
977 struct gotd_client_capability *capa;
979 char *key, *value = NULL;
981 if (client->capabilities == NULL ||
982 client->ncapabilities >= client->ncapa_alloc) {
983 return got_error_msg(GOT_ERR_BAD_REQUEST,
984 "unexpected capability received");
987 memset(&icapa, 0, sizeof(icapa));
989 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
990 if (datalen < sizeof(icapa))
991 return got_error(GOT_ERR_PRIVSEP_LEN);
992 memcpy(&icapa, imsg->data, sizeof(icapa));
994 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
995 return got_error(GOT_ERR_PRIVSEP_LEN);
997 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
999 return got_error_from_errno("strndup");
1000 if (icapa.value_len > 0) {
1001 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1003 if (value == NULL) {
1005 return got_error_from_errno("strndup");
1009 capa = &client->capabilities[client->ncapabilities++];
1011 capa->value = value;
1014 log_debug("uid %d: capability %s=%s", client->euid, key, value);
1016 log_debug("uid %d: capability %s", client->euid, key);
1021 static const struct got_error *
1022 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1024 const struct got_error *err = NULL;
1025 struct gotd_imsg_ref_update ireq;
1026 struct gotd_imsg_ref_update *iref = NULL;
1029 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1030 if (datalen < sizeof(ireq))
1031 return got_error(GOT_ERR_PRIVSEP_LEN);
1032 memcpy(&ireq, imsg->data, sizeof(ireq));
1033 if (datalen != sizeof(ireq) + ireq.name_len)
1034 return got_error(GOT_ERR_PRIVSEP_LEN);
1036 iref = malloc(datalen);
1038 return got_error_from_errno("malloc");
1039 memcpy(iref, imsg->data, datalen);
1041 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1042 GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1043 iref, datalen) == -1)
1044 err = got_error_from_errno("imsg compose REF_UPDATE");
1050 client_has_capability(struct gotd_session_client *client, const char *capastr)
1052 struct gotd_client_capability *capa;
1055 if (client->ncapabilities == 0)
1058 for (i = 0; i < client->ncapabilities; i++) {
1059 capa = &client->capabilities[i];
1060 if (strcmp(capa->key, capastr) == 0)
1067 static const struct got_error *
1068 recv_packfile(struct gotd_session_client *client)
1070 const struct got_error *err = NULL;
1071 struct gotd_imsg_recv_packfile ipack;
1072 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1073 int packfd = -1, idxfd = -1;
1074 int pipe[2] = { -1, -1 };
1076 if (client->packfile_path) {
1077 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1078 "uid %d already has a pack file", client->euid);
1081 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1082 return got_error_from_errno("socketpair");
1084 /* Send pack pipe end 0 to repo child process. */
1085 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1086 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1088 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1094 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1095 if (gotd_imsg_compose_event(&client->iev,
1096 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1098 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1101 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1102 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1103 client->euid) == -1) {
1104 err = got_error_from_errno("asprintf");
1108 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1111 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1112 err = got_error_from_errno2("fchmod", pack_path);
1117 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1118 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1119 client->euid) == -1) {
1120 err = got_error_from_errno("asprintf");
1124 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1127 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1128 err = got_error_from_errno2("fchmod", idx_path);
1132 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1133 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1134 idxfd, NULL, 0) == -1) {
1135 err = got_error_from_errno("imsg compose PACKIDX_FILE");
1141 memset(&ipack, 0, sizeof(ipack));
1142 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1143 ipack.report_status = 1;
1145 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1146 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1147 &ipack, sizeof(ipack)) == -1) {
1148 err = got_error_from_errno("imsg compose RECV_PACKFILE");
1156 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1157 err = got_error_from_errno("close");
1158 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1159 err = got_error_from_errno("close");
1160 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1161 err = got_error_from_errno("close");
1162 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1163 err = got_error_from_errno("close");
1168 client->packfile_path = pack_path;
1169 client->packidx_path = idx_path;
1175 session_dispatch_client(int fd, short events, void *arg)
1177 struct gotd_imsgev *iev = arg;
1178 struct imsgbuf *ibuf = &iev->ibuf;
1179 struct gotd_session_client *client = &gotd_session_client;
1180 const struct got_error *err = NULL;
1184 if (events & EV_WRITE) {
1185 while (ibuf->w.queued) {
1186 n = msgbuf_write(&ibuf->w);
1187 if (n == -1 && errno == EPIPE) {
1189 * The client has closed its socket.
1190 * This can happen when Git clients are
1191 * done sending pack file data.
1193 msgbuf_clear(&ibuf->w);
1195 } else if (n == -1 && errno != EAGAIN) {
1196 err = got_error_from_errno("imsg_flush");
1197 disconnect_on_error(client, err);
1201 /* Connection closed. */
1202 err = got_error(GOT_ERR_EOF);
1203 disconnect_on_error(client, err);
1208 if (client->flush_disconnect) {
1214 if ((events & EV_READ) == 0)
1217 memset(&imsg, 0, sizeof(imsg));
1219 while (err == NULL) {
1220 err = gotd_imsg_recv(&imsg, ibuf, 0);
1222 if (err->code == GOT_ERR_PRIVSEP_READ)
1224 else if (err->code == GOT_ERR_EOF &&
1225 gotd_session.state ==
1226 GOTD_STATE_EXPECT_CAPABILITIES) {
1228 * The client has closed its socket before
1229 * sending its capability announcement.
1230 * This can happen when Git clients have
1231 * no ref-updates to send.
1233 disconnect_on_error(client, err);
1239 evtimer_del(&client->tmo);
1241 switch (imsg.hdr.type) {
1242 case GOTD_IMSG_CAPABILITIES:
1243 if (gotd_session.state !=
1244 GOTD_STATE_EXPECT_CAPABILITIES) {
1245 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1246 "unexpected capabilities received");
1249 log_debug("receiving capabilities from uid %d",
1251 err = recv_capabilities(client, &imsg);
1253 case GOTD_IMSG_CAPABILITY:
1254 if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1255 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1256 "unexpected capability received");
1259 err = recv_capability(client, &imsg);
1260 if (err || client->ncapabilities < client->ncapa_alloc)
1262 gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1263 client->accept_flush_pkt = 1;
1264 log_debug("uid %d: expecting ref-update-lines",
1267 case GOTD_IMSG_REF_UPDATE:
1268 if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1269 gotd_session.state !=
1270 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1271 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1272 "unexpected ref-update-line received");
1275 log_debug("received ref-update-line from uid %d",
1277 err = forward_ref_update(client, &imsg);
1280 gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1281 client->accept_flush_pkt = 1;
1283 case GOTD_IMSG_FLUSH:
1284 if (gotd_session.state !=
1285 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1286 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1287 "unexpected flush-pkt received");
1290 if (!client->accept_flush_pkt) {
1291 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1292 "unexpected flush-pkt received");
1297 * Accept just one flush packet at a time.
1298 * Future client state transitions will set this flag
1299 * again if another flush packet is expected.
1301 client->accept_flush_pkt = 0;
1303 log_debug("received flush-pkt from uid %d",
1305 if (gotd_session.state ==
1306 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1307 gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1308 log_debug("uid %d: expecting packfile",
1310 err = recv_packfile(client);
1312 /* should not happen, see above */
1313 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1314 "unexpected client state");
1319 log_debug("unexpected imsg %d", imsg.hdr.type);
1320 err = got_error(GOT_ERR_PRIVSEP_MSG);
1328 if (err->code != GOT_ERR_EOF ||
1329 gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1330 disconnect_on_error(client, err);
1332 gotd_imsg_event_add(iev);
1333 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1337 static const struct got_error *
1338 list_refs_request(void)
1340 static const struct got_error *err;
1341 struct gotd_session_client *client = &gotd_session_client;
1342 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1345 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1346 return got_error(GOT_ERR_PRIVSEP_MSG);
1348 fd = dup(client->fd);
1350 return got_error_from_errno("dup");
1352 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1353 PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1354 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1359 gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1360 log_debug("uid %d: expecting capabilities", client->euid);
1364 static const struct got_error *
1365 recv_connect(struct imsg *imsg)
1367 struct gotd_session_client *client = &gotd_session_client;
1368 struct gotd_imsg_connect iconnect;
1371 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1372 return got_error(GOT_ERR_PRIVSEP_MSG);
1374 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1375 if (datalen < sizeof(iconnect))
1376 return got_error(GOT_ERR_PRIVSEP_LEN);
1377 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1378 if (iconnect.username_len == 0 ||
1379 datalen != sizeof(iconnect) + iconnect.username_len)
1380 return got_error(GOT_ERR_PRIVSEP_LEN);
1382 client->euid = iconnect.euid;
1383 client->egid = iconnect.egid;
1384 client->fd = imsg_get_fd(imsg);
1385 if (client->fd == -1)
1386 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1388 client->username = strndup(imsg->data + sizeof(iconnect),
1389 iconnect.username_len);
1390 if (client->username == NULL)
1391 return got_error_from_errno("strndup");
1393 imsg_init(&client->iev.ibuf, client->fd);
1394 client->iev.handler = session_dispatch_client;
1395 client->iev.events = EV_READ;
1396 client->iev.handler_arg = NULL;
1397 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1398 session_dispatch_client, &client->iev);
1399 gotd_imsg_event_add(&client->iev);
1400 evtimer_set(&client->tmo, gotd_request_timeout, client);
1406 session_dispatch_notifier(int fd, short event, void *arg)
1408 const struct got_error *err;
1409 struct gotd_session_client *client = &gotd_session_client;
1410 struct gotd_imsgev *iev = arg;
1411 struct imsgbuf *ibuf = &iev->ibuf;
1415 struct gotd_session_notif *notif;
1417 if (event & EV_READ) {
1418 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1419 fatal("imsg_read error");
1421 /* Connection closed. */
1427 if (event & EV_WRITE) {
1428 n = msgbuf_write(&ibuf->w);
1429 if (n == -1 && errno != EAGAIN)
1430 fatal("msgbuf_write");
1432 /* Connection closed. */
1439 if ((n = imsg_get(ibuf, &imsg)) == -1)
1440 fatal("%s: imsg_get error", __func__);
1441 if (n == 0) /* No more messages. */
1444 switch (imsg.hdr.type) {
1445 case GOTD_IMSG_NOTIFICATION_SENT:
1446 if (gotd_session.state != GOTD_STATE_NOTIFY) {
1447 log_warn("unexpected imsg %d", imsg.hdr.type);
1450 notif = STAILQ_FIRST(¬ifications);
1451 if (notif == NULL) {
1453 break; /* NOTREACHED */
1455 /* Request content for the next notification. */
1456 err = request_notification(notif);
1458 log_warn("could not send notification: %s",
1464 log_debug("unexpected imsg %d", imsg.hdr.type);
1472 gotd_imsg_event_add(iev);
1474 /* This pipe is dead. Remove its event handler */
1475 event_del(&iev->ev);
1476 imsg_clear(&iev->ibuf);
1477 imsg_init(&iev->ibuf, -1);
1481 static const struct got_error *
1482 recv_notifier(struct imsg *imsg)
1484 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1485 struct gotd_session_client *client = &gotd_session_client;
1489 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1490 return got_error(GOT_ERR_PRIVSEP_MSG);
1492 /* We should already have received a pipe to the listener. */
1493 if (client->fd == -1)
1494 return got_error(GOT_ERR_PRIVSEP_MSG);
1496 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1498 return got_error(GOT_ERR_PRIVSEP_LEN);
1500 fd = imsg_get_fd(imsg);
1502 return NULL; /* notifications unused */
1504 imsg_init(&iev->ibuf, fd);
1505 iev->handler = session_dispatch_notifier;
1506 iev->events = EV_READ;
1507 iev->handler_arg = NULL;
1508 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1509 session_dispatch_notifier, iev);
1510 gotd_imsg_event_add(iev);
1515 static const struct got_error *
1516 recv_repo_child(struct imsg *imsg)
1518 struct gotd_imsg_connect_repo_child ichild;
1519 struct gotd_session_client *client = &gotd_session_client;
1523 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1524 return got_error(GOT_ERR_PRIVSEP_MSG);
1526 /* We should already have received a pipe to the listener. */
1527 if (client->fd == -1)
1528 return got_error(GOT_ERR_PRIVSEP_MSG);
1530 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1531 if (datalen != sizeof(ichild))
1532 return got_error(GOT_ERR_PRIVSEP_LEN);
1534 memcpy(&ichild, imsg->data, sizeof(ichild));
1536 if (ichild.proc_id != PROC_REPO_WRITE)
1537 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1538 "bad child process type");
1540 fd = imsg_get_fd(imsg);
1542 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1544 imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1545 gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1546 gotd_session.repo_child_iev.events = EV_READ;
1547 gotd_session.repo_child_iev.handler_arg = NULL;
1548 event_set(&gotd_session.repo_child_iev.ev,
1549 gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1550 session_dispatch_repo_child, &gotd_session.repo_child_iev);
1551 gotd_imsg_event_add(&gotd_session.repo_child_iev);
1553 /* The "recvfd" pledge promise is no longer needed. */
1554 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1561 session_dispatch(int fd, short event, void *arg)
1563 struct gotd_imsgev *iev = arg;
1564 struct imsgbuf *ibuf = &iev->ibuf;
1565 struct gotd_session_client *client = &gotd_session_client;
1570 if (event & EV_READ) {
1571 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1572 fatal("imsg_read error");
1574 /* Connection closed. */
1580 if (event & EV_WRITE) {
1581 n = msgbuf_write(&ibuf->w);
1582 if (n == -1 && errno != EAGAIN)
1583 fatal("msgbuf_write");
1585 /* Connection closed. */
1592 const struct got_error *err = NULL;
1593 uint32_t client_id = 0;
1594 int do_disconnect = 0, do_list_refs = 0;
1596 if ((n = imsg_get(ibuf, &imsg)) == -1)
1597 fatal("%s: imsg_get error", __func__);
1598 if (n == 0) /* No more messages. */
1601 switch (imsg.hdr.type) {
1602 case GOTD_IMSG_ERROR:
1604 err = gotd_imsg_recv_error(&client_id, &imsg);
1606 case GOTD_IMSG_CONNECT:
1607 err = recv_connect(&imsg);
1609 case GOTD_IMSG_DISCONNECT:
1612 case GOTD_IMSG_CONNECT_NOTIFIER:
1613 err = recv_notifier(&imsg);
1615 case GOTD_IMSG_CONNECT_REPO_CHILD:
1616 err = recv_repo_child(&imsg);
1622 log_debug("unexpected imsg %d", imsg.hdr.type);
1627 if (do_disconnect) {
1629 disconnect_on_error(client, err);
1632 } else if (do_list_refs)
1633 err = list_refs_request();
1636 log_warnx("uid %d: %s", client->euid, err->msg);
1640 gotd_imsg_event_add(iev);
1642 /* This pipe is dead. Remove its event handler */
1643 event_del(&iev->ev);
1644 event_loopexit(NULL);
1649 session_write_main(const char *title, const char *repo_path,
1650 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1651 struct gotd_repo *repo_cfg)
1653 const struct got_error *err = NULL;
1654 struct event evsigint, evsigterm, evsighup, evsigusr1;
1656 STAILQ_INIT(¬ifications);
1658 gotd_session.title = title;
1659 gotd_session.pid = getpid();
1660 gotd_session.pack_fds = pack_fds;
1661 gotd_session.temp_fds = temp_fds;
1662 memcpy(&gotd_session.request_timeout, request_timeout,
1663 sizeof(gotd_session.request_timeout));
1664 gotd_session.repo_cfg = repo_cfg;
1666 imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1668 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1671 if (!got_repo_is_bare(gotd_session.repo)) {
1672 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1673 "bare git repository required");
1677 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1679 signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1680 signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1681 signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1682 signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1683 signal(SIGPIPE, SIG_IGN);
1685 signal_add(&evsigint, NULL);
1686 signal_add(&evsigterm, NULL);
1687 signal_add(&evsighup, NULL);
1688 signal_add(&evsigusr1, NULL);
1690 gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1692 gotd_session_client.fd = -1;
1693 gotd_session_client.nref_updates = -1;
1694 gotd_session_client.delta_cache_fd = -1;
1695 gotd_session_client.accept_flush_pkt = 1;
1697 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1698 gotd_session.parent_iev.handler = session_dispatch;
1699 gotd_session.parent_iev.events = EV_READ;
1700 gotd_session.parent_iev.handler_arg = NULL;
1701 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1702 EV_READ, session_dispatch, &gotd_session.parent_iev);
1703 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1704 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1705 -1, NULL, 0) == -1) {
1706 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1713 log_warnx("%s: %s", title, err->msg);
1714 session_write_shutdown();
1718 session_write_shutdown(void)
1720 struct gotd_session_notif *notif;
1722 log_debug("%s: shutting down", gotd_session.title);
1724 while (!STAILQ_EMPTY(¬ifications)) {
1725 notif = STAILQ_FIRST(¬ifications);
1726 STAILQ_REMOVE_HEAD(¬ifications, entry);
1727 if (notif->fd != -1)
1729 free(notif->refname);
1733 if (gotd_session.repo)
1734 got_repo_close(gotd_session.repo);
1735 got_repo_pack_fds_close(gotd_session.pack_fds);
1736 got_repo_temp_fds_close(gotd_session.temp_fds);
1737 free(gotd_session_client.username);