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