ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
qmail.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "buffer.h"
3#include "readwrite.h"
4#include "wait.h"
5#include "env.h"
6#include "str.h"
7#include "exit.h"
8#include "fd.h"
9#include "qmail.h"
10#include "auto_qmail.h"
11#include "alloc.h"
12#include "stralloc.h"
13#include "idx.h"
14
15#define WHO "qmail"
16
22
23static const char *binqqargs[2] = { PROG_QMAIL_QUEUE, 0 } ;
24
25int qmail_open(struct qmail *qq, const stralloc *sa)
26{
27 int pim[2];
28 int pie[2];
29 unsigned i,j;
30 const char *cp;
31 const char **cpp;
32
33 qq->msgsize = 0L;
34 if (pipe(pim) == -1) return -1;
35 if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; }
36
37 switch (qq->pid = vfork()) {
38 case -1: close(pim[0]); close(pim[1]);
39 close(pie[0]); close(pie[1]);
40 return -1;
41 case 0: close(pim[1]);
42 close(pie[1]);
43 if (fd_move(0,pim[0]) == -1) _exit(120);
44 if (fd_move(1,pie[0]) == -1) _exit(120);
45 if (chdir(auto_qmail) == -1) _exit(61);
46 if ((cp = env_get("QMAILQUEUE")) != 0)
47 binqqargs[0] = cp;
48 else if (sa && sa->len) { /* count args */
49 j = 2; /* empty sa - qmqpc c control args */
50 for (i = 0; i < sa->len; i++) {
51 if (sa->s[i] == '\0') j++;
52 } /* make space */
53 if (!(cpp = (const char **) alloc(j * sizeof (char *)))) _exit(51);
54 cpp[0] = PROG_QMAIL_QMQPC;
55 cp = sa->s;
56 j = 1;
57 for (i = 0; i < sa->len; i++) {
58 if (sa->s[i]) continue;
59 cpp[j++] = cp;
60 cp = sa->s + i + 1;
61 }
62 cpp[j] = (char *) 0;
63 execv(*cpp,(char**)cpp); // FIXME
64 _exit(120);
65 }
66 execv(*binqqargs,(char**)binqqargs);
67 _exit(120);
68 }
69
70 qq->fdm = pim[1]; close(pim[0]);
71 qq->fde = pie[1]; close(pie[0]);
72 buffer_init(&qq->ss,buffer_unixwrite,qq->fdm,qq->buf,sizeof(qq->buf));
73 qq->flagerr = 0;
74 return 0;
75}
76
77unsigned long qmail_qp(struct qmail *qq)
78{
79 return qq->pid;
80}
81
82void qmail_fail(struct qmail *qq)
83{
84 qq->flagerr = 1;
85}
86
87void qmail_put(struct qmail *qq, const char *s, int len)
88{
89 if (!qq->flagerr)
90 if (buffer_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
91 qq->msgsize += len;
92}
93
94void qmail_puts(struct qmail *qq, const char *s)
95{
96 if (!qq->flagerr) {
97 int len = str_len(s);
98 if (buffer_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
99 qq->msgsize += len;
100 }
101}
102
103void qmail_from(struct qmail *qq, const char *s)
104{
105 if (buffer_flush(&qq->ss) == -1) qq->flagerr = 1;
106 close(qq->fdm);
107 buffer_init(&qq->ss,buffer_unixwrite,qq->fde,qq->buf,sizeof(qq->buf));
108 qmail_put(qq,"F",1);
109 qmail_puts(qq,s);
110 qmail_put(qq,"",1);
111}
112
113void qmail_to(struct qmail *qq, const char *s)
114{
115 qmail_put(qq,"T",1);
116 qmail_puts(qq,s);
117 qmail_put(qq,"",1);
118}
119
120const char *qmail_close(struct qmail *qq)
121{
122 int wstat;
123 int exitcode;
124
125 qmail_put(qq,"",1);
126 if (!qq->flagerr) if (buffer_flush(&qq->ss) == -1) qq->flagerr = 1;
127 close(qq->fde);
128
129 if (wait_pid(&wstat,qq->pid) != qq->pid)
130 return "Zqq waitpid surprise (#4.3.0)";
131 if (wait_crashed(wstat))
132 return "Zqq crashed (#4.3.0)";
133 exitcode = wait_exitcode(wstat);
134
135 switch (exitcode) { // FIXME
136 case 11: return "Denvelope address too long for qq (#5.1.3)";
137 case 31: return "Dmail server permanently rejected message (#5.3.0)";
138 case 51: return "Zqq out of memory (#4.3.0)";
139 case 52: return "Zqq timeout (#4.3.0)";
140 case 53: return "Zqq write error or disk full (#4.3.0)";
141 case 0: if (!qq->flagerr) return ""; /* fall through */
142 case 54: return "Zqq read error (#4.3.0)";
143 case 55: return "Zqq unable to read configuration (#4.3.0)";
144 case 56: return "Zqq trouble making network connection (#4.3.0)";
145 case 61: return "Zqq trouble in home directory (#4.3.0)";
146 case 63:
147 case 64:
148 case 65:
149 case 66:
150 case 62: return "Zqq trouble creating files in queue (#4.3.0)";
151 case 71: return "Zmail server temporarily rejected message (#4.3.0)";
152 case 72: return "Zconnection to mail server timed out (#4.4.1)";
153 case 73: return "Zconnection to mail server rejected (#4.4.1)";
154 case 74: return "Zcommunication with mail server failed (#4.4.2)";
155 case 91: /* fall through */
156 case 81: return "Zqq internal bug (#4.3.0)";
157 case 115: /* compatibility */
158 case 120: return "Zunable to exec qq (#4.3.0)";
159 default: if ((exitcode >= 11) && (exitcode <= 40))
160 return "Dqq permanent problem (#5.3.0)";
161 return "Zqq temporary problem (#4.3.0)";
162 }
163}
#define PROG_QMAIL_QMQPC
Definition idx.h:302
#define PROG_QMAIL_QUEUE
Definition idx.h:301
int execv(char *, char **)
void qmail_puts(struct qmail *qq, const char *s)
Definition qmail.c:94
int qmail_open(struct qmail *qq, const stralloc *sa)
Definition qmail.c:25
void qmail_from(struct qmail *qq, const char *s)
Definition qmail.c:103
const char * qmail_close(struct qmail *qq)
Definition qmail.c:120
void qmail_to(struct qmail *qq, const char *s)
Definition qmail.c:113
void qmail_fail(struct qmail *qq)
Definition qmail.c:82
void qmail_put(struct qmail *qq, const char *s, int len)
Definition qmail.c:87
unsigned long qmail_qp(struct qmail *qq)
Definition qmail.c:77
const char * cp
Definition ezmlm-cron.c:76
unsigned int len
Definition ezmlm-cron.c:68
int wstat
Definition ezmlm-cgi.c:143
const char auto_qmail[]
Definition auto_qmail.c:1
struct qmail qq
Definition ezmlm-clean.c:73
Definition qmail.h:10