spawn_async_with_pipes


Description:

public bool spawn_async_with_pipes (string? working_directory, string[] argv, string[]? envp, SpawnFlags _flags, SpawnChildSetupFunc? child_setup, out Pid child_pid, out int standard_input = null, out int standard_output = null, out int standard_error = null) throws SpawnError

Identical to spawn_async_with_pipes_and_fds but with `n_fds` set to zero, so no FD assignments are used.

Example: Spawn with pipes, async:

private static bool process_line (IOChannel channel, IOCondition condition, string stream_name) {
if (condition == IOCondition.HUP) {
print ("%s: The fd has been closed.\n", stream_name);
return false;
}

try {
string line;
channel.read_line (out line, null, null);
print ("%s: %s", stream_name, line);
} catch (IOChannelError e) {
print ("%s: IOChannelError: %s\n", stream_name, e.message);
return false;
} catch (ConvertError e) {
print ("%s: ConvertError: %s\n", stream_name, e.message);
return false;
}

return true;
}

public static int main (string[] args) {
MainLoop loop = new MainLoop ();
try {
string[] spawn_args = {"ls", "-l", "-h"};
string[] spawn_env = Environ.get ();
Pid child_pid;

int standard_input;
int standard_output;
int standard_error;

Process.spawn_async_with_pipes ("/",
spawn_args,
spawn_env,
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
null,
out child_pid,
out standard_input,
out standard_output,
out standard_error);

// stdout:
IOChannel output = new IOChannel.unix_new (standard_output);
output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
return process_line (channel, condition, "stdout");
});

// stderr:
IOChannel error = new IOChannel.unix_new (standard_error);
error.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
return process_line (channel, condition, "stderr");
});

ChildWatch.add (child_pid, (pid, status) => {
// Triggered when the child indicated by child_pid exits
Process.close_pid (pid);
loop.quit ();
});

loop.run ();
} catch (SpawnError e) {
print ("Error: %s\n", e.message);
}
return 0;
}

valac --pkg glib-2.0 GLib.Process.spawn_async_with_pipes.vala

Parameters:

working_directory

child's current working directory, or null to inherit parent's, in the GLib file name encoding

argv

child's argument vector, in the GLib file name encoding; it must be non-empty and null-terminated

envp

child's environment, or null to inherit parent's, in the GLib file name encoding

child_setup

function to run in the child just before exec

child_pid

return location for child process ID, or null

standard_input

return location for file descriptor to write to child's stdin, or null

standard_output

return location for file descriptor to read child's stdout, or null

standard_error

return location for file descriptor to read child's stderr, or null

flags

flags from SpawnFlags

user_data

user data for child_setup

Returns:

true on success, false if an error was set


Namespace: GLib.Process
Package: glib-2.0