1*11be35a1SLionel Sambuc /* $NetBSD: t_renamerace.c,v 1.13 2011/08/18 21:44:55 riastradh Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc * Modified for rump and atf from a program supplied
5*11be35a1SLionel Sambuc * by Nicolas Joly in kern/40948
6*11be35a1SLionel Sambuc */
7*11be35a1SLionel Sambuc
8*11be35a1SLionel Sambuc #include <sys/types.h>
9*11be35a1SLionel Sambuc #include <sys/mount.h>
10*11be35a1SLionel Sambuc #include <sys/utsname.h>
11*11be35a1SLionel Sambuc
12*11be35a1SLionel Sambuc #include <atf-c.h>
13*11be35a1SLionel Sambuc #include <errno.h>
14*11be35a1SLionel Sambuc #include <fcntl.h>
15*11be35a1SLionel Sambuc #include <pthread.h>
16*11be35a1SLionel Sambuc #include <stdio.h>
17*11be35a1SLionel Sambuc #include <stdlib.h>
18*11be35a1SLionel Sambuc #include <string.h>
19*11be35a1SLionel Sambuc #include <unistd.h>
20*11be35a1SLionel Sambuc
21*11be35a1SLionel Sambuc #include <rump/rump.h>
22*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
23*11be35a1SLionel Sambuc
24*11be35a1SLionel Sambuc #include <fs/tmpfs/tmpfs_args.h>
25*11be35a1SLionel Sambuc
26*11be35a1SLionel Sambuc #include "../../h_macros.h"
27*11be35a1SLionel Sambuc
28*11be35a1SLionel Sambuc ATF_TC(renamerace2);
ATF_TC_HEAD(renamerace2,tc)29*11be35a1SLionel Sambuc ATF_TC_HEAD(renamerace2, tc)
30*11be35a1SLionel Sambuc {
31*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "rename(2) lock order inversion");
32*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "6");
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc static volatile int quittingtime = 0;
36*11be35a1SLionel Sambuc static pid_t wrkpid;
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc static void *
r2w1(void * arg)39*11be35a1SLionel Sambuc r2w1(void *arg)
40*11be35a1SLionel Sambuc {
41*11be35a1SLionel Sambuc int fd;
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc rump_pub_lwproc_newlwp(wrkpid);
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc fd = rump_sys_open("/file", O_CREAT | O_RDWR, 0777);
46*11be35a1SLionel Sambuc if (fd == -1)
47*11be35a1SLionel Sambuc atf_tc_fail_errno("creat");
48*11be35a1SLionel Sambuc rump_sys_close(fd);
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc while (!quittingtime) {
51*11be35a1SLionel Sambuc if (rump_sys_rename("/file", "/dir/file") == -1)
52*11be35a1SLionel Sambuc atf_tc_fail_errno("rename 1");
53*11be35a1SLionel Sambuc if (rump_sys_rename("/dir/file", "/file") == -1)
54*11be35a1SLionel Sambuc atf_tc_fail_errno("rename 2");
55*11be35a1SLionel Sambuc }
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc return NULL;
58*11be35a1SLionel Sambuc }
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc static void *
r2w2(void * arg)61*11be35a1SLionel Sambuc r2w2(void *arg)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc int fd;
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc rump_pub_lwproc_newlwp(wrkpid);
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc while (!quittingtime) {
68*11be35a1SLionel Sambuc fd = rump_sys_open("/dir/file1", O_RDWR);
69*11be35a1SLionel Sambuc if (fd != -1)
70*11be35a1SLionel Sambuc rump_sys_close(fd);
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc return NULL;
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc
ATF_TC_BODY(renamerace2,tc)76*11be35a1SLionel Sambuc ATF_TC_BODY(renamerace2, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc struct tmpfs_args args;
79*11be35a1SLionel Sambuc pthread_t pt[2];
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc /*
82*11be35a1SLionel Sambuc * Force SMP regardless of how many host CPUs there are.
83*11be35a1SLionel Sambuc * Deadlock is highly unlikely to trigger otherwise.
84*11be35a1SLionel Sambuc */
85*11be35a1SLionel Sambuc setenv("RUMP_NCPU", "2", 1);
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc rump_init();
88*11be35a1SLionel Sambuc memset(&args, 0, sizeof(args));
89*11be35a1SLionel Sambuc args.ta_version = TMPFS_ARGS_VERSION;
90*11be35a1SLionel Sambuc args.ta_root_mode = 0777;
91*11be35a1SLionel Sambuc if (rump_sys_mount(MOUNT_TMPFS, "/", 0, &args, sizeof(args)) == -1)
92*11be35a1SLionel Sambuc atf_tc_fail_errno("could not mount tmpfs");
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc if (rump_sys_mkdir("/dir", 0777) == -1)
95*11be35a1SLionel Sambuc atf_tc_fail_errno("cannot create directory");
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
98*11be35a1SLionel Sambuc RL(wrkpid = rump_sys_getpid());
99*11be35a1SLionel Sambuc pthread_create(&pt[0], NULL, r2w1, NULL);
100*11be35a1SLionel Sambuc pthread_create(&pt[1], NULL, r2w2, NULL);
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc /* usually triggers in <<1s for me */
103*11be35a1SLionel Sambuc sleep(4);
104*11be35a1SLionel Sambuc quittingtime = 1;
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc pthread_join(pt[0], NULL);
107*11be35a1SLionel Sambuc pthread_join(pt[1], NULL);
108*11be35a1SLionel Sambuc }
109*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)110*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
111*11be35a1SLionel Sambuc {
112*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, renamerace2);
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc return atf_no_error();
115*11be35a1SLionel Sambuc }
116