2 * Manipulation routines for Job Control Records
4 * Kern E. Sibbald, December 2000
8 * These routines are thread safe.
12 Copyright (C) 2000-2005 Kern Sibbald
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* External variables we reference */
35 extern time_t watchdog_time;
37 /* Forward referenced functions */
38 extern "C" void timeout_handler(int sig);
40 static void jcr_timeout_check(watchdog_t *self);
43 dlist *last_jobs = NULL;
44 const int max_last_jobs = 10;
46 JCR *jobs = NULL; /* pointer to JCR chain */
47 static brwlock_t lock; /* lock for last jobs and JCR chain */
49 void init_last_jobs_list()
52 struct s_last_job *job_entry = NULL;
54 last_jobs = New(dlist(job_entry, &job_entry->link));
55 if ((errstat=rwl_init(&lock)) != 0) {
56 Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"),
63 void term_last_jobs_list()
66 while (!last_jobs->empty()) {
67 void *je = last_jobs->first();
68 last_jobs->remove(je);
77 void read_last_jobs_list(int fd, uint64_t addr)
79 struct s_last_job *je, job;
82 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
83 if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
86 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
89 Dmsg1(100, "Read num_items=%d\n", num);
90 if (num > 4 * max_last_jobs) { /* sanity check */
94 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
95 Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
99 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
100 memcpy((char *)je, (char *)&job, sizeof(job));
102 init_last_jobs_list();
104 last_jobs->append(je);
105 if (last_jobs->size() > max_last_jobs) {
106 je = (struct s_last_job *)last_jobs->first();
107 last_jobs->remove(je);
114 uint64_t write_last_jobs_list(int fd, uint64_t addr)
116 struct s_last_job *je;
119 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
120 if (lseek(fd, (off_t)addr, SEEK_SET) < 0) {
124 /* First record is number of entires */
125 num = last_jobs->size();
126 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
127 Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
130 foreach_dlist(je, last_jobs) {
131 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
132 Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
137 /* Return current address */
138 ssize_t stat = lseek(fd, 0, SEEK_CUR);
146 void lock_last_jobs_list()
148 /* Use jcr chain mutex */
152 void unlock_last_jobs_list()
154 /* Use jcr chain mutex */
159 * Push a subroutine address into the job end callback stack
161 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
163 jcr->job_end_push.append((void *)job_end_cb);
164 jcr->job_end_push.append(ctx);
167 /* Pop each job_end subroutine and call it */
168 static void job_end_pop(JCR *jcr)
170 void (*job_end_cb)(JCR *jcr, void *ctx);
172 for (int i=jcr->job_end_push.size()-1; i > 0; ) {
173 ctx = jcr->job_end_push.get(i--);
174 job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
175 job_end_cb(jcr, ctx);
180 * Create a Job Control Record and link it into JCR chain
181 * Returns newly allocated JCR
182 * Note, since each daemon has a different JCR, he passes
185 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
188 MQUEUE_ITEM *item = NULL;
189 struct sigaction sigtimer;
191 Dmsg0(3400, "Enter new_jcr\n");
192 jcr = (JCR *)malloc(size);
193 memset(jcr, 0, size);
194 jcr->my_thread_id = pthread_self();
195 jcr->msg_queue = New(dlist(item, &item->link));
196 jcr->job_end_push.init(1, false);
197 jcr->sched_time = time(NULL);
198 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
200 pthread_mutex_init(&(jcr->mutex), NULL);
201 jcr->JobStatus = JS_Created; /* ready to run */
202 jcr->VolumeName = get_pool_memory(PM_FNAME);
203 jcr->VolumeName[0] = 0;
204 jcr->errmsg = get_pool_memory(PM_MESSAGE);
206 /* Setup some dummy values */
207 bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
209 jcr->JobType = JT_SYSTEM; /* internal job until defined */
210 jcr->JobLevel = L_NONE;
211 jcr->JobStatus = JS_Created;
213 sigtimer.sa_flags = 0;
214 sigtimer.sa_handler = timeout_handler;
215 sigfillset(&sigtimer.sa_mask);
216 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
231 * Remove a JCR from the chain
232 * NOTE! The chain must be locked prior to calling
235 static void remove_jcr(JCR *jcr)
237 Dmsg0(3400, "Enter remove_jcr\n");
239 Emsg0(M_ABORT, 0, "NULL jcr.\n");
241 if (!jcr->prev) { /* if no prev */
242 jobs = jcr->next; /* set new head */
244 jcr->prev->next = jcr->next; /* update prev */
247 jcr->next->prev = jcr->prev;
249 Dmsg0(3400, "Leave remove_jcr\n");
253 * Free stuff common to all JCRs. N.B. Be careful to include only
254 * generic stuff in the common part of the jcr.
256 static void free_common_jcr(JCR *jcr)
258 struct s_last_job *je, last_job;
260 /* Keep some statistics */
261 switch (jcr->JobType) {
267 last_job.Errors = jcr->Errors;
268 last_job.JobType = jcr->JobType;
269 last_job.JobId = jcr->JobId;
270 last_job.VolSessionId = jcr->VolSessionId;
271 last_job.VolSessionTime = jcr->VolSessionTime;
272 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
273 last_job.JobFiles = jcr->JobFiles;
274 last_job.JobBytes = jcr->JobBytes;
275 last_job.JobStatus = jcr->JobStatus;
276 last_job.JobLevel = jcr->JobLevel;
277 last_job.start_time = jcr->start_time;
278 last_job.end_time = time(NULL);
279 /* Keep list of last jobs, but not Console where JobId==0 */
280 if (last_job.JobId > 0) {
281 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
282 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
284 init_last_jobs_list();
286 last_jobs->append(je);
287 if (last_jobs->size() > max_last_jobs) {
288 je = (struct s_last_job *)last_jobs->first();
289 last_jobs->remove(je);
297 pthread_mutex_destroy(&jcr->mutex);
299 delete jcr->msg_queue;
300 close_msg(jcr); /* close messages for this job */
302 /* do this after closing messages */
303 if (jcr->client_name) {
304 free_pool_memory(jcr->client_name);
305 jcr->client_name = NULL;
309 free_pool_memory(jcr->attr);
313 if (jcr->sd_auth_key) {
314 free(jcr->sd_auth_key);
315 jcr->sd_auth_key = NULL;
317 if (jcr->VolumeName) {
318 free_pool_memory(jcr->VolumeName);
319 jcr->VolumeName = NULL;
322 if (jcr->dir_bsock) {
323 bnet_close(jcr->dir_bsock);
324 jcr->dir_bsock = NULL;
327 free_pool_memory(jcr->errmsg);
334 if (jcr->cached_path) {
335 free_pool_memory(jcr->cached_path);
336 jcr->cached_path = NULL;
339 free_getuser_cache();
340 free_getgroup_cache();
345 * Global routine to free a jcr
348 void b_free_jcr(const char *file, int line, JCR *jcr)
350 Dmsg3(3400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
354 void free_jcr(JCR *jcr)
357 Dmsg1(3400, "Enter free_jcr 0x%x\n", jcr);
361 dequeue_messages(jcr);
363 jcr->use_count--; /* decrement use count */
364 if (jcr->use_count < 0) {
365 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
366 jcr->use_count, jcr->JobId);
368 Dmsg3(3400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
369 if (jcr->use_count > 0) { /* if in use */
371 Dmsg2(3400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
375 remove_jcr(jcr); /* remove Jcr from chain */
378 job_end_pop(jcr); /* pop and call hooked routines */
380 Dmsg1(3400, "End job=%d\n", jcr->JobId);
381 if (jcr->daemon_free_jcr) {
382 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
384 free_common_jcr(jcr);
385 close_msg(NULL); /* flush any daemon messages */
386 Dmsg0(3400, "Exit free_jcr\n");
391 * Global routine to free a jcr
392 * JCR chain is already locked
394 void free_locked_jcr(JCR *jcr)
396 jcr->use_count--; /* decrement use count */
397 Dmsg2(3400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
398 if (jcr->use_count > 0) { /* if in use */
402 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
403 free_common_jcr(jcr);
409 * Given a JobId, find the JCR
410 * Returns: jcr on success
413 JCR *get_jcr_by_id(uint32_t JobId)
417 lock_jcr_chain(); /* lock chain */
418 for (jcr = jobs; jcr; jcr=jcr->next) {
419 if (jcr->JobId == JobId) {
423 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
432 * Given a SessionId and SessionTime, find the JCR
433 * Returns: jcr on success
436 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
441 for (jcr = jobs; jcr; jcr=jcr->next) {
442 if (jcr->VolSessionId == SessionId &&
443 jcr->VolSessionTime == SessionTime) {
447 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
457 * Given a Job, find the JCR
458 * compares on the number of characters in Job
459 * thus allowing partial matches.
460 * Returns: jcr on success
463 JCR *get_jcr_by_partial_name(char *Job)
473 for (jcr = jobs; jcr; jcr=jcr->next) {
474 if (strncmp(Job, jcr->Job, len) == 0) {
478 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
488 * Given a Job, find the JCR
489 * requires an exact match of names.
490 * Returns: jcr on success
493 JCR *get_jcr_by_full_name(char *Job)
501 for (jcr = jobs; jcr; jcr=jcr->next) {
502 if (strcmp(jcr->Job, Job) == 0) {
506 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
514 void set_jcr_job_status(JCR *jcr, int JobStatus)
517 * For a set of errors, ... keep the current status
518 * so it isn't lost. For all others, set it.
520 switch (jcr->JobStatus) {
521 case JS_ErrorTerminated:
528 jcr->JobStatus = JobStatus;
532 #ifdef TRACE_JCR_CHAIN
533 static int lock_count = 0;
539 #ifdef TRACE_JCR_CHAIN
540 void b_lock_jcr_chain(const char *fname, int line)
542 void lock_jcr_chain()
546 #ifdef TRACE_JCR_CHAIN
547 Dmsg3(3400, "Lock jcr chain %d from %s:%d\n", ++lock_count,
550 if ((errstat=rwl_writelock(&lock)) != 0) {
551 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
559 #ifdef TRACE_JCR_CHAIN
560 void b_unlock_jcr_chain(const char *fname, int line)
562 void unlock_jcr_chain()
566 #ifdef TRACE_JCR_CHAIN
567 Dmsg3(3400, "Unlock jcr chain %d from %s:%d\n", lock_count--,
570 if ((errstat=rwl_writeunlock(&lock)) != 0) {
571 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
577 JCR *get_next_jcr(JCR *prev_jcr)
581 if (prev_jcr == NULL) {
584 jcr = prev_jcr->next;
590 Dmsg2(3400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
595 bool init_jcr_subsystem(void)
597 watchdog_t *wd = new_watchdog();
599 wd->one_shot = false;
600 wd->interval = 30; /* FIXME: should be configurable somewhere, even
601 if only with a #define */
602 wd->callback = jcr_timeout_check;
604 register_watchdog(wd);
609 static void jcr_timeout_check(watchdog_t *self)
615 Dmsg0(3400, "Start JCR timeout checks\n");
617 /* Walk through all JCRs checking if any one is
618 * blocked for more than specified max time.
622 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
623 if (jcr->JobId == 0) {
626 fd = jcr->store_bsock;
628 timer_start = fd->timer_start;
629 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
630 fd->timer_start = 0; /* turn off timer */
631 fd->timed_out = TRUE;
632 Jmsg(jcr, M_ERROR, 0, _(
633 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
634 watchdog_time - timer_start);
635 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
638 fd = jcr->file_bsock;
640 timer_start = fd->timer_start;
641 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
642 fd->timer_start = 0; /* turn off timer */
643 fd->timed_out = TRUE;
644 Jmsg(jcr, M_ERROR, 0, _(
645 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
646 watchdog_time - timer_start);
647 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
652 timer_start = fd->timer_start;
653 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
654 fd->timer_start = 0; /* turn off timer */
655 fd->timed_out = TRUE;
656 Jmsg(jcr, M_ERROR, 0, _(
657 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
658 watchdog_time - timer_start);
659 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
666 Dmsg0(3400, "Finished JCR timeout checks\n");
670 * Timeout signal comes here
672 extern "C" void timeout_handler(int sig)
674 return; /* thus interrupting the function */