]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird.c
First cut SIGHUP while jobs running + apply Phil's Gnome 1.4 patch + major update...
[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-2004 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 JCR *wait_for_next_job(char *runjob);
42 void term_scheduler();
43 void term_ua_server();
44 int do_backup(JCR *jcr);
45 void backup_cleanup(void);
46 void start_UA_server(char *addr, int port);
47 void init_job_server(int max_workers);
48 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
49 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
50 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
51
52 static char *configfile = NULL;
53 static char *runjob = NULL;
54 static int background = 1;
55 static void init_reload(void);
56
57 /* Globals Exported */
58 DIRRES *director;                     /* Director resource */
59 int FDConnectTimeout;
60 int SDConnectTimeout;
61
62 /* Globals Imported */
63 extern int r_first, r_last;           /* first and last resources */
64 extern RES_ITEM job_items[];
65 extern URES res_all;
66
67
68 #define CONFIG_FILE "./bacula-dir.conf" /* default configuration file */
69
70 static void usage()
71 {
72    fprintf(stderr, _(
73 "\nVersion: " VERSION " (" BDATE ")\n\n"
74 "Usage: dird [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
75 "       -c <file>   set configuration file to file\n"
76 "       -dnn        set debug level to nn\n"
77 "       -f          run in foreground (for debugging)\n"
78 "       -g          groupid\n"
79 "       -r <job>    run <job> now\n"
80 "       -s          no signals\n"
81 "       -t          test - read configuration and exit\n"
82 "       -u          userid\n"
83 "       -v          verbose user messages\n"
84 "       -?          print this message.\n"  
85 "\n"));
86
87    exit(1);
88 }
89
90
91 /*********************************************************************
92  *
93  *         Main Bacula Server program
94  *
95  */
96 int main (int argc, char *argv[])
97 {
98    int ch;
99    JCR *jcr;
100    int no_signals = FALSE;
101    int test_config = FALSE;
102    char *uid = NULL;
103    char *gid = NULL;
104
105    init_stack_dump();
106    my_name_is(argc, argv, "bacula-dir");
107    textdomain("bacula-dir");
108    init_msg(NULL, NULL);              /* initialize message handler */
109    init_reload();
110    daemon_start_time = time(NULL);
111
112    while ((ch = getopt(argc, argv, "c:d:fg:r:stu:v?")) != -1) {
113       switch (ch) {
114       case 'c':                    /* specify config file */
115          if (configfile != NULL) {
116             free(configfile);
117          }
118          configfile = bstrdup(optarg);
119          break;
120
121       case 'd':                    /* set debug level */
122          debug_level = atoi(optarg);
123          if (debug_level <= 0) {
124             debug_level = 1; 
125          }
126          Dmsg1(0, "Debug level = %d\n", debug_level);
127          break;
128
129       case 'f':                    /* run in foreground */
130          background = FALSE;
131          break;
132
133       case 'g':                    /* set group id */
134          gid = optarg;
135          break;
136
137       case 'r':                    /* run job */
138          if (runjob != NULL) {
139             free(runjob);
140          }
141          if (optarg) {
142             runjob = bstrdup(optarg);
143          }
144          break;
145
146       case 's':                    /* turn off signals */
147          no_signals = TRUE;
148          break;
149
150       case 't':                    /* test config */
151          test_config = TRUE;
152          break;
153
154       case 'u':                    /* set uid */
155          uid = optarg;
156          break;
157
158       case 'v':                    /* verbose */
159          verbose++;
160          break;
161
162       case '?':
163       default:
164          usage();
165
166       }  
167    }
168    argc -= optind;
169    argv += optind;
170
171    if (!no_signals) {
172       init_signals(terminate_dird);
173    }
174
175    if (argc) {
176       if (configfile != NULL) {
177          free(configfile);
178       }
179       configfile = bstrdup(*argv);
180       argc--; 
181       argv++;
182    }
183    if (argc) {
184       usage();
185    }
186
187    if (configfile == NULL) {
188       configfile = bstrdup(CONFIG_FILE);
189    }
190
191    parse_config(configfile);
192
193    if (!check_resources()) {
194       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
195    }
196
197    if (test_config) {
198       terminate_dird(0);
199    }
200
201    my_name_is(0, NULL, director->hdr.name);    /* set user defined name */
202
203    FDConnectTimeout = (int)director->FDConnectTimeout;
204    SDConnectTimeout = (int)director->SDConnectTimeout;
205
206    if (background) {
207       daemon_start();
208       init_stack_dump();              /* grab new pid */
209    }
210
211    /* Create pid must come after we are a daemon -- so we have our final pid */
212    create_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
213    read_state_file(director->working_directory, "bacula-dir", director->DIRport);
214
215    drop(uid, gid);                    /* reduce priveleges if requested */
216
217    signal(SIGHUP, reload_config);
218
219    init_console_msg(working_directory);
220
221    set_thread_concurrency(director->MaxConcurrentJobs * 2 +
222       4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
223
224    Dmsg0(200, "Start UA server\n");
225    start_UA_server(director->DIRaddr, director->DIRport);
226
227    start_watchdog();                  /* start network watchdog thread */
228
229    init_jcr_subsystem();              /* start JCR watchdogs etc. */
230
231    init_job_server(director->MaxConcurrentJobs);
232   
233    Dmsg0(200, "wait for next job\n");
234    /* Main loop -- call scheduler to get next job to run */
235    while ((jcr = wait_for_next_job(runjob))) {
236       run_job(jcr);                   /* run job */
237       free_jcr(jcr);                  /* release jcr */
238       if (runjob) {                   /* command line, run a single job? */
239          break;                       /* yes, terminate */
240       }
241    }
242
243    terminate_dird(0);
244 }
245
246 /* Cleanup and then exit */
247 static void terminate_dird(int sig)
248 {
249    static int already_here = FALSE;
250
251    if (already_here) {                /* avoid recursive temination problems */
252       exit(1);
253    }
254    already_here = TRUE;
255    write_state_file(director->working_directory, "bacula-dir", director->DIRport);
256    delete_pid_file(director->pid_directory, "bacula-dir", director->DIRport);
257 // signal(SIGCHLD, SIG_IGN);          /* don't worry about children now */
258    term_scheduler();
259    if (runjob) {
260       free(runjob);
261    }
262    if (configfile != NULL) {
263       free(configfile);
264    }
265    if (debug_level > 5) {
266       print_memory_pool_stats(); 
267    }
268    free_config_resources();
269    term_ua_server();
270    term_msg();                        /* terminate message handler */
271    stop_watchdog();
272    close_memory_pool();               /* release free memory in pool */
273    sm_dump(False);
274    exit(sig);
275 }
276
277 struct RELOAD_TABLE {
278    int job_count;
279    RES **res_table;
280 };
281
282 static const int max_reloads = 10;
283 static RELOAD_TABLE reload_table[max_reloads];
284
285 static void init_reload(void) 
286 {
287    for (int i=0; i < max_reloads; i++) {
288       reload_table[i].job_count = 0;
289       reload_table[i].res_table = NULL;
290    }
291 }
292
293 /*
294  * Called here at the end of every job that was
295  * hooked decrementing the active job_count. When
296  * it goes to zero, no one is using the associated
297  * resource table, so free it.
298  */
299 static void reload_job_end_cb(JCR *jcr)
300 {
301    int i = jcr->reload_id - 1;
302    RES **res_tab;
303    Dmsg1(000, "reload job_end JobId=%d\n", jcr->JobId);
304    if (--reload_table[i].job_count <= 0) {
305       int num = r_last - r_first + 1;
306       res_tab = reload_table[i].res_table;
307       Dmsg0(000, "Freeing resources\n");
308       for (int j=0; j<num; j++) {
309          free_resource(res_tab[j], r_first + j);
310       }
311       free(res_tab);
312       reload_table[i].job_count = 0;
313       reload_table[i].res_table = NULL;
314    }
315 }
316
317 /*
318  * If we get here, we have received a SIGHUP, which means to
319  *    reread our configuration file. 
320  */
321 static void reload_config(int sig)
322 {
323    static bool already_here = false;
324    sigset_t set;        
325    JCR *jcr;
326    int njobs = 0;
327    int table = -1;
328
329    if (already_here) {
330       abort();                        /* Oops, recursion -> die */
331    }
332    already_here = true;
333    sigfillset(&set);
334    sigprocmask(SIG_BLOCK, &set, NULL);
335
336    lock_jcr_chain();
337    LockRes();
338
339    for (int i=0; i < max_reloads; i++) {
340       if (reload_table[i].res_table == NULL) {
341          table = i;
342          break;
343       }
344    }
345    if (table < 0) {
346       Jmsg(NULL, M_ERROR, 0, _("Too many reload requests.\n"));
347       goto bail_out;
348    }
349
350    /*
351     * Hook all active jobs that are not already hooked (i.e.
352     *  reload_id == 0
353     */
354    foreach_jcr(jcr) {
355       /* JobId==0 => console */
356       if (jcr->JobId != 0 && jcr->reload_id == 0) {
357          reload_table[table].job_count++;
358          jcr->reload_id = table + 1;
359          job_end_push(jcr, reload_job_end_cb);
360          njobs++;
361       }
362       free_locked_jcr(jcr);
363    }
364    Dmsg1(000, "Reload_config njobs=%d\n", njobs);
365    if (njobs > 0) {
366       reload_table[table].res_table = save_config_resources();
367       Dmsg1(000, "Saved old config in table %d\n", table);
368    } else {
369       free_config_resources();
370    }
371
372    Dmsg0(000, "Calling parse config\n");
373    parse_config(configfile);
374
375    Dmsg0(000, "Reloaded config file\n");
376    if (!check_resources()) {
377       Jmsg(NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
378    }
379
380    /* Reset globals */
381    set_working_directory(director->working_directory);
382    FDConnectTimeout = director->FDConnectTimeout;
383    SDConnectTimeout = director->SDConnectTimeout;
384    Dmsg0(0, "Director's configuration file reread.\n");
385
386 bail_out:
387    UnlockRes();
388    unlock_jcr_chain();
389    sigprocmask(SIG_UNBLOCK, &set, NULL);
390    signal(SIGHUP, reload_config);
391    already_here = false;
392 }
393
394 /*
395  * Make a quick check to see that we have all the
396  * resources needed.
397  *
398  *  **** FIXME **** this routine could be a lot more
399  *   intelligent and comprehensive.
400  */
401 static int check_resources()
402 {
403    bool OK = true;
404    JOB *job;
405
406    LockRes();
407
408    job = (JOB *)GetNextRes(R_JOB, NULL);
409    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
410    if (!director) {
411       Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n\
412 Without that I don't know who I am :-(\n"), configfile);
413       OK = false;
414    } else {
415       set_working_directory(director->working_directory);
416       if (!director->messages) {       /* If message resource not specified */
417          director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
418          if (!director->messages) {
419             Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
420             OK = false;
421          }
422       }
423       if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
424          Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
425             configfile);
426          OK = false;
427       } 
428    }
429
430    if (!job) {
431       Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
432       OK = false;
433    }
434    foreach_res(job, R_JOB) {
435       int i;
436
437       if (job->jobdefs) {
438          /* Transfer default items from JobDefs Resource */
439          for (i=0; job_items[i].name; i++) {
440             char **def_svalue, **svalue;  /* string value */
441             int *def_ivalue, *ivalue;     /* integer value */
442             int64_t *def_lvalue, *lvalue; /* 64 bit values */
443             uint32_t offset;
444
445             Dmsg4(400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
446                 job->hdr.name, job_items[i].name, 
447                 bit_is_set(i, job->hdr.item_present),  
448                 bit_is_set(i, job->jobdefs->hdr.item_present));
449
450             if (!bit_is_set(i, job->hdr.item_present) &&
451                  bit_is_set(i, job->jobdefs->hdr.item_present)) { 
452                Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
453                  job->hdr.name, job_items[i].name);
454                offset = (char *)(job_items[i].value) - (char *)&res_all;   
455                /*
456                 * Handle strings and directory strings
457                 */
458                if (job_items[i].handler == store_str ||
459                    job_items[i].handler == store_dir) {
460                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
461                   Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n", 
462                        job->hdr.name, job_items[i].name, *def_svalue, i, offset);
463                   svalue = (char **)((char *)job + offset);
464                   if (*svalue) {
465                      Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
466                   }
467                   *svalue = bstrdup(*def_svalue);
468                   set_bit(i, job->hdr.item_present);
469                } else if (job_items[i].handler == store_res) {
470                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
471                   Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n", 
472                        job->hdr.name, job_items[i].name, i, offset);
473                   svalue = (char **)((char *)job + offset);
474                   if (*svalue) {
475                      Dmsg1(000, "Hey something is wrong. p=0x%u\n", (unsigned)*svalue);
476                   }
477                   *svalue = *def_svalue;
478                   set_bit(i, job->hdr.item_present);
479                /*
480                 * Handle integer fields 
481                 *    Note, our store_yesno does not handle bitmaped fields
482                 */
483                } else if (job_items[i].handler == store_yesno   ||
484                           job_items[i].handler == store_pint    ||
485                           job_items[i].handler == store_jobtype ||
486                           job_items[i].handler == store_level   ||
487                           job_items[i].handler == store_pint    ||
488                           job_items[i].handler == store_replace) {
489                   def_ivalue = (int *)((char *)(job->jobdefs) + offset);
490                   Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n", 
491                        job->hdr.name, job_items[i].name, *def_ivalue, i, offset);
492                   ivalue = (int *)((char *)job + offset);
493                   *ivalue = *def_ivalue;
494                   set_bit(i, job->hdr.item_present);
495                /*
496                 * Handle 64 bit integer fields 
497                 */
498                } else if (job_items[i].handler == store_time   ||
499                           job_items[i].handler == store_size   ||
500                           job_items[i].handler == store_int64) {
501                   def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
502                   Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n", 
503                        job->hdr.name, job_items[i].name, *def_lvalue, i, offset);
504                   lvalue = (int64_t *)((char *)job + offset);
505                   *lvalue = *def_lvalue;
506                   set_bit(i, job->hdr.item_present);
507                }
508             }
509          }
510       }
511       /* 
512        * Ensure that all required items are present
513        */
514       for (i=0; job_items[i].name; i++) {
515          if (job_items[i].flags & ITEM_REQUIRED) {
516                if (!bit_is_set(i, job->hdr.item_present)) {  
517                   Jmsg(NULL, M_FATAL, 0, "Field \"%s\" in Job \"%s\" resource is required, but not found.\n",
518                     job_items[i].name, job->hdr.name);
519                   OK = false;
520                 }
521          }
522          /* If this triggers, take a look at lib/parse_conf.h */
523          if (i >= MAX_RES_ITEMS) {
524             Emsg0(M_ERROR_TERM, 0, "Too many items in Job resource\n");
525          }
526       }
527       if (job->client && job->client->catalog) {
528          CAT *catalog = job->client->catalog;
529          B_DB *db;
530
531          /*
532           * Make sure we can open catalog, otherwise print a warning
533           * message because the server is probably not running.
534           */
535          db = db_init_database(NULL, catalog->db_name, catalog->db_user,
536                             catalog->db_password, catalog->db_address,
537                             catalog->db_port, catalog->db_socket);
538          if (!db || !db_open_database(NULL, db)) {
539             Jmsg(NULL, M_FATAL, 0, _("Could not open database \"%s\".\n"),
540                  catalog->db_name);
541             if (db) {
542                Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
543             }
544             OK = false;
545          } else {
546             /* If a pool is defined for this job, create the pool DB       
547              *  record if it is not already created. 
548              */
549             if (job->pool) {
550                create_pool(NULL, db, job->pool, POOL_OP_UPDATE);  /* update request */
551             }
552             /* Set default value in all counters */
553             COUNTER *counter;
554             foreach_res(counter, R_COUNTER) {
555                /* Write to catalog? */
556                if (!counter->created && counter->Catalog == catalog) {
557                   COUNTER_DBR cr;
558                   bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
559                   cr.MinValue = counter->MinValue;
560                   cr.MaxValue = counter->MaxValue;
561                   cr.CurrentValue = counter->MinValue;
562                   if (counter->WrapCounter) {
563                      bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
564                   } else {
565                      cr.WrapCounter[0] = 0;  /* empty string */
566                   }
567                   if (db_create_counter_record(NULL, db, &cr)) {
568                      counter->CurrentValue = cr.CurrentValue;
569                      counter->created = true;
570                      Dmsg2(100, "Create counter %s val=%d\n", counter->hdr.name, counter->CurrentValue);
571                   }
572                } 
573                if (!counter->created) {
574                   counter->CurrentValue = counter->MinValue;  /* default value */
575                }
576             }
577          }
578          db_close_database(NULL, db);
579       }
580    }
581
582    UnlockRes();
583    if (OK) {
584       close_msg(NULL);                /* close temp message handler */
585       init_msg(NULL, director->messages); /* open daemon message handler */
586    }
587    return OK;
588 }