2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * Second generation Storage daemon.
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
43 /* TODO: fix problem with bls, bextract
44 * that use findlib and already declare
47 #include "sd_plugins.h"
51 #undef _POSIX_C_SOURCE
54 #include "lib/pythonlib.h"
56 /* Imported Functions */
57 extern PyObject *job_getattr(PyObject *self, char *attrname);
58 extern int job_setattr(PyObject *self, char *attrname, PyObject *value);
60 #endif /* HAVE_PYTHON */
62 /* Imported functions */
63 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
65 /* Forward referenced functions */
66 void terminate_stored(int sig);
67 static int check_resources();
68 static void cleanup_old_files();
70 extern "C" void *device_initialization(void *arg);
72 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
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;
84 static uint32_t VolSessionId = 0;
85 uint32_t VolSessionTime;
86 char *configfile = NULL;
87 bool init_done = false;
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;
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);
119 /*********************************************************************
121 * Main Bacula Unix Storage Daemon
124 #if defined(HAVE_WIN32)
125 #define main BaculaMain
128 int main (int argc, char *argv[])
131 bool no_signals = false;
132 bool test_config = false;
137 init_python_interpreter_args python_args;
138 #endif /* HAVE_PYTHON */
140 start_heap = sbrk(0);
141 setlocale(LC_ALL, "");
142 bindtextdomain("bacula", LOCALEDIR);
143 textdomain("bacula");
146 my_name_is(argc, argv, "bacula-sd");
147 init_msg(NULL, NULL);
148 daemon_start_time = time(NULL);
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);
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);
159 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
161 case 'c': /* configuration file */
162 if (configfile != NULL) {
165 configfile = bstrdup(optarg);
168 case 'd': /* debug level */
169 if (*optarg == 't') {
170 dbg_timestamp = true;
172 debug_level = atoi(optarg);
173 if (debug_level <= 0) {
179 case 'f': /* run in foreground */
183 case 'g': /* set group id */
187 case 'm': /* print kaboom output */
191 case 'p': /* proceed in spite of I/O errors */
195 case 's': /* no signals */
203 case 'u': /* set uid */
207 case 'v': /* verbose */
221 if (configfile != NULL) {
224 configfile = bstrdup(*argv);
232 init_signals(terminate_stored);
235 if (configfile == NULL) {
236 configfile = bstrdup(CONFIG_FILE);
239 config = new_config_parser();
240 parse_sd_config(config, configfile, M_ERROR_TERM);
242 if (init_crypto() != 0) {
243 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
246 if (!check_resources()) {
247 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
250 init_reservations_lock();
256 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
259 daemon_start(); /* become daemon */
260 init_stack_dump(); /* pick up new pid */
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));
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 */
272 load_sd_plugins(me->plugin_directory);
274 drop(uid, gid, false);
279 /* Ensure that Volume Session Time and Id are both
280 * set and are both non-zero.
282 VolSessionTime = (uint32_t)daemon_start_time;
283 if (VolSessionTime == 0) { /* paranoid */
284 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
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;
296 init_python_interpreter(&python_args);
297 #endif /* HAVE_PYTHON */
300 * Start the device allocation thread
302 create_volume_lists(); /* do before device_init */
303 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
305 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
308 start_watchdog(); /* start watchdog thread */
309 init_jcr_subsystem(); /* start JCR watchdogs etc. */
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 */
317 /* Return a new Session Id */
318 uint32_t newVolSessionId()
329 /* Check Configuration file for necessary info */
330 static int check_resources()
336 me = (STORES *)GetNextRes(R_STORAGE, NULL);
338 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
343 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
344 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
348 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
349 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
353 if (GetNextRes(R_DEVICE, NULL) == NULL){
354 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
360 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
362 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
368 if (!me->working_directory) {
369 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
376 foreach_res(store, R_STORAGE) {
377 /* tls_require implies tls_enable */
378 if (store->tls_require) {
380 store->tls_enable = true;
382 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
388 tls_needed = store->tls_enable || store->tls_authenticate;
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);
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);
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);
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);
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);
429 foreach_res(director, R_DIRECTOR) {
430 /* tls_require implies tls_enable */
431 if (director->tls_require) {
432 director->tls_enable = true;
435 tls_needed = director->tls_enable || director->tls_authenticate;
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);
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);
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);
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);
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);
476 OK = init_autochangers();
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);
488 static void cleanup_old_files()
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 ");
496 pm_strcpy(cleanup, "/bin/rm -f ");
498 pm_strcat(cleanup, me->working_directory);
499 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
500 pm_strcat(cleanup, "/");
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);
511 * Here we attempt to init and open each device. This is done
512 * once at startup in a separate thread.
515 void *device_initialization(void *arg)
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);
531 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
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);
539 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
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);
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());
559 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
560 switch (read_dev_volume_label(dcr)) {
562 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
563 volume_unused(dcr); /* mark volume "released" */
566 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
575 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
587 /* Clean up and then exit */
588 void terminate_stored(int sig)
590 static bool in_here = false;
594 if (in_here) { /* prevent loops */
595 bmicrosleep(2, 0); /* yield */
599 debug_level = 0; /* turn off any debug */
602 if (sig == SIGTERM) { /* normal shutdown request? */
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
611 if (jcr->JobId == 0) {
613 continue; /* ignore console */
615 jcr->setJobStatus(JS_Canceled);
616 fd = jcr->file_bsock;
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);
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);
631 bmicrosleep(0, 50000);
635 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
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));
641 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
646 foreach_res(device, R_DEVICE) {
647 Dmsg1(10, "Term device %s\n", device->device_name);
649 device->dev->clear_volhdr();
653 Dmsg1(10, "No dev structure %s\n", device->device_name);
662 config->free_resources();
667 if (debug_level > 10) {
668 print_memory_pool_stats();
672 term_reservations_lock();
676 sm_dump(false); /* dump orphaned buffers */