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