From 54c8abfca086a7ebd6190d56f6f2b93195098e35 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Mon, 27 Jul 2026 16:17:09 +0000 Subject: [PATCH 12/12] util: add mkostemp() polyfill for Darwin mkostemp() is a Linux/glibc extension (later adopted by *BSD), never implemented by Darwin on any macOS version. Both call sites (notify.c, shm.c) only ever pass O_CLOEXEC, so wrap mkstemp() + fcntl(F_SETFD, FD_CLOEXEC) the same way the existing pipe2()/fd_set_cloexec_nonblock polyfills in this header handle the analogous gaps -- same accepted small race window between file creation and the fcntl() call. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01U6fuL1PtRJHhr97gAGyS1h --- shm.c | 1 + util.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/shm.c b/shm.c index 5c1573ad..9cfd91de 100644 --- a/shm.c +++ b/shm.c @@ -21,6 +21,7 @@ #include "debug.h" #include "macros.h" #include "stride.h" +#include "util.h" #include "xmalloc.h" #if !defined(MAP_UNINITIALIZED) diff --git a/util.h b/util.h index 7ca3a9c9..0dfc27f1 100644 --- a/util.h +++ b/util.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -84,6 +85,37 @@ fail: { return -1; } } + +/* + * mkostemp() is a Linux/glibc extension (later adopted by *BSD), + * never implemented by Darwin on any macOS version. All current call + * sites only ever pass O_CLOEXEC. + */ +static inline int +mkostemp(char *template, int flags) +{ + if (flags & ~O_CLOEXEC) { + errno = EINVAL; + return -1; + } + + int fd = mkstemp(template); + if (fd < 0) + return -1; + + if (flags & O_CLOEXEC) { + int f = fcntl(fd, F_GETFD); + if (f < 0 || fcntl(fd, F_SETFD, f | FD_CLOEXEC) < 0) { + int saved_errno = errno; + close(fd); + unlink(template); + errno = saved_errno; + return -1; + } + } + + return fd; +} #endif static inline const char * -- 2.43.0