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 if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
83 if (read(fd, &num, sizeof(num)) < 0) {
86 if (num > 4 * MAX_LAST_JOBS) { /* sanity check */
90 if (read(fd, &job, sizeof(job)) < 0) {
94 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
95 memcpy((char *)je, (char *)&job, sizeof(job));
97 init_last_jobs_list();
99 last_jobs->append(je);
100 if (last_jobs->size() > MAX_LAST_JOBS) {
101 last_jobs->remove(last_jobs->first());
107 uint64_t write_last_jobs_list(int fd, uint64_t addr)
109 struct s_last_job *je;
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)) < 0) {
121 foreach_dlist(je, last_jobs) {
122 if (write(fd, je, sizeof(struct s_last_job)) < 0) {
127 /* Return current address */
128 ssize_t stat = lseek(fd, 0, SEEK_CUR);
136 void lock_last_jobs_list()
138 /* Use jcr chain mutex */
142 void unlock_last_jobs_list()
144 /* Use jcr chain mutex */
149 * Create a Job Control Record and link it into JCR chain
150 * Returns newly allocated JCR
151 * Note, since each daemon has a different JCR, he passes
154 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
157 MQUEUE_ITEM *item = NULL;
158 struct sigaction sigtimer;
160 Dmsg0(200, "Enter new_jcr\n");
161 jcr = (JCR *)malloc(size);
162 memset(jcr, 0, size);
163 jcr->msg_queue = new dlist(item, &item->link);
164 jcr->my_thread_id = pthread_self();
165 jcr->sched_time = time(NULL);
166 jcr->daemon_free_jcr = daemon_free_jcr; /* plug daemon free routine */
168 pthread_mutex_init(&(jcr->mutex), NULL);
169 jcr->JobStatus = JS_Created; /* ready to run */
170 jcr->VolumeName = get_pool_memory(PM_FNAME);
171 jcr->VolumeName[0] = 0;
172 jcr->errmsg = get_pool_memory(PM_MESSAGE);
174 strcpy(jcr->Job, "*Console*"); /* default */
176 sigtimer.sa_flags = 0;
177 sigtimer.sa_handler = timeout_handler;
178 sigfillset(&sigtimer.sa_mask);
179 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
194 * Remove a JCR from the chain
195 * NOTE! The chain must be locked prior to calling
198 static void remove_jcr(JCR *jcr)
200 Dmsg0(150, "Enter remove_jcr\n");
202 Emsg0(M_ABORT, 0, "NULL jcr.\n");
204 if (!jcr->prev) { /* if no prev */
205 jobs = jcr->next; /* set new head */
207 jcr->prev->next = jcr->next; /* update prev */
210 jcr->next->prev = jcr->prev;
212 Dmsg0(150, "Leave remove_jcr\n");
216 * Free stuff common to all JCRs. N.B. Be careful to include only
217 * generic stuff in the common part of the jcr.
219 static void free_common_jcr(JCR *jcr)
221 struct s_last_job *je, last_job;
223 /* Keep some statistics */
224 switch (jcr->JobType) {
230 last_job.JobType = jcr->JobType;
231 last_job.JobId = jcr->JobId;
232 last_job.VolSessionId = jcr->VolSessionId;
233 last_job.VolSessionTime = jcr->VolSessionTime;
234 bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
235 last_job.JobFiles = jcr->JobFiles;
236 last_job.JobBytes = jcr->JobBytes;
237 last_job.JobStatus = jcr->JobStatus;
238 last_job.JobLevel = jcr->JobLevel;
239 last_job.start_time = jcr->start_time;
240 last_job.end_time = time(NULL);
241 /* Keep list of last jobs, but not Console where JobId==0 */
242 if (last_job.JobId > 0) {
243 je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
244 memcpy((char *)je, (char *)&last_job, sizeof(last_job));
246 init_last_jobs_list();
248 last_jobs->append(je);
249 if (last_jobs->size() > MAX_LAST_JOBS) {
250 last_jobs->remove(last_jobs->first());
257 pthread_mutex_destroy(&jcr->mutex);
259 close_msg(jcr); /* close messages for this job */
260 delete jcr->msg_queue;
262 /* do this after closing messages */
263 if (jcr->client_name) {
264 free_pool_memory(jcr->client_name);
265 jcr->client_name = NULL;
268 if (jcr->sd_auth_key) {
269 free(jcr->sd_auth_key);
270 jcr->sd_auth_key = NULL;
272 if (jcr->VolumeName) {
273 free_pool_memory(jcr->VolumeName);
274 jcr->VolumeName = NULL;
277 if (jcr->dir_bsock) {
278 bnet_close(jcr->dir_bsock);
279 jcr->dir_bsock = NULL;
282 free_pool_memory(jcr->errmsg);
289 if (jcr->cached_path) {
290 free_pool_memory(jcr->cached_path);
291 jcr->cached_path = NULL;
294 free_getuser_cache();
295 free_getgroup_cache();
300 * Global routine to free a jcr
303 void b_free_jcr(const char *file, int line, JCR *jcr)
305 Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
309 void free_jcr(JCR *jcr)
312 Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
317 jcr->use_count--; /* decrement use count */
318 if (jcr->use_count < 0) {
319 Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
320 jcr->use_count, jcr->JobId);
322 Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
323 if (jcr->use_count > 0) { /* if in use */
325 Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
330 Dmsg1(200, "End job=%d\n", jcr->JobId);
331 if (jcr->daemon_free_jcr) {
332 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
335 free_common_jcr(jcr);
337 close_msg(NULL); /* flush any daemon messages */
339 Dmsg0(200, "Exit free_jcr\n");
344 * Global routine to free a jcr
345 * JCR chain is already locked
347 void free_locked_jcr(JCR *jcr)
349 jcr->use_count--; /* decrement use count */
350 Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
351 if (jcr->use_count > 0) { /* if in use */
355 jcr->daemon_free_jcr(jcr); /* call daemon free routine */
356 free_common_jcr(jcr);
363 * Given a JobId, find the JCR
364 * Returns: jcr on success
367 JCR *get_jcr_by_id(uint32_t JobId)
371 lock_jcr_chain(); /* lock chain */
372 for (jcr = jobs; jcr; jcr=jcr->next) {
373 if (jcr->JobId == JobId) {
377 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
386 * Given a SessionId and SessionTime, find the JCR
387 * Returns: jcr on success
390 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
395 for (jcr = jobs; jcr; jcr=jcr->next) {
396 if (jcr->VolSessionId == SessionId &&
397 jcr->VolSessionTime == SessionTime) {
401 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
411 * Given a Job, find the JCR
412 * compares on the number of characters in Job
413 * thus allowing partial matches.
414 * Returns: jcr on success
417 JCR *get_jcr_by_partial_name(char *Job)
427 for (jcr = jobs; jcr; jcr=jcr->next) {
428 if (strncmp(Job, jcr->Job, len) == 0) {
432 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
442 * Given a Job, find the JCR
443 * requires an exact match of names.
444 * Returns: jcr on success
447 JCR *get_jcr_by_full_name(char *Job)
455 for (jcr = jobs; jcr; jcr=jcr->next) {
456 if (strcmp(jcr->Job, Job) == 0) {
460 Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
468 void set_jcr_job_status(JCR *jcr, int JobStatus)
471 * For a set of errors, ... keep the current status
472 * so it isn't lost. For all others, set it.
474 switch (jcr->JobStatus) {
475 case JS_ErrorTerminated:
482 jcr->JobStatus = JobStatus;
489 void lock_jcr_chain()
492 if ((errstat=rwl_writelock(&lock)) != 0) {
493 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
501 void unlock_jcr_chain()
504 if ((errstat=rwl_writeunlock(&lock)) != 0) {
505 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
511 JCR *get_next_jcr(JCR *prev_jcr)
515 if (prev_jcr == NULL) {
518 jcr = prev_jcr->next;
524 Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
529 bool init_jcr_subsystem(void)
531 watchdog_t *wd = new_watchdog();
533 wd->one_shot = false;
534 wd->interval = 30; /* FIXME: should be configurable somewhere, even
535 if only with a #define */
536 wd->callback = jcr_timeout_check;
538 register_watchdog(wd);
543 static void jcr_timeout_check(watchdog_t *self)
549 Dmsg0(400, "Start JCR timeout checks\n");
551 /* Walk through all JCRs checking if any one is
552 * blocked for more than specified max time.
556 free_locked_jcr(jcr); /* OK to free now cuz chain is locked */
557 if (jcr->JobId == 0) {
560 fd = jcr->store_bsock;
562 timer_start = fd->timer_start;
563 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
564 fd->timer_start = 0; /* turn off timer */
565 fd->timed_out = TRUE;
566 Jmsg(jcr, M_ERROR, 0, _(
567 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
568 watchdog_time - timer_start);
569 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
572 fd = jcr->file_bsock;
574 timer_start = fd->timer_start;
575 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
576 fd->timer_start = 0; /* turn off timer */
577 fd->timed_out = TRUE;
578 Jmsg(jcr, M_ERROR, 0, _(
579 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
580 watchdog_time - timer_start);
581 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
586 timer_start = fd->timer_start;
587 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
588 fd->timer_start = 0; /* turn off timer */
589 fd->timed_out = TRUE;
590 Jmsg(jcr, M_ERROR, 0, _(
591 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
592 watchdog_time - timer_start);
593 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
600 Dmsg0(200, "Finished JCR timeout checks\n");
604 * Timeout signal comes here
606 static void timeout_handler(int sig)
608 return; /* thus interrupting the function */