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();
35 static void reload_config(int sig);
37 /* Exported subroutines */
40 /* Imported subroutines */
41 JCR *wait_for_next_job(char *runjob);
42 void term_scheduler();
43 void term_ua_server();
44 int do_backup(JCR *jcr);
45 void backup_cleanup(void);
46 void start_UA_server(char *addr, int port);
47 void init_job_server(int max_workers);
48 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
49 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
50 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
52 static char *configfile = NULL;
53 static char *runjob = NULL;
54 static int background = 1;
55 static void init_reload(void);
57 /* Globals Exported */
58 DIRRES *director; /* Director resource */
62 /* Globals Imported */
63 extern int r_first, r_last; /* first and last resources */
64 extern RES_ITEM job_items[];
68 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
73 "\nVersion: " VERSION " (" BDATE ")\n\n"
74 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
75 " -c <file> set configuration file to file\n"
76 " -dnn set debug level to nn\n"
77 " -f run in foreground (for debugging)\n"
79 " -r <job> run <job> now\n"
81 " -t test - read configuration and exit\n"
83 " -v verbose user messages\n"
84 " -? print this message.\n"
91 /*********************************************************************
93 * Main Bacula Server program
96 int main (int argc, char *argv[])
100 int no_signals = FALSE;
101 int test_config = FALSE;
106 my_name_is(argc, argv, "bacula-dir");
107 textdomain("bacula-dir");
108 init_msg(NULL, NULL); /* initialize message handler */
110 daemon_start_time = time(NULL);
112 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
114 case 'c': /* specify config file */
115 if (configfile != NULL) {
118 configfile = bstrdup(optarg);
121 case 'd': /* set debug level */
122 debug_level = atoi(optarg);
123 if (debug_level <= 0) {
126 Dmsg1(0, "Debug level = %d\n", debug_level);
129 case 'f': /* run in foreground */
133 case 'g': /* set group id */
137 case 'r': /* run job */
138 if (runjob != NULL) {
142 runjob = bstrdup(optarg);
146 case 's': /* turn off signals */
150 case 't': /* test config */
154 case 'u': /* set uid */
158 case 'v': /* verbose */
172 init_signals(terminate_dird);
176 if (configfile != NULL) {
179 configfile = bstrdup(*argv);
187 if (configfile == NULL) {
188 configfile = bstrdup(CONFIG_FILE);
191 parse_config(configfile);
193 if (!check_resources()) {
194 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
201 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
203 FDConnectTimeout = (int)director->FDConnectTimeout;
204 SDConnectTimeout = (int)director->SDConnectTimeout;
208 init_stack_dump(); /* grab new pid */
211 /* Create pid must come after we are a daemon -- so we have our final pid */
212 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
213 read_state_file(director->working_directory, "bacula-dir", director->DIRport);
215 drop(uid, gid); /* reduce priveleges if requested */
217 signal(SIGHUP, reload_config);
219 init_console_msg(working_directory);
221 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
222 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
224 Dmsg0(200, "Start UA server\n");
225 start_UA_server(director->DIRaddr, director->DIRport);
227 start_watchdog(); /* start network watchdog thread */
229 init_jcr_subsystem(); /* start JCR watchdogs etc. */
231 init_job_server(director->MaxConcurrentJobs);
233 Dmsg0(200, "wait for next job\n");
234 /* Main loop -- call scheduler to get next job to run */
235 while ((jcr = wait_for_next_job(runjob))) {
236 run_job(jcr); /* run job */
237 free_jcr(jcr); /* release jcr */
238 if (runjob) { /* command line, run a single job? */
239 break; /* yes, terminate */
246 /* Cleanup and then exit */
247 static void terminate_dird(int sig)
249 static int already_here = FALSE;
251 if (already_here) { /* avoid recursive temination problems */
255 write_state_file(director->working_directory, "bacula-dir", director->DIRport);
256 delete_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
257 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
262 if (configfile != NULL) {
265 if (debug_level > 5) {
266 print_memory_pool_stats();
268 free_config_resources();
270 term_msg(); /* terminate message handler */
272 close_memory_pool(); /* release free memory in pool */
277 struct RELOAD_TABLE {
282 static const int max_reloads = 10;
283 static RELOAD_TABLE reload_table[max_reloads];
285 static void init_reload(void)
287 for (int i=0; i < max_reloads; i++) {
288 reload_table[i].job_count = 0;
289 reload_table[i].res_table = NULL;
294 * Called here at the end of every job that was
295 * hooked decrementing the active job_count. When
296 * it goes to zero, no one is using the associated
297 * resource table, so free it.
299 static void reload_job_end_cb(JCR *jcr)
301 int i = jcr->reload_id - 1;
303 Dmsg1(000, "reload job_end JobId=%d\n", jcr->JobId);
304 if (--reload_table[i].job_count <= 0) {
305 int num = r_last - r_first + 1;
306 res_tab = reload_table[i].res_table;
307 Dmsg0(000, "Freeing resources\n");
308 for (int j=0; j<num; j++) {
309 free_resource(res_tab[j], r_first + j);
312 reload_table[i].job_count = 0;
313 reload_table[i].res_table = NULL;
318 * If we get here, we have received a SIGHUP, which means to
319 * reread our configuration file.
321 static void reload_config(int sig)
323 static bool already_here = false;
330 abort(); /* Oops, recursion -> die */
334 sigprocmask(SIG_BLOCK, &set, NULL);
339 for (int i=0; i < max_reloads; i++) {
340 if (reload_table[i].res_table == NULL) {
346 Jmsg(NULL, M_ERROR, 0, _("Too many reload requests.\n"));
351 * Hook all active jobs that are not already hooked (i.e.
355 /* JobId==0 => console */
356 if (jcr->JobId != 0 && jcr->reload_id == 0) {
357 reload_table[table].job_count++;
358 jcr->reload_id = table + 1;
359 job_end_push(jcr, reload_job_end_cb);
362 free_locked_jcr(jcr);
364 Dmsg1(000, "Reload_config njobs=%d\n", njobs);
366 reload_table[table].res_table = save_config_resources();
367 Dmsg1(000, "Saved old config in table %d\n", table);
369 free_config_resources();
372 Dmsg0(000, "Calling parse config\n");
373 parse_config(configfile);
375 Dmsg0(000, "Reloaded config file\n");
376 if (!check_resources()) {
377 Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
381 set_working_directory(director->working_directory);
382 FDConnectTimeout = director->FDConnectTimeout;
383 SDConnectTimeout = director->SDConnectTimeout;
384 Dmsg0(0, "Director's configuration file reread.\n");
389 sigprocmask(SIG_UNBLOCK, &set, NULL);
390 signal(SIGHUP, reload_config);
391 already_here = false;
395 * Make a quick check to see that we have all the
398 * **** FIXME **** this routine could be a lot more
399 * intelligent and comprehensive.
401 static int check_resources()
408 job = (JOB *)GetNextRes(R_JOB, NULL);
409 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
411 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
412 Without that I don't know who I am :-(\n"), configfile);
415 set_working_directory(director->working_directory);
416 if (!director->messages) { /* If message resource not specified */
417 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
418 if (!director->messages) {
419 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
423 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
424 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
431 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
434 foreach_res(job, R_JOB) {
438 /* Transfer default items from JobDefs Resource */
439 for (i=0; job_items[i].name; i++) {
440 char **def_svalue, **svalue; /* string value */
441 int *def_ivalue, *ivalue; /* integer value */
442 int64_t *def_lvalue, *lvalue; /* 64 bit values */
445 Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
446 job->hdr.name, job_items[i].name,
447 bit_is_set(i, job->hdr.item_present),
448 bit_is_set(i, job->jobdefs->hdr.item_present));
450 if (!bit_is_set(i, job->hdr.item_present) &&
451 bit_is_set(i, job->jobdefs->hdr.item_present)) {
452 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
453 job->hdr.name, job_items[i].name);
454 offset = (char *)(job_items[i].value) - (char *)&res_all;
456 * Handle strings and directory strings
458 if (job_items[i].handler == store_str ||
459 job_items[i].handler == store_dir) {
460 def_svalue = (char **)((char *)(job->jobdefs) + offset);
461 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
462 job->hdr.name, job_items[i].name, *def_svalue, i, offset);
463 svalue = (char **)((char *)job + offset);
465 Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
467 *svalue = bstrdup(*def_svalue);
468 set_bit(i, job->hdr.item_present);
469 } else if (job_items[i].handler == store_res) {
470 def_svalue = (char **)((char *)(job->jobdefs) + offset);
471 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
472 job->hdr.name, job_items[i].name, i, offset);
473 svalue = (char **)((char *)job + offset);
475 Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
477 *svalue = *def_svalue;
478 set_bit(i, job->hdr.item_present);
480 * Handle integer fields
481 * Note, our store_yesno does not handle bitmaped fields
483 } else if (job_items[i].handler == store_yesno ||
484 job_items[i].handler == store_pint ||
485 job_items[i].handler == store_jobtype ||
486 job_items[i].handler == store_level ||
487 job_items[i].handler == store_pint ||
488 job_items[i].handler == store_replace) {
489 def_ivalue = (int *)((char *)(job->jobdefs) + offset);
490 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
491 job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
492 ivalue = (int *)((char *)job + offset);
493 *ivalue = *def_ivalue;
494 set_bit(i, job->hdr.item_present);
496 * Handle 64 bit integer fields
498 } else if (job_items[i].handler == store_time ||
499 job_items[i].handler == store_size ||
500 job_items[i].handler == store_int64) {
501 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
502 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
503 job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
504 lvalue = (int64_t *)((char *)job + offset);
505 *lvalue = *def_lvalue;
506 set_bit(i, job->hdr.item_present);
512 * Ensure that all required items are present
514 for (i=0; job_items[i].name; i++) {
515 if (job_items[i].flags & ITEM_REQUIRED) {
516 if (!bit_is_set(i, job->hdr.item_present)) {
517 Jmsg(NULL, M_FATAL, 0, "Field \"%s\" in Job \"%s\" resource is required, but not found.\n",
518 job_items[i].name, job->hdr.name);
522 /* If this triggers, take a look at lib/parse_conf.h */
523 if (i >= MAX_RES_ITEMS) {
524 Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
527 if (job->client && job->client->catalog) {
528 CAT *catalog = job->client->catalog;
532 * Make sure we can open catalog, otherwise print a warning
533 * message because the server is probably not running.
535 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
536 catalog->db_password, catalog->db_address,
537 catalog->db_port, catalog->db_socket);
538 if (!db || !db_open_database(NULL, db)) {
539 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
542 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
546 /* If a pool is defined for this job, create the pool DB
547 * record if it is not already created.
550 create_pool(NULL, db, job->pool, POOL_OP_UPDATE); /* update request */
552 /* Set default value in all counters */
554 foreach_res(counter, R_COUNTER) {
555 /* Write to catalog? */
556 if (!counter->created && counter->Catalog == catalog) {
558 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
559 cr.MinValue = counter->MinValue;
560 cr.MaxValue = counter->MaxValue;
561 cr.CurrentValue = counter->MinValue;
562 if (counter->WrapCounter) {
563 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
565 cr.WrapCounter[0] = 0; /* empty string */
567 if (db_create_counter_record(NULL, db, &cr)) {
568 counter->CurrentValue = cr.CurrentValue;
569 counter->created = true;
570 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
573 if (!counter->created) {
574 counter->CurrentValue = counter->MinValue; /* default value */
578 db_close_database(NULL, db);
584 close_msg(NULL); /* close temp message handler */
585 init_msg(NULL, director->messages); /* open daemon message handler */