New internal function.
[platform/upstream/glib.git] / glib / gspawn-win32-helper.c
1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
2  *
3  *  Copyright 2000 Red Hat, Inc.
4  *  Copyright 2000 Tor Lillqvist
5  *
6  * GLib is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * GLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GLib; see the file COPYING.LIB.  If not, write
18  * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <fcntl.h>
25
26 #undef G_LOG_DOMAIN
27 #include "glib.h"
28 #define GSPAWN_HELPER
29 #include "gspawn-win32.c"       /* For shared definitions */
30
31
32 static void
33 write_err_and_exit (gint fd,
34                     gint msg)
35 {
36   gint en = errno;
37   
38   write (fd, &msg, sizeof(msg));
39   write (fd, &en, sizeof(en));
40   
41   _exit (1);
42 }
43
44 #ifdef __GNUC__
45 #  ifndef _stdcall
46 #    define _stdcall  __attribute__((stdcall))
47 #  endif
48 #endif
49
50 /* We build gspawn-win32-helper.exe as a Windows GUI application
51  * to avoid any temporarily flashing console windows in case
52  * the gspawn function is invoked by a GUI program. Thus, no main()
53  * but a WinMain(). We do, however, still use argc and argv tucked
54  * away in the global __argc and __argv by the C runtime startup code.
55  */
56
57 /* Info peeked from mingw runtime's source code. __wgetmainargs() is a
58  * function to get the program's argv in wide char format.
59  */
60
61 typedef struct {
62   int newmode;
63 } _startupinfo;
64
65 extern void __wgetmainargs(int *argc,
66                            wchar_t ***wargv,
67                            wchar_t ***wenviron,
68                            int expand_wildcards,
69                            _startupinfo *startupinfo);
70
71 /* Copy of protect_argv that handles wchar_t strings */
72
73 static gint
74 protect_wargv (wchar_t  **wargv,
75                wchar_t ***new_wargv)
76 {
77   gint i;
78   gint argc = 0;
79   
80   while (wargv[argc])
81     ++argc;
82   *new_wargv = g_new (wchar_t *, argc+1);
83
84   /* Quote each argv element if necessary, so that it will get
85    * reconstructed correctly in the C runtime startup code.  Note that
86    * the unquoting algorithm in the C runtime is really weird, and
87    * rather different than what Unix shells do. See stdargv.c in the C
88    * runtime sources (in the Platform SDK, in src/crt).
89    *
90    * Note that an new_wargv[0] constructed by this function should
91    * *not* be passed as the filename argument to a _wspawn* or _wexec*
92    * family function. That argument should be the real file name
93    * without any quoting.
94    */
95   for (i = 0; i < argc; i++)
96     {
97       wchar_t *p = wargv[i];
98       wchar_t *q;
99       gint len = 0;
100       gboolean need_dblquotes = FALSE;
101       while (*p)
102         {
103           if (*p == ' ' || *p == '\t')
104             need_dblquotes = TRUE;
105           else if (*p == '"')
106             len++;
107           else if (*p == '\\')
108             {
109               wchar_t *pp = p;
110               while (*pp && *pp == '\\')
111                 pp++;
112               if (*pp == '"')
113                 len++;
114             }
115           len++;
116           p++;
117         }
118
119       q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
120       p = wargv[i];
121
122       if (need_dblquotes)
123         *q++ = '"';
124
125       while (*p)
126         {
127           if (*p == '"')
128             *q++ = '\\';
129           else if (*p == '\\')
130             {
131               wchar_t *pp = p;
132               while (*pp && *pp == '\\')
133                 pp++;
134               if (*pp == '"')
135                 *q++ = '\\';
136             }
137           *q++ = *p;
138           p++;
139         }
140
141       if (need_dblquotes)
142         *q++ = '"';
143       *q++ = '\0';
144     }
145   (*new_wargv)[argc] = NULL;
146
147   return argc;
148 }
149
150 int _stdcall
151 WinMain (struct HINSTANCE__ *hInstance,
152          struct HINSTANCE__ *hPrevInstance,
153          char               *lpszCmdLine,
154          int                 nCmdShow)
155 {
156   int child_err_report_fd = -1;
157   int helper_sync_fd = -1;
158   int i;
159   int fd;
160   int mode;
161   int handle;
162   int saved_errno;
163   int no_error = CHILD_NO_ERROR;
164   int zero = 0;
165   gint argv_zero_offset = ARG_PROGRAM;
166   wchar_t **new_wargv;
167   int argc;
168   wchar_t **wargv, **wenvp;
169   _startupinfo si = { 0 };
170   char c;
171
172   g_assert (__argc >= ARG_COUNT);
173
174   /* Fetch the wide-char argument vector */
175   __wgetmainargs (&argc, &wargv, &wenvp, 0, &si);
176
177   /* We still have the system codepage args in __argv. We can look
178    * at the first args in which gspawn-win32.c passes us flags and
179    * fd numbers in __argv, as we know those are just ASCII anyway.
180    */
181   g_assert (argc == __argc);
182
183   /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
184    * which write error messages.
185    */
186   child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
187
188   /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
189    * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
190    * the program to run and its argv[0] separately.
191    */
192   if (__argv[ARG_CHILD_ERR_REPORT][strlen (__argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
193     argv_zero_offset++;
194
195   /* argv[ARG_HELPER_SYNC] is the file descriptor number we read a
196    * byte that tells us it is OK to exit. We have to wait until the
197    * parent allows us to exit, so that the parent has had time to
198    * duplicate the process handle we sent it. Duplicating a handle
199    * from another process works only if that other process exists.
200    */
201   helper_sync_fd = atoi (__argv[ARG_HELPER_SYNC]);
202
203   /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
204    * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
205    * should be left alone, and 'z' if it should be connected to the
206    * bit bucket NUL:.
207    */
208   if (__argv[ARG_STDIN][0] == '-')
209     ; /* Nothing */
210   else if (__argv[ARG_STDIN][0] == 'z')
211     {
212       fd = open ("NUL:", O_RDONLY);
213       if (fd != 0)
214         {
215           dup2 (fd, 0);
216           close (fd);
217         }
218     }
219   else
220     {
221       fd = atoi (__argv[ARG_STDIN]);
222       if (fd != 0)
223         {
224           dup2 (fd, 0);
225           close (fd);
226         }
227     }
228
229   if (__argv[ARG_STDOUT][0] == '-')
230     ; /* Nothing */
231   else if (__argv[ARG_STDOUT][0] == 'z')
232     {
233       fd = open ("NUL:", O_WRONLY);
234       if (fd != 1)
235         {
236           dup2 (fd, 1);
237           close (fd);
238         }
239     }
240   else
241     {
242       fd = atoi (__argv[ARG_STDOUT]);
243       if (fd != 1)
244         {
245           dup2 (fd, 1);
246           close (fd);
247         }
248     }
249
250   if (__argv[ARG_STDERR][0] == '-')
251     ; /* Nothing */
252   else if (__argv[ARG_STDERR][0] == 'z')
253     {
254       fd = open ("NUL:", O_WRONLY);
255       if (fd != 2)
256         {
257           dup2 (fd, 2);
258           close (fd);
259         }
260     }
261   else
262     {
263       fd = atoi (__argv[ARG_STDERR]);
264       if (fd != 2)
265         {
266           dup2 (fd, 2);
267           close (fd);
268         }
269     }
270
271   /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
272    * process.  If "-", don't change directory.
273    */
274   if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
275       __argv[ARG_WORKING_DIRECTORY][1] == 0)
276     ; /* Nothing */
277   else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
278     write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
279
280   /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
281    *  upwards should be closed
282    */
283   if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
284     for (i = 3; i < 1000; i++)  /* FIXME real limit? */
285       if (i != child_err_report_fd && i != helper_sync_fd)
286         close (i);
287
288   /* We don't want our child to inherit the error report and
289    * helper sync fds.
290    */
291   child_err_report_fd = dup_noninherited (child_err_report_fd, _O_WRONLY);
292   helper_sync_fd = dup_noninherited (helper_sync_fd, _O_RDONLY);
293
294   /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
295   if (__argv[ARG_WAIT][0] == 'w')
296     mode = P_WAIT;
297   else
298     mode = P_NOWAIT;
299
300   /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
301
302   /* __argv[ARG_PROGRAM] is executable file to run,
303    * __argv[argv_zero_offset]... is its argv. argv_zero_offset equals
304    * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
305    * case we have a separate executable name and argv[0].
306    */
307
308   /* For the program name passed to spawnv(), don't use the quoted
309    * version.
310    */
311   protect_wargv (wargv + argv_zero_offset, &new_wargv);
312
313   if (__argv[ARG_USE_PATH][0] == 'y')
314     handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
315   else
316     handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
317
318   saved_errno = errno;
319
320   if (handle == -1 && saved_errno != 0)
321     write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
322
323   write (child_err_report_fd, &no_error, sizeof (no_error));
324   if (mode == P_NOWAIT)
325     write (child_err_report_fd, &handle, sizeof (handle));
326   else
327     write (child_err_report_fd, &zero, sizeof (zero));
328
329   read (helper_sync_fd, &c, 1);
330
331   return 0;
332 }