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 /* Mutex for locking various jcr chains while updating */
48 static pthread_mutex_t jcr_chain_mutex = PTHREAD_MUTEX_INITIALIZER;
50 void init_last_jobs_list()
52 struct s_last_job *job_entry;
54 last_jobs = new dlist(job_entry, &job_entry->link);
55 memset(&last_job, 0, sizeof(last_job));
59 void term_last_jobs_list()
63 foreach_dlist(je, last_jobs) {
71 void lock_last_jobs_list()
73 /* Use jcr chain mutex */
77 void unlock_last_jobs_list()
79 /* Use jcr chain mutex */
84 * Create a Job Control Record and link it into JCR chain
85 * Returns newly allocated JCR
86 * Note, since each daemon has a different JCR, he passes
89 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
92 struct sigaction sigtimer;
94 Dmsg0(200, "Enter new_jcr\n");
95 jcr = (JCR *)malloc(size);
97 jcr->my_thread_id = pthread_self();
98 jcr->sched_time = time(NULL);
99 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
101 pthread_mutex_init(&(jcr->mutex), NULL);
102 jcr->JobStatus = JS_Created; /* ready to run */
103 jcr->VolumeName = get_pool_memory(PM_FNAME);
104 jcr->VolumeName[0] = 0;
105 jcr->errmsg = get_pool_memory(PM_MESSAGE);
107 strcpy(jcr->Job, "*Console*"); /* default */
109 sigtimer.sa_flags = 0;
110 sigtimer.sa_handler = timeout_handler;
111 sigfillset(&sigtimer.sa_mask);
112 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
127 * Remove a JCR from the chain
128 * NOTE! The chain must be locked prior to calling
131 static void remove_jcr(JCR *jcr)
133 Dmsg0(150, "Enter remove_jcr\n");
135 Emsg0(M_ABORT, 0, "NULL jcr.\n");
137 if (!jcr->prev) { /* if no prev */
138 jobs = jcr->next; /* set new head */
140 jcr->prev->next = jcr->next; /* update prev */
143 jcr->next->prev = jcr->prev;
145 Dmsg0(150, "Leave remove_jcr\n");
149 * Free stuff common to all JCRs. N.B. Be careful to include only
150 * generic stuff in the common part of the jcr.
152 static void free_common_jcr(JCR *jcr)
154 /* Keep some statistics */
155 switch (jcr->JobType) {
161 last_job.JobType = jcr->JobType;
162 last_job.JobId = jcr->JobId;
163 last_job.VolSessionId = jcr->VolSessionId;
164 last_job.VolSessionTime = jcr->VolSessionTime;
165 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
166 last_job.JobFiles = jcr->JobFiles;
167 last_job.JobBytes = jcr->JobBytes;
168 last_job.JobStatus = jcr->JobStatus;
169 last_job.JobLevel = jcr->JobLevel;
170 last_job.start_time = jcr->start_time;
171 last_job.end_time = time(NULL);
176 pthread_mutex_destroy(&jcr->mutex);
178 close_msg(jcr); /* close messages for this job */
180 /* do this after closing messages */
181 if (jcr->client_name) {
182 free_pool_memory(jcr->client_name);
183 jcr->client_name = NULL;
186 if (jcr->sd_auth_key) {
187 free(jcr->sd_auth_key);
188 jcr->sd_auth_key = NULL;
190 if (jcr->VolumeName) {
191 free_pool_memory(jcr->VolumeName);
192 jcr->VolumeName = NULL;
195 if (jcr->dir_bsock) {
196 bnet_close(jcr->dir_bsock);
197 jcr->dir_bsock = NULL;
200 free_pool_memory(jcr->errmsg);
207 if (jcr->cached_path) {
208 free_pool_memory(jcr->cached_path);
209 jcr->cached_path = NULL;
212 free_getuser_cache();
213 free_getgroup_cache();
218 * Global routine to free a jcr
221 void b_free_jcr(char *file, int line, JCR *jcr)
223 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
227 void free_jcr(JCR *jcr)
230 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
233 struct s_last_job *je;
236 jcr->use_count--; /* decrement use count */
237 Dmsg3(200, "Dec jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
238 if (jcr->use_count > 0) { /* if in use */
240 Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
245 Dmsg1(200, "End job=%d\n", jcr->JobId);
246 if (jcr->daemon_free_jcr) {
247 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
250 free_common_jcr(jcr);
252 /* Keep list of last jobs, but not Console where JobId==0 */
253 if (last_job.JobId > 0) {
254 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
255 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
257 init_last_jobs_list();
259 last_jobs->append(je);
260 if (last_jobs->size() > MAX_LAST_JOBS) {
261 last_jobs->remove(last_jobs->first());
263 last_job.JobId = 0; /* zap last job */
265 close_msg(NULL); /* flush any daemon messages */
267 Dmsg0(200, "Exit free_jcr\n");
272 * Global routine to free a jcr
273 * JCR chain is already locked
275 void free_locked_jcr(JCR *jcr)
277 jcr->use_count--; /* decrement use count */
278 Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
279 if (jcr->use_count > 0) { /* if in use */
283 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
284 free_common_jcr(jcr);
291 * Given a JobId, find the JCR
292 * Returns: jcr on success
295 JCR *get_jcr_by_id(uint32_t JobId)
299 P(jcr_chain_mutex); /* lock chain */
300 for (jcr = jobs; jcr; jcr=jcr->next) {
301 if (jcr->JobId == JobId) {
305 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
314 * Given a SessionId and SessionTime, find the JCR
315 * Returns: jcr on success
318 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
323 for (jcr = jobs; jcr; jcr=jcr->next) {
324 if (jcr->VolSessionId == SessionId &&
325 jcr->VolSessionTime == SessionTime) {
329 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
339 * Given a Job, find the JCR
340 * compares on the number of characters in Job
341 * thus allowing partial matches.
342 * Returns: jcr on success
345 JCR *get_jcr_by_partial_name(char *Job)
355 for (jcr = jobs; jcr; jcr=jcr->next) {
356 if (strncmp(Job, jcr->Job, len) == 0) {
360 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
370 * Given a Job, find the JCR
371 * requires an exact match of names.
372 * Returns: jcr on success
375 JCR *get_jcr_by_full_name(char *Job)
383 for (jcr = jobs; jcr; jcr=jcr->next) {
384 if (strcmp(jcr->Job, Job) == 0) {
388 Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
396 void set_jcr_job_status(JCR *jcr, int JobStatus)
399 * For a set of errors, ... keep the current status
400 * so it isn't lost. For all others, set it.
402 switch (jcr->JobStatus) {
403 case JS_ErrorTerminated:
410 jcr->JobStatus = JobStatus;
417 void lock_jcr_chain()
425 void unlock_jcr_chain()
431 JCR *get_next_jcr(JCR *jcr)
444 Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);
449 bool init_jcr_subsystem(void)
451 watchdog_t *wd = watchdog_new();
453 wd->one_shot = false;
454 wd->interval = 30; /* FIXME: should be configurable somewhere, even
455 if only with a #define */
456 wd->callback = jcr_timeout_check;
458 register_watchdog(wd);
463 static void jcr_timeout_check(watchdog_t *self)
469 Dmsg0(200, "Start JCR timeout checks\n");
471 /* Walk through all JCRs checking if any one is
472 * blocked for more than specified max time.
475 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
476 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
477 if (jcr->JobId == 0) {
480 fd = jcr->store_bsock;
482 timer_start = fd->timer_start;
483 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
484 fd->timer_start = 0; /* turn off timer */
485 fd->timed_out = TRUE;
486 Jmsg(jcr, M_ERROR, 0, _(
487 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
488 watchdog_time - timer_start);
489 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
492 fd = jcr->file_bsock;
494 timer_start = fd->timer_start;
495 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
496 fd->timer_start = 0; /* turn off timer */
497 fd->timed_out = TRUE;
498 Jmsg(jcr, M_ERROR, 0, _(
499 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
500 watchdog_time - timer_start);
501 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
506 timer_start = fd->timer_start;
507 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
508 fd->timer_start = 0; /* turn off timer */
509 fd->timed_out = TRUE;
510 Jmsg(jcr, M_ERROR, 0, _(
511 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
512 watchdog_time - timer_start);
513 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
520 Dmsg0(200, "Finished JCR timeout checks\n");
524 * Timeout signal comes here
526 void timeout_handler(int sig)
528 return; /* thus interrupting the function */