chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / qmail.c
1 #include "substdio.h"
2 #include "readwrite.h"
3 #include "wait.h"
4 #include "exit.h"
5 #include "fork.h"
6 #include "fd.h"
7 #include "qmail.h"
8 #include "auto_qmail.h"
9 #include "env.h"
10
11 static char *binqqargs[2] = { 0, 0 } ;
12
13 static void setup_qqargs()
14 {
15   if(!binqqargs[0])
16     binqqargs[0] = env_get("QMAILQUEUE");
17   if(!binqqargs[0])
18     binqqargs[0] = "/usr/sbin/qmail-queue";
19 }
20
21 int qmail_open(qq)
22 struct qmail *qq;
23 {
24   int pim[2];
25   int pie[2];
26
27   setup_qqargs();
28
29   if (pipe(pim) == -1) return -1;
30   if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; }
31  
32   switch(qq->pid = vfork()) {
33     case -1:
34       close(pim[0]); close(pim[1]);
35       close(pie[0]); close(pie[1]);
36       return -1;
37     case 0:
38       close(pim[1]);
39       close(pie[1]);
40       if (fd_move(0,pim[0]) == -1) _exit(120);
41       if (fd_move(1,pie[0]) == -1) _exit(120);
42       if (chdir(auto_qmail) == -1) _exit(61);
43       execv(*binqqargs,binqqargs);
44       _exit(120);
45   }
46
47   qq->fdm = pim[1]; close(pim[0]);
48   qq->fde = pie[1]; close(pie[0]);
49   substdio_fdbuf(&qq->ss,write,qq->fdm,qq->buf,sizeof(qq->buf));
50   qq->flagerr = 0;
51   return 0;
52 }
53
54 unsigned long qmail_qp(qq) struct qmail *qq;
55 {
56   return qq->pid;
57 }
58
59 void qmail_fail(qq) struct qmail *qq;
60 {
61   qq->flagerr = 1;
62 }
63
64 void qmail_put(qq,s,len) struct qmail *qq; char *s; int len;
65 {
66   if (!qq->flagerr) if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
67 }
68
69 void qmail_puts(qq,s) struct qmail *qq; char *s;
70 {
71   if (!qq->flagerr) if (substdio_puts(&qq->ss,s) == -1) qq->flagerr = 1;
72 }
73
74 void qmail_from(qq,s) struct qmail *qq; char *s;
75 {
76   if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
77   close(qq->fdm);
78   substdio_fdbuf(&qq->ss,write,qq->fde,qq->buf,sizeof(qq->buf));
79   qmail_put(qq,"F",1);
80   qmail_puts(qq,s);
81   qmail_put(qq,"",1);
82 }
83
84 void qmail_to(qq,s) struct qmail *qq; char *s;
85 {
86   qmail_put(qq,"T",1);
87   qmail_puts(qq,s);
88   qmail_put(qq,"",1);
89 }
90
91 char *qmail_close(qq)
92 struct qmail *qq;
93 {
94   int wstat;
95   int exitcode;
96
97   qmail_put(qq,"",1);
98   if (!qq->flagerr) if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
99   close(qq->fde);
100
101   if (wait_pid(&wstat,qq->pid) != qq->pid)
102     return "Zqq waitpid surprise (#4.3.0)";
103   if (wait_crashed(wstat))
104     return "Zqq crashed (#4.3.0)";
105   exitcode = wait_exitcode(wstat);
106
107   switch(exitcode) {
108     case 115: /* compatibility */
109     case 11: return "Denvelope address too long for qq (#5.1.3)";
110     case 31: return "Dmail server permanently rejected message (#5.3.0)";
111     case 51: return "Zqq out of memory (#4.3.0)";
112     case 52: return "Zqq timeout (#4.3.0)";
113     case 53: return "Zqq write error or disk full (#4.3.0)";
114     case 0: if (!qq->flagerr) return ""; /* fall through */
115     case 54: return "Zqq read error (#4.3.0)";
116     case 55: return "Zqq unable to read configuration (#4.3.0)";
117     case 56: return "Zqq trouble making network connection (#4.3.0)";
118     case 61: return "Zqq trouble in home directory (#4.3.0)";
119     case 63:
120     case 64:
121     case 65:
122     case 66:
123     case 62: return "Zqq trouble creating files in queue (#4.3.0)";
124     case 71: return "Zmail server temporarily rejected message (#4.3.0)";
125     case 72: return "Zconnection to mail server timed out (#4.4.1)";
126     case 73: return "Zconnection to mail server rejected (#4.4.1)";
127     case 74: return "Zcommunication with mail server failed (#4.4.2)";
128     case 91: /* fall through */
129     case 81: return "Zqq internal bug (#4.3.0)";
130     case 120: return "Zunable to exec qq (#4.3.0)";
131     default:
132       if ((exitcode >= 11) && (exitcode <= 40))
133         return "Dqq permanent problem (#5.3.0)";
134       return "Zqq temporary problem (#4.3.0)";
135   }
136 }