zsh-workers
 help / color / mirror / code / Atom feed
* SHTTY
@ 1995-07-05 22:38 Goran Larsson
  1995-07-06 10:39 ` SHTTY P.Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Goran Larsson @ 1995-07-05 22:38 UTC (permalink / raw)
  To: zsh-workers

Hi.

I have a problem with a change made to zsh between beta6 and beta9.

zsh-2.6-beta6-hzoli4		ok
zsh-2.6-beta9			problem
zsh-2.6-beta10			problem
zsh-2.6-beta10-hzolipre10	problem

If I su to another user, e.g. root su:ing to news, then the zle
option is unset and TTY is "".

root # echo $TTY
/dev/ttyp4
root # su news
news $ echo $TTY

news $

The reason can be found in init.c where SHTTY is opened with this code:

    if (isatty(0))
        SHTTY = movefd(open(ttyname(0), O_RDWR));
    else
        SHTTY = movefd(open("/dev/tty", O_RDWR));

In 2.5.01 and zsh-2.6-beta6-hzoli4 the code was:

        SHTTY = movefd((isatty(0)) ? dup(0) : open("/dev/tty", O_RDWR));

There is a VERY BIG difference between the old "dup(0)" and the new
"open(ttyname(0), O_RDWR)" if the su:ed to user don't have permission
to open the tty. In that case SHTTY is set to -1 and then the interactive
part of zsh just falls apart.

Why was this change made?

I made this change and the expected behaviour was restored:
---->8-------->8-------->8-------->8-------->8-------->8-------->8----
*** init.c.dist	Sat Jul  1 00:06:20 1995
--- init.c	Thu Jul  6 00:27:34 1995
***************
*** 334,343 ****
--- 334,347 ----
      }
  
      /* Make sure the tty is opened read/write. */
+ #if 1 /* HoH */
+     SHTTY = movefd((isatty(0)) ? dup(0) : open("/dev/tty", O_RDWR));
+ #else
      if (isatty(0))
  	SHTTY = movefd(open(ttyname(0), O_RDWR));
      else
  	SHTTY = movefd(open("/dev/tty", O_RDWR));
+ #endif
  
      if (SHTTY != -1) {
  #if defined(JOB_CONTROL) && defined(TIOCSETD) && defined(NTTYDISC)
---->8-------->8-------->8-------->8-------->8-------->8-------->8----

-- 
 Goran Larsson              Phone: +46 13 299588  FAX: +46 13 299022
 Approve AB                        +46 589 12810       +46 589 16901
 hoh@approve.se
           I was an atheist, until I found out I was God.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: SHTTY
  1995-07-05 22:38 SHTTY Goran Larsson
@ 1995-07-06 10:39 ` P.Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: P.Stephenson @ 1995-07-06 10:39 UTC (permalink / raw)
  To: Zsh hackers list

hoh@approve.se wrote:
> Hi.
> 
> I have a problem with a change made to zsh between beta6 and beta9.
>
> If I su to another user, e.g. root su:ing to news, then the zle
> option is unset and TTY is "".
>
> The reason can be found in init.c where SHTTY is opened with this code:
> 
>     if (isatty(0))
>         SHTTY = movefd(open(ttyname(0), O_RDWR));
>     else
>         SHTTY = movefd(open("/dev/tty", O_RDWR));
> 
> There is a VERY BIG difference between the old "dup(0)" and the new
> "open(ttyname(0), O_RDWR)" if the su:ed to user don't have permission
> to open the tty. In that case SHTTY is set to -1 and then the interactive
> part of zsh just falls apart.
> 
> Why was this change made?

This was made because in the new code I/O for the TTY all goes via the
fd SHTTY's output file, shout (compare with other shells, where this
is distinct from stdout and always attached to the tty while fd 0 is).
It's not possible to guarantee that fd 0 is opened r/w.  In fact, if
you do 'exec <anyfile' within zsh it will only be opened for reading.
This means that, in the case you are having problems with, it's not
actually possible to guarantee output to the terminal at all.  (Try
'exec <$TTY' before an su to another zsh with your patch and you'll
have problems.)

The following does the best I can think of: if opening the tty r/w
fails, it dup's 0, and if that means opening the editor output fails,
it dups 1 for shout if that's a tty.  (Since about the only remaining
way of getting tty output would be stderr this is fairly general.)  To
avoid code to keep track of fileno(shout), I've set the close-on-exec
flag for that.  This all makes it a little more complex than I would
hope: but as I said, there's no guaranteed way of getting read/write
access to the tty via a single fd.

I have actually tested this and it now works even if fd 0 is read-only.

*** Src/init.c.otty	Mon Jul  3 13:34:02 1995
--- Src/init.c	Thu Jul  6 11:30:23 1995
***************
*** 328,342 ****
      setbuffer(stderr, errbuf, BUFSIZ);
  #endif
  
      if (shout) {
  	fclose(shout);
  	shout = 0;
      }
  
      /* Make sure the tty is opened read/write. */
!     if (isatty(0))
  	SHTTY = movefd(open(ttyname(0), O_RDWR));
!     else
  	SHTTY = movefd(open("/dev/tty", O_RDWR));
  
      if (SHTTY != -1) {
--- 328,346 ----
      setbuffer(stderr, errbuf, BUFSIZ);
  #endif
  
+     if (SHTTY >= 10 && (!shout || SHTTY != fileno(shout)))
+ 	close(SHTTY);
      if (shout) {
  	fclose(shout);
  	shout = 0;
      }
  
      /* Make sure the tty is opened read/write. */
!     if (isatty(0)) {
  	SHTTY = movefd(open(ttyname(0), O_RDWR));
! 	if (SHTTY == -1)
! 	    SHTTY = movefd(dup(0));
!     } else
  	SHTTY = movefd(open("/dev/tty", O_RDWR));
  
      if (SHTTY != -1) {
***************
*** 348,353 ****
--- 352,368 ----
  
  	/* Associate terminal file descriptor with a FILE pointer */
  	shout = fdopen(SHTTY, "w");
+ 	/* As a last resort, see if stdout is a tty and use that. */
+ 	if (!shout && isatty(1)) {
+ 	    int shfd = movefd(dup(1));
+ 	    if (shfd != -1) {
+ #ifdef FD_CLOEXEC
+ 		/* Don't leave this fd around in exec'd procs */
+ 		fcntl(shfd, F_SETFD, FD_CLOEXEC);
+ #endif
+ 		shout = fdopen(shfd, "w");
+ 	    }
+ 	}
  
          /* We will only use zle if shell is interactive, *
           * SHTTY != -1, and shout != 0                   */

-- 
Peter Stephenson <P.Stephenson@swansea.ac.uk>  Tel: +44 1792 205678 extn. 4461
WWW:  http://python.swan.ac.uk/~pypeters/      Fax: +44 1792 295324
Department of Physics, University of Wales, Swansea,
Singleton Park, Swansea, SA2 8PP, U.K.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1995-07-06 10:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-07-05 22:38 SHTTY Goran Larsson
1995-07-06 10:39 ` SHTTY P.Stephenson

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).