xref: /netbsd-src/tests/fs/vfs/t_rmdirrace.c (revision ed08fe651239e1fe4de39aff0d89930dfaab8713)
1*ed08fe65Sperseant /*	$NetBSD: t_rmdirrace.c,v 1.9 2012/02/16 02:47:56 perseant Exp $	*/
21511eb19Snjoly 
31511eb19Snjoly /*-
41511eb19Snjoly  * Copyright (c) 2010 The NetBSD Foundation, Inc.
51511eb19Snjoly  * All rights reserved.
61511eb19Snjoly  *
71511eb19Snjoly  * This code is derived from software contributed to The NetBSD Foundation
878043be0Snjoly  * by Nicolas Joly.
91511eb19Snjoly  *
101511eb19Snjoly  * Redistribution and use in source and binary forms, with or without
111511eb19Snjoly  * modification, are permitted provided that the following conditions
121511eb19Snjoly  * are met:
131511eb19Snjoly  * 1. Redistributions of source code must retain the above copyright
141511eb19Snjoly  *    notice, this list of conditions and the following disclaimer.
151511eb19Snjoly  * 2. Redistributions in binary form must reproduce the above copyright
161511eb19Snjoly  *    notice, this list of conditions and the following disclaimer in the
171511eb19Snjoly  *    documentation and/or other materials provided with the distribution.
181511eb19Snjoly  *
191511eb19Snjoly  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201511eb19Snjoly  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211511eb19Snjoly  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221511eb19Snjoly  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231511eb19Snjoly  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241511eb19Snjoly  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251511eb19Snjoly  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261511eb19Snjoly  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271511eb19Snjoly  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281511eb19Snjoly  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291511eb19Snjoly  * POSSIBILITY OF SUCH DAMAGE.
301511eb19Snjoly  */
311511eb19Snjoly 
321511eb19Snjoly #include <sys/stat.h>
331511eb19Snjoly 
341511eb19Snjoly #include <atf-c.h>
351511eb19Snjoly #include <fcntl.h>
361511eb19Snjoly #include <pthread.h>
3795cbf373Spooka #include <stdlib.h>
381511eb19Snjoly #include <unistd.h>
391511eb19Snjoly 
401511eb19Snjoly #include <rump/rump_syscalls.h>
411511eb19Snjoly #include <rump/rump.h>
421511eb19Snjoly 
431511eb19Snjoly #include "../common/h_fsmacros.h"
441511eb19Snjoly 
451511eb19Snjoly #define DIRNAME "rmdir.test"
461511eb19Snjoly 
func1(void * arg)471511eb19Snjoly static void *func1(void *arg)
481511eb19Snjoly {
491511eb19Snjoly 
501511eb19Snjoly 	while (*(int *)arg != 1)
511511eb19Snjoly 		rump_sys_mkdir(DIRNAME, 0755);
521511eb19Snjoly 
531511eb19Snjoly 	return NULL;
541511eb19Snjoly }
551511eb19Snjoly 
func2(void * arg)561511eb19Snjoly static void *func2(void *arg)
571511eb19Snjoly {
581511eb19Snjoly 
591511eb19Snjoly 	while (*(int *)arg != 1)
601511eb19Snjoly 		rump_sys_rmdir(DIRNAME);
611511eb19Snjoly 
621511eb19Snjoly 	return NULL;
631511eb19Snjoly }
641511eb19Snjoly 
651511eb19Snjoly static void
race(const atf_tc_t * tc,const char * path)66e4723ab8Snjoly race(const atf_tc_t *tc, const char *path)
671511eb19Snjoly {
681511eb19Snjoly 	int res, fd, quit;
691511eb19Snjoly 	pthread_t th1, th2;
701511eb19Snjoly 
715b925538Spooka 	if (FSTYPE_SYSVBFS(tc))
727557af29Snjoly 		atf_tc_skip("directories not supported by file system");
73136ff619Snjoly 
741511eb19Snjoly 	fd = rump_sys_open(".", O_RDONLY, 0666);
751511eb19Snjoly 	if (fd == -1)
761511eb19Snjoly 		atf_tc_fail("open failed");
771511eb19Snjoly 	res = rump_sys_chdir(path);
781511eb19Snjoly 	if (res == -1)
791511eb19Snjoly 		atf_tc_fail("chdir failed");
801511eb19Snjoly 
811511eb19Snjoly 	quit = 0;
821511eb19Snjoly 
831511eb19Snjoly 	res = pthread_create(&th1, NULL, func1, &quit);
841511eb19Snjoly 	if (res != 0)
851511eb19Snjoly 		atf_tc_fail("pthread_create1 failed");
861511eb19Snjoly 	res = pthread_create(&th2, NULL, func2, &quit);
871511eb19Snjoly 	if (res != 0)
881511eb19Snjoly 		atf_tc_fail("pthread_create2 failed");
891511eb19Snjoly 
901511eb19Snjoly 	sleep(10);
911511eb19Snjoly 
921511eb19Snjoly 	quit = 1;
931511eb19Snjoly 
941511eb19Snjoly 	res = pthread_join(th2, NULL);
951511eb19Snjoly 	if (res != 0)
961511eb19Snjoly 		atf_tc_fail("pthread_join2 failed");
971511eb19Snjoly 	res = pthread_join(th1, NULL);
981511eb19Snjoly 	if (res != 0)
991511eb19Snjoly 		atf_tc_fail("pthread_join1 failed");
1001511eb19Snjoly 
1011511eb19Snjoly 	res = rump_sys_fchdir(fd);
1021511eb19Snjoly 	if (res == -1)
1031511eb19Snjoly 		atf_tc_fail("fchdir failed");
1041511eb19Snjoly }
1051511eb19Snjoly 
1061511eb19Snjoly ATF_FSAPPLY(race, "rmdir(2) race");
107