Add some more cases to the app-id unit tests
[glib.git] / glib / gspawn-win32-helper.c
blob6614b0a3971f3717860047ae5d1cb1dd54c72714
1 /* gspawn-win32-helper.c - Helper program for process launching on Win32.
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2000 Tor Lillqvist
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include <fcntl.h>
24 /* For _CrtSetReportMode, we don't want Windows CRT (2005 and later)
25 * to terminate the process if a bad file descriptor is passed into
26 * _get_osfhandle(). This is necessary because we use _get_osfhandle()
27 * to check the validity of the fd before we try to call close() on
28 * it as attempting to close an invalid fd will cause the Windows CRT
29 * to abort() this program internally.
31 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
32 * for an explanation on this.
34 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
35 #include <crtdbg.h>
36 #endif
38 #undef G_LOG_DOMAIN
39 #include "glib.h"
40 #define GSPAWN_HELPER
41 #include "gspawn-win32.c" /* For shared definitions */
44 static void
45 write_err_and_exit (gint fd,
46 gintptr msg)
48 gintptr en = errno;
50 write (fd, &msg, sizeof(gintptr));
51 write (fd, &en, sizeof(gintptr));
53 _exit (1);
56 #ifdef __GNUC__
57 # ifndef _stdcall
58 # define _stdcall __attribute__((stdcall))
59 # endif
60 #endif
62 /* We build gspawn-win32-helper.exe as a Windows GUI application
63 * to avoid any temporarily flashing console windows in case
64 * the gspawn function is invoked by a GUI program. Thus, no main()
65 * but a WinMain().
68 /* Copy of protect_argv that handles wchar_t strings */
70 static gint
71 protect_wargv (gint argc,
72 wchar_t **wargv,
73 wchar_t ***new_wargv)
75 gint i;
77 *new_wargv = g_new (wchar_t *, argc+1);
79 /* Quote each argv element if necessary, so that it will get
80 * reconstructed correctly in the C runtime startup code. Note that
81 * the unquoting algorithm in the C runtime is really weird, and
82 * rather different than what Unix shells do. See stdargv.c in the C
83 * runtime sources (in the Platform SDK, in src/crt).
85 * Note that an new_wargv[0] constructed by this function should
86 * *not* be passed as the filename argument to a _wspawn* or _wexec*
87 * family function. That argument should be the real file name
88 * without any quoting.
90 for (i = 0; i < argc; i++)
92 wchar_t *p = wargv[i];
93 wchar_t *q;
94 gint len = 0;
95 gboolean need_dblquotes = FALSE;
96 while (*p)
98 if (*p == ' ' || *p == '\t')
99 need_dblquotes = TRUE;
100 else if (*p == '"')
101 len++;
102 else if (*p == '\\')
104 wchar_t *pp = p;
105 while (*pp && *pp == '\\')
106 pp++;
107 if (*pp == '"')
108 len++;
110 len++;
111 p++;
114 q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
115 p = wargv[i];
117 if (need_dblquotes)
118 *q++ = '"';
120 while (*p)
122 if (*p == '"')
123 *q++ = '\\';
124 else if (*p == '\\')
126 wchar_t *pp = p;
127 while (*pp && *pp == '\\')
128 pp++;
129 if (*pp == '"')
130 *q++ = '\\';
132 *q++ = *p;
133 p++;
136 if (need_dblquotes)
137 *q++ = '"';
138 *q++ = '\0';
140 (*new_wargv)[argc] = NULL;
142 return argc;
145 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
147 * This is the (empty) invalid parameter handler
148 * that is used for Visual C++ 2005 (and later) builds
149 * so that we can use this instead of the system automatically
150 * aborting the process.
152 * This is necessary as we use _get_oshandle() to check the validity
153 * of the file descriptors as we close them, so when an invalid file
154 * descriptor is passed into that function as we check on it, we get
155 * -1 as the result, instead of the gspawn helper program aborting.
157 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
158 * for an explanation on this.
160 extern void
161 myInvalidParameterHandler(const wchar_t *expression,
162 const wchar_t *function,
163 const wchar_t *file,
164 unsigned int line,
165 uintptr_t pReserved);
166 #endif
169 #ifndef HELPER_CONSOLE
170 int _stdcall
171 WinMain (struct HINSTANCE__ *hInstance,
172 struct HINSTANCE__ *hPrevInstance,
173 char *lpszCmdLine,
174 int nCmdShow)
175 #else
177 main (int ignored_argc, char **ignored_argv)
178 #endif
180 int child_err_report_fd = -1;
181 int helper_sync_fd = -1;
182 int i;
183 int fd;
184 int mode;
185 gintptr handle;
186 int saved_errno;
187 gintptr no_error = CHILD_NO_ERROR;
188 gint argv_zero_offset = ARG_PROGRAM;
189 wchar_t **new_wargv;
190 int argc;
191 char **argv;
192 wchar_t **wargv;
193 char c;
195 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
196 /* set up our empty invalid parameter handler */
197 _invalid_parameter_handler oldHandler, newHandler;
198 newHandler = myInvalidParameterHandler;
199 oldHandler = _set_invalid_parameter_handler(newHandler);
201 /* Disable the message box for assertions. */
202 _CrtSetReportMode(_CRT_ASSERT, 0);
203 #endif
205 /* Fetch the wide-char argument vector */
206 wargv = CommandLineToArgvW (GetCommandLineW(), &argc);
208 g_assert (argc >= ARG_COUNT);
210 /* Convert unicode wargs to utf8 */
211 argv = g_new(char *, argc + 1);
212 for (i = 0; i < argc; i++)
213 argv[i] = g_utf16_to_utf8(wargv[i], -1, NULL, NULL, NULL);
214 argv[i] = NULL;
216 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
217 * which write error messages.
219 child_err_report_fd = atoi (argv[ARG_CHILD_ERR_REPORT]);
221 /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
222 * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
223 * the program to run and its argv[0] separately.
225 if (argv[ARG_CHILD_ERR_REPORT][strlen (argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
226 argv_zero_offset++;
228 /* argv[ARG_HELPER_SYNC] is the file descriptor number we read a
229 * byte that tells us it is OK to exit. We have to wait until the
230 * parent allows us to exit, so that the parent has had time to
231 * duplicate the process handle we sent it. Duplicating a handle
232 * from another process works only if that other process exists.
234 helper_sync_fd = atoi (argv[ARG_HELPER_SYNC]);
236 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
237 * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
238 * should be left alone, and 'z' if it should be connected to the
239 * bit bucket NUL:.
241 if (argv[ARG_STDIN][0] == '-')
242 ; /* Nothing */
243 else if (argv[ARG_STDIN][0] == 'z')
245 fd = open ("NUL:", O_RDONLY);
246 if (fd != 0)
248 dup2 (fd, 0);
249 close (fd);
252 else
254 fd = atoi (argv[ARG_STDIN]);
255 if (fd != 0)
257 dup2 (fd, 0);
258 close (fd);
262 if (argv[ARG_STDOUT][0] == '-')
263 ; /* Nothing */
264 else if (argv[ARG_STDOUT][0] == 'z')
266 fd = open ("NUL:", O_WRONLY);
267 if (fd != 1)
269 dup2 (fd, 1);
270 close (fd);
273 else
275 fd = atoi (argv[ARG_STDOUT]);
276 if (fd != 1)
278 dup2 (fd, 1);
279 close (fd);
283 if (argv[ARG_STDERR][0] == '-')
284 ; /* Nothing */
285 else if (argv[ARG_STDERR][0] == 'z')
287 fd = open ("NUL:", O_WRONLY);
288 if (fd != 2)
290 dup2 (fd, 2);
291 close (fd);
294 else
296 fd = atoi (argv[ARG_STDERR]);
297 if (fd != 2)
299 dup2 (fd, 2);
300 close (fd);
304 /* argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
305 * process. If "-", don't change directory.
307 if (argv[ARG_WORKING_DIRECTORY][0] == '-' &&
308 argv[ARG_WORKING_DIRECTORY][1] == 0)
309 ; /* Nothing */
310 else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
311 write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
313 /* argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
314 * upwards should be closed
316 if (argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
317 for (i = 3; i < 1000; i++) /* FIXME real limit? */
318 if (i != child_err_report_fd && i != helper_sync_fd)
319 if (_get_osfhandle (i) != -1)
320 close (i);
322 /* We don't want our child to inherit the error report and
323 * helper sync fds.
325 child_err_report_fd = dup_noninherited (child_err_report_fd, _O_WRONLY);
326 helper_sync_fd = dup_noninherited (helper_sync_fd, _O_RDONLY);
328 /* argv[ARG_WAIT] is "w" to wait for the program to exit */
329 if (argv[ARG_WAIT][0] == 'w')
330 mode = P_WAIT;
331 else
332 mode = P_NOWAIT;
334 /* argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
336 /* argv[ARG_PROGRAM] is executable file to run,
337 * argv[argv_zero_offset]... is its argv. argv_zero_offset equals
338 * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
339 * case we have a separate executable name and argv[0].
342 /* For the program name passed to spawnv(), don't use the quoted
343 * version.
345 protect_wargv (argc - argv_zero_offset, wargv + argv_zero_offset, &new_wargv);
347 if (argv[ARG_USE_PATH][0] == 'y')
348 handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
349 else
350 handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
352 saved_errno = errno;
354 if (handle == -1 && saved_errno != 0)
355 write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
357 write (child_err_report_fd, &no_error, sizeof (no_error));
358 write (child_err_report_fd, &handle, sizeof (handle));
360 read (helper_sync_fd, &c, 1);
362 LocalFree (wargv);
363 g_strfreev (argv);
365 return 0;