xref: /freebsd/contrib/netbsd-tests/fs/vfs/t_rmdirrace.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*	$NetBSD: t_rmdirrace.c,v 1.9 2012/02/16 02:47:56 perseant Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nicolas Joly.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/stat.h>
33 
34 #include <atf-c.h>
35 #include <fcntl.h>
36 #include <pthread.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #include <rump/rump_syscalls.h>
41 #include <rump/rump.h>
42 
43 #include "../common/h_fsmacros.h"
44 
45 #define DIRNAME "rmdir.test"
46 
47 static void *func1(void *arg)
48 {
49 
50 	while (*(int *)arg != 1)
51 		rump_sys_mkdir(DIRNAME, 0755);
52 
53 	return NULL;
54 }
55 
56 static void *func2(void *arg)
57 {
58 
59 	while (*(int *)arg != 1)
60 		rump_sys_rmdir(DIRNAME);
61 
62 	return NULL;
63 }
64 
65 static void
66 race(const atf_tc_t *tc, const char *path)
67 {
68 	int res, fd, quit;
69 	pthread_t th1, th2;
70 
71 	if (FSTYPE_SYSVBFS(tc))
72 		atf_tc_skip("directories not supported by file system");
73 
74 	fd = rump_sys_open(".", O_RDONLY, 0666);
75 	if (fd == -1)
76 		atf_tc_fail("open failed");
77 	res = rump_sys_chdir(path);
78 	if (res == -1)
79 		atf_tc_fail("chdir failed");
80 
81 	quit = 0;
82 
83 	res = pthread_create(&th1, NULL, func1, &quit);
84 	if (res != 0)
85 		atf_tc_fail("pthread_create1 failed");
86 	res = pthread_create(&th2, NULL, func2, &quit);
87 	if (res != 0)
88 		atf_tc_fail("pthread_create2 failed");
89 
90 	sleep(10);
91 
92 	quit = 1;
93 
94 	res = pthread_join(th2, NULL);
95 	if (res != 0)
96 		atf_tc_fail("pthread_join2 failed");
97 	res = pthread_join(th1, NULL);
98 	if (res != 0)
99 		atf_tc_fail("pthread_join1 failed");
100 
101 	res = rump_sys_fchdir(fd);
102 	if (res == -1)
103 		atf_tc_fail("fchdir failed");
104 }
105 
106 ATF_FSAPPLY(race, "rmdir(2) race");
107