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