[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to redirect tty to /dev/null?
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] How to redirect tty to /dev/null? |
Date: |
Wed, 4 Sep 2019 08:18:00 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-09-03 11:06:52 -0500, Eric Blake:
[...]
> All processes have a notion of a controlling terminal. If you want a
> child process' interaction with its controlling terminal to behave
> differently than interacting with YOUR controlling terminal, then you
> need to open up a pty and become the master of that device prior to
> spawning your child process, at which point your child process's tty is
> now the slave end of the pty you just opened.
Note that that child process must be made the leader of a new
session and open that slave pty (without O_NOCTTY) for it to
attach the new tty.
> At that point, as master
> of the pty you can do whatever you want with the child's interactions
> with its controlling terminal (including blindly ignoring whatever the
> child wants to output to its tty, for the effect of that output going to
> /dev/null). Look at the 'expect' program if you want a demonstration of
> creating your own pty for managing terminal interactions of child
> processes.
Or you could create a session detached from any terminal,
to cause unzip to fail when it tries to open /dev/tty.
You'd probably want unzip *not* to be the leader of that session
as it could end up attaching a tty device to the session if it
were to open one without O_NOCTTY.
util-linux have a setsid command to start a new session:
$ setsid -w sh -c 'ps -jp "$$"; exit'
PID PGID SID TTY TIME CMD
6685 6685 6685 ? 00:00:00 sh
(sh is the leader of a session with no controlling terminal, and
ps is running in a child process).
$ setsid -w sh -c 'unzip a.zip; exit'
Archive: a.zip
skipping: a unable to get password
unzip fails to open /dev/tty
Beside expect, socat is another command that can let you create
ptys.
$ (sleep 1; printf '\r') | socat - 'system:ps -j; ls -l /proc/self/fd; unzip
a.zip; exit,setsid,pty,ctty,fdout=3'
PID PGID SID TTY TIME CMD
7505 7505 7505 pts/10 00:00:00 socat
7506 7505 7505 pts/10 00:00:00 sh
7507 7505 7505 pts/10 00:00:00 ps
total 0
lrwx------ 1 chazelas chazelas 64 Sep 4 08:10 0 -> /dev/pts/10
lrwx------ 1 chazelas chazelas 64 Sep 4 08:10 1 -> /dev/pts/9
lrwx------ 1 chazelas chazelas 64 Sep 4 08:10 2 -> /dev/pts/9
lrwx------ 1 chazelas chazelas 64 Sep 4 08:10 3 -> /dev/pts/10
lrwx------ 1 chazelas chazelas 64 Sep 4 08:10 4 -> 'socket:[71040]'
lr-x------ 1 chazelas chazelas 64 Sep 4 08:10 5 -> /proc/7508/fd
Archive: a.zip
[a.zip] a password:
skipping: a incorrect password
2019/09/04 08:10:45 socat[7503] E waitpid(): child 7505 exited with status 82
In that case, we have stdin (and fd 3) on a new pty in a new
session. We simulate pressing enter (\r) after 1 second because
unzip drains the input before issuing the prompt.
Or:
$ socat -u file:/dev/null 'system:ps -jp "$$"; ls -l /proc/self/fd; unzip
a.zip; exit,setsid'
PID PGID SID TTY TIME CMD
7548 7547 7547 ? 00:00:00 sh
total 0
lrwx------ 1 chazelas chazelas 64 Sep 4 08:13 0 -> 'socket:[72978]'
lrwx------ 1 chazelas chazelas 64 Sep 4 08:13 1 -> /dev/pts/10
lrwx------ 1 chazelas chazelas 64 Sep 4 08:13 2 -> /dev/pts/10
lrwx------ 1 chazelas chazelas 64 Sep 4 08:13 3 -> 'socket:[72975]'
lrwx------ 1 chazelas chazelas 64 Sep 4 08:13 4 -> 'socket:[72976]'
lr-x------ 1 chazelas chazelas 64 Sep 4 08:13 5 -> /proc/7550/fd
Archive: a.zip
skipping: a unable to get password
New session, no controlling terminal.
> But that starts to get into the scope of pty management, and
> less about bash, so it becomes questionable that you'll get any better
> help on this topic from this list.
Yes, zsh has builtin support for creating ptys and sessions (in
the zsh/zpty module), I don't think bash does (there's no
mention of setsid in the source code AFAICT).
--
Stephane