libgslutil
1.2.3
|
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... | |
A set of routines for easy process creation and management.
void close_all_file_descriptors | ( | int | leave_std_open | ) |
Close all open file descriptors.
leave_std_open | Pass 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.
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.
filename | Full pathname (possibly relative) to executable. | |
argv | Argument vector (arg 0 is filename as perceived by executable). | |
envp | Environment vector (may be 0 to inherit current environment). | |
[out] | stdin | File descriptor for stdin write-pipe stored here, may be 0. |
[out] | stdout | File descriptor for stdout read-pipe stored here, may be 0. |
[out] | stderr | File descriptor for stderr read-pipe stored here, may be 0. |
(pid_t)-1 | on error (logged – and see errno). |
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.