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