/* ** 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 #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 */ static l2_result_t hook_open(l2_context_t *, l2_channel_t *); /* prototypes */ static l2_result_t hook_close(l2_context_t *, l2_channel_t *); /* declare private channel configuration */ typedef struct { int piFd[2]; /* pipe file descriptor */ int iChild; /* exception status of child pipe process */ int iMode; /* execution mode direct or shell */ pid_t Pid; /* pid set during fork in hook_open() */ char *szCmdpath; /* path to command and arguments */ } l2_ch_pipe_t; static void catchsignal(int sig, ...) { va_list ap = NULL; static l2_context_t *ctx = NULL; static l2_channel_t *chan = NULL; if (sig == 0) { va_start(ap, sig); ctx = va_arg(ap, l2_context_t *); chan = va_arg(ap, l2_channel_t *); va_end(ap); } else if (sig == SIGCHLD) { /* TRACE("SIGCHLD caught\n");*/ waitpid(((l2_ch_pipe_t *)ctx->vp)->Pid, &((l2_ch_pipe_t *)ctx->vp)->iChild, WUNTRACED); if (WIFEXITED(((l2_ch_pipe_t *)ctx->vp)->iChild)) { close(((l2_ch_pipe_t *)ctx->vp)->piFd[1]); ((l2_ch_pipe_t *)ctx->vp)->piFd[1] = -1; ((l2_ch_pipe_t *)ctx->vp)->Pid = -1; /* check if process called exit() abnormally, then if so restarts */ if (WEXITSTATUS(((l2_ch_pipe_t *)ctx->vp)->iChild)) { fprintf(stderr, "exit status is %d\n", WEXITSTATUS(((l2_ch_pipe_t *)ctx->vp)->iChild)); if (hook_open(ctx, chan) != L2_OK) { /* TODO: Fix infinit loop! */ close(((l2_ch_pipe_t *)ctx->vp)->piFd[1]); ((l2_ch_pipe_t *)ctx->vp)->piFd[1] = -1; } } } else if (WIFSTOPPED(((l2_ch_pipe_t *)ctx->vp)->iChild)) { } } else if (sig == SIGPIPE) { /* thrown when we write to child's closed pipe */ /* TRACE("SIGPIPE caught\n");*/ close(((l2_ch_pipe_t *)ctx->vp)->piFd[1]); ((l2_ch_pipe_t *)ctx->vp)->piFd[1] = -1; } } /* 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->iChild = 0; cfg->iMode = -1; cfg->Pid = -1; cfg->szCmdpath = NULL; /* 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 != 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; } catchsignal(0, ctx, ch); /* initialize signal handler with context & ch */ signal(SIGCHLD, (void(*)())catchsignal); /* pipe changes descriptor */ signal(SIGPIPE, (void(*)())catchsignal); /* pipe closes reading fd */ return rv; /* all okay */ } /********************************************************** * 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'; /* 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]; l2_result_t rv; /* make sure a command path was set */ if (cfg->szCmdpath == NULL) return L2_ERR_USE; memset(pVec, 0, sizeof(pVec)); /* 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 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 */ cfg->piFd[0] = -1; return L2_ERR_SYS; /* if child returns, we have an error */ } } 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; if (cfg->piFd[1] == -1) return L2_ERR_IO; if (WIFSTOPPED(cfg->iChild)) { if (kill(cfg->Pid, SIGCONT)) { hook_close(ctx, ch); cfg->iChild = 0; return L2_ERR_SYS; } } /* write message to channel pipe */ else 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; if (kill(cfg->Pid, SIGTERM)) 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 };