xref: /freebsd/contrib/netbsd-tests/lib/librt/t_sem.c (revision d6b92ffa)
1 /* $NetBSD: t_sem.c,v 1.3 2017/01/14 20:58:20 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2008, 2010 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 /*
30  * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice(s), this list of conditions and the following disclaimer as
38  *    the first lines of this file unmodified other than the possible
39  *    addition of one or more copyright notices.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice(s), this list of conditions and the following disclaimer in
42  *    the documentation and/or other materials provided with the
43  *    distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
46  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
49  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
52  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
54  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
55  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
60  The NetBSD Foundation, inc. All rights reserved.");
61 __RCSID("$NetBSD: t_sem.c,v 1.3 2017/01/14 20:58:20 christos Exp $");
62 
63 #include <sys/time.h>
64 #include <sys/wait.h>
65 
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <semaphore.h>
69 #include <signal.h>
70 #include <stdio.h>
71 #include <time.h>
72 #include <unistd.h>
73 
74 #include <atf-c.h>
75 
76 #define NCHILDREN 10
77 
78 #define SEM_REQUIRE(x) \
79 	ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno))
80 
81 ATF_TC_WITH_CLEANUP(basic);
82 ATF_TC_HEAD(basic, tc)
83 {
84 	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
85 	    "semaphores");
86 }
87 ATF_TC_BODY(basic, tc)
88 {
89 	int val;
90 	sem_t *sem_b;
91 
92 	if (sysconf(_SC_SEMAPHORES) == -1)
93 		atf_tc_skip("POSIX semaphores not supported");
94 
95 	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
96 	ATF_REQUIRE(sem_b != SEM_FAILED);
97 
98 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
99 	ATF_REQUIRE_EQ(val, 0);
100 
101 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
102 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
103 	ATF_REQUIRE_EQ(val, 1);
104 
105 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
106 	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
107 	ATF_REQUIRE_EQ(errno, EAGAIN);
108 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
109 	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
110 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
111 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
112 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
113 
114 	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
115 	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
116 }
117 ATF_TC_CLEANUP(basic, tc)
118 {
119 	(void)sem_unlink("/sem_b");
120 }
121 
122 ATF_TC_WITH_CLEANUP(child);
123 ATF_TC_HEAD(child, tc)
124 {
125 	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
126 	    "parent with multiple child processes");
127 	atf_tc_set_md_var(tc, "timeout", "5");
128 }
129 ATF_TC_BODY(child, tc)
130 {
131 	pid_t children[NCHILDREN];
132 	unsigned i, j;
133 	sem_t *sem_a;
134 	int status;
135 
136 	pid_t pid;
137 
138 	if (sysconf(_SC_SEMAPHORES) == -1)
139 		atf_tc_skip("POSIX semaphores not supported");
140 
141 	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
142 	ATF_REQUIRE(sem_a != SEM_FAILED);
143 
144 	for (j = 1; j <= 2; j++) {
145 		for (i = 0; i < NCHILDREN; i++) {
146 			switch ((pid = fork())) {
147 			case -1:
148 				atf_tc_fail("fork() returned -1");
149 			case 0:
150 				printf("PID %d waiting for semaphore...\n",
151 				    getpid());
152 				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
153 				    "sem_wait failed; iteration %d", j);
154 				printf("PID %d got semaphore\n", getpid());
155 				_exit(0);
156 			default:
157 				children[i] = pid;
158 				break;
159 			}
160 		}
161 
162 		for (i = 0; i < NCHILDREN; i++) {
163 			usleep(100000);
164 			printf("main loop %d: posting...\n", j);
165 			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
166 		}
167 
168 		for (i = 0; i < NCHILDREN; i++) {
169 			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
170 			ATF_REQUIRE(WIFEXITED(status));
171 			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
172 		}
173 	}
174 
175 	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
176 	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
177 }
178 ATF_TC_CLEANUP(child, tc)
179 {
180 	(void)sem_unlink("/sem_a");
181 }
182 
183 static inline void
184 timespec_add_ms(struct timespec *ts, int ms)
185 {
186 	ts->tv_nsec += ms * 1000*1000;
187 	if (ts->tv_nsec > 1000*1000*1000) {
188 		ts->tv_sec++;
189 		ts->tv_nsec -= 1000*1000*1000;
190 	}
191 }
192 
193 volatile sig_atomic_t got_sigalrm = 0;
194 
195 static void
196 sigalrm_handler(int sig __unused)
197 {
198 	got_sigalrm = 1;
199 }
200 
201 ATF_TC(timedwait);
202 ATF_TC_HEAD(timedwait, tc)
203 {
204 	atf_tc_set_md_var(tc, "descr", "Tests sem_timedwait(3)"
205 #ifdef __FreeBSD__
206 	    " and sem_clockwait_np(3)"
207 #endif
208 	    );
209 	atf_tc_set_md_var(tc, "timeout", "20");
210 }
211 ATF_TC_BODY(timedwait, tc)
212 {
213 	struct timespec ts;
214 	sem_t sem;
215 	int result;
216 
217 	SEM_REQUIRE(sem_init(&sem, 0, 0));
218 	SEM_REQUIRE(sem_post(&sem));
219 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
220 	    "%s", strerror(errno));
221         timespec_add_ms(&ts, 100);
222 	SEM_REQUIRE(sem_timedwait(&sem, &ts));
223 	ATF_REQUIRE_ERRNO(ETIMEDOUT, sem_timedwait(&sem, &ts));
224         ts.tv_sec--;
225 	ATF_REQUIRE_ERRNO(ETIMEDOUT, sem_timedwait(&sem, &ts));
226 	SEM_REQUIRE(sem_post(&sem));
227 	SEM_REQUIRE(sem_timedwait(&sem, &ts));
228 
229 	/* timespec validation, in the past */
230 	ts.tv_nsec += 1000*1000*1000;
231 	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
232 	ts.tv_nsec = -1;
233 	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
234 	/* timespec validation, in the future */
235 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
236 	    "%s", strerror(errno));
237 	ts.tv_sec++;
238 	ts.tv_nsec = 1000*1000*1000;
239 	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
240 	ts.tv_nsec = -1;
241 	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
242 
243 	/* EINTR */
244 	struct sigaction act = {
245 		.sa_handler = sigalrm_handler,
246 		.sa_flags = 0	/* not SA_RESTART */
247 	};
248 	ATF_REQUIRE_MSG(sigemptyset(&act.sa_mask) == 0,
249 	    "%s", strerror(errno));
250 	ATF_REQUIRE_MSG(sigaction(SIGALRM, &act, NULL) == 0,
251 	    "%s", strerror(errno));
252 	struct itimerval it = {
253 		.it_value.tv_usec = 50*1000
254 	};
255 	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
256 	    "%s", strerror(errno));
257 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
258 	    "%s", strerror(errno));
259         timespec_add_ms(&ts, 100);
260 	ATF_REQUIRE_ERRNO(EINTR, sem_timedwait(&sem, &ts));
261 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
262 
263 #ifdef __FreeBSD__
264 	/* CLOCK_MONOTONIC, absolute */
265 	SEM_REQUIRE(sem_post(&sem));
266 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
267 	    "%s", strerror(errno));
268         timespec_add_ms(&ts, 100);
269 	SEM_REQUIRE(sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME,
270 	    &ts, NULL));
271 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
272 	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL));
273 
274 	/* CLOCK_MONOTONIC, relative */
275 	SEM_REQUIRE(sem_post(&sem));
276 	ts.tv_sec = 0;
277 	ts.tv_nsec = 100*1000*1000;
278 	SEM_REQUIRE(sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0,
279 	    &ts, NULL));
280 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
281 	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts, NULL));
282 
283 	/* absolute does not update remaining time on EINTR */
284 	struct timespec remain = {42, 1000*1000*1000};
285 	got_sigalrm = 0;
286 	it.it_value.tv_usec = 50*1000;
287 	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
288 	    "%s", strerror(errno));
289 	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
290 	    "%s", strerror(errno));
291         timespec_add_ms(&ts, 100);
292 	ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC,
293 	    TIMER_ABSTIME, &ts, &remain));
294 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
295 	ATF_REQUIRE_MSG(remain.tv_sec == 42 && remain.tv_nsec == 1000*1000*1000,
296 	    "an absolute clockwait modified the remaining time on EINTR");
297 
298 	/* relative updates remaining time on EINTR */
299 	remain.tv_sec = 42;
300 	remain.tv_nsec = 1000*1000*1000;
301 	got_sigalrm = 0;
302 	it.it_value.tv_usec = 50*1000;
303 	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
304 	    "%s", strerror(errno));
305 	ts.tv_sec = 0;
306 	ts.tv_nsec = 100*1000*1000;
307 	ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts,
308 	    &remain));
309 	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
310 	/*
311 	 * If this nsec comparison turns out to be unreliable due to timing,
312 	 * it could simply check that nsec < 100 ms.
313 	 */
314 	ATF_REQUIRE_MSG(remain.tv_sec == 0 &&
315 	    remain.tv_nsec >= 25*1000*1000 &&
316 	    remain.tv_nsec <= 75*1000*1000,
317 	    "the remaining time was not as expected when a relative clockwait"
318 	    " got EINTR" );
319 #endif
320 }
321 
322 ATF_TP_ADD_TCS(tp)
323 {
324 
325 	ATF_TP_ADD_TC(tp, basic);
326 	ATF_TP_ADD_TC(tp, child);
327 	ATF_TP_ADD_TC(tp, timedwait);
328 
329 	return atf_no_error();
330 }
331