env.ipp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright (c) 2022 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_V2_EXT_IMPL_ENV_IPP
  6. #define BOOST_PROCESS_V2_EXT_IMPL_ENV_IPP
  7. #include <boost/process/v2/detail/config.hpp>
  8. #include <boost/process/v2/detail/last_error.hpp>
  9. #include <boost/process/v2/detail/throw_error.hpp>
  10. #include <boost/process/v2/ext/detail/proc_info.hpp>
  11. #include <boost/process/v2/ext/env.hpp>
  12. #if defined(BOOST_PROCESS_V2_WINDOWS)
  13. #include <shellapi.h>
  14. #else
  15. #include <cstdlib>
  16. #endif
  17. #if (defined(__linux__) || defined(__ANDROID__))
  18. #include <cstdio>
  19. #endif
  20. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  21. namespace detail {
  22. namespace ext {
  23. #if defined(BOOST_PROCESS_V2_WINDOWS)
  24. void native_env_handle_deleter::operator()(native_env_handle_type h) const
  25. {
  26. delete [] h;
  27. }
  28. native_env_iterator next(native_env_iterator nh)
  29. {
  30. while (*nh != L'\0')
  31. nh ++;
  32. return ++nh ;
  33. }
  34. native_env_iterator find_end(native_env_iterator nh)
  35. {
  36. while (*nh - 1 != L'\0' && *nh != L'\0')
  37. nh ++;
  38. return nh ;
  39. }
  40. const environment::char_type * dereference(native_env_iterator iterator)
  41. {
  42. return iterator;
  43. }
  44. #elif (defined(__linux__) || defined(__ANDROID__))
  45. //linux stores this as a blob with an EOF at the end
  46. void native_env_handle_deleter::operator()(native_env_handle_type h) const
  47. {
  48. delete [] h;
  49. }
  50. native_env_iterator next(native_env_iterator nh)
  51. {
  52. while (*nh != '\0')
  53. nh ++;
  54. return ++nh ;
  55. }
  56. native_env_iterator find_end(native_env_iterator nh)
  57. {
  58. while (*nh != EOF)
  59. nh ++;
  60. return nh ;
  61. }
  62. const environment::char_type * dereference(native_env_iterator iterator)
  63. {
  64. return iterator;
  65. }
  66. #elif (defined(__APPLE___) || defined(__MACH__))
  67. void native_env_handle_deleter::operator()(native_env_handle_type h) const
  68. {
  69. delete [] h;
  70. }
  71. native_env_iterator next(native_env_iterator nh)
  72. {
  73. while (*nh != '\0')
  74. nh ++;
  75. return ++nh ;
  76. }
  77. native_env_iterator find_end(native_env_iterator nh)
  78. {
  79. while (*nh - 1 != '\0' && *nh != '\0')
  80. nh ++;
  81. return nh ;
  82. }
  83. const environment::char_type * dereference(native_env_iterator iterator)
  84. {
  85. return iterator;
  86. }
  87. #elif defined(__FreeBSD__)
  88. void native_env_handle_deleter::operator()(native_env_handle_type h) const
  89. {
  90. delete [] h;
  91. }
  92. native_env_iterator next(native_env_iterator nh)
  93. {
  94. return ++nh ;
  95. }
  96. native_env_iterator find_end(native_env_iterator nh)
  97. {
  98. while (*nh != nullptr)
  99. nh++;
  100. return nh ;
  101. }
  102. const environment::char_type * dereference(native_env_iterator iterator)
  103. {
  104. return *iterator;
  105. }
  106. #endif
  107. }
  108. }
  109. namespace ext
  110. {
  111. #if defined(BOOST_PROCESS_V2_WINDOWS)
  112. env_view env(HANDLE proc, boost::system::error_code & ec)
  113. {
  114. wchar_t *buffer = nullptr;
  115. PEB peb;
  116. SIZE_T nRead = 0;
  117. ULONG len = 0;
  118. PROCESS_BASIC_INFORMATION pbi;
  119. detail::ext::RTL_USER_PROCESS_PARAMETERS_EXTENDED upp;
  120. NTSTATUS status = 0;
  121. PVOID buf = nullptr;
  122. status = NtQueryInformationProcess(proc, ProcessBasicInformation, &pbi, sizeof(pbi), &len);
  123. ULONG error = RtlNtStatusToDosError(status);
  124. if (error)
  125. {
  126. BOOST_PROCESS_V2_ASSIGN_EC(ec, error, boost::system::system_category())
  127. return {};
  128. }
  129. if (!ReadProcessMemory(proc, pbi.PebBaseAddress, &peb, sizeof(peb), &nRead))
  130. {
  131. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  132. return {};
  133. }
  134. if (!ReadProcessMemory(proc, peb.ProcessParameters, &upp, sizeof(upp), &nRead))
  135. {
  136. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  137. return {};
  138. }
  139. env_view ev;
  140. buf = upp.Environment;
  141. len = (ULONG)upp.EnvironmentSize;
  142. ev.handle_.reset(new wchar_t[len / 2 + 1]());
  143. if (!ReadProcessMemory(proc, buf, ev.handle_.get(), len, &nRead))
  144. {
  145. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  146. return {};
  147. }
  148. ev.handle_.get()[len / 2] = L'\0';
  149. return ev;
  150. }
  151. env_view env(HANDLE handle)
  152. {
  153. boost::system::error_code ec;
  154. auto res = env(handle, ec);
  155. if (ec)
  156. detail::throw_error(ec, "env");
  157. return res;
  158. }
  159. env_view env(boost::process::v2::pid_type pid, boost::system::error_code & ec)
  160. {
  161. struct del
  162. {
  163. void operator()(HANDLE h)
  164. {
  165. ::CloseHandle(h);
  166. };
  167. };
  168. std::unique_ptr<void, del> proc{detail::ext::open_process_with_debug_privilege(pid, ec)};
  169. if (proc == nullptr)
  170. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  171. else
  172. return env(proc.get(), ec);
  173. return {};
  174. }
  175. #elif (defined(__APPLE___) || defined(__MACH__))
  176. env_view env(boost::process::v2::pid_type pid, boost::system::error_code & ec)
  177. {
  178. int mib[3] = {CTL_KERN, KERN_ARGMAX, 0};
  179. int argmax = 0;
  180. auto size = sizeof(argmax);
  181. if (sysctl(mib, 2, &argmax, &size, nullptr, 0) == -1)
  182. {
  183. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  184. return {};
  185. }
  186. std::string procargs;
  187. procargs.resize(argmax - 1);
  188. mib[1] = KERN_PROCARGS2;
  189. mib[2] = pid;
  190. size = argmax;
  191. if (sysctl(mib, 3, &*procargs.begin(), &size, nullptr, 0) != 0)
  192. {
  193. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  194. return {};
  195. }
  196. std::uint32_t nargs;
  197. memcpy(&nargs, &*procargs.begin(), sizeof(nargs));
  198. char *cp = &*procargs.begin() + sizeof(nargs);
  199. for (; cp < &*procargs.end(); cp++)
  200. if (*cp == '\0')
  201. break;
  202. if (cp == &procargs[size])
  203. return {};
  204. for (; cp < &*procargs.end(); cp++)
  205. if (*cp != '\0') break;
  206. if (cp == &*procargs.end())
  207. return {};
  208. int i = 0;
  209. char *sp = cp;
  210. std::vector<char> vec;
  211. while ((*sp != '\0' || i < nargs) && sp < &*procargs.end()) {
  212. if (i >= nargs)
  213. vec.push_back(*sp);
  214. sp += 1;
  215. }
  216. env_view ev;
  217. ev.handle_.reset(new char[vec.size()]());
  218. std::copy(vec.begin(), vec.end(), ev.handle_.get());
  219. return ev;
  220. }
  221. #elif (defined(__linux__) || defined(__ANDROID__))
  222. env_view env(boost::process::v2::pid_type pid, boost::system::error_code & ec)
  223. {
  224. std::size_t size = 0;
  225. std::unique_ptr<char, detail::ext::native_env_handle_deleter> procargs{};
  226. int f = ::open(("/proc/" + std::to_string(pid) + "/environ").c_str(), O_RDONLY);
  227. while (!procargs || procargs.get()[size - 1] != EOF)
  228. {
  229. std::unique_ptr<char, detail::ext::native_env_handle_deleter> buf{new char[size + 4096]};
  230. if (size > 0)
  231. std::memmove(buf.get(), procargs.get(), size);
  232. auto r = ::read(f, buf.get() + size, 4096);
  233. if (r < 0)
  234. {
  235. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  236. ::close(f);
  237. return {};
  238. }
  239. procargs = std::move(buf);
  240. size += r;
  241. if (r < 4096) // done!
  242. {
  243. procargs.get()[size] = EOF;
  244. break;
  245. }
  246. }
  247. ::close(f);
  248. env_view ev;
  249. ev.handle_ = std::move(procargs);
  250. return ev;
  251. }
  252. #elif defined(__FreeBSD__)
  253. env_view env(boost::process::v2::pid_type pid, boost::system::error_code & ec)
  254. {
  255. env_view ev;
  256. unsigned cntp = 0;
  257. procstat *proc_stat = procstat_open_sysctl();
  258. if (proc_stat != nullptr)
  259. {
  260. kinfo_proc *proc_info = procstat_getprocs(proc_stat, KERN_PROC_PID, pid, &cntp);
  261. if (proc_info != nullptr)
  262. {
  263. char **env = procstat_getenvv(proc_stat, proc_info, 0);
  264. if (env != nullptr)
  265. {
  266. auto e = env;
  267. std::size_t n = 0u, len = 0u;
  268. while (e && *e != nullptr)
  269. {
  270. n ++;
  271. len += std::strlen(*e);
  272. e++;
  273. }
  274. std::size_t mem_needed =
  275. // environ - nullptr - strlen + null terminators
  276. (n * sizeof(char*)) + sizeof(char*) + len + n;
  277. char * out = new (std::nothrow) char[mem_needed];
  278. if (out != nullptr)
  279. {
  280. auto eno = reinterpret_cast<char**>(out);
  281. auto eeo = eno;
  282. auto str = out + (n * sizeof(char*)) + sizeof(char*);
  283. e = env;
  284. while (*e != nullptr)
  285. {
  286. auto len = std::strlen(*e) + 1u;
  287. std::memcpy(str, *e, len);
  288. *eno = str;
  289. str += len;
  290. eno ++;
  291. e++;
  292. }
  293. *eno = nullptr;
  294. ev.handle_.reset(eeo);
  295. }
  296. else
  297. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  298. }
  299. procstat_freeprocs(proc_stat, proc_info);
  300. }
  301. else
  302. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  303. procstat_close(proc_stat);
  304. }
  305. else
  306. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  307. return ev;
  308. }
  309. #endif
  310. env_view env(boost::process::v2::pid_type pid)
  311. {
  312. boost::system::error_code ec;
  313. auto res = env(pid, ec);
  314. if (ec)
  315. detail::throw_error(ec, "env");
  316. return res;
  317. }
  318. }
  319. BOOST_PROCESS_V2_END_NAMESPACE
  320. #endif //BOOST_PROCESS_V2_EXT_IMPL_ENV_IPP