]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
misc minor updates
[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 static const int max_last_jobs = 10;
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    Dmsg1(010, "read_last_jobs seek to %d\n", (int)addr);
81    if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) {
82       return;
83    }
84    if (read(fd, &num, sizeof(num)) != sizeof(num)) {
85       return;
86    }
87    Dmsg1(010, "Read num_items=%d\n", num);
88    if (num > 4 * max_last_jobs) {  /* sanity check */
89       return;
90    }
91    for ( ; num; num--) {
92       if (read(fd, &job, sizeof(job)) != sizeof(job)) {
93          Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
94          return;
95       }
96       if (job.JobId > 0) {
97          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
98          memcpy((char *)je, (char *)&job, sizeof(job));
99          if (!last_jobs) {
100             init_last_jobs_list();
101          }
102          last_jobs->append(je);
103          if (last_jobs->size() > max_last_jobs) {
104             last_jobs->remove(last_jobs->first());
105          }
106       }
107    }
108 }
109
110 uint64_t write_last_jobs_list(int fd, uint64_t addr)
111 {
112    struct s_last_job *je;
113    uint32_t num;
114
115    Dmsg1(010, "write_last_jobs seek to %d\n", (int)addr);
116    if (lseek(fd, addr, SEEK_SET) < 0) {
117       return 0;
118    }
119    if (last_jobs) {
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));
124          return 0;
125       }
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));
129             return 0;
130          }
131       }
132    }
133    /* Return current address */
134    ssize_t stat = lseek(fd, 0, SEEK_CUR);
135    if (stat < 0) {
136       stat = 0;
137    }
138    return stat;
139       
140 }
141
142 void lock_last_jobs_list() 
143 {
144    /* Use jcr chain mutex */
145    lock_jcr_chain();
146 }
147
148 void unlock_last_jobs_list() 
149 {
150    /* Use jcr chain mutex */
151    unlock_jcr_chain();
152 }
153
154 /*
155  * Push a subroutine address into the job end callback stack
156  */
157 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr))
158 {
159    jcr->job_end_push.prepend((void *)job_end_cb);
160 }
161
162 /* Pop each job_end subroutine and call it */
163 static void job_end_pop(JCR *jcr)
164 {
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);
168       job_end_cb(jcr);
169    }
170 }
171
172 /*
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
176  *  us the size.
177  */
178 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
179 {
180    JCR *jcr;
181    MQUEUE_ITEM *item = NULL;
182    struct sigaction sigtimer;
183
184    Dmsg0(200, "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 */
192    jcr->use_count = 1;
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);
198    jcr->errmsg[0] = 0;
199    /* Setup some dummy values */
200    jcr->Job[0] = 0;                   /* no job name by default */
201    jcr->JobId = 0;
202    jcr->JobType = JT_ADMIN;
203    jcr->JobLevel = L_NONE;
204    jcr->JobStatus = JS_Created;
205
206    sigtimer.sa_flags = 0;
207    sigtimer.sa_handler = timeout_handler;
208    sigfillset(&sigtimer.sa_mask);
209    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
210
211    lock_jcr_chain();
212    jcr->prev = NULL;
213    jcr->next = jobs;
214    if (jobs) {
215       jobs->prev = jcr;
216    }
217    jobs = jcr;
218    unlock_jcr_chain();
219    return jcr;
220 }
221
222
223 /*
224  * Remove a JCR from the chain
225  * NOTE! The chain must be locked prior to calling
226  *       this routine.
227  */
228 static void remove_jcr(JCR *jcr)
229 {
230    Dmsg0(150, "Enter remove_jcr\n");
231    if (!jcr) {
232       Emsg0(M_ABORT, 0, "NULL jcr.\n");
233    }
234    if (!jcr->prev) {                  /* if no prev */
235       jobs = jcr->next;               /* set new head */
236    } else {
237       jcr->prev->next = jcr->next;    /* update prev */
238    }
239    if (jcr->next) {
240       jcr->next->prev = jcr->prev;
241    }
242    Dmsg0(150, "Leave remove_jcr\n");
243 }
244
245 /*
246  * Free stuff common to all JCRs.  N.B. Be careful to include only
247  *  generic stuff in the common part of the jcr. 
248  */
249 static void free_common_jcr(JCR *jcr)
250 {
251    struct s_last_job *je, last_job;
252
253    /* Keep some statistics */
254    switch (jcr->JobType) {
255    case JT_BACKUP:
256    case JT_VERIFY:
257    case JT_RESTORE:
258    case JT_ADMIN:
259       num_jobs_run++;
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));
275          if (!last_jobs) {
276             init_last_jobs_list();
277          }
278          last_jobs->append(je);
279          if (last_jobs->size() > max_last_jobs) {
280             last_jobs->remove(last_jobs->first());
281          }
282       }
283       break;
284    default:
285       break;
286    }
287    pthread_mutex_destroy(&jcr->mutex);
288
289    close_msg(jcr);                    /* close messages for this job */
290    delete jcr->msg_queue;
291
292    /* do this after closing messages */
293    if (jcr->client_name) {
294       free_pool_memory(jcr->client_name);
295       jcr->client_name = NULL;
296    }
297
298    if (jcr->sd_auth_key) {
299       free(jcr->sd_auth_key);
300       jcr->sd_auth_key = NULL;
301    }
302    if (jcr->VolumeName) {
303       free_pool_memory(jcr->VolumeName);
304       jcr->VolumeName = NULL;
305    }
306
307    if (jcr->dir_bsock) {
308       bnet_close(jcr->dir_bsock);
309       jcr->dir_bsock = NULL;
310    }
311    if (jcr->errmsg) {
312       free_pool_memory(jcr->errmsg);
313       jcr->errmsg = NULL;
314    }
315    if (jcr->where) {
316       free(jcr->where);
317       jcr->where = NULL;
318    }
319    if (jcr->cached_path) {
320       free_pool_memory(jcr->cached_path);
321       jcr->cached_path = NULL;
322       jcr->cached_pnl = 0;
323    }
324    free_getuser_cache();
325    free_getgroup_cache();
326    free(jcr);
327 }
328
329 /* 
330  * Global routine to free a jcr
331  */
332 #ifdef DEBUG
333 void b_free_jcr(const char *file, int line, JCR *jcr)
334 {
335    Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
336
337 #else
338
339 void free_jcr(JCR *jcr)
340 {
341
342    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
343
344 #endif
345
346    lock_jcr_chain();
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);
351    }
352    Dmsg3(200, "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 */
354       unlock_jcr_chain();
355       Dmsg2(200, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
356       return;
357    }
358    remove_jcr(jcr);
359    job_end_pop(jcr);                  /* pop and call hooked routines */
360
361    Dmsg1(200, "End job=%d\n", jcr->JobId);
362    if (jcr->daemon_free_jcr) {
363       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
364    }
365
366    free_common_jcr(jcr);
367
368    close_msg(NULL);                   /* flush any daemon messages */
369    unlock_jcr_chain();
370    Dmsg0(200, "Exit free_jcr\n");
371 }
372
373
374 /* 
375  * Global routine to free a jcr
376  *  JCR chain is already locked
377  */
378 void free_locked_jcr(JCR *jcr)
379 {
380    jcr->use_count--;                  /* decrement use count */
381    Dmsg2(200, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
382    if (jcr->use_count > 0) {          /* if in use */
383       return;
384    }
385    remove_jcr(jcr);
386    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
387    free_common_jcr(jcr);
388 }
389
390
391
392 /*
393  * Given a JobId, find the JCR      
394  *   Returns: jcr on success
395  *            NULL on failure
396  */
397 JCR *get_jcr_by_id(uint32_t JobId)
398 {
399    JCR *jcr;       
400
401    lock_jcr_chain();                    /* lock chain */
402    for (jcr = jobs; jcr; jcr=jcr->next) {
403       if (jcr->JobId == JobId) {
404          P(jcr->mutex);
405          jcr->use_count++;
406          V(jcr->mutex);
407          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
408          break;
409       }
410    }
411    unlock_jcr_chain();
412    return jcr; 
413 }
414
415 /*
416  * Given a SessionId and SessionTime, find the JCR      
417  *   Returns: jcr on success
418  *            NULL on failure
419  */
420 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
421 {
422    JCR *jcr;       
423
424    lock_jcr_chain();
425    for (jcr = jobs; jcr; jcr=jcr->next) {
426       if (jcr->VolSessionId == SessionId && 
427           jcr->VolSessionTime == SessionTime) {
428          P(jcr->mutex);
429          jcr->use_count++;
430          V(jcr->mutex);
431          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
432          break;
433       }
434    }
435    unlock_jcr_chain();
436    return jcr; 
437 }
438
439
440 /*
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
445  *            NULL on failure
446  */
447 JCR *get_jcr_by_partial_name(char *Job)
448 {
449    JCR *jcr;       
450    int len;
451
452    if (!Job) {
453       return NULL;
454    }
455    lock_jcr_chain();
456    len = strlen(Job);
457    for (jcr = jobs; jcr; jcr=jcr->next) {
458       if (strncmp(Job, jcr->Job, len) == 0) {
459          P(jcr->mutex);
460          jcr->use_count++;
461          V(jcr->mutex);
462          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
463          break;
464       }
465    }
466    unlock_jcr_chain();
467    return jcr; 
468 }
469
470
471 /*
472  * Given a Job, find the JCR      
473  *  requires an exact match of names.
474  *   Returns: jcr on success
475  *            NULL on failure
476  */
477 JCR *get_jcr_by_full_name(char *Job)
478 {
479    JCR *jcr;       
480
481    if (!Job) {
482       return NULL;
483    }
484    lock_jcr_chain();
485    for (jcr = jobs; jcr; jcr=jcr->next) {
486       if (strcmp(jcr->Job, Job) == 0) {
487          P(jcr->mutex);
488          jcr->use_count++;
489          V(jcr->mutex);
490          Dmsg2(200, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
491          break;
492       }
493    }
494    unlock_jcr_chain();
495    return jcr; 
496 }
497
498 void set_jcr_job_status(JCR *jcr, int JobStatus)
499 {
500    /*
501     * For a set of errors, ... keep the current status
502     *   so it isn't lost. For all others, set it.
503     */
504    switch (jcr->JobStatus) {
505    case JS_ErrorTerminated:
506    case JS_Error:
507    case JS_FatalError:
508    case JS_Differences:
509    case JS_Canceled:
510       break;
511    default:
512       jcr->JobStatus = JobStatus;
513    }
514 }
515
516 /* 
517  * Lock the chain
518  */
519 void lock_jcr_chain()
520 {
521    int errstat;
522    if ((errstat=rwl_writelock(&lock)) != 0) {
523       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
524            strerror(errstat));
525    }
526 }
527
528 /*
529  * Unlock the chain
530  */
531 void unlock_jcr_chain()
532 {
533    int errstat;
534    if ((errstat=rwl_writeunlock(&lock)) != 0) {
535       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
536            strerror(errstat));
537    }
538 }
539
540
541 JCR *get_next_jcr(JCR *prev_jcr)
542 {
543    JCR *jcr;
544
545    if (prev_jcr == NULL) {
546       jcr = jobs;
547    } else {
548       jcr = prev_jcr->next;
549    }
550    if (jcr) {
551       P(jcr->mutex);
552       jcr->use_count++;
553       V(jcr->mutex);
554       Dmsg2(200, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
555    }
556    return jcr;
557 }
558
559 bool init_jcr_subsystem(void)
560 {
561    watchdog_t *wd = new_watchdog();
562
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;
567
568    register_watchdog(wd);
569
570    return true;
571 }
572
573 static void jcr_timeout_check(watchdog_t *self)
574 {
575    JCR *jcr;
576    BSOCK *fd;
577    time_t timer_start;
578
579    Dmsg0(400, "Start JCR timeout checks\n");
580
581    /* Walk through all JCRs checking if any one is 
582     * blocked for more than specified max time.
583     */
584    lock_jcr_chain();
585    foreach_jcr(jcr) {
586       free_locked_jcr(jcr);           /* OK to free now cuz chain is locked */
587       if (jcr->JobId == 0) {
588          continue;
589       }
590       fd = jcr->store_bsock;
591       if (fd) {
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);
600          }
601       }
602       fd = jcr->file_bsock;
603       if (fd) {
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);
612          }
613       }
614       fd = jcr->dir_bsock;
615       if (fd) {
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);
624          }
625       }
626
627    }
628    unlock_jcr_chain();
629
630    Dmsg0(200, "Finished JCR timeout checks\n");
631 }
632
633 /*
634  * Timeout signal comes here
635  */
636 static void timeout_handler(int sig)
637 {
638    return;                            /* thus interrupting the function */
639 }