2 * Manipulation routines for Job Control Records and
3 * handling of last_jobs_list.
5 * Kern E. Sibbald, December 2000
9 * These routines are thread safe.
11 * The job list routines were re-written in May 2005 to
12 * eliminate the global lock while traversing the list, and
13 * to use the dlist subroutines. The locking is now done
14 * on the list each time the list is modified or traversed.
15 * That is it is "micro-locked" rather than globally locked.
16 * The result is that there is one lock/unlock for each entry
17 * in the list while traversing it rather than a single lock
18 * at the beginning of a traversal and one at the end. This
19 * incurs slightly more overhead, but effectively eliminates
20 * the possibilty of race conditions. In addition, with the
21 * exception of the global locking of the list during the
22 * re-reading of the config file, no recursion is needed.
26 Copyright (C) 2000-2006 Kern Sibbald
28 This program is free software; you can redistribute it and/or
29 modify it under the terms of the GNU General Public License
30 version 2 as amended with additional clauses defined in the
31 file LICENSE in the main source directory.
33 This program is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 the file LICENSE for additional details.
43 /* External variables we reference */
44 extern time_t watchdog_time;
46 /* Forward referenced functions */
47 extern "C" void timeout_handler(int sig);
48 static void jcr_timeout_check(watchdog_t *self);
49 #ifdef TRACE_JCR_CHAIN
50 static void b_lock_jcr_chain(const char *filen, int line);
51 static void b_unlock_jcr_chain(const char *filen, int line);
52 #define lock_jcr_chain() b_lock_jcr_chain(__FILE__, __LINE__);
53 #define unlock_jcr_chain() b_unlock_jcr_chain(__FILE__, __LINE__);
55 static void lock_jcr_chain();
56 static void unlock_jcr_chain();
61 dlist *last_jobs = NULL;
62 const int max_last_jobs = 10;
64 static dlist *jcrs = NULL; /* JCR chain */
65 static pthread_mutex_t jcr_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_mutex_t job_start_mutex = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_mutex_t last_jobs_mutex = PTHREAD_MUTEX_INITIALIZER;
82 void init_last_jobs_list()
85 struct s_last_job *job_entry = NULL;
87 last_jobs = New(dlist(job_entry, &job_entry->link));
90 jcrs = New(dlist(jcr, &jcr->link));
94 void term_last_jobs_list()
97 while (!last_jobs->empty()) {
98 void *je = last_jobs->first();
99 last_jobs->remove(je);
111 bool read_last_jobs_list(int fd, uint64_t addr)
113 struct s_last_job *je, job;
116 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
117 if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
120 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
123 Dmsg1(100, "Read num_items=%d\n", num);
124 if (num > 4 * max_last_jobs) { /* sanity check */
127 for ( ; num; num--) {
128 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
129 Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
133 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
134 memcpy((char *)je, (char *)&job, sizeof(job));
136 init_last_jobs_list();
138 last_jobs->append(je);
139 if (last_jobs->size() > max_last_jobs) {
140 je = (struct s_last_job *)last_jobs->first();
141 last_jobs->remove(je);
149 uint64_t write_last_jobs_list(int fd, uint64_t addr)
151 struct s_last_job *je;
154 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
155 if (lseek(fd, (off_t)addr, SEEK_SET) < 0) {
159 /* First record is number of entires */
160 num = last_jobs->size();
161 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
162 Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
165 foreach_dlist(je, last_jobs) {
166 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
167 Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
172 /* Return current address */
173 ssize_t stat = lseek(fd, 0, SEEK_CUR);
181 void lock_last_jobs_list()
186 void unlock_last_jobs_list()
192 * Push a subroutine address into the job end callback stack
194 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
196 jcr->job_end_push.append((void *)job_end_cb);
197 jcr->job_end_push.append(ctx);
200 /* Pop each job_end subroutine and call it */
201 static void job_end_pop(JCR *jcr)
203 void (*job_end_cb)(JCR *jcr, void *ctx);
205 for (int i=jcr->job_end_push.size()-1; i > 0; ) {
206 ctx = jcr->job_end_push.get(i--);
207 job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
208 job_end_cb(jcr, ctx);
213 * Create a Job Control Record and link it into JCR chain
214 * Returns newly allocated JCR
215 * Note, since each daemon has a different JCR, he passes
218 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
221 MQUEUE_ITEM *item = NULL;
222 struct sigaction sigtimer;
224 Dmsg0(3400, "Enter new_jcr\n");
225 jcr = (JCR *)malloc(size);
226 memset(jcr, 0, size);
227 jcr->my_thread_id = pthread_self();
228 jcr->msg_queue = New(dlist(item, &item->link));
229 jcr->job_end_push.init(1, false);
230 jcr->sched_time = time(NULL);
231 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
233 jcr->inc_use_count();
234 jcr->JobStatus = JS_Created; /* ready to run */
235 jcr->VolumeName = get_pool_memory(PM_FNAME);
236 jcr->VolumeName[0] = 0;
237 jcr->errmsg = get_pool_memory(PM_MESSAGE);
239 /* Setup some dummy values */
240 bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
242 jcr->JobType = JT_SYSTEM; /* internal job until defined */
243 jcr->JobLevel = L_NONE;
244 jcr->JobStatus = JS_Created;
246 sigtimer.sa_flags = 0;
247 sigtimer.sa_handler = timeout_handler;
248 sigfillset(&sigtimer.sa_mask);
249 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
252 * Locking jobs is a global lock that is needed
253 * so that the Director can stop new jobs from being
254 * added to the jcr chain while it processes a new
255 * conf file and does the job_end_push().
260 jcrs = New(dlist(jcr, &jcr->link));
271 * Remove a JCR from the chain
272 * NOTE! The chain must be locked prior to calling
275 static void remove_jcr(JCR *jcr)
277 Dmsg0(3400, "Enter remove_jcr\n");
279 Emsg0(M_ABORT, 0, _("NULL jcr.\n"));
282 Dmsg0(3400, "Leave remove_jcr\n");
286 * Free stuff common to all JCRs. N.B. Be careful to include only
287 * generic stuff in the common part of the jcr.
289 static void free_common_jcr(JCR *jcr)
291 struct s_last_job *je, last_job;
293 /* Keep some statistics */
294 switch (jcr->JobType) {
302 last_job.Errors = jcr->Errors;
303 last_job.JobType = jcr->JobType;
304 last_job.JobId = jcr->JobId;
305 last_job.VolSessionId = jcr->VolSessionId;
306 last_job.VolSessionTime = jcr->VolSessionTime;
307 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
308 last_job.JobFiles = jcr->JobFiles;
309 last_job.JobBytes = jcr->JobBytes;
310 last_job.JobStatus = jcr->JobStatus;
311 last_job.JobLevel = jcr->JobLevel;
312 last_job.start_time = jcr->start_time;
313 last_job.end_time = time(NULL);
314 /* Keep list of last jobs, but not Console where JobId==0 */
315 if (last_job.JobId > 0) {
316 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
317 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
319 init_last_jobs_list();
321 last_jobs->append(je);
322 if (last_jobs->size() > max_last_jobs) {
323 je = (struct s_last_job *)last_jobs->first();
324 last_jobs->remove(je);
332 jcr->destroy_mutex();
334 delete jcr->msg_queue;
335 close_msg(jcr); /* close messages for this job */
337 /* do this after closing messages */
338 if (jcr->client_name) {
339 free_pool_memory(jcr->client_name);
340 jcr->client_name = NULL;
344 free_pool_memory(jcr->attr);
348 if (jcr->sd_auth_key) {
349 free(jcr->sd_auth_key);
350 jcr->sd_auth_key = NULL;
352 if (jcr->VolumeName) {
353 free_pool_memory(jcr->VolumeName);
354 jcr->VolumeName = NULL;
357 if (jcr->dir_bsock) {
358 bnet_close(jcr->dir_bsock);
359 jcr->dir_bsock = NULL;
362 free_pool_memory(jcr->errmsg);
369 if (jcr->cached_path) {
370 free_pool_memory(jcr->cached_path);
371 jcr->cached_path = NULL;
374 free_getuser_cache();
375 free_getgroup_cache();
380 * Global routine to free a jcr
383 void b_free_jcr(const char *file, int line, JCR *jcr)
385 Dmsg3(3400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
389 void free_jcr(JCR *jcr)
392 Dmsg2(3400, "Enter free_jcr 0x%x job=%d\n", jcr, jcr->JobId);
396 dequeue_messages(jcr);
398 jcr->dec_use_count(); /* decrement use count */
399 if (jcr->use_count() < 0) {
400 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
401 jcr->use_count(), jcr->JobId);
403 Dmsg3(3400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count(), jcr->JobId);
404 if (jcr->use_count() > 0) { /* if in use */
406 Dmsg3(3400, "free_jcr 0x%x job=%d use_count=%d\n", jcr, jcr->JobId, jcr->use_count());
410 remove_jcr(jcr); /* remove Jcr from chain */
413 job_end_pop(jcr); /* pop and call hooked routines */
415 Dmsg1(3400, "End job=%d\n", jcr->JobId);
416 if (jcr->daemon_free_jcr) {
417 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
419 free_common_jcr(jcr);
420 close_msg(NULL); /* flush any daemon messages */
421 garbage_collect_memory_pool();
422 Dmsg0(3400, "Exit free_jcr\n");
427 * Given a JobId, find the JCR
428 * Returns: jcr on success
431 JCR *get_jcr_by_id(uint32_t JobId)
436 if (jcr->JobId == JobId) {
437 jcr->inc_use_count();
438 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
447 * Given a SessionId and SessionTime, find the JCR
448 * Returns: jcr on success
451 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
456 if (jcr->VolSessionId == SessionId &&
457 jcr->VolSessionTime == SessionTime) {
458 jcr->inc_use_count();
459 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
469 * Given a Job, find the JCR
470 * compares on the number of characters in Job
471 * thus allowing partial matches.
472 * Returns: jcr on success
475 JCR *get_jcr_by_partial_name(char *Job)
485 if (strncmp(Job, jcr->Job, len) == 0) {
486 jcr->inc_use_count();
487 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
497 * Given a Job, find the JCR
498 * requires an exact match of names.
499 * Returns: jcr on success
502 JCR *get_jcr_by_full_name(char *Job)
510 if (strcmp(jcr->Job, Job) == 0) {
511 jcr->inc_use_count();
512 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
520 void set_jcr_job_status(JCR *jcr, int JobStatus)
523 * For a set of errors, ... keep the current status
524 * so it isn't lost. For all others, set it.
526 switch (jcr->JobStatus) {
527 case JS_ErrorTerminated:
534 jcr->JobStatus = JobStatus;
538 #ifdef TRACE_JCR_CHAIN
539 static int lock_count = 0;
545 #ifdef TRACE_JCR_CHAIN
546 static void b_lock_jcr_chain(const char *fname, int line)
548 static void lock_jcr_chain()
551 #ifdef TRACE_JCR_CHAIN
552 Dmsg3(3400, "Lock jcr chain %d from %s:%d\n", ++lock_count,
561 #ifdef TRACE_JCR_CHAIN
562 static void b_unlock_jcr_chain(const char *fname, int line)
564 static void unlock_jcr_chain()
567 #ifdef TRACE_JCR_CHAIN
568 Dmsg3(3400, "Unlock jcr chain %d from %s:%d\n", lock_count--,
576 * Start walk of jcr chain
577 * The proper way to walk the jcr chain is:
584 * It is possible to leave out the endeach_jcr(jcr), but
585 * in that case, the last jcr referenced must be explicitly
591 JCR *jcr_walk_start()
595 jcr = (JCR *)jcrs->first();
597 jcr->inc_use_count();
598 Dmsg3(3400, "Inc jcr_walk_start 0x%x job=%d use_count=%d\n", jcr,
599 jcr->JobId, jcr->use_count());
606 * Get next jcr from chain, and release current one
608 JCR *jcr_walk_next(JCR *prev_jcr)
613 jcr = (JCR *)jcrs->next(prev_jcr);
615 jcr->inc_use_count();
616 Dmsg3(3400, "Inc jcr_walk_next 0x%x job=%d use_count=%d\n", jcr,
617 jcr->JobId, jcr->use_count());
627 * Release last jcr referenced
629 void jcr_walk_end(JCR *jcr)
638 * Setup to call the timeout check routine every 30 seconds
639 * This routine will check any timers that have been enabled.
641 bool init_jcr_subsystem(void)
643 watchdog_t *wd = new_watchdog();
645 wd->one_shot = false;
646 wd->interval = 30; /* FIXME: should be configurable somewhere, even
647 if only with a #define */
648 wd->callback = jcr_timeout_check;
650 register_watchdog(wd);
655 static void jcr_timeout_check(watchdog_t *self)
661 Dmsg0(3400, "Start JCR timeout checks\n");
663 /* Walk through all JCRs checking if any one is
664 * blocked for more than specified max time.
667 Dmsg2(3400, "jcr_timeout_check JobId=%u jcr=0x%x\n", jcr->JobId, jcr);
668 if (jcr->JobId == 0) {
671 fd = jcr->store_bsock;
673 timer_start = fd->timer_start;
674 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
675 fd->timer_start = 0; /* turn off timer */
676 fd->timed_out = true;
677 Jmsg(jcr, M_ERROR, 0, _(
678 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
679 watchdog_time - timer_start);
680 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
683 fd = jcr->file_bsock;
685 timer_start = fd->timer_start;
686 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
687 fd->timer_start = 0; /* turn off timer */
688 fd->timed_out = true;
689 Jmsg(jcr, M_ERROR, 0, _(
690 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
691 watchdog_time - timer_start);
692 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
697 timer_start = fd->timer_start;
698 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
699 fd->timer_start = 0; /* turn off timer */
700 fd->timed_out = true;
701 Jmsg(jcr, M_ERROR, 0, _(
702 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
703 watchdog_time - timer_start);
704 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
710 Dmsg0(3400, "Finished JCR timeout checks\n");
714 * Timeout signal comes here
716 extern "C" void timeout_handler(int sig)
718 return; /* thus interrupting the function */