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 run_job(JCR *jcr);
48 extern void init_job_server(int max_workers);
50 static char *configfile = NULL;
51 static char *runjob = NULL;
52 static int background = 1;
54 /* Globals Exported */
55 DIRRES *director; /* Director resource */
59 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
64 "\nVersion: " VERSION " (" BDATE ")\n\n"
65 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
66 " -c <file> set configuration file to file\n"
67 " -dnn set debug level to nn\n"
68 " -f run in foreground (for debugging)\n"
70 " -r <job> run <job> now\n"
72 " -t test - read configuration and exit\n"
74 " -v verbose user messages\n"
75 " -? print this message.\n"
82 /*********************************************************************
84 * Main Bacula Server program
87 int main (int argc, char *argv[])
91 int no_signals = FALSE;
92 int test_config = FALSE;
97 my_name_is(argc, argv, "bacula-dir");
98 init_msg(NULL, NULL); /* initialize message handler */
99 daemon_start_time = time(NULL);
100 memset(&last_job, 0, sizeof(last_job));
102 while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
104 case 'c': /* specify config file */
105 if (configfile != NULL) {
108 configfile = bstrdup(optarg);
111 case 'd': /* set debug level */
112 debug_level = atoi(optarg);
113 if (debug_level <= 0) {
116 Dmsg1(0, "Debug level = %d\n", debug_level);
119 case 'f': /* run in foreground */
123 case 'g': /* set group id */
127 case 'r': /* run job */
128 if (runjob != NULL) {
132 runjob = bstrdup(optarg);
136 case 's': /* turn off signals */
140 case 't': /* test config */
144 case 'u': /* set uid */
148 case 'v': /* verbose */
162 init_signals(terminate_dird);
164 signal(SIGCHLD, SIG_IGN);
167 if (configfile != NULL) {
170 configfile = bstrdup(*argv);
178 if (configfile == NULL) {
179 configfile = bstrdup(CONFIG_FILE);
182 parse_config(configfile);
184 if (!check_resources()) {
185 Jmsg(NULL, M_ERROR_TERM, 0, "Please correct configuration file: %s\n", configfile);
192 my_name_is(0, NULL, director->hdr.name); /* set user defined name */
194 FDConnectTimeout = (int)director->FDConnectTimeout;
195 SDConnectTimeout = (int)director->SDConnectTimeout;
199 init_stack_dump(); /* grab new pid */
202 drop(uid, gid); /* reduce priveleges if requested */
204 /* Create pid must come after we are a daemon -- so we have our final pid */
205 create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
207 /* signal(SIGHUP, reload_config); */
209 init_console_msg(working_directory);
211 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
212 4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
214 Dmsg0(200, "Start UA server\n");
215 start_UA_server(director->DIRaddr, director->DIRport);
217 start_watchdog(); /* start network watchdog thread */
219 init_job_server(director->MaxConcurrentJobs);
221 Dmsg0(200, "wait for next job\n");
222 /* Main loop -- call scheduler to get next job to run */
223 while ((jcr = wait_for_next_job(runjob))) {
224 run_job(jcr); /* run job */
225 if (runjob) { /* command line, run a single job? */
226 break; /* yes, terminate */
233 /* Cleanup and then exit */
234 static void terminate_dird(int sig)
236 static int already_here = FALSE;
238 if (already_here) { /* avoid recursive temination problems */
242 delete_pid_file(director->pid_directory, "bacula-dir",
245 signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
250 if (configfile != NULL) {
253 if (debug_level > 5) {
254 print_memory_pool_stats();
256 free_config_resources();
258 term_msg(); /* terminate message handler */
259 close_memory_pool(); /* release free memory in pool */
265 * If we get here, we have received a SIGHUP, which means to
266 * reread our configuration file.
268 * ***FIXME*** Check that there are no jobs running before
271 static void reload_config(int sig)
273 static int already_here = FALSE;
277 abort(); /* Oops, recursion -> die */
281 sigprocmask(SIG_BLOCK, &set, NULL);
283 free_config_resources();
285 parse_config(configfile);
287 Dmsg0(200, "check_resources()\n");
288 if (!check_resources()) {
289 Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
293 working_directory = director->working_directory;
294 FDConnectTimeout = director->FDConnectTimeout;
295 SDConnectTimeout = director->SDConnectTimeout;
297 sigprocmask(SIG_UNBLOCK, &set, NULL);
298 signal(SIGHUP, reload_config);
299 already_here = FALSE;
300 Dmsg0(0, "Director's configuration file reread.\n");
304 * Make a quick check to see that we have all the
307 * **** FIXME **** this routine could be a lot more
308 * intelligent and comprehensive.
310 static int check_resources()
317 job = (JOB *)GetNextRes(R_JOB, NULL);
318 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
320 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
321 Without that I don't know who I am :-(\n"), configfile);
324 if (!director->working_directory) {
325 Jmsg(NULL, M_FATAL, 0, _("No working directory specified. Cannot continue.\n"));
328 working_directory = director->working_directory;
329 if (!director->messages) { /* If message resource not specified */
330 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
331 if (!director->messages) {
332 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
336 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
337 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
344 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
347 for (job=NULL; (job = (JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
349 Jmsg(NULL, M_FATAL, 0, _("No Client record defined for job %s\n"), job->hdr.name);
353 Jmsg(NULL, M_FATAL, 0, _("No FileSet record defined for job %s\n"), job->hdr.name);
356 if (!job->storage && job->JobType != JT_VERIFY) {
357 Jmsg(NULL, M_FATAL, 0, _("No Storage resource defined for job %s\n"), job->hdr.name);
361 Jmsg(NULL, M_FATAL, 0, _("No Pool resource defined for job %s\n"), job->hdr.name);
364 if (job->client && job->client->catalog) {
365 CAT *catalog = job->client->catalog;
369 * Make sure we can open catalog, otherwise print a warning
370 * message because the server is probably not running.
372 db = db_init_database(NULL, catalog->db_name, catalog->db_user,
373 catalog->db_password, catalog->db_address,
374 catalog->db_port, catalog->db_socket);
375 if (!db_open_database(NULL, db)) {
376 Jmsg(NULL, M_FATAL, 0, "%s", db_strerror(db));
378 /* If a pool is defined for this job, create the pool DB
379 * record if it is not already created.
382 create_pool(NULL, db, job->pool, 0); /* update request */
384 db_close_database(NULL, db);
389 Jmsg(NULL, M_FATAL, 0, _("No Catalog resource defined for client %s\n"),
390 job->client->hdr.name);
398 close_msg(NULL); /* close temp message handler */
399 init_msg(NULL, director->messages); /* open daemon message handler */