xref: /freebsd/tools/test/stress2/misc/dev2.sh (revision 9768746b)
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# Test parallel access to /dev. A non-root version.
30
31# "panic: Bad link elm 0xfffff8000b07ed00 prev->next != elm" seen.
32# https://people.freebsd.org/~pho/stress/log/dev2.txt
33
34# Fixed by r294204.
35
36# "panic: Assertion !tty_gone(tp) failed at ../sys/ttydevsw.h:165" seen:
37# https://people.freebsd.org/~pho/stress/log/dev2-2.txt
38
39[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
40
41. ../default.cfg
42
43kldstat -v | grep -q pty || { kldload pty || exit 0; }
44here=`pwd`
45cd /tmp
46sed '1,/^EOF/d' < $here/$0 > dev2.c
47mycc -o dev2 -Wall -Wextra -O2 dev2.c || exit 1
48rm -f dev2.c
49
50daemon sh -c \
51    "(cd $here/../testcases/swap; ./swap -t 6m -i 20 -k -l 100)" > \
52    /dev/null
53
54su $testuser -c /tmp/dev2
55
56while pkill -9 swap; do
57	:
58done
59
60rm -f /tmp/dev2
61exit
62EOF
63#include <sys/types.h>
64#include <sys/wait.h>
65
66#include <err.h>
67#include <errno.h>
68#include <fcntl.h>
69#include <fts.h>
70#include <setjmp.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <time.h>
75#include <unistd.h>
76
77#define PARALLEL 4
78#define RUNTIME 300
79
80jmp_buf jbuf;
81char path[80];
82
83void
84handler(int i __unused) {
85        longjmp(jbuf, 1);
86}
87
88void
89churn(char *path)
90{
91	FTS *fts;
92	FTSENT *p;
93	time_t start;
94	int fd, ftsoptions;
95	char *args[2];
96
97	ftsoptions = FTS_PHYSICAL;
98	args[0] = path;
99	args[1] = 0;
100
101	start = time(NULL);
102	while (time(NULL) - start < RUNTIME) {
103		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
104			err(1, "fts_open");
105
106		(void)setjmp(jbuf);
107		ualarm(0, 0);
108		while ((p = fts_read(fts)) != NULL) {
109			if (p->fts_info == FTS_D ||
110			   p->fts_info == FTS_DP)
111				continue;
112			ualarm(500000, 0);
113			if ((fd = open(p->fts_path, arc4random() % (O_CLOEXEC << 2))) == -1)
114				continue;
115			ualarm(0, 0);
116			usleep(arc4random() % 1000);
117			close(fd);
118
119		}
120
121		if (errno != 0 && errno != ENOENT)
122			warn("fts_read");
123		if (fts_close(fts) == -1)
124			err(1, "fts_close()");
125	}
126
127	_exit(0);
128}
129
130int
131main(void)
132{
133	int i;
134
135	signal(SIGALRM, handler);
136	for (i = 0; i < PARALLEL; i++)
137		if (fork() == 0)
138			churn("/dev");
139
140	for (i = 0; i < PARALLEL; i++)
141		wait(NULL);
142
143	return (0);
144}
145