diff options
author | Kevin Krakauer <krakauer@google.com> | 2021-02-11 11:06:56 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-02-11 11:09:22 -0800 |
commit | ae8d966f5af0bba9978a1aedac64038ef65a4cc9 (patch) | |
tree | c6540f92ac18e178dcd0189302ee94e13c12b3d6 /test/syscalls/linux/pty.cc | |
parent | 192780946fdf584c5e504b24f47dbd9bd411a3a6 (diff) |
Assign controlling terminal when tty is opened and support NOCTTY
PiperOrigin-RevId: 357015186
Diffstat (limited to 'test/syscalls/linux/pty.cc')
-rw-r--r-- | test/syscalls/linux/pty.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/syscalls/linux/pty.cc b/test/syscalls/linux/pty.cc index 85ff258df..e6b12f81c 100644 --- a/test/syscalls/linux/pty.cc +++ b/test/syscalls/linux/pty.cc @@ -36,6 +36,7 @@ #include "absl/time/clock.h" #include "absl/time/time.h" #include "test/util/capability_util.h" +#include "test/util/cleanup.h" #include "test/util/file_descriptor.h" #include "test/util/posix_error.h" #include "test/util/pty_util.h" @@ -459,6 +460,59 @@ TEST(BasicPtyTest, OpenMasterReplica) { FileDescriptor replica = ASSERT_NO_ERRNO_AND_VALUE(OpenReplica(master)); } +TEST(BasicPtyTest, OpenSetsControllingTTY) { + SKIP_IF(IsRunningWithVFS1()); + // setsid either puts us in a new session or fails because we're already the + // session leader. Either way, this ensures we're the session leader. + setsid(); + + // Make sure we're ignoring SIGHUP, which will be sent to this process once we + // disconnect they TTY. + struct sigaction sa = {}; + sa.sa_handler = SIG_IGN; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + struct sigaction old_sa; + ASSERT_THAT(sigaction(SIGHUP, &sa, &old_sa), SyscallSucceeds()); + auto cleanup = Cleanup([old_sa] { + EXPECT_THAT(sigaction(SIGHUP, &old_sa, NULL), SyscallSucceeds()); + }); + + FileDescriptor master = ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/ptmx", O_RDWR)); + FileDescriptor replica = + ASSERT_NO_ERRNO_AND_VALUE(OpenReplica(master, O_NONBLOCK | O_RDWR)); + + // Opening replica should make it our controlling TTY, and therefore we are + // able to give it up. + ASSERT_THAT(ioctl(replica.get(), TIOCNOTTY), SyscallSucceeds()); +} + +TEST(BasicPtyTest, OpenMasterDoesNotSetsControllingTTY) { + SKIP_IF(IsRunningWithVFS1()); + // setsid either puts us in a new session or fails because we're already the + // session leader. Either way, this ensures we're the session leader. + setsid(); + FileDescriptor master = ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/ptmx", O_RDWR)); + + // Opening master does not set the controlling TTY, and therefore we are + // unable to give it up. + ASSERT_THAT(ioctl(master.get(), TIOCNOTTY), SyscallFailsWithErrno(ENOTTY)); +} + +TEST(BasicPtyTest, OpenNOCTTY) { + SKIP_IF(IsRunningWithVFS1()); + // setsid either puts us in a new session or fails because we're already the + // session leader. Either way, this ensures we're the session leader. + setsid(); + FileDescriptor master = ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/ptmx", O_RDWR)); + FileDescriptor replica = ASSERT_NO_ERRNO_AND_VALUE( + OpenReplica(master, O_NOCTTY | O_NONBLOCK | O_RDWR)); + + // Opening replica with O_NOCTTY won't make it our controlling TTY, and + // therefore we are unable to give it up. + ASSERT_THAT(ioctl(replica.get(), TIOCNOTTY), SyscallFailsWithErrno(ENOTTY)); +} + // The replica entry in /dev/pts/ disappears when the master is closed, even if // the replica is still open. TEST(BasicPtyTest, ReplicaEntryGoneAfterMasterClose) { |