1
0

subprocess.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. The latest version of this library is available on GitHub;
  3. https://github.com/sheredom/subprocess.h
  4. */
  5. /*
  6. This is free and unencumbered software released into the public domain.
  7. Anyone is free to copy, modify, publish, use, compile, sell, or
  8. distribute this software, either in source code form or as a compiled
  9. binary, for any purpose, commercial or non-commercial, and by any
  10. means.
  11. In jurisdictions that recognize copyright laws, the author or authors
  12. of this software dedicate any and all copyright interest in the
  13. software to the public domain. We make this dedication for the benefit
  14. of the public at large and to the detriment of our heirs and
  15. successors. We intend this dedication to be an overt act of
  16. relinquishment in perpetuity of all present and future rights to this
  17. software under copyright law.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25. For more information, please refer to <http://unlicense.org/>
  26. */
  27. #ifndef SHEREDOM_SUBPROCESS_H_INCLUDED
  28. #define SHEREDOM_SUBPROCESS_H_INCLUDED
  29. #if defined(_MSC_VER)
  30. #pragma warning(push, 1)
  31. /* disable warning: '__cplusplus' is not defined as a preprocessor macro,
  32. * replacing with '0' for '#if/#elif' */
  33. #pragma warning(disable : 4668)
  34. #endif
  35. #include <stdio.h>
  36. #include <string.h>
  37. #if defined(_MSC_VER)
  38. #pragma warning(pop)
  39. #endif
  40. #if defined(__TINYC__)
  41. #define SUBPROCESS_ATTRIBUTE(a) __attribute((a))
  42. #else
  43. #define SUBPROCESS_ATTRIBUTE(a) __attribute__((a))
  44. #endif
  45. #if defined(_MSC_VER)
  46. #define subprocess_pure
  47. #define subprocess_weak __inline
  48. #define subprocess_tls __declspec(thread)
  49. #elif defined(__MINGW32__)
  50. #define subprocess_pure SUBPROCESS_ATTRIBUTE(pure)
  51. #define subprocess_weak static SUBPROCESS_ATTRIBUTE(used)
  52. #define subprocess_tls __thread
  53. #elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__)
  54. #define subprocess_pure SUBPROCESS_ATTRIBUTE(pure)
  55. #define subprocess_weak SUBPROCESS_ATTRIBUTE(weak)
  56. #define subprocess_tls __thread
  57. #else
  58. #error Non clang, non gcc, non MSVC compiler found!
  59. #endif
  60. struct subprocess_s;
  61. enum subprocess_option_e {
  62. // stdout and stderr are the same FILE.
  63. subprocess_option_combined_stdout_stderr = 0x1,
  64. // The child process should inherit the environment variables of the parent.
  65. subprocess_option_inherit_environment = 0x2,
  66. // Enable asynchronous reading of stdout/stderr before it has completed.
  67. subprocess_option_enable_async = 0x4,
  68. // Enable the child process to be spawned with no window visible if supported
  69. // by the platform.
  70. subprocess_option_no_window = 0x8,
  71. // Search for program names in the PATH variable. Always enabled on Windows.
  72. // Note: this will **not** search for paths in any provided custom environment
  73. // and instead uses the PATH of the spawning process.
  74. subprocess_option_search_user_path = 0x10
  75. };
  76. #if defined(__cplusplus)
  77. extern "C" {
  78. #endif
  79. /// @brief Create a process.
  80. /// @param command_line An array of strings for the command line to execute for
  81. /// this process. The last element must be NULL to signify the end of the array.
  82. /// The memory backing this parameter only needs to persist until this function
  83. /// returns.
  84. /// @param options A bit field of subprocess_option_e's to pass.
  85. /// @param out_process The newly created process.
  86. /// @return On success zero is returned.
  87. subprocess_weak int subprocess_create(const char *const command_line[],
  88. int options,
  89. struct subprocess_s *const out_process);
  90. /// @brief Create a process (extended create).
  91. /// @param command_line An array of strings for the command line to execute for
  92. /// this process. The last element must be NULL to signify the end of the array.
  93. /// The memory backing this parameter only needs to persist until this function
  94. /// returns.
  95. /// @param options A bit field of subprocess_option_e's to pass.
  96. /// @param environment An optional array of strings for the environment to use
  97. /// for a child process (each element of the form FOO=BAR). The last element
  98. /// must be NULL to signify the end of the array.
  99. /// @param out_process The newly created process.
  100. /// @return On success zero is returned.
  101. ///
  102. /// If `options` contains `subprocess_option_inherit_environment`, then
  103. /// `environment` must be NULL.
  104. subprocess_weak int
  105. subprocess_create_ex(const char *const command_line[], int options,
  106. const char *const environment[],
  107. struct subprocess_s *const out_process);
  108. /// @brief Get the standard input file for a process.
  109. /// @param process The process to query.
  110. /// @return The file for standard input of the process.
  111. ///
  112. /// The file returned can be written to by the parent process to feed data to
  113. /// the standard input of the process.
  114. subprocess_pure subprocess_weak FILE *
  115. subprocess_stdin(const struct subprocess_s *const process);
  116. /// @brief Get the standard output file for a process.
  117. /// @param process The process to query.
  118. /// @return The file for standard output of the process.
  119. ///
  120. /// The file returned can be read from by the parent process to read data from
  121. /// the standard output of the child process.
  122. subprocess_pure subprocess_weak FILE *
  123. subprocess_stdout(const struct subprocess_s *const process);
  124. /// @brief Get the standard error file for a process.
  125. /// @param process The process to query.
  126. /// @return The file for standard error of the process.
  127. ///
  128. /// The file returned can be read from by the parent process to read data from
  129. /// the standard error of the child process.
  130. ///
  131. /// If the process was created with the subprocess_option_combined_stdout_stderr
  132. /// option bit set, this function will return NULL, and the subprocess_stdout
  133. /// function should be used for both the standard output and error combined.
  134. subprocess_pure subprocess_weak FILE *
  135. subprocess_stderr(const struct subprocess_s *const process);
  136. /// @brief Wait for a process to finish execution.
  137. /// @param process The process to wait for.
  138. /// @param out_return_code The return code of the returned process (can be
  139. /// NULL).
  140. /// @return On success zero is returned.
  141. ///
  142. /// Joining a process will close the stdin pipe to the process.
  143. subprocess_weak int subprocess_join(struct subprocess_s *const process,
  144. int *const out_return_code);
  145. /// @brief Destroy a previously created process.
  146. /// @param process The process to destroy.
  147. /// @return On success zero is returned.
  148. ///
  149. /// If the process to be destroyed had not finished execution, it may out live
  150. /// the parent process.
  151. subprocess_weak int subprocess_destroy(struct subprocess_s *const process);
  152. /// @brief Terminate a previously created process.
  153. /// @param process The process to terminate.
  154. /// @return On success zero is returned.
  155. ///
  156. /// If the process to be destroyed had not finished execution, it will be
  157. /// terminated (i.e killed).
  158. subprocess_weak int subprocess_terminate(struct subprocess_s *const process);
  159. /// @brief Read the standard output from the child process.
  160. /// @param process The process to read from.
  161. /// @param buffer The buffer to read into.
  162. /// @param size The maximum number of bytes to read.
  163. /// @return The number of bytes actually read into buffer. Can only be 0 if the
  164. /// process has complete.
  165. ///
  166. /// The only safe way to read from the standard output of a process during it's
  167. /// execution is to use the `subprocess_option_enable_async` option in
  168. /// conjunction with this method.
  169. subprocess_weak unsigned
  170. subprocess_read_stdout(struct subprocess_s *const process, char *const buffer,
  171. unsigned size);
  172. /// @brief Read the standard error from the child process.
  173. /// @param process The process to read from.
  174. /// @param buffer The buffer to read into.
  175. /// @param size The maximum number of bytes to read.
  176. /// @return The number of bytes actually read into buffer. Can only be 0 if the
  177. /// process has complete.
  178. ///
  179. /// The only safe way to read from the standard error of a process during it's
  180. /// execution is to use the `subprocess_option_enable_async` option in
  181. /// conjunction with this method.
  182. subprocess_weak unsigned
  183. subprocess_read_stderr(struct subprocess_s *const process, char *const buffer,
  184. unsigned size);
  185. /// @brief Returns if the subprocess is currently still alive and executing.
  186. /// @param process The process to check.
  187. /// @return If the process is still alive non-zero is returned.
  188. subprocess_weak int subprocess_alive(struct subprocess_s *const process);
  189. #if defined(__cplusplus)
  190. #define SUBPROCESS_CAST(type, x) static_cast<type>(x)
  191. #define SUBPROCESS_PTR_CAST(type, x) reinterpret_cast<type>(x)
  192. #define SUBPROCESS_CONST_CAST(type, x) const_cast<type>(x)
  193. #define SUBPROCESS_NULL NULL
  194. #else
  195. #define SUBPROCESS_CAST(type, x) ((type)(x))
  196. #define SUBPROCESS_PTR_CAST(type, x) ((type)(x))
  197. #define SUBPROCESS_CONST_CAST(type, x) ((type)(x))
  198. #define SUBPROCESS_NULL 0
  199. #endif
  200. #if !defined(_WIN32)
  201. #include <signal.h>
  202. #include <spawn.h>
  203. #include <stdlib.h>
  204. #include <sys/types.h>
  205. #include <sys/wait.h>
  206. #include <unistd.h>
  207. #endif
  208. #if defined(_WIN32)
  209. #if (_MSC_VER < 1920)
  210. #ifdef _WIN64
  211. typedef __int64 subprocess_intptr_t;
  212. typedef unsigned __int64 subprocess_size_t;
  213. #else
  214. typedef int subprocess_intptr_t;
  215. typedef unsigned int subprocess_size_t;
  216. #endif
  217. #else
  218. #include <inttypes.h>
  219. typedef intptr_t subprocess_intptr_t;
  220. typedef size_t subprocess_size_t;
  221. #endif
  222. #ifdef __clang__
  223. #pragma clang diagnostic push
  224. #pragma clang diagnostic ignored "-Wreserved-identifier"
  225. #endif
  226. typedef struct _PROCESS_INFORMATION *LPPROCESS_INFORMATION;
  227. typedef struct _SECURITY_ATTRIBUTES *LPSECURITY_ATTRIBUTES;
  228. typedef struct _STARTUPINFOA *LPSTARTUPINFOA;
  229. typedef struct _OVERLAPPED *LPOVERLAPPED;
  230. #ifdef __clang__
  231. #pragma clang diagnostic pop
  232. #endif
  233. #ifdef _MSC_VER
  234. #pragma warning(push, 1)
  235. #endif
  236. #ifdef __MINGW32__
  237. #pragma GCC diagnostic push
  238. #pragma GCC diagnostic ignored "-Wpedantic"
  239. #endif
  240. struct subprocess_subprocess_information_s {
  241. void *hProcess;
  242. void *hThread;
  243. unsigned long dwProcessId;
  244. unsigned long dwThreadId;
  245. };
  246. struct subprocess_security_attributes_s {
  247. unsigned long nLength;
  248. void *lpSecurityDescriptor;
  249. int bInheritHandle;
  250. };
  251. struct subprocess_startup_info_s {
  252. unsigned long cb;
  253. char *lpReserved;
  254. char *lpDesktop;
  255. char *lpTitle;
  256. unsigned long dwX;
  257. unsigned long dwY;
  258. unsigned long dwXSize;
  259. unsigned long dwYSize;
  260. unsigned long dwXCountChars;
  261. unsigned long dwYCountChars;
  262. unsigned long dwFillAttribute;
  263. unsigned long dwFlags;
  264. unsigned short wShowWindow;
  265. unsigned short cbReserved2;
  266. unsigned char *lpReserved2;
  267. void *hStdInput;
  268. void *hStdOutput;
  269. void *hStdError;
  270. };
  271. struct subprocess_overlapped_s {
  272. uintptr_t Internal;
  273. uintptr_t InternalHigh;
  274. union {
  275. struct {
  276. unsigned long Offset;
  277. unsigned long OffsetHigh;
  278. } DUMMYSTRUCTNAME;
  279. void *Pointer;
  280. } DUMMYUNIONNAME;
  281. void *hEvent;
  282. };
  283. #ifdef __MINGW32__
  284. #pragma GCC diagnostic pop
  285. #endif
  286. #ifdef _MSC_VER
  287. #pragma warning(pop)
  288. #endif
  289. __declspec(dllimport) unsigned long __stdcall GetLastError(void);
  290. __declspec(dllimport) int __stdcall SetHandleInformation(void *, unsigned long,
  291. unsigned long);
  292. __declspec(dllimport) int __stdcall CreatePipe(void **, void **,
  293. LPSECURITY_ATTRIBUTES,
  294. unsigned long);
  295. __declspec(dllimport) void *__stdcall CreateNamedPipeA(
  296. const char *, unsigned long, unsigned long, unsigned long, unsigned long,
  297. unsigned long, unsigned long, LPSECURITY_ATTRIBUTES);
  298. __declspec(dllimport) int __stdcall ReadFile(void *, void *, unsigned long,
  299. unsigned long *, LPOVERLAPPED);
  300. __declspec(dllimport) unsigned long __stdcall GetCurrentProcessId(void);
  301. __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void);
  302. __declspec(dllimport) void *__stdcall CreateFileA(const char *, unsigned long,
  303. unsigned long,
  304. LPSECURITY_ATTRIBUTES,
  305. unsigned long, unsigned long,
  306. void *);
  307. __declspec(dllimport) void *__stdcall CreateEventA(LPSECURITY_ATTRIBUTES, int,
  308. int, const char *);
  309. __declspec(dllimport) int __stdcall CreateProcessA(
  310. const char *, char *, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, int,
  311. unsigned long, void *, const char *, LPSTARTUPINFOA, LPPROCESS_INFORMATION);
  312. __declspec(dllimport) int __stdcall CloseHandle(void *);
  313. __declspec(dllimport) unsigned long __stdcall WaitForSingleObject(
  314. void *, unsigned long);
  315. __declspec(dllimport) int __stdcall GetExitCodeProcess(
  316. void *, unsigned long *lpExitCode);
  317. __declspec(dllimport) int __stdcall TerminateProcess(void *, unsigned int);
  318. __declspec(dllimport) unsigned long __stdcall WaitForMultipleObjects(
  319. unsigned long, void *const *, int, unsigned long);
  320. __declspec(dllimport) int __stdcall GetOverlappedResult(void *, LPOVERLAPPED,
  321. unsigned long *, int);
  322. #if defined(_DLL)
  323. #define SUBPROCESS_DLLIMPORT __declspec(dllimport)
  324. #else
  325. #define SUBPROCESS_DLLIMPORT
  326. #endif
  327. #ifdef __clang__
  328. #pragma clang diagnostic push
  329. #pragma clang diagnostic ignored "-Wreserved-identifier"
  330. #endif
  331. SUBPROCESS_DLLIMPORT int __cdecl _fileno(FILE *);
  332. SUBPROCESS_DLLIMPORT int __cdecl _open_osfhandle(subprocess_intptr_t, int);
  333. SUBPROCESS_DLLIMPORT subprocess_intptr_t __cdecl _get_osfhandle(int);
  334. #ifndef __MINGW32__
  335. void *__cdecl _alloca(subprocess_size_t);
  336. #else
  337. #include <malloc.h>
  338. #endif
  339. #ifdef __clang__
  340. #pragma clang diagnostic pop
  341. #endif
  342. #else
  343. typedef size_t subprocess_size_t;
  344. #endif
  345. #ifdef __clang__
  346. #pragma clang diagnostic push
  347. #pragma clang diagnostic ignored "-Wpadded"
  348. #endif
  349. struct subprocess_s {
  350. FILE *stdin_file;
  351. FILE *stdout_file;
  352. FILE *stderr_file;
  353. #if defined(_WIN32)
  354. void *hProcess;
  355. void *hStdInput;
  356. void *hEventOutput;
  357. void *hEventError;
  358. #else
  359. pid_t child;
  360. int return_status;
  361. #endif
  362. subprocess_size_t alive;
  363. };
  364. #ifdef __clang__
  365. #pragma clang diagnostic pop
  366. #endif
  367. #if defined(__clang__)
  368. #if __has_warning("-Wunsafe-buffer-usage")
  369. #pragma clang diagnostic push
  370. #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
  371. #endif
  372. #endif
  373. #if defined(_WIN32)
  374. subprocess_weak int subprocess_create_named_pipe_helper(void **rd, void **wr);
  375. int subprocess_create_named_pipe_helper(void **rd, void **wr) {
  376. const unsigned long pipeAccessInbound = 0x00000001;
  377. const unsigned long fileFlagOverlapped = 0x40000000;
  378. const unsigned long pipeTypeByte = 0x00000000;
  379. const unsigned long pipeWait = 0x00000000;
  380. const unsigned long genericWrite = 0x40000000;
  381. const unsigned long openExisting = 3;
  382. const unsigned long fileAttributeNormal = 0x00000080;
  383. const void *const invalidHandleValue =
  384. SUBPROCESS_PTR_CAST(void *, ~(SUBPROCESS_CAST(subprocess_intptr_t, 0)));
  385. struct subprocess_security_attributes_s saAttr = {sizeof(saAttr),
  386. SUBPROCESS_NULL, 1};
  387. char name[256] = {0};
  388. static subprocess_tls long index = 0;
  389. const long unique = index++;
  390. #if defined(_MSC_VER) && _MSC_VER < 1900
  391. #pragma warning(push, 1)
  392. #pragma warning(disable : 4996)
  393. _snprintf(name, sizeof(name) - 1,
  394. "\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld",
  395. GetCurrentProcessId(), GetCurrentThreadId(), unique);
  396. #pragma warning(pop)
  397. #else
  398. snprintf(name, sizeof(name) - 1,
  399. "\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld",
  400. GetCurrentProcessId(), GetCurrentThreadId(), unique);
  401. #endif
  402. *rd =
  403. CreateNamedPipeA(name, pipeAccessInbound | fileFlagOverlapped,
  404. pipeTypeByte | pipeWait, 1, 4096, 4096, SUBPROCESS_NULL,
  405. SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr));
  406. if (invalidHandleValue == *rd) {
  407. return -1;
  408. }
  409. *wr = CreateFileA(name, genericWrite, SUBPROCESS_NULL,
  410. SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr),
  411. openExisting, fileAttributeNormal, SUBPROCESS_NULL);
  412. if (invalidHandleValue == *wr) {
  413. return -1;
  414. }
  415. return 0;
  416. }
  417. #endif
  418. int subprocess_create(const char *const commandLine[], int options,
  419. struct subprocess_s *const out_process) {
  420. return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL,
  421. out_process);
  422. }
  423. int subprocess_create_ex(const char *const commandLine[], int options,
  424. const char *const environment[],
  425. struct subprocess_s *const out_process) {
  426. #if defined(_WIN32)
  427. int fd;
  428. void *rd, *wr;
  429. char *commandLineCombined;
  430. subprocess_size_t len;
  431. int i, j;
  432. int need_quoting;
  433. unsigned long flags = 0;
  434. const unsigned long startFUseStdHandles = 0x00000100;
  435. const unsigned long handleFlagInherit = 0x00000001;
  436. const unsigned long createNoWindow = 0x08000000;
  437. struct subprocess_subprocess_information_s processInfo;
  438. struct subprocess_security_attributes_s saAttr = {sizeof(saAttr),
  439. SUBPROCESS_NULL, 1};
  440. char *used_environment = SUBPROCESS_NULL;
  441. struct subprocess_startup_info_s startInfo = {0,
  442. SUBPROCESS_NULL,
  443. SUBPROCESS_NULL,
  444. SUBPROCESS_NULL,
  445. 0,
  446. 0,
  447. 0,
  448. 0,
  449. 0,
  450. 0,
  451. 0,
  452. 0,
  453. 0,
  454. 0,
  455. SUBPROCESS_NULL,
  456. SUBPROCESS_NULL,
  457. SUBPROCESS_NULL,
  458. SUBPROCESS_NULL};
  459. startInfo.cb = sizeof(startInfo);
  460. startInfo.dwFlags = startFUseStdHandles;
  461. if (subprocess_option_no_window == (options & subprocess_option_no_window)) {
  462. flags |= createNoWindow;
  463. }
  464. if (subprocess_option_inherit_environment !=
  465. (options & subprocess_option_inherit_environment)) {
  466. if (SUBPROCESS_NULL == environment) {
  467. used_environment = SUBPROCESS_CONST_CAST(char *, "\0\0");
  468. } else {
  469. // We always end with two null terminators.
  470. len = 2;
  471. for (i = 0; environment[i]; i++) {
  472. for (j = 0; '\0' != environment[i][j]; j++) {
  473. len++;
  474. }
  475. // For the null terminator too.
  476. len++;
  477. }
  478. used_environment = SUBPROCESS_CAST(char *, _alloca(len));
  479. // Re-use len for the insertion position
  480. len = 0;
  481. for (i = 0; environment[i]; i++) {
  482. for (j = 0; '\0' != environment[i][j]; j++) {
  483. used_environment[len++] = environment[i][j];
  484. }
  485. used_environment[len++] = '\0';
  486. }
  487. // End with the two null terminators.
  488. used_environment[len++] = '\0';
  489. used_environment[len++] = '\0';
  490. }
  491. } else {
  492. if (SUBPROCESS_NULL != environment) {
  493. return -1;
  494. }
  495. }
  496. if (!CreatePipe(&rd, &wr, SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr),
  497. 0)) {
  498. return -1;
  499. }
  500. if (!SetHandleInformation(wr, handleFlagInherit, 0)) {
  501. return -1;
  502. }
  503. fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, wr), 0);
  504. if (-1 != fd) {
  505. out_process->stdin_file = _fdopen(fd, "wb");
  506. if (SUBPROCESS_NULL == out_process->stdin_file) {
  507. return -1;
  508. }
  509. }
  510. startInfo.hStdInput = rd;
  511. if (options & subprocess_option_enable_async) {
  512. if (subprocess_create_named_pipe_helper(&rd, &wr)) {
  513. return -1;
  514. }
  515. } else {
  516. if (!CreatePipe(&rd, &wr,
  517. SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) {
  518. return -1;
  519. }
  520. }
  521. if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
  522. return -1;
  523. }
  524. fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0);
  525. if (-1 != fd) {
  526. out_process->stdout_file = _fdopen(fd, "rb");
  527. if (SUBPROCESS_NULL == out_process->stdout_file) {
  528. return -1;
  529. }
  530. }
  531. startInfo.hStdOutput = wr;
  532. if (subprocess_option_combined_stdout_stderr ==
  533. (options & subprocess_option_combined_stdout_stderr)) {
  534. out_process->stderr_file = out_process->stdout_file;
  535. startInfo.hStdError = startInfo.hStdOutput;
  536. } else {
  537. if (options & subprocess_option_enable_async) {
  538. if (subprocess_create_named_pipe_helper(&rd, &wr)) {
  539. return -1;
  540. }
  541. } else {
  542. if (!CreatePipe(&rd, &wr,
  543. SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) {
  544. return -1;
  545. }
  546. }
  547. if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
  548. return -1;
  549. }
  550. fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0);
  551. if (-1 != fd) {
  552. out_process->stderr_file = _fdopen(fd, "rb");
  553. if (SUBPROCESS_NULL == out_process->stderr_file) {
  554. return -1;
  555. }
  556. }
  557. startInfo.hStdError = wr;
  558. }
  559. if (options & subprocess_option_enable_async) {
  560. out_process->hEventOutput =
  561. CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1,
  562. SUBPROCESS_NULL);
  563. out_process->hEventError =
  564. CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1,
  565. SUBPROCESS_NULL);
  566. } else {
  567. out_process->hEventOutput = SUBPROCESS_NULL;
  568. out_process->hEventError = SUBPROCESS_NULL;
  569. }
  570. // Combine commandLine together into a single string
  571. len = 0;
  572. for (i = 0; commandLine[i]; i++) {
  573. // for the trailing \0
  574. len++;
  575. // Quote the argument if it has a space in it
  576. if (strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL ||
  577. commandLine[i][0] == SUBPROCESS_NULL)
  578. len += 2;
  579. for (j = 0; '\0' != commandLine[i][j]; j++) {
  580. switch (commandLine[i][j]) {
  581. default:
  582. break;
  583. case '\\':
  584. if (commandLine[i][j + 1] == '"') {
  585. len++;
  586. }
  587. break;
  588. case '"':
  589. len++;
  590. break;
  591. }
  592. len++;
  593. }
  594. }
  595. commandLineCombined = SUBPROCESS_CAST(char *, _alloca(len));
  596. if (!commandLineCombined) {
  597. return -1;
  598. }
  599. // Gonna re-use len to store the write index into commandLineCombined
  600. len = 0;
  601. for (i = 0; commandLine[i]; i++) {
  602. if (0 != i) {
  603. commandLineCombined[len++] = ' ';
  604. }
  605. need_quoting = strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL ||
  606. commandLine[i][0] == SUBPROCESS_NULL;
  607. if (need_quoting) {
  608. commandLineCombined[len++] = '"';
  609. }
  610. for (j = 0; '\0' != commandLine[i][j]; j++) {
  611. switch (commandLine[i][j]) {
  612. default:
  613. break;
  614. case '\\':
  615. if (commandLine[i][j + 1] == '"') {
  616. commandLineCombined[len++] = '\\';
  617. }
  618. break;
  619. case '"':
  620. commandLineCombined[len++] = '\\';
  621. break;
  622. }
  623. commandLineCombined[len++] = commandLine[i][j];
  624. }
  625. if (need_quoting) {
  626. commandLineCombined[len++] = '"';
  627. }
  628. }
  629. commandLineCombined[len] = '\0';
  630. if (!CreateProcessA(
  631. SUBPROCESS_NULL,
  632. commandLineCombined, // command line
  633. SUBPROCESS_NULL, // process security attributes
  634. SUBPROCESS_NULL, // primary thread security attributes
  635. 1, // handles are inherited
  636. flags, // creation flags
  637. used_environment, // used environment
  638. SUBPROCESS_NULL, // use parent's current directory
  639. SUBPROCESS_PTR_CAST(LPSTARTUPINFOA,
  640. &startInfo), // STARTUPINFO pointer
  641. SUBPROCESS_PTR_CAST(LPPROCESS_INFORMATION, &processInfo))) {
  642. return -1;
  643. }
  644. out_process->hProcess = processInfo.hProcess;
  645. out_process->hStdInput = startInfo.hStdInput;
  646. // We don't need the handle of the primary thread in the called process.
  647. CloseHandle(processInfo.hThread);
  648. if (SUBPROCESS_NULL != startInfo.hStdOutput) {
  649. CloseHandle(startInfo.hStdOutput);
  650. if (startInfo.hStdError != startInfo.hStdOutput) {
  651. CloseHandle(startInfo.hStdError);
  652. }
  653. }
  654. out_process->alive = 1;
  655. return 0;
  656. #else
  657. int stdinfd[2];
  658. int stdoutfd[2];
  659. int stderrfd[2];
  660. pid_t child;
  661. extern char **environ;
  662. char *const empty_environment[1] = {SUBPROCESS_NULL};
  663. posix_spawn_file_actions_t actions;
  664. char *const *used_environment;
  665. if (subprocess_option_inherit_environment ==
  666. (options & subprocess_option_inherit_environment)) {
  667. if (SUBPROCESS_NULL != environment) {
  668. return -1;
  669. }
  670. }
  671. if (0 != pipe(stdinfd)) {
  672. return -1;
  673. }
  674. if (0 != pipe(stdoutfd)) {
  675. return -1;
  676. }
  677. if (subprocess_option_combined_stdout_stderr !=
  678. (options & subprocess_option_combined_stdout_stderr)) {
  679. if (0 != pipe(stderrfd)) {
  680. return -1;
  681. }
  682. }
  683. if (environment) {
  684. #ifdef __clang__
  685. #pragma clang diagnostic push
  686. #pragma clang diagnostic ignored "-Wcast-qual"
  687. #pragma clang diagnostic ignored "-Wold-style-cast"
  688. #endif
  689. used_environment = SUBPROCESS_CONST_CAST(char *const *, environment);
  690. #ifdef __clang__
  691. #pragma clang diagnostic pop
  692. #endif
  693. } else if (subprocess_option_inherit_environment ==
  694. (options & subprocess_option_inherit_environment)) {
  695. used_environment = environ;
  696. } else {
  697. used_environment = empty_environment;
  698. }
  699. if (0 != posix_spawn_file_actions_init(&actions)) {
  700. return -1;
  701. }
  702. // Close the stdin write end
  703. if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) {
  704. posix_spawn_file_actions_destroy(&actions);
  705. return -1;
  706. }
  707. // Map the read end to stdin
  708. if (0 !=
  709. posix_spawn_file_actions_adddup2(&actions, stdinfd[0], STDIN_FILENO)) {
  710. posix_spawn_file_actions_destroy(&actions);
  711. return -1;
  712. }
  713. // Close the stdout read end
  714. if (0 != posix_spawn_file_actions_addclose(&actions, stdoutfd[0])) {
  715. posix_spawn_file_actions_destroy(&actions);
  716. return -1;
  717. }
  718. // Map the write end to stdout
  719. if (0 !=
  720. posix_spawn_file_actions_adddup2(&actions, stdoutfd[1], STDOUT_FILENO)) {
  721. posix_spawn_file_actions_destroy(&actions);
  722. return -1;
  723. }
  724. if (subprocess_option_combined_stdout_stderr ==
  725. (options & subprocess_option_combined_stdout_stderr)) {
  726. if (0 != posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO,
  727. STDERR_FILENO)) {
  728. posix_spawn_file_actions_destroy(&actions);
  729. return -1;
  730. }
  731. } else {
  732. // Close the stderr read end
  733. if (0 != posix_spawn_file_actions_addclose(&actions, stderrfd[0])) {
  734. posix_spawn_file_actions_destroy(&actions);
  735. return -1;
  736. }
  737. // Map the write end to stdout
  738. if (0 != posix_spawn_file_actions_adddup2(&actions, stderrfd[1],
  739. STDERR_FILENO)) {
  740. posix_spawn_file_actions_destroy(&actions);
  741. return -1;
  742. }
  743. }
  744. #ifdef __clang__
  745. #pragma clang diagnostic push
  746. #pragma clang diagnostic ignored "-Wcast-qual"
  747. #pragma clang diagnostic ignored "-Wold-style-cast"
  748. #endif
  749. if (subprocess_option_search_user_path ==
  750. (options & subprocess_option_search_user_path)) {
  751. if (0 != posix_spawnp(&child, commandLine[0], &actions, SUBPROCESS_NULL,
  752. SUBPROCESS_CONST_CAST(char *const *, commandLine),
  753. used_environment)) {
  754. posix_spawn_file_actions_destroy(&actions);
  755. return -1;
  756. }
  757. } else {
  758. if (0 != posix_spawn(&child, commandLine[0], &actions, SUBPROCESS_NULL,
  759. SUBPROCESS_CONST_CAST(char *const *, commandLine),
  760. used_environment)) {
  761. posix_spawn_file_actions_destroy(&actions);
  762. return -1;
  763. }
  764. }
  765. #ifdef __clang__
  766. #pragma clang diagnostic pop
  767. #endif
  768. // Close the stdin read end
  769. close(stdinfd[0]);
  770. // Store the stdin write end
  771. out_process->stdin_file = fdopen(stdinfd[1], "wb");
  772. // Close the stdout write end
  773. close(stdoutfd[1]);
  774. // Store the stdout read end
  775. out_process->stdout_file = fdopen(stdoutfd[0], "rb");
  776. if (subprocess_option_combined_stdout_stderr ==
  777. (options & subprocess_option_combined_stdout_stderr)) {
  778. out_process->stderr_file = out_process->stdout_file;
  779. } else {
  780. // Close the stderr write end
  781. close(stderrfd[1]);
  782. // Store the stderr read end
  783. out_process->stderr_file = fdopen(stderrfd[0], "rb");
  784. }
  785. // Store the child's pid
  786. out_process->child = child;
  787. out_process->alive = 1;
  788. posix_spawn_file_actions_destroy(&actions);
  789. return 0;
  790. #endif
  791. }
  792. FILE *subprocess_stdin(const struct subprocess_s *const process) {
  793. return process->stdin_file;
  794. }
  795. FILE *subprocess_stdout(const struct subprocess_s *const process) {
  796. return process->stdout_file;
  797. }
  798. FILE *subprocess_stderr(const struct subprocess_s *const process) {
  799. if (process->stdout_file != process->stderr_file) {
  800. return process->stderr_file;
  801. } else {
  802. return SUBPROCESS_NULL;
  803. }
  804. }
  805. int subprocess_join(struct subprocess_s *const process,
  806. int *const out_return_code) {
  807. #if defined(_WIN32)
  808. const unsigned long infinite = 0xFFFFFFFF;
  809. if (process->stdin_file) {
  810. fclose(process->stdin_file);
  811. process->stdin_file = SUBPROCESS_NULL;
  812. }
  813. if (process->hStdInput) {
  814. CloseHandle(process->hStdInput);
  815. process->hStdInput = SUBPROCESS_NULL;
  816. }
  817. WaitForSingleObject(process->hProcess, infinite);
  818. if (out_return_code) {
  819. if (!GetExitCodeProcess(
  820. process->hProcess,
  821. SUBPROCESS_PTR_CAST(unsigned long *, out_return_code))) {
  822. return -1;
  823. }
  824. }
  825. process->alive = 0;
  826. return 0;
  827. #else
  828. int status;
  829. if (process->stdin_file) {
  830. fclose(process->stdin_file);
  831. process->stdin_file = SUBPROCESS_NULL;
  832. }
  833. if (process->child) {
  834. if (process->child != waitpid(process->child, &status, 0)) {
  835. return -1;
  836. }
  837. process->child = 0;
  838. if (WIFEXITED(status)) {
  839. process->return_status = WEXITSTATUS(status);
  840. } else {
  841. process->return_status = EXIT_FAILURE;
  842. }
  843. process->alive = 0;
  844. }
  845. if (out_return_code) {
  846. *out_return_code = process->return_status;
  847. }
  848. return 0;
  849. #endif
  850. }
  851. int subprocess_destroy(struct subprocess_s *const process) {
  852. if (process->stdin_file) {
  853. fclose(process->stdin_file);
  854. process->stdin_file = SUBPROCESS_NULL;
  855. }
  856. if (process->stdout_file) {
  857. fclose(process->stdout_file);
  858. if (process->stdout_file != process->stderr_file) {
  859. fclose(process->stderr_file);
  860. }
  861. process->stdout_file = SUBPROCESS_NULL;
  862. process->stderr_file = SUBPROCESS_NULL;
  863. }
  864. #if defined(_WIN32)
  865. if (process->hProcess) {
  866. CloseHandle(process->hProcess);
  867. process->hProcess = SUBPROCESS_NULL;
  868. if (process->hStdInput) {
  869. CloseHandle(process->hStdInput);
  870. }
  871. if (process->hEventOutput) {
  872. CloseHandle(process->hEventOutput);
  873. }
  874. if (process->hEventError) {
  875. CloseHandle(process->hEventError);
  876. }
  877. }
  878. #endif
  879. return 0;
  880. }
  881. int subprocess_terminate(struct subprocess_s *const process) {
  882. #if defined(_WIN32)
  883. unsigned int killed_process_exit_code;
  884. int success_terminate;
  885. int windows_call_result;
  886. killed_process_exit_code = 99;
  887. windows_call_result =
  888. TerminateProcess(process->hProcess, killed_process_exit_code);
  889. success_terminate = (windows_call_result == 0) ? 1 : 0;
  890. return success_terminate;
  891. #else
  892. int result;
  893. result = kill(process->child, 9);
  894. return result;
  895. #endif
  896. }
  897. unsigned subprocess_read_stdout(struct subprocess_s *const process,
  898. char *const buffer, unsigned size) {
  899. #if defined(_WIN32)
  900. void *handle;
  901. unsigned long bytes_read = 0;
  902. struct subprocess_overlapped_s overlapped = {0, 0, {{0, 0}}, SUBPROCESS_NULL};
  903. overlapped.hEvent = process->hEventOutput;
  904. handle = SUBPROCESS_PTR_CAST(void *,
  905. _get_osfhandle(_fileno(process->stdout_file)));
  906. if (!ReadFile(handle, buffer, size, &bytes_read,
  907. SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped))) {
  908. const unsigned long errorIoPending = 997;
  909. unsigned long error = GetLastError();
  910. // Means we've got an async read!
  911. if (error == errorIoPending) {
  912. if (!GetOverlappedResult(handle,
  913. SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped),
  914. &bytes_read, 1)) {
  915. const unsigned long errorIoIncomplete = 996;
  916. const unsigned long errorHandleEOF = 38;
  917. error = GetLastError();
  918. if ((error != errorIoIncomplete) && (error != errorHandleEOF)) {
  919. return 0;
  920. }
  921. }
  922. }
  923. }
  924. return SUBPROCESS_CAST(unsigned, bytes_read);
  925. #else
  926. const int fd = fileno(process->stdout_file);
  927. const ssize_t bytes_read = read(fd, buffer, size);
  928. if (bytes_read < 0) {
  929. return 0;
  930. }
  931. return SUBPROCESS_CAST(unsigned, bytes_read);
  932. #endif
  933. }
  934. unsigned subprocess_read_stderr(struct subprocess_s *const process,
  935. char *const buffer, unsigned size) {
  936. #if defined(_WIN32)
  937. void *handle;
  938. unsigned long bytes_read = 0;
  939. struct subprocess_overlapped_s overlapped = {0, 0, {{0, 0}}, SUBPROCESS_NULL};
  940. overlapped.hEvent = process->hEventError;
  941. handle = SUBPROCESS_PTR_CAST(void *,
  942. _get_osfhandle(_fileno(process->stderr_file)));
  943. if (!ReadFile(handle, buffer, size, &bytes_read,
  944. SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped))) {
  945. const unsigned long errorIoPending = 997;
  946. unsigned long error = GetLastError();
  947. // Means we've got an async read!
  948. if (error == errorIoPending) {
  949. if (!GetOverlappedResult(handle,
  950. SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped),
  951. &bytes_read, 1)) {
  952. const unsigned long errorIoIncomplete = 996;
  953. const unsigned long errorHandleEOF = 38;
  954. error = GetLastError();
  955. if ((error != errorIoIncomplete) && (error != errorHandleEOF)) {
  956. return 0;
  957. }
  958. }
  959. }
  960. }
  961. return SUBPROCESS_CAST(unsigned, bytes_read);
  962. #else
  963. const int fd = fileno(process->stderr_file);
  964. const ssize_t bytes_read = read(fd, buffer, size);
  965. if (bytes_read < 0) {
  966. return 0;
  967. }
  968. return SUBPROCESS_CAST(unsigned, bytes_read);
  969. #endif
  970. }
  971. int subprocess_alive(struct subprocess_s *const process) {
  972. int is_alive = SUBPROCESS_CAST(int, process->alive);
  973. if (!is_alive) {
  974. return 0;
  975. }
  976. #if defined(_WIN32)
  977. {
  978. const unsigned long zero = 0x0;
  979. const unsigned long wait_object_0 = 0x00000000L;
  980. is_alive = wait_object_0 != WaitForSingleObject(process->hProcess, zero);
  981. }
  982. #else
  983. {
  984. int status;
  985. is_alive = 0 == waitpid(process->child, &status, WNOHANG);
  986. // If the process was successfully waited on we need to cleanup now.
  987. if (!is_alive) {
  988. if (WIFEXITED(status)) {
  989. process->return_status = WEXITSTATUS(status);
  990. } else {
  991. process->return_status = EXIT_FAILURE;
  992. }
  993. // Since we've already successfully waited on the process, we need to wipe
  994. // the child now.
  995. process->child = 0;
  996. if (subprocess_join(process, SUBPROCESS_NULL)) {
  997. return -1;
  998. }
  999. }
  1000. }
  1001. #endif
  1002. if (!is_alive) {
  1003. process->alive = 0;
  1004. }
  1005. return is_alive;
  1006. }
  1007. #if defined(__clang__)
  1008. #if __has_warning("-Wunsafe-buffer-usage")
  1009. #pragma clang diagnostic pop
  1010. #endif
  1011. #endif
  1012. #if defined(__cplusplus)
  1013. } // extern "C"
  1014. #endif
  1015. #endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */