summaryrefslogtreecommitdiff
path: root/tests/lib/librt/t_sem.c
blob: 804b42a84d65e1b8b35fc5c3ed8bb6425317f002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* $NetBSD: t_sem.c,v 1.6 2021/12/14 16:25:11 wiz Exp $ */

/*
 * Copyright (c) 2008, 2010, 2019 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer as
 *    the first lines of this file unmodified other than the possible
 *    addition of one or more copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2008, 2010, 2019\
 The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_sem.c,v 1.6 2021/12/14 16:25:11 wiz Exp $");

#include <sys/mman.h>
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

#include <atf-c.h>

#define NCHILDREN 10

ATF_TC_WITH_CLEANUP(basic);
ATF_TC_HEAD(basic, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
	    "semaphores");
}
ATF_TC_BODY(basic, tc)
{
	int val;
	sem_t *sem_b;

	if (sysconf(_SC_SEMAPHORES) == -1)
		atf_tc_skip("POSIX semaphores not supported");

	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
	ATF_REQUIRE(sem_b != SEM_FAILED);

	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
	ATF_REQUIRE_EQ(val, 0);

	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
	ATF_REQUIRE_EQ(val, 1);

	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
	ATF_REQUIRE_EQ(errno, EAGAIN);
	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
	ATF_REQUIRE_EQ(sem_post(sem_b), 0);

	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
}
ATF_TC_CLEANUP(basic, tc)
{
	(void)sem_unlink("/sem_b");
}

ATF_TC_WITH_CLEANUP(child);
ATF_TC_HEAD(child, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
	    "parent with multiple child processes");
}
ATF_TC_BODY(child, tc)
{
	pid_t children[NCHILDREN];
	unsigned i, j;
	sem_t *sem_a;
	int status;

	pid_t pid;

	if (sysconf(_SC_SEMAPHORES) == -1)
		atf_tc_skip("POSIX semaphores not supported");

	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
	ATF_REQUIRE(sem_a != SEM_FAILED);

	for (j = 1; j <= 2; j++) {
		for (i = 0; i < NCHILDREN; i++) {
			switch ((pid = fork())) {
			case -1:
				atf_tc_fail("fork() returned -1");
			case 0:
				printf("PID %d waiting for semaphore...\n",
				    getpid());
				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
				    "sem_wait failed; iteration %d", j);
				printf("PID %d got semaphore\n", getpid());
				_exit(0);
			default:
				children[i] = pid;
				break;
			}
		}

		for (i = 0; i < NCHILDREN; i++) {
			sleep(1);
			printf("main loop %d: posting...\n", j);
			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
		}

		for (i = 0; i < NCHILDREN; i++) {
			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
			ATF_REQUIRE(WIFEXITED(status));
			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
		}
	}

	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
}
ATF_TC_CLEANUP(child, tc)
{
	(void)sem_unlink("/sem_a");
}

ATF_TC_WITH_CLEANUP(pshared);
ATF_TC_HEAD(pshared, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks using pshared unnamed "
	    "semaphores to synchronize a master with multiple slave processes");
}

struct shared_region {
	sem_t	the_sem;
};

static struct shared_region *
get_shared_region(int o_flags)
{

	int fd = shm_open("/shm_semtest_a", o_flags, 0644);
	ATF_REQUIRE(fd != -1);

	ATF_REQUIRE_EQ(ftruncate(fd, sizeof(struct shared_region)), 0);

	void *rv = mmap(NULL, sizeof(struct shared_region),
	    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	ATF_REQUIRE(rv != MAP_FAILED);

	(void)close(fd);

	return rv;
}

static void
put_shared_region(struct shared_region *r)
{
	ATF_REQUIRE_EQ(munmap(r, sizeof(struct shared_region)), 0);
}

ATF_TC_BODY(pshared, tc)
{
	struct shared_region *master_region, *slave_region;

	if (sysconf(_SC_SEMAPHORES) == -1)
		atf_tc_skip("POSIX semaphores not supported");

	/*
	 * Create a shared memory region to contain the pshared
	 * semaphore, create the semaphore there, and then detach
	 * from the shared memory region to ensure that our child
	 * processes will be getting at it from scratch.
	 */
	master_region = get_shared_region(O_RDWR | O_CREAT | O_EXCL);
	ATF_REQUIRE(sem_init(&master_region->the_sem, 1, 0) == 0);
	put_shared_region(master_region);

	/*
	 * Now execute a test that's essentially equivalent to the
	 * "child" test above, but using the pshared semaphore in the
	 * shared memory region.
	 */
	
	pid_t pid, children[NCHILDREN];
	unsigned i, j;
	int status;

	for (j = 1; j <= 2; j++) {
		for (i = 0; i < NCHILDREN; i++) {
			switch ((pid = fork())) {
			case -1:
				atf_tc_fail("fork() returned -1");
			case 0:
				slave_region = get_shared_region(O_RDWR);
				printf("PID %d waiting for semaphore...\n",
				    getpid());
				ATF_REQUIRE_MSG(sem_wait(&slave_region->the_sem)
				    == 0,
				    "sem_wait failed; iteration %d", j);
				printf("PID %d got semaphore\n", getpid());
				_exit(0);
			default:
				children[i] = pid;
				break;
			}
		}

		master_region = get_shared_region(O_RDWR);

		for (i = 0; i < NCHILDREN; i++) {
			sleep(1);
			printf("main loop %d: posting...\n", j);
			ATF_REQUIRE_EQ(sem_post(&master_region->the_sem), 0);
		}

		put_shared_region(master_region);

		for (i = 0; i < NCHILDREN; i++) {
			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
			ATF_REQUIRE(WIFEXITED(status));
			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
		}
	}

	master_region = get_shared_region(O_RDWR);
	ATF_REQUIRE_EQ(sem_destroy(&master_region->the_sem), 0);
	put_shared_region(master_region);

	ATF_REQUIRE_EQ(shm_unlink("/shm_semtest_a"), 0);
}
ATF_TC_CLEANUP(pshared, tc)
{
	/*
	 * The kernel will g/c the pshared semaphore when the process that
	 * created it exits, so no need to include that in the cleanup here.
	 */
	(void)shm_unlink("/shm_semtest_a");
}

ATF_TC_WITH_CLEANUP(invalid_ops);
ATF_TC_HEAD(invalid_ops, tc)
{
	atf_tc_set_md_var(tc, "descr", "Validates behavior when calling "
	    "bad operations for the semaphore type");
}
ATF_TC_BODY(invalid_ops, tc)
{
	sem_t *sem;
	sem_t the_sem;

	sem = sem_open("/sem_c", O_CREAT | O_EXCL, 0644, 0);
	ATF_REQUIRE(sem != SEM_FAILED);
	ATF_REQUIRE(sem_destroy(sem) == -1 && errno == EINVAL);
	ATF_REQUIRE_EQ(sem_close(sem), 0);

	ATF_REQUIRE_EQ(sem_init(&the_sem, 0, 0), 0);
	ATF_REQUIRE(sem_close(&the_sem) == -1 && errno == EINVAL);
	ATF_REQUIRE_EQ(sem_destroy(&the_sem), 0);
}
ATF_TC_CLEANUP(invalid_ops, tc)
{
	(void)sem_unlink("/sem_c");
}

ATF_TC_WITH_CLEANUP(sem_open_address);
ATF_TC_HEAD(sem_open_address, tc)
{
	atf_tc_set_md_var(tc, "descr", "Validate that multiple sem_open calls "
	    "return the same address");
}
ATF_TC_BODY(sem_open_address, tc)
{
	sem_t *sem, *sem2, *sem3;
	atf_tc_expect_fail("kern/56549: consecutive sem_open() do not return the same address");
	sem = sem_open("/sem_d", O_CREAT | O_EXCL, 0777, 0);
	ATF_REQUIRE(sem != SEM_FAILED);
	sem2 = sem_open("/sem_d", O_CREAT | O_EXCL, 0777, 0);
	ATF_REQUIRE(sem2 == SEM_FAILED && errno == EEXIST);
	sem3 = sem_open("/sem_d", 0);
	ATF_REQUIRE(sem3 != SEM_FAILED);
	ATF_REQUIRE(sem == sem3);
	ATF_REQUIRE_EQ(sem_close(sem3), 0);
	ATF_REQUIRE_EQ(sem_close(sem), 0);
	ATF_REQUIRE_EQ(sem_unlink("/sem_d"), 0);
}
ATF_TC_CLEANUP(sem_open_address, tc)
{
	(void)sem_unlink("/sem_d");
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, basic);
	ATF_TP_ADD_TC(tp, child);
	ATF_TP_ADD_TC(tp, pshared);
	ATF_TP_ADD_TC(tp, invalid_ops);
	ATF_TP_ADD_TC(tp, sem_open_address);

	return atf_no_error();
}