]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/librt/t_sem.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / librt / t_sem.c
1 /* $NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly 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.2 2010/11/08 13:05:49 njoly 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(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 #ifdef __FreeBSD__
90         sem_unlink("/sem_b");
91 #endif
92         sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
93         ATF_REQUIRE(sem_b != SEM_FAILED);
94
95         ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
96         ATF_REQUIRE_EQ(val, 0);
97
98         ATF_REQUIRE_EQ(sem_post(sem_b), 0);
99         ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
100         ATF_REQUIRE_EQ(val, 1);
101
102         ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
103         ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
104         ATF_REQUIRE_EQ(errno, EAGAIN);
105         ATF_REQUIRE_EQ(sem_post(sem_b), 0);
106         ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
107         ATF_REQUIRE_EQ(sem_post(sem_b), 0);
108         ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
109         ATF_REQUIRE_EQ(sem_post(sem_b), 0);
110
111         ATF_REQUIRE_EQ(sem_close(sem_b), 0);
112         ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
113 }
114
115 ATF_TC(child);
116 ATF_TC_HEAD(child, tc)
117 {
118         atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
119             "parent with multiple child processes");
120 }
121 ATF_TC_BODY(child, tc)
122 {
123         pid_t children[NCHILDREN];
124         unsigned i, j;
125         sem_t *sem_a;
126         int status;
127
128         pid_t pid;
129
130         if (sysconf(_SC_SEMAPHORES) == -1)         
131                 atf_tc_skip("POSIX semaphores not supported");
132
133 #ifdef __FreeBSD__
134         sem_unlink("/sem_a");
135 #endif
136         sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
137         ATF_REQUIRE(sem_a != SEM_FAILED);
138
139         for (j = 1; j <= 2; j++) {
140                 for (i = 0; i < NCHILDREN; i++) {
141                         switch ((pid = fork())) {
142                         case -1:
143                                 atf_tc_fail("fork() returned -1");
144                         case 0:
145                                 printf("PID %d waiting for semaphore...\n",
146                                     getpid());
147                                 ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
148                                     "sem_wait failed; iteration %d", j);
149                                 printf("PID %d got semaphore\n", getpid());
150                                 _exit(0);
151                         default:
152                                 children[i] = pid;
153                                 break;
154                         }
155                 }
156
157                 for (i = 0; i < NCHILDREN; i++) {
158                         sleep(1);
159                         printf("main loop %d: posting...\n", j);
160                         ATF_REQUIRE_EQ(sem_post(sem_a), 0);
161                 }
162
163                 for (i = 0; i < NCHILDREN; i++) {
164                         ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
165                         ATF_REQUIRE(WIFEXITED(status));
166                         ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
167                 }
168         }
169
170         ATF_REQUIRE_EQ(sem_close(sem_a), 0);
171         ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
172 }
173
174 ATF_TP_ADD_TCS(tp)
175 {
176
177         ATF_TP_ADD_TC(tp, basic);
178         ATF_TP_ADD_TC(tp, child);
179
180         return atf_no_error();
181 }