xref: /freebsd/tools/test/stress2/misc/pty.sh (revision c7a063741720ef81d4caa4613242579d12f1d605)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# pty(4) test scenario.
30
31# "panic: make_dev_credv: bad si_name (error=17, si_name=ptysn)" seen.
32# https://people.freebsd.org/~pho/stress/log/pty.txt
33
34# /dev/pty[l-sL-S][0-9a-v]  Pseudo-terminal master devices.
35# /dev/tty[l-sL-S][0-9a-v]  Pseudo-terminal slave devices.
36
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38
39. ../default.cfg
40
41kldstat -v | grep -q pty || { kldload pty || exit 0; }
42
43here=`pwd`
44cd /tmp
45sed '1,/^EOF/d' < $here/$0 > pty.c
46mycc -o pty -Wall -Wextra -O2 pty.c || exit 1
47rm -f pty.c
48
49su $testuser -c /tmp/pty
50
51rm -f /tmp/pty
52exit
53EOF
54#include <sys/types.h>
55#include <sys/wait.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <fts.h>
61#include <stdlib.h>
62#include <stdio.h>
63#include <time.h>
64#include <unistd.h>
65
66#define PARALLEL 4
67#define RUNTIME 300
68
69char path[80];
70
71void
72churn(char *path)
73{
74
75	FTS *fts;
76	FTSENT *p;
77	time_t start;
78	int fd, ftsoptions;
79	char *args[2];
80
81	ftsoptions = FTS_PHYSICAL;
82	args[0] = path;
83	args[1] = 0;
84
85	setproctitle("churn");
86	start = time(NULL);
87	while (time(NULL) - start < RUNTIME) {
88		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
89			err(1, "fts_open");
90
91		while ((p = fts_read(fts)) != NULL) {
92			if (p->fts_info == FTS_D ||
93			   p->fts_info == FTS_DP)
94				continue;
95			if ((fd = open(p->fts_path, O_RDWR)) == -1)
96				if ((fd = open(p->fts_path, O_WRONLY)) == -1)
97					if ((fd = open(p->fts_path, O_RDONLY)) == -1)
98						continue;
99			usleep(arc4random() % 1000);
100			close(fd);
101
102		}
103
104		if (errno != 0 && errno != ENOENT)
105			err(1, "fts_read");
106		if (fts_close(fts) == -1)
107			err(1, "fts_close()");
108	}
109
110	_exit(0);
111}
112
113void
114pty(void)
115{
116	int fd[512], i, j, n;
117	char c1, c2;
118
119	n = 0;
120	c1 = 'l';
121	for (i = 0; i < 16; i++) {
122		c2 = '0';
123		for (j = 0; j < 32; j++) {
124			snprintf(path, sizeof(path), "/dev/pty%c%c", c1, c2);
125			fd[n++] = open(path, O_RDWR);
126			if (c2 == '9')
127				c2 = 'a';
128			else
129				c2++;
130		}
131		if (c1 == 's')
132			c1 = 'L';
133		else
134			c1++;
135	}
136
137	for (i = 0; i < n; i++)
138		if (fd[i] != -1)
139			close(fd[i]);
140}
141
142int
143main(void)
144{
145	int i;
146
147	time_t start;
148
149	for (i = 0; i < PARALLEL; i++)
150		if (fork() == 0)
151			churn("/dev");
152
153	start = time(NULL);
154	while (time(NULL) - start < RUNTIME)
155		pty();
156
157	for (i = 0; i < PARALLEL; i++)
158		wait(NULL);
159
160	return (0);
161}
162