[LTP] [PATCH 10/10] Add pty09 test
Andrea Cervesato
andrea.cervesato@suse.de
Wed Dec 11 13:20:33 CET 2024
From: Andrea Cervesato <andrea.cervesato@suse.com>
Verify that slave pseudo-terminal can be opened multiple times
in parallel.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/pty | 1 +
testcases/kernel/pty/.gitignore | 1 +
testcases/kernel/pty/pty09.c | 89 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 91 insertions(+)
diff --git a/runtest/pty b/runtest/pty
index 365a46ee9730aa36b22dbbdbfba82ac0d491ac94..4b1abe7a602a14456877f3bc42f2275a3be0f8f0 100644
--- a/runtest/pty
+++ b/runtest/pty
@@ -7,6 +7,7 @@ pty05 pty05
pty06 pty06
pty07 pty07
pty08 pty08
+pty09 pty09
ptem01 ptem01
ptem02 ptem02
ptem03 ptem03
diff --git a/testcases/kernel/pty/.gitignore b/testcases/kernel/pty/.gitignore
index 7d8d4dceda84f2e2695a8bee39abfe894288b8b6..2d0c8bb6ff7d6883abfc7838e257b9be50244b69 100644
--- a/testcases/kernel/pty/.gitignore
+++ b/testcases/kernel/pty/.gitignore
@@ -13,3 +13,4 @@
/pty06
/pty07
/pty08
+/pty09
diff --git a/testcases/kernel/pty/pty09.c b/testcases/kernel/pty/pty09.c
new file mode 100644
index 0000000000000000000000000000000000000000..a9adf6ef619334b858fb99d5c388c0adac3976f5
--- /dev/null
+++ b/testcases/kernel/pty/pty09.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2002
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that slave pseudo-terminal can be opened multiple times in parallel.
+ */
+
+#define _GNU_SOURCE
+
+#include "tst_test.h"
+
+#define MASTERCLONE "/dev/ptmx"
+
+static unsigned int count_avail_pid(void)
+{
+ DIR *dir;
+ struct dirent *ent;
+ struct rlimit limit;
+ unsigned int count = 0;
+ unsigned int max_pid_num;
+
+ if (access(MASTERCLONE, F_OK))
+ tst_brk(TBROK, "%s device doesn't exist", MASTERCLONE);
+
+ SAFE_GETRLIMIT(RLIMIT_NOFILE, &limit);
+
+ dir = SAFE_OPENDIR("/proc/self/fd");
+ while ((ent = SAFE_READDIR(dir)))
+ count++;
+
+ SAFE_CLOSEDIR(dir);
+
+ max_pid_num = limit.rlim_cur - count;
+
+ tst_res(TINFO, "Available number of pids: %u", max_pid_num);
+
+ return max_pid_num;
+}
+
+static void run(void)
+{
+ int masterfd;
+ char *slavename;
+ unsigned int max_pid_num;
+
+ masterfd = SAFE_OPEN(MASTERCLONE, O_RDWR);
+
+ slavename = ptsname(masterfd);
+ if (slavename == NULL)
+ tst_brk(TBROK | TERRNO, "ptsname() error");
+
+ tst_res(TINFO, "pts device location is %s", slavename);
+
+ if (grantpt(masterfd) == -1)
+ tst_brk(TBROK | TERRNO, "grantpt() error");
+
+ if (unlockpt(masterfd) == -1)
+ tst_brk(TBROK | TERRNO, "unlockpt() error");
+
+ max_pid_num = count_avail_pid();
+
+ int slavefd[max_pid_num];
+
+ for (uint32_t i = 0; i < max_pid_num; i++)
+ slavefd[i] = SAFE_OPEN(slavename, O_RDWR);
+
+ tst_res(TPASS, "%s has been opened %d times", slavename, max_pid_num);
+
+ for (uint32_t i = 0; i < max_pid_num; i++)
+ SAFE_CLOSE(slavefd[i]);
+
+ SAFE_CLOSE(masterfd);
+}
+
+static void setup(void)
+{
+ if (access(MASTERCLONE, F_OK))
+ tst_brk(TBROK, "%s device doesn't exist", MASTERCLONE);
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+};
--
2.43.0
More information about the ltp
mailing list