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