1*11be35a1SLionel Sambuc /* $NetBSD: t_filedesc.c,v 1.5 2012/03/18 09:46:50 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <sys/cdefs.h>
30*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_filedesc.c,v 1.5 2012/03/18 09:46:50 jruoho Exp $");
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc #include <sys/types.h>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <assert.h>
35*11be35a1SLionel Sambuc #include <atf-c.h>
36*11be35a1SLionel Sambuc #include <fcntl.h>
37*11be35a1SLionel Sambuc #include <stdlib.h>
38*11be35a1SLionel Sambuc #include <pthread.h>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #include <rump/rump.h>
41*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc #include "../h_macros.h"
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc ATF_TC(getfilerace);
ATF_TC_HEAD(getfilerace,tc)46*11be35a1SLionel Sambuc ATF_TC_HEAD(getfilerace, tc)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "race between multithreaded proc. "
50*11be35a1SLionel Sambuc "fd_getfile() and fd_close() (PR kern/43694)");
51*11be35a1SLionel Sambuc }
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc static int fd;
54*11be35a1SLionel Sambuc static volatile bool quit;
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc static void *
wrkwrk(void * arg)57*11be35a1SLionel Sambuc wrkwrk(void *arg)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc /* just something to cause fd_getfile() to be called */
61*11be35a1SLionel Sambuc while (!quit)
62*11be35a1SLionel Sambuc rump_sys_write(fd, &fd, sizeof(fd));
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc return NULL;
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc /* for me, 1000 triggers extremely seldom, 10k sometimes, 100k almost always */
68*11be35a1SLionel Sambuc #define DEFAULT_ITERATIONS 10000
69*11be35a1SLionel Sambuc
ATF_TC_BODY(getfilerace,tc)70*11be35a1SLionel Sambuc ATF_TC_BODY(getfilerace, tc)
71*11be35a1SLionel Sambuc {
72*11be35a1SLionel Sambuc pthread_t pt;
73*11be35a1SLionel Sambuc int fd_wrk;
74*11be35a1SLionel Sambuc int i, iters;
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc /*
77*11be35a1SLionel Sambuc * Want a multiprocessor virtual kernel. A multiprocessor host
78*11be35a1SLionel Sambuc * probably helps too, but that's harder to do in software...
79*11be35a1SLionel Sambuc */
80*11be35a1SLionel Sambuc setenv("RUMP_NCPU", "2", 1);
81*11be35a1SLionel Sambuc rump_init();
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc fd = fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
84*11be35a1SLionel Sambuc if (fd == -1)
85*11be35a1SLionel Sambuc atf_tc_fail_errno("cannot open /dev/null");
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc if (atf_tc_has_config_var(tc, "iters"))
88*11be35a1SLionel Sambuc iters = atoi(atf_tc_get_config_var(tc, "iters"));
89*11be35a1SLionel Sambuc else
90*11be35a1SLionel Sambuc iters = DEFAULT_ITERATIONS;
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc pthread_create(&pt, NULL, wrkwrk, NULL);
93*11be35a1SLionel Sambuc for (i = 0; i < iters; i++) {
94*11be35a1SLionel Sambuc rump_sys_close(fd_wrk);
95*11be35a1SLionel Sambuc fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
96*11be35a1SLionel Sambuc assert(fd == fd_wrk);
97*11be35a1SLionel Sambuc }
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc quit = true;
100*11be35a1SLionel Sambuc pthread_join(pt, NULL);
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)103*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, getfilerace);
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc return atf_no_error();
108*11be35a1SLionel Sambuc }
109