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