child's current working directory, or null to inherit parent's, in the GLib file name encoding
child's argument vector, in the GLib file name encoding; it must be non-empty and null-terminated
child's environment, or null to inherit parent's, in the GLib file name encoding
flags from GLib.SpawnFlags
function to run in the child just before exec()
file descriptor to use for child's stdin, or -1
file descriptor to use for child's stdout, or -1
file descriptor to use for child's stderr, or -1
array of FDs from the parent process to make available in the child process
array of FDs to remap source_fds to in the child process
true on success, false if an error was set
Executes a child program asynchronously (your program will not block waiting for the child to exit).
The child program is specified by the only argument that must be provided,
argv.argvshould be anull-terminated array of strings, to be passed as the argument vector for the child. The first string inargvis of course the name of the program to execute. By default, the name of the program must be a full path. Ifflagscontains the GLib.SpawnFlags.SEARCH_PATH flag, thePATHenvironment variable is used to search for the executable. Ifflagscontains the GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP flag, thePATHvariable fromenvpis used to search for the executable. If both the GLib.SpawnFlags.SEARCH_PATH and GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP flags are set, thePATHvariable fromenvptakes precedence over the environment variable.If the program name is not a full path and GLib.SpawnFlags.SEARCH_PATH flag is not used, then the program will be run from the current directory (or
working_directory, if specified); this might be unexpected or even dangerous in some cases when the current directory is world-writable.On Windows, note that all the string or string vector arguments to this function and the other
g_spawn*()functions are in UTF-8, the GLib file name encoding. Unicode characters that are not part of the system codepage passed in these arguments will be correctly available in the spawned program only if it uses wide character API to retrieve its command line. For C programs built with Microsoft's tools it is enough to make the program have awmain()instead ofmain().wmain()has a wide character argument vector as parameter.At least currently, mingw doesn't support
wmain(), so if you use mingw to develop the spawned program, it should callg_win32_get_command_line()to get arguments in UTF-8.On Windows the low-level child process creation API
CreateProcess()doesn't use argument vectors, but a command line. The C runtime library'sspawn*()family of functions (whichg_spawn_async_with_pipes()eventually calls) paste the argument vector elements together into a command line, and the C runtime startup code does a corresponding reconstruction of an argument vector from the command line, to be passed tomain(). Complications arise when you have argument vector elements that contain spaces or double quotes. Thespawn*()functions don't do any quoting or escaping, but on the other hand the startup code does do unquoting and unescaping in order to enable receiving arguments with embedded spaces or double quotes. To work around this asymmetry,g_spawn_async_with_pipes()will do quoting and escaping on argument vector elements that need it before calling the C runtimespawn()function.The returned
child_pidon Windows is a handle to the child process, not its identifier. Process handles and process identifiers are different concepts on Windows.envpis anull-terminated array of strings, where each string has the formKEY=VALUE. This will become the child's environment. Ifenvpisnull, the child inherits its parent's environment.flagsshould be the bitwise OR of any flags you want to affect the function's behaviour. The GLib.SpawnFlags.DO_NOT_REAP_CHILD means that the child will not automatically be reaped; you must use a child watch (g_child_watch_add()) to be notified about the death of the child process, otherwise it will stay around as a zombie process until this process exits. Eventually you must callg_spawn_close_pid()on thechild_pid, in order to free resources which may be associated with the child process. (On Unix, using a child watch is equivalent to callingwaitpid()or handling theSIGCHLDsignal manually. On Windows, callingg_spawn_close_pid()is equivalent to callingCloseHandle()on the process handle returned inchild_pid). Seeg_child_watch_add().Open UNIX file descriptors marked as
FD_CLOEXECwill be automatically closed in the child process. GLib.SpawnFlags.LEAVE_DESCRIPTORS_OPEN means that other open file descriptors will be inherited by the child; otherwise all descriptors except stdin/stdout/stderr will be closed before callingexec()in the child. GLib.SpawnFlags.SEARCH_PATH means thatargv[0] need not be an absolute path, it will be looked for in thePATHenvironment variable. GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP means need not be an absolute path, it will be looked for in thePATHvariable fromenvp. If both GLib.SpawnFlags.SEARCH_PATH and GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP are used, the value fromenvptakes precedence over the environment.GLib.SpawnFlags.CHILD_INHERITS_STDIN means that the child will inherit the parent's standard input (by default, the child's standard input is attached to
/dev/null). GLib.SpawnFlags.STDIN_FROM_DEV_NULL explicitly imposes the default behavior. Both flags cannot be enabled at the same time and, in both cases, thestdin_pipe_outargument is ignored.GLib.SpawnFlags.STDOUT_TO_DEV_NULL means that the child's standard output will be discarded (by default, it goes to the same location as the parent's standard output). GLib.SpawnFlags.CHILD_INHERITS_STDOUT explicitly imposes the default behavior. Both flags cannot be enabled at the same time and, in both cases, the
stdout_pipe_outargument is ignored.GLib.SpawnFlags.STDERR_TO_DEV_NULL means that the child's standard error will be discarded (by default, it goes to the same location as the parent's standard error). GLib.SpawnFlags.CHILD_INHERITS_STDERR explicitly imposes the default behavior. Both flags cannot be enabled at the same time and, in both cases, the
stderr_pipe_outargument is ignored.It is valid to pass the same FD in multiple parameters (e.g. you can pass a single FD for both
stdout_fdandstderr_fd, and include it insource_fdstoo).source_fdsandtarget_fdsallow zero or more FDs from this process to be remapped to different FDs in the spawned process. Ifn_fdsis greater than zero,source_fdsandtarget_fdsmust both be non-nulland the same length. Each FD insource_fdsis remapped to the FD number at the same index intarget_fds. The source and target FD may be equal to simply propagate an FD to the spawned process. FD remappings are processed after standard FDs, so any target FDs which equalstdin_fd,stdout_fdorstderr_fdwill overwrite them in the spawned process.source_fdsis supported on Windows since 2.72.GLib.SpawnFlags.FILE_AND_ARGV_ZERO means that the first element of
argvis the file to execute, while the remaining elements are the actual argument vector to pass to the file. Normallyg_spawn_async_with_pipes()usesargv[0] as the file to execute, and passes all ofargvto the child.child_setupanduser_dataare a function and user data. On POSIX platforms, the function is called in the child after GLib has performed all the setup it plans to perform (including creating pipes, closing file descriptors, etc.) but before callingexec(). That is,child_setupis called just before callingexec()in the child. Obviously actions taken in this function will only affect the child, not the parent.On Windows, there is no separate
fork()andexec()functionality. Child processes are created and run with a single API call,CreateProcess(). There is no sensible thingchild_setupcould be used for on Windows so it is ignored and not called.If non-
null,child_pidwill on Unix be filled with the child's process ID. You can use the process ID to send signals to the child, or to useg_child_watch_add()(orwaitpid()) if you specified the GLib.SpawnFlags.DO_NOT_REAP_CHILD flag. On Windows,child_pidwill be filled with a handle to the child process only if you specified the GLib.SpawnFlags.DO_NOT_REAP_CHILD flag. You can then access the child process using the Win32 API, for example wait for its termination with theWaitFor*()functions, or examine its exit code withGetExitCodeProcess(). You should close the handle withCloseHandle()org_spawn_close_pid()when you no longer need it.If non-
null, thestdin_pipe_out,stdout_pipe_out,stderr_pipe_outlocations will be filled with file descriptors for writing to the child's standard input or reading from its standard output or standard error. The caller ofg_spawn_async_with_pipes()must close these file descriptors when they are no longer in use. If these parameters arenull, the corresponding pipe won't be created.If
stdin_pipe_outisnull, the child's standard input is attached to/dev/nullunless GLib.SpawnFlags.CHILD_INHERITS_STDIN is set.If
stderr_pipe_outis NULL, the child's standard error goes to the same location as the parent's standard error unless GLib.SpawnFlags.STDERR_TO_DEV_NULL is set.If
stdout_pipe_outis NULL, the child's standard output goes to the same location as the parent's standard output unless GLib.SpawnFlags.STDOUT_TO_DEV_NULL is set.errorcan benullto ignore errors, or non-nullto report errors. If an error is set, the function returnsfalse. Errors are reported even if they occur in the child (for example if the executable in ``argv[0]is not found). Typically themessagefield of returned errors should be displayed to users. Possible errors are those from theG_SPAWN_ERRORdomain.If an error occurs,
child_pid,stdin_pipe_out,stdout_pipe_out, andstderr_pipe_outwill not be filled with valid values.If
child_pidis notnulland an error does not occur then the returned process reference must be closed usingg_spawn_close_pid().On modern UNIX platforms, GLib can use an efficient process launching codepath driven internally by
posix_spawn(). This has the advantage of avoiding the fork-time performance costs of cloning the parent process address space, and avoiding associated memory overcommit checks that are not relevant in the context of immediately executing a distinct process. This optimized codepath will be used provided that the following conditions are met:working_directoryisnullchild_setupisnullIf you are writing a GTK application, and the program you are spawning is a graphical application too, then to ensure that the spawned program opens its windows on the right screen, you may want to use
GdkAppLaunchContext,GAppLaunchContext, or set theDISPLAYenvironment variable.