]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dircmd.c
Add more DCRs
[bacula/bacula] / bacula / src / stored / dircmd.c
index efc3343bcf0eed840dd431d283da19ae73bfa692..82ec5bc78dd09b552f0e872de926554cf93795be 100644 (file)
@@ -19,7 +19,7 @@
  *  
  */
 /*
-   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
@@ -72,14 +72,14 @@ 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, 
                               int Slot, int relabel);
 
 struct s_cmds {
-   char *cmd;
+   const char *cmd;
    int (*func)(JCR *jcr);
 };
 
@@ -118,16 +118,18 @@ 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;
-   int i, found, quit;
+   int i;
+   bool found, quit;
    int bnet_stat = 0;
    char name[MAX_NAME_LENGTH];
 
    if (bnet_recv(bs) <= 0) {
       Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
+      bnet_close(bs);
       return NULL;
    }
 
@@ -136,19 +138,30 @@ 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
     *  call FD handler.
     */
-   if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
+   Dmsg1(110, "Conn: %s", bs->msg);
+   if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
       handle_filed_connection(bs, name);
       return NULL;
    }
    
+   Dmsg0(110, "Start Dir Job\n");
    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");
 
@@ -168,20 +181,20 @@ void *connection_request(void *arg)
         break;                       /* connection terminated */
       }
       Dmsg1(9, "<dird: %s\n", bs->msg);
-      found = FALSE;
+      found = false;
       for (i=0; cmds[i].cmd; i++) {
         if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
            if (!cmds[i].func(jcr)) {    /* do command */
-              quit = TRUE;              /* error, get out */
+              quit = true;              /* error, get out */
                Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
            }
-           found = TRUE;            /* indicate command found */
+           found = true;            /* indicate command found */
            break;
         }
       }
       if (!found) {                  /* command not found */
         bnet_fsend(bs, derrmsg);
-        quit = TRUE;
+        quit = true;
         break;
       }
    }
@@ -197,14 +210,15 @@ void *connection_request(void *arg)
 static int setdebug_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
-   int level;
+   int level, trace_flag;
 
    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
-   if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
+   if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
       bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
       return 0;
    }
    debug_level = level;
+   set_trace(trace_flag);
    return bnet_fsend(dir, OKsetdebug, level);
 }
 
@@ -234,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);
@@ -294,18 +308,13 @@ 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)) {
-           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, oldname, newname, poolname, slot, relabel);
-              force_close_dev(dev);
-           }
+           label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
+           force_close_dev(dev);
          /* Under certain "safe" conditions, we can steal the lock */
         } else if (dev->dev_blocked && 
                    (dev->dev_blocked == BST_UNMOUNTED ||
@@ -356,15 +365,17 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
    bsteal_lock_t hold;
    
    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
+   block = new_block(dev);
    
    pm_strcpy(&jcr->VolumeName, newname);
    jcr->VolCatInfo.Slot = slot;
-   autoload_device(jcr, dev, 0, dir);     /* autoload if possible */
-   block = new_block(dev);
+   if (autoload_device(jcr, dev, 0, dir) < 0) {    /* autoload if possible */
+      goto bail_out;
+   }
 
    /* Ensure that the device is open -- autoload_device() closes it */
    for ( ; !(dev->state & ST_OPENED); ) {
-      if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
+      if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
          bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"), 
            dev_name(dev), strerror_dev(dev));
         goto bail_out;
@@ -391,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;
       }
@@ -448,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;
@@ -464,8 +475,17 @@ static bool find_device(JCR *jcr, char *dname)
         break;
       }
    }
+   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;
 }
 
 
@@ -480,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:
@@ -497,7 +517,7 @@ static int mount_cmd(JCR *jcr)
         case BST_UNMOUNTED_WAITING_FOR_SYSOP:
         case BST_UNMOUNTED:
            /* We freed the device, so reopen it and wake any waiting threads */
-           if (open_dev(dev, NULL, READ_WRITE) < 0) {
+           if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
                bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
                  strerror_dev(dev));
               break;
@@ -549,14 +569,14 @@ static int mount_cmd(JCR *jcr)
                   bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
                  break;
               }
-              if (open_dev(dev, NULL, READ_WRITE) < 0) {
+              if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
                   bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
                     strerror_dev(dev));
                  break;
               }
               read_label(jcr, dev);
               if (dev_state(dev, ST_LABEL)) {
-                  bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
+                  bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"), 
                     dev_name(dev), dev->VolHdr.VolName);
               } else {
                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
@@ -594,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");
@@ -670,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");
@@ -731,18 +751,13 @@ 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));
         } else 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);
-           }
+           autochanger_list(jcr, dev, dir);
          /* Under certain "safe" conditions, we can steal the lock */
         } else if (dev->dev_blocked && 
                    (dev->dev_blocked == BST_UNMOUNTED ||
@@ -785,17 +800,12 @@ 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)) {
-           if (open_dev(dev, NULL, READ_WRITE) < 0) {
-               bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
-           } else {
-              read_volume_label(jcr, dev, Slot);
-              force_close_dev(dev);
-           }
+           read_volume_label(jcr, dev, Slot);
+           force_close_dev(dev);
          /* Under certain "safe" conditions, we can steal the lock */
         } else if (dev->dev_blocked && 
                    (dev->dev_blocked == BST_UNMOUNTED ||
@@ -838,15 +848,17 @@ static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
    bsteal_lock_t hold;
    
    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
+   block = new_block(dev);
    
    jcr->VolumeName[0] = 0;
    jcr->VolCatInfo.Slot = Slot;
-   autoload_device(jcr, dev, 0, dir);     /* autoload if possible */
-   block = new_block(dev);
+   if (autoload_device(jcr, dev, 0, dir) < 0) {    /* autoload if possible */
+      goto bail_out;
+   }
 
    /* Ensure that the device is open -- autoload_device() closes it */
    for ( ; !dev_state(dev, ST_OPENED); ) {
-      if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
+      if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
          bnet_fsend(dir, _("3910 Unable to open device \"%s\". ERR=%s\n"), 
            dev_name(dev), strerror_dev(dev));
         goto bail_out;
@@ -856,7 +868,8 @@ static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
    dev->state &= ~ST_LABEL;          /* force read of label */
    switch (read_dev_volume_label(jcr, dev, block)) {               
    case VOL_OK:
-      bnet_fsend(dir, _("3001 Volume=\"%s\" Slot=%d\n"), dev->VolHdr.VolName, Slot);
+      /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
+      bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolName, Slot);
       Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolName);
       break;
    default: