windows-spawn.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifndef _WINDOWS_SPAWN_H
  15. #define _WINDOWS_SPAWN_H
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. /* Get declarations of the native Windows API functions. */
  19. #define WIN32_LEAN_AND_MEAN
  20. #include <windows.h>
  21. /* Prepares an argument vector before calling spawn().
  22. Note that spawn() does not by itself call the command interpreter
  23. (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
  24. ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  25. GetVersionEx(&v);
  26. v.dwPlatformId == VER_PLATFORM_WIN32_NT;
  27. }) ? "cmd.exe" : "command.com").
  28. Instead it simply concatenates the arguments, separated by ' ', and calls
  29. CreateProcess(). We must quote the arguments since Windows CreateProcess()
  30. interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
  31. special way:
  32. - Space and tab are interpreted as delimiters. They are not treated as
  33. delimiters if they are surrounded by double quotes: "...".
  34. - Unescaped double quotes are removed from the input. Their only effect is
  35. that within double quotes, space and tab are treated like normal
  36. characters.
  37. - Backslashes not followed by double quotes are not special.
  38. - But 2*n+1 backslashes followed by a double quote become
  39. n backslashes followed by a double quote (n >= 0):
  40. \" -> "
  41. \\\" -> \"
  42. \\\\\" -> \\"
  43. - '*', '?' characters may get expanded through wildcard expansion in the
  44. callee: By default, in the callee, the initialization code before main()
  45. takes the result of GetCommandLine(), wildcard-expands it, and passes it
  46. to main(). The exceptions to this rule are:
  47. - programs that inspect GetCommandLine() and ignore argv,
  48. - mingw programs that have a global variable 'int _CRT_glob = 0;',
  49. - Cygwin programs, when invoked from a Cygwin program.
  50. prepare_spawn creates and returns a new argument vector, where the arguments
  51. are appropriately quoted and an additional argument "sh.exe" has been added
  52. at the beginning. The new argument vector is freshly allocated. The memory
  53. for all its elements is allocated within *MEM_TO_FREE, which is freshly
  54. allocated as well. In case of memory allocation failure, NULL is returned,
  55. with errno set.
  56. */
  57. extern const char ** prepare_spawn (const char * const *argv,
  58. char **mem_to_free);
  59. /* Composes the command to be passed to CreateProcess().
  60. ARGV must contain appropriately quoted arguments, as returned by
  61. prepare_spawn.
  62. Returns a freshly allocated string. In case of memory allocation failure,
  63. NULL is returned, with errno set. */
  64. extern char * compose_command (const char * const *argv)
  65. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  66. /* Composes the block of memory that contains the environment variables.
  67. ENVP must contain an environment (a NULL-terminated array of string of the
  68. form VARIABLE=VALUE).
  69. Returns a freshly allocated block of memory. In case of memory allocation
  70. failure, NULL is returned, with errno set. */
  71. extern char * compose_envblock (const char * const *envp)
  72. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  73. /* An inheritable handle with some additional information. */
  74. struct IHANDLE
  75. {
  76. /* Either INVALID_HANDLE_VALUE or an inheritable handle. */
  77. HANDLE handle;
  78. /* Only relevant if handle != INVALID_HANDLE_VALUE.
  79. It is a bit mask consisting of:
  80. - 32 for O_APPEND.
  81. - KEEP_OPEN_IN_CHILD if the handle is scheduled to be preserved in the
  82. child process.
  83. - KEEP_OPEN_IN_PARENT if the handle is shared with (and needs to be kept
  84. open in) the parent process.
  85. - DELAYED_DUP2_OLDFD if there is a delayed dup2 (oldfd, newfd) and
  86. this IHANDLE is at position oldfd.
  87. - DELAYED_DUP2_NEWFD if there is a delayed dup2 (oldfd, newfd) and
  88. this IHANDLE is at position newfd.
  89. Note that DELAYED_DUP2_OLDFD and DELAYED_DUP2_NEWFD cannot be set in the
  90. same IHANDLE. */
  91. unsigned short flags;
  92. #define KEEP_OPEN_IN_CHILD 0x100
  93. #define KEEP_OPEN_IN_PARENT 0x200
  94. #define DELAYED_DUP2_OLDFD 0x400
  95. #define DELAYED_DUP2_NEWFD 0x800
  96. /* Only relevant if handle != INVALID_HANDLE_VALUE and flags contains
  97. either DELAYED_DUP2_OLDFD or DELAYED_DUP2_NEWFD.
  98. It is the other fd of the delayed dup2 (oldfd, newfd), i.e.
  99. - for DELAYED_DUP2_OLDFD, the newfd,
  100. - for DELAYED_DUP2_NEWFD, the oldfd. */
  101. int linked_fd;
  102. };
  103. /* This struct keeps track of which handles to potentially pass to a subprocess,
  104. and with which flags. All of the handles here are inheritable.
  105. Regarding handle inheritance, see
  106. <https://docs.microsoft.com/en-us/windows/win32/sysinfo/handle-inheritance>.
  107. Whether a handle is actually scheduled for being preserved in the child
  108. process is determined by the KEEP_OPEN_IN_CHILD bit in the flags. */
  109. struct inheritable_handles
  110. {
  111. /* The number of occupied entries in the two arrays below.
  112. 3 <= count <= allocated. */
  113. size_t count;
  114. /* The number of allocated entries in the two arrays below. */
  115. size_t allocated;
  116. /* ih[0..count-1] are the occupied entries. */
  117. struct IHANDLE *ih;
  118. };
  119. /* Initializes a set of inheritable handles, filling in all or part of the
  120. file descriptors of the current process.
  121. If DUPLICATE is true, the handles stored are those of all file descriptors,
  122. and we use DuplicateHandle to make sure that they are all inheritable.
  123. If DUPLICATE is false, the handles stored are only the inheritables ones;
  124. this is a shortcut for spawnpvech().
  125. Returns 0 upon success. In case of failure, -1 is returned, with errno set.
  126. */
  127. extern int init_inheritable_handles (struct inheritable_handles *inh_handles,
  128. bool duplicate);
  129. /* Fills a set of inheritable handles into a STARTUPINFO for CreateProcess().
  130. Returns 0 upon success. In case of failure, -1 is returned, with errno set.
  131. */
  132. extern int compose_handles_block (const struct inheritable_handles *inh_handles,
  133. STARTUPINFOA *sinfo);
  134. /* Frees the memory held by a set of inheritable handles. */
  135. extern void free_inheritable_handles (struct inheritable_handles *inh_handles);
  136. /* Converts a CreateProcess() error code (retrieved through GetLastError()) to
  137. an errno value. */
  138. extern int convert_CreateProcess_error (DWORD error);
  139. /* Creates a subprocess.
  140. MODE is either P_WAIT or P_NOWAIT.
  141. PROGNAME is the program to invoke.
  142. ARGV is the NULL-terminated array of arguments, ARGV[0] being PROGNAME by
  143. convention.
  144. ENVP is the NULL-terminated set of environment variable assignments, or NULL
  145. to inherit the initial environ variable assignments from the caller and
  146. ignore all calls to putenv(), setenv(), unsetenv() done in the caller.
  147. CURRDIR is the directory in which to start the program, or NULL to inherit
  148. the working directory from the caller.
  149. STDIN_HANDLE, STDOUT_HANDLE, STDERR_HANDLE are the handles to use for the
  150. first three file descriptors in the callee process.
  151. Returns
  152. - 0 for success (if MODE is P_WAIT), or
  153. - a handle that be passed to _cwait (on Windows) or waitpid (on OS/2), or
  154. - -1 upon error, with errno set.
  155. */
  156. extern intptr_t spawnpvech (int mode,
  157. const char *progname, const char * const *argv,
  158. const char * const *envp,
  159. const char *currdir,
  160. HANDLE stdin_handle, HANDLE stdout_handle,
  161. HANDLE stderr_handle);
  162. #endif /* _WINDOWS_SPAWN_H */