[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: background process not inheriting pts device

From: Thomas Dickey
Subject: Re: background process not inheriting pts device
Date: Thu, 11 Nov 2021 08:09:47 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Thu, Nov 11, 2021 at 02:46:36AM +0100, Luuk van Baal wrote:
> Hi,
> Disclaimer I am not familiar with the inner workings of ncurses/relevant
> jargon.
> I'm just the author of a shell script[1] for file-manager nnn[2] which relied
> on tput cols/lines to get the terminal dimensions.
> With ncurses release 6.3, the behavior for tput has changed for the script
> and no longer reports the correct terminal dimensions but instead reports
> what I assume to be a set of default values, namely 80 and 24.
> The main loop for this script is sent to the background and it seems with 6.3
> this background process no longer "inherits" the pts device of the parent
> process which was the case on earlier ncurses versions.  An additional file
> descriptor for the background process is missing in 6.3:
> ls -l /proc/<script PID>/fd
> 0 -> /dev/pts/10
> 1 -> /dev/pts/10
> 10 -> /home/luuk/.config/nnn/plugins/preview-tui
> 11 -> /dev/pts/10 # missing in 6.3
> 2 -> /dev/null
> I've had to resort to writing the correct terminal dimensions to a temporary
> file from the mainprocess and its WINCH handler which works.  I would like to
> avoid writing to a tempfile if it can be avoided however and I wonder if this
> new behavior is documented/expected or a regression.
> I couldn't find anything relevant in the release notes.

I think we're talking about this:

        + modify save_tty_settings() to avoid opening /dev/tty for cases other
          than reset/init, e.g., for clear.

which is in the release notes:


          + modify to allow multiple commands per line.
          + improve  parameter-checking  by analyzing all extended string
            capabilities,  e.g., as used in the Cs and Ms capabilities of
            the tmux description.
          + make  warning  messages  consistently  using alias names when
            those are used, rather than the underlying program's name.
          + improve   usage   message  for  aliases  such  as  clear,  by
            eliminating tput-specific portions.
          + modify  initialization  to  avoid  opening /dev/tty for cases
            other than reset/init, e.g., for clear.

The problem that I was solving was addressing the "clear" alias for tput
which would (without this change) have unexpected results.

The "init" and "reset" commands in tput are special, because they go
beyond the terminal-capabilities and modify the terminal modes (such as
the erase-character).

Without that change, running "clear" in a pipe (where stdin, stdout, and
stderr are all redirected to a non-tty) would clear the actual terminal
rather than write the escape sequences to stdout/stderr.

Since I made the changes in 2016 to merge the internal working of
tput/tset, that part has been an unexpected nuisance.  Over the summer I
was working on my port to OpenBSD (to provide a suitable replacement for
its stagnant version of ncurses), and one of the features was the
"clear" program which in that configuration is an alias for "tput"
rather than separate.

The 2016 merge was motivated by seeing a difference for init/reset
versus Solaris.  Checking now, Solaris "tput lines" applies to stdout.
(Solaris tput/tset use stdout and stderr respectively; in my merge I
allowed for either to be a tty).  ncurses 6.0 (2015) also uses the
file-descriptor from stdout for "tput lines".

With those older versions, you could work around the limitation to
stdout using the "script" program (a little awkward, but it depends
on how much you want to do this).

I'm assuming that your script does something like this:

        foo=$(tput lines)

Your script could do this:

        foo=$(tput lines <$(tty))

and get useful results.

Thomas E. Dickey <dickey@invisible-island.net>

Attachment: signature.asc
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]