/*
Bacula® - The Network Backup Solution
- Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
+ Copyright (C) 2000-2012 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.
This program is Free Software; you can redistribute it and/or
- modify it under the terms of version two of the GNU General Public
+ modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*
* Kern E. Sibbald, December 2000
*
- * Version $Id$
- *
* These routines are thread safe.
*
* The job list routines were re-written in May 2005 to
pthread_once_t key_once = PTHREAD_ONCE_INIT;
+static char Job_status[] = "Status Job=%s JobStatus=%d\n";
+
void lock_jobs()
{
}
jcr = (JCR *)malloc(size);
memset(jcr, 0, size);
- jcr->my_thread_id = pthread_self();
jcr->msg_queue = New(dlist(item, &item->link));
+ if ((status = pthread_mutex_init(&jcr->msg_queue_mutex, NULL)) != 0) {
+ berrno be;
+ Jmsg(NULL, M_ABORT, 0, _("Could not init msg_queue mutex. ERR=%s\n"),
+ be.bstrerror(status));
+ }
jcr->job_end_push.init(1, false);
jcr->sched_time = time(NULL);
jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
jcr->VolumeName[0] = 0;
jcr->errmsg = get_pool_memory(PM_MESSAGE);
jcr->errmsg[0] = 0;
+ jcr->comment = get_pool_memory(PM_FNAME);
+ jcr->comment[0] = 0;
/* Setup some dummy values */
bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
jcr->JobId = 0;
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;
sigfillset(&sigtimer.sa_mask);
*/
static void free_common_jcr(JCR *jcr)
{
+ /* Uses jcr lock/unlock */
+ remove_jcr_from_tsd(jcr);
+ jcr->set_killable(false);
+
jcr->destroy_mutex();
if (jcr->msg_queue) {
delete jcr->msg_queue;
jcr->msg_queue = NULL;
+ pthread_mutex_destroy(&jcr->msg_queue_mutex);
}
close_msg(jcr); /* close messages for this job */
free_guid_list(jcr->id_list);
jcr->id_list = NULL;
}
- remove_jcr_from_tsd(jcr);
+ if (jcr->comment) {
+ free_pool_memory(jcr->comment);
+ jcr->comment = NULL;
+ }
free(jcr);
}
Dmsg1(dbglvl, "End job=%d\n", jcr->JobId);
/* Keep some statistics */
- switch (jcr->get_JobType()) {
+ switch (jcr->getJobType()) {
case JT_BACKUP:
case JT_VERIFY:
case JT_RESTORE:
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->JobErrors;
- je->JobType = jcr->get_JobType();
+ je->JobType = jcr->getJobType();
je->JobId = jcr->JobId;
je->VolSessionId = jcr->VolSessionId;
je->VolSessionTime = jcr->VolSessionTime;
je->JobFiles = jcr->JobFiles;
je->JobBytes = jcr->JobBytes;
je->JobStatus = jcr->JobStatus;
- je->JobLevel = jcr->get_JobLevel();
+ je->JobLevel = jcr->getJobLevel();
je->start_time = jcr->start_time;
je->end_time = time(NULL);
free_common_jcr(jcr);
close_msg(NULL); /* flush any daemon messages */
- garbage_collect_memory_pool();
Dmsg0(dbglvl, "Exit free_jcr\n");
}
}
}
+void JCR::set_killable(bool killable)
+{
+ JCR *jcr = this;
+ jcr->lock();
+ jcr->my_thread_killable = killable;
+ if (killable) {
+ jcr->my_thread_id = pthread_self();
+ } else {
+ memset(&jcr->my_thread_id, 0, sizeof(jcr->my_thread_id));
+ }
+ jcr->unlock();
+}
+
/*
- * Put this jcr in the thread specifc data
+ * Put this jcr in the thread specifc data
+ * if update_thread_info is true and the jcr is valide,
+ * we update the my_thread_id in the JCR
*/
void set_jcr_in_tsd(JCR *jcr)
{
int status = pthread_setspecific(jcr_key, (void *)jcr);
if (status != 0) {
berrno be;
- Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"), be.bstrerror(status));
+ Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"),
+ be.bstrerror(status));
}
}
+void JCR::my_thread_send_signal(int sig)
+{
+ this->lock();
+ if (this->is_killable() &&
+ !pthread_equal(this->my_thread_id, pthread_self()))
+ {
+ Dmsg1(800, "Send kill to jid=%d\n", this->JobId);
+ pthread_kill(this->my_thread_id, sig);
+
+ } else if (!this->is_killable()) {
+ Dmsg1(10, "Warning, can't send kill to jid=%d\n", this->JobId);
+ }
+ this->unlock();
+}
+
/*
* Give me the jcr that is attached to this thread
*/
{
int priority = 0;
switch (JobStatus) {
+ case JS_Incomplete:
+ priority = 10;
+ break;
case JS_ErrorTerminated:
case JS_FatalError:
case JS_Canceled:
- case JS_Incomplete:
- priority = 10;
+ priority = 9;
break;
case JS_Error:
priority = 8;
return priority;
}
+/*
+ * Send Job status to Director
+ */
+bool JCR::sendJobStatus()
+{
+ JCR *jcr = this;
+ if (jcr->dir_bsock) {
+ return jcr->dir_bsock->fsend(Job_status, jcr->Job, jcr->JobStatus);
+ }
+ return true;
+}
-void set_jcr_job_status(JCR *jcr, int JobStatus)
+/*
+ * Set and send Job status to Director
+ */
+bool JCR::sendJobStatus(int newJobStatus)
{
- jcr->setJobStatus(JobStatus);
+ JCR *jcr = this;
+ if (!jcr->is_JobStatus(newJobStatus)) {
+ setJobStatus(newJobStatus);
+ if (jcr->dir_bsock) {
+ return jcr->dir_bsock->fsend(Job_status, jcr->Job, jcr->JobStatus);
+ }
+ }
+ return true;
}
+
void JCR::setJobStatus(int newJobStatus)
{
JCR *jcr = this;
}
if (oldJobStatus != jcr->JobStatus) {
- Dmsg2(800, "leave set_job_status old=%c new=%c\n", oldJobStatus, newJobStatus);
+ Dmsg2(800, "leave setJobStatus old=%c new=%c\n", oldJobStatus, newJobStatus);
// generate_plugin_event(jcr, bEventStatusChange, NULL);
}
}
}
}
+/*
+ * Return number of Jobs
+ */
+int job_count()
+{
+ JCR *jcr;
+ int count = 0;
+
+ lock_jcr_chain();
+ for (jcr = (JCR *)jcrs->first(); (jcr = (JCR *)jcrs->next(jcr)); ) {
+ if (jcr->JobId > 0) {
+ count++;
+ }
+ }
+ unlock_jcr_chain();
+ return count;
+}
+
/*
* Setup to call the timeout check routine every 30 seconds
Qmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
watchdog_time - timer_start);
- pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
}
}
bs = jcr->file_bsock;
Qmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
watchdog_time - timer_start);
- pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
}
}
bs = jcr->dir_bsock;
Qmsg(jcr, M_ERROR, 0, _(
"Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
watchdog_time - timer_start);
- pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
}
}
}
static dbg_jcr_hook_t *dbg_jcr_hooks[MAX_DBG_HOOK];
static int dbg_jcr_handler_count;
-void dbg_jcr_add_hook(dbg_jcr_hook_t *fct)
+void dbg_jcr_add_hook(dbg_jcr_hook_t *hook)
{
ASSERT(dbg_jcr_handler_count < MAX_DBG_HOOK);
- dbg_jcr_hooks[dbg_jcr_handler_count++] = fct;
+ dbg_jcr_hooks[dbg_jcr_handler_count++] = hook;
}
/*
* !!! WARNING !!!
*
* 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.
+ * JCR chain without doing any lock, Bacula should not be running.
*/
-void _dbg_print_jcr(FILE *fp)
+void dbg_print_jcr(FILE *fp)
{
char buf1[128], buf2[128], buf3[128], buf4[128];
if (!jcrs) {
return;
}
- fprintf(fp, "Attempt to dump current JCRs\n");
+ fprintf(fp, "Attempt to dump current JCRs. njcrs=%d\n", jcrs->size());
for (JCR *jcr = (JCR *)jcrs->first(); jcr ; jcr = (JCR *)jcrs->next(jcr)) {
- if (!jcr) { /* protect us against something ? */
- continue;
- }
-
- fprintf(fp, "JCR=%p JobId=%i name=%s JobStatus=%c\n",
- jcr, jcr->JobId, jcr->Job, jcr->JobStatus);
-#ifdef HAVE_WIN32
- fprintf(fp, "\tuse_count=%i\n",
- jcr->use_count());
-#else
- /* KES -- removed non-portable code referencing pthread_t */
- fprintf(fp, "\tuse_count=%d\n", jcr->use_count());
-#endif
+ fprintf(fp, "threadid=%p JobId=%d JobStatus=%c jcr=%p name=%s\n",
+ (void *)jcr->my_thread_id, (int)jcr->JobId, jcr->JobStatus, jcr, jcr->Job);
+ fprintf(fp, "threadid=%p killable=%d JobId=%d JobStatus=%c "
+ "jcr=%p name=%s\n",
+ (void *)jcr->my_thread_id, jcr->is_killable(),
+ (int)jcr->JobId, jcr->JobStatus, jcr, jcr->Job);
+ fprintf(fp, "\tuse_count=%i\n", jcr->use_count());
fprintf(fp, "\tJobType=%c JobLevel=%c\n",
- jcr->get_JobType(), jcr->get_JobLevel());
+ jcr->getJobType(), jcr->getJobLevel());
bstrftime(buf1, sizeof(buf1), jcr->sched_time);
bstrftime(buf2, sizeof(buf2), jcr->start_time);
bstrftime(buf3, sizeof(buf3), jcr->end_time);
bstrftime(buf4, sizeof(buf4), jcr->wait_time);
fprintf(fp, "\tsched_time=%s start_time=%s\n\tend_time=%s wait_time=%s\n",
buf1, buf2, buf3, buf4);
- fprintf(fp, "\tdequeing=%i\n", jcr->dequeuing);
fprintf(fp, "\tdb=%p db_batch=%p batch_started=%i\n",
jcr->db, jcr->db_batch, jcr->batch_started);
+ /*
+ * Call all the jcr debug hooks
+ */
for(int i=0; i < dbg_jcr_handler_count; i++) {
- dbg_jcr_hook_t *fct = dbg_jcr_hooks[i];
- fct(jcr, fp);
+ dbg_jcr_hook_t *hook = dbg_jcr_hooks[i];
+ hook(jcr, fp);
}
}
}