]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/jcr.c
State file debug + full functionality for alist
[bacula/bacula] / bacula / src / lib / jcr.c
index 1bb7e5d810fe70abbb76ab1c72ec5be1fd37cdab..00cd0307d9fe4964b581099d183dd5d3a201f619 100755 (executable)
@@ -38,12 +38,11 @@ extern time_t watchdog_time;
 static void timeout_handler(int sig);
 static void jcr_timeout_check(watchdog_t *self);
 
-struct s_last_job last_job;    /* last job run by this daemon */
+int num_jobs_run;
 dlist *last_jobs = NULL;
-#define MAX_LAST_JOBS 10
+#define MAX_LAST_JOBS 15
 
 static JCR *jobs = NULL;             /* pointer to JCR chain */
-
 static brwlock_t lock;               /* lock for last jobs and JCR chain */
 
 void init_last_jobs_list()
@@ -52,26 +51,92 @@ void init_last_jobs_list()
    struct s_last_job *job_entry = NULL;
    if (!last_jobs) {
       last_jobs = new dlist(job_entry, &job_entry->link);
-      memset(&last_job, 0, sizeof(last_job));
-   }
-   if ((errstat=rwl_init(&lock)) != 0) {
-      Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"), 
-           strerror(errstat));
+      if ((errstat=rwl_init(&lock)) != 0) {
+         Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"), 
+              strerror(errstat));
+      }
    }
 
 }
 
 void term_last_jobs_list()
 {
-   char *je;
+   struct s_last_job *je;
    if (last_jobs) {
       foreach_dlist(je, last_jobs) {
         free(je);                     
       }
       delete last_jobs;
       last_jobs = NULL;
+      rwl_destroy(&lock);
    }
-   rwl_destroy(&lock);
+}
+
+void read_last_jobs_list(int fd, uint64_t addr)
+{
+   struct s_last_job *je, job;
+   uint32_t num;
+
+   Dmsg1(010, "read_last_jobs seek to %d\n", (int)addr);
+   if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
+      return;
+   }
+   if (read(fd, &num, sizeof(num)) != sizeof(num)) {
+      return;
+   }
+   Dmsg1(010, "Read num_items=%d\n", num);
+   if (num > 4 * MAX_LAST_JOBS) {  /* sanity check */
+      return;
+   }
+   for ( ; num; num--) {
+      if (read(fd, &job, sizeof(job)) != sizeof(job)) {
+         Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
+        return;
+      }
+      if (job.JobId > 0) {
+        je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
+        memcpy((char *)je, (char *)&job, sizeof(job));
+        if (!last_jobs) {
+           init_last_jobs_list();
+        }
+        last_jobs->append(je);
+        if (last_jobs->size() > MAX_LAST_JOBS) {
+           last_jobs->remove(last_jobs->first());
+        }
+      }
+   }
+}
+
+uint64_t write_last_jobs_list(int fd, uint64_t addr)
+{
+   struct s_last_job *je;
+   uint32_t num;
+
+   Dmsg1(010, "write_last_jobs seek to %d\n", (int)addr);
+   if (lseek(fd, addr, SEEK_SET) < 0) {
+      return 0;
+   }
+   if (last_jobs) {
+      /* First record is number of entires */
+      num = last_jobs->size();
+      if (write(fd, &num, sizeof(num)) != sizeof(num)) {
+         Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
+        return 0;
+      }
+      foreach_dlist(je, last_jobs) {
+        if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
+            Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
+           return 0;
+        }
+      }
+   }
+   /* Return current address */
+   ssize_t stat = lseek(fd, 0, SEEK_CUR);
+   if (stat < 0) {
+      stat = 0;
+   }
+   return stat;
+      
 }
 
 void lock_last_jobs_list() 
@@ -159,13 +224,15 @@ static void remove_jcr(JCR *jcr)
  */
 static void free_common_jcr(JCR *jcr)
 {
+   struct s_last_job *je, last_job;
+
    /* Keep some statistics */
    switch (jcr->JobType) {
    case JT_BACKUP:
    case JT_VERIFY:
    case JT_RESTORE:
    case JT_ADMIN:
-      last_job.NumJobs++;
+      num_jobs_run++;
       last_job.JobType = jcr->JobType;
       last_job.JobId = jcr->JobId;
       last_job.VolSessionId = jcr->VolSessionId;
@@ -177,6 +244,18 @@ static void free_common_jcr(JCR *jcr)
       last_job.JobLevel = jcr->JobLevel;
       last_job.start_time = jcr->start_time;
       last_job.end_time = time(NULL);
+      /* Keep list of last jobs, but not Console where JobId==0 */
+      if (last_job.JobId > 0) {
+        je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
+        memcpy((char *)je, (char *)&last_job, sizeof(last_job));
+        if (!last_jobs) {
+           init_last_jobs_list();
+        }
+        last_jobs->append(je);
+        if (last_jobs->size() > MAX_LAST_JOBS) {
+           last_jobs->remove(last_jobs->first());
+        }
+      }
       break;
    default:
       break;
@@ -239,10 +318,13 @@ void free_jcr(JCR *jcr)
    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
 
 #endif
-   struct s_last_job *je;
 
    lock_jcr_chain();
    jcr->use_count--;                 /* decrement use count */
+   if (jcr->use_count < 0) {
+      Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
+        jcr->use_count, jcr->JobId);
+   }
    Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
    if (jcr->use_count > 0) {         /* if in use */
       unlock_jcr_chain();
@@ -258,19 +340,6 @@ void free_jcr(JCR *jcr)
 
    free_common_jcr(jcr);
 
-   /* Keep list of last jobs, but not Console where JobId==0 */
-   if (last_job.JobId > 0) {
-      je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
-      memcpy((char *)je, (char *)&last_job, sizeof(last_job));
-      if (!last_jobs) {
-        init_last_jobs_list();
-      }
-      last_jobs->append(je);
-      if (last_jobs->size() > MAX_LAST_JOBS) {
-        last_jobs->remove(last_jobs->first());
-      }
-      last_job.JobId = 0;            /* zap last job */
-   }
    close_msg(NULL);                  /* flush any daemon messages */
    unlock_jcr_chain();
    Dmsg0(200, "Exit free_jcr\n");