Home | History | Annotate | Line # | Download | only in vfs
t_renamerace.c revision 1.7
      1 /*	$NetBSD: t_renamerace.c,v 1.7 2010/07/16 14:14:27 pooka Exp $	*/
      2 
      3 /*
      4  * Modified for rump and atf from a program supplied
      5  * by Nicolas Joly in kern/40948
      6  */
      7 
      8 #include <sys/types.h>
      9 #include <sys/mount.h>
     10 #include <sys/utsname.h>
     11 
     12 #include <atf-c.h>
     13 #include <errno.h>
     14 #include <fcntl.h>
     15 #include <pthread.h>
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <unistd.h>
     20 
     21 #include <rump/rump.h>
     22 #include <rump/rump_syscalls.h>
     23 
     24 #include "../common/h_fsmacros.h"
     25 #include "../../h_macros.h"
     26 
     27 static volatile int quittingtime;
     28 
     29 static void *
     30 w1(void *arg)
     31 {
     32 	int fd;
     33 
     34 	rump_pub_lwp_alloc_and_switch(0, 10);
     35 	rump_sys_chdir(arg);
     36 
     37 	while (!quittingtime) {
     38 		fd = rump_sys_open("rename.test1",
     39 		    O_WRONLY|O_CREAT|O_TRUNC, 0666);
     40 		if (fd == -1)
     41 			atf_tc_fail_errno("create");
     42 		rump_sys_unlink("rename.test1");
     43 		rump_sys_close(fd);
     44 	}
     45 
     46 	rump_sys_chdir("/");
     47 
     48 	return NULL;
     49 }
     50 
     51 static void *
     52 w1_dirs(void *arg)
     53 {
     54 
     55 	rump_pub_lwp_alloc_and_switch(0, 10);
     56 	rump_sys_chdir(arg);
     57 
     58 	while (!quittingtime) {
     59 		if (rump_sys_mkdir("rename.test1", 0777) == -1)
     60 			atf_tc_fail_errno("mkdir");
     61 		rump_sys_rmdir("rename.test1");
     62 	}
     63 
     64 	rump_sys_chdir("/");
     65 
     66 	return NULL;
     67 }
     68 
     69 static void *
     70 w2(void *arg)
     71 {
     72 
     73 	rump_pub_lwp_alloc_and_switch(0, 11);
     74 	rump_sys_chdir(arg);
     75 
     76 	while (!quittingtime) {
     77 		rump_sys_rename("rename.test1", "rename.test2");
     78 	}
     79 
     80 	rump_sys_chdir("/");
     81 
     82 	return NULL;
     83 }
     84 
     85 static void
     86 renamerace(const atf_tc_t *tc, const char *mp)
     87 {
     88 	pthread_t pt1, pt2;
     89 
     90 	if (FSTYPE_LFS(tc))
     91 		atf_tc_expect_signal(-1, "PR kern/43582");
     92 
     93 	pthread_create(&pt1, NULL, w1, __UNCONST(mp));
     94 	pthread_create(&pt2, NULL, w2, __UNCONST(mp));
     95 
     96 	sleep(5);
     97 	quittingtime = 1;
     98 
     99 	pthread_join(pt1, NULL);
    100 	pthread_join(pt2, NULL);
    101 
    102 	/*
    103 	 * XXX: does not always fail on LFS, especially for unicpu
    104 	 * configurations.  see other ramblings about racy tests.
    105 	 */
    106 	if (FSTYPE_LFS(tc))
    107 		abort();
    108 }
    109 
    110 static void
    111 renamerace_dirs(const atf_tc_t *tc, const char *mp)
    112 {
    113 	pthread_t pt1, pt2;
    114 
    115 	if (FSTYPE_SYSVBFS(tc))
    116 		atf_tc_skip("directories not supported");
    117 
    118 	/* XXX: msdosfs also sometimes hangs */
    119 	if (FSTYPE_FFS(tc) || FSTYPE_EXT2FS(tc) || FSTYPE_LFS(tc) ||
    120 	    FSTYPE_MSDOS(tc))
    121 		atf_tc_expect_signal(-1, "PR kern/43626");
    122 
    123 	pthread_create(&pt1, NULL, w1_dirs, __UNCONST(mp));
    124 	pthread_create(&pt2, NULL, w2, __UNCONST(mp));
    125 
    126 	sleep(5);
    127 	quittingtime = 1;
    128 
    129 	pthread_join(pt1, NULL);
    130 	pthread_join(pt2, NULL);
    131 
    132 	/*
    133 	 * Doesn't always trigger when run on a slow backend
    134 	 * (i.e. not on tmpfs/mfs).  So do the usual kludge.
    135 	 */
    136 	if (FSTYPE_FFS(tc) || FSTYPE_EXT2FS(tc) || FSTYPE_LFS(tc) ||
    137 	    FSTYPE_MSDOS(tc))
    138 		abort();
    139 }
    140 
    141 ATF_TC_FSAPPLY(renamerace, "rename(2) race with file unlinked mid-operation");
    142 ATF_TC_FSAPPLY(renamerace_dirs, "rename(2) race with directories");
    143 
    144 ATF_TP_ADD_TCS(tp)
    145 {
    146 
    147 	ATF_TP_FSAPPLY(renamerace); /* PR kern/41128 */
    148 	ATF_TP_FSAPPLY(renamerace_dirs);
    149 
    150 	return atf_no_error();
    151 }
    152