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-2003 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();
41 static void *device_allocation(void *arg);
43 #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; /* our Global resource */
51 static uint32_t VolSessionId = 0;
52 uint32_t VolSessionTime;
55 /* Global static variables */
56 static int foreground = 0;
57 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
58 static workq_t dird_workq; /* queue for processing connections */
63 "\nVersion: " VERSION " (" BDATE ")\n\n"
64 "Usage: stored [-s -f ] [-c config_file] [-d debug_level] [config_file]\n"
65 " -c <file> use <file> as configuration file\n"
66 " -dnn set debug level to nn\n"
67 " -f run in foreground (for debugging)\n"
69 " -s no signals (for debugging)\n"
70 " -t test - read config and exit\n"
72 " -v verbose user messages\n"
73 " -? print this message.\n"
78 /*********************************************************************
80 * Main Bacula Unix Storage Daemon
83 int main (int argc, char *argv[])
86 int no_signals = FALSE;
87 int test_config = FALSE;
93 my_name_is(argc, argv, "bacula-sd");
94 textdomain("bacula-sd");
96 daemon_start_time = time(NULL);
99 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
100 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
101 TAPE_BSIZE, DEV_BSIZE);
103 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
104 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
107 while ((ch = getopt(argc, argv, "c:d:fg:stu:v?")) != -1) {
109 case 'c': /* configuration file */
110 if (configfile != NULL) {
113 configfile = bstrdup(optarg);
116 case 'd': /* debug level */
117 debug_level = atoi(optarg);
118 if (debug_level <= 0) {
123 case 'f': /* run in foreground */
127 case 'g': /* set group id */
131 case 's': /* no signals */
139 case 'u': /* set uid */
143 case 'v': /* verbose */
157 if (configfile != NULL) {
160 configfile = bstrdup(*argv);
168 init_signals(terminate_stored);
172 if (configfile == NULL) {
173 configfile = bstrdup(CONFIG_FILE);
176 parse_config(configfile);
184 daemon_start(); /* become daemon */
185 init_stack_dump(); /* pick up new pid */
190 create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
192 /* Ensure that Volume Session Time and Id are both
193 * set and are both non-zero.
195 VolSessionTime = (long)daemon_start_time;
196 if (VolSessionTime == 0) { /* paranoid */
197 Emsg0(M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
200 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
201 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
204 * Start the device allocation thread
206 if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
207 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
210 start_watchdog(); /* start watchdog thread */
213 * Sleep a bit to give device thread a chance to lock the resource
214 * chain before we start the server.
218 /* Single server used for Director and File daemon */
219 bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
220 &dird_workq, connection_request);
221 exit(1); /* to keep compiler quiet */
224 /* Return a new Session Id */
225 uint32_t newVolSessionId()
236 /* Check Configuration file for necessary info */
237 static void check_config()
240 me = (STORES *)GetNextRes(R_STORAGE, NULL);
243 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
247 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
249 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
251 Emsg1(M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"),
254 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
256 Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
259 if (GetNextRes(R_DEVICE, NULL) == NULL){
261 Emsg1(M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
265 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
267 Emsg1(M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
271 close_msg(NULL); /* close temp message handler */
272 init_msg(NULL, me->messages); /* open daemon message handler */
276 if (!me->working_directory) {
277 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
281 set_working_directory(me->working_directory);
285 * We are started as a separate thread. The
286 * resources are alread locked.
288 static void *device_allocation(void *arg)
294 pthread_detach(pthread_self());
296 for (device=NULL,i=0; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); i++) {
297 Dmsg1(90, "calling init_dev %s\n", device->device_name);
298 device->dev = init_dev(NULL, device);
299 Dmsg1(10, "SD init done %s\n", device->device_name);
301 Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
304 if (device->cap_bits & CAP_ALWAYSOPEN) {
305 Dmsg1(20, "calling open_device %s\n", device->device_name);
306 if (!open_device(device->dev)) {
307 Emsg1(M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
310 if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
311 device->dev->state & ST_OPENED) {
314 block = new_block(device->dev);
315 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
316 switch (read_dev_volume_label(jcr, device->dev, block)) {
320 Emsg1(M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
332 /* Clean up and then exit */
333 void terminate_stored(int sig)
335 static int in_here = FALSE;
339 if (in_here) { /* prevent loops */
344 if (sig == SIGTERM) { /* normal shutdown request? */
346 * This is a normal shutdown request. We wiffle through
347 * all open jobs canceling them and trying to wake
348 * them up so that they will report back the correct
352 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
354 free_locked_jcr(jcr);
355 if (jcr->JobId == 0) {
356 continue; /* ignore console */
358 set_jcr_job_status(jcr, JS_Canceled);
359 fd = jcr->file_bsock;
361 fd->timed_out = TRUE;
362 Dmsg1(100, "killing JobId=%d\n", jcr->JobId);
363 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
364 if (jcr->device && jcr->device->dev && jcr->device->dev->dev_blocked) {
365 pthread_cond_signal(&jcr->device->dev->wait_next_vol);
367 bmicrosleep(0, 50000);
371 bmicrosleep(0, 500000); /* give them 1/2 sec to clean up */
374 delete_pid_file(me->pid_directory, "bacula-sd", me->SDport);
377 Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
380 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
382 term_dev(device->dev);
389 free_config_resources();
391 if (debug_level > 10) {
392 print_memory_pool_stats();
397 sm_dump(False); /* dump orphaned buffers */