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, struct res_items *item, int index, int pass);
49 void store_level(LEX *lc, struct res_items *item, int index, int pass);
50 void store_replace(LEX *lc, struct res_items *item, int index, int pass);
52 static char *configfile = NULL;
53 static char *runjob = NULL;
54 static int background = 1;
56 /* Globals Exported */
57 DIRRES *director; /* Director resource */
61 /* Globals Imported */
62 extern int r_first, r_last; /* first and last resources */
63 extern struct res_items job_items[];
67 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
72 "\nVersion: " VERSION " (" BDATE ")\n\n"
73 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
74 " -c <file> set configuration file to file\n"
75 " -dnn set debug level to nn\n"
76 " -f run in foreground (for debugging)\n"
78 " -r <job> run <job> now\n"
80 " -t test - read configuration and exit\n"
82 " -v verbose user messages\n"
83 " -? print this message.\n"
90 /*********************************************************************
92 * Main Bacula Server program
95 int main (int argc, char *argv[])
99 int no_signals = FALSE;
100 int test_config = FALSE;
105 my_name_is(argc, argv, "bacula-dir");
106 textdomain("bacula-dir");
107 init_msg(NULL, NULL); /* initialize message handler */
108 daemon_start_time = time(NULL);
110 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
112 case 'c': /* specify config file */
113 if (configfile != NULL) {
116 configfile = bstrdup(optarg);
119 case 'd': /* set debug level */
120 debug_level = atoi(optarg);
121 if (debug_level <= 0) {
124 Dmsg1(0, "Debug level = %d\n", debug_level);
127 case 'f': /* run in foreground */
131 case 'g': /* set group id */
135 case 'r': /* run job */
136 if (runjob != NULL) {
140 runjob = bstrdup(optarg);
144 case 's': /* turn off signals */
148 case 't': /* test config */
152 case 'u': /* set uid */
156 case 'v': /* verbose */
170 init_signals(terminate_dird);
174 if (configfile != NULL) {
177 configfile = bstrdup(*argv);
185 if (configfile == NULL) {
186 configfile = bstrdup(CONFIG_FILE);
189 parse_config(configfile);
191 if (!check_resources()) {
192 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
199 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
201 FDConnectTimeout = (int)director->FDConnectTimeout;
202 SDConnectTimeout = (int)director->SDConnectTimeout;
206 init_stack_dump(); /* grab new pid */
209 /* Create pid must come after we are a daemon -- so we have our final pid */
210 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
211 read_state_file(director->working_directory, "bacula-dir", director->DIRport);
213 drop(uid, gid); /* reduce priveleges if requested */
215 signal(SIGHUP, reload_config);
217 init_console_msg(working_directory);
219 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
220 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
222 Dmsg0(200, "Start UA server\n");
223 start_UA_server(director->DIRaddr, director->DIRport);
225 start_watchdog(); /* start network watchdog thread */
227 init_jcr_subsystem(); /* start JCR watchdogs etc. */
229 init_job_server(director->MaxConcurrentJobs);
231 Dmsg0(200, "wait for next job\n");
232 /* Main loop -- call scheduler to get next job to run */
233 while ((jcr = wait_for_next_job(runjob))) {
234 run_job(jcr); /* run job */
235 free_jcr(jcr); /* release jcr */
236 if (runjob) { /* command line, run a single job? */
237 break; /* yes, terminate */
244 /* Cleanup and then exit */
245 static void terminate_dird(int sig)
247 static int already_here = FALSE;
249 if (already_here) { /* avoid recursive temination problems */
253 write_state_file(director->working_directory, "bacula-dir", director->DIRport);
254 delete_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
255 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
260 if (configfile != NULL) {
263 if (debug_level > 5) {
264 print_memory_pool_stats();
266 free_config_resources();
268 term_msg(); /* terminate message handler */
270 close_memory_pool(); /* release free memory in pool */
276 * If we get here, we have received a SIGHUP, which means to
277 * reread our configuration file.
279 * ***FIXME*** Check that there are no jobs running before
282 static void reload_config(int sig)
284 static bool already_here = false;
290 abort(); /* Oops, recursion -> die */
294 sigprocmask(SIG_BLOCK, &set, NULL);
300 if (jcr->JobId != 0) { /* this is a console */
303 free_locked_jcr(jcr);
309 free_config_resources();
311 parse_config(configfile);
313 if (!check_resources()) {
314 Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
318 set_working_directory(director->working_directory);
319 FDConnectTimeout = director->FDConnectTimeout;
320 SDConnectTimeout = director->SDConnectTimeout;
321 Dmsg0(0, "Director's configuration file reread.\n");
326 sigprocmask(SIG_UNBLOCK, &set, NULL);
327 signal(SIGHUP, reload_config);
328 already_here = false;
332 * Make a quick check to see that we have all the
335 * **** FIXME **** this routine could be a lot more
336 * intelligent and comprehensive.
338 static int check_resources()
345 job = (JOB *)GetNextRes(R_JOB, NULL);
346 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
348 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
349 Without that I don't know who I am :-(\n"), configfile);
352 set_working_directory(director->working_directory);
353 if (!director->messages) { /* If message resource not specified */
354 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
355 if (!director->messages) {
356 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
360 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
361 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
368 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
371 foreach_res(job, R_JOB) {
375 /* Transfer default items from JobDefs Resource */
376 for (i=0; job_items[i].name; i++) {
377 char **def_svalue, **svalue; /* string value */
378 int *def_ivalue, *ivalue; /* integer value */
379 int64_t *def_lvalue, *lvalue; /* 64 bit values */
382 Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
383 job->hdr.name, job_items[i].name,
384 bit_is_set(i, job->hdr.item_present),
385 bit_is_set(i, job->jobdefs->hdr.item_present));
387 if (!bit_is_set(i, job->hdr.item_present) &&
388 bit_is_set(i, job->jobdefs->hdr.item_present)) {
389 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
390 job->hdr.name, job_items[i].name);
391 offset = (char *)(job_items[i].value) - (char *)&res_all;
393 * Handle strings and directory strings
395 if (job_items[i].handler == store_str ||
396 job_items[i].handler == store_dir) {
397 def_svalue = (char **)((char *)(job->jobdefs) + offset);
398 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
399 job->hdr.name, job_items[i].name, *def_svalue, i, offset);
400 svalue = (char **)((char *)job + offset);
402 Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
404 *svalue = bstrdup(*def_svalue);
405 set_bit(i, job->hdr.item_present);
406 } else if (job_items[i].handler == store_res) {
407 def_svalue = (char **)((char *)(job->jobdefs) + offset);
408 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
409 job->hdr.name, job_items[i].name, i, offset);
410 svalue = (char **)((char *)job + offset);
412 Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
414 *svalue = *def_svalue;
415 set_bit(i, job->hdr.item_present);
417 * Handle integer fields
418 * Note, our store_yesno does not handle bitmaped fields
420 } else if (job_items[i].handler == store_yesno ||
421 job_items[i].handler == store_pint ||
422 job_items[i].handler == store_jobtype ||
423 job_items[i].handler == store_level ||
424 job_items[i].handler == store_pint ||
425 job_items[i].handler == store_replace) {
426 def_ivalue = (int *)((char *)(job->jobdefs) + offset);
427 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
428 job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
429 ivalue = (int *)((char *)job + offset);
430 *ivalue = *def_ivalue;
431 set_bit(i, job->hdr.item_present);
433 * Handle 64 bit integer fields
435 } else if (job_items[i].handler == store_time ||
436 job_items[i].handler == store_size ||
437 job_items[i].handler == store_int64) {
438 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
439 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
440 job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
441 lvalue = (int64_t *)((char *)job + offset);
442 *lvalue = *def_lvalue;
443 set_bit(i, job->hdr.item_present);
449 * Ensure that all required items are present
451 for (i=0; job_items[i].name; i++) {
452 if (job_items[i].flags & ITEM_REQUIRED) {
453 if (!bit_is_set(i, job->hdr.item_present)) {
454 Jmsg(NULL, M_FATAL, 0, "Field \"%s\" in Job \"%s\" resource is required, but not found.\n",
455 job_items[i].name, job->hdr.name);
459 /* If this triggers, take a look at lib/parse_conf.h */
460 if (i >= MAX_RES_ITEMS) {
461 Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
464 if (job->client && job->client->catalog) {
465 CAT *catalog = job->client->catalog;
469 * Make sure we can open catalog, otherwise print a warning
470 * message because the server is probably not running.
472 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
473 catalog->db_password, catalog->db_address,
474 catalog->db_port, catalog->db_socket);
475 if (!db || !db_open_database(NULL, db)) {
476 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
479 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
483 /* If a pool is defined for this job, create the pool DB
484 * record if it is not already created.
487 create_pool(NULL, db, job->pool, POOL_OP_UPDATE); /* update request */
489 /* Set default value in all counters */
491 foreach_res(counter, R_COUNTER) {
492 /* Write to catalog? */
493 if (!counter->created && counter->Catalog == catalog) {
495 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
496 cr.MinValue = counter->MinValue;
497 cr.MaxValue = counter->MaxValue;
498 cr.CurrentValue = counter->MinValue;
499 if (counter->WrapCounter) {
500 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
502 cr.WrapCounter[0] = 0; /* empty string */
504 if (db_create_counter_record(NULL, db, &cr)) {
505 counter->CurrentValue = cr.CurrentValue;
506 counter->created = true;
507 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
510 if (!counter->created) {
511 counter->CurrentValue = counter->MinValue; /* default value */
515 db_close_database(NULL, db);
521 close_msg(NULL); /* close temp message handler */
522 init_msg(NULL, director->messages); /* open daemon message handler */