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 #define MAX_LAST_JOBS 15
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;
79 if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
83 if (read(fd, &job, sizeof(job)) < 0) {
87 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
88 memcpy((char *)je, (char *)&job, sizeof(job));
90 init_last_jobs_list();
92 last_jobs->append(je);
93 if (last_jobs->size() > MAX_LAST_JOBS) {
94 last_jobs->remove(last_jobs->first());
96 job.JobId = 0; /* zap last job */
103 uint64_t write_last_jobs_list(int fd, uint64_t addr)
105 struct s_last_job *je, job;
106 if (lseek(fd, addr, SEEK_SET) < 0) {
110 foreach_dlist(je, last_jobs) {
111 if (write(fd, je, sizeof(struct s_last_job)) < 0) {
116 /* Write a zero record to terminate list */
117 memset(&job, 0, sizeof(job));
118 write(fd, &job, sizeof(job));
119 ssize_t stat = lseek(fd, 0, SEEK_CUR);
127 void lock_last_jobs_list()
129 /* Use jcr chain mutex */
133 void unlock_last_jobs_list()
135 /* Use jcr chain mutex */
140 * Create a Job Control Record and link it into JCR chain
141 * Returns newly allocated JCR
142 * Note, since each daemon has a different JCR, he passes
145 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
148 MQUEUE_ITEM *item = NULL;
149 struct sigaction sigtimer;
151 Dmsg0(200, "Enter new_jcr\n");
152 jcr = (JCR *)malloc(size);
153 memset(jcr, 0, size);
154 jcr->msg_queue = new dlist(item, &item->link);
155 jcr->my_thread_id = pthread_self();
156 jcr->sched_time = time(NULL);
157 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
159 pthread_mutex_init(&(jcr->mutex), NULL);
160 jcr->JobStatus = JS_Created; /* ready to run */
161 jcr->VolumeName = get_pool_memory(PM_FNAME);
162 jcr->VolumeName[0] = 0;
163 jcr->errmsg = get_pool_memory(PM_MESSAGE);
165 strcpy(jcr->Job, "*Console*"); /* default */
167 sigtimer.sa_flags = 0;
168 sigtimer.sa_handler = timeout_handler;
169 sigfillset(&sigtimer.sa_mask);
170 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
185 * Remove a JCR from the chain
186 * NOTE! The chain must be locked prior to calling
189 static void remove_jcr(JCR *jcr)
191 Dmsg0(150, "Enter remove_jcr\n");
193 Emsg0(M_ABORT, 0, "NULL jcr.\n");
195 if (!jcr->prev) { /* if no prev */
196 jobs = jcr->next; /* set new head */
198 jcr->prev->next = jcr->next; /* update prev */
201 jcr->next->prev = jcr->prev;
203 Dmsg0(150, "Leave remove_jcr\n");
207 * Free stuff common to all JCRs. N.B. Be careful to include only
208 * generic stuff in the common part of the jcr.
210 static void free_common_jcr(JCR *jcr)
212 struct s_last_job *je, last_job;
214 /* Keep some statistics */
215 switch (jcr->JobType) {
221 last_job.JobType = jcr->JobType;
222 last_job.JobId = jcr->JobId;
223 last_job.VolSessionId = jcr->VolSessionId;
224 last_job.VolSessionTime = jcr->VolSessionTime;
225 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
226 last_job.JobFiles = jcr->JobFiles;
227 last_job.JobBytes = jcr->JobBytes;
228 last_job.JobStatus = jcr->JobStatus;
229 last_job.JobLevel = jcr->JobLevel;
230 last_job.start_time = jcr->start_time;
231 last_job.end_time = time(NULL);
232 /* Keep list of last jobs, but not Console where JobId==0 */
233 if (last_job.JobId > 0) {
234 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
235 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
237 init_last_jobs_list();
239 last_jobs->append(je);
240 if (last_jobs->size() > MAX_LAST_JOBS) {
241 last_jobs->remove(last_jobs->first());
248 pthread_mutex_destroy(&jcr->mutex);
250 close_msg(jcr); /* close messages for this job */
251 delete jcr->msg_queue;
253 /* do this after closing messages */
254 if (jcr->client_name) {
255 free_pool_memory(jcr->client_name);
256 jcr->client_name = NULL;
259 if (jcr->sd_auth_key) {
260 free(jcr->sd_auth_key);
261 jcr->sd_auth_key = NULL;
263 if (jcr->VolumeName) {
264 free_pool_memory(jcr->VolumeName);
265 jcr->VolumeName = NULL;
268 if (jcr->dir_bsock) {
269 bnet_close(jcr->dir_bsock);
270 jcr->dir_bsock = NULL;
273 free_pool_memory(jcr->errmsg);
280 if (jcr->cached_path) {
281 free_pool_memory(jcr->cached_path);
282 jcr->cached_path = NULL;
285 free_getuser_cache();
286 free_getgroup_cache();
291 * Global routine to free a jcr
294 void b_free_jcr(const char *file, int line, JCR *jcr)
296 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
300 void free_jcr(JCR *jcr)
303 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
308 jcr->use_count--; /* decrement use count */
309 if (jcr->use_count < 0) {
310 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
311 jcr->use_count, jcr->JobId);
313 Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
314 if (jcr->use_count > 0) { /* if in use */
316 Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
321 Dmsg1(200, "End job=%d\n", jcr->JobId);
322 if (jcr->daemon_free_jcr) {
323 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
326 free_common_jcr(jcr);
328 close_msg(NULL); /* flush any daemon messages */
330 Dmsg0(200, "Exit free_jcr\n");
335 * Global routine to free a jcr
336 * JCR chain is already locked
338 void free_locked_jcr(JCR *jcr)
340 jcr->use_count--; /* decrement use count */
341 Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
342 if (jcr->use_count > 0) { /* if in use */
346 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
347 free_common_jcr(jcr);
354 * Given a JobId, find the JCR
355 * Returns: jcr on success
358 JCR *get_jcr_by_id(uint32_t JobId)
362 lock_jcr_chain(); /* lock chain */
363 for (jcr = jobs; jcr; jcr=jcr->next) {
364 if (jcr->JobId == JobId) {
368 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
377 * Given a SessionId and SessionTime, find the JCR
378 * Returns: jcr on success
381 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
386 for (jcr = jobs; jcr; jcr=jcr->next) {
387 if (jcr->VolSessionId == SessionId &&
388 jcr->VolSessionTime == SessionTime) {
392 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
402 * Given a Job, find the JCR
403 * compares on the number of characters in Job
404 * thus allowing partial matches.
405 * Returns: jcr on success
408 JCR *get_jcr_by_partial_name(char *Job)
418 for (jcr = jobs; jcr; jcr=jcr->next) {
419 if (strncmp(Job, jcr->Job, len) == 0) {
423 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
433 * Given a Job, find the JCR
434 * requires an exact match of names.
435 * Returns: jcr on success
438 JCR *get_jcr_by_full_name(char *Job)
446 for (jcr = jobs; jcr; jcr=jcr->next) {
447 if (strcmp(jcr->Job, Job) == 0) {
451 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
459 void set_jcr_job_status(JCR *jcr, int JobStatus)
462 * For a set of errors, ... keep the current status
463 * so it isn't lost. For all others, set it.
465 switch (jcr->JobStatus) {
466 case JS_ErrorTerminated:
473 jcr->JobStatus = JobStatus;
480 void lock_jcr_chain()
483 if ((errstat=rwl_writelock(&lock)) != 0) {
484 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
492 void unlock_jcr_chain()
495 if ((errstat=rwl_writeunlock(&lock)) != 0) {
496 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
502 JCR *get_next_jcr(JCR *prev_jcr)
506 if (prev_jcr == NULL) {
509 jcr = prev_jcr->next;
515 Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
520 bool init_jcr_subsystem(void)
522 watchdog_t *wd = new_watchdog();
524 wd->one_shot = false;
525 wd->interval = 30; /* FIXME: should be configurable somewhere, even
526 if only with a #define */
527 wd->callback = jcr_timeout_check;
529 register_watchdog(wd);
534 static void jcr_timeout_check(watchdog_t *self)
540 Dmsg0(400, "Start JCR timeout checks\n");
542 /* Walk through all JCRs checking if any one is
543 * blocked for more than specified max time.
547 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
548 if (jcr->JobId == 0) {
551 fd = jcr->store_bsock;
553 timer_start = fd->timer_start;
554 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
555 fd->timer_start = 0; /* turn off timer */
556 fd->timed_out = TRUE;
557 Jmsg(jcr, M_ERROR, 0, _(
558 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
559 watchdog_time - timer_start);
560 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
563 fd = jcr->file_bsock;
565 timer_start = fd->timer_start;
566 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
567 fd->timer_start = 0; /* turn off timer */
568 fd->timed_out = TRUE;
569 Jmsg(jcr, M_ERROR, 0, _(
570 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
571 watchdog_time - timer_start);
572 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
577 timer_start = fd->timer_start;
578 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
579 fd->timer_start = 0; /* turn off timer */
580 fd->timed_out = TRUE;
581 Jmsg(jcr, M_ERROR, 0, _(
582 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
583 watchdog_time - timer_start);
584 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
591 Dmsg0(200, "Finished JCR timeout checks\n");
595 * Timeout signal comes here
597 static void timeout_handler(int sig)
599 return; /* thus interrupting the function */