[patch] timed log rotation for multilog

Jos Backus <[email protected]> Wed, 20 Sep 2006 22:46:58 -0701
Newsgroups gmane.comp.djb.syslog
Message-ID <[email protected]>
The patch below causes multilog, when invoked with `rNNN', to instruct the
kernel to send it a SIGALRM every NNN seconds. This obviates the need for cron
jobs when timed log rotation is desired. Hope this is useful to somebody - we
plan on using it at work.

--- multilog.c.orig	Wed Sep 20 22:38:23 2006
+++ multilog.c	Wed Sep 20 22:41:54 2006
@@ -1,6 +1,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include "direntry.h"
 #include "alloc.h"
 #include "buffer.h"
@@ -379,6 +380,7 @@
   struct cyclog *d;
   char *processor;
   unsigned long num;
+  unsigned long rot;
   unsigned long size;
 
   cnum = 0;
@@ -403,6 +405,18 @@
     else if (script[i][0] == 'n') {
       scan_ulong(script[i] + 1,&num);
       if (num < 2) num = 2;
+    }
+    else if (script[i][0] == 'r') {
+      scan_ulong(script[i] + 1, &rot);
+      if (rot < 1) rot = 1;
+
+      struct itimerval timer;
+      timer.it_value.tv_sec     = rot - (time(NULL) % rot);
+      timer.it_value.tv_usec    = 0;
+      timer.it_interval.tv_sec  = rot;
+      timer.it_interval.tv_usec = 0;
+      if (setitimer(ITIMER_REAL, &timer, NULL) == -1)
+	  strerr_die2sys(111,FATAL,"setitimer: ");
     }
     else if (script[i][0] == '!') {
       processor = script[i] + 1;

-- 
Jos Backus
jos at catnook.com