2 * Second generation Storage daemon.
4 * It accepts a number of simple commands from the File daemon
5 * and acts on them. When a request to append data is made,
6 * it opens a data channel and accepts data from the
13 Copyright (C) 2000-2006 Kern Sibbald
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 version 2 as amended with additional clauses defined in the
18 file LICENSE in the main source directory.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 the file LICENSE for additional details.
30 /* Imported functions */
33 /* Forward referenced functions */
34 void terminate_stored(int sig);
35 static int check_resources();
36 static void cleanup_old_files();
38 extern "C" void *device_initialization(void *arg);
40 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
42 /* Global variables exported */
43 char OK_msg[] = "3000 OK\n";
44 char TERM_msg[] = "3999 Terminate\n";
45 STORES *me = NULL; /* our Global resource */
46 bool forge_on = false; /* proceed inspite of I/O errors */
47 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
48 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
51 static uint32_t VolSessionId = 0;
52 uint32_t VolSessionTime;
53 char *configfile = NULL;
54 bool init_done = false;
56 /* Global static variables */
57 static bool foreground = 0;
58 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
59 static workq_t dird_workq; /* queue for processing connections */
65 "Copyright (C) 2000-2005 Kern Sibbald.\n"
66 "\nVersion: %s (%s)\n\n"
67 "Usage: stored [options] [-c config_file] [config_file]\n"
68 " -c <file> use <file> as configuration file\n"
69 " -dnn set debug level to nn\n"
70 " -f run in foreground (for debugging)\n"
71 " -g <group> set groupid to group\n"
72 " -p proceed despite I/O errors\n"
73 " -s no signals (for debugging)\n"
74 " -t test - read config and exit\n"
75 " -u <user> userid to <user>\n"
76 " -v verbose user messages\n"
77 " -? print this message.\n"
78 "\n"), VERSION, BDATE);
82 /*********************************************************************
84 * Main Bacula Unix Storage Daemon
87 int main (int argc, char *argv[])
90 bool no_signals = false;
91 bool test_config = false;
96 setlocale(LC_ALL, "");
97 bindtextdomain("bacula", LOCALEDIR);
101 my_name_is(argc, argv, "bacula-sd");
102 init_msg(NULL, NULL);
103 daemon_start_time = time(NULL);
106 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
107 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
108 TAPE_BSIZE, B_DEV_BSIZE);
110 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
111 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
114 while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
116 case 'c': /* configuration file */
117 if (configfile != NULL) {
120 configfile = bstrdup(optarg);
123 case 'd': /* debug level */
124 debug_level = atoi(optarg);
125 if (debug_level <= 0) {
130 case 'f': /* run in foreground */
134 case 'g': /* set group id */
138 case 'p': /* proceed in spite of I/O errors */
142 case 's': /* no signals */
150 case 'u': /* set uid */
154 case 'v': /* verbose */
168 if (configfile != NULL) {
171 configfile = bstrdup(*argv);
179 init_signals(terminate_stored);
182 if (configfile == NULL) {
183 configfile = bstrdup(CONFIG_FILE);
186 parse_config(configfile);
188 if (init_crypto() != 0) {
189 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
192 if (!check_resources()) {
193 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
196 init_reservations_lock();
202 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
205 daemon_start(); /* become daemon */
206 init_stack_dump(); /* pick up new pid */
209 create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
210 read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
217 /* Ensure that Volume Session Time and Id are both
218 * set and are both non-zero.
220 VolSessionTime = (uint32_t)daemon_start_time;
221 if (VolSessionTime == 0) { /* paranoid */
222 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
225 init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
227 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
228 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
231 * Start the device allocation thread
233 create_volume_list(); /* do before device_init */
234 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
235 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
238 start_watchdog(); /* start watchdog thread */
239 init_jcr_subsystem(); /* start JCR watchdogs etc. */
241 /* Single server used for Director and File daemon */
242 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
243 &dird_workq, handle_connection_request);
244 exit(1); /* to keep compiler quiet */
247 /* Return a new Session Id */
248 uint32_t newVolSessionId()
259 /* Check Configuration file for necessary info */
260 static int check_resources()
265 me = (STORES *)GetNextRes(R_STORAGE, NULL);
267 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
272 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
273 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
277 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
278 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
282 if (GetNextRes(R_DEVICE, NULL) == NULL){
283 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
289 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
291 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
297 if (!me->working_directory) {
298 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
305 foreach_res(store, R_STORAGE) {
306 /* tls_require implies tls_enable */
307 if (store->tls_require) {
309 store->tls_enable = true;
311 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
317 if (!store->tls_certfile && store->tls_enable) {
318 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
319 store->hdr.name, configfile);
323 if (!store->tls_keyfile && store->tls_enable) {
324 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
325 store->hdr.name, configfile);
329 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && store->tls_enable && store->tls_verify_peer) {
330 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
331 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
332 " At least one CA certificate store is required"
333 " when using \"TLS Verify Peer\".\n"),
334 store->hdr.name, configfile);
338 /* If everything is well, attempt to initialize our per-resource TLS context */
339 if (OK && (store->tls_enable || store->tls_require)) {
340 /* Initialize TLS context:
341 * Args: CA certfile, CA certdir, Certfile, Keyfile,
342 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
343 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
344 store->tls_ca_certdir, store->tls_certfile,
345 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
346 store->tls_verify_peer);
348 if (!store->tls_ctx) {
349 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
350 store->hdr.name, configfile);
356 foreach_res(director, R_DIRECTOR) {
357 /* tls_require implies tls_enable */
358 if (director->tls_require) {
359 director->tls_enable = true;
362 if (!director->tls_certfile && director->tls_enable) {
363 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
364 director->hdr.name, configfile);
368 if (!director->tls_keyfile && director->tls_enable) {
369 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
370 director->hdr.name, configfile);
374 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
375 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
376 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
377 " At least one CA certificate store is required"
378 " when using \"TLS Verify Peer\".\n"),
379 director->hdr.name, configfile);
383 /* If everything is well, attempt to initialize our per-resource TLS context */
384 if (OK && (director->tls_enable || director->tls_require)) {
385 /* Initialize TLS context:
386 * Args: CA certfile, CA certdir, Certfile, Keyfile,
387 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
388 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
389 director->tls_ca_certdir, director->tls_certfile,
390 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
391 director->tls_verify_peer);
393 if (!director->tls_ctx) {
394 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
395 director->hdr.name, configfile);
401 OK = init_autochangers();
405 close_msg(NULL); /* close temp message handler */
406 init_msg(NULL, me->messages); /* open daemon message handler */
407 set_working_directory(me->working_directory);
413 static void cleanup_old_files()
415 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
416 int len = strlen(me->working_directory);
417 pm_strcpy(cleanup, "/bin/rm -f ");
418 pm_strcat(cleanup, me->working_directory);
419 if (len > 0 && me->working_directory[len-1] != '/') {
420 pm_strcat(cleanup, "/");
422 pm_strcat(cleanup, my_name);
423 pm_strcat(cleanup, "*.spool");
424 run_program(cleanup, 0, NULL);
425 free_pool_memory(cleanup);
430 * Here we attempt to init and open each device. This is done
431 * once at startup in a separate thread.
434 void *device_initialization(void *arg)
443 pthread_detach(pthread_self());
444 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
445 jcr->JobType = JT_SYSTEM;
446 /* Initialize FD start condition variable */
447 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
449 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
452 foreach_res(device, R_DEVICE) {
453 Dmsg1(90, "calling init_dev %s\n", device->device_name);
454 dev = init_dev(NULL, device);
455 Dmsg1(10, "SD init done %s\n", device->device_name);
457 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
461 jcr->dcr = dcr = new_dcr(jcr, dev);
462 if (dev->is_autochanger()) {
463 /* If autochanger set slot in dev sturcture */
464 get_autochanger_loaded_slot(dcr);
467 if (device->cap_bits & CAP_ALWAYSOPEN) {
468 Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
469 if (!first_open_device(dcr)) {
470 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
471 Dmsg1(20, "Could not open device %s\n", dev->print_name());
477 if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
478 switch (read_dev_volume_label(dcr)) {
480 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
483 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
497 /* Clean up and then exit */
498 void terminate_stored(int sig)
500 static bool in_here = false;
504 if (in_here) { /* prevent loops */
509 if (sig == SIGTERM) { /* normal shutdown request? */
511 * This is a normal shutdown request. We wiffle through
512 * all open jobs canceling them and trying to wake
513 * them up so that they will report back the correct
518 if (jcr->JobId == 0) {
520 continue; /* ignore console */
522 set_jcr_job_status(jcr, JS_Canceled);
523 fd = jcr->file_bsock;
525 fd->timed_out = true;
526 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
527 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
528 /* ***FIXME*** wiffle through all dcrs */
529 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->dev_blocked) {
530 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
531 pthread_cond_broadcast(&wait_device_release);
533 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->dev_blocked) {
534 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
535 pthread_cond_broadcast(&wait_device_release);
537 bmicrosleep(0, 50000);
541 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
544 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
545 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
547 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
549 foreach_res(device, R_DEVICE) {
550 Dmsg1(10, "Term device %s\n", device->device_name);
552 free_volume(device->dev);
556 Dmsg1(10, "No dev structure %s\n", device->device_name);
564 free_config_resources();
566 if (debug_level > 10) {
567 print_memory_pool_stats();
569 term_reservations_lock();
576 sm_dump(false); /* dump orphaned buffers */