Index: client-src/amandad.c
===================================================================
RCS file: /cvsroot/amanda/amanda/client-src/amandad.c,v
retrieving revision 1.32.2.4
diff -u -r1.32.2.4 amandad.c
--- client-src/amandad.c	1999/09/10 23:27:08	1.32.2.4
+++ client-src/amandad.c	2001/05/12 00:27:01
@@ -77,7 +77,7 @@
 int argc;
 char **argv;
 {
-    int l, n, s;
+    int n;
     int fd;
     char *errstr = NULL;
     unsigned long malloc_hist_1, malloc_size_1;
@@ -97,6 +97,8 @@
 
     struct service_s *servp;
     fd_set insock;
+    fd_set outsock;
+    int fd_count;
 
     for(fd = 3; fd < FD_SETSIZE; fd++) {
 	/*
@@ -304,17 +306,6 @@
 
     default:		/* parent */
 
-        aclose(req_pipe[0]);
-        aclose(rep_pipe[1]);
-
-        reqlen = strlen(in_msg.dgram.cur);
-	for(l = 0, n = reqlen; l < n; l += s) {
-            if((s = write(req_pipe[1], in_msg.dgram.cur + l, n - l)) < 0) {
-		error("write to child pipe: %s", strerror(errno));
-	    }
-	}
-        aclose(req_pipe[1]);
-
         break; 
 
     case 0:		/* child */
@@ -329,6 +320,9 @@
 	transfer_session_key();
 #endif
 
+        aclose(req_pipe[0]); 
+        aclose(rep_pipe[1]);
+
 	/* run service */
 
 	execle(cmd, cmd, NULL, safe_env());
@@ -341,26 +335,57 @@
     add_mutual_authenticator(&out_msg.dgram);
 #endif
 
+    aclose(req_pipe[0]);
+    aclose(rep_pipe[1]);
+
+    reqlen = strlen(in_msg.dgram.cur);
+
+    fd_count = ((rep_pipe[0] > req_pipe[1]) ? rep_pipe[0] : req_pipe[1]) + 1;
     while(1) {
 
 	FD_ZERO(&insock);
 	FD_SET(0, &insock);
-	FD_SET(rep_pipe[0], &insock);
+	if(rep_pipe[0] >= 0) {
+	    FD_SET(rep_pipe[0], &insock);
+	}
+	FD_ZERO(&outsock);
+	if(req_pipe[1] >= 0) {
+	    FD_SET(req_pipe[1], &outsock);
+	}
 
-	if(select(rep_pipe[0]+1, (SELECT_ARG_TYPE *)&insock, NULL, NULL, NULL) < 0)
+	rc = select(fd_count,
+		    (SELECT_ARG_TYPE *)&insock,
+		    (SELECT_ARG_TYPE *)&outsock,
+		    NULL,
+		    NULL);
+	if(rc < 0) {
 	    error("select failed: %s", strerror(errno));
+	}
 
-	if(FD_ISSET(rep_pipe[0],&insock)) {
+	if(rep_pipe[0] >= 0 && FD_ISSET(rep_pipe[0],&insock)) {
 	    if((rc = read(rep_pipe[0], out_msg.dgram.cur+dglen,
 			  MAX_DGRAM-out_msg.dgram.len)) <= 0) {
-		if (rc < 0)
+		if (rc < 0) {
 		    error("reading response pipe: %s", strerror(errno));
+		}
 		break;
 	    }
 	    dglen += rc;
 	}
-	if(!FD_ISSET(0,&insock))
+	if(req_pipe[1] >= 0 && FD_ISSET(req_pipe[1],&outsock)) {
+	    if(reqlen > 0) {
+		if((rc = write(req_pipe[1], in_msg.dgram.cur, reqlen)) < 0) {
+		    error("write to child pipe: %s", strerror(errno));
+		}
+		reqlen -= rc;
+		in_msg.dgram.cur += rc;
+	    } else {
+		aclose(req_pipe[1]);
+	    }
+	}
+	if(!FD_ISSET(0,&insock)) {
 	    continue;
+	}
 
 	if((n = dgram_recv(&dup_msg.dgram, RECV_TIMEOUT, &dup_msg.peer)) <= 0) {
 	    char *s;
@@ -414,6 +439,7 @@
 
     out_msg.dgram.len += dglen;
     out_msg.dgram.data[out_msg.dgram.len] = '\0';
+    aclose(req_pipe[1]);
     aclose(rep_pipe[0]);
 
 send_response:
