xref: /freebsd/tools/test/stress2/misc/cmp.sh (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# Cross mount test of mkdir(2).
30# Page fault seen:
31# http://people.freebsd.org/~pho/stress/log/cmp.txt
32# Fixed by r275347
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36. ../default.cfg
37
38here=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $here/$0 > cmp.c
41mycc -o cmp -Wall -Wextra -O2 -g cmp.c || exit 1
42rm -f cmp.c
43
44mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
45[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
46
47mdconfig -a -t swap -s 2g -u $mdstart || exit 1
48bsdlabel -w md$mdstart auto
49# Don't use SU due to bogus "out of inodes" messages.
50newfs md${mdstart}$part > /dev/null
51mount /dev/md${mdstart}$part $mntpoint
52chmod 777 $mntpoint
53
54daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
55    > /dev/null 2>&1
56sleep 1
57su $testuser -c "/tmp/cmp $mntpoint" &
58
59while kill -0 $! 2>/dev/null; do
60	umount -f $mntpoint &&
61	    mount /dev/md${mdstart}$part $mntpoint
62	chmod 777 $mntpoint
63	sleep .1
64done
65wait
66
67while mount | grep $mntpoint | grep -q /dev/md; do
68	umount $mntpoint || sleep 1
69done
70mdconfig -d -u $mdstart
71[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
72
73# tmpfs
74mount -t tmpfs tmpfs $mntpoint
75chmod 777 $mntpoint
76
77su $testuser -c "/tmp/cmp $mntpoint" &
78
79while kill -0 $! 2>/dev/null; do
80	umount -f $mntpoint &&
81	    mount -t tmpfs tmpfs $mntpoint
82	chmod 777 $mntpoint
83	sleep .1
84done
85pkill -9 swap
86wait
87
88while pkill -9 swap; do
89	:
90done > /dev/null 2>&1
91while mount | grep $mntpoint | grep -q tmpfs; do
92	umount $mntpoint || sleep 1
93done
94[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
95rm -f /tmp/cmp
96exit 0
97EOF
98#include <sys/param.h>
99#include <sys/stat.h>
100#include <sys/wait.h>
101
102#include <err.h>
103#include <errno.h>
104#include <fcntl.h>
105#include <stdio.h>
106#include <stdlib.h>
107#include <string.h>
108#include <unistd.h>
109
110#define LOOPS 160
111#define PARALLEL 16
112
113int nbc, nbd;
114char *dir;
115
116void
117tmkdir(void)
118{
119	int i, j;
120	char d[MAXPATHLEN + 1], name[MAXPATHLEN + 1];
121
122	setproctitle(__func__);
123
124	i = 0;
125	snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
126	if (mkdir(name, 0755) == -1) {
127		if (errno != ENAMETOOLONG && errno != ENOENT &&
128		    errno != EBUSY && errno != EACCES && errno != EPERM)
129			warn("mkdir(%s)", name);
130		_exit(0);
131	}
132	for (;;) {
133		snprintf(d, sizeof(d), "/%d", i++);
134		strncat(name, d, sizeof(name) - 1);
135		if (mkdir(name, 0755) == -1) {
136			if (errno != ENAMETOOLONG && errno != ENOENT &&
137			    errno != EBUSY && errno != EACCES && errno != EPERM)
138				warn("mkdir(%s)", name);
139			i--;
140			break;
141		}
142		nbc++;
143	}
144
145	while (i >= 0) {
146		snprintf(name, sizeof(name), "%s/d1.%05d", dir, getpid());
147		for (j = 0; j < i; j++) {
148			snprintf(d, sizeof(d), "/%d", j);
149			strncat(name, d, sizeof(name) - 1);
150		}
151		if (rmdir(name) == -1) {
152			if (errno != ENOTEMPTY && errno != ENOENT && errno !=
153			    EBUSY)
154				warn("rmdir(%s)", name);
155		} else
156			nbd++;
157		i--;
158	}
159#if defined(TEST)
160	if (nbc == 0)
161		fprintf(stderr, "FAIL nbc = %d, nbd = %d\n", nbc, nbd);
162#endif
163	_exit(0);
164}
165
166int
167main(int argc, char **argv)
168{
169	int i, j;
170
171	if (argc != 2) {
172		fprintf(stderr, "Usage: %s <full path to dir>", argv[0]);
173		exit(1);
174	}
175	dir = argv[1];
176
177	for (j = 0; j < LOOPS; j++) {
178		for (i = 0; i < PARALLEL; i++) {
179			if (fork() == 0)
180				tmkdir();
181		}
182		for (i = 0; i < PARALLEL; i++)
183			wait(NULL);
184	}
185
186	return(0);
187}
188