commit febe25b7cf7410d2a34c127b3981acd3b98edc2d from: Thomas Adam date: Tue Aug 29 16:31:10 2023 UTC portable: gotd: listen Include listen.c; make it portable. commit - e24c4959f1105965cf2ae2d245d6f83c6531a43e commit + febe25b7cf7410d2a34c127b3981acd3b98edc2d blob - 2d7781684ee5bfed430739283d2efa55ad0ffc30 blob + cf7de518223cce9d4733af55713e0c91684c2358 --- gotd/Makefile.am +++ gotd/Makefile.am @@ -5,12 +5,14 @@ include $(top_builddir)/Makefile.common gotd_SOURCES = gotd.c \ auth.c \ imsg.c \ + listen.c \ log.c \ parse.y \ privsep_stub.c \ repo_imsg.c \ repo_read.c \ repo_write.c \ + session.c \ $(top_srcdir)/lib/bloom.c \ $(top_srcdir)/lib/buf.c \ $(top_srcdir)/lib/date.c \ blob - af44a052722a32c66a572c1d58b97b65193706e1 blob + 9ed19e79b8bd0a7e69cf184b6a0bb5abfaa87fc3 --- gotd/gotd.c +++ gotd/gotd.c @@ -993,13 +993,17 @@ connect_repo_child(struct gotd_client *client, struct gotd_imsgev *session_iev = &client->session->iev; struct gotd_imsg_connect_repo_child ireq; int pipe[2]; + int sock_flags = SOCK_STREAM | SOCK_NONBLOCK; + +#ifdef SOCK_CLOEXEC + sock_flags |= SOCK_CLOEXEC; +#endif if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) return got_error_msg(GOT_ERR_BAD_REQUEST, "unexpected repo child ready signal received"); - if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, - PF_UNSPEC, pipe) == -1) + if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1) fatal("socketpair"); memset(&ireq, 0, sizeof(ireq)); @@ -1571,7 +1575,7 @@ start_listener(char *argv0, const char *confpath, int proc->type = PROC_LISTEN; - if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, + if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1) fatal("socketpair"); @@ -1590,7 +1594,12 @@ start_session_child(struct gotd_client *client, struct char *argv0, const char *confpath, int daemonize, int verbosity) { struct gotd_child_proc *proc; + int sock_flags = SOCK_STREAM | SOCK_NONBLOCK; +#ifdef SOCK_CLOEXEC + sock_flags |= SOCK_CLOEXEC; +#endif + proc = calloc(1, sizeof(*proc)); if (proc == NULL) return got_error_from_errno("calloc"); @@ -1610,8 +1619,7 @@ start_session_child(struct gotd_client *client, struct if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >= sizeof(proc->repo_path)) fatalx("repository path too long: %s", repo->path); - if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, - PF_UNSPEC, proc->pipe) == -1) + if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1) fatal("socketpair"); proc->pid = start_child(proc->type, proc->repo_path, argv0, confpath, proc->pipe[1], daemonize, verbosity); @@ -1636,7 +1644,12 @@ start_repo_child(struct gotd_client *client, enum gotd int daemonize, int verbosity) { struct gotd_child_proc *proc; + int sock_flags = SOCK_STREAM|SOCK_NONBLOCK; +#ifdef SOCK_CLOEXEC + sock_flags |= SOCK_CLOEXEC; +#endif + if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE) return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type"); @@ -1653,10 +1666,13 @@ start_repo_child(struct gotd_client *client, enum gotd fatalx("repository name too long: %s", repo->name); log_debug("starting %s for repository %s", proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name); + if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >= sizeof(proc->repo_path)) fatalx("repository path too long: %s", repo->path); - if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, + if (realpath(repo->path, proc->repo_path) == NULL) + fatal("%s", repo->path); + if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1) fatal("socketpair"); proc->pid = start_child(proc->type, proc->repo_path, argv0, @@ -1685,7 +1701,12 @@ start_auth_child(struct gotd_client *client, int requi struct gotd_child_proc *proc; struct gotd_imsg_auth iauth; int fd; + int sock_flags = SOCK_STREAM|SOCK_NONBLOCK; +#ifdef SOCK_CLOEXEC + sock_flags |= SOCK_CLOEXEC; +#endif + memset(&iauth, 0, sizeof(iauth)); fd = dup(client->fd); @@ -1711,7 +1732,9 @@ start_auth_child(struct gotd_client *client, int requi if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >= sizeof(proc->repo_path)) fatalx("repository path too long: %s", repo->path); - if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, + if (realpath(repo->path, proc->repo_path) == NULL) + fatal("%s", repo->path); + if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1) fatal("socketpair"); proc->pid = start_child(proc->type, proc->repo_path, argv0, blob - 5a4ee87d85e91b5837d1d53b64706a661b1a359e blob + 45bdfb39b3e27a99426467c5a42b68c88a4111a8 --- gotd/listen.c +++ gotd/listen.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -34,6 +33,8 @@ #include "got_error.h" #include "got_path.h" +#include "got_compat.h" + #include "gotd.h" #include "log.h" #include "listen.h" @@ -218,16 +219,27 @@ accept_reserve(int fd, struct sockaddr *addr, socklen_ int reserve, volatile int *counter) { int ret; + int sock_flags = SOCK_NONBLOCK; +#ifdef SOCK_CLOEXEC + sock_flags |= SOCK_CLOEXEC; +#endif + if (getdtablecount() + reserve + ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) { log_debug("inflight fds exceeded"); errno = EMFILE; return -1; } +#ifdef __APPLE__ + /* TA: silence warning from GCC. */ + (void)sock_flags; + ret = accept(fd, addr, addrlen); +#else + ret = accept4(fd, addr, addrlen, sock_flags); +#endif - if ((ret = accept4(fd, addr, addrlen, - SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) { + if (ret > -1) { (*counter)++; } blob - 129aebf9ca0fd4335e15f41ac3374ac4670aa9e2 blob + 9b290cc8fb986baa3dc9b69f48d52e94740ac1e0 --- gotd/repo_write.c +++ gotd/repo_write.c @@ -85,7 +85,7 @@ STAILQ_HEAD(gotd_ref_updates, gotd_ref_update); static struct repo_write_client { uint32_t id; int fd; - int pack_pipe[2]; + int pack_pipe; struct got_pack pack; uint8_t pack_sha1[SHA1_DIGEST_LENGTH]; int packidx_fd; @@ -1258,15 +1258,6 @@ recv_packfile(int *have_packfile, struct imsg *imsg) tempfiles[i] = f; } - /* Send pack file pipe to gotsh(1). */ - if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE, - repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) { - (*client)->pack_pipe[1] = -1; - err = got_error_from_errno("imsg_compose ACK"); - if (err) - goto done; - } - (*client)->pack_pipe[1] = -1; err = gotd_imsg_flush(&ibuf); if (err) goto done; blob - ae96f8dd8c3df35dfde0827d038a3fec4e55a08a blob + 108bfe188d7d51323ad7f6323506dba066f9bba0 --- gotd/session.c +++ gotd/session.c @@ -30,6 +30,8 @@ #include #include #include + +#include "got_compat.h" #include "got_error.h" #include "got_repository.h"