*
*/
/*
- 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 */
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));
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;
- 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) {
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);
}
}
}
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;
}
}
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
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;
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;
*/
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->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;
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;
}
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 */
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");
}
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;
}
-
/*
* Given a JobId, find the JCR
* Returns: jcr on success
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;
}
}
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;
}
}
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;
}
}
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;
}
}
}
}
+#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));
/*
* 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));
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;
}
}
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 */
}