]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
btape test and fill improvements + first cut Qmsg
[bacula/bacula] / bacula / src / lib / jcr.c
1 /*
2  * Manipulation routines for Job Control Records
3  *
4  *  Kern E. Sibbald, December 2000
5  *
6  *  Version $Id$
7  *
8  *  These routines are thread safe.
9  *
10  */
11 /*
12    Copyright (C) 2000-2003 Kern Sibbald and John Walker
13
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.
18
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.
23
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,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "jcr.h"
33
34 /* External variables we reference */
35 extern time_t watchdog_time;
36
37 /* Forward referenced functions */
38 static void timeout_handler(int sig);
39 static void jcr_timeout_check(watchdog_t *self);
40
41 struct s_last_job last_job;    /* last job run by this daemon */
42 dlist *last_jobs = NULL;
43 #define MAX_LAST_JOBS 10
44
45 static JCR *jobs = NULL;              /* pointer to JCR chain */
46
47 static brwlock_t lock;                /* lock for last jobs and JCR chain */
48
49 void init_last_jobs_list()
50 {
51    int errstat;
52    struct s_last_job *job_entry = NULL;
53    if (!last_jobs) {
54       last_jobs = new dlist(job_entry,  &job_entry->link);
55       memset(&last_job, 0, sizeof(last_job));
56    }
57    if ((errstat=rwl_init(&lock)) != 0) {
58       Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"), 
59             strerror(errstat));
60    }
61
62 }
63
64 void term_last_jobs_list()
65 {
66    char *je;
67    if (last_jobs) {
68       foreach_dlist(je, last_jobs) {
69          free(je);                     
70       }
71       delete last_jobs;
72       last_jobs = NULL;
73    }
74    rwl_destroy(&lock);
75 }
76
77 void lock_last_jobs_list() 
78 {
79    /* Use jcr chain mutex */
80    lock_jcr_chain();
81 }
82
83 void unlock_last_jobs_list() 
84 {
85    /* Use jcr chain mutex */
86    unlock_jcr_chain();
87 }
88
89 /*
90  * Create a Job Control Record and link it into JCR chain
91  * Returns newly allocated JCR
92  * Note, since each daemon has a different JCR, he passes
93  *  us the size.
94  */
95 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
96 {
97    JCR *jcr;
98    MQUEUE_ITEM *item = NULL;
99    struct sigaction sigtimer;
100
101    Dmsg0(200, "Enter new_jcr\n");
102    jcr = (JCR *)malloc(size);
103    memset(jcr, 0, size);
104    jcr->msg_queue = new dlist(item, &item->link);
105    jcr->my_thread_id = pthread_self();
106    jcr->sched_time = time(NULL);
107    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
108    jcr->use_count = 1;
109    pthread_mutex_init(&(jcr->mutex), NULL);
110    jcr->JobStatus = JS_Created;       /* ready to run */
111    jcr->VolumeName = get_pool_memory(PM_FNAME);
112    jcr->VolumeName[0] = 0;
113    jcr->errmsg = get_pool_memory(PM_MESSAGE);
114    jcr->errmsg[0] = 0;
115    strcpy(jcr->Job, "*Console*");     /* default */
116
117    sigtimer.sa_flags = 0;
118    sigtimer.sa_handler = timeout_handler;
119    sigfillset(&sigtimer.sa_mask);
120    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
121
122    lock_jcr_chain();
123    jcr->prev = NULL;
124    jcr->next = jobs;
125    if (jobs) {
126       jobs->prev = jcr;
127    }
128    jobs = jcr;
129    unlock_jcr_chain();
130    return jcr;
131 }
132
133
134 /*
135  * Remove a JCR from the chain
136  * NOTE! The chain must be locked prior to calling
137  *       this routine.
138  */
139 static void remove_jcr(JCR *jcr)
140 {
141    Dmsg0(150, "Enter remove_jcr\n");
142    if (!jcr) {
143       Emsg0(M_ABORT, 0, "NULL jcr.\n");
144    }
145    if (!jcr->prev) {                  /* if no prev */
146       jobs = jcr->next;               /* set new head */
147    } else {
148       jcr->prev->next = jcr->next;    /* update prev */
149    }
150    if (jcr->next) {
151       jcr->next->prev = jcr->prev;
152    }
153    Dmsg0(150, "Leave remove_jcr\n");
154 }
155
156 /*
157  * Free stuff common to all JCRs.  N.B. Be careful to include only
158  *  generic stuff in the common part of the jcr. 
159  */
160 static void free_common_jcr(JCR *jcr)
161 {
162    /* Keep some statistics */
163    switch (jcr->JobType) {
164    case JT_BACKUP:
165    case JT_VERIFY:
166    case JT_RESTORE:
167    case JT_ADMIN:
168       last_job.NumJobs++;
169       last_job.JobType = jcr->JobType;
170       last_job.JobId = jcr->JobId;
171       last_job.VolSessionId = jcr->VolSessionId;
172       last_job.VolSessionTime = jcr->VolSessionTime;
173       bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
174       last_job.JobFiles = jcr->JobFiles;
175       last_job.JobBytes = jcr->JobBytes;
176       last_job.JobStatus = jcr->JobStatus;
177       last_job.JobLevel = jcr->JobLevel;
178       last_job.start_time = jcr->start_time;
179       last_job.end_time = time(NULL);
180       break;
181    default:
182       break;
183    }
184    pthread_mutex_destroy(&jcr->mutex);
185
186    close_msg(jcr);                    /* close messages for this job */
187    delete jcr->msg_queue;
188
189    /* do this after closing messages */
190    if (jcr->client_name) {
191       free_pool_memory(jcr->client_name);
192       jcr->client_name = NULL;
193    }
194
195    if (jcr->sd_auth_key) {
196       free(jcr->sd_auth_key);
197       jcr->sd_auth_key = NULL;
198    }
199    if (jcr->VolumeName) {
200       free_pool_memory(jcr->VolumeName);
201       jcr->VolumeName = NULL;
202    }
203
204    if (jcr->dir_bsock) {
205       bnet_close(jcr->dir_bsock);
206       jcr->dir_bsock = NULL;
207    }
208    if (jcr->errmsg) {
209       free_pool_memory(jcr->errmsg);
210       jcr->errmsg = NULL;
211    }
212    if (jcr->where) {
213       free(jcr->where);
214       jcr->where = NULL;
215    }
216    if (jcr->cached_path) {
217       free_pool_memory(jcr->cached_path);
218       jcr->cached_path = NULL;
219       jcr->cached_pnl = 0;
220    }
221    free_getuser_cache();
222    free_getgroup_cache();
223    free(jcr);
224 }
225
226 /* 
227  * Global routine to free a jcr
228  */
229 #ifdef DEBUG
230 void b_free_jcr(char *file, int line, JCR *jcr)
231 {
232    Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
233
234 #else
235
236 void free_jcr(JCR *jcr)
237 {
238
239    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
240
241 #endif
242    struct s_last_job *je;
243
244    lock_jcr_chain();
245    jcr->use_count--;                  /* decrement use count */
246    Dmsg3(200, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
247    if (jcr->use_count > 0) {          /* if in use */
248       unlock_jcr_chain();
249       Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
250       return;
251    }
252    remove_jcr(jcr);
253
254    Dmsg1(200, "End job=%d\n", jcr->JobId);
255    if (jcr->daemon_free_jcr) {
256       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
257    }
258
259    free_common_jcr(jcr);
260
261    /* Keep list of last jobs, but not Console where JobId==0 */
262    if (last_job.JobId > 0) {
263       je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
264       memcpy((char *)je, (char *)&last_job, sizeof(last_job));
265       if (!last_jobs) {
266          init_last_jobs_list();
267       }
268       last_jobs->append(je);
269       if (last_jobs->size() > MAX_LAST_JOBS) {
270          last_jobs->remove(last_jobs->first());
271       }
272       last_job.JobId = 0;             /* zap last job */
273    }
274    close_msg(NULL);                   /* flush any daemon messages */
275    unlock_jcr_chain();
276    Dmsg0(200, "Exit free_jcr\n");
277 }
278
279
280 /* 
281  * Global routine to free a jcr
282  *  JCR chain is already locked
283  */
284 void free_locked_jcr(JCR *jcr)
285 {
286    jcr->use_count--;                  /* decrement use count */
287    Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
288    if (jcr->use_count > 0) {          /* if in use */
289       return;
290    }
291    remove_jcr(jcr);
292    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
293    free_common_jcr(jcr);
294 }
295
296
297
298
299 /*
300  * Given a JobId, find the JCR      
301  *   Returns: jcr on success
302  *            NULL on failure
303  */
304 JCR *get_jcr_by_id(uint32_t JobId)
305 {
306    JCR *jcr;       
307
308    lock_jcr_chain();                    /* lock chain */
309    for (jcr = jobs; jcr; jcr=jcr->next) {
310       if (jcr->JobId == JobId) {
311          P(jcr->mutex);
312          jcr->use_count++;
313          V(jcr->mutex);
314          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
315          break;
316       }
317    }
318    unlock_jcr_chain();
319    return jcr; 
320 }
321
322 /*
323  * Given a SessionId and SessionTime, find the JCR      
324  *   Returns: jcr on success
325  *            NULL on failure
326  */
327 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
328 {
329    JCR *jcr;       
330
331    lock_jcr_chain();
332    for (jcr = jobs; jcr; jcr=jcr->next) {
333       if (jcr->VolSessionId == SessionId && 
334           jcr->VolSessionTime == SessionTime) {
335          P(jcr->mutex);
336          jcr->use_count++;
337          V(jcr->mutex);
338          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
339          break;
340       }
341    }
342    unlock_jcr_chain();
343    return jcr; 
344 }
345
346
347 /*
348  * Given a Job, find the JCR      
349  *  compares on the number of characters in Job
350  *  thus allowing partial matches.
351  *   Returns: jcr on success
352  *            NULL on failure
353  */
354 JCR *get_jcr_by_partial_name(char *Job)
355 {
356    JCR *jcr;       
357    int len;
358
359    if (!Job) {
360       return NULL;
361    }
362    lock_jcr_chain();
363    len = strlen(Job);
364    for (jcr = jobs; jcr; jcr=jcr->next) {
365       if (strncmp(Job, jcr->Job, len) == 0) {
366          P(jcr->mutex);
367          jcr->use_count++;
368          V(jcr->mutex);
369          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
370          break;
371       }
372    }
373    unlock_jcr_chain();
374    return jcr; 
375 }
376
377
378 /*
379  * Given a Job, find the JCR      
380  *  requires an exact match of names.
381  *   Returns: jcr on success
382  *            NULL on failure
383  */
384 JCR *get_jcr_by_full_name(char *Job)
385 {
386    JCR *jcr;       
387
388    if (!Job) {
389       return NULL;
390    }
391    lock_jcr_chain();
392    for (jcr = jobs; jcr; jcr=jcr->next) {
393       if (strcmp(jcr->Job, Job) == 0) {
394          P(jcr->mutex);
395          jcr->use_count++;
396          V(jcr->mutex);
397          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
398          break;
399       }
400    }
401    unlock_jcr_chain();
402    return jcr; 
403 }
404
405 void set_jcr_job_status(JCR *jcr, int JobStatus)
406 {
407    /*
408     * For a set of errors, ... keep the current status
409     *   so it isn't lost. For all others, set it.
410     */
411    switch (jcr->JobStatus) {
412    case JS_ErrorTerminated:
413    case JS_Error:
414    case JS_FatalError:
415    case JS_Differences:
416    case JS_Canceled:
417       break;
418    default:
419       jcr->JobStatus = JobStatus;
420    }
421 }
422
423 /* 
424  * Lock the chain
425  */
426 void lock_jcr_chain()
427 {
428    int errstat;
429    if ((errstat=rwl_writelock(&lock)) != 0) {
430       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
431            strerror(errstat));
432    }
433 }
434
435 /*
436  * Unlock the chain
437  */
438 void unlock_jcr_chain()
439 {
440    int errstat;
441    if ((errstat=rwl_writeunlock(&lock)) != 0) {
442       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
443            strerror(errstat));
444    }
445 }
446
447
448 JCR *get_next_jcr(JCR *prev_jcr)
449 {
450    JCR *jcr;
451
452    if (prev_jcr == NULL) {
453       jcr = jobs;
454    } else {
455       jcr = prev_jcr->next;
456    }
457    if (jcr) {
458       P(jcr->mutex);
459       jcr->use_count++;
460       V(jcr->mutex);
461       Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
462    }
463    return jcr;
464 }
465
466 bool init_jcr_subsystem(void)
467 {
468    watchdog_t *wd = new_watchdog();
469
470    wd->one_shot = false;
471    wd->interval = 30;   /* FIXME: should be configurable somewhere, even
472                          if only with a #define */
473    wd->callback = jcr_timeout_check;
474
475    register_watchdog(wd);
476
477    return true;
478 }
479
480 static void jcr_timeout_check(watchdog_t *self)
481 {
482    JCR *jcr;
483    BSOCK *fd;
484    time_t timer_start;
485
486    Dmsg0(400, "Start JCR timeout checks\n");
487
488    /* Walk through all JCRs checking if any one is 
489     * blocked for more than specified max time.
490     */
491    lock_jcr_chain();
492    foreach_jcr(jcr) {
493       free_locked_jcr(jcr);           /* OK to free now cuz chain is locked */
494       if (jcr->JobId == 0) {
495          continue;
496       }
497       fd = jcr->store_bsock;
498       if (fd) {
499          timer_start = fd->timer_start;
500          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
501             fd->timer_start = 0;      /* turn off timer */
502             fd->timed_out = TRUE;
503             Jmsg(jcr, M_ERROR, 0, _(
504 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
505                  watchdog_time - timer_start);
506             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
507          }
508       }
509       fd = jcr->file_bsock;
510       if (fd) {
511          timer_start = fd->timer_start;
512          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
513             fd->timer_start = 0;      /* turn off timer */
514             fd->timed_out = TRUE;
515             Jmsg(jcr, M_ERROR, 0, _(
516 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
517                  watchdog_time - timer_start);
518             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
519          }
520       }
521       fd = jcr->dir_bsock;
522       if (fd) {
523          timer_start = fd->timer_start;
524          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
525             fd->timer_start = 0;      /* turn off timer */
526             fd->timed_out = TRUE;
527             Jmsg(jcr, M_ERROR, 0, _(
528 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
529                  watchdog_time - timer_start);
530             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
531          }
532       }
533
534    }
535    unlock_jcr_chain();
536
537    Dmsg0(200, "Finished JCR timeout checks\n");
538 }
539
540 /*
541  * Timeout signal comes here
542  */
543 void timeout_handler(int sig)
544 {
545    return;                            /* thus interrupting the function */
546 }