]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
- Fix bug in changing tape pools after first backup. Reported
[bacula/bacula] / bacula / src / stored / acquire.c
1 /*
2  *  Routines to acquire and release a device for read/write
3  *
4  *   Kern Sibbald, August MMII
5  *
6  *   Version $Id$
7  */
8 /*
9    Copyright (C) 2002-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"                   /* pull in global headers */
29 #include "stored.h"                   /* pull in Storage Deamon headers */
30
31 static int can_reserve_drive(DCR *dcr);
32
33 /*
34  * Create a new Device Control Record and attach
35  *   it to the device (if this is a real job).
36  */
37 DCR *new_dcr(JCR *jcr, DEVICE *dev)
38 {
39    if (jcr && jcr->dcr) {
40       return jcr->dcr;
41    }
42    DCR *dcr = (DCR *)malloc(sizeof(DCR));
43    memset(dcr, 0, sizeof(DCR));
44    if (jcr) {
45       jcr->dcr = dcr;
46    }
47    dcr->jcr = jcr;
48    dcr->dev = dev;
49    if (dev) {
50       dcr->device = dev->device;
51    }
52    dcr->block = new_block(dev);
53    dcr->rec = new_record();
54    dcr->spool_fd = -1;
55    dcr->max_spool_size = dev->device->max_spool_size;
56    /* Attach this dcr only if dev is initialized */
57    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
58       dev->attached_dcrs->append(dcr);  /* attach dcr to device */
59 //    jcr->dcrs->append(dcr);         /* put dcr in list for Job */
60    }
61    return dcr;
62 }
63
64 /*
65  * Search the dcrs list for the given dcr. If it is found,
66  *  as it should be, then remove it. Also zap the jcr pointer
67  *  to the dcr if it is the same one.
68  */
69 #ifdef needed
70 static void remove_dcr_from_dcrs(DCR *dcr)
71 {
72    JCR *jcr = dcr->jcr;
73    if (jcr->dcrs) {
74       int i = 0;
75       DCR *ldcr;
76       int num = jcr->dcrs->size();
77       for (i=0; i < num; i++) {
78          ldcr = (DCR *)jcr->dcrs->get(i);
79          if (ldcr == dcr) {
80             jcr->dcrs->remove(i);
81             if (jcr->dcr == dcr) {
82                jcr->dcr = NULL;
83             }
84          }
85       }
86    }
87 }
88 #endif
89
90 /*
91  * Free up all aspects of the given dcr -- i.e. dechain it,
92  *  release allocated memory, zap pointers, ...
93  */
94 void free_dcr(DCR *dcr)
95 {
96    JCR *jcr = dcr->jcr;
97    DEVICE *dev = dcr->dev;
98
99    /*
100     * If we reserved the device, we must decrement the
101     *  number of writers.
102     */
103    if (dcr->reserved_device) {
104       lock_device(dev);
105       dev->num_writers--;
106       if (dev->num_writers < 0) {
107          Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
108          dev->num_writers = 0;
109          dcr->reserved_device = false;
110       }
111       unlock_device(dev);
112    }
113
114    /* Detach this dcr only if the dev is initialized */
115    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
116       dev->attached_dcrs->remove(dcr);  /* detach dcr from device */
117 //    remove_dcr_from_dcrs(dcr);      /* remove dcr from jcr list */
118    }
119    if (dcr->block) {
120       free_block(dcr->block);
121    }
122    if (dcr->rec) {
123       free_record(dcr->rec);
124    }
125    if (dcr->jcr) {
126       dcr->jcr->dcr = NULL;
127    }
128    free(dcr);
129 }
130
131
132 /*
133  * We "reserve" the drive by setting the ST_READ bit. No one else
134  *  should touch the drive until that is cleared.
135  *  This allows the DIR to "reserve" the device before actually
136  *  starting the job. If the device is not available, the DIR
137  *  can wait (to be implemented 1/05).
138  */
139 bool reserve_device_for_read(DCR *dcr)
140 {
141    DEVICE *dev = dcr->dev;
142    JCR *jcr = dcr->jcr;
143    bool first;
144
145    ASSERT(dcr);
146
147    init_device_wait_timers(dcr);
148
149    dev->block(BST_DOING_ACQUIRE);
150
151    Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
152         dev->print_name());
153    for (first=true; device_is_unmounted(dev); first=false) {
154       dev->unblock();
155       if (!wait_for_device(dcr, jcr->errmsg, first))  {
156          return false;
157       }
158      dev->block(BST_DOING_ACQUIRE);
159    }
160
161    Mmsg2(jcr->errmsg, _("Device %s is busy. Job %d canceled.\n"),
162          dev->print_name(), jcr->JobId);
163    for (first=true; dev->is_busy(); first=false) {
164       dev->unblock();
165       if (!wait_for_device(dcr, jcr->errmsg, first)) {
166          return false;
167       }
168       dev->block(BST_DOING_ACQUIRE);
169    }
170
171    dev->clear_append();
172    dev->set_read();
173    dev->unblock();
174    return true;
175 }
176
177
178 /*********************************************************************
179  * Acquire device for reading. 
180  *  The drive should have previously been reserved by calling 
181  *  reserve_device_for_read(). We read the Volume label from the block and
182  *  leave the block pointers just after the label.
183  *
184  *  Returns: NULL if failed for any reason
185  *           dcr  if successful
186  */
187 DCR *acquire_device_for_read(DCR *dcr)
188 {
189    DEVICE *dev = dcr->dev;
190    JCR *jcr = dcr->jcr;
191    bool vol_ok = false;
192    bool tape_previously_mounted;
193    bool tape_initially_mounted;
194    VOL_LIST *vol;
195    bool try_autochanger = true;
196    int i;
197    int vol_label_status;
198    
199    dev->block(BST_DOING_ACQUIRE);
200
201    if (dev->num_writers > 0) {
202       Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"), 
203          dev->num_writers, jcr->JobId);
204       goto get_out;
205    }
206
207    /* Find next Volume, if any */
208    vol = jcr->VolList;
209    if (!vol) {
210       Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
211       goto get_out;
212    }
213    jcr->CurVolume++;
214    for (i=1; i<jcr->CurVolume; i++) {
215       vol = vol->next;
216    }
217    if (!vol) {
218       goto get_out;                   /* should not happen */   
219    }
220    bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
221
222    init_device_wait_timers(dcr);
223
224    tape_previously_mounted = dev->can_read() ||
225                              dev->can_append() ||
226                              dev->is_labeled();
227    tape_initially_mounted = tape_previously_mounted;
228
229
230    /* Volume info is always needed because of VolParts */
231    Dmsg0(200, "dir_get_volume_info\n");
232    if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
233       Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
234    }
235    
236    dev->num_parts = dcr->VolCatInfo.VolCatParts;
237    
238    for (i=0; i<5; i++) {
239       dev->clear_labeled();              /* force reread of label */
240       if (job_canceled(jcr)) {
241          Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
242          goto get_out;                /* error return */
243       }
244       /*
245        * This code ensures that the device is ready for
246        * reading. If it is a file, it opens it.
247        * If it is a tape, it checks the volume name
248        */
249       for ( ; !dev->is_open(); ) {
250          Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
251          if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
252             if (dev->dev_errno == EIO) {   /* no tape loaded */
253               Jmsg3(jcr, M_WARNING, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
254                     dev->print_name(), dcr->VolumeName, strerror_dev(dev));
255                goto default_path;
256             }
257             
258             /* If we have a dvd that requires mount, 
259              * we need to try to open the label, so the info can be reported
260              * if a wrong volume has been mounted. */
261             if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
262                break;
263             }
264             
265             Jmsg3(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
266                 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
267             goto get_out;
268          }
269          Dmsg1(129, "open_dev %s OK\n", dev->print_name());
270       }
271       
272       if (dev->is_dvd()) {
273          vol_label_status = read_dev_volume_label_guess(dcr, 0);
274       } else {
275          vol_label_status = read_dev_volume_label(dcr);
276       }
277       
278       Dmsg0(200, "calling read-vol-label\n");
279       switch (vol_label_status) {
280       case VOL_OK:
281          vol_ok = true;
282          memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
283          break;                    /* got it */
284       case VOL_IO_ERROR:
285          /*
286           * Send error message generated by read_dev_volume_label()
287           *  only we really had a tape mounted. This supresses superfluous
288           *  error messages when nothing is mounted.
289           */
290          if (tape_previously_mounted) {
291             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
292          }
293          goto default_path;
294       case VOL_NAME_ERROR:
295          if (tape_initially_mounted) {
296             tape_initially_mounted = false;
297             goto default_path;
298          }
299          /* Fall through */
300       default:
301          Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
302 default_path:
303          tape_previously_mounted = true;
304          
305          /* If the device requires mount, close it, so the device can be ejected.
306           * FIXME: This should perhaps be done for all devices. */
307          if (dev_cap(dev, CAP_REQMOUNT)) {
308             force_close_dev(dev);
309          }
310          
311          /* Call autochanger only once unless ask_sysop called */
312          if (try_autochanger) {
313             int stat;
314             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
315                dcr->VolumeName, dcr->VolCatInfo.Slot);
316             stat = autoload_device(dcr, 0, NULL);
317             if (stat > 0) {
318                try_autochanger = false;
319                continue;              /* try reading volume mounted */
320             }
321          }
322          
323          /* Mount a specific volume and no other */
324          Dmsg0(200, "calling dir_ask_sysop\n");
325          if (!dir_ask_sysop_to_mount_volume(dcr)) {
326             goto get_out;             /* error return */
327          }
328          try_autochanger = true;      /* permit using autochanger again */
329          continue;                    /* try reading again */
330       } /* end switch */
331       break;
332    } /* end for loop */
333    if (!vol_ok) {
334       Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"),
335             dev->print_name());
336       goto get_out;
337    }
338
339    dev->clear_append();
340    dev->set_read();
341    set_jcr_job_status(jcr, JS_Running);
342    dir_send_job_status(jcr);
343    Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
344       dcr->VolumeName, dev->print_name());
345
346 get_out:
347    dev->unblock();
348    if (!vol_ok) {
349       free_dcr(dcr);
350       dcr = NULL;
351    }
352    return dcr;
353 }
354
355 /*
356  * We reserve the device for appending by incrementing the 
357  *  reserved_device. We do virtually all the same work that
358  *  is done in acquire_device_for_append(), but we do
359  *  not attempt to mount the device. This routine allows
360  *  the DIR to reserve multiple devices before *really* 
361  *  starting the job. It also permits the SD to refuse 
362  *  certain devices (not up, ...).
363  *
364  * Note, in reserving a device, if the device is for the
365  *  same pool and the same pool type, then it is acceptable.
366  *  The Media Type has already been checked. If we are
367  *  the first tor reserve the device, we put the pool
368  *  name and pool type in the device record.
369  */
370 bool reserve_device_for_append(DCR *dcr)
371 {
372    JCR *jcr = dcr->jcr;
373    DEVICE *dev = dcr->dev;
374    bool ok = false;
375    bool first;
376
377    ASSERT(dcr);
378
379    init_device_wait_timers(dcr);
380
381    dev->block(BST_DOING_ACQUIRE);
382
383    Mmsg1(jcr->errmsg, _("Device %s is busy reading.\n"),
384          dev->print_name());
385    for (first=true; dev->can_read(); first=false) {
386       dev->unblock();
387       if (!wait_for_device(dcr, jcr->errmsg, first)) {
388          return false;
389       }
390       dev->block(BST_DOING_ACQUIRE);
391    }
392
393
394    Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
395         dev->print_name());
396    for (first=true; device_is_unmounted(dev); first=false) {
397       dev->unblock();      
398       if (!wait_for_device(dcr, jcr->errmsg, first))  {
399          return false;
400       }
401      dev->block(BST_DOING_ACQUIRE);
402    }
403
404    Dmsg1(190, "reserve_append device is %s\n", dev->is_tape()?"tape":"disk");
405
406    for ( ;; ) {
407       switch (can_reserve_drive(dcr)) {
408       case 0:
409          Mmsg1(jcr->errmsg, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
410          dev->unblock();      
411          if (!wait_for_device(dcr, jcr->errmsg, first))  {
412             return false;
413          }
414          dev->block(BST_DOING_ACQUIRE);
415          continue;
416       case -1:
417          goto bail_out;               /* error */
418       default:
419          break;                       /* OK, reserve drive */
420       }
421       break;
422    }
423
424
425    dev->reserved_device++;
426    dcr->reserved_device = true;
427    ok = true;
428
429 bail_out:
430    dev->unblock();
431    return ok;
432 }
433
434 /*
435  * Returns: 1 if drive can be reserved
436  *          0 if we should wait
437  *         -1 on error
438  */
439 static int can_reserve_drive(DCR *dcr) 
440 {
441    DEVICE *dev = dcr->dev;
442    JCR *jcr = dcr->jcr;
443    /*
444     * First handle the case that the drive is not yet in append mode
445     */
446    if (!dev->can_append() && dev->num_writers == 0) {
447       /* Now check if there are any reservations on the drive */
448       if (dev->reserved_device) {           
449          /* Yes, now check if we want the same Pool and pool type */
450          if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
451              strcmp(dev->pool_type, dcr->pool_type) == 0) {
452             /* OK, compatible device */
453          } else {
454             /* Drive not suitable for us */
455             return 0;                 /* wait */
456          }
457       } else {
458          /* Device is available but not yet reserved, reserve it for us */
459          bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
460          bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
461       }
462       return 1;                       /* reserve drive */
463    }
464
465    /*
466     * Check if device in append mode with no writers (i.e. available)
467     */
468    if (dev->can_append() && dev->num_writers == 0) {
469       /* Device is available but not yet reserved, reserve it for us */
470       bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
471       bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
472       return 1;
473    }
474
475    /*
476     * Now check if the device is in append mode with writers (i.e.
477     *  available if pool is the same).
478     */
479    if (dev->can_append() || dev->num_writers > 0) {
480       Dmsg0(190, "device already in append.\n");
481       /* Yes, now check if we want the same Pool and pool type */
482       if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
483           strcmp(dev->pool_type, dcr->pool_type) == 0) {
484          /* OK, compatible device */
485       } else {
486          /* Drive not suitable for us */
487          Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
488          return 0;                    /* wait */
489       }
490    } else {
491       Pmsg0(000, "Logic error!!!! Should not get here.\n");
492       Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
493       return -1;                      /* error, should not get here */
494    }
495    return 1;                          /* reserve drive */
496 }
497
498 /*
499  * Acquire device for writing. We permit multiple writers.
500  *  If this is the first one, we read the label.
501  *
502  *  Returns: NULL if failed for any reason
503  *           dcr if successful.
504  *   Note, normally reserve_device_for_append() is called
505  *   before this routine.
506  */
507 DCR *acquire_device_for_append(DCR *dcr)
508 {
509    bool release = false;
510    bool recycle = false;
511    bool do_mount = false;
512    DEVICE *dev = dcr->dev;
513    JCR *jcr = dcr->jcr;
514
515    init_device_wait_timers(dcr);
516
517    dev->block(BST_DOING_ACQUIRE);
518    Dmsg1(190, "acquire_append device is %s\n", dev->is_tape()?"tape":"disk");
519
520    if (dcr->reserved_device) {
521       dev->reserved_device--;
522       dcr->reserved_device = false;
523    }
524
525    /*
526     * With the reservation system, this should not happen
527     */
528    if (dev->can_read()) {
529       Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
530       goto get_out;
531    }
532
533    if (dev->can_append()) {
534       Dmsg0(190, "device already in append.\n");
535       /*
536        * Device already in append mode
537        *
538        * Check if we have the right Volume mounted
539        *   OK if current volume info OK
540        *   OK if next volume matches current volume
541        *   otherwise mount desired volume obtained from
542        *    dir_find_next_appendable_volume
543        */
544       bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
545       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
546           !(dir_find_next_appendable_volume(dcr) &&
547             strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
548          Dmsg0(190, "Wrong tape mounted.\n");
549          if (dev->num_writers != 0 || dev->reserved_device) {
550             Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
551             goto get_out;
552          }
553          /* Wrong tape mounted, release it, then fall through to get correct one */
554          Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
555          release = true;
556          do_mount = true;
557       } else {
558          /*
559           * At this point, the correct tape is already mounted, so
560           *   we do not need to do mount_next_write_volume(), unless
561           *   we need to recycle the tape.
562           */
563           recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
564           Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
565           if (recycle && dev->num_writers != 0) {
566              Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
567                   " on device %s because it is in use by another job.\n"),
568                   dev->VolHdr.VolName, dev->print_name());
569              goto get_out;
570           }
571           if (dev->num_writers == 0) {
572              memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
573           }
574        }
575    } else {
576       /* Not already in append mode, so mount the device */
577       Dmsg0(190, "Not in append mode, try mount.\n");
578       ASSERT(dev->num_writers == 0);
579       do_mount = true;
580    }
581
582    if (do_mount || recycle) {
583       Dmsg0(190, "Do mount_next_write_vol\n");
584       bool mounted = mount_next_write_volume(dcr, release);
585       if (!mounted) {
586          if (!job_canceled(jcr)) {
587             /* Reduce "noise" -- don't print if job canceled */
588             Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
589                dev->print_name());
590          }
591          goto get_out;
592       }
593    }
594
595    dev->num_writers++;                /* we are now a writer */
596    if (jcr->NumVolumes == 0) {
597       jcr->NumVolumes = 1;
598    }
599    goto ok_out;
600
601 /*
602  * If we jump here, it is an error return because
603  *  rtn_dev will still be NULL
604  */
605 get_out:
606    free_dcr(dcr);
607    dcr = NULL;
608 ok_out:
609    dev->unblock();
610    return dcr;
611 }
612
613 /*
614  * This job is done, so release the device. From a Unix standpoint,
615  *  the device remains open.
616  *
617  */
618 bool release_device(DCR *dcr)
619 {
620    JCR *jcr = dcr->jcr;
621    DEVICE *dev = dcr->dev;
622    bool ok = true;
623
624    lock_device(dev);
625    Dmsg1(100, "release_device device is %s\n", dev->is_tape()?"tape":"disk");
626
627    /* if device is reserved, job never started, so release the reserve here */
628    if (dcr->reserved_device) {
629       dev->reserved_device--;
630       dcr->reserved_device = false;
631    }
632
633    if (dev->can_read()) {
634       dev->clear_read();              /* clear read bit */
635
636       /******FIXME**** send read volume usage statistics to director */
637
638    } else if (dev->num_writers > 0) {
639       /* 
640        * Note if WEOT is set, we are at the end of the tape
641        *   and may not be positioned correctly, so the
642        *   job_media_record and update_vol_info have already been
643        *   done, which means we skip them here.
644        */
645       dev->num_writers--;
646       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
647       if (dev->is_labeled()) {
648          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
649          if (!dev->at_weot() && !dir_create_jobmedia_record(dcr)) {
650             Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
651                dcr->VolCatInfo.VolCatName, jcr->Job);
652          }
653          /* If no more writers, write an EOF */
654          if (!dev->num_writers && dev->can_write()) {
655             weof_dev(dev, 1);
656             write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolName);
657          }
658          if (!dev->at_weot()) {
659             dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
660             dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
661             /* Note! do volume update before close, which zaps VolCatInfo */
662             Dmsg0(100, "dir_update_vol_info. Release0\n");
663             dir_update_volume_info(dcr, false); /* send Volume info to Director */
664          }
665       }
666
667    } else {
668       /*                
669        * If we reach here, it is most likely because the job
670        *   has failed, since the device is not in read mode and
671        *   there are no writers. It was probably reserved.
672        */
673    }
674
675    /* If no writers, close if file or !CAP_ALWAYS_OPEN */
676    if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
677       offline_or_rewind_dev(dev);
678       close_dev(dev);
679    }
680
681    /* Fire off Alert command and include any output */
682    if (!job_canceled(jcr) && dcr->device->alert_command) {
683       POOLMEM *alert;
684       int status = 1;
685       BPIPE *bpipe;
686       char line[MAXSTRING];
687       alert = get_pool_memory(PM_FNAME);
688       alert = edit_device_codes(dcr, alert, dcr->device->alert_command, "");
689       bpipe = open_bpipe(alert, 0, "r");
690       if (bpipe) {
691          while (fgets(line, sizeof(line), bpipe->rfd)) {
692             Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
693          }
694          status = close_bpipe(bpipe);
695       } else {
696          status = errno;
697       }
698       if (status != 0) {
699          berrno be;
700          Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
701               alert, be.strerror(status));
702       }
703
704       Dmsg1(400, "alert status=%d\n", status);
705       free_pool_memory(alert);
706    }
707    unlock_device(dev);
708    free_dcr(dcr);
709    jcr->dcr = NULL;
710    pthread_cond_broadcast(&wait_device_release);
711    return ok;
712 }