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-2005 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 as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
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 GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /* Imported functions */
38 /* Forward referenced functions */
39 void terminate_stored(int sig);
40 static int check_resources();
42 extern "C" void *device_initialization(void *arg);
44 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
46 /* Global variables exported */
47 char OK_msg[] = "3000 OK\n";
48 char TERM_msg[] = "3999 Terminate\n";
49 STORES *me = NULL; /* our Global resource */
50 bool forge_on = false; /* proceed inspite of I/O errors */
51 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
52 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
55 static uint32_t VolSessionId = 0;
56 uint32_t VolSessionTime;
57 char *configfile = NULL;
59 /* Global static variables */
60 static int foreground = 0;
61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
62 static workq_t dird_workq; /* queue for processing connections */
68 "Copyright (C) 2000-2005 Kern Sibbald.\n"
69 "\nVersion: " VERSION " (" BDATE ")\n\n"
70 "Usage: stored [options] [-c config_file] [config_file]\n"
71 " -c <file> use <file> as configuration file\n"
72 " -dnn set debug level to nn\n"
73 " -f run in foreground (for debugging)\n"
74 " -g <group> set groupid to group\n"
75 " -p proceed despite I/O errors\n"
76 " -s no signals (for debugging)\n"
77 " -t test - read config and exit\n"
78 " -u <user> userid to <user>\n"
79 " -v verbose user messages\n"
80 " -? print this message.\n"
85 /*********************************************************************
87 * Main Bacula Unix Storage Daemon
90 int main (int argc, char *argv[])
93 int no_signals = FALSE;
94 int test_config = FALSE;
100 my_name_is(argc, argv, "bacula-sd");
101 textdomain("bacula");
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_tls() != 0) {
189 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("TLS library initialization failed.\n"));
192 if (!check_resources()) {
193 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
200 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
203 daemon_start(); /* become daemon */
204 init_stack_dump(); /* pick up new pid */
207 create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
208 read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
212 /* Ensure that Volume Session Time and Id are both
213 * set and are both non-zero.
215 VolSessionTime = (long)daemon_start_time;
216 if (VolSessionTime == 0) { /* paranoid */
217 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
220 init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
222 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
223 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
226 * Start the device allocation thread
228 if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
229 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
232 start_watchdog(); /* start watchdog thread */
234 init_jcr_subsystem(); /* start JCR watchdogs etc. */
237 * Sleep a bit to give device thread a chance to lock the resource
238 * chain before we start the server.
242 /* Wait for device initialization to complete */
246 /* Single server used for Director and File daemon */
247 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
248 &dird_workq, handle_connection_request);
249 exit(1); /* to keep compiler quiet */
252 /* Return a new Session Id */
253 uint32_t newVolSessionId()
264 /* Check Configuration file for necessary info */
265 static int check_resources()
268 AUTOCHANGER *changer;
272 me = (STORES *)GetNextRes(R_STORAGE, NULL);
274 Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
279 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
280 Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
284 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
285 Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
289 if (GetNextRes(R_DEVICE, NULL) == NULL){
290 Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
296 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
298 Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
304 if (!me->working_directory) {
305 Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
313 foreach_res(store, R_STORAGE) {
314 /* tls_require implies tls_enable */
315 if (store->tls_require) {
316 store->tls_enable = true;
319 if (!store->tls_certfile && store->tls_enable) {
320 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
321 store->hdr.name, configfile);
325 if (!store->tls_keyfile && store->tls_enable) {
326 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
327 store->hdr.name, configfile);
331 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && store->tls_enable && store->tls_verify_peer) {
332 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
333 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
334 " At least one CA certificate store is required"
335 " when using \"TLS Verify Peer\".\n"),
336 store->hdr.name, configfile);
340 /* If everything is well, attempt to initialize our per-resource TLS context */
341 if (OK && (store->tls_enable || store->tls_require)) {
342 /* Initialize TLS context:
343 * Args: CA certfile, CA certdir, Certfile, Keyfile,
344 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
345 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
346 store->tls_ca_certdir, store->tls_certfile,
347 store->tls_keyfile, NULL, NULL, store->tls_dhfile,
348 store->tls_verify_peer);
350 if (!store->tls_ctx) {
351 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
352 store->hdr.name, configfile);
358 foreach_res(director, R_DIRECTOR) {
359 /* tls_require implies tls_enable */
360 if (director->tls_require) {
361 director->tls_enable = true;
364 if (!director->tls_certfile && director->tls_enable) {
365 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
366 director->hdr.name, configfile);
370 if (!director->tls_keyfile && director->tls_enable) {
371 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
372 director->hdr.name, configfile);
376 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
377 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
378 " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
379 " At least one CA certificate store is required"
380 " when using \"TLS Verify Peer\".\n"),
381 director->hdr.name, configfile);
385 /* If everything is well, attempt to initialize our per-resource TLS context */
386 if (OK && (director->tls_enable || director->tls_require)) {
387 /* Initialize TLS context:
388 * Args: CA certfile, CA certdir, Certfile, Keyfile,
389 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
390 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
391 director->tls_ca_certdir, director->tls_certfile,
392 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
393 director->tls_verify_peer);
395 if (!director->tls_ctx) {
396 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
397 director->hdr.name, configfile);
402 #endif /* HAVE_TLS */
404 /* Ensure that the media_type for each device is the same */
405 foreach_res(changer, R_AUTOCHANGER) {
407 char *media_type = NULL;
408 foreach_alist(device, changer->device) {
409 if (media_type == NULL) {
410 media_type = device->media_type;
413 if (strcmp(media_type, device->media_type) != 0) {
414 Jmsg(NULL, M_ERROR, 0,
415 _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
421 * If the device does not have a changer name or changer command
422 * defined, used the one from the Autochanger resource
424 if (!device->changer_name) {
425 device->changer_name = bstrdup(changer->changer_name);
427 if (!device->changer_command) {
428 device->changer_command = bstrdup(changer->changer_command);
436 close_msg(NULL); /* close temp message handler */
437 init_msg(NULL, me->messages); /* open daemon message handler */
438 set_working_directory(me->working_directory);
445 * Here we attempt to init and open each device. This is done
446 * once at startup in a separate thread.
449 void *device_initialization(void *arg)
454 pthread_detach(pthread_self());
456 foreach_res(device, R_DEVICE) {
457 Dmsg1(90, "calling init_dev %s\n", device->device_name);
458 device->dev = init_dev(NULL, NULL, device);
459 Dmsg1(10, "SD init done %s\n", device->device_name);
461 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
465 if (device->cap_bits & CAP_ALWAYSOPEN) {
466 Dmsg1(20, "calling first_open_device %s\n", device->device_name);
467 if (!first_open_device(device->dev)) {
468 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
471 if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
472 device->dev->is_open()) {
475 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
476 jcr->JobType = JT_SYSTEM;
477 /* Initialize FD start condition variable */
478 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
480 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
482 dcr = new_dcr(jcr, device->dev);
483 switch (read_dev_volume_label(dcr)) {
485 memcpy(&dcr->dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dcr->dev->VolCatInfo));
488 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
499 /* Clean up and then exit */
500 void terminate_stored(int sig)
502 static bool in_here = false;
506 if (in_here) { /* prevent loops */
511 if (sig == SIGTERM) { /* normal shutdown request? */
513 * This is a normal shutdown request. We wiffle through
514 * all open jobs canceling them and trying to wake
515 * them up so that they will report back the correct
521 free_locked_jcr(jcr);
522 if (jcr->JobId == 0) {
523 continue; /* ignore console */
525 set_jcr_job_status(jcr, JS_Canceled);
526 fd = jcr->file_bsock;
528 fd->timed_out = true;
529 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
530 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
531 /* ***FIXME*** wiffle through all dcrs */
532 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->dev_blocked) {
533 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
534 pthread_cond_broadcast(&wait_device_release);
536 bmicrosleep(0, 50000);
540 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
543 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
544 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
546 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
549 foreach_res(device, R_DEVICE) {
551 term_dev(device->dev);
558 free_config_resources();
560 if (debug_level > 10) {
561 print_memory_pool_stats();
568 sm_dump(false); /* dump orphaned buffers */