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