Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: allocating a new file descriptor



Stephane Chazelas wrote:
> I would find it more consistent to
> have:
> 
> sysopen -w file
> fd=$REPLY

Yes, that could be added to the zsh/system module easily enough, but it
would be zsh-specific.  I've simply never needed it up to now.

> I find the:
> 
> print ... {fd}> file
> 
> that keeps the file open a bit confusing and unconsistent (and
> useless).

You mean with print?  That's why the manual now suggests using it with
exec.

The syntax is designed to be consistent with existing standard shell
syntax, which imposes quite strong limitations.

One annoyance is that if you use an older version of the shell, the exec
tries to execute {fd} and disappears, which isn't a particularly
graceful way of failing.

> It would be good to have a sysclose as well. At least until
> recently, exec 12>&- didn't work. I had the problem with my
> mouse support, where I couldn't close the fd opened by zsocket
> for gpm interaction.

That syntax is deliberately restricted to single digits and will remain
so.  You can now do:

fd=12
exec {fd}>&-

(Presumably you already have the fd in a parameter, in fact.)  The
socket module is more limited than the tcp module; it doesn't record
sessions, so has no way of knowing which fd's you might want to close.
That may be why there's no zsocket -c.

> I also noticed several problems when fidling with fds used
> internally by zsh (zsocket -d10 or -d11 ended up in a core
> dump).

Yes, it doesn't check for existing use of the fd by the shell.

The same problem exists with the new syntax when closing fd's; I've made
it check for special fd's, which are those from 10 upwards marked as for
internal use.  This covers 10, but not 11; I'm not sure what opened
that, but I think it's some library rather than the shell itself.
If we can track it down it could be marked as in internal use, too.

(Maybe it's about time we introduced zwarn and friends to stdarg.h...)

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.88
diff -u -r1.88 exec.c
--- Src/exec.c	14 Apr 2005 16:24:52 -0000	1.88
+++ Src/exec.c	15 Apr 2005 10:18:15 -0000
@@ -77,7 +77,7 @@
  * by zclose.                                                      */
 
 /**/
-unsigned char *fdtable;
+mod_export unsigned char *fdtable;
 
 /* The allocated size of fdtable */
 
@@ -87,7 +87,7 @@
 /* The highest fd that marked with nonzero in fdtable */
 
 /**/
-int max_zsh_fd;
+mod_export int max_zsh_fd;
 
 /* input fd from the coprocess */
 
@@ -2360,13 +2360,22 @@
 			bad = 2;
 		    } else {
 			fn->fd1 = (int)getintvalue(v);
-			bad = errflag;
+			if (errflag)
+			    bad = 1;
+			else if (fn->fd1 > max_zsh_fd)
+			    bad = 3;
+			else if (fn->fd1 >= 10 &&
+				 fdtable[fn->fd1] == FDT_INTERNAL)
+			    bad = 4;
 		    }
 		    if (bad) {
-			zwarn(bad == 2 ?
-			      "can't close file descriptor from readonly parameter" :
-			      "parameter %s does not contain a file descriptor",
-			      fn->varid, 0);
+			const char *bad_msg[] = {
+			    "parameter %s does not contain a file descriptor",
+			    "can't close file descriptor from readonly parameter %s",
+			    "file descriptor %d out of range, not closed",
+			    "file descriptor %d used by shell, not closed"
+			};
+			zwarn(bad_msg[bad-1], fn->varid, fn->fd1);
 			execerr();
 		    }
 		}
Index: Src/Modules/socket.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/socket.c,v
retrieving revision 1.7
diff -u -r1.7 socket.c
--- Src/Modules/socket.c	16 Nov 2004 11:05:04 -0000	1.7
+++ Src/Modules/socket.c	15 Apr 2005 10:18:15 -0000
@@ -78,6 +78,11 @@
 		     OPT_ARG(ops, 'd'), 0);
 	    return 1;
 	}
+	if (targetfd <= max_zsh_fd && fdtable[targetfd] != FDT_UNUSED) {
+	    zwarnnam(nam, "file descriptor %d is in use by the shell",
+		     NULL, targetfd);
+	    return 1;
+	}
     }
 
     if (OPT_ISSET(ops,'l')) {

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author