-// detach_jcr_from_device(dev, jcr);
- if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
- P(mutex);
- unlock_device(dev);
- dev->prev->next = dev->next; /* dechain */
- term_dev(dev);
- V(mutex);
+
+ /* If no writers, close if file or !CAP_ALWAYS_OPEN */
+ if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
+ offline_or_rewind_dev(dev);
+ close_device(dev);
+ }
+
+ /* Fire off Alert command and include any output */
+ if (!job_canceled(jcr) && dcr->device->alert_command) {
+ POOLMEM *alert;
+ int status = 1;
+ BPIPE *bpipe;
+ char line[MAXSTRING];
+ alert = get_pool_memory(PM_FNAME);
+ alert = edit_device_codes(dcr, alert, dcr->device->alert_command, "");
+ bpipe = open_bpipe(alert, 0, "r");
+ if (bpipe) {
+ while (fgets(line, sizeof(line), bpipe->rfd)) {
+ Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
+ }
+ status = close_bpipe(bpipe);
+ } else {
+ status = errno;
+ }
+ if (status != 0) {
+ berrno be;
+ Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
+ alert, be.strerror(status));
+ }
+
+ Dmsg1(400, "alert status=%d\n", status);
+ free_pool_memory(alert);
+ }
+ unlock_device(dev);
+ free_dcr(dcr);
+ if (was_reading) {
+ jcr->read_dcr = NULL;