1 #include <unistd.h>
2 #include "qconfirm.h"
3 #include "strerr.h"
4 #include "pathexec.h"
5 #include "coe.h"
6 #include "env.h"
7 #include "wait.h"
8 #include "fd.h"
9 
10 #define FATAL "qconfirm-inject: fatal: "
11 
12 static int qpid;
13 
open_qmail_inject(int * fd,char * to)14 int open_qmail_inject(int *fd, char *to) {
15   int p[2];
16 
17   if (pipe(p) == -1) {
18     strerr_warn2(FATAL, "unable to pipe(); ", &strerr_sys);
19     return(-1);
20   }
21   if ((qpid =fork()) == -1) {
22     strerr_warn2(FATAL, "unable to fork(): ", &strerr_sys);
23     close(p[0]); close(p[1]);
24     return(-1);
25   }
26   if (! qpid) {
27     const char *args[4];
28 
29     close(p[1]);
30     if (fd_move(0, p[0]) == -1) {
31       strerr_warn2(FATAL, "unable to set descriptor for qmail-inject: ", \
32 		   &strerr_sys);
33       return(-1);
34     }
35     args[0] =QMAIL_HOME "/bin/qmail-inject";
36     if (to) {
37       args[1] ="--";
38       args[2] =to;
39       args[3] =0;
40     }
41     else args[1] =0;
42     pathexec_run(*args, args, (const char **)environ);
43     strerr_die2sys(111, FATAL, "unable to run qmail-inject: ");
44   }
45   close(p[0]);
46   *fd =p[1];
47   return(1);
48 }
close_qmail_inject()49 int close_qmail_inject() {
50   int wstat;
51 
52   if (wait_pid(&wstat, qpid) == -1) {
53     strerr_warn2(FATAL, "wait failed: ", &strerr_sys);
54     return(-1);
55   }
56   if (wait_exitcode(wstat) != 0) {
57     strerr_warn2(FATAL, "qmail-inject crashed.", 0);
58     return(-1);
59   }
60   return(1);
61 }
62