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