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