chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / qmail-clean.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include "readwrite.h"
4 #include "sig.h"
5 #include "now.h"
6 #include "str.h"
7 #include "direntry.h"
8 #include "getln.h"
9 #include "stralloc.h"
10 #include "substdio.h"
11 #include "subfd.h"
12 #include "byte.h"
13 #include "scan.h"
14 #include "fmt.h"
15 #include "error.h"
16 #include "exit.h"
17 #include "fmtqfn.h"
18 #include "auto_qmail.h"
19
20 #define OSSIFIED 129600 /* see qmail-send.c */
21
22 stralloc line = {0};
23
24 void cleanuppid()
25 {
26  DIR *dir;
27  direntry *d;
28  struct stat st;
29  datetime_sec time;
30
31  time = now();
32  dir = opendir("pid");
33  if (!dir) return;
34  while (d = readdir(dir))
35   {
36    if (str_equal(d->d_name,".")) continue;
37    if (str_equal(d->d_name,"..")) continue;
38    if (!stralloc_copys(&line,"pid/")) continue;
39    if (!stralloc_cats(&line,d->d_name)) continue;
40    if (!stralloc_0(&line)) continue;
41    if (stat(line.s,&st) == -1) continue;
42    if (time < st.st_atime + OSSIFIED) continue;
43    unlink(line.s);
44   }
45  closedir(dir);
46 }
47
48 char fnbuf[FMTQFN];
49
50 void respond(s) char *s; { if (substdio_putflush(subfdoutsmall,s,1) == -1) _exit(100); }
51
52 void main()
53 {
54  int i;
55  int match;
56  int cleanuploop;
57  unsigned long id;
58
59  if (chdir(auto_qmail) == -1) _exit(111);
60  if (chdir("queue") == -1) _exit(111);
61
62  sig_pipeignore();
63
64  if (!stralloc_ready(&line,200)) _exit(111);
65
66  cleanuploop = 0;
67
68  for (;;)
69   {
70    if (cleanuploop) --cleanuploop; else { cleanuppid(); cleanuploop = 30; }
71    if (getln(subfdinsmall,&line,&match,'\0') == -1) break;
72    if (!match) break;
73    if (line.len < 7) { respond("x"); continue; }
74    if (line.len > 100) { respond("x"); continue; }
75    if (line.s[line.len - 1]) { respond("x"); continue; } /* impossible */
76    for (i = 5;i < line.len - 1;++i)
77      if ((unsigned char) (line.s[i] - '0') > 9)
78       { respond("x"); continue; }
79    if (!scan_ulong(line.s + 5,&id)) { respond("x"); continue; }
80    if (byte_equal(line.s,5,"foop/"))
81     {
82 #define U(prefix,flag) fmtqfn(fnbuf,prefix,id,flag); \
83 if (unlink(fnbuf) == -1) if (errno != error_noent) { respond("!"); continue; }
84      U("intd/",0)
85      U("mess/",1)
86      respond("+");
87     }
88    else if (byte_equal(line.s,4,"todo/"))
89     {
90      U("intd/",0)
91      U("todo/",0)
92      respond("+");
93     }
94    else
95      respond("x");
96   }
97  _exit(0);
98 }