]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
Backup bad fix
[bacula/bacula] / bacula / src / stored / stored.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Second generation Storage daemon.
30  *
31  *  Kern Sibbald, MM
32  *
33  * It accepts a number of simple commands from the File daemon
34  * and acts on them. When a request to append data is made,
35  * it opens a data channel and accepts data from the
36  * File daemon.
37  *
38  *   Version $Id$
39  *
40  */
41
42 #include "bacula.h"
43 #include "stored.h"
44
45 /* Imported functions */
46 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
47
48 /* Forward referenced functions */
49 void terminate_stored(int sig);
50 static int check_resources();
51 static void cleanup_old_files();
52
53 extern "C" void *device_initialization(void *arg);
54
55 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
56
57 /* Global variables exported */
58 char OK_msg[]   = "3000 OK\n";
59 char TERM_msg[] = "3999 Terminate\n";
60 STORES *me = NULL;                    /* our Global resource */
61 bool forge_on = false;                /* proceed inspite of I/O errors */
62 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
63 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
64 void *start_heap;
65
66
67 static uint32_t VolSessionId = 0;
68 uint32_t VolSessionTime;
69 char *configfile = NULL;
70 bool init_done = false;
71
72 /* Global static variables */
73 static bool foreground = 0;
74 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
75 static workq_t dird_workq;            /* queue for processing connections */
76 static CONFIG *config;
77
78
79 static void usage()
80 {
81    fprintf(stderr, _(
82 PROG_COPYRIGHT
83 "\nVersion: %s (%s)\n\n"
84 "Usage: stored [options] [-c config_file] [config_file]\n"
85 "        -c <file>   use <file> as configuration file\n"
86 "        -d <nn>     set debug level to <nn>\n"
87 "        -dt         print timestamp in debug output\n"
88 "        -f          run in foreground (for debugging)\n"
89 "        -g <group>  set groupid to group\n"
90 "        -p          proceed despite I/O errors\n"
91 "        -s          no signals (for debugging)\n"
92 "        -t          test - read config and exit\n"
93 "        -u <user>   userid to <user>\n"
94 "        -v          verbose user messages\n"
95 "        -?          print this message.\n"
96 "\n"), 2000, VERSION, BDATE);
97    exit(1);
98 }
99
100 /*********************************************************************
101  *
102  *  Main Bacula Unix Storage Daemon
103  *
104  */
105 #if defined(HAVE_WIN32)
106 #define main BaculaMain
107 #endif
108
109 int main (int argc, char *argv[])
110 {
111    int ch;
112    bool no_signals = false;
113    bool test_config = false;
114    pthread_t thid;
115    char *uid = NULL;
116    char *gid = NULL;
117
118    start_heap = sbrk(0);
119    setlocale(LC_ALL, "");
120    bindtextdomain("bacula", LOCALEDIR);
121    textdomain("bacula");
122
123    init_stack_dump();
124    my_name_is(argc, argv, "bacula-sd");
125    init_msg(NULL, NULL);
126    daemon_start_time = time(NULL);
127
128    /* Sanity checks */
129    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
130       Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
131          TAPE_BSIZE, B_DEV_BSIZE);
132    }
133    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
134       Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
135    }
136
137    while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
138       switch (ch) {
139       case 'c':                    /* configuration file */
140          if (configfile != NULL) {
141             free(configfile);
142          }
143          configfile = bstrdup(optarg);
144          break;
145
146       case 'd':                    /* debug level */
147          if (*optarg == 't') {
148             dbg_timestamp = true;
149          } else {
150             debug_level = atoi(optarg);
151             if (debug_level <= 0) {
152                debug_level = 1;
153             }
154          }
155          break;
156
157       case 'f':                    /* run in foreground */
158          foreground = true;
159          break;
160
161       case 'g':                    /* set group id */
162          gid = optarg;
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 (!no_signals) {
206       init_signals(terminate_stored);
207    }
208
209    if (configfile == NULL) {
210       configfile = bstrdup(CONFIG_FILE);
211    }
212
213    config = new_config_parser();
214    parse_sd_config(config, configfile, M_ERROR_TERM);
215
216    if (init_crypto() != 0) {
217       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
218    }
219
220    if (!check_resources()) {
221       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
222    }
223
224    init_reservations_lock();
225
226    if (test_config) {
227       terminate_stored(0);
228    }
229
230    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
231
232    if (!foreground) {
233       daemon_start();                 /* become daemon */
234       init_stack_dump();              /* pick up new pid */
235    }
236
237    create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
238    read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
239
240    drop(uid, gid);
241
242    cleanup_old_files();
243
244
245    /* Ensure that Volume Session Time and Id are both
246     * set and are both non-zero.
247     */
248    VolSessionTime = (uint32_t)daemon_start_time;
249    if (VolSessionTime == 0) { /* paranoid */
250       Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
251    }
252
253    init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
254
255    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
256    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
257
258     /*
259      * Start the device allocation thread
260      */
261    create_volume_list();              /* do before device_init */
262    if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
263       berrno be;
264       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), be.bstrerror());
265    }
266
267    start_watchdog();                  /* start watchdog thread */
268    init_jcr_subsystem();              /* start JCR watchdogs etc. */
269
270    /* Single server used for Director and File daemon */
271    bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
272                       &dird_workq, handle_connection_request);
273    exit(1);                           /* to keep compiler quiet */
274 }
275
276 /* Return a new Session Id */
277 uint32_t newVolSessionId()
278 {
279    uint32_t Id;
280
281    P(mutex);
282    VolSessionId++;
283    Id = VolSessionId;
284    V(mutex);
285    return Id;
286 }
287
288 /* Check Configuration file for necessary info */
289 static int check_resources()
290 {
291    bool OK = true;
292    bool tls_needed;
293
294
295    me = (STORES *)GetNextRes(R_STORAGE, NULL);
296    if (!me) {
297       Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
298          configfile);
299       OK = false;
300    }
301
302    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
303       Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
304          configfile);
305       OK = false;
306    }
307    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
308       Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
309          configfile);
310       OK = false;
311    }
312    if (GetNextRes(R_DEVICE, NULL) == NULL){
313       Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
314            configfile);
315       OK = false;
316    }
317
318    if (!me->messages) {
319       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
320       if (!me->messages) {
321          Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
322             configfile);
323          OK = false;
324       }
325    }
326
327    if (!me->working_directory) {
328       Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
329          configfile);
330       OK = false;
331    }
332
333    DIRRES *director;
334    STORES *store;
335    foreach_res(store, R_STORAGE) { 
336       /* tls_require implies tls_enable */
337       if (store->tls_require) {
338          if (have_tls) {
339             store->tls_enable = true;
340          } else {
341             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
342             OK = false;
343             continue;
344          }
345       }
346
347       tls_needed = store->tls_enable || store->tls_authenticate;
348
349       if (!store->tls_certfile && tls_needed) {
350          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
351               store->hdr.name, configfile);
352          OK = false;
353       }
354
355       if (!store->tls_keyfile && tls_needed) {
356          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
357               store->hdr.name, configfile);
358          OK = false;
359       }
360
361       if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && tls_needed && store->tls_verify_peer) {
362          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
363               " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
364               " At least one CA certificate store is required"
365               " when using \"TLS Verify Peer\".\n"),
366               store->hdr.name, configfile);
367          OK = false;
368       }
369
370       /* If everything is well, attempt to initialize our per-resource TLS context */
371       if (OK && (tls_needed || store->tls_require)) {
372          /* Initialize TLS context:
373           * Args: CA certfile, CA certdir, Certfile, Keyfile,
374           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
375          store->tls_ctx = new_tls_context(store->tls_ca_certfile,
376             store->tls_ca_certdir, store->tls_certfile,
377             store->tls_keyfile, NULL, NULL, store->tls_dhfile,
378             store->tls_verify_peer);
379
380          if (!store->tls_ctx) { 
381             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
382                  store->hdr.name, configfile);
383             OK = false;
384          }
385       }
386    }
387
388    foreach_res(director, R_DIRECTOR) { 
389       /* tls_require implies tls_enable */
390       if (director->tls_require) {
391          director->tls_enable = true;
392       }
393
394       tls_needed = director->tls_enable || director->tls_authenticate;
395
396       if (!director->tls_certfile && tls_needed) {
397          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
398               director->hdr.name, configfile);
399          OK = false;
400       }
401
402       if (!director->tls_keyfile && tls_needed) {
403          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
404               director->hdr.name, configfile);
405          OK = false;
406       }
407
408       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed && director->tls_verify_peer) {
409          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
410               " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
411               " At least one CA certificate store is required"
412               " when using \"TLS Verify Peer\".\n"),
413               director->hdr.name, configfile);
414          OK = false;
415       }
416
417       /* If everything is well, attempt to initialize our per-resource TLS context */
418       if (OK && (tls_needed || director->tls_require)) {
419          /* Initialize TLS context:
420           * Args: CA certfile, CA certdir, Certfile, Keyfile,
421           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
422          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
423             director->tls_ca_certdir, director->tls_certfile,
424             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
425             director->tls_verify_peer);
426
427          if (!director->tls_ctx) { 
428             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
429                  director->hdr.name, configfile);
430             OK = false;
431          }
432       }
433    }
434
435    OK = init_autochangers();
436
437    
438    if (OK) {
439       close_msg(NULL);                   /* close temp message handler */
440       init_msg(NULL, me->messages);      /* open daemon message handler */
441       set_working_directory(me->working_directory);
442    }
443
444    return OK;
445 }
446
447 static void cleanup_old_files()
448 {
449    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
450    POOLMEM *results = get_pool_memory(PM_MESSAGE);
451    int len = strlen(me->working_directory);
452 #if defined(HAVE_WIN32)
453    pm_strcpy(cleanup, "del /q ");
454 #else
455    pm_strcpy(cleanup, "/bin/rm -f ");
456 #endif
457    pm_strcat(cleanup, me->working_directory);
458    if (len > 0 && !IsPathSeparator(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, results);
464    free_pool_memory(cleanup);
465    free_pool_memory(results);
466 }
467
468
469 /*
470  * Here we attempt to init and open each device. This is done
471  *  once at startup in a separate thread.
472  */
473 extern "C"
474 void *device_initialization(void *arg)
475 {
476    DEVRES *device;
477    DCR *dcr;
478    JCR *jcr;
479    DEVICE *dev;
480
481    LockRes();
482
483    pthread_detach(pthread_self());
484    jcr = new_jcr(sizeof(JCR), stored_free_jcr);
485    jcr->JobType = JT_SYSTEM;
486    /* Initialize FD start condition variable */
487    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
488    if (errstat != 0) {
489       berrno be;
490       Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
491    }
492
493    foreach_res(device, R_DEVICE) {
494       Dmsg1(90, "calling init_dev %s\n", device->device_name);
495       dev = init_dev(NULL, device);
496       Dmsg1(10, "SD init done %s\n", device->device_name);
497       if (!dev) {
498          Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
499          continue;
500       }
501
502       jcr->dcr = dcr = new_dcr(jcr, NULL, dev);
503       if (dev->is_autochanger()) {
504          /* If autochanger set slot in dev sturcture */
505          get_autochanger_loaded_slot(dcr);
506       }
507
508       if (device->cap_bits & CAP_ALWAYSOPEN) {
509          Dmsg1(20, "calling first_open_device %s\n", dev->print_name());
510          if (!first_open_device(dcr)) {
511             Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name());
512             Dmsg1(20, "Could not open device %s\n", dev->print_name());
513             free_dcr(dcr);
514             jcr->dcr = NULL;
515             continue;
516          }
517       }
518       if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) {
519          switch (read_dev_volume_label(dcr)) {
520          case VOL_OK:
521             memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
522             volume_unused(dcr);             /* mark volume "released" */
523             break;
524          default:
525             Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name());
526             break;
527          }
528       }
529       free_dcr(dcr);
530       jcr->dcr = NULL;
531    }
532 #ifdef xxx
533    if (jcr->dcr) {
534       Dmsg1(000, "free_dcr=%p\n", jcr->dcr);
535       free_dcr(jcr->dcr);
536       jcr->dcr = NULL;
537    }
538 #endif
539    free_jcr(jcr); 
540    init_done = true;
541    UnlockRes();
542    return NULL;
543 }
544
545
546 /* Clean up and then exit */
547 void terminate_stored(int sig)
548 {
549    static bool in_here = false;
550    DEVRES *device;
551    JCR *jcr;
552
553    if (in_here) {                     /* prevent loops */
554       bmicrosleep(2, 0);              /* yield */
555       exit(1);
556    }
557    in_here = true;
558    debug_level = 0;                   /* turn off any debug */
559    stop_watchdog();
560
561    if (sig == SIGTERM) {              /* normal shutdown request? */
562       /*
563        * This is a normal shutdown request. We wiffle through
564        *   all open jobs canceling them and trying to wake
565        *   them up so that they will report back the correct
566        *   volume status.
567        */
568       foreach_jcr(jcr) {
569          BSOCK *fd;
570          if (jcr->JobId == 0) {
571             free_jcr(jcr);
572             continue;                 /* ignore console */
573          }
574          set_jcr_job_status(jcr, JS_Canceled);
575          fd = jcr->file_bsock;
576          if (fd) {
577             fd->set_timed_out();
578             Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
579             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
580             /* ***FIXME*** wiffle through all dcrs */
581             if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->blocked()) {
582                pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
583                Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
584                pthread_cond_broadcast(&wait_device_release);
585             }
586             if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->blocked()) {
587                pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
588                pthread_cond_broadcast(&wait_device_release);
589             }
590             bmicrosleep(0, 50000);
591          }
592          free_jcr(jcr);
593       }
594       bmicrosleep(0, 500000);         /* give them 1/2 sec to clean up */
595    }
596
597    write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
598    delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
599
600    Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
601
602    free_volume_list();
603
604    foreach_res(device, R_DEVICE) {
605       Dmsg1(10, "Term device %s\n", device->device_name);
606       if (device->dev) {
607          device->dev->clear_volhdr();
608          device->dev->term();
609          device->dev = NULL;
610       } else {
611          Dmsg1(10, "No dev structure %s\n", device->device_name);
612       }
613    }
614
615    if (configfile) {
616       free(configfile);
617       configfile = NULL;
618    }
619    if (config) {
620       config->free_resources();
621       free(config);
622       config = NULL;
623   }
624
625    if (debug_level > 10) {
626       print_memory_pool_stats();
627    }
628    term_msg();
629    cleanup_crypto();
630    term_reservations_lock();
631    close_memory_pool();
632
633    sm_dump(false);                    /* dump orphaned buffers */
634    exit(sig);
635 }