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