Index: ossp-pkg/l2/l2_ch_pipe.c RCS File: /v/ossp/cvs/ossp-pkg/l2/l2_ch_pipe.c,v rcsdiff -q -kk '-r1.17' '-r1.18' -u '/v/ossp/cvs/ossp-pkg/l2/l2_ch_pipe.c,v' 2>/dev/null --- l2_ch_pipe.c 2001/09/26 09:13:27 1.17 +++ l2_ch_pipe.c 2001/09/26 12:10:31 1.18 @@ -68,7 +68,7 @@ else if (WIFSTOPPED(iStatus)) TRACE("STOPPED child"); /* child stopped due to a signal */ else - TRACE("Unknown SIGNAL"); /* child stopped due to a signal */ + TRACE("SIGNAL Unknown"); /* child stopped due to a signal */ } else if (sig == SIGPIPE); /* noop for now */ } @@ -104,38 +104,22 @@ 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; + char *sz; /* feed and call generic parameter parsing engine */ - L2_PARAM_SET(pa[0], mode, CHARPTR, &szTemp); + 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 (strcmp(szTemp, "direct") == NULL) + if (strcmp(sz, "direct") == 0) cfg->iMode = L2_PIPE_MODEDIRECT; - else if (strcmp(szTemp, "shell") == NULL) + else if (strcmp(sz, "shell") == 0) 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; } @@ -149,14 +133,14 @@ 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 ((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 = NULL; /* add a NULL to mark the end of the chain */ + *szArgs = '\0'; /* add a NULL to mark the end of the chain */ if (iCnt <= L2_PIPE_MAXARGS) return L2_OK; @@ -171,6 +155,7 @@ char *pVec[L2_PIPE_MAXARGS]; struct sigaction locact; l2_result_t rv; + char *sz = NULL; /* initialize auto vars before using them */ memset(pVec, 0, sizeof(pVec)); @@ -186,23 +171,11 @@ 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 */ + free(sz); /* no exec() in parent */ close(cfg->piFd[0]); /* half-duplex (no reading) */ cfg->piFd[0] = -1; } @@ -211,8 +184,27 @@ 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 */ - close(cfg->piFd[0]); /* cleanup in case we fail */ + 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 */ } @@ -232,8 +224,10 @@ /* write message to channel pipe */ if (write(cfg->piFd[1], buf, buf_size) == -1) { if ((errno == EPIPE) && (cfg->iWritefail++ < L2_PIPE_WRITEFAIL)) { - hook_close(ctx, ch); - hook_open(ctx, ch); + if (hook_close(ctx, ch) != L2_OK) + return L2_ERR_SYS; + if (hook_open(ctx, ch) != L2_OK) + return L2_ERR_SYS; return hook_write(ctx, ch, level, buf, buf_size); } else { /* not broken pipe problem or over the fail limit */ @@ -253,9 +247,9 @@ l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp; /* restore previous signal context */ - if (sigaction(SIGCHLD, &cfg->sigchld, NULL) < 0) + if (sigaction(SIGCHLD, &cfg->sigchld, 0) < 0) return L2_ERR_SYS; - if (sigaction(SIGPIPE, &cfg->sigpipe, NULL) < 0) + if (sigaction(SIGPIPE, &cfg->sigpipe, 0) < 0) return L2_ERR_SYS; /* close channel pipe for parent process created in hook_open() */