libslack(coproc) - coprocess module
#include <slack/std.h> #include <slack/coproc.h>
pid_t coproc_open(int *to, int *from, int *err, const char *cmd, char * const *argv, char * const *envv, void (*action)(void *data), void *data); int coproc_close(pid_t pid, int *to, int *from, int *err); 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, void (*action)(void *data), void *data); 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, int *err, const char *cmd, char * const *argv, char * const *envv, void (*action)(void *data), void *data)
cmd
is the name of the process or a shell command.
argv
is the command line argument vector to be passed to execve(2).
envv
is the environment variable vector to be passed to execve(2). 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(2). 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(2) returns EACCES
),
then searching continues. If the header of a file isn't recognised
(execve(2) 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
and *err
respectively. If the function pointer action
is not null
,
it is invoked in the child process between the calls to fork(2) and
execve(2). Specifically, it is invoked before the pipes are duplicated
onto stdin
, stdout
and stderr
. data is passed as the argument to
action. This is useful when you need to prevent the coprocess from
inheriting certain process attributes. It can be used to ignore signals, set
default signal handlers, modify the signal mask and close files. 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 (or no buffering) with setbuf(3) or setvbuf(3). 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(3) instead.
Note: If cmd does contain shell metacharacters, make sure that the application provides the command to execute. If the command comes from outside the application, do not trust it. Verify that it is safe to execute.
int coproc_close(pid_t pid, int *to, int *from, int *err)
pid
which must have been obtained
from coproc_open(3). *to
, *from
and *err
will be closed and set
to -1
if they are not already -1
. The current process will then wait
for the coprocess to terminate by calling waitpid(2). On success, returns
the status of the child process as determined by waitpid(2). On error,
returns -1
with errno
set appropriately. Note: If waitpid(2) is
interrupted by a signal, coproc_close(3) will return -1
with errno
set to EINTR
. The caller has to call coproc_close(3) (or just
waitpid(2)) again until it succeeds (or a real error occurs).
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, void (*action)(void *data), void *data)
*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(3) with the command TCSANOW
to set
the terminal attributes of the slave device. If slave_winsize
is not
null, it is passed to ioctl(2) 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)
pid
which must have been obtained
from coproc_pty_open(3). The slave side of the pseudo terminal is
released with pty_release(3) 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(2). On success, returns the
status of the child process as determined by waitpid(2). On error,
returns -1
with errno
set appropriately. Note: If waitpid(2) is
interrupted by a signal, coproc_close(3) will return -1
with errno
set to EINTR
. The caller has to call coproc_close(3) (or just
waitpid(2)) again until it succeeds (or a real error occurs).
Additional errors may be generated and returned from the underlying system calls. See their manual pages.
MT-Safe (coproc_pty_open(3) is MT-Safe iff the pseudo(3) module is MT-Safe).
libslack(3), execve(2), system(3), popen(3), waitpid(2), sh(1), pseudo(3)>
20020916 raf <raf@raf.org>