Skip to content

Commit

Permalink
Merge pull request #1020 from MacroModel/master
Browse files Browse the repository at this point in the history
fix darwin
  • Loading branch information
trcrsired authored Dec 7, 2024
2 parents 3b96519 + 8aa9637 commit ddcb085
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
20 changes: 16 additions & 4 deletions include/fast_io_hosted/platforms/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,21 @@ inline int open_fd_from_handle(void *handle, open_mode md)
}

#else
#if defined(__DARWIN_C_LEVEL) || defined(__MSDOS__)
extern unsigned int my_posix_open_noexcept(const char *pathname, int flags) noexcept __asm__("_open");
extern unsigned int my_posix_open_noexcept(char const *pathname, int flags, mode_t mode) noexcept __asm__("_open");
#else
extern unsigned int my_posix_open_noexcept(const char *pathname, int flags) noexcept __asm__("open");
extern unsigned int my_posix_open_noexcept(char const *pathname, int flags, mode_t mode) noexcept __asm__("open");
#endif

#if defined(__MSDOS__)
template <bool always_terminate = false>
inline int my_posix_openat(int dirfd, char const *pathname, int flags, mode_t mode)
{
if (dirfd == -100)
{
int fd(::open(pathname, flags, mode));
int fd(my_posix_open_noexcept(pathname, flags, mode));
system_call_throw_error<always_terminate>(fd);
return fd;
}
Expand Down Expand Up @@ -881,7 +888,7 @@ inline int my_posix_openat(int dirfd, char const *pathname, int flags, mode_t mo

// concat
::fast_io::tlc::string pn{::fast_io::tlc::concat_fast_io_tlc(::fast_io::mnp::os_c_str(pathname_cstr), "\\", para_pathname)};
int fd{::open(pn.c_str(), flags, mode)};
int fd{my_posix_open_noexcept(pn.c_str(), flags, mode)};
system_call_throw_error<always_terminate>(fd);
return fd;
}
Expand All @@ -903,7 +910,11 @@ inline int my_posix_openat(int, char const *, int, mode_t)
}
#else

#if defined(__DARWIN_C_LEVEL) || defined(__MSDOS__)
extern int my_posix_openat_noexcept(int fd, char const *path, int aflag, ... /*mode_t mode*/) noexcept __asm__("_openat");
#else
extern int my_posix_openat_noexcept(int fd, char const *path, int aflag, ... /*mode_t mode*/) noexcept __asm__("openat");
#endif

template <bool always_terminate = false>
inline int my_posix_openat(int dirfd, char const *pathname, int flags, mode_t mode)
Expand Down Expand Up @@ -971,6 +982,7 @@ extern unsigned int my_dos_open(char const *, short unsigned, int *) noexcept __
extern unsigned int my_dos_setmode(int, int) noexcept __asm__("_setmode");
extern unsigned int my_dos_close(int) noexcept __asm__("__dos_close");
#endif

template <bool always_terminate = false>
inline int my_posix_open(char const *pathname, int flags,
#if __has_cpp_attribute(maybe_unused)
Expand All @@ -979,7 +991,7 @@ inline int my_posix_open(char const *pathname, int flags,
mode_t mode)
{
#if defined(__MSDOS__) || (defined(__NEWLIB__) && !defined(AT_FDCWD)) || defined(_PICOLIBC__)
int fd{::open(pathname, flags, mode)};
int fd{my_posix_open_noexcept(pathname, flags, mode)};
system_call_throw_error<always_terminate>(fd);
return fd;
#else
Expand Down Expand Up @@ -1370,7 +1382,7 @@ class basic_posix_family_pipe
#elif (defined(__MSDOS__) || defined(__DJGPP__))
if (noexcept_call(::pipe, a2) == -1)
#else
if (noexcept_call(::pipe, a2) == -1 || sys_fcntl(a2[0], F_SETFD, FD_CLOEXEC) == -1 || sys_fcntl(a2[1], F_SETFD, FD_CLOEXEC) == -1)
if (noexcept_call(::pipe, a2) == -1 || ::fast_io::details::sys_fcntl(a2[0], F_SETFD, FD_CLOEXEC) == -1 || ::fast_io::details::sys_fcntl(a2[1], F_SETFD, FD_CLOEXEC) == -1)
#endif
throw_posix_error();
pipes->fd = *a2;
Expand Down
3 changes: 2 additions & 1 deletion include/fast_io_hosted/platforms/systemcall_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ inline int sys_fcntl(int fd, int op, Args... args)
return result;
#else
auto result{posix::fcntl(fd, op, args...)};
#endif
if (result == -1)
{
throw_posix_error();
}
return result;
#endif
}

#endif
Expand Down
42 changes: 27 additions & 15 deletions include/fast_io_hosted/process/process/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ namespace fast_io
namespace posix
{
#if defined(__DARWIN_C_LEVEL) || defined(__MSDOS__)
extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("_faccessat");
// extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("_faccessat");
extern int libc_fexecve(int fd, char *const *argv, char *const *envp) noexcept __asm__("_fexecve");
extern int libc_kill(pid_t pid, int sig) noexcept __asm__("_kill");
extern pid_t libc_fork() noexcept __asm__("_fork");
extern pid_t libc_vfork() noexcept __asm__("_vfork");
extern pid_t libc_waitpid(pid_t pid, int *status, int options) noexcept __asm__("_waitpid");
[[noreturn]] extern void libc_exit2(int status) noexcept __asm__("__Exit");
#else
extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("faccessat");
// extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("faccessat");
extern int libc_fexecve(int fd, char *const *argv, char *const *envp) noexcept __asm__("fexecve");
extern int libc_kill(pid_t pid, int sig) noexcept __asm__("kill");
extern pid_t libc_fork() noexcept __asm__("fork");
extern pid_t libc_vfork() noexcept __asm__("vfork");
extern pid_t libc_waitpid(pid_t pid, int *status, int options) noexcept __asm__("waitpid");
[[noreturn]] extern void libc_exit2(int status) noexcept __asm__("_Exit");
#endif
} // namespace posix

Expand Down Expand Up @@ -98,7 +106,7 @@ inline pid_t posix_fork()
pid_t pid{system_call<__NR_fork, pid_t>()};
system_call_throw_error(pid);
#else
pid_t pid{noexcept_call(::fork)};
pid_t pid{::fast_io::posix::libc_fork()};
if (pid == -1) [[unlikely]]
{
throw_posix_error();
Expand All @@ -113,7 +121,7 @@ inline posix_wait_status posix_waitpid(pid_t pid)
#if defined(__linux__) && defined(__NR_wait4)
system_call_throw_error(system_call<__NR_wait4, int>(pid, __builtin_addressof(status.wait_loc), 0, nullptr));
#else
if (noexcept_call(waitpid, pid, __builtin_addressof(status.wait_loc), 0) == -1)
if (::fast_io::posix::libc_waitpid(pid, __builtin_addressof(status.wait_loc), 0) == -1)
{
throw_posix_error();
}
Expand All @@ -130,7 +138,7 @@ inline void posix_waitpid_noexcept(pid_t pid) noexcept
#if defined(__linux__) && defined(__NR_wait4)
system_call<__NR_wait4, int>(pid, nullptr, 0, nullptr);
#else
noexcept_call(waitpid, pid, nullptr, 0);
::fast_io::posix::libc_waitpid(pid, nullptr, 0);
#endif
}

Expand Down Expand Up @@ -354,9 +362,9 @@ struct fd_remapper
return fd_devnull;
}
#ifdef O_CLOEXEC
fd_devnull = my_posix_open<true>(reinterpret_cast<char const*>(u8"/dev/null"), O_RDWR | O_CLOEXEC, 0644);
fd_devnull = my_posix_open<true>(reinterpret_cast<char const *>(u8"/dev/null"), O_RDWR | O_CLOEXEC, 0644);
#else
fd_devnull = my_posix_open<true>(reinterpret_cast<char const*>(u8"/dev/null"), O_RDWR, 0644);
fd_devnull = my_posix_open<true>(reinterpret_cast<char const *>(u8"/dev/null"), O_RDWR, 0644);
sys_fcntl(tmp_fd, F_SETFD, FD_CLOEXEC);
#endif
return fd_devnull;
Expand All @@ -376,15 +384,19 @@ inline void execveat_inside_vfork(int dirfd, char const *cstr, char const *const
{
t_errno = 0;
}
::fast_io::fast_exit(127);
#ifdef __NR_exit_group
::fast_io::system_call_no_return<__NR_exit_group>(127);
#else
int fd{noexcept_call(::openat, dirfd, cstr, O_RDONLY | O_NOFOLLOW, 0644)};
::fast_io::posix::libc_exit2(127);
#endif
#else
int fd{::fast_io::details::my_posix_openat_noexcept(dirfd, cstr, O_RDONLY | O_NOFOLLOW, 0644)};
if (fd != -1) [[likely]]
{
::fast_io::posix::libc_fexecve(fd, const_cast<char *const *>(argv), const_cast<char *const *>(envp));
::fast_io::posix::libc_fexecve(fd, const_cast<char *const *>(args), const_cast<char *const *>(envp));
}
t_errno = errno;
noexcept_call(::_exit, 127);
::fast_io::posix::libc_exit2(127);
#endif
__builtin_unreachable();
}
Expand All @@ -395,7 +407,7 @@ inline pid_t posix_vfork()
pid_t pid{system_call<__NR_vfork, pid_t>()};
system_call_throw_error(pid);
#else
pid_t pid{noexcept_call(::vfork)};
pid_t pid{::fast_io::posix::libc_vfork()};
if (pid == -1) [[unlikely]]
{
throw_posix_error();
Expand Down Expand Up @@ -517,20 +529,20 @@ class posix_process : public posix_process_observer
}
template <::fast_io::constructible_to_os_c_str path_type>
inline posix_process(posix_at_entry pate, path_type const &filename, posix_process_args const &args,
posix_process_envs const &envp, posix_process_io const &pio)
posix_process_envs const &envp, posix_process_io const &pio)
: posix_process_observer{details::vfork_execveat_impl(pate.fd, filename, args.get(), envp.get(), pio)}
{
}

template <::fast_io::constructible_to_os_c_str path_type>
inline posix_process(path_type const &filename, posix_process_args const &args, posix_process_envs const &envp,
posix_process_io const &pio)
posix_process_io const &pio)
: posix_process_observer{::fast_io::details::vfork_execve_impl(filename, args.get(), envp.get(), pio)}
{
}

inline posix_process(::fast_io::posix_fs_dirent ent, posix_process_args const &args, posix_process_envs const &envp,
posix_process_io const &pio)
posix_process_io const &pio)
: posix_process_observer{
::fast_io::details::vfork_execveat_common_impl(ent.fd, ent.filename, args.get(), envp.get(), pio)}
{
Expand Down

0 comments on commit ddcb085

Please sign in to comment.