w32spawn.h 7.2 KB

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