1 /* $NetBSD: t_rmdirrace.c,v 1.7 2010/07/19 16:00:45 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 <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_LFS(tc)) 72 atf_tc_expect_signal(-1, "PR kern/43582"); 73 if (FSTYPE_SYSVBFS(tc)) 74 atf_tc_skip("rmdir(2) not supported by file system"); 75 76 fd = rump_sys_open(".", O_RDONLY, 0666); 77 if (fd == -1) 78 atf_tc_fail("open failed"); 79 res = rump_sys_chdir(path); 80 if (res == -1) 81 atf_tc_fail("chdir failed"); 82 83 quit = 0; 84 85 res = pthread_create(&th1, NULL, func1, &quit); 86 if (res != 0) 87 atf_tc_fail("pthread_create1 failed"); 88 res = pthread_create(&th2, NULL, func2, &quit); 89 if (res != 0) 90 atf_tc_fail("pthread_create2 failed"); 91 92 sleep(10); 93 94 quit = 1; 95 96 res = pthread_join(th2, NULL); 97 if (res != 0) 98 atf_tc_fail("pthread_join2 failed"); 99 res = pthread_join(th1, NULL); 100 if (res != 0) 101 atf_tc_fail("pthread_join1 failed"); 102 103 res = rump_sys_fchdir(fd); 104 if (res == -1) 105 atf_tc_fail("fchdir failed"); 106 107 /* 108 * Rarely the LFS test does not crash. atf currently has no way of 109 * saying "just chill even if the test doesn't fail", so this 110 * takes care of it. 111 */ 112 if (FSTYPE_LFS(tc)) 113 abort(); 114 } 115 116 ATF_FSAPPLY(race, "rmdir(2) race"); 117