]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
bbbefa8997e049899c29c5d712e3060a4b362ae5
[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-2004 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 static void job_monitor_watchdog(watchdog_t *self);
35 static void job_monitor_destructor(watchdog_t *self);
36 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr);
37 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr);
38
39 /* Exported subroutines */
40
41 /* Imported subroutines */
42 extern void term_scheduler();
43 extern void term_ua_server();
44 extern int do_backup(JCR *jcr);
45 extern int do_admin(JCR *jcr);
46 extern int do_restore(JCR *jcr);
47 extern int do_verify(JCR *jcr);
48
49 /* Imported variables */
50 extern time_t watchdog_time;
51
52 jobq_t  job_queue;
53
54 void init_job_server(int max_workers)
55 {
56    int stat;
57    watchdog_t *wd;
58    
59    if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
60       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), strerror(stat));
61    }
62    if ((wd = new_watchdog()) == NULL) {
63       Emsg0(M_ABORT, 0, _("Could not init job monitor watchdogs\n"));
64    }
65    wd->callback = job_monitor_watchdog;
66    wd->destructor = job_monitor_destructor;
67    wd->one_shot = false;
68    wd->interval = 60;
69    wd->data = new_control_jcr("*JobMonitor*", JT_SYSTEM);
70    register_watchdog(wd);
71 }
72
73 /*
74  * Run a job -- typically called by the scheduler, but may also
75  *              be called by the UA (Console program).
76  *
77  *  Returns: 0 on failure
78  *           JobId on success
79  *
80  */
81 JobId_t run_job(JCR *jcr)
82 {
83    int stat, errstat;
84    JobId_t JobId = 0;
85
86    P(jcr->mutex);
87    sm_check(__FILE__, __LINE__, true);
88    init_msg(jcr, jcr->messages);
89
90    /* Initialize termination condition variable */
91    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
92       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
93       goto bail_out;
94    }
95    jcr->term_wait_inited = true;
96
97    /*
98     * Open database
99     */
100    Dmsg0(50, "Open database\n");
101    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
102                             jcr->catalog->db_password, jcr->catalog->db_address,
103                             jcr->catalog->db_port, jcr->catalog->db_socket);
104    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
105       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
106                  jcr->catalog->db_name);
107       if (jcr->db) {
108          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
109       }
110       goto bail_out;
111    }
112    Dmsg0(50, "DB opened\n");
113
114    /*
115     * Create Job record  
116     */
117    create_unique_job_name(jcr, jcr->job->hdr.name);
118    set_jcr_job_status(jcr, JS_Created);
119    init_jcr_job_record(jcr);
120    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
121       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
122       goto bail_out;
123    }
124    JobId = jcr->JobId = jcr->jr.JobId;
125
126    Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
127        jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
128    Dmsg0(200, "Add jrc to work queue\n");
129
130    /* Queue the job to be run */
131    if ((stat = jobq_add(&job_queue, jcr)) != 0) {
132       Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), strerror(stat));
133       JobId = 0;
134       goto bail_out;
135    }
136    Dmsg0(100, "Done run_job()\n");
137
138    V(jcr->mutex);
139    return JobId;
140
141 bail_out:
142    set_jcr_job_status(jcr, JS_ErrorTerminated);
143    V(jcr->mutex);
144    return JobId;
145
146 }
147
148
149 /* 
150  * This is the engine called by jobq.c:jobq_add() when we were pulled                
151  *  from the work queue.
152  *  At this point, we are running in our own thread and all
153  *    necessary resources are allocated -- see jobq.c
154  */
155 static void *job_thread(void *arg)
156 {
157    JCR *jcr = (JCR *)arg;
158
159    jcr->my_thread_id = pthread_self();
160    pthread_detach(jcr->my_thread_id);
161    sm_check(__FILE__, __LINE__, true);
162
163    for ( ;; ) {
164       Dmsg0(200, "=====Start Job=========\n");
165       jcr->start_time = time(NULL);      /* set the real start time */
166       set_jcr_job_status(jcr, JS_Running);
167
168       if (job_canceled(jcr)) {
169          update_job_end_record(jcr);
170       } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
171           (utime_t)(jcr->start_time - jcr->sched_time)) {
172          Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
173          set_jcr_job_status(jcr, JS_Canceled);
174          update_job_end_record(jcr);
175       } else {
176
177          /* Run Job */
178          if (jcr->job->RunBeforeJob) {
179             POOLMEM *before = get_pool_memory(PM_FNAME);
180             int status;
181             BPIPE *bpipe;
182             char line[MAXSTRING];
183             
184             before = edit_job_codes(jcr, before, jcr->job->RunBeforeJob, "");
185             bpipe = open_bpipe(before, 0, "r");
186             free_pool_memory(before);
187             while (fgets(line, sizeof(line), bpipe->rfd)) {
188                Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
189             }
190             status = close_bpipe(bpipe);
191             if (status != 0) {
192                Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
193                   status);
194                set_jcr_job_status(jcr, JS_FatalError);
195                update_job_end_record(jcr);
196                goto bail_out;
197             }
198          }
199          switch (jcr->JobType) {
200          case JT_BACKUP:
201             do_backup(jcr);
202             if (jcr->JobStatus == JS_Terminated) {
203                do_autoprune(jcr);
204             }
205             break;
206          case JT_VERIFY:
207             do_verify(jcr);
208             if (jcr->JobStatus == JS_Terminated) {
209                do_autoprune(jcr);
210             }
211             break;
212          case JT_RESTORE:
213             do_restore(jcr);
214             if (jcr->JobStatus == JS_Terminated) {
215                do_autoprune(jcr);
216             }
217             break;
218          case JT_ADMIN:
219             do_admin(jcr);
220             if (jcr->JobStatus == JS_Terminated) {
221                do_autoprune(jcr);
222             }
223             break;
224          default:
225             Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
226             break;
227          }
228          if ((jcr->job->RunAfterJob && jcr->JobStatus == JS_Terminated) ||
229              (jcr->job->RunAfterFailedJob && jcr->JobStatus != JS_Terminated)) {
230             POOLMEM *after = get_pool_memory(PM_FNAME);
231             int status;
232             BPIPE *bpipe;
233             char line[MAXSTRING];
234             
235             if (jcr->JobStatus == JS_Terminated) {
236                after = edit_job_codes(jcr, after, jcr->job->RunAfterJob, "");
237             } else {
238                after = edit_job_codes(jcr, after, jcr->job->RunAfterFailedJob, "");
239             }
240             bpipe = open_bpipe(after, 0, "r");
241             free_pool_memory(after);
242             while (fgets(line, sizeof(line), bpipe->rfd)) {
243                Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
244             }
245             status = close_bpipe(bpipe);
246             /*
247              * Note, if we get an error here, do not mark the
248              *  job in error, simply report the error condition.   
249              */
250             if (status != 0) {
251                if (jcr->JobStatus == JS_Terminated) {
252                   Jmsg(jcr, M_WARNING, 0, _("RunAfterJob returned non-zero status=%d\n"),
253                        status);
254                } else {
255                   Jmsg(jcr, M_FATAL, 0, _("RunAfterFailedJob returned non-zero status=%d\n"),
256                        status);
257                }
258             }
259          }
260          /* Send off any queued messages */
261          if (jcr->msg_queue->size() > 0) {
262             dequeue_messages(jcr);
263          }
264       }
265 bail_out:
266       break;
267    }
268
269    Dmsg0(50, "======== End Job ==========\n");
270    sm_check(__FILE__, __LINE__, true);
271    return NULL;
272 }
273
274
275 /*
276  * Cancel a job -- typically called by the UA (Console program), but may also
277  *              be called by the job watchdog.
278  * 
279  *  Returns: 1 if cancel appears to be successful
280  *           0 on failure. Message sent to ua->jcr.
281  */
282 int cancel_job(UAContext *ua, JCR *jcr)
283 {
284    BSOCK *sd, *fd;
285
286    switch (jcr->JobStatus) {
287    case JS_Created:
288    case JS_WaitJobRes:
289    case JS_WaitClientRes:
290    case JS_WaitStoreRes:
291    case JS_WaitPriority:
292    case JS_WaitMaxJobs:
293    case JS_WaitStartTime:
294       set_jcr_job_status(jcr, JS_Canceled);
295       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
296               jcr->JobId, jcr->Job);
297       jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
298       return 1;
299          
300    default:
301       set_jcr_job_status(jcr, JS_Canceled);
302
303       /* Cancel File daemon */
304       if (jcr->file_bsock) {
305          ua->jcr->client = jcr->client;
306          if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
307             bsendmsg(ua, _("Failed to connect to File daemon.\n"));
308             return 0;
309          }
310          Dmsg0(200, "Connected to file daemon\n");
311          fd = ua->jcr->file_bsock;
312          bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
313          while (bnet_recv(fd) >= 0) {
314             bsendmsg(ua, "%s", fd->msg);
315          }
316          bnet_sig(fd, BNET_TERMINATE);
317          bnet_close(fd);
318          ua->jcr->file_bsock = NULL;
319       }
320
321       /* Cancel Storage daemon */
322       if (jcr->store_bsock) {
323          ua->jcr->store = jcr->store;
324          if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
325             bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
326             return 0;
327          }
328          Dmsg0(200, "Connected to storage daemon\n");
329          sd = ua->jcr->store_bsock;
330          bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
331          while (bnet_recv(sd) >= 0) {
332             bsendmsg(ua, "%s", sd->msg);
333          }
334          bnet_sig(sd, BNET_TERMINATE);
335          bnet_close(sd);
336          ua->jcr->store_bsock = NULL;
337       }
338    }
339
340    return 1;
341 }
342
343
344 static void job_monitor_destructor(watchdog_t *self)
345 {
346    JCR *control_jcr = (JCR *) self->data;
347
348    free_jcr(control_jcr);
349 }
350
351 static void job_monitor_watchdog(watchdog_t *self)
352 {
353    JCR *control_jcr, *jcr;
354
355    control_jcr = (JCR *)self->data;
356
357    Dmsg1(400, "job_monitor_watchdog %p called\n", self);
358
359    lock_jcr_chain();
360
361    foreach_jcr(jcr) {
362       bool cancel;
363
364       if (jcr->JobId == 0) {
365          Dmsg2(400, "Skipping JCR %p (%s) with JobId 0\n",
366                jcr, jcr->Job);
367          /* Keep reference counts correct */
368          free_locked_jcr(jcr);
369          continue;
370       }
371
372       /* check MaxWaitTime */
373       cancel = job_check_maxwaittime(control_jcr, jcr);
374
375       /* check MaxRunTime */
376       cancel |= job_check_maxruntime(control_jcr, jcr);
377
378       if (cancel) {
379          Dmsg3(200, "Cancelling JCR %p jobid %d (%s)\n",
380                jcr, jcr->JobId, jcr->Job);
381
382          UAContext *ua = new_ua_context(jcr);
383          ua->jcr = control_jcr;
384          cancel_job(ua, jcr);
385          free_ua_context(ua);
386
387          Dmsg1(200, "Have cancelled JCR %p\n", jcr);
388       }
389
390       /* Keep reference counts correct */
391       free_locked_jcr(jcr);
392    }
393    unlock_jcr_chain();
394 }
395
396 /*
397  * Check if the maxwaittime has expired and it is possible
398  *  to cancel the job.
399  */
400 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
401 {
402    bool cancel = false;
403
404    if (jcr->job->MaxWaitTime == 0) {
405       return false;
406    }
407    if ((watchdog_time - jcr->start_time) < jcr->job->MaxWaitTime) {
408       Dmsg3(200, "Job %p (%s) with MaxWaitTime %d not expired\n",
409             jcr, jcr->Job, jcr->job->MaxWaitTime);
410       return false;
411    }
412    Dmsg3(200, "Job %d (%s): MaxWaitTime of %d seconds exceeded, "
413          "checking status\n",
414          jcr->JobId, jcr->Job, jcr->job->MaxWaitTime);
415    switch (jcr->JobStatus) {
416    case JS_Created:
417    case JS_Blocked:
418    case JS_WaitFD:
419    case JS_WaitSD:
420    case JS_WaitStoreRes:
421    case JS_WaitClientRes:
422    case JS_WaitJobRes:
423    case JS_WaitPriority:
424    case JS_WaitMaxJobs:
425    case JS_WaitStartTime:
426       cancel = true;
427       Dmsg0(200, "JCR blocked in #1\n");
428       break;
429    case JS_Running:
430       Dmsg0(200, "JCR running, checking SD status\n");
431       switch (jcr->SDJobStatus) {
432       case JS_WaitMount:
433       case JS_WaitMedia:
434       case JS_WaitFD:
435          cancel = true;
436          Dmsg0(200, "JCR blocked in #2\n");
437          break;
438       default:
439          Dmsg0(200, "JCR not blocked in #2\n");
440          break;
441       }
442       break;
443    case JS_Terminated:
444    case JS_ErrorTerminated:
445    case JS_Canceled:
446    case JS_FatalError:
447       Dmsg0(200, "JCR already dead in #3\n");
448       break;
449    default:
450       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
451             jcr->JobStatus);
452    }
453    Dmsg3(200, "MaxWaitTime result: %scancel JCR %p (%s)\n",
454          cancel ? "" : "do not ", jcr, jcr->job);
455
456    return cancel;
457 }
458
459 /*
460  * Check if maxruntime has expired and if the job can be
461  *   canceled.
462  */
463 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
464 {
465    bool cancel = false;
466
467    if (jcr->job->MaxRunTime == 0) {
468       return false;
469    }
470    if ((watchdog_time - jcr->start_time) < jcr->job->MaxRunTime) {
471       Dmsg3(200, "Job %p (%s) with MaxRunTime %d not expired\n",
472             jcr, jcr->Job, jcr->job->MaxRunTime);
473       return false;
474    }
475
476    switch (jcr->JobStatus) {
477    case JS_Created:
478    case JS_Running:
479    case JS_Blocked:
480    case JS_WaitFD:
481    case JS_WaitSD:
482    case JS_WaitStoreRes:
483    case JS_WaitClientRes:
484    case JS_WaitJobRes:
485    case JS_WaitPriority:
486    case JS_WaitMaxJobs:
487    case JS_WaitStartTime:
488    case JS_Differences:
489       cancel = true;
490       break;
491    case JS_Terminated:
492    case JS_ErrorTerminated:
493    case JS_Canceled:
494    case JS_FatalError:
495       cancel = false;
496       break;
497    default:
498       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
499             jcr->JobStatus);
500    }
501
502    Dmsg3(200, "MaxRunTime result: %scancel JCR %p (%s)\n",
503          cancel ? "" : "do not ", jcr, jcr->job);
504
505    return cancel;
506 }
507
508
509 /*
510  * Get or create a Client record for this Job
511  */
512 bool get_or_create_client_record(JCR *jcr)
513 {
514    CLIENT_DBR cr;
515
516    memset(&cr, 0, sizeof(cr));
517    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
518    cr.AutoPrune = jcr->client->AutoPrune;
519    cr.FileRetention = jcr->client->FileRetention;
520    cr.JobRetention = jcr->client->JobRetention;
521    if (!jcr->client_name) {
522       jcr->client_name = get_pool_memory(PM_NAME);
523    }
524    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
525    if (!db_create_client_record(jcr, jcr->db, &cr)) {
526       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
527          db_strerror(jcr->db));
528       return false;
529    }
530    jcr->jr.ClientId = cr.ClientId;
531    if (cr.Uname[0]) {
532       if (!jcr->client_uname) {
533          jcr->client_uname = get_pool_memory(PM_NAME);
534       }
535       pm_strcpy(&jcr->client_uname, cr.Uname);
536    }
537    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
538       jcr->jr.ClientId);
539    return true;
540 }
541
542 bool get_or_create_fileset_record(JCR *jcr, FILESET_DBR *fsr)
543 {
544    /*
545     * Get or Create FileSet record
546     */
547    memset(fsr, 0, sizeof(FILESET_DBR));
548    bstrncpy(fsr->FileSet, jcr->fileset->hdr.name, sizeof(fsr->FileSet));
549    if (jcr->fileset->have_MD5) {
550       struct MD5Context md5c;
551       unsigned char signature[16];
552       memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
553       MD5Final(signature, &md5c);
554       bin_to_base64(fsr->MD5, (char *)signature, 16); /* encode 16 bytes */
555       bstrncpy(jcr->fileset->MD5, fsr->MD5, sizeof(jcr->fileset->MD5));
556    } else {
557       Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 signature not found.\n"));
558    }
559    if (!db_create_fileset_record(jcr, jcr->db, fsr)) {
560       Jmsg(jcr, M_ERROR, 0, _("Could not create FileSet \"%s\" record. ERR=%s\n"), 
561          fsr->FileSet, db_strerror(jcr->db));
562       return false;
563    }   
564    jcr->jr.FileSetId = fsr->FileSetId;
565    if (fsr->created) {
566       Jmsg(jcr, M_INFO, 0, _("Created new FileSet record \"%s\" %s\n"), 
567          fsr->FileSet, fsr->cCreateTime);
568    }
569    Dmsg2(119, "Created FileSet %s record %u\n", jcr->fileset->hdr.name, 
570       jcr->jr.FileSetId);
571    return true;
572 }
573
574 void init_jcr_job_record(JCR *jcr)
575 {
576    jcr->jr.SchedTime = jcr->sched_time;
577    jcr->jr.StartTime = jcr->start_time;
578    jcr->jr.EndTime = 0;               /* perhaps rescheduled, clear it */
579    jcr->jr.JobType = jcr->JobType;
580    jcr->jr.JobLevel = jcr->JobLevel;
581    jcr->jr.JobStatus = jcr->JobStatus;
582    jcr->jr.JobId = jcr->JobId;
583    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
584    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
585 }
586
587 /*
588  * Write status and such in DB
589  */
590 void update_job_end_record(JCR *jcr)
591 {
592    jcr->jr.EndTime = time(NULL);
593    jcr->end_time = jcr->jr.EndTime;
594    jcr->jr.JobId = jcr->JobId;
595    jcr->jr.JobStatus = jcr->JobStatus;
596    jcr->jr.JobFiles = jcr->JobFiles;
597    jcr->jr.JobBytes = jcr->JobBytes;
598    jcr->jr.VolSessionId = jcr->VolSessionId;
599    jcr->jr.VolSessionTime = jcr->VolSessionTime;
600    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
601       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
602          db_strerror(jcr->db));
603    }
604 }
605
606 /*
607  * Takes base_name and appends (unique) current
608  *   date and time to form unique job name.
609  *
610  *  Returns: unique job name in jcr->Job
611  *    date/time in jcr->start_time
612  */
613 void create_unique_job_name(JCR *jcr, const char *base_name)
614 {
615    /* Job start mutex */
616    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
617    static time_t last_start_time = 0;
618    time_t now;
619    struct tm tm;
620    char dt[MAX_TIME_LENGTH];
621    char name[MAX_NAME_LENGTH];
622    char *p;
623
624    /* Guarantee unique start time -- maximum one per second, and
625     * thus unique Job Name 
626     */
627    P(mutex);                          /* lock creation of jobs */
628    now = time(NULL);
629    while (now == last_start_time) {
630       bmicrosleep(0, 500000);
631       now = time(NULL);
632    }
633    last_start_time = now;
634    V(mutex);                          /* allow creation of jobs */
635    jcr->start_time = now;
636    /* Form Unique JobName */
637    localtime_r(&now, &tm);
638    /* Use only characters that are permitted in Windows filenames */
639    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
640    bstrncpy(name, base_name, sizeof(name));
641    name[sizeof(name)-22] = 0;          /* truncate if too long */
642    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
643    /* Convert spaces into underscores */
644    for (p=jcr->Job; *p; p++) {
645       if (*p == ' ') {
646          *p = '_';
647       }
648    }
649 }
650
651 /*
652  * Free the Job Control Record if no one is still using it.
653  *  Called from main free_jcr() routine in src/lib/jcr.c so
654  *  that we can do our Director specific cleanup of the jcr.
655  */
656 void dird_free_jcr(JCR *jcr)
657 {
658    Dmsg0(200, "Start dird free_jcr\n");
659
660    if (jcr->sd_auth_key) {
661       free(jcr->sd_auth_key);
662       jcr->sd_auth_key = NULL;
663    }
664    if (jcr->where) {
665       free(jcr->where);
666       jcr->where = NULL;
667    }
668    if (jcr->file_bsock) {
669       Dmsg0(200, "Close File bsock\n");
670       bnet_close(jcr->file_bsock);
671       jcr->file_bsock = NULL;
672    }
673    if (jcr->store_bsock) {
674       Dmsg0(200, "Close Store bsock\n");
675       bnet_close(jcr->store_bsock);
676       jcr->store_bsock = NULL;
677    }
678    if (jcr->fname) {  
679       Dmsg0(200, "Free JCR fname\n");
680       free_pool_memory(jcr->fname);
681       jcr->fname = NULL;
682    }
683    if (jcr->stime) {
684       Dmsg0(200, "Free JCR stime\n");
685       free_pool_memory(jcr->stime);
686       jcr->stime = NULL;
687    }
688    if (jcr->RestoreBootstrap) {
689       free(jcr->RestoreBootstrap);
690       jcr->RestoreBootstrap = NULL;
691    }
692    if (jcr->client_uname) {
693       free_pool_memory(jcr->client_uname);
694       jcr->client_uname = NULL;
695    }
696    if (jcr->term_wait_inited) {
697       pthread_cond_destroy(&jcr->term_wait);
698    }
699    jcr->job_end_push.destroy();
700    Dmsg0(200, "End dird free_jcr\n");
701 }
702
703 /*
704  * Set some defaults in the JCR necessary to
705  * run. These items are pulled from the job
706  * definition as defaults, but can be overridden
707  * later either by the Run record in the Schedule resource,
708  * or by the Console program.
709  */
710 void set_jcr_defaults(JCR *jcr, JOB *job)
711 {
712    jcr->job = job;
713    jcr->JobType = job->JobType;
714    switch (jcr->JobType) {
715    case JT_ADMIN:
716    case JT_RESTORE:
717       jcr->JobLevel = L_NONE;
718       break;
719    default:
720       jcr->JobLevel = job->level;
721       break;
722    }
723    jcr->JobPriority = job->Priority;
724    jcr->store = job->storage;
725    jcr->client = job->client;
726    if (!jcr->client_name) {
727       jcr->client_name = get_pool_memory(PM_NAME);
728    }
729    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
730    jcr->pool = job->pool;
731    jcr->full_pool = job->full_pool;
732    jcr->inc_pool = job->inc_pool;
733    jcr->dif_pool = job->dif_pool;
734    jcr->catalog = job->client->catalog;
735    jcr->fileset = job->fileset;
736    jcr->messages = job->messages; 
737    jcr->spool_data = job->spool_data;
738    if (jcr->RestoreBootstrap) {
739       free(jcr->RestoreBootstrap);
740       jcr->RestoreBootstrap = NULL;
741    }
742    /* This can be overridden by Console program */
743    if (job->RestoreBootstrap) {
744       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
745    }
746    /* If no default level given, set one */
747    if (jcr->JobLevel == 0) {
748       switch (jcr->JobType) {
749       case JT_VERIFY:
750          jcr->JobLevel = L_VERIFY_CATALOG;
751          break;
752       case JT_BACKUP:
753          jcr->JobLevel = L_INCREMENTAL;
754          break;
755       case JT_RESTORE:
756       case JT_ADMIN:
757          jcr->JobLevel = L_NONE;
758          break;
759       default:
760          break;
761       }
762    }
763 }