xref: /freebsd/tests/sys/kqueue/kqueue_fork.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1*f5463265SMark Johnston /*-
2*f5463265SMark Johnston  * SPDX-License-Identifier: BSD-2-Clause
3*f5463265SMark Johnston  *
4*f5463265SMark Johnston  * Copyright (c) 2023 Andreas Bock <andreas.bock@virtual-arts-software.de>
5*f5463265SMark Johnston  * Copyright (c) 2023 Mark Johnston <markj@FreeBSD.org>
6*f5463265SMark Johnston  *
7*f5463265SMark Johnston  * Redistribution and use in source and binary forms, with or without
8*f5463265SMark Johnston  * modification, are permitted provided that the following conditions are
9*f5463265SMark Johnston  * met:
10*f5463265SMark Johnston  * 1. Redistributions of source code must retain the above copyright
11*f5463265SMark Johnston  *    notice, this list of conditions and the following disclaimer.
12*f5463265SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
13*f5463265SMark Johnston  *    notice, this list of conditions and the following disclaimer in
14*f5463265SMark Johnston  *    the documentation and/or other materials provided with the distribution.
15*f5463265SMark Johnston  *
16*f5463265SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*f5463265SMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*f5463265SMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*f5463265SMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*f5463265SMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*f5463265SMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*f5463265SMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*f5463265SMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*f5463265SMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*f5463265SMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*f5463265SMark Johnston  * SUCH DAMAGE.
27*f5463265SMark Johnston  */
28*f5463265SMark Johnston 
29*f5463265SMark Johnston #include <sys/event.h>
30*f5463265SMark Johnston #include <sys/wait.h>
31*f5463265SMark Johnston 
32*f5463265SMark Johnston #include <err.h>
33*f5463265SMark Johnston #include <signal.h>
34*f5463265SMark Johnston #include <unistd.h>
35*f5463265SMark Johnston 
36*f5463265SMark Johnston #include <atf-c.h>
37*f5463265SMark Johnston 
38*f5463265SMark Johnston /*
39*f5463265SMark Johnston  * A regression test for bugzilla 275286.
40*f5463265SMark Johnston  */
41*f5463265SMark Johnston ATF_TC_WITHOUT_HEAD(shared_table_filt_sig);
ATF_TC_BODY(shared_table_filt_sig,tc)42*f5463265SMark Johnston ATF_TC_BODY(shared_table_filt_sig, tc)
43*f5463265SMark Johnston {
44*f5463265SMark Johnston 	struct sigaction sa;
45*f5463265SMark Johnston 	pid_t pid;
46*f5463265SMark Johnston 	int error, status;
47*f5463265SMark Johnston 
48*f5463265SMark Johnston 	sa.sa_handler = SIG_IGN;
49*f5463265SMark Johnston 	sigemptyset(&sa.sa_mask);
50*f5463265SMark Johnston 	sa.sa_flags = 0;
51*f5463265SMark Johnston 	error = sigaction(SIGINT, &sa, NULL);
52*f5463265SMark Johnston 	ATF_REQUIRE(error == 0);
53*f5463265SMark Johnston 
54*f5463265SMark Johnston 	pid = rfork(RFPROC);
55*f5463265SMark Johnston 	ATF_REQUIRE(pid != -1);
56*f5463265SMark Johnston 	if (pid == 0) {
57*f5463265SMark Johnston 		struct kevent ev;
58*f5463265SMark Johnston 		int kq;
59*f5463265SMark Johnston 
60*f5463265SMark Johnston 		kq = kqueue();
61*f5463265SMark Johnston 		if (kq < 0)
62*f5463265SMark Johnston 			err(1, "kqueue");
63*f5463265SMark Johnston 		EV_SET(&ev, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0,
64*f5463265SMark Johnston 		    NULL);
65*f5463265SMark Johnston 		if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0)
66*f5463265SMark Johnston 			err(2, "kevent");
67*f5463265SMark Johnston 		if (kevent(kq, NULL, 0, &ev, 1, NULL) < 0)
68*f5463265SMark Johnston 			err(3, "kevent");
69*f5463265SMark Johnston 		_exit(0);
70*f5463265SMark Johnston 	}
71*f5463265SMark Johnston 
72*f5463265SMark Johnston 	/* Wait for the child to block in kevent(). */
73*f5463265SMark Johnston 	usleep(100000);
74*f5463265SMark Johnston 
75*f5463265SMark Johnston 	error = kill(pid, SIGINT);
76*f5463265SMark Johnston 	ATF_REQUIRE(error == 0);
77*f5463265SMark Johnston 
78*f5463265SMark Johnston 	error = waitpid(pid, &status, 0);
79*f5463265SMark Johnston 	ATF_REQUIRE(error != -1);
80*f5463265SMark Johnston 	ATF_REQUIRE(WIFEXITED(status));
81*f5463265SMark Johnston 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
82*f5463265SMark Johnston }
83*f5463265SMark Johnston 
ATF_TP_ADD_TCS(tp)84*f5463265SMark Johnston ATF_TP_ADD_TCS(tp)
85*f5463265SMark Johnston {
86*f5463265SMark Johnston 	ATF_TP_ADD_TC(tp, shared_table_filt_sig);
87*f5463265SMark Johnston 
88*f5463265SMark Johnston 	return (atf_no_error());
89*f5463265SMark Johnston }
90