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_ITEM job_items[];
70 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
75 "\nVersion: " VERSION " (" BDATE ")\n\n"
76 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
77 " -c <file> set configuration file to file\n"
78 " -dnn set debug level to nn\n"
79 " -f run in foreground (for debugging)\n"
81 " -r <job> run <job> now\n"
83 " -t test - read configuration and exit\n"
85 " -v verbose user messages\n"
86 " -? print this message.\n"
93 /*********************************************************************
95 * Main Bacula Server program
98 int main (int argc, char *argv[])
102 int no_signals = FALSE;
103 int test_config = FALSE;
108 my_name_is(argc, argv, "bacula-dir");
109 textdomain("bacula");
110 init_msg(NULL, NULL); /* initialize message handler */
112 daemon_start_time = time(NULL);
114 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
116 case 'c': /* specify config file */
117 if (configfile != NULL) {
120 configfile = bstrdup(optarg);
123 case 'd': /* set debug level */
124 debug_level = atoi(optarg);
125 if (debug_level <= 0) {
128 Dmsg1(0, "Debug level = %d\n", debug_level);
131 case 'f': /* run in foreground */
135 case 'g': /* set group id */
139 case 'r': /* run job */
140 if (runjob != NULL) {
144 runjob = bstrdup(optarg);
148 case 's': /* turn off signals */
152 case 't': /* test config */
156 case 'u': /* set uid */
160 case 'v': /* verbose */
174 init_signals(terminate_dird);
178 if (configfile != NULL) {
181 configfile = bstrdup(*argv);
189 if (configfile == NULL) {
190 configfile = bstrdup(CONFIG_FILE);
193 parse_config(configfile);
195 if (!check_resources()) {
196 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
203 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
205 FDConnectTimeout = (int)director->FDConnectTimeout;
206 SDConnectTimeout = (int)director->SDConnectTimeout;
210 init_stack_dump(); /* grab new pid */
213 /* Create pid must come after we are a daemon -- so we have our final pid */
214 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
215 read_state_file(director->working_directory, "bacula-dir", director->DIRport);
217 drop(uid, gid); /* reduce priveleges if requested */
219 /* signal(SIGHUP, reload_config); */
221 init_console_msg(working_directory);
223 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
224 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
226 Dmsg0(200, "Start UA server\n");
227 start_UA_server(director->DIRaddr, director->DIRport);
229 start_watchdog(); /* start network watchdog thread */
231 init_jcr_subsystem(); /* start JCR watchdogs etc. */
233 init_job_server(director->MaxConcurrentJobs);
235 Dmsg0(200, "wait for next job\n");
236 /* Main loop -- call scheduler to get next job to run */
237 while ((jcr = wait_for_next_job(runjob))) {
238 run_job(jcr); /* run job */
239 free_jcr(jcr); /* release jcr */
240 if (runjob) { /* command line, run a single job? */
241 break; /* yes, terminate */
248 /* Cleanup and then exit */
249 static void terminate_dird(int sig)
251 static int already_here = FALSE;
253 if (already_here) { /* avoid recursive temination problems */
257 write_state_file(director->working_directory, "bacula-dir", director->DIRport);
258 delete_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
259 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
264 if (configfile != NULL) {
267 if (debug_level > 5) {
268 print_memory_pool_stats();
270 free_config_resources();
272 term_msg(); /* terminate message handler */
274 close_memory_pool(); /* release free memory in pool */
279 struct RELOAD_TABLE {
284 static const int max_reloads = 10;
285 static RELOAD_TABLE reload_table[max_reloads];
287 static void init_reload(void)
289 for (int i=0; i < max_reloads; i++) {
290 reload_table[i].job_count = 0;
291 reload_table[i].res_table = NULL;
295 static void free_saved_resources(int table)
297 int num = r_last - r_first + 1;
298 RES **res_tab = reload_table[table].res_table;
299 Dmsg1(000, "Freeing resources for table %d\n", table);
300 for (int j=0; j<num; j++) {
301 free_resource(res_tab[j], r_first + j);
304 reload_table[table].job_count = 0;
305 reload_table[table].res_table = NULL;
309 * Called here at the end of every job that was
310 * hooked decrementing the active job_count. When
311 * it goes to zero, no one is using the associated
312 * resource table, so free it.
314 static void reload_job_end_cb(JCR *jcr)
317 int i = jcr->reload_id - 1;
318 Dmsg1(000, "reload job_end JobId=%d\n", jcr->JobId);
321 if (--reload_table[i].job_count <= 0) {
322 free_saved_resources(i);
329 static int find_free_table()
332 for (int i=0; i < max_reloads; i++) {
333 if (reload_table[i].res_table == NULL) {
342 * If we get here, we have received a SIGHUP, which means to
343 * reread our configuration file.
346 void reload_config(int sig)
348 static bool already_here = false;
354 Jmsg(NULL, M_ERROR, 0, _("Command not implemented\n"));
358 abort(); /* Oops, recursion -> die */
362 sigprocmask(SIG_BLOCK, &set, NULL);
367 table = find_free_table();
369 Jmsg(NULL, M_ERROR, 0, _("Too many reload requests.\n"));
374 * Hook all active jobs that are not already hooked (i.e.
378 if (jcr->reload_id == 0) {
379 reload_table[table].job_count++;
380 jcr->reload_id = table + 1;
381 job_end_push(jcr, reload_job_end_cb);
384 free_locked_jcr(jcr);
386 Dmsg1(000, "Reload_config njobs=%d\n", njobs);
387 reload_table[table].res_table = save_config_resources();
388 Dmsg1(000, "Saved old config in table %d\n", table);
390 Dmsg0(000, "Calling parse config\n");
391 parse_config(configfile);
393 Dmsg0(000, "Reloaded config file\n");
394 if (!check_resources()) {
395 rtable = find_free_table(); /* save new, bad table */
397 Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
399 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
401 reload_table[rtable].res_table = save_config_resources();
402 /* Now restore old resoure values */
403 int num = r_last - r_first + 1;
404 RES **res_tab = reload_table[table].res_table;
405 for (int i=0; i<num; i++) {
406 resources[i].res_head = res_tab[i];
408 table = rtable; /* release new, bad, saved table below */
412 set_working_directory(director->working_directory);
413 FDConnectTimeout = director->FDConnectTimeout;
414 SDConnectTimeout = director->SDConnectTimeout;
415 Dmsg0(0, "Director's configuration file reread.\n");
417 /* Now release saved resources */
418 free_saved_resources(table);
423 sigprocmask(SIG_UNBLOCK, &set, NULL);
424 signal(SIGHUP, reload_config);
425 already_here = false;
429 * Make a quick check to see that we have all the
432 * **** FIXME **** this routine could be a lot more
433 * intelligent and comprehensive.
435 static int check_resources()
442 job = (JOB *)GetNextRes(R_JOB, NULL);
443 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
445 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
446 Without that I don't know who I am :-(\n"), configfile);
449 set_working_directory(director->working_directory);
450 if (!director->messages) { /* If message resource not specified */
451 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
452 if (!director->messages) {
453 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
457 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
458 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
465 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
468 foreach_res(job, R_JOB) {
472 /* Transfer default items from JobDefs Resource */
473 for (i=0; job_items[i].name; i++) {
474 char **def_svalue, **svalue; /* string value */
475 int *def_ivalue, *ivalue; /* integer value */
476 int64_t *def_lvalue, *lvalue; /* 64 bit values */
479 Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
480 job->hdr.name, job_items[i].name,
481 bit_is_set(i, job->hdr.item_present),
482 bit_is_set(i, job->jobdefs->hdr.item_present));
484 if (!bit_is_set(i, job->hdr.item_present) &&
485 bit_is_set(i, job->jobdefs->hdr.item_present)) {
486 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
487 job->hdr.name, job_items[i].name);
488 offset = (char *)(job_items[i].value) - (char *)&res_all;
490 * Handle strings and directory strings
492 if (job_items[i].handler == store_str ||
493 job_items[i].handler == store_dir) {
494 def_svalue = (char **)((char *)(job->jobdefs) + offset);
495 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
496 job->hdr.name, job_items[i].name, *def_svalue, i, offset);
497 svalue = (char **)((char *)job + offset);
499 Dmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
501 *svalue = bstrdup(*def_svalue);
502 set_bit(i, job->hdr.item_present);
503 } else if (job_items[i].handler == store_res) {
504 def_svalue = (char **)((char *)(job->jobdefs) + offset);
505 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
506 job->hdr.name, job_items[i].name, i, offset);
507 svalue = (char **)((char *)job + offset);
509 Dmsg1(000, "Hey something is wrong. p=0x%lu\n", *svalue);
511 *svalue = *def_svalue;
512 set_bit(i, job->hdr.item_present);
514 * Handle integer fields
515 * Note, our store_yesno does not handle bitmaped fields
517 } else if (job_items[i].handler == store_yesno ||
518 job_items[i].handler == store_pint ||
519 job_items[i].handler == store_jobtype ||
520 job_items[i].handler == store_level ||
521 job_items[i].handler == store_pint ||
522 job_items[i].handler == store_replace) {
523 def_ivalue = (int *)((char *)(job->jobdefs) + offset);
524 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
525 job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
526 ivalue = (int *)((char *)job + offset);
527 *ivalue = *def_ivalue;
528 set_bit(i, job->hdr.item_present);
530 * Handle 64 bit integer fields
532 } else if (job_items[i].handler == store_time ||
533 job_items[i].handler == store_size ||
534 job_items[i].handler == store_int64) {
535 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
536 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
537 job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
538 lvalue = (int64_t *)((char *)job + offset);
539 *lvalue = *def_lvalue;
540 set_bit(i, job->hdr.item_present);
546 * Ensure that all required items are present
548 for (i=0; job_items[i].name; i++) {
549 if (job_items[i].flags & ITEM_REQUIRED) {
550 if (!bit_is_set(i, job->hdr.item_present)) {
551 Jmsg(NULL, M_FATAL, 0, "Field \"%s\" in Job \"%s\" resource is required, but not found.\n",
552 job_items[i].name, job->hdr.name);
556 /* If this triggers, take a look at lib/parse_conf.h */
557 if (i >= MAX_RES_ITEMS) {
558 Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
561 if (job->client && job->client->catalog) {
562 CAT *catalog = job->client->catalog;
566 * Make sure we can open catalog, otherwise print a warning
567 * message because the server is probably not running.
569 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
570 catalog->db_password, catalog->db_address,
571 catalog->db_port, catalog->db_socket);
572 if (!db || !db_open_database(NULL, db)) {
573 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
576 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
580 /* If a pool is defined for this job, create the pool DB
581 * record if it is not already created.
584 create_pool(NULL, db, job->pool, POOL_OP_UPDATE); /* update request */
586 /* Set default value in all counters */
588 foreach_res(counter, R_COUNTER) {
589 /* Write to catalog? */
590 if (!counter->created && counter->Catalog == catalog) {
592 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
593 cr.MinValue = counter->MinValue;
594 cr.MaxValue = counter->MaxValue;
595 cr.CurrentValue = counter->MinValue;
596 if (counter->WrapCounter) {
597 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
599 cr.WrapCounter[0] = 0; /* empty string */
601 if (db_create_counter_record(NULL, db, &cr)) {
602 counter->CurrentValue = cr.CurrentValue;
603 counter->created = true;
604 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
607 if (!counter->created) {
608 counter->CurrentValue = counter->MinValue; /* default value */
612 db_close_database(NULL, db);
618 close_msg(NULL); /* close temp message handler */
619 init_msg(NULL, director->messages); /* open daemon message handler */