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