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

[PATCH2] Re: avoid closed stdin() in zle widgets



2017-06-12 00:15:20 -0400, Eric Cook:
[...]
> > in zle widgets, stdin currently appears to be closed. That's
> > generally not a good idea to close fds 0, 1, 2 as many commands
> > are confused when the files they open suddenly become their
> > stdin/stdout/stderr because the first free fd is 0/1/2.
[...]
> It is documented to work like that in zshzle
[...]

Well spotted. Thanks. (I also left a diff by mistake for an
unrelated issue discussed earlier on. Sorry about that).

New patch:

diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index b65e3be..bd0252f 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -750,12 +750,12 @@ sect(User-Defined Widgets)
 cindex(widgets, user-defined)
 User-defined widgets, being implemented as shell functions,
 can execute any normal shell command.  They can also run other widgets
-(whether built-in or user-defined) using the tt(zle) builtin command.
-The standard input of the function is closed to prevent external commands
-from unintentionally blocking ZLE by reading from the terminal, but
-tt(read -k) or tt(read -q) can be used to read characters.  Finally,
-they can examine and edit the ZLE buffer being edited by
-reading and setting the special parameters described below.
+(whether built-in or user-defined) using the tt(zle) builtin command. The
+standard input of the function is redirected from /dev/null to prevent
+external commands from unintentionally blocking ZLE by reading from the
+terminal, but tt(read -k) or tt(read -q) can be used to read characters.
+Finally, they can examine and edit the ZLE buffer being edited by reading
+and setting the special parameters described below.
 
 cindex(parameters, editor)
 cindex(parameters, zle)
diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c
index 6c271b5..be2b062 100644
--- a/Src/Zle/zle_main.c
+++ b/Src/Zle/zle_main.c
@@ -1485,6 +1485,13 @@ execzlefunc(Thingy func, char **args, int set_bindk)
 	    int inuse = w->flags & WIDGET_INUSE;
 	    w->flags |= WIDGET_INUSE;
 
+	    if (osi > 0) {
+		/*
+		 * Many commands don't like having a closed stdin, open on
+		 * /dev/null instead
+		 */
+		open("/dev/null", O_RDWR | O_NOCTTY); /* ignore failure */
+	    }
 	    if (*args) {
 		largs = newlinklist();
 		addlinknode(largs, dupstring(w->u.fnnam));



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