]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird.c
545aa6fca52042cb77ffa5c7eee68610a154fc8d
[bacula/bacula] / bacula / src / dird / dird.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *   Bacula Director daemon -- this is the main program
21  *
22  *     Kern Sibbald, March MM
23  */
24
25 #include "bacula.h"
26 #include "dird.h"
27 #ifndef HAVE_REGEX_H
28 #include "lib/bregex.h"
29 #else
30 #include <regex.h>
31 #endif
32 #ifdef HAVE_DIRENT_H
33 #include <dirent.h>
34 #define NAMELEN(dirent) (strlen((dirent)->d_name))
35 #endif
36 #ifndef HAVE_READDIR_R
37 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
38 #endif
39
40 /* Forward referenced subroutines */
41 void terminate_dird(int sig);
42 static bool check_resources();
43 static void cleanup_old_files();
44 static void resize_reload(int nb);
45
46 /* Exported subroutines */
47 extern "C" void reload_config(int sig);
48 extern void invalidate_schedules();
49 extern bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code);
50
51 /* Imported subroutines */
52 JCR *wait_for_next_job(char *runjob);
53 void term_scheduler();
54 void term_ua_server();
55 void start_UA_server(dlist *addrs);
56 void init_job_server(int max_workers);
57 void term_job_server();
58 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
59 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
60 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
61 void store_migtype(LEX *lc, RES_ITEM *item, int index, int pass);
62 void init_device_resources();
63
64
65 static char *runjob = NULL;
66 static bool foreground = false;
67 static bool make_pid_file = true;     /* create pid file */
68 static void init_reload(void);
69 static CONFIG *config;
70 static bool test_config = false;
71
72 /* Globals Exported */
73 DIRRES *director;                     /* Director resource */
74 int FDConnectTimeout;
75 int SDConnectTimeout;
76 char *configfile = NULL;
77 void *start_heap;
78 utime_t last_reload_time = 0;
79
80
81 /* Globals Imported */
82 extern dlist client_globals;
83 extern dlist store_globals;
84 extern dlist job_globals;
85 extern dlist sched_globals;
86 extern RES_ITEM job_items[];
87 #if defined(_MSC_VER)
88 extern "C" { // work around visual compiler mangling variables
89    extern URES res_all;
90 }
91 #else
92 extern URES res_all;
93 #endif
94
95 typedef enum {
96    CHECK_CONNECTION,  /* Check catalog connection */
97    UPDATE_CATALOG,    /* Ensure that catalog is ok with conf */
98    UPDATE_AND_FIX     /* Ensure that catalog is ok, and fix old jobs */
99 } cat_op;
100 static bool check_catalog(cat_op mode);
101
102 #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */
103
104 static bool dir_sql_query(JCR *jcr, const char *cmd) 
105
106    if (jcr && jcr->db && jcr->db->is_connected()) {
107       return db_sql_query(jcr->db, cmd, NULL, NULL);
108    } 
109    return false; 
110
111
112 static bool dir_sql_escape(JCR *jcr, BDB *mdb, char *snew, char *sold, int len) 
113
114    if (jcr && jcr->db && jcr->db->is_connected()) { 
115       db_escape_string(jcr, mdb, snew, sold, len);
116       return true;
117    } 
118    return false; 
119
120
121 static void usage()
122 {
123    fprintf(stderr, _(
124       PROG_COPYRIGHT
125       "\n%sVersion: %s (%s)\n\n"
126       "Usage: bacula-dir [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
127       "     -c <file>        set configuration file to file\n"
128       "     -d <nn>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
129       "     -dt              print timestamp in debug output\n"
130       "     -T               set trace on\n"
131       "     -f               run in foreground (for debugging)\n"
132       "     -g               groupid\n"
133       "     -m               print kaboom output (for debugging)\n"
134       "     -r <job>         run <job> now\n"
135       "     -P               do not create pid file\n"
136       "     -s               no signals\n"
137       "     -t               test - read configuration and exit\n"
138       "     -u               userid\n"
139       "     -v               verbose user messages\n"
140       "     -?               print this message.\n"
141       "\n"), 2000, "", VERSION, BDATE);
142
143    exit(1);
144 }
145
146 /*
147  * !!! WARNING !!! Use this function only when bacula is stopped.
148  * ie, after a fatal signal and before exiting the program
149  * Print information about a JCR
150  */
151 static void dir_debug_print(JCR *jcr, FILE *fp)
152 {
153    fprintf(fp, "\twstore=%p rstore=%p wjcr=%p client=%p reschedule_count=%d SD_msg_chan_started=%d\n",
154            jcr->wstore, jcr->rstore, jcr->wjcr, jcr->client, jcr->reschedule_count, (int)jcr->SD_msg_chan_started);
155 }
156
157 /*********************************************************************
158  *
159  *         Main Bacula Director Server program
160  *
161  */
162 #if defined(HAVE_WIN32)
163 /* For Win32 main() is in src/win32 code ... */
164 #define main BaculaMain
165 #endif
166
167 /* DELETE ME when bugs in MA1512, MA1632 MA1639 are fixed */
168 extern void (*MA1512_reload_job_end_cb)(JCR *,void *);
169 static void reload_job_end_cb(JCR *jcr, void *ctx);
170
171 int main (int argc, char *argv[])
172 {
173    int ch;
174    JCR *jcr;
175    bool no_signals = false;
176    char *uid = NULL;
177    char *gid = NULL;
178
179    /* DELETE ME when bugs in MA1512, MA1632 MA1639 are fixed */
180    MA1512_reload_job_end_cb = reload_job_end_cb;
181
182    start_heap = sbrk(0);
183    setlocale(LC_ALL, "");
184    bindtextdomain("bacula", LOCALEDIR);
185    textdomain("bacula");
186
187    init_stack_dump();
188    my_name_is(argc, argv, "bacula-dir");
189    init_msg(NULL, NULL);              /* initialize message handler */
190    init_reload();
191    daemon_start_time = time(NULL);
192    setup_daemon_message_queue();
193    console_command = run_console_command;
194
195    while ((ch = getopt(argc, argv, "c:d:fg:mPr:stu:v?T")) != -1) {
196       switch (ch) {
197       case 'c':                    /* specify config file */
198          if (configfile != NULL) {
199             free(configfile);
200          }
201          configfile = bstrdup(optarg);
202          break;
203
204       case 'd':                    /* set debug level */
205          if (*optarg == 't') {
206             dbg_timestamp = true;
207          } else {
208             char *p;
209             /* We probably find a tag list -d 10,sql,bvfs */
210             if ((p = strchr(optarg, ',')) != NULL) {
211                *p = 0;
212             }
213             debug_level = atoi(optarg);
214             if (debug_level <= 0) {
215                debug_level = 1;
216             }
217             if (p) {
218                debug_parse_tags(p+1, &debug_level_tags);
219             }
220          }
221          Dmsg1(10, "Debug level = %lld\n", debug_level);
222          break;
223
224       case 'T':
225          set_trace(true);
226          break;
227
228       case 'f':                    /* run in foreground */
229          foreground = true;
230          break;
231
232       case 'g':                    /* set group id */
233          gid = optarg;
234          break;
235
236       case 'm':                    /* print kaboom output */
237          prt_kaboom = true;
238          break;
239
240       case 'P':                    /* no pid file */
241          make_pid_file = false;
242          break;
243
244       case 'r':                    /* run job */
245          if (runjob != NULL) {
246             free(runjob);
247          }
248          if (optarg) {
249             runjob = bstrdup(optarg);
250          }
251          break;
252
253       case 's':                    /* turn off signals */
254          no_signals = true;
255          break;
256
257       case 't':                    /* test config */
258          test_config = true;
259          break;
260
261       case 'u':                    /* set uid */
262          uid = optarg;
263          break;
264
265       case 'v':                    /* verbose */
266          verbose++;
267          break;
268
269       case '?':
270       default:
271          usage();
272
273       }
274    }
275    argc -= optind;
276    argv += optind;
277
278    if (argc) {
279       if (configfile != NULL) {
280          free(configfile);
281       }
282       configfile = bstrdup(*argv);
283       argc--;
284       argv++;
285    }
286    if (argc) {
287       usage();
288    }
289
290    if (!foreground && !test_config) {
291       daemon_start();
292       init_stack_dump();              /* grab new pid */
293    }
294
295    if (!no_signals) {
296       init_signals(terminate_dird);
297    }
298
299    if (configfile == NULL) {
300       configfile = bstrdup(CONFIG_FILE);
301    }
302
303    config = New(CONFIG());
304    parse_dir_config(config, configfile, M_ERROR_TERM);
305
306    if (init_crypto() != 0) {
307       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
308    }
309
310    if (!check_resources()) {
311       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
312    }
313
314    if (!test_config) {
315       /* Create pid must come after we are a daemon -- so we have our final pid */
316       if (make_pid_file) {
317          create_pid_file(director->pid_directory, "bacula-dir",
318                           get_first_port_host_order(director->DIRaddrs));
319       }
320       read_state_file(director->working_directory, "bacula-dir",
321                       get_first_port_host_order(director->DIRaddrs));
322    }
323
324    set_jcr_in_tsd(INVALID_JCR);
325    set_thread_concurrency(director->MaxConcurrentJobs * 2 +
326                           4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */);
327    lmgr_init_thread(); /* initialize the lockmanager stack */
328
329    load_dir_plugins(director->plugin_directory);
330
331    drop(uid, gid, false);                    /* reduce privileges if requested */
332
333    /* If we are in testing mode, we don't try to fix the catalog */
334    cat_op mode=(test_config)?CHECK_CONNECTION:UPDATE_AND_FIX;
335
336    if (!check_catalog(mode)) {
337       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
338    }
339
340    if (test_config) {
341       terminate_dird(0);
342    }
343
344    my_name_is(0, NULL, director->name());    /* set user defined name */
345
346    cleanup_old_files();
347
348    /* Plug database interface for library routines */
349    p_sql_query = (sql_query_call)dir_sql_query;
350    p_sql_escape = (sql_escape_call)dir_sql_escape;
351
352    FDConnectTimeout = (int)director->FDConnectTimeout;
353    SDConnectTimeout = (int)director->SDConnectTimeout;
354
355    resize_reload(director->MaxReload);
356
357 #if !defined(HAVE_WIN32)
358    signal(SIGHUP, reload_config);
359 #endif
360
361    init_console_msg(working_directory);
362
363    Dmsg0(200, "Start UA server\n");
364    start_UA_server(director->DIRaddrs);
365
366    start_watchdog();                  /* start network watchdog thread */
367
368    init_jcr_subsystem();              /* start JCR watchdogs etc. */
369
370    init_job_server(director->MaxConcurrentJobs);
371
372    dbg_jcr_add_hook(dir_debug_print); /* used to director variables */
373    dbg_jcr_add_hook(bdb_debug_print);     /* used to debug B_DB connexion after fatal signal */
374
375 //   init_device_resources();
376
377    Dmsg0(200, "wait for next job\n");
378    /* Main loop -- call scheduler to get next job to run */
379    while ( (jcr = wait_for_next_job(runjob)) ) {
380       run_job(jcr);                   /* run job */
381       free_jcr(jcr);                  /* release jcr */
382       set_jcr_in_tsd(INVALID_JCR);
383       if (runjob) {                   /* command line, run a single job? */
384          break;                       /* yes, terminate */
385       }
386    }
387
388    terminate_dird(0);
389
390    return 0;
391 }
392
393 struct RELOAD_TABLE {
394    int job_count;
395    RES_HEAD **res_head;
396 };
397
398 static int max_reloads = 32;
399 static RELOAD_TABLE *reload_table=NULL;
400
401 static void resize_reload(int nb)
402 {
403    if (nb <= max_reloads) {
404       return;
405    }
406
407    reload_table = (RELOAD_TABLE*)realloc(reload_table, nb * sizeof(RELOAD_TABLE));
408    for (int i=max_reloads; i < nb ; i++) {
409       reload_table[i].job_count = 0;
410       reload_table[i].res_head = NULL;
411    }
412    max_reloads = nb;
413 }
414
415 static void init_reload(void)
416 {
417    reload_table = (RELOAD_TABLE*)malloc(max_reloads * sizeof(RELOAD_TABLE));
418    for (int i=0; i < max_reloads; i++) {
419       reload_table[i].job_count = 0;
420       reload_table[i].res_head = NULL;
421    }
422 }
423
424 /*
425  * This subroutine frees a saved resource table.
426  *  It was saved when a new table was created with "reload"
427  */
428 static void free_saved_resources(int table)
429 {
430    RES *next, *res;
431    int num = r_last - r_first + 1;
432    RES_HEAD **res_tab = reload_table[table].res_head;
433
434    if (res_tab == NULL) {
435       Dmsg1(100, "res_tab for table %d already released.\n", table);
436       return;
437    }
438    Dmsg1(100, "Freeing resources for table %d\n", table);
439    for (int j=0; j<num; j++) {
440       if (res_tab[j]) {
441          next = res_tab[j]->first;
442          for ( ; next; ) {
443             res = next;
444             next = res->res_next;
445             free_resource(res, r_first + j);
446          }
447          free(res_tab[j]->res_list);
448          free(res_tab[j]);
449          res_tab[j] = NULL;
450       }
451    }
452    free(res_tab);
453    reload_table[table].job_count = 0;
454    reload_table[table].res_head = NULL;
455 }
456
457 /*
458  * Called here at the end of every job that was
459  * hooked decrementing the active job_count. When
460  * it goes to zero, no one is using the associated
461  * resource table, so free it.
462  */
463 static void reload_job_end_cb(JCR *jcr, void *ctx)
464 {
465    int reload_id = (int)((intptr_t)ctx);
466    Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
467       reload_id, reload_table[reload_id].job_count);
468    lock_jobs();
469    LockRes();
470    if (--reload_table[reload_id].job_count <= 0) {
471       free_saved_resources(reload_id);
472    }
473    UnlockRes();
474    unlock_jobs();
475 }
476
477 static int find_free_reload_table_entry()
478 {
479    int table = -1;
480    for (int i=0; i < max_reloads; i++) {
481       if (reload_table[i].res_head == NULL) {
482          table = i;
483          break;
484       }
485    }
486    return table;
487 }
488
489 static pthread_mutex_t reload_mutex = PTHREAD_MUTEX_INITIALIZER;
490
491 /*
492  * If we get here, we have received a SIGHUP, which means to
493  *    reread our configuration file.
494  *
495  * The algorithm used is as follows: we count how many jobs are
496  *   running and mark the running jobs to make a callback on
497  *   exiting. The old config is saved with the reload table
498  *   id in a reload table. The new config file is read. Now, as
499  *   each job exits, it calls back to the reload_job_end_cb(), which
500  *   decrements the count of open jobs for the given reload table.
501  *   When the count goes to zero, we release those resources.
502  *   This allows us to have pointers into the resource table (from
503  *   jobs), and once they exit and all the pointers are released, we
504  *   release the old table. Note, if no new jobs are running since the
505  *   last reload, then the old resources will be immediately release.
506  *   A console is considered a job because it may have pointers to
507  *   resources, but a SYSTEM job is not since it *should* not have any
508  *   permanent pointers to jobs.
509  */
510 extern "C"
511 void reload_config(int sig)
512 {
513    static bool already_here = false;
514 #if !defined(HAVE_WIN32)
515    sigset_t set;
516 #endif
517    JCR *jcr;
518    int njobs = 0;                     /* number of running jobs */
519    int table, rtable;
520    bool ok=false;
521    int tries=0;
522
523    /* Wait to do the reload */
524    do {
525       P(reload_mutex);
526       if (already_here) {
527          V(reload_mutex);
528          if (tries++ > 10) {
529             Jmsg(NULL, M_INFO, 0, _("Already doing a reload request, "
530                                     "request ignored.\n"));
531             return;
532          }
533          Dmsg0(10, "Already doing a reload request, waiting a bit\n");
534          bmicrosleep(1, 0);
535       } else {
536          already_here = true;
537          V(reload_mutex);
538          ok = true;
539       }
540    } while (!ok);
541
542 #if !defined(HAVE_WIN32)
543    sigemptyset(&set);
544    sigaddset(&set, SIGHUP);
545    sigprocmask(SIG_BLOCK, &set, NULL);
546 #endif
547
548    lock_jobs();
549    LockRes();
550
551    table = find_free_reload_table_entry();
552    if (table < 0) {
553       Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. "
554                                "Request ignored.\n"));
555       goto bail_out;
556    }
557
558    Dmsg1(100, "Reload_config njobs=%d\n", njobs);
559    /* Save current res_head */
560    reload_table[table].res_head = res_head;
561    Dmsg1(100, "Saved old config in table %d\n", table);
562
563    /* Create a new res_head and parse into it */
564    ok = parse_dir_config(config, configfile, M_ERROR);
565
566    Dmsg0(100, "Reloaded config file\n");
567    if (!ok || !check_resources() || !check_catalog(UPDATE_CATALOG)) {
568       /*
569        * We got an error, save broken point, restore old one,
570        *  then release everything from broken pointer.
571        */
572       rtable = find_free_reload_table_entry();    /* save new, bad table */
573       if (rtable < 0) {
574          Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
575          Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
576       } else {
577          Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
578          Jmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
579       }
580       /* Save broken res_head pointer */
581       reload_table[rtable].res_head = res_head;
582
583       /* Now restore old resource pointer */
584       res_head = reload_table[table].res_head;
585       table = rtable;           /* release new, bad, saved table below */
586    } else {
587       invalidate_schedules();
588       /*
589        * Hook all active jobs so that they release this table
590        */
591       foreach_jcr(jcr) {
592          if (jcr->getJobType() != JT_SYSTEM) {
593             reload_table[table].job_count++;
594             job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
595             njobs++;
596          }
597       }
598       endeach_jcr(jcr);
599       /*
600        * Now walk through globals tables and plug them into the
601        * new resources.
602        */
603       CLIENT_GLOBALS *cg;
604       foreach_dlist(cg, &client_globals) {
605          CLIENT *client;
606          client = GetClientResWithName(cg->name);
607          if (!client) {
608             Jmsg(NULL, M_INFO, 0, _("Client=%s not found. Assuming it was removed!!!\n"), cg->name);
609          } else {
610             client->globals = cg;      /* Set globals pointer */
611          }
612       }
613       STORE_GLOBALS *sg;
614       foreach_dlist(sg, &store_globals) {
615          STORE *store;
616          store = GetStoreResWithName(sg->name);
617          if (!store) {
618             Jmsg(NULL, M_INFO, 0, _("Storage=%s not found. Assuming it was removed!!!\n"), sg->name);
619          } else {
620             store->globals = sg;       /* set globals pointer */
621             Dmsg2(200, "Reload found numConcurrent=%ld for Store %s\n",
622                sg->NumConcurrentJobs, sg->name);
623          }
624       }
625       JOB_GLOBALS *jg;
626       foreach_dlist(jg, &job_globals) {
627          JOB *job;
628          job = GetJobResWithName(jg->name);
629          if (!job) {
630             Jmsg(NULL, M_INFO, 0, _("Job=%s not found. Assuming it was removed!!!\n"), jg->name);
631          } else {
632             job->globals = jg;         /* Set globals pointer */
633          }
634       }
635       SCHED_GLOBALS *schg;
636       foreach_dlist(schg, &sched_globals) {
637          SCHED *sched;
638          sched = GetSchedResWithName(schg->name);
639          if (!sched) {
640             Jmsg(NULL, M_INFO, 0, _("Schedule=%s not found. Assuming it was removed!!!\n"), schg->name);
641          } else {
642             sched->globals = schg;     /* Set globals pointer */
643          }
644       }
645    }
646
647    /* Reset other globals */
648    set_working_directory(director->working_directory);
649    FDConnectTimeout = director->FDConnectTimeout;
650    SDConnectTimeout = director->SDConnectTimeout;
651    Dmsg0(10, "Director's configuration file reread.\n");
652
653    /* Now release saved resources, if no jobs using the resources */
654    if (njobs == 0) {
655       free_saved_resources(table);
656    }
657
658 bail_out:
659    UnlockRes();
660    unlock_jobs();
661 #if !defined(HAVE_WIN32)
662    sigprocmask(SIG_UNBLOCK, &set, NULL);
663    signal(SIGHUP, reload_config);
664 #endif
665    already_here = false;
666 }
667
668 /* Cleanup and then exit */
669 void terminate_dird(int sig)
670 {
671    static bool already_here = false;
672
673    if (already_here) {                /* avoid recursive temination problems */
674       bmicrosleep(2, 0);              /* yield */
675       exit(1);
676    }
677    already_here = true;
678    debug_level = 0;                   /* turn off debug */
679    stop_watchdog();
680    generate_daemon_event(NULL, "Exit");
681    unload_plugins();
682    if (!test_config) {
683       write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
684       if (make_pid_file) {
685          delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
686       }
687    }
688    term_scheduler();
689    term_job_server();
690    if (runjob) {
691       free(runjob);
692    }
693    if (configfile != NULL) {
694       free(configfile);
695    }
696    if (chk_dbglvl(5)) {
697       print_memory_pool_stats();
698    }
699    if (config) {
700       delete config;
701       config = NULL;
702    }
703    term_ua_server();
704    term_msg();                        /* terminate message handler */
705    cleanup_crypto();
706
707    free_daemon_message_queue();
708
709    if (reload_table) {
710       free(reload_table);
711    }
712    free(res_head);
713    res_head = NULL;
714    /*
715     * Now walk through resource globals tables and release them
716     */
717    CLIENT_GLOBALS *cg;
718    foreach_dlist(cg, &client_globals) {
719       free(cg->name);
720       if (cg->SetIPaddress) {
721          free(cg->SetIPaddress);
722       }
723       free(cg);
724    }
725    STORE_GLOBALS *sg;
726    foreach_dlist(sg, &store_globals) {
727       free(sg->name);
728       free(sg);
729    }
730    JOB_GLOBALS *jg;
731    foreach_dlist(jg, &job_globals) {
732       free(jg->name);
733       free(jg);
734    }
735    close_memory_pool();               /* release free memory in pool */
736    lmgr_cleanup_main();
737    sm_dump(false);
738    exit(sig);
739 }
740
741 /*
742  * Make a quick check to see that we have all the
743  * resources needed.
744  *
745  *  **** FIXME **** this routine could be a lot more
746  *   intelligent and comprehensive.
747  */
748 static bool check_resources()
749 {
750    bool OK = true;
751    JOB *job;
752    bool need_tls;
753
754    LockRes();
755
756    job = (JOB *)GetNextRes(R_JOB, NULL);
757    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
758    if (!director) {
759       Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
760 "Without that I don't know who I am :-(\n"), configfile);
761       OK = false;
762    } else {
763       set_working_directory(director->working_directory);
764       if (!director->messages) {       /* If message resource not specified */
765          director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
766          if (!director->messages) {
767             Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
768             OK = false;
769          }
770       }
771       if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
772          Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
773             configfile);
774          OK = false;
775       }
776       /* tls_require implies tls_enable */
777       if (director->tls_require) {
778          if (have_tls) {
779             director->tls_enable = true;
780          } else {
781             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
782             OK = false;
783          }
784       }
785
786       need_tls = director->tls_enable || director->tls_authenticate;
787
788       if (!director->tls_certfile && need_tls) {
789          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
790             director->name(), configfile);
791          OK = false;
792       }
793
794       if (!director->tls_keyfile && need_tls) {
795          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
796             director->name(), configfile);
797          OK = false;
798       }
799
800       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) &&
801            need_tls && director->tls_verify_peer) {
802          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
803               " Certificate Dir\" are defined for Director \"%s\" in %s."
804               " At least one CA certificate store is required"
805               " when using \"TLS Verify Peer\".\n"),
806               director->name(), configfile);
807          OK = false;
808       }
809
810       /* If everything is well, attempt to initialize our per-resource TLS context */
811       if (OK && (need_tls || director->tls_require)) {
812          /* Initialize TLS context:
813           * Args: CA certfile, CA certdir, Certfile, Keyfile,
814           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
815          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
816             director->tls_ca_certdir, director->tls_certfile,
817             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
818             director->tls_verify_peer);
819
820          if (!director->tls_ctx) {
821             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
822                  director->name(), configfile);
823             OK = false;
824          }
825       }
826    }
827
828    if (!job) {
829       Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
830       OK = false;
831    }
832    foreach_res(job, R_JOB) {
833       int i;
834
835       if (job->jobdefs) {
836          JOB *jobdefs = job->jobdefs;
837          /* Handle RunScripts alists specifically */
838          if (jobdefs->RunScripts) {
839             RUNSCRIPT *rs, *elt;
840
841             if (!job->RunScripts) {
842                job->RunScripts = New(alist(10, not_owned_by_alist));
843             }
844
845             foreach_alist(rs, jobdefs->RunScripts) {
846                elt = copy_runscript(rs);
847                job->RunScripts->append(elt); /* we have to free it */
848             }
849          }
850
851          /* Transfer default items from JobDefs Resource */
852          for (i=0; job_items[i].name; i++) {
853             char **def_svalue, **svalue;  /* string value */
854             uint32_t *def_ivalue, *ivalue;     /* integer value */
855             bool *def_bvalue, *bvalue;    /* bool value */
856             int64_t *def_lvalue, *lvalue; /* 64 bit values */
857             alist **def_avalue, **avalue; /* alist values */
858             uint32_t offset;
859
860             Dmsg4(1400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
861                 job->name(), job_items[i].name,
862                 bit_is_set(i, job->hdr.item_present),
863                 bit_is_set(i, job->jobdefs->hdr.item_present));
864
865             if (!bit_is_set(i, job->hdr.item_present) &&
866                  bit_is_set(i, job->jobdefs->hdr.item_present)) {
867                Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
868                  job->name(), job_items[i].name);
869                offset = (char *)(job_items[i].value) - (char *)&res_all;
870                /*
871                 * Handle strings and directory strings
872                 */
873                if (job_items[i].handler == store_str ||
874                    job_items[i].handler == store_dir) {
875                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
876                   Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
877                        job->name(), job_items[i].name, *def_svalue, i, offset);
878                   svalue = (char **)((char *)job + offset);
879                   if (*svalue) {
880                      Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
881                   }
882                   *svalue = bstrdup(*def_svalue);
883                   set_bit(i, job->hdr.item_present);
884                /*
885                 * Handle resources
886                 */
887                } else if (job_items[i].handler == store_res) {
888                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
889                   Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
890                        job->name(), job_items[i].name, i, offset);
891                   svalue = (char **)((char *)job + offset);
892                   if (*svalue) {
893                      Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
894                   }
895                   *svalue = *def_svalue;
896                   set_bit(i, job->hdr.item_present);
897                /*
898                 * Handle alist resources
899                 */
900                } else if (job_items[i].handler == store_alist_res) {
901                   void *elt;
902
903                   def_avalue = (alist **)((char *)(job->jobdefs) + offset);
904                   avalue = (alist **)((char *)job + offset);
905
906                   *avalue = New(alist(10, not_owned_by_alist));
907
908                   foreach_alist(elt, (*def_avalue)) {
909                      (*avalue)->append(elt);
910                   }
911                   set_bit(i, job->hdr.item_present);
912                /*
913                 * Handle integer fields
914                 *    Note, our store_bit does not handle bitmaped fields
915                 */
916                } else if (job_items[i].handler == store_bit     ||
917                           job_items[i].handler == store_pint32  ||
918                           job_items[i].handler == store_jobtype ||
919                           job_items[i].handler == store_level   ||
920                           job_items[i].handler == store_int32   ||
921                           job_items[i].handler == store_size32  ||
922                           job_items[i].handler == store_migtype ||
923                           job_items[i].handler == store_replace) {
924                   def_ivalue = (uint32_t *)((char *)(job->jobdefs) + offset);
925                   Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
926                        job->name(), job_items[i].name, *def_ivalue, i, offset);
927                   ivalue = (uint32_t *)((char *)job + offset);
928                   *ivalue = *def_ivalue;
929                   set_bit(i, job->hdr.item_present);
930                /*
931                 * Handle 64 bit integer fields
932                 */
933                } else if (job_items[i].handler == store_time   ||
934                           job_items[i].handler == store_size64 ||
935                           job_items[i].handler == store_speed  ||
936                           job_items[i].handler == store_int64) {
937                   def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
938                   Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
939                        job->name(), job_items[i].name, *def_lvalue, i, offset);
940                   lvalue = (int64_t *)((char *)job + offset);
941                   *lvalue = *def_lvalue;
942                   set_bit(i, job->hdr.item_present);
943                /*
944                 * Handle bool fields
945                 */
946                } else if (job_items[i].handler == store_bool) {
947                   def_bvalue = (bool *)((char *)(job->jobdefs) + offset);
948                   Dmsg5(400, "Job \"%s\", field \"%s\" def_bvalue=%d item %d offset=%u\n",
949                        job->name(), job_items[i].name, *def_bvalue, i, offset);
950                   bvalue = (bool *)((char *)job + offset);
951                   *bvalue = *def_bvalue;
952                   set_bit(i, job->hdr.item_present);
953                }
954             }
955          }
956       }
957       /*
958        * Ensure that all required items are present
959        */
960       for (i=0; job_items[i].name; i++) {
961          if (job_items[i].flags & ITEM_REQUIRED) {
962                if (!bit_is_set(i, job->hdr.item_present)) {
963                   Jmsg(NULL, M_ERROR_TERM, 0, _("\"%s\" directive in Job \"%s\" resource is required, but not found.\n"),
964                     job_items[i].name, job->name());
965                   OK = false;
966                 }
967          }
968          /* If this triggers, take a look at lib/parse_conf.h */
969          if (i >= MAX_RES_ITEMS) {
970             Emsg0(M_ERROR_TERM, 0, _("Too many items in Job resource\n"));
971          }
972       }
973       if (!job->storage && !job->pool->storage) {
974          Jmsg(NULL, M_FATAL, 0, _("No storage specified in Job \"%s\" nor in Pool.\n"),
975             job->name());
976          OK = false;
977       }
978
979       /* Make sure the job doesn't use the Scratch Pool to start with */
980       const char *name;
981       if (!check_pool(job->JobType, job->JobLevel,
982                       job->pool, job->next_pool, &name)) { 
983          Jmsg(NULL, M_FATAL, 0,
984               _("%s \"Scratch\" not valid in Job \"%s\".\n"),
985               name, job->name());
986          OK = false;
987       }
988    } /* End loop over Job res */
989
990
991    /* Loop over Consoles */
992    CONRES *cons;
993    foreach_res(cons, R_CONSOLE) {
994       /* tls_require implies tls_enable */
995       if (cons->tls_require) {
996          if (have_tls) {
997             cons->tls_enable = true;
998          } else {
999             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1000             OK = false;
1001             continue;
1002          }
1003       }
1004
1005       need_tls = cons->tls_enable || cons->tls_authenticate;
1006
1007       if (!cons->tls_certfile && need_tls) {
1008          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Console \"%s\" in %s.\n"),
1009             cons->name(), configfile);
1010          OK = false;
1011       }
1012
1013       if (!cons->tls_keyfile && need_tls) {
1014          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Console \"%s\" in %s.\n"),
1015             cons->name(), configfile);
1016          OK = false;
1017       }
1018
1019       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir)
1020             && need_tls && cons->tls_verify_peer) {
1021          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
1022             " Certificate Dir\" are defined for Console \"%s\" in %s."
1023             " At least one CA certificate store is required"
1024             " when using \"TLS Verify Peer\".\n"),
1025             cons->name(), configfile);
1026          OK = false;
1027       }
1028       /* If everything is well, attempt to initialize our per-resource TLS context */
1029       if (OK && (need_tls || cons->tls_require)) {
1030          /* Initialize TLS context:
1031           * Args: CA certfile, CA certdir, Certfile, Keyfile,
1032           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1033          cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
1034             cons->tls_ca_certdir, cons->tls_certfile,
1035             cons->tls_keyfile, NULL, NULL, cons->tls_dhfile, cons->tls_verify_peer);
1036
1037          if (!cons->tls_ctx) {
1038             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
1039                cons->name(), configfile);
1040             OK = false;
1041          }
1042       }
1043
1044    }
1045
1046    /* Loop over Clients */
1047    CLIENT *client;
1048    foreach_res(client, R_CLIENT) {
1049       /* tls_require implies tls_enable */
1050       if (client->tls_require) {
1051          if (have_tls) {
1052             client->tls_enable = true;
1053          } else {
1054             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1055             OK = false;
1056             continue;
1057          }
1058       }
1059       need_tls = client->tls_enable || client->tls_authenticate;
1060       if ((!client->tls_ca_certfile && !client->tls_ca_certdir) && need_tls) {
1061          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
1062             " or \"TLS CA Certificate Dir\" are defined for File daemon \"%s\" in %s.\n"),
1063             client->name(), configfile);
1064          OK = false;
1065       }
1066
1067       /* If everything is well, attempt to initialize our per-resource TLS context */
1068       if (OK && (need_tls || client->tls_require)) {
1069          /* Initialize TLS context:
1070           * Args: CA certfile, CA certdir, Certfile, Keyfile,
1071           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1072          client->tls_ctx = new_tls_context(client->tls_ca_certfile,
1073             client->tls_ca_certdir, client->tls_certfile,
1074             client->tls_keyfile, NULL, NULL, NULL,
1075             true);
1076
1077          if (!client->tls_ctx) {
1078             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
1079                client->name(), configfile);
1080             OK = false;
1081          }
1082       }
1083    }
1084
1085    /* Loop over all pools, check PoolType */
1086    POOL *pool;
1087    foreach_res(pool, R_POOL) {
1088       if (!pool->pool_type) {
1089          /* This case is checked by the parse engine, we should not */
1090          Jmsg(NULL, M_FATAL, 0, _("PoolType required in Pool resource \"%s\".\n"), pool->hdr.name);
1091          OK = false;
1092          continue;
1093       }
1094       if ((strcasecmp(pool->pool_type, NT_("backup"))  != 0) &&
1095           (strcasecmp(pool->pool_type, NT_("copy"))    != 0) &&
1096           (strcasecmp(pool->pool_type, NT_("cloned"))  != 0) &&
1097           (strcasecmp(pool->pool_type, NT_("archive")) != 0) &&
1098           (strcasecmp(pool->pool_type, NT_("migration")) != 0) &&
1099           (strcasecmp(pool->pool_type, NT_("scratch")) != 0))
1100       {
1101          Jmsg(NULL, M_FATAL, 0, _("Invalid PoolType \"%s\" in Pool resource \"%s\".\n"), pool->pool_type, pool->hdr.name);
1102          OK = false;
1103       }
1104
1105       if (pool->NextPool && strcmp(pool->NextPool->name(), "Scratch") == 0) {
1106          Jmsg(NULL, M_FATAL, 0,
1107               _("NextPool \"Scratch\" not valid in Pool \"%s\".\n"),
1108               pool->name());
1109          OK = false;
1110       }
1111    }
1112
1113    UnlockRes();
1114    if (OK) {
1115       close_msg(NULL);                /* close temp message handler */
1116       init_msg(NULL, director->messages); /* open daemon message handler */
1117       last_reload_time = time(NULL);
1118    }
1119    return OK;
1120 }
1121
1122 /*
1123  * In this routine,
1124  *  - we can check the connection (mode=CHECK_CONNECTION)
1125  *  - we can synchronize the catalog with the configuration (mode=UPDATE_CATALOG)
1126  *  - we can synchronize, and fix old job records (mode=UPDATE_AND_FIX)
1127  *  - we hook up the Autochange children with the parent, and
1128  *    we hook the shared autochangers together.
1129  */
1130 static bool check_catalog(cat_op mode)
1131 {
1132    bool OK = true;
1133    bool need_tls;
1134    STORE *store, *ac_child;
1135
1136    /* Loop over databases */
1137    CAT *catalog;
1138    foreach_res(catalog, R_CATALOG) {
1139       BDB *db;
1140       /*
1141        * Make sure we can open catalog, otherwise print a warning
1142        * message because the server is probably not running.
1143        */
1144       db = db_init_database(NULL, catalog->db_driver, catalog->db_name, 
1145               catalog->db_user,
1146               catalog->db_password, catalog->db_address,
1147               catalog->db_port, catalog->db_socket,
1148               catalog->db_ssl_mode, catalog->db_ssl_key,
1149               catalog->db_ssl_cert, catalog->db_ssl_ca,
1150               catalog->db_ssl_capath, catalog->db_ssl_cipher,
1151               catalog->mult_db_connections,
1152               catalog->disable_batch_insert);
1153       if (!db || !db_open_database(NULL, db)) {
1154          Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1155               catalog->name(), catalog->db_name);
1156          Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1157               catalog->name(), catalog->db_name);
1158          if (db) {
1159             Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
1160             Pmsg1(000, "%s", db_strerror(db));
1161             db_close_database(NULL, db);
1162          }
1163          OK = false;
1164          continue;
1165       }
1166
1167       /* Display a message if the db max_connections is too low */
1168       if (!db_check_max_connections(NULL, db, director->MaxConcurrentJobs)) {
1169          Pmsg1(000, "Warning, settings problem for Catalog=%s\n", catalog->name());
1170          Pmsg1(000, "%s", db_strerror(db));
1171       }
1172
1173       /* we are in testing mode, so don't touch anything in the catalog */
1174       if (mode == CHECK_CONNECTION) {
1175          if (db) db_close_database(NULL, db);
1176          continue;
1177       }
1178
1179       /* Loop over all pools, defining/updating them in each database */
1180       POOL *pool;
1181       foreach_res(pool, R_POOL) {
1182          /*
1183           * If the Pool has a catalog resource create the pool only
1184           *   in that catalog.
1185           */
1186          if (!pool->catalog || pool->catalog == catalog) {
1187             create_pool(NULL, db, pool, POOL_OP_UPDATE);  /* update request */
1188          }
1189       }
1190
1191       /* Once they are created, we can loop over them again, updating
1192        * references (RecyclePool)
1193        */
1194       foreach_res(pool, R_POOL) {
1195          /*
1196           * If the Pool has a catalog resource update the pool only
1197           *   in that catalog.
1198           */
1199          if (!pool->catalog || pool->catalog == catalog) {
1200             update_pool_references(NULL, db, pool);
1201          }
1202       }
1203
1204       /* Ensure basic client record is in DB */
1205       CLIENT *client;
1206       foreach_res(client, R_CLIENT) {
1207          CLIENT_DBR cr;
1208          /* Create clients only if they use the current catalog */
1209          if (client->catalog != catalog) {
1210             Dmsg3(500, "Skip client=%s with cat=%s not catalog=%s\n",
1211                   client->name(), client->catalog->name(), catalog->name());
1212             continue;
1213          }
1214          Dmsg2(500, "create cat=%s for client=%s\n",
1215                client->catalog->name(), client->name());
1216          memset(&cr, 0, sizeof(cr));
1217          bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
1218
1219          db_create_client_record(NULL, db, &cr);
1220       }
1221
1222       /* Ensure basic storage record is in DB */
1223       foreach_res(store, R_STORAGE) {
1224          STORAGE_DBR sr;
1225          MEDIATYPE_DBR mtr;
1226          memset(&sr, 0, sizeof(sr));
1227          memset(&mtr, 0, sizeof(mtr));
1228          if (store->media_type) {
1229             bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType));
1230             mtr.ReadOnly = 0;
1231             db_create_mediatype_record(NULL, db, &mtr);
1232          } else {
1233             mtr.MediaTypeId = 0;
1234          }
1235          bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
1236          sr.AutoChanger = store->autochanger;
1237          if (!db_create_storage_record(NULL, db, &sr)) {
1238             Jmsg(NULL, M_FATAL, 0, _("Could not create storage record for %s\n"),
1239                  store->name());
1240             OK = false;
1241          }
1242          store->StorageId = sr.StorageId;   /* set storage Id */
1243          if (!sr.created) {                 /* if not created, update it */
1244             sr.AutoChanger = store->autochanger;
1245             if (!db_update_storage_record(NULL, db, &sr)) {
1246                Jmsg(NULL, M_FATAL, 0, _("Could not update storage record for %s\n"),
1247                     store->name());
1248                OK = false;
1249             }
1250          }
1251
1252          /* tls_require implies tls_enable */
1253          if (store->tls_require) {
1254             if (have_tls) {
1255                store->tls_enable = true;
1256             } else {
1257                Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1258                OK = false;
1259             }
1260          }
1261
1262          need_tls = store->tls_enable || store->tls_authenticate;
1263
1264          if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
1265             Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
1266                  " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
1267                  store->name(), configfile);
1268             OK = false;
1269          }
1270
1271          /* If everything is well, attempt to initialize our per-resource TLS context */
1272          if (OK && (need_tls || store->tls_require)) {
1273            /* Initialize TLS context:
1274             * Args: CA certfile, CA certdir, Certfile, Keyfile,
1275             * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1276             store->tls_ctx = new_tls_context(store->tls_ca_certfile,
1277                store->tls_ca_certdir, store->tls_certfile,
1278                store->tls_keyfile, NULL, NULL, NULL, true);
1279
1280             if (!store->tls_ctx) {
1281                Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
1282                     store->name(), configfile);
1283                OK = false;
1284             }
1285          }
1286       }
1287
1288       /* Link up all the children for each changer */
1289       foreach_res(store, R_STORAGE) {
1290          char sid[50];
1291          if (store->changer == store) {  /* we are a real Autochanger */
1292             store->ac_group = get_pool_memory(PM_FNAME);
1293             store->ac_group[0] = 0;
1294             pm_strcat(store->ac_group, edit_int64(store->StorageId, sid));
1295             /* Now look for children who point to this storage */
1296             foreach_res(ac_child, R_STORAGE) {
1297                if (ac_child != store && ac_child->changer == store) {
1298                   /* Found a child -- add StorageId */
1299                   pm_strcat(store->ac_group, ",");
1300                   pm_strcat(store->ac_group, edit_int64(ac_child->StorageId, sid));
1301                }
1302             }
1303          }
1304       }
1305
1306       /* Link up all the shared storage devices */
1307       foreach_res(store, R_STORAGE) {
1308          if (store->ac_group) {  /* we are a real Autochanger */
1309             /* Now look for Shared Storage who point to this storage */
1310             foreach_res(ac_child, R_STORAGE) {
1311                if (ac_child->shared_storage == store && ac_child->ac_group &&
1312                    ac_child->shared_storage != ac_child) {
1313                   pm_strcat(store->ac_group, ",");
1314                   pm_strcat(store->ac_group, ac_child->ac_group);
1315                }
1316             }
1317          }
1318       }
1319
1320       /* Loop over all counters, defining them in each database */
1321       /* Set default value in all counters */
1322       COUNTER *counter;
1323       foreach_res(counter, R_COUNTER) {
1324          /* Write to catalog? */
1325          if (!counter->created && counter->Catalog == catalog) {
1326             COUNTER_DBR cr;
1327             bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
1328             cr.MinValue = counter->MinValue;
1329             cr.MaxValue = counter->MaxValue;
1330             cr.CurrentValue = counter->MinValue;
1331             if (counter->WrapCounter) {
1332                bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
1333             } else {
1334                cr.WrapCounter[0] = 0;  /* empty string */
1335             }
1336             if (db_create_counter_record(NULL, db, &cr)) {
1337                counter->CurrentValue = cr.CurrentValue;
1338                counter->created = true;
1339                Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
1340             }
1341          }
1342          if (!counter->created) {
1343             counter->CurrentValue = counter->MinValue;  /* default value */
1344          }
1345       }
1346       /* cleanup old job records */
1347       if (mode == UPDATE_AND_FIX) {
1348          db_sql_query(db, cleanup_created_job, NULL, NULL);
1349          db_sql_query(db, cleanup_running_job, NULL, NULL);
1350       }
1351
1352       /* Set SQL engine name in global for debugging */
1353       set_db_engine_name(db_get_engine_name(db));
1354       if (db) db_close_database(NULL, db);
1355    }
1356    return OK;
1357 }
1358
1359 static void cleanup_old_files()
1360 {
1361    DIR* dp;
1362    struct dirent *entry, *result;
1363    int rc, name_max;
1364    int my_name_len = strlen(my_name);
1365    int len = strlen(director->working_directory);
1366    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
1367    POOLMEM *basename = get_pool_memory(PM_MESSAGE);
1368    regex_t preg1;
1369    char prbuf[500];
1370    const int nmatch = 30;
1371    regmatch_t pmatch[nmatch];
1372    berrno be;
1373
1374    /* Exclude spaces and look for .mail, .tmp or .restore.xx.bsr files */
1375    const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail|tmp)$";
1376
1377    /* Setup working directory prefix */
1378    pm_strcpy(basename, director->working_directory);
1379    if (len > 0 && !IsPathSeparator(director->working_directory[len-1])) {
1380       pm_strcat(basename, "/");
1381    }
1382
1383    /* Compile regex expressions */
1384    rc = regcomp(&preg1, pat1, REG_EXTENDED);
1385    if (rc != 0) {
1386       regerror(rc, &preg1, prbuf, sizeof(prbuf));
1387       Pmsg2(000,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
1388            pat1, prbuf);
1389       goto get_out2;
1390    }
1391
1392    name_max = pathconf(".", _PC_NAME_MAX);
1393    if (name_max < 1024) {
1394       name_max = 1024;
1395    }
1396
1397    if (!(dp = opendir(director->working_directory))) {
1398       berrno be;
1399       Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
1400             director->working_directory, be.bstrerror());
1401       goto get_out1;
1402       return;
1403    }
1404
1405    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
1406    while (1) {
1407       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
1408          break;
1409       }
1410       /* Exclude any name with ., .., not my_name or containing a space */
1411       if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
1412           strncmp(result->d_name, my_name, my_name_len) != 0) {
1413          Dmsg1(500, "Skipped: %s\n", result->d_name);
1414          continue;
1415       }
1416
1417       /* Unlink files that match regexes */
1418       if (regexec(&preg1, result->d_name, nmatch, pmatch,  0) == 0) {
1419          pm_strcpy(cleanup, basename);
1420          pm_strcat(cleanup, result->d_name);
1421          Dmsg1(100, "Unlink: %s\n", cleanup);
1422          unlink(cleanup);
1423       }
1424    }
1425
1426    free(entry);
1427    closedir(dp);
1428 /* Be careful to free up the correct resources */
1429 get_out1:
1430    regfree(&preg1);
1431 get_out2:
1432    free_pool_memory(cleanup);
1433    free_pool_memory(basename);
1434 }