]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
fa716293ba0f7fb7da39a23597818f6a543fe7c3
[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 extern void timeout_handler(int sig);
35
36 struct s_last_job last_job;           /* last job run by this daemon */
37
38 static JCR *jobs = NULL;              /* pointer to JCR chain */
39 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40
41 /*
42  * Create a Job Control Record and link it into JCR chain
43  * Returns newly allocated JCR
44  * Note, since each daemon has a different JCR, he passes
45  *  us the size.
46  */
47 JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
48 {
49    JCR *jcr;
50    struct sigaction sigtimer;
51
52    Dmsg0(200, "Enter new_jcr\n");
53    jcr = (JCR *)malloc(size);
54    memset(jcr, 0, size);
55    jcr->my_thread_id = pthread_self();
56    jcr->sched_time = time(NULL);
57    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
58    jcr->use_count = 1;
59    pthread_mutex_init(&(jcr->mutex), NULL);
60    jcr->JobStatus = JS_Created;       /* ready to run */
61    jcr->VolumeName = get_pool_memory(PM_FNAME);
62    jcr->VolumeName[0] = 0;
63    jcr->errmsg = get_pool_memory(PM_MESSAGE);
64    jcr->errmsg[0] = 0;
65    strcpy(jcr->Job, "*Console*");     /* default */
66
67    sigtimer.sa_flags = 0;
68    sigtimer.sa_handler = timeout_handler;
69    sigfillset(&sigtimer.sa_mask);
70    sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
71
72    P(mutex);
73    jcr->prev = NULL;
74    jcr->next = jobs;
75    if (jobs) {
76       jobs->prev = jcr;
77    }
78    jobs = jcr;
79    V(mutex);
80    return jcr;
81 }
82
83
84 /*
85  * Remove a JCR from the chain
86  * NOTE! The chain must be locked prior to calling
87  *       this routine.
88  */
89 static void remove_jcr(JCR *jcr)
90 {
91    Dmsg0(150, "Enter remove_jcr\n");
92    if (!jcr) {
93       Emsg0(M_ABORT, 0, "NULL jcr.\n");
94    }
95    if (!jcr->prev) {                  /* if no prev */
96       jobs = jcr->next;               /* set new head */
97    } else {
98       jcr->prev->next = jcr->next;    /* update prev */
99    }
100    if (jcr->next) {
101       jcr->next->prev = jcr->prev;
102    }
103    Dmsg0(150, "Leave remove_jcr\n");
104 }
105
106 /*
107  * Free stuff common to all JCRs
108  */
109 static void free_common_jcr(JCR *jcr)
110 {
111    /* Keep some statistics */
112    switch (jcr->JobType) {
113       case JT_BACKUP:
114       case JT_VERIFY:
115       case JT_RESTORE:
116          last_job.NumJobs++;
117          last_job.JobType = jcr->JobType;
118          last_job.JobId = jcr->JobId;
119          last_job.VolSessionId = jcr->VolSessionId;
120          last_job.VolSessionTime = jcr->VolSessionTime;
121          strcpy(last_job.Job, jcr->Job);
122          last_job.JobFiles = jcr->JobFiles;
123          last_job.JobBytes = jcr->JobBytes;
124          last_job.JobStatus = jcr->JobStatus;
125          last_job.start_time = jcr->start_time;
126          last_job.end_time = time(NULL);
127          break;
128       default:
129          break;
130    }
131    pthread_mutex_destroy(&jcr->mutex);
132
133    close_msg(jcr);                    /* close messages for this job */
134
135    /* do this after closing messages */
136    if (jcr->client_name) {
137       free_pool_memory(jcr->client_name);
138       jcr->client_name = NULL;
139    }
140
141    if (jcr->sd_auth_key) {
142       Dmsg0(200, "Free JCR sd_auth_key\n");
143       free(jcr->sd_auth_key);
144       jcr->sd_auth_key = NULL;
145    }
146    if (jcr->VolumeName) {
147       free_pool_memory(jcr->VolumeName);
148       jcr->VolumeName = NULL;
149    }
150
151    if (jcr->dir_bsock) {
152       bnet_close(jcr->dir_bsock);
153       jcr->dir_bsock = NULL;
154    }
155    if (jcr->errmsg) {
156       free_pool_memory(jcr->errmsg);
157       jcr->errmsg = NULL;
158    }
159    free(jcr);
160 }
161
162 /* 
163  * Global routine to free a jcr
164  */
165 #ifdef DEBUG
166 void b_free_jcr(char *file, int line, JCR *jcr)
167 {
168    Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
169
170 #else
171
172 void free_jcr(JCR *jcr)
173 {
174    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
175
176 #endif
177
178    P(mutex);
179    jcr->use_count--;                  /* decrement use count */
180    Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
181    if (jcr->use_count > 0) {          /* if in use */
182       V(mutex);
183       Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
184       return;
185    }
186    remove_jcr(jcr);
187    V(mutex);
188
189    if (jcr->daemon_free_jcr) {
190       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
191    }
192    free_common_jcr(jcr);
193
194    P(mutex);
195    close_msg(NULL);                   /* flush any daemon messages */
196    V(mutex);
197    Dmsg0(200, "Exit free_jcr\n");
198 }
199
200
201 /* 
202  * Global routine to free a jcr
203  *  JCR chain is already locked
204  */
205 void free_locked_jcr(JCR *jcr)
206 {
207    jcr->use_count--;                  /* decrement use count */
208    Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
209    if (jcr->use_count > 0) {          /* if in use */
210       return;
211    }
212    remove_jcr(jcr);
213    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
214    free_common_jcr(jcr);
215 }
216
217
218
219
220 /*
221  * Given a JobId, find the JCR      
222  *   Returns: jcr on success
223  *            NULL on failure
224  */
225 JCR *get_jcr_by_id(uint32_t JobId)
226 {
227    JCR *jcr;       
228
229    P(mutex);
230    for (jcr = jobs; jcr; jcr=jcr->next) {
231       if (jcr->JobId == JobId) {
232          jcr->use_count++;
233          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
234          break;
235       }
236    }
237    V(mutex);
238    return jcr; 
239 }
240
241 /*
242  * Given a SessionId and SessionTime, find the JCR      
243  *   Returns: jcr on success
244  *            NULL on failure
245  */
246 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
247 {
248    JCR *jcr;       
249
250    P(mutex);
251    for (jcr = jobs; jcr; jcr=jcr->next) {
252       if (jcr->VolSessionId == SessionId && 
253           jcr->VolSessionTime == SessionTime) {
254          jcr->use_count++;
255          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
256          break;
257       }
258    }
259    V(mutex);
260    return jcr; 
261 }
262
263
264 /*
265  * Given a Job, find the JCR      
266  *  compares on the number of characters in Job
267  *  thus allowing partial matches.
268  *   Returns: jcr on success
269  *            NULL on failure
270  */
271 JCR *get_jcr_by_partial_name(char *Job)
272 {
273    JCR *jcr;       
274    int len;
275
276    P(mutex);
277    len = strlen(Job);
278    for (jcr = jobs; jcr; jcr=jcr->next) {
279       if (strncmp(Job, jcr->Job, len) == 0) {
280          jcr->use_count++;
281          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
282          break;
283       }
284    }
285    V(mutex);
286    return jcr; 
287 }
288
289
290 /*
291  * Given a Job, find the JCR      
292  *  requires an exact match of names.
293  *   Returns: jcr on success
294  *            NULL on failure
295  */
296 JCR *get_jcr_by_full_name(char *Job)
297 {
298    JCR *jcr;       
299
300    P(mutex);
301    for (jcr = jobs; jcr; jcr=jcr->next) {
302       if (strcmp(jcr->Job, Job) == 0) {
303          jcr->use_count++;
304          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
305          break;
306       }
307    }
308    V(mutex);
309    return jcr; 
310 }
311
312 void set_jcr_job_status(JCR *jcr, int JobStatus)
313 {
314    /*
315     * For a set of errors, ... keep the current status
316     *   so it isn't lost. For all others, set it.
317     */
318    switch (jcr->JobStatus) {
319    case JS_ErrorTerminated:
320    case JS_Error:
321    case JS_FatalError:
322    case JS_Differences:
323    case JS_Canceled:
324       break;
325    default:
326       jcr->JobStatus = JobStatus;
327    }
328 }
329
330 /* 
331  * Lock the chain
332  */
333 void lock_jcr_chain()
334 {
335    P(mutex);
336 }
337
338 /*
339  * Unlock the chain
340  */
341 void unlock_jcr_chain()
342 {
343    V(mutex);
344 }
345
346
347 JCR *get_next_jcr(JCR *jcr)
348 {
349    JCR *rjcr;
350
351    if (jcr == NULL) {
352       rjcr = jobs;
353    } else {
354       rjcr = jcr->next;
355    }
356    if (rjcr) {
357       rjcr->use_count++;
358       Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);
359    }
360    return rjcr;
361 }