xref: /netbsd-src/tests/kernel/t_timeleft.c (revision 323118b11cc119563312d2bf266fde3dd7a1e778)
1 /* $NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $");
36 
37 #include <sys/types.h>
38 #include <sys/select.h>
39 
40 #include <atf-c.h>
41 #include <time.h>
42 #include <errno.h>
43 #include <lwp.h>
44 #include <signal.h>
45 #include <pthread.h>
46 #include <stdio.h>
47 #include <sched.h>
48 #include <unistd.h>
49 
50 static void
sighandler(int signo __unused)51 sighandler(int signo __unused)
52 {
53 }
54 
55 struct info {
56 	void (*fun)(struct timespec *);
57 	struct timespec ts;
58 };
59 
60 static void
timeleft__lwp_park(struct timespec * ts)61 timeleft__lwp_park(struct timespec *ts)
62 {
63 	ATF_REQUIRE_ERRNO(EINTR, _lwp_park(CLOCK_MONOTONIC, TIMER_RELTIME,
64 	    ts, 0, ts, NULL) == -1);
65 }
66 
67 #if 0
68 static void
69 timeleft_pselect(struct timespec *ts)
70 {
71 	ATF_REQUIRE_ERRNO(EINTR, pselect(1, NULL, NULL, NULL, ts, NULL));
72 }
73 #endif
74 
75 static void *
runner(void * arg)76 runner(void *arg)
77 {
78 	struct info *i = arg;
79 	(*i->fun)(&i->ts);
80 	return NULL;
81 }
82 
83 static void
tester(void (* fun)(struct timespec *))84 tester(void (*fun)(struct timespec *))
85 {
86 	const struct timespec ts = { 5, 0 };
87 	const struct timespec sts = { 1, 0 };
88 	struct info i = { fun, ts };
89 	pthread_t thr;
90 
91 	ATF_REQUIRE(signal(SIGINT, sighandler) == 0);
92 	ATF_REQUIRE(pthread_create(&thr, NULL, runner, &i) == 0);
93 
94 	ATF_REQUIRE(nanosleep(&sts, NULL) == 0);
95 	ATF_REQUIRE(pthread_kill(thr, SIGINT) == 0);
96 	ATF_REQUIRE(pthread_join(thr, NULL) == 0);
97 	printf("Orig time %ju.%lu\n", (intmax_t)ts.tv_sec, ts.tv_nsec);
98 	printf("Time left %ju.%lu\n", (intmax_t)i.ts.tv_sec, i.ts.tv_nsec);
99 	ATF_REQUIRE(timespeccmp(&i.ts, &ts, <));
100 }
101 
102 ATF_TC(timeleft__lwp_park);
ATF_TC_HEAD(timeleft__lwp_park,tc)103 ATF_TC_HEAD(timeleft__lwp_park, tc)
104 {
105 	atf_tc_set_md_var(tc, "descr", "Checks that _lwp_park(2) returns "
106 	    "the time left to sleep after interrupted");
107 }
108 
ATF_TC_BODY(timeleft__lwp_park,tc)109 ATF_TC_BODY(timeleft__lwp_park, tc)
110 {
111 	tester(timeleft__lwp_park);
112 }
113 
114 #if 0
115 ATF_TC(timeleft_pselect);
116 ATF_TC_HEAD(timeleft_pselect, tc)
117 {
118 	atf_tc_set_md_var(tc, "descr", "Checks that pselect(2) returns "
119 	    "the time left to sleep after interrupted");
120 }
121 
122 ATF_TC_BODY(timeleft_pselect, tc)
123 {
124 	tester(timeleft_pselect);
125 }
126 #endif
127 
ATF_TP_ADD_TCS(tp)128 ATF_TP_ADD_TCS(tp)
129 {
130 
131 	ATF_TP_ADD_TC(tp, timeleft__lwp_park);
132 #if 0
133 	ATF_TP_ADD_TC(tp, timeleft_pselect);
134 #endif
135 
136 	return atf_no_error();
137 }
138