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