]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird.c
Documentation, RecycleOldestVol, fix create_media bug
[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 "       -g          groupid\n"
70 "       -r <job>    run <job> now\n"
71 "       -s          no signals\n"
72 "       -t          test - read configuration and exit\n"
73 "       -u          userid\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    init_msg(NULL, NULL);              /* initialize message handler */
98    daemon_start_time = time(NULL);
99    memset(&last_job, 0, sizeof(last_job));
100
101    while ((ch = getopt(argc, argv, "c:d:fg:r:stu:?")) != -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 '?':
148          default:
149             usage();
150
151       }  
152    }
153    argc -= optind;
154    argv += optind;
155
156    if (!no_signals) {
157       init_signals(terminate_dird);
158    }
159    signal(SIGCHLD, SIG_IGN);
160
161    if (argc) {
162       if (configfile != NULL) {
163          free(configfile);
164       }
165       configfile = bstrdup(*argv);
166       argc--; 
167       argv++;
168    }
169    if (argc) {
170       usage();
171    }
172
173    if (configfile == NULL) {
174       configfile = bstrdup(CONFIG_FILE);
175    }
176
177    parse_config(configfile);
178
179    if (!check_resources()) {
180       Jmsg(NULL, M_ERROR_TERM, 0, "Please correct configuration file: %s\n", configfile);
181    }
182
183    if (test_config) {
184       terminate_dird(0);
185    }
186
187    my_name_is(0, NULL, director->hdr.name);    /* set user defined name */
188
189    FDConnectTimeout = (int)director->FDConnectTimeout;
190    SDConnectTimeout = (int)director->SDConnectTimeout;
191
192    if (background) {
193       daemon_start();
194       init_stack_dump();              /* grab new pid */
195    }
196
197    drop(uid, gid);                    /* reduce priveleges if requested */
198
199    /* Create pid must come after we are a daemon -- so we have our final pid */
200    create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
201
202 /* signal(SIGHUP, reload_config); */
203
204    init_console_msg(working_directory);
205
206    set_thread_concurrency(director->MaxConcurrentJobs * 2 +
207       4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
208
209    Dmsg0(200, "Start UA server\n");
210    start_UA_server(director->DIRaddr, director->DIRport);
211
212    start_watchdog();                  /* start network watchdog thread */
213
214    init_job_server(director->MaxConcurrentJobs);
215   
216    Dmsg0(200, "wait for next job\n");
217    /* Main loop -- call scheduler to get next job to run */
218    while ((jcr = wait_for_next_job(runjob))) {
219       run_job(jcr);                   /* run job */
220       if (runjob) {                   /* command line, run a single job? */
221          break;                       /* yes, terminate */
222       }
223    }
224
225    terminate_dird(0);
226 }
227
228 /* Cleanup and then exit */
229 static void terminate_dird(int sig)
230 {
231    static int already_here = FALSE;
232
233    if (already_here) {                /* avoid recursive temination problems */
234       exit(1);
235    }
236    already_here = TRUE;
237    delete_pid_file(director->pid_directory, "bacula-dir",  
238                    director->DIRport);
239    stop_watchdog();
240    signal(SIGCHLD, SIG_IGN);          /* don't worry about children now */
241    term_scheduler();
242    if (runjob) {
243       free(runjob);
244    }
245    if (configfile != NULL) {
246       free(configfile);
247    }
248    if (debug_level > 5) {
249       print_memory_pool_stats(); 
250    }
251    free_config_resources();
252    term_ua_server();
253    term_msg();                        /* terminate message handler */
254    close_memory_pool();               /* release free memory in pool */
255    sm_dump(False);
256    exit(sig != 0);
257 }
258
259 /*
260  * If we get here, we have received a SIGHUP, which means to
261  * reread our configuration file. 
262  *
263  *  ***FIXME***  Check that there are no jobs running before
264  *               doing this. 
265  */
266 static void reload_config(int sig)
267 {
268    static int already_here = FALSE;
269    sigset_t set;        
270
271    if (already_here) {
272       abort();                        /* Oops, recursion -> die */
273    }
274    already_here = TRUE;
275    sigfillset(&set);
276    sigprocmask(SIG_BLOCK, &set, NULL);
277
278    free_config_resources();
279
280    parse_config(configfile);
281
282    Dmsg0(200, "check_resources()\n");
283    if (!check_resources()) {
284       Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
285    }
286
287    /* Reset globals */
288    working_directory = director->working_directory;
289    FDConnectTimeout = director->FDConnectTimeout;
290    SDConnectTimeout = director->SDConnectTimeout;
291  
292    sigprocmask(SIG_UNBLOCK, &set, NULL);
293    signal(SIGHUP, reload_config);
294    already_here = FALSE;
295    Dmsg0(0, "Director's configuration file reread.\n");
296 }
297
298 /*
299  * Make a quick check to see that we have all the
300  * resources needed.
301  *
302  *  **** FIXME **** this routine could be a lot more
303  *   intelligent and comprehensive.
304  */
305 static int check_resources()
306 {
307    int OK = TRUE;
308    JOB *job;
309
310    LockRes();
311
312    job  = (JOB *)GetNextRes(R_JOB, NULL);
313    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
314    if (!director) {
315       Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
316 Without that I don't know who I am :-(\n"), configfile);
317       OK = FALSE;
318    } else {
319       if (!director->working_directory) {
320          Jmsg(NULL, M_FATAL, 0, _("No working directory specified. Cannot continue.\n"));
321          OK = FALSE;
322       }       
323       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_open_database(NULL, db)) {
371             Jmsg(NULL, M_FATAL,  0, "%s", db_strerror(db));
372          } else {
373             /* If a pool is defined for this job, create the pool DB       
374              *  record if it is not already created. 
375              */
376             if (job->pool) {
377                create_pool(NULL, db, job->pool, 1);
378             }
379             db_close_database(NULL, db);
380          }
381
382       } else {
383          if (job->client) {
384             Jmsg(NULL, M_FATAL, 0, _("No Catalog resource defined for client %s\n"), 
385                job->client->hdr.name);
386             OK = FALSE;
387          }
388       }
389    }
390
391    UnlockRes();
392    if (OK) {
393       close_msg(NULL);                /* close temp message handler */
394       init_msg(NULL, director->messages); /* open daemon message handler */
395    }
396    return OK;
397 }