]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/watchdog.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / watchdog.c
index 2bb599daeca5c2bd81f48147606a255e62f58733..14fda2273b566fd3b420e4bbe555b994509f5568 100755 (executable)
@@ -33,8 +33,6 @@
 /* Exported globals */
 time_t watchdog_time;                /* this has granularity of SLEEP_TIME */
 
-
-#define TIMEOUT_SIGNAL SIGUSR2
 #define SLEEP_TIME 30                /* examine things every 30 seconds */
 
 /* Forward referenced functions */
@@ -43,8 +41,8 @@ static void stop_btimer(btimer_id wid);
 static btimer_id btimer_start_common(uint32_t wait);
 
 /* Static globals */
-static pthread_mutex_t mutex;
-static pthread_cond_t  timer;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t  timer = PTHREAD_COND_INITIALIZER;
 static int quit;
 static btimer_t *timer_chain = NULL;
 
@@ -52,7 +50,7 @@ static btimer_t *timer_chain = NULL;
 /*
  * Timeout signal comes here
  */
-static void timeout_handler(int sig)
+void timeout_handler(int sig)
 {
    return;                           /* thus interrupting the function */
 }
@@ -75,17 +73,8 @@ int start_watchdog(void)
    sigfillset(&sigtimer.sa_mask);
    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
    watchdog_time = time(NULL);
-   if ((stat = pthread_mutex_init(&mutex, NULL)) != 0) {
-      return stat;
-   }
-   if ((stat = pthread_cond_init(&timer, NULL)) != 0) {
-      pthread_mutex_destroy(&mutex);
-      return stat;
-   }
    quit = FALSE;
    if ((stat = pthread_create(&wdid, NULL, btimer_thread, (void *)NULL)) != 0) {
-      pthread_mutex_destroy(&mutex);
-      pthread_cond_destroy(&timer);
       return stat;
    }
    return 0;
@@ -101,9 +90,8 @@ int stop_watchdog(void)
 {
    int stat;
 
-   P(mutex);
    quit = TRUE;
-
+   P(mutex);
    if ((stat = pthread_cond_signal(&timer)) != 0) {
       V(mutex);
       return stat;
@@ -118,8 +106,6 @@ int stop_watchdog(void)
  */
 static void *btimer_thread(void *arg)
 {
-   struct timespec timeout;
-   int stat;
    JCR *jcr;
    BSOCK *fd;
    btimer_t *wid;
@@ -127,13 +113,10 @@ static void *btimer_thread(void *arg)
    Dmsg0(200, "Start watchdog thread\n");
    pthread_detach(pthread_self());
 
-   P(mutex);
    for ( ;!quit; ) {
-      struct timeval tv;
-      struct timezone tz;
       time_t timer_start, now;
 
-      Dmsg0(200, "Top of for loop\n");
+      Dmsg0(200, "Top of watchdog loop\n");
 
       watchdog_time = time(NULL);     /* update timer */
 
@@ -150,6 +133,7 @@ static void *btimer_thread(void *arg)
         if (fd) {
            timer_start = fd->timer_start;
            if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+              fd->timer_start = 0;   /* turn off timer */
               fd->timed_out = TRUE;
               Jmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
@@ -161,6 +145,7 @@ static void *btimer_thread(void *arg)
         if (fd) {
            timer_start = fd->timer_start;
            if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+              fd->timer_start = 0;   /* turn off timer */
               fd->timed_out = TRUE;
               Jmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
@@ -172,6 +157,7 @@ static void *btimer_thread(void *arg)
         if (fd) {
            timer_start = fd->timer_start;
            if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+              fd->timer_start = 0;   /* turn off timer */
               fd->timed_out = TRUE;
               Jmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
@@ -183,18 +169,15 @@ static void *btimer_thread(void *arg)
       }
       unlock_jcr_chain();
 
-      gettimeofday(&tv, &tz);
-      timeout.tv_nsec = 0;
-      timeout.tv_sec = tv.tv_sec + SLEEP_TIME;
-
-      Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout.tv_sec);
-      /* Note, this unlocks mutex during the sleep */
-      stat = pthread_cond_timedwait(&timer, &mutex, &timeout);
-      Dmsg1(200, "pthread_cond_timedwait stat=%d\n", stat);
-
+      Dmsg0(200, "Watchdog sleep.\n");
+      bmicrosleep(SLEEP_TIME, 0);
       now = time(NULL);
 
+      /* 
+       * Now handle child and thread timers set by the code.
+       */
       /* Walk child chain killing off any process overdue */
+      P(mutex);
       for (wid = timer_chain; wid; wid=wid->next) {
         int killed = FALSE;
         /* First ask him politely to go away */
@@ -212,7 +195,7 @@ static void *btimer_thread(void *arg)
         /* If we asked a child to die, wait 3 seconds and slam him */
         if (killed) {
            btimer_t *wid1;
-           sleep(3);
+           bmicrosleep(3, 0);
            for (wid1 = timer_chain; wid1; wid1=wid1->next) {
               if (wid->type == TYPE_CHILD &&
                   !wid1->killed && now > (wid1->start_time + wid1->wait)) {
@@ -223,10 +206,9 @@ static void *btimer_thread(void *arg)
            }
         }
       }
-      
+      V(mutex);
    } /* end of big for loop */
 
-   V(mutex);
    Dmsg0(200, "End watchdog\n");
    return NULL;
 }