]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
43d21450342e6d7fb363e8e2de2ec48c6b3926a5
[bacula/bacula] / bacula / src / stored / stored.c
1 /*
2  * Second generation Storage daemon.
3  *
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
7  * File daemon. 
8  *
9  *   Version $Id$
10  * 
11  */
12 /*
13    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
14
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.
19
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.
24
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,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "stored.h"
34
35 /* Imported functions */
36
37
38 /* Forward referenced functions */
39 void terminate_stored(int sig);
40 static void check_config();
41
42 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
43
44
45 /* Global variables exported */
46
47
48 struct s_shm *shm;                    /* memory shared with children */
49 BSHM bshm;                            /* shared memory control packet */
50
51
52 /* This is our own global resource */
53 STORES *me;
54
55 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56 static uint32_t VolSessionId = 0;
57 uint32_t VolSessionTime;
58
59 static char *configfile;
60 static int foreground = 0;
61
62 static workq_t dird_workq;            /* queue for processing connections */
63
64
65 static void usage()
66 {
67    fprintf(stderr, _(
68 "\nVersion: " VERSION " (" DATE ")\n\n"
69 "Usage: stored [-s -f ] [-c config_file] [-d debug_level]  [config_file]\n"
70 "        -c <file>   use <file> as configuration file\n"
71 "        -dnn        set debug level to nn\n"
72 "        -f          run in foreground (for debugging)\n"
73 "        -s          no signals (for debugging)\n"
74 "        -t          test - read config and exit\n"
75 "        -?          print this message.\n"
76 "\n"));
77    exit(1);
78 }
79
80 /********************************************************************* 
81  *
82  *  Main Bacula Unix Storage Daemon
83  *
84  */
85 int main (int argc, char *argv[])
86 {
87    int ch, i;
88    int no_signals = FALSE;
89    int test_config = FALSE;
90    DEVRES *device;
91
92    init_stack_dump();
93    my_name_is(argc, argv, "stored");
94    init_msg(NULL, NULL);
95    daemon_start_time = time(NULL);
96    memset(&last_job, 0, sizeof(last_job));
97
98    /* Sanity checks */
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);
102    }
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);
105    }
106
107    while ((ch = getopt(argc, argv, "c:d:fst?")) != -1) {
108       switch (ch) {
109          case 'c':                    /* configuration file */
110             if (configfile != NULL) {
111                free(configfile);
112             }
113             configfile = bstrdup(optarg);
114             break;
115
116          case 'd':                    /* debug level */
117             debug_level = atoi(optarg);
118             if (debug_level <= 0) {
119                debug_level = 1; 
120             }
121             break;
122
123          case 'f':                    /* run in foreground */
124             foreground = TRUE;
125             break;
126
127          case 's':                    /* no signals */
128             no_signals = TRUE;
129             break;
130
131          case 't':
132             test_config = TRUE;
133             break;
134
135          case '?':
136          default:
137             usage();
138
139       }  
140    }
141    argc -= optind;
142    argv += optind;
143
144    if (argc) {
145       if (configfile != NULL) {
146          free(configfile);
147       }
148       configfile = bstrdup(*argv);
149       argc--; 
150       argv++;
151    }
152    if (argc)
153       usage();
154
155    if (!no_signals) {
156       init_signals(terminate_stored);
157    }
158
159
160    if (configfile == NULL) {
161       configfile = bstrdup(CONFIG_FILE);
162    }
163
164    parse_config(configfile);
165    check_config();
166
167    bshm.size = 0;
168    if (test_config) {
169       terminate_stored(0);
170    }
171
172    if (!foreground) {
173       daemon_start();                 /* become daemon */
174       init_stack_dump();              /* pick up new pid */
175    }
176
177    create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
178
179    /*  ****FIXME**** clean this up */
180    /* Create and attach to shared memory. This is a
181     * hold over from the days of child processes. 
182     * Note, in reality all memory is shared. This
183     * is just a global buffer for the device packets.
184     */
185    shm = (s_shm *) malloc(sizeof(struct s_shm));
186    /* Zero shared memory */
187    memset(shm, 0, sizeof(struct s_shm));
188
189    /* Ensure that Volume Session Time and Id are both
190     * set and are both non-zero.
191     */
192    VolSessionTime = (long)daemon_start_time;
193    if (VolSessionTime == 0) { /* paranoid */
194       Emsg0(M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
195    }
196
197    LockRes();
198    for (device=NULL,i=0;  (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); i++) {
199       if (i >= MAX_DEVICES) {
200          UnlockRes();
201          Emsg1(M_ABORT, 0, _("Too many Device Resources. Max=%d\n"), MAX_DEVICES);
202       }
203       Dmsg1(90, "calling init_dev %s\n", device->device_name);
204       device->dev = init_dev(&shm->dev[i], device->device_name);
205       /* Copy some attributes from the Device Resource to the DEV structure */
206       if (device->dev) {
207          device->dev->capabilities = device->cap_bits;
208          device->dev->min_block_size = device->min_block_size;
209          device->dev->max_block_size = device->max_block_size;
210          device->dev->max_volume_jobs = device->max_volume_jobs;
211          device->dev->max_volume_files = device->max_volume_files;
212          device->dev->max_volume_size = device->max_volume_size;
213          device->dev->max_file_size = device->max_file_size;
214          device->dev->volume_capacity = device->volume_capacity;
215          device->dev->max_rewind_wait = device->max_rewind_wait;
216          device->dev->max_open_wait = device->max_open_wait;
217          device->dev->device = device;
218       }
219       Dmsg1(10, "SD init done %s\n", device->device_name);
220       if (!device->dev) {
221          Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
222       }
223       if (device->cap_bits & CAP_ALWAYSOPEN) {
224          Dmsg1(20, "calling open_device %s\n", device->device_name);
225          if (!open_device(device->dev)) {
226             Emsg1(M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
227          }
228       }
229       if (device->cap_bits & CAP_AUTOMOUNT && device->dev && 
230           device->dev->state & ST_OPENED) {
231          DEV_BLOCK *block;
232          JCR *jcr;
233          block = new_block(device->dev);
234          jcr = new_jcr(sizeof(JCR), stored_free_jcr);
235          switch (read_dev_volume_label(jcr, device->dev, block)) {
236             case VOL_OK:
237                break;
238             default:
239                Emsg1(M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
240                break;
241          }
242          free_jcr(jcr);
243          free_block(block);
244       }
245    } 
246    UnlockRes();
247    device = NULL;
248
249    set_thread_concurrency(me->max_concurrent_jobs * 2 +
250       4 /* watch dog + servers + misc */);
251
252    start_watchdog();                  /* start watchdog thread */
253
254    /* Single server used for Director and File daemon */
255    bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
256                       &dird_workq, connection_request);
257    exit(1);                           /* to keep compiler quiet */
258 }
259
260 /* Return a new Session Id */
261 uint32_t newVolSessionId()
262 {
263    uint32_t Id;
264
265    P(mutex);
266    VolSessionId++;
267    Id = VolSessionId;
268    V(mutex);
269    return Id;
270 }
271
272 /* Check Configuration file for necessary info */
273 static void check_config()
274 {
275    struct stat stat_buf; 
276
277    LockRes();
278    me = (STORES *)GetNextRes(R_STORAGE, NULL);
279    if (!me) {
280       UnlockRes();
281       Emsg1(M_ABORT, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
282          configfile);
283    }
284
285    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
286
287    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
288       UnlockRes();
289       Emsg1(M_ABORT, 0, _("Only one Storage resource permitted in %s\n"), 
290          configfile);
291    }
292    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
293       UnlockRes();
294       Emsg1(M_ABORT, 0, _("No Director resource defined in %s. Cannot continue.\n"),
295          configfile);
296    }
297    if (GetNextRes(R_DEVICE, NULL) == NULL){
298       UnlockRes();
299       Emsg1(M_ABORT, 0, _("No Device resource defined in %s. Cannot continue.\n"),
300            configfile);
301    }
302    if (!me->messages) {
303       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
304       if (!me->messages) {
305          Emsg1(M_ABORT, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
306             configfile);
307       }
308    }
309    close_msg(NULL);                   /* close temp message handler */
310    init_msg(NULL, me->messages);      /* open daemon message handler */
311
312    UnlockRes();
313
314    if (!me->working_directory) {
315       Emsg1(M_ABORT, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
316          configfile);
317    }
318    if (stat(me->working_directory, &stat_buf) != 0) {
319       Emsg1(M_ABORT, 0, _("Working Directory: %s not found. Cannot continue.\n"),
320          me->working_directory);
321    }
322    if (!S_ISDIR(stat_buf.st_mode)) {
323       Emsg1(M_ABORT, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
324          me->working_directory);
325    }
326    working_directory = me->working_directory;
327 }
328
329 /* Clean up and then exit */
330 void terminate_stored(int sig)
331 {
332    static int in_here = FALSE;
333    DEVRES *device;
334
335    if (in_here) {                     /* prevent loops */
336       exit(1);
337    }
338    in_here = TRUE;
339
340    delete_pid_file(me->pid_directory, "bacula-sd", me->SDport);
341    stop_watchdog();
342
343    Dmsg0(200, "In terminate_stored()\n");
344
345    LockRes();
346    for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
347       if (device->dev) {
348          term_dev(device->dev);
349       }
350    } 
351    UnlockRes();
352
353    if (configfile)
354    free(configfile);
355    free_config_resources();
356
357    if (debug_level > 10) {
358       print_memory_pool_stats();
359    }
360    term_msg();
361    close_memory_pool();
362
363    if (shm) {
364       free(shm);
365    }
366
367    sm_dump(False);                    /* dump orphaned buffers */
368    exit(1);
369 }