--- l2_ch_pipe.c 2001/09/14 15:40:04 1.10
+++ l2_ch_pipe.c 2001/09/18 14:38:51 1.11
@@ -32,24 +32,37 @@
#include <unistd.h>
#include <signal.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 */
+
/* declare private channel configuration */
typedef struct {
- int iPipe; /* pipe filedescriptor used for writing only */
+ 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_context_t *ctx = NULL;
+ static l2_ch_pipe_t *cfg = NULL;
va_start(ap, sig);
if (sig == 0)
- ctx = va_arg(ap, l2_context_t *);
+ cfg = va_arg(ap, l2_ch_pipe_t *);
else if (sig == SIGCHLD) {
- ((l2_ch_pipe_t *)(int *)ctx->vp)->iPipe = -1; /* TODO: Fill in with new pipe fd */
+ 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; */
}
- else if (sig == SIGPIPE)
- ((l2_ch_pipe_t *)(int *)ctx->vp)->iPipe = -1;
va_end(ap);
}
@@ -63,7 +76,11 @@
return L2_ERR_ARG;
/* initialize configuration with reasonable defaults */
- cfg->iPipe = -1;
+ 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;
@@ -75,45 +92,114 @@
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[2];
+ 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], pipe, INT, &cfg->iPipe);
- L2_PARAM_END(pa[1]);
+ 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);
- /* validate the pipe fd */
- if (cfg->iPipe == -1)
+ 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, ctx); /* initialize signal handler with incoming context */
+ 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;
- l2_channel_t *downstream = l2_channel_downstream(ch);
- l2_result_t rv;
-
+
/* validate the pipe fd */
- if (cfg->iPipe == -1)
- return L2_ERR_ARG;
+ 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->iPipe, buf, buf_size) == -1)
+ if (write(cfg->piFd[1], buf, buf_size) == -1)
return L2_ERR_SYS;
- /* write to downstream channel */
- if ((rv = l2_channel_write(downstream, level, buf, buf_size)) != L2_OK)
- return rv;
-
return L2_OK;
}
@@ -121,16 +207,10 @@
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;
- l2_channel_t *downstream = l2_channel_downstream(ch);
- l2_result_t rv;
-
- /* close channel pipe */
- close(cfg->iPipe);
- cfg->iPipe = -1;
- /* close downstream channel, too */
- if ((rv = l2_channel_close(downstream)) != L2_OK)
- return rv;
+ /* close channel pipe for parent process created in hook_open() */
+ close(cfg->piFd[1]);
+ cfg->piFd[1] = -1;
return L2_OK;
}
@@ -139,25 +219,21 @@
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;
- l2_channel_t *downstream = l2_channel_downstream(ch);
- l2_result_t rv;
/* destroy channel configuration */
+ free(&cfg->szCmdpath);
+ cfg->szCmdpath = NULL;
free(cfg);
- /* destroy downstream channel, too */
- if ((rv = l2_channel_destroy(downstream)) != L2_OK)
- return rv;
-
return L2_OK;
}
/* exported channel handler structure */
l2_handler_t l2_handler_pipe = {
- L2_CHANNEL_FILTER,
+ L2_CHANNEL_OUTPUT,
hook_create,
hook_configure,
- NULL,
+ hook_open,
hook_write,
NULL,
hook_close,
|