File indexing completed on 2025-01-18 09:50:10
0001
0002
0003
0004
0005
0006 #ifndef BOOST_PROCESS_V2_IMPL_DETAIL_PROC_INFO_IPP
0007 #define BOOST_PROCESS_V2_IMPL_DETAIL_PROC_INFO_IPP
0008
0009 #include <boost/process/v2/detail/config.hpp>
0010 #include <boost/process/v2/detail/last_error.hpp>
0011 #include <boost/process/v2/detail/throw_error.hpp>
0012 #include <boost/process/v2/ext/detail/proc_info.hpp>
0013
0014 #include <string>
0015
0016 #if (defined(__APPLE__) && defined(__MACH__))
0017 #include <cstdlib>
0018 #include <sys/types.h>
0019 #include <sys/sysctl.h>
0020 #include <sys/proc_info.h>
0021 #include <libproc.h>
0022 #endif
0023
0024 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0025
0026 namespace detail
0027 {
0028
0029 namespace ext
0030 {
0031
0032 #if defined(BOOST_PROCESS_V2_WINDOWS)
0033
0034 enum MEMTYP {MEMCMD, MEMCWD};
0035 std::wstring cwd_cmd_from_proc(HANDLE proc, int type, boost::system::error_code & ec)
0036 {
0037 std::wstring buffer;
0038 PEB peb;
0039 SIZE_T nRead = 0;
0040 ULONG len = 0;
0041 PROCESS_BASIC_INFORMATION pbi;
0042 RTL_USER_PROCESS_PARAMETERS_EXTENDED upp;
0043
0044 NTSTATUS status = 0;
0045 PVOID buf = nullptr;
0046 status = NtQueryInformationProcess(proc, ProcessBasicInformation, &pbi, sizeof(pbi), &len);
0047 ULONG error = RtlNtStatusToDosError(status);
0048
0049 if (error)
0050 {
0051 BOOST_PROCESS_V2_ASSIGN_EC(ec, error, boost::system::system_category())
0052 return {};
0053 }
0054
0055 if (!ReadProcessMemory(proc, pbi.PebBaseAddress, &peb, sizeof(peb), &nRead))
0056 {
0057 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0058 return {};
0059 }
0060
0061 if (!ReadProcessMemory(proc, peb.ProcessParameters, &upp, sizeof(upp), &nRead))
0062 {
0063 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0064 return {};
0065 }
0066
0067 if (type == MEMCWD)
0068 {
0069 buf = upp.CurrentDirectory.DosPath.Buffer;
0070 len = upp.CurrentDirectory.DosPath.Length;
0071 }
0072 else if (type == MEMCMD)
0073 {
0074 buf = upp.CommandLine.Buffer;
0075 len = upp.CommandLine.Length;
0076 }
0077
0078 buffer.resize(len / 2 + 1);
0079
0080 if (!ReadProcessMemory(proc, buf, &buffer[0], len, &nRead))
0081 {
0082 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0083 return {};
0084 }
0085
0086 buffer.pop_back();
0087 return buffer;
0088 }
0089
0090
0091
0092 HANDLE open_process_with_debug_privilege(boost::process::v2::pid_type pid, boost::system::error_code & ec)
0093 {
0094 HANDLE proc = nullptr;
0095 HANDLE hToken = nullptr;
0096 LUID luid;
0097 TOKEN_PRIVILEGES tkp;
0098 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
0099 {
0100 if (LookupPrivilegeValue(nullptr, SE_DEBUG_NAME, &luid))
0101 {
0102 tkp.PrivilegeCount = 1;
0103 tkp.Privileges[0].Luid = luid;
0104 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
0105 if (AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), nullptr, nullptr))
0106 {
0107 proc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
0108 }
0109 }
0110 CloseHandle(hToken);
0111 }
0112 if (!proc)
0113 proc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
0114 if (!proc)
0115 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0116 return proc;
0117 }
0118 #endif
0119
0120 }
0121
0122 }
0123
0124 BOOST_PROCESS_V2_END_NAMESPACE
0125
0126 #endif
0127