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