1 /* $NetBSD: t_rmdirrace.c,v 1.6 2010/07/13 11:12:19 pooka 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 <unistd.h> 38 39 #include <rump/rump_syscalls.h> 40 #include <rump/rump.h> 41 42 #include "../common/h_fsmacros.h" 43 44 #define DIRNAME "rmdir.test" 45 46 static void *func1(void *arg) 47 { 48 49 while (*(int *)arg != 1) 50 rump_sys_mkdir(DIRNAME, 0755); 51 52 return NULL; 53 } 54 55 static void *func2(void *arg) 56 { 57 58 while (*(int *)arg != 1) 59 rump_sys_rmdir(DIRNAME); 60 61 return NULL; 62 } 63 64 static void 65 race(const atf_tc_t *tc, const char *path) 66 { 67 int res, fd, quit; 68 pthread_t th1, th2; 69 70 if (FSTYPE_LFS(tc)) 71 atf_tc_expect_signal(-1, "PR kern/43582"); 72 if (FSTYPE_SYSVBFS(tc)) 73 atf_tc_skip("rmdir(2) not supported by file system"); 74 75 fd = rump_sys_open(".", O_RDONLY, 0666); 76 if (fd == -1) 77 atf_tc_fail("open failed"); 78 res = rump_sys_chdir(path); 79 if (res == -1) 80 atf_tc_fail("chdir failed"); 81 82 quit = 0; 83 84 res = pthread_create(&th1, NULL, func1, &quit); 85 if (res != 0) 86 atf_tc_fail("pthread_create1 failed"); 87 res = pthread_create(&th2, NULL, func2, &quit); 88 if (res != 0) 89 atf_tc_fail("pthread_create2 failed"); 90 91 sleep(10); 92 93 quit = 1; 94 95 res = pthread_join(th2, NULL); 96 if (res != 0) 97 atf_tc_fail("pthread_join2 failed"); 98 res = pthread_join(th1, NULL); 99 if (res != 0) 100 atf_tc_fail("pthread_join1 failed"); 101 102 res = rump_sys_fchdir(fd); 103 if (res == -1) 104 atf_tc_fail("fchdir failed"); 105 106 /* 107 * Rarely the LFS test does not crash. atf currently has no way of 108 * saying "just chill even if the test doesn't fail", so this 109 * takes care of it. 110 */ 111 if (FSTYPE_LFS(tc)) 112 abort(); 113 } 114 115 ATF_FSAPPLY(race, "rmdir(2) race"); 116