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;
80 Dmsg1(010, "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(010, "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(010, "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 * Create a Job Control Record and link it into JCR chain
156 * Returns newly allocated JCR
157 * Note, since each daemon has a different JCR, he passes
160 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
163 MQUEUE_ITEM *item = NULL;
164 struct sigaction sigtimer;
166 Dmsg0(200, "Enter new_jcr\n");
167 jcr = (JCR *)malloc(size);
168 memset(jcr, 0, size);
169 jcr->msg_queue = new dlist(item, &item->link);
170 jcr->my_thread_id = pthread_self();
171 jcr->sched_time = time(NULL);
172 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
174 pthread_mutex_init(&(jcr->mutex), NULL);
175 jcr->JobStatus = JS_Created; /* ready to run */
176 jcr->VolumeName = get_pool_memory(PM_FNAME);
177 jcr->VolumeName[0] = 0;
178 jcr->errmsg = get_pool_memory(PM_MESSAGE);
180 strcpy(jcr->Job, "*Console*"); /* default */
182 sigtimer.sa_flags = 0;
183 sigtimer.sa_handler = timeout_handler;
184 sigfillset(&sigtimer.sa_mask);
185 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
200 * Remove a JCR from the chain
201 * NOTE! The chain must be locked prior to calling
204 static void remove_jcr(JCR *jcr)
206 Dmsg0(150, "Enter remove_jcr\n");
208 Emsg0(M_ABORT, 0, "NULL jcr.\n");
210 if (!jcr->prev) { /* if no prev */
211 jobs = jcr->next; /* set new head */
213 jcr->prev->next = jcr->next; /* update prev */
216 jcr->next->prev = jcr->prev;
218 Dmsg0(150, "Leave remove_jcr\n");
222 * Free stuff common to all JCRs. N.B. Be careful to include only
223 * generic stuff in the common part of the jcr.
225 static void free_common_jcr(JCR *jcr)
227 struct s_last_job *je, last_job;
229 /* Keep some statistics */
230 switch (jcr->JobType) {
236 last_job.JobType = jcr->JobType;
237 last_job.JobId = jcr->JobId;
238 last_job.VolSessionId = jcr->VolSessionId;
239 last_job.VolSessionTime = jcr->VolSessionTime;
240 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
241 last_job.JobFiles = jcr->JobFiles;
242 last_job.JobBytes = jcr->JobBytes;
243 last_job.JobStatus = jcr->JobStatus;
244 last_job.JobLevel = jcr->JobLevel;
245 last_job.start_time = jcr->start_time;
246 last_job.end_time = time(NULL);
247 /* Keep list of last jobs, but not Console where JobId==0 */
248 if (last_job.JobId > 0) {
249 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
250 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
252 init_last_jobs_list();
254 last_jobs->append(je);
255 if (last_jobs->size() > MAX_LAST_JOBS) {
256 last_jobs->remove(last_jobs->first());
263 pthread_mutex_destroy(&jcr->mutex);
265 close_msg(jcr); /* close messages for this job */
266 delete jcr->msg_queue;
268 /* do this after closing messages */
269 if (jcr->client_name) {
270 free_pool_memory(jcr->client_name);
271 jcr->client_name = NULL;
274 if (jcr->sd_auth_key) {
275 free(jcr->sd_auth_key);
276 jcr->sd_auth_key = NULL;
278 if (jcr->VolumeName) {
279 free_pool_memory(jcr->VolumeName);
280 jcr->VolumeName = NULL;
283 if (jcr->dir_bsock) {
284 bnet_close(jcr->dir_bsock);
285 jcr->dir_bsock = NULL;
288 free_pool_memory(jcr->errmsg);
295 if (jcr->cached_path) {
296 free_pool_memory(jcr->cached_path);
297 jcr->cached_path = NULL;
300 free_getuser_cache();
301 free_getgroup_cache();
306 * Global routine to free a jcr
309 void b_free_jcr(const char *file, int line, JCR *jcr)
311 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
315 void free_jcr(JCR *jcr)
318 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
323 jcr->use_count--; /* decrement use count */
324 if (jcr->use_count < 0) {
325 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
326 jcr->use_count, jcr->JobId);
328 Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
329 if (jcr->use_count > 0) { /* if in use */
331 Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
336 Dmsg1(200, "End job=%d\n", jcr->JobId);
337 if (jcr->daemon_free_jcr) {
338 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
341 free_common_jcr(jcr);
343 close_msg(NULL); /* flush any daemon messages */
345 Dmsg0(200, "Exit free_jcr\n");
350 * Global routine to free a jcr
351 * JCR chain is already locked
353 void free_locked_jcr(JCR *jcr)
355 jcr->use_count--; /* decrement use count */
356 Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
357 if (jcr->use_count > 0) { /* if in use */
361 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
362 free_common_jcr(jcr);
369 * Given a JobId, find the JCR
370 * Returns: jcr on success
373 JCR *get_jcr_by_id(uint32_t JobId)
377 lock_jcr_chain(); /* lock chain */
378 for (jcr = jobs; jcr; jcr=jcr->next) {
379 if (jcr->JobId == JobId) {
383 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
392 * Given a SessionId and SessionTime, find the JCR
393 * Returns: jcr on success
396 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
401 for (jcr = jobs; jcr; jcr=jcr->next) {
402 if (jcr->VolSessionId == SessionId &&
403 jcr->VolSessionTime == SessionTime) {
407 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
417 * Given a Job, find the JCR
418 * compares on the number of characters in Job
419 * thus allowing partial matches.
420 * Returns: jcr on success
423 JCR *get_jcr_by_partial_name(char *Job)
433 for (jcr = jobs; jcr; jcr=jcr->next) {
434 if (strncmp(Job, jcr->Job, len) == 0) {
438 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
448 * Given a Job, find the JCR
449 * requires an exact match of names.
450 * Returns: jcr on success
453 JCR *get_jcr_by_full_name(char *Job)
461 for (jcr = jobs; jcr; jcr=jcr->next) {
462 if (strcmp(jcr->Job, Job) == 0) {
466 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
474 void set_jcr_job_status(JCR *jcr, int JobStatus)
477 * For a set of errors, ... keep the current status
478 * so it isn't lost. For all others, set it.
480 switch (jcr->JobStatus) {
481 case JS_ErrorTerminated:
488 jcr->JobStatus = JobStatus;
495 void lock_jcr_chain()
498 if ((errstat=rwl_writelock(&lock)) != 0) {
499 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
507 void unlock_jcr_chain()
510 if ((errstat=rwl_writeunlock(&lock)) != 0) {
511 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
517 JCR *get_next_jcr(JCR *prev_jcr)
521 if (prev_jcr == NULL) {
524 jcr = prev_jcr->next;
530 Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
535 bool init_jcr_subsystem(void)
537 watchdog_t *wd = new_watchdog();
539 wd->one_shot = false;
540 wd->interval = 30; /* FIXME: should be configurable somewhere, even
541 if only with a #define */
542 wd->callback = jcr_timeout_check;
544 register_watchdog(wd);
549 static void jcr_timeout_check(watchdog_t *self)
555 Dmsg0(400, "Start JCR timeout checks\n");
557 /* Walk through all JCRs checking if any one is
558 * blocked for more than specified max time.
562 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
563 if (jcr->JobId == 0) {
566 fd = jcr->store_bsock;
568 timer_start = fd->timer_start;
569 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
570 fd->timer_start = 0; /* turn off timer */
571 fd->timed_out = TRUE;
572 Jmsg(jcr, M_ERROR, 0, _(
573 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
574 watchdog_time - timer_start);
575 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
578 fd = jcr->file_bsock;
580 timer_start = fd->timer_start;
581 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
582 fd->timer_start = 0; /* turn off timer */
583 fd->timed_out = TRUE;
584 Jmsg(jcr, M_ERROR, 0, _(
585 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
586 watchdog_time - timer_start);
587 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
592 timer_start = fd->timer_start;
593 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
594 fd->timer_start = 0; /* turn off timer */
595 fd->timed_out = TRUE;
596 Jmsg(jcr, M_ERROR, 0, _(
597 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
598 watchdog_time - timer_start);
599 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
606 Dmsg0(200, "Finished JCR timeout checks\n");
610 * Timeout signal comes here
612 static void timeout_handler(int sig)
614 return; /* thus interrupting the function */