libslack(coproc) - coprocess module
#include <slack/coproc.h>
pid_t coproc_open(int *to, int *from, const char *cmd, char * const *argv, char * const *envv); int coproc_close(pid_t pid, int *to, int *from); pid_t coproc_pty_open(int *masterfd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize, const char *cmd, char * const *argv, char * const *envv); int coproc_pty_close(pid_t pid, int *masterfd, const char *slavename);
This module contains functions for creating coprocesses that use either pipes or pseudo terminals for communication.
pid_t coproc_open(int *to, int *from, const char *cmd, char * const *argv, char * const *envv)
Starts a coprocess. cmd
is the name of the process or a shell command.
argv
is the command line argument vector to be passed to execve().
envv
is the environment variable vector to be passed to execve(). If
envv
is null
, the current environment is used. If cmd
is the name of a program, argv
must not be null
. If cmd
contains shell metacharacters, it will executed by sh -c
and argv
must be null
. This provides some protection from unintentionally invoking sh -c
. If
cmd
does not contain any shell metacharacters, but does contain a slash
character (/
), it is passed directly to execve(). If it doesn't contain a slash character, we search for the executable in
the directories specified by the PATH
variable. If the PATH
variable is not set, a default path is used: /bin:/usr/bin
for root; :/bin:/usr/bin
for other users. If permission is denied for a file (execve() returns EACCES
), then searching continues. If the header of a file isn't recognised (execve() returns ENOEXEC
), then /bin/sh
will be executed with
cmd
as its first argument. This is done so that shell scripts without a
#!
line can be used. If this attempt fails, no further searching is done.
Communication with the coprocess occurs over pipes. Data written to *to
can be read from the standard input of the coprocess. Data written to the
standard output or standard error of the coprocess may be read from
*from
. On success, returns the process id of the coprocess. On error, returns -1
with errno
set appropriately.
Note: That this can only be used with coprocesses that do not buffer I/O or that explicitly set line buffering with setbuf() or setvbuf(). If a potential coprocess uses standard I/O and you don't have access to the source code, you will need to use coproc_pty_open() instead.
Note: If cmd does contain shell metacharacters, make sure that the program provides the command to execute. If the command comes from outside the program, do not trust it. Verify that it is safe to execute.
int coproc_close(pid_t pid, int *to, int *from)
Closes the coprocess referred to by pid
which must have been obtained from coproc_open(). *to
will be closed and set to -1
if it is not already -1
. *from
will be closed and set to -1
if it is not already
-1
. The current process will then wait for the coprocess to terminate by
calling waitpid(). On success, returns the status of the child process as determined by waitpid(). On error, returns -1
with errno
set appropriately.
pid_t coproc_pty_open(int *masterfd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize, const char *cmd, char * const *argv, char * const *envv)
Equivalent to coproc_open() except that communication with the coprocess occurs over a pseudo terminal.
This is useful when the coprocess uses standard I/O and you don't have the
source code. Standard I/O is fully buffered unless connected to a terminal. *masterfd
is set to the master side of a pseudo terminal. Data written to *masterfd
can be read from the standard input of the coprocess. Data written to the
standard output or error of the coprocess can be read from *masterfd
. The device name of the slave side of the pseudo terminal is stored in the
buffer pointed to by
slavename
which must be able to store at least 64 bytes. slavenamesize
is the size of the buffer pointed to by slavename
. No more than
slavenamesize
bytes will be written into the buffer pointed to by
slavename
including the terminating nul
byte. If slave_termios
is not null, it is passed to tcsetattr() with the command TCSANOW
to set the terminal attributes of the slave device. If slave_winsize
is not null, it is passed to ioctl() with the command TIOCSWINSZ
to set the window size of the slave device. On success, returns 0
. On error, returns
-1
with errno
set appropriately.
int coproc_pty_close(pid_t pid, int *masterfd, const char *slavename)
Closes the coprocess referred to by pid
which must have been obtained from coproc_pty_open(). The slave side of the pseudo terminal is released with pty_release() and *masterfd
is closed and set to -1
if it is not already -1
. The current process will then wait for the coprocess to terminate by
calling waitpid(). On success, returns the status of the child process as determined by waitpid(). On error, returns -1
with
errno
set appropriately.
Additional errors may be generated and returned from the underlying system calls. See their manual pages.
Invalid arguments were passed to coproc_open() or coproc_pty_open().
MT-Safe (coproc_pty_open() is MT-Safe iff the pseudo module is MT-Safe).
libslack(3), execve(2), system(3), popen(3), waitpid(2), sh(1), pseudo(3)>
20011109 raf <raf@raf.org>