From 2c2faa299e6c1f2a9b6f8220c75cf878d4e005b0 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Fri, 20 Aug 2010 13:47:05 +0300 Subject: [PATCH] Move --pipe handling to cliutils helpers --- cliutils.c | 38 ++++++++++++++++++++++++++++++++++++++ cliutils.h | 4 ++++ rpmbuild.c | 32 +++----------------------------- rpmqv.c | 32 +++----------------------------- 4 files changed, 48 insertions(+), 58 deletions(-) diff --git a/cliutils.c b/cliutils.c index f022803dd..23c632372 100644 --- a/cliutils.c +++ b/cliutils.c @@ -2,6 +2,8 @@ #if HAVE_MCHECK_H #include #endif +#include + #include #include #include @@ -10,6 +12,8 @@ #include "cliutils.h" #include "debug.h" +static pid_t pipeChild = 0; + RPM_GNUC_NORETURN void argerror(const char * desc) { @@ -110,3 +114,37 @@ int finishCli(poptContext optCon, int rc) /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */ return (rc > 254) ? 254 : rc; } + +int initPipe(void) +{ + int p[2]; + + if (pipe(p) < 0) { + fprintf(stderr, _("creating a pipe for --pipe failed: %m\n")); + return -1; + } + + if (!(pipeChild = fork())) { + (void) signal(SIGPIPE, SIG_DFL); + (void) close(p[1]); + (void) dup2(p[0], STDIN_FILENO); + (void) close(p[0]); + (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL); + fprintf(stderr, _("exec failed\n")); + exit(EXIT_FAILURE); + } + + (void) close(p[0]); + (void) dup2(p[1], STDOUT_FILENO); + (void) close(p[1]); + return 0; +} + +void finishPipe(void) +{ + int status; + if (pipeChild) { + (void) fclose(stdout); + (void) waitpid(pipeChild, &status, 0); + } +} diff --git a/cliutils.h b/cliutils.h index 830b4c4f5..ea4539899 100644 --- a/cliutils.h +++ b/cliutils.h @@ -17,4 +17,8 @@ poptContext initCli(const char *ctx, struct poptOption *optionsTable, /* Free up common resources, return "normalized" exit code */ int finishCli(poptContext optCon, int rc); +int initPipe(void); + +void finishPipe(void); + #endif /* _CLIUTIL_H */ diff --git a/rpmbuild.c b/rpmbuild.c index 3b0b8cecd..51fcbfe63 100644 --- a/rpmbuild.c +++ b/rpmbuild.c @@ -6,7 +6,6 @@ const char *__progname; #include #include #include -#include #include #include /* RPMSIGTAG, rpmReadPackageFile .. */ @@ -387,11 +386,8 @@ int main(int argc, char *argv[]) char * passPhrase = ""; const char *pkg = NULL; - pid_t pipeChild = 0; poptContext optCon; int ec = 0; - int status; - int p[2]; setprogname(argv[0]); /* Retrofit glibc __progname */ @@ -451,25 +447,8 @@ int main(int argc, char *argv[]) /* rpmbuild is rather chatty by default */ rpmSetVerbosity(quiet ? RPMLOG_WARNING : RPMLOG_INFO); - if (rpmcliPipeOutput) { - if (pipe(p) < 0) { - fprintf(stderr, _("creating a pipe for --pipe failed: %m\n")); - goto exit; - } - - if (!(pipeChild = fork())) { - (void) signal(SIGPIPE, SIG_DFL); - (void) close(p[1]); - (void) dup2(p[0], STDIN_FILENO); - (void) close(p[0]); - (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL); - fprintf(stderr, _("exec failed\n")); - } - - (void) close(p[0]); - (void) dup2(p[1], STDOUT_FILENO); - (void) close(p[1]); - } + if (rpmcliPipeOutput && initPipe()) + exit(EXIT_FAILURE); ts = rpmtsCreate(); (void) rpmtsSetRootDir(ts, rpmcliRootDir); @@ -550,12 +529,7 @@ int main(int argc, char *argv[]) exit: ts = rpmtsFree(ts); - - if (pipeChild) { - (void) fclose(stdout); - (void) waitpid(pipeChild, &status, 0); - } - + finishPipe(); freeNames(); ba->buildRootOverride = _free(ba->buildRootOverride); ba->targets = _free(ba->targets); diff --git a/rpmqv.c b/rpmqv.c index 87d4b7577..f63e015ca 100644 --- a/rpmqv.c +++ b/rpmqv.c @@ -3,7 +3,6 @@ const char *__progname; #define _AUTOHELP -#include #include #include /* RPMSIGTAG, rpmReadPackageFile .. */ #include @@ -118,11 +117,8 @@ int main(int argc, char *argv[]) char * passPhrase = ""; #endif - pid_t pipeChild = 0; poptContext optCon; int ec = 0; - int status; - int p[2]; #ifdef IAM_RPMEIU int i; #endif @@ -403,25 +399,8 @@ int main(int argc, char *argv[]) } #endif /* IAM_RPMK */ - if (rpmcliPipeOutput) { - if (pipe(p) < 0) { - fprintf(stderr, _("creating a pipe for --pipe failed: %m\n")); - goto exit; - } - - if (!(pipeChild = fork())) { - (void) signal(SIGPIPE, SIG_DFL); - (void) close(p[1]); - (void) dup2(p[0], STDIN_FILENO); - (void) close(p[0]); - (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL); - fprintf(stderr, _("exec failed\n")); - } - - (void) close(p[0]); - (void) dup2(p[1], STDOUT_FILENO); - (void) close(p[1]); - } + if (rpmcliPipeOutput && initPipe()) + exit(EXIT_FAILURE); ts = rpmtsCreate(); (void) rpmtsSetRootDir(ts, rpmcliRootDir); @@ -554,13 +533,8 @@ int main(int argc, char *argv[]) } exit: - ts = rpmtsFree(ts); - - if (pipeChild) { - (void) fclose(stdout); - (void) waitpid(pipeChild, &status, 0); - } + finishPipe(); #ifdef IAM_RPMQV qva->qva_queryFormat = _free(qva->qva_queryFormat);