]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/autochanger.c
Implement berrno for bpipes and run_program
[bacula/bacula] / bacula / src / stored / autochanger.c
index d459689e32d639e135da001e651799d22a05dc10..342619ad304eaa00b6c9e497f8172dc27c825109 100644 (file)
@@ -7,7 +7,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -48,8 +48,10 @@ static int get_autochanger_loaded_slot(JCR *jcr);
  *          0 on failure (no changer available) 
  *         -1 on error on autochanger
  */
-int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
+int autoload_device(DCR *dcr, int writing, BSOCK *dir)
 {
+   JCR *jcr = dcr->jcr;
+   DEVICE *dev = dcr->dev;
    int slot = jcr->VolCatInfo.Slot;
    int drive = jcr->device->drive_index;
    int rtn_stat = -1;                /* error status */
@@ -62,7 +64,7 @@ int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
       if (dir) {
         return 0;                    /* For user, bail out right now */
       }
-      if (dir_find_next_appendable_volume(jcr)) {
+      if (dir_find_next_appendable_volume(dcr)) {
         slot = jcr->VolCatInfo.Slot; 
       } else {
         slot = 0;
@@ -93,6 +95,13 @@ int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
            changer = edit_device_codes(jcr, changer, 
                         jcr->device->changer_command, "unload");
            status = run_program(changer, timeout, NULL);
+           if (status != 0) {
+              berrno be;
+              be.set_errno(status);
+               Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
+                   slot, drive, be.strerror());
+           }
+
             Dmsg1(400, "unload status=%d\n", status);
         }
         /*
@@ -110,8 +119,10 @@ int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
                    slot, drive);
         } else {
-            Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
-                   slot, drive, status);
+          berrno be;
+          be.set_errno(status);
+            Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
+                   slot, drive, be.strerror());
         }
          Dmsg2(400, "load slot %d status=%d\n", slot, status);
       } else { 
@@ -155,8 +166,10 @@ static int get_autochanger_loaded_slot(JCR *jcr)
              drive);
       }
    } else {
-      Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), 
-          drive, status);
+      berrno be;
+      be.set_errno(status);
+      Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"), 
+          drive, be.strerror());
       loaded = -1;             /* force unload */
    }
    free_pool_memory(changer);
@@ -168,15 +181,17 @@ static int get_autochanger_loaded_slot(JCR *jcr)
  * The Volume is not in the correct slot, so mark this 
  *   Volume as not being in the Changer.
  */
-void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
+void invalid_slot_in_catalog(DCR *dcr)
 {
+   JCR *jcr = dcr->jcr;
+   DEVICE *dev = dcr->dev;
    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
 "    Setting slot to zero in catalog.\n"),
        jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
    jcr->VolCatInfo.InChanger = false;
    dev->VolCatInfo.InChanger = false;
    Dmsg0(100, "update vol info in mount\n");
-   dir_update_volume_info(jcr, dev, 1);  /* set new status */
+   dir_update_volume_info(dcr, true);  /* set new status */
 }
 
 /*
@@ -184,8 +199,10 @@ void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
  *   with their barcodes.
  *   We assume that it is always the Console that is calling us.
  */
-int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
+bool autochanger_list(DCR *dcr, BSOCK *dir)
 {
+   DEVICE *dev = dcr->dev;
+   JCR *jcr = dcr->jcr;
    uint32_t timeout = jcr->device->max_changer_wait;
    POOLMEM *changer;
    BPIPE *bpipe;
@@ -195,7 +212,7 @@ int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
        !jcr->device->changer_command) {
       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
-      return 0;
+      return false;
    }
 
    changer = get_pool_memory(PM_FNAME);
@@ -210,7 +227,13 @@ int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
       slot = jcr->VolCatInfo.Slot; 
       jcr->VolCatInfo.Slot = loaded;
       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
-      run_program(changer, timeout, NULL);
+      int stat = run_program(changer, timeout, NULL);
+      if (stat != 0) {
+        berrno be;
+        be.set_errno(stat);
+         Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"), 
+             loaded, be.strerror());
+      }
       jcr->VolCatInfo.Slot = slot;
    }
 
@@ -220,23 +243,27 @@ int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
    bpipe = open_bpipe(changer, timeout, "r");
    if (!bpipe) {
       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
-      goto bail_out;
+      free_pool_memory(changer);
+      return false;
    }
    /* Get output from changer */
    while (fgets(dir->msg, len, bpipe->rfd)) { 
       dir->msglen = strlen(dir->msg);
       bnet_send(dir);
    }
+   int stat = close_bpipe(bpipe);
+   if (stat != 0) {
+      berrno be;
+      be.set_errno(stat);
+      bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
+   }
    bnet_sig(dir, BNET_EOD);
-   close_bpipe(bpipe);
 
-bail_out:
    free_pool_memory(changer);
-   return 1;
+   return true;
 }
 
 
-
 /*
  * Edit codes into ChangerCommand
  *  %% = %