File: condredirect.c

package info (click to toggle)
qmail 1.01-5
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,188 kB
  • ctags: 1,711
  • sloc: ansic: 13,993; makefile: 1,914; perl: 448; sh: 377
file content (87 lines) | stat: -rw-r--r-- 2,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "sig.h"
#include "readwrite.h"
#include "exit.h"
#include "env.h"
#include "error.h"
#include "fork.h"
#include "wait.h"
#include "seek.h"
#include "qmail.h"
#include "stralloc.h"
#include "subfd.h"
#include "substdio.h"

void die_success() { _exit(0); }
void die_99() { _exit(99); }
void die_perm(s) char *s; { substdio_putsflush(subfderr,s); _exit(100); }
void die_temp(s) char *s; { substdio_putsflush(subfderr,s); _exit(111); }
void die_nomem() { die_temp("condredirect: fatal: out of memory\n"); }

struct qmail qqt;

int mywrite(fd,buf,len) int fd; char *buf; int len;
{
 qmail_put(&qqt,buf,len);
 return len;
}

substdio ssin;
substdio ssout;
char inbuf[SUBSTDIO_INSIZE];
char outbuf[16];

void main(argc,argv)
int argc;
char **argv;
{
 char *sender;
 char *dtline;
 int pid;
 int wstat;

 if (!argv[1] || !argv[2])
   die_perm("condredirect: usage: condredirect newaddress program arg ...\n");

 switch(pid = fork())
  {
   case -1: die_temp("condredirect: fatal: unable to fork\n");
   case 0:
     execvp(argv[2],argv + 2);
     if (error_temp(errno)) _exit(111);
     _exit(100);
  }
 if (wait_pid(&wstat,pid) != pid)
   die_perm("condredirect: fatal: internal bug\n");
 if (wait_crashed(wstat)) die_temp("condredirect: fatal: child crashed\n");
 switch(wait_exitcode(wstat))
  {
   case 0: break;
   case 111: die_temp("condredirect: fatal: temporary child error\n");
   default: die_success();
  }

 if (seek_begin(0) == -1) die_temp("condredirect: fatal: unable to rewind\n");
 sig_pipeignore();

 sender = env_get("SENDER");
 if (!sender) die_perm("condredirect: fatal: SENDER not set\n");
 dtline = env_get("DTLINE");
 if (!dtline) die_perm("condredirect: fatal: DTLINE not set\n");

 if (qmail_open(&qqt) == -1) die_temp("condredirect: fatal: unable to fork\n");
 qmail_puts(&qqt,dtline);
 substdio_fdbuf(&ssin,read,0,inbuf,sizeof(inbuf));
 substdio_fdbuf(&ssout,mywrite,-1,outbuf,sizeof(outbuf));
 if (substdio_copy(&ssout,&ssin) != 0)
   die_temp("condredirect: fatal: error while reading message\n");
 substdio_flush(&ssout);

 qmail_from(&qqt,sender);
 qmail_to(&qqt,argv[1]);
 switch(qmail_close(&qqt))
  {
   case 0: die_99();
   case QMAIL_TOOLONG: die_perm("condredirect: fatal: permanent qmail-queue error\n");
   default: die_temp("condredirect: fatal: temporary qmail-queue error\n");
  }
}