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