]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
Make bad Storage check in is_on_same_storage non-fatal
[bacula/bacula] / bacula / src / stored / stored.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Second generation Storage daemon.
30  *
31  *  Kern Sibbald, MM
32  *
33  * It accepts a number of simple commands from the File daemon
34  * and acts on them. When a request to append data is made,
35  * it opens a data channel and accepts data from the
36  * File daemon.
37  *
38  */
39
40 #include "bacula.h"
41 #include "stored.h"
42
43 /* TODO: fix problem with bls, bextract
44  * that use findlib and already declare
45  * filed plugins 
46  */
47 #include "sd_plugins.h"         
48
49 #ifdef HAVE_PYTHON
50
51 #undef _POSIX_C_SOURCE
52 #include <Python.h>
53
54 #include "lib/pythonlib.h"
55
56 /* Imported Functions */
57 extern PyObject *job_getattr(PyObject *self, char *attrname);
58 extern int job_setattr(PyObject *self, char *attrname, PyObject *value);
59
60 #endif /* HAVE_PYTHON */
61
62 /* Imported functions */
63 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
64
65 /* Forward referenced functions */
66 void terminate_stored(int sig);
67 static int check_resources();
68 static void cleanup_old_files();
69
70 extern "C" void *device_initialization(void *arg);
71
72 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
73
74 /* Global variables exported */
75 char OK_msg[]   = "3000 OK\n";
76 char TERM_msg[] = "3999 Terminate\n";
77 STORES *me = NULL;                    /* our Global resource */
78 bool forge_on = false;                /* proceed inspite of I/O errors */
79 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
80 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
81 void *start_heap;
82
83
84 static uint32_t VolSessionId = 0;
85 uint32_t VolSessionTime;
86 char *configfile = NULL;
87 bool init_done = false;
88
89 /* Global static variables */
90 static bool foreground = 0;
91 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
92 static workq_t dird_workq;            /* queue for processing connections */
93 static CONFIG *config;
94
95
96 static void usage()
97 {
98    fprintf(stderr, _(
99 PROG_COPYRIGHT
100 "\nVersion: %s (%s)\n\n"
101 "Usage: stored [options] [-c config_file] [config_file]\n"
102 "        -c <file>   use <file> as configuration file\n"
103 "        -d <nn>     set debug level to <nn>\n"
104 "        -dt         print timestamp in debug output\n"
105 "        -f          run in foreground (for debugging)\n"
106 "        -g <group>  set groupid to group\n"
107 "        -m          print kaboom output (for debugging)\n"
108 "        -p          proceed despite I/O errors\n"
109 "        -s          no signals (for debugging)\n"
110 "        -t          test - read config and exit\n"
111 "        -u <user>   userid to <user>\n"
112 "        -v          verbose user messages\n"
113 "        -?          print this message.\n"
114 "\n"), 2000, VERSION, BDATE);
115
116    exit(1);
117 }
118
119 /*********************************************************************
120  *
121  *  Main Bacula Unix Storage Daemon
122  *
123  */
124 #if defined(HAVE_WIN32)
125 #define main BaculaMain
126 #endif
127
128 int main (int argc, char *argv[])
129 {
130    int ch;
131    bool no_signals = false;
132    bool test_config = false;
133    pthread_t thid;
134    char *uid = NULL;
135    char *gid = NULL;
136 #ifdef HAVE_PYTHON
137    init_python_interpreter_args python_args;
138 #endif /* HAVE_PYTHON */
139
140    start_heap = sbrk(0);
141    setlocale(LC_ALL, "");
142    bindtextdomain("bacula", LOCALEDIR);
143    textdomain("bacula");
144
145    init_stack_dump();
146    my_name_is(argc, argv, "bacula-sd");
147    init_msg(NULL, NULL);
148    daemon_start_time = time(NULL);
149
150    /* Sanity checks */
151    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
152       Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
153          TAPE_BSIZE, B_DEV_BSIZE);
154    }
155    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
156       Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
157    }
158
159    while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
160       switch (ch) {
161       case 'c':                    /* configuration file */
162          if (configfile != NULL) {
163             free(configfile);
164          }
165          configfile = bstrdup(optarg);
166          break;
167
168       case 'd':                    /* debug level */
169          if (*optarg == 't') {
170             dbg_timestamp = true;
171          } else {
172             debug_level = atoi(optarg);
173             if (debug_level <= 0) {
174                debug_level = 1;
175             }
176          }
177          break;
178
179       case 'f':                    /* run in foreground */
180          foreground = true;
181          break;
182
183       case 'g':                    /* set group id */
184          gid = optarg;
185          break;
186
187       case 'm':                    /* print kaboom output */
188          prt_kaboom = true;
189          break;
190
191       case 'p':                    /* proceed in spite of I/O errors */
192          forge_on = true;
193          break;
194
195       case 's':                    /* no signals */
196          no_signals = true;
197          break;
198
199       case 't':
200          test_config = true;
201          break;
202
203       case 'u':                    /* set uid */
204          uid = optarg;
205          break;
206
207       case 'v':                    /* verbose */
208          verbose++;
209          break;
210
211       case '?':
212       default:
213          usage();
214          break;
215       }
216    }
217    argc -= optind;
218    argv += optind;
219
220    if (argc) {
221       if (configfile != NULL) {
222          free(configfile);
223       }
224       configfile = bstrdup(*argv);
225       argc--;
226       argv++;
227    }
228    if (argc)
229       usage();
230
231    if (!no_signals) {
232       init_signals(terminate_stored);
233    }
234
235    if (configfile == NULL) {
236       configfile = bstrdup(CONFIG_FILE);
237    }
238
239    config = new_config_parser();
240    parse_sd_config(config, configfile, M_ERROR_TERM);
241
242    if (init_crypto() != 0) {
243       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
244    }
245
246    if (!check_resources()) {
247       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
248    }
249
250    init_reservations_lock();
251
252    if (test_config) {
253       terminate_stored(0);
254    }
255
256    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
257
258    if (!foreground) {
259       daemon_start();                 /* become daemon */
260       init_stack_dump();              /* pick up new pid */
261    }
262
263    create_pid_file(me->pid_directory, "bacula-sd",
264                    get_first_port_host_order(me->sdaddrs));
265    read_state_file(me->working_directory, "bacula-sd", 
266                    get_first_port_host_order(me->sdaddrs));
267
268    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
269    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
270    lmgr_init_thread(); /* initialize the lockmanager stack */
271
272    load_sd_plugins(me->plugin_directory);
273
274    drop(uid, gid, false);
275
276    cleanup_old_files();
277
278
279    /* Ensure that Volume Session Time and Id are both
280     * set and are both non-zero.
281     */
282    VolSessionTime = (uint32_t)daemon_start_time;
283    if (VolSessionTime == 0) { /* paranoid */
284       Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
285    }
286
287 #ifdef HAVE_PYTHON
288    python_args.progname = me->hdr.name;
289    python_args.scriptdir = me->scripts_directory;
290    python_args.modulename = "SDStartUp";
291    python_args.configfile = configfile;
292    python_args.workingdir = me->working_directory;
293    python_args.job_getattr = job_getattr;
294    python_args.job_setattr = job_setattr;
295
296    init_python_interpreter(&python_args);
297 #endif /* HAVE_PYTHON */
298
299     /*
300      * Start the device allocation thread
301      */
302    create_volume_lists();             /* do before device_init */
303    if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
304       berrno be;
305       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
306    }
307
308    start_watchdog();                  /* start watchdog thread */
309    init_jcr_subsystem();              /* start JCR watchdogs etc. */
310
311    /* Single server used for Director and File daemon */
312    bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
313                       &dird_workq, handle_connection_request);
314    exit(1);                           /* to keep compiler quiet */
315 }
316
317 /* Return a new Session Id */
318 uint32_t newVolSessionId()
319 {
320    uint32_t Id;
321
322    P(mutex);
323    VolSessionId++;
324    Id = VolSessionId;
325    V(mutex);
326    return Id;
327 }
328
329 /* Check Configuration file for necessary info */
330 static int check_resources()
331 {
332    bool OK = true;
333    bool tls_needed;
334
335
336    me = (STORES *)GetNextRes(R_STORAGE, NULL);
337    if (!me) {
338       Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
339          configfile);
340       OK = false;
341    }
342
343    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
344       Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
345          configfile);
346       OK = false;
347    }
348    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
349       Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
350          configfile);
351       OK = false;
352    }
353    if (GetNextRes(R_DEVICE, NULL) == NULL){
354       Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
355            configfile);
356       OK = false;
357    }
358
359    if (!me->messages) {
360       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
361       if (!me->messages) {
362          Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
363             configfile);
364          OK = false;
365       }
366    }
367
368    if (!me->working_directory) {
369       Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
370          configfile);
371       OK = false;
372    }
373
374    DIRRES *director;
375    STORES *store;
376    foreach_res(store, R_STORAGE) { 
377       /* tls_require implies tls_enable */
378       if (store->tls_require) {
379          if (have_tls) {
380             store->tls_enable = true;
381          } else {
382             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
383             OK = false;
384             continue;
385          }
386       }
387
388       tls_needed = store->tls_enable || store->tls_authenticate;
389
390       if (!store->tls_certfile && tls_needed) {
391          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
392               store->hdr.name, configfile);
393          OK = false;
394       }
395
396       if (!store->tls_keyfile && tls_needed) {
397          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
398               store->hdr.name, configfile);
399          OK = false;
400       }
401
402       if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
403          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
404               " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
405               " At least one CA certificate store is required"
406               " when using \"TLS Verify Peer\".\n"),
407               store->hdr.name, configfile);
408          OK = false;
409       }
410
411       /* If everything is well, attempt to initialize our per-resource TLS context */
412       if (OK && (tls_needed || store->tls_require)) {
413          /* Initialize TLS context:
414           * Args: CA certfile, CA certdir, Certfile, Keyfile,
415           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
416          store->tls_ctx = new_tls_context(store->tls_ca_certfile,
417             store->tls_ca_certdir, store->tls_certfile,
418             store->tls_keyfile, NULL, NULL, store->tls_dhfile,
419             store->tls_verify_peer);
420
421          if (!store->tls_ctx) { 
422             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
423                  store->hdr.name, configfile);
424             OK = false;
425          }
426       }
427    }
428
429    foreach_res(director, R_DIRECTOR) { 
430       /* tls_require implies tls_enable */
431       if (director->tls_require) {
432          director->tls_enable = true;
433       }
434
435       tls_needed = director->tls_enable || director->tls_authenticate;
436
437       if (!director->tls_certfile && tls_needed) {
438          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
439               director->hdr.name, configfile);
440          OK = false;
441       }
442
443       if (!director->tls_keyfile && tls_needed) {
444          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
445               director->hdr.name, configfile);
446          OK = false;
447       }
448
449       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
450          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
451               " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
452               " At least one CA certificate store is required"
453               " when using \"TLS Verify Peer\".\n"),
454               director->hdr.name, configfile);
455          OK = false;
456       }
457
458       /* If everything is well, attempt to initialize our per-resource TLS context */
459       if (OK && (tls_needed || director->tls_require)) {
460          /* Initialize TLS context:
461           * Args: CA certfile, CA certdir, Certfile, Keyfile,
462           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
463          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
464             director->tls_ca_certdir, director->tls_certfile,
465             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
466             director->tls_verify_peer);
467
468          if (!director->tls_ctx) { 
469             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
470                  director->hdr.name, configfile);
471             OK = false;
472          }
473       }
474    }
475
476    OK = init_autochangers();
477
478    
479    if (OK) {
480       close_msg(NULL);                   /* close temp message handler */
481       init_msg(NULL, me->messages);      /* open daemon message handler */
482       set_working_directory(me->working_directory);
483    }
484
485    return OK;
486 }
487
488 static void cleanup_old_files()
489 {
490    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
491    POOLMEM *results = get_pool_memory(PM_MESSAGE);
492    int len = strlen(me->working_directory);
493 #if defined(HAVE_WIN32)
494    pm_strcpy(cleanup, "del /q ");
495 #else
496    pm_strcpy(cleanup, "/bin/rm -f ");
497 #endif
498    pm_strcat(cleanup, me->working_directory);
499    if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
500       pm_strcat(cleanup, "/");
501    }
502    pm_strcat(cleanup, my_name);
503    pm_strcat(cleanup, "*.spool");
504    run_program(cleanup, 0, results);
505    free_pool_memory(cleanup);
506    free_pool_memory(results);
507 }
508
509
510 /*
511  * Here we attempt to init and open each device. This is done
512  *  once at startup in a separate thread.
513  */
514 extern "C"
515 void *device_initialization(void *arg)
516 {
517    DEVRES *device;
518    DCR *dcr;
519    JCR *jcr;
520    DEVICE *dev;
521
522    LockRes();
523
524    pthread_detach(pthread_self());
525    jcr = new_jcr(sizeof(JCR), stored_free_jcr);
526    jcr->setJobType(JT_SYSTEM);
527    /* Initialize FD start condition variable */
528    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
529    if (errstat != 0) {
530       berrno be;
531       Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
532    }
533
534    foreach_res(device, R_DEVICE) {
535       Dmsg1(90, "calling init_dev %s\n", device->device_name);
536       dev = init_dev(NULL, device);
537       Dmsg1(10, "SD init done %s\n", device->device_name);
538       if (!dev) {
539          Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
540          continue;
541       }
542
543       jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
544       if (dev->is_autochanger()) {
545          /* If autochanger set slot in dev sturcture */
546          get_autochanger_loaded_slot(dcr);
547       }
548
549       if (device->cap_bits & CAP_ALWAYSOPEN) {
550          Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
551          if (!first_open_device(dcr)) {
552             Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
553             Dmsg1(20, "Could not open device %s\n", dev->print_name());
554             free_dcr(dcr);
555             jcr->dcr = NULL;
556             continue;
557          }
558       }
559       if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
560          switch (read_dev_volume_label(dcr)) {
561          case VOL_OK:
562             memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
563             volume_unused(dcr);             /* mark volume "released" */
564             break;
565          default:
566             Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
567             break;
568          }
569       }
570       free_dcr(dcr);
571       jcr->dcr = NULL;
572    }
573 #ifdef xxx
574    if (jcr->dcr) {
575       Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
576       free_dcr(jcr->dcr);
577       jcr->dcr = NULL;
578    }
579 #endif
580    free_jcr(jcr); 
581    init_done = true;
582    UnlockRes();
583    return NULL;
584 }
585
586
587 /* Clean up and then exit */
588 void terminate_stored(int sig)
589 {
590    static bool in_here = false;
591    DEVRES *device;
592    JCR *jcr;
593
594    if (in_here) {                     /* prevent loops */
595       bmicrosleep(2, 0);              /* yield */
596       exit(1);
597    }
598    in_here = true;
599    debug_level = 0;                   /* turn off any debug */
600    stop_watchdog();
601
602    if (sig == SIGTERM) {              /* normal shutdown request? */
603       /*
604        * This is a normal shutdown request. We wiffle through
605        *   all open jobs canceling them and trying to wake
606        *   them up so that they will report back the correct
607        *   volume status.
608        */
609       foreach_jcr(jcr) {
610          BSOCK *fd;
611          if (jcr->JobId == 0) {
612             free_jcr(jcr);
613             continue;                 /* ignore console */
614          }
615          jcr->setJobStatus(JS_Canceled);
616          fd = jcr->file_bsock;
617          if (fd) {
618             fd->set_timed_out();
619             jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
620             Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
621             /* ***FIXME*** wiffle through all dcrs */
622             if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
623                pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
624                Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
625                pthread_cond_broadcast(&wait_device_release);
626             }
627             if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
628                pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
629                pthread_cond_broadcast(&wait_device_release);
630             }
631             bmicrosleep(0, 50000);
632          }
633          free_jcr(jcr);
634       }
635       bmicrosleep(0, 500000);         /* give them 1/2 sec to clean up */
636    }
637
638    write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
639    delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
640
641    Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
642
643    unload_plugins();
644    free_volume_lists();
645
646    foreach_res(device, R_DEVICE) {
647       Dmsg1(10, "Term device %s\n", device->device_name);
648       if (device->dev) {
649          device->dev->clear_volhdr();
650          device->dev->term();
651          device->dev = NULL;
652       } else {
653          Dmsg1(10, "No dev structure %s\n", device->device_name);
654       }
655    }
656
657    if (configfile) {
658       free(configfile);
659       configfile = NULL;
660    }
661    if (config) {
662       config->free_resources();
663       free(config);
664       config = NULL;
665    }
666
667    if (debug_level > 10) {
668       print_memory_pool_stats();
669    }
670    term_msg();
671    cleanup_crypto();
672    term_reservations_lock();
673    close_memory_pool();
674    lmgr_cleanup_main();
675
676    sm_dump(false);                    /* dump orphaned buffers */
677    exit(sig);
678 }