2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/socket.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_hash.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
64 #include "repo_read.h"
65 #include "repo_write.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 enum gotd_client_state {
72 GOTD_CLIENT_STATE_NEW,
73 GOTD_CLIENT_STATE_ACCESS_GRANTED,
77 STAILQ_ENTRY(gotd_client) entry;
78 enum gotd_client_state state;
81 struct gotd_imsgev iev;
85 struct gotd_child_proc *repo;
86 struct gotd_child_proc *auth;
87 struct gotd_child_proc *session;
90 STAILQ_HEAD(gotd_clients, gotd_client);
92 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
93 static SIPHASH_KEY clients_hash_key;
94 volatile int client_cnt;
95 static struct timeval auth_timeout = { 5, 0 };
96 static struct gotd gotd;
98 void gotd_sighdlr(int sig, short event, void *arg);
99 static void gotd_shutdown(void);
100 static const struct got_error *start_session_child(struct gotd_client *,
101 struct gotd_repo *, char *, const char *, int, int);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
111 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
159 unlink(unix_socket_path);
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
166 unlink(unix_socket_path);
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
173 unlink(unix_socket_path);
181 client_hash(uint32_t client_id)
183 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
187 add_client(struct gotd_client *client)
189 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
190 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
194 static struct gotd_client *
195 find_client(uint32_t client_id)
198 struct gotd_client *c;
200 slot = client_hash(client_id) % nitems(gotd_clients);
201 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
202 if (c->id == client_id)
209 static struct gotd_client *
210 find_client_by_proc_fd(int fd)
214 for (slot = 0; slot < nitems(gotd_clients); slot++) {
215 struct gotd_client *c;
217 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
218 if (c->repo && c->repo->iev.ibuf.fd == fd)
220 if (c->auth && c->auth->iev.ibuf.fd == fd)
222 if (c->session && c->session->iev.ibuf.fd == fd)
231 client_is_reading(struct gotd_client *client)
233 return (client->required_auth &
234 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
238 client_is_writing(struct gotd_client *client)
240 return (client->required_auth &
241 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
242 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
245 static const struct got_error *
246 ensure_client_is_not_writing(struct gotd_client *client)
248 if (client_is_writing(client)) {
249 return got_error_fmt(GOT_ERR_BAD_PACKET,
250 "uid %d made a read-request but is writing to "
251 "a repository", client->euid);
257 static const struct got_error *
258 ensure_client_is_not_reading(struct gotd_client *client)
260 if (client_is_reading(client)) {
261 return got_error_fmt(GOT_ERR_BAD_PACKET,
262 "uid %d made a write-request but is reading from "
263 "a repository", client->euid);
270 wait_for_child(pid_t child_pid)
275 log_debug("waiting for child PID %ld to terminate",
279 pid = waitpid(child_pid, &status, WNOHANG);
281 if (errno != EINTR && errno != ECHILD)
283 } else if (WIFSIGNALED(status)) {
284 log_warnx("child PID %ld terminated; signal %d",
285 (long)pid, WTERMSIG(status));
287 } while (pid != -1 || (pid == -1 && errno == EINTR));
291 proc_done(struct gotd_child_proc *proc)
293 event_del(&proc->iev.ev);
294 msgbuf_clear(&proc->iev.ibuf.w);
295 close(proc->iev.ibuf.fd);
297 wait_for_child(proc->pid);
302 kill_auth_proc(struct gotd_client *client)
304 struct gotd_child_proc *proc;
306 if (client->auth == NULL)
316 kill_session_proc(struct gotd_client *client)
318 struct gotd_child_proc *proc;
320 if (client->session == NULL)
323 proc = client->session;
324 client->session = NULL;
330 disconnect(struct gotd_client *client)
332 struct gotd_imsg_disconnect idisconnect;
333 struct gotd_child_proc *proc = client->repo;
334 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
337 log_debug("uid %d: disconnecting", client->euid);
339 kill_auth_proc(client);
340 kill_session_proc(client);
343 event_del(&proc->iev.ev);
344 msgbuf_clear(&proc->iev.ibuf.w);
345 close(proc->iev.ibuf.fd);
347 wait_for_child(proc->pid);
352 idisconnect.client_id = client->id;
353 if (gotd_imsg_compose_event(&listen_proc->iev,
354 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
355 &idisconnect, sizeof(idisconnect)) == -1)
356 log_warn("imsg compose DISCONNECT");
358 slot = client_hash(client->id) % nitems(gotd_clients);
359 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
360 imsg_clear(&client->iev.ibuf);
361 event_del(&client->iev.ev);
362 evtimer_del(&client->tmo);
363 if (client->fd != -1)
365 else if (client->iev.ibuf.fd != -1)
366 close(client->iev.ibuf.fd);
372 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
376 log_warnx("uid %d: %s", client->euid, err->msg);
377 if (err->code != GOT_ERR_EOF && client->fd != -1) {
378 imsg_init(&ibuf, client->fd);
379 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
385 static const struct got_error *
386 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
388 const struct got_error *err = NULL;
389 struct gotd_imsg_info_repo irepo;
391 memset(&irepo, 0, sizeof(irepo));
393 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
394 >= sizeof(irepo.repo_name))
395 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
396 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
397 >= sizeof(irepo.repo_path))
398 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
400 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
401 &irepo, sizeof(irepo)) == -1) {
402 err = got_error_from_errno("imsg compose INFO_REPO");
410 static const struct got_error *
411 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
413 const struct got_error *err = NULL;
414 struct gotd_imsg_info_client iclient;
415 struct gotd_child_proc *proc;
417 memset(&iclient, 0, sizeof(iclient));
418 iclient.euid = client->euid;
419 iclient.egid = client->egid;
423 if (strlcpy(iclient.repo_name, proc->repo_path,
424 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
425 return got_error_msg(GOT_ERR_NO_SPACE,
426 "repo name too long");
428 if (client_is_writing(client))
429 iclient.is_writing = 1;
431 iclient.repo_child_pid = proc->pid;
435 iclient.session_child_pid = client->session->pid;
437 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
438 &iclient, sizeof(iclient)) == -1) {
439 err = got_error_from_errno("imsg compose INFO_CLIENT");
447 static const struct got_error *
448 send_info(struct gotd_client *client)
450 const struct got_error *err = NULL;
451 struct gotd_imsg_info info;
453 struct gotd_repo *repo;
455 if (client->euid != 0)
456 return got_error_set_errno(EPERM, "info");
459 info.verbosity = gotd.verbosity;
460 info.nrepos = gotd.nrepos;
461 info.nclients = client_cnt - 1;
463 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
464 &info, sizeof(info)) == -1) {
465 err = got_error_from_errno("imsg compose INFO");
470 TAILQ_FOREACH(repo, &gotd.repos, entry) {
471 err = send_repo_info(&client->iev, repo);
476 for (slot = 0; slot < nitems(gotd_clients); slot++) {
477 struct gotd_client *c;
478 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
479 if (c->id == client->id)
481 err = send_client_info(&client->iev, c);
490 static const struct got_error *
491 stop_gotd(struct gotd_client *client)
494 if (client->euid != 0)
495 return got_error_set_errno(EPERM, "stop");
502 static const struct got_error *
503 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
505 const struct got_error *err;
506 struct gotd_imsg_list_refs ireq;
507 struct gotd_repo *repo = NULL;
510 log_debug("list-refs request from uid %d", client->euid);
512 if (client->state != GOTD_CLIENT_STATE_NEW)
513 return got_error_msg(GOT_ERR_BAD_REQUEST,
514 "unexpected list-refs request received");
516 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
517 if (datalen != sizeof(ireq))
518 return got_error(GOT_ERR_PRIVSEP_LEN);
520 memcpy(&ireq, imsg->data, datalen);
522 if (ireq.client_is_reading) {
523 err = ensure_client_is_not_writing(client);
526 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
528 return got_error(GOT_ERR_NOT_GIT_REPO);
529 err = start_auth_child(client, GOTD_AUTH_READ, repo,
530 gotd.argv0, gotd.confpath, gotd.daemonize,
535 err = ensure_client_is_not_reading(client);
538 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
540 return got_error(GOT_ERR_NOT_GIT_REPO);
541 err = start_auth_child(client,
542 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
543 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
549 evtimer_add(&client->tmo, &auth_timeout);
551 /* Flow continues upon authentication successs/failure or timeout. */
556 gotd_request(int fd, short events, void *arg)
558 struct gotd_imsgev *iev = arg;
559 struct imsgbuf *ibuf = &iev->ibuf;
560 struct gotd_client *client = iev->handler_arg;
561 const struct got_error *err = NULL;
565 if (events & EV_WRITE) {
566 while (ibuf->w.queued) {
567 n = msgbuf_write(&ibuf->w);
568 if (n == -1 && errno == EPIPE) {
570 * The client has closed its socket.
571 * This can happen when Git clients are
572 * done sending pack file data.
574 msgbuf_clear(&ibuf->w);
576 } else if (n == -1 && errno != EAGAIN) {
577 err = got_error_from_errno("imsg_flush");
578 disconnect_on_error(client, err);
582 /* Connection closed. */
583 err = got_error(GOT_ERR_EOF);
584 disconnect_on_error(client, err);
589 /* Disconnect gotctl(8) now that messages have been sent. */
590 if (!client_is_reading(client) && !client_is_writing(client)) {
596 if ((events & EV_READ) == 0)
599 memset(&imsg, 0, sizeof(imsg));
601 while (err == NULL) {
602 err = gotd_imsg_recv(&imsg, ibuf, 0);
604 if (err->code == GOT_ERR_PRIVSEP_READ)
609 evtimer_del(&client->tmo);
611 switch (imsg.hdr.type) {
613 err = send_info(client);
616 err = stop_gotd(client);
618 case GOTD_IMSG_LIST_REFS:
619 err = start_client_authentication(client, &imsg);
622 log_debug("unexpected imsg %d", imsg.hdr.type);
623 err = got_error(GOT_ERR_PRIVSEP_MSG);
631 disconnect_on_error(client, err);
633 gotd_imsg_event_add(&client->iev);
638 gotd_auth_timeout(int fd, short events, void *arg)
640 struct gotd_client *client = arg;
642 log_debug("disconnecting uid %d due to authentication timeout",
647 static const struct got_error *
648 recv_connect(uint32_t *client_id, struct imsg *imsg)
650 const struct got_error *err = NULL;
651 struct gotd_imsg_connect iconnect;
654 struct gotd_client *client = NULL;
658 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
659 if (datalen != sizeof(iconnect))
660 return got_error(GOT_ERR_PRIVSEP_LEN);
661 memcpy(&iconnect, imsg->data, sizeof(iconnect));
665 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
669 if (find_client(iconnect.client_id)) {
670 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
674 client = calloc(1, sizeof(*client));
675 if (client == NULL) {
676 err = got_error_from_errno("calloc");
680 *client_id = iconnect.client_id;
682 client->state = GOTD_CLIENT_STATE_NEW;
683 client->id = iconnect.client_id;
686 /* The auth process will verify UID/GID for us. */
687 client->euid = iconnect.euid;
688 client->egid = iconnect.egid;
690 imsg_init(&client->iev.ibuf, client->fd);
691 client->iev.handler = gotd_request;
692 client->iev.events = EV_READ;
693 client->iev.handler_arg = client;
695 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
697 gotd_imsg_event_add(&client->iev);
699 evtimer_set(&client->tmo, gotd_auth_timeout, client);
702 log_debug("%s: new client uid %d connected on fd %d", __func__,
703 client->euid, client->fd);
706 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
707 struct gotd_imsg_disconnect idisconnect;
709 idisconnect.client_id = client->id;
710 if (gotd_imsg_compose_event(&listen_proc->iev,
711 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
712 &idisconnect, sizeof(idisconnect)) == -1)
713 log_warn("imsg compose DISCONNECT");
722 static const char *gotd_proc_names[PROC_MAX] = {
732 kill_proc(struct gotd_child_proc *proc, int fatal)
735 log_warnx("sending SIGKILL to PID %d", proc->pid);
736 kill(proc->pid, SIGKILL);
738 kill(proc->pid, SIGTERM);
744 struct gotd_child_proc *proc;
747 log_debug("shutting down");
748 for (slot = 0; slot < nitems(gotd_clients); slot++) {
749 struct gotd_client *c, *tmp;
751 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
755 proc = &gotd.listen_proc;
756 msgbuf_clear(&proc->iev.ibuf.w);
757 close(proc->iev.ibuf.fd);
759 wait_for_child(proc->pid);
761 log_info("terminating");
766 gotd_sighdlr(int sig, short event, void *arg)
769 * Normal signal handler rules don't apply because libevent
775 log_info("%s: ignoring SIGHUP", __func__);
778 log_info("%s: ignoring SIGUSR1", __func__);
785 fatalx("unexpected signal");
789 static const struct got_error *
790 ensure_proc_is_reading(struct gotd_client *client,
791 struct gotd_child_proc *proc)
793 if (!client_is_reading(client)) {
795 return got_error_fmt(GOT_ERR_BAD_PACKET,
796 "PID %d handled a read-request for uid %d but this "
797 "user is not reading from a repository", proc->pid,
804 static const struct got_error *
805 ensure_proc_is_writing(struct gotd_client *client,
806 struct gotd_child_proc *proc)
808 if (!client_is_writing(client)) {
810 return got_error_fmt(GOT_ERR_BAD_PACKET,
811 "PID %d handled a write-request for uid %d but this "
812 "user is not writing to a repository", proc->pid,
820 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
823 const struct got_error *err;
826 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
827 if (client->repo == NULL)
828 fatalx("no process found for uid %d", client->euid);
829 if (proc->pid != client->repo->pid) {
831 log_warnx("received message from PID %d for uid %d, "
832 "while PID %d is the process serving this user",
833 proc->pid, client->euid, client->repo->pid);
837 if (proc->type == PROC_SESSION) {
838 if (client->session == NULL) {
839 log_warnx("no session found for uid %d", client->euid);
842 if (proc->pid != client->session->pid) {
844 log_warnx("received message from PID %d for uid %d, "
845 "while PID %d is the process serving this user",
846 proc->pid, client->euid, client->session->pid);
851 switch (imsg->hdr.type) {
852 case GOTD_IMSG_ERROR:
855 case GOTD_IMSG_CONNECT:
856 if (proc->type != PROC_LISTEN) {
857 err = got_error_fmt(GOT_ERR_BAD_PACKET,
858 "new connection for uid %d from PID %d "
859 "which is not the listen process",
860 proc->pid, client->euid);
864 case GOTD_IMSG_ACCESS_GRANTED:
865 if (proc->type != PROC_AUTH) {
866 err = got_error_fmt(GOT_ERR_BAD_PACKET,
867 "authentication of uid %d from PID %d "
868 "which is not the auth process",
869 proc->pid, client->euid);
873 case GOTD_IMSG_CLIENT_SESSION_READY:
874 if (proc->type != PROC_SESSION) {
875 err = got_error_fmt(GOT_ERR_BAD_PACKET,
876 "unexpected \"ready\" signal from PID %d",
881 case GOTD_IMSG_REPO_CHILD_READY:
882 if (proc->type != PROC_REPO_READ &&
883 proc->type != PROC_REPO_WRITE) {
884 err = got_error_fmt(GOT_ERR_BAD_PACKET,
885 "unexpected \"ready\" signal from PID %d",
890 case GOTD_IMSG_PACKFILE_DONE:
891 err = ensure_proc_is_reading(client, proc);
893 log_warnx("uid %d: %s", client->euid, err->msg);
897 case GOTD_IMSG_PACKFILE_INSTALL:
898 case GOTD_IMSG_REF_UPDATES_START:
899 case GOTD_IMSG_REF_UPDATE:
900 err = ensure_proc_is_writing(client, proc);
902 log_warnx("uid %d: %s", client->euid, err->msg);
907 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
914 static const struct got_error *
915 connect_repo_child(struct gotd_client *client,
916 struct gotd_child_proc *repo_proc)
918 static const struct got_error *err;
919 struct gotd_imsgev *session_iev = &client->session->iev;
920 struct gotd_imsg_connect_repo_child ireq;
923 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
924 return got_error_msg(GOT_ERR_BAD_REQUEST,
925 "unexpected repo child ready signal received");
927 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
928 PF_UNSPEC, pipe) == -1)
931 memset(&ireq, 0, sizeof(ireq));
932 ireq.client_id = client->id;
933 ireq.proc_id = repo_proc->type;
935 /* Pass repo child pipe to session child process. */
936 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
937 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
938 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
944 /* Pass session child pipe to repo child process. */
945 if (gotd_imsg_compose_event(&repo_proc->iev,
946 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
947 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
956 gotd_dispatch_listener(int fd, short event, void *arg)
958 struct gotd_imsgev *iev = arg;
959 struct imsgbuf *ibuf = &iev->ibuf;
960 struct gotd_child_proc *proc = &gotd.listen_proc;
965 if (proc->iev.ibuf.fd != fd)
966 fatalx("%s: unexpected fd %d", __func__, fd);
968 if (event & EV_READ) {
969 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
970 fatal("imsg_read error");
972 /* Connection closed. */
978 if (event & EV_WRITE) {
979 n = msgbuf_write(&ibuf->w);
980 if (n == -1 && errno != EAGAIN)
981 fatal("msgbuf_write");
983 /* Connection closed. */
990 const struct got_error *err = NULL;
991 struct gotd_client *client = NULL;
992 uint32_t client_id = 0;
993 int do_disconnect = 0;
995 if ((n = imsg_get(ibuf, &imsg)) == -1)
996 fatal("%s: imsg_get error", __func__);
997 if (n == 0) /* No more messages. */
1000 switch (imsg.hdr.type) {
1001 case GOTD_IMSG_ERROR:
1003 err = gotd_imsg_recv_error(&client_id, &imsg);
1005 case GOTD_IMSG_CONNECT:
1006 err = recv_connect(&client_id, &imsg);
1009 log_debug("unexpected imsg %d", imsg.hdr.type);
1013 client = find_client(client_id);
1014 if (client == NULL) {
1015 log_warnx("%s: client not found", __func__);
1021 log_warnx("uid %d: %s", client->euid, err->msg);
1023 if (do_disconnect) {
1025 disconnect_on_error(client, err);
1034 gotd_imsg_event_add(iev);
1036 /* This pipe is dead. Remove its event handler */
1037 event_del(&iev->ev);
1038 event_loopexit(NULL);
1043 gotd_dispatch_auth_child(int fd, short event, void *arg)
1045 const struct got_error *err = NULL;
1046 struct gotd_imsgev *iev = arg;
1047 struct imsgbuf *ibuf = &iev->ibuf;
1048 struct gotd_client *client;
1049 struct gotd_repo *repo = NULL;
1053 uint32_t client_id = 0;
1054 int do_disconnect = 0;
1056 client = find_client_by_proc_fd(fd);
1057 if (client == NULL) {
1058 /* Can happen during process teardown. */
1059 warnx("cannot find client for fd %d", fd);
1064 if (client->auth == NULL)
1065 fatalx("cannot find auth child process for fd %d", fd);
1067 if (event & EV_READ) {
1068 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1069 fatal("imsg_read error");
1071 /* Connection closed. */
1077 if (event & EV_WRITE) {
1078 n = msgbuf_write(&ibuf->w);
1079 if (n == -1 && errno != EAGAIN)
1080 fatal("msgbuf_write");
1082 /* Connection closed. */
1088 if (client->auth->iev.ibuf.fd != fd)
1089 fatalx("%s: unexpected fd %d", __func__, fd);
1091 if ((n = imsg_get(ibuf, &imsg)) == -1)
1092 fatal("%s: imsg_get error", __func__);
1093 if (n == 0) /* No more messages. */
1096 evtimer_del(&client->tmo);
1098 switch (imsg.hdr.type) {
1099 case GOTD_IMSG_ERROR:
1101 err = gotd_imsg_recv_error(&client_id, &imsg);
1103 case GOTD_IMSG_ACCESS_GRANTED:
1104 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1108 log_debug("unexpected imsg %d", imsg.hdr.type);
1112 if (!verify_imsg_src(client, client->auth, &imsg)) {
1114 log_debug("dropping imsg type %d from PID %d",
1115 imsg.hdr.type, client->auth->pid);
1119 if (do_disconnect) {
1121 disconnect_on_error(client, err);
1127 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1129 err = got_error(GOT_ERR_NOT_GIT_REPO);
1132 kill_auth_proc(client);
1134 log_info("authenticated uid %d for repository %s",
1135 client->euid, repo->name);
1137 err = start_session_child(client, repo, gotd.argv0,
1138 gotd.confpath, gotd.daemonize, gotd.verbosity);
1143 log_warnx("uid %d: %s", client->euid, err->msg);
1145 /* We might have killed the auth process by now. */
1146 if (client->auth != NULL) {
1148 gotd_imsg_event_add(iev);
1150 /* This pipe is dead. Remove its event handler */
1151 event_del(&iev->ev);
1156 static const struct got_error *
1157 connect_session(struct gotd_client *client)
1159 const struct got_error *err = NULL;
1160 struct gotd_imsg_connect iconnect;
1163 memset(&iconnect, 0, sizeof(iconnect));
1165 s = dup(client->fd);
1167 return got_error_from_errno("dup");
1169 iconnect.client_id = client->id;
1170 iconnect.euid = client->euid;
1171 iconnect.egid = client->egid;
1173 if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1174 PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1175 err = got_error_from_errno("imsg compose CONNECT");
1181 * We are no longer interested in messages from this client.
1182 * Further client requests will be handled by the session process.
1184 msgbuf_clear(&client->iev.ibuf.w);
1185 imsg_clear(&client->iev.ibuf);
1186 event_del(&client->iev.ev);
1187 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1193 gotd_dispatch_client_session(int fd, short event, void *arg)
1195 struct gotd_imsgev *iev = arg;
1196 struct imsgbuf *ibuf = &iev->ibuf;
1197 struct gotd_child_proc *proc = NULL;
1198 struct gotd_client *client = NULL;
1203 client = find_client_by_proc_fd(fd);
1204 if (client == NULL) {
1205 /* Can happen during process teardown. */
1206 warnx("cannot find client for fd %d", fd);
1211 if (event & EV_READ) {
1212 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1213 fatal("imsg_read error");
1215 /* Connection closed. */
1221 if (event & EV_WRITE) {
1222 n = msgbuf_write(&ibuf->w);
1223 if (n == -1 && errno != EAGAIN)
1224 fatal("msgbuf_write");
1226 /* Connection closed. */
1232 proc = client->session;
1234 fatalx("cannot find session child process for fd %d", fd);
1237 const struct got_error *err = NULL;
1238 uint32_t client_id = 0;
1239 int do_disconnect = 0, do_start_repo_child = 0;
1241 if ((n = imsg_get(ibuf, &imsg)) == -1)
1242 fatal("%s: imsg_get error", __func__);
1243 if (n == 0) /* No more messages. */
1246 switch (imsg.hdr.type) {
1247 case GOTD_IMSG_ERROR:
1249 err = gotd_imsg_recv_error(&client_id, &imsg);
1251 case GOTD_IMSG_CLIENT_SESSION_READY:
1252 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1253 err = got_error(GOT_ERR_PRIVSEP_MSG);
1256 do_start_repo_child = 1;
1258 case GOTD_IMSG_DISCONNECT:
1262 log_debug("unexpected imsg %d", imsg.hdr.type);
1266 if (!verify_imsg_src(client, proc, &imsg)) {
1267 log_debug("dropping imsg type %d from PID %d",
1268 imsg.hdr.type, proc->pid);
1273 log_warnx("uid %d: %s", client->euid, err->msg);
1275 if (do_start_repo_child) {
1276 struct gotd_repo *repo;
1277 const char *name = client->session->repo_name;
1279 repo = gotd_find_repo_by_name(name, &gotd);
1281 enum gotd_procid proc_type;
1283 if (client->required_auth & GOTD_AUTH_WRITE)
1284 proc_type = PROC_REPO_WRITE;
1286 proc_type = PROC_REPO_READ;
1288 err = start_repo_child(client, proc_type, repo,
1289 gotd.argv0, gotd.confpath, gotd.daemonize,
1292 err = got_error(GOT_ERR_NOT_GIT_REPO);
1295 log_warnx("uid %d: %s", client->euid, err->msg);
1300 if (do_disconnect) {
1302 disconnect_on_error(client, err);
1311 gotd_imsg_event_add(iev);
1313 /* This pipe is dead. Remove its event handler */
1314 event_del(&iev->ev);
1320 gotd_dispatch_repo_child(int fd, short event, void *arg)
1322 struct gotd_imsgev *iev = arg;
1323 struct imsgbuf *ibuf = &iev->ibuf;
1324 struct gotd_child_proc *proc = NULL;
1325 struct gotd_client *client;
1330 client = find_client_by_proc_fd(fd);
1331 if (client == NULL) {
1332 /* Can happen during process teardown. */
1333 warnx("cannot find client for fd %d", fd);
1338 if (event & EV_READ) {
1339 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1340 fatal("imsg_read error");
1342 /* Connection closed. */
1348 if (event & EV_WRITE) {
1349 n = msgbuf_write(&ibuf->w);
1350 if (n == -1 && errno != EAGAIN)
1351 fatal("msgbuf_write");
1353 /* Connection closed. */
1359 proc = client->repo;
1361 fatalx("cannot find child process for fd %d", fd);
1364 const struct got_error *err = NULL;
1365 uint32_t client_id = 0;
1366 int do_disconnect = 0;
1368 if ((n = imsg_get(ibuf, &imsg)) == -1)
1369 fatal("%s: imsg_get error", __func__);
1370 if (n == 0) /* No more messages. */
1373 switch (imsg.hdr.type) {
1374 case GOTD_IMSG_ERROR:
1376 err = gotd_imsg_recv_error(&client_id, &imsg);
1378 case GOTD_IMSG_REPO_CHILD_READY:
1379 err = connect_session(client);
1382 err = connect_repo_child(client, proc);
1385 log_debug("unexpected imsg %d", imsg.hdr.type);
1389 if (!verify_imsg_src(client, proc, &imsg)) {
1390 log_debug("dropping imsg type %d from PID %d",
1391 imsg.hdr.type, proc->pid);
1396 log_warnx("uid %d: %s", client->euid, err->msg);
1398 if (do_disconnect) {
1400 disconnect_on_error(client, err);
1409 gotd_imsg_event_add(iev);
1411 /* This pipe is dead. Remove its event handler */
1412 event_del(&iev->ev);
1418 start_child(enum gotd_procid proc_id, const char *repo_path,
1419 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1425 switch (pid = fork()) {
1427 fatal("cannot fork");
1435 if (fd != GOTD_FILENO_MSG_PIPE) {
1436 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1437 fatal("cannot setup imsg fd");
1438 } else if (fcntl(fd, F_SETFD, 0) == -1)
1439 fatal("cannot setup imsg fd");
1441 argv[argc++] = argv0;
1444 argv[argc++] = (char *)"-L";
1447 argv[argc++] = (char *)"-A";
1450 argv[argc++] = (char *)"-S";
1452 case PROC_REPO_READ:
1453 argv[argc++] = (char *)"-R";
1455 case PROC_REPO_WRITE:
1456 argv[argc++] = (char *)"-W";
1459 fatalx("invalid process id %d", proc_id);
1462 argv[argc++] = (char *)"-f";
1463 argv[argc++] = (char *)confpath;
1466 argv[argc++] = (char *)"-P";
1467 argv[argc++] = (char *)repo_path;
1471 argv[argc++] = (char *)"-d";
1473 argv[argc++] = (char *)"-v";
1475 argv[argc++] = (char *)"-v";
1476 argv[argc++] = NULL;
1478 execvp(argv0, argv);
1483 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1485 struct gotd_child_proc *proc = &gotd.listen_proc;
1487 proc->type = PROC_LISTEN;
1489 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1490 PF_UNSPEC, proc->pipe) == -1)
1491 fatal("socketpair");
1493 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1494 proc->pipe[1], daemonize, verbosity);
1495 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1496 proc->iev.handler = gotd_dispatch_listener;
1497 proc->iev.events = EV_READ;
1498 proc->iev.handler_arg = NULL;
1501 static const struct got_error *
1502 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1503 char *argv0, const char *confpath, int daemonize, int verbosity)
1505 struct gotd_child_proc *proc;
1507 proc = calloc(1, sizeof(*proc));
1509 return got_error_from_errno("calloc");
1511 proc->type = PROC_SESSION;
1512 if (strlcpy(proc->repo_name, repo->name,
1513 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1514 fatalx("repository name too long: %s", repo->name);
1515 log_debug("starting client uid %d session for repository %s",
1516 client->euid, repo->name);
1517 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1518 sizeof(proc->repo_path))
1519 fatalx("repository path too long: %s", repo->path);
1520 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1521 PF_UNSPEC, proc->pipe) == -1)
1522 fatal("socketpair");
1523 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1524 confpath, proc->pipe[1], daemonize, verbosity);
1525 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1526 log_debug("proc %s %s is on fd %d",
1527 gotd_proc_names[proc->type], proc->repo_path,
1529 proc->iev.handler = gotd_dispatch_client_session;
1530 proc->iev.events = EV_READ;
1531 proc->iev.handler_arg = NULL;
1532 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1533 gotd_dispatch_client_session, &proc->iev);
1534 gotd_imsg_event_add(&proc->iev);
1536 client->session = proc;
1540 static const struct got_error *
1541 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1542 struct gotd_repo *repo, char *argv0, const char *confpath,
1543 int daemonize, int verbosity)
1545 struct gotd_child_proc *proc;
1547 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1548 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1550 proc = calloc(1, sizeof(*proc));
1552 return got_error_from_errno("calloc");
1554 proc->type = proc_type;
1555 if (strlcpy(proc->repo_name, repo->name,
1556 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1557 fatalx("repository name too long: %s", repo->name);
1558 log_debug("starting %s for repository %s",
1559 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1560 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1561 sizeof(proc->repo_path))
1562 fatalx("repository path too long: %s", repo->path);
1563 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1564 PF_UNSPEC, proc->pipe) == -1)
1565 fatal("socketpair");
1566 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1567 confpath, proc->pipe[1], daemonize, verbosity);
1568 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1569 log_debug("proc %s %s is on fd %d",
1570 gotd_proc_names[proc->type], proc->repo_path,
1572 proc->iev.handler = gotd_dispatch_repo_child;
1573 proc->iev.events = EV_READ;
1574 proc->iev.handler_arg = NULL;
1575 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1576 gotd_dispatch_repo_child, &proc->iev);
1577 gotd_imsg_event_add(&proc->iev);
1579 client->repo = proc;
1583 static const struct got_error *
1584 start_auth_child(struct gotd_client *client, int required_auth,
1585 struct gotd_repo *repo, char *argv0, const char *confpath,
1586 int daemonize, int verbosity)
1588 const struct got_error *err = NULL;
1589 struct gotd_child_proc *proc;
1590 struct gotd_imsg_auth iauth;
1593 memset(&iauth, 0, sizeof(iauth));
1595 fd = dup(client->fd);
1597 return got_error_from_errno("dup");
1599 proc = calloc(1, sizeof(*proc));
1601 err = got_error_from_errno("calloc");
1606 proc->type = PROC_AUTH;
1607 if (strlcpy(proc->repo_name, repo->name,
1608 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1609 fatalx("repository name too long: %s", repo->name);
1610 log_debug("starting auth for uid %d repository %s",
1611 client->euid, repo->name);
1612 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1613 sizeof(proc->repo_path))
1614 fatalx("repository path too long: %s", repo->path);
1615 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1616 PF_UNSPEC, proc->pipe) == -1)
1617 fatal("socketpair");
1618 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1619 confpath, proc->pipe[1], daemonize, verbosity);
1620 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1621 log_debug("proc %s %s is on fd %d",
1622 gotd_proc_names[proc->type], proc->repo_path,
1624 proc->iev.handler = gotd_dispatch_auth_child;
1625 proc->iev.events = EV_READ;
1626 proc->iev.handler_arg = NULL;
1627 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1628 gotd_dispatch_auth_child, &proc->iev);
1629 gotd_imsg_event_add(&proc->iev);
1631 iauth.euid = client->euid;
1632 iauth.egid = client->egid;
1633 iauth.required_auth = required_auth;
1634 iauth.client_id = client->id;
1635 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1636 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1637 log_warn("imsg compose AUTHENTICATE");
1639 /* Let the auth_timeout handler tidy up. */
1642 client->auth = proc;
1643 client->required_auth = required_auth;
1648 apply_unveil_repo_readonly(const char *repo_path)
1650 if (unveil(repo_path, "r") == -1)
1651 fatal("unveil %s", repo_path);
1653 if (unveil(NULL, NULL) == -1)
1658 apply_unveil_repo_readwrite(const char *repo_path)
1660 if (unveil(repo_path, "rwc") == -1)
1661 fatal("unveil %s", repo_path);
1663 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1664 fatal("unveil %s", GOT_TMPDIR_STR);
1666 if (unveil(NULL, NULL) == -1)
1671 apply_unveil_none(void)
1673 if (unveil("/", "") == -1)
1676 if (unveil(NULL, NULL) == -1)
1681 apply_unveil_selfexec(void)
1683 if (unveil(gotd.argv0, "x") == -1)
1684 fatal("unveil %s", gotd.argv0);
1686 if (unveil(NULL, NULL) == -1)
1691 main(int argc, char **argv)
1693 const struct got_error *error = NULL;
1694 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1695 const char *confpath = GOTD_CONF_PATH;
1696 char *argv0 = argv[0];
1698 struct passwd *pw = NULL;
1699 char *repo_path = NULL;
1700 enum gotd_procid proc_id = PROC_GOTD;
1701 struct event evsigint, evsigterm, evsighup, evsigusr1;
1702 int *pack_fds = NULL, *temp_fds = NULL;
1703 struct gotd_repo *repo = NULL;
1705 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1707 while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1710 proc_id = PROC_AUTH;
1719 proc_id = PROC_LISTEN;
1725 repo_path = realpath(optarg, NULL);
1726 if (repo_path == NULL)
1727 fatal("realpath '%s'", optarg);
1730 proc_id = PROC_REPO_READ;
1733 proc_id = PROC_SESSION;
1740 proc_id = PROC_REPO_WRITE;
1753 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1754 fatalx("need root privileges");
1756 if (parse_config(confpath, proc_id, &gotd, 1) != 0)
1759 pw = getpwnam(gotd.user_name);
1761 fatalx("user %s not found", gotd.user_name);
1763 if (pw->pw_uid == 0)
1764 fatalx("cannot run %s as the superuser", getprogname());
1767 fprintf(stderr, "configuration OK\n");
1772 gotd.daemonize = daemonize;
1773 gotd.verbosity = verbosity;
1774 gotd.confpath = confpath;
1776 /* Require an absolute path in argv[0] for reliable re-exec. */
1777 if (!got_path_is_absolute(argv0))
1778 fatalx("bad path \"%s\": must be an absolute path", argv0);
1780 log_init(daemonize ? 0 : 1, LOG_DAEMON);
1781 log_setverbose(verbosity);
1783 if (proc_id == PROC_GOTD) {
1784 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1785 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1786 if (daemonize && daemon(1, 0) == -1)
1788 gotd.pid = getpid();
1789 start_listener(argv0, confpath, daemonize, verbosity);
1790 } else if (proc_id == PROC_LISTEN) {
1791 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1793 log_info("socket: %s", gotd.unix_socket_path);
1794 log_info("user: %s", pw->pw_name);
1797 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1800 fatal("cannot listen on unix socket %s",
1801 gotd.unix_socket_path);
1803 } else if (proc_id == PROC_AUTH) {
1804 snprintf(title, sizeof(title), "%s %s",
1805 gotd_proc_names[proc_id], repo_path);
1806 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1807 proc_id == PROC_SESSION) {
1808 error = got_repo_pack_fds_open(&pack_fds);
1810 fatalx("cannot open pack tempfiles: %s", error->msg);
1811 error = got_repo_temp_fds_open(&temp_fds);
1813 fatalx("cannot open pack tempfiles: %s", error->msg);
1814 if (repo_path == NULL)
1815 fatalx("repository path not specified");
1816 snprintf(title, sizeof(title), "%s %s",
1817 gotd_proc_names[proc_id], repo_path);
1819 fatal("invalid process id %d", proc_id);
1821 setproctitle("%s", title);
1822 log_procinit(title);
1824 /* Drop root privileges. */
1825 if (setgid(pw->pw_gid) == -1)
1826 fatal("setgid %d failed", pw->pw_gid);
1827 if (setuid(pw->pw_uid) == -1)
1828 fatal("setuid %d failed", pw->pw_uid);
1835 /* "exec" promise will be limited to argv[0] via unveil(2). */
1836 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1842 if (pledge("stdio sendfd unix unveil", NULL) == -1)
1846 * Ensure that AF_UNIX bind(2) cannot be used with any other
1847 * sockets by revoking all filesystem access via unveil(2).
1849 apply_unveil_none();
1851 listen_main(title, fd, gotd.connection_limits,
1852 gotd.nconnection_limits);
1857 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1861 * We need the "unix" pledge promise for getpeername(2) only.
1862 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1863 * filesystem access via unveil(2). Access to password database
1864 * files will still work since "getpw" bypasses unveil(2).
1866 apply_unveil_none();
1868 auth_main(title, &gotd.repos, repo_path);
1874 * The "recvfd" promise is only needed during setup and
1875 * will be removed in a later pledge(2) call.
1877 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1878 "unveil", NULL) == -1)
1881 apply_unveil_repo_readwrite(repo_path);
1882 session_main(title, repo_path, pack_fds, temp_fds,
1883 &gotd.request_timeout);
1886 case PROC_REPO_READ:
1888 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1891 apply_unveil_repo_readonly(repo_path);
1892 repo_read_main(title, repo_path, pack_fds, temp_fds);
1895 case PROC_REPO_WRITE:
1897 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1900 apply_unveil_repo_readonly(repo_path);
1901 repo = gotd_find_repo_by_path(repo_path, &gotd);
1903 fatalx("no repository for path %s", repo_path);
1904 repo_write_main(title, repo_path, pack_fds, temp_fds,
1905 &repo->protected_tag_namespaces,
1906 &repo->protected_branch_namespaces,
1907 &repo->protected_branches);
1911 fatal("invalid process id %d", proc_id);
1914 if (proc_id != PROC_GOTD)
1915 fatal("invalid process id %d", proc_id);
1917 apply_unveil_selfexec();
1919 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1920 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1921 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1922 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1923 signal(SIGPIPE, SIG_IGN);
1925 signal_add(&evsigint, NULL);
1926 signal_add(&evsigterm, NULL);
1927 signal_add(&evsighup, NULL);
1928 signal_add(&evsigusr1, NULL);
1930 gotd_imsg_event_add(&gotd.listen_proc.iev);