2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many 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 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Third generation Storage daemon.
22 * Written by Kern Sibbald, MM
24 * It accepts a number of simple commands from the File daemon
25 * and acts on them. When a request to append data is made,
26 * it opens a data channel and accepts data from the
34 /* TODO: fix problem with bls, bextract
35 * that use findlib and already declare
38 #include "sd_plugins.h"
40 /* Imported functions and variables */
41 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
43 /* Forward referenced functions */
44 void terminate_stored(int sig);
45 static int check_resources();
46 static void cleanup_old_files();
48 extern "C" void *device_initialization(void *arg);
50 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
52 /* Global variables exported */
53 char OK_msg[] = "3000 OK\n";
54 char TERM_msg[] = "3999 Terminate\n";
56 static bool test_config = false;
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 "\n%sVersion: %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>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
81 " -dt print timestamp in debug output\n"
83 " -f run in foreground (for debugging)\n"
84 " -g <group> set groupid to group\n"
85 " -m print kaboom output (for debugging)\n"
86 " -p proceed despite I/O errors\n"
87 " -s no signals (for debugging)\n"
88 " -t test - read config and exit\n"
89 " -u <user> userid to <user>\n"
90 " -v verbose user messages\n"
91 " -? print this message.\n"
92 "\n"), 2000, "", VERSION, BDATE);
97 * !!! WARNING !!! Use this function only when bacula is stopped.
98 * ie, after a fatal signal and before exiting the program
99 * Print information about a JCR
101 static void sd_debug_print(JCR *jcr, FILE *fp)
105 fprintf(fp, "\tdcr=%p volumename=%s dev=%p newvol=%d reserved=%d locked=%d\n",
106 dcr, dcr->VolumeName, dcr->dev, dcr->NewVol,
108 dcr->is_dev_locked());
110 fprintf(fp, "dcr=*None*\n");
114 /*********************************************************************
116 * Main Bacula Unix Storage Daemon
119 #if defined(HAVE_WIN32)
120 #define main BaculaMain
123 int main (int argc, char *argv[])
126 bool no_signals = false;
131 start_heap = sbrk(0);
132 setlocale(LC_ALL, "");
133 bindtextdomain("bacula", LOCALEDIR);
134 textdomain("bacula");
137 my_name_is(argc, argv, "bacula-sd");
138 init_msg(NULL, NULL);
139 daemon_start_time = time(NULL);
140 setup_daemon_message_queue();
143 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
144 Jmsg2(NULL, M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
145 TAPE_BSIZE, B_DEV_BSIZE);
147 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
148 Jmsg1(NULL, M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
151 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?Ti")) != -1) {
153 case 'c': /* configuration file */
154 if (configfile != NULL) {
157 configfile = bstrdup(optarg);
160 case 'd': /* debug level */
161 if (*optarg == 't') {
162 dbg_timestamp = true;
165 /* We probably find a tag list -d 10,sql,bvfs */
166 if ((p = strchr(optarg, ',')) != NULL) {
169 debug_level = atoi(optarg);
170 if (debug_level <= 0) {
174 debug_parse_tags(p+1, &debug_level_tags);
183 case 'f': /* run in foreground */
187 case 'g': /* set group id */
191 case 'm': /* print kaboom output */
195 case 'p': /* proceed in spite of I/O errors */
199 case 's': /* no signals */
207 case 'u': /* set uid */
211 case 'v': /* verbose */
215 /* Temp code to enable new match_bsr() code, not documented */
217 use_new_match_all = 1;
230 if (configfile != NULL) {
233 configfile = bstrdup(*argv);
241 if (!foreground && !test_config) {
242 daemon_start(); /* become daemon */
243 init_stack_dump(); /* pick up new pid */
247 init_signals(terminate_stored);
250 if (configfile == NULL) {
251 configfile = bstrdup(CONFIG_FILE);
254 config = New(CONFIG());
255 parse_sd_config(config, configfile, M_ERROR_TERM);
257 if (init_crypto() != 0) {
258 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
261 if (!check_resources()) {
262 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
265 init_reservations_lock();
271 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
274 create_pid_file(me->pid_directory, "bacula-sd",
275 get_first_port_host_order(me->sdaddrs));
276 read_state_file(me->working_directory, "bacula-sd",
277 get_first_port_host_order(me->sdaddrs));
279 set_jcr_in_tsd(INVALID_JCR);
280 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
281 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
282 lmgr_init_thread(); /* initialize the lockmanager stack */
284 load_sd_plugins(me->plugin_directory);
286 drop(uid, gid, false);
290 /* Ensure that Volume Session Time and Id are both
291 * set and are both non-zero.
293 VolSessionTime = (uint32_t)daemon_start_time;
294 if (VolSessionTime == 0) { /* paranoid */
295 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
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 Jmsg1(NULL, 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. */
309 dbg_jcr_add_hook(sd_debug_print); /* used to director variables */
311 /* Single server used for Director and File daemon */
312 server_tid = pthread_self();
313 server_tid_valid = true;
314 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
315 &dird_workq, handle_connection_request);
316 exit(1); /* to keep compiler quiet */
319 /* Return a new Session Id */
320 uint32_t newVolSessionId()
331 /* Check Configuration file for necessary info */
332 static int check_resources()
336 AUTOCHANGER *changer;
339 me = (STORES *)GetNextRes(R_STORAGE, NULL);
341 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
346 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
347 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
351 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
352 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
356 if (GetNextRes(R_DEVICE, NULL) == NULL){
357 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
363 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
365 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
371 if (!me->working_directory) {
372 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
379 foreach_res(store, R_STORAGE) {
380 /* tls_require implies tls_enable */
381 if (store->tls_require) {
383 store->tls_enable = true;
385 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
391 tls_needed = store->tls_enable || store->tls_authenticate;
393 if (!store->tls_certfile && tls_needed) {
394 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
395 store->hdr.name, configfile);
399 if (!store->tls_keyfile && tls_needed) {
400 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
401 store->hdr.name, configfile);
405 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
406 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
407 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
408 " At least one CA certificate store is required"
409 " when using \"TLS Verify Peer\".\n"),
410 store->hdr.name, configfile);
414 /* If everything is well, attempt to initialize our per-resource TLS context */
415 if (OK && (tls_needed || store->tls_require)) {
416 /* Initialize TLS context:
417 * Args: CA certfile, CA certdir, Certfile, Keyfile,
418 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
419 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
420 store->tls_ca_certdir, store->tls_certfile,
421 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
422 store->tls_verify_peer);
424 if (!store->tls_ctx) {
425 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
426 store->hdr.name, configfile);
432 foreach_res(director, R_DIRECTOR) {
433 /* tls_require implies tls_enable */
434 if (director->tls_require) {
435 director->tls_enable = true;
438 tls_needed = director->tls_enable || director->tls_authenticate;
440 if (!director->tls_certfile && tls_needed) {
441 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
442 director->hdr.name, configfile);
446 if (!director->tls_keyfile && tls_needed) {
447 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
448 director->hdr.name, configfile);
452 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
453 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
454 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
455 " At least one CA certificate store is required"
456 " when using \"TLS Verify Peer\".\n"),
457 director->hdr.name, configfile);
461 /* If everything is well, attempt to initialize our per-resource TLS context */
462 if (OK && (tls_needed || director->tls_require)) {
463 /* Initialize TLS context:
464 * Args: CA certfile, CA certdir, Certfile, Keyfile,
465 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
466 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
467 director->tls_ca_certdir, director->tls_certfile,
468 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
469 director->tls_verify_peer);
471 if (!director->tls_ctx) {
472 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
473 director->hdr.name, configfile);
479 foreach_res(changer, R_AUTOCHANGER) {
480 foreach_alist(device, changer->device) {
481 device->cap_bits |= CAP_AUTOCHANGER;
486 OK = init_autochangers();
490 close_msg(NULL); /* close temp message handler */
491 init_msg(NULL, me->messages); /* open daemon message handler */
492 set_working_directory(me->working_directory);
499 * Remove old .spool files written by me from the working directory.
501 static void cleanup_old_files()
504 struct dirent *entry, *result;
506 int my_name_len = strlen(my_name);
507 int len = strlen(me->working_directory);
508 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
509 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
512 const int nmatch = 30;
513 regmatch_t pmatch[nmatch];
516 /* Look for .spool files but don't allow spaces */
517 const char *pat1 = "^[^ ]+\\.spool$";
519 /* Setup working directory prefix */
520 pm_strcpy(basename, me->working_directory);
521 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
522 pm_strcat(basename, "/");
525 /* Compile regex expressions */
526 rc = regcomp(&preg1, pat1, REG_EXTENDED);
528 regerror(rc, &preg1, prbuf, sizeof(prbuf));
529 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
534 name_max = pathconf(".", _PC_NAME_MAX);
535 if (name_max < 1024) {
539 if (!(dp = opendir(me->working_directory))) {
541 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
542 me->working_directory, be.bstrerror());
546 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
548 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
551 /* Exclude any name with ., .., not my_name or containing a space */
552 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
553 strncmp(result->d_name, my_name, my_name_len) != 0) {
554 Dmsg1(500, "Skipped: %s\n", result->d_name);
558 /* Unlink files that match regex */
559 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
560 pm_strcpy(cleanup, basename);
561 pm_strcat(cleanup, result->d_name);
562 Dmsg1(500, "Unlink: %s\n", cleanup);
572 free_pool_memory(cleanup);
573 free_pool_memory(basename);
577 * Here we attempt to init and open each device. This is done
578 * once at startup in a separate thread.
581 void *device_initialization(void *arg)
591 pthread_detach(pthread_self());
592 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
593 new_plugins(jcr); /* instantiate plugins */
594 jcr->setJobType(JT_SYSTEM);
595 /* Initialize FD start condition variable */
596 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
599 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
602 foreach_res(device, R_DEVICE) {
603 Dmsg1(90, "calling init_dev %s\n", device->hdr.name);
604 dev = init_dev(NULL, device);
605 Dmsg1(10, "SD init done %s\n", device->hdr.name);
607 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize SD device \"%s\"\n"), device->hdr.name);
611 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
612 generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
614 if (device->control_name && stat(device->control_name, &statp) < 0) {
616 Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat ControlDevice %s: ERR=%s\n"),
617 device->control_name, be.bstrerror());
620 if ((device->lock_command && device->control_name) &&
621 !me->plugin_directory) {
622 Jmsg0(jcr, M_ERROR_TERM, 0, _("No plugin directory configured for SAN shared storage\n"));
627 * Note: be careful setting the slot here. If the drive
628 * is shared storage, the contents can change before
631 if (device->cap_bits & CAP_ALWAYSOPEN) {
632 if (dev->is_autochanger()) {
633 /* If autochanger set slot in dev sturcture */
634 get_autochanger_loaded_slot(dcr);
636 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
637 if (generate_plugin_event(jcr, bsdEventDeviceOpen, dcr) != bRC_OK) {
638 Jmsg(jcr, M_FATAL, 0, _("generate_plugin_event(bsdEventDeviceOpen) Failed\n"));
642 if (!first_open_device(dcr)) {
643 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
644 Dmsg1(20, "Could not open device %s\n", dev->print_name());
645 generate_plugin_event(jcr, bsdEventDeviceClose, dcr);
651 /* If not always open, we don't know what is in the drive */
654 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
655 switch (dev->read_dev_volume_label(dcr)) {
657 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
658 volume_unused(dcr); /* mark volume "released" */
661 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
673 Pmsg1(000, "free_dcr=%p\n", jcr->dcr);
686 /* Clean up and then exit */
687 void terminate_stored(int sig)
689 static bool in_here = false;
693 if (in_here) { /* prevent loops */
694 bmicrosleep(2, 0); /* yield */
698 debug_level = 0; /* turn off any debug */
701 if (sig == SIGTERM || sig == SIGINT) { /* normal shutdown request? or ^C */
703 * This is a normal shutdown request. We wiffle through
704 * all open jobs canceling them and trying to wake
705 * them up so that they will report back the correct
710 if (jcr->JobId == 0) {
712 continue; /* ignore console */
715 /* Make sure no device remains locked */
716 generate_plugin_event(jcr, bsdEventDeviceClose, jcr->dcr);
718 jcr->setJobStatus(JS_Canceled);
719 fd = jcr->file_bsock;
722 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
723 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
724 /* ***FIXME*** wiffle through all dcrs */
725 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
726 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
727 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
728 pthread_cond_broadcast(&wait_device_release);
730 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
731 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
732 pthread_cond_broadcast(&wait_device_release);
734 bmicrosleep(0, 50000);
738 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
742 write_state_file(me->working_directory,
743 "bacula-sd", get_first_port_host_order(me->sdaddrs));
744 delete_pid_file(me->pid_directory,
745 "bacula-sd", get_first_port_host_order(me->sdaddrs));
748 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
753 free_daemon_message_queue();
755 foreach_res(device, R_DEVICE) {
756 Dmsg2(10, "Term device %s %s\n", device->hdr.name, device->device_name);
758 device->dev->clear_volhdr();
759 device->dev->term(NULL);
762 Dmsg2(10, "No dev structure %s %s\n", device->hdr.name, device->device_name);
765 if (server_tid_valid) {
766 bnet_stop_thread_server(server_tid);
778 if (chk_dbglvl(10)) {
779 print_memory_pool_stats();
783 term_reservations_lock();
789 sm_dump(false); /* dump orphaned buffers */