230
240
* if those parameters are non-%NULL. Note that you must set the
231
241
* %G_SPAWN_STDOUT_TO_DEV_NULL and %G_SPAWN_STDERR_TO_DEV_NULL flags when
232
242
* passing %NULL for @standard_output and @standard_error.
233
* If @exit_status is non-%NULL, the exit status of the child is stored
234
* there as it would be returned by waitpid(); standard UNIX macros such
235
* as WIFEXITED() and WEXITSTATUS() must be used to evaluate the exit status.
236
* Note that this function call waitpid() even if @exit_status is %NULL, and
237
* does not accept the %G_SPAWN_DO_NOT_REAP_CHILD flag.
238
* If an error occurs, no data is returned in @standard_output,
239
* @standard_error, or @exit_status.
244
* If @exit_status is non-%NULL, the platform-specific exit status of
245
* the child is stored there; see the doucumentation of
246
* g_spawn_check_exit_status() for how to use and interpret this.
247
* Note that it is invalid to pass %G_SPAWN_DO_NOT_REAP_CHILD in
250
* If an error occurs, no data is returned in @standard_output,
251
* @standard_error, or @exit_status.
241
253
* This function calls g_spawn_async_with_pipes() internally; see that
242
254
* function for full details on the other parameters and details on
396
410
close_and_invalidate (&outpipe);
397
411
if (errpipe >= 0)
398
412
close_and_invalidate (&errpipe);
400
/* Wait for child to exit, even if we have
414
/* Now create a temporary main context and loop, with just one
415
* waitpid source. We used to invoke waitpid() directly here, but
416
* this way we unify with the worker thread in gmain.c.
405
ret = waitpid (pid, &status, 0);
411
else if (errno == ECHILD)
415
g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but SIGCHLD action was set to SIG_IGN and ECHILD was received by waitpid(), so exit status can't be returned. This is a bug in the program calling g_spawn_sync(); either don't request the exit status, or don't set the SIGCHLD action.");
419
/* We don't need the exit status. */
424
if (!failed) /* avoid error pileups */
433
_("Unexpected error in waitpid() (%s)"),
419
GMainContext *context;
423
context = g_main_context_new ();
424
loop = g_main_loop_new (context, TRUE);
426
waitpid_data.loop = loop;
427
waitpid_data.status_p = &status;
429
source = g_child_watch_source_new (pid);
430
g_source_set_callback (source, (GSourceFunc)on_sync_waitpid, &waitpid_data, NULL);
431
g_source_attach (source, context);
432
g_source_unref (source);
434
g_main_loop_run (loop);
436
g_main_context_unref (context);
437
g_main_loop_unref (loop);
480
481
* should be a %NULL-terminated array of strings, to be passed as the
481
482
* argument vector for the child. The first string in @argv is of
482
483
* course the name of the program to execute. By default, the name of
483
* the program must be a full path; the <envar>PATH</envar> shell variable
484
* will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
484
* the program must be a full path. If @flags contains the
485
* %G_SPAWN_SEARCH_PATH flag, the <envar>PATH</envar> environment variable
486
* is used to search for the executable. If @flags contains the
487
* %G_SPAWN_SEARCH_PATH_FROM_ENVP flag, the <envar>PATH</envar> variable from
488
* @envp is used to search for the executable.
489
* If both the %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP
490
* flags are set, the <envar>PATH</envar> variable from @envp takes precedence
491
* over the environment variable.
485
493
* If the program name is not a full path and %G_SPAWN_SEARCH_PATH flag is not
486
494
* used, then the program will be run from the current directory (or
487
495
* @working_directory, if specified); this might be unexpected or even
547
555
* descriptors except stdin/stdout/stderr will be closed before
548
556
* calling exec() in the child. %G_SPAWN_SEARCH_PATH
549
557
* means that <literal>argv[0]</literal> need not be an absolute path, it
550
* will be looked for in the user's <envar>PATH</envar>.
558
* will be looked for in the <envar>PATH</envar> environment variable.
559
* %G_SPAWN_SEARCH_PATH_FROM_ENVP means need not be an absolute path, it
560
* will be looked for in the <envar>PATH</envar> variable from @envp. If
561
* both %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP are used,
562
* the value from @envp takes precedence over the environment.
551
563
* %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will
552
564
* be discarded, instead of going to the same location as the parent's
553
565
* standard output. If you use this flag, @standard_output must be %NULL.
686
699
* appropriate. Possible errors are those from g_spawn_sync() and those
687
700
* from g_shell_parse_argv().
689
* If @exit_status is non-%NULL, the exit status of the child is stored there as
690
* it would be returned by waitpid(); standard UNIX macros such as WIFEXITED()
691
* and WEXITSTATUS() must be used to evaluate the exit status.
702
* If @exit_status is non-%NULL, the platform-specific exit status of
703
* the child is stored there; see the documentation of
704
* g_spawn_check_exit_status() for how to use and interpret this.
693
706
* On Windows, please note the implications of g_shell_parse_argv()
694
707
* parsing @command_line. Parsing is done according to Unix shell rules, not
795
* g_spawn_check_exit_status:
796
* @exit_status: An exit code as returned from g_spawn_sync()
799
* Set @error if @exit_status indicates the child exited abnormally
800
* (e.g. with a nonzero exit code, or via a fatal signal).
802
* The g_spawn_sync() and g_child_watch_add() family of APIs return an
803
* exit status for subprocesses encoded in a platform-specific way.
804
* On Unix, this is guaranteed to be in the same format
805
* <literal>waitpid(2)</literal> returns, and on Windows it is
806
* guaranteed to be the result of
807
* <literal>GetExitCodeProcess()</literal>. Prior to the introduction
808
* of this function in GLib 2.34, interpreting @exit_status required
809
* use of platform-specific APIs, which is problematic for software
810
* using GLib as a cross-platform layer.
812
* Additionally, many programs simply want to determine whether or not
813
* the child exited successfully, and either propagate a #GError or
814
* print a message to standard error. In that common case, this
815
* function can be used. Note that the error message in @error will
816
* contain human-readable information about the exit status.
818
* The <literal>domain</literal> and <literal>code</literal> of @error
819
* have special semantics in the case where the process has an "exit
820
* code", as opposed to being killed by a signal. On Unix, this
821
* happens if <literal>WIFEXITED</literal> would be true of
822
* @exit_status. On Windows, it is always the case.
824
* The special semantics are that the actual exit code will be the
825
* code set in @error, and the domain will be %G_SPAWN_EXIT_ERROR.
826
* This allows you to differentiate between different exit codes.
828
* If the process was terminated by some means other than an exit
829
* status, the domain will be %G_SPAWN_ERROR, and the code will be
830
* %G_SPAWN_ERROR_FAILED.
832
* This function just offers convenience; you can of course also check
833
* the available platform via a macro such as %G_OS_UNIX, and use
834
* <literal>WIFEXITED()</literal> and <literal>WEXITSTATUS()</literal>
835
* on @exit_status directly. Do not attempt to scan or parse the
836
* error message string; it may be translated and/or change in future
839
* Returns: %TRUE if child exited successfully, %FALSE otherwise (and @error will be set)
843
g_spawn_check_exit_status (gint exit_status,
846
gboolean ret = FALSE;
848
if (WIFEXITED (exit_status))
850
if (WEXITSTATUS (exit_status) != 0)
852
g_set_error (error, G_SPAWN_EXIT_ERROR, WEXITSTATUS (exit_status),
853
_("Child process exited with code %ld"),
854
(long) WEXITSTATUS (exit_status));
858
else if (WIFSIGNALED (exit_status))
860
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
861
_("Child process killed by signal %ld"),
862
(long) WTERMSIG (exit_status));
865
else if (WIFSTOPPED (exit_status))
867
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
868
_("Child process stopped by signal %ld"),
869
(long) WSTOPSIG (exit_status));
874
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
875
_("Child process exited abnormally"));
782
885
exec_err_to_g_error (gint en)