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_read.h"
56 e70fd952 2024-03-22 stsp enum gotd_session_read_state {
57 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_LIST_REFS,
58 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES,
59 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_WANT,
60 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE,
61 e70fd952 2024-03-22 stsp GOTD_STATE_DONE,
64 e70fd952 2024-03-22 stsp static struct gotd_session_read {
66 e70fd952 2024-03-22 stsp const char *title;
67 e70fd952 2024-03-22 stsp struct got_repository *repo;
68 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg;
69 e70fd952 2024-03-22 stsp int *pack_fds;
70 e70fd952 2024-03-22 stsp int *temp_fds;
71 e70fd952 2024-03-22 stsp struct gotd_imsgev parent_iev;
72 e70fd952 2024-03-22 stsp struct gotd_imsgev notifier_iev;
73 e70fd952 2024-03-22 stsp struct timeval request_timeout;
74 e70fd952 2024-03-22 stsp enum gotd_session_read_state state;
75 e70fd952 2024-03-22 stsp struct gotd_imsgev repo_child_iev;
76 e70fd952 2024-03-22 stsp } gotd_session;
78 e70fd952 2024-03-22 stsp static struct gotd_session_client {
79 e70fd952 2024-03-22 stsp struct gotd_client_capability *capabilities;
80 e70fd952 2024-03-22 stsp size_t ncapa_alloc;
81 e70fd952 2024-03-22 stsp size_t ncapabilities;
82 e70fd952 2024-03-22 stsp uint32_t id;
84 e70fd952 2024-03-22 stsp int delta_cache_fd;
85 e70fd952 2024-03-22 stsp struct gotd_imsgev iev;
86 e70fd952 2024-03-22 stsp struct event tmo;
89 e70fd952 2024-03-22 stsp char *username;
90 e70fd952 2024-03-22 stsp char *packfile_path;
91 e70fd952 2024-03-22 stsp char *packidx_path;
92 e70fd952 2024-03-22 stsp int nref_updates;
93 e70fd952 2024-03-22 stsp int accept_flush_pkt;
94 e70fd952 2024-03-22 stsp int flush_disconnect;
95 e70fd952 2024-03-22 stsp } gotd_session_client;
97 e70fd952 2024-03-22 stsp static void session_read_shutdown(void);
100 e70fd952 2024-03-22 stsp disconnect(struct gotd_session_client *client)
102 e70fd952 2024-03-22 stsp log_debug("uid %d: disconnecting", client->euid);
104 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
105 e70fd952 2024-03-22 stsp GOTD_IMSG_DISCONNECT, PROC_SESSION_READ, -1, NULL, 0) == -1)
106 e70fd952 2024-03-22 stsp log_warn("imsg compose DISCONNECT");
108 e70fd952 2024-03-22 stsp imsg_clear(&gotd_session.repo_child_iev.ibuf);
109 e70fd952 2024-03-22 stsp event_del(&gotd_session.repo_child_iev.ev);
110 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
111 e70fd952 2024-03-22 stsp close(client->fd);
112 e70fd952 2024-03-22 stsp if (client->delta_cache_fd != -1)
113 e70fd952 2024-03-22 stsp close(client->delta_cache_fd);
114 e70fd952 2024-03-22 stsp if (client->packfile_path) {
115 e70fd952 2024-03-22 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
116 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packfile_path);
117 e70fd952 2024-03-22 stsp free(client->packfile_path);
119 e70fd952 2024-03-22 stsp if (client->packidx_path) {
120 e70fd952 2024-03-22 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
121 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packidx_path);
122 e70fd952 2024-03-22 stsp free(client->packidx_path);
124 e70fd952 2024-03-22 stsp free(client->capabilities);
126 e70fd952 2024-03-22 stsp session_read_shutdown();
129 e70fd952 2024-03-22 stsp static void
130 e70fd952 2024-03-22 stsp disconnect_on_error(struct gotd_session_client *client,
131 e70fd952 2024-03-22 stsp const struct got_error *err)
133 e70fd952 2024-03-22 stsp struct imsgbuf ibuf;
135 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF) {
136 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
137 e70fd952 2024-03-22 stsp imsg_init(&ibuf, client->fd);
138 e70fd952 2024-03-22 stsp gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_READ, err);
139 e70fd952 2024-03-22 stsp imsg_clear(&ibuf);
142 e70fd952 2024-03-22 stsp disconnect(client);
145 e70fd952 2024-03-22 stsp static void
146 e70fd952 2024-03-22 stsp gotd_request_timeout(int fd, short events, void *arg)
148 e70fd952 2024-03-22 stsp struct gotd_session_client *client = arg;
150 7268d461 2024-05-01 stsp log_warnx("disconnecting uid %d due to timeout", client->euid);
151 e70fd952 2024-03-22 stsp disconnect(client);
154 e70fd952 2024-03-22 stsp static void
155 e70fd952 2024-03-22 stsp session_read_sighdlr(int sig, short event, void *arg)
158 e70fd952 2024-03-22 stsp * Normal signal handler rules don't apply because libevent
159 e70fd952 2024-03-22 stsp * decouples for us.
162 e70fd952 2024-03-22 stsp switch (sig) {
163 e70fd952 2024-03-22 stsp case SIGHUP:
164 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGHUP", __func__);
166 e70fd952 2024-03-22 stsp case SIGUSR1:
167 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGUSR1", __func__);
169 e70fd952 2024-03-22 stsp case SIGTERM:
170 e70fd952 2024-03-22 stsp case SIGINT:
171 e70fd952 2024-03-22 stsp session_read_shutdown();
172 e70fd952 2024-03-22 stsp /* NOTREACHED */
175 e70fd952 2024-03-22 stsp fatalx("unexpected signal");
179 e70fd952 2024-03-22 stsp static const struct got_error *
180 e70fd952 2024-03-22 stsp recv_packfile_done(struct imsg *imsg)
182 e70fd952 2024-03-22 stsp size_t datalen;
184 e70fd952 2024-03-22 stsp log_debug("packfile-done received");
186 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
187 e70fd952 2024-03-22 stsp if (datalen != 0)
188 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
190 e70fd952 2024-03-22 stsp return NULL;
193 e70fd952 2024-03-22 stsp static void
194 e70fd952 2024-03-22 stsp session_dispatch_repo_child(int fd, short event, void *arg)
196 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
197 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
198 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
200 e70fd952 2024-03-22 stsp int shut = 0;
201 e70fd952 2024-03-22 stsp struct imsg imsg;
203 e70fd952 2024-03-22 stsp if (event & EV_READ) {
204 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
205 e70fd952 2024-03-22 stsp fatal("imsg_read error");
206 e70fd952 2024-03-22 stsp if (n == 0) {
207 e70fd952 2024-03-22 stsp /* Connection closed. */
213 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
214 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
215 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
216 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
217 e70fd952 2024-03-22 stsp if (n == 0) {
218 e70fd952 2024-03-22 stsp /* Connection closed. */
225 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
226 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
227 e70fd952 2024-03-22 stsp int do_disconnect = 0;
229 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
230 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
231 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
234 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
235 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
236 e70fd952 2024-03-22 stsp do_disconnect = 1;
237 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
239 e70fd952 2024-03-22 stsp case GOTD_IMSG_PACKFILE_DONE:
240 e70fd952 2024-03-22 stsp do_disconnect = 1;
241 e70fd952 2024-03-22 stsp err = recv_packfile_done(&imsg);
244 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
248 e70fd952 2024-03-22 stsp if (do_disconnect) {
250 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
252 e70fd952 2024-03-22 stsp disconnect(client);
255 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
257 e70fd952 2024-03-22 stsp imsg_free(&imsg);
260 e70fd952 2024-03-22 stsp if (!shut) {
261 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
263 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
264 e70fd952 2024-03-22 stsp event_del(&iev->ev);
265 e70fd952 2024-03-22 stsp event_loopexit(NULL);
269 e70fd952 2024-03-22 stsp static const struct got_error *
270 e70fd952 2024-03-22 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
272 e70fd952 2024-03-22 stsp struct gotd_imsg_capabilities icapas;
273 e70fd952 2024-03-22 stsp size_t datalen;
275 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapas))
277 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
278 e70fd952 2024-03-22 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
280 e70fd952 2024-03-22 stsp client->ncapa_alloc = icapas.ncapabilities;
281 e70fd952 2024-03-22 stsp client->capabilities = calloc(client->ncapa_alloc,
282 e70fd952 2024-03-22 stsp sizeof(*client->capabilities));
283 e70fd952 2024-03-22 stsp if (client->capabilities == NULL) {
284 e70fd952 2024-03-22 stsp client->ncapa_alloc = 0;
285 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
288 e70fd952 2024-03-22 stsp log_debug("expecting %zu capabilities from uid %d",
289 e70fd952 2024-03-22 stsp client->ncapa_alloc, client->euid);
290 e70fd952 2024-03-22 stsp return NULL;
293 e70fd952 2024-03-22 stsp static const struct got_error *
294 e70fd952 2024-03-22 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
296 e70fd952 2024-03-22 stsp struct gotd_imsg_capability icapa;
297 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
298 e70fd952 2024-03-22 stsp size_t datalen;
299 e70fd952 2024-03-22 stsp char *key, *value = NULL;
301 e70fd952 2024-03-22 stsp if (client->capabilities == NULL ||
302 e70fd952 2024-03-22 stsp client->ncapabilities >= client->ncapa_alloc) {
303 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
304 e70fd952 2024-03-22 stsp "unexpected capability received");
307 e70fd952 2024-03-22 stsp memset(&icapa, 0, sizeof(icapa));
309 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
310 e70fd952 2024-03-22 stsp if (datalen < sizeof(icapa))
311 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
312 e70fd952 2024-03-22 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
314 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
315 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
317 e70fd952 2024-03-22 stsp key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
318 e70fd952 2024-03-22 stsp if (key == NULL)
319 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
320 e70fd952 2024-03-22 stsp if (icapa.value_len > 0) {
321 e70fd952 2024-03-22 stsp value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
322 e70fd952 2024-03-22 stsp icapa.value_len);
323 e70fd952 2024-03-22 stsp if (value == NULL) {
325 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
329 e70fd952 2024-03-22 stsp capa = &client->capabilities[client->ncapabilities++];
330 e70fd952 2024-03-22 stsp capa->key = key;
331 e70fd952 2024-03-22 stsp capa->value = value;
334 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
336 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s", client->euid, key);
338 e70fd952 2024-03-22 stsp return NULL;
341 e70fd952 2024-03-22 stsp static const struct got_error *
342 e70fd952 2024-03-22 stsp forward_want(struct gotd_session_client *client, struct imsg *imsg)
344 e70fd952 2024-03-22 stsp struct gotd_imsg_want ireq;
345 e70fd952 2024-03-22 stsp struct gotd_imsg_want iwant;
346 e70fd952 2024-03-22 stsp size_t datalen;
348 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
349 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq))
350 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
352 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, datalen);
354 e70fd952 2024-03-22 stsp memset(&iwant, 0, sizeof(iwant));
355 e70fd952 2024-03-22 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
357 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
358 e70fd952 2024-03-22 stsp GOTD_IMSG_WANT, PROC_SESSION_READ, -1,
359 e70fd952 2024-03-22 stsp &iwant, sizeof(iwant)) == -1)
360 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg compose WANT");
362 e70fd952 2024-03-22 stsp return NULL;
365 e70fd952 2024-03-22 stsp static const struct got_error *
366 e70fd952 2024-03-22 stsp forward_have(struct gotd_session_client *client, struct imsg *imsg)
368 e70fd952 2024-03-22 stsp struct gotd_imsg_have ireq;
369 e70fd952 2024-03-22 stsp struct gotd_imsg_have ihave;
370 e70fd952 2024-03-22 stsp size_t datalen;
372 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
373 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq))
374 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
376 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, datalen);
378 e70fd952 2024-03-22 stsp memset(&ihave, 0, sizeof(ihave));
379 e70fd952 2024-03-22 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
381 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
382 e70fd952 2024-03-22 stsp GOTD_IMSG_HAVE, PROC_SESSION_READ, -1,
383 e70fd952 2024-03-22 stsp &ihave, sizeof(ihave)) == -1)
384 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg compose HAVE");
386 e70fd952 2024-03-22 stsp return NULL;
390 e70fd952 2024-03-22 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
392 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
395 e70fd952 2024-03-22 stsp if (client->ncapabilities == 0)
398 e70fd952 2024-03-22 stsp for (i = 0; i < client->ncapabilities; i++) {
399 e70fd952 2024-03-22 stsp capa = &client->capabilities[i];
400 e70fd952 2024-03-22 stsp if (strcmp(capa->key, capastr) == 0)
407 e70fd952 2024-03-22 stsp static const struct got_error *
408 e70fd952 2024-03-22 stsp send_packfile(struct gotd_session_client *client)
410 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
411 e70fd952 2024-03-22 stsp struct gotd_imsg_send_packfile ipack;
412 e70fd952 2024-03-22 stsp int pipe[2];
414 e70fd952 2024-03-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
415 e70fd952 2024-03-22 stsp return got_error_from_errno("socketpair");
417 e70fd952 2024-03-22 stsp memset(&ipack, 0, sizeof(ipack));
419 e70fd952 2024-03-22 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
420 e70fd952 2024-03-22 stsp ipack.report_progress = 1;
422 e70fd952 2024-03-22 stsp client->delta_cache_fd = got_opentempfd();
423 e70fd952 2024-03-22 stsp if (client->delta_cache_fd == -1)
424 e70fd952 2024-03-22 stsp return got_error_from_errno("got_opentempfd");
426 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
427 e70fd952 2024-03-22 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
428 e70fd952 2024-03-22 stsp &ipack, sizeof(ipack)) == -1) {
429 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
430 e70fd952 2024-03-22 stsp close(pipe[0]);
431 e70fd952 2024-03-22 stsp close(pipe[1]);
432 e70fd952 2024-03-22 stsp return err;
435 e70fd952 2024-03-22 stsp /* Send pack pipe end 0 to repo child process. */
436 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
437 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0], NULL, 0) == -1) {
438 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
439 e70fd952 2024-03-22 stsp close(pipe[1]);
440 e70fd952 2024-03-22 stsp return err;
443 e70fd952 2024-03-22 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
444 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev,
445 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
446 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
448 e70fd952 2024-03-22 stsp return err;
451 e70fd952 2024-03-22 stsp static void
452 e70fd952 2024-03-22 stsp session_dispatch_client(int fd, short events, void *arg)
454 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
455 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
456 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
457 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
458 e70fd952 2024-03-22 stsp struct imsg imsg;
461 e70fd952 2024-03-22 stsp if (events & EV_WRITE) {
462 e70fd952 2024-03-22 stsp while (ibuf->w.queued) {
463 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
464 bcb30926 2024-04-30 stsp if (n == -1 && errno != EAGAIN) {
465 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_flush");
466 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
469 e70fd952 2024-03-22 stsp if (n == 0) {
470 e70fd952 2024-03-22 stsp /* Connection closed. */
471 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_EOF);
472 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
477 e70fd952 2024-03-22 stsp if (client->flush_disconnect) {
478 e70fd952 2024-03-22 stsp disconnect(client);
483 e70fd952 2024-03-22 stsp if ((events & EV_READ) == 0)
486 e70fd952 2024-03-22 stsp memset(&imsg, 0, sizeof(imsg));
488 e70fd952 2024-03-22 stsp while (err == NULL) {
489 e70fd952 2024-03-22 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
491 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
492 e70fd952 2024-03-22 stsp err = NULL;
493 e70fd952 2024-03-22 stsp else if (err->code == GOT_ERR_EOF &&
494 e70fd952 2024-03-22 stsp gotd_session.state ==
495 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
497 e70fd952 2024-03-22 stsp * The client has closed its socket before
498 e70fd952 2024-03-22 stsp * sending its capability announcement.
499 e70fd952 2024-03-22 stsp * This can happen when Git clients have
500 e70fd952 2024-03-22 stsp * no ref-updates to send.
502 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
508 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
510 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
511 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITIES:
512 e70fd952 2024-03-22 stsp if (gotd_session.state !=
513 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
514 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
515 e70fd952 2024-03-22 stsp "unexpected capabilities received");
518 e70fd952 2024-03-22 stsp log_debug("receiving capabilities from uid %d",
519 e70fd952 2024-03-22 stsp client->euid);
520 e70fd952 2024-03-22 stsp err = recv_capabilities(client, &imsg);
522 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITY:
523 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
524 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
525 e70fd952 2024-03-22 stsp "unexpected capability received");
528 e70fd952 2024-03-22 stsp err = recv_capability(client, &imsg);
529 e70fd952 2024-03-22 stsp if (err || client->ncapabilities < client->ncapa_alloc)
531 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_WANT;
532 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
533 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting want-lines", client->euid);
535 e70fd952 2024-03-22 stsp case GOTD_IMSG_WANT:
536 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_WANT) {
537 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
538 e70fd952 2024-03-22 stsp "unexpected want-line received");
541 e70fd952 2024-03-22 stsp log_debug("received want-line from uid %d",
542 e70fd952 2024-03-22 stsp client->euid);
543 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
544 e70fd952 2024-03-22 stsp err = forward_want(client, &imsg);
546 e70fd952 2024-03-22 stsp case GOTD_IMSG_HAVE:
547 c811fd62 2024-05-05 stsp if (gotd_session.state !=
548 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
549 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
550 e70fd952 2024-03-22 stsp "unexpected have-line received");
553 e70fd952 2024-03-22 stsp log_debug("received have-line from uid %d",
554 e70fd952 2024-03-22 stsp client->euid);
555 e70fd952 2024-03-22 stsp err = forward_have(client, &imsg);
558 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
560 e70fd952 2024-03-22 stsp case GOTD_IMSG_FLUSH:
561 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_WANT &&
562 c811fd62 2024-05-05 stsp gotd_session.state !=
563 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
564 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
565 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
568 e70fd952 2024-03-22 stsp if (!client->accept_flush_pkt) {
569 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
570 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
575 e70fd952 2024-03-22 stsp * Accept just one flush packet at a time.
576 e70fd952 2024-03-22 stsp * Future client state transitions will set this flag
577 e70fd952 2024-03-22 stsp * again if another flush packet is expected.
579 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 0;
581 e70fd952 2024-03-22 stsp log_debug("received flush-pkt from uid %d",
582 e70fd952 2024-03-22 stsp client->euid);
583 e70fd952 2024-03-22 stsp if (gotd_session.state == GOTD_STATE_EXPECT_WANT) {
584 c811fd62 2024-05-05 stsp gotd_session.state =
585 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE;
586 c811fd62 2024-05-05 stsp log_debug("uid %d: expecting have-lines "
587 c811fd62 2024-05-05 stsp "or 'done'", client->euid);
588 c811fd62 2024-05-05 stsp } else if (gotd_session.state ==
589 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
590 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
591 c811fd62 2024-05-05 stsp log_debug("uid %d: expecting more have-lines "
592 c811fd62 2024-05-05 stsp "or 'done'", client->euid);
593 c811fd62 2024-05-05 stsp } else if (gotd_session.state !=
594 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
595 e70fd952 2024-03-22 stsp /* should not happen, see above */
596 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
597 e70fd952 2024-03-22 stsp "unexpected client state");
601 e70fd952 2024-03-22 stsp case GOTD_IMSG_DONE:
602 c811fd62 2024-05-05 stsp if (gotd_session.state !=
603 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
604 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
605 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
608 e70fd952 2024-03-22 stsp log_debug("received 'done' from uid %d", client->euid);
609 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_DONE;
610 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
611 e70fd952 2024-03-22 stsp err = send_packfile(client);
614 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
615 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
619 e70fd952 2024-03-22 stsp imsg_free(&imsg);
623 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF)
624 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
626 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
627 e70fd952 2024-03-22 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
631 e70fd952 2024-03-22 stsp static const struct got_error *
632 e70fd952 2024-03-22 stsp list_refs_request(void)
634 e70fd952 2024-03-22 stsp static const struct got_error *err;
635 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
636 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
639 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
640 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
642 e70fd952 2024-03-22 stsp fd = dup(client->fd);
643 e70fd952 2024-03-22 stsp if (fd == -1)
644 e70fd952 2024-03-22 stsp return got_error_from_errno("dup");
646 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
647 e70fd952 2024-03-22 stsp PROC_SESSION_READ, fd, NULL, 0) == -1) {
648 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
650 e70fd952 2024-03-22 stsp return err;
653 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
654 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting capabilities", client->euid);
655 e70fd952 2024-03-22 stsp return NULL;
658 e70fd952 2024-03-22 stsp static const struct got_error *
659 e70fd952 2024-03-22 stsp recv_connect(struct imsg *imsg)
661 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
662 e70fd952 2024-03-22 stsp struct gotd_imsg_connect iconnect;
663 e70fd952 2024-03-22 stsp size_t datalen;
665 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
666 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
668 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
669 e70fd952 2024-03-22 stsp if (datalen < sizeof(iconnect))
670 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
671 e70fd952 2024-03-22 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
672 e70fd952 2024-03-22 stsp if (iconnect.username_len == 0 ||
673 e70fd952 2024-03-22 stsp datalen != sizeof(iconnect) + iconnect.username_len)
674 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
676 e70fd952 2024-03-22 stsp client->euid = iconnect.euid;
677 e70fd952 2024-03-22 stsp client->egid = iconnect.egid;
678 e70fd952 2024-03-22 stsp client->fd = imsg_get_fd(imsg);
679 e70fd952 2024-03-22 stsp if (client->fd == -1)
680 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
682 e70fd952 2024-03-22 stsp client->username = strndup(imsg->data + sizeof(iconnect),
683 e70fd952 2024-03-22 stsp iconnect.username_len);
684 e70fd952 2024-03-22 stsp if (client->username == NULL)
685 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
687 e70fd952 2024-03-22 stsp imsg_init(&client->iev.ibuf, client->fd);
688 e70fd952 2024-03-22 stsp client->iev.handler = session_dispatch_client;
689 e70fd952 2024-03-22 stsp client->iev.events = EV_READ;
690 e70fd952 2024-03-22 stsp client->iev.handler_arg = NULL;
691 e70fd952 2024-03-22 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
692 e70fd952 2024-03-22 stsp session_dispatch_client, &client->iev);
693 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&client->iev);
694 e70fd952 2024-03-22 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
695 caa6cf11 2024-04-30 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
697 e70fd952 2024-03-22 stsp return NULL;
700 e70fd952 2024-03-22 stsp static const struct got_error *
701 e70fd952 2024-03-22 stsp recv_repo_child(struct imsg *imsg)
703 e70fd952 2024-03-22 stsp struct gotd_imsg_connect_repo_child ichild;
704 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
705 e70fd952 2024-03-22 stsp size_t datalen;
708 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
709 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
711 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
712 e70fd952 2024-03-22 stsp if (client->fd == -1)
713 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
715 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
716 e70fd952 2024-03-22 stsp if (datalen != sizeof(ichild))
717 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
719 e70fd952 2024-03-22 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
721 e70fd952 2024-03-22 stsp if (ichild.proc_id != PROC_REPO_READ)
722 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
723 e70fd952 2024-03-22 stsp "bad child process type");
725 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
726 e70fd952 2024-03-22 stsp if (fd == -1)
727 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
729 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
730 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
731 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.events = EV_READ;
732 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler_arg = NULL;
733 e70fd952 2024-03-22 stsp event_set(&gotd_session.repo_child_iev.ev,
734 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.ibuf.fd, EV_READ,
735 e70fd952 2024-03-22 stsp session_dispatch_repo_child, &gotd_session.repo_child_iev);
736 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&gotd_session.repo_child_iev);
738 e70fd952 2024-03-22 stsp /* The "recvfd" pledge promise is no longer needed. */
739 e70fd952 2024-03-22 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
740 e70fd952 2024-03-22 stsp fatal("pledge");
742 e70fd952 2024-03-22 stsp return NULL;
745 e70fd952 2024-03-22 stsp static void
746 e70fd952 2024-03-22 stsp session_dispatch(int fd, short event, void *arg)
748 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
749 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
750 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
752 e70fd952 2024-03-22 stsp int shut = 0;
753 e70fd952 2024-03-22 stsp struct imsg imsg;
755 e70fd952 2024-03-22 stsp if (event & EV_READ) {
756 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
757 e70fd952 2024-03-22 stsp fatal("imsg_read error");
758 e70fd952 2024-03-22 stsp if (n == 0) {
759 e70fd952 2024-03-22 stsp /* Connection closed. */
765 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
766 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
767 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
768 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
769 e70fd952 2024-03-22 stsp if (n == 0) {
770 e70fd952 2024-03-22 stsp /* Connection closed. */
777 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
778 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
779 e70fd952 2024-03-22 stsp int do_disconnect = 0, do_list_refs = 0;
781 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
782 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
783 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
786 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
787 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
788 e70fd952 2024-03-22 stsp do_disconnect = 1;
789 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
791 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT:
792 e70fd952 2024-03-22 stsp err = recv_connect(&imsg);
794 e70fd952 2024-03-22 stsp case GOTD_IMSG_DISCONNECT:
795 e70fd952 2024-03-22 stsp do_disconnect = 1;
797 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
798 e70fd952 2024-03-22 stsp err = recv_repo_child(&imsg);
801 e70fd952 2024-03-22 stsp do_list_refs = 1;
804 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
807 e70fd952 2024-03-22 stsp imsg_free(&imsg);
809 e70fd952 2024-03-22 stsp if (do_disconnect) {
811 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
813 e70fd952 2024-03-22 stsp disconnect(client);
814 e70fd952 2024-03-22 stsp } else if (do_list_refs)
815 e70fd952 2024-03-22 stsp err = list_refs_request();
818 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
821 e70fd952 2024-03-22 stsp if (!shut) {
822 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
824 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
825 e70fd952 2024-03-22 stsp event_del(&iev->ev);
826 e70fd952 2024-03-22 stsp event_loopexit(NULL);
831 e70fd952 2024-03-22 stsp session_read_main(const char *title, const char *repo_path,
832 e70fd952 2024-03-22 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
833 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg)
835 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
836 e70fd952 2024-03-22 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
838 e70fd952 2024-03-22 stsp gotd_session.title = title;
839 e70fd952 2024-03-22 stsp gotd_session.pid = getpid();
840 e70fd952 2024-03-22 stsp gotd_session.pack_fds = pack_fds;
841 e70fd952 2024-03-22 stsp gotd_session.temp_fds = temp_fds;
842 e70fd952 2024-03-22 stsp memcpy(&gotd_session.request_timeout, request_timeout,
843 e70fd952 2024-03-22 stsp sizeof(gotd_session.request_timeout));
844 e70fd952 2024-03-22 stsp gotd_session.repo_cfg = repo_cfg;
846 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.notifier_iev.ibuf, -1);
848 e70fd952 2024-03-22 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
851 e70fd952 2024-03-22 stsp if (!got_repo_is_bare(gotd_session.repo)) {
852 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
853 e70fd952 2024-03-22 stsp "bare git repository required");
856 c214ca4f 2024-08-01 op if (got_repo_get_object_format(gotd_session.repo) != GOT_HASH_SHA1) {
857 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
858 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
862 e70fd952 2024-03-22 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
864 e70fd952 2024-03-22 stsp signal_set(&evsigint, SIGINT, session_read_sighdlr, NULL);
865 e70fd952 2024-03-22 stsp signal_set(&evsigterm, SIGTERM, session_read_sighdlr, NULL);
866 e70fd952 2024-03-22 stsp signal_set(&evsighup, SIGHUP, session_read_sighdlr, NULL);
867 e70fd952 2024-03-22 stsp signal_set(&evsigusr1, SIGUSR1, session_read_sighdlr, NULL);
868 e70fd952 2024-03-22 stsp signal(SIGPIPE, SIG_IGN);
870 e70fd952 2024-03-22 stsp signal_add(&evsigint, NULL);
871 e70fd952 2024-03-22 stsp signal_add(&evsigterm, NULL);
872 e70fd952 2024-03-22 stsp signal_add(&evsighup, NULL);
873 e70fd952 2024-03-22 stsp signal_add(&evsigusr1, NULL);
875 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
877 e70fd952 2024-03-22 stsp gotd_session_client.fd = -1;
878 e70fd952 2024-03-22 stsp gotd_session_client.nref_updates = -1;
879 e70fd952 2024-03-22 stsp gotd_session_client.delta_cache_fd = -1;
880 e70fd952 2024-03-22 stsp gotd_session_client.accept_flush_pkt = 1;
882 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
883 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler = session_dispatch;
884 e70fd952 2024-03-22 stsp gotd_session.parent_iev.events = EV_READ;
885 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler_arg = NULL;
886 e70fd952 2024-03-22 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
887 e70fd952 2024-03-22 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
888 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
889 e70fd952 2024-03-22 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_READ,
890 e70fd952 2024-03-22 stsp -1, NULL, 0) == -1) {
891 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
895 e70fd952 2024-03-22 stsp event_dispatch();
898 e70fd952 2024-03-22 stsp log_warnx("%s: %s", title, err->msg);
899 e70fd952 2024-03-22 stsp session_read_shutdown();
902 e70fd952 2024-03-22 stsp static void
903 e70fd952 2024-03-22 stsp session_read_shutdown(void)
905 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_session.title);
907 e70fd952 2024-03-22 stsp if (gotd_session.repo)
908 e70fd952 2024-03-22 stsp got_repo_close(gotd_session.repo);
909 e70fd952 2024-03-22 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
910 e70fd952 2024-03-22 stsp got_repo_temp_fds_close(gotd_session.temp_fds);
911 e70fd952 2024-03-22 stsp free(gotd_session_client.username);