xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_sigtimedwait.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_sigtimedwait.c,v 1.2 2013/03/08 23:18:00 martin Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper  * are met:
10*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper  *
16*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
27*57718be8SEnji Cooper  */
28*57718be8SEnji Cooper 
29*57718be8SEnji Cooper #include <sys/cdefs.h>
30*57718be8SEnji Cooper __RCSID("$NetBSD: t_sigtimedwait.c,v 1.2 2013/03/08 23:18:00 martin Exp $");
31*57718be8SEnji Cooper 
32*57718be8SEnji Cooper #include <sys/time.h>
33*57718be8SEnji Cooper #include <errno.h>
34*57718be8SEnji Cooper #include <signal.h>
35*57718be8SEnji Cooper #include <stdio.h>
36*57718be8SEnji Cooper #include <string.h>
37*57718be8SEnji Cooper #include <atf-c.h>
38*57718be8SEnji Cooper 
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper ATF_TC(sigtimedwait_all0timeout);
41*57718be8SEnji Cooper 
ATF_TC_HEAD(sigtimedwait_all0timeout,tc)42*57718be8SEnji Cooper ATF_TC_HEAD(sigtimedwait_all0timeout, tc)
43*57718be8SEnji Cooper {
44*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "timeout", "10");
45*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test for PR kern/47625: sigtimedwait"
46*57718be8SEnji Cooper 	    " with a timeout value of all zero should return imediately");
47*57718be8SEnji Cooper }
48*57718be8SEnji Cooper 
ATF_TC_BODY(sigtimedwait_all0timeout,tc)49*57718be8SEnji Cooper ATF_TC_BODY(sigtimedwait_all0timeout, tc)
50*57718be8SEnji Cooper {
51*57718be8SEnji Cooper 	sigset_t block;
52*57718be8SEnji Cooper 	struct timespec ts, before, after, len;
53*57718be8SEnji Cooper 	siginfo_t info;
54*57718be8SEnji Cooper 	int r;
55*57718be8SEnji Cooper 
56*57718be8SEnji Cooper 	sigemptyset(&block);
57*57718be8SEnji Cooper 	ts.tv_sec = 0;
58*57718be8SEnji Cooper 	ts.tv_nsec = 0;
59*57718be8SEnji Cooper 	clock_gettime(CLOCK_MONOTONIC, &before);
60*57718be8SEnji Cooper 	r = sigtimedwait(&block, &info, &ts);
61*57718be8SEnji Cooper 	clock_gettime(CLOCK_MONOTONIC, &after);
62*57718be8SEnji Cooper 	ATF_REQUIRE(r == -1);
63*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EAGAIN, errno);
64*57718be8SEnji Cooper 	timespecsub(&after, &before, &len);
65*57718be8SEnji Cooper 	ATF_REQUIRE(len.tv_sec < 1);
66*57718be8SEnji Cooper }
67*57718be8SEnji Cooper 
68*57718be8SEnji Cooper ATF_TC(sigtimedwait_NULL_timeout);
69*57718be8SEnji Cooper 
ATF_TC_HEAD(sigtimedwait_NULL_timeout,tc)70*57718be8SEnji Cooper ATF_TC_HEAD(sigtimedwait_NULL_timeout, tc)
71*57718be8SEnji Cooper {
72*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "timeout", "10");
73*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test sigtimedwait() without timeout");
74*57718be8SEnji Cooper }
75*57718be8SEnji Cooper 
ATF_TC_BODY(sigtimedwait_NULL_timeout,tc)76*57718be8SEnji Cooper ATF_TC_BODY(sigtimedwait_NULL_timeout, tc)
77*57718be8SEnji Cooper {
78*57718be8SEnji Cooper 	sigset_t sig;
79*57718be8SEnji Cooper 	siginfo_t info;
80*57718be8SEnji Cooper 	struct itimerval it;
81*57718be8SEnji Cooper 	int r;
82*57718be8SEnji Cooper 
83*57718be8SEnji Cooper 	/* arrange for a SIGALRM signal in a few seconds */
84*57718be8SEnji Cooper 	memset(&it, 0, sizeof it);
85*57718be8SEnji Cooper 	it.it_value.tv_sec = 5;
86*57718be8SEnji Cooper 	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);
87*57718be8SEnji Cooper 
88*57718be8SEnji Cooper 	/* wait without timeout */
89*57718be8SEnji Cooper 	sigemptyset(&sig);
90*57718be8SEnji Cooper 	sigaddset(&sig, SIGALRM);
91*57718be8SEnji Cooper 	r = sigtimedwait(&sig, &info, NULL);
92*57718be8SEnji Cooper 	ATF_REQUIRE(r == SIGALRM);
93*57718be8SEnji Cooper }
94*57718be8SEnji Cooper 
95*57718be8SEnji Cooper ATF_TC(sigtimedwait_small_timeout);
96*57718be8SEnji Cooper 
ATF_TC_HEAD(sigtimedwait_small_timeout,tc)97*57718be8SEnji Cooper ATF_TC_HEAD(sigtimedwait_small_timeout, tc)
98*57718be8SEnji Cooper {
99*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "timeout", "15");
100*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test sigtimedwait with a small "
101*57718be8SEnji Cooper 	    "timeout");
102*57718be8SEnji Cooper }
103*57718be8SEnji Cooper 
ATF_TC_BODY(sigtimedwait_small_timeout,tc)104*57718be8SEnji Cooper ATF_TC_BODY(sigtimedwait_small_timeout, tc)
105*57718be8SEnji Cooper {
106*57718be8SEnji Cooper 	sigset_t block;
107*57718be8SEnji Cooper 	struct timespec ts;
108*57718be8SEnji Cooper 	siginfo_t info;
109*57718be8SEnji Cooper 	int r;
110*57718be8SEnji Cooper 
111*57718be8SEnji Cooper 	sigemptyset(&block);
112*57718be8SEnji Cooper 	ts.tv_sec = 5;
113*57718be8SEnji Cooper 	ts.tv_nsec = 0;
114*57718be8SEnji Cooper 	r = sigtimedwait(&block, &info, &ts);
115*57718be8SEnji Cooper 	ATF_REQUIRE(r == -1);
116*57718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EAGAIN, errno);
117*57718be8SEnji Cooper }
118*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)119*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
120*57718be8SEnji Cooper {
121*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sigtimedwait_all0timeout);
122*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sigtimedwait_NULL_timeout);
123*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sigtimedwait_small_timeout);
124*57718be8SEnji Cooper 
125*57718be8SEnji Cooper 	return atf_no_error();
126*57718be8SEnji Cooper }
127