rpm  4.10.0
cliutils.c
Go to the documentation of this file.
00001 #include "system.h"
00002 #if HAVE_MCHECK_H
00003 #include <mcheck.h>
00004 #endif
00005 #include <errno.h>
00006 #include <sys/wait.h>
00007 
00008 #include <rpm/rpmlog.h>
00009 #include <rpm/rpmlib.h>
00010 #include <rpm/rpmfileutil.h>
00011 #include <rpm/rpmmacro.h>
00012 #include <rpm/rpmcli.h>
00013 #include "cliutils.h"
00014 #include "debug.h"
00015 
00016 static pid_t pipeChild = 0;
00017 
00018 RPM_GNUC_NORETURN
00019 void argerror(const char * desc)
00020 {
00021     fprintf(stderr, _("%s: %s\n"), __progname, desc);
00022     exit(EXIT_FAILURE);
00023 }
00024 
00025 static void printVersion(FILE * fp)
00026 {
00027     fprintf(fp, _("RPM version %s\n"), rpmEVR);
00028 }
00029 
00030 static void printBanner(FILE * fp)
00031 {
00032     fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
00033     fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
00034 }
00035 
00036 void printUsage(poptContext con, FILE * fp, int flags)
00037 {
00038     printVersion(fp);
00039     printBanner(fp);
00040     fprintf(fp, "\n");
00041 
00042     if (rpmIsVerbose())
00043         poptPrintHelp(con, fp, flags);
00044     else
00045         poptPrintUsage(con, fp, flags);
00046 }
00047 
00048 int initPipe(void)
00049 {
00050     int p[2];
00051 
00052     if (pipe(p) < 0) {
00053         fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
00054         return -1;
00055     }
00056 
00057     if (!(pipeChild = fork())) {
00058         (void) signal(SIGPIPE, SIG_DFL);
00059         (void) close(p[1]);
00060         (void) dup2(p[0], STDIN_FILENO);
00061         (void) close(p[0]);
00062         (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
00063         fprintf(stderr, _("exec failed\n"));
00064         exit(EXIT_FAILURE);
00065     }
00066 
00067     (void) close(p[0]);
00068     (void) dup2(p[1], STDOUT_FILENO);
00069     (void) close(p[1]);
00070     return 0;
00071 }
00072 
00073 int finishPipe(void)
00074 {
00075     int rc = 0;
00076     if (pipeChild) {
00077         int status;
00078         pid_t reaped;
00079 
00080         (void) fclose(stdout);
00081         do {
00082             reaped = waitpid(pipeChild, &status, 0);
00083         } while (reaped == -1 && errno == EINTR);
00084             
00085         if (reaped == -1 || !WIFEXITED(status) || WEXITSTATUS(status))
00086             rc = 1;
00087     }
00088     return rc;
00089 }