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.1 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/>.
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)
41 #include "gspawn-win32.c" /* For shared definitions */
45 write_err_and_exit (gint fd
,
50 write (fd
, &msg
, sizeof(gintptr
));
51 write (fd
, &en
, sizeof(gintptr
));
58 # define _stdcall __attribute__((stdcall))
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()
68 /* Copy of protect_argv that handles wchar_t strings */
71 protect_wargv (gint argc
,
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
];
95 gboolean need_dblquotes
= FALSE
;
98 if (*p
== ' ' || *p
== '\t')
99 need_dblquotes
= TRUE
;
105 while (*pp
&& *pp
== '\\')
114 q
= (*new_wargv
)[i
] = g_new (wchar_t, len
+ need_dblquotes
*2 + 1);
127 while (*pp
&& *pp
== '\\')
140 (*new_wargv
)[argc
] = NULL
;
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.
161 myInvalidParameterHandler(const wchar_t *expression
,
162 const wchar_t *function
,
165 uintptr_t pReserved
);
169 #ifndef HELPER_CONSOLE
171 WinMain (struct HINSTANCE__
*hInstance
,
172 struct HINSTANCE__
*hPrevInstance
,
177 main (int ignored_argc
, char **ignored_argv
)
180 int child_err_report_fd
= -1;
181 int helper_sync_fd
= -1;
187 gintptr no_error
= CHILD_NO_ERROR
;
188 gint argv_zero_offset
= ARG_PROGRAM
;
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);
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
);
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] == '#')
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
241 if (argv
[ARG_STDIN
][0] == '-')
243 else if (argv
[ARG_STDIN
][0] == 'z')
245 fd
= open ("NUL:", O_RDONLY
);
254 fd
= atoi (argv
[ARG_STDIN
]);
262 if (argv
[ARG_STDOUT
][0] == '-')
264 else if (argv
[ARG_STDOUT
][0] == 'z')
266 fd
= open ("NUL:", O_WRONLY
);
275 fd
= atoi (argv
[ARG_STDOUT
]);
283 if (argv
[ARG_STDERR
][0] == '-')
285 else if (argv
[ARG_STDERR
][0] == 'z')
287 fd
= open ("NUL:", O_WRONLY
);
296 fd
= atoi (argv
[ARG_STDERR
]);
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)
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)
322 /* We don't want our child to inherit the error report and
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')
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
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
);
350 handle
= _wspawnv (mode
, wargv
[ARG_PROGRAM
], (const wchar_t **) new_wargv
);
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);