t_sysv.c revision 1.4 1 1.4 jmmv /* $NetBSD: t_sysv.c,v 1.4 2014/03/02 20:13:12 jmmv Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pgoyette * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 pgoyette * NASA Ames Research Center, and by Andrew Doran.
10 1.1 pgoyette *
11 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
12 1.1 pgoyette * modification, are permitted provided that the following conditions
13 1.1 pgoyette * are met:
14 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
15 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
16 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
18 1.1 pgoyette * documentation and/or other materials provided with the distribution.
19 1.1 pgoyette *
20 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
31 1.1 pgoyette */
32 1.1 pgoyette
33 1.1 pgoyette /*
34 1.1 pgoyette * Test the SVID-compatible Message Queue facility.
35 1.1 pgoyette */
36 1.1 pgoyette
37 1.1 pgoyette #include <atf-c.h>
38 1.1 pgoyette
39 1.1 pgoyette #include <err.h>
40 1.1 pgoyette #include <errno.h>
41 1.1 pgoyette #include <fcntl.h>
42 1.1 pgoyette #include <signal.h>
43 1.1 pgoyette #include <stdio.h>
44 1.1 pgoyette #include <stdlib.h>
45 1.1 pgoyette #include <string.h>
46 1.1 pgoyette #include <time.h>
47 1.1 pgoyette #include <unistd.h>
48 1.1 pgoyette
49 1.1 pgoyette #include <sys/ipc.h>
50 1.1 pgoyette #include <sys/msg.h>
51 1.1 pgoyette #include <sys/param.h>
52 1.1 pgoyette #include <sys/sem.h>
53 1.1 pgoyette #include <sys/shm.h>
54 1.1 pgoyette #include <sys/wait.h>
55 1.1 pgoyette
56 1.1 pgoyette volatile int did_sigsys, did_sigchild;
57 1.1 pgoyette volatile int child_status, child_count;
58 1.1 pgoyette
59 1.1 pgoyette void sigsys_handler(int);
60 1.1 pgoyette void sigchld_handler(int);
61 1.1 pgoyette
62 1.2 pgoyette key_t get_ftok(int);
63 1.2 pgoyette
64 1.1 pgoyette void print_msqid_ds(struct msqid_ds *, mode_t);
65 1.1 pgoyette void receiver(void);
66 1.1 pgoyette
67 1.1 pgoyette void print_semid_ds(struct semid_ds *, mode_t);
68 1.1 pgoyette void waiter(void);
69 1.1 pgoyette
70 1.1 pgoyette void print_shmid_ds(struct shmid_ds *, mode_t);
71 1.1 pgoyette void sharer(void);
72 1.1 pgoyette
73 1.1 pgoyette #define MESSAGE_TEXT_LEN 256
74 1.1 pgoyette
75 1.1 pgoyette struct mymsg {
76 1.1 pgoyette long mtype;
77 1.1 pgoyette char mtext[MESSAGE_TEXT_LEN];
78 1.1 pgoyette };
79 1.1 pgoyette
80 1.1 pgoyette const char *m1_str = "California is overrated.";
81 1.1 pgoyette const char *m2_str = "The quick brown fox jumped over the lazy dog.";
82 1.1 pgoyette
83 1.1 pgoyette size_t pgsize;
84 1.1 pgoyette
85 1.1 pgoyette #define MTYPE_1 1
86 1.1 pgoyette #define MTYPE_1_ACK 2
87 1.1 pgoyette
88 1.1 pgoyette #define MTYPE_2 3
89 1.1 pgoyette #define MTYPE_2_ACK 4
90 1.1 pgoyette
91 1.1 pgoyette pid_t child_pid;
92 1.1 pgoyette
93 1.1 pgoyette key_t msgkey, semkey, shmkey;
94 1.1 pgoyette
95 1.1 pgoyette int maxloop = 1;
96 1.1 pgoyette
97 1.1 pgoyette union semun {
98 1.1 pgoyette int val; /* value for SETVAL */
99 1.1 pgoyette struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */
100 1.1 pgoyette u_short *array; /* array for GETALL & SETALL */
101 1.1 pgoyette };
102 1.1 pgoyette
103 1.1 pgoyette
104 1.4 jmmv /* Writes an integer to a file. To be used from the body of the test
105 1.4 jmmv * cases below to pass any global identifiers to the cleanup routine. */
106 1.4 jmmv static void
107 1.4 jmmv write_int(const char *path, const int value)
108 1.4 jmmv {
109 1.4 jmmv int output;
110 1.4 jmmv
111 1.4 jmmv output = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
112 1.4 jmmv ATF_REQUIRE_MSG(output != -1, "Failed to create %s", path);
113 1.4 jmmv write(output, &value, sizeof(value));
114 1.4 jmmv close(output);
115 1.4 jmmv }
116 1.4 jmmv
117 1.4 jmmv
118 1.4 jmmv /* Reads an integer from a file. To be used from the cleanup routines
119 1.4 jmmv * of the test cases below. */
120 1.4 jmmv static int
121 1.4 jmmv read_int(const char *path)
122 1.4 jmmv {
123 1.4 jmmv int input;
124 1.4 jmmv
125 1.4 jmmv input = open(path, O_RDONLY);
126 1.4 jmmv if (input == -1)
127 1.4 jmmv return -1;
128 1.4 jmmv else {
129 1.4 jmmv int value;
130 1.4 jmmv read(input, &value, sizeof(value));
131 1.4 jmmv return value;
132 1.4 jmmv }
133 1.4 jmmv }
134 1.4 jmmv
135 1.4 jmmv
136 1.1 pgoyette void
137 1.1 pgoyette sigsys_handler(int signo)
138 1.1 pgoyette {
139 1.1 pgoyette
140 1.1 pgoyette did_sigsys = 1;
141 1.1 pgoyette }
142 1.1 pgoyette
143 1.1 pgoyette void
144 1.1 pgoyette sigchld_handler(int signo)
145 1.1 pgoyette {
146 1.1 pgoyette int c_status;
147 1.1 pgoyette
148 1.1 pgoyette did_sigchild = 1;
149 1.1 pgoyette /*
150 1.1 pgoyette * Reap the child and return its status
151 1.1 pgoyette */
152 1.1 pgoyette if (wait(&c_status) == -1)
153 1.1 pgoyette child_status = -errno;
154 1.1 pgoyette else
155 1.1 pgoyette child_status = c_status;
156 1.1 pgoyette
157 1.1 pgoyette child_count--;
158 1.1 pgoyette }
159 1.1 pgoyette
160 1.2 pgoyette key_t get_ftok(int id)
161 1.2 pgoyette {
162 1.2 pgoyette int fd;
163 1.2 pgoyette char token_key[64], token_dir[64];
164 1.2 pgoyette char *tmpdir;
165 1.2 pgoyette key_t key;
166 1.2 pgoyette
167 1.2 pgoyette strlcpy(token_key, "/tmp/t_sysv.XXXXXX", sizeof(token_key));
168 1.2 pgoyette tmpdir = mkdtemp(token_key);
169 1.2 pgoyette ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp() failed: %d", errno);
170 1.2 pgoyette
171 1.2 pgoyette strlcpy(token_dir, tmpdir, sizeof(token_dir));
172 1.2 pgoyette strlcpy(token_key, tmpdir, sizeof(token_key));
173 1.2 pgoyette strlcat(token_key, "/token_key", sizeof(token_key));
174 1.2 pgoyette
175 1.2 pgoyette /* Create the file, since ftok() requires it to exist! */
176 1.2 pgoyette
177 1.2 pgoyette fd = open(token_key, O_RDWR | O_CREAT | O_EXCL);
178 1.2 pgoyette if (fd == -1) {
179 1.2 pgoyette rmdir(tmpdir);
180 1.2 pgoyette atf_tc_fail("open() of temp file failed: %d", errno);
181 1.2 pgoyette return (key_t)-1;
182 1.2 pgoyette } else
183 1.2 pgoyette close(fd);
184 1.2 pgoyette
185 1.2 pgoyette key = ftok(token_key, id);
186 1.2 pgoyette
187 1.2 pgoyette ATF_REQUIRE_MSG(unlink(token_key) != -1, "unlink() failed: %d", errno);
188 1.2 pgoyette ATF_REQUIRE_MSG(rmdir(token_dir) != -1, "rmdir() failed: %d", errno);
189 1.2 pgoyette
190 1.2 pgoyette return key;
191 1.2 pgoyette }
192 1.2 pgoyette
193 1.2 pgoyette ATF_TC_WITH_CLEANUP(msg);
194 1.1 pgoyette ATF_TC_HEAD(msg, tc)
195 1.1 pgoyette {
196 1.1 pgoyette
197 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "3");
198 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
199 1.1 pgoyette }
200 1.1 pgoyette
201 1.1 pgoyette ATF_TC_BODY(msg, tc)
202 1.1 pgoyette {
203 1.1 pgoyette struct sigaction sa;
204 1.1 pgoyette struct msqid_ds m_ds;
205 1.1 pgoyette struct mymsg m;
206 1.1 pgoyette sigset_t sigmask;
207 1.4 jmmv int sender_msqid;
208 1.1 pgoyette int loop;
209 1.1 pgoyette int c_status;
210 1.1 pgoyette
211 1.1 pgoyette /*
212 1.1 pgoyette * Install a SIGSYS handler so that we can exit gracefully if
213 1.1 pgoyette * System V Message Queue support isn't in the kernel.
214 1.1 pgoyette */
215 1.1 pgoyette did_sigsys = 0;
216 1.1 pgoyette sa.sa_handler = sigsys_handler;
217 1.1 pgoyette sigemptyset(&sa.sa_mask);
218 1.1 pgoyette sa.sa_flags = 0;
219 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
220 1.1 pgoyette "sigaction SIGSYS: %d", errno);
221 1.1 pgoyette
222 1.1 pgoyette /*
223 1.1 pgoyette * Install a SIGCHLD handler to deal with all possible exit
224 1.1 pgoyette * conditions of the receiver.
225 1.1 pgoyette */
226 1.1 pgoyette did_sigchild = 0;
227 1.1 pgoyette child_count = 0;
228 1.1 pgoyette sa.sa_handler = sigchld_handler;
229 1.1 pgoyette sigemptyset(&sa.sa_mask);
230 1.1 pgoyette sa.sa_flags = 0;
231 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
232 1.1 pgoyette "sigaction SIGCHLD: %d", errno);
233 1.1 pgoyette
234 1.2 pgoyette msgkey = get_ftok(4160);
235 1.2 pgoyette ATF_REQUIRE_MSG(msgkey != (key_t)-1, "get_ftok failed");
236 1.1 pgoyette
237 1.1 pgoyette sender_msqid = msgget(msgkey, IPC_CREAT | 0640);
238 1.1 pgoyette ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno);
239 1.4 jmmv write_int("sender_msqid", sender_msqid);
240 1.1 pgoyette
241 1.1 pgoyette if (did_sigsys) {
242 1.1 pgoyette atf_tc_skip("SYSV Message Queue not supported");
243 1.1 pgoyette return;
244 1.1 pgoyette }
245 1.1 pgoyette
246 1.1 pgoyette ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
247 1.1 pgoyette "msgctl IPC_STAT 1: %d", errno);
248 1.1 pgoyette
249 1.1 pgoyette print_msqid_ds(&m_ds, 0640);
250 1.1 pgoyette
251 1.1 pgoyette m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
252 1.1 pgoyette
253 1.1 pgoyette ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_SET, &m_ds) != -1,
254 1.1 pgoyette "msgctl IPC_SET: %d", errno);
255 1.1 pgoyette
256 1.1 pgoyette memset(&m_ds, 0, sizeof(m_ds));
257 1.1 pgoyette
258 1.1 pgoyette ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
259 1.1 pgoyette "msgctl IPC_STAT 2: %d", errno);
260 1.1 pgoyette
261 1.1 pgoyette ATF_REQUIRE_MSG((m_ds.msg_perm.mode & 0777) == 0600,
262 1.1 pgoyette "IPC_SET of mode didn't hold");
263 1.1 pgoyette
264 1.1 pgoyette print_msqid_ds(&m_ds, 0600);
265 1.1 pgoyette
266 1.1 pgoyette switch ((child_pid = fork())) {
267 1.1 pgoyette case -1:
268 1.1 pgoyette atf_tc_fail("fork: %d", errno);
269 1.1 pgoyette return;
270 1.1 pgoyette
271 1.1 pgoyette case 0:
272 1.1 pgoyette child_count++;
273 1.1 pgoyette receiver();
274 1.1 pgoyette break;
275 1.1 pgoyette
276 1.1 pgoyette default:
277 1.1 pgoyette break;
278 1.1 pgoyette }
279 1.1 pgoyette
280 1.1 pgoyette for (loop = 0; loop < maxloop; loop++) {
281 1.1 pgoyette /*
282 1.1 pgoyette * Send the first message to the receiver and wait for the ACK.
283 1.1 pgoyette */
284 1.1 pgoyette m.mtype = MTYPE_1;
285 1.1 pgoyette strcpy(m.mtext, m1_str);
286 1.3 skrll ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN,
287 1.3 skrll 0) != -1, "sender: msgsnd 1: %d", errno);
288 1.1 pgoyette
289 1.3 skrll ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
290 1.3 skrll MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
291 1.1 pgoyette "sender: msgrcv 1 ack: %d", errno);
292 1.1 pgoyette
293 1.1 pgoyette print_msqid_ds(&m_ds, 0600);
294 1.1 pgoyette
295 1.1 pgoyette /*
296 1.1 pgoyette * Send the second message to the receiver and wait for the ACK.
297 1.1 pgoyette */
298 1.1 pgoyette m.mtype = MTYPE_2;
299 1.1 pgoyette strcpy(m.mtext, m2_str);
300 1.3 skrll ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0) != -1,
301 1.1 pgoyette "sender: msgsnd 2: %d", errno);
302 1.1 pgoyette
303 1.3 skrll ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
304 1.3 skrll MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
305 1.1 pgoyette "sender: msgrcv 2 ack: %d", errno);
306 1.1 pgoyette }
307 1.1 pgoyette
308 1.1 pgoyette /*
309 1.1 pgoyette * Wait for child to finish
310 1.1 pgoyette */
311 1.1 pgoyette sigemptyset(&sigmask);
312 1.1 pgoyette (void) sigsuspend(&sigmask);
313 1.1 pgoyette
314 1.1 pgoyette /*
315 1.1 pgoyette * ...and any other signal is an unexpected error.
316 1.1 pgoyette */
317 1.1 pgoyette if (did_sigchild) {
318 1.1 pgoyette c_status = child_status;
319 1.1 pgoyette if (c_status < 0)
320 1.1 pgoyette atf_tc_fail("waitpid: %d", -c_status);
321 1.1 pgoyette else if (WIFEXITED(c_status) == 0)
322 1.1 pgoyette atf_tc_fail("child abnormal exit: %d", c_status);
323 1.1 pgoyette else if (WEXITSTATUS(c_status) != 0)
324 1.1 pgoyette atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
325 1.1 pgoyette else {
326 1.1 pgoyette ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds)
327 1.1 pgoyette != -1, "msgctl IPC_STAT: %d", errno);
328 1.1 pgoyette
329 1.1 pgoyette print_msqid_ds(&m_ds, 0600);
330 1.1 pgoyette atf_tc_pass();
331 1.1 pgoyette }
332 1.1 pgoyette } else
333 1.1 pgoyette atf_tc_fail("sender: received unexpected signal");
334 1.1 pgoyette }
335 1.1 pgoyette
336 1.2 pgoyette ATF_TC_CLEANUP(msg, tc)
337 1.1 pgoyette {
338 1.4 jmmv int sender_msqid;
339 1.1 pgoyette
340 1.1 pgoyette /*
341 1.2 pgoyette * Remove the message queue if it exists.
342 1.1 pgoyette */
343 1.4 jmmv sender_msqid = read_int("sender_msqid");
344 1.2 pgoyette if (sender_msqid != -1)
345 1.4 jmmv if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
346 1.4 jmmv err(1, "msgctl IPC_RMID");
347 1.1 pgoyette }
348 1.1 pgoyette
349 1.1 pgoyette void
350 1.1 pgoyette print_msqid_ds(mp, mode)
351 1.1 pgoyette struct msqid_ds *mp;
352 1.1 pgoyette mode_t mode;
353 1.1 pgoyette {
354 1.1 pgoyette uid_t uid = geteuid();
355 1.1 pgoyette gid_t gid = getegid();
356 1.1 pgoyette
357 1.1 pgoyette printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
358 1.1 pgoyette mp->msg_perm.uid, mp->msg_perm.gid,
359 1.1 pgoyette mp->msg_perm.cuid, mp->msg_perm.cgid,
360 1.1 pgoyette mp->msg_perm.mode & 0777);
361 1.1 pgoyette
362 1.1 pgoyette printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
363 1.1 pgoyette mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
364 1.1 pgoyette mp->msg_lrpid);
365 1.1 pgoyette
366 1.1 pgoyette printf("stime: %s", ctime(&mp->msg_stime));
367 1.1 pgoyette printf("rtime: %s", ctime(&mp->msg_rtime));
368 1.1 pgoyette printf("ctime: %s", ctime(&mp->msg_ctime));
369 1.1 pgoyette
370 1.1 pgoyette /*
371 1.1 pgoyette * Sanity check a few things.
372 1.1 pgoyette */
373 1.1 pgoyette
374 1.1 pgoyette ATF_REQUIRE_MSG(mp->msg_perm.uid == uid && mp->msg_perm.cuid == uid,
375 1.1 pgoyette "uid mismatch");
376 1.1 pgoyette
377 1.1 pgoyette ATF_REQUIRE_MSG(mp->msg_perm.gid == gid && mp->msg_perm.cgid == gid,
378 1.1 pgoyette "gid mismatch");
379 1.1 pgoyette
380 1.1 pgoyette ATF_REQUIRE_MSG((mp->msg_perm.mode & 0777) == mode, "mode mismatch");
381 1.1 pgoyette }
382 1.1 pgoyette
383 1.1 pgoyette void
384 1.1 pgoyette receiver()
385 1.1 pgoyette {
386 1.1 pgoyette struct mymsg m;
387 1.1 pgoyette int msqid, loop;
388 1.1 pgoyette
389 1.1 pgoyette if ((msqid = msgget(msgkey, 0)) == -1)
390 1.1 pgoyette err(1, "receiver: msgget");
391 1.1 pgoyette
392 1.1 pgoyette for (loop = 0; loop < maxloop; loop++) {
393 1.1 pgoyette /*
394 1.1 pgoyette * Receive the first message, print it, and send an ACK.
395 1.1 pgoyette */
396 1.3 skrll if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0) != MESSAGE_TEXT_LEN)
397 1.1 pgoyette err(1, "receiver: msgrcv 1");
398 1.1 pgoyette
399 1.1 pgoyette printf("%s\n", m.mtext);
400 1.1 pgoyette if (strcmp(m.mtext, m1_str) != 0)
401 1.1 pgoyette err(1, "receiver: message 1 data isn't correct");
402 1.1 pgoyette
403 1.1 pgoyette m.mtype = MTYPE_1_ACK;
404 1.1 pgoyette
405 1.3 skrll if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
406 1.1 pgoyette err(1, "receiver: msgsnd ack 1");
407 1.1 pgoyette
408 1.1 pgoyette /*
409 1.1 pgoyette * Receive the second message, print it, and send an ACK.
410 1.1 pgoyette */
411 1.1 pgoyette
412 1.3 skrll if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0) != MESSAGE_TEXT_LEN)
413 1.1 pgoyette err(1, "receiver: msgrcv 2");
414 1.1 pgoyette
415 1.1 pgoyette printf("%s\n", m.mtext);
416 1.1 pgoyette if (strcmp(m.mtext, m2_str) != 0)
417 1.1 pgoyette err(1, "receiver: message 2 data isn't correct");
418 1.1 pgoyette
419 1.1 pgoyette m.mtype = MTYPE_2_ACK;
420 1.1 pgoyette
421 1.3 skrll if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
422 1.1 pgoyette err(1, "receiver: msgsnd ack 2");
423 1.1 pgoyette }
424 1.1 pgoyette
425 1.1 pgoyette exit(0);
426 1.1 pgoyette }
427 1.1 pgoyette
428 1.1 pgoyette /*
429 1.1 pgoyette * Test the SVID-compatible Semaphore facility.
430 1.1 pgoyette */
431 1.1 pgoyette
432 1.2 pgoyette ATF_TC_WITH_CLEANUP(sem);
433 1.1 pgoyette ATF_TC_HEAD(sem, tc)
434 1.1 pgoyette {
435 1.1 pgoyette
436 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "3");
437 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
438 1.1 pgoyette }
439 1.1 pgoyette
440 1.1 pgoyette ATF_TC_BODY(sem, tc)
441 1.1 pgoyette {
442 1.1 pgoyette struct sigaction sa;
443 1.1 pgoyette union semun sun;
444 1.1 pgoyette struct semid_ds s_ds;
445 1.1 pgoyette sigset_t sigmask;
446 1.4 jmmv int sender_semid;
447 1.1 pgoyette int i;
448 1.1 pgoyette int c_status;
449 1.1 pgoyette
450 1.1 pgoyette /*
451 1.1 pgoyette * Install a SIGSYS handler so that we can exit gracefully if
452 1.1 pgoyette * System V Semaphore support isn't in the kernel.
453 1.1 pgoyette */
454 1.1 pgoyette did_sigsys = 0;
455 1.1 pgoyette sa.sa_handler = sigsys_handler;
456 1.1 pgoyette sigemptyset(&sa.sa_mask);
457 1.1 pgoyette sa.sa_flags = 0;
458 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
459 1.1 pgoyette "sigaction SIGSYS: %d", errno);
460 1.1 pgoyette
461 1.1 pgoyette /*
462 1.1 pgoyette * Install a SIGCHLD handler to deal with all possible exit
463 1.1 pgoyette * conditions of the receiver.
464 1.1 pgoyette */
465 1.1 pgoyette did_sigchild = 0;
466 1.1 pgoyette child_count = 0;
467 1.1 pgoyette sa.sa_handler = sigchld_handler;
468 1.1 pgoyette sigemptyset(&sa.sa_mask);
469 1.1 pgoyette sa.sa_flags = 0;
470 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
471 1.1 pgoyette "sigaction SIGCHLD: %d", errno);
472 1.1 pgoyette
473 1.2 pgoyette semkey = get_ftok(4160);
474 1.2 pgoyette ATF_REQUIRE_MSG(semkey != (key_t)-1, "get_ftok failed");
475 1.1 pgoyette
476 1.1 pgoyette sender_semid = semget(semkey, 1, IPC_CREAT | 0640);
477 1.1 pgoyette ATF_REQUIRE_MSG(sender_semid != -1, "semget: %d", errno);
478 1.4 jmmv write_int("sender_semid", sender_semid);
479 1.1 pgoyette
480 1.1 pgoyette if (did_sigsys) {
481 1.1 pgoyette atf_tc_skip("SYSV Semaphore not supported");
482 1.1 pgoyette return;
483 1.1 pgoyette }
484 1.1 pgoyette
485 1.1 pgoyette sun.buf = &s_ds;
486 1.1 pgoyette ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
487 1.1 pgoyette "semctl IPC_STAT: %d", errno);
488 1.1 pgoyette
489 1.1 pgoyette print_semid_ds(&s_ds, 0640);
490 1.1 pgoyette
491 1.1 pgoyette s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
492 1.1 pgoyette
493 1.1 pgoyette sun.buf = &s_ds;
494 1.1 pgoyette ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_SET, sun) != -1,
495 1.1 pgoyette "semctl IPC_SET: %d", errno);
496 1.1 pgoyette
497 1.1 pgoyette memset(&s_ds, 0, sizeof(s_ds));
498 1.1 pgoyette
499 1.1 pgoyette sun.buf = &s_ds;
500 1.1 pgoyette ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
501 1.1 pgoyette "semctl IPC_STAT: %d", errno);
502 1.1 pgoyette
503 1.1 pgoyette ATF_REQUIRE_MSG((s_ds.sem_perm.mode & 0777) == 0600,
504 1.1 pgoyette "IPC_SET of mode didn't hold");
505 1.1 pgoyette
506 1.1 pgoyette print_semid_ds(&s_ds, 0600);
507 1.1 pgoyette
508 1.1 pgoyette for (child_count = 0; child_count < 5; child_count++) {
509 1.1 pgoyette switch ((child_pid = fork())) {
510 1.1 pgoyette case -1:
511 1.1 pgoyette atf_tc_fail("fork: %d", errno);
512 1.1 pgoyette return;
513 1.1 pgoyette
514 1.1 pgoyette case 0:
515 1.1 pgoyette waiter();
516 1.1 pgoyette break;
517 1.1 pgoyette
518 1.1 pgoyette default:
519 1.1 pgoyette break;
520 1.1 pgoyette }
521 1.1 pgoyette }
522 1.1 pgoyette
523 1.1 pgoyette /*
524 1.1 pgoyette * Wait for all of the waiters to be attempting to acquire the
525 1.1 pgoyette * semaphore.
526 1.1 pgoyette */
527 1.1 pgoyette for (;;) {
528 1.1 pgoyette i = semctl(sender_semid, 0, GETNCNT);
529 1.1 pgoyette if (i == -1)
530 1.1 pgoyette atf_tc_fail("semctl GETNCNT: %d", i);
531 1.1 pgoyette if (i == 5)
532 1.1 pgoyette break;
533 1.1 pgoyette }
534 1.1 pgoyette
535 1.1 pgoyette /*
536 1.1 pgoyette * Now set the thundering herd in motion by initializing the
537 1.1 pgoyette * semaphore to the value 1.
538 1.1 pgoyette */
539 1.1 pgoyette sun.val = 1;
540 1.1 pgoyette ATF_REQUIRE_MSG(semctl(sender_semid, 0, SETVAL, sun) != -1,
541 1.1 pgoyette "sender: semctl SETVAL to 1: %d", errno);
542 1.1 pgoyette
543 1.1 pgoyette /*
544 1.1 pgoyette * Wait for all children to finish
545 1.1 pgoyette */
546 1.1 pgoyette sigemptyset(&sigmask);
547 1.1 pgoyette for (;;) {
548 1.1 pgoyette (void) sigsuspend(&sigmask);
549 1.1 pgoyette if (did_sigchild) {
550 1.1 pgoyette c_status = child_status;
551 1.1 pgoyette if (c_status < 0)
552 1.1 pgoyette atf_tc_fail("waitpid: %d", -c_status);
553 1.1 pgoyette else if (WIFEXITED(c_status) == 0)
554 1.1 pgoyette atf_tc_fail("c abnormal exit: %d", c_status);
555 1.1 pgoyette else if (WEXITSTATUS(c_status) != 0)
556 1.1 pgoyette atf_tc_fail("c status: %d",
557 1.1 pgoyette WEXITSTATUS(c_status));
558 1.1 pgoyette else {
559 1.1 pgoyette sun.buf = &s_ds;
560 1.1 pgoyette ATF_REQUIRE_MSG(semctl(sender_semid, 0,
561 1.1 pgoyette IPC_STAT, sun) != -1,
562 1.1 pgoyette "semctl IPC_STAT: %d", errno);
563 1.1 pgoyette
564 1.1 pgoyette print_semid_ds(&s_ds, 0600);
565 1.1 pgoyette atf_tc_pass();
566 1.1 pgoyette }
567 1.1 pgoyette if (child_count <= 0)
568 1.1 pgoyette break;
569 1.1 pgoyette did_sigchild = 0;
570 1.1 pgoyette } else {
571 1.1 pgoyette atf_tc_fail("sender: received unexpected signal");
572 1.1 pgoyette break;
573 1.1 pgoyette }
574 1.1 pgoyette }
575 1.1 pgoyette }
576 1.1 pgoyette
577 1.2 pgoyette ATF_TC_CLEANUP(sem, tc)
578 1.1 pgoyette {
579 1.4 jmmv int sender_semid;
580 1.1 pgoyette
581 1.1 pgoyette /*
582 1.2 pgoyette * Remove the semaphore if it exists
583 1.1 pgoyette */
584 1.4 jmmv sender_semid = read_int("sender_semid");
585 1.2 pgoyette if (sender_semid != -1)
586 1.4 jmmv if (semctl(sender_semid, 0, IPC_RMID) == -1)
587 1.4 jmmv err(1, "semctl IPC_RMID");
588 1.1 pgoyette }
589 1.1 pgoyette
590 1.1 pgoyette void
591 1.1 pgoyette print_semid_ds(sp, mode)
592 1.1 pgoyette struct semid_ds *sp;
593 1.1 pgoyette mode_t mode;
594 1.1 pgoyette {
595 1.1 pgoyette uid_t uid = geteuid();
596 1.1 pgoyette gid_t gid = getegid();
597 1.1 pgoyette
598 1.1 pgoyette printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
599 1.1 pgoyette sp->sem_perm.uid, sp->sem_perm.gid,
600 1.1 pgoyette sp->sem_perm.cuid, sp->sem_perm.cgid,
601 1.1 pgoyette sp->sem_perm.mode & 0777);
602 1.1 pgoyette
603 1.1 pgoyette printf("nsems %u\n", sp->sem_nsems);
604 1.1 pgoyette
605 1.1 pgoyette printf("otime: %s", ctime(&sp->sem_otime));
606 1.1 pgoyette printf("ctime: %s", ctime(&sp->sem_ctime));
607 1.1 pgoyette
608 1.1 pgoyette /*
609 1.1 pgoyette * Sanity check a few things.
610 1.1 pgoyette */
611 1.1 pgoyette
612 1.1 pgoyette ATF_REQUIRE_MSG(sp->sem_perm.uid == uid && sp->sem_perm.cuid == uid,
613 1.1 pgoyette "uid mismatch");
614 1.1 pgoyette
615 1.1 pgoyette ATF_REQUIRE_MSG(sp->sem_perm.gid == gid && sp->sem_perm.cgid == gid,
616 1.1 pgoyette "gid mismatch");
617 1.1 pgoyette
618 1.1 pgoyette ATF_REQUIRE_MSG((sp->sem_perm.mode & 0777) == mode,
619 1.1 pgoyette "mode mismatch %o != %o", (sp->sem_perm.mode & 0777), mode);
620 1.1 pgoyette }
621 1.1 pgoyette
622 1.1 pgoyette void
623 1.1 pgoyette waiter()
624 1.1 pgoyette {
625 1.1 pgoyette struct sembuf s;
626 1.1 pgoyette int semid;
627 1.1 pgoyette
628 1.1 pgoyette if ((semid = semget(semkey, 1, 0)) == -1)
629 1.1 pgoyette err(1, "waiter: semget");
630 1.1 pgoyette
631 1.1 pgoyette /*
632 1.1 pgoyette * Attempt to acquire the semaphore.
633 1.1 pgoyette */
634 1.1 pgoyette s.sem_num = 0;
635 1.1 pgoyette s.sem_op = -1;
636 1.1 pgoyette s.sem_flg = SEM_UNDO;
637 1.1 pgoyette
638 1.1 pgoyette if (semop(semid, &s, 1) == -1)
639 1.1 pgoyette err(1, "waiter: semop -1");
640 1.1 pgoyette
641 1.1 pgoyette printf("WOO! GOT THE SEMAPHORE!\n");
642 1.1 pgoyette sleep(1);
643 1.1 pgoyette
644 1.1 pgoyette /*
645 1.1 pgoyette * Release the semaphore and exit.
646 1.1 pgoyette */
647 1.1 pgoyette s.sem_num = 0;
648 1.1 pgoyette s.sem_op = 1;
649 1.1 pgoyette s.sem_flg = SEM_UNDO;
650 1.1 pgoyette
651 1.1 pgoyette if (semop(semid, &s, 1) == -1)
652 1.1 pgoyette err(1, "waiter: semop +1");
653 1.1 pgoyette
654 1.1 pgoyette exit(0);
655 1.1 pgoyette }
656 1.1 pgoyette
657 1.1 pgoyette /*
658 1.1 pgoyette * Test the SVID-compatible Shared Memory facility.
659 1.1 pgoyette */
660 1.1 pgoyette
661 1.2 pgoyette ATF_TC_WITH_CLEANUP(shm);
662 1.1 pgoyette ATF_TC_HEAD(shm, tc)
663 1.1 pgoyette {
664 1.1 pgoyette
665 1.1 pgoyette atf_tc_set_md_var(tc, "timeout", "3");
666 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks sysv shared memory");
667 1.1 pgoyette }
668 1.1 pgoyette
669 1.1 pgoyette ATF_TC_BODY(shm, tc)
670 1.1 pgoyette {
671 1.1 pgoyette struct sigaction sa;
672 1.1 pgoyette struct shmid_ds s_ds;
673 1.1 pgoyette sigset_t sigmask;
674 1.1 pgoyette char *shm_buf;
675 1.4 jmmv int sender_shmid;
676 1.1 pgoyette int c_status;
677 1.1 pgoyette
678 1.1 pgoyette /*
679 1.1 pgoyette * Install a SIGSYS handler so that we can exit gracefully if
680 1.1 pgoyette * System V Shared Memory support isn't in the kernel.
681 1.1 pgoyette */
682 1.1 pgoyette did_sigsys = 0;
683 1.1 pgoyette sa.sa_handler = sigsys_handler;
684 1.1 pgoyette sigemptyset(&sa.sa_mask);
685 1.1 pgoyette sa.sa_flags = 0;
686 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
687 1.1 pgoyette "sigaction SIGSYS: %d", errno);
688 1.1 pgoyette
689 1.1 pgoyette /*
690 1.1 pgoyette * Install a SIGCHLD handler to deal with all possible exit
691 1.1 pgoyette * conditions of the sharer.
692 1.1 pgoyette */
693 1.1 pgoyette did_sigchild = 0;
694 1.1 pgoyette child_count = 0;
695 1.1 pgoyette sa.sa_handler = sigchld_handler;
696 1.1 pgoyette sigemptyset(&sa.sa_mask);
697 1.1 pgoyette sa.sa_flags = 0;
698 1.1 pgoyette ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
699 1.1 pgoyette "sigaction SIGCHLD: %d", errno);
700 1.1 pgoyette
701 1.1 pgoyette pgsize = sysconf(_SC_PAGESIZE);
702 1.1 pgoyette
703 1.2 pgoyette shmkey = get_ftok(4160);
704 1.2 pgoyette ATF_REQUIRE_MSG(shmkey != (key_t)-1, "get_ftok failed");
705 1.1 pgoyette
706 1.1 pgoyette ATF_REQUIRE_MSG((sender_shmid = shmget(shmkey, pgsize,
707 1.1 pgoyette IPC_CREAT | 0640)) != -1,
708 1.1 pgoyette "shmget: %d", errno);
709 1.4 jmmv write_int("sender_shmid", sender_shmid);
710 1.1 pgoyette
711 1.1 pgoyette ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
712 1.1 pgoyette "shmctl IPC_STAT: %d", errno);
713 1.1 pgoyette
714 1.1 pgoyette print_shmid_ds(&s_ds, 0640);
715 1.1 pgoyette
716 1.1 pgoyette s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
717 1.1 pgoyette
718 1.1 pgoyette ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_SET, &s_ds) != -1,
719 1.1 pgoyette "shmctl IPC_SET: %d", errno);
720 1.1 pgoyette
721 1.1 pgoyette memset(&s_ds, 0, sizeof(s_ds));
722 1.1 pgoyette
723 1.1 pgoyette ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
724 1.1 pgoyette "shmctl IPC_STAT: %d", errno);
725 1.1 pgoyette
726 1.1 pgoyette ATF_REQUIRE_MSG((s_ds.shm_perm.mode & 0777) == 0600,
727 1.1 pgoyette "IPC_SET of mode didn't hold");
728 1.1 pgoyette
729 1.1 pgoyette print_shmid_ds(&s_ds, 0600);
730 1.1 pgoyette
731 1.1 pgoyette shm_buf = shmat(sender_shmid, NULL, 0);
732 1.1 pgoyette ATF_REQUIRE_MSG(shm_buf != (void *) -1, "sender: shmat: %d", errno);
733 1.1 pgoyette
734 1.1 pgoyette /*
735 1.1 pgoyette * Write the test pattern into the shared memory buffer.
736 1.1 pgoyette */
737 1.1 pgoyette strcpy(shm_buf, m2_str);
738 1.1 pgoyette
739 1.1 pgoyette switch ((child_pid = fork())) {
740 1.1 pgoyette case -1:
741 1.1 pgoyette atf_tc_fail("fork: %d", errno);
742 1.1 pgoyette return;
743 1.1 pgoyette
744 1.1 pgoyette case 0:
745 1.1 pgoyette sharer();
746 1.1 pgoyette break;
747 1.1 pgoyette
748 1.1 pgoyette default:
749 1.1 pgoyette break;
750 1.1 pgoyette }
751 1.1 pgoyette
752 1.1 pgoyette /*
753 1.1 pgoyette * Wait for child to finish
754 1.1 pgoyette */
755 1.1 pgoyette sigemptyset(&sigmask);
756 1.1 pgoyette (void) sigsuspend(&sigmask);
757 1.1 pgoyette
758 1.1 pgoyette if (did_sigchild) {
759 1.1 pgoyette c_status = child_status;
760 1.1 pgoyette if (c_status < 0)
761 1.1 pgoyette atf_tc_fail("waitpid: %d", -c_status);
762 1.1 pgoyette else if (WIFEXITED(c_status) == 0)
763 1.1 pgoyette atf_tc_fail("c abnormal exit: %d", c_status);
764 1.1 pgoyette else if (WEXITSTATUS(c_status) != 0)
765 1.1 pgoyette atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
766 1.1 pgoyette else {
767 1.1 pgoyette ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT,
768 1.1 pgoyette &s_ds) != -1,
769 1.1 pgoyette "shmctl IPC_STAT: %d", errno);
770 1.1 pgoyette
771 1.1 pgoyette print_shmid_ds(&s_ds, 0600);
772 1.1 pgoyette atf_tc_pass();
773 1.1 pgoyette }
774 1.1 pgoyette } else
775 1.1 pgoyette atf_tc_fail("sender: received unexpected signal");
776 1.1 pgoyette }
777 1.1 pgoyette
778 1.2 pgoyette ATF_TC_CLEANUP(shm, tc)
779 1.1 pgoyette {
780 1.4 jmmv int sender_shmid;
781 1.1 pgoyette
782 1.1 pgoyette /*
783 1.2 pgoyette * Remove the shared memory area if it exists.
784 1.1 pgoyette */
785 1.4 jmmv sender_shmid = read_int("sender_shmid");
786 1.2 pgoyette if (sender_shmid != -1)
787 1.4 jmmv if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
788 1.4 jmmv err(1, "shmctl IPC_RMID");
789 1.1 pgoyette }
790 1.1 pgoyette
791 1.1 pgoyette void
792 1.1 pgoyette print_shmid_ds(sp, mode)
793 1.1 pgoyette struct shmid_ds *sp;
794 1.1 pgoyette mode_t mode;
795 1.1 pgoyette {
796 1.1 pgoyette uid_t uid = geteuid();
797 1.1 pgoyette gid_t gid = getegid();
798 1.1 pgoyette
799 1.1 pgoyette printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
800 1.1 pgoyette sp->shm_perm.uid, sp->shm_perm.gid,
801 1.1 pgoyette sp->shm_perm.cuid, sp->shm_perm.cgid,
802 1.1 pgoyette sp->shm_perm.mode & 0777);
803 1.1 pgoyette
804 1.1 pgoyette printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
805 1.1 pgoyette (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
806 1.1 pgoyette sp->shm_nattch);
807 1.1 pgoyette
808 1.1 pgoyette printf("atime: %s", ctime(&sp->shm_atime));
809 1.1 pgoyette printf("dtime: %s", ctime(&sp->shm_dtime));
810 1.1 pgoyette printf("ctime: %s", ctime(&sp->shm_ctime));
811 1.1 pgoyette
812 1.1 pgoyette /*
813 1.1 pgoyette * Sanity check a few things.
814 1.1 pgoyette */
815 1.1 pgoyette
816 1.1 pgoyette ATF_REQUIRE_MSG(sp->shm_perm.uid == uid && sp->shm_perm.cuid == uid,
817 1.1 pgoyette "uid mismatch");
818 1.1 pgoyette
819 1.1 pgoyette ATF_REQUIRE_MSG(sp->shm_perm.gid == gid && sp->shm_perm.cgid == gid,
820 1.1 pgoyette "gid mismatch");
821 1.1 pgoyette
822 1.1 pgoyette ATF_REQUIRE_MSG((sp->shm_perm.mode & 0777) == mode, "mode mismatch");
823 1.1 pgoyette }
824 1.1 pgoyette
825 1.1 pgoyette void
826 1.1 pgoyette sharer()
827 1.1 pgoyette {
828 1.1 pgoyette int shmid;
829 1.1 pgoyette void *shm_buf;
830 1.1 pgoyette
831 1.1 pgoyette shmid = shmget(shmkey, pgsize, 0);
832 1.1 pgoyette ATF_REQUIRE_MSG(shmid != -1, "receiver: shmget:%d", errno);
833 1.1 pgoyette
834 1.1 pgoyette shm_buf = shmat(shmid, NULL, 0);
835 1.1 pgoyette ATF_REQUIRE_MSG(shm_buf != (void *) -1, "receiver: shmat: %d", errno);
836 1.1 pgoyette
837 1.1 pgoyette printf("%s\n", (const char *)shm_buf);
838 1.1 pgoyette
839 1.1 pgoyette ATF_REQUIRE_MSG(strcmp((const char *)shm_buf, m2_str) == 0,
840 1.1 pgoyette "receiver: data isn't correct");
841 1.1 pgoyette
842 1.1 pgoyette exit(0);
843 1.1 pgoyette }
844 1.1 pgoyette
845 1.1 pgoyette ATF_TP_ADD_TCS(tp)
846 1.1 pgoyette {
847 1.1 pgoyette
848 1.1 pgoyette ATF_TP_ADD_TC(tp, msg);
849 1.1 pgoyette ATF_TP_ADD_TC(tp, sem);
850 1.1 pgoyette ATF_TP_ADD_TC(tp, shm);
851 1.1 pgoyette
852 1.1 pgoyette return atf_no_error();
853 1.1 pgoyette }
854 1.1 pgoyette
855