]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/jcr.c
Fix conio.h problem on Solaris
[bacula/bacula] / bacula / src / lib / jcr.c
index 6a283b6537b176e1dde827e87dd61803e5893f2d..afa0555c8ee91c25720489c0e8041b3ed225e16b 100755 (executable)
@@ -9,7 +9,7 @@
  *
  */
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
 extern time_t watchdog_time;
 
 /* Forward referenced functions */
-static void timeout_handler(int sig);
+extern "C" void timeout_handler(int sig);
+
 static void jcr_timeout_check(watchdog_t *self);
 
 int num_jobs_run;
 dlist *last_jobs = NULL;
-#define MAX_LAST_JOBS 15
+const int max_last_jobs = 10;
 
 static JCR *jobs = NULL;             /* pointer to JCR chain */
 static brwlock_t lock;               /* lock for last jobs and JCR chain */
@@ -50,7 +51,7 @@ void init_last_jobs_list()
    int errstat;
    struct s_last_job *job_entry = NULL;
    if (!last_jobs) {
-      last_jobs = new dlist(job_entry, &job_entry->link);
+      last_jobs = New(dlist(job_entry, &job_entry->link));
       if ((errstat=rwl_init(&lock)) != 0) {
          Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"), 
               strerror(errstat));
@@ -61,10 +62,11 @@ void init_last_jobs_list()
 
 void term_last_jobs_list()
 {
-   struct s_last_job *je;
    if (last_jobs) {
-      foreach_dlist(je, last_jobs) {
-        free(je);                     
+      while (!last_jobs->empty()) {
+        void *je = last_jobs->first(); 
+        last_jobs->remove(je);
+        free(je);     
       }
       delete last_jobs;
       last_jobs = NULL;
@@ -77,17 +79,20 @@ void read_last_jobs_list(int fd, uint64_t addr)
    struct s_last_job *je, job;
    uint32_t num;
 
-   if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
+   Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
+   if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
       return;
    }
-   if (read(fd, &num, sizeof(num)) < 0) {
+   if (read(fd, &num, sizeof(num)) != sizeof(num)) {
       return;
    }
-   if (num > 4 * MAX_LAST_JOBS) {  /* sanity check */
+   Dmsg1(100, "Read num_items=%d\n", num);
+   if (num > 4 * max_last_jobs) {  /* sanity check */
       return;
    }
    for ( ; num; num--) {
-      if (read(fd, &job, sizeof(job)) < 0) {
+      if (read(fd, &job, sizeof(job)) != sizeof(job)) {
+         Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
         return;
       }
       if (job.JobId > 0) {
@@ -97,8 +102,10 @@ void read_last_jobs_list(int fd, uint64_t addr)
            init_last_jobs_list();
         }
         last_jobs->append(je);
-        if (last_jobs->size() > MAX_LAST_JOBS) {
-           last_jobs->remove(last_jobs->first());
+        if (last_jobs->size() > max_last_jobs) {
+           je = (struct s_last_job *)last_jobs->first();
+           last_jobs->remove(je);
+           free(je);
         }
       }
    }
@@ -109,17 +116,20 @@ uint64_t write_last_jobs_list(int fd, uint64_t addr)
    struct s_last_job *je;
    uint32_t num;
 
-   if (lseek(fd, addr, SEEK_SET) < 0) {
+   Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
+   if (lseek(fd, (off_t)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)) < 0) {
+      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)) < 0) {
+        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;
         }
       }
@@ -145,6 +155,27 @@ void unlock_last_jobs_list()
    unlock_jcr_chain();
 }
 
+/*
+ * Push a subroutine address into the job end callback stack
+ */
+void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
+{
+   jcr->job_end_push.append((void *)job_end_cb);
+   jcr->job_end_push.append(ctx);
+}
+
+/* Pop each job_end subroutine and call it */
+static void job_end_pop(JCR *jcr)
+{
+   void (*job_end_cb)(JCR *jcr, void *ctx);
+   void *ctx;
+   for (int i=jcr->job_end_push.size()-1; i > 0; ) {
+      ctx = jcr->job_end_push.get(i--);
+      job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
+      job_end_cb(jcr, ctx);
+   }
+}
+
 /*
  * Create a Job Control Record and link it into JCR chain
  * Returns newly allocated JCR
@@ -157,11 +188,11 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    MQUEUE_ITEM *item = NULL;
    struct sigaction sigtimer;
 
-   Dmsg0(200, "Enter new_jcr\n");
+   Dmsg0(400, "Enter new_jcr\n");
    jcr = (JCR *)malloc(size);
    memset(jcr, 0, size);
-   jcr->msg_queue = new dlist(item, &item->link);
-   jcr->my_thread_id = pthread_self();
+   jcr->msg_queue = New(dlist(item, &item->link));
+   jcr->job_end_push.init(1, false);
    jcr->sched_time = time(NULL);
    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
    jcr->use_count = 1;
@@ -171,7 +202,12 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    jcr->VolumeName[0] = 0;
    jcr->errmsg = get_pool_memory(PM_MESSAGE);
    jcr->errmsg[0] = 0;
-   strcpy(jcr->Job, "*Console*");     /* default */
+   /* Setup some dummy values */
+   jcr->Job[0] = 0;                  /* no job name by default */
+   jcr->JobId = 0;
+   jcr->JobType = JT_SYSTEM;         /* internal job until defined */
+   jcr->JobLevel = L_NONE;
+   jcr->JobStatus = JS_Created;
 
    sigtimer.sa_flags = 0;
    sigtimer.sa_handler = timeout_handler;
@@ -197,7 +233,7 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
  */
 static void remove_jcr(JCR *jcr)
 {
-   Dmsg0(150, "Enter remove_jcr\n");
+   Dmsg0(400, "Enter remove_jcr\n");
    if (!jcr) {
       Emsg0(M_ABORT, 0, "NULL jcr.\n");
    }
@@ -209,7 +245,7 @@ static void remove_jcr(JCR *jcr)
    if (jcr->next) {
       jcr->next->prev = jcr->prev;
    }
-   Dmsg0(150, "Leave remove_jcr\n");
+   Dmsg0(400, "Leave remove_jcr\n");
 }
 
 /*
@@ -227,6 +263,7 @@ static void free_common_jcr(JCR *jcr)
    case JT_RESTORE:
    case JT_ADMIN:
       num_jobs_run++;
+      last_job.Errors = jcr->Errors;
       last_job.JobType = jcr->JobType;
       last_job.JobId = jcr->JobId;
       last_job.VolSessionId = jcr->VolSessionId;
@@ -246,8 +283,10 @@ static void free_common_jcr(JCR *jcr)
            init_last_jobs_list();
         }
         last_jobs->append(je);
-        if (last_jobs->size() > MAX_LAST_JOBS) {
-           last_jobs->remove(last_jobs->first());
+        if (last_jobs->size() > max_last_jobs) {
+           je = (struct s_last_job *)last_jobs->first();
+           last_jobs->remove(je);
+           free(je);
         }
       }
       break;
@@ -256,8 +295,8 @@ static void free_common_jcr(JCR *jcr)
    }
    pthread_mutex_destroy(&jcr->mutex);
 
-   close_msg(jcr);                   /* close messages for this job */
    delete jcr->msg_queue;
+   close_msg(jcr);                   /* close messages for this job */
 
    /* do this after closing messages */
    if (jcr->client_name) {
@@ -302,41 +341,42 @@ static void free_common_jcr(JCR *jcr)
 #ifdef DEBUG
 void b_free_jcr(const char *file, int line, JCR *jcr)
 {
-   Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
+   Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
 
 #else
 
 void free_jcr(JCR *jcr)
 {
 
-   Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
+   Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
 
 #endif
 
+   dequeue_messages(jcr);
    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);
+   Dmsg3(400, "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();
-      Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+      Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
       return;
    }
-   remove_jcr(jcr);
+   remove_jcr(jcr);                  /* remove Jcr from chain */
+   unlock_jcr_chain();
+
+   job_end_pop(jcr);                 /* pop and call hooked routines */
 
-   Dmsg1(200, "End job=%d\n", jcr->JobId);
+   Dmsg1(400, "End job=%d\n", jcr->JobId);
    if (jcr->daemon_free_jcr) {
       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
    }
-
    free_common_jcr(jcr);
-
    close_msg(NULL);                  /* flush any daemon messages */
-   unlock_jcr_chain();
-   Dmsg0(200, "Exit free_jcr\n");
+   Dmsg0(400, "Exit free_jcr\n");
 }
 
 
@@ -347,7 +387,7 @@ void free_jcr(JCR *jcr)
 void free_locked_jcr(JCR *jcr)
 {
    jcr->use_count--;                 /* decrement use count */
-   Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+   Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
    if (jcr->use_count > 0) {         /* if in use */
       return;
    }
@@ -358,7 +398,6 @@ void free_locked_jcr(JCR *jcr)
 
 
 
-
 /*
  * Given a JobId, find the JCR     
  *   Returns: jcr on success
@@ -374,7 +413,7 @@ JCR *get_jcr_by_id(uint32_t JobId)
         P(jcr->mutex);
         jcr->use_count++;
         V(jcr->mutex);
-         Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+         Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
         break;
       }
    }
@@ -398,7 +437,7 @@ JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
         P(jcr->mutex);
         jcr->use_count++;
         V(jcr->mutex);
-         Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+         Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
         break;
       }
    }
@@ -429,7 +468,7 @@ JCR *get_jcr_by_partial_name(char *Job)
         P(jcr->mutex);
         jcr->use_count++;
         V(jcr->mutex);
-         Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+         Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
         break;
       }
    }
@@ -457,7 +496,7 @@ JCR *get_jcr_by_full_name(char *Job)
         P(jcr->mutex);
         jcr->use_count++;
         V(jcr->mutex);
-         Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+         Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
         break;
       }
    }
@@ -483,12 +522,24 @@ void set_jcr_job_status(JCR *jcr, int JobStatus)
    }
 }
 
+#ifdef TRACE_JCR_CHAIN
+static int lock_count = 0;
+#endif
+
 /* 
  * Lock the chain
  */
+#ifdef TRACE_JCR_CHAIN
+void b_lock_jcr_chain(const char *fname, int line)
+#else
 void lock_jcr_chain()
+#endif
 {
    int errstat;
+#ifdef TRACE_JCR_CHAIN 
+   Dmsg3(000, "Lock jcr chain %d from %s:%d\n", ++lock_count,
+      fname, line);
+#endif
    if ((errstat=rwl_writelock(&lock)) != 0) {
       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
           strerror(errstat));
@@ -498,9 +549,17 @@ void lock_jcr_chain()
 /*
  * Unlock the chain
  */
+#ifdef TRACE_JCR_CHAIN
+void b_unlock_jcr_chain(const char *fname, int line)
+#else
 void unlock_jcr_chain()
+#endif
 {
    int errstat;
+#ifdef TRACE_JCR_CHAIN 
+   Dmsg3(000, "Unlock jcr chain %d from %s:%d\n", lock_count--,
+      fname, line);
+#endif
    if ((errstat=rwl_writeunlock(&lock)) != 0) {
       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
           strerror(errstat));
@@ -521,7 +580,7 @@ JCR *get_next_jcr(JCR *prev_jcr)
       P(jcr->mutex);
       jcr->use_count++;
       V(jcr->mutex);
-      Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+      Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
    }
    return jcr;
 }
@@ -597,13 +656,13 @@ static void jcr_timeout_check(watchdog_t *self)
    }
    unlock_jcr_chain();
 
-   Dmsg0(200, "Finished JCR timeout checks\n");
+   Dmsg0(400, "Finished JCR timeout checks\n");
 }
 
 /*
  * Timeout signal comes here
  */
-static void timeout_handler(int sig)
+extern "C" void timeout_handler(int sig)
 {
    return;                           /* thus interrupting the function */
 }