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()
71 void read_last_jobs_list(int fd, uint64_t addr)
73 struct s_last_job *je, job;
76 Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
77 if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
80 if (read(fd, &num, sizeof(num)) != sizeof(num)) {
83 Dmsg1(100, "Read num_items=%d\n", num);
84 if (num > 4 * max_last_jobs) { /* sanity check */
88 if (read(fd, &job, sizeof(job)) != sizeof(job)) {
89 Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
93 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
94 memcpy((char *)je, (char *)&job, sizeof(job));
96 init_last_jobs_list();
98 last_jobs->append(je);
99 if (last_jobs->size() > max_last_jobs) {
100 last_jobs->remove(last_jobs->first());
106 uint64_t write_last_jobs_list(int fd, uint64_t addr)
108 struct s_last_job *je;
111 Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
112 if (lseek(fd, addr, SEEK_SET) < 0) {
116 /* First record is number of entires */
117 num = last_jobs->size();
118 if (write(fd, &num, sizeof(num)) != sizeof(num)) {
119 Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
122 foreach_dlist(je, last_jobs) {
123 if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
124 Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
129 /* Return current address */
130 ssize_t stat = lseek(fd, 0, SEEK_CUR);
138 void lock_last_jobs_list()
140 /* Use jcr chain mutex */
144 void unlock_last_jobs_list()
146 /* Use jcr chain mutex */
151 * Push a subroutine address into the job end callback stack
153 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
155 jcr->job_end_push.prepend((void *)job_end_cb);
158 /* Pop each job_end subroutine and call it */
159 static void job_end_pop(JCR *jcr)
161 void (*job_end_cb)(JCR *jcr);
162 for (int i=0; i<jcr->job_end_push.size(); i++) {
163 job_end_cb = (void (*)(JCR *))jcr->job_end_push.get(i);
169 * Create a Job Control Record and link it into JCR chain
170 * Returns newly allocated JCR
171 * Note, since each daemon has a different JCR, he passes
174 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
177 MQUEUE_ITEM *item = NULL;
178 struct sigaction sigtimer;
180 Dmsg0(400, "Enter new_jcr\n");
181 jcr = (JCR *)malloc(size);
182 memset(jcr, 0, size);
183 jcr->msg_queue = new dlist(item, &item->link);
184 jcr->job_end_push.init(1, false);
185 jcr->sched_time = time(NULL);
186 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
188 pthread_mutex_init(&(jcr->mutex), NULL);
189 jcr->JobStatus = JS_Created; /* ready to run */
190 jcr->VolumeName = get_pool_memory(PM_FNAME);
191 jcr->VolumeName[0] = 0;
192 jcr->errmsg = get_pool_memory(PM_MESSAGE);
194 /* Setup some dummy values */
195 jcr->Job[0] = 0; /* no job name by default */
197 jcr->JobType = JT_ADMIN;
198 jcr->JobLevel = L_NONE;
199 jcr->JobStatus = JS_Created;
201 sigtimer.sa_flags = 0;
202 sigtimer.sa_handler = timeout_handler;
203 sigfillset(&sigtimer.sa_mask);
204 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
219 * Remove a JCR from the chain
220 * NOTE! The chain must be locked prior to calling
223 static void remove_jcr(JCR *jcr)
225 Dmsg0(400, "Enter remove_jcr\n");
227 Emsg0(M_ABORT, 0, "NULL jcr.\n");
229 if (!jcr->prev) { /* if no prev */
230 jobs = jcr->next; /* set new head */
232 jcr->prev->next = jcr->next; /* update prev */
235 jcr->next->prev = jcr->prev;
237 Dmsg0(400, "Leave remove_jcr\n");
241 * Free stuff common to all JCRs. N.B. Be careful to include only
242 * generic stuff in the common part of the jcr.
244 static void free_common_jcr(JCR *jcr)
246 struct s_last_job *je, last_job;
248 /* Keep some statistics */
249 switch (jcr->JobType) {
255 last_job.JobType = jcr->JobType;
256 last_job.JobId = jcr->JobId;
257 last_job.VolSessionId = jcr->VolSessionId;
258 last_job.VolSessionTime = jcr->VolSessionTime;
259 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
260 last_job.JobFiles = jcr->JobFiles;
261 last_job.JobBytes = jcr->JobBytes;
262 last_job.JobStatus = jcr->JobStatus;
263 last_job.JobLevel = jcr->JobLevel;
264 last_job.start_time = jcr->start_time;
265 last_job.end_time = time(NULL);
266 /* Keep list of last jobs, but not Console where JobId==0 */
267 if (last_job.JobId > 0) {
268 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
269 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
271 init_last_jobs_list();
273 last_jobs->append(je);
274 if (last_jobs->size() > max_last_jobs) {
275 last_jobs->remove(last_jobs->first());
282 pthread_mutex_destroy(&jcr->mutex);
284 delete jcr->msg_queue;
285 close_msg(jcr); /* close messages for this job */
287 /* do this after closing messages */
288 if (jcr->client_name) {
289 free_pool_memory(jcr->client_name);
290 jcr->client_name = NULL;
293 if (jcr->sd_auth_key) {
294 free(jcr->sd_auth_key);
295 jcr->sd_auth_key = NULL;
297 if (jcr->VolumeName) {
298 free_pool_memory(jcr->VolumeName);
299 jcr->VolumeName = NULL;
302 if (jcr->dir_bsock) {
303 bnet_close(jcr->dir_bsock);
304 jcr->dir_bsock = NULL;
307 free_pool_memory(jcr->errmsg);
314 if (jcr->cached_path) {
315 free_pool_memory(jcr->cached_path);
316 jcr->cached_path = NULL;
319 free_getuser_cache();
320 free_getgroup_cache();
325 * Global routine to free a jcr
328 void b_free_jcr(const char *file, int line, JCR *jcr)
330 Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
334 void free_jcr(JCR *jcr)
337 Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
342 jcr->use_count--; /* decrement use count */
343 if (jcr->use_count < 0) {
344 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
345 jcr->use_count, jcr->JobId);
347 Dmsg3(400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
348 if (jcr->use_count > 0) { /* if in use */
350 Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
353 dequeue_messages(jcr);
355 job_end_pop(jcr); /* pop and call hooked routines */
357 Dmsg1(400, "End job=%d\n", jcr->JobId);
358 if (jcr->daemon_free_jcr) {
359 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
362 free_common_jcr(jcr);
364 close_msg(NULL); /* flush any daemon messages */
366 Dmsg0(400, "Exit free_jcr\n");
371 * Global routine to free a jcr
372 * JCR chain is already locked
374 void free_locked_jcr(JCR *jcr)
376 jcr->use_count--; /* decrement use count */
377 Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
378 if (jcr->use_count > 0) { /* if in use */
382 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
383 free_common_jcr(jcr);
389 * Given a JobId, find the JCR
390 * Returns: jcr on success
393 JCR *get_jcr_by_id(uint32_t JobId)
397 lock_jcr_chain(); /* lock chain */
398 for (jcr = jobs; jcr; jcr=jcr->next) {
399 if (jcr->JobId == JobId) {
403 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
412 * Given a SessionId and SessionTime, find the JCR
413 * Returns: jcr on success
416 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
421 for (jcr = jobs; jcr; jcr=jcr->next) {
422 if (jcr->VolSessionId == SessionId &&
423 jcr->VolSessionTime == SessionTime) {
427 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
437 * Given a Job, find the JCR
438 * compares on the number of characters in Job
439 * thus allowing partial matches.
440 * Returns: jcr on success
443 JCR *get_jcr_by_partial_name(char *Job)
453 for (jcr = jobs; jcr; jcr=jcr->next) {
454 if (strncmp(Job, jcr->Job, len) == 0) {
458 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
468 * Given a Job, find the JCR
469 * requires an exact match of names.
470 * Returns: jcr on success
473 JCR *get_jcr_by_full_name(char *Job)
481 for (jcr = jobs; jcr; jcr=jcr->next) {
482 if (strcmp(jcr->Job, Job) == 0) {
486 Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
494 void set_jcr_job_status(JCR *jcr, int JobStatus)
497 * For a set of errors, ... keep the current status
498 * so it isn't lost. For all others, set it.
500 switch (jcr->JobStatus) {
501 case JS_ErrorTerminated:
508 jcr->JobStatus = JobStatus;
515 void lock_jcr_chain()
518 if ((errstat=rwl_writelock(&lock)) != 0) {
519 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
527 void unlock_jcr_chain()
530 if ((errstat=rwl_writeunlock(&lock)) != 0) {
531 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
537 JCR *get_next_jcr(JCR *prev_jcr)
541 if (prev_jcr == NULL) {
544 jcr = prev_jcr->next;
550 Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
555 bool init_jcr_subsystem(void)
557 watchdog_t *wd = new_watchdog();
559 wd->one_shot = false;
560 wd->interval = 30; /* FIXME: should be configurable somewhere, even
561 if only with a #define */
562 wd->callback = jcr_timeout_check;
564 register_watchdog(wd);
569 static void jcr_timeout_check(watchdog_t *self)
575 Dmsg0(400, "Start JCR timeout checks\n");
577 /* Walk through all JCRs checking if any one is
578 * blocked for more than specified max time.
582 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
583 if (jcr->JobId == 0) {
586 fd = jcr->store_bsock;
588 timer_start = fd->timer_start;
589 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
590 fd->timer_start = 0; /* turn off timer */
591 fd->timed_out = TRUE;
592 Jmsg(jcr, M_ERROR, 0, _(
593 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
594 watchdog_time - timer_start);
595 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
598 fd = jcr->file_bsock;
600 timer_start = fd->timer_start;
601 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
602 fd->timer_start = 0; /* turn off timer */
603 fd->timed_out = TRUE;
604 Jmsg(jcr, M_ERROR, 0, _(
605 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
606 watchdog_time - timer_start);
607 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
612 timer_start = fd->timer_start;
613 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
614 fd->timer_start = 0; /* turn off timer */
615 fd->timed_out = TRUE;
616 Jmsg(jcr, M_ERROR, 0, _(
617 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
618 watchdog_time - timer_start);
619 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
626 Dmsg0(400, "Finished JCR timeout checks\n");
630 * Timeout signal comes here
632 static void timeout_handler(int sig)
634 return; /* thus interrupting the function */