1 /* gspawn.c - Process launching
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not, write
17 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Implementation details on Win32.
24 * - There is no way to set the no-inherit flag for
25 * a "file descriptor" in the MS C runtime. The flag is there,
26 * and the dospawn() function uses it, but unfortunately
27 * this flag can only be set when opening the file.
28 * - As there is no fork(), we cannot reliably change directory
29 * before starting the child process. (There might be several threads
30 * running, and the current directory is common for all threads.)
32 * Thus, we must in most cases use a helper program to handle closing
33 * of (inherited) file descriptors and changing of directory. In fact,
34 * we do it all the time.
36 * This source file contains the source for that helper program.
37 * To compile it, #define GSPAWN_HELPER.
40 /* Define this to get some logging all the time */
41 /* #define G_SPAWN_WIN32_DEBUG */
57 #warning "FIXME remove gettext hack"
62 #ifdef G_SPAWN_WIN32_DEBUG
64 #define SETUP_DEBUG() /* empty */
67 static int debug
= -1;
68 #define SETUP_DEBUG() \
72 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
88 ARG_CHILD_ERR_REPORT
= 1,
92 ARG_WORKING_DIRECTORY
,
93 ARG_CLOSE_DESCRIPTORS
,
97 ARG_COUNT
= ARG_PROGRAM
100 #ifndef GSPAWN_HELPER
102 static gboolean
make_pipe (gint p
[2],
104 static gboolean
fork_exec_with_pipes (gboolean dont_wait
,
105 const gchar
*working_directory
,
108 gboolean close_descriptors
,
109 gboolean search_path
,
110 gboolean stdout_to_null
,
111 gboolean stderr_to_null
,
112 gboolean child_inherits_stdin
,
113 GSpawnChildSetupFunc child_setup
,
115 gint
*standard_input
,
116 gint
*standard_output
,
117 gint
*standard_error
,
122 g_spawn_error_quark (void)
124 static GQuark quark
= 0;
126 quark
= g_quark_from_static_string ("g-exec-error-quark");
132 * @working_directory: child's current working directory, or NULL to inherit parent's
133 * @argv: child's argument vector
134 * @envp: child's environment, or NULL to inherit parent's
135 * @flags: flags from #GSpawnFlags
136 * @child_setup: function to run in the child just before exec()
137 * @user_data: user data for @child_setup
138 * @child_pid: return location for child process ID, or NULL
139 * @error: return location for error
141 * See g_spawn_async_with_pipes() for a full description; this function
142 * simply calls the g_spawn_async_with_pipes() without any pipes.
144 * Return value: TRUE on success, FALSE if error is set
147 g_spawn_async (const gchar
*working_directory
,
151 GSpawnChildSetupFunc child_setup
,
156 g_return_val_if_fail (argv
!= NULL
, FALSE
);
158 return g_spawn_async_with_pipes (working_directory
,
168 /* Avoids a danger in threaded situations (calling close()
169 * on a file descriptor twice, and another thread has
170 * re-opened it since the first close)
173 close_and_invalidate (gint
*fd
)
185 READ_FAILED
= 0, /* FALSE */
191 read_data (GString
*str
,
192 GIOChannel
*iochannel
,
201 gioerror
= g_io_channel_read (iochannel
, buf
, sizeof (buf
), &bytes
);
207 g_string_append_len (str
, buf
, bytes
);
210 else if (gioerror
== G_IO_ERROR_AGAIN
)
212 else if (gioerror
!= G_IO_ERROR_NONE
)
217 _("Failed to read data from child process"));
227 * @working_directory: child's current working directory, or NULL to inherit parent's
228 * @argv: child's argument vector
229 * @envp: child's environment, or NULL to inherit parent's
230 * @flags: flags from #GSpawnFlags
231 * @child_setup: function to run in the child just before exec()
232 * @user_data: user data for @child_setup
233 * @standard_output: return location for child output
234 * @standard_error: return location for child error messages
235 * @exit_status: child exit status, as returned by waitpid()
236 * @error: return location for error
238 * Executes a child synchronously (waits for the child to exit before returning).
239 * All output from the child is stored in @standard_output and @standard_error,
240 * if those parameters are non-NULL. If @exit_status is non-NULL, the exit status
241 * of the child is stored there as it would be by waitpid(); standard UNIX
242 * macros such as WIFEXITED() and WEXITSTATUS() must be used to evaluate the
243 * exit status. If an error occurs, no data is returned in @standard_output,
244 * @standard_error, or @exit_status.
246 * This function calls g_spawn_async_with_pipes() internally; see that function
247 * for full details on the other parameters.
249 * Return value: TRUE on success, FALSE if an error was set.
252 g_spawn_sync (const gchar
*working_directory
,
256 GSpawnChildSetupFunc child_setup
,
258 gchar
**standard_output
,
259 gchar
**standard_error
,
265 GIOChannel
*outchannel
= NULL
;
266 GIOChannel
*errchannel
= NULL
;
267 GPollFD outfd
, errfd
;
273 GString
*outstr
= NULL
;
274 GString
*errstr
= NULL
;
278 g_return_val_if_fail (argv
!= NULL
, FALSE
);
279 g_return_val_if_fail (!(flags
& G_SPAWN_DO_NOT_REAP_CHILD
), FALSE
);
280 g_return_val_if_fail (standard_output
== NULL
||
281 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
282 g_return_val_if_fail (standard_error
== NULL
||
283 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
285 /* Just to ensure segfaults if callers try to use
286 * these when an error is reported.
289 *standard_output
= NULL
;
292 *standard_error
= NULL
;
294 if (!fork_exec_with_pipes (FALSE
,
298 !(flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
),
299 (flags
& G_SPAWN_SEARCH_PATH
) != 0,
300 (flags
& G_SPAWN_STDOUT_TO_DEV_NULL
) != 0,
301 (flags
& G_SPAWN_STDERR_TO_DEV_NULL
) != 0,
302 (flags
& G_SPAWN_CHILD_INHERITS_STDIN
) != 0,
306 standard_output
? &outpipe
: NULL
,
307 standard_error
? &errpipe
: NULL
,
312 /* Read data from child. */
318 outstr
= g_string_new ("");
319 outchannel
= g_io_channel_win32_new_fd (outpipe
);
320 g_io_channel_win32_make_pollfd (outchannel
,
321 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
327 errstr
= g_string_new ("");
328 errchannel
= g_io_channel_win32_new_fd (errpipe
);
329 g_io_channel_win32_make_pollfd (errchannel
,
330 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
334 /* Read data until we get EOF on both pipes. */
354 g_print ("%s:g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
357 ret
= g_io_channel_win32_poll (fds
, nfds
, -1);
366 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
371 if (outpipe
>= 0 && (fds
[outindex
].revents
& G_IO_IN
))
373 switch (read_data (outstr
, outchannel
, error
))
377 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
382 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
383 g_io_channel_unref (outchannel
);
385 close_and_invalidate (&outpipe
);
389 g_print ("g_spawn_sync: outchannel: OK\n");
397 if (errpipe
>= 0 && (fds
[errindex
].revents
& G_IO_IN
))
399 switch (read_data (errstr
, errchannel
, error
))
403 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
408 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
409 g_io_channel_unref (errchannel
);
411 close_and_invalidate (&errpipe
);
415 g_print ("g_spawn_sync: errchannel: OK\n");
424 /* These should only be open still if we had an error. */
426 if (outchannel
!= NULL
)
427 g_io_channel_unref (outchannel
);
428 if (errchannel
!= NULL
)
429 g_io_channel_unref (errchannel
);
431 close_and_invalidate (&outpipe
);
433 close_and_invalidate (&errpipe
);
438 g_string_free (outstr
, TRUE
);
440 g_string_free (errstr
, TRUE
);
447 *exit_status
= status
;
450 *standard_output
= g_string_free (outstr
, FALSE
);
453 *standard_error
= g_string_free (errstr
, FALSE
);
460 * g_spawn_async_with_pipes:
461 * @working_directory: child's current working directory, or NULL to inherit parent's
462 * @argv: child's argument vector
463 * @envp: child's environment, or NULL to inherit parent's
464 * @flags: flags from #GSpawnFlags
465 * @child_setup: function to run in the child just before exec()
466 * @user_data: user data for @child_setup
467 * @child_pid: return location for child process ID, or NULL
468 * @standard_input: return location for file descriptor to write to child's stdin, or NULL
469 * @standard_output: return location for file descriptor to read child's stdout, or NULL
470 * @standard_error: return location for file descriptor to read child's stderr, or NULL
471 * @error: return location for error
473 * Executes a child program asynchronously (your program will not
474 * block waiting for the child to exit). The child program is
475 * specified by the only argument that must be provided, @argv. @argv
476 * should be a NULL-terminated array of strings, to be passed as the
477 * argument vector for the child. The first string in @argv is of
478 * course the name of the program to execute. By default, the name of
479 * the program must be a full path; the PATH shell variable will only
480 * be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
482 * @envp is a NULL-terminated array of strings, where each string
483 * has the form <literal>KEY=VALUE</literal>. This will become
484 * the child's environment. If @envp is NULL, the child inherits its
485 * parent's environment.
487 * @flags should be the bitwise OR of any flags you want to affect the
488 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
489 * child will not be automatically reaped; you must call waitpid() or
490 * handle SIGCHLD yourself, or the child will become a zombie.
491 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
492 * descriptors will be inherited by the child; otherwise all
493 * descriptors except stdin/stdout/stderr will be closed before
494 * calling exec() in the child. %G_SPAWN_SEARCH_PATH means that
495 * <literal>argv[0]</literal> need not be an absolute path, it
496 * will be looked for in the user's PATH. %G_SPAWN_STDOUT_TO_DEV_NULL
497 * means that the child's standad output will be discarded, instead
498 * of going to the same location as the parent's standard output.
499 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
500 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
501 * the child will inherit the parent's standard input (by default,
502 * the child's standard input is attached to /dev/null).
504 * @child_setup and @user_data are a function and user data to be
505 * called in the child after GLib has performed all the setup it plans
506 * to perform (including creating pipes, closing file descriptors,
507 * etc.) but before calling exec(). That is, @child_setup is called
508 * just before calling exec() in the child. Obviously actions taken in
509 * this function will only affect the child, not the parent.
511 * If non-NULL, @child_pid will be filled with the child's process
512 * ID. You can use the process ID to send signals to the child, or
513 * to waitpid() if you specified the %G_SPAWN_DO_NOT_REAP_CHILD flag.
515 * If non-NULL, the @standard_input, @standard_output, @standard_error
516 * locations will be filled with file descriptors for writing to the child's
517 * standard input or reading from its standard output or standard error.
518 * The caller of g_spawn_async_with_pipes() must close these file descriptors
519 * when they are no longer in use. If these parameters are NULL, the
520 * corresponding pipe won't be created.
522 * @error can be NULL to ignore errors, or non-NULL to report errors.
523 * If an error is set, the function returns FALSE. Errors
524 * are reported even if they occur in the child (for example if the
525 * executable in <literal>argv[0]</literal> is not found). Typically
526 * the <literal>message</literal> field of returned errors should be displayed
527 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
529 * If an error occurs, @child_pid, @standard_input, @standard_output,
530 * and @standard_error will not be filled with valid values.
532 * Return value: TRUE on success, FALSE if an error was set
535 g_spawn_async_with_pipes (const gchar
*working_directory
,
539 GSpawnChildSetupFunc child_setup
,
542 gint
*standard_input
,
543 gint
*standard_output
,
544 gint
*standard_error
,
547 g_return_val_if_fail (argv
!= NULL
, FALSE
);
548 g_return_val_if_fail (standard_output
== NULL
||
549 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
550 g_return_val_if_fail (standard_error
== NULL
||
551 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
552 /* can't inherit stdin if we have an input pipe. */
553 g_return_val_if_fail (standard_input
== NULL
||
554 !(flags
& G_SPAWN_CHILD_INHERITS_STDIN
), FALSE
);
556 return fork_exec_with_pipes (!(flags
& G_SPAWN_DO_NOT_REAP_CHILD
),
560 !(flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
),
561 (flags
& G_SPAWN_SEARCH_PATH
) != 0,
562 (flags
& G_SPAWN_STDOUT_TO_DEV_NULL
) != 0,
563 (flags
& G_SPAWN_STDERR_TO_DEV_NULL
) != 0,
564 (flags
& G_SPAWN_CHILD_INHERITS_STDIN
) != 0,
575 * g_spawn_command_line_sync:
576 * @command_line: a command line
577 * @standard_output: return location for child output
578 * @standard_error: return location for child errors
579 * @exit_status: return location for child exit status
580 * @error: return location for errors
582 * A simple version of g_spawn_sync() with little-used parameters
583 * removed, taking a command line instead of an argument vector. See
584 * g_spawn_sync() for full details. @command_line will be parsed by
585 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
586 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
587 * implications, so consider using g_spawn_sync() directly if
588 * appropriate. Possible errors are those from g_spawn_sync() and those
589 * from g_shell_parse_argv().
591 * Return value: TRUE on success, FALSE if an error was set
594 g_spawn_command_line_sync (const gchar
*command_line
,
595 gchar
**standard_output
,
596 gchar
**standard_error
,
603 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
605 if (!g_shell_parse_argv (command_line
,
610 retval
= g_spawn_sync (NULL
,
626 * g_spawn_command_line_async:
627 * @command_line: a command line
628 * @error: return location for errors
630 * A simple version of g_spawn_async() that parses a command line with
631 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
632 * command line in the background. Unlike g_spawn_async(), the
633 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
634 * that %G_SPAWN_SEARCH_PATH can have security implications, so
635 * consider using g_spawn_async() directly if appropriate. Possible
636 * errors are those from g_shell_parse_argv() and g_spawn_async().
638 * Return value: TRUE on success, FALSE if error is set.
641 g_spawn_command_line_async (const gchar
*command_line
,
647 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
649 if (!g_shell_parse_argv (command_line
,
654 retval
= g_spawn_async (NULL
,
668 do_exec (gboolean dont_wait
,
669 gint child_err_report_fd
,
673 const gchar
*working_directory
,
676 gboolean close_descriptors
,
677 gboolean search_path
,
678 gboolean stdout_to_null
,
679 gboolean stderr_to_null
,
680 gboolean child_inherits_stdin
,
681 GSpawnChildSetupFunc child_setup
,
685 gchar args
[ARG_COUNT
][10];
694 new_argv
= g_new (gchar
*, argc
+ 1 + ARG_COUNT
);
696 new_argv
[0] = "gspawn-win32-helper";
697 sprintf (args
[ARG_CHILD_ERR_REPORT
], "%d", child_err_report_fd
);
698 new_argv
[ARG_CHILD_ERR_REPORT
] = args
[ARG_CHILD_ERR_REPORT
];
702 sprintf (args
[ARG_STDIN
], "%d", stdin_fd
);
703 new_argv
[ARG_STDIN
] = args
[ARG_STDIN
];
705 else if (child_inherits_stdin
)
707 /* Let stdin be alone */
708 new_argv
[ARG_STDIN
] = "-";
712 /* Keep process from blocking on a read of stdin */
713 new_argv
[ARG_STDIN
] = "z";
718 sprintf (args
[ARG_STDOUT
], "%d", stdout_fd
);
719 new_argv
[ARG_STDOUT
] = args
[ARG_STDOUT
];
721 else if (stdout_to_null
)
723 new_argv
[ARG_STDOUT
] = "z";
727 new_argv
[ARG_STDOUT
] = "-";
732 sprintf (args
[ARG_STDERR
], "%d", stderr_fd
);
733 new_argv
[ARG_STDERR
] = args
[ARG_STDERR
];
735 else if (stderr_to_null
)
737 new_argv
[ARG_STDERR
] = "z";
741 new_argv
[ARG_STDERR
] = "-";
744 if (working_directory
&& *working_directory
)
745 new_argv
[ARG_WORKING_DIRECTORY
] = working_directory
;
747 new_argv
[ARG_WORKING_DIRECTORY
] = "-";
749 if (close_descriptors
)
750 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "y";
752 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "-";
755 new_argv
[ARG_USE_PATH
] = "y";
757 new_argv
[ARG_USE_PATH
] = "-";
760 new_argv
[ARG_WAIT
] = "-";
762 new_argv
[ARG_WAIT
] = "w";
764 for (i
= 0; i
<= argc
; i
++)
765 new_argv
[ARG_PROGRAM
+ i
] = argv
[i
];
767 /* Call user function just before we execute the helper program,
768 * which executes the program. Dunno what's the usefulness of this.
769 * A child setup function used on Unix probably isn't of much use
770 * as such on Win32, anyhow.
774 (* child_setup
) (user_data
);
779 g_print ("calling gspawn-win32-helper with argv:\n");
780 for (i
= 0; i
< argc
+ 1 + ARG_COUNT
; i
++)
781 g_print ("argv[%d]: %s\n", i
, (new_argv
[i
] ? new_argv
[i
] : "NULL"));
785 /* Let's hope envp hasn't mucked with PATH so that
786 * gspawn-win32-helper.exe isn't found.
788 spawnvpe (P_NOWAIT
, "gspawn-win32-helper", new_argv
, envp
);
790 spawnvp (P_NOWAIT
, "gspawn-win32-helper", new_argv
);
792 /* FIXME: What if gspawn-win32-helper.exe isn't found? */
794 /* Close the child_err_report_fd and the other process's ends of the
795 * pipes in this process, otherwise the reader will never get
798 close (child_err_report_fd
);
820 while (bytes
< sizeof(gint
)*n_ints_in_buf
)
825 g_print ("%s:read_ints: trying to read %d bytes from pipe...\n",
827 sizeof(gint
)*n_ints_in_buf
- bytes
);
829 chunk
= read (fd
, ((gchar
*)buf
) + bytes
,
830 sizeof(gint
)*n_ints_in_buf
- bytes
);
833 g_print ("... got %d bytes\n", chunk
);
837 /* Some weird shit happened, bail out */
841 G_SPAWN_ERROR_FAILED
,
842 _("Failed to read from child pipe (%s)"),
853 *n_ints_read
= bytes
/sizeof(gint
);
859 fork_exec_with_pipes (gboolean dont_wait
,
860 const gchar
*working_directory
,
863 gboolean close_descriptors
,
864 gboolean search_path
,
865 gboolean stdout_to_null
,
866 gboolean stderr_to_null
,
867 gboolean child_inherits_stdin
,
868 GSpawnChildSetupFunc child_setup
,
870 gint
*standard_input
,
871 gint
*standard_output
,
872 gint
*standard_error
,
876 gint stdin_pipe
[2] = { -1, -1 };
877 gint stdout_pipe
[2] = { -1, -1 };
878 gint stderr_pipe
[2] = { -1, -1 };
879 gint child_err_report_pipe
[2] = { -1, -1 };
885 if (!make_pipe (child_err_report_pipe
, error
))
888 if (standard_input
&& !make_pipe (stdin_pipe
, error
))
889 goto cleanup_and_fail
;
891 if (standard_output
&& !make_pipe (stdout_pipe
, error
))
892 goto cleanup_and_fail
;
894 if (standard_error
&& !make_pipe (stderr_pipe
, error
))
895 goto cleanup_and_fail
;
897 status
= do_exec (dont_wait
,
898 child_err_report_pipe
[1],
909 child_inherits_stdin
,
913 if (!read_ints (child_err_report_pipe
[0],
916 goto cleanup_and_fail
;
920 /* Error from the child. */
927 case CHILD_CHDIR_FAILED
:
931 _("Failed to change to directory '%s' (%s)"),
933 g_strerror (buf
[1]));
934 goto cleanup_and_fail
;
936 case CHILD_SPAWN_FAILED
:
939 G_SPAWN_ERROR_FAILED
,
940 _("Failed to execute child process (%s)"),
941 g_strerror (buf
[1]));
942 goto cleanup_and_fail
;
946 /* Success against all odds! return the information */
949 *standard_input
= stdin_pipe
[1];
951 *standard_output
= stdout_pipe
[0];
953 *standard_error
= stderr_pipe
[0];
955 *exit_status
= status
;
960 close_and_invalidate (&child_err_report_pipe
[0]);
961 close_and_invalidate (&child_err_report_pipe
[1]);
962 close_and_invalidate (&stdin_pipe
[0]);
963 close_and_invalidate (&stdin_pipe
[1]);
964 close_and_invalidate (&stdout_pipe
[0]);
965 close_and_invalidate (&stdout_pipe
[1]);
966 close_and_invalidate (&stderr_pipe
[0]);
967 close_and_invalidate (&stderr_pipe
[1]);
973 make_pipe (gint p
[2],
980 G_SPAWN_ERROR_FAILED
,
981 _("Failed to create pipe for communicating with child process (%s)"),
989 #else /* GSPAWN_HELPER */
992 write_err_and_exit (gint fd
,
997 write (fd
, &msg
, sizeof(msg
));
998 write (fd
, &en
, sizeof(en
));
1004 write_no_error (gint fd
)
1006 gint msg
= CHILD_NO_ERROR
;
1009 write (fd
, &msg
, sizeof(msg
));
1010 write (fd
, &en
, sizeof(en
));
1015 # define _stdcall __attribute__((stdcall))
1019 /* We build gspawn-win32-helper.exe as a Windows GUI application
1020 * to avoid any temporarily flashing console windows in case
1021 * the gspawn function is invoked by a GUI program. Thus, no main()
1022 * but a WinMain(). We do, however, still use argc and argv tucked
1023 * away in the global __argc and __argv by the C runtime startup code.
1027 WinMain (struct HINSTANCE__
*hInstance
,
1028 struct HINSTANCE__
*hPrevInstance
,
1032 int child_err_report_fd
;
1036 GString
*debugstring
;
1042 debugstring
= g_string_new ("");
1044 g_string_append (debugstring
,
1045 g_strdup_printf ("g-spawn-win32-helper: "
1046 "argc = %d, argv: ",
1048 for (i
= 0; i
< __argc
; i
++)
1051 g_string_append (debugstring
, " ");
1052 g_string_append (debugstring
, __argv
[i
]);
1055 MessageBox (NULL
, debugstring
->str
, "gspawn-win32-helper", 0);
1058 g_assert (__argc
>= ARG_COUNT
);
1060 /* argv[ARG_CHILD_ERR_REPORT] is the file descriptor onto which
1061 * write error messages.
1063 child_err_report_fd
= atoi (__argv
[ARG_CHILD_ERR_REPORT
]);
1065 /* argv[ARG_STDIN..ARG_STDERR] are the file descriptors that should
1066 * be dup2'd to stdin, stdout and stderr, '-' if the corresponding
1067 * std* should be let alone, and 'z' if it should be connected to
1068 * the bit bucket NUL:.
1070 if (__argv
[ARG_STDIN
][0] == '-')
1072 else if (__argv
[ARG_STDIN
][0] == 'z')
1074 fd
= open ("NUL:", O_RDONLY
);
1083 fd
= atoi (__argv
[ARG_STDIN
]);
1091 if (__argv
[ARG_STDOUT
][0] == '-')
1093 else if (__argv
[ARG_STDOUT
][0] == 'z')
1095 fd
= open ("NUL:", O_WRONLY
);
1104 fd
= atoi (__argv
[ARG_STDOUT
]);
1112 if (__argv
[ARG_STDERR
][0] == '-')
1114 else if (__argv
[ARG_STDERR
][0] == 'z')
1116 fd
= open ("NUL:", O_WRONLY
);
1125 fd
= atoi (__argv
[ARG_STDERR
]);
1133 /* __argv[ARG_WORKING_DIRECTORY] is the directory in which to run the
1134 * process. If "-", don't change directory.
1136 if (__argv
[ARG_WORKING_DIRECTORY
][0] == '-' &&
1137 __argv
[ARG_WORKING_DIRECTORY
][1] == 0)
1139 else if (chdir (__argv
[ARG_WORKING_DIRECTORY
]) < 0)
1140 write_err_and_exit (child_err_report_fd
,
1141 CHILD_CHDIR_FAILED
);
1143 /* __argv[ARG_CLOSE_DESCRIPTORS] is "y" if file descriptors from 3
1144 * upwards should be closed
1147 if (__argv
[ARG_CLOSE_DESCRIPTORS
][0] == 'y')
1148 for (i
= 3; i
< 1000; i
++) /* FIXME real limit? */
1149 if (i
!= child_err_report_fd
)
1152 /* __argv[ARG_WAIT] is "w" to wait for the program to exit */
1154 if (__argv
[ARG_WAIT
][0] == 'w')
1159 /* __argv[ARG_USE_PATH] is "y" to use PATH, otherwise not */
1161 /* __argv[ARG_PROGRAM] is program file to run,
1162 * __argv[ARG_PROGRAM+1]... is its __argv.
1167 debugstring
= g_string_new ("");
1168 g_string_append (debugstring
,
1169 g_strdup_printf ("calling %s on program %s, __argv: ",
1170 (__argv
[ARG_USE_PATH
][0] == 'y' ?
1171 "spawnvp" : "spawnv"),
1172 __argv
[ARG_PROGRAM
]));
1175 g_string_append (debugstring
, __argv
[i
++]);
1176 MessageBox (NULL
, debugstring
->str
, "gspawn-win32-helper", 0);
1179 if (__argv
[ARG_USE_PATH
][0] == 'y')
1181 if (spawnvp (mode
, __argv
[ARG_PROGRAM
], __argv
+ARG_PROGRAM
) < 0)
1182 write_err_and_exit (child_err_report_fd
, CHILD_SPAWN_FAILED
);
1186 if (spawnv (mode
, __argv
[ARG_PROGRAM
], __argv
+ARG_PROGRAM
) < 0)
1187 write_err_and_exit (child_err_report_fd
, CHILD_SPAWN_FAILED
);
1193 #endif /* GSPAWN_HELPER */