From d298059f9f0cc0ba8243f59ae10e83588d3ff635 Mon Sep 17 00:00:00 2001 From: aleck099 Date: Fri, 13 Dec 2024 21:53:38 +0800 Subject: [PATCH 1/2] new style posix_api_return_code in replacement of previous return_code --- .../platforms/systemcall_details.h | 30 ++++++++++++++----- .../fast_io_hosted/process/process/posix.h | 20 ++++++------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/include/fast_io_hosted/platforms/systemcall_details.h b/include/fast_io_hosted/platforms/systemcall_details.h index 9b45758d..bc5db761 100644 --- a/include/fast_io_hosted/platforms/systemcall_details.h +++ b/include/fast_io_hosted/platforms/systemcall_details.h @@ -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( @@ -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 diff --git a/include/fast_io_hosted/process/process/posix.h b/include/fast_io_hosted/process/process/posix.h index a79985da..75bf7005 100644 --- a/include/fast_io_hosted/process/process/posix.h +++ b/include/fast_io_hosted/process/process/posix.h @@ -174,16 +174,16 @@ 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) + if (!rc) { return rc; } rc = redirect(1, pio.out); - if (rc.error) + if (!rc) { return rc; } @@ -191,20 +191,20 @@ struct io_redirector 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) { 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; } @@ -219,7 +219,7 @@ struct io_redirector { rc = sys_dup2_nothrow(d.fd, target_fd); } - if (rc.error) + if (!rc) { return rc; } @@ -272,9 +272,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; } } From 76bf59635ef11b6b76d6235010e187ebce46cb69 Mon Sep 17 00:00:00 2001 From: aleck099 Date: Fri, 13 Dec 2024 21:55:51 +0800 Subject: [PATCH 2/2] Re-use vfork for posix_process on darwin --- include/fast_io_hosted/process/process/posix.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/fast_io_hosted/process/process/posix.h b/include/fast_io_hosted/process/process/posix.h index 75bf7005..4e05044b 100644 --- a/include/fast_io_hosted/process/process/posix.h +++ b/include/fast_io_hosted/process/process/posix.h @@ -604,7 +604,8 @@ class posix_process : public posix_process_observer 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) @@ -617,7 +618,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) @@ -629,7 +631,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)