]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dircmd.c
Clean up some SD message nos.
[bacula/bacula] / bacula / src / stored / dircmd.c
index 598b8644676e57ee9f284953f1116d56e60d3676..aa0f57f581b3f32c917cec093a8290f533bf003e 100644 (file)
@@ -19,7 +19,7 @@
  *  
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 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
@@ -62,13 +62,17 @@ extern int job_cmd(JCR *jcr);
 
 /* Forward referenced functions */
 static int label_cmd(JCR *jcr);
+static int relabel_cmd(JCR *jcr);
 static int setdebug_cmd(JCR *jcr);
 static int cancel_cmd(JCR *cjcr);
 static int mount_cmd(JCR *jcr);
 static int unmount_cmd(JCR *jcr);
 static int status_cmd(JCR *sjcr);
-static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname, 
-                              int Slot);
+static int autochanger_cmd(JCR *sjcr);
+static int do_label(JCR *jcr, int relabel);
+static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
+                              char *newname, char *poolname, 
+                              int Slot, int relabel);
 static void send_blocked_status(JCR *jcr, DEVICE *dev);
 
 struct s_cmds {
@@ -84,9 +88,11 @@ static struct s_cmds cmds[] = {
    {"setdebug=", setdebug_cmd},       /* set debug level */
    {"cancel",    cancel_cmd},
    {"label",     label_cmd},          /* label a tape */
+   {"relabel",   relabel_cmd},        /* relabel a tape */
    {"mount",     mount_cmd},
    {"unmount",   unmount_cmd},
    {"status",    status_cmd},
+   {"autochanger", autochanger_cmd},
    {NULL,       NULL}                /* list terminator */
 };
 
@@ -106,7 +112,7 @@ static struct s_cmds cmds[] = {
  *  - We execute the command
  *  - We continue or exit depending on the return status
  */
-void connection_request(void *arg)
+void *connection_request(void *arg)
 {
    BSOCK *bs = (BSOCK *)arg;
    JCR *jcr;
@@ -115,16 +121,19 @@ void connection_request(void *arg)
    char name[MAX_NAME_LENGTH];
 
    if (bnet_recv(bs) <= 0) {
-      Emsg0(M_ERROR, 0, "Connection request failed.\n");
-      return;
+      Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
+      return NULL;
    }
 
    /* 
     * See if this is a File daemon connection
     */
+   if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)+25) {
+      Emsg1(M_ERROR, 0, _("Invalid Dir connection. Len=%d\n"), bs->msglen);
+   }
    if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
       handle_filed_connection(bs, name);
-      return;
+      return NULL;
    }
    
    jcr = new_jcr(sizeof(JCR), stored_free_jcr);     /* create Job Control Record */
@@ -138,12 +147,11 @@ void connection_request(void *arg)
    if (!authenticate_director(jcr)) {
       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
       free_jcr(jcr);
-      return;
+      return NULL;
    }
    Dmsg0(90, "Message channel init completed.\n");
 
    for (quit=0; !quit;) {
-
       /* Read command */
       if ((bnet_stat = bnet_recv(bs)) <= 0) {
         break;                       /* connection terminated */
@@ -170,7 +178,7 @@ void connection_request(void *arg)
       bnet_sig(bs, BNET_TERMINATE);
    }
    free_jcr(jcr);
-   return;
+   return NULL;
 }
 
 /*
@@ -208,7 +216,7 @@ static int cancel_cmd(JCR *cjcr)
       } else {
         P(jcr->mutex);
         oldStatus = jcr->JobStatus;
-        set_jcr_job_status(jcr, JS_Cancelled);
+        set_jcr_job_status(jcr, JS_Canceled);
         if (!jcr->authenticated && oldStatus == JS_WaitFD) {
            pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
         }
@@ -216,8 +224,14 @@ static int cancel_cmd(JCR *cjcr)
         if (jcr->file_bsock) {
            bnet_sig(jcr->file_bsock, BNET_TERMINATE);
         }
-         bnet_fsend(dir, _("3000 Job %s Status=%c marked to be cancelled.\n"), 
-           jcr->Job, oldStatus);
+        /* If thread waiting on mount, wake him */
+        if (jcr->device && jcr->device->dev &&      
+             (jcr->device->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
+              jcr->device->dev->dev_blocked == BST_UNMOUNTED ||
+              jcr->device->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
+            pthread_cond_signal(&jcr->device->dev->wait_next_vol);
+        }
+         bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
         free_jcr(jcr);
       }
    } else {
@@ -233,21 +247,44 @@ static int cancel_cmd(JCR *cjcr)
  */
 static int label_cmd(JCR *jcr) 
 {
-   POOLMEM *dname, *volname, *poolname, *mtype;
+   return do_label(jcr, 0);
+}
+
+static int relabel_cmd(JCR *jcr) 
+{
+   return do_label(jcr, 1);
+}
+
+static int do_label(JCR *jcr, int relabel)  
+{
+   POOLMEM *dname, *newname, *oldname, *poolname, *mtype;
    BSOCK *dir = jcr->dir_bsock;
    DEVRES *device;
    DEVICE *dev;
-   int found = 0;
+   int found = 0, ok = 0;
    int slot;   
 
    dname = get_memory(dir->msglen+1);
-   volname = get_memory(dir->msglen+1);
+   newname = get_memory(dir->msglen+1);
+   oldname = get_memory(dir->msglen+1);
    poolname = get_memory(dir->msglen+1);
    mtype = get_memory(dir->msglen+1);
-   if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
-       dname, volname, poolname, mtype, &slot) == 5) {
+   if (relabel) {
+      if (sscanf(dir->msg, "relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d",
+         dname, oldname, newname, poolname, mtype, &slot) == 6) {
+        ok = 1;
+      }
+   } else {
+      *oldname = 0;
+      if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
+         dname, newname, poolname, mtype, &slot) == 5) {
+        ok = 1;
+      }
+   }
+   if (ok) {
       unbash_spaces(dname);
-      unbash_spaces(volname);
+      unbash_spaces(newname);
+      unbash_spaces(oldname);
       unbash_spaces(poolname);
       unbash_spaces(mtype);
       device = NULL;
@@ -268,10 +305,10 @@ static int label_cmd(JCR *jcr)
 
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!(dev->state & ST_OPENED)) {
-           if (open_dev(dev, volname, READ_WRITE) < 0) {
+           if (open_dev(dev, newname, READ_WRITE) < 0) {
                bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
            } else {
-              label_volume_if_ok(jcr, dev, volname, poolname, slot);
+              label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
               force_close_dev(dev);
            }
          /* Under certain "safe" conditions, we can steal the lock */
@@ -279,7 +316,7 @@ static int label_cmd(JCR *jcr)
                    (dev->dev_blocked == BST_UNMOUNTED ||
                     dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
                     dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
-           label_volume_if_ok(jcr, dev, volname, poolname, slot);
+           label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
         } else if (dev->state & ST_READ || dev->num_writers) {
            if (dev->state & ST_READ) {
                 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
@@ -289,7 +326,7 @@ static int label_cmd(JCR *jcr)
                   dev_name(dev), dev->num_writers);
            }
         } else {                     /* device not being used */
-           label_volume_if_ok(jcr, dev, volname, poolname, slot);
+           label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
         }
         V(dev->mutex);
       } else {
@@ -297,11 +334,12 @@ static int label_cmd(JCR *jcr)
       }
    } else {
       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
-      strcpy(dname, dir->msg);
-      bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
    }
    free_memory(dname);
-   free_memory(volname);
+   free_memory(oldname);
+   free_memory(newname);
    free_memory(poolname);
    free_memory(mtype);
    bnet_sig(dir, BNET_EOD);
@@ -314,8 +352,9 @@ static int label_cmd(JCR *jcr)
  *
  *  Enter with the mutex set
  */
-static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
-                              int slot) 
+static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname, 
+                              char *newname, char *poolname,
+                              int slot, int relabel)
 {
    BSOCK *dir = jcr->dir_bsock;
    DEV_BLOCK *block;
@@ -323,7 +362,7 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolnam
    
    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
    
-   strcpy(jcr->VolumeName, vname);
+   strcpy(jcr->VolumeName, newname);
    jcr->VolCatInfo.Slot = slot;
    autoload_device(jcr, dev, 0, dir);     /* autoload if possible */
    block = new_block(dev);
@@ -334,7 +373,7 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolnam
          if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
             sleep(30);
          }
-          bnet_fsend(dir, _("3903 Unable to open device %s. ERR=%s\n"), 
+          bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"), 
             dev_name(dev), strerror_dev(dev));
          goto bail_out;
        }
@@ -346,21 +385,33 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolnam
       case VOL_VERSION_ERROR:
       case VOL_LABEL_ERROR:
       case VOL_OK:
-         bnet_fsend(dir, _("3901 Cannot label Volume because it is \
-already labeled: %s\n"), dev->VolHdr.VolName);
-        break;
+        if (!relabel) {
+           bnet_fsend(dir, _(
+               "3911 Cannot label Volume because it is already labeled: %s\n"), 
+               dev->VolHdr.VolName);
+           break;
+        }
+        /* Relabel request. If oldname matches, continue */
+        if (strcmp(oldname, dev->VolHdr.VolName) != 0) {
+            bnet_fsend(dir, _("Wrong volume mounted.\n"));
+           break;
+        }
+        /* Fall through wanted! */
       case VOL_IO_ERROR:
       case VOL_NO_LABEL:
-        if (!write_volume_label_to_dev(jcr, jcr->device, vname, poolname)) {
-            bnet_fsend(dir, _("3903 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
+        if (!write_volume_label_to_dev(jcr, jcr->device, newname, poolname)) {
+            bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
            break;
         }
-        strcpy(jcr->VolumeName, vname);
+        strcpy(jcr->VolumeName, newname);
          bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"), 
-           vname, dev->dev_name);
+           newname, dev->dev_name);
+        break;
+      case VOL_NO_MEDIA:
+         bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
         break;
       default:
-         bnet_fsend(dir, _("3902 Cannot label Volume. \
+         bnet_fsend(dir, _("3913 Cannot label Volume. \
 Unknown status %d from read_volume_label()\n"), jcr->label_status);
         break;
    }
@@ -412,13 +463,13 @@ static int read_label(JCR *jcr, DEVICE *dev)
  */
 static int mount_cmd(JCR *jcr)
 {
-   char *dev_name;
+   POOLMEM *dev_name;
    BSOCK *dir = jcr->dir_bsock;
    DEVRES *device;
    DEVICE *dev;
    int found = 0;
 
-   dev_name = (char *) get_memory(dir->msglen);
+   dev_name = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
       unbash_spaces(dev_name);
       device = NULL;
@@ -466,10 +517,11 @@ static int mount_cmd(JCR *jcr)
                  pthread_cond_signal(&dev->wait_next_vol);
               }
               if (dev->state & ST_LABEL) {
-                  bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"), 
+                  bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
                     dev->dev_name, dev->VolHdr.VolName);
               } else {
-                  bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
+                  bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
+                                    "Try unmounting and remounting the Volume.\n"),
                             dev->dev_name);
               }
               break;
@@ -486,10 +538,11 @@ static int mount_cmd(JCR *jcr)
            case BST_NOT_BLOCKED:
               if (dev->state & ST_OPENED) {
                  if (dev->state & ST_LABEL) {
-                     bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
+                     bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
                        dev->dev_name, dev->VolHdr.VolName);
                  } else {
-                     bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
+                     bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"   
+                                    "Try unmounting and remounting the Volume.\n"),
                                dev->dev_name);
                  }
               } else {
@@ -504,10 +557,11 @@ static int mount_cmd(JCR *jcr)
                  }
                  read_label(jcr, dev);
                  if (dev->state & ST_LABEL) {
-                     bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"), 
+                     bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
                        dev->dev_name, dev->VolHdr.VolName);
                  } else {
-                     bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
+                     bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
+                                       "Try unmounting and remounting the Volume.\n"),
                                dev->dev_name);
                  }
               }
@@ -522,8 +576,8 @@ static int mount_cmd(JCR *jcr)
          bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
       }
    } else {
-      strcpy(dev_name, dir->msg);
-      bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), jcr->errmsg);
    }
    free_memory(dev_name);
    bnet_sig(dir, BNET_EOD);
@@ -535,13 +589,13 @@ static int mount_cmd(JCR *jcr)
  */
 static int unmount_cmd(JCR *jcr)
 {
-   char *dname;
+   POOLMEM *dname;
    BSOCK *dir = jcr->dir_bsock;
    DEVRES *device;
    DEVICE *dev;
    int found = 0;
 
-   dname = (char *) get_memory(dir->msglen+1);
+   dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "unmount %s", dname) == 1) {
       unbash_spaces(dname);
       device = NULL;
@@ -566,7 +620,7 @@ static int unmount_cmd(JCR *jcr)
         } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
               dev->dev_blocked);
-           if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
+           if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
               offline_dev(dev);
            }
            force_close_dev(dev);
@@ -595,7 +649,7 @@ static int unmount_cmd(JCR *jcr)
         } else {                     /* device not being used */
             Dmsg0(90, "Device not in use, unmounting\n");
            block_device(dev, BST_UNMOUNTED);
-           if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
+           if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
               offline_dev(dev);
            }
            force_close_dev(dev);
@@ -607,8 +661,8 @@ static int unmount_cmd(JCR *jcr)
       }
    } else {
       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
-      strcpy(dname, dir->msg);
-      bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
    }
    free_memory(dname);
    bnet_sig(dir, BNET_EOD);
@@ -627,7 +681,7 @@ static int status_cmd(JCR *jcr)
    char dt[MAX_TIME_LENGTH];
    char b1[30], b2[30], b3[30];
 
-   bnet_fsend(user, "\n%s Version: " VERSION " (" DATE ")\n", my_name);
+   bnet_fsend(user, "\n%s Version: " VERSION " (" BDATE ")\n", my_name);
    bstrftime(dt, sizeof(dt), daemon_start_time);
    bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
         last_job.NumJobs == 1 ? "" : "s");
@@ -646,11 +700,10 @@ static int status_cmd(JCR *jcr)
 
    LockRes();
    for (device=NULL;  (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
-      dev = device->dev;
-      if (dev) {
+      for (dev=device->dev; dev; dev=dev->next) {
         if (dev->state & ST_OPENED) {
            if (dev->state & ST_LABEL) {
-               bnet_fsend(user, _("Device %s is mounted with Volume %s\n"), 
+               bnet_fsend(user, _("Device %s is mounted with Volume \"%s\"\n"), 
                  dev_name(dev), dev->VolHdr.VolName);
            } else {
                bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
@@ -686,10 +739,11 @@ static int status_cmd(JCR *jcr)
            job_type_to_str(jcr->JobType), jcr->Job);
       }
       if (jcr->device) {
-         bnet_fsend(user, _("%s %s job %s is using device %s\n"), 
+         bnet_fsend(user, _("%s %s job %s is using device %s volume %s\n"), 
                   job_level_to_str(jcr->JobLevel),
                   job_type_to_str(jcr->JobType),
-                  jcr->Job, jcr->device->device_name);
+                  jcr->Job, jcr->device->device_name,
+                  jcr->VolumeName);
         sec = time(NULL) - jcr->run_time;
         if (sec <= 0) {
            sec = 1;
@@ -754,3 +808,70 @@ static void send_blocked_status(JCR *jcr, DEVICE *dev)
         break;
    }
 }
+
+/*
+ * Autochanger command from Director
+ */
+static int autochanger_cmd(JCR *jcr)
+{
+   POOLMEM *devname;
+   BSOCK *dir = jcr->dir_bsock;
+   DEVRES *device;
+   DEVICE *dev;
+   int found = 0;
+
+   devname = get_memory(dir->msglen+1);
+   if (sscanf(dir->msg, "autochanger list %s ", devname) == 1) {
+      unbash_spaces(devname);
+      device = NULL;
+      LockRes();
+      while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
+        /* Find resource, and make sure we were able to open it */
+        if (strcmp(device->hdr.name, devname) == 0 && device->dev) {
+            Dmsg1(20, "Found device %s\n", device->hdr.name);
+           found = 1;
+           break;
+        }
+      }
+      UnlockRes();
+      if (found) {
+        jcr->device = device;
+        dev = device->dev;
+        P(dev->mutex);               /* Use P to avoid indefinite block */
+        if (!(dev->state & ST_OPENED)) {
+           if (open_dev(dev, NULL, READ_WRITE) < 0) {
+               bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
+           } else {
+              autochanger_list(jcr, dev, dir);
+              force_close_dev(dev);
+           }
+         /* Under certain "safe" conditions, we can steal the lock */
+        } else if (dev->dev_blocked && 
+                   (dev->dev_blocked == BST_UNMOUNTED ||
+                    dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
+                    dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
+           autochanger_list(jcr, dev, dir);
+        } else if (dev->state & ST_READ || dev->num_writers) {
+           if (dev->state & ST_READ) {
+                bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
+                  dev_name(dev));
+           } else {
+                bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
+                  dev_name(dev), dev->num_writers);
+           }
+        } else {                     /* device not being used */
+           autochanger_list(jcr, dev, dir);
+        }
+        V(dev->mutex);
+      } else {
+         bnet_fsend(dir, _("3999 Device %s not found\n"), devname);
+      }
+   } else {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      bnet_fsend(dir, _("3907 Error scanning autocharger list command: %s\n"),
+        jcr->errmsg);
+   }
+   free_memory(devname);
+   bnet_sig(dir, BNET_EOD);
+   return 1;
+}