t_timeleft.c revision 1.1 1 /* $NetBSD: t_timeleft.c,v 1.1 2017/12/08 01:19:29 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.1 2017/12/08 01:19:29 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
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
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 *
76 runner(void *arg)
77 {
78 struct info *i = arg;
79 (*i->fun)(&i->ts);
80 return NULL;
81 }
82
83 static void
84 tester(void (*fun)(struct timespec *))
85 {
86 const struct timespec ts = { 5, 0 };
87 const struct timespec sts = { 0, 1000000 };
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 nanosleep(&sts, NULL);
95 pthread_kill(thr, SIGINT);
96 printf("Orig time %ju.%lu\n", (intmax_t)ts.tv_sec, ts.tv_nsec);
97 printf("Time left %ju.%lu\n", (intmax_t)i.ts.tv_sec, i.ts.tv_nsec);
98 ATF_REQUIRE(timespeccmp(&i.ts, &ts, <));
99 }
100
101 ATF_TC(timeleft__lwp_park);
102 ATF_TC_HEAD(timeleft__lwp_park, tc)
103 {
104 atf_tc_set_md_var(tc, "descr", "Checks that _lwp_park(2) returns "
105 "the time left to sleep after interrupted");
106 }
107
108 ATF_TC_BODY(timeleft__lwp_park, tc)
109 {
110 tester(timeleft__lwp_park);
111 }
112
113 #if 0
114 ATF_TC(timeleft_pselect);
115 ATF_TC_HEAD(timeleft_pselect, tc)
116 {
117 atf_tc_set_md_var(tc, "descr", "Checks that pselect(2) returns "
118 "the time left to sleep after interrupted");
119 }
120
121 ATF_TC_BODY(timeleft_pselect, tc)
122 {
123 tester(timeleft_pselect);
124 }
125 #endif
126
127 ATF_TP_ADD_TCS(tp)
128 {
129
130 ATF_TP_ADD_TC(tp, timeleft__lwp_park);
131 #if 0
132 ATF_TP_ADD_TC(tp, timeleft_pselect);
133 #endif
134
135 return atf_no_error();
136 }
137