2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 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 two of the GNU 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 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
47 #undef _POSIX_C_SOURCE
50 #include "lib/pythonlib.h"
52 /* Imported Functions */
53 extern PyObject *job_getattr(PyObject *self, char *attrname);
54 extern int job_setattr(PyObject *self, char *attrname, PyObject *value);
56 #endif /* HAVE_PYTHON */
58 /* Imported functions */
59 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
61 /* Forward referenced functions */
62 void terminate_stored(int sig);
63 static int check_resources();
64 static void cleanup_old_files();
66 extern "C" void *device_initialization(void *arg);
68 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
70 /* Global variables exported */
71 char OK_msg[] = "3000 OK\n";
72 char TERM_msg[] = "3999 Terminate\n";
73 STORES *me = NULL; /* our Global resource */
74 bool forge_on = false; /* proceed inspite of I/O errors */
75 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
76 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
80 static uint32_t VolSessionId = 0;
81 uint32_t VolSessionTime;
82 char *configfile = NULL;
83 bool init_done = false;
85 /* Global static variables */
86 static bool foreground = 0;
87 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
88 static workq_t dird_workq; /* queue for processing connections */
89 static CONFIG *config;
96 "\nVersion: %s (%s)\n\n"
97 "Usage: stored [options] [-c config_file] [config_file]\n"
98 " -c <file> use <file> as configuration file\n"
99 " -d <nn> set debug level to <nn>\n"
100 " -dt print timestamp in debug output\n"
101 " -f run in foreground (for debugging)\n"
102 " -g <group> set groupid to group\n"
103 " -p proceed despite I/O errors\n"
104 " -s no signals (for debugging)\n"
105 " -t test - read config and exit\n"
106 " -u <user> userid to <user>\n"
107 " -v verbose user messages\n"
108 " -? print this message.\n"
109 "\n"), 2000, VERSION, BDATE);
113 /*********************************************************************
115 * Main Bacula Unix Storage Daemon
118 #if defined(HAVE_WIN32)
119 #define main BaculaMain
122 int main (int argc, char *argv[])
125 bool no_signals = false;
126 bool test_config = false;
131 init_python_interpreter_args python_args;
132 #endif /* HAVE_PYTHON */
134 start_heap = sbrk(0);
135 setlocale(LC_ALL, "");
136 bindtextdomain("bacula", LOCALEDIR);
137 textdomain("bacula");
140 my_name_is(argc, argv, "bacula-sd");
141 init_msg(NULL, NULL);
142 daemon_start_time = time(NULL);
145 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
146 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
147 TAPE_BSIZE, B_DEV_BSIZE);
149 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
150 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
153 while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
155 case 'c': /* configuration file */
156 if (configfile != NULL) {
159 configfile = bstrdup(optarg);
162 case 'd': /* debug level */
163 if (*optarg == 't') {
164 dbg_timestamp = true;
166 debug_level = atoi(optarg);
167 if (debug_level <= 0) {
173 case 'f': /* run in foreground */
177 case 'g': /* set group id */
181 case 'p': /* proceed in spite of I/O errors */
185 case 's': /* no signals */
193 case 'u': /* set uid */
197 case 'v': /* verbose */
211 if (configfile != NULL) {
214 configfile = bstrdup(*argv);
222 init_signals(terminate_stored);
225 if (configfile == NULL) {
226 configfile = bstrdup(CONFIG_FILE);
229 config = new_config_parser();
230 parse_sd_config(config, configfile, M_ERROR_TERM);
232 if (init_crypto() != 0) {
233 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
236 if (!check_resources()) {
237 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
240 init_reservations_lock();
246 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
249 daemon_start(); /* become daemon */
250 init_stack_dump(); /* pick up new pid */
253 create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
254 read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
261 /* Ensure that Volume Session Time and Id are both
262 * set and are both non-zero.
264 VolSessionTime = (uint32_t)daemon_start_time;
265 if (VolSessionTime == 0) { /* paranoid */
266 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
270 python_args.progname = me->hdr.name;
271 python_args.scriptdir = me->scripts_directory;
272 python_args.modulename = "SDStartUp";
273 python_args.configfile = configfile;
274 python_args.workingdir = me->working_directory;
275 python_args.job_getattr = job_getattr;
276 python_args.job_setattr = job_setattr;
278 init_python_interpreter(&python_args);
279 #endif /* HAVE_PYTHON */
281 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
282 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
285 * Start the device allocation thread
287 create_volume_lists(); /* do before device_init */
288 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
290 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
293 start_watchdog(); /* start watchdog thread */
294 init_jcr_subsystem(); /* start JCR watchdogs etc. */
296 /* Single server used for Director and File daemon */
297 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
298 &dird_workq, handle_connection_request);
299 exit(1); /* to keep compiler quiet */
302 /* Return a new Session Id */
303 uint32_t newVolSessionId()
314 /* Check Configuration file for necessary info */
315 static int check_resources()
321 me = (STORES *)GetNextRes(R_STORAGE, NULL);
323 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
328 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
329 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
333 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
334 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
338 if (GetNextRes(R_DEVICE, NULL) == NULL){
339 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
345 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
347 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
353 if (!me->working_directory) {
354 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
361 foreach_res(store, R_STORAGE) {
362 /* tls_require implies tls_enable */
363 if (store->tls_require) {
365 store->tls_enable = true;
367 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
373 tls_needed = store->tls_enable || store->tls_authenticate;
375 if (!store->tls_certfile && tls_needed) {
376 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
377 store->hdr.name, configfile);
381 if (!store->tls_keyfile && tls_needed) {
382 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
383 store->hdr.name, configfile);
387 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
388 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
389 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
390 " At least one CA certificate store is required"
391 " when using \"TLS Verify Peer\".\n"),
392 store->hdr.name, configfile);
396 /* If everything is well, attempt to initialize our per-resource TLS context */
397 if (OK && (tls_needed || store->tls_require)) {
398 /* Initialize TLS context:
399 * Args: CA certfile, CA certdir, Certfile, Keyfile,
400 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
401 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
402 store->tls_ca_certdir, store->tls_certfile,
403 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
404 store->tls_verify_peer);
406 if (!store->tls_ctx) {
407 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
408 store->hdr.name, configfile);
414 foreach_res(director, R_DIRECTOR) {
415 /* tls_require implies tls_enable */
416 if (director->tls_require) {
417 director->tls_enable = true;
420 tls_needed = director->tls_enable || director->tls_authenticate;
422 if (!director->tls_certfile && tls_needed) {
423 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
424 director->hdr.name, configfile);
428 if (!director->tls_keyfile && tls_needed) {
429 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
430 director->hdr.name, configfile);
434 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
435 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
436 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
437 " At least one CA certificate store is required"
438 " when using \"TLS Verify Peer\".\n"),
439 director->hdr.name, configfile);
443 /* If everything is well, attempt to initialize our per-resource TLS context */
444 if (OK && (tls_needed || director->tls_require)) {
445 /* Initialize TLS context:
446 * Args: CA certfile, CA certdir, Certfile, Keyfile,
447 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
448 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
449 director->tls_ca_certdir, director->tls_certfile,
450 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
451 director->tls_verify_peer);
453 if (!director->tls_ctx) {
454 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
455 director->hdr.name, configfile);
461 OK = init_autochangers();
465 close_msg(NULL); /* close temp message handler */
466 init_msg(NULL, me->messages); /* open daemon message handler */
467 set_working_directory(me->working_directory);
473 static void cleanup_old_files()
475 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
476 POOLMEM *results = get_pool_memory(PM_MESSAGE);
477 int len = strlen(me->working_directory);
478 #if defined(HAVE_WIN32)
479 pm_strcpy(cleanup, "del /q ");
481 pm_strcpy(cleanup, "/bin/rm -f ");
483 pm_strcat(cleanup, me->working_directory);
484 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
485 pm_strcat(cleanup, "/");
487 pm_strcat(cleanup, my_name);
488 pm_strcat(cleanup, "*.spool");
489 run_program(cleanup, 0, results);
490 free_pool_memory(cleanup);
491 free_pool_memory(results);
496 * Here we attempt to init and open each device. This is done
497 * once at startup in a separate thread.
500 void *device_initialization(void *arg)
509 pthread_detach(pthread_self());
510 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
511 jcr->set_JobType(JT_SYSTEM);
512 /* Initialize FD start condition variable */
513 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
516 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
519 foreach_res(device, R_DEVICE) {
520 Dmsg1(90, "calling init_dev %s\n", device->device_name);
521 dev = init_dev(NULL, device);
522 Dmsg1(10, "SD init done %s\n", device->device_name);
524 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
528 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
529 if (dev->is_autochanger()) {
530 /* If autochanger set slot in dev sturcture */
531 get_autochanger_loaded_slot(dcr);
534 if (device->cap_bits & CAP_ALWAYSOPEN) {
535 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
536 if (!first_open_device(dcr)) {
537 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
538 Dmsg1(20, "Could not open device %s\n", dev->print_name());
544 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
545 switch (read_dev_volume_label(dcr)) {
547 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
548 volume_unused(dcr); /* mark volume "released" */
551 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
560 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
572 /* Clean up and then exit */
573 void terminate_stored(int sig)
575 static bool in_here = false;
579 if (in_here) { /* prevent loops */
580 bmicrosleep(2, 0); /* yield */
584 debug_level = 0; /* turn off any debug */
587 if (sig == SIGTERM) { /* normal shutdown request? */
589 * This is a normal shutdown request. We wiffle through
590 * all open jobs canceling them and trying to wake
591 * them up so that they will report back the correct
596 if (jcr->JobId == 0) {
598 continue; /* ignore console */
600 set_jcr_job_status(jcr, JS_Canceled);
601 fd = jcr->file_bsock;
604 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
605 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
606 /* ***FIXME*** wiffle through all dcrs */
607 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
608 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
609 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
610 pthread_cond_broadcast(&wait_device_release);
612 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
613 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
614 pthread_cond_broadcast(&wait_device_release);
616 bmicrosleep(0, 50000);
620 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
623 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
624 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
626 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
630 foreach_res(device, R_DEVICE) {
631 Dmsg1(10, "Term device %s\n", device->device_name);
633 device->dev->clear_volhdr();
637 Dmsg1(10, "No dev structure %s\n", device->device_name);
646 config->free_resources();
651 if (debug_level > 10) {
652 print_memory_pool_stats();
656 term_reservations_lock();
659 sm_dump(false); /* dump orphaned buffers */