xref: /freebsd/tools/test/stress2/misc/rename7.sh (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 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# After a few runs this will happen:
32# $ umount /mnt
33# umount: unmount of /mnt failed: Device busy
34# $ umount -f /mnt
35# $
36
37. ../default.cfg
38
39here=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $here/$0 > rename7.c
42mycc -o rename7 -Wall -Wextra -O2 rename7.c || exit
43rm -f rename7.c
44cd $here
45
46mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
47mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 2g -u $mdstart
49bsdlabel -w md$mdstart auto
50newfs $newfs_flags md${mdstart}$part > /dev/null
51mount /dev/md${mdstart}$part $mntpoint
52chmod 777 $mntpoint
53
54su $testuser -c "cd $mntpoint; /tmp/rename7 || echo FAIL"
55
56for i in `jot 10`; do
57	mount | grep -q md${mdstart}$part  && \
58		umount $mntpoint && mdconfig -d -u $mdstart && break
59done
60if mount | grep -q md${mdstart}$part; then
61	echo "Test failed"
62	exit 1
63fi
64rm -f /tmp/rename7
65exit 0
66EOF
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <sched.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <strings.h>
75#include <sys/param.h>
76#include <sys/stat.h>
77#include <sys/time.h>
78#include <sys/wait.h>
79#include <time.h>
80#include <unistd.h>
81
82const char *logfile = "test.log";
83pid_t wpid, spid;
84
85void
86r1(void)
87{
88	int i;
89	struct stat sb1, sb2;
90
91	for (i = 0; i < 800000; i++) {
92		rename(logfile, "r1");
93		if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
94		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
95			fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r1\")\n", logfile);
96			system("ls -ail");
97			_exit(1);
98		}
99	}
100	_exit(0);
101}
102
103void
104r2(void)
105{
106	int i;
107	struct stat sb1, sb2;
108
109//	_exit(0); /* No problems with only r1 running */
110	for (i = 0; i < 800000; i++) {
111		rename(logfile, "r2");
112		if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
113		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
114			usleep(10000);
115			fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r2\")\n", logfile);
116			system("ls -ail");
117			_exit(1);
118		}
119	}
120	_exit(0);
121}
122int
123main(void)
124{
125	pid_t wpid, spid;
126	int e, fd, i, status;
127
128	if ((wpid = fork()) == 0)
129		r1();
130	if ((spid = fork()) == 0)
131		r2();
132
133	setproctitle("main");
134	e = 0;
135
136	for (i = 0; i < 800000; i++) {
137		if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
138			warn("creat(%s)", logfile);
139		close(fd);
140	}
141
142	kill(wpid, SIGINT);
143	kill(spid, SIGINT);
144	wait(&status);
145	e += WEXITSTATUS(status);
146	wait(&status);
147	e += WEXITSTATUS(status);
148
149	return (e);
150}
151