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 static const int max_last_jobs = 10;
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()
65 while (!last_jobs->empty()) {
66 free(last_jobs->first());
74 void read_last_jobs_list(int fd, uint64_t addr)
76 struct s_last_job *je, job;
79 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
80 if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
83 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
86 Dmsg1(100, "Read num_items=%d\n", num);
87 if (num > 4 * max_last_jobs) { /* sanity check */
91 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
92 Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
96 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
97 memcpy((char *)je, (char *)&job, sizeof(job));
99 init_last_jobs_list();
101 last_jobs->append(je);
102 if (last_jobs->size() > max_last_jobs) {
103 last_jobs->remove(last_jobs->first());
109 uint64_t write_last_jobs_list(int fd, uint64_t addr)
111 struct s_last_job *je;
114 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
115 if (lseek(fd, addr, SEEK_SET) < 0) {
119 /* First record is number of entires */
120 num = last_jobs->size();
121 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
122 Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
125 foreach_dlist(je, last_jobs) {
126 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
127 Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
132 /* Return current address */
133 ssize_t stat = lseek(fd, 0, SEEK_CUR);
141 void lock_last_jobs_list()
143 /* Use jcr chain mutex */
147 void unlock_last_jobs_list()
149 /* Use jcr chain mutex */
154 * Push a subroutine address into the job end callback stack
156 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
158 jcr->job_end_push.prepend((void *)job_end_cb);
161 /* Pop each job_end subroutine and call it */
162 static void job_end_pop(JCR *jcr)
164 void (*job_end_cb)(JCR *jcr);
165 for (int i=0; i<jcr->job_end_push.size(); i++) {
166 job_end_cb = (void (*)(JCR *))jcr->job_end_push.get(i);
172 * Create a Job Control Record and link it into JCR chain
173 * Returns newly allocated JCR
174 * Note, since each daemon has a different JCR, he passes
177 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
180 MQUEUE_ITEM *item = NULL;
181 struct sigaction sigtimer;
183 Dmsg0(400, "Enter new_jcr\n");
184 jcr = (JCR *)malloc(size);
185 memset(jcr, 0, size);
186 jcr->msg_queue = new dlist(item, &item->link);
187 jcr->job_end_push.init(1, false);
188 jcr->sched_time = time(NULL);
189 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
191 pthread_mutex_init(&(jcr->mutex), NULL);
192 jcr->JobStatus = JS_Created; /* ready to run */
193 jcr->VolumeName = get_pool_memory(PM_FNAME);
194 jcr->VolumeName[0] = 0;
195 jcr->errmsg = get_pool_memory(PM_MESSAGE);
197 /* Setup some dummy values */
198 jcr->Job[0] = 0; /* no job name by default */
200 jcr->JobType = JT_ADMIN;
201 jcr->JobLevel = L_NONE;
202 jcr->JobStatus = JS_Created;
204 sigtimer.sa_flags = 0;
205 sigtimer.sa_handler = timeout_handler;
206 sigfillset(&sigtimer.sa_mask);
207 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
222 * Remove a JCR from the chain
223 * NOTE! The chain must be locked prior to calling
226 static void remove_jcr(JCR *jcr)
228 Dmsg0(400, "Enter remove_jcr\n");
230 Emsg0(M_ABORT, 0, "NULL jcr.\n");
232 if (!jcr->prev) { /* if no prev */
233 jobs = jcr->next; /* set new head */
235 jcr->prev->next = jcr->next; /* update prev */
238 jcr->next->prev = jcr->prev;
240 Dmsg0(400, "Leave remove_jcr\n");
244 * Free stuff common to all JCRs. N.B. Be careful to include only
245 * generic stuff in the common part of the jcr.
247 static void free_common_jcr(JCR *jcr)
249 struct s_last_job *je, last_job;
251 /* Keep some statistics */
252 switch (jcr->JobType) {
258 last_job.JobType = jcr->JobType;
259 last_job.JobId = jcr->JobId;
260 last_job.VolSessionId = jcr->VolSessionId;
261 last_job.VolSessionTime = jcr->VolSessionTime;
262 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
263 last_job.JobFiles = jcr->JobFiles;
264 last_job.JobBytes = jcr->JobBytes;
265 last_job.JobStatus = jcr->JobStatus;
266 last_job.JobLevel = jcr->JobLevel;
267 last_job.start_time = jcr->start_time;
268 last_job.end_time = time(NULL);
269 /* Keep list of last jobs, but not Console where JobId==0 */
270 if (last_job.JobId > 0) {
271 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
272 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
274 init_last_jobs_list();
276 last_jobs->append(je);
277 if (last_jobs->size() > max_last_jobs) {
278 last_jobs->remove(last_jobs->first());
285 pthread_mutex_destroy(&jcr->mutex);
287 delete jcr->msg_queue;
288 close_msg(jcr); /* close messages for this job */
290 /* do this after closing messages */
291 if (jcr->client_name) {
292 free_pool_memory(jcr->client_name);
293 jcr->client_name = NULL;
296 if (jcr->sd_auth_key) {
297 free(jcr->sd_auth_key);
298 jcr->sd_auth_key = NULL;
300 if (jcr->VolumeName) {
301 free_pool_memory(jcr->VolumeName);
302 jcr->VolumeName = NULL;
305 if (jcr->dir_bsock) {
306 bnet_close(jcr->dir_bsock);
307 jcr->dir_bsock = NULL;
310 free_pool_memory(jcr->errmsg);
317 if (jcr->cached_path) {
318 free_pool_memory(jcr->cached_path);
319 jcr->cached_path = NULL;
322 free_getuser_cache();
323 free_getgroup_cache();
328 * Global routine to free a jcr
331 void b_free_jcr(const char *file, int line, JCR *jcr)
333 Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
337 void free_jcr(JCR *jcr)
340 Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
345 jcr->use_count--; /* decrement use count */
346 if (jcr->use_count < 0) {
347 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
348 jcr->use_count, jcr->JobId);
350 Dmsg3(400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
351 if (jcr->use_count > 0) { /* if in use */
353 Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
356 dequeue_messages(jcr);
358 job_end_pop(jcr); /* pop and call hooked routines */
360 Dmsg1(400, "End job=%d\n", jcr->JobId);
361 if (jcr->daemon_free_jcr) {
362 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
365 free_common_jcr(jcr);
367 close_msg(NULL); /* flush any daemon messages */
369 Dmsg0(400, "Exit free_jcr\n");
374 * Global routine to free a jcr
375 * JCR chain is already locked
377 void free_locked_jcr(JCR *jcr)
379 jcr->use_count--; /* decrement use count */
380 Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
381 if (jcr->use_count > 0) { /* if in use */
385 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
386 free_common_jcr(jcr);
392 * Given a JobId, find the JCR
393 * Returns: jcr on success
396 JCR *get_jcr_by_id(uint32_t JobId)
400 lock_jcr_chain(); /* lock chain */
401 for (jcr = jobs; jcr; jcr=jcr->next) {
402 if (jcr->JobId == JobId) {
406 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
415 * Given a SessionId and SessionTime, find the JCR
416 * Returns: jcr on success
419 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
424 for (jcr = jobs; jcr; jcr=jcr->next) {
425 if (jcr->VolSessionId == SessionId &&
426 jcr->VolSessionTime == SessionTime) {
430 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
440 * Given a Job, find the JCR
441 * compares on the number of characters in Job
442 * thus allowing partial matches.
443 * Returns: jcr on success
446 JCR *get_jcr_by_partial_name(char *Job)
456 for (jcr = jobs; jcr; jcr=jcr->next) {
457 if (strncmp(Job, jcr->Job, len) == 0) {
461 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
471 * Given a Job, find the JCR
472 * requires an exact match of names.
473 * Returns: jcr on success
476 JCR *get_jcr_by_full_name(char *Job)
484 for (jcr = jobs; jcr; jcr=jcr->next) {
485 if (strcmp(jcr->Job, Job) == 0) {
489 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
497 void set_jcr_job_status(JCR *jcr, int JobStatus)
500 * For a set of errors, ... keep the current status
501 * so it isn't lost. For all others, set it.
503 switch (jcr->JobStatus) {
504 case JS_ErrorTerminated:
511 jcr->JobStatus = JobStatus;
518 void lock_jcr_chain()
521 if ((errstat=rwl_writelock(&lock)) != 0) {
522 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
530 void unlock_jcr_chain()
533 if ((errstat=rwl_writeunlock(&lock)) != 0) {
534 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
540 JCR *get_next_jcr(JCR *prev_jcr)
544 if (prev_jcr == NULL) {
547 jcr = prev_jcr->next;
553 Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
558 bool init_jcr_subsystem(void)
560 watchdog_t *wd = new_watchdog();
562 wd->one_shot = false;
563 wd->interval = 30; /* FIXME: should be configurable somewhere, even
564 if only with a #define */
565 wd->callback = jcr_timeout_check;
567 register_watchdog(wd);
572 static void jcr_timeout_check(watchdog_t *self)
578 Dmsg0(400, "Start JCR timeout checks\n");
580 /* Walk through all JCRs checking if any one is
581 * blocked for more than specified max time.
585 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
586 if (jcr->JobId == 0) {
589 fd = jcr->store_bsock;
591 timer_start = fd->timer_start;
592 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
593 fd->timer_start = 0; /* turn off timer */
594 fd->timed_out = TRUE;
595 Jmsg(jcr, M_ERROR, 0, _(
596 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
597 watchdog_time - timer_start);
598 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
601 fd = jcr->file_bsock;
603 timer_start = fd->timer_start;
604 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
605 fd->timer_start = 0; /* turn off timer */
606 fd->timed_out = TRUE;
607 Jmsg(jcr, M_ERROR, 0, _(
608 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
609 watchdog_time - timer_start);
610 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
615 timer_start = fd->timer_start;
616 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
617 fd->timer_start = 0; /* turn off timer */
618 fd->timed_out = TRUE;
619 Jmsg(jcr, M_ERROR, 0, _(
620 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
621 watchdog_time - timer_start);
622 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
629 Dmsg0(400, "Finished JCR timeout checks\n");
633 * Timeout signal comes here
635 static void timeout_handler(int sig)
637 return; /* thus interrupting the function */