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 struct sigaction sigtimer;
100 Dmsg0(200, "Enter new_jcr\n");
101 jcr = (JCR *)malloc(size);
102 memset(jcr, 0, size);
103 jcr->my_thread_id = pthread_self();
104 jcr->sched_time = time(NULL);
105 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
107 pthread_mutex_init(&(jcr->mutex), NULL);
108 jcr->JobStatus = JS_Created; /* ready to run */
109 jcr->VolumeName = get_pool_memory(PM_FNAME);
110 jcr->VolumeName[0] = 0;
111 jcr->errmsg = get_pool_memory(PM_MESSAGE);
113 strcpy(jcr->Job, "*Console*"); /* default */
115 sigtimer.sa_flags = 0;
116 sigtimer.sa_handler = timeout_handler;
117 sigfillset(&sigtimer.sa_mask);
118 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
133 * Remove a JCR from the chain
134 * NOTE! The chain must be locked prior to calling
137 static void remove_jcr(JCR *jcr)
139 Dmsg0(150, "Enter remove_jcr\n");
141 Emsg0(M_ABORT, 0, "NULL jcr.\n");
143 if (!jcr->prev) { /* if no prev */
144 jobs = jcr->next; /* set new head */
146 jcr->prev->next = jcr->next; /* update prev */
149 jcr->next->prev = jcr->prev;
151 Dmsg0(150, "Leave remove_jcr\n");
155 * Free stuff common to all JCRs. N.B. Be careful to include only
156 * generic stuff in the common part of the jcr.
158 static void free_common_jcr(JCR *jcr)
160 /* Keep some statistics */
161 switch (jcr->JobType) {
167 last_job.JobType = jcr->JobType;
168 last_job.JobId = jcr->JobId;
169 last_job.VolSessionId = jcr->VolSessionId;
170 last_job.VolSessionTime = jcr->VolSessionTime;
171 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
172 last_job.JobFiles = jcr->JobFiles;
173 last_job.JobBytes = jcr->JobBytes;
174 last_job.JobStatus = jcr->JobStatus;
175 last_job.JobLevel = jcr->JobLevel;
176 last_job.start_time = jcr->start_time;
177 last_job.end_time = time(NULL);
182 pthread_mutex_destroy(&jcr->mutex);
184 close_msg(jcr); /* close messages for this job */
186 /* do this after closing messages */
187 if (jcr->client_name) {
188 free_pool_memory(jcr->client_name);
189 jcr->client_name = NULL;
192 if (jcr->sd_auth_key) {
193 free(jcr->sd_auth_key);
194 jcr->sd_auth_key = NULL;
196 if (jcr->VolumeName) {
197 free_pool_memory(jcr->VolumeName);
198 jcr->VolumeName = NULL;
201 if (jcr->dir_bsock) {
202 bnet_close(jcr->dir_bsock);
203 jcr->dir_bsock = NULL;
206 free_pool_memory(jcr->errmsg);
213 if (jcr->cached_path) {
214 free_pool_memory(jcr->cached_path);
215 jcr->cached_path = NULL;
218 free_getuser_cache();
219 free_getgroup_cache();
224 * Global routine to free a jcr
227 void b_free_jcr(char *file, int line, JCR *jcr)
229 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
233 void free_jcr(JCR *jcr)
236 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
239 struct s_last_job *je;
242 jcr->use_count--; /* decrement use count */
243 Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
244 if (jcr->use_count > 0) { /* if in use */
246 Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
251 Dmsg1(200, "End job=%d\n", jcr->JobId);
252 if (jcr->daemon_free_jcr) {
253 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
256 free_common_jcr(jcr);
258 /* Keep list of last jobs, but not Console where JobId==0 */
259 if (last_job.JobId > 0) {
260 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
261 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
263 init_last_jobs_list();
265 last_jobs->append(je);
266 if (last_jobs->size() > MAX_LAST_JOBS) {
267 last_jobs->remove(last_jobs->first());
269 last_job.JobId = 0; /* zap last job */
271 close_msg(NULL); /* flush any daemon messages */
273 Dmsg0(200, "Exit free_jcr\n");
278 * Global routine to free a jcr
279 * JCR chain is already locked
281 void free_locked_jcr(JCR *jcr)
283 jcr->use_count--; /* decrement use count */
284 Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
285 if (jcr->use_count > 0) { /* if in use */
289 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
290 free_common_jcr(jcr);
297 * Given a JobId, find the JCR
298 * Returns: jcr on success
301 JCR *get_jcr_by_id(uint32_t JobId)
305 lock_jcr_chain(); /* lock chain */
306 for (jcr = jobs; jcr; jcr=jcr->next) {
307 if (jcr->JobId == JobId) {
311 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
320 * Given a SessionId and SessionTime, find the JCR
321 * Returns: jcr on success
324 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
329 for (jcr = jobs; jcr; jcr=jcr->next) {
330 if (jcr->VolSessionId == SessionId &&
331 jcr->VolSessionTime == SessionTime) {
335 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
345 * Given a Job, find the JCR
346 * compares on the number of characters in Job
347 * thus allowing partial matches.
348 * Returns: jcr on success
351 JCR *get_jcr_by_partial_name(char *Job)
361 for (jcr = jobs; jcr; jcr=jcr->next) {
362 if (strncmp(Job, jcr->Job, len) == 0) {
366 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
376 * Given a Job, find the JCR
377 * requires an exact match of names.
378 * Returns: jcr on success
381 JCR *get_jcr_by_full_name(char *Job)
389 for (jcr = jobs; jcr; jcr=jcr->next) {
390 if (strcmp(jcr->Job, Job) == 0) {
394 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
402 void set_jcr_job_status(JCR *jcr, int JobStatus)
405 * For a set of errors, ... keep the current status
406 * so it isn't lost. For all others, set it.
408 switch (jcr->JobStatus) {
409 case JS_ErrorTerminated:
416 jcr->JobStatus = JobStatus;
423 void lock_jcr_chain()
426 if ((errstat=rwl_writelock(&lock)) != 0) {
427 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
435 void unlock_jcr_chain()
438 if ((errstat=rwl_writeunlock(&lock)) != 0) {
439 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
445 JCR *get_next_jcr(JCR *prev_jcr)
449 if (prev_jcr == NULL) {
452 jcr = prev_jcr->next;
458 Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
463 bool init_jcr_subsystem(void)
465 watchdog_t *wd = new_watchdog();
467 wd->one_shot = false;
468 wd->interval = 30; /* FIXME: should be configurable somewhere, even
469 if only with a #define */
470 wd->callback = jcr_timeout_check;
472 register_watchdog(wd);
477 static void jcr_timeout_check(watchdog_t *self)
483 Dmsg0(400, "Start JCR timeout checks\n");
485 /* Walk through all JCRs checking if any one is
486 * blocked for more than specified max time.
490 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
491 if (jcr->JobId == 0) {
494 fd = jcr->store_bsock;
496 timer_start = fd->timer_start;
497 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
498 fd->timer_start = 0; /* turn off timer */
499 fd->timed_out = TRUE;
500 Jmsg(jcr, M_ERROR, 0, _(
501 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
502 watchdog_time - timer_start);
503 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
506 fd = jcr->file_bsock;
508 timer_start = fd->timer_start;
509 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
510 fd->timer_start = 0; /* turn off timer */
511 fd->timed_out = TRUE;
512 Jmsg(jcr, M_ERROR, 0, _(
513 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
514 watchdog_time - timer_start);
515 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
520 timer_start = fd->timer_start;
521 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
522 fd->timer_start = 0; /* turn off timer */
523 fd->timed_out = TRUE;
524 Jmsg(jcr, M_ERROR, 0, _(
525 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
526 watchdog_time - timer_start);
527 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
534 Dmsg0(200, "Finished JCR timeout checks\n");
538 * Timeout signal comes here
540 void timeout_handler(int sig)
542 return; /* thus interrupting the function */