2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 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 two of the GNU 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 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);
118 /*********************************************************************
120 * Main Bacula Unix Storage Daemon
123 #if defined(HAVE_WIN32)
124 #define main BaculaMain
127 int main (int argc, char *argv[])
130 bool no_signals = false;
131 bool test_config = false;
136 init_python_interpreter_args python_args;
137 #endif /* HAVE_PYTHON */
139 start_heap = sbrk(0);
140 setlocale(LC_ALL, "");
141 bindtextdomain("bacula", LOCALEDIR);
142 textdomain("bacula");
145 my_name_is(argc, argv, "bacula-sd");
146 init_msg(NULL, NULL);
147 daemon_start_time = time(NULL);
150 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
151 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
152 TAPE_BSIZE, B_DEV_BSIZE);
154 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
155 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
158 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
160 case 'c': /* configuration file */
161 if (configfile != NULL) {
164 configfile = bstrdup(optarg);
167 case 'd': /* debug level */
168 if (*optarg == 't') {
169 dbg_timestamp = true;
171 debug_level = atoi(optarg);
172 if (debug_level <= 0) {
178 case 'f': /* run in foreground */
182 case 'g': /* set group id */
186 case 'm': /* print kaboom output */
190 case 'p': /* proceed in spite of I/O errors */
194 case 's': /* no signals */
202 case 'u': /* set uid */
206 case 'v': /* verbose */
220 if (configfile != NULL) {
223 configfile = bstrdup(*argv);
231 init_signals(terminate_stored);
234 if (configfile == NULL) {
235 configfile = bstrdup(CONFIG_FILE);
238 config = new_config_parser();
239 parse_sd_config(config, configfile, M_ERROR_TERM);
241 if (init_crypto() != 0) {
242 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
245 if (!check_resources()) {
246 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
249 init_reservations_lock();
255 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
258 daemon_start(); /* become daemon */
259 init_stack_dump(); /* pick up new pid */
262 create_pid_file(me->pid_directory, "bacula-sd",
263 get_first_port_host_order(me->sdaddrs));
264 read_state_file(me->working_directory, "bacula-sd",
265 get_first_port_host_order(me->sdaddrs));
267 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
268 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
269 lmgr_init_thread(); /* initialize the lockmanager stack */
271 load_sd_plugins(me->plugin_directory);
273 drop(uid, gid, false);
278 /* Ensure that Volume Session Time and Id are both
279 * set and are both non-zero.
281 VolSessionTime = (uint32_t)daemon_start_time;
282 if (VolSessionTime == 0) { /* paranoid */
283 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
287 python_args.progname = me->hdr.name;
288 python_args.scriptdir = me->scripts_directory;
289 python_args.modulename = "SDStartUp";
290 python_args.configfile = configfile;
291 python_args.workingdir = me->working_directory;
292 python_args.job_getattr = job_getattr;
293 python_args.job_setattr = job_setattr;
295 init_python_interpreter(&python_args);
296 #endif /* HAVE_PYTHON */
299 * Start the device allocation thread
301 create_volume_lists(); /* do before device_init */
302 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
304 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
307 start_watchdog(); /* start watchdog thread */
308 init_jcr_subsystem(); /* start JCR watchdogs etc. */
310 /* Single server used for Director and File daemon */
311 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
312 &dird_workq, handle_connection_request);
313 exit(1); /* to keep compiler quiet */
316 /* Return a new Session Id */
317 uint32_t newVolSessionId()
328 /* Check Configuration file for necessary info */
329 static int check_resources()
335 me = (STORES *)GetNextRes(R_STORAGE, NULL);
337 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
342 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
343 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
347 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
348 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
352 if (GetNextRes(R_DEVICE, NULL) == NULL){
353 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
359 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
361 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
367 if (!me->working_directory) {
368 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
375 foreach_res(store, R_STORAGE) {
376 /* tls_require implies tls_enable */
377 if (store->tls_require) {
379 store->tls_enable = true;
381 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
387 tls_needed = store->tls_enable || store->tls_authenticate;
389 if (!store->tls_certfile && tls_needed) {
390 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
391 store->hdr.name, configfile);
395 if (!store->tls_keyfile && tls_needed) {
396 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
397 store->hdr.name, configfile);
401 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
402 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
403 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
404 " At least one CA certificate store is required"
405 " when using \"TLS Verify Peer\".\n"),
406 store->hdr.name, configfile);
410 /* If everything is well, attempt to initialize our per-resource TLS context */
411 if (OK && (tls_needed || store->tls_require)) {
412 /* Initialize TLS context:
413 * Args: CA certfile, CA certdir, Certfile, Keyfile,
414 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
415 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
416 store->tls_ca_certdir, store->tls_certfile,
417 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
418 store->tls_verify_peer);
420 if (!store->tls_ctx) {
421 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
422 store->hdr.name, configfile);
428 foreach_res(director, R_DIRECTOR) {
429 /* tls_require implies tls_enable */
430 if (director->tls_require) {
431 director->tls_enable = true;
434 tls_needed = director->tls_enable || director->tls_authenticate;
436 if (!director->tls_certfile && tls_needed) {
437 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
438 director->hdr.name, configfile);
442 if (!director->tls_keyfile && tls_needed) {
443 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
444 director->hdr.name, configfile);
448 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
449 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
450 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
451 " At least one CA certificate store is required"
452 " when using \"TLS Verify Peer\".\n"),
453 director->hdr.name, configfile);
457 /* If everything is well, attempt to initialize our per-resource TLS context */
458 if (OK && (tls_needed || director->tls_require)) {
459 /* Initialize TLS context:
460 * Args: CA certfile, CA certdir, Certfile, Keyfile,
461 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
462 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
463 director->tls_ca_certdir, director->tls_certfile,
464 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
465 director->tls_verify_peer);
467 if (!director->tls_ctx) {
468 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
469 director->hdr.name, configfile);
475 OK = init_autochangers();
479 close_msg(NULL); /* close temp message handler */
480 init_msg(NULL, me->messages); /* open daemon message handler */
481 set_working_directory(me->working_directory);
487 static void cleanup_old_files()
489 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
490 POOLMEM *results = get_pool_memory(PM_MESSAGE);
491 int len = strlen(me->working_directory);
492 #if defined(HAVE_WIN32)
493 pm_strcpy(cleanup, "del /q ");
495 pm_strcpy(cleanup, "/bin/rm -f ");
497 pm_strcat(cleanup, me->working_directory);
498 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
499 pm_strcat(cleanup, "/");
501 pm_strcat(cleanup, my_name);
502 pm_strcat(cleanup, "*.spool");
503 run_program(cleanup, 0, results);
504 free_pool_memory(cleanup);
505 free_pool_memory(results);
510 * Here we attempt to init and open each device. This is done
511 * once at startup in a separate thread.
514 void *device_initialization(void *arg)
523 pthread_detach(pthread_self());
524 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
525 jcr->set_JobType(JT_SYSTEM);
526 /* Initialize FD start condition variable */
527 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
530 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
533 foreach_res(device, R_DEVICE) {
534 Dmsg1(90, "calling init_dev %s\n", device->device_name);
535 dev = init_dev(NULL, device);
536 Dmsg1(10, "SD init done %s\n", device->device_name);
538 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
542 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
543 if (dev->is_autochanger()) {
544 /* If autochanger set slot in dev sturcture */
545 get_autochanger_loaded_slot(dcr);
548 if (device->cap_bits & CAP_ALWAYSOPEN) {
549 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
550 if (!first_open_device(dcr)) {
551 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
552 Dmsg1(20, "Could not open device %s\n", dev->print_name());
558 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
559 switch (read_dev_volume_label(dcr)) {
561 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
562 volume_unused(dcr); /* mark volume "released" */
565 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
574 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
586 /* Clean up and then exit */
587 void terminate_stored(int sig)
589 static bool in_here = false;
593 if (in_here) { /* prevent loops */
594 bmicrosleep(2, 0); /* yield */
598 debug_level = 0; /* turn off any debug */
601 if (sig == SIGTERM) { /* normal shutdown request? */
603 * This is a normal shutdown request. We wiffle through
604 * all open jobs canceling them and trying to wake
605 * them up so that they will report back the correct
610 if (jcr->JobId == 0) {
612 continue; /* ignore console */
614 set_jcr_job_status(jcr, JS_Canceled);
615 fd = jcr->file_bsock;
618 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
619 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
620 /* ***FIXME*** wiffle through all dcrs */
621 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
622 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
623 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
624 pthread_cond_broadcast(&wait_device_release);
626 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
627 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
628 pthread_cond_broadcast(&wait_device_release);
630 bmicrosleep(0, 50000);
634 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
637 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
638 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
640 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
645 foreach_res(device, R_DEVICE) {
646 Dmsg1(10, "Term device %s\n", device->device_name);
648 device->dev->clear_volhdr();
652 Dmsg1(10, "No dev structure %s\n", device->device_name);
661 config->free_resources();
666 if (debug_level > 10) {
667 print_memory_pool_stats();
671 term_reservations_lock();
675 sm_dump(false); /* dump orphaned buffers */