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