xref: /netbsd-src/tests/lib/libpthread/t_sleep.c (revision ac555471cafe6fc06b9337a8708e2b398bf714c8)
1*ac555471Sjmmv /* $NetBSD: t_sleep.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2*ac555471Sjmmv 
3*ac555471Sjmmv /*
4*ac555471Sjmmv  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*ac555471Sjmmv  * All rights reserved.
6*ac555471Sjmmv  *
7*ac555471Sjmmv  * Redistribution and use in source and binary forms, with or without
8*ac555471Sjmmv  * modification, are permitted provided that the following conditions
9*ac555471Sjmmv  * are met:
10*ac555471Sjmmv  * 1. Redistributions of source code must retain the above copyright
11*ac555471Sjmmv  *    notice, this list of conditions and the following disclaimer.
12*ac555471Sjmmv  * 2. Redistributions in binary form must reproduce the above copyright
13*ac555471Sjmmv  *    notice, this list of conditions and the following disclaimer in the
14*ac555471Sjmmv  *    documentation and/or other materials provided with the distribution.
15*ac555471Sjmmv  *
16*ac555471Sjmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*ac555471Sjmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*ac555471Sjmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*ac555471Sjmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*ac555471Sjmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*ac555471Sjmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*ac555471Sjmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*ac555471Sjmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*ac555471Sjmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*ac555471Sjmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*ac555471Sjmmv  * POSSIBILITY OF SUCH DAMAGE.
27*ac555471Sjmmv  */
28*ac555471Sjmmv 
29*ac555471Sjmmv #include <sys/cdefs.h>
30*ac555471Sjmmv __COPYRIGHT("@(#) Copyright (c) 2008\
31*ac555471Sjmmv  The NetBSD Foundation, inc. All rights reserved.");
32*ac555471Sjmmv __RCSID("$NetBSD: t_sleep.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33*ac555471Sjmmv 
34*ac555471Sjmmv #include <sys/time.h>
35*ac555471Sjmmv 
36*ac555471Sjmmv #include <pthread.h>
37*ac555471Sjmmv #include <signal.h>
38*ac555471Sjmmv #include <stdio.h>
39*ac555471Sjmmv #include <unistd.h>
40*ac555471Sjmmv 
41*ac555471Sjmmv #include <atf-c.h>
42*ac555471Sjmmv 
43*ac555471Sjmmv #include "h_common.h"
44*ac555471Sjmmv 
45*ac555471Sjmmv #define LONGTIME 2000000000
46*ac555471Sjmmv 
47*ac555471Sjmmv static void *
threadfunc(void * arg)48*ac555471Sjmmv threadfunc(void *arg)
49*ac555471Sjmmv {
50*ac555471Sjmmv 	sleep(LONGTIME);
51*ac555471Sjmmv 
52*ac555471Sjmmv 	return NULL;
53*ac555471Sjmmv }
54*ac555471Sjmmv 
55*ac555471Sjmmv static void
handler(int sig)56*ac555471Sjmmv handler(int sig)
57*ac555471Sjmmv {
58*ac555471Sjmmv 	/*
59*ac555471Sjmmv 	 * Nothing to do; invoking the handler is enough to interrupt
60*ac555471Sjmmv 	 * the sleep.
61*ac555471Sjmmv 	 */
62*ac555471Sjmmv }
63*ac555471Sjmmv 
64*ac555471Sjmmv ATF_TC(sleep1);
ATF_TC_HEAD(sleep1,tc)65*ac555471Sjmmv ATF_TC_HEAD(sleep1, tc)
66*ac555471Sjmmv {
67*ac555471Sjmmv 	atf_tc_set_md_var(tc, "descr", "Checks sleeping past the time when "
68*ac555471Sjmmv 	    "time_t wraps");
69*ac555471Sjmmv }
ATF_TC_BODY(sleep1,tc)70*ac555471Sjmmv ATF_TC_BODY(sleep1, tc)
71*ac555471Sjmmv {
72*ac555471Sjmmv 	pthread_t thread;
73*ac555471Sjmmv 	struct itimerval it;
74*ac555471Sjmmv 	struct sigaction act;
75*ac555471Sjmmv 	sigset_t mtsm;
76*ac555471Sjmmv 
77*ac555471Sjmmv 	printf("Testing sleeps unreasonably far into the future.\n");
78*ac555471Sjmmv 
79*ac555471Sjmmv 	act.sa_handler = handler;
80*ac555471Sjmmv 	sigemptyset(&act.sa_mask);
81*ac555471Sjmmv 	act.sa_flags = 0;
82*ac555471Sjmmv 	sigaction(SIGALRM, &act, NULL);
83*ac555471Sjmmv 
84*ac555471Sjmmv 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
85*ac555471Sjmmv 
86*ac555471Sjmmv 	/* make sure the signal is delivered to the child thread */
87*ac555471Sjmmv 	sigemptyset(&mtsm);
88*ac555471Sjmmv 	sigaddset(&mtsm, SIGALRM);
89*ac555471Sjmmv 	PTHREAD_REQUIRE(pthread_sigmask(SIG_BLOCK, &mtsm, 0));
90*ac555471Sjmmv 
91*ac555471Sjmmv 	timerclear(&it.it_interval);
92*ac555471Sjmmv 	timerclear(&it.it_value);
93*ac555471Sjmmv 	it.it_value.tv_sec = 1;
94*ac555471Sjmmv 	setitimer(ITIMER_REAL, &it, NULL);
95*ac555471Sjmmv 
96*ac555471Sjmmv 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
97*ac555471Sjmmv }
98*ac555471Sjmmv 
ATF_TP_ADD_TCS(tp)99*ac555471Sjmmv ATF_TP_ADD_TCS(tp)
100*ac555471Sjmmv {
101*ac555471Sjmmv 	ATF_TP_ADD_TC(tp, sleep1);
102*ac555471Sjmmv 
103*ac555471Sjmmv 	return atf_no_error();
104*ac555471Sjmmv }
105