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