Commit | Line | Data |
---|---|---|
2117e02e MW |
1 | #include "sig.h" |
2 | #include "readwrite.h" | |
3 | #include "exit.h" | |
4 | #include "env.h" | |
5 | #include "error.h" | |
6 | #include "fork.h" | |
7 | #include "wait.h" | |
8 | #include "seek.h" | |
9 | #include "qmail.h" | |
212b6f5d | 10 | #include "strerr.h" |
2117e02e | 11 | #include "substdio.h" |
212b6f5d | 12 | #include "fmt.h" |
2117e02e | 13 | |
212b6f5d | 14 | #define FATAL "condredirect: fatal: " |
2117e02e MW |
15 | |
16 | struct qmail qqt; | |
17 | ||
18 | int mywrite(fd,buf,len) int fd; char *buf; int len; | |
19 | { | |
212b6f5d MW |
20 | qmail_put(&qqt,buf,len); |
21 | return len; | |
2117e02e MW |
22 | } |
23 | ||
2117e02e | 24 | char inbuf[SUBSTDIO_INSIZE]; |
212b6f5d MW |
25 | char outbuf[1]; |
26 | substdio ssin = SUBSTDIO_FDBUF(read,0,inbuf,sizeof inbuf); | |
27 | substdio ssout = SUBSTDIO_FDBUF(mywrite,-1,outbuf,sizeof outbuf); | |
28 | ||
29 | char num[FMT_ULONG]; | |
2117e02e MW |
30 | |
31 | void main(argc,argv) | |
32 | int argc; | |
33 | char **argv; | |
34 | { | |
212b6f5d MW |
35 | char *sender; |
36 | char *dtline; | |
37 | int pid; | |
38 | int wstat; | |
39 | char *qqx; | |
40 | ||
41 | if (!argv[1] || !argv[2]) | |
42 | strerr_die1x(100,"condredirect: usage: condredirect newaddress program [ arg ... ]"); | |
43 | ||
44 | pid = fork(); | |
45 | if (pid == -1) | |
46 | strerr_die2sys(111,FATAL,"unable to fork: "); | |
47 | if (pid == 0) { | |
48 | execvp(argv[2],argv + 2); | |
49 | if (error_temp(errno)) _exit(111); | |
50 | _exit(100); | |
2117e02e | 51 | } |
212b6f5d MW |
52 | if (wait_pid(&wstat,pid) == -1) |
53 | strerr_die2x(111,FATAL,"wait failed"); | |
54 | if (wait_crashed(wstat)) | |
55 | strerr_die2x(111,FATAL,"child crashed"); | |
56 | switch(wait_exitcode(wstat)) { | |
57 | case 0: break; | |
58 | case 111: strerr_die2x(111,FATAL,"temporary child error"); | |
59 | default: _exit(0); | |
2117e02e MW |
60 | } |
61 | ||
212b6f5d MW |
62 | if (seek_begin(0) == -1) |
63 | strerr_die2sys(111,FATAL,"unable to rewind: "); | |
64 | sig_pipeignore(); | |
65 | ||
66 | sender = env_get("SENDER"); | |
67 | if (!sender) strerr_die2x(100,FATAL,"SENDER not set"); | |
68 | dtline = env_get("DTLINE"); | |
69 | if (!dtline) strerr_die2x(100,FATAL,"DTLINE not set"); | |
70 | ||
71 | if (qmail_open(&qqt) == -1) | |
72 | strerr_die2sys(111,FATAL,"unable to fork: "); | |
73 | qmail_puts(&qqt,dtline); | |
74 | if (substdio_copy(&ssout,&ssin) != 0) | |
75 | strerr_die2sys(111,FATAL,"unable to read message: "); | |
76 | substdio_flush(&ssout); | |
77 | ||
78 | num[fmt_ulong(num,qmail_qp(&qqt))] = 0; | |
2117e02e | 79 | |
212b6f5d MW |
80 | qmail_from(&qqt,sender); |
81 | qmail_to(&qqt,argv[1]); | |
82 | qqx = qmail_close(&qqt); | |
83 | if (*qqx) strerr_die2x(*qqx == 'D' ? 100 : 111,FATAL,qqx + 1); | |
84 | strerr_die2x(99,"condredirect: qp ",num); | |
2117e02e | 85 | } |