windows-spawn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* Auxiliary functions for the creation of subprocesses. Native Windows API.
  2. Copyright (C) 2001, 2003-2023 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "windows-spawn.h"
  17. /* Get declarations of the native Windows API functions. */
  18. #define WIN32_LEAN_AND_MEAN
  19. #include <windows.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. /* Get _get_osfhandle(). */
  25. #if GNULIB_MSVC_NOTHROW
  26. # include "msvc-nothrow.h"
  27. #else
  28. # include <io.h>
  29. #endif
  30. #include <process.h>
  31. #include "findprog.h"
  32. /* Don't assume that UNICODE is not defined. */
  33. #undef STARTUPINFO
  34. #define STARTUPINFO STARTUPINFOA
  35. #undef CreateProcess
  36. #define CreateProcess CreateProcessA
  37. #define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037*?"
  38. #define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
  39. /* Returns the length of a quoted argument string. */
  40. static size_t
  41. quoted_arg_length (const char *string)
  42. {
  43. bool quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
  44. size_t length;
  45. unsigned int backslashes;
  46. const char *s;
  47. length = 0;
  48. backslashes = 0;
  49. if (quote_around)
  50. length++;
  51. for (s = string; *s != '\0'; s++)
  52. {
  53. char c = *s;
  54. if (c == '"')
  55. length += backslashes + 1;
  56. length++;
  57. if (c == '\\')
  58. backslashes++;
  59. else
  60. backslashes = 0;
  61. }
  62. if (quote_around)
  63. length += backslashes + 1;
  64. return length;
  65. }
  66. /* Produces a quoted argument string.
  67. Stores exactly quoted_arg_length (STRING) + 1 bytes, including the final
  68. NUL byte, at MEM.
  69. Returns a pointer past the stored quoted argument string. */
  70. static char *
  71. quoted_arg_string (const char *string, char *mem)
  72. {
  73. bool quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
  74. char *p;
  75. unsigned int backslashes;
  76. const char *s;
  77. p = mem;
  78. backslashes = 0;
  79. if (quote_around)
  80. *p++ = '"';
  81. for (s = string; *s != '\0'; s++)
  82. {
  83. char c = *s;
  84. if (c == '"')
  85. {
  86. unsigned int j;
  87. for (j = backslashes + 1; j > 0; j--)
  88. *p++ = '\\';
  89. }
  90. *p++ = c;
  91. if (c == '\\')
  92. backslashes++;
  93. else
  94. backslashes = 0;
  95. }
  96. if (quote_around)
  97. {
  98. unsigned int j;
  99. for (j = backslashes; j > 0; j--)
  100. *p++ = '\\';
  101. *p++ = '"';
  102. }
  103. *p++ = '\0';
  104. return p;
  105. }
  106. const char **
  107. prepare_spawn (const char * const *argv, char **mem_to_free)
  108. {
  109. size_t argc;
  110. const char **new_argv;
  111. size_t i;
  112. /* Count number of arguments. */
  113. for (argc = 0; argv[argc] != NULL; argc++)
  114. ;
  115. /* Allocate new argument vector. */
  116. new_argv = (const char **) malloc ((1 + argc + 1) * sizeof (const char *));
  117. /* Add an element upfront that can be used when argv[0] turns out to be a
  118. script, not a program.
  119. On Unix, this would be "/bin/sh". On native Windows, "sh" is actually
  120. "sh.exe". We have to omit the directory part and rely on the search in
  121. PATH, because the mingw "mount points" are not visible inside Windows
  122. CreateProcess(). */
  123. new_argv[0] = "sh.exe";
  124. /* Put quoted arguments into the new argument vector. */
  125. size_t needed_size = 0;
  126. for (i = 0; i < argc; i++)
  127. {
  128. const char *string = argv[i];
  129. size_t length;
  130. if (string[0] == '\0')
  131. length = strlen ("\"\"");
  132. else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
  133. length = quoted_arg_length (string);
  134. else
  135. length = strlen (string);
  136. needed_size += length + 1;
  137. }
  138. char *mem;
  139. if (needed_size == 0)
  140. mem = NULL;
  141. else
  142. {
  143. mem = (char *) malloc (needed_size);
  144. if (mem == NULL)
  145. {
  146. /* Memory allocation failure. */
  147. free (new_argv);
  148. errno = ENOMEM;
  149. return NULL;
  150. }
  151. }
  152. *mem_to_free = mem;
  153. for (i = 0; i < argc; i++)
  154. {
  155. const char *string = argv[i];
  156. new_argv[1 + i] = mem;
  157. if (string[0] == '\0')
  158. {
  159. size_t length = strlen ("\"\"");
  160. memcpy (mem, "\"\"", length + 1);
  161. mem += length + 1;
  162. }
  163. else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
  164. {
  165. mem = quoted_arg_string (string, mem);
  166. }
  167. else
  168. {
  169. size_t length = strlen (string);
  170. memcpy (mem, string, length + 1);
  171. mem += length + 1;
  172. }
  173. }
  174. new_argv[1 + argc] = NULL;
  175. return new_argv;
  176. }
  177. char *
  178. compose_command (const char * const *argv)
  179. {
  180. /* Just concatenate the argv[] strings, separated by spaces. */
  181. char *command;
  182. /* Determine the size of the needed block of memory. */
  183. size_t total_size = 0;
  184. const char * const *ap;
  185. const char *p;
  186. for (ap = argv; (p = *ap) != NULL; ap++)
  187. total_size += strlen (p) + 1;
  188. size_t command_size = (total_size > 0 ? total_size : 1);
  189. /* Allocate the block of memory. */
  190. command = (char *) malloc (command_size);
  191. if (command == NULL)
  192. {
  193. errno = ENOMEM;
  194. return NULL;
  195. }
  196. /* Fill it. */
  197. if (total_size > 0)
  198. {
  199. char *cp = command;
  200. for (ap = argv; (p = *ap) != NULL; ap++)
  201. {
  202. size_t size = strlen (p) + 1;
  203. memcpy (cp, p, size - 1);
  204. cp += size;
  205. cp[-1] = ' ';
  206. }
  207. cp[-1] = '\0';
  208. }
  209. else
  210. *command = '\0';
  211. return command;
  212. }
  213. char *
  214. compose_envblock (const char * const *envp)
  215. {
  216. /* This is a bit hairy, because we don't have a lock that would prevent other
  217. threads from making modifications in ENVP. So, just make sure we don't
  218. crash; but if other threads are making modifications, part of the result
  219. may be wrong. */
  220. retry:
  221. {
  222. /* Guess the size of the needed block of memory.
  223. The guess will be exact if other threads don't make modifications. */
  224. size_t total_size = 0;
  225. const char * const *ep;
  226. const char *p;
  227. for (ep = envp; (p = *ep) != NULL; ep++)
  228. total_size += strlen (p) + 1;
  229. size_t envblock_size = total_size;
  230. /* Allocate the block of memory. */
  231. char *envblock = (char *) malloc (envblock_size + 1);
  232. if (envblock == NULL)
  233. {
  234. errno = ENOMEM;
  235. return NULL;
  236. }
  237. size_t envblock_used = 0;
  238. for (ep = envp; (p = *ep) != NULL; ep++)
  239. {
  240. size_t size = strlen (p) + 1;
  241. if (envblock_used + size > envblock_size)
  242. {
  243. /* Other threads did modifications. Need more memory. */
  244. envblock_size += envblock_size / 2;
  245. if (envblock_used + size > envblock_size)
  246. envblock_size = envblock_used + size;
  247. char *new_envblock = (char *) realloc (envblock, envblock_size + 1);
  248. if (new_envblock == NULL)
  249. {
  250. free (envblock);
  251. errno = ENOMEM;
  252. return NULL;
  253. }
  254. envblock = new_envblock;
  255. }
  256. memcpy (envblock + envblock_used, p, size);
  257. envblock_used += size;
  258. if (envblock[envblock_used - 1] != '\0')
  259. {
  260. /* Other threads did modifications. Restart. */
  261. free (envblock);
  262. goto retry;
  263. }
  264. }
  265. envblock[envblock_used] = '\0';
  266. return envblock;
  267. }
  268. }
  269. int
  270. init_inheritable_handles (struct inheritable_handles *inh_handles,
  271. bool duplicate)
  272. {
  273. /* Determine the minimal count of handles we need to care about. */
  274. size_t handles_count;
  275. {
  276. /* _getmaxstdio
  277. <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getmaxstdio>
  278. Default value is 512. */
  279. unsigned int fdmax = _getmaxstdio ();
  280. if (fdmax < 3)
  281. fdmax = 3;
  282. for (; fdmax > 3; fdmax--)
  283. {
  284. unsigned int fd = fdmax - 1;
  285. /* _get_osfhandle
  286. <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle> */
  287. HANDLE handle = (HANDLE) _get_osfhandle (fd);
  288. if (handle != INVALID_HANDLE_VALUE)
  289. {
  290. if (duplicate)
  291. /* We will add fd to the array, regardless of whether it is
  292. inheritable or not. */
  293. break;
  294. else
  295. {
  296. DWORD hflags;
  297. /* GetHandleInformation
  298. <https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation> */
  299. if (GetHandleInformation (handle, &hflags))
  300. {
  301. if ((hflags & HANDLE_FLAG_INHERIT) != 0)
  302. /* fd denotes an inheritable descriptor. */
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. handles_count = fdmax;
  309. }
  310. /* Note: handles_count >= 3. */
  311. /* Allocate the array. */
  312. size_t handles_allocated = handles_count;
  313. struct IHANDLE *ih =
  314. (struct IHANDLE *) malloc (handles_allocated * sizeof (struct IHANDLE));
  315. if (ih == NULL)
  316. {
  317. errno = ENOMEM;
  318. return -1;
  319. }
  320. /* Fill in the array. */
  321. {
  322. HANDLE curr_process = (duplicate ? GetCurrentProcess () : INVALID_HANDLE_VALUE);
  323. unsigned int fd;
  324. for (fd = 0; fd < handles_count; fd++)
  325. {
  326. ih[fd].handle = INVALID_HANDLE_VALUE;
  327. /* _get_osfhandle
  328. <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle> */
  329. HANDLE handle = (HANDLE) _get_osfhandle (fd);
  330. if (handle != INVALID_HANDLE_VALUE)
  331. {
  332. DWORD hflags;
  333. /* GetHandleInformation
  334. <https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation> */
  335. if (GetHandleInformation (handle, &hflags))
  336. {
  337. if (duplicate)
  338. {
  339. /* Add fd to the array, regardless of whether it is
  340. inheritable or not. */
  341. if ((hflags & HANDLE_FLAG_INHERIT) != 0)
  342. {
  343. /* Instead of duplicating it, just mark it as shared. */
  344. ih[fd].handle = handle;
  345. ih[fd].flags = KEEP_OPEN_IN_PARENT | KEEP_OPEN_IN_CHILD;
  346. }
  347. else
  348. {
  349. if (!DuplicateHandle (curr_process, handle,
  350. curr_process, &ih[fd].handle,
  351. 0, TRUE, DUPLICATE_SAME_ACCESS))
  352. {
  353. unsigned int i;
  354. for (i = 0; i < fd; i++)
  355. if (ih[i].handle != INVALID_HANDLE_VALUE
  356. && !(ih[i].flags & KEEP_OPEN_IN_PARENT))
  357. CloseHandle (ih[i].handle);
  358. free (ih);
  359. errno = EBADF; /* arbitrary */
  360. return -1;
  361. }
  362. ih[fd].flags = 0;
  363. }
  364. }
  365. else
  366. {
  367. if ((hflags & HANDLE_FLAG_INHERIT) != 0)
  368. {
  369. /* fd denotes an inheritable descriptor. */
  370. ih[fd].handle = handle;
  371. ih[fd].flags = KEEP_OPEN_IN_CHILD;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. /* Return the result. */
  379. inh_handles->count = handles_count;
  380. inh_handles->allocated = handles_allocated;
  381. inh_handles->ih = ih;
  382. return 0;
  383. }
  384. int
  385. compose_handles_block (const struct inheritable_handles *inh_handles,
  386. STARTUPINFO *sinfo)
  387. {
  388. /* STARTUPINFO
  389. <https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa> */
  390. sinfo->dwFlags = STARTF_USESTDHANDLES;
  391. sinfo->hStdInput = inh_handles->ih[0].handle;
  392. sinfo->hStdOutput = inh_handles->ih[1].handle;
  393. sinfo->hStdError = inh_handles->ih[2].handle;
  394. /* On newer versions of Windows, more file descriptors / handles than the
  395. first three can be passed.
  396. The format is as follows: Let N be an exclusive upper bound for the file
  397. descriptors to be passed. Two arrays are constructed in memory:
  398. - flags[0..N-1], of element type 'unsigned char',
  399. - handles[0..N-1], of element type 'HANDLE' or 'intptr_t'.
  400. For used entries, handles[i] is the handle, and flags[i] is a set of flags,
  401. a combination of:
  402. 1 for open file descriptors,
  403. 64 for handles of type FILE_TYPE_CHAR,
  404. 8 for handles of type FILE_TYPE_PIPE,
  405. 32 for O_APPEND.
  406. For unused entries - this may include any of the first three, since they
  407. are already passed above -, handles[i] is INVALID_HANDLE_VALUE and flags[i]
  408. is zero.
  409. lpReserved2 now is a pointer to the concatenation (without padding) of:
  410. - an 'unsigned int' whose value is N,
  411. - the contents of the flags[0..N-1] array,
  412. - the contents of the handles[0..N-1] array.
  413. cbReserved2 is the size (in bytes) of the object at lpReserved2. */
  414. size_t handles_count = inh_handles->count;
  415. sinfo->cbReserved2 =
  416. sizeof (unsigned int)
  417. + handles_count * sizeof (unsigned char)
  418. + handles_count * sizeof (HANDLE);
  419. /* Add some padding, so that we can work with a properly aligned HANDLE
  420. array. */
  421. char *hblock = (char *) malloc (sinfo->cbReserved2 + (sizeof (HANDLE) - 1));
  422. if (hblock == NULL)
  423. {
  424. errno = ENOMEM;
  425. return -1;
  426. }
  427. unsigned char *flags = (unsigned char *) (hblock + sizeof (unsigned int));
  428. char *handles = (char *) (flags + handles_count);
  429. HANDLE *handles_aligned =
  430. (HANDLE *) (((uintptr_t) handles + (sizeof (HANDLE) - 1))
  431. & - (uintptr_t) sizeof (HANDLE));
  432. * (unsigned int *) hblock = handles_count;
  433. {
  434. unsigned int fd;
  435. for (fd = 0; fd < handles_count; fd++)
  436. {
  437. handles_aligned[fd] = INVALID_HANDLE_VALUE;
  438. flags[fd] = 0;
  439. HANDLE handle = inh_handles->ih[fd].handle;
  440. if (handle != INVALID_HANDLE_VALUE
  441. /* The first three are possibly already passed above.
  442. But they need to passed here as well, if they have some flags. */
  443. && (fd >= 3 || (unsigned char) inh_handles->ih[fd].flags != 0))
  444. {
  445. DWORD hflags;
  446. /* GetHandleInformation
  447. <https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation> */
  448. if (GetHandleInformation (handle, &hflags))
  449. {
  450. if ((hflags & HANDLE_FLAG_INHERIT) != 0)
  451. {
  452. /* fd denotes an inheritable descriptor. */
  453. handles_aligned[fd] = handle;
  454. /* On Microsoft Windows, it would be sufficient to set
  455. flags[fd] = 1. But on ReactOS or Wine, adding the bit
  456. that indicates the handle type may be necessary. So,
  457. just do it everywhere. */
  458. flags[fd] = 1 | (unsigned char) inh_handles->ih[fd].flags;
  459. switch (GetFileType (handle))
  460. {
  461. case FILE_TYPE_CHAR:
  462. flags[fd] |= 64;
  463. break;
  464. case FILE_TYPE_PIPE:
  465. flags[fd] |= 8;
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. else
  472. /* We shouldn't have any non-inheritable handles in
  473. inh_handles->handles. */
  474. abort ();
  475. }
  476. }
  477. }
  478. }
  479. if (handles != (char *) handles_aligned)
  480. memmove (handles, (char *) handles_aligned, handles_count * sizeof (HANDLE));
  481. sinfo->lpReserved2 = (BYTE *) hblock;
  482. return 0;
  483. }
  484. void
  485. free_inheritable_handles (struct inheritable_handles *inh_handles)
  486. {
  487. free (inh_handles->ih);
  488. }
  489. int
  490. convert_CreateProcess_error (DWORD error)
  491. {
  492. /* Some of these errors probably cannot happen. But who knows... */
  493. switch (error)
  494. {
  495. case ERROR_FILE_NOT_FOUND:
  496. case ERROR_PATH_NOT_FOUND:
  497. case ERROR_BAD_PATHNAME:
  498. case ERROR_BAD_NET_NAME:
  499. case ERROR_INVALID_NAME:
  500. case ERROR_DIRECTORY:
  501. return ENOENT;
  502. break;
  503. case ERROR_ACCESS_DENIED:
  504. case ERROR_SHARING_VIOLATION:
  505. return EACCES;
  506. break;
  507. case ERROR_OUTOFMEMORY:
  508. return ENOMEM;
  509. break;
  510. case ERROR_BUFFER_OVERFLOW:
  511. case ERROR_FILENAME_EXCED_RANGE:
  512. return ENAMETOOLONG;
  513. break;
  514. case ERROR_BAD_FORMAT:
  515. case ERROR_BAD_EXE_FORMAT:
  516. return ENOEXEC;
  517. break;
  518. default:
  519. return EINVAL;
  520. break;
  521. }
  522. }
  523. intptr_t
  524. spawnpvech (int mode,
  525. const char *progname, const char * const *argv,
  526. const char * const *envp,
  527. const char *currdir,
  528. HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle)
  529. {
  530. /* Validate the arguments. */
  531. if (!(mode == P_WAIT
  532. || mode == P_NOWAIT
  533. || mode == P_DETACH
  534. || mode == P_OVERLAY)
  535. || progname == NULL || argv == NULL)
  536. {
  537. errno = EINVAL;
  538. return -1;
  539. }
  540. /* Implement the 'p' letter: search for PROGNAME in getenv ("PATH"). */
  541. const char *resolved_progname =
  542. find_in_given_path (progname, getenv ("PATH"), NULL, false);
  543. if (resolved_progname == NULL)
  544. return -1;
  545. /* Compose the command. */
  546. char *command = compose_command (argv);
  547. if (command == NULL)
  548. goto out_of_memory_1;
  549. /* Copy *ENVP into a contiguous block of memory. */
  550. char *envblock;
  551. if (envp == NULL)
  552. envblock = NULL;
  553. else
  554. {
  555. envblock = compose_envblock (envp);
  556. if (envblock == NULL)
  557. goto out_of_memory_2;
  558. }
  559. /* Collect the inheritable handles. */
  560. struct inheritable_handles inh_handles;
  561. if (init_inheritable_handles (&inh_handles, false) < 0)
  562. {
  563. int saved_errno = errno;
  564. if (envblock != NULL)
  565. free (envblock);
  566. free (command);
  567. if (resolved_progname != progname)
  568. free ((char *) resolved_progname);
  569. errno = saved_errno;
  570. return -1;
  571. }
  572. inh_handles.ih[0].handle = stdin_handle;
  573. inh_handles.ih[0].flags = KEEP_OPEN_IN_CHILD;
  574. inh_handles.ih[1].handle = stdout_handle;
  575. inh_handles.ih[1].flags = KEEP_OPEN_IN_CHILD;
  576. inh_handles.ih[2].handle = stderr_handle;
  577. inh_handles.ih[2].flags = KEEP_OPEN_IN_CHILD;
  578. /* CreateProcess
  579. <https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa> */
  580. /* <https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags> */
  581. DWORD process_creation_flags = (mode == P_DETACH ? DETACHED_PROCESS : 0);
  582. /* STARTUPINFO
  583. <https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa> */
  584. STARTUPINFO sinfo;
  585. sinfo.cb = sizeof (STARTUPINFO);
  586. sinfo.lpReserved = NULL;
  587. sinfo.lpDesktop = NULL;
  588. sinfo.lpTitle = NULL;
  589. if (compose_handles_block (&inh_handles, &sinfo) < 0)
  590. {
  591. int saved_errno = errno;
  592. free_inheritable_handles (&inh_handles);
  593. if (envblock != NULL)
  594. free (envblock);
  595. free (command);
  596. if (resolved_progname != progname)
  597. free ((char *) resolved_progname);
  598. errno = saved_errno;
  599. return -1;
  600. }
  601. PROCESS_INFORMATION pinfo;
  602. if (!CreateProcess (resolved_progname, command, NULL, NULL, TRUE,
  603. process_creation_flags, envblock, currdir, &sinfo,
  604. &pinfo))
  605. {
  606. DWORD error = GetLastError ();
  607. free (sinfo.lpReserved2);
  608. free_inheritable_handles (&inh_handles);
  609. if (envblock != NULL)
  610. free (envblock);
  611. free (command);
  612. if (resolved_progname != progname)
  613. free ((char *) resolved_progname);
  614. errno = convert_CreateProcess_error (error);
  615. return -1;
  616. }
  617. if (pinfo.hThread)
  618. CloseHandle (pinfo.hThread);
  619. free (sinfo.lpReserved2);
  620. free_inheritable_handles (&inh_handles);
  621. if (envblock != NULL)
  622. free (envblock);
  623. free (command);
  624. if (resolved_progname != progname)
  625. free ((char *) resolved_progname);
  626. switch (mode)
  627. {
  628. case P_WAIT:
  629. {
  630. /* Wait until it terminates. Then get its exit status code. */
  631. switch (WaitForSingleObject (pinfo.hProcess, INFINITE))
  632. {
  633. case WAIT_OBJECT_0:
  634. break;
  635. case WAIT_FAILED:
  636. errno = ECHILD;
  637. return -1;
  638. default:
  639. abort ();
  640. }
  641. DWORD exit_code;
  642. if (!GetExitCodeProcess (pinfo.hProcess, &exit_code))
  643. {
  644. errno = ECHILD;
  645. return -1;
  646. }
  647. CloseHandle (pinfo.hProcess);
  648. return exit_code;
  649. }
  650. case P_NOWAIT:
  651. /* Return pinfo.hProcess, not pinfo.dwProcessId. */
  652. return (intptr_t) pinfo.hProcess;
  653. case P_DETACH:
  654. case P_OVERLAY:
  655. CloseHandle (pinfo.hProcess);
  656. return 0;
  657. default:
  658. /* Already checked above. */
  659. abort ();
  660. }
  661. /*NOTREACHED*/
  662. out_of_memory_2:
  663. free (command);
  664. out_of_memory_1:
  665. if (resolved_progname != progname)
  666. free ((char *) resolved_progname);
  667. errno = ENOMEM;
  668. return -1;
  669. }