xref: /freebsd/tools/test/stress2/misc/dev3.sh (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# pts memory leak regression test.
30
31# Leaks seen when flags is either O_SHLOCK or O_EXLOCK and /dev/ptmx and
32# /dev/pts/ is being opened.
33# Fixed in r313496.
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37. ../default.cfg
38
39kldstat -v | grep -q pty || { kldload pty || exit 0; }
40here=`pwd`
41cd /tmp
42sed '1,/^EOF/d' < $here/$0 > dev3.c
43mycc -o dev3 -Wall -Wextra -O2 dev3.c || exit 1
44rm -f dev3.c
45
46#(cd $here/../testcases/swap; ./swap -t 10h -i 20 -l 100) > \
47#    /dev/null &
48
49pts=`vmstat -m | grep pts | awk '{print $2}'`
50[ -z "$pts" ] && pts=0
51
52e=0
53n=0
54while true; do
55	su $testuser -c "/tmp/dev3 $n"
56	new=`vmstat -m | grep pts | awk '{print $2}'`
57	if [ $new -gt $pts ]; then
58		leak=$((new - pts))
59		printf "flag %d (0x%x) leaks %d pts, %d allocated.\n" $n $n \
60		    $leak $new
61		pts=$new
62		e=1
63	fi
64	[ $n -eq 0 ] && n=1 || n=$((n * 2))
65	[ $n -gt $((0x00200000)) ] && break	# O_VERIFY
66done
67while pkill -9 swap; do
68	sleep 1
69done
70wait
71rm -f /tmp/dev3
72exit $e
73EOF
74#include <sys/types.h>
75#include <sys/wait.h>
76
77#include <err.h>
78#include <errno.h>
79#include <fcntl.h>
80#include <fts.h>
81#include <setjmp.h>
82#include <signal.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <time.h>
86#include <unistd.h>
87
88#define PARALLEL 4
89#define RUNTIME 60
90
91jmp_buf jbuf;
92char path[80];
93
94void
95handler(int i __unused) {
96        longjmp(jbuf, 1);
97}
98
99void
100churn(int flag, char *path)
101{
102	FTS *fts;
103	FTSENT *p;
104	time_t start;
105	int fd, ftsoptions;
106	char *args[2];
107
108	ftsoptions = FTS_PHYSICAL;
109	args[0] = path;
110	args[1] = 0;
111
112	start = time(NULL);
113	while (time(NULL) - start < RUNTIME) {
114		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
115			err(1, "fts_open");
116
117		(void)setjmp(jbuf);
118		ualarm(0, 0);
119		while ((p = fts_read(fts)) != NULL) {
120			if (p->fts_info == FTS_D ||
121			   p->fts_info == FTS_DP)
122				continue;
123			ualarm(500000, 0);
124			if ((fd = open(p->fts_path, flag)) == -1)
125				continue;
126			ualarm(0, 0);
127			usleep(arc4random() % 1000);
128			close(fd);
129
130		}
131
132		if (errno != 0 && errno != ENOENT)
133			warn("fts_read");
134		if (fts_close(fts) == -1)
135			err(1, "fts_close()");
136	}
137
138	_exit(0);
139}
140
141int
142main(int argc, char *argv[])
143{
144	int flag, i;
145
146	if (argc != 2) {
147		fprintf(stderr, "Usage: %s <flag>\n", argv[0]);
148		exit(1);
149	}
150	flag = atoi(argv[1]);
151	signal(SIGALRM, handler);
152	for (i = 0; i < PARALLEL; i++)
153		if (fork() == 0)
154			churn(flag, "/dev");
155
156	for (i = 0; i < PARALLEL; i++)
157		wait(NULL);
158
159	return (0);
160}
161