xref: /freebsd/tools/test/stress2/misc/dev.sh (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
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 read access to /dev.
30
31# "panic: Most recently used by DEVFS1" seen.
32# https://people.freebsd.org/~pho/stress/log/dev.txt
33# Fixed by r293826.
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37. ../default.cfg
38
39here=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $here/$0 > dev.c
42mycc -o dev -Wall -Wextra -O2 dev.c || exit 1
43rm -f dev.c
44
45echo "Expect: g_access(958): provider ufsid/5c581221fcac7d80 has error 6 set"
46daemon sh -c \
47    "(cd $here/../testcases/swap; ./swap -t 6m -i 20 -k -l 100)" > \
48    /dev/null
49
50/tmp/dev	# Note: this runs as root.
51s=$?
52
53while pkill -9 swap; do
54	:
55done
56
57rm -f /tmp/dev
58exit $s
59EOF
60#include <sys/types.h>
61#include <sys/wait.h>
62
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <fts.h>
67#include <stdlib.h>
68#include <stdio.h>
69#include <string.h>
70#include <time.h>
71#include <unistd.h>
72
73#define PARALLEL 4
74#define RUNTIME 300
75
76char path[80];
77
78void
79churn(char *path)
80{
81	FTS *fts;
82	FTSENT *p;
83	time_t start;
84	int fd, ftsoptions;
85	char *args[2];
86
87	ftsoptions = FTS_PHYSICAL;
88	args[0] = path;
89	args[1] = 0;
90
91	start = time(NULL);
92	while (time(NULL) - start < RUNTIME) {
93		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
94			err(1, "fts_open");
95
96		while ((p = fts_read(fts)) != NULL) {
97			if (p->fts_info == FTS_D ||
98			   p->fts_info == FTS_DP)
99				continue;
100			if (strstr(p->fts_path, "ttyu") != NULL)
101				continue;
102			if (strstr(p->fts_path, "fcdm") != NULL)
103				continue;
104			if ((fd = open(p->fts_path, O_RDONLY|O_NONBLOCK)) == -1)
105				continue;
106			usleep(arc4random() % 1000);
107			close(fd);
108
109		}
110
111		if (errno != 0 && errno != ENOENT)
112			warn("fts_read");
113		if (fts_close(fts) == -1)
114			err(1, "fts_close()");
115	}
116
117	_exit(0);
118}
119
120int
121main(void)
122{
123	int e, i, status;;
124
125	e = 0;
126	for (i = 0; i < PARALLEL; i++)
127		if (fork() == 0)
128			churn("/dev");
129
130	for (i = 0; i < PARALLEL; i++) {
131		wait(&status);
132		if (status != 0)
133			e++;
134	}
135
136	return (e);
137}
138