Package ch.bailu.gtk.gio
Class Subprocess
java.lang.Object
ch.bailu.gtk.type.Type
ch.bailu.gtk.type.Pointer
ch.bailu.gtk.gobject.Object
ch.bailu.gtk.gio.Subprocess
- All Implemented Interfaces:
PointerInterface
#GSubprocess allows the creation of and interaction with child
processes.
Processes can be communicated with using standard GIO-style APIs (ie:
#GInputStream, #GOutputStream). There are GIO-style APIs to wait for
process termination (ie: cancellable and with an asynchronous
variant).
There is an API to force a process to terminate, as well as a
race-free API for sending UNIX signals to a subprocess.
One major advantage that GIO brings over the core GLib library is
comprehensive API for asynchronous I/O, such
g_output_stream_splice_async(). This makes GSubprocess
significantly more powerful and flexible than equivalent APIs in
some other languages such as the `subprocess.py`
included with Python. For example, using #GSubprocess one could
create two child processes, reading standard output from the first,
processing it, and writing to the input stream of the second, all
without blocking the main loop.
A powerful g_subprocess_communicate() API is provided similar to the
`communicate()` method of `subprocess.py`. This enables very easy
interaction with a subprocess that has been opened with pipes.
#GSubprocess defaults to tight control over the file descriptors open
in the child process, avoiding dangling-fd issues that are caused by
a simple fork()/exec(). The only open file descriptors in the
spawned process are ones that were explicitly specified by the
#GSubprocess API (unless %G_SUBPROCESS_FLAGS_INHERIT_FDS was
specified).
#GSubprocess will quickly reap all child processes as they exit,
avoiding "zombie processes" remaining around for long periods of
time. g_subprocess_wait() can be used to wait for this to happen,
but it will happen even without the call being explicitly made.
As a matter of principle, #GSubprocess has no API that accepts
shell-style space-separated strings. It will, however, match the
typical shell behaviour of searching the PATH for executables that do
not contain a directory separator in their name. By default, the `PATH`
of the current process is used. You can specify
%G_SUBPROCESS_FLAGS_SEARCH_PATH_FROM_ENVP to use the `PATH` of the
launcher environment instead.
#GSubprocess attempts to have a very simple API for most uses (ie:
spawning a subprocess with arguments and support for most typical
kinds of input and output redirection). See g_subprocess_new(). The
#GSubprocessLauncher API is provided for more complicated cases
(advanced types of redirection, environment variable manipulation,
change of working directory, child setup functions, etc).
A typical use of #GSubprocess will involve calling
g_subprocess_new(), followed by g_subprocess_wait_async() or
g_subprocess_wait(). After the process exits, the status can be
checked using functions such as g_subprocess_get_if_exited() (which
are similar to the familiar WIFEXITED-style POSIX macros).
processes.
Processes can be communicated with using standard GIO-style APIs (ie:
#GInputStream, #GOutputStream). There are GIO-style APIs to wait for
process termination (ie: cancellable and with an asynchronous
variant).
There is an API to force a process to terminate, as well as a
race-free API for sending UNIX signals to a subprocess.
One major advantage that GIO brings over the core GLib library is
comprehensive API for asynchronous I/O, such
g_output_stream_splice_async(). This makes GSubprocess
significantly more powerful and flexible than equivalent APIs in
some other languages such as the `subprocess.py`
included with Python. For example, using #GSubprocess one could
create two child processes, reading standard output from the first,
processing it, and writing to the input stream of the second, all
without blocking the main loop.
A powerful g_subprocess_communicate() API is provided similar to the
`communicate()` method of `subprocess.py`. This enables very easy
interaction with a subprocess that has been opened with pipes.
#GSubprocess defaults to tight control over the file descriptors open
in the child process, avoiding dangling-fd issues that are caused by
a simple fork()/exec(). The only open file descriptors in the
spawned process are ones that were explicitly specified by the
#GSubprocess API (unless %G_SUBPROCESS_FLAGS_INHERIT_FDS was
specified).
#GSubprocess will quickly reap all child processes as they exit,
avoiding "zombie processes" remaining around for long periods of
time. g_subprocess_wait() can be used to wait for this to happen,
but it will happen even without the call being explicitly made.
As a matter of principle, #GSubprocess has no API that accepts
shell-style space-separated strings. It will, however, match the
typical shell behaviour of searching the PATH for executables that do
not contain a directory separator in their name. By default, the `PATH`
of the current process is used. You can specify
%G_SUBPROCESS_FLAGS_SEARCH_PATH_FROM_ENVP to use the `PATH` of the
launcher environment instead.
#GSubprocess attempts to have a very simple API for most uses (ie:
spawning a subprocess with arguments and support for most typical
kinds of input and output redirection). See g_subprocess_new(). The
#GSubprocessLauncher API is provided for more complicated cases
(advanced types of redirection, environment variable manipulation,
change of working directory, child setup functions, etc).
A typical use of #GSubprocess will involve calling
g_subprocess_new(), followed by g_subprocess_wait_async() or
g_subprocess_wait(). After the process exits, the status can be
checked using functions such as g_subprocess_get_if_exited() (which
are similar to the familiar WIFEXITED-style POSIX macros).
-
Nested Class Summary
Nested classes/interfaces inherited from class ch.bailu.gtk.gobject.Object
Object.OnBindingTransformFunc, Object.OnDestroyNotify, Object.OnDuplicateFunc, Object.OnNotify, Object.OnToggleNotify, Object.OnWeakNotify
-
Field Summary
Fields inherited from class ch.bailu.gtk.gobject.Object
SIGNAL_ON_NOTIFY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionImplements interfaceInitable
.void
communicateAsync
(Bytes stdin_buf, Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, Pointer user_data) Asynchronous version of g_subprocess_communicate().boolean
communicateUtf8
(Str stdin_buf, Cancellable cancellable, Strs stdout_buf, Strs stderr_buf) Like g_subprocess_communicate(), but validates the output of the
process as UTF-8, and returns it as a regular NUL terminated string.boolean
communicateUtf8
(String stdin_buf, Cancellable cancellable, Strs stdout_buf, Strs stderr_buf) Like g_subprocess_communicate(), but validates the output of the
process as UTF-8, and returns it as a regular NUL terminated string.void
communicateUtf8Async
(Str stdin_buf, Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, Pointer user_data) Asynchronous version of g_subprocess_communicate_utf8().void
communicateUtf8Async
(String stdin_buf, Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, Pointer user_data) Asynchronous version of g_subprocess_communicate_utf8().boolean
communicateUtf8Finish
(AsyncResult result, Strs stdout_buf, Strs stderr_buf) Complete an invocation of g_subprocess_communicate_utf8_async().void
Use an operating-system specific method to attempt an immediate,
forceful termination of the process.static ClassHandler
int
Check the exit status of the subprocess, given that it exited
normally.On UNIX, returns the process ID as a decimal string.boolean
Check if the given subprocess exited normally (ie: by way of exit()
or return from main()).boolean
Check if the given subprocess terminated in response to a signal.static int
static long
static TypeSystem.TypeSize
int
Gets the raw status code of the process, as from waitpid().Gets the #GInputStream from which to read the stderr output of
@subprocess.Gets the #GOutputStream that you can write to in order to give data
to the stdin of @subprocess.Gets the #GInputStream from which to read the stdout output of
@subprocess.boolean
Checks if the process was "successful".int
Get the signal number that caused the subprocess to terminate, given
that it terminated due to a signal.static long
static TypeSystem.TypeSize
void
sendSignal
(int signal_num) Sends the UNIX signal @signal_num to the subprocess, if it is still
running.boolean
wait
(Cancellable cancellable) Synchronously wait for the subprocess to terminate.void
waitAsync
(Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, Pointer user_data) Wait for the subprocess to terminate.boolean
waitCheck
(Cancellable cancellable) Combines g_subprocess_wait() with g_spawn_check_wait_status().void
waitCheckAsync
(Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, Pointer user_data) Combines g_subprocess_wait_async() with g_spawn_check_wait_status().boolean
waitCheckFinish
(AsyncResult result) Collects the result of a previous call to
g_subprocess_wait_check_async().boolean
waitFinish
(AsyncResult result) Collects the result of a previous call to
g_subprocess_wait_async().Methods inherited from class ch.bailu.gtk.gobject.Object
addToggleRef, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, disconnect, disconnect, dupData, dupData, dupQdata, forceFloating, freezeNotify, get, get, getData, getData, getProperty, getProperty, getQdata, interfaceFindProperty, interfaceInstallProperty, isFloating, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, replaceData, replaceData, replaceQdata, runDispose, set, set, setData, setData, setDataFull, setDataFull, setProperty, setProperty, setQdata, setQdataFull, stealData, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref
Methods inherited from class ch.bailu.gtk.type.Pointer
asCPointer, cast, connectSignal, disconnectSignals, disconnectSignals, equals, hashCode, throwIfNull, throwNullPointerException, toString, unregisterCallbacks, unregisterCallbacks
Methods inherited from class ch.bailu.gtk.type.Type
asCPointer, asCPointer, asCPointerNotNull, asJnaPointer, asJnaPointer, asPointer, asPointer, cast, cast, throwIfNull
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.bailu.gtk.type.PointerInterface
asCPointerNotNull, asJnaPointer, asPointer, isNotNull, isNull
-
Constructor Details
-
Subprocess
-
-
Method Details
-
getClassHandler
-
communicateAsync
public void communicateAsync(@Nullable Bytes stdin_buf, @Nullable Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, @Nullable Pointer user_data) Asynchronous version of g_subprocess_communicate(). Complete
invocation with g_subprocess_communicate_finish().- Parameters:
stdin_buf
- Input data, or %NULLcancellable
- Cancellablecallback
- Callbackuser_data
- User data
-
communicateUtf8
public boolean communicateUtf8(@Nullable Str stdin_buf, @Nullable Cancellable cancellable, @Nullable Strs stdout_buf, @Nullable Strs stderr_buf) throws AllocationError Like g_subprocess_communicate(), but validates the output of the
process as UTF-8, and returns it as a regular NUL terminated string.
On error, @stdout_buf and @stderr_buf will be set to undefined values and
should not be used.- Parameters:
stdin_buf
- data to send to the stdin of the subprocess, or %NULLcancellable
- a #GCancellablestdout_buf
- data read from the subprocess stdoutstderr_buf
- data read from the subprocess stderr- Returns:
- Throws:
AllocationError
-
communicateUtf8
public boolean communicateUtf8(String stdin_buf, @Nullable Cancellable cancellable, @Nullable Strs stdout_buf, @Nullable Strs stderr_buf) throws AllocationError Like g_subprocess_communicate(), but validates the output of the
process as UTF-8, and returns it as a regular NUL terminated string.
On error, @stdout_buf and @stderr_buf will be set to undefined values and
should not be used.- Parameters:
stdin_buf
- data to send to the stdin of the subprocess, or %NULLcancellable
- a #GCancellablestdout_buf
- data read from the subprocess stdoutstderr_buf
- data read from the subprocess stderr- Returns:
- Throws:
AllocationError
-
communicateUtf8Async
public void communicateUtf8Async(@Nullable Str stdin_buf, @Nullable Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, @Nullable Pointer user_data) Asynchronous version of g_subprocess_communicate_utf8(). Complete
invocation with g_subprocess_communicate_utf8_finish().- Parameters:
stdin_buf
- Input data, or %NULLcancellable
- Cancellablecallback
- Callbackuser_data
- User data
-
communicateUtf8Async
public void communicateUtf8Async(String stdin_buf, @Nullable Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, @Nullable Pointer user_data) Asynchronous version of g_subprocess_communicate_utf8(). Complete
invocation with g_subprocess_communicate_utf8_finish().- Parameters:
stdin_buf
- Input data, or %NULLcancellable
- Cancellablecallback
- Callbackuser_data
- User data
-
communicateUtf8Finish
public boolean communicateUtf8Finish(@Nonnull AsyncResult result, @Nullable Strs stdout_buf, @Nullable Strs stderr_buf) throws AllocationError Complete an invocation of g_subprocess_communicate_utf8_async().- Parameters:
result
- Resultstdout_buf
- Return location for stdout datastderr_buf
- Return location for stderr data- Returns:
- Throws:
AllocationError
-
forceExit
public void forceExit()Use an operating-system specific method to attempt an immediate,
forceful termination of the process. There is no mechanism to
determine whether or not the request itself was successful;
however, you can use g_subprocess_wait() to monitor the status of
the process after calling this function.
On Unix, this function sends %SIGKILL. -
getExitStatus
public int getExitStatus()Check the exit status of the subprocess, given that it exited
normally. This is the value passed to the exit() system call or the
return value from main.
This is equivalent to the system WEXITSTATUS macro.
It is an error to call this function before g_subprocess_wait() and
unless g_subprocess_get_if_exited() returned %TRUE.- Returns:
- the exit status
-
getIdentifier
On UNIX, returns the process ID as a decimal string.
On Windows, returns the result of GetProcessId() also as a string.
If the subprocess has terminated, this will return %NULL.- Returns:
- the subprocess identifier, or %NULL if the subprocess has terminated
-
getIfExited
public boolean getIfExited()Check if the given subprocess exited normally (ie: by way of exit()
or return from main()).
This is equivalent to the system WIFEXITED macro.
It is an error to call this function before g_subprocess_wait() has
returned.- Returns:
- %TRUE if the case of a normal exit
-
getIfSignaled
public boolean getIfSignaled()Check if the given subprocess terminated in response to a signal.
This is equivalent to the system WIFSIGNALED macro.
It is an error to call this function before g_subprocess_wait() has
returned.- Returns:
- %TRUE if the case of termination due to a signal
-
getStatus
public int getStatus()Gets the raw status code of the process, as from waitpid().
This value has no particular meaning, but it can be used with the
macros defined by the system headers such as WIFEXITED. It can also
be used with g_spawn_check_wait_status().
It is more likely that you want to use g_subprocess_get_if_exited()
followed by g_subprocess_get_exit_status().
It is an error to call this function before g_subprocess_wait() has
returned.- Returns:
- the (meaningless) waitpid() exit status from the kernel
-
getStderrPipe
Gets the #GInputStream from which to read the stderr output of
@subprocess.
The process must have been created with %G_SUBPROCESS_FLAGS_STDERR_PIPE,
otherwise %NULL will be returned.- Returns:
- the stderr pipe
-
getStdinPipe
Gets the #GOutputStream that you can write to in order to give data
to the stdin of @subprocess.
The process must have been created with %G_SUBPROCESS_FLAGS_STDIN_PIPE and
not %G_SUBPROCESS_FLAGS_STDIN_INHERIT, otherwise %NULL will be returned.- Returns:
- the stdout pipe
-
getStdoutPipe
Gets the #GInputStream from which to read the stdout output of
@subprocess.
The process must have been created with %G_SUBPROCESS_FLAGS_STDOUT_PIPE,
otherwise %NULL will be returned.- Returns:
- the stdout pipe
-
getSuccessful
public boolean getSuccessful()Checks if the process was "successful". A process is considered
successful if it exited cleanly with an exit status of 0, either by
way of the exit() system call or return from main().
It is an error to call this function before g_subprocess_wait() has
returned.- Returns:
- %TRUE if the process exited cleanly with a exit status of 0
-
getTermSig
public int getTermSig()Get the signal number that caused the subprocess to terminate, given
that it terminated due to a signal.
This is equivalent to the system WTERMSIG macro.
It is an error to call this function before g_subprocess_wait() and
unless g_subprocess_get_if_signaled() returned %TRUE.- Returns:
- the signal causing termination
-
sendSignal
public void sendSignal(int signal_num) Sends the UNIX signal @signal_num to the subprocess, if it is still
running.
This API is race-free. If the subprocess has terminated, it will not
be signalled.
This API is not available on Windows.- Parameters:
signal_num
- the signal number to send
-
wait
Synchronously wait for the subprocess to terminate.
After the process terminates you can query its exit status with
functions such as g_subprocess_get_if_exited() and
g_subprocess_get_exit_status().
This function does not fail in the case of the subprocess having
abnormal termination. See g_subprocess_wait_check() for that.
Cancelling @cancellable doesn't kill the subprocess. Call
g_subprocess_force_exit() if it is desirable.- Parameters:
cancellable
- a #GCancellable- Returns:
- %TRUE on success, %FALSE if @cancellable was cancelled
- Throws:
AllocationError
-
waitAsync
public void waitAsync(@Nullable Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, @Nullable Pointer user_data) Wait for the subprocess to terminate.
This is the asynchronous version of g_subprocess_wait().- Parameters:
cancellable
- a #GCancellable, or %NULLcallback
- a #GAsyncReadyCallback to call when the operation is completeuser_data
- user_data for @callback
-
waitCheck
Combines g_subprocess_wait() with g_spawn_check_wait_status().- Parameters:
cancellable
- a #GCancellable- Returns:
- %TRUE on success, %FALSE if process exited abnormally, or @cancellable was cancelled
- Throws:
AllocationError
-
waitCheckAsync
public void waitCheckAsync(@Nullable Cancellable cancellable, Subprocess.OnAsyncReadyCallback callback, @Nullable Pointer user_data) Combines g_subprocess_wait_async() with g_spawn_check_wait_status().
This is the asynchronous version of g_subprocess_wait_check().- Parameters:
cancellable
- a #GCancellable, or %NULLcallback
- a #GAsyncReadyCallback to call when the operation is completeuser_data
- user_data for @callback
-
waitCheckFinish
Collects the result of a previous call to
g_subprocess_wait_check_async().- Parameters:
result
- the #GAsyncResult passed to your #GAsyncReadyCallback- Returns:
- %TRUE if successful, or %FALSE with @error set
- Throws:
AllocationError
-
waitFinish
Collects the result of a previous call to
g_subprocess_wait_async().- Parameters:
result
- the #GAsyncResult passed to your #GAsyncReadyCallback- Returns:
- %TRUE if successful, or %FALSE with @error set
- Throws:
AllocationError
-
asInitable
Implements interfaceInitable
. Call this to get access to interface functions.- Returns:
Initable
-
getTypeID
public static long getTypeID() -
getParentTypeID
public static long getParentTypeID() -
getTypeSize
-
getParentTypeSize
-
getInstanceSize
public static int getInstanceSize()
-