rpm  4.11.1-rc1
cliutils.c
Go to the documentation of this file.
1 #include "system.h"
2 #if HAVE_MCHECK_H
3 #include <mcheck.h>
4 #endif
5 #include <errno.h>
6 #include <sys/wait.h>
7 
8 #include <rpm/rpmlog.h>
9 #include <rpm/rpmlib.h>
10 #include <rpm/rpmfileutil.h>
11 #include <rpm/rpmmacro.h>
12 #include <rpm/rpmcli.h>
13 #include "cliutils.h"
14 #include "debug.h"
15 
16 static pid_t pipeChild = 0;
17 
19 void argerror(const char * desc)
20 {
21  fprintf(stderr, _("%s: %s\n"), __progname, desc);
22  exit(EXIT_FAILURE);
23 }
24 
25 static void printVersion(FILE * fp)
26 {
27  fprintf(fp, _("RPM version %s\n"), rpmEVR);
28 }
29 
30 static void printBanner(FILE * fp)
31 {
32  fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
33  fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
34 }
35 
36 void printUsage(poptContext con, FILE * fp, int flags)
37 {
38  printVersion(fp);
39  printBanner(fp);
40  fprintf(fp, "\n");
41 
42  if (rpmIsVerbose())
43  poptPrintHelp(con, fp, flags);
44  else
45  poptPrintUsage(con, fp, flags);
46 }
47 
48 int initPipe(void)
49 {
50  int p[2];
51 
52  if (pipe(p) < 0) {
53  fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
54  return -1;
55  }
56 
57  if (!(pipeChild = fork())) {
58  (void) signal(SIGPIPE, SIG_DFL);
59  (void) close(p[1]);
60  (void) dup2(p[0], STDIN_FILENO);
61  (void) close(p[0]);
62  (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
63  fprintf(stderr, _("exec failed\n"));
64  exit(EXIT_FAILURE);
65  }
66 
67  (void) close(p[0]);
68  (void) dup2(p[1], STDOUT_FILENO);
69  (void) close(p[1]);
70  return 0;
71 }
72 
73 int finishPipe(void)
74 {
75  int rc = 0;
76  if (pipeChild) {
77  int status;
78  pid_t reaped;
79 
80  (void) fclose(stdout);
81  do {
82  reaped = waitpid(pipeChild, &status, 0);
83  } while (reaped == -1 && errno == EINTR);
84 
85  if (reaped == -1 || !WIFEXITED(status) || WEXITSTATUS(status))
86  rc = 1;
87  }
88  return rc;
89 }