]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird.c
Init messages fix
[bacula/bacula] / bacula / src / dird / dird.c
1 /*
2  *
3  *   Bacula Director daemon -- this is the main program
4  *
5  *     Kern Sibbald, March MM
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void terminate_dird(int sig);
34 static int check_resources();
35 static void reload_config(int sig);
36
37 /* Exported subroutines */
38
39
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(int port);
47 extern void run_job(JCR *jcr);
48 extern void init_job_server(int max_workers);
49
50 static char *configfile = NULL;
51 static char *runjob = NULL;
52
53 static int background = 1;
54
55 /* Globals Exported */
56 DIRRES *director;                     /* Director resource */
57 int FDConnectTimeout;
58 int SDConnectTimeout;
59
60 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
61
62 static void usage()
63 {
64    fprintf(stderr, _(
65 "\nVersion: " VERSION " (" DATE ")\n\n"
66 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
67 "       -c <file>   set configuration file to file\n"
68 "       -dnn        set debug level to nn\n"
69 "       -f          run in foreground (for debugging)\n"
70 "       -r <job>    run <job> now\n"
71 "       -s          no signals\n"
72 "       -t          test - read configuration and exit\n"
73 "       -?          print this message.\n"  
74 "\n"));
75
76    exit(1);
77 }
78
79
80 /*********************************************************************
81  *
82  *         Main Bacula Server program
83  *
84  */
85 int main (int argc, char *argv[])
86 {
87    int ch;
88    JCR *jcr;
89    int no_signals = FALSE;
90    int test_config = FALSE;
91
92    init_stack_dump();
93    my_name_is(argc, argv, "bacula-dir");
94    init_msg(NULL, NULL);              /* initialize message handler */
95    daemon_start_time = time(NULL);
96    memset(&last_job, 0, sizeof(last_job));
97
98    while ((ch = getopt(argc, argv, "c:d:fr:st?")) != -1) {
99       switch (ch) {
100          case 'c':                    /* specify config file */
101             if (configfile != NULL) {
102                free(configfile);
103             }
104             configfile = bstrdup(optarg);
105             break;
106
107          case 'd':                    /* set debug level */
108             debug_level = atoi(optarg);
109             if (debug_level <= 0) {
110                debug_level = 1; 
111             }
112             Dmsg1(0, "Debug level = %d\n", debug_level);
113             break;
114
115          case 'f':                    /* run in foreground */
116             background = FALSE;
117             break;
118
119          case 'r':                    /* run job */
120             if (runjob != NULL) {
121                free(runjob);
122             }
123             if (optarg) {
124                runjob = bstrdup(optarg);
125             }
126             break;
127
128          case 's':                    /* turn off signals */
129             no_signals = TRUE;
130             break;
131
132          case 't':                    /* test config */
133             test_config = TRUE;
134             break;
135
136          case '?':
137          default:
138             usage();
139
140       }  
141    }
142    argc -= optind;
143    argv += optind;
144
145    if (!no_signals) {
146       init_signals(terminate_dird);
147    }
148    signal(SIGCHLD, SIG_IGN);
149
150    if (argc) {
151       if (configfile != NULL) {
152          free(configfile);
153       }
154       configfile = bstrdup(*argv);
155       argc--; 
156       argv++;
157    }
158    if (argc) {
159       usage();
160    }
161
162    if (configfile == NULL) {
163       configfile = bstrdup(CONFIG_FILE);
164    }
165
166    parse_config(configfile);
167
168    if (!check_resources()) {
169       Emsg1(M_ABORT, 0, "Please correct configuration file: %s\n", configfile);
170    }
171
172    if (test_config) {
173       terminate_dird(0);
174    }
175
176    my_name_is(0, NULL, director->hdr.name);    /* set user defined name */
177
178    FDConnectTimeout = director->FDConnectTimeout;
179    SDConnectTimeout = director->SDConnectTimeout;
180
181    if (background) {
182       daemon_start();
183       init_stack_dump();              /* grab new pid */
184    }
185
186    /* Create pid must come after we are a daemon -- so we have our final pid */
187    create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
188
189 /* signal(SIGHUP, reload_config); */
190
191    init_console_msg(working_directory);
192
193    Dmsg0(200, "Start UA server\n");
194    start_UA_server(director->DIRport);
195
196    start_watchdog();                  /* start network watchdog thread */
197
198    init_job_server(director->MaxConcurrentJobs);
199   
200    Dmsg0(200, "wait for next job\n");
201    /* Main loop -- call scheduler to get next job to run */
202    while ((jcr = wait_for_next_job(runjob))) {
203       run_job(jcr);                   /* run job */
204       if (runjob) {                   /* command line, run a single job? */
205          break;                       /* yes, terminate */
206       }
207    }
208
209    terminate_dird(0);
210 }
211
212 /* Cleanup and then exit */
213 static void terminate_dird(int sig)
214 {
215    static int already_here = FALSE;
216
217    if (already_here) {                /* avoid recursive temination problems */
218       exit(1);
219    }
220    already_here = TRUE;
221    delete_pid_file(director->pid_directory, "bacula-dir",  
222                    director->DIRport);
223    stop_watchdog();
224    signal(SIGCHLD, SIG_IGN);          /* don't worry about children now */
225    term_scheduler();
226    if (runjob) {
227       free(runjob);
228    }
229    if (configfile != NULL) {
230       free(configfile);
231    }
232    if (debug_level > 5) {
233       print_memory_pool_stats(); 
234    }
235    free_config_resources();
236    term_ua_server();
237    term_msg();                        /* terminate message handler */
238    close_memory_pool();               /* release free memory in pool */
239    sm_dump(False);
240    exit(sig != 0);
241 }
242
243 /*
244  * If we get here, we have received a SIGHUP, which means to
245  * reread our configuration file. 
246  *
247  *  ***FIXME***  Check that there are no jobs running before
248  *               doing this. 
249  */
250 static void reload_config(int sig)
251 {
252    static int already_here = FALSE;
253    sigset_t set;        
254
255    if (already_here) {
256       abort();                        /* Oops, recursion -> die */
257    }
258    already_here = TRUE;
259    sigfillset(&set);
260    sigprocmask(SIG_BLOCK, &set, NULL);
261
262    free_config_resources();
263
264    parse_config(configfile);
265
266    Dmsg0(200, "check_resources()\n");
267    if (!check_resources()) {
268       Emsg1(M_ABORT, 0, _("Please correct configuration file: %s\n"), configfile);
269    }
270
271    /* Reset globals */
272    working_directory = director->working_directory;
273    FDConnectTimeout = director->FDConnectTimeout;
274    SDConnectTimeout = director->SDConnectTimeout;
275  
276    sigprocmask(SIG_UNBLOCK, &set, NULL);
277    signal(SIGHUP, reload_config);
278    already_here = FALSE;
279    Dmsg0(0, "Director's configuration file reread.\n");
280 }
281
282 /*
283  * Make a quick check to see that we have all the
284  * resources needed.
285  *
286  *  **** FIXME **** this routine could be a lot more
287  *   intelligent and comprehensive.
288  */
289 static int check_resources()
290 {
291    int OK = TRUE;
292    JOB *job;
293
294    LockRes();
295
296    job  = (JOB *)GetNextRes(R_JOB, NULL);
297    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
298    if (!director) {
299       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n\
300 Without that I don't know who I am :-(\n"), configfile);
301       OK = FALSE;
302    } else {
303       if (!director->working_directory) {
304          Emsg0(M_FATAL, 0, _("No working directory specified. Cannot continue.\n"));
305          OK = FALSE;
306       }       
307       working_directory = director->working_directory;
308       if (!director->messages) {       /* If message resource not specified */
309          director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
310          if (!director->messages) {
311             Emsg1(M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
312             OK = FALSE;
313          }
314       }
315       if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
316          Emsg1(M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
317             configfile);
318          OK = FALSE;
319       } 
320    }
321
322    if (!job) {
323       Emsg1(M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
324       OK = FALSE;
325    }
326    for (job=NULL; (job = (JOB *)GetNextRes(R_JOB, (RES *)job)); ) {
327       if (!job->client) {
328          Emsg1(M_FATAL, 0, _("No Client record defined for job %s\n"), job->hdr.name);
329          OK = FALSE;
330       }
331       if (!job->fs) {
332          Emsg1(M_FATAL, 0, _("No FileSet record defined for job %s\n"), job->hdr.name);
333          OK = FALSE;
334       }
335       if (!job->storage && job->JobType != JT_VERIFY) {
336          Emsg1(M_FATAL, 0, _("No Storage resource defined for job %s\n"), job->hdr.name);
337          OK = FALSE;
338       }
339       if (!job->pool) {
340          Emsg1(M_FATAL, 0, _("No Pool resource defined for job %s\n"), job->hdr.name);
341          OK = FALSE;
342       }
343       if (job->client->catalog) {
344          CAT *catalog = job->client->catalog;
345          B_DB *db;
346
347          /*
348           * Make sure we can open catalog, otherwise print a warning
349           * message because the server is probably not running.
350           */
351          db = db_init_database(catalog->db_name, catalog->db_user,
352                             catalog->db_password);
353          if (!db_open_database(db)) {
354             Emsg1(M_FATAL,  0, "%s", db_strerror(db));
355          }
356          db_close_database(db);
357       } else {
358          Emsg1(M_FATAL, 0, _("No Catalog resource defined for client %s\n"), 
359                job->client->hdr.name);
360          OK = FALSE;
361       }
362    }
363
364    UnlockRes();
365    if (OK) {
366       close_msg(NULL);                /* close temp message handler */
367       init_msg(NULL, director->messages); /* open daemon message handler */
368    }
369    return OK;
370 }