From a042da18473d2cb32d3cb6740e899e2c8573206a Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Tue, 1 Aug 2023 14:18:23 +0800 Subject: [PATCH] Add pty test --- regression/apps/Makefile | 2 +- regression/apps/pty/Makefile | 3 ++ regression/apps/pty/open_pty.c | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 regression/apps/pty/Makefile create mode 100644 regression/apps/pty/open_pty.c diff --git a/regression/apps/Makefile b/regression/apps/Makefile index 09db3e9c4..5f70f6635 100644 --- a/regression/apps/Makefile +++ b/regression/apps/Makefile @@ -3,7 +3,7 @@ MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH))) INITRAMFS ?= $(CUR_DIR)/../build/initramfs REGRESSION_BUILD_DIR ?= $(INITRAMFS)/regression -TEST_APPS := signal_c pthread network hello_world hello_pie hello_c fork_c fork execve +TEST_APPS := signal_c pthread network hello_world hello_pie hello_c fork_c fork execve pty .PHONY: all diff --git a/regression/apps/pty/Makefile b/regression/apps/pty/Makefile new file mode 100644 index 000000000..05ff449d2 --- /dev/null +++ b/regression/apps/pty/Makefile @@ -0,0 +1,3 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := diff --git a/regression/apps/pty/open_pty.c b/regression/apps/pty/open_pty.c new file mode 100644 index 000000000..4e65d81b4 --- /dev/null +++ b/regression/apps/pty/open_pty.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + +int main() { + int master, slave; + char name[256]; + struct termios term; + + if (openpty(&master, &slave, name, NULL, NULL) == -1) { + perror("openpty"); + exit(EXIT_FAILURE); + } + + printf("master fd: %d\n", master); + printf("slave fd: %d\n", slave); + printf("slave name: %s\n", name); + + // Set pty slave terminal attributes + tcgetattr(slave, &term); + term.c_lflag &= ~(ICANON | ECHO); + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(slave, TCSANOW, &term); + + // Print to pty slave + dprintf(slave, "Hello world!\n"); + + // Read from pty slave + char buf[256]; + ssize_t n = read(master, buf, sizeof(buf)); + if (n > 0) { + printf("read %ld bytes from slave: %.*s\n", n, (int)n, buf); + } + + // Write to pty master + dprintf(master, "hello world from master\n"); + + // Read from pty master + char nbuf[256]; + ssize_t nn = read(slave, nbuf, sizeof(nbuf)); + if (nn > 0) { + printf("read %ld bytes from master: %.*s\n", nn, (int)nn, nbuf); + } + + close(master); + close(slave); + + return 0; +}