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

Duplicating file descriptor 10



Greetings friends,

I’m looking for a way to get a file descriptor that is definitely the interactive tty (in an interactive shell, obviously). I can open a new fd to $TTY but that reaches out to open a new fd when a regular interactive shell already has at leats four fds pointing at the tty. Zsh duplicates the tty (from stdin) to fd 10, which is exactly what I’m looking for, except… I can’t write to fd 10. I can’t dup fd 10. lsof -p $$ confirms that fd 10 is open and pointing to the tty, but zsh appears unaware of this. `exec {tty}>&10` gives ‘zsh: 10: bad file descriptor’. `echo help me >&10` gives 'zsh: 10: bad file descriptor’. Everything I’ve tried just gives the same thing. Testing a known-unused fd gives the same error, and testing a known-open fd works normally as expected. 

Output of `lsof -p $$` (irrelevant lines removed):
zsh     52306 pellican    0u   CHR   16,3   0t8978                 971 /dev/ttys003
zsh     52306 pellican    1u   CHR   16,3   0t8978                 971 /dev/ttys003
zsh     52306 pellican    2u   CHR   16,3   0t8978                 971 /dev/ttys003
zsh     52306 pellican   10u   CHR   16,3   0t3819                 971 /dev/ttys003


I looked through movefd(), redup(), and init_io() and I don’t see anything that seems to mark this fd as special and to be hidden/protected. It sure seems like stdio is just dup’d to 10 (via movefd()) and it’s just normal. 

The goal here is to be able to print to the tty even if stdin/stdout/stderr are redirected elsewhere at various times. 

Help me understand what I’m missing? I’m more than happy to read the source, but I don’t know where else to look. If the answer is that fd 10 is specially protected/hidden deliberately, that’s fine and I’ll just open a new fd on $TTY or maybe just dup stdin if it’s still connected to the tty when I first see it or something. 

Thanks for any pointers!!


JP2





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