From 74da6272294166018ec95dc2334aa6e04e6bda7f Mon Sep 17 00:00:00 2001 From: Samuel Venable Date: Mon, 5 Aug 2024 19:53:05 -0400 Subject: [PATCH 1/4] [DragonFly BSD] Use Proper CWD From PID Code --- src/ext/cwd.cpp | 53 ++++++++----------------------------------------- 1 file changed, 8 insertions(+), 45 deletions(-) diff --git a/src/ext/cwd.cpp b/src/ext/cwd.cpp index b88a4df26..3345d33b9 100644 --- a/src/ext/cwd.cpp +++ b/src/ext/cwd.cpp @@ -46,8 +46,10 @@ #endif #if defined(__DragonFly__) +#include #include -#include +#include +#include #endif #ifdef BOOST_PROCESS_USE_STD_FS @@ -152,53 +154,14 @@ filesystem::path cwd(boost::process::v2::pid_type pid, boost::system::error_code filesystem::path cwd(boost::process::v2::pid_type pid, boost::system::error_code & ec) { - /* filesystem::path path; - // Official API (broken OS-level) - including code from DragonFly's fstat(1) - // command line interface utility currently requires way too much code FWIW. - std::size_t sz = 4, len = 0; + char buffer[PATH_MAX]; + std::size_t sz = 4, len = sizeof(buffer); int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_CWD, pid}; - if (sysctl(mib, sz, nullptr, &len, nullptr, 0) == 0) - { - std::vector vecbuff; - vecbuff.resize(len); - if (sysctl(mib, sz, &vecbuff[0], &len, nullptr, 0) == 0) - { - path = filesystem::canonical(&vecbuff[0], ec); - } - else - BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) - } - else - BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) - return path; - */ - - filesystem::path path; - /* Probably the hackiest thing ever we are doing here, because the official API is broken OS-level. */ - FILE *fp = popen(("pos=`ans=\\`/usr/bin/fstat -w -p " + std::to_string(pid) + " | /usr/bin/sed -n 1p\\`; " + - "/usr/bin/awk -v ans=\"$ans\" 'BEGIN{print index(ans, \"INUM\")}'`; str=`/usr/bin/fstat -w -p " + - std::to_string(pid) + " | /usr/bin/sed -n 3p`; /usr/bin/awk -v str=\"$str\" -v pos=\"$pos\" " + - "'BEGIN{print substr(str, 0, pos + 4)}' | /usr/bin/awk 'NF{NF--};1 {$1=$2=$3=$4=\"\"; print" + - " substr($0, 5)'}").c_str(), "r"); - if (fp) + if (sysctl(mib, sz, buffer, &len, nullptr, 0) == 0) { - char buffer[PATH_MAX]; - if (fgets(buffer, sizeof(buffer), fp)) - { - std::string str = buffer; - std::size_t pos = str.find("\n", strlen(buffer) - 1); - if (pos != std::string::npos) - { - str.replace(pos, 1, ""); - } - path = filesystem::canonical(str.c_str(), ec); - } - else - BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) - if (pclose(fp) == -1) - BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) - } + path = filesystem::canonical(buffer, ec); + } else BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) return path; From 5674e6948aa601dd44b1de11c98d856869cc8383 Mon Sep 17 00:00:00 2001 From: Samuel Venable Date: Thu, 26 Sep 2024 22:13:24 -0600 Subject: [PATCH 2/4] yay --- test/v2/Jamfile.jam | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/v2/Jamfile.jam b/test/v2/Jamfile.jam index 3b7769601..0e930000b 100644 --- a/test/v2/Jamfile.jam +++ b/test/v2/Jamfile.jam @@ -24,6 +24,9 @@ project : requirements freebsd:-lkvm freebsd:-lprocstat bsd:-lkvm + netbsd:-lkvm + openbsd:-lkvm + solaris:-lkvm NT,cw:ws2_32 NT,gcc:ws2_32 NT,gcc:Bcrypt From c7bfe991242290c154aaebf4aace0350df435d5a Mon Sep 17 00:00:00 2001 From: Samuel Venable Date: Thu, 26 Sep 2024 22:14:18 -0600 Subject: [PATCH 3/4] Initial Commit --- include/boost/process/v2/posix/default_launcher.hpp | 2 +- src/ext/cmd.cpp | 4 ++-- src/pid.cpp | 4 ++-- src/posix/close_handles.cpp | 3 +-- src/shell.cpp | 6 +++--- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/boost/process/v2/posix/default_launcher.hpp b/include/boost/process/v2/posix/default_launcher.hpp index 9cfec4b2a..09f8ce428 100644 --- a/include/boost/process/v2/posix/default_launcher.hpp +++ b/include/boost/process/v2/posix/default_launcher.hpp @@ -29,7 +29,7 @@ #include -#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__MACH__) +#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) extern "C" { extern char **environ; } #endif diff --git a/src/ext/cmd.cpp b/src/ext/cmd.cpp index d74a06ef4..58e16db5e 100644 --- a/src/ext/cmd.cpp +++ b/src/ext/cmd.cpp @@ -343,7 +343,7 @@ shell cmd(boost::process::v2::pid_type pid, boost::system::error_code & ec) }; std::unique_ptr kd{kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, nullptr)}; - if (!kd) {BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) return vec;} + if (!kd.get()) {BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) return {};} if ((proc_info = kvm_getprocs(kd.get(), KERN_PROC_PID, pid, sizeof(struct kinfo_proc), &cntp))) { char **cmd = kvm_getargv(kd.get(), proc_info, 0); @@ -354,7 +354,7 @@ shell cmd(boost::process::v2::pid_type pid, boost::system::error_code & ec) } else BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) - kvm_close(kd); + kvm_close(kd.get()); return {}; } diff --git a/src/pid.cpp b/src/pid.cpp index f32df9ecc..574a38cf7 100644 --- a/src/pid.cpp +++ b/src/pid.cpp @@ -551,9 +551,9 @@ std::vector all_pids(boost::system::error_code & ec) vec.reserve(cntp); for (int i = cntp - 1; i >= 0; i--) { - if (proc_info[i].kp_pid >= 0) + if (proc_info[i].p_pid >= 0) { - vec.push_back(proc_info[i].kp_pid); + vec.push_back(proc_info[i].p_pid); } } } diff --git a/src/posix/close_handles.cpp b/src/posix/close_handles.cpp index 39d63e6dd..67b6381d4 100644 --- a/src/posix/close_handles.cpp +++ b/src/posix/close_handles.cpp @@ -17,10 +17,9 @@ // linux has close_range since 5.19 -#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) // https://www.freebsd.org/cgi/man.cgi?query=close_range&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html -// https://man.netbsd.org/closefrom.3 // __FreeBSD__ // // gives us diff --git a/src/shell.cpp b/src/shell.cpp index 0fffc501a..a03d30096 100644 --- a/src/shell.cpp +++ b/src/shell.cpp @@ -19,7 +19,7 @@ #if defined(BOOST_PROCESS_V2_WINDOWS) #include #include -#else +#elif defined(__OpenBSD__) #include #endif @@ -30,7 +30,7 @@ BOOST_PROCESS_V2_DECL const error_category& get_shell_category() { return system_category(); } -#else +#elif defined(__OpenBSD__) struct shell_category_t final : public error_category { @@ -92,7 +92,7 @@ auto shell::args() const-> args_type return input_.c_str(); } -#else +#elif defined(__OpenBSD__) void shell::parse_() { From 8f762f2c7e6206ec402b5dcd7429a5f025d2f6cd Mon Sep 17 00:00:00 2001 From: Samuel Venable Date: Thu, 26 Sep 2024 22:23:06 -0600 Subject: [PATCH 4/4] foobar --- src/shell.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shell.cpp b/src/shell.cpp index a03d30096..9bb7501c2 100644 --- a/src/shell.cpp +++ b/src/shell.cpp @@ -19,7 +19,7 @@ #if defined(BOOST_PROCESS_V2_WINDOWS) #include #include -#elif defined(__OpenBSD__) +#elif !defined(__OpenBSD__) #include #endif @@ -30,7 +30,7 @@ BOOST_PROCESS_V2_DECL const error_category& get_shell_category() { return system_category(); } -#elif defined(__OpenBSD__) +#elif !defined(__OpenBSD__) struct shell_category_t final : public error_category { @@ -92,7 +92,7 @@ auto shell::args() const-> args_type return input_.c_str(); } -#elif defined(__OpenBSD__) +#elif !defined(__OpenBSD__) void shell::parse_() {