2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Second generation Storage daemon.
23 * Written by Kern Sibbald, MM
25 * It accepts a number of simple commands from the File daemon
26 * and acts on them. When a request to append data is made,
27 * it opens a data channel and accepts data from the
35 /* TODO: fix problem with bls, bextract
36 * that use findlib and already declare
39 #include "sd_plugins.h"
41 /* Imported functions */
42 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
44 /* Forward referenced functions */
45 void terminate_stored(int sig);
46 static int check_resources();
47 static void cleanup_old_files();
49 extern "C" void *device_initialization(void *arg);
51 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
53 /* Global variables exported */
54 char OK_msg[] = "3000 OK\n";
55 char TERM_msg[] = "3999 Terminate\n";
56 STORES *me = NULL; /* our Global resource */
58 bool forge_on = false; /* proceed inspite of I/O errors */
59 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
60 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
62 static bool test_config = false;
65 static uint32_t VolSessionId = 0;
66 uint32_t VolSessionTime;
67 char *configfile = NULL;
68 bool init_done = false;
69 static pthread_t server_tid;
70 static bool server_tid_valid = false;
72 /* Global static variables */
73 static bool foreground = 0;
74 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
75 static workq_t dird_workq; /* queue for processing connections */
76 static CONFIG *config;
83 "\n%sVersion: %s (%s)\n\n"
84 "Usage: bacula-sd [options] [-c config_file] [config_file]\n"
85 " -c <file> use <file> as configuration file\n"
86 " -d <nn>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
87 " -dt print timestamp in debug output\n"
89 " -f run in foreground (for debugging)\n"
90 " -g <group> set groupid to group\n"
91 " -m print kaboom output (for debugging)\n"
92 " -p proceed despite I/O errors\n"
93 " -s no signals (for debugging)\n"
94 " -t test - read config and exit\n"
95 " -u <user> userid to <user>\n"
96 " -v verbose user messages\n"
97 " -? print this message.\n"
98 "\n"), 2000, "", VERSION, BDATE);
102 /*********************************************************************
104 * Main Bacula Unix Storage Daemon
107 #if defined(HAVE_WIN32)
108 #define main BaculaMain
111 int main (int argc, char *argv[])
114 bool no_signals = false;
119 start_heap = sbrk(0);
120 setlocale(LC_ALL, "");
121 bindtextdomain("bacula", LOCALEDIR);
122 textdomain("bacula");
125 my_name_is(argc, argv, "bacula-sd");
126 init_msg(NULL, NULL);
127 daemon_start_time = time(NULL);
130 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
131 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
132 TAPE_BSIZE, B_DEV_BSIZE);
134 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
135 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
138 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?T")) != -1) {
140 case 'c': /* configuration file */
141 if (configfile != NULL) {
144 configfile = bstrdup(optarg);
147 case 'd': /* debug level */
148 if (*optarg == 't') {
149 dbg_timestamp = true;
152 /* We probably find a tag list -d 10,sql,bvfs */
153 if ((p = strchr(optarg, ',')) != NULL) {
156 debug_level = atoi(optarg);
157 if (debug_level <= 0) {
161 debug_parse_tags(p+1, &debug_level_tags);
170 case 'f': /* run in foreground */
174 case 'g': /* set group id */
178 case 'm': /* print kaboom output */
182 case 'p': /* proceed in spite of I/O errors */
186 case 's': /* no signals */
194 case 'u': /* set uid */
198 case 'v': /* verbose */
212 if (configfile != NULL) {
215 configfile = bstrdup(*argv);
223 daemon_start(); /* become daemon */
224 init_stack_dump(); /* pick up new pid */
228 init_signals(terminate_stored);
231 if (configfile == NULL) {
232 configfile = bstrdup(CONFIG_FILE);
235 config = new_config_parser();
236 parse_sd_config(config, configfile, M_ERROR_TERM);
238 if (init_crypto() != 0) {
239 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
242 if (!check_resources()) {
243 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
246 init_reservations_lock();
252 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
255 create_pid_file(me->pid_directory, "bacula-sd",
256 get_first_port_host_order(me->sdaddrs));
257 read_state_file(me->working_directory, "bacula-sd",
258 get_first_port_host_order(me->sdaddrs));
260 set_jcr_in_tsd(INVALID_JCR);
261 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
262 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
263 lmgr_init_thread(); /* initialize the lockmanager stack */
265 load_sd_plugins(me->plugin_directory);
267 drop(uid, gid, false);
271 /* Ensure that Volume Session Time and Id are both
272 * set and are both non-zero.
274 VolSessionTime = (uint32_t)daemon_start_time;
275 if (VolSessionTime == 0) { /* paranoid */
276 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
280 * Start the device allocation thread
282 create_volume_lists(); /* do before device_init */
283 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
285 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
288 start_watchdog(); /* start watchdog thread */
289 init_jcr_subsystem(); /* start JCR watchdogs etc. */
291 /* Single server used for Director and File daemon */
292 server_tid = pthread_self();
293 server_tid_valid = true;
294 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
295 &dird_workq, handle_connection_request);
296 exit(1); /* to keep compiler quiet */
299 /* Return a new Session Id */
300 uint32_t newVolSessionId()
311 /* Check Configuration file for necessary info */
312 static int check_resources()
317 me = (STORES *)GetNextRes(R_STORAGE, NULL);
319 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
324 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
325 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
329 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
330 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
334 if (GetNextRes(R_DEVICE, NULL) == NULL){
335 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
341 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
343 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
349 if (!me->working_directory) {
350 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
357 foreach_res(store, R_STORAGE) {
358 /* tls_require implies tls_enable */
359 if (store->tls_require) {
361 store->tls_enable = true;
363 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
369 tls_needed = store->tls_enable || store->tls_authenticate;
371 if (!store->tls_certfile && tls_needed) {
372 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
373 store->hdr.name, configfile);
377 if (!store->tls_keyfile && tls_needed) {
378 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
379 store->hdr.name, configfile);
383 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
384 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
385 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
386 " At least one CA certificate store is required"
387 " when using \"TLS Verify Peer\".\n"),
388 store->hdr.name, configfile);
392 /* If everything is well, attempt to initialize our per-resource TLS context */
393 if (OK && (tls_needed || store->tls_require)) {
394 /* Initialize TLS context:
395 * Args: CA certfile, CA certdir, Certfile, Keyfile,
396 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
397 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
398 store->tls_ca_certdir, store->tls_certfile,
399 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
400 store->tls_verify_peer);
402 if (!store->tls_ctx) {
403 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
404 store->hdr.name, configfile);
410 foreach_res(director, R_DIRECTOR) {
411 /* tls_require implies tls_enable */
412 if (director->tls_require) {
413 director->tls_enable = true;
416 tls_needed = director->tls_enable || director->tls_authenticate;
418 if (!director->tls_certfile && tls_needed) {
419 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
420 director->hdr.name, configfile);
424 if (!director->tls_keyfile && tls_needed) {
425 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
426 director->hdr.name, configfile);
430 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
431 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
432 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
433 " At least one CA certificate store is required"
434 " when using \"TLS Verify Peer\".\n"),
435 director->hdr.name, configfile);
439 /* If everything is well, attempt to initialize our per-resource TLS context */
440 if (OK && (tls_needed || director->tls_require)) {
441 /* Initialize TLS context:
442 * Args: CA certfile, CA certdir, Certfile, Keyfile,
443 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
444 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
445 director->tls_ca_certdir, director->tls_certfile,
446 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
447 director->tls_verify_peer);
449 if (!director->tls_ctx) {
450 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
451 director->hdr.name, configfile);
457 OK = init_autochangers();
461 close_msg(NULL); /* close temp message handler */
462 init_msg(NULL, me->messages); /* open daemon message handler */
463 set_working_directory(me->working_directory);
470 * Remove old .spool files written by me from the working directory.
472 static void cleanup_old_files()
475 struct dirent *entry, *result;
477 int my_name_len = strlen(my_name);
478 int len = strlen(me->working_directory);
479 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
480 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
483 const int nmatch = 30;
484 regmatch_t pmatch[nmatch];
487 /* Look for .spool files but don't allow spaces */
488 const char *pat1 = "^[^ ]+\\.spool$";
490 /* Setup working directory prefix */
491 pm_strcpy(basename, me->working_directory);
492 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
493 pm_strcat(basename, "/");
496 /* Compile regex expressions */
497 rc = regcomp(&preg1, pat1, REG_EXTENDED);
499 regerror(rc, &preg1, prbuf, sizeof(prbuf));
500 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
505 name_max = pathconf(".", _PC_NAME_MAX);
506 if (name_max < 1024) {
510 if (!(dp = opendir(me->working_directory))) {
512 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
513 me->working_directory, be.bstrerror());
517 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
519 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
522 /* Exclude any name with ., .., not my_name or containing a space */
523 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
524 strncmp(result->d_name, my_name, my_name_len) != 0) {
525 Dmsg1(500, "Skipped: %s\n", result->d_name);
529 /* Unlink files that match regex */
530 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
531 pm_strcpy(cleanup, basename);
532 pm_strcat(cleanup, result->d_name);
533 Dmsg1(500, "Unlink: %s\n", cleanup);
543 free_pool_memory(cleanup);
544 free_pool_memory(basename);
548 * Here we attempt to init and open each device. This is done
549 * once at startup in a separate thread.
552 void *device_initialization(void *arg)
561 pthread_detach(pthread_self());
562 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
563 new_plugins(jcr); /* instantiate plugins */
564 jcr->setJobType(JT_SYSTEM);
565 /* Initialize FD start condition variable */
566 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
569 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
572 foreach_res(device, R_DEVICE) {
573 Dmsg1(90, "calling init_dev %s\n", device->device_name);
574 dev = init_dev(NULL, device);
575 Dmsg1(10, "SD init done %s\n", device->device_name);
577 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
581 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
582 generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
584 if (device->cap_bits & CAP_ALWAYSOPEN) {
585 if (dev->is_autochanger()) {
586 /* If autochanger set slot in dev sturcture */
587 get_autochanger_loaded_slot(dcr);
589 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
590 if (generate_plugin_event(jcr, bsdEventDeviceOpen, dcr) != bRC_OK) {
591 Jmsg(jcr, M_FATAL, 0, _("generate_plugin_event(bsdEventDeviceOpen) Failed\n"));
595 if (!first_open_device(dcr)) {
596 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
597 Dmsg1(20, "Could not open device %s\n", dev->print_name());
598 generate_plugin_event(jcr, bsdEventDeviceClose, dcr);
604 /* If not always open, we don't know what is in the drive */
607 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
608 switch (read_dev_volume_label(dcr)) {
610 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
611 volume_unused(dcr); /* mark volume "released" */
614 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
626 Pmsg1(000, "free_dcr=%p\n", jcr->dcr);
639 /* Clean up and then exit */
640 void terminate_stored(int sig)
642 static bool in_here = false;
646 if (in_here) { /* prevent loops */
647 bmicrosleep(2, 0); /* yield */
651 debug_level = 0; /* turn off any debug */
654 if (sig == SIGTERM || sig == SIGINT) { /* normal shutdown request? or ^C */
656 * This is a normal shutdown request. We wiffle through
657 * all open jobs canceling them and trying to wake
658 * them up so that they will report back the correct
663 if (jcr->JobId == 0) {
665 continue; /* ignore console */
668 /* Make sure no device remains locked */
669 generate_plugin_event(jcr, bsdEventDeviceClose, jcr->dcr);
671 jcr->setJobStatus(JS_Canceled);
672 fd = jcr->file_bsock;
675 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
676 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
677 /* ***FIXME*** wiffle through all dcrs */
678 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
679 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
680 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
681 pthread_cond_broadcast(&wait_device_release);
683 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
684 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
685 pthread_cond_broadcast(&wait_device_release);
687 bmicrosleep(0, 50000);
691 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
695 write_state_file(me->working_directory,
696 "bacula-sd", get_first_port_host_order(me->sdaddrs));
697 delete_pid_file(me->pid_directory,
698 "bacula-sd", get_first_port_host_order(me->sdaddrs));
701 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
706 foreach_res(device, R_DEVICE) {
707 Dmsg1(10, "Term device %s\n", device->device_name);
709 device->dev->clear_volhdr();
713 Dmsg1(10, "No dev structure %s\n", device->device_name);
716 if (server_tid_valid) {
717 bnet_stop_thread_server(server_tid);
725 config->free_resources();
730 if (chk_dbglvl(10)) {
731 print_memory_pool_stats();
735 term_reservations_lock();
739 sm_dump(false); /* dump orphaned buffers */