2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2011 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"
49 /* Imported functions */
50 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
52 /* Forward referenced functions */
53 void terminate_stored(int sig);
54 static int check_resources();
55 static void cleanup_old_files();
57 extern "C" void *device_initialization(void *arg);
59 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
61 /* Global variables exported */
62 char OK_msg[] = "3000 OK\n";
63 char TERM_msg[] = "3999 Terminate\n";
64 STORES *me = NULL; /* our Global resource */
65 bool forge_on = false; /* proceed inspite of I/O errors */
66 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
67 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
71 static uint32_t VolSessionId = 0;
72 uint32_t VolSessionTime;
73 char *configfile = NULL;
74 bool init_done = false;
76 /* Global static variables */
77 static bool foreground = 0;
78 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
79 static workq_t dird_workq; /* queue for processing connections */
80 static CONFIG *config;
87 "\nVersion: %s (%s)\n\n"
88 "Usage: bacula-sd [options] [-c config_file] [config_file]\n"
89 " -c <file> use <file> as configuration file\n"
90 " -d <nn> set debug level to <nn>\n"
91 " -dt print timestamp in debug output\n"
92 " -f run in foreground (for debugging)\n"
93 " -g <group> set groupid to group\n"
94 " -m print kaboom output (for debugging)\n"
95 " -p proceed despite I/O errors\n"
96 " -s no signals (for debugging)\n"
97 " -t test - read config and exit\n"
98 " -u <user> userid to <user>\n"
99 " -v verbose user messages\n"
100 " -? print this message.\n"
101 "\n"), 2000, VERSION, BDATE);
106 /*********************************************************************
108 * Main Bacula Unix Storage Daemon
111 #if defined(HAVE_WIN32)
112 #define main BaculaMain
115 int main (int argc, char *argv[])
118 bool no_signals = false;
119 bool test_config = false;
124 start_heap = sbrk(0);
125 setlocale(LC_ALL, "");
126 bindtextdomain("bacula", LOCALEDIR);
127 textdomain("bacula");
130 my_name_is(argc, argv, "bacula-sd");
131 init_msg(NULL, NULL);
132 daemon_start_time = time(NULL);
135 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
136 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
137 TAPE_BSIZE, B_DEV_BSIZE);
139 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
140 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
143 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
145 case 'c': /* configuration file */
146 if (configfile != NULL) {
149 configfile = bstrdup(optarg);
152 case 'd': /* debug level */
153 if (*optarg == 't') {
154 dbg_timestamp = true;
156 debug_level = atoi(optarg);
157 if (debug_level <= 0) {
163 case 'f': /* run in foreground */
167 case 'g': /* set group id */
171 case 'm': /* print kaboom output */
175 case 'p': /* proceed in spite of I/O errors */
179 case 's': /* no signals */
187 case 'u': /* set uid */
191 case 'v': /* verbose */
205 if (configfile != NULL) {
208 configfile = bstrdup(*argv);
216 init_signals(terminate_stored);
219 if (configfile == NULL) {
220 configfile = bstrdup(CONFIG_FILE);
223 config = new_config_parser();
224 parse_sd_config(config, configfile, M_ERROR_TERM);
226 if (init_crypto() != 0) {
227 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
230 if (!check_resources()) {
231 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
234 init_reservations_lock();
240 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
243 daemon_start(); /* become daemon */
244 init_stack_dump(); /* pick up new pid */
247 create_pid_file(me->pid_directory, "bacula-sd",
248 get_first_port_host_order(me->sdaddrs));
249 read_state_file(me->working_directory, "bacula-sd",
250 get_first_port_host_order(me->sdaddrs));
252 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
253 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
254 lmgr_init_thread(); /* initialize the lockmanager stack */
256 load_sd_plugins(me->plugin_directory);
258 drop(uid, gid, false);
262 /* Ensure that Volume Session Time and Id are both
263 * set and are both non-zero.
265 VolSessionTime = (uint32_t)daemon_start_time;
266 if (VolSessionTime == 0) { /* paranoid */
267 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
271 * Start the device allocation thread
273 create_volume_lists(); /* do before device_init */
274 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
276 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
279 start_watchdog(); /* start watchdog thread */
280 init_jcr_subsystem(); /* start JCR watchdogs etc. */
282 /* Single server used for Director and File daemon */
283 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
284 &dird_workq, handle_connection_request);
285 exit(1); /* to keep compiler quiet */
288 /* Return a new Session Id */
289 uint32_t newVolSessionId()
300 /* Check Configuration file for necessary info */
301 static int check_resources()
307 me = (STORES *)GetNextRes(R_STORAGE, NULL);
309 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
314 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
315 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
319 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
320 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
324 if (GetNextRes(R_DEVICE, NULL) == NULL){
325 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
331 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
333 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
339 if (!me->working_directory) {
340 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
347 foreach_res(store, R_STORAGE) {
348 /* tls_require implies tls_enable */
349 if (store->tls_require) {
351 store->tls_enable = true;
353 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
359 tls_needed = store->tls_enable || store->tls_authenticate;
361 if (!store->tls_certfile && tls_needed) {
362 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
363 store->hdr.name, configfile);
367 if (!store->tls_keyfile && tls_needed) {
368 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
369 store->hdr.name, configfile);
373 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
374 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
375 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
376 " At least one CA certificate store is required"
377 " when using \"TLS Verify Peer\".\n"),
378 store->hdr.name, configfile);
382 /* If everything is well, attempt to initialize our per-resource TLS context */
383 if (OK && (tls_needed || store->tls_require)) {
384 /* Initialize TLS context:
385 * Args: CA certfile, CA certdir, Certfile, Keyfile,
386 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
387 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
388 store->tls_ca_certdir, store->tls_certfile,
389 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
390 store->tls_verify_peer);
392 if (!store->tls_ctx) {
393 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
394 store->hdr.name, configfile);
400 foreach_res(director, R_DIRECTOR) {
401 /* tls_require implies tls_enable */
402 if (director->tls_require) {
403 director->tls_enable = true;
406 tls_needed = director->tls_enable || director->tls_authenticate;
408 if (!director->tls_certfile && tls_needed) {
409 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
410 director->hdr.name, configfile);
414 if (!director->tls_keyfile && tls_needed) {
415 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
416 director->hdr.name, configfile);
420 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
421 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
422 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
423 " At least one CA certificate store is required"
424 " when using \"TLS Verify Peer\".\n"),
425 director->hdr.name, configfile);
429 /* If everything is well, attempt to initialize our per-resource TLS context */
430 if (OK && (tls_needed || director->tls_require)) {
431 /* Initialize TLS context:
432 * Args: CA certfile, CA certdir, Certfile, Keyfile,
433 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
434 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
435 director->tls_ca_certdir, director->tls_certfile,
436 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
437 director->tls_verify_peer);
439 if (!director->tls_ctx) {
440 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
441 director->hdr.name, configfile);
447 OK = init_autochangers();
451 close_msg(NULL); /* close temp message handler */
452 init_msg(NULL, me->messages); /* open daemon message handler */
453 set_working_directory(me->working_directory);
460 * Remove old .spool files written by me from the working directory.
462 static void cleanup_old_files()
465 struct dirent *entry, *result;
467 int my_name_len = strlen(my_name);
468 int len = strlen(me->working_directory);
469 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
470 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
473 const int nmatch = 30;
474 regmatch_t pmatch[nmatch];
477 /* Look for .spool files but don't allow spaces */
478 const char *pat1 = "^[^ ]+\\.spool$";
480 /* Setup working directory prefix */
481 pm_strcpy(basename, me->working_directory);
482 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
483 pm_strcat(basename, "/");
486 /* Compile regex expressions */
487 rc = regcomp(&preg1, pat1, REG_EXTENDED);
489 regerror(rc, &preg1, prbuf, sizeof(prbuf));
490 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
495 name_max = pathconf(".", _PC_NAME_MAX);
496 if (name_max < 1024) {
500 if (!(dp = opendir(me->working_directory))) {
502 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
503 me->working_directory, be.bstrerror());
507 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
509 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
512 /* Exclude any name with ., .., not my_name or containing a space */
513 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
514 strncmp(result->d_name, my_name, my_name_len) != 0) {
515 Dmsg1(500, "Skipped: %s\n", result->d_name);
519 /* Unlink files that match regex */
520 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
521 pm_strcpy(cleanup, basename);
522 pm_strcat(cleanup, result->d_name);
523 Dmsg1(500, "Unlink: %s\n", cleanup);
533 free_pool_memory(cleanup);
534 free_pool_memory(basename);
539 * Here we attempt to init and open each device. This is done
540 * once at startup in a separate thread.
543 void *device_initialization(void *arg)
552 pthread_detach(pthread_self());
553 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
554 jcr->setJobType(JT_SYSTEM);
555 /* Initialize FD start condition variable */
556 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
559 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
562 foreach_res(device, R_DEVICE) {
563 Dmsg1(90, "calling init_dev %s\n", device->device_name);
564 dev = init_dev(NULL, device);
565 Dmsg1(10, "SD init done %s\n", device->device_name);
567 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
571 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
572 generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
573 if (dev->is_autochanger()) {
574 /* If autochanger set slot in dev sturcture */
575 get_autochanger_loaded_slot(dcr);
578 if (device->cap_bits & CAP_ALWAYSOPEN) {
579 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
580 if (!first_open_device(dcr)) {
581 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
582 Dmsg1(20, "Could not open device %s\n", dev->print_name());
588 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
589 switch (read_dev_volume_label(dcr)) {
591 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
592 volume_unused(dcr); /* mark volume "released" */
595 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
604 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
616 /* Clean up and then exit */
617 void terminate_stored(int sig)
619 static bool in_here = false;
623 if (in_here) { /* prevent loops */
624 bmicrosleep(2, 0); /* yield */
628 debug_level = 0; /* turn off any debug */
631 if (sig == SIGTERM) { /* normal shutdown request? */
633 * This is a normal shutdown request. We wiffle through
634 * all open jobs canceling them and trying to wake
635 * them up so that they will report back the correct
640 if (jcr->JobId == 0) {
642 continue; /* ignore console */
644 jcr->setJobStatus(JS_Canceled);
645 fd = jcr->file_bsock;
648 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
649 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
650 /* ***FIXME*** wiffle through all dcrs */
651 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
652 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
653 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
654 pthread_cond_broadcast(&wait_device_release);
656 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
657 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
658 pthread_cond_broadcast(&wait_device_release);
660 bmicrosleep(0, 50000);
664 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
667 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
668 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
670 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
675 foreach_res(device, R_DEVICE) {
676 Dmsg1(10, "Term device %s\n", device->device_name);
678 device->dev->clear_volhdr();
682 Dmsg1(10, "No dev structure %s\n", device->device_name);
691 config->free_resources();
696 if (debug_level > 10) {
697 print_memory_pool_stats();
701 term_reservations_lock();
705 sm_dump(false); /* dump orphaned buffers */