Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16435

Does bash save/restore terminal settings as required by POSIX job control?

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Godmar Back <godmar@gmail.com>
Newsgroups gnu.bash.bug
Subject Does bash save/restore terminal settings as required by POSIX job control?
Date Mon, 22 Jun 2020 16:48:14 -0400
Lines 95
Approved bug-bash@gnu.org
Message-ID <mailman.289.1592858918.2574.bug-bash@gnu.org> (permalink)
References <CAB4+JYKZjPPYaNmhLZCDMUhvj9nEfLbaNgX3Q0c+hDHPC2mAew@mail.gmail.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
X-Trace usenet.stanford.edu 1592858919 22093 209.51.188.17 (22 Jun 2020 20:48:39 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=mZCXzc16Hj8vnTtyqHU5e17ie3uer9DaMi7m6F4l1fA=; b=RlH80ztTJx3TUGC7XgmmWREMdCaLCrZ2NjI53mdcTz4+jq59Gy0cPNjT2Oof186SBI 8FkYVWxp+1BJ3EeEenIBFRg3KjDjBzLoutMuxR4Tu5+IdZiTpBiJwW8jHB73EBkiUeV9 6jn70tmU5QScpdPtv1GQVV5Hkn/1bPGqMy+n9M8JZDoGIsFtqGTjsWvQeOzkcpBnkSSX E1dLTILnldrPjZPhtvcfiX5KdZaXG6tJq4ChO8D/2CXEsgzqnklyY6YFl/beX0FNcf/K 22pF8sF+wHVW2bl1oFVn6rHNvBeD91dPMo4ziaJ6/Ng6v/ctoN7Mur5js3rsu1f1lHDZ ED7A==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=mZCXzc16Hj8vnTtyqHU5e17ie3uer9DaMi7m6F4l1fA=; b=JaPO5OfZIBpyihtl3SN+kF9t1UTiXFZpx0SonkoCi4Kw6IzMGuBR85YoHq6iDctF+K U8HjW3r0m6wEkXjSOgDODSr68VwBTWn8z8sIB34jIft0Q40pim3fBFxtkh7wVBmZcU5+ APMZ3g5BImCDgNHgwgnKL7Id0SHNnQs5AqxOYjARakkYx25tr8sxI/7paQCi35LuT24F i/L2ZfdMZsgBXGmvftw6zg6oX72hd/V3NnrAQdzIS3VwOKjiYt84cUBWJiCLywlVo/b6 9RxXqfB14hZxUFar2Kgytw+S9xxdSKuE6B2ce0nmngf3IOZvTbzbbOmTBgkN7B33bUY/ SXpw==
X-Gm-Message-State AOAM5304MYU7LDKG3GllwKbPqPi4wG3wtBik/QYctSwGbgPMroMhZERL u9/8QjhDZiVA6fNKn3sZGJC9IDcM2I/X+OdvRkfbg4AZv4Q=
X-Google-Smtp-Source ABdhPJzywPo+Avnp81pvaUZOsoARuH0XxTEbfGw2lxbZ55uUtLM95O98GqLT2MSeKrq6HWvIsrjSFBAmvE8mAr1IOk4=
X-Received by 2002:a1c:2e47:: with SMTP id u68mr20934990wmu.45.1592858905538; Mon, 22 Jun 2020 13:48:25 -0700 (PDT)
Received-SPF pass client-ip=2a00:1450:4864:20::343; envelope-from=godmar@gmail.com; helo=mail-wm1-x343.google.com
X-detected-operating-system by eggs.gnu.org: No matching host in p0f cache. That's all we know.
X-Spam_score_int -20
X-Spam_score -2.1
X-Spam_bar --
X-Spam_report (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN
X-Spam_action no action
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAB4+JYKZjPPYaNmhLZCDMUhvj9nEfLbaNgX3Q0c+hDHPC2mAew@mail.gmail.com>
Xref csiph.com gnu.bash.bug:16435

Show key headers only | View raw


(Disclosure: I performed a search for terminal, terminal settings,
tcsetattr in the bash-bug mailing list, without finding a discussion.
My apologies if this is a known issue or was already discussed.)

Hi,

according to POSIX Part A, Base Definitions (line 726-728, pg 20, Part
A: Base Definitions-IEEE and The Open Group.), a job control shell has
the following responsibility:

"When a foreground (not background) job stops, the shell must sample
and remember the current terminal settings so that it can restore them
later when it continues the stopped job in the foreground (via the
tcgetattr( ) and tcsetattr( ) functions)."

Does bash implement this functionality?  If implemented correctly, I
would expect the program appended below to not fail its assertions.

However, it fails - tested with bash 4.4 on CentOS 8 using a pty
created by sshd.
I also looked at the bash 5.0 source code and could not spot where
bash implements this.

I was curious how other shells handle this.
It also doesn't work with the version of zsh (5.5.1) installed on CentOS 8.
It "works" with the version of ksh installed on CentOS 8 (93u+), but
then ksh fails to restore the terminal state afterwards (leaves it in
the state the program placed it when it exits).

I first noticed this when students in my class's shell project failed
to properly implement terminal handling, but programs such vi/vim
still continued to work even when stopped and later placed back in the
foreground. I discovered that these programs implement a SIGCONT
handler where they restore their terminal state when continued, but I
wrote this off as an attempt to be robust in the presence of buggy
shells.  I had not checked the bash code base, however.  Finally, I
got around to adding a test (below) and to my surprise discovered that
bash fails this test as well.

Am I reading the right version of the POSIX standard?

Could anyone shed some light on this?

 - Godmar

Code follows:

// ts_test.c
/* Test that terminal state is properly restored when a process is
stopped and restored. */
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <signal.h>

#define CTRL_D  4
#define CTRL_E  5

int
main()
{
    int terminal_fd = open(ctermid(NULL), O_RDWR);
    assert (terminal_fd != -1);

    // Step 1. make a change to the terminal state:
    // change VEOF from Ctrl-D to Ctrl-E
    struct termios saved_tty_state;
    int rc = tcgetattr(terminal_fd, &saved_tty_state);
    assert (rc == 0);

    assert (saved_tty_state.c_cc[VEOF] == CTRL_D);       // ^D
    saved_tty_state.c_cc[VEOF] = CTRL_E;                 // ^E
    rc = tcsetattr(terminal_fd, TCSANOW, &saved_tty_state);
    assert (rc == 0);

    // Step 2.  Suspend and let user resume
    printf("This job should now stop, please run 'fg' to continue it\n");
    raise(SIGTSTP);
    printf("Job now continuing...\n");

    // Step 3.
    // Expect that job control shell saved the terminal state
    rc = tcgetattr(terminal_fd, &saved_tty_state);
    assert (rc == 0);
    if (saved_tty_state.c_cc[VEOF] != CTRL_E) {
        printf("I expected a POSIX job control shell to preserve my
terminal settings\n");
        printf("VEOF was not saved, it is %d...\n", saved_tty_state.c_cc[VEOF]);
    }

    assert (saved_tty_state.c_cc[VEOF] == CTRL_E);       // ^E
}

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Does bash save/restore terminal settings as required by POSIX job control? Godmar Back <godmar@gmail.com> - 2020-06-22 16:48 -0400

csiph-web