3 * Bacula Director daemon -- this is the main program
5 * Kern Sibbald, March MM
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /* Forward referenced subroutines */
31 static void terminate_dird(int sig);
32 static int check_resources();
33 static void reload_config(int sig);
35 /* Exported subroutines */
38 /* Imported subroutines */
39 extern JCR *wait_for_next_job(char *runjob);
40 extern void term_scheduler();
41 extern void term_ua_server();
42 extern int do_backup(JCR *jcr);
43 extern void backup_cleanup(void);
44 extern void start_UA_server(int port);
45 extern void run_job(JCR *jcr);
46 extern void init_job_server(int max_workers);
48 static char *configfile = NULL;
49 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 " (" DATE ")\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"
68 " -r <job> run <job> now\n"
70 " -t test - read configuration and exit\n"
71 " -? print this message.\n"
78 /*********************************************************************
80 * Main Bacula Server program
83 int main (int argc, char *argv[])
87 int no_signals = FALSE;
88 int test_config = FALSE;
91 my_name_is(argc, argv, "bacula-dir");
92 daemon_start_time = time(NULL);
93 memset(&last_job, 0, sizeof(last_job));
95 while ((ch = getopt(argc, argv, "c:d:fr:st?")) != -1) {
97 case 'c': /* specify config file */
98 if (configfile != NULL) {
101 configfile = bstrdup(optarg);
104 case 'd': /* set debug level */
105 debug_level = atoi(optarg);
106 if (debug_level <= 0) {
109 Dmsg1(0, "Debug level = %d\n", debug_level);
112 case 'f': /* run in foreground */
116 case 'r': /* run job */
117 if (runjob != NULL) {
121 runjob = bstrdup(optarg);
125 case 's': /* turn off signals */
129 case 't': /* test config */
143 init_signals(terminate_dird);
145 signal(SIGCHLD, SIG_IGN);
148 if (configfile != NULL) {
151 configfile = bstrdup(*argv);
159 if (configfile == NULL) {
160 configfile = bstrdup(CONFIG_FILE);
163 init_msg(NULL); /* initialize message handler */
164 parse_config(configfile);
166 if (!check_resources()) {
167 Emsg1(M_ABORT, 0, "Please correct configuration file: %s\n", configfile);
170 my_name_is(0, (char **)NULL, director->hdr.name); /* set user defined name */
172 FDConnectTimeout = director->FDConnectTimeout;
173 SDConnectTimeout = director->SDConnectTimeout;
183 init_stack_dump(); /* grab new pid */
186 signal(SIGHUP, reload_config);
188 init_console_msg(working_directory);
190 Dmsg0(200, "Start UA server\n");
191 start_UA_server(director->DIRport);
193 init_watchdog(); /* start network watchdog thread */
195 init_job_server(director->MaxConcurrentJobs);
197 Dmsg0(200, "wait for next job\n");
198 /* Main loop -- call scheduler to get next job to run */
199 while ((jcr = wait_for_next_job(runjob))) {
200 run_job(jcr); /* run job */
201 if (runjob) /* command line, run a single job? */
202 break; /* yes, terminate */
206 exit(0); /* for compiler */
209 /* Cleanup and then exit */
210 static void terminate_dird(int sig)
212 static int already_here = FALSE;
214 if (already_here) { /* avoid recursive temination problems */
219 signal(SIGCHLD, SIG_IGN); /* don't worry about children now */
224 if (configfile != NULL) {
227 if (debug_level > 5) {
228 print_memory_pool_stats();
230 free_config_resources();
232 close_memory_pool(); /* free memory in pool */
233 term_msg(); /* terminate message handler */
239 * If we get here, we have received a SIGHUP, which means to
240 * reread our configuration file.
242 * ***FIXME*** Check that there are no jobs running before
245 static void reload_config(int sig)
247 static int already_here = FALSE;
251 abort(); /* Oops, recursion -> die */
255 sigprocmask(SIG_BLOCK, &set, NULL);
257 free_config_resources();
259 parse_config(configfile);
261 Dmsg0(200, "check_resources()\n");
262 if (!check_resources()) {
263 Emsg1(M_ABORT, 0, _("Please correct configuration file: %s\n"), configfile);
267 working_directory = director->working_directory;
268 FDConnectTimeout = director->FDConnectTimeout;
269 SDConnectTimeout = director->SDConnectTimeout;
271 sigprocmask(SIG_UNBLOCK, &set, NULL);
272 signal(SIGHUP, reload_config);
273 already_here = FALSE;
274 Dmsg0(0, "Director's configuration file reread.\n");
278 * Make a quick check to see that we have all the
281 * **** FIXME **** this routine could be a lot more
282 * intelligent and comprehensive.
284 static int check_resources()
291 job = (JOB *)GetNextRes(R_JOB, NULL);
292 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
294 Emsg1(M_WARNING, 0, _("No Director resource defined in %s\n\
295 Without that I don't know who I am :-(\n"), configfile);
298 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
299 Emsg1(M_WARNING, 0, _("Only one Director resource permitted in %s\n"),
303 if (!director->working_directory) {
304 Emsg0(M_WARNING, 0, _("No working directory specified. Cannot continue.\n"));
307 working_directory = director->working_directory;
309 Emsg1(M_WARNING, 0, _("No Job records defined in %s\n"), configfile);
312 for (job=NULL; (job = (JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
314 Emsg1(M_WARNING, 0, _("No Client record defined for job %s\n"), job->hdr.name);
318 Emsg1(M_WARNING, 0, _("No FileSet record defined for job %s\n"), job->hdr.name);
321 if (!job->storage && job->JobType != JT_VERIFY) {
322 Emsg1(M_WARNING, 0, _("No Storage resource defined for job %s\n"), job->hdr.name);
326 Emsg1(M_WARNING, 0, _("No Pool resource defined for job %s\n"), job->hdr.name);
329 if (job->client->catalog) {
330 CAT *catalog = job->client->catalog;
334 * Make sure we can open catalog, otherwise print a warning
335 * message because the server is probably not running.
337 db = db_init_database(catalog->db_name, catalog->db_user,
338 catalog->db_password);
339 if (!db_open_database(db)) {
340 Emsg1(M_WARNING, 0, "%s", db_strerror(db));
342 db_close_database(db);
344 Emsg1(M_WARNING, 0, _("No Catalog resource defined for client %s\n"),
345 job->client->hdr.name);