• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2002
4  * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that slave pseudo-terminal can be opened multiple times in parallel.
11  */
12 
13 #define _GNU_SOURCE
14 
15 #include "common.h"
16 
17 static int masterfd = -1;
18 
count_avail_pid(void)19 static unsigned int count_avail_pid(void)
20 {
21 	DIR *dir;
22 	struct dirent *ent;
23 	struct rlimit limit;
24 	unsigned int count = 0;
25 	unsigned int max_pid_num;
26 
27 	SAFE_GETRLIMIT(RLIMIT_NOFILE, &limit);
28 
29 	dir = SAFE_OPENDIR("/proc/self/fd");
30 	while ((ent = SAFE_READDIR(dir)))
31 		count++;
32 
33 	SAFE_CLOSEDIR(dir);
34 
35 	max_pid_num = limit.rlim_cur - count;
36 
37 	tst_res(TINFO, "Available number of pids: %u", max_pid_num);
38 
39 	return max_pid_num;
40 }
41 
run(void)42 static void run(void)
43 {
44 	unsigned int max_pid_num;
45 
46 	max_pid_num = count_avail_pid();
47 
48 	int slavefd[max_pid_num];
49 
50 	for (uint32_t i = 0; i < max_pid_num; i++)
51 		slavefd[i] = open_slave(masterfd);
52 
53 	tst_res(TPASS, "pty has been opened %d times", max_pid_num);
54 
55 	for (uint32_t i = 0; i < max_pid_num; i++)
56 		SAFE_CLOSE(slavefd[i]);
57 }
58 
setup(void)59 static void setup(void)
60 {
61 	masterfd = open_master();
62 }
63 
cleanup(void)64 static void cleanup(void)
65 {
66 	if (masterfd != -1)
67 		SAFE_CLOSE(masterfd);
68 }
69 
70 static struct tst_test test = {
71 	.test_all = run,
72 	.setup = setup,
73 	.cleanup = cleanup,
74 };
75