2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Second generation Storage daemon.
21 * It accepts a number of simple commands from the File daemon
22 * and acts on them. When a request to append data is made,
23 * it opens a data channel and accepts data from the
31 /* TODO: fix problem with bls, bextract
32 * that use findlib and already declare
35 #include "sd_plugins.h"
37 /* Imported functions */
38 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
40 /* Forward referenced functions */
41 void terminate_stored(int sig);
42 static int check_resources();
43 static void cleanup_old_files();
45 extern "C" void *device_initialization(void *arg);
47 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
49 /* Global variables exported */
50 char OK_msg[] = "3000 OK\n";
51 char TERM_msg[] = "3999 Terminate\n";
52 STORES *me = NULL; /* our Global resource */
53 bool forge_on = false; /* proceed inspite of I/O errors */
54 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
55 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
59 static uint32_t VolSessionId = 0;
60 uint32_t VolSessionTime;
61 char *configfile = NULL;
62 bool init_done = false;
63 static pthread_t server_tid;
64 static bool server_tid_valid = false;
66 /* Global static variables */
67 static bool foreground = 0;
68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
69 static workq_t dird_workq; /* queue for processing connections */
70 static CONFIG *config;
77 "\nVersion: %s (%s)\n\n"
78 "Usage: bacula-sd [options] [-c config_file] [config_file]\n"
79 " -c <file> use <file> as configuration file\n"
80 " -d <nn> set debug level to <nn>\n"
81 " -dt print timestamp in debug output\n"
82 " -f run in foreground (for debugging)\n"
83 " -g <group> set groupid to group\n"
84 " -m print kaboom output (for debugging)\n"
85 " -p proceed despite I/O errors\n"
86 " -s no signals (for debugging)\n"
87 " -t test - read config and exit\n"
88 " -u <user> userid to <user>\n"
89 " -v verbose user messages\n"
90 " -? print this message.\n"
91 "\n"), 2000, VERSION, BDATE);
96 /*********************************************************************
98 * Main Bacula Unix Storage Daemon
101 #if defined(HAVE_WIN32)
102 #define main BaculaMain
105 int main (int argc, char *argv[])
108 bool no_signals = false;
109 bool test_config = false;
114 start_heap = sbrk(0);
115 setlocale(LC_ALL, "");
116 bindtextdomain("bacula", LOCALEDIR);
117 textdomain("bacula");
120 my_name_is(argc, argv, "bacula-sd");
121 init_msg(NULL, NULL);
122 daemon_start_time = time(NULL);
125 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
126 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
127 TAPE_BSIZE, B_DEV_BSIZE);
129 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
130 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
133 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
135 case 'c': /* configuration file */
136 if (configfile != NULL) {
139 configfile = bstrdup(optarg);
142 case 'd': /* debug level */
143 if (*optarg == 't') {
144 dbg_timestamp = true;
146 debug_level = atoi(optarg);
147 if (debug_level <= 0) {
153 case 'f': /* run in foreground */
157 case 'g': /* set group id */
161 case 'm': /* print kaboom output */
165 case 'p': /* proceed in spite of I/O errors */
169 case 's': /* no signals */
177 case 'u': /* set uid */
181 case 'v': /* verbose */
195 if (configfile != NULL) {
198 configfile = bstrdup(*argv);
206 daemon_start(); /* become daemon */
207 init_stack_dump(); /* pick up new pid */
211 init_signals(terminate_stored);
214 if (configfile == NULL) {
215 configfile = bstrdup(CONFIG_FILE);
218 config = new_config_parser();
219 parse_sd_config(config, configfile, M_ERROR_TERM);
221 if (init_crypto() != 0) {
222 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
225 if (!check_resources()) {
226 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
229 init_reservations_lock();
235 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
238 create_pid_file(me->pid_directory, "bacula-sd",
239 get_first_port_host_order(me->sdaddrs));
240 read_state_file(me->working_directory, "bacula-sd",
241 get_first_port_host_order(me->sdaddrs));
243 set_jcr_in_tsd(INVALID_JCR);
244 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
245 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
246 lmgr_init_thread(); /* initialize the lockmanager stack */
248 load_sd_plugins(me->plugin_directory);
250 drop(uid, gid, false);
254 /* Ensure that Volume Session Time and Id are both
255 * set and are both non-zero.
257 VolSessionTime = (uint32_t)daemon_start_time;
258 if (VolSessionTime == 0) { /* paranoid */
259 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
263 * Start the device allocation thread
265 create_volume_lists(); /* do before device_init */
266 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
268 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
271 start_watchdog(); /* start watchdog thread */
272 init_jcr_subsystem(); /* start JCR watchdogs etc. */
274 /* Single server used for Director and File daemon */
275 server_tid = pthread_self();
276 server_tid_valid = true;
277 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
278 &dird_workq, handle_connection_request);
279 exit(1); /* to keep compiler quiet */
282 /* Return a new Session Id */
283 uint32_t newVolSessionId()
294 /* Check Configuration file for necessary info */
295 static int check_resources()
301 me = (STORES *)GetNextRes(R_STORAGE, NULL);
303 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
308 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
309 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
313 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
314 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
318 if (GetNextRes(R_DEVICE, NULL) == NULL){
319 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
325 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
327 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
333 if (!me->working_directory) {
334 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
341 foreach_res(store, R_STORAGE) {
342 /* tls_require implies tls_enable */
343 if (store->tls_require) {
345 store->tls_enable = true;
347 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
353 tls_needed = store->tls_enable || store->tls_authenticate;
355 if (!store->tls_certfile && tls_needed) {
356 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
357 store->hdr.name, configfile);
361 if (!store->tls_keyfile && tls_needed) {
362 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
363 store->hdr.name, configfile);
367 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
368 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
369 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
370 " At least one CA certificate store is required"
371 " when using \"TLS Verify Peer\".\n"),
372 store->hdr.name, configfile);
376 /* If everything is well, attempt to initialize our per-resource TLS context */
377 if (OK && (tls_needed || store->tls_require)) {
378 /* Initialize TLS context:
379 * Args: CA certfile, CA certdir, Certfile, Keyfile,
380 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
381 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
382 store->tls_ca_certdir, store->tls_certfile,
383 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
384 store->tls_verify_peer);
386 if (!store->tls_ctx) {
387 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
388 store->hdr.name, configfile);
394 foreach_res(director, R_DIRECTOR) {
395 /* tls_require implies tls_enable */
396 if (director->tls_require) {
397 director->tls_enable = true;
400 tls_needed = director->tls_enable || director->tls_authenticate;
402 if (!director->tls_certfile && tls_needed) {
403 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
404 director->hdr.name, configfile);
408 if (!director->tls_keyfile && tls_needed) {
409 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
410 director->hdr.name, configfile);
414 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
415 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
416 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
417 " At least one CA certificate store is required"
418 " when using \"TLS Verify Peer\".\n"),
419 director->hdr.name, configfile);
423 /* If everything is well, attempt to initialize our per-resource TLS context */
424 if (OK && (tls_needed || director->tls_require)) {
425 /* Initialize TLS context:
426 * Args: CA certfile, CA certdir, Certfile, Keyfile,
427 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
428 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
429 director->tls_ca_certdir, director->tls_certfile,
430 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
431 director->tls_verify_peer);
433 if (!director->tls_ctx) {
434 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
435 director->hdr.name, configfile);
441 OK = init_autochangers();
445 close_msg(NULL); /* close temp message handler */
446 init_msg(NULL, me->messages); /* open daemon message handler */
447 set_working_directory(me->working_directory);
454 * Remove old .spool files written by me from the working directory.
456 static void cleanup_old_files()
459 struct dirent *entry, *result;
461 int my_name_len = strlen(my_name);
462 int len = strlen(me->working_directory);
463 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
464 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
467 const int nmatch = 30;
468 regmatch_t pmatch[nmatch];
471 /* Look for .spool files but don't allow spaces */
472 const char *pat1 = "^[^ ]+\\.spool$";
474 /* Setup working directory prefix */
475 pm_strcpy(basename, me->working_directory);
476 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
477 pm_strcat(basename, "/");
480 /* Compile regex expressions */
481 rc = regcomp(&preg1, pat1, REG_EXTENDED);
483 regerror(rc, &preg1, prbuf, sizeof(prbuf));
484 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
489 name_max = pathconf(".", _PC_NAME_MAX);
490 if (name_max < 1024) {
494 if (!(dp = opendir(me->working_directory))) {
496 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
497 me->working_directory, be.bstrerror());
501 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
503 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
506 /* Exclude any name with ., .., not my_name or containing a space */
507 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
508 strncmp(result->d_name, my_name, my_name_len) != 0) {
509 Dmsg1(500, "Skipped: %s\n", result->d_name);
513 /* Unlink files that match regex */
514 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
515 pm_strcpy(cleanup, basename);
516 pm_strcat(cleanup, result->d_name);
517 Dmsg1(500, "Unlink: %s\n", cleanup);
527 free_pool_memory(cleanup);
528 free_pool_memory(basename);
533 * Here we attempt to init and open each device. This is done
534 * once at startup in a separate thread.
537 void *device_initialization(void *arg)
546 pthread_detach(pthread_self());
547 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
548 jcr->setJobType(JT_SYSTEM);
549 /* Initialize FD start condition variable */
550 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
553 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
556 foreach_res(device, R_DEVICE) {
557 Dmsg1(90, "calling init_dev %s\n", device->device_name);
558 dev = init_dev(NULL, device);
559 Dmsg1(10, "SD init done %s\n", device->device_name);
561 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
565 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
566 generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
567 if (dev->is_autochanger()) {
568 /* If autochanger set slot in dev sturcture */
569 get_autochanger_loaded_slot(dcr);
572 if (device->cap_bits & CAP_ALWAYSOPEN) {
573 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
574 if (!first_open_device(dcr)) {
575 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
576 Dmsg1(20, "Could not open device %s\n", dev->print_name());
582 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
583 switch (read_dev_volume_label(dcr)) {
585 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
586 volume_unused(dcr); /* mark volume "released" */
589 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
598 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
611 /* Clean up and then exit */
612 void terminate_stored(int sig)
614 static bool in_here = false;
618 if (in_here) { /* prevent loops */
619 bmicrosleep(2, 0); /* yield */
623 debug_level = 0; /* turn off any debug */
626 if (sig == SIGTERM) { /* normal shutdown request? */
628 * This is a normal shutdown request. We wiffle through
629 * all open jobs canceling them and trying to wake
630 * them up so that they will report back the correct
635 if (jcr->JobId == 0) {
637 continue; /* ignore console */
639 jcr->setJobStatus(JS_Canceled);
640 fd = jcr->file_bsock;
643 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
644 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
645 /* ***FIXME*** wiffle through all dcrs */
646 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
647 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
648 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
649 pthread_cond_broadcast(&wait_device_release);
651 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
652 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
653 pthread_cond_broadcast(&wait_device_release);
655 bmicrosleep(0, 50000);
659 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
662 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
663 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
665 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
670 foreach_res(device, R_DEVICE) {
671 Dmsg1(10, "Term device %s\n", device->device_name);
673 device->dev->clear_volhdr();
677 Dmsg1(10, "No dev structure %s\n", device->device_name);
680 if (server_tid_valid) {
681 bnet_stop_thread_server(server_tid);
688 config->free_resources();
693 if (chk_dbglvl(10)) {
694 print_memory_pool_stats();
698 term_reservations_lock();
702 sm_dump(false); /* dump orphaned buffers */