xref: /freebsd/tools/test/stress2/misc/pts.sh (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
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# Page fault in ttydev_open+0x2d seen. Fixed in r237219.
30
31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > pts.c
38mycc -o pts -Wall -Wextra -O2 pts.c -lutil || exit 1
39rm -f pts.c
40
41/tmp/pts &
42pid=$!
43
44while kill -0 $! 2>/dev/null; do
45	$here/../testcases/swap/swap -t 2m -i 20 > /dev/null
46done
47wait $pid
48status=$?
49
50rm -f /tmp/pts
51exit $status
52EOF
53#include <sys/types.h>
54#include <sys/ioctl.h>
55#include <sys/param.h>
56#include <sys/wait.h>
57
58#include <err.h>
59#include <errno.h>
60#include <fcntl.h>
61#include <fts.h>
62#include <libutil.h>
63#include <stdint.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <termios.h>
68#include <time.h>
69#include <unistd.h>
70
71#define LOOPS 10
72#define RUNTIME 60
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) {
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 master, slave;
116	char slname[1025];
117
118	start = time(NULL);
119	while (time(NULL) - start < RUNTIME) {
120		if (openpty(&master, &slave, slname, NULL, NULL) == -1)
121			err(1, "openpty");
122		usleep(arc4random() % 10000);
123		if (close(master) == -1)
124			err(1, "close(master)");
125		if (close(slave) == -1)
126			err(1, "close(%s)", slname);
127	}
128	_exit(0);
129}
130
131int
132main(void)
133{
134	int i, j, s, status;
135
136	status = 0;
137	for (j = 0; j < LOOPS && status == 0; j++) {
138		for (i = 0; i < 2; i++) {
139			if (fork() == 0)
140				pty();
141		}
142		for (i = 0; i < 2; i++) {
143			if (fork() == 0)
144				churn("/dev/pts");
145		}
146		for (i = 0; i < 4; i++) {
147			wait(&s);
148			if (s != 0)
149				status = 1;
150		}
151	}
152
153	return (status);
154}
155