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