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