--- lib_eio_posix/eio_posix_stubs.c.orig +++ lib_eio_posix/eio_posix_stubs.c @@ -38,6 +38,39 @@ #include "fork_action.h" #include "eio_unix_stubs.h" +#ifdef __APPLE__ +#include +#include +#if MAC_OS_X_VERSION_MAX_ALLOWED < 110000 +/* preadv/pwritev are only available on macOS 11.0+. + Emulate them with pread/pwrite loops; the file offset is not modified. */ +static ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset) { + ssize_t total = 0; + int i; + for (i = 0; i < iovcnt; i++) { + ssize_t r = pread(fd, iov[i].iov_base, iov[i].iov_len, offset); + if (r < 0) return total > 0 ? total : -1; + total += r; + offset += r; + if ((size_t)r < iov[i].iov_len) break; + } + return total; +} +static ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset) { + ssize_t total = 0; + int i; + for (i = 0; i < iovcnt; i++) { + ssize_t r = pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset); + if (r < 0) return total > 0 ? total : -1; + total += r; + offset += r; + if ((size_t)r < iov[i].iov_len) break; + } + return total; +} +#endif +#endif /* __APPLE__ */ + #ifdef ARCH_SIXTYFOUR #define Int63_val(v) Long_val(v) #define caml_copy_int63(v) Val_long(v) --- lib_eio/unix/pty.c.orig +++ lib_eio/unix/pty.c @@ -13,6 +13,10 @@ #include #endif +#ifdef __APPLE__ +#include +#endif + #include #include #include @@ -31,10 +35,11 @@ #else CAMLparam1(v_unit); -#if defined(__OpenBSD__) +#if defined(__OpenBSD__) || (defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070) /* OpenBSD posix_openpt accepts only O_RDWR | O_NOCTTY, so request close-on-exec separately via fcntl. - https://man.openbsd.org/posix_openpt.3 */ + https://man.openbsd.org/posix_openpt.3 + Same on macOS < 10.7, which predates O_CLOEXEC. */ int pty_fd = posix_openpt(O_RDWR | O_NOCTTY); if (pty_fd < 0) caml_uerror("posix_openpt", Nothing); @@ -70,7 +75,7 @@ caml_uerror("unlockpt", Nothing); { -#if defined(__OpenBSD__) +#if defined(__OpenBSD__) || (defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 101304) char *name = ptsname(pty_fd); if (name == NULL) caml_uerror("ptsname", Nothing); @@ -92,10 +97,22 @@ if (tty_fd < 0) caml_uerror("ioctl(TIOCGPTPEER)", Nothing); #else +#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070 + tty_fd = open(String_val(v_name), O_RDWR | O_NOCTTY); + if (tty_fd < 0) + caml_uerror("open", v_name); + if (fcntl(tty_fd, F_SETFD, FD_CLOEXEC) < 0) { + int e = errno; + close(tty_fd); + errno = e; + caml_uerror("open.fcntl", v_name); + } +#else tty_fd = open(String_val(v_name), O_RDWR | O_NOCTTY | O_CLOEXEC); if (tty_fd < 0) caml_uerror("open", v_name); #endif +#endif v_ret = caml_alloc_tuple(2); Store_field(v_ret, 0, Val_int(tty_fd));