]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dircmd.c
Add more DCRs
[bacula/bacula] / bacula / src / stored / dircmd.c
index 39c33bfecc4a2ae3d26acd099408c3e199e7dece..82ec5bc78dd09b552f0e872de926554cf93795be 100644 (file)
@@ -72,7 +72,7 @@ static int mount_cmd(JCR *jcr);
 static int unmount_cmd(JCR *jcr);
 static int autochanger_cmd(JCR *sjcr);
 static int do_label(JCR *jcr, int relabel);
-static bool find_device(JCR *jcr, char *dname);
+static DEVICE *find_device(JCR *jcr, char *dname);
 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
                               char *newname, char *poolname, 
@@ -118,7 +118,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 *handle_connection_request(void *arg)
 {
    BSOCK *bs = (BSOCK *)arg;
    JCR *jcr;
@@ -129,6 +129,7 @@ void *connection_request(void *arg)
 
    if (bnet_recv(bs) <= 0) {
       Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
+      bnet_close(bs);
       return NULL;
    }
 
@@ -137,6 +138,8 @@ void *connection_request(void *arg)
     */
    if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
       Emsg1(M_ERROR, 0, _("Invalid Dir connection. Len=%d\n"), bs->msglen);
+      bnet_close(bs);
+      return NULL;
    }
    /* 
     * See if this is a File daemon connection. If so
@@ -152,6 +155,13 @@ void *connection_request(void *arg)
    jcr = new_jcr(sizeof(JCR), stored_free_jcr);     /* create Job Control Record */
    jcr->dir_bsock = bs;              /* save Director bsock */
    jcr->dir_bsock->jcr = jcr;
+   /* Initialize FD start condition variable */
+   int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
+   if (errstat != 0) {
+      Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
+      free_jcr(jcr);
+      return NULL;
+   }
 
    Dmsg0(1000, "stored in start_job\n");
 
@@ -238,11 +248,11 @@ static int cancel_cmd(JCR *cjcr)
            bnet_sig(jcr->file_bsock, BNET_TERMINATE);
         }
         /* 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);
+        if (jcr->dcr && jcr->dcr->dev &&
+             (jcr->dcr->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
+              jcr->dcr->dev->dev_blocked == BST_UNMOUNTED ||
+              jcr->dcr->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
+            pthread_cond_signal(&jcr->dcr->dev->wait_next_vol);
         }
          bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
         free_jcr(jcr);
@@ -298,10 +308,9 @@ static int do_label(JCR *jcr, int relabel)
       unbash_spaces(oldname);
       unbash_spaces(poolname);
       unbash_spaces(mtype);
-      if (find_device(jcr, dname)) {
+      dev = find_device(jcr, dname);
+      if (dev) {
         /******FIXME**** compare MediaTypes */
-        dev = jcr->device->dev;
-
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!(dev->state & ST_OPENED)) {
            label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
@@ -393,7 +402,7 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
       /* Fall through wanted! */
    case VOL_IO_ERROR:
    case VOL_NO_LABEL:
-      if (!write_volume_label_to_dev(jcr, jcr->device, newname, poolname)) {
+      if (!write_new_volume_label_to_dev(jcr, dev, newname, poolname)) {
          bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
         break;
       }
@@ -450,7 +459,7 @@ static int read_label(JCR *jcr, DEVICE *dev)
    return stat;
 }
 
-static bool find_device(JCR *jcr, char *dname)
+static DEVICE *find_device(JCR *jcr, char *dname)
 {
    DEVRES *device = NULL;
    bool found = false;
@@ -467,10 +476,16 @@ static bool find_device(JCR *jcr, char *dname)
       }
    }
    if (found) {
+      /*
+       * ****FIXME*****  device->dev may not point to right device
+       *  if there are multiple devices open
+       */
       jcr->dcr = new_dcr(jcr, device->dev);
+      UnlockRes();
+      return jcr->dcr->dev;
    }
    UnlockRes();
-   return found;
+   return NULL;
 }
 
 
@@ -485,9 +500,9 @@ static int mount_cmd(JCR *jcr)
 
    dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "mount %s", dname) == 1) {
-      if (find_device(jcr, dname)) {
+      dev = find_device(jcr, dname);
+      if (dev) {
         DEV_BLOCK *block;
-        dev = jcr->device->dev;
         P(dev->mutex);               /* Use P to avoid indefinite block */
         switch (dev->dev_blocked) {         /* device blocked? */
         case BST_WAITING_FOR_SYSOP:
@@ -599,8 +614,8 @@ static int unmount_cmd(JCR *jcr)
 
    dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "unmount %s", dname) == 1) {
-      if (find_device(jcr, dname)) {
-        dev = jcr->device->dev;
+      dev = find_device(jcr, dname);
+      if (dev) {
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!(dev->state & ST_OPENED)) {
             Dmsg0(90, "Device already unmounted\n");
@@ -675,8 +690,8 @@ static int release_cmd(JCR *jcr)
 
    dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "release %s", dname) == 1) {
-      if (find_device(jcr, dname)) {
-        dev = jcr->device->dev;
+      dev = find_device(jcr, dname);
+      if (dev) {
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!(dev->state & ST_OPENED)) {
             Dmsg0(90, "Device already released\n");
@@ -736,8 +751,8 @@ static int autochanger_cmd(JCR *jcr)
 
    dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "autochanger list %s ", dname) == 1) {
-      if (find_device(jcr, dname)) {
-        dev = jcr->device->dev;
+      dev = find_device(jcr, dname);
+      if (dev) {
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!dev_is_tape(dev)) {
             bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), dev_name(dev));
@@ -785,9 +800,8 @@ static int readlabel_cmd(JCR *jcr)
 
    dname = get_memory(dir->msglen+1);
    if (sscanf(dir->msg, "readlabel %s Slot=%d", dname, &Slot) == 2) {
-      if (find_device(jcr, dname)) {
-        dev = jcr->device->dev;
-
+      dev = find_device(jcr, dname);
+      if (dev) {
         P(dev->mutex);               /* Use P to avoid indefinite block */
         if (!dev_state(dev, ST_OPENED)) {
            read_volume_label(jcr, dev, Slot);