*
*/
/*
- 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 */
+static JCR *jobs = NULL; /* pointer to JCR chain */
+static brwlock_t lock; /* lock for last jobs and JCR chain */
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));
+ strerror(errstat));
}
}
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;
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) {
+ 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)) != sizeof(num)) {
return;
}
- Dmsg1(010, "Read num_items=%d\n", num);
+ 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)) != sizeof(job)) {
Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
- return;
+ 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());
- }
+ 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) {
+ je = (struct s_last_job *)last_jobs->first();
+ last_jobs->remove(je);
+ free(je);
+ }
}
}
}
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) {
+ 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) {
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;
+ return 0;
}
foreach_dlist(je, last_jobs) {
- if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
+ 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 0;
+ }
}
}
/* Return current address */
/*
* 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);
}
}
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->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;
jcr->errmsg = get_pool_memory(PM_MESSAGE);
jcr->errmsg[0] = 0;
/* Setup some dummy values */
- jcr->Job[0] = 0; /* no job name by default */
+ 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;
/*
* Remove a JCR from the chain
* NOTE! The chain must be locked prior to calling
- * this routine.
+ * this routine.
*/
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");
}
- if (!jcr->prev) { /* if no prev */
- jobs = jcr->next; /* set new head */
+ if (!jcr->prev) { /* if no prev */
+ jobs = jcr->next; /* set new head */
} else {
jcr->prev->next = jcr->next; /* update prev */
}
if (jcr->next) {
jcr->next->prev = jcr->prev;
}
- Dmsg0(150, "Leave remove_jcr\n");
+ Dmsg0(400, "Leave remove_jcr\n");
}
/*
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;
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());
- }
+ 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) {
+ je = (struct s_last_job *)last_jobs->first();
+ last_jobs->remove(je);
+ free(je);
+ }
}
break;
default:
}
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) {
#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 */
+ 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);
+ 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 */
+ 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);
- job_end_pop(jcr); /* pop and call hooked routines */
+ remove_jcr(jcr); /* remove Jcr from chain */
+ unlock_jcr_chain();
- Dmsg1(200, "End job=%d\n", jcr->JobId);
+ 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(200, "Exit free_jcr\n");
+ close_msg(NULL); /* flush any daemon messages */
+ Dmsg0(400, "Exit free_jcr\n");
}
*/
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);
- if (jcr->use_count > 0) { /* if in use */
+ jcr->use_count--; /* decrement 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;
}
remove_jcr(jcr);
- jcr->daemon_free_jcr(jcr); /* call daemon free routine */
+ jcr->daemon_free_jcr(jcr); /* call daemon free routine */
free_common_jcr(jcr);
}
/*
- * Given a JobId, find the JCR
+ * Given a JobId, find the JCR
* Returns: jcr on success
- * NULL on failure
+ * NULL on failure
*/
JCR *get_jcr_by_id(uint32_t JobId)
{
- JCR *jcr;
+ JCR *jcr;
- lock_jcr_chain(); /* lock chain */
+ lock_jcr_chain(); /* lock chain */
for (jcr = jobs; jcr; jcr=jcr->next) {
if (jcr->JobId == 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);
- break;
+ P(jcr->mutex);
+ jcr->use_count++;
+ V(jcr->mutex);
+ Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+ break;
}
}
unlock_jcr_chain();
}
/*
- * Given a SessionId and SessionTime, find the JCR
+ * Given a SessionId and SessionTime, find the JCR
* Returns: jcr on success
- * NULL on failure
+ * NULL on failure
*/
JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
{
- JCR *jcr;
+ JCR *jcr;
lock_jcr_chain();
for (jcr = jobs; jcr; jcr=jcr->next) {
if (jcr->VolSessionId == SessionId &&
- jcr->VolSessionTime == 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);
- break;
+ jcr->VolSessionTime == SessionTime) {
+ P(jcr->mutex);
+ jcr->use_count++;
+ V(jcr->mutex);
+ Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+ break;
}
}
unlock_jcr_chain();
/*
- * Given a Job, find the JCR
+ * Given a Job, find the JCR
* compares on the number of characters in Job
* thus allowing partial matches.
* Returns: jcr on success
- * NULL on failure
+ * NULL on failure
*/
JCR *get_jcr_by_partial_name(char *Job)
{
- JCR *jcr;
+ JCR *jcr;
int len;
if (!Job) {
len = strlen(Job);
for (jcr = jobs; jcr; jcr=jcr->next) {
if (strncmp(Job, jcr->Job, len) == 0) {
- P(jcr->mutex);
- jcr->use_count++;
- V(jcr->mutex);
- Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
- break;
+ P(jcr->mutex);
+ jcr->use_count++;
+ V(jcr->mutex);
+ Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+ break;
}
}
unlock_jcr_chain();
/*
- * Given a Job, find the JCR
+ * Given a Job, find the JCR
* requires an exact match of names.
* Returns: jcr on success
- * NULL on failure
+ * NULL on failure
*/
JCR *get_jcr_by_full_name(char *Job)
{
- JCR *jcr;
+ JCR *jcr;
if (!Job) {
return NULL;
lock_jcr_chain();
for (jcr = jobs; jcr; jcr=jcr->next) {
if (strcmp(jcr->Job, Job) == 0) {
- P(jcr->mutex);
- jcr->use_count++;
- V(jcr->mutex);
- Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
- break;
+ P(jcr->mutex);
+ jcr->use_count++;
+ V(jcr->mutex);
+ Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
+ break;
}
}
unlock_jcr_chain();
}
}
+#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));
+ strerror(errstat));
}
}
/*
* 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));
+ strerror(errstat));
}
}
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;
}
watchdog_t *wd = new_watchdog();
wd->one_shot = false;
- wd->interval = 30; /* FIXME: should be configurable somewhere, even
- if only with a #define */
+ wd->interval = 30; /* FIXME: should be configurable somewhere, even
+ if only with a #define */
wd->callback = jcr_timeout_check;
register_watchdog(wd);
*/
lock_jcr_chain();
foreach_jcr(jcr) {
- free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
+ free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
if (jcr->JobId == 0) {
- continue;
+ continue;
}
fd = jcr->store_bsock;
if (fd) {
- timer_start = fd->timer_start;
- if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
- fd->timer_start = 0; /* turn off timer */
- fd->timed_out = TRUE;
- Jmsg(jcr, M_ERROR, 0, _(
+ timer_start = fd->timer_start;
+ if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
+ fd->timed_out = TRUE;
+ Jmsg(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);
- }
+ watchdog_time - timer_start);
+ pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ }
}
fd = jcr->file_bsock;
if (fd) {
- timer_start = fd->timer_start;
- if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
- fd->timer_start = 0; /* turn off timer */
- fd->timed_out = TRUE;
- Jmsg(jcr, M_ERROR, 0, _(
+ timer_start = fd->timer_start;
+ if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
+ fd->timed_out = TRUE;
+ Jmsg(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);
- }
+ watchdog_time - timer_start);
+ pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ }
}
fd = jcr->dir_bsock;
if (fd) {
- timer_start = fd->timer_start;
- if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
- fd->timer_start = 0; /* turn off timer */
- fd->timed_out = TRUE;
- Jmsg(jcr, M_ERROR, 0, _(
+ timer_start = fd->timer_start;
+ if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
+ fd->timer_start = 0; /* turn off timer */
+ fd->timed_out = TRUE;
+ Jmsg(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);
- }
+ watchdog_time - timer_start);
+ pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+ }
}
}
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 */
+ return; /* thus interrupting the function */
}