1314817Sngie/*	$NetBSD: t_filedesc.c,v 1.6 2017/01/13 21:30:41 christos Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30314817Sngie__RCSID("$NetBSD: t_filedesc.c,v 1.6 2017/01/13 21:30:41 christos Exp $");
31272343Sngie
32272343Sngie#include <sys/types.h>
33272343Sngie
34272343Sngie#include <assert.h>
35272343Sngie#include <atf-c.h>
36272343Sngie#include <fcntl.h>
37272343Sngie#include <stdlib.h>
38272343Sngie#include <pthread.h>
39272343Sngie
40272343Sngie#include <rump/rump.h>
41272343Sngie#include <rump/rump_syscalls.h>
42272343Sngie
43314817Sngie#include "h_macros.h"
44272343Sngie
45272343SngieATF_TC(getfilerace);
46272343SngieATF_TC_HEAD(getfilerace, tc)
47272343Sngie{
48272343Sngie
49272343Sngie	atf_tc_set_md_var(tc, "descr", "race between multithreaded proc. "
50272343Sngie	    "fd_getfile() and fd_close() (PR kern/43694)");
51272343Sngie}
52272343Sngie
53272343Sngiestatic int fd;
54272343Sngiestatic volatile bool quit;
55272343Sngie
56272343Sngiestatic void *
57272343Sngiewrkwrk(void *arg)
58272343Sngie{
59272343Sngie
60272343Sngie	/* just something to cause fd_getfile() to be called */
61272343Sngie	while (!quit)
62272343Sngie		rump_sys_write(fd, &fd, sizeof(fd));
63272343Sngie
64272343Sngie	return NULL;
65272343Sngie}
66272343Sngie
67272343Sngie/* for me, 1000 triggers extremely seldom, 10k sometimes, 100k almost always */
68272343Sngie#define DEFAULT_ITERATIONS 10000
69272343Sngie
70272343SngieATF_TC_BODY(getfilerace, tc)
71272343Sngie{
72272343Sngie	pthread_t pt;
73272343Sngie	int fd_wrk;
74272343Sngie	int i, iters;
75272343Sngie
76272343Sngie	/*
77272343Sngie	 * Want a multiprocessor virtual kernel.  A multiprocessor host
78272343Sngie	 * probably helps too, but that's harder to do in software...
79272343Sngie	 */
80272343Sngie	setenv("RUMP_NCPU", "2", 1);
81272343Sngie	rump_init();
82272343Sngie
83272343Sngie	fd = fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
84272343Sngie	if (fd == -1)
85272343Sngie		atf_tc_fail_errno("cannot open /dev/null");
86272343Sngie
87272343Sngie	if (atf_tc_has_config_var(tc, "iters"))
88272343Sngie		iters = atoi(atf_tc_get_config_var(tc, "iters"));
89272343Sngie	else
90272343Sngie		iters = DEFAULT_ITERATIONS;
91272343Sngie
92272343Sngie	pthread_create(&pt, NULL, wrkwrk, NULL);
93272343Sngie	for (i = 0; i < iters; i++) {
94272343Sngie		rump_sys_close(fd_wrk);
95272343Sngie		fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
96272343Sngie		assert(fd == fd_wrk);
97272343Sngie	}
98272343Sngie
99272343Sngie	quit = true;
100272343Sngie	pthread_join(pt, NULL);
101272343Sngie}
102272343Sngie
103272343SngieATF_TP_ADD_TCS(tp)
104272343Sngie{
105272343Sngie	ATF_TP_ADD_TC(tp, getfilerace);
106272343Sngie
107272343Sngie	return atf_no_error();
108272343Sngie}
109