1 /* $NetBSD: t_sem.c,v 1.3 2017/01/14 20:58:20 christos Exp $ */
4 * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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
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.
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 $");
68 #include <semaphore.h>
78 #define SEM_REQUIRE(x) \
79 ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno))
81 ATF_TC_WITH_CLEANUP(basic);
82 ATF_TC_HEAD(basic, tc)
84 atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
87 ATF_TC_BODY(basic, tc)
92 if (sysconf(_SC_SEMAPHORES) == -1)
93 atf_tc_skip("POSIX semaphores not supported");
95 sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
96 ATF_REQUIRE(sem_b != SEM_FAILED);
98 ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
99 ATF_REQUIRE_EQ(val, 0);
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);
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);
114 ATF_REQUIRE_EQ(sem_close(sem_b), 0);
115 ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
117 ATF_TC_CLEANUP(basic, tc)
119 (void)sem_unlink("/sem_b");
122 ATF_TC_WITH_CLEANUP(child);
123 ATF_TC_HEAD(child, tc)
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");
129 ATF_TC_BODY(child, tc)
131 pid_t children[NCHILDREN];
138 if (sysconf(_SC_SEMAPHORES) == -1)
139 atf_tc_skip("POSIX semaphores not supported");
141 sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
142 ATF_REQUIRE(sem_a != SEM_FAILED);
144 for (j = 1; j <= 2; j++) {
145 for (i = 0; i < NCHILDREN; i++) {
146 switch ((pid = fork())) {
148 atf_tc_fail("fork() returned -1");
150 printf("PID %d waiting for semaphore...\n",
152 ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
153 "sem_wait failed; iteration %d", j);
154 printf("PID %d got semaphore\n", getpid());
162 for (i = 0; i < NCHILDREN; i++) {
164 printf("main loop %d: posting...\n", j);
165 ATF_REQUIRE_EQ(sem_post(sem_a), 0);
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);
175 ATF_REQUIRE_EQ(sem_close(sem_a), 0);
176 ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
178 ATF_TC_CLEANUP(child, tc)
180 (void)sem_unlink("/sem_a");
184 timespec_add_ms(struct timespec *ts, int ms)
186 ts->tv_nsec += ms * 1000*1000;
187 if (ts->tv_nsec > 1000*1000*1000) {
189 ts->tv_nsec -= 1000*1000*1000;
193 volatile sig_atomic_t got_sigalrm = 0;
196 sigalrm_handler(int sig __unused)
202 ATF_TC_HEAD(timedwait, tc)
204 atf_tc_set_md_var(tc, "descr", "Tests sem_timedwait(3)"
206 " and sem_clockwait_np(3)"
209 atf_tc_set_md_var(tc, "timeout", "20");
211 ATF_TC_BODY(timedwait, tc)
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));
225 ATF_REQUIRE_ERRNO(ETIMEDOUT, sem_timedwait(&sem, &ts));
226 SEM_REQUIRE(sem_post(&sem));
227 SEM_REQUIRE(sem_timedwait(&sem, &ts));
229 /* timespec validation, in the past */
230 ts.tv_nsec += 1000*1000*1000;
231 ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
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));
238 ts.tv_nsec = 1000*1000*1000;
239 ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
241 ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
244 struct sigaction act = {
245 .sa_handler = sigalrm_handler,
246 .sa_flags = 0 /* not SA_RESTART */
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
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");
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,
271 ATF_REQUIRE_ERRNO(ETIMEDOUT,
272 sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL));
274 /* CLOCK_MONOTONIC, relative */
275 SEM_REQUIRE(sem_post(&sem));
277 ts.tv_nsec = 100*1000*1000;
278 SEM_REQUIRE(sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0,
280 ATF_REQUIRE_ERRNO(ETIMEDOUT,
281 sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts, NULL));
283 /* absolute does not update remaining time on EINTR */
284 struct timespec remain = {42, 1000*1000*1000};
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");
298 /* relative updates remaining time on EINTR */
300 remain.tv_nsec = 1000*1000*1000;
302 it.it_value.tv_usec = 50*1000;
303 ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
304 "%s", strerror(errno));
306 ts.tv_nsec = 100*1000*1000;
307 ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts,
309 ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
311 * If this nsec comparison turns out to be unreliable due to timing,
312 * it could simply check that nsec < 100 ms.
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"
325 ATF_TP_ADD_TC(tp, basic);
326 ATF_TP_ADD_TC(tp, child);
327 ATF_TP_ADD_TC(tp, timedwait);
329 return atf_no_error();