]> 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 8f7d60bd1b828e12400d3af0028fcade3e0dc2a2..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;
-static const int max_last_jobs = 10;
+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;
@@ -78,7 +80,7 @@ void read_last_jobs_list(int fd, uint64_t addr)
    uint32_t num;
 
    Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
-   if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
+   if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
       return;
    }
    if (read(fd, &num, sizeof(num)) != sizeof(num)) {
@@ -101,7 +103,9 @@ void read_last_jobs_list(int fd, uint64_t addr)
         }
         last_jobs->append(je);
         if (last_jobs->size() > max_last_jobs) {
-           last_jobs->remove(last_jobs->first());
+           je = (struct s_last_job *)last_jobs->first();
+           last_jobs->remove(je);
+           free(je);
         }
       }
    }
@@ -113,7 +117,7 @@ uint64_t write_last_jobs_list(int fd, uint64_t addr)
    uint32_t num;
 
    Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
-   if (lseek(fd, addr, SEEK_SET) < 0) {
+   if (lseek(fd, (off_t)addr, SEEK_SET) < 0) {
       return 0;
    }
    if (last_jobs) {
@@ -154,18 +158,21 @@ void unlock_last_jobs_list()
 /*
  * Push a subroutine address into the job end callback stack
  */
-void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
+void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
 {
-   jcr->job_end_push.prepend((void *)job_end_cb);
+   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);
-   for (int i=0; i<jcr->job_end_push.size(); i++) {
-      job_end_cb = (void (*)(JCR *))jcr->job_end_push.get(i);
-      job_end_cb(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);
    }
 }
 
@@ -184,9 +191,8 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    Dmsg0(400, "Enter new_jcr\n");
    jcr = (JCR *)malloc(size);
    memset(jcr, 0, size);
-   jcr->msg_queue = new dlist(item, &item->link);
+   jcr->msg_queue = New(dlist(item, &item->link));
    jcr->job_end_push.init(1, false);
-   jcr->my_thread_id = pthread_self();
    jcr->sched_time = time(NULL);
    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
    jcr->use_count = 1;
@@ -199,7 +205,7 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    /* Setup some dummy values */
    jcr->Job[0] = 0;                  /* no job name by default */
    jcr->JobId = 0;
-   jcr->JobType = JT_ADMIN;
+   jcr->JobType = JT_SYSTEM;         /* internal job until defined */
    jcr->JobLevel = L_NONE;
    jcr->JobStatus = JS_Created;
 
@@ -257,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;
@@ -277,7 +284,9 @@ static void free_common_jcr(JCR *jcr)
         }
         last_jobs->append(je);
         if (last_jobs->size() > max_last_jobs) {
-           last_jobs->remove(last_jobs->first());
+           je = (struct s_last_job *)last_jobs->first();
+           last_jobs->remove(je);
+           free(je);
         }
       }
       break;
@@ -343,6 +352,7 @@ void free_jcr(JCR *jcr)
 
 #endif
 
+   dequeue_messages(jcr);
    lock_jcr_chain();
    jcr->use_count--;                 /* decrement use count */
    if (jcr->use_count < 0) {
@@ -355,19 +365,17 @@ void free_jcr(JCR *jcr)
       Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
       return;
    }
-   dequeue_messages(jcr);
-   remove_jcr(jcr);
+   remove_jcr(jcr);                  /* remove Jcr from chain */
+   unlock_jcr_chain();
+
    job_end_pop(jcr);                 /* pop and call hooked routines */
 
    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(400, "Exit free_jcr\n");
 }
 
@@ -514,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));
@@ -529,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));
@@ -634,7 +662,7 @@ static void jcr_timeout_check(watchdog_t *self)
 /*
  * Timeout signal comes here
  */
-static void timeout_handler(int sig)
+extern "C" void timeout_handler(int sig)
 {
    return;                           /* thus interrupting the function */
 }