2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Manipulation routines for Job Control Records and
30 * handling of last_jobs_list.
32 * Kern E. Sibbald, December 2000
36 * These routines are thread safe.
38 * The job list routines were re-written in May 2005 to
39 * eliminate the global lock while traversing the list, and
40 * to use the dlist subroutines. The locking is now done
41 * on the list each time the list is modified or traversed.
42 * That is it is "micro-locked" rather than globally locked.
43 * The result is that there is one lock/unlock for each entry
44 * in the list while traversing it rather than a single lock
45 * at the beginning of a traversal and one at the end. This
46 * incurs slightly more overhead, but effectively eliminates
47 * the possibilty of race conditions. In addition, with the
48 * exception of the global locking of the list during the
49 * re-reading of the config file, no recursion is needed.
56 /* External variables we reference */
57 extern time_t watchdog_time;
59 /* External referenced functions */
60 void free_bregexps(alist *bregexps);
62 /* Forward referenced functions */
63 extern "C" void timeout_handler(int sig);
64 static void jcr_timeout_check(watchdog_t *self);
65 #ifdef TRACE_JCR_CHAIN
66 static void b_lock_jcr_chain(const char *filen, int line);
67 static void b_unlock_jcr_chain(const char *filen, int line);
68 #define lock_jcr_chain() b_lock_jcr_chain(__FILE__, __LINE__);
69 #define unlock_jcr_chain() b_unlock_jcr_chain(__FILE__, __LINE__);
71 static void lock_jcr_chain();
72 static void unlock_jcr_chain();
77 dlist *last_jobs = NULL;
78 const int max_last_jobs = 10;
80 static dlist *jcrs = NULL; /* JCR chain */
81 static pthread_mutex_t jcr_lock = PTHREAD_MUTEX_INITIALIZER;
83 static pthread_mutex_t job_start_mutex = PTHREAD_MUTEX_INITIALIZER;
85 static pthread_mutex_t last_jobs_mutex = PTHREAD_MUTEX_INITIALIZER;
98 void init_last_jobs_list()
101 struct s_last_job *job_entry = NULL;
103 last_jobs = New(dlist(job_entry, &job_entry->link));
106 jcrs = New(dlist(jcr, &jcr->link));
110 void term_last_jobs_list()
113 while (!last_jobs->empty()) {
114 void *je = last_jobs->first();
115 last_jobs->remove(je);
127 bool read_last_jobs_list(int fd, uint64_t addr)
129 struct s_last_job *je, job;
132 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
133 if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
136 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
139 Dmsg1(100, "Read num_items=%d\n", num);
140 if (num > 4 * max_last_jobs) { /* sanity check */
143 for ( ; num; num--) {
144 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
146 Pmsg1(000, "Read job entry. ERR=%s\n", be.bstrerror());
150 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
151 memcpy((char *)je, (char *)&job, sizeof(job));
153 init_last_jobs_list();
155 last_jobs->append(je);
156 if (last_jobs->size() > max_last_jobs) {
157 je = (struct s_last_job *)last_jobs->first();
158 last_jobs->remove(je);
166 uint64_t write_last_jobs_list(int fd, uint64_t addr)
168 struct s_last_job *je;
171 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
172 if (lseek(fd, (off_t)addr, SEEK_SET) < 0) {
176 /* First record is number of entires */
177 num = last_jobs->size();
178 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
180 Pmsg1(000, "Error writing num_items: ERR=%s\n", be.bstrerror());
183 foreach_dlist(je, last_jobs) {
184 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
186 Pmsg1(000, "Error writing job: ERR=%s\n", be.bstrerror());
191 /* Return current address */
192 ssize_t stat = lseek(fd, 0, SEEK_CUR);
200 void lock_last_jobs_list()
205 void unlock_last_jobs_list()
211 * Push a subroutine address into the job end callback stack
213 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
215 jcr->job_end_push.append((void *)job_end_cb);
216 jcr->job_end_push.append(ctx);
219 /* Pop each job_end subroutine and call it */
220 static void job_end_pop(JCR *jcr)
222 void (*job_end_cb)(JCR *jcr, void *ctx);
224 for (int i=jcr->job_end_push.size()-1; i > 0; ) {
225 ctx = jcr->job_end_push.get(i--);
226 job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
227 job_end_cb(jcr, ctx);
232 * Create a Job Control Record and link it into JCR chain
233 * Returns newly allocated JCR
234 * Note, since each daemon has a different JCR, he passes
237 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
240 MQUEUE_ITEM *item = NULL;
241 struct sigaction sigtimer;
243 Dmsg0(3400, "Enter new_jcr\n");
244 jcr = (JCR *)malloc(size);
245 memset(jcr, 0, size);
246 jcr->my_thread_id = pthread_self();
247 jcr->msg_queue = New(dlist(item, &item->link));
248 jcr->job_end_push.init(1, false);
249 jcr->sched_time = time(NULL);
250 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
252 jcr->inc_use_count();
253 jcr->VolumeName = get_pool_memory(PM_FNAME);
254 jcr->VolumeName[0] = 0;
255 jcr->errmsg = get_pool_memory(PM_MESSAGE);
257 /* Setup some dummy values */
258 bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
260 jcr->JobType = JT_SYSTEM; /* internal job until defined */
261 jcr->JobLevel = L_NONE;
262 set_jcr_job_status(jcr, JS_Created); /* ready to run */
264 sigtimer.sa_flags = 0;
265 sigtimer.sa_handler = timeout_handler;
266 sigfillset(&sigtimer.sa_mask);
267 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
270 * Locking jobs is a global lock that is needed
271 * so that the Director can stop new jobs from being
272 * added to the jcr chain while it processes a new
273 * conf file and does the job_end_push().
278 jcrs = New(dlist(jcr, &jcr->link));
289 * Remove a JCR from the chain
290 * NOTE! The chain must be locked prior to calling
293 static void remove_jcr(JCR *jcr)
295 Dmsg0(3400, "Enter remove_jcr\n");
297 Emsg0(M_ABORT, 0, _("NULL jcr.\n"));
300 Dmsg0(3400, "Leave remove_jcr\n");
304 * Free stuff common to all JCRs. N.B. Be careful to include only
305 * generic stuff in the common part of the jcr.
307 static void free_common_jcr(JCR *jcr)
309 struct s_last_job *je, last_job;
311 /* Keep some statistics */
312 switch (jcr->JobType) {
320 last_job.Errors = jcr->Errors;
321 last_job.JobType = jcr->JobType;
322 last_job.JobId = jcr->JobId;
323 last_job.VolSessionId = jcr->VolSessionId;
324 last_job.VolSessionTime = jcr->VolSessionTime;
325 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
326 last_job.JobFiles = jcr->JobFiles;
327 last_job.JobBytes = jcr->JobBytes;
328 last_job.JobStatus = jcr->JobStatus;
329 last_job.JobLevel = jcr->JobLevel;
330 last_job.start_time = jcr->start_time;
331 last_job.end_time = time(NULL);
332 /* Keep list of last jobs, but not Console where JobId==0 */
333 if (last_job.JobId > 0) {
334 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
335 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
337 init_last_jobs_list();
339 last_jobs->append(je);
340 if (last_jobs->size() > max_last_jobs) {
341 je = (struct s_last_job *)last_jobs->first();
342 last_jobs->remove(je);
350 jcr->destroy_mutex();
352 if (jcr->msg_queue) {
353 delete jcr->msg_queue;
354 jcr->msg_queue = NULL;
356 close_msg(jcr); /* close messages for this job */
358 /* do this after closing messages */
359 if (jcr->client_name) {
360 free_pool_memory(jcr->client_name);
361 jcr->client_name = NULL;
365 free_pool_memory(jcr->attr);
369 if (jcr->sd_auth_key) {
370 free(jcr->sd_auth_key);
371 jcr->sd_auth_key = NULL;
373 if (jcr->VolumeName) {
374 free_pool_memory(jcr->VolumeName);
375 jcr->VolumeName = NULL;
378 if (jcr->dir_bsock) {
379 bnet_close(jcr->dir_bsock);
380 jcr->dir_bsock = NULL;
383 free_pool_memory(jcr->errmsg);
390 if (jcr->RegexWhere) {
391 free(jcr->RegexWhere);
392 jcr->RegexWhere = NULL;
394 if (jcr->where_bregexp) {
395 free_bregexps(jcr->where_bregexp);
396 delete jcr->where_bregexp;
397 jcr->where_bregexp = NULL;
399 if (jcr->cached_path) {
400 free_pool_memory(jcr->cached_path);
401 jcr->cached_path = NULL;
404 free_getuser_cache();
405 free_getgroup_cache();
410 * Global routine to free a jcr
413 void b_free_jcr(const char *file, int line, JCR *jcr)
415 Dmsg3(3400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
419 void free_jcr(JCR *jcr)
422 Dmsg2(3400, "Enter free_jcr 0x%x job=%d\n", jcr, jcr->JobId);
426 dequeue_messages(jcr);
428 jcr->dec_use_count(); /* decrement use count */
429 if (jcr->use_count() < 0) {
430 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
431 jcr->use_count(), jcr->JobId);
433 Dmsg3(3400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count(), jcr->JobId);
434 if (jcr->use_count() > 0) { /* if in use */
436 Dmsg3(3400, "free_jcr 0x%x job=%d use_count=%d\n", jcr, jcr->JobId, jcr->use_count());
440 remove_jcr(jcr); /* remove Jcr from chain */
443 job_end_pop(jcr); /* pop and call hooked routines */
445 Dmsg1(3400, "End job=%d\n", jcr->JobId);
446 if (jcr->daemon_free_jcr) {
447 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
449 free_common_jcr(jcr);
450 close_msg(NULL); /* flush any daemon messages */
451 garbage_collect_memory_pool();
452 Dmsg0(3400, "Exit free_jcr\n");
457 * Given a JobId, find the JCR
458 * Returns: jcr on success
461 JCR *get_jcr_by_id(uint32_t JobId)
466 if (jcr->JobId == JobId) {
467 jcr->inc_use_count();
468 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
477 * Given a SessionId and SessionTime, find the JCR
478 * Returns: jcr on success
481 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
486 if (jcr->VolSessionId == SessionId &&
487 jcr->VolSessionTime == SessionTime) {
488 jcr->inc_use_count();
489 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
499 * Given a Job, find the JCR
500 * compares on the number of characters in Job
501 * thus allowing partial matches.
502 * Returns: jcr on success
505 JCR *get_jcr_by_partial_name(char *Job)
515 if (strncmp(Job, jcr->Job, len) == 0) {
516 jcr->inc_use_count();
517 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
527 * Given a Job, find the JCR
528 * requires an exact match of names.
529 * Returns: jcr on success
532 JCR *get_jcr_by_full_name(char *Job)
540 if (strcmp(jcr->Job, Job) == 0) {
541 jcr->inc_use_count();
542 Dmsg2(3400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count());
550 void set_jcr_job_status(JCR *jcr, int JobStatus)
553 * For a set of errors, ... keep the current status
554 * so it isn't lost. For all others, set it.
556 switch (jcr->JobStatus) {
557 case JS_ErrorTerminated:
564 jcr->JobStatus = JobStatus;
568 #ifdef TRACE_JCR_CHAIN
569 static int lock_count = 0;
575 #ifdef TRACE_JCR_CHAIN
576 static void b_lock_jcr_chain(const char *fname, int line)
578 static void lock_jcr_chain()
581 #ifdef TRACE_JCR_CHAIN
582 Dmsg3(3400, "Lock jcr chain %d from %s:%d\n", ++lock_count, fname, line);
590 #ifdef TRACE_JCR_CHAIN
591 static void b_unlock_jcr_chain(const char *fname, int line)
593 static void unlock_jcr_chain()
596 #ifdef TRACE_JCR_CHAIN
597 Dmsg3(3400, "Unlock jcr chain %d from %s:%d\n", lock_count--, fname, line);
604 * Start walk of jcr chain
605 * The proper way to walk the jcr chain is:
612 * It is possible to leave out the endeach_jcr(jcr), but
613 * in that case, the last jcr referenced must be explicitly
619 JCR *jcr_walk_start()
623 jcr = (JCR *)jcrs->first();
625 jcr->inc_use_count();
626 Dmsg3(3400, "Inc jcr_walk_start 0x%x job=%d use_count=%d\n", jcr, jcr->JobId, jcr->use_count());
633 * Get next jcr from chain, and release current one
635 JCR *jcr_walk_next(JCR *prev_jcr)
640 jcr = (JCR *)jcrs->next(prev_jcr);
642 jcr->inc_use_count();
643 Dmsg3(3400, "Inc jcr_walk_next 0x%x job=%d use_count=%d\n", jcr, jcr->JobId, jcr->use_count());
653 * Release last jcr referenced
655 void jcr_walk_end(JCR *jcr)
664 * Setup to call the timeout check routine every 30 seconds
665 * This routine will check any timers that have been enabled.
667 bool init_jcr_subsystem(void)
669 watchdog_t *wd = new_watchdog();
671 wd->one_shot = false;
672 wd->interval = 30; /* FIXME: should be configurable somewhere, even
673 if only with a #define */
674 wd->callback = jcr_timeout_check;
676 register_watchdog(wd);
681 static void jcr_timeout_check(watchdog_t *self)
687 Dmsg0(3400, "Start JCR timeout checks\n");
689 /* Walk through all JCRs checking if any one is
690 * blocked for more than specified max time.
693 Dmsg2(3400, "jcr_timeout_check JobId=%u jcr=0x%x\n", jcr->JobId, jcr);
694 if (jcr->JobId == 0) {
697 fd = jcr->store_bsock;
699 timer_start = fd->timer_start;
700 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
701 fd->timer_start = 0; /* turn off timer */
702 fd->m_timed_out = true;
703 Jmsg(jcr, M_ERROR, 0, _(
704 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
705 watchdog_time - timer_start);
706 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
709 fd = jcr->file_bsock;
711 timer_start = fd->timer_start;
712 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
713 fd->timer_start = 0; /* turn off timer */
714 fd->m_timed_out = true;
715 Jmsg(jcr, M_ERROR, 0, _(
716 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
717 watchdog_time - timer_start);
718 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
723 timer_start = fd->timer_start;
724 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
725 fd->timer_start = 0; /* turn off timer */
726 fd->m_timed_out = true;
727 Jmsg(jcr, M_ERROR, 0, _(
728 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
729 watchdog_time - timer_start);
730 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
736 Dmsg0(3400, "Finished JCR timeout checks\n");
740 * Timeout signal comes here
742 extern "C" void timeout_handler(int sig)
744 return; /* thus interrupting the function */