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# "panic: Assertion !tty_gone(tp) failed at ttydevsw.h:153" seen.
30# https://people.freebsd.org/~pho/stress/log/posix_openpt2.txt
31# Fixed by r312077
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > posix_openpt2.c
40mycc -o posix_openpt2 -Wall -Wextra -O2 posix_openpt2.c -lutil || exit 1
41rm -f posix_openpt2.c
42
43/tmp/posix_openpt2 &
44
45while kill -0 $! 2>/dev/null; do
46	$here/../testcases/swap/swap -t 2m -i 20
47done
48wait
49
50rm -f /tmp/posix_openpt2
51exit 0
52EOF
53#include <sys/types.h>
54#include <sys/ioctl.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 <libutil.h>
62#include <stdint.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <termios.h>
67#include <time.h>
68#include <unistd.h>
69
70#define LOOPS 10
71#define PARALLEL 2
72#define RUNTIME 300
73
74void
75churn(char *path)
76{
77	FTS *fts;
78	FTSENT *p;
79	time_t start;
80	int fd, ftsoptions;
81	char *args[2];
82
83	ftsoptions = FTS_PHYSICAL;
84	args[0] = path;
85	args[1] = 0;
86
87	setproctitle("churn");
88	start = time(NULL);
89	while (time(NULL) - start < RUNTIME / LOOPS) {
90		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
91			err(1, "fts_open");
92
93		while ((p = fts_read(fts)) != NULL) {
94			if (p->fts_info == FTS_D ||
95			   p->fts_info == FTS_DP)
96				continue;
97			if ((fd = open(p->fts_path, O_RDONLY)) > 0)
98				close(fd);
99
100		}
101
102		if (errno != 0 && errno != ENOENT)
103			err(1, "fts_read");
104		if (fts_close(fts) == -1)
105			err(1, "fts_close()");
106	}
107
108	_exit(0);
109}
110
111void
112pty(void)
113{
114	time_t start;
115        int masterfd, slavefd;
116	char *slave;
117
118	start = time(NULL);
119	while (time(NULL) - start < RUNTIME / LOOPS) {
120		if ((masterfd = posix_openpt(O_RDWR | O_NOCTTY)) == -1)
121			err(1, "posix_openpt");
122		if ((slave = ptsname (masterfd)) == NULL)
123			err(1, "ptsname");
124		if ((slavefd = open(slave, O_RDWR|O_NOCTTY)) == -1)
125			err(1, "open(%s)", slave);
126		usleep(arc4random() % 10000);
127		close(slavefd);
128		close(masterfd);
129	}
130	_exit(0);
131}
132
133int
134main(void)
135{
136	int i, j;
137
138	for (j = 0; j < LOOPS; j++) {
139		for (i = 0; i < PARALLEL; i++) {
140			if (fork() == 0)
141				pty();
142		}
143		for (i = 0; i < PARALLEL; i++) {
144			if (fork() == 0)
145				churn("/dev/pts");
146		}
147		for (i = 0; i < 2 * PARALLEL; i++)
148			wait(NULL);
149	}
150
151	return (0);
152}
153