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, 2001, 2002 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 */
49 /* This is our own global resource */
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53 static uint32_t VolSessionId = 0;
54 uint32_t VolSessionTime;
57 static int foreground = 0;
59 static workq_t dird_workq; /* queue for processing connections */
65 "\nVersion: " VERSION " (" DATE ")\n\n"
66 "Usage: stored [-s -f ] [-c config_file] [-d debug_level] [config_file]\n"
67 " -c <file> use <file> as configuration file\n"
68 " -dnn set debug level to nn\n"
69 " -f run in foreground (for debugging)\n"
70 " -s no signals (for debugging)\n"
71 " -t test - read config and exit\n"
72 " -? print this message.\n"
77 /*********************************************************************
79 * Main Bacula Unix Storage Daemon
82 int main (int argc, char *argv[])
85 int no_signals = FALSE;
86 int test_config = FALSE;
90 my_name_is(argc, argv, "stored");
92 daemon_start_time = time(NULL);
93 memset(&last_job, 0, sizeof(last_job));
96 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
97 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
98 TAPE_BSIZE, DEV_BSIZE);
100 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
101 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
104 while ((ch = getopt(argc, argv, "c:d:fst?")) != -1) {
106 case 'c': /* configuration file */
107 if (configfile != NULL) {
110 configfile = bstrdup(optarg);
113 case 'd': /* debug level */
114 debug_level = atoi(optarg);
115 if (debug_level <= 0) {
120 case 'f': /* run in foreground */
124 case 's': /* no signals */
142 if (configfile != NULL) {
145 configfile = bstrdup(*argv);
153 init_signals(terminate_stored);
157 if (configfile == NULL) {
158 configfile = bstrdup(CONFIG_FILE);
161 parse_config(configfile);
169 daemon_start(); /* become daemon */
170 init_stack_dump(); /* pick up new pid */
173 create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
175 /* Ensure that Volume Session Time and Id are both
176 * set and are both non-zero.
178 VolSessionTime = (long)daemon_start_time;
179 if (VolSessionTime == 0) { /* paranoid */
180 Emsg0(M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
183 /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
184 set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
187 * Here we lock the resources then fire off the device allocation
188 * thread. That thread will release the resources when all the
189 * devices are allocated. This allows use to start the server
190 * right away, but any jobs will wait until the resources are
194 if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
195 Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
200 start_watchdog(); /* start watchdog thread */
202 /* Single server used for Director and File daemon */
203 bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
204 &dird_workq, connection_request);
205 exit(1); /* to keep compiler quiet */
208 /* Return a new Session Id */
209 uint32_t newVolSessionId()
220 /* Check Configuration file for necessary info */
221 static void check_config()
223 struct stat stat_buf;
226 me = (STORES *)GetNextRes(R_STORAGE, NULL);
229 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
233 my_name_is(0, (char **)NULL, me->hdr.name); /* Set our real name */
235 if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
237 Emsg1(M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"),
240 if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
242 Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
245 if (GetNextRes(R_DEVICE, NULL) == NULL){
247 Emsg1(M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
251 me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
253 Emsg1(M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
257 close_msg(NULL); /* close temp message handler */
258 init_msg(NULL, me->messages); /* open daemon message handler */
262 if (!me->working_directory) {
263 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
266 if (stat(me->working_directory, &stat_buf) != 0) {
267 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
268 me->working_directory);
270 if (!S_ISDIR(stat_buf.st_mode)) {
271 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
272 me->working_directory);
274 working_directory = me->working_directory;
278 * We are started as a separate thread. The
279 * resources are alread locked.
281 static void *device_allocation(void *arg)
286 pthread_detach(pthread_self());
288 /* LockRes() alread done */
289 for (device=NULL,i=0; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); i++) {
290 Dmsg1(90, "calling init_dev %s\n", device->device_name);
291 device->dev = init_dev(NULL, device);
292 Dmsg1(10, "SD init done %s\n", device->device_name);
294 Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
296 if (device->cap_bits & CAP_ALWAYSOPEN) {
297 Dmsg1(20, "calling open_device %s\n", device->device_name);
298 if (!open_device(device->dev)) {
299 Emsg1(M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
302 if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
303 device->dev->state & ST_OPENED) {
306 block = new_block(device->dev);
307 jcr = new_jcr(sizeof(JCR), stored_free_jcr);
308 switch (read_dev_volume_label(jcr, device->dev, block)) {
312 Emsg1(M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
324 /* Clean up and then exit */
325 void terminate_stored(int sig)
327 static int in_here = FALSE;
330 if (in_here) { /* prevent loops */
335 delete_pid_file(me->pid_directory, "bacula-sd", me->SDport);
338 Dmsg0(200, "In terminate_stored()\n");
341 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
343 term_dev(device->dev);
350 free_config_resources();
352 if (debug_level > 10) {
353 print_memory_pool_stats();
358 sm_dump(False); /* dump orphaned buffers */