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.
27 #include "gspawn-win32.c" /* For shared definitions */
31 write_err_and_exit (gint fd,
36 write (fd, &msg, sizeof(msg));
37 write (fd, &en, sizeof(en));
44 # define _stdcall __attribute__((stdcall))
48 /* We build gspawn-win32-helper.exe as a Windows GUI application
49 * to avoid any temporarily flashing console windows in case
50 * the gspawn function is invoked by a GUI program. Thus, no main()
51 * but a WinMain(). We do, however, still use argc and argv tucked
52 * away in the global __argc and __argv by the C runtime startup code.
55 /* Info peeked from mingw runtime's source code. __wgetmainargs() is a
56 * function to get the program's argv in wide char format.
63 extern void __wgetmainargs(int *argc,
67 _startupinfo *startupinfo);
69 /* Copy of protect_argv that handles wchar_t strings */
72 protect_wargv (wchar_t **wargv,
80 *new_wargv = g_new (wchar_t *, argc+1);
82 /* Quote each argv element if necessary, so that it will get
83 * reconstructed correctly in the C runtime startup code. Note that
84 * the unquoting algorithm in the C runtime is really weird, and
85 * rather different than what Unix shells do. See stdargv.c in the C
86 * runtime sources (in the Platform SDK, in src/crt).
88 * Note that an new_wargv[0] constructed by this function should
89 * *not* be passed as the filename argument to a _wspawn* or _wexec*
90 * family function. That argument should be the real file name
91 * without any quoting.
93 for (i = 0; i < argc; i++)
95 wchar_t *p = wargv[i];
98 gboolean need_dblquotes = FALSE;
101 if (*p == ' ' || *p == '\t')
102 need_dblquotes = TRUE;
108 while (*pp && *pp == '\\')
117 q = (*new_wargv)[i] = g_new (wchar_t, len + need_dblquotes*2 + 1);
130 while (*pp && *pp == '\\')
143 (*new_wargv)[argc] = NULL;
149 WinMain (struct HINSTANCE__ *hInstance,
150 struct HINSTANCE__ *hPrevInstance,
154 int child_err_report_fd;
160 int no_error = CHILD_NO_ERROR;
162 gint argv_zero_offset = ARG_PROGRAM;
165 wchar_t **wargv, **wenvp;
166 _startupinfo si = { 0 };
168 g_assert (__argc >= ARG_COUNT);
170 /* Fetch the wide-char argument vector */
171 __wgetmainargs (&argc, &wargv, &wenvp, 0, &si);
173 /* We still have the system codepage args in __argv. We can look
174 * at the first args in which gspawn-win32.c passes us flags and
175 * fd numbers in __argv, as we know those are just ASCII anyway.
177 g_assert (argc == __argc);
179 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor number onto
180 * which write error messages.
182 child_err_report_fd = atoi (__argv[ARG_CHILD_ERR_REPORT]);
184 /* Hack to implement G_SPAWN_FILE_AND_ARGV_ZERO. If
185 * argv[ARG_CHILD_ERR_REPORT] is suffixed with a '#' it means we get
186 * the program to run and its argv[0] separately.
188 if (__argv[ARG_CHILD_ERR_REPORT][strlen (__argv[ARG_CHILD_ERR_REPORT]) - 1] == '#')
191 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptor numbers that
192 * should be dup2'd to 0, 1 and 2. '-' if the corresponding fd
193 * should be left alone, and 'z' if it should be connected to the
196 if (__argv[ARG_STDIN][0] == '-')
198 else if (__argv[ARG_STDIN][0] == 'z')
200 fd = open ("NUL:", O_RDONLY);
209 fd = atoi (__argv[ARG_STDIN]);
217 if (__argv[ARG_STDOUT][0] == '-')
219 else if (__argv[ARG_STDOUT][0] == 'z')
221 fd = open ("NUL:", O_WRONLY);
230 fd = atoi (__argv[ARG_STDOUT]);
238 if (__argv[ARG_STDERR][0] == '-')
240 else if (__argv[ARG_STDERR][0] == 'z')
242 fd = open ("NUL:", O_WRONLY);
251 fd = atoi (__argv[ARG_STDERR]);
259 /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
260 * process. If "-", don't change directory.
262 if (__argv[ARG_WORKING_DIRECTORY][0] == '-' &&
263 __argv[ARG_WORKING_DIRECTORY][1] == 0)
265 else if (_wchdir (wargv[ARG_WORKING_DIRECTORY]) < 0)
266 write_err_and_exit (child_err_report_fd, CHILD_CHDIR_FAILED);
268 /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
269 * upwards should be closed
271 if (__argv[ARG_CLOSE_DESCRIPTORS][0] == 'y')
272 for (i = 3; i < 1000; i++) /* FIXME real limit? */
273 if (i != child_err_report_fd)
276 /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
277 if (__argv[ARG_WAIT][0] == 'w')
282 /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
284 /* __argv[ARG_PROGRAM] is executable file to run,
285 * __argv[argv_zero_offset]... is its argv. argv_zero_offset equals
286 * ARG_PROGRAM unless G_SPAWN_FILE_AND_ARGV_ZERO was used, in which
287 * case we have a separate executable name and argv[0].
290 /* For the program name passed to spawnv(), don't use the quoted
293 protect_wargv (wargv + argv_zero_offset, &new_wargv);
295 if (__argv[ARG_USE_PATH][0] == 'y')
296 handle = _wspawnvp (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
298 handle = _wspawnv (mode, wargv[ARG_PROGRAM], (const wchar_t **) new_wargv);
302 if (handle == -1 && saved_errno != 0)
303 write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
305 write (child_err_report_fd, &no_error, sizeof (no_error));
306 if (mode == P_NOWAIT)
307 write (child_err_report_fd, &handle, sizeof (handle));
309 write (child_err_report_fd, &zero, sizeof (zero));