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()
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(100, "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(100, "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(100, "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 * Push a subroutine address into the job end callback stack
157 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
159 jcr->job_end_push.prepend((void *)job_end_cb);
162 /* Pop each job_end subroutine and call it */
163 static void job_end_pop(JCR *jcr)
165 void (*job_end_cb)(JCR *jcr);
166 for (int i=0; i<jcr->job_end_push.size(); i++) {
167 job_end_cb = (void (*)(JCR *))jcr->job_end_push.get(i);
173 * Create a Job Control Record and link it into JCR chain
174 * Returns newly allocated JCR
175 * Note, since each daemon has a different JCR, he passes
178 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
181 MQUEUE_ITEM *item = NULL;
182 struct sigaction sigtimer;
184 Dmsg0(400, "Enter new_jcr\n");
185 jcr = (JCR *)malloc(size);
186 memset(jcr, 0, size);
187 jcr->msg_queue = new dlist(item, &item->link);
188 jcr->job_end_push.init(1, false);
189 jcr->my_thread_id = pthread_self();
190 jcr->sched_time = time(NULL);
191 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
193 pthread_mutex_init(&(jcr->mutex), NULL);
194 jcr->JobStatus = JS_Created; /* ready to run */
195 jcr->VolumeName = get_pool_memory(PM_FNAME);
196 jcr->VolumeName[0] = 0;
197 jcr->errmsg = get_pool_memory(PM_MESSAGE);
199 /* Setup some dummy values */
200 jcr->Job[0] = 0; /* no job name by default */
202 jcr->JobType = JT_ADMIN;
203 jcr->JobLevel = L_NONE;
204 jcr->JobStatus = JS_Created;
206 sigtimer.sa_flags = 0;
207 sigtimer.sa_handler = timeout_handler;
208 sigfillset(&sigtimer.sa_mask);
209 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
224 * Remove a JCR from the chain
225 * NOTE! The chain must be locked prior to calling
228 static void remove_jcr(JCR *jcr)
230 Dmsg0(400, "Enter remove_jcr\n");
232 Emsg0(M_ABORT, 0, "NULL jcr.\n");
234 if (!jcr->prev) { /* if no prev */
235 jobs = jcr->next; /* set new head */
237 jcr->prev->next = jcr->next; /* update prev */
240 jcr->next->prev = jcr->prev;
242 Dmsg0(400, "Leave remove_jcr\n");
246 * Free stuff common to all JCRs. N.B. Be careful to include only
247 * generic stuff in the common part of the jcr.
249 static void free_common_jcr(JCR *jcr)
251 struct s_last_job *je, last_job;
253 /* Keep some statistics */
254 switch (jcr->JobType) {
260 last_job.JobType = jcr->JobType;
261 last_job.JobId = jcr->JobId;
262 last_job.VolSessionId = jcr->VolSessionId;
263 last_job.VolSessionTime = jcr->VolSessionTime;
264 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
265 last_job.JobFiles = jcr->JobFiles;
266 last_job.JobBytes = jcr->JobBytes;
267 last_job.JobStatus = jcr->JobStatus;
268 last_job.JobLevel = jcr->JobLevel;
269 last_job.start_time = jcr->start_time;
270 last_job.end_time = time(NULL);
271 /* Keep list of last jobs, but not Console where JobId==0 */
272 if (last_job.JobId > 0) {
273 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
274 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
276 init_last_jobs_list();
278 last_jobs->append(je);
279 if (last_jobs->size() > max_last_jobs) {
280 last_jobs->remove(last_jobs->first());
287 pthread_mutex_destroy(&jcr->mutex);
289 close_msg(jcr); /* close messages for this job */
290 delete jcr->msg_queue;
292 /* do this after closing messages */
293 if (jcr->client_name) {
294 free_pool_memory(jcr->client_name);
295 jcr->client_name = NULL;
298 if (jcr->sd_auth_key) {
299 free(jcr->sd_auth_key);
300 jcr->sd_auth_key = NULL;
302 if (jcr->VolumeName) {
303 free_pool_memory(jcr->VolumeName);
304 jcr->VolumeName = NULL;
307 if (jcr->dir_bsock) {
308 bnet_close(jcr->dir_bsock);
309 jcr->dir_bsock = NULL;
312 free_pool_memory(jcr->errmsg);
319 if (jcr->cached_path) {
320 free_pool_memory(jcr->cached_path);
321 jcr->cached_path = NULL;
324 free_getuser_cache();
325 free_getgroup_cache();
330 * Global routine to free a jcr
333 void b_free_jcr(const char *file, int line, JCR *jcr)
335 Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
339 void free_jcr(JCR *jcr)
342 Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
347 jcr->use_count--; /* decrement use count */
348 if (jcr->use_count < 0) {
349 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
350 jcr->use_count, jcr->JobId);
352 Dmsg3(400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
353 if (jcr->use_count > 0) { /* if in use */
355 Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
359 job_end_pop(jcr); /* pop and call hooked routines */
361 Dmsg1(400, "End job=%d\n", jcr->JobId);
362 if (jcr->daemon_free_jcr) {
363 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
366 free_common_jcr(jcr);
368 close_msg(NULL); /* flush any daemon messages */
370 Dmsg0(400, "Exit free_jcr\n");
375 * Global routine to free a jcr
376 * JCR chain is already locked
378 void free_locked_jcr(JCR *jcr)
380 jcr->use_count--; /* decrement use count */
381 Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
382 if (jcr->use_count > 0) { /* if in use */
386 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
387 free_common_jcr(jcr);
393 * Given a JobId, find the JCR
394 * Returns: jcr on success
397 JCR *get_jcr_by_id(uint32_t JobId)
401 lock_jcr_chain(); /* lock chain */
402 for (jcr = jobs; jcr; jcr=jcr->next) {
403 if (jcr->JobId == JobId) {
407 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
416 * Given a SessionId and SessionTime, find the JCR
417 * Returns: jcr on success
420 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
425 for (jcr = jobs; jcr; jcr=jcr->next) {
426 if (jcr->VolSessionId == SessionId &&
427 jcr->VolSessionTime == SessionTime) {
431 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
441 * Given a Job, find the JCR
442 * compares on the number of characters in Job
443 * thus allowing partial matches.
444 * Returns: jcr on success
447 JCR *get_jcr_by_partial_name(char *Job)
457 for (jcr = jobs; jcr; jcr=jcr->next) {
458 if (strncmp(Job, jcr->Job, len) == 0) {
462 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
472 * Given a Job, find the JCR
473 * requires an exact match of names.
474 * Returns: jcr on success
477 JCR *get_jcr_by_full_name(char *Job)
485 for (jcr = jobs; jcr; jcr=jcr->next) {
486 if (strcmp(jcr->Job, Job) == 0) {
490 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
498 void set_jcr_job_status(JCR *jcr, int JobStatus)
501 * For a set of errors, ... keep the current status
502 * so it isn't lost. For all others, set it.
504 switch (jcr->JobStatus) {
505 case JS_ErrorTerminated:
512 jcr->JobStatus = JobStatus;
519 void lock_jcr_chain()
522 if ((errstat=rwl_writelock(&lock)) != 0) {
523 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
531 void unlock_jcr_chain()
534 if ((errstat=rwl_writeunlock(&lock)) != 0) {
535 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
541 JCR *get_next_jcr(JCR *prev_jcr)
545 if (prev_jcr == NULL) {
548 jcr = prev_jcr->next;
554 Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
559 bool init_jcr_subsystem(void)
561 watchdog_t *wd = new_watchdog();
563 wd->one_shot = false;
564 wd->interval = 30; /* FIXME: should be configurable somewhere, even
565 if only with a #define */
566 wd->callback = jcr_timeout_check;
568 register_watchdog(wd);
573 static void jcr_timeout_check(watchdog_t *self)
579 Dmsg0(400, "Start JCR timeout checks\n");
581 /* Walk through all JCRs checking if any one is
582 * blocked for more than specified max time.
586 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
587 if (jcr->JobId == 0) {
590 fd = jcr->store_bsock;
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 Storage daemon.\n"),
598 watchdog_time - timer_start);
599 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
602 fd = jcr->file_bsock;
604 timer_start = fd->timer_start;
605 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
606 fd->timer_start = 0; /* turn off timer */
607 fd->timed_out = TRUE;
608 Jmsg(jcr, M_ERROR, 0, _(
609 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
610 watchdog_time - timer_start);
611 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
616 timer_start = fd->timer_start;
617 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
618 fd->timer_start = 0; /* turn off timer */
619 fd->timed_out = TRUE;
620 Jmsg(jcr, M_ERROR, 0, _(
621 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
622 watchdog_time - timer_start);
623 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
630 Dmsg0(400, "Finished JCR timeout checks\n");
634 * Timeout signal comes here
636 static void timeout_handler(int sig)
638 return; /* thus interrupting the function */