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 void check_config();
42 extern "C" void *device_allocation(void *arg);
46 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
49 /* Global variables exported */
50 char OK_msg[] = "3000 OK\n";
51 char TERM_msg[] = "3999 Terminate\n";
52 STORES *me = NULL; /* our Global resource */
53 bool forge_on = false; /* proceed inspite of I/O errors */
55 static uint32_t VolSessionId = 0;
56 uint32_t VolSessionTime;
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);
194 daemon_start(); /* become daemon */
195 init_stack_dump(); /* pick up new pid */
198 create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
199 read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
203 /* Ensure that Volume Session Time and Id are both
204 * set and are both non-zero.
206 VolSessionTime = (long)daemon_start_time;
207 if (VolSessionTime == 0) { /* paranoid */
208 Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
211 init_python_interpreter(me->hdr.name, me->scripts_directory ?
212 me->scripts_directory : ".");
214 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
215 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
218 * Start the device allocation thread
220 if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
221 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
224 start_watchdog(); /* start watchdog thread */
226 init_jcr_subsystem(); /* start JCR watchdogs etc. */
229 * Sleep a bit to give device thread a chance to lock the resource
230 * chain before we start the server.
234 /* Single server used for Director and File daemon */
235 bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
236 &dird_workq, handle_connection_request);
237 exit(1); /* to keep compiler quiet */
240 /* Return a new Session Id */
241 uint32_t newVolSessionId()
252 /* Check Configuration file for necessary info */
253 static void check_config()
255 AUTOCHANGER *changer;
258 me = (STORES *)GetNextRes(R_STORAGE, NULL);
261 Jmsg1(NULL, M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
264 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
266 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
268 Jmsg1(NULL, M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"),
271 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
273 Jmsg1(NULL, M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
276 if (GetNextRes(R_DEVICE, NULL) == NULL){
278 Jmsg1(NULL, M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
282 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
284 Jmsg1(NULL, M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
288 close_msg(NULL); /* close temp message handler */
289 init_msg(NULL, me->messages); /* open daemon message handler */
292 if (!me->working_directory) {
293 Jmsg1(NULL, M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
296 set_working_directory(me->working_directory);
298 /* Ensure that the media_type for each device is the same */
299 foreach_res(changer, R_AUTOCHANGER) {
301 char *media_type = NULL;
302 foreach_alist(device, changer->device) {
303 if (media_type == NULL) {
304 media_type = device->media_type;
307 if (strcmp(media_type, device->media_type) != 0) {
308 Jmsg(NULL, M_ERROR_TERM, 0,
309 _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
313 * If the device does not have a changer name or changer command
314 * defined, used the one from the Autochanger resource
316 if (!device->changer_name) {
317 device->changer_name = bstrdup(changer->changer_name);
319 if (!device->changer_command) {
320 device->changer_command = bstrdup(changer->changer_command);
328 * Here we attempt to init and open each device. This is done
329 * once at startup in a separate thread.
332 void *device_allocation(void *arg)
337 pthread_detach(pthread_self());
339 foreach_res(device, R_DEVICE) {
340 Dmsg1(90, "calling init_dev %s\n", device->device_name);
341 device->dev = init_dev(NULL, NULL, device);
342 Dmsg1(10, "SD init done %s\n", device->device_name);
344 Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
348 if (device->cap_bits & CAP_ALWAYSOPEN) {
349 Dmsg1(20, "calling first_open_device %s\n", device->device_name);
350 if (!first_open_device(device->dev)) {
351 Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
354 if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
355 device->dev->is_open()) {
358 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
359 jcr->JobType = JT_SYSTEM;
360 /* Initialize FD start condition variable */
361 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
363 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
365 dcr = new_dcr(jcr, device->dev);
366 switch (read_dev_volume_label(dcr)) {
368 memcpy(&dcr->dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dcr->dev->VolCatInfo));
371 Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
382 /* Clean up and then exit */
383 void terminate_stored(int sig)
385 static bool in_here = false;
389 if (in_here) { /* prevent loops */
394 if (sig == SIGTERM) { /* normal shutdown request? */
396 * This is a normal shutdown request. We wiffle through
397 * all open jobs canceling them and trying to wake
398 * them up so that they will report back the correct
404 free_locked_jcr(jcr);
405 if (jcr->JobId == 0) {
406 continue; /* ignore console */
408 set_jcr_job_status(jcr, JS_Canceled);
409 fd = jcr->file_bsock;
411 fd->timed_out = true;
412 Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
413 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
414 /* ***FIXME*** wiffle through all dcrs */
415 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->dev_blocked) {
416 pthread_cond_signal(&jcr->dcr->dev->wait_next_vol);
418 bmicrosleep(0, 50000);
422 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
425 write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
426 delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
428 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
431 foreach_res(device, R_DEVICE) {
433 term_dev(device->dev);
440 free_config_resources();
442 if (debug_level > 10) {
443 print_memory_pool_stats();
449 sm_dump(false); /* dump orphaned buffers */