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);
41 struct s_last_job last_job; /* last job run by this daemon */
42 dlist *last_jobs = NULL;
43 #define MAX_LAST_JOBS 10
45 static 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 memset(&last_job, 0, sizeof(last_job));
57 if ((errstat=rwl_init(&lock)) != 0) {
58 Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"),
64 void term_last_jobs_list()
68 foreach_dlist(je, last_jobs) {
77 void lock_last_jobs_list()
79 /* Use jcr chain mutex */
83 void unlock_last_jobs_list()
85 /* Use jcr chain mutex */
90 * Create a Job Control Record and link it into JCR chain
91 * Returns newly allocated JCR
92 * Note, since each daemon has a different JCR, he passes
95 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
98 MQUEUE_ITEM *item = NULL;
99 struct sigaction sigtimer;
101 Dmsg0(200, "Enter new_jcr\n");
102 jcr = (JCR *)malloc(size);
103 memset(jcr, 0, size);
104 jcr->msg_queue = new dlist(item, &item->link);
105 jcr->my_thread_id = pthread_self();
106 jcr->sched_time = time(NULL);
107 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
109 pthread_mutex_init(&(jcr->mutex), NULL);
110 jcr->JobStatus = JS_Created; /* ready to run */
111 jcr->VolumeName = get_pool_memory(PM_FNAME);
112 jcr->VolumeName[0] = 0;
113 jcr->errmsg = get_pool_memory(PM_MESSAGE);
115 strcpy(jcr->Job, "*Console*"); /* default */
117 sigtimer.sa_flags = 0;
118 sigtimer.sa_handler = timeout_handler;
119 sigfillset(&sigtimer.sa_mask);
120 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
135 * Remove a JCR from the chain
136 * NOTE! The chain must be locked prior to calling
139 static void remove_jcr(JCR *jcr)
141 Dmsg0(150, "Enter remove_jcr\n");
143 Emsg0(M_ABORT, 0, "NULL jcr.\n");
145 if (!jcr->prev) { /* if no prev */
146 jobs = jcr->next; /* set new head */
148 jcr->prev->next = jcr->next; /* update prev */
151 jcr->next->prev = jcr->prev;
153 Dmsg0(150, "Leave remove_jcr\n");
157 * Free stuff common to all JCRs. N.B. Be careful to include only
158 * generic stuff in the common part of the jcr.
160 static void free_common_jcr(JCR *jcr)
162 /* Keep some statistics */
163 switch (jcr->JobType) {
169 last_job.JobType = jcr->JobType;
170 last_job.JobId = jcr->JobId;
171 last_job.VolSessionId = jcr->VolSessionId;
172 last_job.VolSessionTime = jcr->VolSessionTime;
173 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
174 last_job.JobFiles = jcr->JobFiles;
175 last_job.JobBytes = jcr->JobBytes;
176 last_job.JobStatus = jcr->JobStatus;
177 last_job.JobLevel = jcr->JobLevel;
178 last_job.start_time = jcr->start_time;
179 last_job.end_time = time(NULL);
184 pthread_mutex_destroy(&jcr->mutex);
186 close_msg(jcr); /* close messages for this job */
187 delete jcr->msg_queue;
189 /* do this after closing messages */
190 if (jcr->client_name) {
191 free_pool_memory(jcr->client_name);
192 jcr->client_name = NULL;
195 if (jcr->sd_auth_key) {
196 free(jcr->sd_auth_key);
197 jcr->sd_auth_key = NULL;
199 if (jcr->VolumeName) {
200 free_pool_memory(jcr->VolumeName);
201 jcr->VolumeName = NULL;
204 if (jcr->dir_bsock) {
205 bnet_close(jcr->dir_bsock);
206 jcr->dir_bsock = NULL;
209 free_pool_memory(jcr->errmsg);
216 if (jcr->cached_path) {
217 free_pool_memory(jcr->cached_path);
218 jcr->cached_path = NULL;
221 free_getuser_cache();
222 free_getgroup_cache();
227 * Global routine to free a jcr
230 void b_free_jcr(const char *file, int line, JCR *jcr)
232 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
236 void free_jcr(JCR *jcr)
239 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
242 struct s_last_job *je;
245 jcr->use_count--; /* decrement use count */
246 Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
247 if (jcr->use_count > 0) { /* if in use */
249 Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
254 Dmsg1(200, "End job=%d\n", jcr->JobId);
255 if (jcr->daemon_free_jcr) {
256 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
259 free_common_jcr(jcr);
261 /* Keep list of last jobs, but not Console where JobId==0 */
262 if (last_job.JobId > 0) {
263 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
264 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
266 init_last_jobs_list();
268 last_jobs->append(je);
269 if (last_jobs->size() > MAX_LAST_JOBS) {
270 last_jobs->remove(last_jobs->first());
272 last_job.JobId = 0; /* zap last job */
274 close_msg(NULL); /* flush any daemon messages */
276 Dmsg0(200, "Exit free_jcr\n");
281 * Global routine to free a jcr
282 * JCR chain is already locked
284 void free_locked_jcr(JCR *jcr)
286 jcr->use_count--; /* decrement use count */
287 Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
288 if (jcr->use_count > 0) { /* if in use */
292 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
293 free_common_jcr(jcr);
300 * Given a JobId, find the JCR
301 * Returns: jcr on success
304 JCR *get_jcr_by_id(uint32_t JobId)
308 lock_jcr_chain(); /* lock chain */
309 for (jcr = jobs; jcr; jcr=jcr->next) {
310 if (jcr->JobId == JobId) {
314 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
323 * Given a SessionId and SessionTime, find the JCR
324 * Returns: jcr on success
327 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
332 for (jcr = jobs; jcr; jcr=jcr->next) {
333 if (jcr->VolSessionId == SessionId &&
334 jcr->VolSessionTime == SessionTime) {
338 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
348 * Given a Job, find the JCR
349 * compares on the number of characters in Job
350 * thus allowing partial matches.
351 * Returns: jcr on success
354 JCR *get_jcr_by_partial_name(char *Job)
364 for (jcr = jobs; jcr; jcr=jcr->next) {
365 if (strncmp(Job, jcr->Job, len) == 0) {
369 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
379 * Given a Job, find the JCR
380 * requires an exact match of names.
381 * Returns: jcr on success
384 JCR *get_jcr_by_full_name(char *Job)
392 for (jcr = jobs; jcr; jcr=jcr->next) {
393 if (strcmp(jcr->Job, Job) == 0) {
397 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
405 void set_jcr_job_status(JCR *jcr, int JobStatus)
408 * For a set of errors, ... keep the current status
409 * so it isn't lost. For all others, set it.
411 switch (jcr->JobStatus) {
412 case JS_ErrorTerminated:
419 jcr->JobStatus = JobStatus;
426 void lock_jcr_chain()
429 if ((errstat=rwl_writelock(&lock)) != 0) {
430 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
438 void unlock_jcr_chain()
441 if ((errstat=rwl_writeunlock(&lock)) != 0) {
442 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
448 JCR *get_next_jcr(JCR *prev_jcr)
452 if (prev_jcr == NULL) {
455 jcr = prev_jcr->next;
461 Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
466 bool init_jcr_subsystem(void)
468 watchdog_t *wd = new_watchdog();
470 wd->one_shot = false;
471 wd->interval = 30; /* FIXME: should be configurable somewhere, even
472 if only with a #define */
473 wd->callback = jcr_timeout_check;
475 register_watchdog(wd);
480 static void jcr_timeout_check(watchdog_t *self)
486 Dmsg0(400, "Start JCR timeout checks\n");
488 /* Walk through all JCRs checking if any one is
489 * blocked for more than specified max time.
493 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
494 if (jcr->JobId == 0) {
497 fd = jcr->store_bsock;
499 timer_start = fd->timer_start;
500 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
501 fd->timer_start = 0; /* turn off timer */
502 fd->timed_out = TRUE;
503 Jmsg(jcr, M_ERROR, 0, _(
504 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
505 watchdog_time - timer_start);
506 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
509 fd = jcr->file_bsock;
511 timer_start = fd->timer_start;
512 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
513 fd->timer_start = 0; /* turn off timer */
514 fd->timed_out = TRUE;
515 Jmsg(jcr, M_ERROR, 0, _(
516 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
517 watchdog_time - timer_start);
518 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
523 timer_start = fd->timer_start;
524 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
525 fd->timer_start = 0; /* turn off timer */
526 fd->timed_out = TRUE;
527 Jmsg(jcr, M_ERROR, 0, _(
528 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
529 watchdog_time - timer_start);
530 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
537 Dmsg0(200, "Finished JCR timeout checks\n");
541 * Timeout signal comes here
543 static void timeout_handler(int sig)
545 return; /* thus interrupting the function */