]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
kes Mark build-depkgs-mingw3 executable in configure process.
[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    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version two of the GNU General Public
18    License as published by the Free Software Foundation plus additions
19    that are listed in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of John Walker.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 #include "bacula.h"
38 #include "dird.h"
39
40 /* Forward referenced subroutines */
41 static void *job_thread(void *arg);
42 static void job_monitor_watchdog(watchdog_t *self);
43 static void job_monitor_destructor(watchdog_t *self);
44 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr);
45 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr);
46
47 /* Imported subroutines */
48 extern void term_scheduler();
49 extern void term_ua_server();
50
51 /* Imported variables */
52
53 jobq_t job_queue;
54
55 void init_job_server(int max_workers)
56 {
57    int stat;
58    watchdog_t *wd;
59
60    if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
61       berrno be;
62       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), be.strerror(stat));
63    }
64    wd = new_watchdog();
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 void term_job_server()
74 {
75    jobq_destroy(&job_queue);          /* ignore any errors */
76 }
77
78 /*
79  * Run a job -- typically called by the scheduler, but may also
80  *              be called by the UA (Console program).
81  *
82  *  Returns: 0 on failure
83  *           JobId on success
84  *
85  */
86 JobId_t run_job(JCR *jcr)
87 {
88    int stat;
89    if (setup_job(jcr)) {
90       Dmsg0(200, "Add jrc to work queue\n");
91       /* Queue the job to be run */
92       if ((stat = jobq_add(&job_queue, jcr)) != 0) {
93          berrno be;
94          Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.strerror(stat));
95          return 0;
96       }
97       return jcr->JobId;
98    }
99    return 0;
100 }            
101
102 bool setup_job(JCR *jcr) 
103 {
104    int errstat;
105
106    jcr->lock();
107    sm_check(__FILE__, __LINE__, true);
108    init_msg(jcr, jcr->messages);
109
110    /* Initialize termination condition variable */
111    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
112       berrno be;
113       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.strerror(errstat));
114       goto bail_out;
115    }
116    jcr->term_wait_inited = true;
117
118    create_unique_job_name(jcr, jcr->job->name());
119    set_jcr_job_status(jcr, JS_Created);
120    jcr->unlock();
121
122    /*
123     * Open database
124     */
125    Dmsg0(50, "Open database\n");
126    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
127                             jcr->catalog->db_password, jcr->catalog->db_address,
128                             jcr->catalog->db_port, jcr->catalog->db_socket,
129                             jcr->catalog->mult_db_connections);
130    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
131       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
132                  jcr->catalog->db_name);
133       if (jcr->db) {
134          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
135       }
136       goto bail_out;
137    }
138    Dmsg0(50, "DB opened\n");
139
140    if (!jcr->fname) {
141       jcr->fname = get_pool_memory(PM_FNAME);
142    }
143    if (!jcr->pool_source) {
144       jcr->pool_source = get_pool_memory(PM_MESSAGE);
145       pm_strcpy(jcr->pool_source, _("unknown source"));
146    }
147    Dmsg2(500, "pool=%s (From %s)\n", jcr->pool->name(), jcr->pool_source);
148    if (jcr->JobType == JT_MIGRATE) {
149       if (!jcr->rpool_source) {
150          jcr->rpool_source = get_pool_memory(PM_MESSAGE);
151          pm_strcpy(jcr->rpool_source, _("unknown source"));
152       }
153    }
154
155    /*
156     * Create Job record
157     */
158    init_jcr_job_record(jcr);
159    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
160       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
161       goto bail_out;
162    }
163    jcr->JobId = jcr->jr.JobId;
164    Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
165        jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
166
167    if (!get_or_create_client_record(jcr)) {
168       goto bail_out;
169    }
170
171    generate_daemon_event(jcr, "JobStart");
172
173    if (job_canceled(jcr)) {
174       goto bail_out;
175    }
176
177
178    /*
179     * Now, do pre-run stuff, like setting job level (Inc/diff, ...)
180     *  this allows us to setup a proper job start record for restarting
181     *  in case of later errors.
182     */
183    switch (jcr->JobType) {
184    case JT_BACKUP:
185       if (!do_backup_init(jcr)) {
186          backup_cleanup(jcr, JS_ErrorTerminated);
187       }
188       break;
189    case JT_VERIFY:
190       if (!do_verify_init(jcr)) {
191          verify_cleanup(jcr, JS_ErrorTerminated);
192       }
193       break;
194    case JT_RESTORE:
195       if (!do_restore_init(jcr)) {
196          restore_cleanup(jcr, JS_ErrorTerminated);
197       }
198       break;
199    case JT_ADMIN:
200       if (!do_admin_init(jcr)) {
201          admin_cleanup(jcr, JS_ErrorTerminated);
202       }
203       break;
204    case JT_MIGRATE:
205       if (!do_migration_init(jcr)) { 
206          migration_cleanup(jcr, JS_ErrorTerminated);
207       }
208       break;
209    default:
210       Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->JobType);
211       set_jcr_job_status(jcr, JS_ErrorTerminated);
212       break;
213    }
214
215    generate_job_event(jcr, "JobInit");
216    return true;
217
218 bail_out:
219    return false;
220 }
221
222
223 /*
224  * This is the engine called by jobq.c:jobq_add() when we were pulled
225  *  from the work queue.
226  *  At this point, we are running in our own thread and all
227  *    necessary resources are allocated -- see jobq.c
228  */
229 static void *job_thread(void *arg)
230 {
231    JCR *jcr = (JCR *)arg;
232
233    jcr->my_thread_id = pthread_self();
234    pthread_detach(jcr->my_thread_id);
235    sm_check(__FILE__, __LINE__, true);
236
237    Dmsg0(200, "=====Start Job=========\n");
238    set_jcr_job_status(jcr, JS_Running);   /* this will be set only if no error */
239    jcr->start_time = time(NULL);      /* set the real start time */
240    jcr->jr.StartTime = jcr->start_time;
241
242    if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
243        (utime_t)(jcr->start_time - jcr->sched_time)) {
244       Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
245       set_jcr_job_status(jcr, JS_Canceled);
246    }
247
248    /* TODO : check if it is used somewhere */
249    if (jcr->job->RunScripts == NULL) {
250       Dmsg0(200, "Warning, job->RunScripts is empty\n");
251       jcr->job->RunScripts = New(alist(10, not_owned_by_alist));
252    }
253
254    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
255       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
256    }
257
258    /* Run any script BeforeJob on dird */
259    run_scripts(jcr, jcr->job->RunScripts, "BeforeJob");
260
261    if (job_canceled(jcr)) {
262       update_job_end_record(jcr);
263
264    } else {
265       /*
266        * We re-update the job start record so that the start
267        *  time is set after the run before job.  This avoids
268        *  that any files created by the run before job will
269        *  be saved twice.  They will be backed up in the current
270        *  job, but not in the next one unless they are changed.
271        *  Without this, they will be backed up in this job and
272        *  in the next job run because in that case, their date
273        *   is after the start of this run.
274        */
275       jcr->start_time = time(NULL);
276       jcr->jr.StartTime = jcr->start_time;
277       if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
278          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
279       }
280       generate_job_event(jcr, "JobRun");
281
282       switch (jcr->JobType) {
283       case JT_BACKUP:
284          if (do_backup(jcr)) {
285             do_autoprune(jcr);
286          } else {
287             backup_cleanup(jcr, JS_ErrorTerminated);
288          }
289          break;
290       case JT_VERIFY:
291          if (do_verify(jcr)) {
292             do_autoprune(jcr);
293          } else {
294             verify_cleanup(jcr, JS_ErrorTerminated);
295          }
296          break;
297       case JT_RESTORE:
298          if (do_restore(jcr)) {
299             do_autoprune(jcr);
300          } else {
301             restore_cleanup(jcr, JS_ErrorTerminated);
302          }
303          break;
304       case JT_ADMIN:
305          if (do_admin(jcr)) {
306             do_autoprune(jcr);
307          } else {
308             admin_cleanup(jcr, JS_ErrorTerminated);
309          }
310          break;
311       case JT_MIGRATE:
312       case JT_COPY:
313       case JT_ARCHIVE:
314          if (do_migration(jcr)) {
315             do_autoprune(jcr);
316          } else {
317             migration_cleanup(jcr, JS_ErrorTerminated);
318          }
319          break;
320       default:
321          Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->JobType);
322          break;
323       }
324
325       run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
326
327       /* Send off any queued messages */
328       if (jcr->msg_queue && jcr->msg_queue->size() > 0) {
329          dequeue_messages(jcr);
330       }
331    }
332
333    generate_daemon_event(jcr, "JobEnd");
334    Dmsg1(50, "======== End Job stat=%c ==========\n", jcr->JobStatus);
335    sm_check(__FILE__, __LINE__, true);
336    return NULL;
337 }
338
339
340 /*
341  * Cancel a job -- typically called by the UA (Console program), but may also
342  *              be called by the job watchdog.
343  *
344  *  Returns: true  if cancel appears to be successful
345  *           false on failure. Message sent to ua->jcr.
346  */
347 bool cancel_job(UAContext *ua, JCR *jcr)
348 {
349    BSOCK *sd, *fd;
350
351    set_jcr_job_status(jcr, JS_Canceled);
352
353    switch (jcr->JobStatus) {
354    case JS_Created:
355    case JS_WaitJobRes:
356    case JS_WaitClientRes:
357    case JS_WaitStoreRes:
358    case JS_WaitPriority:
359    case JS_WaitMaxJobs:
360    case JS_WaitStartTime:
361       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
362               jcr->JobId, jcr->Job);
363       jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
364       return true;
365
366    default:
367       /* Cancel File daemon */
368       if (jcr->file_bsock) {
369          ua->jcr->client = jcr->client;
370          if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
371             bsendmsg(ua, _("Failed to connect to File daemon.\n"));
372             return 0;
373          }
374          Dmsg0(200, "Connected to file daemon\n");
375          fd = ua->jcr->file_bsock;
376          bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
377          while (bnet_recv(fd) >= 0) {
378             bsendmsg(ua, "%s", fd->msg);
379          }
380          bnet_sig(fd, BNET_TERMINATE);
381          bnet_close(fd);
382          ua->jcr->file_bsock = NULL;
383       }
384
385       /* Cancel Storage daemon */
386       if (jcr->store_bsock) {
387          if (!ua->jcr->wstorage) {
388             if (jcr->rstorage) {
389                copy_wstorage(ua->jcr, jcr->rstorage, _("Job resource")); 
390             } else {
391                copy_wstorage(ua->jcr, jcr->wstorage, _("Job resource")); 
392             }
393          } else {
394             USTORE store;
395             if (jcr->rstorage) {
396                store.store = jcr->rstore;
397                pm_strcpy(store.store_source, jcr->rstore_source);
398             } else {
399                store.store = jcr->wstore;
400                pm_strcpy(store.store_source, jcr->wstore_source);
401             }
402             set_wstorage(ua->jcr, &store);
403          }
404
405          if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
406             bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
407             return false;
408          }
409          Dmsg0(200, "Connected to storage daemon\n");
410          sd = ua->jcr->store_bsock;
411          bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
412          while (bnet_recv(sd) >= 0) {
413             bsendmsg(ua, "%s", sd->msg);
414          }
415          bnet_sig(sd, BNET_TERMINATE);
416          bnet_close(sd);
417          ua->jcr->store_bsock = NULL;
418       }
419    }
420
421    return true;
422 }
423
424
425 static void job_monitor_destructor(watchdog_t *self)
426 {
427    JCR *control_jcr = (JCR *)self->data;
428
429    free_jcr(control_jcr);
430 }
431
432 static void job_monitor_watchdog(watchdog_t *self)
433 {
434    JCR *control_jcr, *jcr;
435
436    control_jcr = (JCR *)self->data;
437
438    Dmsg1(800, "job_monitor_watchdog %p called\n", self);
439
440    foreach_jcr(jcr) {
441       bool cancel;
442
443       if (jcr->JobId == 0) {
444          Dmsg2(800, "Skipping JCR %p (%s) with JobId 0\n",
445                jcr, jcr->Job);
446          continue;
447       }
448
449       /* check MaxWaitTime */
450       cancel = job_check_maxwaittime(control_jcr, jcr);
451
452       /* check MaxRunTime */
453       cancel |= job_check_maxruntime(control_jcr, jcr);
454
455       if (cancel) {
456          Dmsg3(800, "Cancelling JCR %p jobid %d (%s)\n",
457                jcr, jcr->JobId, jcr->Job);
458
459          UAContext *ua = new_ua_context(jcr);
460          ua->jcr = control_jcr;
461          cancel_job(ua, jcr);
462          free_ua_context(ua);
463
464          Dmsg2(800, "Have cancelled JCR %p Job=%d\n", jcr, jcr->JobId);
465       }
466
467       /* Keep reference counts correct */
468    }
469    endeach_jcr(jcr);
470 }
471
472 /*
473  * Check if the maxwaittime has expired and it is possible
474  *  to cancel the job.
475  */
476 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
477 {
478    bool cancel = false;
479    bool ok_to_cancel = false;
480    JOB *job = jcr->job;
481
482    if (job->MaxWaitTime == 0 && job->FullMaxWaitTime == 0 &&
483        job->IncMaxWaitTime == 0 && job->DiffMaxWaitTime == 0) {
484       return false;
485    } 
486    if (jcr->JobLevel == L_FULL && job->FullMaxWaitTime != 0 &&
487          (watchdog_time - jcr->start_time) >= job->FullMaxWaitTime) {
488       ok_to_cancel = true;
489    } else if (jcr->JobLevel == L_DIFFERENTIAL && job->DiffMaxWaitTime != 0 &&
490          (watchdog_time - jcr->start_time) >= job->DiffMaxWaitTime) {
491       ok_to_cancel = true;
492    } else if (jcr->JobLevel == L_INCREMENTAL && job->IncMaxWaitTime != 0 &&
493          (watchdog_time - jcr->start_time) >= job->IncMaxWaitTime) {
494       ok_to_cancel = true;
495    } else if (job->MaxWaitTime != 0 &&
496          (watchdog_time - jcr->start_time) >= job->MaxWaitTime) {
497       ok_to_cancel = true;
498    }
499    if (!ok_to_cancel) {
500       return false;
501    }
502    Dmsg3(800, "Job %d (%s): MaxWaitTime of %d seconds exceeded, "
503          "checking status\n",
504          jcr->JobId, jcr->Job, job->MaxWaitTime);
505    switch (jcr->JobStatus) {
506    case JS_Created:
507    case JS_Blocked:
508    case JS_WaitFD:
509    case JS_WaitSD:
510    case JS_WaitStoreRes:
511    case JS_WaitClientRes:
512    case JS_WaitJobRes:
513    case JS_WaitPriority:
514    case JS_WaitMaxJobs:
515    case JS_WaitStartTime:
516       cancel = true;
517       Dmsg0(200, "JCR blocked in #1\n");
518       break;
519    case JS_Running:
520       Dmsg0(800, "JCR running, checking SD status\n");
521       switch (jcr->SDJobStatus) {
522       case JS_WaitMount:
523       case JS_WaitMedia:
524       case JS_WaitFD:
525          cancel = true;
526          Dmsg0(800, "JCR blocked in #2\n");
527          break;
528       default:
529          Dmsg0(800, "JCR not blocked in #2\n");
530          break;
531       }
532       break;
533    case JS_Terminated:
534    case JS_ErrorTerminated:
535    case JS_Canceled:
536    case JS_FatalError:
537       Dmsg0(800, "JCR already dead in #3\n");
538       break;
539    default:
540       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
541             jcr->JobStatus);
542    }
543    Dmsg3(800, "MaxWaitTime result: %scancel JCR %p (%s)\n",
544          cancel ? "" : "do not ", jcr, jcr->job);
545
546    return cancel;
547 }
548
549 /*
550  * Check if maxruntime has expired and if the job can be
551  *   canceled.
552  */
553 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
554 {
555    bool cancel = false;
556
557    if (jcr->job->MaxRunTime == 0) {
558       return false;
559    }
560    if ((watchdog_time - jcr->start_time) < jcr->job->MaxRunTime) {
561       Dmsg3(200, "Job %p (%s) with MaxRunTime %d not expired\n",
562             jcr, jcr->Job, jcr->job->MaxRunTime);
563       return false;
564    }
565
566    switch (jcr->JobStatus) {
567    case JS_Created:
568    case JS_Running:
569    case JS_Blocked:
570    case JS_WaitFD:
571    case JS_WaitSD:
572    case JS_WaitStoreRes:
573    case JS_WaitClientRes:
574    case JS_WaitJobRes:
575    case JS_WaitPriority:
576    case JS_WaitMaxJobs:
577    case JS_WaitStartTime:
578    case JS_Differences:
579       cancel = true;
580       break;
581    case JS_Terminated:
582    case JS_ErrorTerminated:
583    case JS_Canceled:
584    case JS_FatalError:
585       cancel = false;
586       break;
587    default:
588       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
589             jcr->JobStatus);
590    }
591
592    Dmsg3(200, "MaxRunTime result: %scancel JCR %p (%s)\n",
593          cancel ? "" : "do not ", jcr, jcr->job);
594
595    return cancel;
596 }
597
598 /*
599  * Get or create a Pool record with the given name.
600  * Returns: 0 on error
601  *          poolid if OK
602  */
603 DBId_t get_or_create_pool_record(JCR *jcr, char *pool_name)
604 {
605    POOL_DBR pr;
606
607    memset(&pr, 0, sizeof(pr));
608    bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
609    Dmsg1(010, "get_or_create_pool=%s\n", pool_name);
610
611    while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
612       /* Try to create the pool */
613       if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
614          Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
615             db_strerror(jcr->db));
616          return 0;
617       } else {
618          Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
619       }
620    }
621    return pr.PoolId;
622 }
623
624 void apply_pool_overrides(JCR *jcr)
625 {
626    if (jcr->run_pool_override) {
627       pm_strcpy(jcr->pool_source, _("Run pool override"));
628    }
629    /*
630     * Apply any level related Pool selections
631     */
632    switch (jcr->JobLevel) {
633    case L_FULL:
634       if (jcr->full_pool) {
635          jcr->pool = jcr->full_pool;
636          if (jcr->run_full_pool_override) {
637             pm_strcpy(jcr->pool_source, _("Run FullPool override"));
638          } else {
639             pm_strcpy(jcr->pool_source, _("Job FullPool override"));
640          }
641       }
642       break;
643    case L_INCREMENTAL:
644       if (jcr->inc_pool) {
645          jcr->pool = jcr->inc_pool;
646          if (jcr->run_inc_pool_override) {
647             pm_strcpy(jcr->pool_source, _("Run IncPool override"));
648          } else {
649             pm_strcpy(jcr->pool_source, _("Job IncPool override"));
650          }
651       }
652       break;
653    case L_DIFFERENTIAL:
654       if (jcr->diff_pool) {
655          jcr->pool = jcr->diff_pool;
656          if (jcr->run_diff_pool_override) {
657             pm_strcpy(jcr->pool_source, _("Run DiffPool override"));
658          } else {
659             pm_strcpy(jcr->pool_source, _("Job DiffPool override"));
660          }
661       }
662       break;
663    }
664 }
665
666
667 /*
668  * Get or create a Client record for this Job
669  */
670 bool get_or_create_client_record(JCR *jcr)
671 {
672    CLIENT_DBR cr;
673
674    memset(&cr, 0, sizeof(cr));
675    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
676    cr.AutoPrune = jcr->client->AutoPrune;
677    cr.FileRetention = jcr->client->FileRetention;
678    cr.JobRetention = jcr->client->JobRetention;
679    if (!jcr->client_name) {
680       jcr->client_name = get_pool_memory(PM_NAME);
681    }
682    pm_strcpy(jcr->client_name, jcr->client->hdr.name);
683    if (!db_create_client_record(jcr, jcr->db, &cr)) {
684       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"),
685          db_strerror(jcr->db));
686       return false;
687    }
688    jcr->jr.ClientId = cr.ClientId;
689    if (cr.Uname[0]) {
690       if (!jcr->client_uname) {
691          jcr->client_uname = get_pool_memory(PM_NAME);
692       }
693       pm_strcpy(jcr->client_uname, cr.Uname);
694    }
695    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name,
696       jcr->jr.ClientId);
697    return true;
698 }
699
700 bool get_or_create_fileset_record(JCR *jcr)
701 {
702    FILESET_DBR fsr;
703    /*
704     * Get or Create FileSet record
705     */
706    memset(&fsr, 0, sizeof(FILESET_DBR));
707    bstrncpy(fsr.FileSet, jcr->fileset->hdr.name, sizeof(fsr.FileSet));
708    if (jcr->fileset->have_MD5) {
709       struct MD5Context md5c;
710       unsigned char digest[MD5HashSize];
711       memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
712       MD5Final(digest, &md5c);
713       /*
714        * Keep the flag (last arg) set to false otherwise old FileSets will
715        * get new MD5 sums and the user will get Full backups on everything
716        */
717       bin_to_base64(fsr.MD5, sizeof(fsr.MD5), (char *)digest, MD5HashSize, false);
718       bstrncpy(jcr->fileset->MD5, fsr.MD5, sizeof(jcr->fileset->MD5));
719    } else {
720       Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 digest not found.\n"));
721    }
722    if (!jcr->fileset->ignore_fs_changes ||
723        !db_get_fileset_record(jcr, jcr->db, &fsr)) {
724       if (!db_create_fileset_record(jcr, jcr->db, &fsr)) {
725          Jmsg(jcr, M_ERROR, 0, _("Could not create FileSet \"%s\" record. ERR=%s\n"),
726             fsr.FileSet, db_strerror(jcr->db));
727          return false;
728       }
729    }
730    jcr->jr.FileSetId = fsr.FileSetId;
731    bstrncpy(jcr->FSCreateTime, fsr.cCreateTime, sizeof(jcr->FSCreateTime));
732    Dmsg2(119, "Created FileSet %s record %u\n", jcr->fileset->hdr.name,
733       jcr->jr.FileSetId);
734    return true;
735 }
736
737 void init_jcr_job_record(JCR *jcr)
738 {
739    jcr->jr.SchedTime = jcr->sched_time;
740    jcr->jr.StartTime = jcr->start_time;
741    jcr->jr.EndTime = 0;               /* perhaps rescheduled, clear it */
742    jcr->jr.JobType = jcr->JobType;
743    jcr->jr.JobLevel = jcr->JobLevel;
744    jcr->jr.JobStatus = jcr->JobStatus;
745    jcr->jr.JobId = jcr->JobId;
746    bstrncpy(jcr->jr.Name, jcr->job->name(), sizeof(jcr->jr.Name));
747    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
748 }
749
750 /*
751  * Write status and such in DB
752  */
753 void update_job_end_record(JCR *jcr)
754 {
755    jcr->jr.EndTime = time(NULL);
756    jcr->end_time = jcr->jr.EndTime;
757    jcr->jr.JobId = jcr->JobId;
758    jcr->jr.JobStatus = jcr->JobStatus;
759    jcr->jr.JobFiles = jcr->JobFiles;
760    jcr->jr.JobBytes = jcr->JobBytes;
761    jcr->jr.VolSessionId = jcr->VolSessionId;
762    jcr->jr.VolSessionTime = jcr->VolSessionTime;
763    jcr->jr.JobErrors = jcr->Errors;
764    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
765       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
766          db_strerror(jcr->db));
767    }
768 }
769
770 /*
771  * Takes base_name and appends (unique) current
772  *   date and time to form unique job name.
773  *
774  *  Returns: unique job name in jcr->Job
775  *    date/time in jcr->start_time
776  */
777 void create_unique_job_name(JCR *jcr, const char *base_name)
778 {
779    /* Job start mutex */
780    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
781    static time_t last_start_time = 0;
782    time_t now;
783    struct tm tm;
784    char dt[MAX_TIME_LENGTH];
785    char name[MAX_NAME_LENGTH];
786    char *p;
787
788    /* Guarantee unique start time -- maximum one per second, and
789     * thus unique Job Name
790     */
791    P(mutex);                          /* lock creation of jobs */
792    now = time(NULL);
793    while (now == last_start_time) {
794       bmicrosleep(0, 500000);
795       now = time(NULL);
796    }
797    last_start_time = now;
798    V(mutex);                          /* allow creation of jobs */
799    jcr->start_time = now;
800    /* Form Unique JobName */
801    (void)localtime_r(&now, &tm);
802    /* Use only characters that are permitted in Windows filenames */
803    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm);
804    bstrncpy(name, base_name, sizeof(name));
805    name[sizeof(name)-22] = 0;          /* truncate if too long */
806    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
807    /* Convert spaces into underscores */
808    for (p=jcr->Job; *p; p++) {
809       if (*p == ' ') {
810          *p = '_';
811       }
812    }
813 }
814
815 /* Called directly from job rescheduling */
816 void dird_free_jcr_pointers(JCR *jcr)
817 {
818    if (jcr->sd_auth_key) {
819       free(jcr->sd_auth_key);
820       jcr->sd_auth_key = NULL;
821    }
822    if (jcr->where) {
823       free(jcr->where);
824       jcr->where = NULL;
825    }
826    if (jcr->file_bsock) {
827       Dmsg0(200, "Close File bsock\n");
828       bnet_close(jcr->file_bsock);
829       jcr->file_bsock = NULL;
830    }
831    if (jcr->store_bsock) {
832       Dmsg0(200, "Close Store bsock\n");
833       bnet_close(jcr->store_bsock);
834       jcr->store_bsock = NULL;
835    }
836    if (jcr->fname) {
837       Dmsg0(200, "Free JCR fname\n");
838       free_pool_memory(jcr->fname);
839       jcr->fname = NULL;
840    }
841    if (jcr->pool_source) {
842       free_pool_memory(jcr->pool_source);
843       jcr->pool_source = NULL;
844    }
845    if (jcr->rpool_source) {
846       free_pool_memory(jcr->rpool_source);
847       jcr->rpool_source = NULL;
848    }
849    if (jcr->wstore_source) {
850       free_pool_memory(jcr->wstore_source);
851       jcr->wstore_source = NULL;
852    }
853    if (jcr->rstore_source) {
854       free_pool_memory(jcr->rstore_source);
855       jcr->rstore_source = NULL;
856    }
857    if (jcr->stime) {
858       Dmsg0(200, "Free JCR stime\n");
859       free_pool_memory(jcr->stime);
860       jcr->stime = NULL;
861    }
862    if (jcr->RestoreBootstrap) {
863       free(jcr->RestoreBootstrap);
864       jcr->RestoreBootstrap = NULL;
865    }
866    if (jcr->client_uname) {
867       free_pool_memory(jcr->client_uname);
868       jcr->client_uname = NULL;
869    }
870    if (jcr->attr) {
871       free_pool_memory(jcr->attr);
872       jcr->attr = NULL;
873    }
874    if (jcr->ar) {
875       free(jcr->ar);
876       jcr->ar = NULL;
877    }
878 }
879
880 /*
881  * Free the Job Control Record if no one is still using it.
882  *  Called from main free_jcr() routine in src/lib/jcr.c so
883  *  that we can do our Director specific cleanup of the jcr.
884  */
885 void dird_free_jcr(JCR *jcr)
886 {
887    Dmsg0(200, "Start dird free_jcr\n");
888
889    dird_free_jcr_pointers(jcr);
890    if (jcr->term_wait_inited) {
891       pthread_cond_destroy(&jcr->term_wait);
892       jcr->term_wait_inited = false;
893    }
894
895    /* Delete lists setup to hold storage pointers */
896    free_rwstorage(jcr);
897
898    jcr->job_end_push.destroy();
899    Dmsg0(200, "End dird free_jcr\n");
900 }
901
902 /* 
903  * The Job storage definition must be either in the Job record
904  *  or in the Pool record.  The Pool record overrides the Job 
905  *  record.
906  */
907 void get_job_storage(USTORE *store, JOB *job, RUN *run) 
908 {
909    if (run && run->pool && run->pool->storage) {
910       store->store = (STORE *)run->pool->storage->first();
911       pm_strcpy(store->store_source, _("Run pool override"));
912       return;
913    }
914    if (run && run->storage) {
915       store->store = run->storage;
916       pm_strcpy(store->store_source, _("Run storage override"));
917       return;
918    }
919    if (job->pool->storage) {
920       store->store = (STORE *)job->pool->storage->first();
921       pm_strcpy(store->store_source, _("Pool resource"));
922    } else {
923       store->store = (STORE *)job->storage->first();
924       pm_strcpy(store->store_source, _("Job resource"));
925    }
926 }
927
928 /*
929  * Set some defaults in the JCR necessary to
930  * run. These items are pulled from the job
931  * definition as defaults, but can be overridden
932  * later either by the Run record in the Schedule resource,
933  * or by the Console program.
934  */
935 void set_jcr_defaults(JCR *jcr, JOB *job)
936 {
937    jcr->job = job;
938    jcr->JobType = job->JobType;
939    switch (jcr->JobType) {
940    case JT_ADMIN:
941    case JT_RESTORE:
942       jcr->JobLevel = L_NONE;
943       break;
944    default:
945       jcr->JobLevel = job->JobLevel;
946       break;
947    }
948    if (!jcr->fname) {
949       jcr->fname = get_pool_memory(PM_FNAME);
950    }
951    if (!jcr->pool_source) {
952       jcr->pool_source = get_pool_memory(PM_MESSAGE);
953       pm_strcpy(jcr->pool_source, _("unknown source"));
954    }
955    jcr->JobPriority = job->Priority;
956    /* Copy storage definitions -- deleted in dir_free_jcr above */
957    if (job->storage) {
958       copy_rwstorage(jcr, job->storage, _("Job resource"));
959    } else {
960       copy_rwstorage(jcr, job->pool->storage, _("Pool resource"));
961    }
962    jcr->client = job->client;
963    if (!jcr->client_name) {
964       jcr->client_name = get_pool_memory(PM_NAME);
965    }
966    pm_strcpy(jcr->client_name, jcr->client->hdr.name);
967    pm_strcpy(jcr->pool_source, _("Job resource"));
968    jcr->pool = job->pool;
969    jcr->full_pool = job->full_pool;
970    jcr->inc_pool = job->inc_pool;
971    jcr->diff_pool = job->diff_pool;
972    jcr->catalog = job->client->catalog;
973    jcr->fileset = job->fileset;
974    jcr->messages = job->messages;
975    jcr->spool_data = job->spool_data;
976    jcr->write_part_after_job = job->write_part_after_job;
977    if (jcr->RestoreBootstrap) {
978       free(jcr->RestoreBootstrap);
979       jcr->RestoreBootstrap = NULL;
980    }
981    /* This can be overridden by Console program */
982    if (job->RestoreBootstrap) {
983       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
984    }
985    /* This can be overridden by Console program */
986    jcr->verify_job = job->verify_job;
987    /* If no default level given, set one */
988    if (jcr->JobLevel == 0) {
989       switch (jcr->JobType) {
990       case JT_VERIFY:
991          jcr->JobLevel = L_VERIFY_CATALOG;
992          break;
993       case JT_BACKUP:
994          jcr->JobLevel = L_INCREMENTAL;
995          break;
996       case JT_RESTORE:
997       case JT_ADMIN:
998          jcr->JobLevel = L_NONE;
999          break;
1000       default:
1001          jcr->JobLevel = L_FULL;
1002          break;
1003       }
1004    }
1005 }
1006
1007 /* 
1008  * Copy the storage definitions from an alist to the JCR
1009  */
1010 void copy_rwstorage(JCR *jcr, alist *storage, const char *where)
1011 {
1012    switch(jcr->JobType) {
1013    case JT_RESTORE:
1014    case JT_VERIFY:
1015    case JT_MIGRATE:
1016       copy_rstorage(jcr, storage, where);
1017       break;
1018    default:
1019       copy_wstorage(jcr, storage, where);
1020       break;
1021    }
1022 }
1023
1024
1025 /* Set storage override */
1026 void set_rwstorage(JCR *jcr, USTORE *store)
1027 {
1028    if (!store) {
1029       Jmsg(jcr, M_FATAL, 0, _("No storage specified.\n"));
1030       return;
1031    }
1032    switch(jcr->JobType) {
1033    case JT_RESTORE:
1034    case JT_VERIFY:
1035    case JT_MIGRATE:
1036       set_rstorage(jcr, store);
1037       break;
1038    default:
1039       set_wstorage(jcr, store);
1040       break;
1041    }
1042 }
1043
1044 void free_rwstorage(JCR *jcr)
1045 {
1046    free_rstorage(jcr);
1047    free_wstorage(jcr);
1048 }
1049
1050 /* 
1051  * Copy the storage definitions from an alist to the JCR
1052  */
1053 void copy_rstorage(JCR *jcr, alist *storage, const char *where)
1054 {
1055    if (storage) {
1056       STORE *st;
1057       if (jcr->rstorage) {
1058          delete jcr->rstorage;
1059       }
1060       jcr->rstorage = New(alist(10, not_owned_by_alist));
1061       foreach_alist(st, storage) {
1062          jcr->rstorage->append(st);
1063       }
1064       if (!jcr->rstore_source) {
1065          jcr->rstore_source = get_pool_memory(PM_MESSAGE);
1066       }
1067       pm_strcpy(jcr->rstore_source, where);
1068       if (jcr->rstorage) {
1069          jcr->rstore = (STORE *)jcr->rstorage->first();
1070       }
1071    }
1072 }
1073
1074
1075 /* Set storage override */
1076 void set_rstorage(JCR *jcr, USTORE *store)
1077 {
1078    STORE *storage;
1079
1080    if (!store->store) {
1081       return;
1082    }
1083    if (!jcr->rstorage) {
1084       jcr->rstorage = New(alist(10, not_owned_by_alist));
1085    }
1086    jcr->rstore = store->store;
1087    if (!jcr->rstore_source) {
1088       jcr->rstore_source = get_pool_memory(PM_MESSAGE);
1089    }
1090    pm_strcpy(jcr->rstore_source, store->store_source);
1091    foreach_alist(storage, jcr->rstorage) {
1092       if (store->store == storage) {
1093          return;
1094       }
1095    }
1096    /* Store not in list, so add it */
1097    jcr->rstorage->prepend(store->store);
1098 }
1099
1100 void free_rstorage(JCR *jcr)
1101 {
1102    if (jcr->rstorage) {
1103       delete jcr->rstorage;
1104       jcr->rstorage = NULL;
1105    }
1106    jcr->rstore = NULL;
1107 }
1108
1109 /* 
1110  * Copy the storage definitions from an alist to the JCR
1111  */
1112 void copy_wstorage(JCR *jcr, alist *storage, const char *where)
1113 {
1114    if (storage) {
1115       STORE *st;
1116       if (jcr->wstorage) {
1117          delete jcr->wstorage;
1118       }
1119       jcr->wstorage = New(alist(10, not_owned_by_alist));
1120       foreach_alist(st, storage) {
1121          Dmsg1(50, "storage=%s\n", st->name());
1122          jcr->wstorage->append(st);
1123       }
1124       if (!jcr->wstore_source) {
1125          jcr->wstore_source = get_pool_memory(PM_MESSAGE);
1126       }
1127       pm_strcpy(jcr->wstore_source, where);
1128       if (jcr->wstorage) {
1129          jcr->wstore = (STORE *)jcr->wstorage->first();
1130          Dmsg2(100, "wstore=%s where=%s\n", jcr->wstore->name(), jcr->wstore_source);
1131       }
1132    }
1133 }
1134
1135
1136 /* Set storage override */
1137 void set_wstorage(JCR *jcr, USTORE *store)
1138 {
1139    STORE *storage;
1140
1141    if (!store->store) {
1142       return;
1143    }
1144    if (!jcr->wstorage) {
1145       jcr->wstorage = New(alist(10, not_owned_by_alist));
1146    }
1147    jcr->wstore = store->store;
1148    if (!jcr->wstore_source) {
1149       jcr->wstore_source = get_pool_memory(PM_MESSAGE);
1150    }
1151    pm_strcpy(jcr->wstore_source, store->store_source);
1152    Dmsg2(50, "wstore=%s where=%s\n", jcr->wstore->name(), jcr->wstore_source);
1153    foreach_alist(storage, jcr->wstorage) {
1154       if (store->store == storage) {
1155          return;
1156       }
1157    }
1158    /* Store not in list, so add it */
1159    jcr->wstorage->prepend(store->store);
1160 }
1161
1162 void free_wstorage(JCR *jcr)
1163 {
1164    if (jcr->wstorage) {
1165       delete jcr->wstorage;
1166       jcr->wstorage = NULL;
1167    }
1168    jcr->wstore = NULL;
1169 }
1170
1171 void create_clones(JCR *jcr)
1172 {
1173    /*
1174     * Fire off any clone jobs (run directives)
1175     */
1176    Dmsg2(900, "cloned=%d run_cmds=%p\n", jcr->cloned, jcr->job->run_cmds);
1177    if (!jcr->cloned && jcr->job->run_cmds) {
1178       char *runcmd;
1179       JOB *job = jcr->job;
1180       POOLMEM *cmd = get_pool_memory(PM_FNAME);
1181       UAContext *ua = new_ua_context(jcr);
1182       ua->batch = true;
1183       foreach_alist(runcmd, job->run_cmds) {
1184          cmd = edit_job_codes(jcr, cmd, runcmd, "");              
1185          Mmsg(ua->cmd, "run %s cloned=yes", cmd);
1186          Dmsg1(900, "=============== Clone cmd=%s\n", ua->cmd);
1187          parse_ua_args(ua);                 /* parse command */
1188          int stat = run_cmd(ua, ua->cmd);
1189          if (stat == 0) {
1190             Jmsg(jcr, M_ERROR, 0, _("Could not start clone job.\n"));
1191          } else {
1192             Jmsg(jcr, M_INFO, 0, _("Clone JobId %d started.\n"), stat);
1193          }
1194       }
1195       free_ua_context(ua);
1196       free_pool_memory(cmd);
1197    }
1198 }
1199
1200 bool create_restore_bootstrap_file(JCR *jcr)
1201 {
1202    RESTORE_CTX rx;
1203    UAContext *ua;
1204    memset(&rx, 0, sizeof(rx));
1205    rx.bsr = new_bsr();
1206    rx.JobIds = "";                       
1207    rx.bsr->JobId = jcr->previous_jr.JobId;
1208    ua = new_ua_context(jcr);
1209    complete_bsr(ua, rx.bsr);
1210    rx.bsr->fi = new_findex();
1211    rx.bsr->fi->findex = 1;
1212    rx.bsr->fi->findex2 = jcr->previous_jr.JobFiles;
1213    jcr->ExpectedFiles = write_bsr_file(ua, rx);
1214    if (jcr->ExpectedFiles == 0) {
1215       free_ua_context(ua);
1216       free_bsr(rx.bsr);
1217       return false;
1218    }
1219    free_ua_context(ua);
1220    free_bsr(rx.bsr);
1221    jcr->needs_sd = true;
1222    return true;
1223 }