From ba35d15de91ba19268f4fbe6ebff4e3389afd6fe Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Thu, 18 Jan 2024 22:56:54 -0600 Subject: [PATCH] Port orch(1) to OpenBSD We'll just build orch_compat.c all the time now; any potentially cnoflicting symbols are already preproc-gated behind OS that need it. OpenBSD's posix_openpt doesn't support setting O_CLOEXEC, so set it after the fact. All tests pass, with exception to the shebang test. OpenBSD's env(1) does not support -S splitting that we need in order for the script to be passed in as the -f argument. Signed-off-by: Kyle Evans --- Makefile | 5 ++--- orch.c | 12 +++++++++++- orch.h | 2 +- orch_compat.c | 2 +- orch_lua.c | 4 +++- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 4b7556d..aa7192b 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,11 @@ SRCS= orch.c \ orch_interp.c \ orch_lua.c -.if ${.MAKE.OS} == "Linux" +.if defined(.MAKE.OS) && ${.MAKE.OS} == "Linux" CFLAGS+= -D_GNU_SOURCE .endif -.if ${.MAKE.OS} == "Linux" || ${.MAKE.OS} == "Darwin" + SRCS+= orch_compat.c -.endif .if !empty(ORCHLUA_PATH) CFLAGS+= -DORCHLUA_PATH=\"${ORCHLUA_PATH}\" diff --git a/orch.c b/orch.c index 701dc55..062f214 100644 --- a/orch.c +++ b/orch.c @@ -16,6 +16,12 @@ #include "orch.h" +#ifdef __OpenBSD__ +#define POSIX_OPENPT_FLAGS (O_RDWR | O_NOCTTY) +#else +#define POSIX_OPENPT_FLAGS (O_RDWR | O_NOCTTY | O_CLOEXEC) +#endif + extern char **environ; static void orch_exec(int argc, const char *argv[], int cmdsock); @@ -175,9 +181,13 @@ orch_newpt(void) { int newpt; - newpt = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC); + newpt = posix_openpt(POSIX_OPENPT_FLAGS); if (newpt == -1) err(1, "posix_openpt"); +#if (POSIX_OPENPT_FLAGS & O_CLOEXEC) == 0 + if (fcntl(newpt, F_SETFD, fcntl(newpt, F_GETFD) | FD_CLOEXEC) == -1) + err(1, "fcntl"); +#endif if (grantpt(newpt) == -1) err(1, "grantpt"); diff --git a/orch.h b/orch.h index 9dbcc83..5bb199d 100644 --- a/orch.h +++ b/orch.h @@ -51,6 +51,6 @@ int luaopen_orch(lua_State *); size_t strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize); size_t strlcat(char * __restrict dst, const char * __restrict src, size_t dsize); #endif -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) int tcsetsid(int tty, int sess); #endif diff --git a/orch_compat.c b/orch_compat.c index db48788..09efe65 100644 --- a/orch_compat.c +++ b/orch_compat.c @@ -97,7 +97,7 @@ strlcat(char * __restrict dst, const char * __restrict src, size_t dsize) return(dlen + (src - osrc)); /* count does not include NUL */ } #endif /* __linux__ */ -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) /* Not associated with the above... incredibly simple. */ int diff --git a/orch_lua.c b/orch_lua.c index c6c838c..25819ac 100644 --- a/orch_lua.c +++ b/orch_lua.c @@ -35,7 +35,9 @@ #ifdef __linux__ #define CLOCK_REALTIME_FAST CLOCK_REALTIME_COARSE #endif -#ifdef __APPLE__ + +/* Fallback */ +#ifndef CLOCK_REALTIME_FAST #define CLOCK_REALTIME_FAST CLOCK_REALTIME #endif