]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
- Fix bug that missed drive=nn specification.
[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-2005 Kern Sibbald
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
17    version 2 as amended with additional clauses defined in the
18    file LICENSE in the main source directory.
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 
23    the file LICENSE for additional details.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 /* Imported functions */
31
32
33 /* Forward referenced functions */
34 void terminate_stored(int sig);
35 static int check_resources();
36 static void cleanup_old_files();
37
38 extern "C" void *device_initialization(void *arg);
39
40 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
41
42 /* Global variables exported */
43 char OK_msg[]   = "3000 OK\n";
44 char TERM_msg[] = "3999 Terminate\n";
45 STORES *me = NULL;                    /* our Global resource */
46 bool forge_on = false;                /* proceed inspite of I/O errors */
47 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
48 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
49
50
51 static uint32_t VolSessionId = 0;
52 uint32_t VolSessionTime;
53 char *configfile = NULL;
54 bool init_done = false;
55
56 /* Global static variables */
57 static int foreground = 0;
58 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
59 static workq_t dird_workq;            /* queue for processing connections */
60
61
62 static void usage()
63 {
64    fprintf(stderr, _(
65 "Copyright (C) 2000-2005 Kern Sibbald.\n"
66 "\nVersion: %s (%s)\n\n"
67 "Usage: stored [options] [-c config_file] [config_file]\n"
68 "        -c <file>   use <file> as configuration file\n"
69 "        -dnn        set debug level to nn\n"
70 "        -f          run in foreground (for debugging)\n"
71 "        -g <group>  set groupid to group\n"
72 "        -p          proceed despite I/O errors\n"
73 "        -s          no signals (for debugging)\n"
74 "        -t          test - read config and exit\n"
75 "        -u <user>   userid to <user>\n"
76 "        -v          verbose user messages\n"
77 "        -?          print this message.\n"
78 "\n"), VERSION, BDATE);
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;
90    int no_signals = FALSE;
91    int test_config = FALSE;
92    pthread_t thid;
93    char *uid = NULL;
94    char *gid = NULL;
95
96    setlocale(LC_ALL, "");
97    bindtextdomain("bacula", LOCALEDIR);
98    textdomain("bacula");
99
100    init_stack_dump();
101    my_name_is(argc, argv, "bacula-sd");
102    init_msg(NULL, NULL);
103    daemon_start_time = time(NULL);
104
105    /* Sanity checks */
106    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
107       Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
108          TAPE_BSIZE, B_DEV_BSIZE);
109    }
110    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
111       Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
112    }
113
114    while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
115       switch (ch) {
116       case 'c':                    /* configuration file */
117          if (configfile != NULL) {
118             free(configfile);
119          }
120          configfile = bstrdup(optarg);
121          break;
122
123       case 'd':                    /* debug level */
124          debug_level = atoi(optarg);
125          if (debug_level <= 0) {
126             debug_level = 1;
127          }
128          break;
129
130       case 'f':                    /* run in foreground */
131          foreground = TRUE;
132          break;
133
134       case 'g':                    /* set group id */
135          gid = optarg;
136          break;
137
138       case 'p':                    /* proceed in spite of I/O errors */
139          forge_on = true;
140          break;
141
142       case 's':                    /* no signals */
143          no_signals = TRUE;
144          break;
145
146       case 't':
147          test_config = TRUE;
148          break;
149
150       case 'u':                    /* set uid */
151          uid = optarg;
152          break;
153
154       case 'v':                    /* verbose */
155          verbose++;
156          break;
157
158       case '?':
159       default:
160          usage();
161          break;
162       }
163    }
164    argc -= optind;
165    argv += optind;
166
167    if (argc) {
168       if (configfile != NULL) {
169          free(configfile);
170       }
171       configfile = bstrdup(*argv);
172       argc--;
173       argv++;
174    }
175    if (argc)
176       usage();
177
178    if (!no_signals) {
179       init_signals(terminate_stored);
180    }
181
182    if (configfile == NULL) {
183       configfile = bstrdup(CONFIG_FILE);
184    }
185
186    parse_config(configfile);
187
188    if (init_tls() != 0) {
189       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("TLS library initialization failed.\n"));
190    }
191
192    if (!check_resources()) {
193       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
194    }
195
196    if (test_config) {
197       terminate_stored(0);
198    }
199
200    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
201
202    if (!foreground) {
203       daemon_start();                 /* become daemon */
204       init_stack_dump();              /* pick up new pid */
205    }
206
207    create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
208    read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
209
210    drop(uid, gid);
211
212    cleanup_old_files();
213
214
215    /* Ensure that Volume Session Time and Id are both
216     * set and are both non-zero.
217     */
218    VolSessionTime = (long)daemon_start_time;
219    if (VolSessionTime == 0) { /* paranoid */
220       Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
221    }
222
223    init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
224
225    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
226    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
227
228     /*
229      * Start the device allocation thread
230      */
231    create_volume_list();              /* do before device_init */
232    if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
233       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
234    }
235
236    start_watchdog();                  /* start watchdog thread */
237    init_jcr_subsystem();              /* start JCR watchdogs etc. */
238
239    /* Single server used for Director and File daemon */
240    bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
241                       &dird_workq, handle_connection_request);
242    exit(1);                           /* to keep compiler quiet */
243 }
244
245 /* Return a new Session Id */
246 uint32_t newVolSessionId()
247 {
248    uint32_t Id;
249
250    P(mutex);
251    VolSessionId++;
252    Id = VolSessionId;
253    V(mutex);
254    return Id;
255 }
256
257 /* Check Configuration file for necessary info */
258 static int check_resources()
259 {
260    bool OK = true;
261    AUTOCHANGER *changer;
262
263
264    me = (STORES *)GetNextRes(R_STORAGE, NULL);
265    if (!me) {
266       Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
267          configfile);
268       OK = false;
269    }
270
271    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
272       Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
273          configfile);
274       OK = false;
275    }
276    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
277       Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
278          configfile);
279       OK = false;
280    }
281    if (GetNextRes(R_DEVICE, NULL) == NULL){
282       Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
283            configfile);
284       OK = false;
285    }
286
287    if (!me->messages) {
288       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
289       if (!me->messages) {
290          Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
291             configfile);
292          OK = false;
293       }
294    }
295
296    if (!me->working_directory) {
297       Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
298          configfile);
299       OK = false;
300    }
301
302    DIRRES *director;
303    STORES *store;
304    foreach_res(store, R_STORAGE) { 
305       /* tls_require implies tls_enable */
306       if (store->tls_require) {
307          if (have_tls) {
308             store->tls_enable = true;
309          } else {
310             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
311             OK = false;
312             continue;
313          }
314       }
315
316       if (!store->tls_certfile && store->tls_enable) {
317          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
318               store->hdr.name, configfile);
319          OK = false;
320       }
321
322       if (!store->tls_keyfile && store->tls_enable) {
323          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
324               store->hdr.name, configfile);
325          OK = false;
326       }
327
328       if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && store->tls_enable && store->tls_verify_peer) {
329          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
330               " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
331               " At least one CA certificate store is required"
332               " when using \"TLS Verify Peer\".\n"),
333               store->hdr.name, configfile);
334          OK = false;
335       }
336
337       /* If everything is well, attempt to initialize our per-resource TLS context */
338       if (OK && (store->tls_enable || store->tls_require)) {
339          /* Initialize TLS context:
340           * Args: CA certfile, CA certdir, Certfile, Keyfile,
341           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
342          store->tls_ctx = new_tls_context(store->tls_ca_certfile,
343             store->tls_ca_certdir, store->tls_certfile,
344             store->tls_keyfile, NULL, NULL, store->tls_dhfile,
345             store->tls_verify_peer);
346
347          if (!store->tls_ctx) { 
348             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
349                  store->hdr.name, configfile);
350             OK = false;
351          }
352       }
353    }
354
355    foreach_res(director, R_DIRECTOR) { 
356       /* tls_require implies tls_enable */
357       if (director->tls_require) {
358          director->tls_enable = true;
359       }
360
361       if (!director->tls_certfile && director->tls_enable) {
362          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
363               director->hdr.name, configfile);
364          OK = false;
365       }
366
367       if (!director->tls_keyfile && director->tls_enable) {
368          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
369               director->hdr.name, configfile);
370          OK = false;
371       }
372
373       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
374          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
375               " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
376               " At least one CA certificate store is required"
377               " when using \"TLS Verify Peer\".\n"),
378               director->hdr.name, configfile);
379          OK = false;
380       }
381
382       /* If everything is well, attempt to initialize our per-resource TLS context */
383       if (OK && (director->tls_enable || director->tls_require)) {
384          /* Initialize TLS context:
385           * Args: CA certfile, CA certdir, Certfile, Keyfile,
386           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
387          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
388             director->tls_ca_certdir, director->tls_certfile,
389             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
390             director->tls_verify_peer);
391
392          if (!director->tls_ctx) { 
393             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
394                  director->hdr.name, configfile);
395             OK = false;
396          }
397       }
398    }
399
400    /* Ensure that the media_type for each device is the same */
401    foreach_res(changer, R_AUTOCHANGER) {
402       DEVRES *device;
403       char *media_type = NULL;
404       foreach_alist(device, changer->device) {
405          /*
406           * If the device does not have a changer name or changer command
407           *   defined, used the one from the Autochanger resource 
408           */
409          if (!device->changer_name && changer->changer_name) {
410             device->changer_name = bstrdup(changer->changer_name);
411          }
412          if (!device->changer_command && changer->changer_command) {
413             device->changer_command = bstrdup(changer->changer_command);
414          }
415          if (!device->changer_name) {
416             Jmsg(NULL, M_ERROR, 0, 
417                _("No Changer Name given for device %s. Cannot continue.\n"),
418                device->hdr.name);
419             OK = false;
420          }   
421          if (!device->changer_command) {
422             Jmsg(NULL, M_ERROR, 0, 
423                _("No Changer Command given for device %s. Cannot continue.\n"),
424                device->hdr.name);
425             OK = false;
426          }   
427
428          if (media_type == NULL) {
429             media_type = device->media_type;     /* get Media Type of first device */
430             continue;
431          }     
432          /* Ensure that other devices Media Types are the same */
433          if (strcmp(media_type, device->media_type) != 0) {
434             Jmsg(NULL, M_ERROR, 0, 
435                _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
436                changer->hdr.name);
437             OK = false;
438             continue;
439          }
440       }
441    }
442    
443    if (OK) {
444       close_msg(NULL);                   /* close temp message handler */
445       init_msg(NULL, me->messages);      /* open daemon message handler */
446       set_working_directory(me->working_directory);
447    }
448
449    return OK;
450 }
451
452 static void cleanup_old_files()
453 {
454    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
455    int len = strlen(me->working_directory);
456    pm_strcpy(cleanup, "/bin/rm -f ");
457    pm_strcat(cleanup, me->working_directory);
458    if (len > 0 && me->working_directory[len-1] != '/') {
459       pm_strcat(cleanup, "/");
460    }
461    pm_strcat(cleanup, my_name);
462    pm_strcat(cleanup, "*.spool");
463    run_program(cleanup, 0, NULL);
464    free_pool_memory(cleanup);
465 }      
466
467
468 /*
469  * Here we attempt to init and open each device. This is done
470  *  once at startup in a separate thread.
471  */
472 extern "C"
473 void *device_initialization(void *arg)
474 {
475    DEVRES *device;
476    DCR *dcr;
477    JCR *jcr;
478    DEVICE *dev;
479
480    LockRes();
481
482    pthread_detach(pthread_self());
483    jcr = new_jcr(sizeof(JCR), stored_free_jcr);
484    jcr->JobType = JT_SYSTEM;
485    /* Initialize FD start condition variable */
486    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
487    if (errstat != 0) {
488       Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
489    }
490
491    foreach_res(device, R_DEVICE) {
492       Dmsg1(90, "calling init_dev %s\n", device->device_name);
493       device->dev = dev = init_dev(NULL, device);
494       Dmsg1(10, "SD init done %s\n", device->device_name);
495       if (!dev) {
496          Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
497          continue;
498       }
499
500       dcr = new_dcr(jcr, dev);
501       if (dev->is_autochanger()) {
502          /* If autochanger set slot in dev sturcture */
503          get_autochanger_loaded_slot(dcr);
504       }
505
506       if (device->cap_bits & CAP_ALWAYSOPEN) {
507          Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
508          if (!first_open_device(dcr)) {
509             Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
510             Dmsg1(20, "Could not open device %s\n", dev->print_name());
511             free_dcr(dcr);
512             continue;
513          }
514       }
515       if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
516          switch (read_dev_volume_label(dcr)) {
517          case VOL_OK:
518             memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
519             break;
520          default:
521             Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
522             break;
523          }
524       }
525       free_dcr(dcr);
526    }
527    free_jcr(jcr); 
528    init_done = true;
529    UnlockRes();
530    return NULL;
531 }
532
533
534 /* Clean up and then exit */
535 void terminate_stored(int sig)
536 {
537    static bool in_here = false;
538    DEVRES *device;
539    JCR *jcr;
540
541    if (in_here) {                     /* prevent loops */
542       exit(1);
543    }
544    in_here = true;
545
546    if (sig == SIGTERM) {              /* normal shutdown request? */
547       /*
548        * This is a normal shutdown request. We wiffle through
549        *   all open jobs canceling them and trying to wake
550        *   them up so that they will report back the correct
551        *   volume status.
552        */
553       foreach_jcr(jcr) {
554          BSOCK *fd;
555          if (jcr->JobId == 0) {
556             free_jcr(jcr);
557             continue;                 /* ignore console */
558          }
559          set_jcr_job_status(jcr, JS_Canceled);
560          fd = jcr->file_bsock;
561          if (fd) {
562             fd->timed_out = true;
563             Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
564             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
565             /* ***FIXME*** wiffle through all dcrs */
566             if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->dev_blocked) {
567                pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
568                pthread_cond_broadcast(&wait_device_release);
569             }
570             bmicrosleep(0, 50000);
571          }
572          free_jcr(jcr);
573       }
574       bmicrosleep(0, 500000);         /* give them 1/2 sec to clean up */
575    }
576
577    write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
578    delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
579
580    Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
581
582    foreach_res(device, R_DEVICE) {
583       Dmsg1(10, "Term device %s\n", device->device_name);
584       if (device->dev) {
585          free_volume(device->dev);
586          term_dev(device->dev);
587       } else {
588          Dmsg1(10, "No dev structure %s\n", device->device_name);
589       }
590    }
591
592    if (configfile) {
593       free(configfile);
594       configfile = NULL;
595    }
596    free_config_resources();
597
598    if (debug_level > 10) {
599       print_memory_pool_stats();
600    }
601    term_msg();
602    stop_watchdog();
603    cleanup_tls();
604    free_volume_list();
605    close_memory_pool();
606
607    sm_dump(false);                    /* dump orphaned buffers */
608    exit(sig);
609 }