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