1 /* $NetBSD: t_nanosleep.c,v 1.4 2017/01/13 21:15:14 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_nanosleep.c,v 1.4 2017/01/13 21:15:14 christos Exp $");
48 handler(int signo __unused)
55 info_handler(int signo __unused)
62 ATF_TC(nanosleep_basic);
63 ATF_TC_HEAD(nanosleep_basic, tc)
65 atf_tc_set_md_var(tc, "descr", "Test that nanosleep(2) works");
68 ATF_TC_BODY(nanosleep_basic, tc)
70 static const size_t maxiter = 10;
71 struct timespec ts1, ts2, tsn;
74 for (i = 1; i < maxiter; i++) {
79 (void)memset(&ts1, 0, sizeof(struct timespec));
80 (void)memset(&ts2, 0, sizeof(struct timespec));
82 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts1) == 0);
83 ATF_REQUIRE(nanosleep(&tsn, NULL) == 0);
84 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts2) == 0);
87 * Verify that we slept at least one nanosecond.
89 if (timespeccmp(&ts2, &ts1, <=) != 0) {
92 "sleep time:: sec %llu, nsec %lu\n\t\t"
93 "ts1: sec %llu, nsec %lu\n\t\t"
94 "ts2: sec %llu, nsec %lu\n",
95 (unsigned long long)tsn.tv_sec, tsn.tv_nsec,
96 (unsigned long long)ts1.tv_sec, ts1.tv_nsec,
97 (unsigned long long)ts2.tv_sec, ts2.tv_nsec);
99 atf_tc_fail_nonfatal("inaccuracies in sleep time "
100 "(resolution = %lu nsec)", tsn.tv_nsec);
105 ATF_TC(nanosleep_err);
106 ATF_TC_HEAD(nanosleep_err, tc)
108 atf_tc_set_md_var(tc, "descr",
109 "Test errors from nanosleep(2) (PR bin/14558)");
112 ATF_TC_BODY(nanosleep_err, tc)
119 ATF_REQUIRE_ERRNO(EINVAL, nanosleep(&ts, NULL) == -1);
122 ts.tv_nsec = 1000000000;
124 ATF_REQUIRE_ERRNO(EINVAL, nanosleep(&ts, NULL) == -1);
129 ATF_REQUIRE_ERRNO(0, nanosleep(&ts, NULL) == 0);
132 ATF_REQUIRE_ERRNO(EFAULT, nanosleep((void *)-1, NULL) == -1);
135 ATF_TC(nanosleep_sig);
136 ATF_TC_HEAD(nanosleep_sig, tc)
138 atf_tc_set_md_var(tc, "descr", "Test signal for nanosleep(2)");
141 ATF_TC_BODY(nanosleep_sig, tc)
143 struct timespec tsn, tsr;
148 * Test that a signal interrupts nanosleep(2).
150 * (In which case the return value should be -1 and the
151 * second parameter should contain the unslept time.)
155 ATF_REQUIRE(pid >= 0);
156 ATF_REQUIRE(signal(SIGINT, handler) == 0);
168 if (nanosleep(&tsn, &tsr) != -1)
174 if (tsr.tv_sec == 0 && tsr.tv_nsec == 0)
181 (void)kill(pid, SIGINT);
184 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
185 atf_tc_fail("signal did not interrupt nanosleep(2)");
188 ATF_TC(nanosleep_eintr);
189 ATF_TC_HEAD(nanosleep_eintr, tc)
191 atf_tc_set_md_var(tc, "descr", "Test [EINTR] for nanosleep(2)");
192 atf_tc_set_md_var(tc, "timeout", "7");
195 ATF_TC_BODY(nanosleep_eintr, tc)
197 struct sigaction act;
198 struct timespec tso, ts;
203 * Test that [EINTR] properly handles rmtp for nanosleep(2).
207 ATF_REQUIRE(pid >= 0);
212 act.sa_handler = info_handler;
213 sigemptyset(&act.sa_mask);
214 act.sa_flags = 0; /* Don't allow restart. */
215 ATF_REQUIRE(sigaction(SIGINFO, &act, NULL) == 0);
220 ts.tv_sec = tso.tv_sec;
221 ts.tv_nsec = tso.tv_nsec;
224 while (nanosleep(&ts, &ts) != 0) {
225 ATF_REQUIRE_MSG(timespeccmp(&ts, &tso, <=),
226 "errno=%d ts=%0.9f should be <= last tso=%0.9f\n",
228 ts.tv_sec + ts.tv_nsec / 1e9,
229 tso.tv_sec + tso.tv_nsec / 1e9);
230 if (errno == EINTR && got_info == 1) {
233 tso.tv_sec = ts.tv_sec;
234 tso.tv_nsec = ts.tv_nsec;
246 /* Flood the process with SIGINFO until it exits. */
248 for (int i = 0; i < 10; i++)
249 ATF_REQUIRE(kill(pid, SIGINFO) == 0);
250 ATF_REQUIRE(usleep(10000) == 0);
251 } while (waitpid(pid, &sta, WNOHANG) == 0);
253 ATF_REQUIRE(WIFEXITED(sta) == 1);
255 if (WEXITSTATUS(sta) != EXIT_SUCCESS)
256 atf_tc_fail("nanosleep(2) handled rtmp incorrectly");
262 ATF_TP_ADD_TC(tp, nanosleep_basic);
263 ATF_TP_ADD_TC(tp, nanosleep_err);
264 ATF_TP_ADD_TC(tp, nanosleep_sig);
265 ATF_TP_ADD_TC(tp, nanosleep_eintr);
267 return atf_no_error();