xref: /freebsd/contrib/netbsd-tests/lib/librt/t_sem.c (revision 3b0a9131e6cb4ada420cbfc4e2d89a04ca7ad384)
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/wait.h>
64 
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <semaphore.h>
68 #include <stdio.h>
69 #include <unistd.h>
70 
71 #include <atf-c.h>
72 
73 #define NCHILDREN 10
74 
75 ATF_TC_WITH_CLEANUP(basic);
76 ATF_TC_HEAD(basic, tc)
77 {
78 	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
79 	    "semaphores");
80 }
81 ATF_TC_BODY(basic, tc)
82 {
83 	int val;
84 	sem_t *sem_b;
85 
86 	if (sysconf(_SC_SEMAPHORES) == -1)
87 		atf_tc_skip("POSIX semaphores not supported");
88 
89 	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
90 	ATF_REQUIRE(sem_b != SEM_FAILED);
91 
92 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
93 	ATF_REQUIRE_EQ(val, 0);
94 
95 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
96 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
97 	ATF_REQUIRE_EQ(val, 1);
98 
99 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
100 	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
101 	ATF_REQUIRE_EQ(errno, EAGAIN);
102 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
103 	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
104 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
105 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
106 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
107 
108 	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
109 	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
110 }
111 ATF_TC_CLEANUP(basic, tc)
112 {
113 	(void)sem_unlink("/sem_b");
114 }
115 
116 ATF_TC_WITH_CLEANUP(child);
117 ATF_TC_HEAD(child, tc)
118 {
119 	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
120 	    "parent with multiple child processes");
121 }
122 ATF_TC_BODY(child, tc)
123 {
124 	pid_t children[NCHILDREN];
125 	unsigned i, j;
126 	sem_t *sem_a;
127 	int status;
128 
129 	pid_t pid;
130 
131 	if (sysconf(_SC_SEMAPHORES) == -1)
132 		atf_tc_skip("POSIX semaphores not supported");
133 
134 	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
135 	ATF_REQUIRE(sem_a != SEM_FAILED);
136 
137 	for (j = 1; j <= 2; j++) {
138 		for (i = 0; i < NCHILDREN; i++) {
139 			switch ((pid = fork())) {
140 			case -1:
141 				atf_tc_fail("fork() returned -1");
142 			case 0:
143 				printf("PID %d waiting for semaphore...\n",
144 				    getpid());
145 				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
146 				    "sem_wait failed; iteration %d", j);
147 				printf("PID %d got semaphore\n", getpid());
148 				_exit(0);
149 			default:
150 				children[i] = pid;
151 				break;
152 			}
153 		}
154 
155 		for (i = 0; i < NCHILDREN; i++) {
156 			sleep(1);
157 			printf("main loop %d: posting...\n", j);
158 			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
159 		}
160 
161 		for (i = 0; i < NCHILDREN; i++) {
162 			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
163 			ATF_REQUIRE(WIFEXITED(status));
164 			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
165 		}
166 	}
167 
168 	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
169 	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
170 }
171 ATF_TC_CLEANUP(child, tc)
172 {
173 	(void)sem_unlink("/sem_a");
174 }
175 
176 ATF_TP_ADD_TCS(tp)
177 {
178 
179 	ATF_TP_ADD_TC(tp, basic);
180 	ATF_TP_ADD_TC(tp, child);
181 
182 	return atf_no_error();
183 }
184