]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/jcr.c
Implement Auto Prune and Auto Recycle
[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, 2001, 2002 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    P(mutex);
54    jcr->sched_time = time(NULL);
55    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
56    jcr->prev = NULL;
57    jcr->next = jobs;
58    if (jobs) {
59       jobs->prev = jcr;
60    }
61    jcr->use_count = 1;
62    pthread_mutex_init(&(jcr->mutex), NULL);
63    jcr->JobStatus = JS_Created;       /* ready to run */
64    jcr->VolumeName = get_pool_memory(PM_FNAME);
65    jcr->VolumeName[0] = 0;
66    jcr->errmsg = get_pool_memory(PM_MESSAGE);
67    jcr->errmsg[0] = 0;
68    jobs = jcr;
69    V(mutex);
70    return jcr;
71 }
72
73
74 /*
75  * Remove a JCR from the chain
76  * NOTE! The chain must be locked prior to calling
77  *       this routine.
78  */
79 static void remove_jcr(JCR *jcr)
80 {
81    Dmsg0(150, "Enter remove_jcr\n");
82    if (!jcr) {
83       Emsg0(M_ABORT, 0, "NULL jcr.\n");
84    }
85    if (!jcr->prev) {                  /* if no prev */
86       jobs = jcr->next;               /* set new head */
87    } else {
88       jcr->prev->next = jcr->next;    /* update prev */
89    }
90    if (jcr->next) {
91       jcr->next->prev = jcr->prev;
92    }
93    Dmsg0(150, "Leave remove_jcr\n");
94 }
95
96 /*
97  * Free stuff common to all JCRs
98  */
99 static void free_common_jcr(JCR *jcr)
100 {
101    /* Keep some statistics */
102    switch (jcr->JobType) {
103       case JT_BACKUP:
104       case JT_VERIFY:
105       case JT_RESTORE:
106          last_job.NumJobs++;
107          last_job.JobType = jcr->JobType;
108          last_job.JobId = jcr->JobId;
109          last_job.VolSessionId = jcr->VolSessionId;
110          last_job.VolSessionTime = jcr->VolSessionTime;
111          strcpy(last_job.Job, jcr->Job);
112          last_job.JobFiles = jcr->JobFiles;
113          last_job.JobBytes = jcr->JobBytes;
114          last_job.JobStatus = jcr->JobStatus;
115          last_job.start_time = jcr->start_time;
116          last_job.end_time = time(NULL);
117          break;
118       default:
119          break;
120    }
121    pthread_mutex_destroy(&jcr->mutex);
122
123    close_msg(jcr);                    /* close messages for this job */
124
125    /* do this after closing messages */
126    if (jcr->client_name) {
127       free(jcr->client_name);
128       jcr->client_name = NULL;
129    }
130
131    if (jcr->sd_auth_key) {
132       Dmsg0(200, "Free JCR sd_auth_key\n");
133       free(jcr->sd_auth_key);
134       jcr->sd_auth_key = NULL;
135    }
136    if (jcr->VolumeName) {
137       free_pool_memory(jcr->VolumeName);
138       jcr->VolumeName = NULL;
139    }
140
141    if (jcr->dir_bsock) {
142       bnet_close(jcr->dir_bsock);
143       jcr->dir_bsock = NULL;
144    }
145    if (jcr->errmsg) {
146       free_pool_memory(jcr->errmsg);
147       jcr->errmsg = NULL;
148    }
149    free(jcr);
150 }
151
152 /* 
153  * Global routine to free a jcr
154  */
155 void free_jcr(JCR *jcr)
156 {
157    Dmsg0(200, "Enter free_jcr\n");
158    P(mutex);
159    jcr->use_count--;                  /* decrement use count */
160    Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
161    if (jcr->use_count > 0) {          /* if in use */
162       V(mutex);
163       Dmsg1(200, "jcr use_count=%d\n", jcr->use_count);
164       return;
165    }
166    remove_jcr(jcr);
167    V(mutex);
168    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
169    free_common_jcr(jcr);
170    Dmsg0(200, "Exit free_jcr\n");
171 }
172
173
174 /* 
175  * Global routine to free a jcr
176  *  JCR chain is already locked
177  */
178 void free_locked_jcr(JCR *jcr)
179 {
180    jcr->use_count--;                  /* decrement use count */
181    Dmsg1(200, "Decrement jcr use_count=%d\n", jcr->use_count);
182    if (jcr->use_count > 0) {          /* if in use */
183       return;
184    }
185    remove_jcr(jcr);
186    jcr->daemon_free_jcr(jcr);         /* call daemon free routine */
187    free_common_jcr(jcr);
188 }
189
190
191
192
193 /*
194  * Given a JobId, find the JCR      
195  *   Returns: jcr on success
196  *            NULL on failure
197  */
198 JCR *get_jcr_by_id(uint32_t JobId)
199 {
200    JCR *jcr;       
201
202    P(mutex);
203    for (jcr = jobs; jcr; jcr=jcr->next) {
204       if (jcr->JobId == JobId) {
205          jcr->use_count++;
206          Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
207          break;
208       }
209    }
210    V(mutex);
211    return jcr; 
212 }
213
214
215
216 /*
217  * Given a Job, find the JCR      
218  *  compares on the number of characters in Job
219  *  thus allowing partial matches.
220  *   Returns: jcr on success
221  *            NULL on failure
222  */
223 JCR *get_jcr_by_partial_name(char *Job)
224 {
225    JCR *jcr;       
226    int len;
227
228    P(mutex);
229    len = strlen(Job);
230    for (jcr = jobs; jcr; jcr=jcr->next) {
231       if (strncmp(Job, jcr->Job, len) == 0) {
232          jcr->use_count++;
233          Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
234          break;
235       }
236    }
237    V(mutex);
238    return jcr; 
239 }
240
241
242 /*
243  * Given a Job, find the JCR      
244  *  requires an exact match of names.
245  *   Returns: jcr on success
246  *            NULL on failure
247  */
248 JCR *get_jcr_by_full_name(char *Job)
249 {
250    JCR *jcr;       
251
252    P(mutex);
253    for (jcr = jobs; jcr; jcr=jcr->next) {
254       if (strcmp(jcr->Job, Job) == 0) {
255          jcr->use_count++;
256          Dmsg1(200, "Increment jcr use_count=%d\n", jcr->use_count);
257          break;
258       }
259    }
260    V(mutex);
261    return jcr; 
262 }
263
264 /* 
265  * Lock the chain
266  */
267 void lock_jcr_chain()
268 {
269    P(mutex);
270 }
271
272 /*
273  * Unlock the chain
274  */
275 void unlock_jcr_chain()
276 {
277    V(mutex);
278 }
279
280
281 JCR *get_next_jcr(JCR *jcr)
282 {
283    JCR *rjcr;
284
285    if (jcr == NULL) {
286       rjcr = jobs;
287    } else {
288       rjcr = jcr->next;
289    }
290    if (rjcr) {
291       rjcr->use_count++;
292       Dmsg1(200, "Increment jcr use_count=%d\n", rjcr->use_count);
293    }
294    return rjcr;
295 }