xref: /freebsd/tests/sys/kern/waitpid_nohang.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1*d30e66e5SJilles Tjoelker /*-
2*d30e66e5SJilles Tjoelker  * Copyright (c) 2016 Jilles Tjoelker
3*d30e66e5SJilles Tjoelker  * All rights reserved.
4*d30e66e5SJilles Tjoelker  *
5*d30e66e5SJilles Tjoelker  * Redistribution and use in source and binary forms, with or without
6*d30e66e5SJilles Tjoelker  * modification, are permitted provided that the following conditions
7*d30e66e5SJilles Tjoelker  * are met:
8*d30e66e5SJilles Tjoelker  * 1. Redistributions of source code must retain the above copyright
9*d30e66e5SJilles Tjoelker  *    notice, this list of conditions and the following disclaimer.
10*d30e66e5SJilles Tjoelker  * 2. Redistributions in binary form must reproduce the above copyright
11*d30e66e5SJilles Tjoelker  *    notice, this list of conditions and the following disclaimer in the
12*d30e66e5SJilles Tjoelker  *    documentation and/or other materials provided with the distribution.
13*d30e66e5SJilles Tjoelker  *
14*d30e66e5SJilles Tjoelker  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*d30e66e5SJilles Tjoelker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*d30e66e5SJilles Tjoelker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*d30e66e5SJilles Tjoelker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*d30e66e5SJilles Tjoelker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*d30e66e5SJilles Tjoelker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*d30e66e5SJilles Tjoelker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*d30e66e5SJilles Tjoelker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*d30e66e5SJilles Tjoelker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*d30e66e5SJilles Tjoelker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*d30e66e5SJilles Tjoelker  * SUCH DAMAGE.
25*d30e66e5SJilles Tjoelker  */
26*d30e66e5SJilles Tjoelker 
27*d30e66e5SJilles Tjoelker #include <sys/cdefs.h>
28*d30e66e5SJilles Tjoelker #include <sys/wait.h>
29*d30e66e5SJilles Tjoelker 
30*d30e66e5SJilles Tjoelker #include <atf-c.h>
31*d30e66e5SJilles Tjoelker #include <signal.h>
32*d30e66e5SJilles Tjoelker #include <unistd.h>
33*d30e66e5SJilles Tjoelker 
34*d30e66e5SJilles Tjoelker ATF_TC_WITHOUT_HEAD(waitpid_nohang);
ATF_TC_BODY(waitpid_nohang,tc)35*d30e66e5SJilles Tjoelker ATF_TC_BODY(waitpid_nohang, tc)
36*d30e66e5SJilles Tjoelker {
37*d30e66e5SJilles Tjoelker 	pid_t child, pid;
38*d30e66e5SJilles Tjoelker 	int status, r;
39*d30e66e5SJilles Tjoelker 
40*d30e66e5SJilles Tjoelker 	child = fork();
41*d30e66e5SJilles Tjoelker 	ATF_REQUIRE(child != -1);
42*d30e66e5SJilles Tjoelker 	if (child == 0) {
43*d30e66e5SJilles Tjoelker 		sleep(10);
44*d30e66e5SJilles Tjoelker 		_exit(1);
45*d30e66e5SJilles Tjoelker 	}
46*d30e66e5SJilles Tjoelker 
47*d30e66e5SJilles Tjoelker 	status = 42;
48*d30e66e5SJilles Tjoelker 	pid = waitpid(child, &status, WNOHANG);
49*d30e66e5SJilles Tjoelker 	ATF_REQUIRE(pid == 0);
50*d30e66e5SJilles Tjoelker 	ATF_CHECK(status == 42);
51*d30e66e5SJilles Tjoelker 
52*d30e66e5SJilles Tjoelker 	r = kill(child, SIGTERM);
53*d30e66e5SJilles Tjoelker 	ATF_REQUIRE(r == 0);
54*d30e66e5SJilles Tjoelker 	r = waitid(P_PID, child, NULL, WEXITED | WNOWAIT);
55*d30e66e5SJilles Tjoelker 	ATF_REQUIRE(r == 0);
56*d30e66e5SJilles Tjoelker 
57*d30e66e5SJilles Tjoelker 	status = -1;
58*d30e66e5SJilles Tjoelker 	pid = waitpid(child, &status, WNOHANG);
59*d30e66e5SJilles Tjoelker 	ATF_REQUIRE(pid == child);
60*d30e66e5SJilles Tjoelker 	ATF_CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
61*d30e66e5SJilles Tjoelker }
62*d30e66e5SJilles Tjoelker 
ATF_TP_ADD_TCS(tp)63*d30e66e5SJilles Tjoelker ATF_TP_ADD_TCS(tp)
64*d30e66e5SJilles Tjoelker {
65*d30e66e5SJilles Tjoelker 
66*d30e66e5SJilles Tjoelker 	ATF_TP_ADD_TC(tp, waitpid_nohang);
67*d30e66e5SJilles Tjoelker 	return (atf_no_error());
68*d30e66e5SJilles Tjoelker }
69