On 10/2/22 14:29, Will Cohen wrote:
From: Keno Fischer <k...@juliacomputing.com>

Darwin does not support mknodat. However, to avoid race conditions
with later setting the permissions, we must avoid using mknod on
the full path instead. We could try to fchdir, but that would cause
problems if multiple threads try to call mknodat at the same time.
However, luckily there is a solution: Darwin includes a function
that sets the cwd for the current thread only.
This should suffice to use mknod safely.

This function (pthread_fchdir_np) is protected by a check in
meson in a patch later in this series.

Signed-off-by: Keno Fischer <k...@juliacomputing.com>
Signed-off-by: Michael Roitzsch <reactorcont...@icloud.com>
[Will Cohen: - Adjust coding style
              - Replace clang references with gcc
              - Note radar filed with Apple for missing syscall
              - Replace direct syscall with pthread_fchdir_np and
                adjust patch notes accordingly
              - Move qemu_mknodat from 9p-util to osdep and os-posix
              - Move pthread_fchdir_np declaration only to osdep
              - Declare pthread_fchdir_np with
              - __attribute__((weak_import)) to allow checking for
                its presence before usage
              - Move declarations above cplusplus guard]
Signed-off-by: Will Cohen <wwco...@gmail.com>
---
  hw/9pfs/9p-local.c   |  4 ++--
  include/qemu/osdep.h | 12 ++++++++++++
  os-posix.c           | 35 +++++++++++++++++++++++++++++++++++
  3 files changed, 49 insertions(+), 2 deletions(-)

+/*
+ * As long as mknodat is not available on macOS, this workaround
+ * using pthread_fchdir_np is needed. qemu_mknodat is defined in
+ * os-posix.c. pthread_fchdir_np is weakly linked here as a guard
+ * in case it disappears in future macOS versions, because it is
+ * is a private API.
+ */
+#ifdef CONFIG_DARWIN
+int pthread_fchdir_np(int fd) __attribute__((weak_import));
+#endif

Please use Meson to define CONFIG_PTHREAD_FCHDIR_NP, similar to commits
10f6b23187 ("configure, meson: move pthread_setname_np checks to Meson")
and 479a57475e ("util: Implement debug-threads for macOS").

Reply via email to