2 * Manipulation routines for Job Control Records
4 * Kern E. Sibbald, December 2000
8 * These routines are thread safe.
12 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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 static void timeout_handler(int sig);
39 static void jcr_timeout_check(watchdog_t *self);
42 dlist *last_jobs = NULL;
43 static const int max_last_jobs = 10;
45 static JCR *jobs = NULL; /* pointer to JCR chain */
46 static brwlock_t lock; /* lock for last jobs and JCR chain */
48 void init_last_jobs_list()
51 struct s_last_job *job_entry = NULL;
53 last_jobs = new dlist(job_entry, &job_entry->link);
54 if ((errstat=rwl_init(&lock)) != 0) {
55 Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"),
62 void term_last_jobs_list()
64 struct s_last_job *je;
66 foreach_dlist(je, last_jobs) {
75 void read_last_jobs_list(int fd, uint64_t addr)
77 struct s_last_job *je, job;
80 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
81 if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
84 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
87 Dmsg1(100, "Read num_items=%d\n", num);
88 if (num > 4 * max_last_jobs) { /* sanity check */
92 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
93 Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
97 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
98 memcpy((char *)je, (char *)&job, sizeof(job));
100 init_last_jobs_list();
102 last_jobs->append(je);
103 if (last_jobs->size() > max_last_jobs) {
104 last_jobs->remove(last_jobs->first());
110 uint64_t write_last_jobs_list(int fd, uint64_t addr)
112 struct s_last_job *je;
115 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
116 if (lseek(fd, addr, SEEK_SET) < 0) {
120 /* First record is number of entires */
121 num = last_jobs->size();
122 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
123 Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
126 foreach_dlist(je, last_jobs) {
127 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
128 Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
133 /* Return current address */
134 ssize_t stat = lseek(fd, 0, SEEK_CUR);
142 void lock_last_jobs_list()
144 /* Use jcr chain mutex */
148 void unlock_last_jobs_list()
150 /* Use jcr chain mutex */
155 * Push a subroutine address into the job end callback stack
157 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
159 jcr->job_end_push.prepend((void *)job_end_cb);
162 /* Pop each job_end subroutine and call it */
163 static void job_end_pop(JCR *jcr)
165 void (*job_end_cb)(JCR *jcr);
166 for (int i=0; i<jcr->job_end_push.size(); i++) {
167 job_end_cb = (void (*)(JCR *))jcr->job_end_push.get(i);
173 * Create a Job Control Record and link it into JCR chain
174 * Returns newly allocated JCR
175 * Note, since each daemon has a different JCR, he passes
178 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
181 MQUEUE_ITEM *item = NULL;
182 struct sigaction sigtimer;
184 Dmsg0(400, "Enter new_jcr\n");
185 jcr = (JCR *)malloc(size);
186 memset(jcr, 0, size);
187 jcr->msg_queue = new dlist(item, &item->link);
188 jcr->job_end_push.init(1, false);
189 jcr->my_thread_id = pthread_self();
190 jcr->sched_time = time(NULL);
191 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
193 pthread_mutex_init(&(jcr->mutex), NULL);
194 jcr->JobStatus = JS_Created; /* ready to run */
195 jcr->VolumeName = get_pool_memory(PM_FNAME);
196 jcr->VolumeName[0] = 0;
197 jcr->errmsg = get_pool_memory(PM_MESSAGE);
199 /* Setup some dummy values */
200 jcr->Job[0] = 0; /* no job name by default */
202 jcr->JobType = JT_ADMIN;
203 jcr->JobLevel = L_NONE;
204 jcr->JobStatus = JS_Created;
206 sigtimer.sa_flags = 0;
207 sigtimer.sa_handler = timeout_handler;
208 sigfillset(&sigtimer.sa_mask);
209 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
224 * Remove a JCR from the chain
225 * NOTE! The chain must be locked prior to calling
228 static void remove_jcr(JCR *jcr)
230 Dmsg0(400, "Enter remove_jcr\n");
232 Emsg0(M_ABORT, 0, "NULL jcr.\n");
234 if (!jcr->prev) { /* if no prev */
235 jobs = jcr->next; /* set new head */
237 jcr->prev->next = jcr->next; /* update prev */
240 jcr->next->prev = jcr->prev;
242 Dmsg0(400, "Leave remove_jcr\n");
246 * Free stuff common to all JCRs. N.B. Be careful to include only
247 * generic stuff in the common part of the jcr.
249 static void free_common_jcr(JCR *jcr)
251 struct s_last_job *je, last_job;
253 /* Keep some statistics */
254 switch (jcr->JobType) {
260 last_job.JobType = jcr->JobType;
261 last_job.JobId = jcr->JobId;
262 last_job.VolSessionId = jcr->VolSessionId;
263 last_job.VolSessionTime = jcr->VolSessionTime;
264 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
265 last_job.JobFiles = jcr->JobFiles;
266 last_job.JobBytes = jcr->JobBytes;
267 last_job.JobStatus = jcr->JobStatus;
268 last_job.JobLevel = jcr->JobLevel;
269 last_job.start_time = jcr->start_time;
270 last_job.end_time = time(NULL);
271 /* Keep list of last jobs, but not Console where JobId==0 */
272 if (last_job.JobId > 0) {
273 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
274 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
276 init_last_jobs_list();
278 last_jobs->append(je);
279 if (last_jobs->size() > max_last_jobs) {
280 last_jobs->remove(last_jobs->first());
287 pthread_mutex_destroy(&jcr->mutex);
289 delete jcr->msg_queue;
290 close_msg(jcr); /* close messages for this job */
292 /* do this after closing messages */
293 if (jcr->client_name) {
294 free_pool_memory(jcr->client_name);
295 jcr->client_name = NULL;
298 if (jcr->sd_auth_key) {
299 free(jcr->sd_auth_key);
300 jcr->sd_auth_key = NULL;
302 if (jcr->VolumeName) {
303 free_pool_memory(jcr->VolumeName);
304 jcr->VolumeName = NULL;
307 if (jcr->dir_bsock) {
308 bnet_close(jcr->dir_bsock);
309 jcr->dir_bsock = NULL;
312 free_pool_memory(jcr->errmsg);
319 if (jcr->cached_path) {
320 free_pool_memory(jcr->cached_path);
321 jcr->cached_path = NULL;
324 free_getuser_cache();
325 free_getgroup_cache();
330 * Global routine to free a jcr
333 void b_free_jcr(const char *file, int line, JCR *jcr)
335 Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
339 void free_jcr(JCR *jcr)
342 Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
347 jcr->use_count--; /* decrement use count */
348 if (jcr->use_count < 0) {
349 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
350 jcr->use_count, jcr->JobId);
352 Dmsg3(400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
353 if (jcr->use_count > 0) { /* if in use */
355 Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
358 dequeue_messages(jcr);
360 job_end_pop(jcr); /* pop and call hooked routines */
362 Dmsg1(400, "End job=%d\n", jcr->JobId);
363 if (jcr->daemon_free_jcr) {
364 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
367 free_common_jcr(jcr);
369 close_msg(NULL); /* flush any daemon messages */
371 Dmsg0(400, "Exit free_jcr\n");
376 * Global routine to free a jcr
377 * JCR chain is already locked
379 void free_locked_jcr(JCR *jcr)
381 jcr->use_count--; /* decrement use count */
382 Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
383 if (jcr->use_count > 0) { /* if in use */
387 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
388 free_common_jcr(jcr);
394 * Given a JobId, find the JCR
395 * Returns: jcr on success
398 JCR *get_jcr_by_id(uint32_t JobId)
402 lock_jcr_chain(); /* lock chain */
403 for (jcr = jobs; jcr; jcr=jcr->next) {
404 if (jcr->JobId == JobId) {
408 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
417 * Given a SessionId and SessionTime, find the JCR
418 * Returns: jcr on success
421 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
426 for (jcr = jobs; jcr; jcr=jcr->next) {
427 if (jcr->VolSessionId == SessionId &&
428 jcr->VolSessionTime == SessionTime) {
432 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
442 * Given a Job, find the JCR
443 * compares on the number of characters in Job
444 * thus allowing partial matches.
445 * Returns: jcr on success
448 JCR *get_jcr_by_partial_name(char *Job)
458 for (jcr = jobs; jcr; jcr=jcr->next) {
459 if (strncmp(Job, jcr->Job, len) == 0) {
463 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
473 * Given a Job, find the JCR
474 * requires an exact match of names.
475 * Returns: jcr on success
478 JCR *get_jcr_by_full_name(char *Job)
486 for (jcr = jobs; jcr; jcr=jcr->next) {
487 if (strcmp(jcr->Job, Job) == 0) {
491 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
499 void set_jcr_job_status(JCR *jcr, int JobStatus)
502 * For a set of errors, ... keep the current status
503 * so it isn't lost. For all others, set it.
505 switch (jcr->JobStatus) {
506 case JS_ErrorTerminated:
513 jcr->JobStatus = JobStatus;
520 void lock_jcr_chain()
523 if ((errstat=rwl_writelock(&lock)) != 0) {
524 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
532 void unlock_jcr_chain()
535 if ((errstat=rwl_writeunlock(&lock)) != 0) {
536 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
542 JCR *get_next_jcr(JCR *prev_jcr)
546 if (prev_jcr == NULL) {
549 jcr = prev_jcr->next;
555 Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
560 bool init_jcr_subsystem(void)
562 watchdog_t *wd = new_watchdog();
564 wd->one_shot = false;
565 wd->interval = 30; /* FIXME: should be configurable somewhere, even
566 if only with a #define */
567 wd->callback = jcr_timeout_check;
569 register_watchdog(wd);
574 static void jcr_timeout_check(watchdog_t *self)
580 Dmsg0(400, "Start JCR timeout checks\n");
582 /* Walk through all JCRs checking if any one is
583 * blocked for more than specified max time.
587 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
588 if (jcr->JobId == 0) {
591 fd = jcr->store_bsock;
593 timer_start = fd->timer_start;
594 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
595 fd->timer_start = 0; /* turn off timer */
596 fd->timed_out = TRUE;
597 Jmsg(jcr, M_ERROR, 0, _(
598 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
599 watchdog_time - timer_start);
600 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
603 fd = jcr->file_bsock;
605 timer_start = fd->timer_start;
606 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
607 fd->timer_start = 0; /* turn off timer */
608 fd->timed_out = TRUE;
609 Jmsg(jcr, M_ERROR, 0, _(
610 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
611 watchdog_time - timer_start);
612 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
617 timer_start = fd->timer_start;
618 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
619 fd->timer_start = 0; /* turn off timer */
620 fd->timed_out = TRUE;
621 Jmsg(jcr, M_ERROR, 0, _(
622 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
623 watchdog_time - timer_start);
624 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
631 Dmsg0(400, "Finished JCR timeout checks\n");
635 * Timeout signal comes here
637 static void timeout_handler(int sig)
639 return; /* thus interrupting the function */