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