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