]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
- Add index file to JobId field of File records for PostgreSQL.
[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-2004 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 extern "C" void timeout_handler(int sig);
39
40 static void jcr_timeout_check(watchdog_t *self);
41
42 int num_jobs_run;
43 dlist *last_jobs = NULL;
44 const int max_last_jobs = 10;
45
46 static JCR *jobs = NULL;              /* pointer to JCR chain */
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       if ((errstat=rwl_init(&lock)) != 0) {
56          Emsg1(M_ABORT, 0, _("Unable to initialize jcr_chain lock. ERR=%s\n"), 
57                strerror(errstat));
58       }
59    }
60
61 }
62
63 void term_last_jobs_list()
64 {
65    if (last_jobs) {
66       while (!last_jobs->empty()) {
67          void *je = last_jobs->first(); 
68          last_jobs->remove(je);
69          free(je);     
70       }
71       delete last_jobs;
72       last_jobs = NULL;
73       rwl_destroy(&lock);
74    }
75 }
76
77 void read_last_jobs_list(int fd, uint64_t addr)
78 {
79    struct s_last_job *je, job;
80    uint32_t num;
81
82    Dmsg1(100, "read_last_jobs seek to %d\n", (int)addr);
83    if (addr == 0 || lseek(fd, (off_t)addr, SEEK_SET) < 0) {
84       return;
85    }
86    if (read(fd, &num, sizeof(num)) != sizeof(num)) {
87       return;
88    }
89    Dmsg1(100, "Read num_items=%d\n", num);
90    if (num > 4 * max_last_jobs) {  /* sanity check */
91       return;
92    }
93    for ( ; num; num--) {
94       if (read(fd, &job, sizeof(job)) != sizeof(job)) {
95          Dmsg1(000, "Read job entry. ERR=%s\n", strerror(errno));
96          return;
97       }
98       if (job.JobId > 0) {
99          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
100          memcpy((char *)je, (char *)&job, sizeof(job));
101          if (!last_jobs) {
102             init_last_jobs_list();
103          }
104          last_jobs->append(je);
105          if (last_jobs->size() > max_last_jobs) {
106             je = (struct s_last_job *)last_jobs->first();
107             last_jobs->remove(je);
108             free(je);
109          }
110       }
111    }
112 }
113
114 uint64_t write_last_jobs_list(int fd, uint64_t addr)
115 {
116    struct s_last_job *je;
117    uint32_t num;
118
119    Dmsg1(100, "write_last_jobs seek to %d\n", (int)addr);
120    if (lseek(fd, (off_t)addr, SEEK_SET) < 0) {
121       return 0;
122    }
123    if (last_jobs) {
124       /* First record is number of entires */
125       num = last_jobs->size();
126       if (write(fd, &num, sizeof(num)) != sizeof(num)) {
127          Dmsg1(000, "Error writing num_items: ERR=%s\n", strerror(errno));
128          return 0;
129       }
130       foreach_dlist(je, last_jobs) {
131          if (write(fd, je, sizeof(struct s_last_job)) != sizeof(struct s_last_job)) {
132             Dmsg1(000, "Error writing job: ERR=%s\n", strerror(errno));
133             return 0;
134          }
135       }
136    }
137    /* Return current address */
138    ssize_t stat = lseek(fd, 0, SEEK_CUR);
139    if (stat < 0) {
140       stat = 0;
141    }
142    return stat;
143       
144 }
145
146 void lock_last_jobs_list() 
147 {
148    /* Use jcr chain mutex */
149    lock_jcr_chain();
150 }
151
152 void unlock_last_jobs_list() 
153 {
154    /* Use jcr chain mutex */
155    unlock_jcr_chain();
156 }
157
158 /*
159  * Push a subroutine address into the job end callback stack
160  */
161 void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx)
162 {
163    jcr->job_end_push.append((void *)job_end_cb);
164    jcr->job_end_push.append(ctx);
165 }
166
167 /* Pop each job_end subroutine and call it */
168 static void job_end_pop(JCR *jcr)
169 {
170    void (*job_end_cb)(JCR *jcr, void *ctx);
171    void *ctx;
172    for (int i=jcr->job_end_push.size()-1; i > 0; ) {
173       ctx = jcr->job_end_push.get(i--);
174       job_end_cb = (void (*)(JCR *,void *))jcr->job_end_push.get(i--);
175       job_end_cb(jcr, ctx);
176    }
177 }
178
179 /*
180  * Create a Job Control Record and link it into JCR chain
181  * Returns newly allocated JCR
182  * Note, since each daemon has a different JCR, he passes
183  *  us the size.
184  */
185 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
186 {
187    JCR *jcr;
188    MQUEUE_ITEM *item = NULL;
189    struct sigaction sigtimer;
190
191    Dmsg0(400, "Enter new_jcr\n");
192    jcr = (JCR *)malloc(size);
193    memset(jcr, 0, size);
194    jcr->msg_queue = New(dlist(item, &item->link));
195    jcr->job_end_push.init(1, false);
196    jcr->sched_time = time(NULL);
197    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
198    jcr->use_count = 1;
199    pthread_mutex_init(&(jcr->mutex), NULL);
200    jcr->JobStatus = JS_Created;       /* ready to run */
201    jcr->VolumeName = get_pool_memory(PM_FNAME);
202    jcr->VolumeName[0] = 0;
203    jcr->errmsg = get_pool_memory(PM_MESSAGE);
204    jcr->errmsg[0] = 0;
205    /* Setup some dummy values */
206    jcr->Job[0] = 0;                   /* no job name by default */
207    jcr->JobId = 0;
208    jcr->JobType = JT_SYSTEM;          /* internal job until defined */
209    jcr->JobLevel = L_NONE;
210    jcr->JobStatus = JS_Created;
211
212    sigtimer.sa_flags = 0;
213    sigtimer.sa_handler = timeout_handler;
214    sigfillset(&sigtimer.sa_mask);
215    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
216
217    lock_jcr_chain();
218    jcr->prev = NULL;
219    jcr->next = jobs;
220    if (jobs) {
221       jobs->prev = jcr;
222    }
223    jobs = jcr;
224    unlock_jcr_chain();
225    return jcr;
226 }
227
228
229 /*
230  * Remove a JCR from the chain
231  * NOTE! The chain must be locked prior to calling
232  *       this routine.
233  */
234 static void remove_jcr(JCR *jcr)
235 {
236    Dmsg0(400, "Enter remove_jcr\n");
237    if (!jcr) {
238       Emsg0(M_ABORT, 0, "NULL jcr.\n");
239    }
240    if (!jcr->prev) {                  /* if no prev */
241       jobs = jcr->next;               /* set new head */
242    } else {
243       jcr->prev->next = jcr->next;    /* update prev */
244    }
245    if (jcr->next) {
246       jcr->next->prev = jcr->prev;
247    }
248    Dmsg0(400, "Leave remove_jcr\n");
249 }
250
251 /*
252  * Free stuff common to all JCRs.  N.B. Be careful to include only
253  *  generic stuff in the common part of the jcr. 
254  */
255 static void free_common_jcr(JCR *jcr)
256 {
257    struct s_last_job *je, last_job;
258
259    /* Keep some statistics */
260    switch (jcr->JobType) {
261    case JT_BACKUP:
262    case JT_VERIFY:
263    case JT_RESTORE:
264    case JT_ADMIN:
265       num_jobs_run++;
266       last_job.Errors = jcr->Errors;
267       last_job.JobType = jcr->JobType;
268       last_job.JobId = jcr->JobId;
269       last_job.VolSessionId = jcr->VolSessionId;
270       last_job.VolSessionTime = jcr->VolSessionTime;
271       bstrncpy(last_job.Job, jcr->Job, sizeof(last_job.Job));
272       last_job.JobFiles = jcr->JobFiles;
273       last_job.JobBytes = jcr->JobBytes;
274       last_job.JobStatus = jcr->JobStatus;
275       last_job.JobLevel = jcr->JobLevel;
276       last_job.start_time = jcr->start_time;
277       last_job.end_time = time(NULL);
278       /* Keep list of last jobs, but not Console where JobId==0 */
279       if (last_job.JobId > 0) {
280          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
281          memcpy((char *)je, (char *)&last_job, sizeof(last_job));
282          if (!last_jobs) {
283             init_last_jobs_list();
284          }
285          last_jobs->append(je);
286          if (last_jobs->size() > max_last_jobs) {
287             je = (struct s_last_job *)last_jobs->first();
288             last_jobs->remove(je);
289             free(je);
290          }
291       }
292       break;
293    default:
294       break;
295    }
296    pthread_mutex_destroy(&jcr->mutex);
297
298    delete jcr->msg_queue;
299    close_msg(jcr);                    /* close messages for this job */
300
301    /* do this after closing messages */
302    if (jcr->client_name) {
303       free_pool_memory(jcr->client_name);
304       jcr->client_name = NULL;
305    }
306
307    if (jcr->sd_auth_key) {
308       free(jcr->sd_auth_key);
309       jcr->sd_auth_key = NULL;
310    }
311    if (jcr->VolumeName) {
312       free_pool_memory(jcr->VolumeName);
313       jcr->VolumeName = NULL;
314    }
315
316    if (jcr->dir_bsock) {
317       bnet_close(jcr->dir_bsock);
318       jcr->dir_bsock = NULL;
319    }
320    if (jcr->errmsg) {
321       free_pool_memory(jcr->errmsg);
322       jcr->errmsg = NULL;
323    }
324    if (jcr->where) {
325       free(jcr->where);
326       jcr->where = NULL;
327    }
328    if (jcr->cached_path) {
329       free_pool_memory(jcr->cached_path);
330       jcr->cached_path = NULL;
331       jcr->cached_pnl = 0;
332    }
333    free_getuser_cache();
334    free_getgroup_cache();
335    free(jcr);
336 }
337
338 /* 
339  * Global routine to free a jcr
340  */
341 #ifdef DEBUG
342 void b_free_jcr(const char *file, int line, JCR *jcr)
343 {
344    Dmsg3(400, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
345
346 #else
347
348 void free_jcr(JCR *jcr)
349 {
350
351    Dmsg1(400, "Enter free_jcr 0x%x\n", jcr);
352
353 #endif
354
355    dequeue_messages(jcr);
356    lock_jcr_chain();
357    jcr->use_count--;                  /* decrement use count */
358    if (jcr->use_count < 0) {
359       Emsg2(M_ERROR, 0, _("JCR use_count=%d JobId=%d\n"),
360          jcr->use_count, jcr->JobId);
361    }
362    Dmsg3(400, "Dec free_jcr 0x%x use_count=%d jobid=%d\n", jcr, jcr->use_count, jcr->JobId);
363    if (jcr->use_count > 0) {          /* if in use */
364       unlock_jcr_chain();
365       Dmsg2(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
366       return;
367    }
368    remove_jcr(jcr);                   /* remove Jcr from chain */
369    unlock_jcr_chain();
370
371    job_end_pop(jcr);                  /* pop and call hooked routines */
372
373    Dmsg1(400, "End job=%d\n", jcr->JobId);
374    if (jcr->daemon_free_jcr) {
375       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
376    }
377    free_common_jcr(jcr);
378    close_msg(NULL);                   /* flush any daemon messages */
379    Dmsg0(400, "Exit free_jcr\n");
380 }
381
382
383 /* 
384  * Global routine to free a jcr
385  *  JCR chain is already locked
386  */
387 void free_locked_jcr(JCR *jcr)
388 {
389    jcr->use_count--;                  /* decrement use count */
390    Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
391    if (jcr->use_count > 0) {          /* if in use */
392       return;
393    }
394    remove_jcr(jcr);
395    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
396    free_common_jcr(jcr);
397 }
398
399
400
401 /*
402  * Given a JobId, find the JCR      
403  *   Returns: jcr on success
404  *            NULL on failure
405  */
406 JCR *get_jcr_by_id(uint32_t JobId)
407 {
408    JCR *jcr;       
409
410    lock_jcr_chain();                    /* lock chain */
411    for (jcr = jobs; jcr; jcr=jcr->next) {
412       if (jcr->JobId == JobId) {
413          P(jcr->mutex);
414          jcr->use_count++;
415          V(jcr->mutex);
416          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
417          break;
418       }
419    }
420    unlock_jcr_chain();
421    return jcr; 
422 }
423
424 /*
425  * Given a SessionId and SessionTime, find the JCR      
426  *   Returns: jcr on success
427  *            NULL on failure
428  */
429 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
430 {
431    JCR *jcr;       
432
433    lock_jcr_chain();
434    for (jcr = jobs; jcr; jcr=jcr->next) {
435       if (jcr->VolSessionId == SessionId && 
436           jcr->VolSessionTime == SessionTime) {
437          P(jcr->mutex);
438          jcr->use_count++;
439          V(jcr->mutex);
440          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
441          break;
442       }
443    }
444    unlock_jcr_chain();
445    return jcr; 
446 }
447
448
449 /*
450  * Given a Job, find the JCR      
451  *  compares on the number of characters in Job
452  *  thus allowing partial matches.
453  *   Returns: jcr on success
454  *            NULL on failure
455  */
456 JCR *get_jcr_by_partial_name(char *Job)
457 {
458    JCR *jcr;       
459    int len;
460
461    if (!Job) {
462       return NULL;
463    }
464    lock_jcr_chain();
465    len = strlen(Job);
466    for (jcr = jobs; jcr; jcr=jcr->next) {
467       if (strncmp(Job, jcr->Job, len) == 0) {
468          P(jcr->mutex);
469          jcr->use_count++;
470          V(jcr->mutex);
471          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
472          break;
473       }
474    }
475    unlock_jcr_chain();
476    return jcr; 
477 }
478
479
480 /*
481  * Given a Job, find the JCR      
482  *  requires an exact match of names.
483  *   Returns: jcr on success
484  *            NULL on failure
485  */
486 JCR *get_jcr_by_full_name(char *Job)
487 {
488    JCR *jcr;       
489
490    if (!Job) {
491       return NULL;
492    }
493    lock_jcr_chain();
494    for (jcr = jobs; jcr; jcr=jcr->next) {
495       if (strcmp(jcr->Job, Job) == 0) {
496          P(jcr->mutex);
497          jcr->use_count++;
498          V(jcr->mutex);
499          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
500          break;
501       }
502    }
503    unlock_jcr_chain();
504    return jcr; 
505 }
506
507 void set_jcr_job_status(JCR *jcr, int JobStatus)
508 {
509    /*
510     * For a set of errors, ... keep the current status
511     *   so it isn't lost. For all others, set it.
512     */
513    switch (jcr->JobStatus) {
514    case JS_ErrorTerminated:
515    case JS_Error:
516    case JS_FatalError:
517    case JS_Differences:
518    case JS_Canceled:
519       break;
520    default:
521       jcr->JobStatus = JobStatus;
522    }
523 }
524
525 #ifdef TRACE_JCR_CHAIN
526 static int lock_count = 0;
527 #endif
528
529 /* 
530  * Lock the chain
531  */
532 #ifdef TRACE_JCR_CHAIN
533 void b_lock_jcr_chain(const char *fname, int line)
534 #else
535 void lock_jcr_chain()
536 #endif
537 {
538    int errstat;
539 #ifdef TRACE_JCR_CHAIN 
540    Dmsg3(400, "Lock jcr chain %d from %s:%d\n", ++lock_count,
541       fname, line);
542 #endif
543    if ((errstat=rwl_writelock(&lock)) != 0) {
544       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
545            strerror(errstat));
546    }
547 }
548
549 /*
550  * Unlock the chain
551  */
552 #ifdef TRACE_JCR_CHAIN
553 void b_unlock_jcr_chain(const char *fname, int line)
554 #else
555 void unlock_jcr_chain()
556 #endif
557 {
558    int errstat;
559 #ifdef TRACE_JCR_CHAIN 
560    Dmsg3(400, "Unlock jcr chain %d from %s:%d\n", lock_count--,
561       fname, line);
562 #endif
563    if ((errstat=rwl_writeunlock(&lock)) != 0) {
564       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
565            strerror(errstat));
566    }
567 }
568
569
570 JCR *get_next_jcr(JCR *prev_jcr)
571 {
572    JCR *jcr;
573
574    if (prev_jcr == NULL) {
575       jcr = jobs;
576    } else {
577       jcr = prev_jcr->next;
578    }
579    if (jcr) {
580       P(jcr->mutex);
581       jcr->use_count++;
582       V(jcr->mutex);
583       Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
584    }
585    return jcr;
586 }
587
588 bool init_jcr_subsystem(void)
589 {
590    watchdog_t *wd = new_watchdog();
591
592    wd->one_shot = false;
593    wd->interval = 30;   /* FIXME: should be configurable somewhere, even
594                          if only with a #define */
595    wd->callback = jcr_timeout_check;
596
597    register_watchdog(wd);
598
599    return true;
600 }
601
602 static void jcr_timeout_check(watchdog_t *self)
603 {
604    JCR *jcr;
605    BSOCK *fd;
606    time_t timer_start;
607
608    Dmsg0(400, "Start JCR timeout checks\n");
609
610    /* Walk through all JCRs checking if any one is 
611     * blocked for more than specified max time.
612     */
613    lock_jcr_chain();
614    foreach_jcr(jcr) {
615       free_locked_jcr(jcr);           /* OK to free now cuz chain is locked */
616       if (jcr->JobId == 0) {
617          continue;
618       }
619       fd = jcr->store_bsock;
620       if (fd) {
621          timer_start = fd->timer_start;
622          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
623             fd->timer_start = 0;      /* turn off timer */
624             fd->timed_out = TRUE;
625             Jmsg(jcr, M_ERROR, 0, _(
626 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
627                  watchdog_time - timer_start);
628             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
629          }
630       }
631       fd = jcr->file_bsock;
632       if (fd) {
633          timer_start = fd->timer_start;
634          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
635             fd->timer_start = 0;      /* turn off timer */
636             fd->timed_out = TRUE;
637             Jmsg(jcr, M_ERROR, 0, _(
638 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
639                  watchdog_time - timer_start);
640             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
641          }
642       }
643       fd = jcr->dir_bsock;
644       if (fd) {
645          timer_start = fd->timer_start;
646          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
647             fd->timer_start = 0;      /* turn off timer */
648             fd->timed_out = TRUE;
649             Jmsg(jcr, M_ERROR, 0, _(
650 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
651                  watchdog_time - timer_start);
652             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
653          }
654       }
655
656    }
657    unlock_jcr_chain();
658
659    Dmsg0(400, "Finished JCR timeout checks\n");
660 }
661
662 /*
663  * Timeout signal comes here
664  */
665 extern "C" void timeout_handler(int sig)
666 {
667    return;                            /* thus interrupting the function */
668 }