]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/jcr.c
fix variable name
[bacula/bacula] / bacula / src / lib / jcr.c
index 58a155b08b1dc723c3eef66455f76ae0c76bfe51..477fde6161b9eca5e75a1ec75a7569dcca82c757 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -56,7 +56,6 @@
 const int dbglvl = 3400;
 
 /* External variables we reference */
-extern time_t watchdog_time;
 
 /* External referenced functions */
 void free_bregexps(alist *bregexps);
@@ -271,18 +270,6 @@ const char *JCR::get_ActionName(bool past)
    }
 }
 
-/* Set Job type in JCR and also set appropriate read flag */
-void JCR::set_JobType(int32_t JobType)
-{
-   m_JobType = JobType;
-}
-
-/* Set Job level in JCR and also set appropriate read flag */
-void JCR::set_JobLevel(int32_t JobLevel)
-{
-   m_JobLevel = JobLevel;
-}
-
 bool JCR::JobReads()
 {
    switch (m_JobType) {
@@ -368,9 +355,9 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    /* Setup some dummy values */
    bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
    jcr->JobId = 0;
-   jcr->set_JobType(JT_SYSTEM);          /* internal job until defined */
-   jcr->set_JobLevel(L_NONE);
-   set_jcr_job_status(jcr, JS_Created);       /* ready to run */
+   jcr->setJobType(JT_SYSTEM);           /* internal job until defined */
+   jcr->setJobLevel(L_NONE);
+   jcr->setJobStatus(JS_Created);        /* ready to run */
    set_jcr_in_tsd(jcr);
    sigtimer.sa_flags = 0;
    sigtimer.sa_handler = timeout_handler;
@@ -475,8 +462,7 @@ static void free_common_jcr(JCR *jcr)
       free_guid_list(jcr->id_list);
       jcr->id_list = NULL;
    }
-   /* Invalidate the tsd jcr data */
-   set_jcr_in_tsd(INVALID_JCR);
+   remove_jcr_from_tsd(jcr);
    free(jcr);
 }
 
@@ -501,7 +487,6 @@ void free_jcr(JCR *jcr)
 
 #endif
 
-   dequeue_messages(jcr);
    lock_jcr_chain();
    jcr->dec_use_count();              /* decrement use count */
    if (jcr->use_count() < 0) {
@@ -521,7 +506,9 @@ void free_jcr(JCR *jcr)
             jcr->JobId, jcr->use_count(), jcr->Job);
    }
    remove_jcr(jcr);                   /* remove Jcr from chain */
+   unlock_jcr_chain();
 
+   dequeue_messages(jcr);
    job_end_pop(jcr);                  /* pop and call hooked routines */
 
    Dmsg1(dbglvl, "End job=%d\n", jcr->JobId);
@@ -540,7 +527,7 @@ void free_jcr(JCR *jcr)
          num_jobs_run++;
          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
          memset(je, 0, sizeof(struct s_last_job));  /* zero in case unset fields */
-         je->Errors = jcr->Errors;
+         je->Errors = jcr->JobErrors;
          je->JobType = jcr->get_JobType();
          je->JobId = jcr->JobId;
          je->VolSessionId = jcr->VolSessionId;
@@ -573,13 +560,27 @@ void free_jcr(JCR *jcr)
       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
    }
 
-   unlock_jcr_chain();
    free_common_jcr(jcr);
    close_msg(NULL);                   /* flush any daemon messages */
    garbage_collect_memory_pool();
    Dmsg0(dbglvl, "Exit free_jcr\n");
 }
 
+/*
+ * Remove jcr from thread specific data, but
+ *   but make sure it is us who are attached.
+ */
+void remove_jcr_from_tsd(JCR *jcr)
+{
+   JCR *tjcr = get_jcr_from_tsd();
+   if (tjcr == jcr) { 
+      set_jcr_in_tsd(INVALID_JCR);
+   }
+}
+
+/*
+ * Put this jcr in the thread specifc data 
+ */
 void set_jcr_in_tsd(JCR *jcr)
 {
    int status = pthread_setspecific(jcr_key, (void *)jcr);
@@ -589,6 +590,9 @@ void set_jcr_in_tsd(JCR *jcr)
    }
 }
 
+/*
+ * Give me the jcr that is attached to this thread
+ */
 JCR *get_jcr_from_tsd()
 {
    JCR *jcr = (JCR *)pthread_getspecific(jcr_key);
@@ -637,6 +641,30 @@ JCR *get_jcr_by_id(uint32_t JobId)
    return jcr;
 }
 
+/*
+ * Given a thread id, find the JobId
+ *   Returns: JobId on success
+ *            0 on failure
+ */
+uint32_t get_jobid_from_tid(pthread_t tid)
+{
+   JCR *jcr = NULL;
+   bool found = false;
+
+   foreach_jcr(jcr) {
+      if (pthread_equal(jcr->my_thread_id, tid)) {
+         found = true;
+         break;
+      }
+   }
+   endeach_jcr(jcr);
+   if (found) {
+      return jcr->JobId;
+   }
+   return 0;
+}
+
+
 /*
  * Given a SessionId and SessionTime, find the JCR
  *   Returns: jcr on success
@@ -723,10 +751,9 @@ static int get_status_priority(int JobStatus)
    switch (JobStatus) {
    case JS_ErrorTerminated:
    case JS_FatalError:
-      priority = 10;
-      break;
    case JS_Canceled:
-      priority = 9;
+   case JS_Incomplete:
+      priority = 10;
       break;
    case JS_Error:
       priority = 8;
@@ -793,22 +820,28 @@ static void update_wait_time(JCR *jcr, int newJobStatus)
 
 void set_jcr_job_status(JCR *jcr, int JobStatus)
 {
+   jcr->setJobStatus(JobStatus);
+}
+
+void JCR::setJobStatus(int JobStatus)
+{
+   JCR *jcr = this;
    int priority, old_priority;
-   int oldJobStatus = jcr->JobStatus;
+   int oldJobStatus = JobStatus;
    priority = get_status_priority(JobStatus);
    old_priority = get_status_priority(oldJobStatus);
    
-   Dmsg2(800, "set_jcr_job_status(%s, %c)\n", jcr->Job, JobStatus);
+   Dmsg2(800, "set_jcr_job_status(%s, %c)\n", Job, JobStatus);
 
    /* Update wait_time depending on newJobStatus and oldJobStatus */
-   update_wait_time(jcr, JobStatus);
+   update_wait_time(this, JobStatus);
 
    /*
     * For a set of errors, ... keep the current status
     *   so it isn't lost. For all others, set it.
     */
-   Dmsg3(300, "jid=%u OnEntry JobStatus=%c set=%c\n", (uint32_t)jcr->JobId,
-         jcr->JobStatus, JobStatus);
+   Dmsg3(300, "jid=%u OnEntry JobStatus=%c set=%c\n", (uint32_t)JobId,
+         JobStatus, JobStatus);
    if (priority >= old_priority) {
       jcr->JobStatus = JobStatus;     /* replace with new priority */
    }
@@ -1000,6 +1033,41 @@ static void jcr_timeout_check(watchdog_t *self)
    Dmsg0(dbglvl, "Finished JCR timeout checks\n");
 }
 
+/* 
+ * Return next JobId from comma separated list   
+ *
+ * Returns:
+ *   1 if next JobId returned
+ *   0 if no more JobIds are in list
+ *  -1 there is an error
+ */
+int get_next_jobid_from_list(char **p, uint32_t *JobId)
+{
+   const int maxlen = 30;
+   char jobid[maxlen+1];
+   char *q = *p;
+
+   jobid[0] = 0;
+   for (int i=0; i<maxlen; i++) {
+      if (*q == 0) {
+         break;
+      } else if (*q == ',') {
+         q++;
+         break;
+      }
+      jobid[i] = *q++;
+      jobid[i+1] = 0;
+   }
+   if (jobid[0] == 0) {
+      return 0;
+   } else if (!is_a_number(jobid)) {
+      return -1;                      /* error */
+   }
+   *p = q;
+   *JobId = str_to_int64(jobid);
+   return 1;
+}
+
 /*
  * Timeout signal comes here
  */
@@ -1008,24 +1076,26 @@ extern "C" void timeout_handler(int sig)
    return;                            /* thus interrupting the function */
 }
 
-/* Used to display mdb information after a fatal signal */
+/* Used to display specific daemon information after a fatal signal 
+ * (like B_DB in the director)
+ */
 #define MAX_DBG_HOOK 10
-static dbg_jcr_hook *dbg_hooks[MAX_DBG_HOOK];
+static dbg_jcr_hook_t *dbg_jcr_hooks[MAX_DBG_HOOK];
 static int dbg_jcr_handler_count;
 
-void dbg_add_hook(dbg_jcr_hook *fct)
+void dbg_jcr_add_hook(dbg_jcr_hook_t *fct)
 {
    ASSERT(dbg_jcr_handler_count < MAX_DBG_HOOK);
-   dbg_hooks[dbg_jcr_handler_count++] = fct;
+   dbg_jcr_hooks[dbg_jcr_handler_count++] = fct;
 }
 
 /*
  * !!! WARNING !!! 
  *
- * This function should be used ONLY after a violent signal. We walk through the
+ * This function should be used ONLY after a fatal signal. We walk through the
  * JCR chain without doing any lock, bacula should not be running.
  */
-void _print_jcr_dbg(FILE *fp)
+void _dbg_print_jcr(FILE *fp)
 {
    char buf1[128], buf2[128], buf3[128], buf4[128];
    if (!jcrs) {
@@ -1045,8 +1115,8 @@ void _print_jcr_dbg(FILE *fp)
       fprintf(fp, "\tuse_count=%i\n",
               jcr->use_count());
 #else
-      fprintf(fp, "\tuse_count=%i threadid=0x%x\n",
-              jcr->use_count(), (int)jcr->my_thread_id);
+      /* KES -- removed non-portable code referencing pthread_t */
+      fprintf(fp, "\tuse_count=%d\n", jcr->use_count());
 #endif
       fprintf(fp, "\tJobType=%c JobLevel=%c\n",
               jcr->get_JobType(), jcr->get_JobLevel());
@@ -1061,9 +1131,8 @@ void _print_jcr_dbg(FILE *fp)
               jcr->db, jcr->db_batch, jcr->batch_started);
       
       for(int i=0; i < dbg_jcr_handler_count; i++) {
-         dbg_jcr_hook *fct = dbg_hooks[i];
+         dbg_jcr_hook_t *fct = dbg_jcr_hooks[i];
          fct(jcr, fp);
       }
    }
 }
-