]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
Fix cached_path to be local to jcr + fixed idcache
[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    mp_chr(jcr->VolumeName)[0] = 0;
63    jcr->errmsg = get_pool_memory(PM_MESSAGE);
64    mp_chr(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.  N.B. Be careful to include only
108  *  generic stuff in the common part of the jcr. 
109  */
110 static void free_common_jcr(JCR *jcr)
111 {
112    /* Keep some statistics */
113    switch (jcr->JobType) {
114       case JT_BACKUP:
115       case JT_VERIFY:
116       case JT_RESTORE:
117          last_job.NumJobs++;
118          last_job.JobType = jcr->JobType;
119          last_job.JobId = jcr->JobId;
120          last_job.VolSessionId = jcr->VolSessionId;
121          last_job.VolSessionTime = jcr->VolSessionTime;
122          strcpy(last_job.Job, jcr->Job);
123          last_job.JobFiles = jcr->JobFiles;
124          last_job.JobBytes = jcr->JobBytes;
125          last_job.JobStatus = jcr->JobStatus;
126          last_job.start_time = jcr->start_time;
127          last_job.end_time = time(NULL);
128          break;
129       default:
130          break;
131    }
132    pthread_mutex_destroy(&jcr->mutex);
133
134    close_msg(jcr);                    /* close messages for this job */
135
136    /* do this after closing messages */
137    if (jcr->client_name) {
138       free_pool_memory(jcr->client_name);
139       jcr->client_name = NULL;
140    }
141
142    if (jcr->sd_auth_key) {
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    if (jcr->where) {
160       free(jcr->where);
161       jcr->where = NULL;
162    }
163    if (jcr->cached_path) {
164       free_pool_memory(jcr->cached_path);
165       jcr->cached_path = NULL;
166       jcr->cached_pnl = 0;
167    }
168    free_getuser_cache();
169    free_getgroup_cache();
170    free(jcr);
171 }
172
173 /* 
174  * Global routine to free a jcr
175  */
176 #ifdef DEBUG
177 void b_free_jcr(char *file, int line, JCR *jcr)
178 {
179    Dmsg3(200, "Enter free_jcr 0x%x from %s:%d\n", jcr, file, line);
180
181 #else
182
183 void free_jcr(JCR *jcr)
184 {
185    Dmsg1(200, "Enter free_jcr 0x%x\n", jcr);
186
187 #endif
188
189    P(mutex);
190    jcr->use_count--;                  /* decrement use count */
191    Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
192    if (jcr->use_count > 0) {          /* if in use */
193       V(mutex);
194       Dmsg2(200, "jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
195       return;
196    }
197    remove_jcr(jcr);
198    V(mutex);
199
200    if (jcr->daemon_free_jcr) {
201       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
202    }
203    free_common_jcr(jcr);
204
205    P(mutex);
206    close_msg(NULL);                   /* flush any daemon messages */
207    V(mutex);
208    Dmsg0(200, "Exit free_jcr\n");
209 }
210
211
212 /* 
213  * Global routine to free a jcr
214  *  JCR chain is already locked
215  */
216 void free_locked_jcr(JCR *jcr)
217 {
218    jcr->use_count--;                  /* decrement use count */
219    Dmsg2(200, "Dec jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
220    if (jcr->use_count > 0) {          /* if in use */
221       return;
222    }
223    remove_jcr(jcr);
224    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
225    free_common_jcr(jcr);
226 }
227
228
229
230
231 /*
232  * Given a JobId, find the JCR      
233  *   Returns: jcr on success
234  *            NULL on failure
235  */
236 JCR *get_jcr_by_id(uint32_t JobId)
237 {
238    JCR *jcr;       
239
240    P(mutex);
241    for (jcr = jobs; jcr; jcr=jcr->next) {
242       if (jcr->JobId == JobId) {
243          jcr->use_count++;
244          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
245          break;
246       }
247    }
248    V(mutex);
249    return jcr; 
250 }
251
252 /*
253  * Given a SessionId and SessionTime, find the JCR      
254  *   Returns: jcr on success
255  *            NULL on failure
256  */
257 JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime)
258 {
259    JCR *jcr;       
260
261    P(mutex);
262    for (jcr = jobs; jcr; jcr=jcr->next) {
263       if (jcr->VolSessionId == SessionId && 
264           jcr->VolSessionTime == SessionTime) {
265          jcr->use_count++;
266          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
267          break;
268       }
269    }
270    V(mutex);
271    return jcr; 
272 }
273
274
275 /*
276  * Given a Job, find the JCR      
277  *  compares on the number of characters in Job
278  *  thus allowing partial matches.
279  *   Returns: jcr on success
280  *            NULL on failure
281  */
282 JCR *get_jcr_by_partial_name(char *Job)
283 {
284    JCR *jcr;       
285    int len;
286
287    P(mutex);
288    len = strlen(Job);
289    for (jcr = jobs; jcr; jcr=jcr->next) {
290       if (strncmp(Job, jcr->Job, len) == 0) {
291          jcr->use_count++;
292          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
293          break;
294       }
295    }
296    V(mutex);
297    return jcr; 
298 }
299
300
301 /*
302  * Given a Job, find the JCR      
303  *  requires an exact match of names.
304  *   Returns: jcr on success
305  *            NULL on failure
306  */
307 JCR *get_jcr_by_full_name(char *Job)
308 {
309    JCR *jcr;       
310
311    P(mutex);
312    for (jcr = jobs; jcr; jcr=jcr->next) {
313       if (strcmp(jcr->Job, Job) == 0) {
314          jcr->use_count++;
315          Dmsg2(200, "Inc jcr 0x%x use_count=%d\n", jcr, jcr->use_count);
316          break;
317       }
318    }
319    V(mutex);
320    return jcr; 
321 }
322
323 void set_jcr_job_status(JCR *jcr, int JobStatus)
324 {
325    /*
326     * For a set of errors, ... keep the current status
327     *   so it isn't lost. For all others, set it.
328     */
329    switch (jcr->JobStatus) {
330    case JS_ErrorTerminated:
331    case JS_Error:
332    case JS_FatalError:
333    case JS_Differences:
334    case JS_Canceled:
335       break;
336    default:
337       jcr->JobStatus = JobStatus;
338    }
339 }
340
341 /* 
342  * Lock the chain
343  */
344 void lock_jcr_chain()
345 {
346    P(mutex);
347 }
348
349 /*
350  * Unlock the chain
351  */
352 void unlock_jcr_chain()
353 {
354    V(mutex);
355 }
356
357
358 JCR *get_next_jcr(JCR *jcr)
359 {
360    JCR *rjcr;
361
362    if (jcr == NULL) {
363       rjcr = jobs;
364    } else {
365       rjcr = jcr->next;
366    }
367    if (rjcr) {
368       rjcr->use_count++;
369       Dmsg1(200, "Inc jcr use_count=%d\n", rjcr->use_count);
370    }
371    return rjcr;
372 }