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
81ATF_TC_WITH_CLEANUP(basic);
82ATF_TC_HEAD(basic, tc)
83{
84	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
85	    "semaphores");
86}
87ATF_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}
117ATF_TC_CLEANUP(basic, tc)
118{
119	(void)sem_unlink("/sem_b");
120}
121
122ATF_TC_WITH_CLEANUP(child);
123ATF_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}
129ATF_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}
178ATF_TC_CLEANUP(child, tc)
179{
180	(void)sem_unlink("/sem_a");
181}
182
183static inline void
184timespec_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
193static volatile sig_atomic_t got_sigalrm = 0;
194
195static void
196sigalrm_handler(int sig __unused)
197{
198	got_sigalrm = 1;
199}
200
201ATF_TC(timedwait);
202ATF_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}
211ATF_TC_BODY(timedwait, tc)
212{
213	struct timespec ts;
214	sem_t sem;
215
216	SEM_REQUIRE(sem_init(&sem, 0, 0));
217	SEM_REQUIRE(sem_post(&sem));
218	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
219	    "%s", strerror(errno));
220        timespec_add_ms(&ts, 100);
221	SEM_REQUIRE(sem_timedwait(&sem, &ts));
222	ATF_REQUIRE_ERRNO(ETIMEDOUT, sem_timedwait(&sem, &ts));
223        ts.tv_sec--;
224	ATF_REQUIRE_ERRNO(ETIMEDOUT, sem_timedwait(&sem, &ts));
225	SEM_REQUIRE(sem_post(&sem));
226	SEM_REQUIRE(sem_timedwait(&sem, &ts));
227
228	/* timespec validation, in the past */
229	ts.tv_nsec += 1000*1000*1000;
230	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
231	ts.tv_nsec = -1;
232	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
233	/* timespec validation, in the future */
234	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
235	    "%s", strerror(errno));
236	ts.tv_sec++;
237	ts.tv_nsec = 1000*1000*1000;
238	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
239	ts.tv_nsec = -1;
240	ATF_REQUIRE_ERRNO(EINVAL, sem_timedwait(&sem, &ts));
241
242	/* EINTR */
243	struct sigaction act = {
244		.sa_handler = sigalrm_handler,
245		.sa_flags = 0	/* not SA_RESTART */
246	};
247	ATF_REQUIRE_MSG(sigemptyset(&act.sa_mask) == 0,
248	    "%s", strerror(errno));
249	ATF_REQUIRE_MSG(sigaction(SIGALRM, &act, NULL) == 0,
250	    "%s", strerror(errno));
251	struct itimerval it = {
252		.it_value.tv_usec = 50*1000
253	};
254	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
255	    "%s", strerror(errno));
256	ATF_REQUIRE_MSG(clock_gettime(CLOCK_REALTIME, &ts) == 0,
257	    "%s", strerror(errno));
258        timespec_add_ms(&ts, 100);
259	ATF_REQUIRE_ERRNO(EINTR, sem_timedwait(&sem, &ts));
260	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
261
262#ifdef __FreeBSD__
263	/* CLOCK_MONOTONIC, absolute */
264	SEM_REQUIRE(sem_post(&sem));
265	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
266	    "%s", strerror(errno));
267        timespec_add_ms(&ts, 100);
268	SEM_REQUIRE(sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME,
269	    &ts, NULL));
270	ATF_REQUIRE_ERRNO(ETIMEDOUT,
271	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL));
272
273	/* CLOCK_MONOTONIC, relative */
274	SEM_REQUIRE(sem_post(&sem));
275	ts.tv_sec = 0;
276	ts.tv_nsec = 100*1000*1000;
277	SEM_REQUIRE(sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0,
278	    &ts, NULL));
279	ATF_REQUIRE_ERRNO(ETIMEDOUT,
280	    sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts, NULL));
281
282	/* absolute does not update remaining time on EINTR */
283	struct timespec remain = {42, 1000*1000*1000};
284	got_sigalrm = 0;
285	it.it_value.tv_usec = 50*1000;
286	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
287	    "%s", strerror(errno));
288	ATF_REQUIRE_MSG(clock_gettime(CLOCK_MONOTONIC, &ts) == 0,
289	    "%s", strerror(errno));
290        timespec_add_ms(&ts, 100);
291	ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC,
292	    TIMER_ABSTIME, &ts, &remain));
293	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
294	ATF_REQUIRE_MSG(remain.tv_sec == 42 && remain.tv_nsec == 1000*1000*1000,
295	    "an absolute clockwait modified the remaining time on EINTR");
296
297	/* relative updates remaining time on EINTR */
298	remain.tv_sec = 42;
299	remain.tv_nsec = 1000*1000*1000;
300	got_sigalrm = 0;
301	it.it_value.tv_usec = 50*1000;
302	ATF_REQUIRE_MSG(setitimer(ITIMER_REAL, &it, NULL) == 0,
303	    "%s", strerror(errno));
304	ts.tv_sec = 0;
305	ts.tv_nsec = 100*1000*1000;
306	ATF_REQUIRE_ERRNO(EINTR, sem_clockwait_np(&sem, CLOCK_MONOTONIC, 0, &ts,
307	    &remain));
308	ATF_REQUIRE_MSG(got_sigalrm, "did not get SIGALRM");
309	/*
310	 * If this nsec comparison turns out to be unreliable due to timing,
311	 * it could simply check that nsec < 100 ms.
312	 */
313	ATF_REQUIRE_MSG(remain.tv_sec == 0 &&
314	    remain.tv_nsec >= 25*1000*1000 &&
315	    remain.tv_nsec <= 75*1000*1000,
316	    "the remaining time was not as expected when a relative clockwait"
317	    " got EINTR" );
318#endif
319}
320
321ATF_TP_ADD_TCS(tp)
322{
323
324	ATF_TP_ADD_TC(tp, basic);
325	ATF_TP_ADD_TC(tp, child);
326	ATF_TP_ADD_TC(tp, timedwait);
327
328	return atf_no_error();
329}
330