]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
Fix segfault from double free or RestoreBootstrap; add L_NONE for restore and admin...
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void *job_thread(void *arg);
34
35 /* Exported subroutines */
36
37
38 /* Imported subroutines */
39 extern void term_scheduler();
40 extern void term_ua_server();
41 extern int do_backup(JCR *jcr);
42 extern int do_admin(JCR *jcr);
43 extern int do_restore(JCR *jcr);
44 extern int do_verify(JCR *jcr);
45
46 jobq_t  job_queue;
47
48 void init_job_server(int max_workers)
49 {
50    int stat;
51    if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
52       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), strerror(stat));
53    }
54    return;
55 }
56
57 /*
58  * Run a job -- typically called by the scheduler, but may also
59  *              be called by the UA (Console program).
60  *
61  */
62 void run_job(JCR *jcr)
63 {
64    int stat, errstat;
65
66    sm_check(__FILE__, __LINE__, True);
67    init_msg(jcr, jcr->messages);
68    create_unique_job_name(jcr, jcr->job->hdr.name);
69    set_jcr_job_status(jcr, JS_Created);
70    jcr->jr.SchedTime = jcr->sched_time;
71    jcr->jr.StartTime = jcr->start_time;
72    jcr->jr.EndTime = 0;               /* perhaps rescheduled, clear it */
73    jcr->jr.Type = jcr->JobType;
74    jcr->jr.Level = jcr->JobLevel;
75    jcr->jr.JobStatus = jcr->JobStatus;
76    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
77    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
78
79    /* Initialize termination condition variable */
80    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
81       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
82       set_jcr_job_status(jcr, JS_ErrorTerminated);
83       free_jcr(jcr);
84       return;
85    }
86
87    /*
88     * Open database
89     */
90    Dmsg0(50, "Open database\n");
91    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
92                             jcr->catalog->db_password, jcr->catalog->db_address,
93                             jcr->catalog->db_port, jcr->catalog->db_socket);
94    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
95       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
96                  jcr->catalog->db_name);
97       if (jcr->db) {
98          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
99       }
100       set_jcr_job_status(jcr, JS_ErrorTerminated);
101       free_jcr(jcr);
102       return;
103    }
104    Dmsg0(50, "DB opened\n");
105
106    /*
107     * Create Job record  
108     */
109    jcr->jr.JobStatus = jcr->JobStatus;
110    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
111       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
112       set_jcr_job_status(jcr, JS_ErrorTerminated);
113       free_jcr(jcr);
114       return;
115    }
116    jcr->JobId = jcr->jr.JobId;
117    ASSERT(jcr->jr.JobId > 0);
118
119    Dmsg4(50, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
120        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
121    Dmsg0(200, "Add jrc to work queue\n");
122
123    /* Queue the job to be run */
124    if ((stat = jobq_add(&job_queue, jcr)) != 0) {
125       Emsg1(M_ABORT, 0, _("Could not add job queue: ERR=%s\n"), strerror(stat));
126    }
127    Dmsg0(100, "Done run_job()\n");
128 }
129
130 /* 
131  * This is the engine called by job_add() when we were pulled                
132  *  from the work queue.
133  *  At this point, we are running in our own thread and all
134  *    necessary resources are allocated -- see jobq.c
135  */
136 static void *job_thread(void *arg)
137 {
138    JCR *jcr = (JCR *)arg;
139
140    pthread_detach(pthread_self());
141    sm_check(__FILE__, __LINE__, True);
142
143    for ( ;; ) {
144
145       Dmsg0(200, "=====Start Job=========\n");
146       jcr->start_time = time(NULL);      /* set the real start time */
147       set_jcr_job_status(jcr, JS_Running);
148
149       if (job_canceled(jcr)) {
150          update_job_end_record(jcr);
151       } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
152           (utime_t)(jcr->start_time - jcr->sched_time)) {
153          Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
154          set_jcr_job_status(jcr, JS_Canceled);
155          update_job_end_record(jcr);
156       } else {
157
158          /* Run Job */
159          if (jcr->job->RunBeforeJob) {
160             POOLMEM *before = get_pool_memory(PM_FNAME);
161             int status;
162             BPIPE *bpipe;
163             char line[MAXSTRING];
164             
165             before = edit_job_codes(jcr, before, jcr->job->RunBeforeJob, "");
166             bpipe = open_bpipe(before, 0, "r");
167             free_pool_memory(before);
168             while (fgets(line, sizeof(line), bpipe->rfd)) {
169                Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
170             }
171             status = close_bpipe(bpipe);
172             if (status != 0) {
173                Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
174                   status);
175                set_jcr_job_status(jcr, JS_FatalError);
176                update_job_end_record(jcr);
177                goto bail_out;
178             }
179          }
180          switch (jcr->JobType) {
181          case JT_BACKUP:
182             do_backup(jcr);
183             if (jcr->JobStatus == JS_Terminated) {
184                do_autoprune(jcr);
185             }
186             break;
187          case JT_VERIFY:
188             do_verify(jcr);
189             if (jcr->JobStatus == JS_Terminated) {
190                do_autoprune(jcr);
191             }
192             break;
193          case JT_RESTORE:
194             do_restore(jcr);
195             if (jcr->JobStatus == JS_Terminated) {
196                do_autoprune(jcr);
197             }
198             break;
199          case JT_ADMIN:
200             do_admin(jcr);
201             if (jcr->JobStatus == JS_Terminated) {
202                do_autoprune(jcr);
203             }
204             break;
205          default:
206             Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
207             break;
208          }
209          if ((jcr->job->RunAfterJob && jcr->JobStatus == JS_Terminated) ||
210              (jcr->job->RunAfterFailedJob && jcr->JobStatus != JS_Terminated)) {
211             POOLMEM *after = get_pool_memory(PM_FNAME);
212             int status;
213             BPIPE *bpipe;
214             char line[MAXSTRING];
215             
216             if (jcr->JobStatus == JS_Terminated) {
217                after = edit_job_codes(jcr, after, jcr->job->RunAfterJob, "");
218             } else {
219                after = edit_job_codes(jcr, after, jcr->job->RunAfterFailedJob, "");
220             }
221             bpipe = open_bpipe(after, 0, "r");
222             free_pool_memory(after);
223             while (fgets(line, sizeof(line), bpipe->rfd)) {
224                Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
225             }
226             status = close_bpipe(bpipe);
227             if (status != 0) {
228                if (jcr->JobStatus == JS_Terminated) {
229                   Jmsg(jcr, M_FATAL, 0, _("RunAfterJob returned non-zero status=%d\n"),
230                        status);
231                } else {
232                   Jmsg(jcr, M_FATAL, 0, _("RunAfterFailedJob returned non-zero status=%d\n"),
233                        status);
234                }
235                set_jcr_job_status(jcr, JS_FatalError);
236                update_job_end_record(jcr);
237             }
238          }
239       }
240 bail_out:
241       break;
242    }
243
244    Dmsg0(50, "======== End Job ==========\n");
245    sm_check(__FILE__, __LINE__, True);
246    return NULL;
247 }
248
249
250 /*
251  * Get or create a Client record for this Job
252  */
253 int get_or_create_client_record(JCR *jcr)
254 {
255    CLIENT_DBR cr;
256
257    memset(&cr, 0, sizeof(cr));
258    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
259    cr.AutoPrune = jcr->client->AutoPrune;
260    cr.FileRetention = jcr->client->FileRetention;
261    cr.JobRetention = jcr->client->JobRetention;
262    if (!jcr->client_name) {
263       jcr->client_name = get_pool_memory(PM_NAME);
264    }
265    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
266    if (!db_create_client_record(jcr, jcr->db, &cr)) {
267       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
268          db_strerror(jcr->db));
269       return 0;
270    }
271    jcr->jr.ClientId = cr.ClientId;
272    if (cr.Uname[0]) {
273       if (!jcr->client_uname) {
274          jcr->client_uname = get_pool_memory(PM_NAME);
275       }
276       pm_strcpy(&jcr->client_uname, cr.Uname);
277    }
278    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
279       jcr->jr.ClientId);
280    return 1;
281 }
282
283
284 /*
285  * Write status and such in DB
286  */
287 void update_job_end_record(JCR *jcr)
288 {
289    if (jcr->jr.EndTime == 0) {
290       jcr->jr.EndTime = time(NULL);
291    }
292    jcr->end_time = jcr->jr.EndTime;
293    jcr->jr.JobId = jcr->JobId;
294    jcr->jr.JobStatus = jcr->JobStatus;
295    jcr->jr.JobFiles = jcr->JobFiles;
296    jcr->jr.JobBytes = jcr->JobBytes;
297    jcr->jr.VolSessionId = jcr->VolSessionId;
298    jcr->jr.VolSessionTime = jcr->VolSessionTime;
299    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
300       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
301          db_strerror(jcr->db));
302    }
303 }
304
305 /*
306  * Takes base_name and appends (unique) current
307  *   date and time to form unique job name.
308  *
309  *  Returns: unique job name in jcr->Job
310  *    date/time in jcr->start_time
311  */
312 void create_unique_job_name(JCR *jcr, char *base_name)
313 {
314    /* Job start mutex */
315    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
316    static time_t last_start_time = 0;
317    time_t now;
318    struct tm tm;
319    char dt[MAX_TIME_LENGTH];
320    char name[MAX_NAME_LENGTH];
321    char *p;
322
323    /* Guarantee unique start time -- maximum one per second, and
324     * thus unique Job Name 
325     */
326    P(mutex);                          /* lock creation of jobs */
327    now = time(NULL);
328    while (now == last_start_time) {
329       bmicrosleep(0, 500000);
330       now = time(NULL);
331    }
332    last_start_time = now;
333    V(mutex);                          /* allow creation of jobs */
334    jcr->start_time = now;
335    /* Form Unique JobName */
336    localtime_r(&now, &tm);
337    /* Use only characters that are permitted in Windows filenames */
338    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
339    bstrncpy(name, base_name, sizeof(name));
340    name[sizeof(name)-22] = 0;          /* truncate if too long */
341    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
342    /* Convert spaces into underscores */
343    for (p=jcr->Job; *p; p++) {
344       if (*p == ' ') {
345          *p = '_';
346       }
347    }
348 }
349
350 /*
351  * Free the Job Control Record if no one is still using it.
352  *  Called from main free_jcr() routine in src/lib/jcr.c so
353  *  that we can do our Director specific cleanup of the jcr.
354  */
355 void dird_free_jcr(JCR *jcr)
356 {
357    Dmsg0(200, "Start dird free_jcr\n");
358
359    if (jcr->sd_auth_key) {
360       free(jcr->sd_auth_key);
361       jcr->sd_auth_key = NULL;
362    }
363    if (jcr->where) {
364       free(jcr->where);
365       jcr->where = NULL;
366    }
367    if (jcr->file_bsock) {
368       Dmsg0(200, "Close File bsock\n");
369       bnet_close(jcr->file_bsock);
370       jcr->file_bsock = NULL;
371    }
372    if (jcr->store_bsock) {
373       Dmsg0(200, "Close Store bsock\n");
374       bnet_close(jcr->store_bsock);
375       jcr->store_bsock = NULL;
376    }
377    if (jcr->fname) {  
378       Dmsg0(200, "Free JCR fname\n");
379       free_pool_memory(jcr->fname);
380       jcr->fname = NULL;
381    }
382    if (jcr->stime) {
383       Dmsg0(200, "Free JCR stime\n");
384       free_pool_memory(jcr->stime);
385       jcr->stime = NULL;
386    }
387    if (jcr->RestoreBootstrap) {
388       free(jcr->RestoreBootstrap);
389       jcr->RestoreBootstrap = NULL;
390    }
391    if (jcr->client_uname) {
392       free_pool_memory(jcr->client_uname);
393       jcr->client_uname = NULL;
394    }
395    pthread_cond_destroy(&jcr->term_wait);
396    Dmsg0(200, "End dird free_jcr\n");
397 }
398
399 /*
400  * Set some defaults in the JCR necessary to
401  * run. These items are pulled from the job
402  * definition as defaults, but can be overridden
403  * later either by the Run record in the Schedule resource,
404  * or by the Console program.
405  */
406 void set_jcr_defaults(JCR *jcr, JOB *job)
407 {
408    jcr->job = job;
409    jcr->JobType = job->JobType;
410    switch (jcr->JobType) {
411    case JT_ADMIN:
412    case JT_RESTORE:
413       jcr->JobLevel = L_NONE;
414       break;
415    default:
416       jcr->JobLevel = job->level;
417       break;
418    }
419    jcr->JobPriority = job->Priority;
420    jcr->store = job->storage;
421    jcr->client = job->client;
422    if (!jcr->client_name) {
423       jcr->client_name = get_pool_memory(PM_NAME);
424    }
425    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
426    jcr->pool = job->pool;
427    jcr->catalog = job->client->catalog;
428    jcr->fileset = job->fileset;
429    jcr->messages = job->messages; 
430    if (jcr->RestoreBootstrap) {
431       free(jcr->RestoreBootstrap);
432       jcr->RestoreBootstrap = NULL;
433    }
434    /* This can be overridden by Console program */
435    if (job->RestoreBootstrap) {
436       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
437    }
438    /* If no default level given, set one */
439    if (jcr->JobLevel == 0) {
440       switch (jcr->JobType) {
441       case JT_VERIFY:
442          jcr->JobLevel = L_VERIFY_CATALOG;
443          break;
444       case JT_BACKUP:
445          jcr->JobLevel = L_INCREMENTAL;
446          break;
447       case JT_RESTORE:
448       case JT_ADMIN:
449          jcr->JobLevel = L_NONE;
450          break;
451       default:
452          break;
453       }
454    }
455 }