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