/* ** 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 #include #define L2_PIPE_MODEDIRECT 1 /* direct command execution */ #define L2_PIPE_MODESHELL 2 /* shell command execution */ #define L2_PIPE_MAXARGS 256 /* shell command execution */ /* declare private channel configuration */ typedef struct { int piFd[2]; /* pipe file descriptor */ int iMode; /* execution mode direct or shell */ char *szCmdpath; /* path to command and arguments */ char *pVec[L2_PIPE_MAXARGS]; /* vector of command and params */ } l2_ch_pipe_t; static void catchsignal(int sig, ...) { va_list ap; static l2_ch_pipe_t *cfg = NULL; va_start(ap, sig); if (sig == 0) cfg = va_arg(ap, l2_ch_pipe_t *); else if (sig == SIGCHLD) { fprintf(stderr, "Pipe: SIGCHLD caught\n"); close(cfg->piFd[1]); cfg->piFd[1] = -1; /* TODO: Fill in with new pipe fd */ } else if (sig == SIGPIPE) { fprintf(stderr, "Pipe: SIGPIPE caught\n"); close(cfg->piFd[1]); cfg->piFd[1] = -1; /* TODO: Fill in with new pipe fd */ /* ((l2_ch_pipe_t *)(int *)ctx->vp)->piFd[1] = -1; */ } va_end(ap); } /* 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->piFd[0] = -1; cfg->piFd[1] = -1; cfg->iMode = -1; cfg->szCmdpath = NULL; memset(cfg->pVec, 0, sizeof(cfg->pVec)); /* 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 szMode[16]; /* just eneough for double-byte "direct" and NULL */ /* feed and call generic parameter parsing engine */ L2_PARAM_SET(pa[0], mode, CHARPTR, szMode); L2_PARAM_SET(pa[1], path, STRING, &cfg->szCmdpath); L2_PARAM_END(pa[2]); rv = l2_util_setparams(pa, fmt, ap); if (strcmp(szMode, "direct") == NULL) cfg->iMode = L2_PIPE_MODEDIRECT; else if (strcmp(szMode, "shell") == NULL) cfg->iMode = L2_PIPE_MODESHELL; else return L2_ERR_ARG; catchsignal(0, cfg); /* initialize signal handler with incoming context */ signal(SIGCHLD, (void(*)())catchsignal); /* pipe changes descriptor */ signal(SIGPIPE, (void(*)())catchsignal); /* pipe closes reading fd */ 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; while ((iCnt++ < L2_PIPE_MAXARGS) && (*szBuf != NULL)) { while ((*szBuf == ' ') || (*szBuf == '\t')) *szBuf++ = '\0'; *szArgs++ = szBuf; while ((*szBuf != '\0') && (*szBuf != ' ') && (*szBuf != '\t')) szBuf++; } *szArgs = NULL; if (iCnt <= L2_PIPE_MAXARGS) return L2_OK; else return L2_ERR_ARG; } /********************************************************** * child_exec: Helper method to hook_open * * Redirect stdin to read from cfg->piFd. Exec a given * * command or passes it to the sh shell for execution. * **********************************************************/ static l2_result_t child_exec(l2_ch_pipe_t *cfg) { /* make sure a command path was set */ if (cfg->szCmdpath == NULL) return L2_ERR_USE; 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 */ return execvp(*cfg->pVec, cfg->pVec); /* launch */ } /* 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; l2_result_t rv; pid_t Pid; if ((rv = parse_cmdpath(cfg->szCmdpath, cfg->pVec)) != L2_OK) return rv; if (pipe(cfg->piFd) == -1) /* open the pipe */ return L2_ERR_SYS; if ((Pid = fork()) > 0) { /* parent process */ close(cfg->piFd[0]); cfg->piFd[0] = -1; } else if ((Pid = fork()) == 0) { /* child process */ /* if child returns, there was an error */ if (child_exec(cfg) == -1) { close(cfg->piFd[0]); cfg->piFd[0] = -1; return L2_ERR_SYS; } } else /* fork failed */ return L2_ERR_SYS; return L2_OK; } /* write to channel */ 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; /* validate the pipe fd */ if (cfg->piFd[1] == -1) /* TODO: possibly remove this validation in */ return L2_ERR_ARG; /* lieu of incoming SIGPIPE signal */ /* write message to channel pipe */ if (write(cfg->piFd[1], buf, buf_size) == -1) return L2_ERR_SYS; 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; /* close channel pipe for parent process created in hook_open() */ close(cfg->piFd[1]); cfg->piFd[1] = -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 };