t_semtimedop.c revision 1.1 1 1.1 christos /* $NetBSD: t_semtimedop.c,v 1.1 2024/10/03 17:11:14 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos #include <sys/cdefs.h>
29 1.1 christos __RCSID("$NetBSD: t_semtimedop.c,v 1.1 2024/10/03 17:11:14 christos Exp $");
30 1.1 christos
31 1.1 christos #include <sys/types.h>
32 1.1 christos #include <sys/ipc.h>
33 1.1 christos #include <sys/sem.h>
34 1.1 christos #include <errno.h>
35 1.1 christos #include <string.h>
36 1.1 christos #include <time.h>
37 1.1 christos #include <stdlib.h>
38 1.1 christos #include <stdio.h>
39 1.1 christos #include <unistd.h>
40 1.1 christos #include <atf-c.h>
41 1.1 christos #include <sys/wait.h>
42 1.1 christos
43 1.1 christos ATF_TC(semtimedop_basic);
44 1.1 christos ATF_TC_HEAD(semtimedop_basic, tc)
45 1.1 christos {
46 1.1 christos atf_tc_set_md_var(tc, "descr", "Basic semtimedop functionality");
47 1.1 christos }
48 1.1 christos
49 1.1 christos ATF_TC_BODY(semtimedop_basic, tc)
50 1.1 christos {
51 1.1 christos key_t key = IPC_PRIVATE;
52 1.1 christos int semid;
53 1.1 christos struct sembuf sops;
54 1.1 christos struct timespec timeout;
55 1.1 christos
56 1.1 christos /* Create semaphore set with 1 semaphore */
57 1.1 christos semid = semget(key, 1, IPC_CREAT | 0600);
58 1.1 christos ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
59 1.1 christos
60 1.1 christos /* Initialize semaphore to 0 */
61 1.1 christos if (semctl(semid, 0, SETVAL, 0) == -1) {
62 1.1 christos ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s",
63 1.1 christos strerror(errno));
64 1.1 christos }
65 1.1 christos
66 1.1 christos /* Define semtimedop operation: increment semaphore */
67 1.1 christos sops.sem_num = 0;
68 1.1 christos sops.sem_op = 1;
69 1.1 christos sops.sem_flg = 0;
70 1.1 christos
71 1.1 christos /* Define timeout */
72 1.1 christos timeout.tv_sec = 1;
73 1.1 christos timeout.tv_nsec = 0;
74 1.1 christos
75 1.1 christos /* Perform semtimedop */
76 1.1 christos if (semtimedop(semid, &sops, 1, &timeout) == -1) {
77 1.1 christos ATF_REQUIRE_MSG(0, "semtimedop failed: %s", strerror(errno));
78 1.1 christos }
79 1.1 christos
80 1.1 christos /* Check semaphore value */
81 1.1 christos int val = semctl(semid, 0, GETVAL);
82 1.1 christos ATF_REQUIRE_MSG(val == 1,
83 1.1 christos "Semaphore value incorrect: got %d, expected 1", val);
84 1.1 christos
85 1.1 christos /* Clean up */
86 1.1 christos ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
87 1.1 christos "semctl IPC_RMID failed: %s", strerror(errno));
88 1.1 christos }
89 1.1 christos
90 1.1 christos /* semtimedop blocks until timeout expires */
91 1.1 christos ATF_TC(semtimedop_timeout);
92 1.1 christos ATF_TC_HEAD(semtimedop_timeout, tc)
93 1.1 christos {
94 1.1 christos atf_tc_set_md_var(tc, "descr",
95 1.1 christos "semtimedop blocks until timeout expires");
96 1.1 christos }
97 1.1 christos
98 1.1 christos ATF_TC_BODY(semtimedop_timeout, tc)
99 1.1 christos {
100 1.1 christos key_t key = IPC_PRIVATE;
101 1.1 christos int semid;
102 1.1 christos struct sembuf sops;
103 1.1 christos struct timespec timeout;
104 1.1 christos pid_t pid;
105 1.1 christos int status;
106 1.1 christos
107 1.1 christos /* Create semaphore set with 1 semaphore */
108 1.1 christos semid = semget(key, 1, IPC_CREAT | 0600);
109 1.1 christos ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
110 1.1 christos
111 1.1 christos /* Initialize semaphore to 0 */
112 1.1 christos if (semctl(semid, 0, SETVAL, 0) == -1) {
113 1.1 christos ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
114 1.1 christos }
115 1.1 christos
116 1.1 christos pid = fork();
117 1.1 christos ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
118 1.1 christos
119 1.1 christos if (pid == 0) {
120 1.1 christos /*
121 1.1 christos * Child: perform semtimedop with negative sem_op, should
122 1.1 christos * block until timeout
123 1.1 christos */
124 1.1 christos sops.sem_num = 0;
125 1.1 christos sops.sem_op = -1;
126 1.1 christos sops.sem_flg = 0;
127 1.1 christos
128 1.1 christos timeout.tv_sec = 2;
129 1.1 christos timeout.tv_nsec = 0;
130 1.1 christos
131 1.1 christos if (semtimedop(semid, &sops, 1, &timeout) == -1) {
132 1.1 christos if (errno == EAGAIN || errno == EINTR) {
133 1.1 christos exit(0); /* Expected */
134 1.1 christos }
135 1.1 christos }
136 1.1 christos exit(1); /* Unexpected failure/success */
137 1.1 christos }
138 1.1 christos
139 1.1 christos /* Parent: wait for child to finish */
140 1.1 christos waitpid(pid, &status, 0);
141 1.1 christos ATF_REQUIRE(WIFEXITED(status));
142 1.1 christos ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
143 1.1 christos
144 1.1 christos /* Clean up */
145 1.1 christos ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
146 1.1 christos "semctl IPC_RMID failed: %s", strerror(errno));
147 1.1 christos }
148 1.1 christos
149 1.1 christos /* semtimedop with SEM_UNDO adjusts semaphore on exit */
150 1.1 christos ATF_TC(semtimedop_semundo);
151 1.1 christos ATF_TC_HEAD(semtimedop_semundo, tc)
152 1.1 christos {
153 1.1 christos atf_tc_set_md_var(tc, "descr",
154 1.1 christos "semtimedop with SEM_UNDO adjusts semaphore on exit");
155 1.1 christos }
156 1.1 christos
157 1.1 christos ATF_TC_BODY(semtimedop_semundo, tc)
158 1.1 christos {
159 1.1 christos key_t key = IPC_PRIVATE;
160 1.1 christos int semid;
161 1.1 christos struct sembuf sops;
162 1.1 christos struct timespec timeout;
163 1.1 christos pid_t pid;
164 1.1 christos int val;
165 1.1 christos
166 1.1 christos /* Create semaphore set with 1 semaphore */
167 1.1 christos semid = semget(key, 1, IPC_CREAT | 0600);
168 1.1 christos ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
169 1.1 christos
170 1.1 christos /* Initialize semaphore to 0 */
171 1.1 christos if (semctl(semid, 0, SETVAL, 0) == -1) {
172 1.1 christos ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
173 1.1 christos }
174 1.1 christos
175 1.1 christos pid = fork();
176 1.1 christos ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
177 1.1 christos
178 1.1 christos if (pid == 0) {
179 1.1 christos /* Child: perform semtimedop with SEM_UNDO */
180 1.1 christos sops.sem_num = 0;
181 1.1 christos sops.sem_op = 1;
182 1.1 christos sops.sem_flg = SEM_UNDO;
183 1.1 christos
184 1.1 christos timeout.tv_sec = 1;
185 1.1 christos timeout.tv_nsec = 0;
186 1.1 christos
187 1.1 christos if (semtimedop(semid, &sops, 1, &timeout) == -1) {
188 1.1 christos exit(1); /* Unexpected failure */
189 1.1 christos }
190 1.1 christos
191 1.1 christos exit(0); /* Exit normally, SEM_UNDO should be triggered */
192 1.1 christos }
193 1.1 christos
194 1.1 christos /* Parent: wait for child to exit */
195 1.1 christos waitpid(pid, NULL, 0);
196 1.1 christos
197 1.1 christos /* Check semaphore value; should be 0 after SEM_UNDO */
198 1.1 christos val = semctl(semid, 0, GETVAL);
199 1.1 christos ATF_REQUIRE_MSG(val == 0,
200 1.1 christos "Semaphore value incorrect after SEM_UNDO: got %d, "
201 1.1 christos "expected 0", val);
202 1.1 christos
203 1.1 christos /* Clean up */
204 1.1 christos ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
205 1.1 christos "semctl IPC_RMID failed: %s", strerror(errno));
206 1.1 christos }
207 1.1 christos
208 1.1 christos /* semtimedop handles invalid parameters correctly */
209 1.1 christos ATF_TC(semtimedop_invalid);
210 1.1 christos ATF_TC_HEAD(semtimedop_invalid, tc)
211 1.1 christos {
212 1.1 christos atf_tc_set_md_var(tc, "descr",
213 1.1 christos "semtimedop handles invalid parameters correctly");
214 1.1 christos }
215 1.1 christos
216 1.1 christos ATF_TC_BODY(semtimedop_invalid, tc)
217 1.1 christos {
218 1.1 christos struct sembuf sops;
219 1.1 christos struct timespec timeout;
220 1.1 christos
221 1.1 christos /* Invalid semaphore id */
222 1.1 christos sops.sem_num = 0;
223 1.1 christos sops.sem_op = -1;
224 1.1 christos sops.sem_flg = 0;
225 1.1 christos
226 1.1 christos timeout.tv_sec = 1;
227 1.1 christos timeout.tv_nsec = 0;
228 1.1 christos
229 1.1 christos /* Attempt to perform semtimedop on invalid semid */
230 1.1 christos ATF_REQUIRE_MSG(semtimedop(-1, &sops, 1, &timeout) == -1
231 1.1 christos && errno == EINVAL, "semtimedop did not fail on invalid semid");
232 1.1 christos
233 1.1 christos /* Create semaphore set */
234 1.1 christos key_t key = IPC_PRIVATE;
235 1.1 christos int semid = semget(key, 1, IPC_CREAT | 0600);
236 1.1 christos ATF_REQUIRE_MSG(semid != -1, "semget failed: %s", strerror(errno));
237 1.1 christos
238 1.1 christos /* Initialize semaphore to 0 */
239 1.1 christos if (semctl(semid, 0, SETVAL, 0) == -1) {
240 1.1 christos ATF_REQUIRE_MSG(0, "semctl SETVAL failed: %s", strerror(errno));
241 1.1 christos }
242 1.1 christos
243 1.1 christos /* Set an invalid semaphore number */
244 1.1 christos sops.sem_num = 1; /* Only 1 semaphore in set, index 0 */
245 1.1 christos sops.sem_op = 1;
246 1.1 christos sops.sem_flg = 0;
247 1.1 christos
248 1.1 christos /* Attempt semtimedop with invalid sem_num */
249 1.1 christos ATF_REQUIRE_MSG(semtimedop(semid, &sops, 1, &timeout) == -1
250 1.1 christos && errno == EFBIG, "semtimedop did not fail on invalid sem_num");
251 1.1 christos
252 1.1 christos /* Clean up */
253 1.1 christos ATF_REQUIRE_MSG(semctl(semid, 0, IPC_RMID) != -1,
254 1.1 christos "semctl IPC_RMID failed: %s", strerror(errno));
255 1.1 christos }
256 1.1 christos
257 1.1 christos ATF_TP_ADD_TCS(tp)
258 1.1 christos {
259 1.1 christos ATF_TP_ADD_TC(tp, semtimedop_basic);
260 1.1 christos ATF_TP_ADD_TC(tp, semtimedop_timeout);
261 1.1 christos ATF_TP_ADD_TC(tp, semtimedop_semundo);
262 1.1 christos ATF_TP_ADD_TC(tp, semtimedop_invalid);
263 1.1 christos
264 1.1 christos return atf_no_error();
265 1.1 christos }
266