chiark / gitweb /
build-depends tetex-extra
[userv.git] / client.c
index 9fb76df18c34be6822de85c953d57bc3d9a0ef7a..5f60d0e5cb0d2458b43c80871540812fe5f9dad5 100644 (file)
--- a/client.c
+++ b/client.c
@@ -2,7 +2,7 @@
  * userv - client.c
  * client code
  *
- * Copyright (C)1996-1997,1999 Ian Jackson
+ * Copyright (C)1996-1997,1999-2001,2003 Ian Jackson
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
@@ -1163,6 +1163,21 @@ static void prepare_asynchsignals(void) {
   if (alarm(timeout)<0) syscallerror("set up timeout alarm");
 }
 
+
+static void close_unwanted_pipes(void) {
+  int fd;
+
+  for (fd=0; fd<fdsetupsize; fd++) {
+    if (!fdsetup[fd].filename) continue;
+    if (close(fdsetup[fd].pipefd)) fsyscallerror("close pipe fd for %d",fd);
+    if (fdsetup[fd].copyfd>2)
+      if (close(fdsetup[fd].copyfd))
+       if (errno != EBADF)
+         /* EBADF can be induced if cmd line specifies same fd twice */
+         fsyscallerror("close real fd for %d",fd);
+  }
+}
+
 static void catdup(const char *which, int from, int to) {
   if (dup2(from,to)<0) {
     blocksignals(SIG_BLOCK);
@@ -1205,14 +1220,13 @@ static void connect_pipes(void) {
       reading= fdsetup[fd].mods & fdm_read;
       catdup(catnamebuf, fdsetup[fd].copyfd, reading ? 0 : 1);
       catdup(catnamebuf, fdsetup[fd].pipefd, reading ? 1 : 0);
+      close_unwanted_pipes();
       execl("/bin/cat",catnamebuf,(char*)0);
       fprintf(stderr,"userv: %s: cannot exec `cat': %s\n",catnamebuf,strerror(errno));
       exit(-1);
     }
-    if (fdsetup[fd].copyfd>2)
-      if (close(fdsetup[fd].copyfd)) fsyscallerror("close real fd for %d",fd);
-    if (close(fdsetup[fd].pipefd)) fsyscallerror("close pipe fd for %d",fd);
   }
+  close_unwanted_pipes();
 }
 
 static void server_sendconfirm(void) {