diff options
Diffstat (limited to 'test/syscalls/linux')
-rw-r--r-- | test/syscalls/linux/BUILD | 1 | ||||
-rw-r--r-- | test/syscalls/linux/pty.cc | 54 |
2 files changed, 55 insertions, 0 deletions
diff --git a/test/syscalls/linux/BUILD b/test/syscalls/linux/BUILD index 42fc363a2..31dc2525c 100644 --- a/test/syscalls/linux/BUILD +++ b/test/syscalls/linux/BUILD @@ -1441,6 +1441,7 @@ cc_binary( "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", gtest, + "//test/util:cleanup", "//test/util:posix_error", "//test/util:pty_util", "//test/util:test_main", 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) { |