xref: /freebsd/tools/test/stress2/misc/suj5.sh (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1#!/bin/sh
2
3#
4# Copyright (c) 2010 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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# "panic: general protection fault" seen:
32# https://people.freebsd.org/~pho/stress/log/log0398.txt
33
34. ../default.cfg
35
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > suj5.c
39mycc -o suj5 -Wall -O2 suj5.c
40rm -f suj5.c
41cd $here
42
43mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
44mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
45mdconfig -a -t swap -s 1g -u $mdstart
46newfs -j md$mdstart > /dev/null
47mount /dev/md$mdstart $mntpoint
48chmod 777 $mntpoint
49
50su $testuser -c "cd $mntpoint; /tmp/suj5" > /dev/null
51
52while mount | grep $mntpoint | grep -q /dev/md; do
53	umount $mntpoint || sleep 1
54done
55mdconfig -d -u $mdstart
56rm -f /tmp/suj5
57exit
58EOF
59#include <sys/param.h>
60#include <sys/mount.h>
61#include <sys/stat.h>
62#include <sys/wait.h>
63#include <err.h>
64#include <fcntl.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <unistd.h>
68
69#define PARALLEL 10
70
71static int
72random_int(int mi, int ma)
73{
74	return (arc4random()  % (ma - mi + 1) + mi);
75}
76
77static int64_t
78df(void)
79{
80	char path[MAXPATHLEN+1];
81	struct statfs buf;
82
83	if (getcwd(path, sizeof(path)) == NULL)
84		err(1, "getcwd()");
85
86	if (statfs(path, &buf) < 0)
87		err(1, "statfs(%s)", path);
88	printf("Free space on %s: %jd Mb\n", path, buf.f_bavail * buf.f_bsize / 1024 / 1024);
89	return (buf.f_bavail * buf.f_bsize);
90}
91
92static void
93test(int size)
94{
95	int buf[1024], index, to;
96#ifdef TEST
97	int i;
98#endif
99	int fd;
100	char file[128];
101
102	sprintf(file,"p%05d", getpid());
103	if ((fd = creat(file, 0660)) == -1)
104		err(1, "creat(%s)", file);
105
106	to = sizeof(buf);
107	index = 0;
108	while (index < size) {
109		if (index + to > size)
110			to = size - index;
111#ifdef TEST
112		for (i = 0; i < to; i++)
113			buf[i] = index + i;
114#endif
115		index += to;
116		if (write(fd, buf, to) != to)
117			err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
118	}
119	if (close(fd) == -1)
120		err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
121
122	if ((fd = open(file, O_RDONLY)) == -1)
123		err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
124
125	index = 0;
126	while (index < size) {
127		if (index + to > size)
128			to = size - index;
129		if (read(fd, buf, to) != to)
130			err(1, "rw read. %s.%d", __FILE__, __LINE__);
131#ifdef TEST
132		for (i = 0; i < to; i++) {
133			if (buf[i] != index + i) {
134				fprintf(stderr,
135					"%s, pid %d: expected %d @ %d, got %d\n",
136					getprogname(), getpid(), index+i, index+i,
137					buf[i]);
138				exit(EXIT_FAILURE);
139			}
140		}
141#endif
142		index += to;
143	}
144	if (close(fd) == -1)
145		err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
146	if (unlink(file) == -1)
147		err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
148	exit(0);
149}
150
151int
152main()
153{
154	int i, j, pct;
155	int size;	/* in k */
156	int64_t bl;
157
158	bl = df();
159	if (bl > (int64_t)INT_MAX * PARALLEL)
160		bl = (int64_t)INT_MAX * PARALLEL;
161	size = bl / PARALLEL / 1024;
162
163	pct = random_int(1, 50);
164	size = size / 100 * pct + 1;
165	if (random_int(1, 100) <= 50)
166		size = 34 * 1024;	/* Known good deadlock value */
167	printf("Max file size: %d Mb\n", size / 1024);
168
169	for (i = 0; i < 100; i++) {
170		for (j = 0; j < PARALLEL; j++) {
171			if (fork() == 0) {
172				test(random_int(1, size) * 1024);
173			}
174		}
175		for (j = 0; j < PARALLEL; j++)
176			wait(NULL);
177	}
178
179	return (0);
180}
181