2 62ee7d94 2023-01-10 thomas * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 62ee7d94 2023-01-10 thomas * Permission to use, copy, modify, and distribute this software for any
5 62ee7d94 2023-01-10 thomas * purpose with or without fee is hereby granted, provided that the above
6 62ee7d94 2023-01-10 thomas * copyright notice and this permission notice appear in all copies.
8 62ee7d94 2023-01-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 62ee7d94 2023-01-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 62ee7d94 2023-01-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 62ee7d94 2023-01-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 62ee7d94 2023-01-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 62ee7d94 2023-01-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 62ee7d94 2023-01-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
19 62ee7d94 2023-01-10 thomas #include <sys/types.h>
20 62ee7d94 2023-01-10 thomas #include <sys/queue.h>
21 62ee7d94 2023-01-10 thomas #include <sys/socket.h>
22 851a5b48 2023-02-03 thomas #include <sys/stat.h>
23 62ee7d94 2023-01-10 thomas #include <sys/uio.h>
25 62ee7d94 2023-01-10 thomas #include <errno.h>
26 62ee7d94 2023-01-10 thomas #include <event.h>
27 62ee7d94 2023-01-10 thomas #include <limits.h>
28 62ee7d94 2023-01-10 thomas #include <signal.h>
29 62ee7d94 2023-01-10 thomas #include <stdint.h>
30 62ee7d94 2023-01-10 thomas #include <stdio.h>
31 62ee7d94 2023-01-10 thomas #include <stdlib.h>
32 62ee7d94 2023-01-10 thomas #include <string.h>
33 62ee7d94 2023-01-10 thomas #include <imsg.h>
34 62ee7d94 2023-01-10 thomas #include <unistd.h>
36 febe25b7 2023-08-29 thomas #include "got_compat.h"
38 62ee7d94 2023-01-10 thomas #include "got_error.h"
39 62ee7d94 2023-01-10 thomas #include "got_repository.h"
40 62ee7d94 2023-01-10 thomas #include "got_object.h"
41 62ee7d94 2023-01-10 thomas #include "got_path.h"
42 62ee7d94 2023-01-10 thomas #include "got_reference.h"
43 62ee7d94 2023-01-10 thomas #include "got_opentemp.h"
45 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
46 62ee7d94 2023-01-10 thomas #include "got_lib_delta.h"
47 62ee7d94 2023-01-10 thomas #include "got_lib_object.h"
48 62ee7d94 2023-01-10 thomas #include "got_lib_object_cache.h"
49 62ee7d94 2023-01-10 thomas #include "got_lib_pack.h"
50 62ee7d94 2023-01-10 thomas #include "got_lib_repository.h"
51 62ee7d94 2023-01-10 thomas #include "got_lib_gitproto.h"
53 62ee7d94 2023-01-10 thomas #include "gotd.h"
54 62ee7d94 2023-01-10 thomas #include "log.h"
55 62ee7d94 2023-01-10 thomas #include "session.h"
57 ce1bfad9 2024-03-30 thomas struct gotd_session_notif {
58 ce1bfad9 2024-03-30 thomas STAILQ_ENTRY(gotd_session_notif) entry;
60 ce1bfad9 2024-03-30 thomas enum gotd_notification_action action;
61 ce1bfad9 2024-03-30 thomas char *refname;
62 ce1bfad9 2024-03-30 thomas struct got_object_id old_id;
63 ce1bfad9 2024-03-30 thomas struct got_object_id new_id;
65 ce1bfad9 2024-03-30 thomas STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
67 62ee7d94 2023-01-10 thomas static struct gotd_session {
68 62ee7d94 2023-01-10 thomas pid_t pid;
69 62ee7d94 2023-01-10 thomas const char *title;
70 62ee7d94 2023-01-10 thomas struct got_repository *repo;
71 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo_cfg;
72 62ee7d94 2023-01-10 thomas int *pack_fds;
73 62ee7d94 2023-01-10 thomas int *temp_fds;
74 62ee7d94 2023-01-10 thomas struct gotd_imsgev parent_iev;
75 ce1bfad9 2024-03-30 thomas struct gotd_imsgev notifier_iev;
76 62ee7d94 2023-01-10 thomas struct timeval request_timeout;
77 7fed8fa4 2023-06-22 thomas enum gotd_procid proc_id;
78 176350bf 2024-03-30 thomas enum gotd_session_state state;
79 74e85132 2024-03-30 thomas struct gotd_imsgev repo_child_iev;
80 62ee7d94 2023-01-10 thomas } gotd_session;
82 62ee7d94 2023-01-10 thomas static struct gotd_session_client {
83 62ee7d94 2023-01-10 thomas int is_writing;
84 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capabilities;
85 62ee7d94 2023-01-10 thomas size_t ncapa_alloc;
86 62ee7d94 2023-01-10 thomas size_t ncapabilities;
87 62ee7d94 2023-01-10 thomas uint32_t id;
89 62ee7d94 2023-01-10 thomas int delta_cache_fd;
90 62ee7d94 2023-01-10 thomas struct gotd_imsgev iev;
91 62ee7d94 2023-01-10 thomas struct event tmo;
92 62ee7d94 2023-01-10 thomas uid_t euid;
93 62ee7d94 2023-01-10 thomas gid_t egid;
94 ce1bfad9 2024-03-30 thomas char *username;
95 62ee7d94 2023-01-10 thomas char *packfile_path;
96 62ee7d94 2023-01-10 thomas char *packidx_path;
97 62ee7d94 2023-01-10 thomas int nref_updates;
98 98c7fd82 2023-01-23 thomas int accept_flush_pkt;
99 c8b73ac1 2023-08-23 thomas int flush_disconnect;
100 62ee7d94 2023-01-10 thomas } gotd_session_client;
102 62ee7d94 2023-01-10 thomas void gotd_session_sighdlr(int sig, short event, void *arg);
103 62ee7d94 2023-01-10 thomas static void gotd_session_shutdown(void);
105 62ee7d94 2023-01-10 thomas static void
106 62ee7d94 2023-01-10 thomas disconnect(struct gotd_session_client *client)
108 62ee7d94 2023-01-10 thomas log_debug("uid %d: disconnecting", client->euid);
110 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
111 7fed8fa4 2023-06-22 thomas GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
112 62ee7d94 2023-01-10 thomas log_warn("imsg compose DISCONNECT");
114 74e85132 2024-03-30 thomas imsg_clear(&gotd_session.repo_child_iev.ibuf);
115 74e85132 2024-03-30 thomas event_del(&gotd_session.repo_child_iev.ev);
116 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
117 62ee7d94 2023-01-10 thomas close(client->fd);
118 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd != -1)
119 62ee7d94 2023-01-10 thomas close(client->delta_cache_fd);
120 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
121 62ee7d94 2023-01-10 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
122 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packfile_path);
123 62ee7d94 2023-01-10 thomas free(client->packfile_path);
125 62ee7d94 2023-01-10 thomas if (client->packidx_path) {
126 62ee7d94 2023-01-10 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
127 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packidx_path);
128 62ee7d94 2023-01-10 thomas free(client->packidx_path);
130 62ee7d94 2023-01-10 thomas free(client->capabilities);
132 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
135 62ee7d94 2023-01-10 thomas static void
136 62ee7d94 2023-01-10 thomas disconnect_on_error(struct gotd_session_client *client,
137 62ee7d94 2023-01-10 thomas const struct got_error *err)
139 62ee7d94 2023-01-10 thomas struct imsgbuf ibuf;
141 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF) {
142 a6153ffb 2023-08-12 thomas log_warnx("uid %d: %s", client->euid, err->msg);
143 62ee7d94 2023-01-10 thomas imsg_init(&ibuf, client->fd);
144 7fed8fa4 2023-06-22 thomas gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
145 62ee7d94 2023-01-10 thomas imsg_clear(&ibuf);
148 62ee7d94 2023-01-10 thomas disconnect(client);
151 62ee7d94 2023-01-10 thomas static void
152 62ee7d94 2023-01-10 thomas gotd_request_timeout(int fd, short events, void *arg)
154 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = arg;
156 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
157 62ee7d94 2023-01-10 thomas disconnect(client);
161 62ee7d94 2023-01-10 thomas gotd_session_sighdlr(int sig, short event, void *arg)
164 62ee7d94 2023-01-10 thomas * Normal signal handler rules don't apply because libevent
165 62ee7d94 2023-01-10 thomas * decouples for us.
168 62ee7d94 2023-01-10 thomas switch (sig) {
169 62ee7d94 2023-01-10 thomas case SIGHUP:
170 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGHUP", __func__);
172 62ee7d94 2023-01-10 thomas case SIGUSR1:
173 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGUSR1", __func__);
175 62ee7d94 2023-01-10 thomas case SIGTERM:
176 62ee7d94 2023-01-10 thomas case SIGINT:
177 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
178 62ee7d94 2023-01-10 thomas /* NOTREACHED */
181 62ee7d94 2023-01-10 thomas fatalx("unexpected signal");
185 62ee7d94 2023-01-10 thomas static const struct got_error *
186 62ee7d94 2023-01-10 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
188 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_done idone;
189 62ee7d94 2023-01-10 thomas size_t datalen;
191 62ee7d94 2023-01-10 thomas log_debug("packfile-done received");
193 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
194 62ee7d94 2023-01-10 thomas if (datalen != sizeof(idone))
195 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
196 62ee7d94 2023-01-10 thomas memcpy(&idone, imsg->data, sizeof(idone));
198 62ee7d94 2023-01-10 thomas *client_id = idone.client_id;
199 62ee7d94 2023-01-10 thomas return NULL;
202 62ee7d94 2023-01-10 thomas static const struct got_error *
203 62ee7d94 2023-01-10 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
205 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
206 62ee7d94 2023-01-10 thomas size_t datalen;
208 62ee7d94 2023-01-10 thomas log_debug("packfile-install received");
210 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
211 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
212 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
213 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
215 62ee7d94 2023-01-10 thomas *client_id = inst.client_id;
216 62ee7d94 2023-01-10 thomas return NULL;
219 62ee7d94 2023-01-10 thomas static const struct got_error *
220 62ee7d94 2023-01-10 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
222 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
223 62ee7d94 2023-01-10 thomas size_t datalen;
225 62ee7d94 2023-01-10 thomas log_debug("ref-updates-start received");
227 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
228 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
229 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
230 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
232 62ee7d94 2023-01-10 thomas *client_id = istart.client_id;
233 62ee7d94 2023-01-10 thomas return NULL;
236 62ee7d94 2023-01-10 thomas static const struct got_error *
237 62ee7d94 2023-01-10 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
239 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
240 62ee7d94 2023-01-10 thomas size_t datalen;
242 62ee7d94 2023-01-10 thomas log_debug("ref-update received");
244 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
245 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
246 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
247 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
249 62ee7d94 2023-01-10 thomas *client_id = iref.client_id;
250 62ee7d94 2023-01-10 thomas return NULL;
253 62ee7d94 2023-01-10 thomas static const struct got_error *
254 62ee7d94 2023-01-10 thomas send_ref_update_ok(struct gotd_session_client *client,
255 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname)
257 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ok iok;
258 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
259 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
260 62ee7d94 2023-01-10 thomas size_t len;
262 62ee7d94 2023-01-10 thomas memset(&iok, 0, sizeof(iok));
263 62ee7d94 2023-01-10 thomas iok.client_id = client->id;
264 62ee7d94 2023-01-10 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
265 62ee7d94 2023-01-10 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
266 62ee7d94 2023-01-10 thomas iok.name_len = strlen(refname);
268 62ee7d94 2023-01-10 thomas len = sizeof(iok) + iok.name_len;
269 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
270 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
271 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
272 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
274 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
275 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
276 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
277 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
279 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
280 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
281 62ee7d94 2023-01-10 thomas return NULL;
284 62ee7d94 2023-01-10 thomas static void
285 62ee7d94 2023-01-10 thomas send_refs_updated(struct gotd_session_client *client)
287 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
288 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, -1, NULL, 0) == -1)
289 62ee7d94 2023-01-10 thomas log_warn("imsg compose REFS_UPDATED");
292 62ee7d94 2023-01-10 thomas static const struct got_error *
293 62ee7d94 2023-01-10 thomas send_ref_update_ng(struct gotd_session_client *client,
294 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname,
295 62ee7d94 2023-01-10 thomas const char *reason)
297 62ee7d94 2023-01-10 thomas const struct got_error *ng_err;
298 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ng ing;
299 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
300 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
301 62ee7d94 2023-01-10 thomas size_t len;
303 62ee7d94 2023-01-10 thomas memset(&ing, 0, sizeof(ing));
304 62ee7d94 2023-01-10 thomas ing.client_id = client->id;
305 62ee7d94 2023-01-10 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
306 62ee7d94 2023-01-10 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
307 62ee7d94 2023-01-10 thomas ing.name_len = strlen(refname);
309 62ee7d94 2023-01-10 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
310 62ee7d94 2023-01-10 thomas ing.reason_len = strlen(ng_err->msg);
312 62ee7d94 2023-01-10 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
313 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
314 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
315 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
316 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
318 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
319 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
320 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
321 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
322 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
323 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
325 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
326 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
327 62ee7d94 2023-01-10 thomas return NULL;
330 62ee7d94 2023-01-10 thomas static const struct got_error *
331 62ee7d94 2023-01-10 thomas install_pack(struct gotd_session_client *client, const char *repo_path,
332 62ee7d94 2023-01-10 thomas struct imsg *imsg)
334 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
335 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
336 62ee7d94 2023-01-10 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
337 62ee7d94 2023-01-10 thomas size_t datalen;
338 62ee7d94 2023-01-10 thomas char *packfile_path = NULL, *packidx_path = NULL;
340 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
341 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
342 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
343 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
345 62ee7d94 2023-01-10 thomas if (client->packfile_path == NULL)
346 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
347 62ee7d94 2023-01-10 thomas "client has no pack file");
348 62ee7d94 2023-01-10 thomas if (client->packidx_path == NULL)
349 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
350 62ee7d94 2023-01-10 thomas "client has no pack file index");
352 62ee7d94 2023-01-10 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
353 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_NO_SPACE,
354 62ee7d94 2023-01-10 thomas "could not convert pack file SHA1 to hex");
356 62ee7d94 2023-01-10 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
357 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
358 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
359 62ee7d94 2023-01-10 thomas goto done;
362 62ee7d94 2023-01-10 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
363 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
364 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
365 62ee7d94 2023-01-10 thomas goto done;
368 62ee7d94 2023-01-10 thomas if (rename(client->packfile_path, packfile_path) == -1) {
369 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packfile_path,
370 62ee7d94 2023-01-10 thomas packfile_path);
371 62ee7d94 2023-01-10 thomas goto done;
374 62ee7d94 2023-01-10 thomas free(client->packfile_path);
375 62ee7d94 2023-01-10 thomas client->packfile_path = NULL;
377 62ee7d94 2023-01-10 thomas if (rename(client->packidx_path, packidx_path) == -1) {
378 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packidx_path,
379 62ee7d94 2023-01-10 thomas packidx_path);
380 62ee7d94 2023-01-10 thomas goto done;
383 342fdad2 2024-03-19 thomas /* Ensure we re-read the pack index list upon next access. */
384 342fdad2 2024-03-19 thomas gotd_session.repo->pack_path_mtime.tv_sec = 0;
385 342fdad2 2024-03-19 thomas gotd_session.repo->pack_path_mtime.tv_nsec = 0;
387 62ee7d94 2023-01-10 thomas free(client->packidx_path);
388 62ee7d94 2023-01-10 thomas client->packidx_path = NULL;
390 62ee7d94 2023-01-10 thomas free(packfile_path);
391 62ee7d94 2023-01-10 thomas free(packidx_path);
392 62ee7d94 2023-01-10 thomas return err;
395 62ee7d94 2023-01-10 thomas static const struct got_error *
396 62ee7d94 2023-01-10 thomas begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
398 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
399 62ee7d94 2023-01-10 thomas size_t datalen;
401 62ee7d94 2023-01-10 thomas if (client->nref_updates != -1)
402 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
404 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
405 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
406 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
407 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
409 62ee7d94 2023-01-10 thomas if (istart.nref_updates <= 0)
410 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
412 62ee7d94 2023-01-10 thomas client->nref_updates = istart.nref_updates;
413 ce1bfad9 2024-03-30 thomas return NULL;
416 ce1bfad9 2024-03-30 thomas static const struct got_error *
417 ce1bfad9 2024-03-30 thomas validate_namespace(const char *namespace)
419 ce1bfad9 2024-03-30 thomas size_t len = strlen(namespace);
421 ce1bfad9 2024-03-30 thomas if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
422 ce1bfad9 2024-03-30 thomas namespace[len - 1] != '/') {
423 ce1bfad9 2024-03-30 thomas return got_error_fmt(GOT_ERR_BAD_REF_NAME,
424 ce1bfad9 2024-03-30 thomas "reference namespace '%s'", namespace);
427 62ee7d94 2023-01-10 thomas return NULL;
430 62ee7d94 2023-01-10 thomas static const struct got_error *
431 ce1bfad9 2024-03-30 thomas queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
432 ce1bfad9 2024-03-30 thomas struct got_repository *repo, struct got_reference *ref)
434 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
435 74e85132 2024-03-30 thomas struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
436 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
437 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
438 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
440 ce1bfad9 2024-03-30 thomas if (iev->ibuf.fd == -1 ||
441 ce1bfad9 2024-03-30 thomas STAILQ_EMPTY(&repo_cfg->notification_targets))
442 ce1bfad9 2024-03-30 thomas return NULL; /* notifications unused */
444 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
445 ce1bfad9 2024-03-30 thomas const char *refname = pe->path;
446 ce1bfad9 2024-03-30 thomas if (strcmp(got_ref_get_name(ref), refname) == 0)
449 ce1bfad9 2024-03-30 thomas if (pe == NULL) {
450 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
452 ce1bfad9 2024-03-30 thomas const char *namespace = pe->path;
454 ce1bfad9 2024-03-30 thomas err = validate_namespace(namespace);
456 ce1bfad9 2024-03-30 thomas return err;
457 ce1bfad9 2024-03-30 thomas if (strncmp(namespace, got_ref_get_name(ref),
458 ce1bfad9 2024-03-30 thomas strlen(namespace)) == 0)
464 ce1bfad9 2024-03-30 thomas * If a branch or a reference namespace was specified in the
465 ce1bfad9 2024-03-30 thomas * configuration file then only send notifications if a match
466 ce1bfad9 2024-03-30 thomas * was found.
468 ce1bfad9 2024-03-30 thomas if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
469 ce1bfad9 2024-03-30 thomas !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
470 ce1bfad9 2024-03-30 thomas return NULL;
472 ce1bfad9 2024-03-30 thomas notif = calloc(1, sizeof(*notif));
473 ce1bfad9 2024-03-30 thomas if (notif == NULL)
474 ce1bfad9 2024-03-30 thomas return got_error_from_errno("calloc");
476 ce1bfad9 2024-03-30 thomas notif->fd = -1;
478 ce1bfad9 2024-03-30 thomas if (old_id == NULL)
479 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_CREATED;
480 ce1bfad9 2024-03-30 thomas else if (new_id == NULL)
481 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_REMOVED;
483 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_CHANGED;
485 ce1bfad9 2024-03-30 thomas if (old_id != NULL)
486 ce1bfad9 2024-03-30 thomas memcpy(¬if->old_id, old_id, sizeof(notif->old_id));
487 ce1bfad9 2024-03-30 thomas if (new_id != NULL)
488 ce1bfad9 2024-03-30 thomas memcpy(¬if->new_id, new_id, sizeof(notif->new_id));
490 ce1bfad9 2024-03-30 thomas notif->refname = strdup(got_ref_get_name(ref));
491 ce1bfad9 2024-03-30 thomas if (notif->refname == NULL) {
492 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strdup");
493 ce1bfad9 2024-03-30 thomas goto done;
496 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(¬ifications, notif, entry);
498 ce1bfad9 2024-03-30 thomas if (err && notif) {
499 ce1bfad9 2024-03-30 thomas free(notif->refname);
500 ce1bfad9 2024-03-30 thomas free(notif);
502 ce1bfad9 2024-03-30 thomas return err;
505 ce1bfad9 2024-03-30 thomas /* Forward notification content to the NOTIFY process. */
506 ce1bfad9 2024-03-30 thomas static const struct got_error *
507 ce1bfad9 2024-03-30 thomas forward_notification(struct gotd_session_client *client, struct imsg *imsg)
509 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
510 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.notifier_iev;
511 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
512 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content icontent;
513 ce1bfad9 2024-03-30 thomas char *refname = NULL;
514 ce1bfad9 2024-03-30 thomas size_t datalen;
515 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notify inotify;
516 ce1bfad9 2024-03-30 thomas const char *action;
518 ce1bfad9 2024-03-30 thomas memset(&inotify, 0, sizeof(inotify));
520 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
521 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(icontent))
522 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
523 ce1bfad9 2024-03-30 thomas memcpy(&icontent, imsg->data, sizeof(icontent));
524 ce1bfad9 2024-03-30 thomas if (datalen != sizeof(icontent) + icontent.refname_len)
525 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
526 ce1bfad9 2024-03-30 thomas refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
527 ce1bfad9 2024-03-30 thomas if (refname == NULL)
528 ce1bfad9 2024-03-30 thomas return got_error_from_errno("strndup");
530 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(¬ifications);
531 ce1bfad9 2024-03-30 thomas if (notif == NULL)
532 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
534 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE(¬ifications, notif, gotd_session_notif, entry);
536 ce1bfad9 2024-03-30 thomas if (notif->action != icontent.action || notif->fd == -1 ||
537 ce1bfad9 2024-03-30 thomas strcmp(notif->refname, refname) != 0) {
538 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
539 ce1bfad9 2024-03-30 thomas goto done;
541 ce1bfad9 2024-03-30 thomas if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
542 ce1bfad9 2024-03-30 thomas if (memcmp(notif->new_id.sha1, icontent.new_id,
543 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
544 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
545 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
546 ce1bfad9 2024-03-30 thomas goto done;
548 ce1bfad9 2024-03-30 thomas } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
549 ce1bfad9 2024-03-30 thomas if (memcmp(notif->old_id.sha1, icontent.old_id,
550 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
551 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
552 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
553 ce1bfad9 2024-03-30 thomas goto done;
555 ce1bfad9 2024-03-30 thomas } else if (memcmp(notif->old_id.sha1, icontent.old_id,
556 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0 ||
557 ce1bfad9 2024-03-30 thomas memcmp(notif->new_id.sha1, icontent.new_id,
558 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
559 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
560 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
561 ce1bfad9 2024-03-30 thomas goto done;
564 ce1bfad9 2024-03-30 thomas switch (notif->action) {
565 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_CREATED:
566 ce1bfad9 2024-03-30 thomas action = "created";
568 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_REMOVED:
569 ce1bfad9 2024-03-30 thomas action = "removed";
571 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_CHANGED:
572 ce1bfad9 2024-03-30 thomas action = "changed";
575 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
576 ce1bfad9 2024-03-30 thomas goto done;
579 ce1bfad9 2024-03-30 thomas strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
580 ce1bfad9 2024-03-30 thomas sizeof(inotify.repo_name));
582 ce1bfad9 2024-03-30 thomas snprintf(inotify.subject_line, sizeof(inotify.subject_line),
583 ce1bfad9 2024-03-30 thomas "%s: %s %s %s", gotd_session.repo_cfg->name,
584 ce1bfad9 2024-03-30 thomas client->username, action, notif->refname);
586 ce1bfad9 2024-03-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFY,
587 ce1bfad9 2024-03-30 thomas PROC_SESSION_WRITE, notif->fd, &inotify, sizeof(inotify))
589 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg compose NOTIFY");
590 ce1bfad9 2024-03-30 thomas goto done;
592 ce1bfad9 2024-03-30 thomas notif->fd = -1;
594 ce1bfad9 2024-03-30 thomas if (notif->fd != -1)
595 ce1bfad9 2024-03-30 thomas close(notif->fd);
596 ce1bfad9 2024-03-30 thomas free(notif);
597 ce1bfad9 2024-03-30 thomas free(refname);
598 ce1bfad9 2024-03-30 thomas return err;
601 ce1bfad9 2024-03-30 thomas /* Request notification content from REPO_WRITE process. */
602 ce1bfad9 2024-03-30 thomas static const struct got_error *
603 ce1bfad9 2024-03-30 thomas request_notification(struct gotd_session_notif *notif)
605 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
606 ce1bfad9 2024-03-30 thomas struct gotd_session_client *client = &gotd_session_client;
607 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
608 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content icontent;
609 ce1bfad9 2024-03-30 thomas struct ibuf *wbuf;
610 ce1bfad9 2024-03-30 thomas size_t len;
613 ce1bfad9 2024-03-30 thomas fd = got_opentempfd();
614 ce1bfad9 2024-03-30 thomas if (fd == -1)
615 ce1bfad9 2024-03-30 thomas return got_error_from_errno("got_opentemp");
617 ce1bfad9 2024-03-30 thomas memset(&icontent, 0, sizeof(icontent));
618 ce1bfad9 2024-03-30 thomas icontent.client_id = client->id;
620 ce1bfad9 2024-03-30 thomas icontent.action = notif->action;
621 ce1bfad9 2024-03-30 thomas memcpy(&icontent.old_id, ¬if->old_id, sizeof(notif->old_id));
622 ce1bfad9 2024-03-30 thomas memcpy(&icontent.new_id, ¬if->new_id, sizeof(notif->new_id));
623 ce1bfad9 2024-03-30 thomas icontent.refname_len = strlen(notif->refname);
625 ce1bfad9 2024-03-30 thomas len = sizeof(icontent) + icontent.refname_len;
626 ce1bfad9 2024-03-30 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
627 ce1bfad9 2024-03-30 thomas gotd_session.proc_id, gotd_session.pid, len);
628 ce1bfad9 2024-03-30 thomas if (wbuf == NULL) {
629 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_create NOTIFY");
630 ce1bfad9 2024-03-30 thomas goto done;
632 ce1bfad9 2024-03-30 thomas if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
633 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_add NOTIFY");
634 ce1bfad9 2024-03-30 thomas goto done;
636 ce1bfad9 2024-03-30 thomas if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
637 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_add NOTIFY");
638 ce1bfad9 2024-03-30 thomas goto done;
641 ce1bfad9 2024-03-30 thomas notif->fd = dup(fd);
642 ce1bfad9 2024-03-30 thomas if (notif->fd == -1) {
643 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("dup");
644 ce1bfad9 2024-03-30 thomas goto done;
647 ce1bfad9 2024-03-30 thomas ibuf_fd_set(wbuf, fd);
650 ce1bfad9 2024-03-30 thomas imsg_close(&iev->ibuf, wbuf);
651 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
653 ce1bfad9 2024-03-30 thomas if (err && fd != -1)
654 ce1bfad9 2024-03-30 thomas close(fd);
655 ce1bfad9 2024-03-30 thomas return err;
658 ce1bfad9 2024-03-30 thomas static const struct got_error *
659 d98779cd 2023-01-19 thomas update_ref(int *shut, struct gotd_session_client *client,
660 d98779cd 2023-01-19 thomas const char *repo_path, struct imsg *imsg)
662 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
663 e8c5d4f8 2024-03-19 thomas struct got_repository *repo = gotd_session.repo;
664 62ee7d94 2023-01-10 thomas struct got_reference *ref = NULL;
665 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
666 62ee7d94 2023-01-10 thomas struct got_object_id old_id, new_id;
667 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
668 62ee7d94 2023-01-10 thomas struct got_object_id *id = NULL;
669 62ee7d94 2023-01-10 thomas char *refname = NULL;
670 62ee7d94 2023-01-10 thomas size_t datalen;
671 62ee7d94 2023-01-10 thomas int locked = 0;
672 8a9c582b 2023-06-22 thomas char hex1[SHA1_DIGEST_STRING_LENGTH];
673 8a9c582b 2023-06-22 thomas char hex2[SHA1_DIGEST_STRING_LENGTH];
675 62ee7d94 2023-01-10 thomas log_debug("update-ref from uid %d", client->euid);
677 62ee7d94 2023-01-10 thomas if (client->nref_updates <= 0)
678 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
680 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
681 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
682 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
683 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
684 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iref) + iref.name_len)
685 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
686 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
687 62ee7d94 2023-01-10 thomas if (refname == NULL)
688 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
690 62ee7d94 2023-01-10 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
692 62ee7d94 2023-01-10 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
693 62ee7d94 2023-01-10 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
694 44f875ad 2024-01-18 thomas err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
697 62ee7d94 2023-01-10 thomas goto done;
699 62ee7d94 2023-01-10 thomas if (iref.ref_is_new) {
700 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 0);
701 62ee7d94 2023-01-10 thomas if (err) {
702 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_NOT_REF)
703 62ee7d94 2023-01-10 thomas goto done;
704 62ee7d94 2023-01-10 thomas err = got_ref_alloc(&ref, refname, &new_id);
706 62ee7d94 2023-01-10 thomas goto done;
707 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
709 62ee7d94 2023-01-10 thomas goto done;
710 ce1bfad9 2024-03-30 thomas err = queue_notification(NULL, &new_id, repo, ref);
712 ce1bfad9 2024-03-30 thomas goto done;
714 8a9c582b 2023-06-22 thomas err = got_ref_resolve(&id, repo, ref);
716 8a9c582b 2023-06-22 thomas goto done;
717 8a9c582b 2023-06-22 thomas got_object_id_hex(&new_id, hex1, sizeof(hex1));
718 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
719 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
720 8a9c582b 2023-06-22 thomas "Addition %s: %s failed; %s: %s has been "
721 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
722 8a9c582b 2023-06-22 thomas "was in progress",
723 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
724 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
725 62ee7d94 2023-01-10 thomas goto done;
727 49563dfb 2023-01-28 thomas } else if (iref.delete_ref) {
728 49563dfb 2023-01-28 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
730 49563dfb 2023-01-28 thomas goto done;
731 49563dfb 2023-01-28 thomas locked = 1;
733 49563dfb 2023-01-28 thomas err = got_ref_resolve(&id, repo, ref);
735 49563dfb 2023-01-28 thomas goto done;
737 49563dfb 2023-01-28 thomas if (got_object_id_cmp(id, &old_id) != 0) {
738 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
739 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
740 49563dfb 2023-01-28 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
741 8a9c582b 2023-06-22 thomas "Deletion %s: %s failed; %s: %s has been "
742 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
743 8a9c582b 2023-06-22 thomas "was in progress",
744 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
745 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
746 49563dfb 2023-01-28 thomas goto done;
749 49563dfb 2023-01-28 thomas err = got_ref_delete(ref, repo);
751 49563dfb 2023-01-28 thomas goto done;
752 ce1bfad9 2024-03-30 thomas err = queue_notification(&old_id, NULL, repo, ref);
754 ce1bfad9 2024-03-30 thomas goto done;
755 49563dfb 2023-01-28 thomas free(id);
756 49563dfb 2023-01-28 thomas id = NULL;
758 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
760 62ee7d94 2023-01-10 thomas goto done;
761 62ee7d94 2023-01-10 thomas locked = 1;
763 62ee7d94 2023-01-10 thomas err = got_ref_resolve(&id, repo, ref);
765 62ee7d94 2023-01-10 thomas goto done;
767 62ee7d94 2023-01-10 thomas if (got_object_id_cmp(id, &old_id) != 0) {
768 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
769 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
770 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
771 8a9c582b 2023-06-22 thomas "Update %s: %s failed; %s: %s has been "
772 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
773 8a9c582b 2023-06-22 thomas "was in progress",
774 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
775 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
776 62ee7d94 2023-01-10 thomas goto done;
779 169def41 2023-06-22 thomas if (got_object_id_cmp(&new_id, &old_id) != 0) {
780 169def41 2023-06-22 thomas err = got_ref_change_ref(ref, &new_id);
782 169def41 2023-06-22 thomas goto done;
783 169def41 2023-06-22 thomas err = got_ref_write(ref, repo);
785 169def41 2023-06-22 thomas goto done;
786 ce1bfad9 2024-03-30 thomas err = queue_notification(&old_id, &new_id, repo, ref);
788 ce1bfad9 2024-03-30 thomas goto done;
791 62ee7d94 2023-01-10 thomas free(id);
792 62ee7d94 2023-01-10 thomas id = NULL;
795 62ee7d94 2023-01-10 thomas if (err) {
796 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
797 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
798 62ee7d94 2023-01-10 thomas "could not acquire exclusive file lock for %s",
799 62ee7d94 2023-01-10 thomas refname);
801 62ee7d94 2023-01-10 thomas send_ref_update_ng(client, &iref, refname, err->msg);
803 62ee7d94 2023-01-10 thomas send_ref_update_ok(client, &iref, refname);
805 62ee7d94 2023-01-10 thomas if (client->nref_updates > 0) {
806 62ee7d94 2023-01-10 thomas client->nref_updates--;
807 d98779cd 2023-01-19 thomas if (client->nref_updates == 0) {
808 62ee7d94 2023-01-10 thomas send_refs_updated(client);
809 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(¬ifications);
810 ce1bfad9 2024-03-30 thomas if (notif) {
811 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_NOTIFY;
812 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
813 ce1bfad9 2024-03-30 thomas if (err) {
814 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: "
815 ce1bfad9 2024-03-30 thomas "%s", err->msg);
816 ce1bfad9 2024-03-30 thomas client->flush_disconnect = 1;
819 ce1bfad9 2024-03-30 thomas client->flush_disconnect = 1;
823 62ee7d94 2023-01-10 thomas if (locked) {
824 62ee7d94 2023-01-10 thomas const struct got_error *unlock_err;
825 62ee7d94 2023-01-10 thomas unlock_err = got_ref_unlock(ref);
826 62ee7d94 2023-01-10 thomas if (unlock_err && err == NULL)
827 62ee7d94 2023-01-10 thomas err = unlock_err;
830 62ee7d94 2023-01-10 thomas got_ref_close(ref);
831 62ee7d94 2023-01-10 thomas free(refname);
832 62ee7d94 2023-01-10 thomas free(id);
833 62ee7d94 2023-01-10 thomas return err;
836 ce1bfad9 2024-03-30 thomas static const struct got_error *
837 ce1bfad9 2024-03-30 thomas recv_notification_content(uint32_t *client_id, struct imsg *imsg)
839 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content inotif;
840 ce1bfad9 2024-03-30 thomas size_t datalen;
842 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
843 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(inotif))
844 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
845 ce1bfad9 2024-03-30 thomas memcpy(&inotif, imsg->data, sizeof(inotif));
847 ce1bfad9 2024-03-30 thomas *client_id = inotif.client_id;
848 ce1bfad9 2024-03-30 thomas return NULL;
851 62ee7d94 2023-01-10 thomas static void
852 62ee7d94 2023-01-10 thomas session_dispatch_repo_child(int fd, short event, void *arg)
854 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
855 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
856 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
857 62ee7d94 2023-01-10 thomas ssize_t n;
858 62ee7d94 2023-01-10 thomas int shut = 0;
859 62ee7d94 2023-01-10 thomas struct imsg imsg;
861 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
862 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
863 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
864 62ee7d94 2023-01-10 thomas if (n == 0) {
865 62ee7d94 2023-01-10 thomas /* Connection closed. */
866 62ee7d94 2023-01-10 thomas shut = 1;
867 62ee7d94 2023-01-10 thomas goto done;
871 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
872 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
873 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
874 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
875 62ee7d94 2023-01-10 thomas if (n == 0) {
876 62ee7d94 2023-01-10 thomas /* Connection closed. */
877 62ee7d94 2023-01-10 thomas shut = 1;
878 62ee7d94 2023-01-10 thomas goto done;
882 62ee7d94 2023-01-10 thomas for (;;) {
883 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
884 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
885 62ee7d94 2023-01-10 thomas int do_disconnect = 0;
886 62ee7d94 2023-01-10 thomas int do_ref_updates = 0, do_ref_update = 0;
887 ce1bfad9 2024-03-30 thomas int do_packfile_install = 0, do_notify = 0;
889 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
890 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
891 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
894 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
895 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
896 62ee7d94 2023-01-10 thomas do_disconnect = 1;
897 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
899 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_DONE:
900 62ee7d94 2023-01-10 thomas do_disconnect = 1;
901 62ee7d94 2023-01-10 thomas err = recv_packfile_done(&client_id, &imsg);
903 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_INSTALL:
904 62ee7d94 2023-01-10 thomas err = recv_packfile_install(&client_id, &imsg);
905 62ee7d94 2023-01-10 thomas if (err == NULL)
906 62ee7d94 2023-01-10 thomas do_packfile_install = 1;
908 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATES_START:
909 62ee7d94 2023-01-10 thomas err = recv_ref_updates_start(&client_id, &imsg);
910 62ee7d94 2023-01-10 thomas if (err == NULL)
911 62ee7d94 2023-01-10 thomas do_ref_updates = 1;
913 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
914 62ee7d94 2023-01-10 thomas err = recv_ref_update(&client_id, &imsg);
915 62ee7d94 2023-01-10 thomas if (err == NULL)
916 62ee7d94 2023-01-10 thomas do_ref_update = 1;
918 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFY:
919 ce1bfad9 2024-03-30 thomas err = recv_notification_content(&client_id, &imsg);
920 ce1bfad9 2024-03-30 thomas if (err == NULL)
921 ce1bfad9 2024-03-30 thomas do_notify = 1;
924 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
928 62ee7d94 2023-01-10 thomas if (do_disconnect) {
930 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
932 62ee7d94 2023-01-10 thomas disconnect(client);
934 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
936 62ee7d94 2023-01-10 thomas if (do_packfile_install)
937 62ee7d94 2023-01-10 thomas err = install_pack(client,
938 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
939 62ee7d94 2023-01-10 thomas else if (do_ref_updates)
940 62ee7d94 2023-01-10 thomas err = begin_ref_updates(client, &imsg);
941 62ee7d94 2023-01-10 thomas else if (do_ref_update)
942 d98779cd 2023-01-19 thomas err = update_ref(&shut, client,
943 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
944 ce1bfad9 2024-03-30 thomas else if (do_notify)
945 ce1bfad9 2024-03-30 thomas err = forward_notification(client, &imsg);
947 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
949 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(¬ifications);
950 ce1bfad9 2024-03-30 thomas if (notif && do_notify) {
951 ce1bfad9 2024-03-30 thomas /* Request content for next notification. */
952 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
953 ce1bfad9 2024-03-30 thomas if (err) {
954 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: "
955 ce1bfad9 2024-03-30 thomas "%s", err->msg);
956 ce1bfad9 2024-03-30 thomas shut = 1;
960 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
963 62ee7d94 2023-01-10 thomas if (!shut) {
964 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
966 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
967 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
968 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
972 62ee7d94 2023-01-10 thomas static const struct got_error *
973 62ee7d94 2023-01-10 thomas recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
975 62ee7d94 2023-01-10 thomas struct gotd_imsg_capabilities icapas;
976 62ee7d94 2023-01-10 thomas size_t datalen;
978 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
979 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapas))
980 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
981 62ee7d94 2023-01-10 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
983 62ee7d94 2023-01-10 thomas client->ncapa_alloc = icapas.ncapabilities;
984 62ee7d94 2023-01-10 thomas client->capabilities = calloc(client->ncapa_alloc,
985 62ee7d94 2023-01-10 thomas sizeof(*client->capabilities));
986 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL) {
987 62ee7d94 2023-01-10 thomas client->ncapa_alloc = 0;
988 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
991 62ee7d94 2023-01-10 thomas log_debug("expecting %zu capabilities from uid %d",
992 62ee7d94 2023-01-10 thomas client->ncapa_alloc, client->euid);
993 62ee7d94 2023-01-10 thomas return NULL;
996 62ee7d94 2023-01-10 thomas static const struct got_error *
997 62ee7d94 2023-01-10 thomas recv_capability(struct gotd_session_client *client, struct imsg *imsg)
999 62ee7d94 2023-01-10 thomas struct gotd_imsg_capability icapa;
1000 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
1001 62ee7d94 2023-01-10 thomas size_t datalen;
1002 62ee7d94 2023-01-10 thomas char *key, *value = NULL;
1004 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL ||
1005 62ee7d94 2023-01-10 thomas client->ncapabilities >= client->ncapa_alloc) {
1006 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1007 62ee7d94 2023-01-10 thomas "unexpected capability received");
1010 62ee7d94 2023-01-10 thomas memset(&icapa, 0, sizeof(icapa));
1012 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1013 62ee7d94 2023-01-10 thomas if (datalen < sizeof(icapa))
1014 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1015 62ee7d94 2023-01-10 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
1017 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1018 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1020 fcbb06bf 2023-01-14 thomas key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1021 62ee7d94 2023-01-10 thomas if (key == NULL)
1022 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1023 62ee7d94 2023-01-10 thomas if (icapa.value_len > 0) {
1024 fcbb06bf 2023-01-14 thomas value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1025 fcbb06bf 2023-01-14 thomas icapa.value_len);
1026 62ee7d94 2023-01-10 thomas if (value == NULL) {
1027 62ee7d94 2023-01-10 thomas free(key);
1028 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1032 62ee7d94 2023-01-10 thomas capa = &client->capabilities[client->ncapabilities++];
1033 62ee7d94 2023-01-10 thomas capa->key = key;
1034 62ee7d94 2023-01-10 thomas capa->value = value;
1036 62ee7d94 2023-01-10 thomas if (value)
1037 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
1039 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s", client->euid, key);
1041 62ee7d94 2023-01-10 thomas return NULL;
1044 62ee7d94 2023-01-10 thomas static const struct got_error *
1045 62ee7d94 2023-01-10 thomas ensure_client_is_reading(struct gotd_session_client *client)
1047 62ee7d94 2023-01-10 thomas if (client->is_writing) {
1048 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1049 62ee7d94 2023-01-10 thomas "uid %d made a read-request but is not reading from "
1050 62ee7d94 2023-01-10 thomas "a repository", client->euid);
1053 62ee7d94 2023-01-10 thomas return NULL;
1056 62ee7d94 2023-01-10 thomas static const struct got_error *
1057 62ee7d94 2023-01-10 thomas ensure_client_is_writing(struct gotd_session_client *client)
1059 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1060 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1061 62ee7d94 2023-01-10 thomas "uid %d made a write-request but is not writing to "
1062 62ee7d94 2023-01-10 thomas "a repository", client->euid);
1065 62ee7d94 2023-01-10 thomas return NULL;
1068 62ee7d94 2023-01-10 thomas static const struct got_error *
1069 62ee7d94 2023-01-10 thomas forward_want(struct gotd_session_client *client, struct imsg *imsg)
1071 62ee7d94 2023-01-10 thomas struct gotd_imsg_want ireq;
1072 62ee7d94 2023-01-10 thomas struct gotd_imsg_want iwant;
1073 62ee7d94 2023-01-10 thomas size_t datalen;
1075 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1076 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
1077 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1079 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
1081 62ee7d94 2023-01-10 thomas memset(&iwant, 0, sizeof(iwant));
1082 62ee7d94 2023-01-10 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
1083 62ee7d94 2023-01-10 thomas iwant.client_id = client->id;
1085 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1086 74e85132 2024-03-30 thomas GOTD_IMSG_WANT, gotd_session.proc_id, -1,
1087 74e85132 2024-03-30 thomas &iwant, sizeof(iwant)) == -1)
1088 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose WANT");
1090 62ee7d94 2023-01-10 thomas return NULL;
1093 62ee7d94 2023-01-10 thomas static const struct got_error *
1094 62ee7d94 2023-01-10 thomas forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1096 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1097 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update ireq;
1098 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref = NULL;
1099 62ee7d94 2023-01-10 thomas size_t datalen;
1101 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1102 62ee7d94 2023-01-10 thomas if (datalen < sizeof(ireq))
1103 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1104 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1105 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq) + ireq.name_len)
1106 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1108 62ee7d94 2023-01-10 thomas iref = malloc(datalen);
1109 62ee7d94 2023-01-10 thomas if (iref == NULL)
1110 62ee7d94 2023-01-10 thomas return got_error_from_errno("malloc");
1111 62ee7d94 2023-01-10 thomas memcpy(iref, imsg->data, datalen);
1113 62ee7d94 2023-01-10 thomas iref->client_id = client->id;
1114 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1115 7fed8fa4 2023-06-22 thomas GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
1116 7fed8fa4 2023-06-22 thomas iref, datalen) == -1)
1117 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
1118 62ee7d94 2023-01-10 thomas free(iref);
1119 62ee7d94 2023-01-10 thomas return err;
1122 62ee7d94 2023-01-10 thomas static const struct got_error *
1123 62ee7d94 2023-01-10 thomas forward_have(struct gotd_session_client *client, struct imsg *imsg)
1125 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ireq;
1126 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ihave;
1127 62ee7d94 2023-01-10 thomas size_t datalen;
1129 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1130 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
1131 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1133 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
1135 62ee7d94 2023-01-10 thomas memset(&ihave, 0, sizeof(ihave));
1136 62ee7d94 2023-01-10 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
1137 62ee7d94 2023-01-10 thomas ihave.client_id = client->id;
1139 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1140 74e85132 2024-03-30 thomas GOTD_IMSG_HAVE, gotd_session.proc_id, -1,
1141 74e85132 2024-03-30 thomas &ihave, sizeof(ihave)) == -1)
1142 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose HAVE");
1144 62ee7d94 2023-01-10 thomas return NULL;
1147 62ee7d94 2023-01-10 thomas static int
1148 62ee7d94 2023-01-10 thomas client_has_capability(struct gotd_session_client *client, const char *capastr)
1150 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
1151 62ee7d94 2023-01-10 thomas size_t i;
1153 62ee7d94 2023-01-10 thomas if (client->ncapabilities == 0)
1154 62ee7d94 2023-01-10 thomas return 0;
1156 62ee7d94 2023-01-10 thomas for (i = 0; i < client->ncapabilities; i++) {
1157 62ee7d94 2023-01-10 thomas capa = &client->capabilities[i];
1158 62ee7d94 2023-01-10 thomas if (strcmp(capa->key, capastr) == 0)
1159 62ee7d94 2023-01-10 thomas return 1;
1162 62ee7d94 2023-01-10 thomas return 0;
1165 62ee7d94 2023-01-10 thomas static const struct got_error *
1166 62ee7d94 2023-01-10 thomas recv_packfile(struct gotd_session_client *client)
1168 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1169 62ee7d94 2023-01-10 thomas struct gotd_imsg_recv_packfile ipack;
1170 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
1171 62ee7d94 2023-01-10 thomas struct gotd_imsg_packidx_file ifile;
1172 62ee7d94 2023-01-10 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1173 62ee7d94 2023-01-10 thomas int packfd = -1, idxfd = -1;
1174 62ee7d94 2023-01-10 thomas int pipe[2] = { -1, -1 };
1176 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
1177 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1178 62ee7d94 2023-01-10 thomas "uid %d already has a pack file", client->euid);
1181 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1182 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
1184 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
1185 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
1187 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1188 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1189 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
1190 62ee7d94 2023-01-10 thomas &ipipe, sizeof(ipipe)) == -1) {
1191 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1192 62ee7d94 2023-01-10 thomas pipe[0] = -1;
1193 62ee7d94 2023-01-10 thomas goto done;
1195 62ee7d94 2023-01-10 thomas pipe[0] = -1;
1197 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1198 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1199 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
1200 7fed8fa4 2023-06-22 thomas NULL, 0) == -1)
1201 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1202 62ee7d94 2023-01-10 thomas pipe[1] = -1;
1204 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1205 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1206 62ee7d94 2023-01-10 thomas client->euid) == -1) {
1207 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
1208 62ee7d94 2023-01-10 thomas goto done;
1211 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1212 62ee7d94 2023-01-10 thomas if (err)
1213 62ee7d94 2023-01-10 thomas goto done;
1214 851a5b48 2023-02-03 thomas if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1215 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", pack_path);
1216 851a5b48 2023-02-03 thomas goto done;
1219 62ee7d94 2023-01-10 thomas free(basepath);
1220 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1221 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1222 62ee7d94 2023-01-10 thomas client->euid) == -1) {
1223 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
1224 62ee7d94 2023-01-10 thomas basepath = NULL;
1225 62ee7d94 2023-01-10 thomas goto done;
1227 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1228 62ee7d94 2023-01-10 thomas if (err)
1229 62ee7d94 2023-01-10 thomas goto done;
1230 851a5b48 2023-02-03 thomas if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1231 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", idx_path);
1232 851a5b48 2023-02-03 thomas goto done;
1235 62ee7d94 2023-01-10 thomas memset(&ifile, 0, sizeof(ifile));
1236 62ee7d94 2023-01-10 thomas ifile.client_id = client->id;
1237 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1238 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
1239 62ee7d94 2023-01-10 thomas idxfd, &ifile, sizeof(ifile)) == -1) {
1240 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
1241 62ee7d94 2023-01-10 thomas idxfd = -1;
1242 62ee7d94 2023-01-10 thomas goto done;
1244 62ee7d94 2023-01-10 thomas idxfd = -1;
1246 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
1247 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
1248 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1249 62ee7d94 2023-01-10 thomas ipack.report_status = 1;
1251 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1252 7fed8fa4 2023-06-22 thomas GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
1253 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
1254 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
1255 62ee7d94 2023-01-10 thomas packfd = -1;
1256 62ee7d94 2023-01-10 thomas goto done;
1258 62ee7d94 2023-01-10 thomas packfd = -1;
1261 62ee7d94 2023-01-10 thomas free(basepath);
1262 62ee7d94 2023-01-10 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1263 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1264 62ee7d94 2023-01-10 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1265 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1266 62ee7d94 2023-01-10 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
1267 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1268 62ee7d94 2023-01-10 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1269 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1270 62ee7d94 2023-01-10 thomas if (err) {
1271 62ee7d94 2023-01-10 thomas free(pack_path);
1272 62ee7d94 2023-01-10 thomas free(idx_path);
1273 62ee7d94 2023-01-10 thomas } else {
1274 62ee7d94 2023-01-10 thomas client->packfile_path = pack_path;
1275 62ee7d94 2023-01-10 thomas client->packidx_path = idx_path;
1277 62ee7d94 2023-01-10 thomas return err;
1280 62ee7d94 2023-01-10 thomas static const struct got_error *
1281 62ee7d94 2023-01-10 thomas send_packfile(struct gotd_session_client *client)
1283 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1284 62ee7d94 2023-01-10 thomas struct gotd_imsg_send_packfile ipack;
1285 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
1286 62ee7d94 2023-01-10 thomas int pipe[2];
1288 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1289 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
1291 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
1292 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
1294 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
1295 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
1296 62ee7d94 2023-01-10 thomas ipack.report_progress = 1;
1298 62ee7d94 2023-01-10 thomas client->delta_cache_fd = got_opentempfd();
1299 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd == -1)
1300 62ee7d94 2023-01-10 thomas return got_error_from_errno("got_opentempfd");
1302 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1303 62ee7d94 2023-01-10 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
1304 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
1305 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
1306 62ee7d94 2023-01-10 thomas close(pipe[0]);
1307 62ee7d94 2023-01-10 thomas close(pipe[1]);
1308 62ee7d94 2023-01-10 thomas return err;
1311 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
1313 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1314 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1315 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1316 62ee7d94 2023-01-10 thomas pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1317 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1318 62ee7d94 2023-01-10 thomas close(pipe[1]);
1319 62ee7d94 2023-01-10 thomas return err;
1322 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1323 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1324 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1325 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1327 62ee7d94 2023-01-10 thomas return err;
1330 62ee7d94 2023-01-10 thomas static void
1331 8cb46987 2023-02-07 thomas session_dispatch_client(int fd, short events, void *arg)
1333 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1334 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1335 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1336 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1337 62ee7d94 2023-01-10 thomas struct imsg imsg;
1338 62ee7d94 2023-01-10 thomas ssize_t n;
1340 62ee7d94 2023-01-10 thomas if (events & EV_WRITE) {
1341 62ee7d94 2023-01-10 thomas while (ibuf->w.queued) {
1342 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1343 62ee7d94 2023-01-10 thomas if (n == -1 && errno == EPIPE) {
1345 62ee7d94 2023-01-10 thomas * The client has closed its socket.
1346 62ee7d94 2023-01-10 thomas * This can happen when Git clients are
1347 62ee7d94 2023-01-10 thomas * done sending pack file data.
1349 62ee7d94 2023-01-10 thomas msgbuf_clear(&ibuf->w);
1350 62ee7d94 2023-01-10 thomas continue;
1351 62ee7d94 2023-01-10 thomas } else if (n == -1 && errno != EAGAIN) {
1352 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg_flush");
1353 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1356 62ee7d94 2023-01-10 thomas if (n == 0) {
1357 62ee7d94 2023-01-10 thomas /* Connection closed. */
1358 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_EOF);
1359 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1364 c8b73ac1 2023-08-23 thomas if (client->flush_disconnect) {
1365 c8b73ac1 2023-08-23 thomas disconnect(client);
1370 62ee7d94 2023-01-10 thomas if ((events & EV_READ) == 0)
1373 62ee7d94 2023-01-10 thomas memset(&imsg, 0, sizeof(imsg));
1375 62ee7d94 2023-01-10 thomas while (err == NULL) {
1376 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1377 62ee7d94 2023-01-10 thomas if (err) {
1378 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1379 62ee7d94 2023-01-10 thomas err = NULL;
1380 8e92c55c 2023-06-08 thomas else if (err->code == GOT_ERR_EOF &&
1381 176350bf 2024-03-30 thomas gotd_session.state ==
1382 176350bf 2024-03-30 thomas GOTD_STATE_EXPECT_CAPABILITIES) {
1384 8e92c55c 2023-06-08 thomas * The client has closed its socket before
1385 8e92c55c 2023-06-08 thomas * sending its capability announcement.
1386 8e92c55c 2023-06-08 thomas * This can happen when Git clients have
1387 8e92c55c 2023-06-08 thomas * no ref-updates to send.
1389 8e92c55c 2023-06-08 thomas disconnect_on_error(client, err);
1395 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
1397 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1398 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITIES:
1399 176350bf 2024-03-30 thomas if (gotd_session.state !=
1400 176350bf 2024-03-30 thomas GOTD_STATE_EXPECT_CAPABILITIES) {
1401 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1402 62ee7d94 2023-01-10 thomas "unexpected capabilities received");
1405 62ee7d94 2023-01-10 thomas log_debug("receiving capabilities from uid %d",
1406 62ee7d94 2023-01-10 thomas client->euid);
1407 62ee7d94 2023-01-10 thomas err = recv_capabilities(client, &imsg);
1409 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITY:
1410 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1411 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1412 62ee7d94 2023-01-10 thomas "unexpected capability received");
1415 62ee7d94 2023-01-10 thomas err = recv_capability(client, &imsg);
1416 62ee7d94 2023-01-10 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1418 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1419 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_WANT;
1420 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1421 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting want-lines",
1422 62ee7d94 2023-01-10 thomas client->euid);
1423 62ee7d94 2023-01-10 thomas } else if (client->is_writing) {
1424 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1425 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1426 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting ref-update-lines",
1427 62ee7d94 2023-01-10 thomas client->euid);
1429 62ee7d94 2023-01-10 thomas fatalx("client %d is both reading and writing",
1430 62ee7d94 2023-01-10 thomas client->euid);
1432 62ee7d94 2023-01-10 thomas case GOTD_IMSG_WANT:
1433 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_WANT) {
1434 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1435 62ee7d94 2023-01-10 thomas "unexpected want-line received");
1438 62ee7d94 2023-01-10 thomas log_debug("received want-line from uid %d",
1439 62ee7d94 2023-01-10 thomas client->euid);
1440 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1441 62ee7d94 2023-01-10 thomas if (err)
1443 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1444 62ee7d94 2023-01-10 thomas err = forward_want(client, &imsg);
1446 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
1447 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1448 176350bf 2024-03-30 thomas gotd_session.state !=
1449 0445d8ec 2023-01-19 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1450 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1451 62ee7d94 2023-01-10 thomas "unexpected ref-update-line received");
1454 62ee7d94 2023-01-10 thomas log_debug("received ref-update-line from uid %d",
1455 62ee7d94 2023-01-10 thomas client->euid);
1456 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1457 62ee7d94 2023-01-10 thomas if (err)
1459 62ee7d94 2023-01-10 thomas err = forward_ref_update(client, &imsg);
1460 62ee7d94 2023-01-10 thomas if (err)
1462 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1463 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1465 62ee7d94 2023-01-10 thomas case GOTD_IMSG_HAVE:
1466 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_HAVE) {
1467 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1468 62ee7d94 2023-01-10 thomas "unexpected have-line received");
1471 62ee7d94 2023-01-10 thomas log_debug("received have-line from uid %d",
1472 62ee7d94 2023-01-10 thomas client->euid);
1473 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1474 62ee7d94 2023-01-10 thomas if (err)
1476 62ee7d94 2023-01-10 thomas err = forward_have(client, &imsg);
1477 62ee7d94 2023-01-10 thomas if (err)
1479 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1481 62ee7d94 2023-01-10 thomas case GOTD_IMSG_FLUSH:
1482 176350bf 2024-03-30 thomas if (gotd_session.state == GOTD_STATE_EXPECT_WANT ||
1483 176350bf 2024-03-30 thomas gotd_session.state == GOTD_STATE_EXPECT_HAVE) {
1484 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1485 62ee7d94 2023-01-10 thomas if (err)
1487 176350bf 2024-03-30 thomas } else if (gotd_session.state ==
1488 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1489 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1490 62ee7d94 2023-01-10 thomas if (err)
1492 176350bf 2024-03-30 thomas } else if (gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1493 98c7fd82 2023-01-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1494 98c7fd82 2023-01-23 thomas "unexpected flush-pkt received");
1497 98c7fd82 2023-01-23 thomas if (!client->accept_flush_pkt) {
1498 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1499 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1504 98c7fd82 2023-01-23 thomas * Accept just one flush packet at a time.
1505 98c7fd82 2023-01-23 thomas * Future client state transitions will set this flag
1506 98c7fd82 2023-01-23 thomas * again if another flush packet is expected.
1508 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 0;
1510 62ee7d94 2023-01-10 thomas log_debug("received flush-pkt from uid %d",
1511 62ee7d94 2023-01-10 thomas client->euid);
1512 176350bf 2024-03-30 thomas if (gotd_session.state == GOTD_STATE_EXPECT_WANT) {
1513 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_HAVE;
1514 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting have-lines",
1515 62ee7d94 2023-01-10 thomas client->euid);
1516 176350bf 2024-03-30 thomas } else if (gotd_session.state == GOTD_STATE_EXPECT_HAVE) {
1517 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_DONE;
1518 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1519 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting 'done'",
1520 62ee7d94 2023-01-10 thomas client->euid);
1521 176350bf 2024-03-30 thomas } else if (gotd_session.state ==
1522 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1523 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1524 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting packfile",
1525 62ee7d94 2023-01-10 thomas client->euid);
1526 62ee7d94 2023-01-10 thomas err = recv_packfile(client);
1527 176350bf 2024-03-30 thomas } else if (gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1528 62ee7d94 2023-01-10 thomas /* should not happen, see above */
1529 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1530 62ee7d94 2023-01-10 thomas "unexpected client state");
1534 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DONE:
1535 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_HAVE &&
1536 176350bf 2024-03-30 thomas gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1537 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1538 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1541 62ee7d94 2023-01-10 thomas log_debug("received 'done' from uid %d", client->euid);
1542 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1543 62ee7d94 2023-01-10 thomas if (err)
1545 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_DONE;
1546 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1547 62ee7d94 2023-01-10 thomas err = send_packfile(client);
1549 62ee7d94 2023-01-10 thomas default:
1550 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1551 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1555 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1558 62ee7d94 2023-01-10 thomas if (err) {
1559 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF ||
1560 176350bf 2024-03-30 thomas gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1561 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1562 62ee7d94 2023-01-10 thomas } else {
1563 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1564 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &gotd_session.request_timeout);
1568 62ee7d94 2023-01-10 thomas static const struct got_error *
1569 62ee7d94 2023-01-10 thomas list_refs_request(void)
1571 62ee7d94 2023-01-10 thomas static const struct got_error *err;
1572 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1573 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1574 62ee7d94 2023-01-10 thomas struct gotd_imsg_list_refs_internal ilref;
1577 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1578 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1580 62ee7d94 2023-01-10 thomas memset(&ilref, 0, sizeof(ilref));
1581 62ee7d94 2023-01-10 thomas ilref.client_id = client->id;
1583 62ee7d94 2023-01-10 thomas fd = dup(client->fd);
1584 62ee7d94 2023-01-10 thomas if (fd == -1)
1585 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1587 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1588 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1589 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1590 62ee7d94 2023-01-10 thomas close(fd);
1591 62ee7d94 2023-01-10 thomas return err;
1594 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1595 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting capabilities", client->euid);
1596 62ee7d94 2023-01-10 thomas return NULL;
1599 62ee7d94 2023-01-10 thomas static const struct got_error *
1600 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1602 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1603 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1604 62ee7d94 2023-01-10 thomas size_t datalen;
1606 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1607 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1609 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1610 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(iconnect))
1611 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1612 62ee7d94 2023-01-10 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1613 ce1bfad9 2024-03-30 thomas if (iconnect.username_len == 0 ||
1614 ce1bfad9 2024-03-30 thomas datalen != sizeof(iconnect) + iconnect.username_len)
1615 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1617 62ee7d94 2023-01-10 thomas client->euid = iconnect.euid;
1618 62ee7d94 2023-01-10 thomas client->egid = iconnect.egid;
1619 3d97effa 2024-01-31 thomas client->fd = imsg_get_fd(imsg);
1620 3d97effa 2024-01-31 thomas if (client->fd == -1)
1621 3d97effa 2024-01-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1623 ce1bfad9 2024-03-30 thomas client->username = strndup(imsg->data + sizeof(iconnect),
1624 ce1bfad9 2024-03-30 thomas iconnect.username_len);
1625 ce1bfad9 2024-03-30 thomas if (client->username == NULL)
1626 ce1bfad9 2024-03-30 thomas return got_error_from_errno("strndup");
1628 62ee7d94 2023-01-10 thomas imsg_init(&client->iev.ibuf, client->fd);
1629 8cb46987 2023-02-07 thomas client->iev.handler = session_dispatch_client;
1630 62ee7d94 2023-01-10 thomas client->iev.events = EV_READ;
1631 62ee7d94 2023-01-10 thomas client->iev.handler_arg = NULL;
1632 62ee7d94 2023-01-10 thomas event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1633 8cb46987 2023-02-07 thomas session_dispatch_client, &client->iev);
1634 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->iev);
1635 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1637 62ee7d94 2023-01-10 thomas return NULL;
1640 ce1bfad9 2024-03-30 thomas static void
1641 ce1bfad9 2024-03-30 thomas session_dispatch_notifier(int fd, short event, void *arg)
1643 ce1bfad9 2024-03-30 thomas const struct got_error *err;
1644 ce1bfad9 2024-03-30 thomas struct gotd_session_client *client = &gotd_session_client;
1645 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
1646 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1647 ce1bfad9 2024-03-30 thomas ssize_t n;
1648 ce1bfad9 2024-03-30 thomas int shut = 0;
1649 ce1bfad9 2024-03-30 thomas struct imsg imsg;
1650 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
1652 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
1653 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1654 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
1655 ce1bfad9 2024-03-30 thomas if (n == 0) {
1656 ce1bfad9 2024-03-30 thomas /* Connection closed. */
1657 ce1bfad9 2024-03-30 thomas shut = 1;
1658 ce1bfad9 2024-03-30 thomas goto done;
1662 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
1663 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
1664 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
1665 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
1666 ce1bfad9 2024-03-30 thomas if (n == 0) {
1667 ce1bfad9 2024-03-30 thomas /* Connection closed. */
1668 ce1bfad9 2024-03-30 thomas shut = 1;
1669 ce1bfad9 2024-03-30 thomas goto done;
1673 ce1bfad9 2024-03-30 thomas for (;;) {
1674 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1675 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
1676 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
1679 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
1680 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFICATION_SENT:
1681 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_NOTIFY) {
1682 ce1bfad9 2024-03-30 thomas log_warn("unexpected imsg %d", imsg.hdr.type);
1685 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(¬ifications);
1686 ce1bfad9 2024-03-30 thomas if (notif == NULL) {
1687 ce1bfad9 2024-03-30 thomas disconnect(client);
1688 ce1bfad9 2024-03-30 thomas break; /* NOTREACHED */
1690 ce1bfad9 2024-03-30 thomas /* Request content for the next notification. */
1691 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
1692 ce1bfad9 2024-03-30 thomas if (err) {
1693 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: %s",
1694 ce1bfad9 2024-03-30 thomas err->msg);
1695 ce1bfad9 2024-03-30 thomas disconnect(client);
1698 ce1bfad9 2024-03-30 thomas default:
1699 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1703 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
1706 ce1bfad9 2024-03-30 thomas if (!shut) {
1707 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
1708 ce1bfad9 2024-03-30 thomas } else {
1709 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
1710 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
1711 ce1bfad9 2024-03-30 thomas imsg_clear(&iev->ibuf);
1712 ce1bfad9 2024-03-30 thomas imsg_init(&iev->ibuf, -1);
1716 62ee7d94 2023-01-10 thomas static const struct got_error *
1717 ce1bfad9 2024-03-30 thomas recv_notifier(struct imsg *imsg)
1719 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1720 ce1bfad9 2024-03-30 thomas struct gotd_session_client *client = &gotd_session_client;
1721 ce1bfad9 2024-03-30 thomas size_t datalen;
1724 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1725 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1727 ce1bfad9 2024-03-30 thomas /* We should already have received a pipe to the listener. */
1728 ce1bfad9 2024-03-30 thomas if (client->fd == -1)
1729 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1731 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1732 ce1bfad9 2024-03-30 thomas if (datalen != 0)
1733 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1735 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
1736 ce1bfad9 2024-03-30 thomas if (fd == -1)
1737 ce1bfad9 2024-03-30 thomas return NULL; /* notifications unused */
1739 ce1bfad9 2024-03-30 thomas imsg_init(&iev->ibuf, fd);
1740 ce1bfad9 2024-03-30 thomas iev->handler = session_dispatch_notifier;
1741 ce1bfad9 2024-03-30 thomas iev->events = EV_READ;
1742 ce1bfad9 2024-03-30 thomas iev->handler_arg = NULL;
1743 ce1bfad9 2024-03-30 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1744 ce1bfad9 2024-03-30 thomas session_dispatch_notifier, iev);
1745 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
1747 ce1bfad9 2024-03-30 thomas return NULL;
1750 ce1bfad9 2024-03-30 thomas static const struct got_error *
1751 62ee7d94 2023-01-10 thomas recv_repo_child(struct imsg *imsg)
1753 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ichild;
1754 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1755 62ee7d94 2023-01-10 thomas size_t datalen;
1758 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1759 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1761 62ee7d94 2023-01-10 thomas /* We should already have received a pipe to the listener. */
1762 62ee7d94 2023-01-10 thomas if (client->fd == -1)
1763 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1765 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1766 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ichild))
1767 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1769 62ee7d94 2023-01-10 thomas memcpy(&ichild, imsg->data, sizeof(ichild));
1771 62ee7d94 2023-01-10 thomas client->id = ichild.client_id;
1772 62ee7d94 2023-01-10 thomas if (ichild.proc_id == PROC_REPO_WRITE)
1773 62ee7d94 2023-01-10 thomas client->is_writing = 1;
1774 62ee7d94 2023-01-10 thomas else if (ichild.proc_id == PROC_REPO_READ)
1775 62ee7d94 2023-01-10 thomas client->is_writing = 0;
1777 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1778 62ee7d94 2023-01-10 thomas "bad child process type");
1780 3d97effa 2024-01-31 thomas fd = imsg_get_fd(imsg);
1781 3d97effa 2024-01-31 thomas if (fd == -1)
1782 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1784 74e85132 2024-03-30 thomas imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1785 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1786 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.events = EV_READ;
1787 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.handler_arg = NULL;
1788 74e85132 2024-03-30 thomas event_set(&gotd_session.repo_child_iev.ev,
1789 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1790 74e85132 2024-03-30 thomas session_dispatch_repo_child, &gotd_session.repo_child_iev);
1791 74e85132 2024-03-30 thomas gotd_imsg_event_add(&gotd_session.repo_child_iev);
1793 62ee7d94 2023-01-10 thomas /* The "recvfd" pledge promise is no longer needed. */
1794 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1795 62ee7d94 2023-01-10 thomas fatal("pledge");
1797 62ee7d94 2023-01-10 thomas return NULL;
1800 62ee7d94 2023-01-10 thomas static void
1801 62ee7d94 2023-01-10 thomas session_dispatch(int fd, short event, void *arg)
1803 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1804 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1805 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1806 62ee7d94 2023-01-10 thomas ssize_t n;
1807 62ee7d94 2023-01-10 thomas int shut = 0;
1808 62ee7d94 2023-01-10 thomas struct imsg imsg;
1810 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1811 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1812 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1813 62ee7d94 2023-01-10 thomas if (n == 0) {
1814 62ee7d94 2023-01-10 thomas /* Connection closed. */
1815 62ee7d94 2023-01-10 thomas shut = 1;
1816 62ee7d94 2023-01-10 thomas goto done;
1820 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1821 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1822 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1823 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1824 62ee7d94 2023-01-10 thomas if (n == 0) {
1825 62ee7d94 2023-01-10 thomas /* Connection closed. */
1826 62ee7d94 2023-01-10 thomas shut = 1;
1827 62ee7d94 2023-01-10 thomas goto done;
1831 62ee7d94 2023-01-10 thomas for (;;) {
1832 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1833 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1834 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_list_refs = 0;
1836 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1837 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1838 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1841 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1842 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1843 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1844 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1846 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT:
1847 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1849 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1850 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1852 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_CONNECT_NOTIFIER:
1853 ce1bfad9 2024-03-30 thomas err = recv_notifier(&imsg);
1855 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1856 62ee7d94 2023-01-10 thomas err = recv_repo_child(&imsg);
1857 62ee7d94 2023-01-10 thomas if (err)
1859 62ee7d94 2023-01-10 thomas do_list_refs = 1;
1861 62ee7d94 2023-01-10 thomas default:
1862 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1865 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1867 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1868 62ee7d94 2023-01-10 thomas if (err)
1869 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1871 62ee7d94 2023-01-10 thomas disconnect(client);
1872 62ee7d94 2023-01-10 thomas } else if (do_list_refs)
1873 62ee7d94 2023-01-10 thomas err = list_refs_request();
1875 62ee7d94 2023-01-10 thomas if (err)
1876 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1879 62ee7d94 2023-01-10 thomas if (!shut) {
1880 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1881 62ee7d94 2023-01-10 thomas } else {
1882 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1883 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1884 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1889 62ee7d94 2023-01-10 thomas session_main(const char *title, const char *repo_path,
1890 7fed8fa4 2023-06-22 thomas int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1891 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo_cfg, enum gotd_procid proc_id)
1893 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1894 62ee7d94 2023-01-10 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
1896 ce1bfad9 2024-03-30 thomas STAILQ_INIT(¬ifications);
1898 62ee7d94 2023-01-10 thomas gotd_session.title = title;
1899 62ee7d94 2023-01-10 thomas gotd_session.pid = getpid();
1900 62ee7d94 2023-01-10 thomas gotd_session.pack_fds = pack_fds;
1901 62ee7d94 2023-01-10 thomas gotd_session.temp_fds = temp_fds;
1902 62ee7d94 2023-01-10 thomas memcpy(&gotd_session.request_timeout, request_timeout,
1903 62ee7d94 2023-01-10 thomas sizeof(gotd_session.request_timeout));
1904 ce1bfad9 2024-03-30 thomas gotd_session.repo_cfg = repo_cfg;
1905 7fed8fa4 2023-06-22 thomas gotd_session.proc_id = proc_id;
1907 ce1bfad9 2024-03-30 thomas imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1909 62ee7d94 2023-01-10 thomas err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1910 62ee7d94 2023-01-10 thomas if (err)
1911 62ee7d94 2023-01-10 thomas goto done;
1912 62ee7d94 2023-01-10 thomas if (!got_repo_is_bare(gotd_session.repo)) {
1913 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1914 62ee7d94 2023-01-10 thomas "bare git repository required");
1915 62ee7d94 2023-01-10 thomas goto done;
1918 62ee7d94 2023-01-10 thomas got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1920 62ee7d94 2023-01-10 thomas signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1921 62ee7d94 2023-01-10 thomas signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1922 62ee7d94 2023-01-10 thomas signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1923 62ee7d94 2023-01-10 thomas signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1924 62ee7d94 2023-01-10 thomas signal(SIGPIPE, SIG_IGN);
1926 62ee7d94 2023-01-10 thomas signal_add(&evsigint, NULL);
1927 62ee7d94 2023-01-10 thomas signal_add(&evsigterm, NULL);
1928 62ee7d94 2023-01-10 thomas signal_add(&evsighup, NULL);
1929 62ee7d94 2023-01-10 thomas signal_add(&evsigusr1, NULL);
1931 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1933 62ee7d94 2023-01-10 thomas gotd_session_client.fd = -1;
1934 62ee7d94 2023-01-10 thomas gotd_session_client.nref_updates = -1;
1935 a6f25078 2023-01-10 thomas gotd_session_client.delta_cache_fd = -1;
1936 98c7fd82 2023-01-23 thomas gotd_session_client.accept_flush_pkt = 1;
1938 62ee7d94 2023-01-10 thomas imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1939 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler = session_dispatch;
1940 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.events = EV_READ;
1941 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler_arg = NULL;
1942 62ee7d94 2023-01-10 thomas event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1943 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch, &gotd_session.parent_iev);
1944 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1945 7fed8fa4 2023-06-22 thomas GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1946 7fed8fa4 2023-06-22 thomas -1, NULL, 0) == -1) {
1947 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1948 62ee7d94 2023-01-10 thomas goto done;
1951 62ee7d94 2023-01-10 thomas event_dispatch();
1953 62ee7d94 2023-01-10 thomas if (err)
1954 62ee7d94 2023-01-10 thomas log_warnx("%s: %s", title, err->msg);
1955 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
1959 62ee7d94 2023-01-10 thomas gotd_session_shutdown(void)
1961 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
1963 5330ab76 2023-02-17 thomas log_debug("shutting down");
1965 ce1bfad9 2024-03-30 thomas while (!STAILQ_EMPTY(¬ifications)) {
1966 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(¬ifications);
1967 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE_HEAD(¬ifications, entry);
1968 ce1bfad9 2024-03-30 thomas if (notif->fd != -1)
1969 ce1bfad9 2024-03-30 thomas close(notif->fd);
1970 ce1bfad9 2024-03-30 thomas free(notif->refname);
1971 ce1bfad9 2024-03-30 thomas free(notif);
1974 62ee7d94 2023-01-10 thomas if (gotd_session.repo)
1975 62ee7d94 2023-01-10 thomas got_repo_close(gotd_session.repo);
1976 62ee7d94 2023-01-10 thomas got_repo_pack_fds_close(gotd_session.pack_fds);
1977 62ee7d94 2023-01-10 thomas got_repo_temp_fds_close(gotd_session.temp_fds);
1978 ce1bfad9 2024-03-30 thomas free(gotd_session_client.username);
1979 62ee7d94 2023-01-10 thomas exit(0);