2 e70fd952 2024-03-22 stsp * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 e70fd952 2024-03-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 e70fd952 2024-03-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 e70fd952 2024-03-22 stsp * copyright notice and this permission notice appear in all copies.
8 e70fd952 2024-03-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e70fd952 2024-03-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e70fd952 2024-03-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e70fd952 2024-03-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e70fd952 2024-03-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e70fd952 2024-03-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e70fd952 2024-03-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 e70fd952 2024-03-22 stsp #include <sys/types.h>
18 e70fd952 2024-03-22 stsp #include <sys/queue.h>
19 e70fd952 2024-03-22 stsp #include <sys/tree.h>
20 e70fd952 2024-03-22 stsp #include <sys/socket.h>
21 e70fd952 2024-03-22 stsp #include <sys/stat.h>
22 e70fd952 2024-03-22 stsp #include <sys/uio.h>
24 e70fd952 2024-03-22 stsp #include <errno.h>
25 e70fd952 2024-03-22 stsp #include <event.h>
26 e70fd952 2024-03-22 stsp #include <limits.h>
27 e70fd952 2024-03-22 stsp #include <sha1.h>
28 e70fd952 2024-03-22 stsp #include <sha2.h>
29 e70fd952 2024-03-22 stsp #include <signal.h>
30 e70fd952 2024-03-22 stsp #include <stdint.h>
31 e70fd952 2024-03-22 stsp #include <stdio.h>
32 e70fd952 2024-03-22 stsp #include <stdlib.h>
33 e70fd952 2024-03-22 stsp #include <string.h>
34 e70fd952 2024-03-22 stsp #include <imsg.h>
35 e70fd952 2024-03-22 stsp #include <unistd.h>
37 e70fd952 2024-03-22 stsp #include "got_error.h"
38 e70fd952 2024-03-22 stsp #include "got_repository.h"
39 e70fd952 2024-03-22 stsp #include "got_object.h"
40 e70fd952 2024-03-22 stsp #include "got_path.h"
41 e70fd952 2024-03-22 stsp #include "got_reference.h"
42 e70fd952 2024-03-22 stsp #include "got_opentemp.h"
44 e70fd952 2024-03-22 stsp #include "got_lib_hash.h"
45 e70fd952 2024-03-22 stsp #include "got_lib_delta.h"
46 e70fd952 2024-03-22 stsp #include "got_lib_object.h"
47 e70fd952 2024-03-22 stsp #include "got_lib_object_cache.h"
48 e70fd952 2024-03-22 stsp #include "got_lib_pack.h"
49 e70fd952 2024-03-22 stsp #include "got_lib_repository.h"
50 e70fd952 2024-03-22 stsp #include "got_lib_gitproto.h"
52 e70fd952 2024-03-22 stsp #include "gotd.h"
53 e70fd952 2024-03-22 stsp #include "log.h"
54 e70fd952 2024-03-22 stsp #include "session_write.h"
56 e70fd952 2024-03-22 stsp struct gotd_session_notif {
57 e70fd952 2024-03-22 stsp STAILQ_ENTRY(gotd_session_notif) entry;
59 e70fd952 2024-03-22 stsp enum gotd_notification_action action;
60 e70fd952 2024-03-22 stsp char *refname;
61 e70fd952 2024-03-22 stsp struct got_object_id old_id;
62 e70fd952 2024-03-22 stsp struct got_object_id new_id;
64 e70fd952 2024-03-22 stsp STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
66 e70fd952 2024-03-22 stsp enum gotd_session_write_state {
67 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_LIST_REFS,
68 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES,
69 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_REF_UPDATE,
70 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_PACKFILE,
72 e70fd952 2024-03-22 stsp GOTD_STATE_NOTIFY,
75 e70fd952 2024-03-22 stsp static struct gotd_session_write {
77 e70fd952 2024-03-22 stsp const char *title;
78 e70fd952 2024-03-22 stsp struct got_repository *repo;
79 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg;
80 e70fd952 2024-03-22 stsp int *pack_fds;
81 e70fd952 2024-03-22 stsp int *temp_fds;
82 e70fd952 2024-03-22 stsp struct gotd_imsgev parent_iev;
83 e70fd952 2024-03-22 stsp struct gotd_imsgev notifier_iev;
84 e70fd952 2024-03-22 stsp struct timeval request_timeout;
85 e70fd952 2024-03-22 stsp enum gotd_session_write_state state;
86 e70fd952 2024-03-22 stsp struct gotd_imsgev repo_child_iev;
87 e70fd952 2024-03-22 stsp } gotd_session;
89 e70fd952 2024-03-22 stsp static struct gotd_session_client {
90 e70fd952 2024-03-22 stsp struct gotd_client_capability *capabilities;
91 e70fd952 2024-03-22 stsp size_t ncapa_alloc;
92 e70fd952 2024-03-22 stsp size_t ncapabilities;
93 e70fd952 2024-03-22 stsp uint32_t id;
95 e70fd952 2024-03-22 stsp int delta_cache_fd;
96 e70fd952 2024-03-22 stsp struct gotd_imsgev iev;
97 e70fd952 2024-03-22 stsp struct event tmo;
100 e70fd952 2024-03-22 stsp char *username;
101 e70fd952 2024-03-22 stsp char *packfile_path;
102 e70fd952 2024-03-22 stsp char *packidx_path;
103 e70fd952 2024-03-22 stsp int nref_updates;
104 e70fd952 2024-03-22 stsp int accept_flush_pkt;
105 e70fd952 2024-03-22 stsp int flush_disconnect;
106 e70fd952 2024-03-22 stsp } gotd_session_client;
108 e70fd952 2024-03-22 stsp static void session_write_shutdown(void);
110 e70fd952 2024-03-22 stsp static void
111 e70fd952 2024-03-22 stsp disconnect(struct gotd_session_client *client)
113 e70fd952 2024-03-22 stsp log_debug("uid %d: disconnecting", client->euid);
115 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
116 e70fd952 2024-03-22 stsp GOTD_IMSG_DISCONNECT, PROC_SESSION_WRITE, -1, NULL, 0) == -1)
117 e70fd952 2024-03-22 stsp log_warn("imsg compose DISCONNECT");
119 e70fd952 2024-03-22 stsp imsg_clear(&gotd_session.repo_child_iev.ibuf);
120 e70fd952 2024-03-22 stsp event_del(&gotd_session.repo_child_iev.ev);
121 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
122 e70fd952 2024-03-22 stsp close(client->fd);
123 e70fd952 2024-03-22 stsp if (client->delta_cache_fd != -1)
124 e70fd952 2024-03-22 stsp close(client->delta_cache_fd);
125 e70fd952 2024-03-22 stsp if (client->packfile_path) {
126 e70fd952 2024-03-22 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
127 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packfile_path);
128 e70fd952 2024-03-22 stsp free(client->packfile_path);
130 e70fd952 2024-03-22 stsp if (client->packidx_path) {
131 e70fd952 2024-03-22 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
132 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packidx_path);
133 e70fd952 2024-03-22 stsp free(client->packidx_path);
135 e70fd952 2024-03-22 stsp free(client->capabilities);
137 e70fd952 2024-03-22 stsp session_write_shutdown();
140 e70fd952 2024-03-22 stsp static void
141 e70fd952 2024-03-22 stsp disconnect_on_error(struct gotd_session_client *client,
142 e70fd952 2024-03-22 stsp const struct got_error *err)
144 e70fd952 2024-03-22 stsp struct imsgbuf ibuf;
146 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF) {
147 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
148 e70fd952 2024-03-22 stsp imsg_init(&ibuf, client->fd);
149 e70fd952 2024-03-22 stsp gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_WRITE, err);
150 e70fd952 2024-03-22 stsp imsg_clear(&ibuf);
153 e70fd952 2024-03-22 stsp disconnect(client);
156 e70fd952 2024-03-22 stsp static void
157 e70fd952 2024-03-22 stsp gotd_request_timeout(int fd, short events, void *arg)
159 e70fd952 2024-03-22 stsp struct gotd_session_client *client = arg;
161 e70fd952 2024-03-22 stsp log_debug("disconnecting uid %d due to timeout", client->euid);
162 e70fd952 2024-03-22 stsp disconnect(client);
165 e70fd952 2024-03-22 stsp static void
166 e70fd952 2024-03-22 stsp session_write_sighdlr(int sig, short event, void *arg)
169 e70fd952 2024-03-22 stsp * Normal signal handler rules don't apply because libevent
170 e70fd952 2024-03-22 stsp * decouples for us.
173 e70fd952 2024-03-22 stsp switch (sig) {
174 e70fd952 2024-03-22 stsp case SIGHUP:
175 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGHUP", __func__);
177 e70fd952 2024-03-22 stsp case SIGUSR1:
178 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGUSR1", __func__);
180 e70fd952 2024-03-22 stsp case SIGTERM:
181 e70fd952 2024-03-22 stsp case SIGINT:
182 e70fd952 2024-03-22 stsp session_write_shutdown();
183 e70fd952 2024-03-22 stsp /* NOTREACHED */
186 e70fd952 2024-03-22 stsp fatalx("unexpected signal");
190 e70fd952 2024-03-22 stsp static const struct got_error *
191 e70fd952 2024-03-22 stsp recv_packfile_install(struct imsg *imsg)
193 e70fd952 2024-03-22 stsp struct gotd_imsg_packfile_install inst;
194 e70fd952 2024-03-22 stsp size_t datalen;
196 e70fd952 2024-03-22 stsp log_debug("packfile-install received");
198 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 e70fd952 2024-03-22 stsp if (datalen != sizeof(inst))
200 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
201 e70fd952 2024-03-22 stsp memcpy(&inst, imsg->data, sizeof(inst));
203 e70fd952 2024-03-22 stsp return NULL;
206 e70fd952 2024-03-22 stsp static const struct got_error *
207 e70fd952 2024-03-22 stsp recv_ref_updates_start(struct imsg *imsg)
209 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_updates_start istart;
210 e70fd952 2024-03-22 stsp size_t datalen;
212 e70fd952 2024-03-22 stsp log_debug("ref-updates-start received");
214 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 e70fd952 2024-03-22 stsp if (datalen != sizeof(istart))
216 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
217 e70fd952 2024-03-22 stsp memcpy(&istart, imsg->data, sizeof(istart));
219 e70fd952 2024-03-22 stsp return NULL;
222 e70fd952 2024-03-22 stsp static const struct got_error *
223 e70fd952 2024-03-22 stsp recv_ref_update(struct imsg *imsg)
225 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update iref;
226 e70fd952 2024-03-22 stsp size_t datalen;
228 e70fd952 2024-03-22 stsp log_debug("ref-update received");
230 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 e70fd952 2024-03-22 stsp if (datalen < sizeof(iref))
232 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
233 e70fd952 2024-03-22 stsp memcpy(&iref, imsg->data, sizeof(iref));
235 e70fd952 2024-03-22 stsp return NULL;
238 e70fd952 2024-03-22 stsp static const struct got_error *
239 e70fd952 2024-03-22 stsp send_ref_update_ok(struct gotd_session_client *client,
240 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref, const char *refname)
242 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update_ok iok;
243 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &client->iev;
244 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
245 e70fd952 2024-03-22 stsp size_t len;
247 e70fd952 2024-03-22 stsp memset(&iok, 0, sizeof(iok));
248 e70fd952 2024-03-22 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
249 e70fd952 2024-03-22 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
250 e70fd952 2024-03-22 stsp iok.name_len = strlen(refname);
252 e70fd952 2024-03-22 stsp len = sizeof(iok) + iok.name_len;
253 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
254 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
255 e70fd952 2024-03-22 stsp if (wbuf == NULL)
256 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
258 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
259 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
260 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
261 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
264 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
265 e70fd952 2024-03-22 stsp return NULL;
268 e70fd952 2024-03-22 stsp static void
269 e70fd952 2024-03-22 stsp send_refs_updated(struct gotd_session_client *client)
271 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, -1, NULL, 0) == -1)
273 e70fd952 2024-03-22 stsp log_warn("imsg compose REFS_UPDATED");
276 e70fd952 2024-03-22 stsp static const struct got_error *
277 e70fd952 2024-03-22 stsp send_ref_update_ng(struct gotd_session_client *client,
278 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref, const char *refname,
279 e70fd952 2024-03-22 stsp const char *reason)
281 e70fd952 2024-03-22 stsp const struct got_error *ng_err;
282 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update_ng ing;
283 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &client->iev;
284 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
285 e70fd952 2024-03-22 stsp size_t len;
287 e70fd952 2024-03-22 stsp memset(&ing, 0, sizeof(ing));
288 e70fd952 2024-03-22 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 e70fd952 2024-03-22 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 e70fd952 2024-03-22 stsp ing.name_len = strlen(refname);
292 e70fd952 2024-03-22 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 e70fd952 2024-03-22 stsp ing.reason_len = strlen(ng_err->msg);
295 e70fd952 2024-03-22 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
296 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
298 e70fd952 2024-03-22 stsp if (wbuf == NULL)
299 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
309 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
310 e70fd952 2024-03-22 stsp return NULL;
313 e70fd952 2024-03-22 stsp static const struct got_error *
314 e70fd952 2024-03-22 stsp install_pack(struct gotd_session_client *client, const char *repo_path,
315 e70fd952 2024-03-22 stsp struct imsg *imsg)
317 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
318 e70fd952 2024-03-22 stsp struct gotd_imsg_packfile_install inst;
319 e70fd952 2024-03-22 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
320 e70fd952 2024-03-22 stsp size_t datalen;
321 e70fd952 2024-03-22 stsp char *packfile_path = NULL, *packidx_path = NULL;
323 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
324 e70fd952 2024-03-22 stsp if (datalen != sizeof(inst))
325 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
326 e70fd952 2024-03-22 stsp memcpy(&inst, imsg->data, sizeof(inst));
328 e70fd952 2024-03-22 stsp if (client->packfile_path == NULL)
329 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
330 e70fd952 2024-03-22 stsp "client has no pack file");
331 e70fd952 2024-03-22 stsp if (client->packidx_path == NULL)
332 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
333 e70fd952 2024-03-22 stsp "client has no pack file index");
335 e70fd952 2024-03-22 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
336 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_NO_SPACE,
337 e70fd952 2024-03-22 stsp "could not convert pack file SHA1 to hex");
339 e70fd952 2024-03-22 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
340 e70fd952 2024-03-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
341 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
345 e70fd952 2024-03-22 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
346 e70fd952 2024-03-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
351 e70fd952 2024-03-22 stsp if (rename(client->packfile_path, packfile_path) == -1) {
352 e70fd952 2024-03-22 stsp err = got_error_from_errno3("rename", client->packfile_path,
353 e70fd952 2024-03-22 stsp packfile_path);
357 e70fd952 2024-03-22 stsp free(client->packfile_path);
358 e70fd952 2024-03-22 stsp client->packfile_path = NULL;
360 e70fd952 2024-03-22 stsp if (rename(client->packidx_path, packidx_path) == -1) {
361 e70fd952 2024-03-22 stsp err = got_error_from_errno3("rename", client->packidx_path,
362 e70fd952 2024-03-22 stsp packidx_path);
366 e70fd952 2024-03-22 stsp /* Ensure we re-read the pack index list upon next access. */
367 e70fd952 2024-03-22 stsp gotd_session.repo->pack_path_mtime.tv_sec = 0;
368 e70fd952 2024-03-22 stsp gotd_session.repo->pack_path_mtime.tv_nsec = 0;
370 e70fd952 2024-03-22 stsp free(client->packidx_path);
371 e70fd952 2024-03-22 stsp client->packidx_path = NULL;
373 e70fd952 2024-03-22 stsp free(packfile_path);
374 e70fd952 2024-03-22 stsp free(packidx_path);
375 e70fd952 2024-03-22 stsp return err;
378 e70fd952 2024-03-22 stsp static const struct got_error *
379 e70fd952 2024-03-22 stsp begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
381 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_updates_start istart;
382 e70fd952 2024-03-22 stsp size_t datalen;
384 e70fd952 2024-03-22 stsp if (client->nref_updates != -1)
385 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
387 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 e70fd952 2024-03-22 stsp if (datalen != sizeof(istart))
389 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
390 e70fd952 2024-03-22 stsp memcpy(&istart, imsg->data, sizeof(istart));
392 e70fd952 2024-03-22 stsp if (istart.nref_updates <= 0)
393 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
395 e70fd952 2024-03-22 stsp client->nref_updates = istart.nref_updates;
396 e70fd952 2024-03-22 stsp return NULL;
399 e70fd952 2024-03-22 stsp static const struct got_error *
400 e70fd952 2024-03-22 stsp validate_namespace(const char *namespace)
402 e70fd952 2024-03-22 stsp size_t len = strlen(namespace);
404 e70fd952 2024-03-22 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
405 e70fd952 2024-03-22 stsp namespace[len - 1] != '/') {
406 e70fd952 2024-03-22 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
407 e70fd952 2024-03-22 stsp "reference namespace '%s'", namespace);
410 e70fd952 2024-03-22 stsp return NULL;
413 e70fd952 2024-03-22 stsp static const struct got_error *
414 e70fd952 2024-03-22 stsp queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
415 e70fd952 2024-03-22 stsp struct got_repository *repo, struct got_reference *ref)
417 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
418 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
419 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
420 e70fd952 2024-03-22 stsp struct got_pathlist_entry *pe;
421 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
423 e70fd952 2024-03-22 stsp if (iev->ibuf.fd == -1 ||
424 e70fd952 2024-03-22 stsp STAILQ_EMPTY(&repo_cfg->notification_targets))
425 e70fd952 2024-03-22 stsp return NULL; /* notifications unused */
427 e70fd952 2024-03-22 stsp TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
428 e70fd952 2024-03-22 stsp const char *refname = pe->path;
429 e70fd952 2024-03-22 stsp if (strcmp(got_ref_get_name(ref), refname) == 0)
432 e70fd952 2024-03-22 stsp if (pe == NULL) {
433 e70fd952 2024-03-22 stsp TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
435 e70fd952 2024-03-22 stsp const char *namespace = pe->path;
437 e70fd952 2024-03-22 stsp err = validate_namespace(namespace);
439 e70fd952 2024-03-22 stsp return err;
440 e70fd952 2024-03-22 stsp if (strncmp(namespace, got_ref_get_name(ref),
441 e70fd952 2024-03-22 stsp strlen(namespace)) == 0)
447 e70fd952 2024-03-22 stsp * If a branch or a reference namespace was specified in the
448 e70fd952 2024-03-22 stsp * configuration file then only send notifications if a match
449 e70fd952 2024-03-22 stsp * was found.
451 e70fd952 2024-03-22 stsp if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
452 e70fd952 2024-03-22 stsp !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
453 e70fd952 2024-03-22 stsp return NULL;
455 e70fd952 2024-03-22 stsp notif = calloc(1, sizeof(*notif));
456 e70fd952 2024-03-22 stsp if (notif == NULL)
457 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
459 e70fd952 2024-03-22 stsp notif->fd = -1;
461 e70fd952 2024-03-22 stsp if (old_id == NULL)
462 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_CREATED;
463 e70fd952 2024-03-22 stsp else if (new_id == NULL)
464 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_REMOVED;
466 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_CHANGED;
468 e70fd952 2024-03-22 stsp if (old_id != NULL)
469 e70fd952 2024-03-22 stsp memcpy(¬if->old_id, old_id, sizeof(notif->old_id));
470 e70fd952 2024-03-22 stsp if (new_id != NULL)
471 e70fd952 2024-03-22 stsp memcpy(¬if->new_id, new_id, sizeof(notif->new_id));
473 e70fd952 2024-03-22 stsp notif->refname = strdup(got_ref_get_name(ref));
474 e70fd952 2024-03-22 stsp if (notif->refname == NULL) {
475 e70fd952 2024-03-22 stsp err = got_error_from_errno("strdup");
479 e70fd952 2024-03-22 stsp STAILQ_INSERT_TAIL(¬ifications, notif, entry);
481 e70fd952 2024-03-22 stsp if (err && notif) {
482 e70fd952 2024-03-22 stsp free(notif->refname);
483 e70fd952 2024-03-22 stsp free(notif);
485 e70fd952 2024-03-22 stsp return err;
488 e70fd952 2024-03-22 stsp /* Forward notification content to the NOTIFY process. */
489 e70fd952 2024-03-22 stsp static const struct got_error *
490 e70fd952 2024-03-22 stsp forward_notification(struct gotd_session_client *client, struct imsg *imsg)
492 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
493 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.notifier_iev;
494 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
495 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content icontent;
496 e70fd952 2024-03-22 stsp char *refname = NULL;
497 e70fd952 2024-03-22 stsp size_t datalen;
498 e70fd952 2024-03-22 stsp struct gotd_imsg_notify inotify;
499 e70fd952 2024-03-22 stsp const char *action;
501 e70fd952 2024-03-22 stsp memset(&inotify, 0, sizeof(inotify));
503 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
504 e70fd952 2024-03-22 stsp if (datalen < sizeof(icontent))
505 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
506 e70fd952 2024-03-22 stsp memcpy(&icontent, imsg->data, sizeof(icontent));
507 e70fd952 2024-03-22 stsp if (datalen != sizeof(icontent) + icontent.refname_len)
508 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
509 e70fd952 2024-03-22 stsp refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
510 e70fd952 2024-03-22 stsp if (refname == NULL)
511 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
513 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
514 e70fd952 2024-03-22 stsp if (notif == NULL)
515 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
517 e70fd952 2024-03-22 stsp STAILQ_REMOVE(¬ifications, notif, gotd_session_notif, entry);
519 e70fd952 2024-03-22 stsp if (notif->action != icontent.action || notif->fd == -1 ||
520 e70fd952 2024-03-22 stsp strcmp(notif->refname, refname) != 0) {
521 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
524 e70fd952 2024-03-22 stsp if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
525 e70fd952 2024-03-22 stsp if (memcmp(notif->new_id.sha1, icontent.new_id,
526 e70fd952 2024-03-22 stsp SHA1_DIGEST_LENGTH) != 0) {
527 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
528 e70fd952 2024-03-22 stsp "received notification content for unknown event");
531 e70fd952 2024-03-22 stsp } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
532 e70fd952 2024-03-22 stsp if (memcmp(notif->old_id.sha1, icontent.old_id,
533 e70fd952 2024-03-22 stsp SHA1_DIGEST_LENGTH) != 0) {
534 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
535 e70fd952 2024-03-22 stsp "received notification content for unknown event");
538 e70fd952 2024-03-22 stsp } else if (memcmp(notif->old_id.sha1, icontent.old_id,
539 e70fd952 2024-03-22 stsp SHA1_DIGEST_LENGTH) != 0 ||
540 e70fd952 2024-03-22 stsp memcmp(notif->new_id.sha1, icontent.new_id,
541 e70fd952 2024-03-22 stsp SHA1_DIGEST_LENGTH) != 0) {
542 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
543 e70fd952 2024-03-22 stsp "received notification content for unknown event");
547 e70fd952 2024-03-22 stsp switch (notif->action) {
548 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_CREATED:
549 e70fd952 2024-03-22 stsp action = "created";
551 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_REMOVED:
552 e70fd952 2024-03-22 stsp action = "removed";
554 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_CHANGED:
555 e70fd952 2024-03-22 stsp action = "changed";
558 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
562 e70fd952 2024-03-22 stsp strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
563 e70fd952 2024-03-22 stsp sizeof(inotify.repo_name));
565 e70fd952 2024-03-22 stsp snprintf(inotify.subject_line, sizeof(inotify.subject_line),
566 e70fd952 2024-03-22 stsp "%s: %s %s %s", gotd_session.repo_cfg->name,
567 e70fd952 2024-03-22 stsp client->username, action, notif->refname);
569 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFY,
570 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, notif->fd, &inotify, sizeof(inotify))
572 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose NOTIFY");
575 e70fd952 2024-03-22 stsp notif->fd = -1;
577 e70fd952 2024-03-22 stsp if (notif->fd != -1)
578 e70fd952 2024-03-22 stsp close(notif->fd);
579 e70fd952 2024-03-22 stsp free(notif);
580 e70fd952 2024-03-22 stsp free(refname);
581 e70fd952 2024-03-22 stsp return err;
584 e70fd952 2024-03-22 stsp /* Request notification content from REPO_WRITE process. */
585 e70fd952 2024-03-22 stsp static const struct got_error *
586 e70fd952 2024-03-22 stsp request_notification(struct gotd_session_notif *notif)
588 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
589 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
590 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content icontent;
591 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
592 e70fd952 2024-03-22 stsp size_t len;
595 e70fd952 2024-03-22 stsp fd = got_opentempfd();
596 e70fd952 2024-03-22 stsp if (fd == -1)
597 e70fd952 2024-03-22 stsp return got_error_from_errno("got_opentemp");
599 e70fd952 2024-03-22 stsp memset(&icontent, 0, sizeof(icontent));
601 e70fd952 2024-03-22 stsp icontent.action = notif->action;
602 e70fd952 2024-03-22 stsp memcpy(&icontent.old_id, ¬if->old_id, sizeof(notif->old_id));
603 e70fd952 2024-03-22 stsp memcpy(&icontent.new_id, ¬if->new_id, sizeof(notif->new_id));
604 e70fd952 2024-03-22 stsp icontent.refname_len = strlen(notif->refname);
606 e70fd952 2024-03-22 stsp len = sizeof(icontent) + icontent.refname_len;
607 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
608 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
609 e70fd952 2024-03-22 stsp if (wbuf == NULL) {
610 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_create NOTIFY");
613 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
614 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_add NOTIFY");
617 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
618 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_add NOTIFY");
622 e70fd952 2024-03-22 stsp notif->fd = dup(fd);
623 e70fd952 2024-03-22 stsp if (notif->fd == -1) {
624 e70fd952 2024-03-22 stsp err = got_error_from_errno("dup");
628 e70fd952 2024-03-22 stsp ibuf_fd_set(wbuf, fd);
631 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
632 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
634 e70fd952 2024-03-22 stsp if (err && fd != -1)
636 e70fd952 2024-03-22 stsp return err;
639 e70fd952 2024-03-22 stsp static const struct got_error *
640 e70fd952 2024-03-22 stsp update_ref(int *shut, struct gotd_session_client *client,
641 e70fd952 2024-03-22 stsp const char *repo_path, struct imsg *imsg)
643 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
644 e70fd952 2024-03-22 stsp struct got_repository *repo = gotd_session.repo;
645 e70fd952 2024-03-22 stsp struct got_reference *ref = NULL;
646 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update iref;
647 e70fd952 2024-03-22 stsp struct got_object_id old_id, new_id;
648 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
649 e70fd952 2024-03-22 stsp struct got_object_id *id = NULL;
650 e70fd952 2024-03-22 stsp char *refname = NULL;
651 e70fd952 2024-03-22 stsp size_t datalen;
652 e70fd952 2024-03-22 stsp int locked = 0;
653 e70fd952 2024-03-22 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
654 e70fd952 2024-03-22 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
656 e70fd952 2024-03-22 stsp log_debug("update-ref from uid %d", client->euid);
658 e70fd952 2024-03-22 stsp if (client->nref_updates <= 0)
659 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
661 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
662 e70fd952 2024-03-22 stsp if (datalen < sizeof(iref))
663 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
664 e70fd952 2024-03-22 stsp memcpy(&iref, imsg->data, sizeof(iref));
665 e70fd952 2024-03-22 stsp if (datalen != sizeof(iref) + iref.name_len)
666 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
667 e70fd952 2024-03-22 stsp refname = strndup(imsg->data + sizeof(iref), iref.name_len);
668 e70fd952 2024-03-22 stsp if (refname == NULL)
669 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
671 e70fd952 2024-03-22 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
673 e70fd952 2024-03-22 stsp memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
674 e70fd952 2024-03-22 stsp memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
675 e70fd952 2024-03-22 stsp err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
680 e70fd952 2024-03-22 stsp if (iref.ref_is_new) {
681 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 0);
683 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
685 e70fd952 2024-03-22 stsp err = got_ref_alloc(&ref, refname, &new_id);
688 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
691 e70fd952 2024-03-22 stsp err = queue_notification(NULL, &new_id, repo, ref);
695 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
698 e70fd952 2024-03-22 stsp got_object_id_hex(&new_id, hex1, sizeof(hex1));
699 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
700 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
701 e70fd952 2024-03-22 stsp "Addition %s: %s failed; %s: %s has been "
702 e70fd952 2024-03-22 stsp "created by someone else while transaction "
703 e70fd952 2024-03-22 stsp "was in progress",
704 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
705 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
708 e70fd952 2024-03-22 stsp } else if (iref.delete_ref) {
709 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
712 e70fd952 2024-03-22 stsp locked = 1;
714 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
718 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
719 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
720 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
721 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
722 e70fd952 2024-03-22 stsp "Deletion %s: %s failed; %s: %s has been "
723 e70fd952 2024-03-22 stsp "created by someone else while transaction "
724 e70fd952 2024-03-22 stsp "was in progress",
725 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
726 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
730 e70fd952 2024-03-22 stsp err = got_ref_delete(ref, repo);
733 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, NULL, repo, ref);
739 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
742 e70fd952 2024-03-22 stsp locked = 1;
744 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
748 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
749 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
750 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
751 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
752 e70fd952 2024-03-22 stsp "Update %s: %s failed; %s: %s has been "
753 e70fd952 2024-03-22 stsp "created by someone else while transaction "
754 e70fd952 2024-03-22 stsp "was in progress",
755 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
756 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
760 e70fd952 2024-03-22 stsp if (got_object_id_cmp(&new_id, &old_id) != 0) {
761 e70fd952 2024-03-22 stsp err = got_ref_change_ref(ref, &new_id);
764 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo);
767 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, &new_id, repo, ref);
777 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
778 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
779 e70fd952 2024-03-22 stsp "could not acquire exclusive file lock for %s",
782 e70fd952 2024-03-22 stsp send_ref_update_ng(client, &iref, refname, err->msg);
784 e70fd952 2024-03-22 stsp send_ref_update_ok(client, &iref, refname);
786 e70fd952 2024-03-22 stsp if (client->nref_updates > 0) {
787 e70fd952 2024-03-22 stsp client->nref_updates--;
788 e70fd952 2024-03-22 stsp if (client->nref_updates == 0) {
789 e70fd952 2024-03-22 stsp send_refs_updated(client);
790 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
791 e70fd952 2024-03-22 stsp if (notif) {
792 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_NOTIFY;
793 e70fd952 2024-03-22 stsp err = request_notification(notif);
795 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
796 e70fd952 2024-03-22 stsp "%s", err->msg);
797 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
800 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
804 e70fd952 2024-03-22 stsp if (locked) {
805 e70fd952 2024-03-22 stsp const struct got_error *unlock_err;
806 e70fd952 2024-03-22 stsp unlock_err = got_ref_unlock(ref);
807 e70fd952 2024-03-22 stsp if (unlock_err && err == NULL)
808 e70fd952 2024-03-22 stsp err = unlock_err;
811 e70fd952 2024-03-22 stsp got_ref_close(ref);
812 e70fd952 2024-03-22 stsp free(refname);
814 e70fd952 2024-03-22 stsp return err;
817 e70fd952 2024-03-22 stsp static const struct got_error *
818 e70fd952 2024-03-22 stsp recv_notification_content(struct imsg *imsg)
820 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content inotif;
821 e70fd952 2024-03-22 stsp size_t datalen;
823 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
824 e70fd952 2024-03-22 stsp if (datalen < sizeof(inotif))
825 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
826 e70fd952 2024-03-22 stsp memcpy(&inotif, imsg->data, sizeof(inotif));
828 e70fd952 2024-03-22 stsp return NULL;
831 e70fd952 2024-03-22 stsp static void
832 e70fd952 2024-03-22 stsp session_dispatch_repo_child(int fd, short event, void *arg)
834 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
835 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
836 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
838 e70fd952 2024-03-22 stsp int shut = 0;
839 e70fd952 2024-03-22 stsp struct imsg imsg;
841 e70fd952 2024-03-22 stsp if (event & EV_READ) {
842 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
843 e70fd952 2024-03-22 stsp fatal("imsg_read error");
844 e70fd952 2024-03-22 stsp if (n == 0) {
845 e70fd952 2024-03-22 stsp /* Connection closed. */
851 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
852 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
853 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
854 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
855 e70fd952 2024-03-22 stsp if (n == 0) {
856 e70fd952 2024-03-22 stsp /* Connection closed. */
863 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
864 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
865 e70fd952 2024-03-22 stsp int do_disconnect = 0;
866 e70fd952 2024-03-22 stsp int do_ref_updates = 0, do_ref_update = 0;
867 e70fd952 2024-03-22 stsp int do_packfile_install = 0, do_notify = 0;
869 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
870 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
871 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
874 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
875 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
876 e70fd952 2024-03-22 stsp do_disconnect = 1;
877 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
879 e70fd952 2024-03-22 stsp case GOTD_IMSG_PACKFILE_INSTALL:
880 e70fd952 2024-03-22 stsp err = recv_packfile_install(&imsg);
881 e70fd952 2024-03-22 stsp if (err == NULL)
882 e70fd952 2024-03-22 stsp do_packfile_install = 1;
884 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATES_START:
885 e70fd952 2024-03-22 stsp err = recv_ref_updates_start(&imsg);
886 e70fd952 2024-03-22 stsp if (err == NULL)
887 e70fd952 2024-03-22 stsp do_ref_updates = 1;
889 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
890 e70fd952 2024-03-22 stsp err = recv_ref_update(&imsg);
891 e70fd952 2024-03-22 stsp if (err == NULL)
892 e70fd952 2024-03-22 stsp do_ref_update = 1;
894 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFY:
895 e70fd952 2024-03-22 stsp err = recv_notification_content(&imsg);
896 e70fd952 2024-03-22 stsp if (err == NULL)
897 e70fd952 2024-03-22 stsp do_notify = 1;
900 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
904 e70fd952 2024-03-22 stsp if (do_disconnect) {
906 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
908 e70fd952 2024-03-22 stsp disconnect(client);
910 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
912 e70fd952 2024-03-22 stsp if (do_packfile_install)
913 e70fd952 2024-03-22 stsp err = install_pack(client,
914 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
915 e70fd952 2024-03-22 stsp else if (do_ref_updates)
916 e70fd952 2024-03-22 stsp err = begin_ref_updates(client, &imsg);
917 e70fd952 2024-03-22 stsp else if (do_ref_update)
918 e70fd952 2024-03-22 stsp err = update_ref(&shut, client,
919 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
920 e70fd952 2024-03-22 stsp else if (do_notify)
921 e70fd952 2024-03-22 stsp err = forward_notification(client, &imsg);
923 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
925 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
926 e70fd952 2024-03-22 stsp if (notif && do_notify) {
927 e70fd952 2024-03-22 stsp /* Request content for next notification. */
928 e70fd952 2024-03-22 stsp err = request_notification(notif);
930 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
931 e70fd952 2024-03-22 stsp "%s", err->msg);
936 e70fd952 2024-03-22 stsp imsg_free(&imsg);
939 e70fd952 2024-03-22 stsp if (!shut) {
940 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
942 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
943 e70fd952 2024-03-22 stsp event_del(&iev->ev);
944 e70fd952 2024-03-22 stsp event_loopexit(NULL);
948 e70fd952 2024-03-22 stsp static const struct got_error *
949 e70fd952 2024-03-22 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
951 e70fd952 2024-03-22 stsp struct gotd_imsg_capabilities icapas;
952 e70fd952 2024-03-22 stsp size_t datalen;
954 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
955 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapas))
956 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
957 e70fd952 2024-03-22 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
959 e70fd952 2024-03-22 stsp client->ncapa_alloc = icapas.ncapabilities;
960 e70fd952 2024-03-22 stsp client->capabilities = calloc(client->ncapa_alloc,
961 e70fd952 2024-03-22 stsp sizeof(*client->capabilities));
962 e70fd952 2024-03-22 stsp if (client->capabilities == NULL) {
963 e70fd952 2024-03-22 stsp client->ncapa_alloc = 0;
964 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
967 e70fd952 2024-03-22 stsp log_debug("expecting %zu capabilities from uid %d",
968 e70fd952 2024-03-22 stsp client->ncapa_alloc, client->euid);
969 e70fd952 2024-03-22 stsp return NULL;
972 e70fd952 2024-03-22 stsp static const struct got_error *
973 e70fd952 2024-03-22 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
975 e70fd952 2024-03-22 stsp struct gotd_imsg_capability icapa;
976 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
977 e70fd952 2024-03-22 stsp size_t datalen;
978 e70fd952 2024-03-22 stsp char *key, *value = NULL;
980 e70fd952 2024-03-22 stsp if (client->capabilities == NULL ||
981 e70fd952 2024-03-22 stsp client->ncapabilities >= client->ncapa_alloc) {
982 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
983 e70fd952 2024-03-22 stsp "unexpected capability received");
986 e70fd952 2024-03-22 stsp memset(&icapa, 0, sizeof(icapa));
988 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
989 e70fd952 2024-03-22 stsp if (datalen < sizeof(icapa))
990 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
991 e70fd952 2024-03-22 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
993 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
994 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
996 e70fd952 2024-03-22 stsp key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
997 e70fd952 2024-03-22 stsp if (key == NULL)
998 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
999 e70fd952 2024-03-22 stsp if (icapa.value_len > 0) {
1000 e70fd952 2024-03-22 stsp value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1001 e70fd952 2024-03-22 stsp icapa.value_len);
1002 e70fd952 2024-03-22 stsp if (value == NULL) {
1003 e70fd952 2024-03-22 stsp free(key);
1004 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1008 e70fd952 2024-03-22 stsp capa = &client->capabilities[client->ncapabilities++];
1009 e70fd952 2024-03-22 stsp capa->key = key;
1010 e70fd952 2024-03-22 stsp capa->value = value;
1012 e70fd952 2024-03-22 stsp if (value)
1013 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
1015 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s", client->euid, key);
1017 e70fd952 2024-03-22 stsp return NULL;
1020 e70fd952 2024-03-22 stsp static const struct got_error *
1021 e70fd952 2024-03-22 stsp forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1023 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1024 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update ireq;
1025 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref = NULL;
1026 e70fd952 2024-03-22 stsp size_t datalen;
1028 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1029 e70fd952 2024-03-22 stsp if (datalen < sizeof(ireq))
1030 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1031 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1032 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq) + ireq.name_len)
1033 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1035 e70fd952 2024-03-22 stsp iref = malloc(datalen);
1036 e70fd952 2024-03-22 stsp if (iref == NULL)
1037 e70fd952 2024-03-22 stsp return got_error_from_errno("malloc");
1038 e70fd952 2024-03-22 stsp memcpy(iref, imsg->data, datalen);
1040 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1041 e70fd952 2024-03-22 stsp GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1042 e70fd952 2024-03-22 stsp iref, datalen) == -1)
1043 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
1044 e70fd952 2024-03-22 stsp free(iref);
1045 e70fd952 2024-03-22 stsp return err;
1048 e70fd952 2024-03-22 stsp static int
1049 e70fd952 2024-03-22 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
1051 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
1054 e70fd952 2024-03-22 stsp if (client->ncapabilities == 0)
1057 e70fd952 2024-03-22 stsp for (i = 0; i < client->ncapabilities; i++) {
1058 e70fd952 2024-03-22 stsp capa = &client->capabilities[i];
1059 e70fd952 2024-03-22 stsp if (strcmp(capa->key, capastr) == 0)
1066 e70fd952 2024-03-22 stsp static const struct got_error *
1067 e70fd952 2024-03-22 stsp recv_packfile(struct gotd_session_client *client)
1069 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1070 e70fd952 2024-03-22 stsp struct gotd_imsg_recv_packfile ipack;
1071 e70fd952 2024-03-22 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1072 e70fd952 2024-03-22 stsp int packfd = -1, idxfd = -1;
1073 e70fd952 2024-03-22 stsp int pipe[2] = { -1, -1 };
1075 e70fd952 2024-03-22 stsp if (client->packfile_path) {
1076 e70fd952 2024-03-22 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1077 e70fd952 2024-03-22 stsp "uid %d already has a pack file", client->euid);
1080 e70fd952 2024-03-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1081 e70fd952 2024-03-22 stsp return got_error_from_errno("socketpair");
1083 e70fd952 2024-03-22 stsp /* Send pack pipe end 0 to repo child process. */
1084 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1085 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1086 e70fd952 2024-03-22 stsp NULL, 0) == -1) {
1087 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1088 e70fd952 2024-03-22 stsp pipe[0] = -1;
1089 e70fd952 2024-03-22 stsp goto done;
1091 e70fd952 2024-03-22 stsp pipe[0] = -1;
1093 e70fd952 2024-03-22 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1094 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev,
1095 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1096 e70fd952 2024-03-22 stsp NULL, 0) == -1)
1097 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1098 e70fd952 2024-03-22 stsp pipe[1] = -1;
1100 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1101 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1102 e70fd952 2024-03-22 stsp client->euid) == -1) {
1103 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1104 e70fd952 2024-03-22 stsp goto done;
1107 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1109 e70fd952 2024-03-22 stsp goto done;
1110 e70fd952 2024-03-22 stsp if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1111 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", pack_path);
1112 e70fd952 2024-03-22 stsp goto done;
1115 e70fd952 2024-03-22 stsp free(basepath);
1116 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1117 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1118 e70fd952 2024-03-22 stsp client->euid) == -1) {
1119 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1120 e70fd952 2024-03-22 stsp basepath = NULL;
1121 e70fd952 2024-03-22 stsp goto done;
1123 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1125 e70fd952 2024-03-22 stsp goto done;
1126 e70fd952 2024-03-22 stsp if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1127 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", idx_path);
1128 e70fd952 2024-03-22 stsp goto done;
1131 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1132 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1133 e70fd952 2024-03-22 stsp idxfd, NULL, 0) == -1) {
1134 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
1135 e70fd952 2024-03-22 stsp idxfd = -1;
1136 e70fd952 2024-03-22 stsp goto done;
1138 e70fd952 2024-03-22 stsp idxfd = -1;
1140 e70fd952 2024-03-22 stsp memset(&ipack, 0, sizeof(ipack));
1141 e70fd952 2024-03-22 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1142 e70fd952 2024-03-22 stsp ipack.report_status = 1;
1144 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1145 e70fd952 2024-03-22 stsp GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1146 e70fd952 2024-03-22 stsp &ipack, sizeof(ipack)) == -1) {
1147 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
1148 e70fd952 2024-03-22 stsp packfd = -1;
1149 e70fd952 2024-03-22 stsp goto done;
1151 e70fd952 2024-03-22 stsp packfd = -1;
1154 e70fd952 2024-03-22 stsp free(basepath);
1155 e70fd952 2024-03-22 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1156 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1157 e70fd952 2024-03-22 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1158 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1159 e70fd952 2024-03-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1160 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1161 e70fd952 2024-03-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1162 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1163 e70fd952 2024-03-22 stsp if (err) {
1164 e70fd952 2024-03-22 stsp free(pack_path);
1165 e70fd952 2024-03-22 stsp free(idx_path);
1167 e70fd952 2024-03-22 stsp client->packfile_path = pack_path;
1168 e70fd952 2024-03-22 stsp client->packidx_path = idx_path;
1170 e70fd952 2024-03-22 stsp return err;
1173 e70fd952 2024-03-22 stsp static void
1174 e70fd952 2024-03-22 stsp session_dispatch_client(int fd, short events, void *arg)
1176 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1177 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1178 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1179 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1180 e70fd952 2024-03-22 stsp struct imsg imsg;
1181 e70fd952 2024-03-22 stsp ssize_t n;
1183 e70fd952 2024-03-22 stsp if (events & EV_WRITE) {
1184 e70fd952 2024-03-22 stsp while (ibuf->w.queued) {
1185 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1186 e70fd952 2024-03-22 stsp if (n == -1 && errno == EPIPE) {
1188 e70fd952 2024-03-22 stsp * The client has closed its socket.
1189 e70fd952 2024-03-22 stsp * This can happen when Git clients are
1190 e70fd952 2024-03-22 stsp * done sending pack file data.
1192 e70fd952 2024-03-22 stsp msgbuf_clear(&ibuf->w);
1194 e70fd952 2024-03-22 stsp } else if (n == -1 && errno != EAGAIN) {
1195 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_flush");
1196 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1199 e70fd952 2024-03-22 stsp if (n == 0) {
1200 e70fd952 2024-03-22 stsp /* Connection closed. */
1201 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_EOF);
1202 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1207 e70fd952 2024-03-22 stsp if (client->flush_disconnect) {
1208 e70fd952 2024-03-22 stsp disconnect(client);
1213 e70fd952 2024-03-22 stsp if ((events & EV_READ) == 0)
1216 e70fd952 2024-03-22 stsp memset(&imsg, 0, sizeof(imsg));
1218 e70fd952 2024-03-22 stsp while (err == NULL) {
1219 e70fd952 2024-03-22 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1220 e70fd952 2024-03-22 stsp if (err) {
1221 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1222 e70fd952 2024-03-22 stsp err = NULL;
1223 e70fd952 2024-03-22 stsp else if (err->code == GOT_ERR_EOF &&
1224 e70fd952 2024-03-22 stsp gotd_session.state ==
1225 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1227 e70fd952 2024-03-22 stsp * The client has closed its socket before
1228 e70fd952 2024-03-22 stsp * sending its capability announcement.
1229 e70fd952 2024-03-22 stsp * This can happen when Git clients have
1230 e70fd952 2024-03-22 stsp * no ref-updates to send.
1232 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1238 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
1240 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1241 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITIES:
1242 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1243 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1244 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1245 e70fd952 2024-03-22 stsp "unexpected capabilities received");
1248 e70fd952 2024-03-22 stsp log_debug("receiving capabilities from uid %d",
1249 e70fd952 2024-03-22 stsp client->euid);
1250 e70fd952 2024-03-22 stsp err = recv_capabilities(client, &imsg);
1252 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITY:
1253 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1254 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1255 e70fd952 2024-03-22 stsp "unexpected capability received");
1258 e70fd952 2024-03-22 stsp err = recv_capability(client, &imsg);
1259 e70fd952 2024-03-22 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1261 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1262 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1263 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting ref-update-lines",
1264 e70fd952 2024-03-22 stsp client->euid);
1266 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
1267 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1268 e70fd952 2024-03-22 stsp gotd_session.state !=
1269 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1270 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1271 e70fd952 2024-03-22 stsp "unexpected ref-update-line received");
1274 e70fd952 2024-03-22 stsp log_debug("received ref-update-line from uid %d",
1275 e70fd952 2024-03-22 stsp client->euid);
1276 e70fd952 2024-03-22 stsp err = forward_ref_update(client, &imsg);
1279 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1280 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1282 e70fd952 2024-03-22 stsp case GOTD_IMSG_FLUSH:
1283 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1284 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1285 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1286 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1289 e70fd952 2024-03-22 stsp if (!client->accept_flush_pkt) {
1290 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1291 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1296 e70fd952 2024-03-22 stsp * Accept just one flush packet at a time.
1297 e70fd952 2024-03-22 stsp * Future client state transitions will set this flag
1298 e70fd952 2024-03-22 stsp * again if another flush packet is expected.
1300 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 0;
1302 e70fd952 2024-03-22 stsp log_debug("received flush-pkt from uid %d",
1303 e70fd952 2024-03-22 stsp client->euid);
1304 e70fd952 2024-03-22 stsp if (gotd_session.state ==
1305 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1306 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1307 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting packfile",
1308 e70fd952 2024-03-22 stsp client->euid);
1309 e70fd952 2024-03-22 stsp err = recv_packfile(client);
1311 e70fd952 2024-03-22 stsp /* should not happen, see above */
1312 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1313 e70fd952 2024-03-22 stsp "unexpected client state");
1318 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1319 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1323 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1326 e70fd952 2024-03-22 stsp if (err) {
1327 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF ||
1328 e70fd952 2024-03-22 stsp gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1329 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1331 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1332 e70fd952 2024-03-22 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1336 e70fd952 2024-03-22 stsp static const struct got_error *
1337 e70fd952 2024-03-22 stsp list_refs_request(void)
1339 e70fd952 2024-03-22 stsp static const struct got_error *err;
1340 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1341 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1344 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1345 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1347 e70fd952 2024-03-22 stsp fd = dup(client->fd);
1348 e70fd952 2024-03-22 stsp if (fd == -1)
1349 e70fd952 2024-03-22 stsp return got_error_from_errno("dup");
1351 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1352 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1353 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1354 e70fd952 2024-03-22 stsp close(fd);
1355 e70fd952 2024-03-22 stsp return err;
1358 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1359 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting capabilities", client->euid);
1360 e70fd952 2024-03-22 stsp return NULL;
1363 e70fd952 2024-03-22 stsp static const struct got_error *
1364 e70fd952 2024-03-22 stsp recv_connect(struct imsg *imsg)
1366 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1367 e70fd952 2024-03-22 stsp struct gotd_imsg_connect iconnect;
1368 e70fd952 2024-03-22 stsp size_t datalen;
1370 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1371 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1373 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1374 e70fd952 2024-03-22 stsp if (datalen < sizeof(iconnect))
1375 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1376 e70fd952 2024-03-22 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1377 e70fd952 2024-03-22 stsp if (iconnect.username_len == 0 ||
1378 e70fd952 2024-03-22 stsp datalen != sizeof(iconnect) + iconnect.username_len)
1379 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1381 e70fd952 2024-03-22 stsp client->euid = iconnect.euid;
1382 e70fd952 2024-03-22 stsp client->egid = iconnect.egid;
1383 e70fd952 2024-03-22 stsp client->fd = imsg_get_fd(imsg);
1384 e70fd952 2024-03-22 stsp if (client->fd == -1)
1385 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1387 e70fd952 2024-03-22 stsp client->username = strndup(imsg->data + sizeof(iconnect),
1388 e70fd952 2024-03-22 stsp iconnect.username_len);
1389 e70fd952 2024-03-22 stsp if (client->username == NULL)
1390 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1392 e70fd952 2024-03-22 stsp imsg_init(&client->iev.ibuf, client->fd);
1393 e70fd952 2024-03-22 stsp client->iev.handler = session_dispatch_client;
1394 e70fd952 2024-03-22 stsp client->iev.events = EV_READ;
1395 e70fd952 2024-03-22 stsp client->iev.handler_arg = NULL;
1396 e70fd952 2024-03-22 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1397 e70fd952 2024-03-22 stsp session_dispatch_client, &client->iev);
1398 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&client->iev);
1399 e70fd952 2024-03-22 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1401 e70fd952 2024-03-22 stsp return NULL;
1404 e70fd952 2024-03-22 stsp static void
1405 e70fd952 2024-03-22 stsp session_dispatch_notifier(int fd, short event, void *arg)
1407 e70fd952 2024-03-22 stsp const struct got_error *err;
1408 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1409 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1410 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1411 e70fd952 2024-03-22 stsp ssize_t n;
1412 e70fd952 2024-03-22 stsp int shut = 0;
1413 e70fd952 2024-03-22 stsp struct imsg imsg;
1414 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1416 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1417 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1418 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1419 e70fd952 2024-03-22 stsp if (n == 0) {
1420 e70fd952 2024-03-22 stsp /* Connection closed. */
1422 e70fd952 2024-03-22 stsp goto done;
1426 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1427 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1428 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1429 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1430 e70fd952 2024-03-22 stsp if (n == 0) {
1431 e70fd952 2024-03-22 stsp /* Connection closed. */
1433 e70fd952 2024-03-22 stsp goto done;
1437 e70fd952 2024-03-22 stsp for (;;) {
1438 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1439 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1440 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1443 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1444 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFICATION_SENT:
1445 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_NOTIFY) {
1446 e70fd952 2024-03-22 stsp log_warn("unexpected imsg %d", imsg.hdr.type);
1449 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
1450 e70fd952 2024-03-22 stsp if (notif == NULL) {
1451 e70fd952 2024-03-22 stsp disconnect(client);
1452 e70fd952 2024-03-22 stsp break; /* NOTREACHED */
1454 e70fd952 2024-03-22 stsp /* Request content for the next notification. */
1455 e70fd952 2024-03-22 stsp err = request_notification(notif);
1456 e70fd952 2024-03-22 stsp if (err) {
1457 e70fd952 2024-03-22 stsp log_warn("could not send notification: %s",
1458 e70fd952 2024-03-22 stsp err->msg);
1459 e70fd952 2024-03-22 stsp disconnect(client);
1463 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1467 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1470 e70fd952 2024-03-22 stsp if (!shut) {
1471 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1473 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1474 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1475 e70fd952 2024-03-22 stsp imsg_clear(&iev->ibuf);
1476 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, -1);
1480 e70fd952 2024-03-22 stsp static const struct got_error *
1481 e70fd952 2024-03-22 stsp recv_notifier(struct imsg *imsg)
1483 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1484 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1485 e70fd952 2024-03-22 stsp size_t datalen;
1488 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1489 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1491 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1492 e70fd952 2024-03-22 stsp if (client->fd == -1)
1493 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1495 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1496 e70fd952 2024-03-22 stsp if (datalen != 0)
1497 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1499 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1500 e70fd952 2024-03-22 stsp if (fd == -1)
1501 e70fd952 2024-03-22 stsp return NULL; /* notifications unused */
1503 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, fd);
1504 e70fd952 2024-03-22 stsp iev->handler = session_dispatch_notifier;
1505 e70fd952 2024-03-22 stsp iev->events = EV_READ;
1506 e70fd952 2024-03-22 stsp iev->handler_arg = NULL;
1507 e70fd952 2024-03-22 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1508 e70fd952 2024-03-22 stsp session_dispatch_notifier, iev);
1509 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1511 e70fd952 2024-03-22 stsp return NULL;
1514 e70fd952 2024-03-22 stsp static const struct got_error *
1515 e70fd952 2024-03-22 stsp recv_repo_child(struct imsg *imsg)
1517 e70fd952 2024-03-22 stsp struct gotd_imsg_connect_repo_child ichild;
1518 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1519 e70fd952 2024-03-22 stsp size_t datalen;
1522 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1523 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1525 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1526 e70fd952 2024-03-22 stsp if (client->fd == -1)
1527 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1529 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1530 e70fd952 2024-03-22 stsp if (datalen != sizeof(ichild))
1531 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1533 e70fd952 2024-03-22 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
1535 e70fd952 2024-03-22 stsp if (ichild.proc_id != PROC_REPO_WRITE)
1536 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1537 e70fd952 2024-03-22 stsp "bad child process type");
1539 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1540 e70fd952 2024-03-22 stsp if (fd == -1)
1541 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1543 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1544 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1545 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.events = EV_READ;
1546 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler_arg = NULL;
1547 e70fd952 2024-03-22 stsp event_set(&gotd_session.repo_child_iev.ev,
1548 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1549 e70fd952 2024-03-22 stsp session_dispatch_repo_child, &gotd_session.repo_child_iev);
1550 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&gotd_session.repo_child_iev);
1552 e70fd952 2024-03-22 stsp /* The "recvfd" pledge promise is no longer needed. */
1553 e70fd952 2024-03-22 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1554 e70fd952 2024-03-22 stsp fatal("pledge");
1556 e70fd952 2024-03-22 stsp return NULL;
1559 e70fd952 2024-03-22 stsp static void
1560 e70fd952 2024-03-22 stsp session_dispatch(int fd, short event, void *arg)
1562 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1563 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1564 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1565 e70fd952 2024-03-22 stsp ssize_t n;
1566 e70fd952 2024-03-22 stsp int shut = 0;
1567 e70fd952 2024-03-22 stsp struct imsg imsg;
1569 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1570 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1571 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1572 e70fd952 2024-03-22 stsp if (n == 0) {
1573 e70fd952 2024-03-22 stsp /* Connection closed. */
1575 e70fd952 2024-03-22 stsp goto done;
1579 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1580 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1581 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1582 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1583 e70fd952 2024-03-22 stsp if (n == 0) {
1584 e70fd952 2024-03-22 stsp /* Connection closed. */
1586 e70fd952 2024-03-22 stsp goto done;
1590 e70fd952 2024-03-22 stsp for (;;) {
1591 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1592 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
1593 e70fd952 2024-03-22 stsp int do_disconnect = 0, do_list_refs = 0;
1595 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1596 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1597 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1600 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1601 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
1602 e70fd952 2024-03-22 stsp do_disconnect = 1;
1603 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1605 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT:
1606 e70fd952 2024-03-22 stsp err = recv_connect(&imsg);
1608 e70fd952 2024-03-22 stsp case GOTD_IMSG_DISCONNECT:
1609 e70fd952 2024-03-22 stsp do_disconnect = 1;
1611 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_NOTIFIER:
1612 e70fd952 2024-03-22 stsp err = recv_notifier(&imsg);
1614 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1615 e70fd952 2024-03-22 stsp err = recv_repo_child(&imsg);
1618 e70fd952 2024-03-22 stsp do_list_refs = 1;
1621 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1624 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1626 e70fd952 2024-03-22 stsp if (do_disconnect) {
1628 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1630 e70fd952 2024-03-22 stsp disconnect(client);
1631 e70fd952 2024-03-22 stsp } else if (do_list_refs)
1632 e70fd952 2024-03-22 stsp err = list_refs_request();
1635 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1638 e70fd952 2024-03-22 stsp if (!shut) {
1639 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1641 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1642 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1643 e70fd952 2024-03-22 stsp event_loopexit(NULL);
1648 e70fd952 2024-03-22 stsp session_write_main(const char *title, const char *repo_path,
1649 e70fd952 2024-03-22 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1650 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg)
1652 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1653 e70fd952 2024-03-22 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1655 e70fd952 2024-03-22 stsp STAILQ_INIT(¬ifications);
1657 e70fd952 2024-03-22 stsp gotd_session.title = title;
1658 e70fd952 2024-03-22 stsp gotd_session.pid = getpid();
1659 e70fd952 2024-03-22 stsp gotd_session.pack_fds = pack_fds;
1660 e70fd952 2024-03-22 stsp gotd_session.temp_fds = temp_fds;
1661 e70fd952 2024-03-22 stsp memcpy(&gotd_session.request_timeout, request_timeout,
1662 e70fd952 2024-03-22 stsp sizeof(gotd_session.request_timeout));
1663 e70fd952 2024-03-22 stsp gotd_session.repo_cfg = repo_cfg;
1665 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1667 e70fd952 2024-03-22 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1669 e70fd952 2024-03-22 stsp goto done;
1670 e70fd952 2024-03-22 stsp if (!got_repo_is_bare(gotd_session.repo)) {
1671 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1672 e70fd952 2024-03-22 stsp "bare git repository required");
1673 e70fd952 2024-03-22 stsp goto done;
1676 e70fd952 2024-03-22 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1678 e70fd952 2024-03-22 stsp signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1679 e70fd952 2024-03-22 stsp signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1680 e70fd952 2024-03-22 stsp signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1681 e70fd952 2024-03-22 stsp signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1682 e70fd952 2024-03-22 stsp signal(SIGPIPE, SIG_IGN);
1684 e70fd952 2024-03-22 stsp signal_add(&evsigint, NULL);
1685 e70fd952 2024-03-22 stsp signal_add(&evsigterm, NULL);
1686 e70fd952 2024-03-22 stsp signal_add(&evsighup, NULL);
1687 e70fd952 2024-03-22 stsp signal_add(&evsigusr1, NULL);
1689 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1691 e70fd952 2024-03-22 stsp gotd_session_client.fd = -1;
1692 e70fd952 2024-03-22 stsp gotd_session_client.nref_updates = -1;
1693 e70fd952 2024-03-22 stsp gotd_session_client.delta_cache_fd = -1;
1694 e70fd952 2024-03-22 stsp gotd_session_client.accept_flush_pkt = 1;
1696 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1697 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler = session_dispatch;
1698 e70fd952 2024-03-22 stsp gotd_session.parent_iev.events = EV_READ;
1699 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler_arg = NULL;
1700 e70fd952 2024-03-22 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1701 e70fd952 2024-03-22 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
1702 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1703 e70fd952 2024-03-22 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1704 e70fd952 2024-03-22 stsp -1, NULL, 0) == -1) {
1705 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1706 e70fd952 2024-03-22 stsp goto done;
1709 e70fd952 2024-03-22 stsp event_dispatch();
1712 e70fd952 2024-03-22 stsp log_warnx("%s: %s", title, err->msg);
1713 e70fd952 2024-03-22 stsp session_write_shutdown();
1716 e70fd952 2024-03-22 stsp static void
1717 e70fd952 2024-03-22 stsp session_write_shutdown(void)
1719 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1721 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_session.title);
1723 e70fd952 2024-03-22 stsp while (!STAILQ_EMPTY(¬ifications)) {
1724 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
1725 e70fd952 2024-03-22 stsp STAILQ_REMOVE_HEAD(¬ifications, entry);
1726 e70fd952 2024-03-22 stsp if (notif->fd != -1)
1727 e70fd952 2024-03-22 stsp close(notif->fd);
1728 e70fd952 2024-03-22 stsp free(notif->refname);
1729 e70fd952 2024-03-22 stsp free(notif);
1732 e70fd952 2024-03-22 stsp if (gotd_session.repo)
1733 e70fd952 2024-03-22 stsp got_repo_close(gotd_session.repo);
1734 e70fd952 2024-03-22 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
1735 e70fd952 2024-03-22 stsp got_repo_temp_fds_close(gotd_session.temp_fds);
1736 e70fd952 2024-03-22 stsp free(gotd_session_client.username);