]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
6a283b6537b176e1dde827e87dd61803e5893f2d
[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 int num_jobs_run;
42 dlist *last_jobs = NULL;
43 #define MAX_LAST_JOBS 15
44
45 static JCR *jobs = NULL;              /* pointer to JCR chain */
46 static brwlock_t lock;                /* lock for last jobs and JCR chain */
47
48 void init_last_jobs_list()
49 {
50    int errstat;
51    struct s_last_job *job_entry = NULL;
52    if (!last_jobs) {
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"), 
56                strerror(errstat));
57       }
58    }
59
60 }
61
62 void term_last_jobs_list()
63 {
64    struct s_last_job *je;
65    if (last_jobs) {
66       foreach_dlist(je, last_jobs) {
67          free(je);                     
68       }
69       delete last_jobs;
70       last_jobs = NULL;
71       rwl_destroy(&lock);
72    }
73 }
74
75 void read_last_jobs_list(int fd, uint64_t addr)
76 {
77    struct s_last_job *je, job;
78    uint32_t num;
79
80    if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
81       return;
82    }
83    if (read(fd, &num, sizeof(num)) < 0) {
84       return;
85    }
86    if (num > 4 * MAX_LAST_JOBS) {  /* sanity check */
87       return;
88    }
89    for ( ; num; num--) {
90       if (read(fd, &job, sizeof(job)) < 0) {
91          return;
92       }
93       if (job.JobId > 0) {
94          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
95          memcpy((char *)je, (char *)&job, sizeof(job));
96          if (!last_jobs) {
97             init_last_jobs_list();
98          }
99          last_jobs->append(je);
100          if (last_jobs->size() > MAX_LAST_JOBS) {
101             last_jobs->remove(last_jobs->first());
102          }
103       }
104    }
105 }
106
107 uint64_t write_last_jobs_list(int fd, uint64_t addr)
108 {
109    struct s_last_job *je;
110    uint32_t num;
111
112    if (lseek(fd, addr, SEEK_SET) < 0) {
113       return 0;
114    }
115    if (last_jobs) {
116       /* First record is number of entires */
117       num = last_jobs->size();
118       if (write(fd, &num, sizeof(num)) < 0) {
119          return 0;
120       }
121       foreach_dlist(je, last_jobs) {
122          if (write(fd, je, sizeof(struct s_last_job)) < 0) {
123             return 0;
124          }
125       }
126    }
127    /* Return current address */
128    ssize_t stat = lseek(fd, 0, SEEK_CUR);
129    if (stat < 0) {
130       stat = 0;
131    }
132    return stat;
133       
134 }
135
136 void lock_last_jobs_list() 
137 {
138    /* Use jcr chain mutex */
139    lock_jcr_chain();
140 }
141
142 void unlock_last_jobs_list() 
143 {
144    /* Use jcr chain mutex */
145    unlock_jcr_chain();
146 }
147
148 /*
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
152  *  us the size.
153  */
154 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
155 {
156    JCR *jcr;
157    MQUEUE_ITEM *item = NULL;
158    struct sigaction sigtimer;
159
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 */
167    jcr->use_count = 1;
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);
173    jcr->errmsg[0] = 0;
174    strcpy(jcr->Job, "*Console*");     /* default */
175
176    sigtimer.sa_flags = 0;
177    sigtimer.sa_handler = timeout_handler;
178    sigfillset(&sigtimer.sa_mask);
179    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
180
181    lock_jcr_chain();
182    jcr->prev = NULL;
183    jcr->next = jobs;
184    if (jobs) {
185       jobs->prev = jcr;
186    }
187    jobs = jcr;
188    unlock_jcr_chain();
189    return jcr;
190 }
191
192
193 /*
194  * Remove a JCR from the chain
195  * NOTE! The chain must be locked prior to calling
196  *       this routine.
197  */
198 static void remove_jcr(JCR *jcr)
199 {
200    Dmsg0(150, "Enter remove_jcr\n");
201    if (!jcr) {
202       Emsg0(M_ABORT, 0, "NULL jcr.\n");
203    }
204    if (!jcr->prev) {                  /* if no prev */
205       jobs = jcr->next;               /* set new head */
206    } else {
207       jcr->prev->next = jcr->next;    /* update prev */
208    }
209    if (jcr->next) {
210       jcr->next->prev = jcr->prev;
211    }
212    Dmsg0(150, "Leave remove_jcr\n");
213 }
214
215 /*
216  * Free stuff common to all JCRs.  N.B. Be careful to include only
217  *  generic stuff in the common part of the jcr. 
218  */
219 static void free_common_jcr(JCR *jcr)
220 {
221    struct s_last_job *je, last_job;
222
223    /* Keep some statistics */
224    switch (jcr->JobType) {
225    case JT_BACKUP:
226    case JT_VERIFY:
227    case JT_RESTORE:
228    case JT_ADMIN:
229       num_jobs_run++;
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));
245          if (!last_jobs) {
246             init_last_jobs_list();
247          }
248          last_jobs->append(je);
249          if (last_jobs->size() > MAX_LAST_JOBS) {
250             last_jobs->remove(last_jobs->first());
251          }
252       }
253       break;
254    default:
255       break;
256    }
257    pthread_mutex_destroy(&jcr->mutex);
258
259    close_msg(jcr);                    /* close messages for this job */
260    delete jcr->msg_queue;
261
262    /* do this after closing messages */
263    if (jcr->client_name) {
264       free_pool_memory(jcr->client_name);
265       jcr->client_name = NULL;
266    }
267
268    if (jcr->sd_auth_key) {
269       free(jcr->sd_auth_key);
270       jcr->sd_auth_key = NULL;
271    }
272    if (jcr->VolumeName) {
273       free_pool_memory(jcr->VolumeName);
274       jcr->VolumeName = NULL;
275    }
276
277    if (jcr->dir_bsock) {
278       bnet_close(jcr->dir_bsock);
279       jcr->dir_bsock = NULL;
280    }
281    if (jcr->errmsg) {
282       free_pool_memory(jcr->errmsg);
283       jcr->errmsg = NULL;
284    }
285    if (jcr->where) {
286       free(jcr->where);
287       jcr->where = NULL;
288    }
289    if (jcr->cached_path) {
290       free_pool_memory(jcr->cached_path);
291       jcr->cached_path = NULL;
292       jcr->cached_pnl = 0;
293    }
294    free_getuser_cache();
295    free_getgroup_cache();
296    free(jcr);
297 }
298
299 /* 
300  * Global routine to free a jcr
301  */
302 #ifdef DEBUG
303 void b_free_jcr(const char *file, int line, JCR *jcr)
304 {
305    Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
306
307 #else
308
309 void free_jcr(JCR *jcr)
310 {
311
312    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
313
314 #endif
315
316    lock_jcr_chain();
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);
321    }
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 */
324       unlock_jcr_chain();
325       Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
326       return;
327    }
328    remove_jcr(jcr);
329
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 */
333    }
334
335    free_common_jcr(jcr);
336
337    close_msg(NULL);                   /* flush any daemon messages */
338    unlock_jcr_chain();
339    Dmsg0(200, "Exit free_jcr\n");
340 }
341
342
343 /* 
344  * Global routine to free a jcr
345  *  JCR chain is already locked
346  */
347 void free_locked_jcr(JCR *jcr)
348 {
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 */
352       return;
353    }
354    remove_jcr(jcr);
355    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
356    free_common_jcr(jcr);
357 }
358
359
360
361
362 /*
363  * Given a JobId, find the JCR      
364  *   Returns: jcr on success
365  *            NULL on failure
366  */
367 JCR *get_jcr_by_id(uint32_t JobId)
368 {
369    JCR *jcr;       
370
371    lock_jcr_chain();                    /* lock chain */
372    for (jcr = jobs; jcr; jcr=jcr->next) {
373       if (jcr->JobId == JobId) {
374          P(jcr->mutex);
375          jcr->use_count++;
376          V(jcr->mutex);
377          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
378          break;
379       }
380    }
381    unlock_jcr_chain();
382    return jcr; 
383 }
384
385 /*
386  * Given a SessionId and SessionTime, find the JCR      
387  *   Returns: jcr on success
388  *            NULL on failure
389  */
390 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
391 {
392    JCR *jcr;       
393
394    lock_jcr_chain();
395    for (jcr = jobs; jcr; jcr=jcr->next) {
396       if (jcr->VolSessionId == SessionId && 
397           jcr->VolSessionTime == SessionTime) {
398          P(jcr->mutex);
399          jcr->use_count++;
400          V(jcr->mutex);
401          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
402          break;
403       }
404    }
405    unlock_jcr_chain();
406    return jcr; 
407 }
408
409
410 /*
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
415  *            NULL on failure
416  */
417 JCR *get_jcr_by_partial_name(char *Job)
418 {
419    JCR *jcr;       
420    int len;
421
422    if (!Job) {
423       return NULL;
424    }
425    lock_jcr_chain();
426    len = strlen(Job);
427    for (jcr = jobs; jcr; jcr=jcr->next) {
428       if (strncmp(Job, jcr->Job, len) == 0) {
429          P(jcr->mutex);
430          jcr->use_count++;
431          V(jcr->mutex);
432          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
433          break;
434       }
435    }
436    unlock_jcr_chain();
437    return jcr; 
438 }
439
440
441 /*
442  * Given a Job, find the JCR      
443  *  requires an exact match of names.
444  *   Returns: jcr on success
445  *            NULL on failure
446  */
447 JCR *get_jcr_by_full_name(char *Job)
448 {
449    JCR *jcr;       
450
451    if (!Job) {
452       return NULL;
453    }
454    lock_jcr_chain();
455    for (jcr = jobs; jcr; jcr=jcr->next) {
456       if (strcmp(jcr->Job, Job) == 0) {
457          P(jcr->mutex);
458          jcr->use_count++;
459          V(jcr->mutex);
460          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
461          break;
462       }
463    }
464    unlock_jcr_chain();
465    return jcr; 
466 }
467
468 void set_jcr_job_status(JCR *jcr, int JobStatus)
469 {
470    /*
471     * For a set of errors, ... keep the current status
472     *   so it isn't lost. For all others, set it.
473     */
474    switch (jcr->JobStatus) {
475    case JS_ErrorTerminated:
476    case JS_Error:
477    case JS_FatalError:
478    case JS_Differences:
479    case JS_Canceled:
480       break;
481    default:
482       jcr->JobStatus = JobStatus;
483    }
484 }
485
486 /* 
487  * Lock the chain
488  */
489 void lock_jcr_chain()
490 {
491    int errstat;
492    if ((errstat=rwl_writelock(&lock)) != 0) {
493       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
494            strerror(errstat));
495    }
496 }
497
498 /*
499  * Unlock the chain
500  */
501 void unlock_jcr_chain()
502 {
503    int errstat;
504    if ((errstat=rwl_writeunlock(&lock)) != 0) {
505       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
506            strerror(errstat));
507    }
508 }
509
510
511 JCR *get_next_jcr(JCR *prev_jcr)
512 {
513    JCR *jcr;
514
515    if (prev_jcr == NULL) {
516       jcr = jobs;
517    } else {
518       jcr = prev_jcr->next;
519    }
520    if (jcr) {
521       P(jcr->mutex);
522       jcr->use_count++;
523       V(jcr->mutex);
524       Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
525    }
526    return jcr;
527 }
528
529 bool init_jcr_subsystem(void)
530 {
531    watchdog_t *wd = new_watchdog();
532
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;
537
538    register_watchdog(wd);
539
540    return true;
541 }
542
543 static void jcr_timeout_check(watchdog_t *self)
544 {
545    JCR *jcr;
546    BSOCK *fd;
547    time_t timer_start;
548
549    Dmsg0(400, "Start JCR timeout checks\n");
550
551    /* Walk through all JCRs checking if any one is 
552     * blocked for more than specified max time.
553     */
554    lock_jcr_chain();
555    foreach_jcr(jcr) {
556       free_locked_jcr(jcr);           /* OK to free now cuz chain is locked */
557       if (jcr->JobId == 0) {
558          continue;
559       }
560       fd = jcr->store_bsock;
561       if (fd) {
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);
570          }
571       }
572       fd = jcr->file_bsock;
573       if (fd) {
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);
582          }
583       }
584       fd = jcr->dir_bsock;
585       if (fd) {
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);
594          }
595       }
596
597    }
598    unlock_jcr_chain();
599
600    Dmsg0(200, "Finished JCR timeout checks\n");
601 }
602
603 /*
604  * Timeout signal comes here
605  */
606 static void timeout_handler(int sig)
607 {
608    return;                            /* thus interrupting the function */
609 }