libgslutil  1.2.3
Functions

Functions

void close_all_file_descriptors (int leave_std_open)
 Close all open file descriptors. More...
 
pid_t fork_and_execve (const char *filename, char *const argv[], char *const envp[], int *stdin, int *stdout, int *stderr)))
 Fork a child process and use it to execute another executable. More...
 

Detailed Description

A set of routines for easy process creation and management.

Function Documentation

◆ close_all_file_descriptors()

void close_all_file_descriptors ( int  leave_std_open)

Close all open file descriptors.

Parameters
leave_std_openPass 0 to close stdin, stdout and stderr or non-0 to keep them open.

This function attempts to close all file descriptors that the process has open. It calls opendir("/proc/self/fd"), closing each file descriptor in turn. If a non-0 value is passed for leave_std_open, then file descriptors 0, 1 and 2 (conventionally stdin, stdout and stderr) will be left open.

◆ fork_and_execve()

pid_t fork_and_execve ( const char *  filename,
char *const  argv[],
char *const  envp[],
int *  stdin,
int *  stdout,
int *  stderr 
)

Fork a child process and use it to execute another executable.

Parameters
filenameFull pathname (possibly relative) to executable.
argvArgument vector (arg 0 is filename as perceived by executable).
envpEnvironment vector (may be 0 to inherit current environment).
[out]stdinFile descriptor for stdin write-pipe stored here, may be 0.
[out]stdoutFile descriptor for stdout read-pipe stored here, may be 0.
[out]stderrFile descriptor for stderr read-pipe stored here, may be 0.
Return values
(pid_t)-1on error (logged – and see errno).
Returns
PID of child process on success.

Forks a child process and uses the child to execute another executable. You must specify the executable's full filename and its argument vector argv. You may optionally specify an environment with envp (but leaving this as zero will cause the child to inherit its parent's environment).

The child will have only three file descriptors open: 0 (stdin), 1 (stdout) and 2 (stderr). If you want to connect these to the parent with pipes, pass in the address of an integer where the pipe file descriptor can be stored. Otherwise, the child's file descriptors will point at /dev/null.

The child process will not have any blocked signals.

This call may fail if pipe() fails or if fork() fails. Either case is logged to syslog. If execve() fails, the child will exit with a status of 1, but that will not be indicated as an error through the return value of this function. It will be logged to syslog, however.