xref: /freebsd/tools/test/stress2/misc/fcntl2.sh (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# fcntl(2) fuzz
31# "umount: unmount of /mnt failed: Device busy" seen:
32# https://people.freebsd.org/~pho/stress/log/fcntl2.txt
33
34. ../default.cfg
35[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/fcntl2.c
41mycc -o fcntl2 -Wall -Wextra -O0 -g fcntl2.c || exit 1
42rm -f fcntl2.c
43cd $odir
44
45set -e
46mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
47[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 2g -u $mdstart
49newfs $newfs_flags md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51set +e
52
53(cd $odir/../testcases/swap; ./swap -t 3m -i 20) &
54cd $mntpoint
55limits -n 10000 $dir/fcntl2
56s=$?
57[ -f fcntl2.core -a $s -eq 0 ] &&
58    { ls -l fcntl2.core; mv fcntl2.core $dir; s=1; }
59cd $odir
60while pkill swap; do :; done
61
62for i in `jot 6`; do
63	mount | grep -q "on $mntpoint " || break
64	umount $mntpoint && break || sleep 10
65	[ $i -eq 6 ] &&
66	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
67done
68mdconfig -d -u $mdstart
69rm -rf $dir/fcntl2
70exit $s
71
72EOF
73#include <sys/param.h>
74#include <sys/mman.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <machine/atomic.h>
79
80#include <err.h>
81#include <errno.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <time.h>
86#include <unistd.h>
87
88static volatile u_int *share;
89
90#define PARALLEL 64
91#define RUNTIME (3 * 60)
92#define SYNC 0
93
94#define N (128 * 1024 / (int)sizeof(u_int32_t))
95static u_int32_t r[N];
96
97static unsigned long
98makearg(void)
99{
100	unsigned long val;
101	unsigned int i;
102
103	val = arc4random();
104	i   = arc4random() % 100;
105	if (i < 20)
106		val = val & 0xff;
107	if (i >= 20 && i < 40)
108		val = val & 0xffff;
109	if (i >= 40 && i < 60)
110		val = (unsigned long)(r) | (val & 0xffff);
111#if defined(__LP64__)
112	if (i >= 60) {
113		val = (val << 32) | arc4random();
114		if (i > 80)
115			val = val & 0x00007fffffffffffUL;
116	}
117#endif
118
119	return (val);
120}
121static void
122test(void)
123{
124	time_t start;
125	unsigned long arg3;
126	int cmd, fd, i, n, success;
127	char file[80];
128
129	atomic_add_int(&share[SYNC], 1);
130	while (share[SYNC] != PARALLEL)
131		;
132
133	success = 0;
134	snprintf(file, sizeof(file), "file.%d", getpid());
135	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE))
136	    == -1)
137		err(1, "open(%s)", file);
138	n = arc4random() % 100 + 1;
139	for (i = 0; i < n; i++)
140		if (write(fd, file, sizeof(file)) != sizeof(file))
141			err(1, "write()");
142	start = time(NULL);
143	while (time(NULL) - start < 60) {
144		cmd = arc4random() % 20;
145		arg3 = makearg();
146		alarm(20);
147		if (fcntl(fd, cmd, arg3) != -1)
148			success++;
149		alarm(0);
150	}
151	close(fd);
152	unlink(file);
153
154	_exit(0);
155}
156
157int
158main(void)
159{
160	pid_t pids[PARALLEL];
161	size_t len;
162	time_t start;
163	int i, status;
164
165	len = PAGE_SIZE;
166	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
167	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
168		err(1, "mmap");
169
170	start = time(NULL);
171	while ((time(NULL) - start) < RUNTIME) {
172		for (i = 0; i < N; i++)
173			r[i] = arc4random();
174		share[SYNC] = 0;
175		for (i = 0; i < PARALLEL; i++) {
176			if ((pids[i] = fork()) == 0)
177				test();
178			if (pids[i] == -1)
179				err(1, "fork()");
180		}
181		for (i = 0; i < PARALLEL; i++) {
182			if (waitpid(pids[i], &status, 0) == -1)
183				err(1, "waitpid(%d)", pids[i]);
184		}
185	}
186
187	return (0);
188}
189