debian/rules: Use `git' potty wrapper.
[qmail] / qmail-tcpto.c
1 /* XXX: this program knows quite a bit about tcpto's internals */
2
3 #include "substdio.h"
4 #include "subfd.h"
5 #include "auto_qmail.h"
6 #include "fmt.h"
7 #include "ip.h"
8 #include "lock.h"
9 #include "error.h"
10 #include "exit.h"
11 #include "datetime.h"
12 #include "now.h"
13
14 void die(n) int n; { substdio_flush(subfdout); _exit(n); }
15
16 void warn(s) char *s;
17 {
18 char *x;
19 x = error_str(errno);
20 substdio_puts(subfdout,s);
21 substdio_puts(subfdout,": ");
22 substdio_puts(subfdout,x);
23 substdio_puts(subfdout,"\n");
24 }
25
26 void die_chdir() { warn("fatal: unable to chdir"); die(111); }
27 void die_open() { warn("fatal: unable to open tcpto"); die(111); }
28 void die_lock() { warn("fatal: unable to lock tcpto"); die(111); }
29 void die_read() { warn("fatal: unable to read tcpto"); die(111); }
30
31 char tcpto_buf[1024];
32
33 char tmp[FMT_ULONG + IPFMT];
34
35 void main()
36 {
37 int fdlock;
38 int fd;
39 int r;
40 int i;
41 char *record;
42 struct ip_address ip;
43 datetime_sec when;
44 datetime_sec start;
45
46 if (chdir(auto_qmail) == -1) die_chdir();
47 if (chdir("queue/lock") == -1) die_chdir();
48
49 fdlock = open_write("tcpto");
50 if (fdlock == -1) die_open();
51 fd = open_read("tcpto");
52 if (fd == -1) die_open();
53 if (lock_ex(fdlock) == -1) die_lock();
54 r = read(fd,tcpto_buf,sizeof(tcpto_buf));
55 close(fd);
56 close(fdlock);
57
58 if (r == -1) die_read();
59 r >>= 4;
60
61 start = now();
62
63 record = tcpto_buf;
64 for (i = 0;i < r;++i)
65 {
66 if (record[4] >= 1)
67 {
68 byte_copy(&ip,4,record);
69 when = (unsigned long) (unsigned char) record[11];
70 when = (when << 8) + (unsigned long) (unsigned char) record[10];
71 when = (when << 8) + (unsigned long) (unsigned char) record[9];
72 when = (when << 8) + (unsigned long) (unsigned char) record[8];
73
74 substdio_put(subfdout,tmp,ip_fmt(tmp,&ip));
75 substdio_puts(subfdout," timed out ");
76 substdio_put(subfdout,tmp,fmt_ulong(tmp,(unsigned long) (start - when)));
77 substdio_puts(subfdout," seconds ago; # recent timeouts: ");
78 substdio_put(subfdout,tmp,fmt_ulong(tmp,(unsigned long) (unsigned char) record[4]));
79 substdio_puts(subfdout,"\n");
80 }
81 record += 16;
82 }
83
84 die(0);
85 }