xref: /netbsd-src/tests/fs/tmpfs/t_renamerace.c (revision c505c4429840c353a86d4eb53b5e2bfc0092264e)
1 /*	$NetBSD: t_renamerace.c,v 1.8 2010/07/14 21:39:31 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 <fs/tmpfs/tmpfs_args.h>
25 
26 #include "../../h_macros.h"
27 
28 ATF_TC(renamerace2);
29 ATF_TC_HEAD(renamerace2, tc)
30 {
31 	atf_tc_set_md_var(tc, "descr", "rename(2) lock order inversion");
32 	atf_tc_set_md_var(tc, "timeout", "6");
33 }
34 
35 static volatile int quittingtime = 0;
36 
37 static void *
38 r2w1(void *arg)
39 {
40 	int fd;
41 
42 	rump_pub_lwp_alloc_and_switch(0, 0);
43 
44 	fd = rump_sys_open("/file", O_CREAT | O_RDWR, 0777);
45 	if (fd == -1)
46 		atf_tc_fail_errno("creat");
47 	rump_sys_close(fd);
48 
49 	while (!quittingtime) {
50 		if (rump_sys_rename("/file", "/dir/file") == -1)
51 			atf_tc_fail_errno("rename 1");
52 		if (rump_sys_rename("/dir/file", "/file") == -1)
53 			atf_tc_fail_errno("rename 2");
54 	}
55 
56 	return NULL;
57 }
58 
59 static void *
60 r2w2(void *arg)
61 {
62 	int fd;
63 
64 	rump_pub_lwp_alloc_and_switch(0, 0);
65 
66 	while (!quittingtime) {
67 		fd = rump_sys_open("/dir/file1", O_RDWR);
68 		if (fd != -1)
69 			rump_sys_close(fd);
70 	}
71 
72 	return NULL;
73 }
74 
75 ATF_TC_BODY(renamerace2, tc)
76 {
77 	struct tmpfs_args args;
78 	struct utsname un;
79 	pthread_t pt[2];
80 
81 	/*
82 	 * Check that we are running on an SMP-capable arch.  It should
83 	 * be a rump capability, but after the CPU_INFO_FOREACH is
84 	 * fixed, it will be every arch (for rump), so don't bother.
85 	 */
86 	if (uname(&un) == -1)
87 		atf_tc_fail_errno("uname");
88 	if (strcmp(un.machine, "i386") != 0 && strcmp(un.machine, "amd64") != 0)
89 		atf_tc_skip("i386 or amd64 required (have %s)", un.machine);
90 
91 	/*
92 	 * Force SMP regardless of how many host CPUs there are.
93 	 * Deadlock is highly unlikely to trigger otherwise.
94 	 */
95 	setenv("RUMP_NCPU", "2", 1);
96 
97 	rump_init();
98 	memset(&args, 0, sizeof(args));
99 	args.ta_version = TMPFS_ARGS_VERSION;
100 	args.ta_root_mode = 0777;
101 	if (rump_sys_mount(MOUNT_TMPFS, "/", 0, &args, sizeof(args)) == -1)
102 		atf_tc_fail_errno("could not mount tmpfs");
103 
104 	if (rump_sys_mkdir("/dir", 0777) == -1)
105 		atf_tc_fail_errno("cannot create directory");
106 
107 	pthread_create(&pt[0], NULL, r2w1, NULL);
108 	pthread_create(&pt[1], NULL, r2w2, NULL);
109 
110 	/* usually triggers in <<1s for me */
111 	sleep(4);
112 	quittingtime = 1;
113 
114 	atf_tc_expect_timeout("PR kern/36681");
115 
116 	pthread_join(pt[0], NULL);
117 	pthread_join(pt[1], NULL);
118 }
119 
120 ATF_TP_ADD_TCS(tp)
121 {
122 	ATF_TP_ADD_TC(tp, renamerace2);
123 
124 	return atf_no_error();
125 }
126