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-2004 Kern Sibbald and John Walker
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);
44 #define CONFIG_FILE "bacula-sd.conf" /* Default config file */
47 /* Global variables exported */
48 char OK_msg[] = "3000 OK\n";
49 char TERM_msg[] = "3999 Terminate\n";
50 STORES *me = NULL; /* our Global resource */
51 bool forge_on = false; /* proceed inspite of I/O errors */
53 static uint32_t VolSessionId = 0;
54 uint32_t VolSessionTime;
57 /* Global static variables */
58 static int foreground = 0;
59 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
60 static workq_t dird_workq; /* queue for processing connections */
66 "Copyright (C) 2000-2004 Kern Sibbald and John Walker.\n"
67 "\nVersion: " VERSION " (" BDATE ")\n\n"
68 "Usage: stored [options] [-c config_file] [config_file]\n"
69 " -c <file> use <file> as configuration file\n"
70 " -dnn set debug level to nn\n"
71 " -f run in foreground (for debugging)\n"
72 " -g <group> set groupid to group\n"
73 " -p proceed despite I/O errors\n"
74 " -s no signals (for debugging)\n"
75 " -t test - read config and exit\n"
76 " -u <user> userid to <user>\n"
77 " -v verbose user messages\n"
78 " -? print this message.\n"
83 /*********************************************************************
85 * Main Bacula Unix Storage Daemon
88 int main (int argc, char *argv[])
91 int no_signals = FALSE;
92 int test_config = FALSE;
98 my_name_is(argc, argv, "bacula-sd");
100 init_msg(NULL, NULL);
101 daemon_start_time = time(NULL);
104 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
105 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
106 TAPE_BSIZE, DEV_BSIZE);
108 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
109 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
112 while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
114 case 'c': /* configuration file */
115 if (configfile != NULL) {
118 configfile = bstrdup(optarg);
121 case 'd': /* debug level */
122 debug_level = atoi(optarg);
123 if (debug_level <= 0) {
128 case 'f': /* run in foreground */
132 case 'g': /* set group id */
136 case 'p': /* proceed in spite of I/O errors */
140 case 's': /* no signals */
148 case 'u': /* set uid */
152 case 'v': /* verbose */
166 if (configfile != NULL) {
169 configfile = bstrdup(*argv);
177 init_signals(terminate_stored);
181 if (configfile == NULL) {
182 configfile = bstrdup(CONFIG_FILE);
185 parse_config(configfile);
193 daemon_start(); /* become daemon */
194 init_stack_dump(); /* pick up new pid */
197 create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
198 read_state_file(me->working_directory, "bacula-sd", me->SDport);
202 /* Ensure that Volume Session Time and Id are both
203 * set and are both non-zero.
205 VolSessionTime = (long)daemon_start_time;
206 if (VolSessionTime == 0) { /* paranoid */
207 Emsg0(M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
210 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
211 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
214 * Start the device allocation thread
216 if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
217 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
220 start_watchdog(); /* start watchdog thread */
222 init_jcr_subsystem(); /* start JCR watchdogs etc. */
225 * Sleep a bit to give device thread a chance to lock the resource
226 * chain before we start the server.
230 /* Single server used for Director and File daemon */
231 bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
232 &dird_workq, handle_connection_request);
233 exit(1); /* to keep compiler quiet */
236 /* Return a new Session Id */
237 uint32_t newVolSessionId()
248 /* Check Configuration file for necessary info */
249 static void check_config()
252 me = (STORES *)GetNextRes(R_STORAGE, NULL);
255 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
259 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
261 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
263 Emsg1(M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"),
266 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
268 Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
271 if (GetNextRes(R_DEVICE, NULL) == NULL){
273 Emsg1(M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
277 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
279 Emsg1(M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
283 close_msg(NULL); /* close temp message handler */
284 init_msg(NULL, me->messages); /* open daemon message handler */
288 if (!me->working_directory) {
289 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
293 set_working_directory(me->working_directory);
297 * We are started as a separate thread. The
298 * resources are alread locked.
301 void *device_allocation(void *arg)
306 pthread_detach(pthread_self());
308 foreach_res(device, R_DEVICE) {
309 Dmsg1(90, "calling init_dev %s\n", device->device_name);
310 device->dev = init_dev(NULL, device);
311 Dmsg1(10, "SD init done %s\n", device->device_name);
313 Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
317 if (device->cap_bits & CAP_ALWAYSOPEN) {
318 Dmsg1(20, "calling first_open_device %s\n", device->device_name);
319 if (!first_open_device(device->dev)) {
320 Emsg1(M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
323 if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
324 device->dev->state & ST_OPENED) {
327 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
328 jcr->JobType = JT_SYSTEM;
329 /* Initialize FD start condition variable */
330 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
332 Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
334 jcr->device = device;
335 dcr = new_dcr(jcr, device->dev);
336 switch (read_dev_volume_label(dcr, dcr->block)) {
340 Emsg1(M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
351 /* Clean up and then exit */
352 void terminate_stored(int sig)
354 static int in_here = FALSE;
358 if (in_here) { /* prevent loops */
363 if (sig == SIGTERM) { /* normal shutdown request? */
365 * This is a normal shutdown request. We wiffle through
366 * all open jobs canceling them and trying to wake
367 * them up so that they will report back the correct
373 free_locked_jcr(jcr);
374 if (jcr->JobId == 0) {
375 continue; /* ignore console */
377 set_jcr_job_status(jcr, JS_Canceled);
378 fd = jcr->file_bsock;
380 fd->timed_out = TRUE;
381 Dmsg1(100, "killing JobId=%d\n", jcr->JobId);
382 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
383 if (jcr->device && jcr->device->dev && jcr->device->dev->dev_blocked) {
384 pthread_cond_signal(&jcr->device->dev->wait_next_vol);
386 bmicrosleep(0, 50000);
390 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
393 write_state_file(me->working_directory, "bacula-sd", me->SDport);
394 delete_pid_file(me->pid_directory, "bacula-sd", me->SDport);
396 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
399 foreach_res(device, R_DEVICE) {
401 term_dev(device->dev);
408 free_config_resources();
410 if (debug_level > 10) {
411 print_memory_pool_stats();
417 sm_dump(false); /* dump orphaned buffers */