]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
Additional message plus exclusion fix
[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-2003 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 *device_allocation(void *arg);
42
43 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
44
45
46 /* Global variables exported */
47
48
49 /* This is our own global resource */
50 STORES *me;
51
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53 static uint32_t VolSessionId = 0;
54 uint32_t VolSessionTime;
55
56 char *configfile;
57 static int foreground = 0;
58
59 static workq_t dird_workq;            /* queue for processing connections */
60
61
62 static void usage()
63 {
64    fprintf(stderr, _(
65 "\nVersion: " VERSION " (" BDATE ")\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 "        -g          groupid\n"
71 "        -s          no signals (for debugging)\n"
72 "        -t          test - read config and exit\n"
73 "        -u          userid\n"
74 "        -?          print this message.\n"
75 "\n"));
76    exit(1);
77 }
78
79 /********************************************************************* 
80  *
81  *  Main Bacula Unix Storage Daemon
82  *
83  */
84 int main (int argc, char *argv[])
85 {
86    int ch;   
87    int no_signals = FALSE;
88    int test_config = FALSE;
89    pthread_t thid;
90    char *uid = NULL;
91    char *gid = NULL;
92
93    init_stack_dump();
94    my_name_is(argc, argv, "stored");
95    init_msg(NULL, NULL);
96    daemon_start_time = time(NULL);
97    memset(&last_job, 0, sizeof(last_job));
98
99    /* Sanity checks */
100    if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
101       Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
102          TAPE_BSIZE, DEV_BSIZE);
103    }
104    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
105       Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
106    }
107
108    while ((ch = getopt(argc, argv, "c:d:fg:stu:?")) != -1) {
109       switch (ch) {
110          case 'c':                    /* configuration file */
111             if (configfile != NULL) {
112                free(configfile);
113             }
114             configfile = bstrdup(optarg);
115             break;
116
117          case 'd':                    /* debug level */
118             debug_level = atoi(optarg);
119             if (debug_level <= 0) {
120                debug_level = 1; 
121             }
122             break;
123
124          case 'f':                    /* run in foreground */
125             foreground = TRUE;
126             break;
127
128          case 'g':                    /* set group id */
129             gid = optarg;
130             break;
131
132          case 's':                    /* no signals */
133             no_signals = TRUE;
134             break;
135
136          case 't':
137             test_config = TRUE;
138             break;
139
140          case 'u':                    /* set uid */
141             uid = optarg;
142             break;
143
144          case '?':
145          default:
146             usage();
147
148       }  
149    }
150    argc -= optind;
151    argv += optind;
152
153    if (argc) {
154       if (configfile != NULL) {
155          free(configfile);
156       }
157       configfile = bstrdup(*argv);
158       argc--; 
159       argv++;
160    }
161    if (argc)
162       usage();
163
164    if (!no_signals) {
165       init_signals(terminate_stored);
166    }
167
168
169    if (configfile == NULL) {
170       configfile = bstrdup(CONFIG_FILE);
171    }
172
173    parse_config(configfile);
174    check_config();
175
176    if (test_config) {
177       terminate_stored(0);
178    }
179
180    if (!foreground) {
181       daemon_start();                 /* become daemon */
182       init_stack_dump();              /* pick up new pid */
183    }
184
185    drop(uid, gid);
186
187    create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
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    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
198    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
199
200     /*
201      * Start the device allocation thread
202      */
203    if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
204       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
205    }
206
207    start_watchdog();                  /* start watchdog thread */
208
209    /* 
210     * Sleep a bit to give device thread a chance to lock the resource
211     * chain before we start the server.
212     */
213    sleep(1); 
214                                  
215    /* Single server used for Director and File daemon */
216    bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
217                       &dird_workq, connection_request);
218    exit(1);                           /* to keep compiler quiet */
219 }
220
221 /* Return a new Session Id */
222 uint32_t newVolSessionId()
223 {
224    uint32_t Id;
225
226    P(mutex);
227    VolSessionId++;
228    Id = VolSessionId;
229    V(mutex);
230    return Id;
231 }
232
233 /* Check Configuration file for necessary info */
234 static void check_config()
235 {
236    struct stat stat_buf; 
237
238    LockRes();
239    me = (STORES *)GetNextRes(R_STORAGE, NULL);
240    if (!me) {
241       UnlockRes();
242       Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
243          configfile);
244    }
245
246    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
247
248    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
249       UnlockRes();
250       Emsg1(M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"), 
251          configfile);
252    }
253    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
254       UnlockRes();
255       Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
256          configfile);
257    }
258    if (GetNextRes(R_DEVICE, NULL) == NULL){
259       UnlockRes();
260       Emsg1(M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
261            configfile);
262    }
263    if (!me->messages) {
264       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
265       if (!me->messages) {
266          Emsg1(M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
267             configfile);
268       }
269    }
270    close_msg(NULL);                   /* close temp message handler */
271    init_msg(NULL, me->messages);      /* open daemon message handler */
272
273    UnlockRes();
274
275    if (!me->working_directory) {
276       Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
277          configfile);
278    }
279    if (stat(me->working_directory, &stat_buf) != 0) {
280       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
281          me->working_directory);
282    }
283    if (!S_ISDIR(stat_buf.st_mode)) {
284       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
285          me->working_directory);
286    }
287    working_directory = me->working_directory;
288 }
289
290 /*
291  * We are started as a separate thread.  The
292  *  resources are alread locked.
293  */
294 static void *device_allocation(void *arg)
295 {
296    int i;
297    DEVRES *device;
298
299    LockRes();
300    pthread_detach(pthread_self());
301
302    for (device=NULL,i=0;  (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); i++) {
303       Dmsg1(90, "calling init_dev %s\n", device->device_name);
304       device->dev = init_dev(NULL, device);
305       Dmsg1(10, "SD init done %s\n", device->device_name);
306       if (!device->dev) {
307          Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
308       }
309       if (device->cap_bits & CAP_ALWAYSOPEN) {
310          Dmsg1(20, "calling open_device %s\n", device->device_name);
311          if (!open_device(device->dev)) {
312             Emsg1(M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
313          }
314       }
315       if (device->cap_bits & CAP_AUTOMOUNT && device->dev && 
316           device->dev->state & ST_OPENED) {
317          DEV_BLOCK *block;
318          JCR *jcr;
319          block = new_block(device->dev);
320          jcr = new_jcr(sizeof(JCR), stored_free_jcr);
321          switch (read_dev_volume_label(jcr, device->dev, block)) {
322             case VOL_OK:
323                break;
324             default:
325                Emsg1(M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
326                break;
327          }
328          free_jcr(jcr);
329          free_block(block);
330       }
331    } 
332    UnlockRes();
333    return NULL;
334 }
335
336
337 /* Clean up and then exit */
338 void terminate_stored(int sig)
339 {
340    static int in_here = FALSE;
341    DEVRES *device;
342
343    if (in_here) {                     /* prevent loops */
344       exit(1);
345    }
346    in_here = TRUE;
347
348    delete_pid_file(me->pid_directory, "bacula-sd", me->SDport);
349    stop_watchdog();
350
351    Dmsg0(200, "In terminate_stored()\n");
352
353    LockRes();
354    for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
355       if (device->dev) {
356          term_dev(device->dev);
357       }
358    } 
359    UnlockRes();
360
361    if (configfile)
362    free(configfile);
363    free_config_resources();
364
365    if (debug_level > 10) {
366       print_memory_pool_stats();
367    }
368    term_msg();
369    close_memory_pool();
370
371    sm_dump(False);                    /* dump orphaned buffers */
372    exit(1);
373 }