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.
29 #include "gspawn-win32.c" /* For shared definitions */
33 write_err_and_exit (gint fd,
38 write (fd, &msg, sizeof(gintptr));
39 write (fd, &en, sizeof(gintptr));
46 # define _stdcall __attribute__((stdcall))
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.
57 /* Info peeked from mingw runtime's source code. __wgetmainargs() is a
58 * function to get the program's argv in wide char format.
65 extern void __wgetmainargs(int *argc,
69 _startupinfo *startupinfo);
71 /* Copy of protect_argv that handles wchar_t strings */
74 protect_wargv (wchar_t **wargv,
82 *new_wargv = g_new (wchar_t *, argc+1);
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).
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.
95 for (i = 0; i < argc; i++)
97 wchar_t *p = wargv[i];
100 gboolean need_dblquotes = FALSE;
103 if (*p == ' ' || *p == '\t')
104 need_dblquotes = TRUE;
110 while (*pp && *pp == '\\')
119 q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
132 while (*pp && *pp == '\\')
145 (*new_wargv)[argc] = NULL;
150 #ifndef HELPER_CONSOLE
152 WinMain (struct HINSTANCE__ *hInstance,
153 struct HINSTANCE__ *hPrevInstance,
158 main (int ignored_argc, char **ignored_argv)
161 int child_err_report_fd = -1;
162 int helper_sync_fd = -1;
168 gintptr no_error = CHILD_NO_ERROR;
169 gint argv_zero_offset = ARG_PROGRAM;
172 wchar_t **wargv, **wenvp;
173 _startupinfo si = { 0 };
176 g_assert (__argc >= ARG_COUNT);
178 /* Fetch the wide-char argument vector */
179 __wgetmainargs (&argc, &wargv, &wenvp, 0, &si);
181 /* We still have the system codepage args in __argv. We can look
182 * at the first args in which gspawn-win32.c passes us flags and
183 * fd numbers in __argv, as we know those are just ASCII anyway.
185 g_assert (argc == __argc);
187 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
188 * which write error messages.
190 child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
192 /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
193 * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
194 * the program to run and its argv[0] separately.
196 if (__argv[ARG_CHILD_ERR_REPORT][strlen (__argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
199 /* argv[ARG_HELPER_SYNC] is the file descriptor number we read a
200 * byte that tells us it is OK to exit. We have to wait until the
201 * parent allows us to exit, so that the parent has had time to
202 * duplicate the process handle we sent it. Duplicating a handle
203 * from another process works only if that other process exists.
205 helper_sync_fd = atoi (__argv[ARG_HELPER_SYNC]);
207 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
208 * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
209 * should be left alone, and 'z' if it should be connected to the
212 if (__argv[ARG_STDIN][0] == '-')
214 else if (__argv[ARG_STDIN][0] == 'z')
216 fd = open ("NUL:", O_RDONLY);
225 fd = atoi (__argv[ARG_STDIN]);
233 if (__argv[ARG_STDOUT][0] == '-')
235 else if (__argv[ARG_STDOUT][0] == 'z')
237 fd = open ("NUL:", O_WRONLY);
246 fd = atoi (__argv[ARG_STDOUT]);
254 if (__argv[ARG_STDERR][0] == '-')
256 else if (__argv[ARG_STDERR][0] == 'z')
258 fd = open ("NUL:", O_WRONLY);
267 fd = atoi (__argv[ARG_STDERR]);
275 /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
276 * process. If "-", don't change directory.
278 if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
279 __argv[ARG_WORKING_DIRECTORY][1] == 0)
281 else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
282 write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
284 /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
285 * upwards should be closed
287 if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
288 for (i = 3; i < 1000; i++) /* FIXME real limit? */
289 if (i != child_err_report_fd && i != helper_sync_fd)
292 /* We don't want our child to inherit the error report and
295 child_err_report_fd = dup_noninherited (child_err_report_fd, _O_WRONLY);
296 helper_sync_fd = dup_noninherited (helper_sync_fd, _O_RDONLY);
298 /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
299 if (__argv[ARG_WAIT][0] == 'w')
304 /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
306 /* __argv[ARG_PROGRAM] is executable file to run,
307 * __argv[argv_zero_offset]... is its argv. argv_zero_offset equals
308 * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
309 * case we have a separate executable name and argv[0].
312 /* For the program name passed to spawnv(), don't use the quoted
315 protect_wargv (wargv + argv_zero_offset, &new_wargv);
317 if (__argv[ARG_USE_PATH][0] == 'y')
318 handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
320 handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
324 if (handle == -1 && saved_errno != 0)
325 write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
327 write (child_err_report_fd, &no_error, sizeof (no_error));
328 write (child_err_report_fd, &handle, sizeof (handle));
330 read (helper_sync_fd, &c, 1);