3 * Bacula Director daemon -- this is the main program
5 * Kern Sibbald, March MM
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Forward referenced subroutines */
33 static void terminate_dird(int sig);
34 static int check_resources();
36 /* Exported subroutines */
38 extern "C" void reload_config(int sig);
41 /* Imported subroutines */
42 JCR *wait_for_next_job(char *runjob);
43 void term_scheduler();
44 void term_ua_server();
45 int do_backup(JCR *jcr);
46 void backup_cleanup(void);
47 void start_UA_server(dlist *addrs);
48 void init_job_server(int max_workers);
49 void term_job_server();
50 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
51 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
52 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
54 static char *configfile = NULL;
55 static char *runjob = NULL;
56 static int background = 1;
57 static void init_reload(void);
59 /* Globals Exported */
60 DIRRES *director; /* Director resource */
64 /* Globals Imported */
65 extern int r_first, r_last; /* first and last resources */
66 extern RES_TABLE resources[];
67 extern RES **res_head;
68 extern RES_ITEM job_items[];
72 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
77 "Copyright (C) 2000-2004 Kern Sibbald and John Walker\n"
78 "\nVersion: " VERSION " (" BDATE ")\n\n"
79 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
80 " -c <file> set configuration file to file\n"
81 " -dnn set debug level to nn\n"
82 " -f run in foreground (for debugging)\n"
84 " -r <job> run <job> now\n"
86 " -t test - read configuration and exit\n"
88 " -v verbose user messages\n"
89 " -? print this message.\n"
96 /*********************************************************************
98 * Main Bacula Server program
101 int main (int argc, char *argv[])
105 int no_signals = FALSE;
106 int test_config = FALSE;
111 my_name_is(argc, argv, "bacula-dir");
112 textdomain("bacula");
113 init_msg(NULL, NULL); /* initialize message handler */
115 daemon_start_time = time(NULL);
117 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
119 case 'c': /* specify config file */
120 if (configfile != NULL) {
123 configfile = bstrdup(optarg);
126 case 'd': /* set debug level */
127 debug_level = atoi(optarg);
128 if (debug_level <= 0) {
131 Dmsg1(0, "Debug level = %d\n", debug_level);
134 case 'f': /* run in foreground */
138 case 'g': /* set group id */
142 case 'r': /* run job */
143 if (runjob != NULL) {
147 runjob = bstrdup(optarg);
151 case 's': /* turn off signals */
155 case 't': /* test config */
159 case 'u': /* set uid */
163 case 'v': /* verbose */
177 init_signals(terminate_dird);
181 if (configfile != NULL) {
184 configfile = bstrdup(*argv);
192 if (configfile == NULL) {
193 configfile = bstrdup(CONFIG_FILE);
196 parse_config(configfile);
198 if (!check_resources()) {
199 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
206 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
208 FDConnectTimeout = (int)director->FDConnectTimeout;
209 SDConnectTimeout = (int)director->SDConnectTimeout;
213 init_stack_dump(); /* grab new pid */
216 /* Create pid must come after we are a daemon -- so we have our final pid */
217 create_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
218 read_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
220 drop(uid, gid); /* reduce priveleges if requested */
222 signal(SIGHUP, reload_config);
224 init_console_msg(working_directory);
226 init_python_interpreter(director->hdr.name, director->scripts_directory ?
227 director->scripts_directory : ".");
229 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
230 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
232 Dmsg0(200, "Start UA server\n");
233 start_UA_server(director->DIRaddrs);
235 start_watchdog(); /* start network watchdog thread */
237 init_jcr_subsystem(); /* start JCR watchdogs etc. */
239 init_job_server(director->MaxConcurrentJobs);
241 Dmsg0(200, "wait for next job\n");
242 /* Main loop -- call scheduler to get next job to run */
243 while ((jcr = wait_for_next_job(runjob))) {
244 run_job(jcr); /* run job */
245 free_jcr(jcr); /* release jcr */
246 if (runjob) { /* command line, run a single job? */
247 break; /* yes, terminate */
254 /* Cleanup and then exit */
255 static void terminate_dird(int sig)
257 static int already_here = FALSE;
259 if (already_here) { /* avoid recursive temination problems */
263 write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
264 delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
265 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
271 if (configfile != NULL) {
274 if (debug_level > 5) {
275 print_memory_pool_stats();
277 free_config_resources();
279 term_msg(); /* terminate message handler */
281 close_memory_pool(); /* release free memory in pool */
286 struct RELOAD_TABLE {
291 static const int max_reloads = 10;
292 static RELOAD_TABLE reload_table[max_reloads];
294 static void init_reload(void)
296 for (int i=0; i < max_reloads; i++) {
297 reload_table[i].job_count = 0;
298 reload_table[i].res_table = NULL;
302 static void free_saved_resources(int table)
304 int num = r_last - r_first + 1;
305 RES **res_tab = reload_table[table].res_table;
307 Dmsg1(100, "res_tab for table %d already released.\n", table);
310 Dmsg1(100, "Freeing resources for table %d\n", table);
311 for (int j=0; j<num; j++) {
312 free_resource(res_tab[j], r_first + j);
315 reload_table[table].job_count = 0;
316 reload_table[table].res_table = NULL;
320 * Called here at the end of every job that was
321 * hooked decrementing the active job_count. When
322 * it goes to zero, no one is using the associated
323 * resource table, so free it.
325 static void reload_job_end_cb(JCR *jcr, void *ctx)
327 int reload_id = (int)((long int)ctx);
328 Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
329 reload_id, reload_table[reload_id].job_count);
332 if (--reload_table[reload_id].job_count <= 0) {
333 free_saved_resources(reload_id);
339 static int find_free_reload_table_entry()
342 for (int i=0; i < max_reloads; i++) {
343 if (reload_table[i].res_table == NULL) {
352 * If we get here, we have received a SIGHUP, which means to
353 * reread our configuration file.
355 * The algorithm used is as follows: we count how many jobs are
356 * running and mark the running jobs to make a callback on
357 * exiting. The old config is saved with the reload table
358 * id in a reload table. The new config file is read. Now, as
359 * each job exits, it calls back to the reload_job_end_cb(), which
360 * decrements the count of open jobs for the given reload table.
361 * When the count goes to zero, we release those resources.
362 * This allows us to have pointers into the resource table (from
363 * jobs), and once they exit and all the pointers are released, we
364 * release the old table. Note, if no new jobs are running since the
365 * last reload, then the old resources will be immediately release.
366 * A console is considered a job because it may have pointers to
367 * resources, but a SYSTEM job is not since it *should* not have any
368 * permanent pointers to jobs.
371 void reload_config(int sig)
373 static bool already_here = false;
376 int njobs = 0; /* number of running jobs */
381 abort(); /* Oops, recursion -> die */
385 sigaddset(&set, SIGHUP);
386 sigprocmask(SIG_BLOCK, &set, NULL);
388 // Jmsg(NULL, M_INFO, 0, "Entering experimental reload config code. Bug reports will not be accepted.\n");
393 table = find_free_reload_table_entry();
395 Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. Request ignored.\n"));
399 Dmsg1(100, "Reload_config njobs=%d\n", njobs);
400 reload_table[table].res_table = save_config_resources();
401 Dmsg1(100, "Saved old config in table %d\n", table);
403 ok = parse_config(configfile, 0); /* no exit on error */
405 Dmsg0(100, "Reloaded config file\n");
406 if (!ok || !check_resources()) {
407 rtable = find_free_reload_table_entry(); /* save new, bad table */
409 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
410 Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
412 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
413 Jmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
415 reload_table[rtable].res_table = save_config_resources();
416 /* Now restore old resoure values */
417 int num = r_last - r_first + 1;
418 RES **res_tab = reload_table[table].res_table;
419 for (int i=0; i<num; i++) {
420 res_head[i] = res_tab[i];
422 table = rtable; /* release new, bad, saved table below */
425 * Hook all active jobs so that they release this table
428 if (jcr->JobType != JT_SYSTEM) {
429 reload_table[table].job_count++;
430 job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
433 free_locked_jcr(jcr);
438 set_working_directory(director->working_directory);
439 FDConnectTimeout = director->FDConnectTimeout;
440 SDConnectTimeout = director->SDConnectTimeout;
441 Dmsg0(0, "Director's configuration file reread.\n");
443 /* Now release saved resources, if no jobs using the resources */
445 free_saved_resources(table);
451 sigprocmask(SIG_UNBLOCK, &set, NULL);
452 signal(SIGHUP, reload_config);
453 already_here = false;
457 * Make a quick check to see that we have all the
460 * **** FIXME **** this routine could be a lot more
461 * intelligent and comprehensive.
463 static int check_resources()
470 job = (JOB *)GetNextRes(R_JOB, NULL);
471 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
473 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
474 "Without that I don't know who I am :-(\n"), configfile);
477 set_working_directory(director->working_directory);
478 if (!director->messages) { /* If message resource not specified */
479 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
480 if (!director->messages) {
481 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
485 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
486 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
493 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
496 foreach_res(job, R_JOB) {
500 /* Handle Storage alists specifically */
501 JOB *jobdefs = job->jobdefs;
502 for (i=0; i < MAX_STORE; i++) {
503 if (jobdefs->storage[i] && !job->storage[i]) {
505 job->storage[i] = New(alist(10, not_owned_by_alist));
506 foreach_alist(st, jobdefs->storage[i]) {
507 job->storage[i]->append(st);
512 /* Transfer default items from JobDefs Resource */
513 for (i=0; job_items[i].name; i++) {
514 char **def_svalue, **svalue; /* string value */
515 int *def_ivalue, *ivalue; /* integer value */
516 int64_t *def_lvalue, *lvalue; /* 64 bit values */
519 Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
520 job->hdr.name, job_items[i].name,
521 bit_is_set(i, job->hdr.item_present),
522 bit_is_set(i, job->jobdefs->hdr.item_present));
524 if (!bit_is_set(i, job->hdr.item_present) &&
525 bit_is_set(i, job->jobdefs->hdr.item_present)) {
526 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
527 job->hdr.name, job_items[i].name);
528 offset = (char *)(job_items[i].value) - (char *)&res_all;
530 * Handle strings and directory strings
532 if (job_items[i].handler == store_str ||
533 job_items[i].handler == store_dir) {
534 def_svalue = (char **)((char *)(job->jobdefs) + offset);
535 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
536 job->hdr.name, job_items[i].name, *def_svalue, i, offset);
537 svalue = (char **)((char *)job + offset);
539 Pmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
541 *svalue = bstrdup(*def_svalue);
542 set_bit(i, job->hdr.item_present);
546 } else if (job_items[i].handler == store_res) {
547 def_svalue = (char **)((char *)(job->jobdefs) + offset);
548 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
549 job->hdr.name, job_items[i].name, i, offset);
550 svalue = (char **)((char *)job + offset);
552 Pmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
554 *svalue = *def_svalue;
555 set_bit(i, job->hdr.item_present);
557 * Handle alist resources
559 } else if (job_items[i].handler == store_alist_res) {
560 if (bit_is_set(i, job->jobdefs->hdr.item_present)) {
561 set_bit(i, job->hdr.item_present);
564 * Handle integer fields
565 * Note, our store_yesno does not handle bitmaped fields
567 } else if (job_items[i].handler == store_yesno ||
568 job_items[i].handler == store_pint ||
569 job_items[i].handler == store_jobtype ||
570 job_items[i].handler == store_level ||
571 job_items[i].handler == store_pint ||
572 job_items[i].handler == store_replace) {
573 def_ivalue = (int *)((char *)(job->jobdefs) + offset);
574 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
575 job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
576 ivalue = (int *)((char *)job + offset);
577 *ivalue = *def_ivalue;
578 set_bit(i, job->hdr.item_present);
580 * Handle 64 bit integer fields
582 } else if (job_items[i].handler == store_time ||
583 job_items[i].handler == store_size ||
584 job_items[i].handler == store_int64) {
585 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
586 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
587 job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
588 lvalue = (int64_t *)((char *)job + offset);
589 *lvalue = *def_lvalue;
590 set_bit(i, job->hdr.item_present);
596 * Ensure that all required items are present
598 for (i=0; job_items[i].name; i++) {
599 if (job_items[i].flags & ITEM_REQUIRED) {
600 if (!bit_is_set(i, job->hdr.item_present)) {
601 Jmsg(NULL, M_FATAL, 0, "\"%s\" directive in Job \"%s\" resource is required, but not found.\n",
602 job_items[i].name, job->hdr.name);
606 /* If this triggers, take a look at lib/parse_conf.h */
607 if (i >= MAX_RES_ITEMS) {
608 Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
611 } /* End loop over Job res */
613 /* Loop over databases */
615 foreach_res(catalog, R_CATALOG) {
618 * Make sure we can open catalog, otherwise print a warning
619 * message because the server is probably not running.
621 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
622 catalog->db_password, catalog->db_address,
623 catalog->db_port, catalog->db_socket,
624 catalog->mult_db_connections);
625 if (!db || !db_open_database(NULL, db)) {
626 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
629 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
635 /* Loop over all pools, defining/updating them in each database */
637 foreach_res(pool, R_POOL) {
638 create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
640 /* Loop over all counters, defining them in each database */
642 /* Set default value in all counters */
644 foreach_res(counter, R_COUNTER) {
645 /* Write to catalog? */
646 if (!counter->created && counter->Catalog == catalog) {
648 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
649 cr.MinValue = counter->MinValue;
650 cr.MaxValue = counter->MaxValue;
651 cr.CurrentValue = counter->MinValue;
652 if (counter->WrapCounter) {
653 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
655 cr.WrapCounter[0] = 0; /* empty string */
657 if (db_create_counter_record(NULL, db, &cr)) {
658 counter->CurrentValue = cr.CurrentValue;
659 counter->created = true;
660 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
663 if (!counter->created) {
664 counter->CurrentValue = counter->MinValue; /* default value */
667 db_close_database(NULL, db);
672 close_msg(NULL); /* close temp message handler */
673 init_msg(NULL, director->messages); /* open daemon message handler */