ossp-pkg/l2/l2_ch_pipe.c
1.15
/*
** 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_MAXARGS 256 /* shell command execution */
static l2_result_t hook_close(l2_context_t *, l2_channel_t *); /* prototypes */
/* 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\n");
Pid = waitpid(-1, &iStatus, WUNTRACED);
if (WIFEXITED(iStatus))
TRACE("EXITED child\n"); /* child finished and returned */
else if (WIFSIGNALED(iStatus))
TRACE("SIGNALED child\n"); /* child finished due to a signal */
else if (WIFSTOPPED(iStatus))
TRACE("STOPPED child\n"); /* child stopped due to a signal */
else
TRACE("Unknown SIGNAL\n"); /* child stopped due to a signal */
}
else if (sig == SIGPIPE); /* noop for now */
}
/* 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;
FILE *File;
char *szTemp;
char *pbIndex;
/* feed and call generic parameter parsing engine */
L2_PARAM_SET(pa[0], mode, CHARPTR, &szTemp);
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 (strcmp(szTemp, "direct") == NULL)
cfg->iMode = L2_PIPE_MODEDIRECT;
else if (strcmp(szTemp, "shell") == NULL)
cfg->iMode = L2_PIPE_MODESHELL;
else
return L2_ERR_ARG;
/* check to see if a file exists at the user specified path */
if (cfg->iMode != L2_PIPE_MODESHELL) {
szTemp = strdup(cfg->szCmdpath);
for (pbIndex = szTemp; (*pbIndex != ' ') && (*pbIndex != NULL); pbIndex++);
*pbIndex = NULL;
if (!(File = fopen(szTemp, "r")))
return L2_ERR_ARG; /* the command does not exist at the given path */
else
fclose(File);
free(szTemp);
szTemp = NULL;
pbIndex = NULL;
}
return rv;
}
/**********************************************************
* 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 != NULL)) {
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 = NULL; /* 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;
/* 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;
/* 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 */
if ((rv = parse_cmdpath(cfg->szCmdpath, pVec)) != L2_OK)
return rv;
if (pipe(cfg->piFd) == -1) /* open the pipe */
return L2_ERR_SYS;
if ((cfg->Pid = fork()) > 0) { /* parent process */
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 */
if (execvp(*pVec, pVec) == -1) { /* launch */
close(cfg->piFd[0]); /* cleanup in case we fail */
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;
/* write message to channel pipe */
if (write(cfg->piFd[1], buf, buf_size) == -1) {
if ((errno == EPIPE) && (cfg->iWritefail++ < 6)) {
hook_close(ctx, ch);
hook_open(ctx, ch);
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, NULL) < 0)
return L2_ERR_SYS;
if (sigaction(SIGPIPE, &cfg->sigpipe, NULL) < 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
};