]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
Add -p option to SD => forge on + fix Makefile for gnome files
[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(100, "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(100, "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(100, "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(400, "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(400, "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(400, "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    delete jcr->msg_queue;
290    close_msg(jcr);                    /* close messages for this job */
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(400, "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(400, "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(400, "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(400, "free_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
356       return;
357    }
358    dequeue_messages(jcr);
359    remove_jcr(jcr);
360    job_end_pop(jcr);                  /* pop and call hooked routines */
361
362    Dmsg1(400, "End job=%d\n", jcr->JobId);
363    if (jcr->daemon_free_jcr) {
364       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
365    }
366
367    free_common_jcr(jcr);
368
369    close_msg(NULL);                   /* flush any daemon messages */
370    unlock_jcr_chain();
371    Dmsg0(400, "Exit free_jcr\n");
372 }
373
374
375 /* 
376  * Global routine to free a jcr
377  *  JCR chain is already locked
378  */
379 void free_locked_jcr(JCR *jcr)
380 {
381    jcr->use_count--;                  /* decrement use count */
382    Dmsg2(400, "Dec free_locked_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
383    if (jcr->use_count > 0) {          /* if in use */
384       return;
385    }
386    remove_jcr(jcr);
387    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
388    free_common_jcr(jcr);
389 }
390
391
392
393 /*
394  * Given a JobId, find the JCR      
395  *   Returns: jcr on success
396  *            NULL on failure
397  */
398 JCR *get_jcr_by_id(uint32_t JobId)
399 {
400    JCR *jcr;       
401
402    lock_jcr_chain();                    /* lock chain */
403    for (jcr = jobs; jcr; jcr=jcr->next) {
404       if (jcr->JobId == JobId) {
405          P(jcr->mutex);
406          jcr->use_count++;
407          V(jcr->mutex);
408          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
409          break;
410       }
411    }
412    unlock_jcr_chain();
413    return jcr; 
414 }
415
416 /*
417  * Given a SessionId and SessionTime, find the JCR      
418  *   Returns: jcr on success
419  *            NULL on failure
420  */
421 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
422 {
423    JCR *jcr;       
424
425    lock_jcr_chain();
426    for (jcr = jobs; jcr; jcr=jcr->next) {
427       if (jcr->VolSessionId == SessionId && 
428           jcr->VolSessionTime == SessionTime) {
429          P(jcr->mutex);
430          jcr->use_count++;
431          V(jcr->mutex);
432          Dmsg2(400, "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  *  compares on the number of characters in Job
444  *  thus allowing partial matches.
445  *   Returns: jcr on success
446  *            NULL on failure
447  */
448 JCR *get_jcr_by_partial_name(char *Job)
449 {
450    JCR *jcr;       
451    int len;
452
453    if (!Job) {
454       return NULL;
455    }
456    lock_jcr_chain();
457    len = strlen(Job);
458    for (jcr = jobs; jcr; jcr=jcr->next) {
459       if (strncmp(Job, jcr->Job, len) == 0) {
460          P(jcr->mutex);
461          jcr->use_count++;
462          V(jcr->mutex);
463          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
464          break;
465       }
466    }
467    unlock_jcr_chain();
468    return jcr; 
469 }
470
471
472 /*
473  * Given a Job, find the JCR      
474  *  requires an exact match of names.
475  *   Returns: jcr on success
476  *            NULL on failure
477  */
478 JCR *get_jcr_by_full_name(char *Job)
479 {
480    JCR *jcr;       
481
482    if (!Job) {
483       return NULL;
484    }
485    lock_jcr_chain();
486    for (jcr = jobs; jcr; jcr=jcr->next) {
487       if (strcmp(jcr->Job, Job) == 0) {
488          P(jcr->mutex);
489          jcr->use_count++;
490          V(jcr->mutex);
491          Dmsg2(400, "Inc get_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
492          break;
493       }
494    }
495    unlock_jcr_chain();
496    return jcr; 
497 }
498
499 void set_jcr_job_status(JCR *jcr, int JobStatus)
500 {
501    /*
502     * For a set of errors, ... keep the current status
503     *   so it isn't lost. For all others, set it.
504     */
505    switch (jcr->JobStatus) {
506    case JS_ErrorTerminated:
507    case JS_Error:
508    case JS_FatalError:
509    case JS_Differences:
510    case JS_Canceled:
511       break;
512    default:
513       jcr->JobStatus = JobStatus;
514    }
515 }
516
517 /* 
518  * Lock the chain
519  */
520 void lock_jcr_chain()
521 {
522    int errstat;
523    if ((errstat=rwl_writelock(&lock)) != 0) {
524       Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
525            strerror(errstat));
526    }
527 }
528
529 /*
530  * Unlock the chain
531  */
532 void unlock_jcr_chain()
533 {
534    int errstat;
535    if ((errstat=rwl_writeunlock(&lock)) != 0) {
536       Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
537            strerror(errstat));
538    }
539 }
540
541
542 JCR *get_next_jcr(JCR *prev_jcr)
543 {
544    JCR *jcr;
545
546    if (prev_jcr == NULL) {
547       jcr = jobs;
548    } else {
549       jcr = prev_jcr->next;
550    }
551    if (jcr) {
552       P(jcr->mutex);
553       jcr->use_count++;
554       V(jcr->mutex);
555       Dmsg2(400, "Inc get_next_jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
556    }
557    return jcr;
558 }
559
560 bool init_jcr_subsystem(void)
561 {
562    watchdog_t *wd = new_watchdog();
563
564    wd->one_shot = false;
565    wd->interval = 30;   /* FIXME: should be configurable somewhere, even
566                          if only with a #define */
567    wd->callback = jcr_timeout_check;
568
569    register_watchdog(wd);
570
571    return true;
572 }
573
574 static void jcr_timeout_check(watchdog_t *self)
575 {
576    JCR *jcr;
577    BSOCK *fd;
578    time_t timer_start;
579
580    Dmsg0(400, "Start JCR timeout checks\n");
581
582    /* Walk through all JCRs checking if any one is 
583     * blocked for more than specified max time.
584     */
585    lock_jcr_chain();
586    foreach_jcr(jcr) {
587       free_locked_jcr(jcr);           /* OK to free now cuz chain is locked */
588       if (jcr->JobId == 0) {
589          continue;
590       }
591       fd = jcr->store_bsock;
592       if (fd) {
593          timer_start = fd->timer_start;
594          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
595             fd->timer_start = 0;      /* turn off timer */
596             fd->timed_out = TRUE;
597             Jmsg(jcr, M_ERROR, 0, _(
598 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
599                  watchdog_time - timer_start);
600             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
601          }
602       }
603       fd = jcr->file_bsock;
604       if (fd) {
605          timer_start = fd->timer_start;
606          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
607             fd->timer_start = 0;      /* turn off timer */
608             fd->timed_out = TRUE;
609             Jmsg(jcr, M_ERROR, 0, _(
610 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
611                  watchdog_time - timer_start);
612             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
613          }
614       }
615       fd = jcr->dir_bsock;
616       if (fd) {
617          timer_start = fd->timer_start;
618          if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
619             fd->timer_start = 0;      /* turn off timer */
620             fd->timed_out = TRUE;
621             Jmsg(jcr, M_ERROR, 0, _(
622 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
623                  watchdog_time - timer_start);
624             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
625          }
626       }
627
628    }
629    unlock_jcr_chain();
630
631    Dmsg0(400, "Finished JCR timeout checks\n");
632 }
633
634 /*
635  * Timeout signal comes here
636  */
637 static void timeout_handler(int sig)
638 {
639    return;                            /* thus interrupting the function */
640 }