]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
Simplify two messages in acquire.c
[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 char OK_msg[]   = "3000 OK\n";
48 char TERM_msg[] = "3999 Terminate\n";
49 STORES *me;                           /* our Global resource */
50
51 static uint32_t VolSessionId = 0;
52 uint32_t VolSessionTime;
53 char *configfile;
54
55 /* Global static variables */
56 static int foreground = 0;
57 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
58 static workq_t dird_workq;            /* queue for processing connections */
59
60 static void usage()
61 {
62    fprintf(stderr, _(
63 "\nVersion: " VERSION " (" BDATE ")\n\n"
64 "Usage: stored [-s -f ] [-c config_file] [-d debug_level]  [config_file]\n"
65 "        -c <file>   use <file> as configuration file\n"
66 "        -dnn        set debug level to nn\n"
67 "        -f          run in foreground (for debugging)\n"
68 "        -g          groupid\n"
69 "        -s          no signals (for debugging)\n"
70 "        -t          test - read config and exit\n"
71 "        -u          userid\n"
72 "        -v          verbose user messages\n"
73 "        -?          print this message.\n"
74 "\n"));
75    exit(1);
76 }
77
78 /********************************************************************* 
79  *
80  *  Main Bacula Unix Storage Daemon
81  *
82  */
83 int main (int argc, char *argv[])
84 {
85    int ch;   
86    int no_signals = FALSE;
87    int test_config = FALSE;
88    pthread_t thid;
89    char *uid = NULL;
90    char *gid = NULL;
91
92    init_stack_dump();
93    my_name_is(argc, argv, "bacula-sd");
94    textdomain("bacula-sd");
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:v?")) != -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 'v':                    /* verbose */
145             verbose++;
146             break;
147
148          case '?':
149          default:
150             usage();
151
152       }  
153    }
154    argc -= optind;
155    argv += optind;
156
157    if (argc) {
158       if (configfile != NULL) {
159          free(configfile);
160       }
161       configfile = bstrdup(*argv);
162       argc--; 
163       argv++;
164    }
165    if (argc)
166       usage();
167
168    if (!no_signals) {
169       init_signals(terminate_stored);
170    }
171
172
173    if (configfile == NULL) {
174       configfile = bstrdup(CONFIG_FILE);
175    }
176
177    parse_config(configfile);
178    check_config();
179
180    if (test_config) {
181       terminate_stored(0);
182    }
183
184    if (!foreground) {
185       daemon_start();                 /* become daemon */
186       init_stack_dump();              /* pick up new pid */
187    }
188
189    drop(uid, gid);
190
191    create_pid_file(me->pid_directory, "bacula-sd", me->SDport);
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    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
202    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
203
204     /*
205      * Start the device allocation thread
206      */
207    if (pthread_create(&thid, NULL, device_allocation, NULL) != 0) {
208       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
209    }
210
211    start_watchdog();                  /* start watchdog thread */
212
213    /* 
214     * Sleep a bit to give device thread a chance to lock the resource
215     * chain before we start the server.
216     */
217    bmicrosleep(1, 0);
218                                  
219    /* Single server used for Director and File daemon */
220    bnet_thread_server(me->SDaddr, me->SDport, me->max_concurrent_jobs * 2 + 1,
221                       &dird_workq, connection_request);
222    exit(1);                           /* to keep compiler quiet */
223 }
224
225 /* Return a new Session Id */
226 uint32_t newVolSessionId()
227 {
228    uint32_t Id;
229
230    P(mutex);
231    VolSessionId++;
232    Id = VolSessionId;
233    V(mutex);
234    return Id;
235 }
236
237 /* Check Configuration file for necessary info */
238 static void check_config()
239 {
240    struct stat stat_buf; 
241
242    LockRes();
243    me = (STORES *)GetNextRes(R_STORAGE, NULL);
244    if (!me) {
245       UnlockRes();
246       Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
247          configfile);
248    }
249
250    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
251
252    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
253       UnlockRes();
254       Emsg1(M_ERROR_TERM, 0, _("Only one Storage resource permitted in %s\n"), 
255          configfile);
256    }
257    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
258       UnlockRes();
259       Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s. Cannot continue.\n"),
260          configfile);
261    }
262    if (GetNextRes(R_DEVICE, NULL) == NULL){
263       UnlockRes();
264       Emsg1(M_ERROR_TERM, 0, _("No Device resource defined in %s. Cannot continue.\n"),
265            configfile);
266    }
267    if (!me->messages) {
268       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
269       if (!me->messages) {
270          Emsg1(M_ERROR_TERM, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
271             configfile);
272       }
273    }
274    close_msg(NULL);                   /* close temp message handler */
275    init_msg(NULL, me->messages);      /* open daemon message handler */
276
277    UnlockRes();
278
279    if (!me->working_directory) {
280       Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
281          configfile);
282    }
283    if (stat(me->working_directory, &stat_buf) != 0) {
284       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
285          me->working_directory);
286    }
287    if (!S_ISDIR(stat_buf.st_mode)) {
288       Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
289          me->working_directory);
290    }
291    working_directory = me->working_directory;
292 }
293
294 /*
295  * We are started as a separate thread.  The
296  *  resources are alread locked.
297  */
298 static void *device_allocation(void *arg)
299 {
300    int i;
301    DEVRES *device;
302
303    LockRes();
304    pthread_detach(pthread_self());
305
306    for (device=NULL,i=0;  (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); i++) {
307       Dmsg1(90, "calling init_dev %s\n", device->device_name);
308       device->dev = init_dev(NULL, device);
309       Dmsg1(10, "SD init done %s\n", device->device_name);
310       if (!device->dev) {
311          Emsg1(M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
312          continue;
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 }