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(char *addr, int port);
48 void init_job_server(int max_workers);
49 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
50 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
51 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
53 static char *configfile = NULL;
54 static char *runjob = NULL;
55 static int background = 1;
56 static void init_reload(void);
58 /* Globals Exported */
59 DIRRES *director; /* Director resource */
63 /* Globals Imported */
64 extern int r_first, r_last; /* first and last resources */
65 extern RES_TABLE resources[];
66 extern RES **res_head;
67 extern RES_ITEM job_items[];
71 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
76 "Copyright (C) 2000-2004 Kern Sibbald and John Walker\n"
77 "\nVersion: " VERSION " (" BDATE ")\n\n"
78 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
79 " -c <file> set configuration file to file\n"
80 " -dnn set debug level to nn\n"
81 " -f run in foreground (for debugging)\n"
83 " -r <job> run <job> now\n"
85 " -t test - read configuration and exit\n"
87 " -v verbose user messages\n"
88 " -? print this message.\n"
95 /*********************************************************************
97 * Main Bacula Server program
100 int main (int argc, char *argv[])
104 int no_signals = FALSE;
105 int test_config = FALSE;
110 my_name_is(argc, argv, "bacula-dir");
111 textdomain("bacula");
112 init_msg(NULL, NULL); /* initialize message handler */
114 daemon_start_time = time(NULL);
116 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
118 case 'c': /* specify config file */
119 if (configfile != NULL) {
122 configfile = bstrdup(optarg);
125 case 'd': /* set debug level */
126 debug_level = atoi(optarg);
127 if (debug_level <= 0) {
130 Dmsg1(0, "Debug level = %d\n", debug_level);
133 case 'f': /* run in foreground */
137 case 'g': /* set group id */
141 case 'r': /* run job */
142 if (runjob != NULL) {
146 runjob = bstrdup(optarg);
150 case 's': /* turn off signals */
154 case 't': /* test config */
158 case 'u': /* set uid */
162 case 'v': /* verbose */
176 init_signals(terminate_dird);
180 if (configfile != NULL) {
183 configfile = bstrdup(*argv);
191 if (configfile == NULL) {
192 configfile = bstrdup(CONFIG_FILE);
195 parse_config(configfile);
197 if (!check_resources()) {
198 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
205 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
207 FDConnectTimeout = (int)director->FDConnectTimeout;
208 SDConnectTimeout = (int)director->SDConnectTimeout;
212 init_stack_dump(); /* grab new pid */
215 /* Create pid must come after we are a daemon -- so we have our final pid */
216 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
217 read_state_file(director->working_directory, "bacula-dir", director->DIRport);
219 drop(uid, gid); /* reduce priveleges if requested */
221 signal(SIGHUP, reload_config);
223 init_console_msg(working_directory);
225 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
226 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
228 Dmsg0(200, "Start UA server\n");
229 start_UA_server(director->DIRaddr, director->DIRport);
231 start_watchdog(); /* start network watchdog thread */
233 init_jcr_subsystem(); /* start JCR watchdogs etc. */
235 init_job_server(director->MaxConcurrentJobs);
237 Dmsg0(200, "wait for next job\n");
238 /* Main loop -- call scheduler to get next job to run */
239 while ((jcr = wait_for_next_job(runjob))) {
240 run_job(jcr); /* run job */
241 free_jcr(jcr); /* release jcr */
242 if (runjob) { /* command line, run a single job? */
243 break; /* yes, terminate */
250 /* Cleanup and then exit */
251 static void terminate_dird(int sig)
253 static int already_here = FALSE;
255 if (already_here) { /* avoid recursive temination problems */
259 write_state_file(director->working_directory, "bacula-dir", director->DIRport);
260 delete_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
261 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
266 if (configfile != NULL) {
269 if (debug_level > 5) {
270 print_memory_pool_stats();
272 free_config_resources();
274 term_msg(); /* terminate message handler */
276 close_memory_pool(); /* release free memory in pool */
281 struct RELOAD_TABLE {
286 static const int max_reloads = 10;
287 static RELOAD_TABLE reload_table[max_reloads];
289 static void init_reload(void)
291 for (int i=0; i < max_reloads; i++) {
292 reload_table[i].job_count = 0;
293 reload_table[i].res_table = NULL;
297 static void free_saved_resources(int table)
299 int num = r_last - r_first + 1;
300 RES **res_tab = reload_table[table].res_table;
302 Dmsg1(100, "res_tab for table %d already released.\n", table);
305 Dmsg1(100, "Freeing resources for table %d\n", table);
306 for (int j=0; j<num; j++) {
307 free_resource(res_tab[j], r_first + j);
310 reload_table[table].job_count = 0;
311 reload_table[table].res_table = NULL;
315 * Called here at the end of every job that was
316 * hooked decrementing the active job_count. When
317 * it goes to zero, no one is using the associated
318 * resource table, so free it.
320 static void reload_job_end_cb(JCR *jcr, void *ctx)
322 int reload_id = (int)((long int)ctx);
323 Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
324 reload_id, reload_table[reload_id].job_count);
327 if (--reload_table[reload_id].job_count <= 0) {
328 free_saved_resources(reload_id);
334 static int find_free_reload_table_entry()
337 for (int i=0; i < max_reloads; i++) {
338 if (reload_table[i].res_table == NULL) {
347 * If we get here, we have received a SIGHUP, which means to
348 * reread our configuration file.
350 * The algorithm used is as follows: we count how many jobs are
351 * running and mark the running jobs to make a callback on
352 * exiting. The old config is saved with the reload table
353 * id in a reload table. The new config file is read. Now, as
354 * each job exits, it calls back to the reload_job_end_cb(), which
355 * decrements the count of open jobs for the given reload table.
356 * When the count goes to zero, we release those resources.
357 * This allows us to have pointers into the resource table (from
358 * jobs), and once they exit and all the pointers are released, we
359 * release the old table. Note, if no new jobs are running since the
360 * last reload, then the old resources will be immediately release.
361 * A console is considered a job because it may have pointers to
362 * resources, but a SYSTEM job is not since it *should* not have any
363 * permanent pointers to jobs.
366 void reload_config(int sig)
368 static bool already_here = false;
371 int njobs = 0; /* number of running jobs */
375 abort(); /* Oops, recursion -> die */
379 sigaddset(&set, SIGHUP);
380 sigprocmask(SIG_BLOCK, &set, NULL);
382 // Jmsg(NULL, M_INFO, 0, "Entering experimental reload config code. Bug reports will not be accepted.\n");
387 table = find_free_reload_table_entry();
389 Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. Request ignored.\n"));
393 Dmsg1(100, "Reload_config njobs=%d\n", njobs);
394 reload_table[table].res_table = save_config_resources();
395 Dmsg1(100, "Saved old config in table %d\n", table);
397 parse_config(configfile);
399 Dmsg0(100, "Reloaded config file\n");
400 if (!check_resources()) {
401 rtable = find_free_reload_table_entry(); /* save new, bad table */
403 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
404 Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
406 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
408 reload_table[rtable].res_table = save_config_resources();
409 /* Now restore old resoure values */
410 int num = r_last - r_first + 1;
411 RES **res_tab = reload_table[table].res_table;
412 for (int i=0; i<num; i++) {
413 res_head[i] = res_tab[i];
415 table = rtable; /* release new, bad, saved table below */
418 * Hook all active jobs so that they release this table
421 if (jcr->JobType != JT_SYSTEM) {
422 reload_table[table].job_count++;
423 job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
426 free_locked_jcr(jcr);
431 set_working_directory(director->working_directory);
432 FDConnectTimeout = director->FDConnectTimeout;
433 SDConnectTimeout = director->SDConnectTimeout;
434 Dmsg0(0, "Director's configuration file reread.\n");
436 /* Now release saved resources, if no jobs using the resources */
438 free_saved_resources(table);
444 sigprocmask(SIG_UNBLOCK, &set, NULL);
445 signal(SIGHUP, reload_config);
446 already_here = false;
450 * Make a quick check to see that we have all the
453 * **** FIXME **** this routine could be a lot more
454 * intelligent and comprehensive.
456 static int check_resources()
463 job = (JOB *)GetNextRes(R_JOB, NULL);
464 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
466 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
467 Without that I don't know who I am :-(\n"), configfile);
470 set_working_directory(director->working_directory);
471 if (!director->messages) { /* If message resource not specified */
472 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
473 if (!director->messages) {
474 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
478 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
479 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
486 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
489 foreach_res(job, R_JOB) {
493 /* Transfer default items from JobDefs Resource */
494 for (i=0; job_items[i].name; i++) {
495 char **def_svalue, **svalue; /* string value */
496 int *def_ivalue, *ivalue; /* integer value */
497 int64_t *def_lvalue, *lvalue; /* 64 bit values */
500 Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
501 job->hdr.name, job_items[i].name,
502 bit_is_set(i, job->hdr.item_present),
503 bit_is_set(i, job->jobdefs->hdr.item_present));
505 if (!bit_is_set(i, job->hdr.item_present) &&
506 bit_is_set(i, job->jobdefs->hdr.item_present)) {
507 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
508 job->hdr.name, job_items[i].name);
509 offset = (char *)(job_items[i].value) - (char *)&res_all;
511 * Handle strings and directory strings
513 if (job_items[i].handler == store_str ||
514 job_items[i].handler == store_dir) {
515 def_svalue = (char **)((char *)(job->jobdefs) + offset);
516 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
517 job->hdr.name, job_items[i].name, *def_svalue, i, offset);
518 svalue = (char **)((char *)job + offset);
520 Pmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
522 *svalue = bstrdup(*def_svalue);
523 set_bit(i, job->hdr.item_present);
524 } else if (job_items[i].handler == store_res) {
525 def_svalue = (char **)((char *)(job->jobdefs) + offset);
526 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
527 job->hdr.name, job_items[i].name, i, offset);
528 svalue = (char **)((char *)job + offset);
530 Pmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
532 *svalue = *def_svalue;
533 set_bit(i, job->hdr.item_present);
535 * Handle integer fields
536 * Note, our store_yesno does not handle bitmaped fields
538 } else if (job_items[i].handler == store_yesno ||
539 job_items[i].handler == store_pint ||
540 job_items[i].handler == store_jobtype ||
541 job_items[i].handler == store_level ||
542 job_items[i].handler == store_pint ||
543 job_items[i].handler == store_replace) {
544 def_ivalue = (int *)((char *)(job->jobdefs) + offset);
545 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
546 job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
547 ivalue = (int *)((char *)job + offset);
548 *ivalue = *def_ivalue;
549 set_bit(i, job->hdr.item_present);
551 * Handle 64 bit integer fields
553 } else if (job_items[i].handler == store_time ||
554 job_items[i].handler == store_size ||
555 job_items[i].handler == store_int64) {
556 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
557 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
558 job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
559 lvalue = (int64_t *)((char *)job + offset);
560 *lvalue = *def_lvalue;
561 set_bit(i, job->hdr.item_present);
567 * Ensure that all required items are present
569 for (i=0; job_items[i].name; i++) {
570 if (job_items[i].flags & ITEM_REQUIRED) {
571 if (!bit_is_set(i, job->hdr.item_present)) {
572 Jmsg(NULL, M_FATAL, 0, "Field \"%s\" in Job \"%s\" resource is required, but not found.\n",
573 job_items[i].name, job->hdr.name);
577 /* If this triggers, take a look at lib/parse_conf.h */
578 if (i >= MAX_RES_ITEMS) {
579 Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
582 } /* End loop over Job res */
584 /* Loop over databases */
586 foreach_res(catalog, R_CATALOG) {
589 * Make sure we can open catalog, otherwise print a warning
590 * message because the server is probably not running.
592 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
593 catalog->db_password, catalog->db_address,
594 catalog->db_port, catalog->db_socket);
595 if (!db || !db_open_database(NULL, db)) {
596 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
599 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
605 /* Loop over all pools, defining/updating them in each database */
607 foreach_res(pool, R_POOL) {
608 create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
610 /* Loop over all counters, defining them in each database */
612 /* Set default value in all counters */
614 foreach_res(counter, R_COUNTER) {
615 /* Write to catalog? */
616 if (!counter->created && counter->Catalog == catalog) {
618 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
619 cr.MinValue = counter->MinValue;
620 cr.MaxValue = counter->MaxValue;
621 cr.CurrentValue = counter->MinValue;
622 if (counter->WrapCounter) {
623 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
625 cr.WrapCounter[0] = 0; /* empty string */
627 if (db_create_counter_record(NULL, db, &cr)) {
628 counter->CurrentValue = cr.CurrentValue;
629 counter->created = true;
630 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
633 if (!counter->created) {
634 counter->CurrentValue = counter->MinValue; /* default value */
637 db_close_database(NULL, db);
642 close_msg(NULL); /* close temp message handler */
643 init_msg(NULL, director->messages); /* open daemon message handler */