GSimpleProxyResolver: convert docs to markdown
[glib.git] / glib / gspawn-win32-helper.c
blob136eb4a1650a166a71a7f217acccb012b276b353
1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2000 Tor Lillqvist
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.
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.
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.
22 #include "config.h"
24 #include <fcntl.h>
26 /* For _CrtSetReportMode, we don't want Windows CRT (2005 and later)
27 * to terminate the process if a bad file descriptor is passed into
28 * _get_osfhandle(). This is necessary because we use _get_osfhandle()
29 * to check the validity of the fd before we try to call close() on
30 * it as attempting to close an invalid fd will cause the Windows CRT
31 * to abort() this program internally.
33 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
34 * for an explanation on this.
36 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
37 #include <crtdbg.h>
38 #endif
40 #undef G_LOG_DOMAIN
41 #include "glib.h"
42 #define GSPAWN_HELPER
43 #include "gspawn-win32.c" /* For shared definitions */
46 static void
47 write_err_and_exit (gint fd,
48 gintptr msg)
50 gintptr en = errno;
52 write (fd, &msg, sizeof(gintptr));
53 write (fd, &en, sizeof(gintptr));
55 _exit (1);
58 #ifdef __GNUC__
59 # ifndef _stdcall
60 # define _stdcall __attribute__((stdcall))
61 # endif
62 #endif
64 /* We build gspawn-win32-helper.exe as a Windows GUI application
65 * to avoid any temporarily flashing console windows in case
66 * the gspawn function is invoked by a GUI program. Thus, no main()
67 * but a WinMain(). We do, however, still use argc and argv tucked
68 * away in the global __argc and __argv by the C runtime startup code.
71 /* Info peeked from mingw runtime's source code. __wgetmainargs() is a
72 * function to get the program's argv in wide char format.
75 typedef struct {
76 int newmode;
77 } _startupinfo;
79 extern void __wgetmainargs(int *argc,
80 wchar_t ***wargv,
81 wchar_t ***wenviron,
82 int expand_wildcards,
83 _startupinfo *startupinfo);
85 /* Copy of protect_argv that handles wchar_t strings */
87 static gint
88 protect_wargv (wchar_t **wargv,
89 wchar_t ***new_wargv)
91 gint i;
92 gint argc = 0;
94 while (wargv[argc])
95 ++argc;
96 *new_wargv = g_new (wchar_t *, argc+1);
98 /* Quote each argv element if necessary, so that it will get
99 * reconstructed correctly in the C runtime startup code. Note that
100 * the unquoting algorithm in the C runtime is really weird, and
101 * rather different than what Unix shells do. See stdargv.c in the C
102 * runtime sources (in the Platform SDK, in src/crt).
104 * Note that an new_wargv[0] constructed by this function should
105 * *not* be passed as the filename argument to a _wspawn* or _wexec*
106 * family function. That argument should be the real file name
107 * without any quoting.
109 for (i = 0; i < argc; i++)
111 wchar_t *p = wargv[i];
112 wchar_t *q;
113 gint len = 0;
114 gboolean need_dblquotes = FALSE;
115 while (*p)
117 if (*p == ' ' || *p == '\t')
118 need_dblquotes = TRUE;
119 else if (*p == '"')
120 len++;
121 else if (*p == '\\')
123 wchar_t *pp = p;
124 while (*pp && *pp == '\\')
125 pp++;
126 if (*pp == '"')
127 len++;
129 len++;
130 p++;
133 q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
134 p = wargv[i];
136 if (need_dblquotes)
137 *q++ = '"';
139 while (*p)
141 if (*p == '"')
142 *q++ = '\\';
143 else if (*p == '\\')
145 wchar_t *pp = p;
146 while (*pp && *pp == '\\')
147 pp++;
148 if (*pp == '"')
149 *q++ = '\\';
151 *q++ = *p;
152 p++;
155 if (need_dblquotes)
156 *q++ = '"';
157 *q++ = '\0';
159 (*new_wargv)[argc] = NULL;
161 return argc;
164 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
166 * This is the (empty) invalid parameter handler
167 * that is used for Visual C++ 2005 (and later) builds
168 * so that we can use this instead of the system automatically
169 * aborting the process.
171 * This is necessary as we use _get_oshandle() to check the validity
172 * of the file descriptors as we close them, so when an invalid file
173 * descriptor is passed into that function as we check on it, we get
174 * -1 as the result, instead of the gspawn helper program aborting.
176 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
177 * for an explanation on this.
179 void myInvalidParameterHandler(
180 const wchar_t * expression,
181 const wchar_t * function,
182 const wchar_t * file,
183 unsigned int line,
184 uintptr_t pReserved
187 return;
189 #endif
192 #ifndef HELPER_CONSOLE
193 int _stdcall
194 WinMain (struct HINSTANCE__ *hInstance,
195 struct HINSTANCE__ *hPrevInstance,
196 char *lpszCmdLine,
197 int nCmdShow)
198 #else
200 main (int ignored_argc, char **ignored_argv)
201 #endif
203 int child_err_report_fd = -1;
204 int helper_sync_fd = -1;
205 int i;
206 int fd;
207 int mode;
208 gintptr handle;
209 int saved_errno;
210 gintptr no_error = CHILD_NO_ERROR;
211 gint argv_zero_offset = ARG_PROGRAM;
212 wchar_t **new_wargv;
213 int argc;
214 wchar_t **wargv, **wenvp;
215 _startupinfo si = { 0 };
216 char c;
218 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
219 /* set up our empty invalid parameter handler */
220 _invalid_parameter_handler oldHandler, newHandler;
221 newHandler = myInvalidParameterHandler;
222 oldHandler = _set_invalid_parameter_handler(newHandler);
224 /* Disable the message box for assertions. */
225 _CrtSetReportMode(_CRT_ASSERT, 0);
226 #endif
229 g_assert (__argc >= ARG_COUNT);
231 /* Fetch the wide-char argument vector */
232 __wgetmainargs (&argc, &wargv, &wenvp, 0, &si);
234 /* We still have the system codepage args in __argv. We can look
235 * at the first args in which gspawn-win32.c passes us flags and
236 * fd numbers in __argv, as we know those are just ASCII anyway.
238 g_assert (argc == __argc);
240 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
241 * which write error messages.
243 child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
245 /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
246 * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
247 * the program to run and its argv[0] separately.
249 if (__argv[ARG_CHILD_ERR_REPORT][strlen (__argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
250 argv_zero_offset++;
252 /* argv[ARG_HELPER_SYNC] is the file descriptor number we read a
253 * byte that tells us it is OK to exit. We have to wait until the
254 * parent allows us to exit, so that the parent has had time to
255 * duplicate the process handle we sent it. Duplicating a handle
256 * from another process works only if that other process exists.
258 helper_sync_fd = atoi (__argv[ARG_HELPER_SYNC]);
260 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
261 * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
262 * should be left alone, and 'z' if it should be connected to the
263 * bit bucket NUL:.
265 if (__argv[ARG_STDIN][0] == '-')
266 ; /* Nothing */
267 else if (__argv[ARG_STDIN][0] == 'z')
269 fd = open ("NUL:", O_RDONLY);
270 if (fd != 0)
272 dup2 (fd, 0);
273 close (fd);
276 else
278 fd = atoi (__argv[ARG_STDIN]);
279 if (fd != 0)
281 dup2 (fd, 0);
282 close (fd);
286 if (__argv[ARG_STDOUT][0] == '-')
287 ; /* Nothing */
288 else if (__argv[ARG_STDOUT][0] == 'z')
290 fd = open ("NUL:", O_WRONLY);
291 if (fd != 1)
293 dup2 (fd, 1);
294 close (fd);
297 else
299 fd = atoi (__argv[ARG_STDOUT]);
300 if (fd != 1)
302 dup2 (fd, 1);
303 close (fd);
307 if (__argv[ARG_STDERR][0] == '-')
308 ; /* Nothing */
309 else if (__argv[ARG_STDERR][0] == 'z')
311 fd = open ("NUL:", O_WRONLY);
312 if (fd != 2)
314 dup2 (fd, 2);
315 close (fd);
318 else
320 fd = atoi (__argv[ARG_STDERR]);
321 if (fd != 2)
323 dup2 (fd, 2);
324 close (fd);
328 /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
329 * process. If "-", don't change directory.
331 if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
332 __argv[ARG_WORKING_DIRECTORY][1] == 0)
333 ; /* Nothing */
334 else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
335 write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
337 /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
338 * upwards should be closed
340 if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
341 for (i = 3; i < 1000; i++) /* FIXME real limit? */
342 if (i != child_err_report_fd && i != helper_sync_fd)
343 if (_get_osfhandle (i) != -1)
344 close (i);
346 /* We don't want our child to inherit the error report and
347 * helper sync fds.
349 child_err_report_fd = dup_noninherited (child_err_report_fd, _O_WRONLY);
350 helper_sync_fd = dup_noninherited (helper_sync_fd, _O_RDONLY);
352 /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
353 if (__argv[ARG_WAIT][0] == 'w')
354 mode = P_WAIT;
355 else
356 mode = P_NOWAIT;
358 /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
360 /* __argv[ARG_PROGRAM] is executable file to run,
361 * __argv[argv_zero_offset]... is its argv. argv_zero_offset equals
362 * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
363 * case we have a separate executable name and argv[0].
366 /* For the program name passed to spawnv(), don't use the quoted
367 * version.
369 protect_wargv (wargv + argv_zero_offset, &new_wargv);
371 if (__argv[ARG_USE_PATH][0] == 'y')
372 handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
373 else
374 handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
376 saved_errno = errno;
378 if (handle == -1 && saved_errno != 0)
379 write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
381 write (child_err_report_fd, &no_error, sizeof (no_error));
382 write (child_err_report_fd, &handle, sizeof (handle));
384 read (helper_sync_fd, &c, 1);
386 return 0;