3 * Bacula Director daemon -- this is the main program
5 * Kern Sibbald, March MM
10 Copyright (C) 2000-2003 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 extern JCR *wait_for_next_job(char *runjob);
42 extern void term_scheduler();
43 extern void term_ua_server();
44 extern int do_backup(JCR *jcr);
45 extern void backup_cleanup(void);
46 extern void start_UA_server(char *addr, int port);
47 extern void init_job_server(int max_workers);
49 static char *configfile = NULL;
50 static char *runjob = NULL;
51 static int background = 1;
53 /* Globals Exported */
54 DIRRES *director; /* Director resource */
58 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
63 "\nVersion: " VERSION " (" BDATE ")\n\n"
64 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
65 " -c <file> set configuration file to file\n"
66 " -dnn set debug level to nn\n"
67 " -f run in foreground (for debugging)\n"
69 " -r <job> run <job> now\n"
71 " -t test - read configuration and exit\n"
73 " -v verbose user messages\n"
74 " -? print this message.\n"
81 /*********************************************************************
83 * Main Bacula Server program
86 int main (int argc, char *argv[])
90 int no_signals = FALSE;
91 int test_config = FALSE;
96 my_name_is(argc, argv, "bacula-dir");
97 textdomain("bacula-dir");
98 init_msg(NULL, NULL); /* initialize message handler */
99 daemon_start_time = time(NULL);
101 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
103 case 'c': /* specify config file */
104 if (configfile != NULL) {
107 configfile = bstrdup(optarg);
110 case 'd': /* set debug level */
111 debug_level = atoi(optarg);
112 if (debug_level <= 0) {
115 Dmsg1(0, "Debug level = %d\n", debug_level);
118 case 'f': /* run in foreground */
122 case 'g': /* set group id */
126 case 'r': /* run job */
127 if (runjob != NULL) {
131 runjob = bstrdup(optarg);
135 case 's': /* turn off signals */
139 case 't': /* test config */
143 case 'u': /* set uid */
147 case 'v': /* verbose */
161 init_signals(terminate_dird);
165 if (configfile != NULL) {
168 configfile = bstrdup(*argv);
176 if (configfile == NULL) {
177 configfile = bstrdup(CONFIG_FILE);
180 parse_config(configfile);
182 if (!check_resources()) {
183 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
190 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
192 FDConnectTimeout = (int)director->FDConnectTimeout;
193 SDConnectTimeout = (int)director->SDConnectTimeout;
197 init_stack_dump(); /* grab new pid */
200 drop(uid, gid); /* reduce priveleges if requested */
202 /* Create pid must come after we are a daemon -- so we have our final pid */
203 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
205 /* signal(SIGHUP, reload_config); */
207 init_console_msg(working_directory);
209 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
210 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
212 Dmsg0(200, "Start UA server\n");
213 start_UA_server(director->DIRaddr, director->DIRport);
215 start_watchdog(); /* start network watchdog thread */
217 init_job_server(director->MaxConcurrentJobs);
219 Dmsg0(200, "wait for next job\n");
220 /* Main loop -- call scheduler to get next job to run */
221 while ((jcr = wait_for_next_job(runjob))) {
222 run_job(jcr); /* run job */
223 if (runjob) { /* command line, run a single job? */
224 break; /* yes, terminate */
231 /* Cleanup and then exit */
232 static void terminate_dird(int sig)
234 static int already_here = FALSE;
236 if (already_here) { /* avoid recursive temination problems */
240 delete_pid_file(director->pid_directory, "bacula-dir",
243 // signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
248 if (configfile != NULL) {
251 if (debug_level > 5) {
252 print_memory_pool_stats();
254 free_config_resources();
256 term_msg(); /* terminate message handler */
257 close_memory_pool(); /* release free memory in pool */
263 * If we get here, we have received a SIGHUP, which means to
264 * reread our configuration file.
266 * ***FIXME*** Check that there are no jobs running before
269 static void reload_config(int sig)
271 static int already_here = FALSE;
275 abort(); /* Oops, recursion -> die */
279 sigprocmask(SIG_BLOCK, &set, NULL);
281 free_config_resources();
283 parse_config(configfile);
285 Dmsg0(200, "check_resources()\n");
286 if (!check_resources()) {
287 Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
291 set_working_directory(director->working_directory);
292 FDConnectTimeout = director->FDConnectTimeout;
293 SDConnectTimeout = director->SDConnectTimeout;
295 sigprocmask(SIG_UNBLOCK, &set, NULL);
296 signal(SIGHUP, reload_config);
297 already_here = FALSE;
298 Dmsg0(0, "Director's configuration file reread.\n");
302 * Make a quick check to see that we have all the
305 * **** FIXME **** this routine could be a lot more
306 * intelligent and comprehensive.
308 static int check_resources()
315 job = (JOB *)GetNextRes(R_JOB, NULL);
316 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
318 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
319 Without that I don't know who I am :-(\n"), configfile);
322 set_working_directory(director->working_directory);
323 if (!director->messages) { /* If message resource not specified */
324 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
325 if (!director->messages) {
326 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
330 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
331 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
338 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
341 for (job=NULL; (job = (JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
343 Jmsg(NULL, M_FATAL, 0, _("No Client record defined for job %s\n"), job->hdr.name);
347 Jmsg(NULL, M_FATAL, 0, _("No FileSet record defined for job %s\n"), job->hdr.name);
350 if (!job->storage && job->JobType != JT_VERIFY) {
351 Jmsg(NULL, M_FATAL, 0, _("No Storage resource defined for job %s\n"), job->hdr.name);
355 Jmsg(NULL, M_FATAL, 0, _("No Pool resource defined for job %s\n"), job->hdr.name);
358 if (job->client && job->client->catalog) {
359 CAT *catalog = job->client->catalog;
363 * Make sure we can open catalog, otherwise print a warning
364 * message because the server is probably not running.
366 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
367 catalog->db_password, catalog->db_address,
368 catalog->db_port, catalog->db_socket);
369 if (!db || !db_open_database(NULL, db)) {
370 Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
373 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
377 /* If a pool is defined for this job, create the pool DB
378 * record if it is not already created.
381 create_pool(NULL, db, job->pool, POOL_OP_UPDATE); /* update request */
383 /* Set default value in all counters */
384 for (COUNTER *counter=NULL; (counter = (COUNTER *)GetNextRes(R_COUNTER, (RES *)counter)); ) {
385 /* Write to catalog? */
386 if (!counter->created && counter->Catalog == catalog) {
388 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
389 cr.MinValue = counter->MinValue;
390 cr.MaxValue = counter->MaxValue;
391 cr.CurrentValue = counter->MinValue;
392 if (counter->WrapCounter) {
393 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
395 cr.WrapCounter[0] = 0; /* empty string */
397 if (db_create_counter_record(NULL, db, &cr)) {
398 counter->CurrentValue = cr.CurrentValue;
399 counter->created = true;
400 Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
403 if (!counter->created) {
404 counter->CurrentValue = counter->MinValue; /* default value */
407 db_close_database(NULL, db);
412 Jmsg(NULL, M_FATAL, 0, _("No Catalog resource defined for client %s\n"),
413 job->client->hdr.name);
421 close_msg(NULL); /* close temp message handler */
422 init_msg(NULL, director->messages); /* open daemon message handler */