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 extern dlist *daemon_msg_queue;
44 extern pthread_mutex_t daemon_msg_queue_mutex;
47 /* Forward referenced functions */
48 void terminate_stored(int sig);
49 static int check_resources();
50 static void cleanup_old_files();
52 extern "C" void *device_initialization(void *arg);
54 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
56 /* Global variables exported */
57 char OK_msg[] = "3000 OK\n";
58 char TERM_msg[] = "3999 Terminate\n";
60 static bool test_config = false;
63 static uint32_t VolSessionId = 0;
64 uint32_t VolSessionTime;
65 char *configfile = NULL;
66 bool init_done = false;
67 static pthread_t server_tid;
68 static bool server_tid_valid = false;
70 /* Global static variables */
71 static bool foreground = 0;
72 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
73 static workq_t dird_workq; /* queue for processing connections */
74 static CONFIG *config;
81 "\n%sVersion: %s (%s)\n\n"
82 "Usage: bacula-sd [options] [-c config_file] [config_file]\n"
83 " -c <file> use <file> as configuration file\n"
84 " -d <nn>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
85 " -dt print timestamp in debug output\n"
87 " -f run in foreground (for debugging)\n"
88 " -g <group> set groupid to group\n"
89 " -m print kaboom output (for debugging)\n"
90 " -p proceed despite I/O errors\n"
91 " -s no signals (for debugging)\n"
92 " -t test - read config and exit\n"
93 " -u <user> userid to <user>\n"
94 " -v verbose user messages\n"
95 " -? print this message.\n"
96 "\n"), 2000, "", VERSION, BDATE);
101 * !!! WARNING !!! Use this function only when bacula is stopped.
102 * ie, after a fatal signal and before exiting the program
103 * Print information about a JCR
105 static void sd_debug_print(JCR *jcr, FILE *fp)
109 fprintf(fp, "\tdcr=%p volumename=%s dev=%p newvol=%d reserved=%d locked=%d\n",
110 dcr, dcr->VolumeName, dcr->dev, dcr->NewVol,
112 dcr->is_dev_locked());
114 fprintf(fp, "dcr=*None*\n");
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;
134 MQUEUE_ITEM *item = NULL;
136 start_heap = sbrk(0);
137 setlocale(LC_ALL, "");
138 bindtextdomain("bacula", LOCALEDIR);
139 textdomain("bacula");
142 my_name_is(argc, argv, "bacula-sd");
143 init_msg(NULL, NULL);
144 daemon_start_time = time(NULL);
145 /* Setup daemon message queue */
146 daemon_msg_queue = New(dlist(item, &item->link));
149 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
150 Jmsg2(NULL, M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
151 TAPE_BSIZE, B_DEV_BSIZE);
153 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
154 Jmsg1(NULL, M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
157 while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?Ti")) != -1) {
159 case 'c': /* configuration file */
160 if (configfile != NULL) {
163 configfile = bstrdup(optarg);
166 case 'd': /* debug level */
167 if (*optarg == 't') {
168 dbg_timestamp = true;
171 /* We probably find a tag list -d 10,sql,bvfs */
172 if ((p = strchr(optarg, ',')) != NULL) {
175 debug_level = atoi(optarg);
176 if (debug_level <= 0) {
180 debug_parse_tags(p+1, &debug_level_tags);
189 case 'f': /* run in foreground */
193 case 'g': /* set group id */
197 case 'm': /* print kaboom output */
201 case 'p': /* proceed in spite of I/O errors */
205 case 's': /* no signals */
213 case 'u': /* set uid */
217 case 'v': /* verbose */
221 /* Temp code to enable new match_bsr() code, not documented */
223 use_new_match_all = 1;
236 if (configfile != NULL) {
239 configfile = bstrdup(*argv);
247 if (!foreground && !test_config) {
248 daemon_start(); /* become daemon */
249 init_stack_dump(); /* pick up new pid */
253 init_signals(terminate_stored);
256 if (configfile == NULL) {
257 configfile = bstrdup(CONFIG_FILE);
260 config = New(CONFIG());
261 parse_sd_config(config, configfile, M_ERROR_TERM);
263 if (init_crypto() != 0) {
264 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
267 if (!check_resources()) {
268 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
271 init_reservations_lock();
277 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
280 create_pid_file(me->pid_directory, "bacula-sd",
281 get_first_port_host_order(me->sdaddrs));
282 read_state_file(me->working_directory, "bacula-sd",
283 get_first_port_host_order(me->sdaddrs));
285 set_jcr_in_tsd(INVALID_JCR);
286 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
287 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
288 lmgr_init_thread(); /* initialize the lockmanager stack */
290 load_sd_plugins(me->plugin_directory);
292 drop(uid, gid, false);
296 /* Ensure that Volume Session Time and Id are both
297 * set and are both non-zero.
299 VolSessionTime = (uint32_t)daemon_start_time;
300 if (VolSessionTime == 0) { /* paranoid */
301 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
305 * Start the device allocation thread
307 create_volume_lists(); /* do before device_init */
308 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
310 Jmsg1(NULL, M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
313 start_watchdog(); /* start watchdog thread */
314 init_jcr_subsystem(); /* start JCR watchdogs etc. */
315 dbg_jcr_add_hook(sd_debug_print); /* used to director variables */
317 /* Single server used for Director and File daemon */
318 server_tid = pthread_self();
319 server_tid_valid = true;
320 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
321 &dird_workq, handle_connection_request);
322 exit(1); /* to keep compiler quiet */
325 /* Return a new Session Id */
326 uint32_t newVolSessionId()
337 /* Check Configuration file for necessary info */
338 static int check_resources()
342 AUTOCHANGER *changer;
345 me = (STORES *)GetNextRes(R_STORAGE, NULL);
347 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
352 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
353 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
357 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
358 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
362 if (GetNextRes(R_DEVICE, NULL) == NULL){
363 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
369 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
371 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
377 if (!me->working_directory) {
378 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
385 foreach_res(store, R_STORAGE) {
386 /* tls_require implies tls_enable */
387 if (store->tls_require) {
389 store->tls_enable = true;
391 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
397 tls_needed = store->tls_enable || store->tls_authenticate;
399 if (!store->tls_certfile && tls_needed) {
400 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
401 store->hdr.name, configfile);
405 if (!store->tls_keyfile && tls_needed) {
406 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
407 store->hdr.name, configfile);
411 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
412 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
413 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
414 " At least one CA certificate store is required"
415 " when using \"TLS Verify Peer\".\n"),
416 store->hdr.name, configfile);
420 /* If everything is well, attempt to initialize our per-resource TLS context */
421 if (OK && (tls_needed || store->tls_require)) {
422 /* Initialize TLS context:
423 * Args: CA certfile, CA certdir, Certfile, Keyfile,
424 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
425 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
426 store->tls_ca_certdir, store->tls_certfile,
427 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
428 store->tls_verify_peer);
430 if (!store->tls_ctx) {
431 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
432 store->hdr.name, configfile);
438 foreach_res(director, R_DIRECTOR) {
439 /* tls_require implies tls_enable */
440 if (director->tls_require) {
441 director->tls_enable = true;
444 tls_needed = director->tls_enable || director->tls_authenticate;
446 if (!director->tls_certfile && tls_needed) {
447 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
448 director->hdr.name, configfile);
452 if (!director->tls_keyfile && tls_needed) {
453 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
454 director->hdr.name, configfile);
458 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
459 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
460 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
461 " At least one CA certificate store is required"
462 " when using \"TLS Verify Peer\".\n"),
463 director->hdr.name, configfile);
467 /* If everything is well, attempt to initialize our per-resource TLS context */
468 if (OK && (tls_needed || director->tls_require)) {
469 /* Initialize TLS context:
470 * Args: CA certfile, CA certdir, Certfile, Keyfile,
471 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
472 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
473 director->tls_ca_certdir, director->tls_certfile,
474 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
475 director->tls_verify_peer);
477 if (!director->tls_ctx) {
478 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
479 director->hdr.name, configfile);
485 foreach_res(changer, R_AUTOCHANGER) {
486 foreach_alist(device, changer->device) {
487 device->cap_bits |= CAP_AUTOCHANGER;
492 OK = init_autochangers();
496 close_msg(NULL); /* close temp message handler */
497 init_msg(NULL, me->messages); /* open daemon message handler */
498 set_working_directory(me->working_directory);
505 * Remove old .spool files written by me from the working directory.
507 static void cleanup_old_files()
510 struct dirent *entry, *result;
512 int my_name_len = strlen(my_name);
513 int len = strlen(me->working_directory);
514 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
515 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
518 const int nmatch = 30;
519 regmatch_t pmatch[nmatch];
522 /* Look for .spool files but don't allow spaces */
523 const char *pat1 = "^[^ ]+\\.spool$";
525 /* Setup working directory prefix */
526 pm_strcpy(basename, me->working_directory);
527 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
528 pm_strcat(basename, "/");
531 /* Compile regex expressions */
532 rc = regcomp(&preg1, pat1, REG_EXTENDED);
534 regerror(rc, &preg1, prbuf, sizeof(prbuf));
535 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
540 name_max = pathconf(".", _PC_NAME_MAX);
541 if (name_max < 1024) {
545 if (!(dp = opendir(me->working_directory))) {
547 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
548 me->working_directory, be.bstrerror());
552 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
554 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
557 /* Exclude any name with ., .., not my_name or containing a space */
558 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
559 strncmp(result->d_name, my_name, my_name_len) != 0) {
560 Dmsg1(500, "Skipped: %s\n", result->d_name);
564 /* Unlink files that match regex */
565 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
566 pm_strcpy(cleanup, basename);
567 pm_strcat(cleanup, result->d_name);
568 Dmsg1(500, "Unlink: %s\n", cleanup);
578 free_pool_memory(cleanup);
579 free_pool_memory(basename);
583 * Here we attempt to init and open each device. This is done
584 * once at startup in a separate thread.
587 void *device_initialization(void *arg)
597 pthread_detach(pthread_self());
598 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
599 new_plugins(jcr); /* instantiate plugins */
600 jcr->setJobType(JT_SYSTEM);
601 /* Initialize FD start condition variable */
602 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
605 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
608 foreach_res(device, R_DEVICE) {
609 Dmsg1(90, "calling init_dev %s\n", device->hdr.name);
610 dev = init_dev(NULL, device);
611 Dmsg1(10, "SD init done %s\n", device->hdr.name);
613 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize SD device \"%s\"\n"), device->hdr.name);
617 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
618 generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
620 if (device->control_name && stat(device->control_name, &statp) < 0) {
622 Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat ControlDevice %s: ERR=%s\n"),
623 device->control_name, be.bstrerror());
626 if ((device->lock_command && device->control_name) &&
627 !me->plugin_directory) {
628 Jmsg0(jcr, M_ERROR_TERM, 0, _("No plugin directory configured for SAN shared storage\n"));
633 * Note: be careful setting the slot here. If the drive
634 * is shared storage, the contents can change before
637 if (device->cap_bits & CAP_ALWAYSOPEN) {
638 if (dev->is_autochanger()) {
639 /* If autochanger set slot in dev sturcture */
640 get_autochanger_loaded_slot(dcr);
642 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
643 if (generate_plugin_event(jcr, bsdEventDeviceOpen, dcr) != bRC_OK) {
644 Jmsg(jcr, M_FATAL, 0, _("generate_plugin_event(bsdEventDeviceOpen) Failed\n"));
648 if (!first_open_device(dcr)) {
649 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
650 Dmsg1(20, "Could not open device %s\n", dev->print_name());
651 generate_plugin_event(jcr, bsdEventDeviceClose, dcr);
657 /* If not always open, we don't know what is in the drive */
660 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
661 switch (dev->read_dev_volume_label(dcr)) {
663 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
664 volume_unused(dcr); /* mark volume "released" */
667 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
679 Pmsg1(000, "free_dcr=%p\n", jcr->dcr);
692 /* Clean up and then exit */
693 void terminate_stored(int sig)
695 static bool in_here = false;
699 if (in_here) { /* prevent loops */
700 bmicrosleep(2, 0); /* yield */
704 debug_level = 0; /* turn off any debug */
707 if (sig == SIGTERM || sig == SIGINT) { /* normal shutdown request? or ^C */
709 * This is a normal shutdown request. We wiffle through
710 * all open jobs canceling them and trying to wake
711 * them up so that they will report back the correct
716 if (jcr->JobId == 0) {
718 continue; /* ignore console */
721 /* Make sure no device remains locked */
722 generate_plugin_event(jcr, bsdEventDeviceClose, jcr->dcr);
724 jcr->setJobStatus(JS_Canceled);
725 fd = jcr->file_bsock;
728 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
729 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
730 /* ***FIXME*** wiffle through all dcrs */
731 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
732 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
733 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
734 pthread_cond_broadcast(&wait_device_release);
736 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
737 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
738 pthread_cond_broadcast(&wait_device_release);
740 bmicrosleep(0, 50000);
744 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
748 write_state_file(me->working_directory,
749 "bacula-sd", get_first_port_host_order(me->sdaddrs));
750 delete_pid_file(me->pid_directory,
751 "bacula-sd", get_first_port_host_order(me->sdaddrs));
754 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
759 P(daemon_msg_queue_mutex);
760 daemon_msg_queue->destroy();
761 free(daemon_msg_queue);
762 V(daemon_msg_queue_mutex);
764 foreach_res(device, R_DEVICE) {
765 Dmsg2(10, "Term device %s %s\n", device->hdr.name, device->device_name);
767 device->dev->clear_volhdr();
768 device->dev->term(NULL);
771 Dmsg2(10, "No dev structure %s %s\n", device->hdr.name, device->device_name);
774 if (server_tid_valid) {
775 bnet_stop_thread_server(server_tid);
787 if (chk_dbglvl(10)) {
788 print_memory_pool_stats();
792 term_reservations_lock();
798 sm_dump(false); /* dump orphaned buffers */