2 * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.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"
54 #include "session_write.h"
56 struct gotd_session_notif {
57 STAILQ_ENTRY(gotd_session_notif) entry;
59 enum gotd_notification_action action;
61 struct got_object_id old_id;
62 struct got_object_id new_id;
64 STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
66 enum gotd_session_write_state {
67 GOTD_STATE_EXPECT_LIST_REFS,
68 GOTD_STATE_EXPECT_CAPABILITIES,
69 GOTD_STATE_EXPECT_REF_UPDATE,
70 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 GOTD_STATE_EXPECT_PACKFILE,
75 static struct gotd_session_write {
78 struct got_repository *repo;
79 struct gotd_repo *repo_cfg;
82 struct gotd_imsgev parent_iev;
83 struct gotd_imsgev notifier_iev;
84 struct timeval request_timeout;
85 enum gotd_session_write_state state;
86 struct gotd_imsgev repo_child_iev;
89 static struct gotd_session_client {
90 struct gotd_client_capability *capabilities;
96 struct gotd_imsgev iev;
104 int accept_flush_pkt;
105 int flush_disconnect;
106 } gotd_session_client;
108 static void session_write_shutdown(void);
111 disconnect(struct gotd_session_client *client)
113 log_debug("uid %d: disconnecting", client->euid);
115 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
116 GOTD_IMSG_DISCONNECT, PROC_SESSION_WRITE, -1, NULL, 0) == -1)
117 log_warn("imsg compose DISCONNECT");
119 imsg_clear(&gotd_session.repo_child_iev.ibuf);
120 event_del(&gotd_session.repo_child_iev.ev);
121 evtimer_del(&client->tmo);
123 if (client->delta_cache_fd != -1)
124 close(client->delta_cache_fd);
125 if (client->packfile_path) {
126 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
127 log_warn("unlink %s: ", client->packfile_path);
128 free(client->packfile_path);
130 if (client->packidx_path) {
131 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
132 log_warn("unlink %s: ", client->packidx_path);
133 free(client->packidx_path);
135 free(client->capabilities);
137 session_write_shutdown();
141 disconnect_on_error(struct gotd_session_client *client,
142 const struct got_error *err)
146 if (err->code != GOT_ERR_EOF) {
147 log_warnx("uid %d: %s", client->euid, err->msg);
148 imsg_init(&ibuf, client->fd);
149 gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_WRITE, err);
157 gotd_request_timeout(int fd, short events, void *arg)
159 struct gotd_session_client *client = arg;
161 log_warnx("disconnecting uid %d due to timeout", client->euid);
166 session_write_sighdlr(int sig, short event, void *arg)
169 * Normal signal handler rules don't apply because libevent
175 log_info("%s: ignoring SIGHUP", __func__);
178 log_info("%s: ignoring SIGUSR1", __func__);
182 session_write_shutdown();
186 fatalx("unexpected signal");
190 static const struct got_error *
191 recv_packfile_install(struct imsg *imsg)
193 struct gotd_imsg_packfile_install inst;
196 log_debug("packfile-install received");
198 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 if (datalen != sizeof(inst))
200 return got_error(GOT_ERR_PRIVSEP_LEN);
201 memcpy(&inst, imsg->data, sizeof(inst));
206 static const struct got_error *
207 recv_ref_updates_start(struct imsg *imsg)
209 struct gotd_imsg_ref_updates_start istart;
212 log_debug("ref-updates-start received");
214 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 if (datalen != sizeof(istart))
216 return got_error(GOT_ERR_PRIVSEP_LEN);
217 memcpy(&istart, imsg->data, sizeof(istart));
222 static const struct got_error *
223 recv_ref_update(struct imsg *imsg)
225 struct gotd_imsg_ref_update iref;
228 log_debug("ref-update received");
230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 if (datalen < sizeof(iref))
232 return got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&iref, imsg->data, sizeof(iref));
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;
247 memset(&iok, 0, sizeof(iok));
248 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
249 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
250 iok.name_len = strlen(refname);
252 len = sizeof(iok) + iok.name_len;
253 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
254 PROC_SESSION_WRITE, gotd_session.pid, len);
256 return got_error_from_errno("imsg_create REF_UPDATE_OK");
258 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
259 return got_error_from_errno("imsg_add REF_UPDATE_OK");
260 if (imsg_add(wbuf, refname, iok.name_len) == -1)
261 return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 imsg_close(&iev->ibuf, wbuf);
264 gotd_imsg_event_add(iev);
269 send_refs_updated(struct gotd_session_client *client)
271 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 PROC_SESSION_WRITE, -1, NULL, 0) == -1)
273 log_warn("imsg compose REFS_UPDATED");
276 static const struct got_error *
277 send_ref_update_ng(struct gotd_session_client *client,
278 struct gotd_imsg_ref_update *iref, const char *refname,
281 const struct got_error *ng_err;
282 struct gotd_imsg_ref_update_ng ing;
283 struct gotd_imsgev *iev = &client->iev;
287 memset(&ing, 0, sizeof(ing));
288 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 ing.name_len = strlen(refname);
292 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 ing.reason_len = strlen(ng_err->msg);
295 len = sizeof(ing) + ing.name_len + ing.reason_len;
296 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 PROC_SESSION_WRITE, gotd_session.pid, len);
299 return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 imsg_close(&iev->ibuf, wbuf);
309 gotd_imsg_event_add(iev);
313 static const struct got_error *
314 install_pack(struct gotd_session_client *client, const char *repo_path,
317 const struct got_error *err = NULL;
318 struct gotd_imsg_packfile_install inst;
319 char hex[SHA1_DIGEST_STRING_LENGTH];
321 char *packfile_path = NULL, *packidx_path = NULL;
323 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
324 if (datalen != sizeof(inst))
325 return got_error(GOT_ERR_PRIVSEP_LEN);
326 memcpy(&inst, imsg->data, sizeof(inst));
328 if (client->packfile_path == NULL)
329 return got_error_msg(GOT_ERR_BAD_REQUEST,
330 "client has no pack file");
331 if (client->packidx_path == NULL)
332 return got_error_msg(GOT_ERR_BAD_REQUEST,
333 "client has no pack file index");
335 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
336 return got_error_msg(GOT_ERR_NO_SPACE,
337 "could not convert pack file SHA1 to hex");
339 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
340 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
341 err = got_error_from_errno("asprintf");
345 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
346 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 err = got_error_from_errno("asprintf");
351 if (rename(client->packfile_path, packfile_path) == -1) {
352 err = got_error_from_errno3("rename", client->packfile_path,
357 free(client->packfile_path);
358 client->packfile_path = NULL;
360 if (rename(client->packidx_path, packidx_path) == -1) {
361 err = got_error_from_errno3("rename", client->packidx_path,
366 /* Ensure we re-read the pack index list upon next access. */
367 gotd_session.repo->pack_path_mtime.tv_sec = 0;
368 gotd_session.repo->pack_path_mtime.tv_nsec = 0;
370 free(client->packidx_path);
371 client->packidx_path = NULL;
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;
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;
399 static const struct got_error *
400 validate_namespace(const char *namespace)
402 size_t len = strlen(namespace);
404 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
405 namespace[len - 1] != '/') {
406 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
407 "reference namespace '%s'", namespace);
413 static const struct got_error *
414 queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
415 struct got_repository *repo, struct got_reference *ref)
417 const struct got_error *err = NULL;
418 struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
419 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
420 struct got_pathlist_entry *pe;
421 struct gotd_session_notif *notif;
423 if (iev->ibuf.fd == -1 ||
424 STAILQ_EMPTY(&repo_cfg->notification_targets))
425 return NULL; /* notifications unused */
427 TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
428 const char *refname = pe->path;
429 if (strcmp(got_ref_get_name(ref), refname) == 0)
433 TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
435 const char *namespace = pe->path;
437 err = validate_namespace(namespace);
440 if (strncmp(namespace, got_ref_get_name(ref),
441 strlen(namespace)) == 0)
447 * If a branch or a reference namespace was specified in the
448 * configuration file then only send notifications if a match
451 if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
452 !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
455 notif = calloc(1, sizeof(*notif));
457 return got_error_from_errno("calloc");
462 notif->action = GOTD_NOTIF_ACTION_CREATED;
463 else if (new_id == NULL)
464 notif->action = GOTD_NOTIF_ACTION_REMOVED;
466 notif->action = GOTD_NOTIF_ACTION_CHANGED;
469 memcpy(¬if->old_id, old_id, sizeof(notif->old_id));
471 memcpy(¬if->new_id, new_id, sizeof(notif->new_id));
473 notif->refname = strdup(got_ref_get_name(ref));
474 if (notif->refname == NULL) {
475 err = got_error_from_errno("strdup");
479 STAILQ_INSERT_TAIL(¬ifications, notif, entry);
482 free(notif->refname);
488 /* Forward notification content to the NOTIFY process. */
489 static const struct got_error *
490 forward_notification(struct gotd_session_client *client, struct imsg *imsg)
492 const struct got_error *err = NULL;
493 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
494 struct gotd_session_notif *notif;
495 struct gotd_imsg_notification_content icontent;
496 char *refname = NULL, *id_str = NULL;
498 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:
551 err = got_object_id_str(&id_str, ¬if->new_id);
555 case GOTD_NOTIF_ACTION_REMOVED:
557 err = got_object_id_str(&id_str, ¬if->old_id);
561 case GOTD_NOTIF_ACTION_CHANGED:
563 err = got_object_id_str(&id_str, ¬if->new_id);
568 err = got_error(GOT_ERR_PRIVSEP_MSG);
572 strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
573 sizeof(inotify.repo_name));
575 snprintf(inotify.subject_line, sizeof(inotify.subject_line),
576 "%s: %s %s %s: %.12s", gotd_session.repo_cfg->name,
577 client->username, action, notif->refname, id_str);
579 inotify.username_len = strlen(client->username);
580 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
581 PROC_SESSION_WRITE, gotd_session.pid,
582 sizeof(inotify) + inotify.username_len);
584 err = got_error_from_errno("imsg_create NOTIFY");
587 if (imsg_add(wbuf, &inotify, sizeof(inotify)) == -1) {
588 err = got_error_from_errno("imsg_add NOTIFY");
591 if (imsg_add(wbuf, client->username, inotify.username_len) == -1) {
592 err = got_error_from_errno("imsg_add NOTIFY");
596 ibuf_fd_set(wbuf, notif->fd);
599 imsg_close(&iev->ibuf, wbuf);
600 gotd_imsg_event_add(iev);
610 /* Request notification content from REPO_WRITE process. */
611 static const struct got_error *
612 request_notification(struct gotd_session_notif *notif)
614 const struct got_error *err = NULL;
615 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
616 struct gotd_imsg_notification_content icontent;
621 fd = got_opentempfd();
623 return got_error_from_errno("got_opentemp");
625 memset(&icontent, 0, sizeof(icontent));
627 icontent.action = notif->action;
628 memcpy(&icontent.old_id, ¬if->old_id, sizeof(notif->old_id));
629 memcpy(&icontent.new_id, ¬if->new_id, sizeof(notif->new_id));
630 icontent.refname_len = strlen(notif->refname);
632 len = sizeof(icontent) + icontent.refname_len;
633 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
634 PROC_SESSION_WRITE, gotd_session.pid, len);
636 err = got_error_from_errno("imsg_create NOTIFY");
639 if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
640 err = got_error_from_errno("imsg_add NOTIFY");
643 if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
644 err = got_error_from_errno("imsg_add NOTIFY");
649 if (notif->fd == -1) {
650 err = got_error_from_errno("dup");
654 ibuf_fd_set(wbuf, fd);
657 imsg_close(&iev->ibuf, wbuf);
658 gotd_imsg_event_add(iev);
665 static const struct got_error *
666 update_ref(int *shut, struct gotd_session_client *client,
667 const char *repo_path, struct imsg *imsg)
669 const struct got_error *err = NULL;
670 struct got_repository *repo = gotd_session.repo;
671 struct got_reference *ref = NULL;
672 struct gotd_imsg_ref_update iref;
673 struct got_object_id old_id, new_id;
674 struct gotd_session_notif *notif;
675 struct got_object_id *id = NULL;
676 char *refname = NULL;
679 char hex1[SHA1_DIGEST_STRING_LENGTH];
680 char hex2[SHA1_DIGEST_STRING_LENGTH];
682 log_debug("update-ref from uid %d", client->euid);
684 if (client->nref_updates <= 0)
685 return got_error(GOT_ERR_PRIVSEP_MSG);
687 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
688 if (datalen < sizeof(iref))
689 return got_error(GOT_ERR_PRIVSEP_LEN);
690 memcpy(&iref, imsg->data, sizeof(iref));
691 if (datalen != sizeof(iref) + iref.name_len)
692 return got_error(GOT_ERR_PRIVSEP_LEN);
693 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
695 return got_error_from_errno("strndup");
697 log_debug("updating ref %s for uid %d", refname, client->euid);
699 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
700 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
701 err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
706 if (iref.ref_is_new) {
707 err = got_ref_open(&ref, repo, refname, 0);
709 if (err->code != GOT_ERR_NOT_REF)
711 err = got_ref_alloc(&ref, refname, &new_id);
714 err = got_ref_write(ref, repo); /* will lock/unlock */
717 err = queue_notification(NULL, &new_id, repo, ref);
721 err = got_ref_resolve(&id, repo, ref);
724 got_object_id_hex(&new_id, hex1, sizeof(hex1));
725 got_object_id_hex(id, hex2, sizeof(hex2));
726 err = got_error_fmt(GOT_ERR_REF_BUSY,
727 "Addition %s: %s failed; %s: %s has been "
728 "created by someone else while transaction "
730 got_ref_get_name(ref), hex1,
731 got_ref_get_name(ref), hex2);
734 } else if (iref.delete_ref) {
735 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
740 err = got_ref_resolve(&id, repo, ref);
744 if (got_object_id_cmp(id, &old_id) != 0) {
745 got_object_id_hex(&old_id, hex1, sizeof(hex1));
746 got_object_id_hex(id, hex2, sizeof(hex2));
747 err = got_error_fmt(GOT_ERR_REF_BUSY,
748 "Deletion %s: %s failed; %s: %s has been "
749 "created by someone else while transaction "
751 got_ref_get_name(ref), hex1,
752 got_ref_get_name(ref), hex2);
756 err = got_ref_delete(ref, repo);
759 err = queue_notification(&old_id, NULL, repo, ref);
765 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
770 err = got_ref_resolve(&id, repo, ref);
774 if (got_object_id_cmp(id, &old_id) != 0) {
775 got_object_id_hex(&old_id, hex1, sizeof(hex1));
776 got_object_id_hex(id, hex2, sizeof(hex2));
777 err = got_error_fmt(GOT_ERR_REF_BUSY,
778 "Update %s: %s failed; %s: %s has been "
779 "created by someone else while transaction "
781 got_ref_get_name(ref), hex1,
782 got_ref_get_name(ref), hex2);
786 if (got_object_id_cmp(&new_id, &old_id) != 0) {
787 err = got_ref_change_ref(ref, &new_id);
790 err = got_ref_write(ref, repo);
793 err = queue_notification(&old_id, &new_id, repo, ref);
803 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
804 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
805 "could not acquire exclusive file lock for %s",
808 send_ref_update_ng(client, &iref, refname, err->msg);
810 send_ref_update_ok(client, &iref, refname);
812 if (client->nref_updates > 0) {
813 client->nref_updates--;
814 if (client->nref_updates == 0) {
815 send_refs_updated(client);
816 notif = STAILQ_FIRST(¬ifications);
818 gotd_session.state = GOTD_STATE_NOTIFY;
819 err = request_notification(notif);
821 log_warn("could not send notification: "
823 client->flush_disconnect = 1;
826 client->flush_disconnect = 1;
831 const struct got_error *unlock_err;
832 unlock_err = got_ref_unlock(ref);
833 if (unlock_err && err == NULL)
843 static const struct got_error *
844 recv_notification_content(struct imsg *imsg)
846 struct gotd_imsg_notification_content inotif;
849 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
850 if (datalen < sizeof(inotif))
851 return got_error(GOT_ERR_PRIVSEP_LEN);
852 memcpy(&inotif, imsg->data, sizeof(inotif));
858 session_dispatch_repo_child(int fd, short event, void *arg)
860 struct gotd_imsgev *iev = arg;
861 struct imsgbuf *ibuf = &iev->ibuf;
862 struct gotd_session_client *client = &gotd_session_client;
867 if (event & EV_READ) {
868 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
869 fatal("imsg_read error");
871 /* Connection closed. */
877 if (event & EV_WRITE) {
878 n = msgbuf_write(&ibuf->w);
879 if (n == -1 && errno != EAGAIN)
880 fatal("msgbuf_write");
882 /* Connection closed. */
889 const struct got_error *err = NULL;
890 uint32_t client_id = 0;
891 int do_disconnect = 0;
892 int do_ref_updates = 0, do_ref_update = 0;
893 int do_packfile_install = 0, do_notify = 0;
895 if ((n = imsg_get(ibuf, &imsg)) == -1)
896 fatal("%s: imsg_get error", __func__);
897 if (n == 0) /* No more messages. */
900 switch (imsg.hdr.type) {
901 case GOTD_IMSG_ERROR:
903 err = gotd_imsg_recv_error(&client_id, &imsg);
905 case GOTD_IMSG_PACKFILE_INSTALL:
906 err = recv_packfile_install(&imsg);
908 do_packfile_install = 1;
910 case GOTD_IMSG_REF_UPDATES_START:
911 err = recv_ref_updates_start(&imsg);
915 case GOTD_IMSG_REF_UPDATE:
916 err = recv_ref_update(&imsg);
920 case GOTD_IMSG_NOTIFY:
921 err = recv_notification_content(&imsg);
926 log_debug("unexpected imsg %d", imsg.hdr.type);
930 if (do_disconnect || err) {
932 disconnect_on_error(client, err);
936 struct gotd_session_notif *notif;
938 if (do_packfile_install)
939 err = install_pack(client,
940 gotd_session.repo->path, &imsg);
941 else if (do_ref_updates)
942 err = begin_ref_updates(client, &imsg);
943 else if (do_ref_update)
944 err = update_ref(&shut, client,
945 gotd_session.repo->path, &imsg);
947 err = forward_notification(client, &imsg);
949 log_warnx("uid %d: %s", client->euid, err->msg);
951 notif = STAILQ_FIRST(¬ifications);
952 if (notif && do_notify) {
953 /* Request content for next notification. */
954 err = request_notification(notif);
956 log_warn("could not send notification: "
966 gotd_imsg_event_add(iev);
968 /* This pipe is dead. Remove its event handler */
970 event_loopexit(NULL);
974 static const struct got_error *
975 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
977 struct gotd_imsg_capabilities icapas;
980 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
981 if (datalen != sizeof(icapas))
982 return got_error(GOT_ERR_PRIVSEP_LEN);
983 memcpy(&icapas, imsg->data, sizeof(icapas));
985 client->ncapa_alloc = icapas.ncapabilities;
986 client->capabilities = calloc(client->ncapa_alloc,
987 sizeof(*client->capabilities));
988 if (client->capabilities == NULL) {
989 client->ncapa_alloc = 0;
990 return got_error_from_errno("calloc");
993 log_debug("expecting %zu capabilities from uid %d",
994 client->ncapa_alloc, client->euid);
998 static const struct got_error *
999 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
1001 struct gotd_imsg_capability icapa;
1002 struct gotd_client_capability *capa;
1004 char *key, *value = NULL;
1006 if (client->capabilities == NULL ||
1007 client->ncapabilities >= client->ncapa_alloc) {
1008 return got_error_msg(GOT_ERR_BAD_REQUEST,
1009 "unexpected capability received");
1012 memset(&icapa, 0, sizeof(icapa));
1014 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1015 if (datalen < sizeof(icapa))
1016 return got_error(GOT_ERR_PRIVSEP_LEN);
1017 memcpy(&icapa, imsg->data, sizeof(icapa));
1019 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1020 return got_error(GOT_ERR_PRIVSEP_LEN);
1022 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1024 return got_error_from_errno("strndup");
1025 if (icapa.value_len > 0) {
1026 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1028 if (value == NULL) {
1030 return got_error_from_errno("strndup");
1034 capa = &client->capabilities[client->ncapabilities++];
1036 capa->value = value;
1039 log_debug("uid %d: capability %s=%s", client->euid, key, value);
1041 log_debug("uid %d: capability %s", client->euid, key);
1046 static const struct got_error *
1047 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1049 const struct got_error *err = NULL;
1050 struct gotd_imsg_ref_update ireq;
1051 struct gotd_imsg_ref_update *iref = NULL;
1054 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1055 if (datalen < sizeof(ireq))
1056 return got_error(GOT_ERR_PRIVSEP_LEN);
1057 memcpy(&ireq, imsg->data, sizeof(ireq));
1058 if (datalen != sizeof(ireq) + ireq.name_len)
1059 return got_error(GOT_ERR_PRIVSEP_LEN);
1061 iref = malloc(datalen);
1063 return got_error_from_errno("malloc");
1064 memcpy(iref, imsg->data, datalen);
1066 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1067 GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1068 iref, datalen) == -1)
1069 err = got_error_from_errno("imsg compose REF_UPDATE");
1075 client_has_capability(struct gotd_session_client *client, const char *capastr)
1077 struct gotd_client_capability *capa;
1080 if (client->ncapabilities == 0)
1083 for (i = 0; i < client->ncapabilities; i++) {
1084 capa = &client->capabilities[i];
1085 if (strcmp(capa->key, capastr) == 0)
1092 static const struct got_error *
1093 recv_packfile(struct gotd_session_client *client)
1095 const struct got_error *err = NULL;
1096 struct gotd_imsg_recv_packfile ipack;
1097 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1098 int packfd = -1, idxfd = -1;
1099 int pipe[2] = { -1, -1 };
1101 if (client->packfile_path) {
1102 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1103 "uid %d already has a pack file", client->euid);
1106 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1107 return got_error_from_errno("socketpair");
1109 /* Send pack pipe end 0 to repo child process. */
1110 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1111 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1113 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1119 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1120 if (gotd_imsg_compose_event(&client->iev,
1121 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1123 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1126 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1127 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1128 client->euid) == -1) {
1129 err = got_error_from_errno("asprintf");
1133 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1136 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1137 err = got_error_from_errno2("fchmod", pack_path);
1142 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1143 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1144 client->euid) == -1) {
1145 err = got_error_from_errno("asprintf");
1149 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1152 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1153 err = got_error_from_errno2("fchmod", idx_path);
1157 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1158 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1159 idxfd, NULL, 0) == -1) {
1160 err = got_error_from_errno("imsg compose PACKIDX_FILE");
1166 memset(&ipack, 0, sizeof(ipack));
1167 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1168 ipack.report_status = 1;
1170 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1171 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1172 &ipack, sizeof(ipack)) == -1) {
1173 err = got_error_from_errno("imsg compose RECV_PACKFILE");
1181 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1182 err = got_error_from_errno("close");
1183 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1184 err = got_error_from_errno("close");
1185 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1186 err = got_error_from_errno("close");
1187 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1188 err = got_error_from_errno("close");
1193 client->packfile_path = pack_path;
1194 client->packidx_path = idx_path;
1200 session_dispatch_client(int fd, short events, void *arg)
1202 struct gotd_imsgev *iev = arg;
1203 struct imsgbuf *ibuf = &iev->ibuf;
1204 struct gotd_session_client *client = &gotd_session_client;
1205 const struct got_error *err = NULL;
1209 if (events & EV_WRITE) {
1210 while (ibuf->w.queued) {
1211 n = msgbuf_write(&ibuf->w);
1212 if (n == -1 && errno == EPIPE) {
1214 * The client has closed its socket.
1215 * This can happen when Git clients are
1216 * done sending pack file data.
1218 msgbuf_clear(&ibuf->w);
1220 } else if (n == -1 && errno != EAGAIN) {
1221 err = got_error_from_errno("imsg_flush");
1222 disconnect_on_error(client, err);
1226 /* Connection closed. */
1227 err = got_error(GOT_ERR_EOF);
1228 disconnect_on_error(client, err);
1233 if (client->flush_disconnect) {
1239 if ((events & EV_READ) == 0)
1242 memset(&imsg, 0, sizeof(imsg));
1244 while (err == NULL) {
1245 err = gotd_imsg_recv(&imsg, ibuf, 0);
1247 if (err->code == GOT_ERR_PRIVSEP_READ)
1249 else if (err->code == GOT_ERR_EOF &&
1250 gotd_session.state ==
1251 GOTD_STATE_EXPECT_CAPABILITIES) {
1253 * The client has closed its socket before
1254 * sending its capability announcement.
1255 * This can happen when Git clients have
1256 * no ref-updates to send.
1258 disconnect_on_error(client, err);
1264 evtimer_del(&client->tmo);
1266 switch (imsg.hdr.type) {
1267 case GOTD_IMSG_CAPABILITIES:
1268 if (gotd_session.state !=
1269 GOTD_STATE_EXPECT_CAPABILITIES) {
1270 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1271 "unexpected capabilities received");
1274 log_debug("receiving capabilities from uid %d",
1276 err = recv_capabilities(client, &imsg);
1278 case GOTD_IMSG_CAPABILITY:
1279 if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1280 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1281 "unexpected capability received");
1284 err = recv_capability(client, &imsg);
1285 if (err || client->ncapabilities < client->ncapa_alloc)
1287 gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1288 client->accept_flush_pkt = 1;
1289 log_debug("uid %d: expecting ref-update-lines",
1292 case GOTD_IMSG_REF_UPDATE:
1293 if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1294 gotd_session.state !=
1295 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1296 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1297 "unexpected ref-update-line received");
1300 log_debug("received ref-update-line from uid %d",
1302 err = forward_ref_update(client, &imsg);
1305 gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1306 client->accept_flush_pkt = 1;
1308 case GOTD_IMSG_FLUSH:
1309 if (gotd_session.state !=
1310 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1311 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1312 "unexpected flush-pkt received");
1315 if (!client->accept_flush_pkt) {
1316 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1317 "unexpected flush-pkt received");
1322 * Accept just one flush packet at a time.
1323 * Future client state transitions will set this flag
1324 * again if another flush packet is expected.
1326 client->accept_flush_pkt = 0;
1328 log_debug("received flush-pkt from uid %d",
1330 if (gotd_session.state ==
1331 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1332 gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1333 log_debug("uid %d: expecting packfile",
1335 err = recv_packfile(client);
1337 /* should not happen, see above */
1338 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1339 "unexpected client state");
1344 log_debug("unexpected imsg %d", imsg.hdr.type);
1345 err = got_error(GOT_ERR_PRIVSEP_MSG);
1353 if (err->code != GOT_ERR_EOF ||
1354 (gotd_session.state != GOTD_STATE_EXPECT_PACKFILE &&
1355 gotd_session.state != GOTD_STATE_NOTIFY))
1356 disconnect_on_error(client, err);
1358 gotd_imsg_event_add(iev);
1359 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1363 static const struct got_error *
1364 list_refs_request(void)
1366 static const struct got_error *err;
1367 struct gotd_session_client *client = &gotd_session_client;
1368 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1371 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1372 return got_error(GOT_ERR_PRIVSEP_MSG);
1374 fd = dup(client->fd);
1376 return got_error_from_errno("dup");
1378 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1379 PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1380 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1385 gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1386 log_debug("uid %d: expecting capabilities", client->euid);
1390 static const struct got_error *
1391 recv_connect(struct imsg *imsg)
1393 struct gotd_session_client *client = &gotd_session_client;
1394 struct gotd_imsg_connect iconnect;
1397 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1398 return got_error(GOT_ERR_PRIVSEP_MSG);
1400 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1401 if (datalen < sizeof(iconnect))
1402 return got_error(GOT_ERR_PRIVSEP_LEN);
1403 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1404 if (iconnect.username_len == 0 ||
1405 datalen != sizeof(iconnect) + iconnect.username_len)
1406 return got_error(GOT_ERR_PRIVSEP_LEN);
1408 client->euid = iconnect.euid;
1409 client->egid = iconnect.egid;
1410 client->fd = imsg_get_fd(imsg);
1411 if (client->fd == -1)
1412 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1414 client->username = strndup(imsg->data + sizeof(iconnect),
1415 iconnect.username_len);
1416 if (client->username == NULL)
1417 return got_error_from_errno("strndup");
1419 imsg_init(&client->iev.ibuf, client->fd);
1420 client->iev.handler = session_dispatch_client;
1421 client->iev.events = EV_READ;
1422 client->iev.handler_arg = NULL;
1423 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1424 session_dispatch_client, &client->iev);
1425 gotd_imsg_event_add(&client->iev);
1426 evtimer_set(&client->tmo, gotd_request_timeout, client);
1427 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1433 session_dispatch_notifier(int fd, short event, void *arg)
1435 const struct got_error *err;
1436 struct gotd_session_client *client = &gotd_session_client;
1437 struct gotd_imsgev *iev = arg;
1438 struct imsgbuf *ibuf = &iev->ibuf;
1442 struct gotd_session_notif *notif;
1444 if (event & EV_READ) {
1445 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1446 fatal("imsg_read error");
1448 /* Connection closed. */
1454 if (event & EV_WRITE) {
1455 n = msgbuf_write(&ibuf->w);
1456 if (n == -1 && errno != EAGAIN)
1457 fatal("msgbuf_write");
1459 /* Connection closed. */
1466 if ((n = imsg_get(ibuf, &imsg)) == -1)
1467 fatal("%s: imsg_get error", __func__);
1468 if (n == 0) /* No more messages. */
1471 switch (imsg.hdr.type) {
1472 case GOTD_IMSG_NOTIFICATION_SENT:
1473 if (gotd_session.state != GOTD_STATE_NOTIFY) {
1474 log_warn("unexpected imsg %d", imsg.hdr.type);
1477 notif = STAILQ_FIRST(¬ifications);
1478 if (notif == NULL) {
1480 break; /* NOTREACHED */
1482 /* Request content for the next notification. */
1483 err = request_notification(notif);
1485 log_warn("could not send notification: %s",
1491 log_debug("unexpected imsg %d", imsg.hdr.type);
1499 gotd_imsg_event_add(iev);
1501 /* This pipe is dead. Remove its event handler */
1502 event_del(&iev->ev);
1503 imsg_clear(&iev->ibuf);
1504 imsg_init(&iev->ibuf, -1);
1508 static const struct got_error *
1509 recv_notifier(struct imsg *imsg)
1511 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1512 struct gotd_session_client *client = &gotd_session_client;
1516 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1517 return got_error(GOT_ERR_PRIVSEP_MSG);
1519 /* We should already have received a pipe to the listener. */
1520 if (client->fd == -1)
1521 return got_error(GOT_ERR_PRIVSEP_MSG);
1523 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1525 return got_error(GOT_ERR_PRIVSEP_LEN);
1527 fd = imsg_get_fd(imsg);
1529 return NULL; /* notifications unused */
1531 imsg_init(&iev->ibuf, fd);
1532 iev->handler = session_dispatch_notifier;
1533 iev->events = EV_READ;
1534 iev->handler_arg = NULL;
1535 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1536 session_dispatch_notifier, iev);
1537 gotd_imsg_event_add(iev);
1542 static const struct got_error *
1543 recv_repo_child(struct imsg *imsg)
1545 struct gotd_imsg_connect_repo_child ichild;
1546 struct gotd_session_client *client = &gotd_session_client;
1550 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1551 return got_error(GOT_ERR_PRIVSEP_MSG);
1553 /* We should already have received a pipe to the listener. */
1554 if (client->fd == -1)
1555 return got_error(GOT_ERR_PRIVSEP_MSG);
1557 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1558 if (datalen != sizeof(ichild))
1559 return got_error(GOT_ERR_PRIVSEP_LEN);
1561 memcpy(&ichild, imsg->data, sizeof(ichild));
1563 if (ichild.proc_id != PROC_REPO_WRITE)
1564 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1565 "bad child process type");
1567 fd = imsg_get_fd(imsg);
1569 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1571 imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1572 gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1573 gotd_session.repo_child_iev.events = EV_READ;
1574 gotd_session.repo_child_iev.handler_arg = NULL;
1575 event_set(&gotd_session.repo_child_iev.ev,
1576 gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1577 session_dispatch_repo_child, &gotd_session.repo_child_iev);
1578 gotd_imsg_event_add(&gotd_session.repo_child_iev);
1580 /* The "recvfd" pledge promise is no longer needed. */
1581 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1588 session_dispatch(int fd, short event, void *arg)
1590 struct gotd_imsgev *iev = arg;
1591 struct imsgbuf *ibuf = &iev->ibuf;
1592 struct gotd_session_client *client = &gotd_session_client;
1597 if (event & EV_READ) {
1598 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1599 fatal("imsg_read error");
1601 /* Connection closed. */
1607 if (event & EV_WRITE) {
1608 n = msgbuf_write(&ibuf->w);
1609 if (n == -1 && errno != EAGAIN)
1610 fatal("msgbuf_write");
1612 /* Connection closed. */
1619 const struct got_error *err = NULL;
1620 uint32_t client_id = 0;
1621 int do_disconnect = 0, do_list_refs = 0;
1623 if ((n = imsg_get(ibuf, &imsg)) == -1)
1624 fatal("%s: imsg_get error", __func__);
1625 if (n == 0) /* No more messages. */
1628 switch (imsg.hdr.type) {
1629 case GOTD_IMSG_ERROR:
1631 err = gotd_imsg_recv_error(&client_id, &imsg);
1633 case GOTD_IMSG_CONNECT:
1634 err = recv_connect(&imsg);
1636 case GOTD_IMSG_DISCONNECT:
1639 case GOTD_IMSG_CONNECT_NOTIFIER:
1640 err = recv_notifier(&imsg);
1642 case GOTD_IMSG_CONNECT_REPO_CHILD:
1643 err = recv_repo_child(&imsg);
1649 log_debug("unexpected imsg %d", imsg.hdr.type);
1654 if (do_disconnect) {
1656 disconnect_on_error(client, err);
1659 } else if (do_list_refs)
1660 err = list_refs_request();
1663 log_warnx("uid %d: %s", client->euid, err->msg);
1667 gotd_imsg_event_add(iev);
1669 /* This pipe is dead. Remove its event handler */
1670 event_del(&iev->ev);
1671 event_loopexit(NULL);
1676 session_write_main(const char *title, const char *repo_path,
1677 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1678 struct gotd_repo *repo_cfg)
1680 const struct got_error *err = NULL;
1681 struct event evsigint, evsigterm, evsighup, evsigusr1;
1683 STAILQ_INIT(¬ifications);
1685 gotd_session.title = title;
1686 gotd_session.pid = getpid();
1687 gotd_session.pack_fds = pack_fds;
1688 gotd_session.temp_fds = temp_fds;
1689 memcpy(&gotd_session.request_timeout, request_timeout,
1690 sizeof(gotd_session.request_timeout));
1691 gotd_session.repo_cfg = repo_cfg;
1693 imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1695 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1698 if (!got_repo_is_bare(gotd_session.repo)) {
1699 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1700 "bare git repository required");
1704 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1706 signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1707 signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1708 signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1709 signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1710 signal(SIGPIPE, SIG_IGN);
1712 signal_add(&evsigint, NULL);
1713 signal_add(&evsigterm, NULL);
1714 signal_add(&evsighup, NULL);
1715 signal_add(&evsigusr1, NULL);
1717 gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1719 gotd_session_client.fd = -1;
1720 gotd_session_client.nref_updates = -1;
1721 gotd_session_client.delta_cache_fd = -1;
1722 gotd_session_client.accept_flush_pkt = 1;
1724 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1725 gotd_session.parent_iev.handler = session_dispatch;
1726 gotd_session.parent_iev.events = EV_READ;
1727 gotd_session.parent_iev.handler_arg = NULL;
1728 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1729 EV_READ, session_dispatch, &gotd_session.parent_iev);
1730 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1731 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1732 -1, NULL, 0) == -1) {
1733 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1740 log_warnx("%s: %s", title, err->msg);
1741 session_write_shutdown();
1745 session_write_shutdown(void)
1747 struct gotd_session_notif *notif;
1749 log_debug("%s: shutting down", gotd_session.title);
1751 while (!STAILQ_EMPTY(¬ifications)) {
1752 notif = STAILQ_FIRST(¬ifications);
1753 STAILQ_REMOVE_HEAD(¬ifications, entry);
1754 if (notif->fd != -1)
1756 free(notif->refname);
1760 if (gotd_session.repo)
1761 got_repo_close(gotd_session.repo);
1762 got_repo_pack_fds_close(gotd_session.pack_fds);
1763 got_repo_temp_fds_close(gotd_session.temp_fds);
1764 free(gotd_session_client.username);