Imported Upstream version 2.5.1p1
[debian/amanda] / server-src / changer.c
index 02519c543aa47ea450ac3dc21c62058ac1987d1c..0d2898cd9fd949f39c5efd78f9a087b4348eaf44 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: changer.c,v 1.36 2006/08/24 01:57:16 paddy_s Exp $
+ * $Id: changer.c,v 1.36.2.1 2006/09/27 14:04:27 martinea Exp $
  *
  * interface routines for tape changers
  */
@@ -331,6 +331,7 @@ changer_command(
     char num2[NUM_STR_SIZE];
     char *cmdstr;
     pid_t pid, changer_pid = 0;
+    int fd_to_close[4], *pfd_to_close = fd_to_close;
 
     if (*tapechanger != '/') {
        tapechanger = vstralloc(libexecdir, "/", tapechanger, versionsuffix(),
@@ -360,12 +361,21 @@ changer_command(
        goto failed;
     }
 
-    /* make sure fd[0] != 1 */
-    if(fd[0] == 1) {
+    /* make sure fd[0] > 2  && fd[1] > 2 */
+    pfd_to_close = fd_to_close;
+    while(fd[0] <= 2) {
        int a = dup(fd[0]);
-       close(fd[0]);
+       *pfd_to_close++ = fd[0];
        fd[0] = a;
     }
+    while(fd[1] <= 2) {
+       int a = dup(fd[1]);
+       *pfd_to_close++ = fd[1];
+       fd[1] = a;
+    }
+    while (pfd_to_close > fd_to_close) {
+       close(*--pfd_to_close);
+    }
 
     if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
        snprintf(num1, SIZEOF(num1), "%d", fd[0]);