xref: /freebsd/tools/test/stress2/misc/rename.sh (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1#!/bin/sh
2
3#
4# Copyright (c) 2009 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# Test race between ISDOTDOT lookups and directory removal/rename
30
31# With lookup_shared=1 rename() will fail from time to time with ENOENT and
32# the following stat() will succed.
33
34# Test scenario by tegge
35
36. ../default.cfg
37
38here=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $here/$0 > rename.c
41mycc -o rename -Wall rename.c || exit 1
42rm -f rename.c
43
44rm -rf /tmp/rename.dir.*
45for i in `jot 10`; do
46	for j in `jot 10`; do
47		/tmp/rename &
48	done
49	for j in `jot 10`; do
50		wait
51	done
52done
53rm -rf /tmp/rename.dir.* /tmp/rename
54exit 0
55EOF
56#include <err.h>
57#include <fcntl.h>
58#include <libgen.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sys/stat.h>
63#include <sys/types.h>
64#include <sys/wait.h>
65#include <time.h>
66#include <unistd.h>
67
68#define RUNTIME 30
69
70static char dir1[128];
71static char dir2[128];
72
73int
74main(int argc, char **argv)
75{
76	time_t start;
77	int status;
78	struct stat sb;
79	pid_t p;
80
81	sprintf(dir1, "rename.dir.%d", getpid());
82	sprintf(dir2, "rename.dir.2.%d", getpid());
83	if (mkdir(dir1, 0700) == -1)
84		err(1, "mkdir(%s)", dir1);
85
86	if (chdir(dir1) == -1)
87		err(1, "chdir(%s)", dir1);
88	if ((p = fork()) == -1)
89		err(1, "fork()");
90	if (p == 0) {
91		setproctitle("child");
92		if (chdir("..") == -1)
93			err(1, "chdir(%s)", "..");
94		start = time(NULL);
95		while (time(NULL) - start < RUNTIME) {
96			if (rename(dir1, dir2) == -1) {
97				warn("rename(%s, %s)", dir1, dir2);
98				stat(dir1, &sb);
99				if (stat(dir1, &sb) == -1)
100					err(1, "stat(%s)", dir1);
101				else
102					errx(1, "stat(%s) succeeded!", dir1);
103			}
104			if (rename(dir2, dir1) == -1) {
105				warn("rename(%s, %s)", dir2, dir1);
106				stat(dir2, &sb);
107				if (stat(dir2, &sb) == -1)
108					err(1, "stat(%s)", dir2);
109				else
110					errx(1, "stat(%s) succeeded!", dir2);
111			}
112		}
113		_exit(0);
114	} else {
115		setproctitle("parent");
116		start = time(NULL);
117		while (time(NULL) - start < RUNTIME) {
118			if (stat("..", &sb) == -1)
119				err(1, "stat(..)");
120		}
121	}
122	if (waitpid(p, &status, 0) == -1)
123		err(1, "waitpid()");
124	if (chdir("..") == -1)
125		err(1, "chdir(%s)", "..");
126	if (rmdir(dir1) == -1)
127		err(1, "rmdir(%s)", dir1);
128
129	return (0);
130}
131