ossp-pkg/l2/l2_ch_pipe.c
1.21
/*
** L2 - OSSP Logging Library
** Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
** Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
**
** This file is part of OSSP L2, a flexible logging library which
** can be found at http://www.ossp.org/pkg/l2/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** l2_ch_pipe.c: pipe channel implementation
*/
#include "l2.h"
#include "l2_p.h" /* for TRACE() */
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#define L2_PIPE_MODEDIRECT 1 /* direct command execution */
#define L2_PIPE_MODESHELL 2 /* shell command execution */
#define L2_PIPE_WRITEFAIL 6 /* how long before real failure is indicated */
#define L2_PIPE_MAXARGS 256 /* how many args can our piped command have */
/* declare private channel configuration */
typedef struct {
pid_t Pid; /* process id of child command */
int iWritefail; /* counter to failed write() operations */
int piFd[2]; /* pipe file descriptor */
int iMode; /* execution mode direct or shell */
char *szCmdpath; /* path to command and arguments */
struct sigaction sigchld; /* initial state of chld signal handler */
struct sigaction sigpipe; /* initial state of pipe signal handler */
} l2_ch_pipe_t;
static void catchsignal(int sig, ...)
{
pid_t Pid; /* for wait() */
int iStatus = 0; /* for wait() */
if (sig == SIGCHLD) {
TRACE("SIGCHLD caught");
Pid = waitpid(-1, &iStatus, WUNTRACED);
if (WIFEXITED(iStatus))
TRACE("EXITED child"); /* child finished and returned */
else if (WIFSIGNALED(iStatus))
TRACE("SIGNALED child"); /* child finished due to a signal */
else if (WIFSTOPPED(iStatus))
TRACE("STOPPED child"); /* child stopped due to a signal */
else
TRACE("SIGNAL Unknown"); /* child stopped due to a signal */
}
else if (sig == SIGPIPE); /* noop for now */
}
/*
* A channel is the central object for a logging stream. It is
* implemented by a framework (the code implemented here) which provides
* the channel API and which is equal for all channels and a particular
* handler which implements the characteristics specific to a particular
* channel class. The lifecycle of a channel is illustrated in the
* following figure:
*
* -----BEGIN EMBEDDED OBJECT-----
* Content-editor: xfig %s
* Content-encoding: gzip:9 base64
* Content-type: application/fig
* Content-viewer: xfig %s
* Description: L2 Pipe Channel Command Lifecycle
* Filename: l2_ch_pipe.fig
* Last-modified: 2000-09-28/14:40
* Name: l2_ch_pipe
* Version: eo/1.0
* H4sIALKVuDsCA61Z32/cNgx+3v0VAvqwDWgO+mnLz8U2FMiwYW3f9uL4lJ43n+9m
* +5rlvx9J2Sc5bRrp1qZxRdvfJ9KkSEp99fPbX5jays1t3e/Gpj65zRvXT27Y/Oqm
* oW02t24CibGN4HzL+eZd23/s3OZGboTknMkNZ0qyVw392RRM6tIwWcIjIQQOK2k2
* mnH4MXgRBRPwL1DBK5oBCdPwKw1erOWsGVw9uT85F1+Dwc1CG6YUTkGwY3/ffjwP
* LyJhHgVIrYoZeTy5PhFklJi1fBjaKXWqQi6opjuOqaYV1bWmldUVptnqGtOqcpkq
* 1TQjD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6y3OJ6/RDBmNSIM1cYwwqeAXmLermpiYakm4tmEFdq8
* lAqLJRPQtv393rHzCLn1UD9C0vrn3A6QYHp3A+t/Ss1aMOvMSQFz5/b1p/Y4bNlb
* KDb7FipMPbrXMHSQcCjhfj8m0Ep+odVQHetxPEL6nzBtzaXnUibHU/3Qp+TXS1mh
* juXcT21HWRXT7J372PZQx96TnpgY59KWVWHIsfdtD5nfjQv3F/PqC9ari/UWHEWm
* Tm44tD18AvbXeZxYfY8HXtFnjdI1ln4MHSH89gwCwwe5YBaDXDzXVGKMI9hv62aa
* l7HCY3EgthjG+BRPX7a0zQuUi0Lck+KPkhGrfFYj2hF6OA4jQ5XUyxMDzUWmpX73
* t/B8G1N9DyuW5jzXVGqGPZy65WBqwdXyxCqZa6ovvAvPtzGVOGeNMDVlmuqzXDVn
* PrQ08QzWFOU1Z7Aeds0ZLCFzz2AJlHNQSceb2Sg63vSmZR9vEiw+3kw8KM5WkipO
* NooOibNQlxCx2EPlh4iHXRMihMwNEQJlH9N7JXOP6a8yjTyQaxrFcbZpFMdZpoU4
* JtjTOLYxUGxNyW00HfUYBY5gtxYp+TyKDo95iQP6ktSIfBm3Ms7gzk7gdKTmcIbW
* qD18bqD93EBEkn2Q7xHYQ0PxEg5XAJYQwgmoRdAl5cxFwfJ0LhtwMsJhScEDAVPR
* GRZMtmyvf4NG8h02krftvWsem87l+EWhNtl+Uba8zi+UYq7xC7Yc1/iFtgiZftHC
* 5vhFcT37hXYri1/+oJMO6EYvfmE/DA67/R9zHIRrNds/uIm5yj/0v3DX+Aeb32v8
* Q0cemf4RqsryD57YkX9g2/BV9/hjS++e/wBMybpdCx8AAA==
* -----END EMBEDDED OBJECT-----
*/
/* create channel */
static l2_result_t hook_create(l2_context_t *ctx, l2_channel_t *ch)
{
l2_ch_pipe_t *cfg;
/* allocate private channel configuration */
if ((cfg = (l2_ch_pipe_t *)malloc(sizeof(l2_ch_pipe_t))) == NULL)
return L2_ERR_ARG;
/* initialize configuration with reasonable defaults */
cfg->Pid = -1;
cfg->iWritefail = 0;
cfg->piFd[0] = -1;
cfg->piFd[1] = -1;
cfg->iMode = -1;
cfg->szCmdpath = NULL;
memset(&cfg->sigchld, 0, sizeof(cfg->sigchld));
memset(&cfg->sigpipe, 0, sizeof(cfg->sigpipe));
/* link private channel configuration into channel context */
ctx->vp = cfg;
return L2_OK;
}
/* configure channel */
static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
{
l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
l2_param_t pa[3];
l2_result_t rv;
char *sz = NULL;
/* feed and call generic parameter parsing engine */
L2_PARAM_SET(pa[0], mode, CHARPTR, &sz);
L2_PARAM_SET(pa[1], path, STRING, &cfg->szCmdpath);
L2_PARAM_END(pa[2]);
if ((rv = l2_util_setparams(pa, fmt, ap)) != L2_OK)
return rv;
if (sz != NULL) {
if (strcmp(sz, "direct") == 0)
cfg->iMode = L2_PIPE_MODEDIRECT;
else if (strcmp(sz, "shell") == 0)
cfg->iMode = L2_PIPE_MODESHELL;
else
return L2_ERR_ARG;
}
return L2_OK;
}
/**********************************************************
* parse_cmdpath: Helper method to hook_open *
* Parses szBuf into an argv-style string vector szArgs *
**********************************************************/
static l2_result_t parse_cmdpath (char *szBuf, char *szArgs[]) {
int iCnt = 0;
if (szBuf == NULL) /* check for bad input before we */
return L2_ERR_ARG; /* dereference and throw a SIGSEV */
while ((iCnt++ < L2_PIPE_MAXARGS) && (*szBuf != '\0')) {
while ((*szBuf == ' ') || (*szBuf == '\t'))
*szBuf++ = '\0'; /* overwrite whitespace with EOL */
*szArgs++ = szBuf; /* found the start of a new token */
while ((*szBuf != '\0') && (*szBuf != ' ') && (*szBuf != '\t'))
szBuf++;
}
*szArgs = '\0'; /* add a NULL to mark the end of the chain */
if (iCnt <= L2_PIPE_MAXARGS)
return L2_OK;
else
return L2_ERR_ARG;
}
/* open channel */
static l2_result_t hook_open(l2_context_t *ctx, l2_channel_t *ch)
{
l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
char *pVec[L2_PIPE_MAXARGS];
struct sigaction locact;
l2_result_t rv;
char *sz = NULL;
/* consistency check */
if (cfg->szCmdpath == NULL)
return L2_ERR_USE;
/* initialize auto vars before using them */
memset(pVec, 0, sizeof(pVec));
memset(&locact, 0, sizeof(locact));
locact.sa_handler = (void(*)())catchsignal;
sigemptyset(&locact.sa_mask);
locact.sa_flags = 0;
/* save old signal context before replacing with our own */
if (sigaction(SIGCHLD, &locact, &cfg->sigchld) < 0)
return L2_ERR_SYS;
if (sigaction(SIGPIPE, &locact, &cfg->sigpipe) < 0)
return L2_ERR_SYS;
if (pipe(cfg->piFd) == -1) /* open the pipe */
return L2_ERR_SYS;
if ((cfg->Pid = fork()) > 0) { /* parent process */
free(sz); /* no exec() in parent */
close(cfg->piFd[0]); /* half-duplex (no reading) */
cfg->piFd[0] = -1;
}
else if (cfg->Pid == 0) { /* child process */
close(cfg->piFd[1]); /* close the writing end, */
cfg->piFd[1] = -1; /* because we don't use it */
dup2(cfg->piFd[0], fileno(stdin)); /* copy the reading end */
/* the distinction between modes is necessary, because only executing */
/* commands in a shell environment allows usage of variables and such */
if (cfg->iMode == L2_PIPE_MODESHELL) {
pVec[0] = "/bin/sh";
pVec[1] = "-c";
pVec[2] = cfg->szCmdpath;
pVec[3] = NULL; /* add a NULL to mark the end of the chain */
}
else { /* plain direct command execution */
sz = strdup(cfg->szCmdpath);
if ((rv = parse_cmdpath(sz, pVec)) != L2_OK) {
free(sz);
return rv;
}
}
if (execvp(*pVec, pVec) == -1) { /* launch */
TRACE("execvp in child returned -1");
free(sz); /* cleanup in case we fail */
close(cfg->piFd[0]);
cfg->piFd[0] = -1; /* if execvp() doesn't swap our context or */
return L2_ERR_SYS; /* if child returns, we have an error */
}
}
else /* fork failed */
return L2_ERR_SYS;
return L2_OK;
}
/* write to channel, possibly recursively */
static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch,
l2_level_t level, const char *buf, size_t buf_size)
{
l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
l2_result_t rv;
/* write message to channel pipe */
if (write(cfg->piFd[1], buf, buf_size) == -1) {
if ((errno == EPIPE) && (cfg->iWritefail++ < L2_PIPE_WRITEFAIL)) {
if ((rv = l2_channel_close(ch)) != L2_OK)
return rv;
if ((rv = l2_channel_open(ch)) != L2_OK)
return rv;
return hook_write(ctx, ch, level, buf, buf_size);
}
else { /* not broken pipe problem or over the fail limit */
cfg->iWritefail = 0; /* reset pipe failure counter */
return L2_ERR_SYS;
}
}
else { /* write() to pipe succeeded */
cfg->iWritefail = 0; /* reset pipe failure counter */
return L2_OK;
}
}
/* close channel */
static l2_result_t hook_close(l2_context_t *ctx, l2_channel_t *ch)
{
l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
/* restore previous signal context */
if (sigaction(SIGCHLD, &cfg->sigchld, 0) < 0)
return L2_ERR_SYS;
if (sigaction(SIGPIPE, &cfg->sigpipe, 0) < 0)
return L2_ERR_SYS;
/* close channel pipe for parent process created in hook_open() */
close(cfg->piFd[1]);
cfg->piFd[1] = -1;
if ((kill (cfg->Pid, SIGTERM)) && (errno != ESRCH))
return L2_ERR_SYS;
cfg->Pid = -1;
return L2_OK;
}
/* destroy channel */
static l2_result_t hook_destroy(l2_context_t *ctx, l2_channel_t *ch)
{
l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
/* destroy channel configuration */
free(cfg->szCmdpath);
cfg->szCmdpath = NULL;
free(cfg);
return L2_OK;
}
/* exported channel handler structure */
l2_handler_t l2_handler_pipe = {
L2_CHANNEL_OUTPUT,
hook_create,
hook_configure,
hook_open,
hook_write,
NULL,
hook_close,
hook_destroy
};