xref: /freebsd/tools/test/stress2/misc/rename7.sh (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
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
49newfs $newfs_flags md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51chmod 777 $mntpoint
52
53su $testuser -c "cd $mntpoint; /tmp/rename7 || echo FAIL"
54
55for i in `jot 10`; do
56	mount | grep -q md$mdstart  && \
57		umount $mntpoint && mdconfig -d -u $mdstart && break
58done
59if mount | grep -q md$mdstart; then
60	echo "Test failed"
61	exit 1
62fi
63rm -f /tmp/rename7
64exit 0
65EOF
66#include <err.h>
67#include <errno.h>
68#include <fcntl.h>
69#include <sched.h>
70#include <signal.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <strings.h>
74#include <sys/param.h>
75#include <sys/stat.h>
76#include <sys/time.h>
77#include <sys/wait.h>
78#include <time.h>
79#include <unistd.h>
80
81const char *logfile = "test.log";
82pid_t wpid, spid;
83
84void
85r1(void)
86{
87	int i;
88	struct stat sb1, sb2;
89
90	for (i = 0; i < 800000; i++) {
91		rename(logfile, "r1");
92		if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
93		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
94			fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r1\")\n", logfile);
95			system("ls -ail");
96			_exit(1);
97		}
98	}
99	_exit(0);
100}
101
102void
103r2(void)
104{
105	int i;
106	struct stat sb1, sb2;
107
108//	_exit(0); /* No problems with only r1 running */
109	for (i = 0; i < 800000; i++) {
110		rename(logfile, "r2");
111		if (stat("r1", &sb1) == 0 && stat("r2", &sb2) == 0 &&
112		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
113			usleep(10000);
114			fprintf(stderr, "r1 and r2 are identical after rename(%s, \"r2\")\n", logfile);
115			system("ls -ail");
116			_exit(1);
117		}
118	}
119	_exit(0);
120}
121int
122main(void)
123{
124	pid_t wpid, spid;
125	time_t start;
126	int e, fd, 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	start = time(NULL);
137	while (time(NULL) - start < 60) {
138		if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
139			warn("creat(%s)", logfile);
140		close(fd);
141	}
142
143	kill(wpid, SIGINT);
144	kill(spid, SIGINT);
145	wait(&status);
146	e += WEXITSTATUS(status);
147	wait(&status);
148	e += WEXITSTATUS(status);
149
150	return (e);
151}
152