1272343Sngie/*	$NetBSD: t_rmdirrace.c,v 1.9 2012/02/16 02:47:56 perseant Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Nicolas Joly.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <sys/stat.h>
33272343Sngie
34272343Sngie#include <atf-c.h>
35272343Sngie#include <fcntl.h>
36272343Sngie#include <pthread.h>
37272343Sngie#include <stdlib.h>
38272343Sngie#include <unistd.h>
39272343Sngie
40272343Sngie#include <rump/rump_syscalls.h>
41272343Sngie#include <rump/rump.h>
42272343Sngie
43272343Sngie#include "../common/h_fsmacros.h"
44272343Sngie
45272343Sngie#define DIRNAME "rmdir.test"
46272343Sngie
47272343Sngiestatic void *func1(void *arg)
48272343Sngie{
49272343Sngie
50272343Sngie	while (*(int *)arg != 1)
51272343Sngie		rump_sys_mkdir(DIRNAME, 0755);
52272343Sngie
53272343Sngie	return NULL;
54272343Sngie}
55272343Sngie
56272343Sngiestatic void *func2(void *arg)
57272343Sngie{
58272343Sngie
59272343Sngie	while (*(int *)arg != 1)
60272343Sngie		rump_sys_rmdir(DIRNAME);
61272343Sngie
62272343Sngie	return NULL;
63272343Sngie}
64272343Sngie
65272343Sngiestatic void
66272343Sngierace(const atf_tc_t *tc, const char *path)
67272343Sngie{
68272343Sngie	int res, fd, quit;
69272343Sngie	pthread_t th1, th2;
70272343Sngie
71272343Sngie	if (FSTYPE_SYSVBFS(tc))
72272343Sngie		atf_tc_skip("directories not supported by file system");
73272343Sngie
74272343Sngie	fd = rump_sys_open(".", O_RDONLY, 0666);
75272343Sngie	if (fd == -1)
76272343Sngie		atf_tc_fail("open failed");
77272343Sngie	res = rump_sys_chdir(path);
78272343Sngie	if (res == -1)
79272343Sngie		atf_tc_fail("chdir failed");
80272343Sngie
81272343Sngie	quit = 0;
82272343Sngie
83272343Sngie	res = pthread_create(&th1, NULL, func1, &quit);
84272343Sngie	if (res != 0)
85272343Sngie		atf_tc_fail("pthread_create1 failed");
86272343Sngie	res = pthread_create(&th2, NULL, func2, &quit);
87272343Sngie	if (res != 0)
88272343Sngie		atf_tc_fail("pthread_create2 failed");
89272343Sngie
90272343Sngie	sleep(10);
91272343Sngie
92272343Sngie	quit = 1;
93272343Sngie
94272343Sngie	res = pthread_join(th2, NULL);
95272343Sngie	if (res != 0)
96272343Sngie		atf_tc_fail("pthread_join2 failed");
97272343Sngie	res = pthread_join(th1, NULL);
98272343Sngie	if (res != 0)
99272343Sngie		atf_tc_fail("pthread_join1 failed");
100272343Sngie
101272343Sngie	res = rump_sys_fchdir(fd);
102272343Sngie	if (res == -1)
103272343Sngie		atf_tc_fail("fchdir failed");
104272343Sngie}
105272343Sngie
106272343SngieATF_FSAPPLY(race, "rmdir(2) race");
107