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