xref: /freebsd/tools/test/stress2/misc/kevent16.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#!/bin/sh
2
3# Bug 258310 - kevent() does not see signal with zero timeout
4# Test scenario copied from the bug report.
5
6# Fixed by: 98168a6e6c12 - main - kqueue: drain kqueue taskqueue if syscall tickled it
7
8cat > /tmp/kevent16.c <<EOF
9#ifdef NDEBUG
10#undef NDEBUG
11#endif
12
13#define _GNU_SOURCE
14
15#include <sys/types.h>
16
17#include <sys/event.h>
18
19#include <assert.h>
20#include <errno.h>
21#include <signal.h>
22#include <stdlib.h>
23
24#include <poll.h>
25#include <unistd.h>
26
27int
28main(void)
29{
30	long l;
31	int rv;
32
33	sigset_t set;
34	rv = sigemptyset(&set);
35	assert(rv == 0);
36	rv = sigaddset(&set, SIGUSR1);
37	assert(rv == 0);
38
39	rv = sigprocmask(SIG_BLOCK, &set, NULL);
40	assert(rv == 0);
41
42	int skq = kqueue();
43	assert(skq >= 0);
44
45	struct kevent kev;
46	EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
47	rv = kevent(skq, &kev, 1, NULL, 0, NULL);
48	assert(rv == 0);
49
50	int kq = kqueue();
51	assert(kq >= 0);
52
53	EV_SET(&kev, skq, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);
54	rv = kevent(kq, &kev, 1, NULL, 0, NULL);
55	assert(rv == 0);
56
57	for (l = 0; l < 1000000; l++) {
58		rv = kill(getpid(), SIGUSR1);
59		assert(rv == 0);
60
61		/* Turn this into `#if 1` to avoid the race. */
62#if 0
63		rv = kevent(kq, NULL, 0, &kev, 1, NULL);
64#else
65		rv = kevent(kq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });
66#endif
67		assert(rv == 1);
68		rv = kevent(kq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });
69		assert(rv == 0);
70
71		rv = kevent(skq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });
72		assert(rv == 1);
73		rv = kevent(skq, NULL, 0, &kev, 1, &(struct timespec) { 0, 0 });
74		assert(rv == 0);
75
76		siginfo_t siginfo;
77
78		rv = sigtimedwait(&set, &siginfo, &(struct timespec) { 0, 0 });
79		assert(rv == SIGUSR1);
80
81		rv = sigtimedwait(&set, &siginfo, &(struct timespec) { 0, 0 });
82		assert(rv < 0);
83		assert(errno == EAGAIN);
84	}
85}
86EOF
87cc -o /tmp/kevent16 -Wall -Wextra -O2 /tmp/kevent16.c || exit 1
88
89/tmp/kevent16; s=$?
90
91rm -f /tmp/kevent16.c kevent16 kevent16.core
92exit $s
93