zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: Re: PATCH: allocating a new file descriptor
Date: Fri, 15 Apr 2005 11:27:10 +0100	[thread overview]
Message-ID: <200504151027.j3FARBdH004522@news01.csr.com> (raw)
In-Reply-To: <20050414181020.GA5064@sc>

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@csr.com>                  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.

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


  reply	other threads:[~2005-04-15 10:27 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-12 12:57 Peter Stephenson
2005-04-12 17:35 ` Peter Stephenson
2005-04-14  4:57 ` Bart Schaefer
2005-04-14  9:49   ` Peter Stephenson
2005-04-14 14:23     ` Bart Schaefer
2005-04-14 16:16       ` Peter Stephenson
2005-04-14 18:10         ` Stephane Chazelas
2005-04-15 10:27           ` Peter Stephenson [this message]
2005-04-15 16:19             ` Bart Schaefer
2005-04-15 19:06               ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200504151027.j3FARBdH004522@news01.csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).