OSSP CVS Repository

ossp - Check-in [3629]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 3629
Date: 2003-Sep-25 15:22:32 (local)
2003-Sep-25 13:22:32 (UTC)
User:thl
Branch:
Comment: add monitor option to file channel
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ch_file.c      1.30 -> 1.31     64 inserted, 2 deleted

ossp-pkg/l2/l2_ch_file.c 1.30 -> 1.31

--- l2_ch_file.c 2003/09/25 13:20:34     1.30
+++ l2_ch_file.c 2003/09/25 13:22:32     1.31
@@ -33,6 +33,9 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 /* declare private channel configuration */
 typedef struct {
@@ -43,6 +46,10 @@
     int   perm;
     int   jitter;
     int   jittercount;
+    int   monitor;
+    long  monitortime;
+    dev_t monitordev;
+    ino_t monitorino;
 } l2_ch_file_t;
 
 /* open channel file */
@@ -50,6 +57,8 @@
 {
     l2_ch_file_t *cfg = (l2_ch_file_t *)ctx->vp;
     mode_t mask;
+    struct timeval tv;
+    struct stat st;
 
     /* open channel file */
     mask = umask(0);
@@ -58,6 +67,23 @@
 
     /* prepare jittering counter */
     cfg->jittercount = 0;
+
+    /* prepare monitoring time and stat */
+    if (cfg->monitor >= 1) {
+        if (gettimeofday(&tv, NULL) != -1)
+            cfg->monitortime = tv.tv_sec;
+        else
+            cfg->monitortime = 0;
+        if (   (cfg->fd != -1)
+            && (fstat(cfg->fd, &st) != -1)) {
+            cfg->monitordev = st.st_dev;
+            cfg->monitorino = st.st_ino;
+        }
+        else {
+            cfg->monitordev = 0;
+            cfg->monitorino = 0;
+        }
+    }
 }
 
 /* create channel */
@@ -77,6 +103,10 @@
     cfg->perm        = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
     cfg->jitter      = 0;
     cfg->jittercount = 0;
+    cfg->monitor     = 0;
+    cfg->monitortime = 0;
+    cfg->monitordev  = 0;
+    cfg->monitorino  = 0;
 
     /* link private channel configuration into channel context */
     ctx->vp = cfg;
@@ -88,7 +118,7 @@
 static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
 {
     l2_ch_file_t *cfg = (l2_ch_file_t *)ctx->vp;
-    l2_param_t pa[6];
+    l2_param_t pa[7];
     l2_result_t rv;
     l2_env_t *env;
 
@@ -98,7 +128,8 @@
     L2_PARAM_SET(pa[2], trunc,   INT, &cfg->trunc);
     L2_PARAM_SET(pa[3], perm,    INT, &cfg->perm);
     L2_PARAM_SET(pa[4], jitter,  INT, &cfg->jitter);
-    L2_PARAM_END(pa[5]);
+    L2_PARAM_SET(pa[5], monitor, INT, &cfg->monitor);
+    L2_PARAM_END(pa[6]);
     l2_channel_env(ch, &env);
     rv = l2_util_setparams(env, pa, fmt, ap);
 
@@ -140,6 +171,10 @@
     if (cfg->jitter < 0)
         return L2_ERR_USE;
 
+    /* make sure monitor time is positive number */
+    if (cfg->monitor < 0)
+        return L2_ERR_USE;
+
     /* make sure a path was set */
     if (cfg->path == NULL)
         return L2_ERR_USE;
@@ -163,6 +198,8 @@
     l2_ch_file_t *cfg = (l2_ch_file_t *)ctx->vp;
     l2_result_t rc = L2_OK;
     int reopen = 0;
+    struct timeval tv;
+    struct stat st;
 
     /* if jittering, count writes and reopen file if jitter threshold is reached or exceeded */
     if (cfg->jitter >= 1) {
@@ -173,6 +210,31 @@
         }
     }
 
+    /* if monitoring, from time to time check for a renamed log and reopen file on detection */
+    if (cfg->monitor >= 1) {
+        int dostat = 0;
+        if (gettimeofday(&tv, NULL) != -1) {
+            if ((tv.tv_sec - cfg->monitortime) >= cfg->monitor) {
+                cfg->monitortime = tv.tv_sec;
+                dostat = 1;
+            }
+        }
+        else {
+            dostat = 1;
+        }
+        if (dostat == 1) {
+            if (stat(cfg->path, &st) == -1) {
+                reopen = 1;
+            }
+            else {
+                if (   (cfg->monitordev != st.st_dev)
+                    || (cfg->monitorino != st.st_ino)) {
+                    reopen = 1;
+                }
+            }
+        }
+    }
+
     /* close for reopen if required */
     if (reopen == 1 && cfg->fd != -1) {
         close(cfg->fd);

CVSTrac 2.0.1