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