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