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