]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
1429aa7a5f67fc9edf34c858ced3c88f1cc4c617
[bacula/bacula] / bacula / src / stored / stored.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * Second generation Storage daemon.
18  *
19  *  Kern Sibbald, MM
20  *
21  * It accepts a number of simple commands from the File daemon
22  * and acts on them. When a request to append data is made,
23  * it opens a data channel and accepts data from the
24  * File daemon.
25  *
26  */
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 /* TODO: fix problem with bls, bextract
32  * that use findlib and already declare
33  * filed plugins
34  */
35 #include "sd_plugins.h"
36
37 /* Imported functions */
38 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
39
40 /* Forward referenced functions */
41 void terminate_stored(int sig);
42 static int check_resources();
43 static void cleanup_old_files();
44
45 extern "C" void *device_initialization(void *arg);
46
47 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
48
49 /* Global variables exported */
50 char OK_msg[]   = "3000 OK\n";
51 char TERM_msg[] = "3999 Terminate\n";
52 STORES *me = NULL;                    /* our Global resource */
53 bool forge_on = false;                /* proceed inspite of I/O errors */
54 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
55 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
56 void *start_heap;
57
58
59 static uint32_t VolSessionId = 0;
60 uint32_t VolSessionTime;
61 char *configfile = NULL;
62 bool init_done = false;
63 static pthread_t server_tid;
64 static bool server_tid_valid = false;
65
66 /* Global static variables */
67 static bool foreground = 0;
68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
69 static workq_t dird_workq;            /* queue for processing connections */
70 static CONFIG *config;
71
72
73 static void usage()
74 {
75    fprintf(stderr, _(
76 PROG_COPYRIGHT
77 "\nVersion: %s (%s)\n\n"
78 "Usage: bacula-sd [options] [-c config_file] [config_file]\n"
79 "        -c <file>   use <file> as configuration file\n"
80 "        -d <nn>     set debug level to <nn>\n"
81 "        -dt         print timestamp in debug output\n"
82 "        -f          run in foreground (for debugging)\n"
83 "        -g <group>  set groupid to group\n"
84 "        -m          print kaboom output (for debugging)\n"
85 "        -p          proceed despite I/O errors\n"
86 "        -s          no signals (for debugging)\n"
87 "        -t          test - read config and exit\n"
88 "        -u <user>   userid to <user>\n"
89 "        -v          verbose user messages\n"
90 "        -?          print this message.\n"
91 "\n"), 2000, VERSION, BDATE);
92
93    exit(1);
94 }
95
96 /*********************************************************************
97  *
98  *  Main Bacula Unix Storage Daemon
99  *
100  */
101 #if defined(HAVE_WIN32)
102 #define main BaculaMain
103 #endif
104
105 int main (int argc, char *argv[])
106 {
107    int ch;
108    bool no_signals = false;
109    bool test_config = false;
110    pthread_t thid;
111    char *uid = NULL;
112    char *gid = NULL;
113
114    start_heap = sbrk(0);
115    setlocale(LC_ALL, "");
116    bindtextdomain("bacula", LOCALEDIR);
117    textdomain("bacula");
118
119    init_stack_dump();
120    my_name_is(argc, argv, "bacula-sd");
121    init_msg(NULL, NULL);
122    daemon_start_time = time(NULL);
123
124    /* Sanity checks */
125    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
126       Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
127          TAPE_BSIZE, B_DEV_BSIZE);
128    }
129    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
130       Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
131    }
132
133    while ((ch = getopt(argc, argv, "c:d:fg:mpstu:v?")) != -1) {
134       switch (ch) {
135       case 'c':                    /* configuration file */
136          if (configfile != NULL) {
137             free(configfile);
138          }
139          configfile = bstrdup(optarg);
140          break;
141
142       case 'd':                    /* debug level */
143          if (*optarg == 't') {
144             dbg_timestamp = true;
145          } else {
146             debug_level = atoi(optarg);
147             if (debug_level <= 0) {
148                debug_level = 1;
149             }
150          }
151          break;
152
153       case 'f':                    /* run in foreground */
154          foreground = true;
155          break;
156
157       case 'g':                    /* set group id */
158          gid = optarg;
159          break;
160
161       case 'm':                    /* print kaboom output */
162          prt_kaboom = true;
163          break;
164
165       case 'p':                    /* proceed in spite of I/O errors */
166          forge_on = true;
167          break;
168
169       case 's':                    /* no signals */
170          no_signals = true;
171          break;
172
173       case 't':
174          test_config = true;
175          break;
176
177       case 'u':                    /* set uid */
178          uid = optarg;
179          break;
180
181       case 'v':                    /* verbose */
182          verbose++;
183          break;
184
185       case '?':
186       default:
187          usage();
188          break;
189       }
190    }
191    argc -= optind;
192    argv += optind;
193
194    if (argc) {
195       if (configfile != NULL) {
196          free(configfile);
197       }
198       configfile = bstrdup(*argv);
199       argc--;
200       argv++;
201    }
202    if (argc)
203       usage();
204
205    if (!foreground) {
206       daemon_start();                 /* become daemon */
207       init_stack_dump();              /* pick up new pid */
208    }
209
210    if (!no_signals) {
211       init_signals(terminate_stored);
212    }
213
214    if (configfile == NULL) {
215       configfile = bstrdup(CONFIG_FILE);
216    }
217
218    config = new_config_parser();
219    parse_sd_config(config, configfile, M_ERROR_TERM);
220
221    if (init_crypto() != 0) {
222       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
223    }
224
225    if (!check_resources()) {
226       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
227    }
228
229    init_reservations_lock();
230
231    if (test_config) {
232       terminate_stored(0);
233    }
234
235    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
236
237
238    create_pid_file(me->pid_directory, "bacula-sd",
239                    get_first_port_host_order(me->sdaddrs));
240    read_state_file(me->working_directory, "bacula-sd",
241                    get_first_port_host_order(me->sdaddrs));
242
243    set_jcr_in_tsd(INVALID_JCR);
244    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
245    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
246    lmgr_init_thread(); /* initialize the lockmanager stack */
247
248    load_sd_plugins(me->plugin_directory);
249
250    drop(uid, gid, false);
251
252    cleanup_old_files();
253
254    /* Ensure that Volume Session Time and Id are both
255     * set and are both non-zero.
256     */
257    VolSessionTime = (uint32_t)daemon_start_time;
258    if (VolSessionTime == 0) { /* paranoid */
259       Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
260    }
261
262    /*
263     * Start the device allocation thread
264     */
265    create_volume_lists();             /* do before device_init */
266    if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
267       berrno be;
268       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
269    }
270
271    start_watchdog();                  /* start watchdog thread */
272    init_jcr_subsystem();              /* start JCR watchdogs etc. */
273
274    /* Single server used for Director and File daemon */
275    server_tid = pthread_self();
276    server_tid_valid = true;
277    bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
278                       &dird_workq, handle_connection_request);
279    exit(1);                           /* to keep compiler quiet */
280 }
281
282 /* Return a new Session Id */
283 uint32_t newVolSessionId()
284 {
285    uint32_t Id;
286
287    P(mutex);
288    VolSessionId++;
289    Id = VolSessionId;
290    V(mutex);
291    return Id;
292 }
293
294 /* Check Configuration file for necessary info */
295 static int check_resources()
296 {
297    bool OK = true;
298    bool tls_needed;
299
300
301    me = (STORES *)GetNextRes(R_STORAGE, NULL);
302    if (!me) {
303       Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
304          configfile);
305       OK = false;
306    }
307
308    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
309       Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
310          configfile);
311       OK = false;
312    }
313    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
314       Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
315          configfile);
316       OK = false;
317    }
318    if (GetNextRes(R_DEVICE, NULL) == NULL){
319       Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
320            configfile);
321       OK = false;
322    }
323
324    if (!me->messages) {
325       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
326       if (!me->messages) {
327          Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
328             configfile);
329          OK = false;
330       }
331    }
332
333    if (!me->working_directory) {
334       Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
335          configfile);
336       OK = false;
337    }
338
339    DIRRES *director;
340    STORES *store;
341    foreach_res(store, R_STORAGE) {
342       /* tls_require implies tls_enable */
343       if (store->tls_require) {
344          if (have_tls) {
345             store->tls_enable = true;
346          } else {
347             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
348             OK = false;
349             continue;
350          }
351       }
352
353       tls_needed = store->tls_enable || store->tls_authenticate;
354
355       if (!store->tls_certfile && tls_needed) {
356          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
357               store->hdr.name, configfile);
358          OK = false;
359       }
360
361       if (!store->tls_keyfile && tls_needed) {
362          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
363               store->hdr.name, configfile);
364          OK = false;
365       }
366
367       if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
368          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
369               " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
370               " At least one CA certificate store is required"
371               " when using \"TLS Verify Peer\".\n"),
372               store->hdr.name, configfile);
373          OK = false;
374       }
375
376       /* If everything is well, attempt to initialize our per-resource TLS context */
377       if (OK && (tls_needed || store->tls_require)) {
378          /* Initialize TLS context:
379           * Args: CA certfile, CA certdir, Certfile, Keyfile,
380           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
381          store->tls_ctx = new_tls_context(store->tls_ca_certfile,
382             store->tls_ca_certdir, store->tls_certfile,
383             store->tls_keyfile, NULL, NULL, store->tls_dhfile,
384             store->tls_verify_peer);
385
386          if (!store->tls_ctx) {
387             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
388                  store->hdr.name, configfile);
389             OK = false;
390          }
391       }
392    }
393
394    foreach_res(director, R_DIRECTOR) {
395       /* tls_require implies tls_enable */
396       if (director->tls_require) {
397          director->tls_enable = true;
398       }
399
400       tls_needed = director->tls_enable || director->tls_authenticate;
401
402       if (!director->tls_certfile && tls_needed) {
403          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
404               director->hdr.name, configfile);
405          OK = false;
406       }
407
408       if (!director->tls_keyfile && tls_needed) {
409          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
410               director->hdr.name, configfile);
411          OK = false;
412       }
413
414       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
415          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
416               " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
417               " At least one CA certificate store is required"
418               " when using \"TLS Verify Peer\".\n"),
419               director->hdr.name, configfile);
420          OK = false;
421       }
422
423       /* If everything is well, attempt to initialize our per-resource TLS context */
424       if (OK && (tls_needed || director->tls_require)) {
425          /* Initialize TLS context:
426           * Args: CA certfile, CA certdir, Certfile, Keyfile,
427           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
428          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
429             director->tls_ca_certdir, director->tls_certfile,
430             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
431             director->tls_verify_peer);
432
433          if (!director->tls_ctx) {
434             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
435                  director->hdr.name, configfile);
436             OK = false;
437          }
438       }
439    }
440
441    OK = init_autochangers();
442
443
444    if (OK) {
445       close_msg(NULL);                   /* close temp message handler */
446       init_msg(NULL, me->messages);      /* open daemon message handler */
447       set_working_directory(me->working_directory);
448    }
449
450    return OK;
451 }
452
453 /*
454  * Remove old .spool files written by me from the working directory.
455  */
456 static void cleanup_old_files()
457 {
458    DIR* dp;
459    struct dirent *entry, *result;
460    int rc, name_max;
461    int my_name_len = strlen(my_name);
462    int len = strlen(me->working_directory);
463    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
464    POOLMEM *basename = get_pool_memory(PM_MESSAGE);
465    regex_t preg1;
466    char prbuf[500];
467    const int nmatch = 30;
468    regmatch_t pmatch[nmatch];
469    berrno be;
470
471    /* Look for .spool files but don't allow spaces */
472    const char *pat1 = "^[^ ]+\\.spool$";
473
474    /* Setup working directory prefix */
475    pm_strcpy(basename, me->working_directory);
476    if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) {
477       pm_strcat(basename, "/");
478    }
479
480    /* Compile regex expressions */
481    rc = regcomp(&preg1, pat1, REG_EXTENDED);
482    if (rc != 0) {
483       regerror(rc, &preg1, prbuf, sizeof(prbuf));
484       Pmsg2(000,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
485            pat1, prbuf);
486       goto get_out2;
487    }
488
489    name_max = pathconf(".", _PC_NAME_MAX);
490    if (name_max < 1024) {
491       name_max = 1024;
492    }
493
494    if (!(dp = opendir(me->working_directory))) {
495       berrno be;
496       Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
497             me->working_directory, be.bstrerror());
498       goto get_out1;
499    }
500
501    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
502    while (1) {
503       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
504          break;
505       }
506       /* Exclude any name with ., .., not my_name or containing a space */
507       if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
508           strncmp(result->d_name, my_name, my_name_len) != 0) {
509          Dmsg1(500, "Skipped: %s\n", result->d_name);
510          continue;
511       }
512
513       /* Unlink files that match regex */
514       if (regexec(&preg1, result->d_name, nmatch, pmatch,  0) == 0) {
515          pm_strcpy(cleanup, basename);
516          pm_strcat(cleanup, result->d_name);
517          Dmsg1(500, "Unlink: %s\n", cleanup);
518          unlink(cleanup);
519       }
520    }
521    free(entry);
522    closedir(dp);
523
524 get_out1:
525    regfree(&preg1);
526 get_out2:
527    free_pool_memory(cleanup);
528    free_pool_memory(basename);
529 }
530
531
532 /*
533  * Here we attempt to init and open each device. This is done
534  *  once at startup in a separate thread.
535  */
536 extern "C"
537 void *device_initialization(void *arg)
538 {
539    DEVRES *device;
540    DCR *dcr;
541    JCR *jcr;
542    DEVICE *dev;
543
544    LockRes();
545
546    pthread_detach(pthread_self());
547    jcr = new_jcr(sizeof(JCR), stored_free_jcr);
548    jcr->setJobType(JT_SYSTEM);
549    /* Initialize FD start condition variable */
550    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
551    if (errstat != 0) {
552       berrno be;
553       Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
554    }
555
556    foreach_res(device, R_DEVICE) {
557       Dmsg1(90, "calling init_dev %s\n", device->device_name);
558       dev = init_dev(NULL, device);
559       Dmsg1(10, "SD init done %s\n", device->device_name);
560       if (!dev) {
561          Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
562          continue;
563       }
564
565       jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
566       generate_plugin_event(jcr, bsdEventDeviceInit, dcr);
567       if (dev->is_autochanger()) {
568          /* If autochanger set slot in dev sturcture */
569          get_autochanger_loaded_slot(dcr);
570       }
571
572       if (device->cap_bits & CAP_ALWAYSOPEN) {
573          Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
574          if (!first_open_device(dcr)) {
575             Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
576             Dmsg1(20, "Could not open device %s\n", dev->print_name());
577             free_dcr(dcr);
578             jcr->dcr = NULL;
579             continue;
580          }
581       }
582       if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
583          switch (read_dev_volume_label(dcr)) {
584          case VOL_OK:
585             memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
586             volume_unused(dcr);             /* mark volume "released" */
587             break;
588          default:
589             Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
590             break;
591          }
592       }
593       free_dcr(dcr);
594       jcr->dcr = NULL;
595    }
596 #ifdef xxx
597    if (jcr->dcr) {
598       Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
599       free_dcr(jcr->dcr);
600       jcr->dcr = NULL;
601    }
602 #endif
603    free_plugins(jcr);
604    free_jcr(jcr);
605    init_done = true;
606    UnlockRes();
607    return NULL;
608 }
609
610
611 /* Clean up and then exit */
612 void terminate_stored(int sig)
613 {
614    static bool in_here = false;
615    DEVRES *device;
616    JCR *jcr;
617
618    if (in_here) {                     /* prevent loops */
619       bmicrosleep(2, 0);              /* yield */
620       exit(1);
621    }
622    in_here = true;
623    debug_level = 0;                   /* turn off any debug */
624    stop_watchdog();
625
626    if (sig == SIGTERM) {              /* normal shutdown request? */
627       /*
628        * This is a normal shutdown request. We wiffle through
629        *   all open jobs canceling them and trying to wake
630        *   them up so that they will report back the correct
631        *   volume status.
632        */
633       foreach_jcr(jcr) {
634          BSOCK *fd;
635          if (jcr->JobId == 0) {
636             free_jcr(jcr);
637             continue;                 /* ignore console */
638          }
639          jcr->setJobStatus(JS_Canceled);
640          fd = jcr->file_bsock;
641          if (fd) {
642             fd->set_timed_out();
643             jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
644             Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
645             /* ***FIXME*** wiffle through all dcrs */
646             if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
647                pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
648                Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
649                pthread_cond_broadcast(&wait_device_release);
650             }
651             if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
652                pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
653                pthread_cond_broadcast(&wait_device_release);
654             }
655             bmicrosleep(0, 50000);
656          }
657          free_jcr(jcr);
658       }
659       bmicrosleep(0, 500000);         /* give them 1/2 sec to clean up */
660    }
661
662    write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
663    delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
664
665    Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
666
667    unload_plugins();
668    free_volume_lists();
669
670    foreach_res(device, R_DEVICE) {
671       Dmsg1(10, "Term device %s\n", device->device_name);
672       if (device->dev) {
673          device->dev->clear_volhdr();
674          device->dev->term();
675          device->dev = NULL;
676       } else {
677          Dmsg1(10, "No dev structure %s\n", device->device_name);
678       }
679    }
680    if (server_tid_valid) {
681       bnet_stop_thread_server(server_tid);
682    }
683    if (configfile) {
684       free(configfile);
685       configfile = NULL;
686    }
687    if (config) {
688       config->free_resources();
689       free(config);
690       config = NULL;
691    }
692
693    if (chk_dbglvl(10)) {
694       print_memory_pool_stats();
695    }
696    term_msg();
697    cleanup_crypto();
698    term_reservations_lock();
699    close_memory_pool();
700    lmgr_cleanup_main();
701
702    sm_dump(false);                    /* dump orphaned buffers */
703    exit(sig);
704 }