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