t_renamerace.c revision 1.11
1/*	$NetBSD: t_renamerace.c,v 1.11 2011/01/04 16:25:20 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
28ATF_TC(renamerace2);
29ATF_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
35static volatile int quittingtime = 0;
36static pid_t wrkpid;
37
38static void *
39r2w1(void *arg)
40{
41	int fd;
42
43	rump_pub_lwproc_newlwp(wrkpid);
44
45	fd = rump_sys_open("/file", O_CREAT | O_RDWR, 0777);
46	if (fd == -1)
47		atf_tc_fail_errno("creat");
48	rump_sys_close(fd);
49
50	while (!quittingtime) {
51		if (rump_sys_rename("/file", "/dir/file") == -1)
52			atf_tc_fail_errno("rename 1");
53		if (rump_sys_rename("/dir/file", "/file") == -1)
54			atf_tc_fail_errno("rename 2");
55	}
56
57	return NULL;
58}
59
60static void *
61r2w2(void *arg)
62{
63	int fd;
64
65	rump_pub_lwproc_newlwp(wrkpid);
66
67	while (!quittingtime) {
68		fd = rump_sys_open("/dir/file1", O_RDWR);
69		if (fd != -1)
70			rump_sys_close(fd);
71	}
72
73	return NULL;
74}
75
76ATF_TC_BODY(renamerace2, tc)
77{
78	struct tmpfs_args args;
79	struct utsname un;
80	pthread_t pt[2];
81
82	/*
83	 * Force SMP regardless of how many host CPUs there are.
84	 * Deadlock is highly unlikely to trigger otherwise.
85	 */
86	setenv("RUMP_NCPU", "2", 1);
87
88	rump_init();
89	memset(&args, 0, sizeof(args));
90	args.ta_version = TMPFS_ARGS_VERSION;
91	args.ta_root_mode = 0777;
92	if (rump_sys_mount(MOUNT_TMPFS, "/", 0, &args, sizeof(args)) == -1)
93		atf_tc_fail_errno("could not mount tmpfs");
94
95	if (rump_sys_mkdir("/dir", 0777) == -1)
96		atf_tc_fail_errno("cannot create directory");
97
98	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
99	RL(wrkpid = rump_sys_getpid());
100	pthread_create(&pt[0], NULL, r2w1, NULL);
101	pthread_create(&pt[1], NULL, r2w2, NULL);
102
103	/* usually triggers in <<1s for me */
104	sleep(4);
105	quittingtime = 1;
106
107	atf_tc_expect_timeout("PR kern/36681");
108
109	pthread_join(pt[0], NULL);
110	pthread_join(pt[1], NULL);
111}
112
113ATF_TP_ADD_TCS(tp)
114{
115	ATF_TP_ADD_TC(tp, renamerace2);
116
117	return atf_no_error();
118}
119