w32spawn.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Auxiliary functions for the creation of subprocesses. Native Windows API.
  2. Copyright (C) 2001, 2003-2016 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef __KLIBC__
  15. /* Get declarations of the native Windows API functions. */
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <windows.h>
  18. #endif
  19. /* Get _open_osfhandle(). */
  20. #include <io.h>
  21. #include <stdbool.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. /* Get _get_osfhandle(). */
  26. #include "msvc-nothrow.h"
  27. #include "cloexec.h"
  28. #include "xalloc.h"
  29. /* Duplicates a file handle, making the copy uninheritable.
  30. Returns -1 for a file handle that is equivalent to closed. */
  31. static int
  32. dup_noinherit (int fd)
  33. {
  34. fd = dup_cloexec (fd);
  35. if (fd < 0 && errno == EMFILE)
  36. error (EXIT_FAILURE, errno, _("_open_osfhandle failed"));
  37. return fd;
  38. }
  39. /* Returns a file descriptor equivalent to FD, except that the resulting file
  40. descriptor is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
  41. FD must be open and non-inheritable. The result will be non-inheritable as
  42. well.
  43. If FD < 0, FD itself is returned. */
  44. static int
  45. fd_safer_noinherit (int fd)
  46. {
  47. if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
  48. {
  49. /* The recursion depth is at most 3. */
  50. int nfd = fd_safer_noinherit (dup_noinherit (fd));
  51. int saved_errno = errno;
  52. close (fd);
  53. errno = saved_errno;
  54. return nfd;
  55. }
  56. return fd;
  57. }
  58. /* Duplicates a file handle, making the copy uninheritable and ensuring the
  59. result is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
  60. Returns -1 for a file handle that is equivalent to closed. */
  61. static int
  62. dup_safer_noinherit (int fd)
  63. {
  64. return fd_safer_noinherit (dup_noinherit (fd));
  65. }
  66. /* Undoes the effect of TEMPFD = dup_safer_noinherit (ORIGFD); */
  67. static void
  68. undup_safer_noinherit (int tempfd, int origfd)
  69. {
  70. if (tempfd >= 0)
  71. {
  72. if (dup2 (tempfd, origfd) < 0)
  73. error (EXIT_FAILURE, errno, _("cannot restore fd %d: dup2 failed"),
  74. origfd);
  75. close (tempfd);
  76. }
  77. else
  78. {
  79. /* origfd was closed or open to no handle at all. Set it to a closed
  80. state. This is (nearly) equivalent to the original state. */
  81. close (origfd);
  82. }
  83. }
  84. /* Prepares an argument vector before calling spawn().
  85. Note that spawn() does not by itself call the command interpreter
  86. (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
  87. ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  88. GetVersionEx(&v);
  89. v.dwPlatformId == VER_PLATFORM_WIN32_NT;
  90. }) ? "cmd.exe" : "command.com").
  91. Instead it simply concatenates the arguments, separated by ' ', and calls
  92. CreateProcess(). We must quote the arguments since Windows CreateProcess()
  93. interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
  94. special way:
  95. - Space and tab are interpreted as delimiters. They are not treated as
  96. delimiters if they are surrounded by double quotes: "...".
  97. - Unescaped double quotes are removed from the input. Their only effect is
  98. that within double quotes, space and tab are treated like normal
  99. characters.
  100. - Backslashes not followed by double quotes are not special.
  101. - But 2*n+1 backslashes followed by a double quote become
  102. n backslashes followed by a double quote (n >= 0):
  103. \" -> "
  104. \\\" -> \"
  105. \\\\\" -> \\"
  106. - '*', '?' characters may get expanded through wildcard expansion in the
  107. callee: By default, in the callee, the initialization code before main()
  108. takes the result of GetCommandLine(), wildcard-expands it, and passes it
  109. to main(). The exceptions to this rule are:
  110. - programs that inspect GetCommandLine() and ignore argv,
  111. - mingw programs that have a global variable 'int _CRT_glob = 0;',
  112. - Cygwin programs, when invoked from a Cygwin program.
  113. */
  114. #ifndef __KLIBC__
  115. # 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*?"
  116. # 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"
  117. #else
  118. # define SHELL_SPECIAL_CHARS ""
  119. # define SHELL_SPACE_CHARS ""
  120. #endif
  121. static char **
  122. prepare_spawn (char **argv)
  123. {
  124. size_t argc;
  125. char **new_argv;
  126. size_t i;
  127. /* Count number of arguments. */
  128. for (argc = 0; argv[argc] != NULL; argc++)
  129. ;
  130. /* Allocate new argument vector. */
  131. new_argv = XNMALLOC (1 + argc + 1, char *);
  132. /* Add an element upfront that can be used when argv[0] turns out to be a
  133. script, not a program.
  134. On Unix, this would be "/bin/sh". On native Windows, "sh" is actually
  135. "sh.exe". We have to omit the directory part and rely on the search in
  136. PATH, because the mingw "mount points" are not visible inside Windows
  137. CreateProcess(). */
  138. *new_argv++ = "sh.exe";
  139. /* Put quoted arguments into the new argument vector. */
  140. for (i = 0; i < argc; i++)
  141. {
  142. const char *string = argv[i];
  143. if (string[0] == '\0')
  144. new_argv[i] = xstrdup ("\"\"");
  145. else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
  146. {
  147. bool quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
  148. size_t length;
  149. unsigned int backslashes;
  150. const char *s;
  151. char *quoted_string;
  152. char *p;
  153. length = 0;
  154. backslashes = 0;
  155. if (quote_around)
  156. length++;
  157. for (s = string; *s != '\0'; s++)
  158. {
  159. char c = *s;
  160. if (c == '"')
  161. length += backslashes + 1;
  162. length++;
  163. if (c == '\\')
  164. backslashes++;
  165. else
  166. backslashes = 0;
  167. }
  168. if (quote_around)
  169. length += backslashes + 1;
  170. quoted_string = (char *) xmalloc (length + 1);
  171. p = quoted_string;
  172. backslashes = 0;
  173. if (quote_around)
  174. *p++ = '"';
  175. for (s = string; *s != '\0'; s++)
  176. {
  177. char c = *s;
  178. if (c == '"')
  179. {
  180. unsigned int j;
  181. for (j = backslashes + 1; j > 0; j--)
  182. *p++ = '\\';
  183. }
  184. *p++ = c;
  185. if (c == '\\')
  186. backslashes++;
  187. else
  188. backslashes = 0;
  189. }
  190. if (quote_around)
  191. {
  192. unsigned int j;
  193. for (j = backslashes; j > 0; j--)
  194. *p++ = '\\';
  195. *p++ = '"';
  196. }
  197. *p = '\0';
  198. new_argv[i] = quoted_string;
  199. }
  200. else
  201. new_argv[i] = (char *) string;
  202. }
  203. new_argv[argc] = NULL;
  204. return new_argv;
  205. }