1/* $NetBSD: t_scan.c,v 1.2 2021/10/10 19:17:31 wiz Exp $ */
2
3/*-
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_scan.c,v 1.2 2021/10/10 19:17:31 wiz Exp $");
31
32#include <sys/event.h>
33#include <sys/time.h>
34#include <sys/types.h>
35
36#include <err.h>
37#include <pthread.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41
42#include <atf-c.h>
43
44/*
45 * Each kevent thread will make this many kevent() calls, and if it
46 * achieves this mark, we assume the race condition has not occurred
47 * the declare the test passes.
48 */
49#define	NKEVENT_CALLS		10000
50
51static int kq;
52
53static void *
54kevent_thread(void *arg)
55{
56	struct timespec ts = { 0, 0 };
57	struct kevent event;
58	int rv, i;
59
60	for (i = 0; i < NKEVENT_CALLS; i++) {
61		rv = kevent(kq, NULL, 0, &event, 1, &ts);
62		if (rv < 0) {
63			i = rv;
64			break;
65		}
66		if (rv != 1) {
67			break;
68		}
69	}
70
71	return (void *)(intptr_t)i;
72}
73
74ATF_TC(scan1);
75ATF_TC_HEAD(scan1, tc)
76{
77	atf_tc_set_md_var(tc, "descr",
78	    "Exercises multi-threaded kevent() calls (PR kern/50094)");
79}
80
81ATF_TC_BODY(scan1, tc)
82{
83	pthread_t t1, t2;
84	void *v1, *v2;
85	intptr_t r1, r2;
86	struct kevent event;
87	int p[2];
88
89	ATF_REQUIRE(pipe(p) == 0);
90	ATF_REQUIRE((kq = kqueue()) >= 0);
91
92	EV_SET(&event, p[0], EVFILT_READ, EV_ADD, 0, 0, 0);
93	ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);
94	ATF_REQUIRE(write(p[1], p, 1) == 1);
95
96	ATF_REQUIRE(pthread_create(&t1, NULL, kevent_thread, NULL) == 0);
97	ATF_REQUIRE(pthread_create(&t2, NULL, kevent_thread, NULL) == 0);
98
99	ATF_REQUIRE(pthread_join(t1, &v1) == 0);
100	r1 = (intptr_t)v1;
101
102	ATF_REQUIRE(pthread_join(t2, &v2) == 0);
103	r2 = (intptr_t)v2;
104
105	ATF_REQUIRE(r1 >= 0);
106	ATF_REQUIRE(r2 >= 0);
107
108	ATF_REQUIRE(r1 == NKEVENT_CALLS);
109	ATF_REQUIRE(r2 == NKEVENT_CALLS);
110}
111
112ATF_TP_ADD_TCS(tp)
113{
114	ATF_TP_ADD_TC(tp, scan1);
115
116	return atf_no_error();
117}
118