Skip to content

Commit

Permalink
Port orch(1) to OpenBSD
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kevans91 committed Jan 19, 2024
1 parent 34ce835 commit ba35d15
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"
Expand Down
12 changes: 11 additions & 1 deletion orch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion orch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion orch_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion orch_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ba35d15

Please sign in to comment.