ThreadGroup should disassociate from tty on exit.

Added syscall test case and also tested with:
```
$ docker container run -it --name debian-runsc --runtime=runsc debian:12 bash -c "apt update && apt install -y curl"
<snip>
Setting up libkeyutils1:amd64 (1.6.3-2) ...
Setting up libpsl5:amd64 (0.21.2-1) ...
Setting up libbrotli1:amd64 (1.0.9-2+b6) ...
Setting up libssl3:amd64 (3.0.11-1~deb12u2) ...
Setting up libnghttp2-14:amd64 (1.52.0-1) ...
Setting up krb5-locales (1.20.1-2+deb12u1) ...
Setting up libldap-common (2.5.13+dfsg-5) ...
Setting up libkrb5support0:amd64 (1.20.1-2+deb12u1) ...
Setting up libsasl2-modules-db:amd64 (2.1.28+dfsg-10) ...
Setting up librtmp1:amd64 (2.4+20151223.gitfa8646d.1-2+b2) ...
Setting up libk5crypto3:amd64 (1.20.1-2+deb12u1) ...
Setting up libsasl2-2:amd64 (2.1.28+dfsg-10) ...
Setting up libssh2-1:amd64 (1.10.0-3+b1) ...
Setting up libkrb5-3:amd64 (1.20.1-2+deb12u1) ...
Setting up openssl (3.0.11-1~deb12u2) ...
Setting up publicsuffix (20230209.2326-1) ...
Setting up libsasl2-modules:amd64 (2.1.28+dfsg-10) ...
Setting up libldap-2.5-0:amd64 (2.5.13+dfsg-5) ...
Setting up ca-certificates (20230311) ...
<snip>
```

With these changes, there is no more error about TIOCSCTTY, and the `Setting
up..` log lines are formatted properly.

Fixes #9642

PiperOrigin-RevId: 580204710
This commit is contained in:
Nicolas Lacasse
2023-11-07 09:26:16 -08:00
committed by gVisor bot
parent b988b57921
commit 47db4119a2
2 changed files with 27 additions and 2 deletions
+7
View File
@@ -322,6 +322,13 @@ func (tg *ThreadGroup) Release(ctx context.Context) {
its = append(its, it)
}
tg.timers = make(map[linux.TimerID]*IntervalTimer) // nil maps can't be saved
// Disassociate from the tty if we have one.
if tg.tty != nil {
tg.tty.mu.Lock()
tg.tty.tg = nil
tg.tty.mu.Unlock()
tg.tty = nil
}
tg.signalHandlers.mu.Unlock()
tg.pidns.owner.mu.Unlock()
for _, it := range its {
+20 -2
View File
@@ -1475,9 +1475,9 @@ TEST_F(JobControlTest, SetTTYMaster) {
TEST_F(JobControlTest, SetTTY) {
auto res = RunInChild([=]() {
TEST_PCHECK(setsid() >= 0);
TEST_PCHECK(ioctl(!replica_.get(), TIOCSCTTY, 0));
TEST_PCHECK(ioctl(replica_.get(), TIOCSCTTY, 0) >= 0);
// The second attempt setting the same terminal has to be no-op.
TEST_PCHECK(ioctl(!replica_.get(), TIOCSCTTY, 0));
TEST_PCHECK(ioctl(replica_.get(), TIOCSCTTY, 0) >= 0);
});
ASSERT_NO_ERRNO(res);
}
@@ -1959,6 +1959,24 @@ TEST_F(JobControlTest, OrphanRegression) {
ASSERT_EQ(wstatus, 0);
}
// Test setting a controlling tty, then exiting, then re-using the same tty in a
// different process.
//
// Regression test for https://github.com/google/gvisor/issues/9642.
TEST_F(JobControlTest, ReuseControllingTTYAfterExit) {
auto res = RunInChild([=]() {
TEST_PCHECK(setsid() >= 0);
TEST_PCHECK(ioctl(replica_.get(), TIOCSCTTY, 0) >= 0);
});
ASSERT_NO_ERRNO(res);
auto res2 = RunInChild([=]() {
TEST_PCHECK(setsid() >= 0);
TEST_PCHECK(ioctl(replica_.get(), TIOCSCTTY, 0) >= 0);
});
ASSERT_NO_ERRNO(res2);
}
} // namespace
} // namespace testing
} // namespace gvisor