Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroModel committed Dec 14, 2024
2 parents 80b4665 + 76bf596 commit cb4ea8a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
30 changes: 23 additions & 7 deletions include/fast_io_hosted/platforms/systemcall_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,35 @@ inline int sys_dup2(int old_fd, int new_fd)
#endif
}

struct return_code
struct posix_api_return_code
{
int code{};
bool error{};
int value{};
int err{};

// false if there is an error, otherwise true
inline operator bool() const
{
return err == 0;
}

static inline posix_api_return_code fd(int fd)
{
return {fd, 0};
}

static inline posix_api_return_code error(int errnum)
{
return {-1, errnum};
}
};

inline return_code sys_dup2_nothrow(int old_fd, int new_fd) noexcept
inline posix_api_return_code sys_dup2_nothrow(int old_fd, int new_fd) noexcept
{
#if defined(__linux__) && defined(__NR_dup2)
int fd{system_call<__NR_dup2, int>(old_fd, new_fd)};
if (linux_system_call_fails(fd))
{
return {-fd, true};
return posix_api_return_code::error(-fd);
}
#else
auto fd{noexcept_call(
Expand All @@ -98,10 +114,10 @@ inline return_code sys_dup2_nothrow(int old_fd, int new_fd) noexcept
old_fd, new_fd)};
if (fd == -1)
{
return {errno, true};
return posix_api_return_code::error(errno);
}
#endif
return {fd};
return posix_api_return_code::fd(fd);
}

inline int sys_close(int fd) noexcept
Expand Down
58 changes: 32 additions & 26 deletions include/fast_io_hosted/process/process/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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_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");
Expand All @@ -18,7 +17,6 @@ extern pid_t libc_waitpid(pid_t pid, int *status, int options) noexcept __asm__(
[[noreturn]] extern void libc_exit(int status) noexcept __asm__("__Exit");
[[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_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");
Expand All @@ -33,6 +31,7 @@ struct posix_wait_status
{
int wait_loc{};
};

#if 0
inline constexpr posix_wait_reason reason(posix_wait_status pws) noexcept
{
Expand Down Expand Up @@ -104,7 +103,6 @@ namespace details
{
inline pid_t posix_fork()
{

#if defined(__linux__) && defined(__NR_fork)
pid_t pid{system_call<__NR_fork, pid_t>()};
system_call_throw_error(pid);
Expand Down Expand Up @@ -175,37 +173,37 @@ struct io_redirector
}

// only used by sub process
inline return_code redirect_all(posix_process_io const &pio) noexcept
inline posix_api_return_code redirect_all(posix_process_io const &pio) noexcept
{
return_code rc;
posix_api_return_code rc;
rc = redirect(0, pio.in);
if (rc.error) [[unlikely]]
if (!rc) [[unlikely]]
{
return rc;
}
rc = redirect(1, pio.out);
if (rc.error) [[unlikely]]
if (!rc) [[unlikely]]
{
return rc;
}
rc = redirect(2, pio.err);
return rc;
}

inline return_code redirect(int target_fd, posix_io_redirection const &d) noexcept
inline posix_api_return_code redirect(int target_fd, posix_io_redirection const &d) noexcept
{
if (!d)
if (!d) [[unlikely]]
{
return {};
}
bool const is_stdin{target_fd == 0};
return_code rc;
posix_api_return_code rc;
if (d.pipe_fds)
{
// the read/write ends of pipe are all open
// the user shouldn't close them if they pass entire pipe as argument
rc = sys_dup2_nothrow(d.pipe_fds[is_stdin ? 0 : 1], target_fd);
if (rc.error)
if (!rc)
{
return rc;
}
Expand All @@ -220,7 +218,7 @@ struct io_redirector
{
rc = sys_dup2_nothrow(d.fd, target_fd);
}
if (rc.error) [[unlikely]]
if (!rc) [[unlikely]]
{
return rc;
}
Expand All @@ -242,7 +240,6 @@ struct io_redirector
sys_close(d.pipe_fds[is_stdin ? 0 : 1]);
}

private:
inline int devnull()
{
if (fd_devnull != -1)
Expand Down Expand Up @@ -273,9 +270,9 @@ inline pid_t pipefork_execveat_common_impl(int dirfd, char const *cstr, char con
{
io_redirector r;
auto rc = r.redirect_all(pio);
if (rc.error)
if (!rc)
{
t_errno = rc.code;
t_errno = rc.value;
}
}

Expand Down Expand Up @@ -397,7 +394,7 @@ struct fd_remapper
// fd in {0, 1, 2}
inline void map(int fd, posix_io_redirection const &io)
{
if (!io)
if (!io) [[unlikely]]
{
return;
}
Expand All @@ -424,7 +421,6 @@ struct fd_remapper
}
}

private:
inline int devnull()
{
if (fd_devnull != -1)
Expand Down Expand Up @@ -454,23 +450,27 @@ inline void execveat_inside_vfork(int dirfd, char const *cstr, char const *const
{
t_errno = 0;
}
#if defined(__linux__)
#ifdef __NR_exit_group
#if defined(__NR_exit_group)
::fast_io::system_call_no_return<__NR_exit_group>(127);
#else
::fast_io::system_call_no_return<__NR_exit>(127);
#endif
#else
::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 *>(args), const_cast<char *const *>(envp));
}
t_errno = errno;
#if defined(__linux__)
#if defined(__NR_exit_group)
::fast_io::system_call_no_return<__NR_exit_group>(127);
#else
::fast_io::system_call_no_return<__NR_exit>(127);
#endif
#else
::fast_io::posix::libc_exit2(127);
#endif
#endif
__builtin_unreachable();
}
Expand Down Expand Up @@ -594,18 +594,22 @@ class posix_process : public posix_process_observer
{
public:
using native_handle_type = pid_t;

inline explicit constexpr posix_process() noexcept = default;

template <typename native_hd>
requires ::std::same_as<native_handle_type, ::std::remove_cvref_t<native_hd>>
inline explicit constexpr posix_process(native_hd pid1) noexcept
: posix_process_observer{pid1}
{
}

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_observer{
#ifdef __DARWIN_C_LEVEL
//#ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execveat_impl(pate.fd, filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execveat_impl(pate.fd, filename, args.get(), envp.get(), pio)
Expand All @@ -618,7 +622,8 @@ class posix_process : public posix_process_observer
inline posix_process(path_type const &filename, posix_process_args const &args, posix_process_envs const &envp,
posix_process_io const &pio)
: posix_process_observer{
#ifdef __DARWIN_C_LEVEL
//#ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execve_impl(filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execve_impl(filename, args.get(), envp.get(), pio)
Expand All @@ -630,7 +635,8 @@ class posix_process : public posix_process_observer
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_observer{
#ifdef __DARWIN_C_LEVEL
//#ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execveat_common_impl(ent.fd, ent.filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execveat_common_impl(ent.fd, ent.filename, args.get(), envp.get(), pio)
Expand All @@ -652,7 +658,7 @@ class posix_process : public posix_process_observer
{
return *this;
}
::fast_io::details::posix_waitpid_noexcept(this->pid);
details::posix_waitpid_noexcept(this->pid);
this->pid = other.pid;
other.pid = -1;
return *this;
Expand Down

0 comments on commit cb4ea8a

Please sign in to comment.