2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 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 John Walker.
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
45 /* Imported functions */
48 /* Forward referenced functions */
49 void terminate_stored(int sig);
50 static int check_resources();
51 static void cleanup_old_files();
53 extern "C" void *device_initialization(void *arg);
55 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
57 /* Global variables exported */
58 char OK_msg[] = "3000 OK\n";
59 char TERM_msg[] = "3999 Terminate\n";
60 STORES *me = NULL; /* our Global resource */
61 bool forge_on = false; /* proceed inspite of I/O errors */
62 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
63 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
67 static uint32_t VolSessionId = 0;
68 uint32_t VolSessionTime;
69 char *configfile = NULL;
70 bool init_done = 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 */
82 "\nVersion: %s (%s)\n\n"
83 "Usage: stored [options] [-c config_file] [config_file]\n"
84 " -c <file> use <file> as configuration file\n"
85 " -dnn set debug level to nn\n"
86 " -f run in foreground (for debugging)\n"
87 " -g <group> set groupid to group\n"
88 " -p proceed despite I/O errors\n"
89 " -s no signals (for debugging)\n"
90 " -t test - read config and exit\n"
91 " -u <user> userid to <user>\n"
92 " -v verbose user messages\n"
93 " -? print this message.\n"
94 "\n"), 2000, VERSION, BDATE);
98 /*********************************************************************
100 * Main Bacula Unix Storage Daemon
103 #if defined(HAVE_WIN32)
104 #define main BaculaMain
107 int main (int argc, char *argv[])
110 bool no_signals = false;
111 bool test_config = false;
116 start_heap = sbrk(0);
117 setlocale(LC_ALL, "");
118 bindtextdomain("bacula", LOCALEDIR);
119 textdomain("bacula");
122 my_name_is(argc, argv, "bacula-sd");
123 init_msg(NULL, NULL);
124 daemon_start_time = time(NULL);
127 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
128 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
129 TAPE_BSIZE, B_DEV_BSIZE);
131 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
132 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
135 while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
137 case 'c': /* configuration file */
138 if (configfile != NULL) {
141 configfile = bstrdup(optarg);
144 case 'd': /* debug level */
145 debug_level = atoi(optarg);
146 if (debug_level <= 0) {
151 case 'f': /* run in foreground */
155 case 'g': /* set group id */
159 case 'p': /* proceed in spite of I/O errors */
163 case 's': /* no signals */
171 case 'u': /* set uid */
175 case 'v': /* verbose */
189 if (configfile != NULL) {
192 configfile = bstrdup(*argv);
200 init_signals(terminate_stored);
203 if (configfile == NULL) {
204 configfile = bstrdup(CONFIG_FILE);
207 parse_config(configfile);
209 if (init_crypto() != 0) {
210 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
213 if (!check_resources()) {
214 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
217 init_reservations_lock();
223 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
226 daemon_start(); /* become daemon */
227 init_stack_dump(); /* pick up new pid */
230 create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
231 read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
238 /* Ensure that Volume Session Time and Id are both
239 * set and are both non-zero.
241 VolSessionTime = (uint32_t)daemon_start_time;
242 if (VolSessionTime == 0) { /* paranoid */
243 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
246 init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
248 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
249 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
252 * Start the device allocation thread
254 create_volume_list(); /* do before device_init */
255 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
257 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
260 start_watchdog(); /* start watchdog thread */
261 init_jcr_subsystem(); /* start JCR watchdogs etc. */
263 /* Single server used for Director and File daemon */
264 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
265 &dird_workq, handle_connection_request);
266 exit(1); /* to keep compiler quiet */
269 /* Return a new Session Id */
270 uint32_t newVolSessionId()
281 /* Check Configuration file for necessary info */
282 static int check_resources()
287 me = (STORES *)GetNextRes(R_STORAGE, NULL);
289 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
294 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
295 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
299 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
300 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
304 if (GetNextRes(R_DEVICE, NULL) == NULL){
305 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
311 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
313 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
319 if (!me->working_directory) {
320 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
327 foreach_res(store, R_STORAGE) {
328 /* tls_require implies tls_enable */
329 if (store->tls_require) {
331 store->tls_enable = true;
333 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
339 if (!store->tls_certfile && store->tls_enable) {
340 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
341 store->hdr.name, configfile);
345 if (!store->tls_keyfile && store->tls_enable) {
346 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
347 store->hdr.name, configfile);
351 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && store->tls_enable && store->tls_verify_peer) {
352 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
353 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
354 " At least one CA certificate store is required"
355 " when using \"TLS Verify Peer\".\n"),
356 store->hdr.name, configfile);
360 /* If everything is well, attempt to initialize our per-resource TLS context */
361 if (OK && (store->tls_enable || store->tls_require)) {
362 /* Initialize TLS context:
363 * Args: CA certfile, CA certdir, Certfile, Keyfile,
364 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
365 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
366 store->tls_ca_certdir, store->tls_certfile,
367 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
368 store->tls_verify_peer);
370 if (!store->tls_ctx) {
371 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
372 store->hdr.name, configfile);
378 foreach_res(director, R_DIRECTOR) {
379 /* tls_require implies tls_enable */
380 if (director->tls_require) {
381 director->tls_enable = true;
384 if (!director->tls_certfile && director->tls_enable) {
385 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
386 director->hdr.name, configfile);
390 if (!director->tls_keyfile && director->tls_enable) {
391 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
392 director->hdr.name, configfile);
396 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
397 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
398 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
399 " At least one CA certificate store is required"
400 " when using \"TLS Verify Peer\".\n"),
401 director->hdr.name, configfile);
405 /* If everything is well, attempt to initialize our per-resource TLS context */
406 if (OK && (director->tls_enable || director->tls_require)) {
407 /* Initialize TLS context:
408 * Args: CA certfile, CA certdir, Certfile, Keyfile,
409 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
410 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
411 director->tls_ca_certdir, director->tls_certfile,
412 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
413 director->tls_verify_peer);
415 if (!director->tls_ctx) {
416 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
417 director->hdr.name, configfile);
423 OK = init_autochangers();
427 close_msg(NULL); /* close temp message handler */
428 init_msg(NULL, me->messages); /* open daemon message handler */
429 set_working_directory(me->working_directory);
435 static void cleanup_old_files()
437 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
438 int len = strlen(me->working_directory);
439 #if defined(HAVE_WIN32)
440 pm_strcpy(cleanup, "del /q ");
442 pm_strcpy(cleanup, "/bin/rm -f ");
444 pm_strcat(cleanup, me->working_directory);
445 if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
446 pm_strcat(cleanup, "/");
448 pm_strcat(cleanup, my_name);
449 pm_strcat(cleanup, "*.spool");
450 run_program(cleanup, 0, NULL);
451 free_pool_memory(cleanup);
456 * Here we attempt to init and open each device. This is done
457 * once at startup in a separate thread.
460 void *device_initialization(void *arg)
469 pthread_detach(pthread_self());
470 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
471 jcr->JobType = JT_SYSTEM;
472 /* Initialize FD start condition variable */
473 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
476 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
479 foreach_res(device, R_DEVICE) {
480 Dmsg1(90, "calling init_dev %s\n", device->device_name);
481 dev = init_dev(NULL, device);
482 Dmsg1(10, "SD init done %s\n", device->device_name);
484 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
488 jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
489 if (dev->is_autochanger()) {
490 /* If autochanger set slot in dev sturcture */
491 get_autochanger_loaded_slot(dcr);
494 if (device->cap_bits & CAP_ALWAYSOPEN) {
495 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
496 if (!first_open_device(dcr)) {
497 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
498 Dmsg1(20, "Could not open device %s\n", dev->print_name());
504 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
505 switch (read_dev_volume_label(dcr)) {
507 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
510 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
519 Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
531 /* Clean up and then exit */
532 void terminate_stored(int sig)
534 static bool in_here = false;
538 if (in_here) { /* prevent loops */
539 bmicrosleep(2, 0); /* yield */
545 if (sig == SIGTERM) { /* normal shutdown request? */
547 * This is a normal shutdown request. We wiffle through
548 * all open jobs canceling them and trying to wake
549 * them up so that they will report back the correct
554 if (jcr->JobId == 0) {
556 continue; /* ignore console */
558 set_jcr_job_status(jcr, JS_Canceled);
559 fd = jcr->file_bsock;
562 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
563 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
564 /* ***FIXME*** wiffle through all dcrs */
565 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
566 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
567 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
568 pthread_cond_broadcast(&wait_device_release);
570 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
571 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
572 pthread_cond_broadcast(&wait_device_release);
574 bmicrosleep(0, 50000);
578 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
581 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
582 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
584 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
586 foreach_res(device, R_DEVICE) {
587 Dmsg1(10, "Term device %s\n", device->device_name);
589 device->dev->clear_volhdr();
593 Dmsg1(10, "No dev structure %s\n", device->device_name);
601 free_config_resources();
603 if (debug_level > 10) {
604 print_memory_pool_stats();
609 term_reservations_lock();
612 sm_dump(false); /* dump orphaned buffers */