Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / kern / sysvmsg / msgtest.c
blobbd81fa86d12a61f8b86cff2df075b65fbd1c80b7
1 /* $NetBSD: msgtest.c,v 1.9 2007/02/06 15:08:17 ad Exp $ */
3 /*-
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Test the SVID-compatible Message Queue facility.
37 #include <sys/param.h>
38 #include <sys/ipc.h>
39 #include <sys/msg.h>
40 #include <sys/wait.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <unistd.h>
51 int main(int, char *[]);
52 void print_msqid_ds(struct msqid_ds *, mode_t);
53 void sigsys_handler(int);
54 void sigchld_handler(int);
55 void cleanup(void);
56 void receiver(void);
57 void usage(void);
59 #define MESSAGE_TEXT_LEN 256
61 struct mymsg {
62 long mtype;
63 char mtext[MESSAGE_TEXT_LEN];
66 const char *m1_str = "California is overrated.";
67 const char *m2_str = "The quick brown fox jumped over the lazy dog.";
69 #define MTYPE_1 1
70 #define MTYPE_1_ACK 2
72 #define MTYPE_2 3
73 #define MTYPE_2_ACK 4
75 int sender_msqid = -1;
76 pid_t child_pid;
78 key_t msgkey;
80 int maxloop = 1;
82 int
83 main(argc, argv)
84 int argc;
85 char *argv[];
87 struct sigaction sa;
88 struct msqid_ds m_ds;
89 struct mymsg m;
90 sigset_t sigmask;
91 int loop;
93 if (argc == 3)
94 maxloop = atoi(argv[2]);
95 else if (argc != 2)
96 usage();
99 * Install a SIGSYS handler so that we can exit gracefully if
100 * System V Message Queue support isn't in the kernel.
102 sa.sa_handler = sigsys_handler;
103 sigemptyset(&sa.sa_mask);
104 sa.sa_flags = 0;
105 if (sigaction(SIGSYS, &sa, NULL) == -1)
106 err(1, "sigaction SIGSYS");
109 * Install and SIGCHLD handler to deal with all possible exit
110 * conditions of the receiver.
112 sa.sa_handler = sigchld_handler;
113 sigemptyset(&sa.sa_mask);
114 sa.sa_flags = 0;
115 if (sigaction(SIGCHLD, &sa, NULL) == -1)
116 err(1, "sigaction SIGCHLD");
118 msgkey = ftok(argv[1], 4160);
121 * Initialize child_pid to ourselves to that the cleanup function
122 * works before we create the receiver.
124 child_pid = getpid();
127 * Make sure that when the sender exits, the message queue is
128 * removed.
130 if (atexit(cleanup) == -1)
131 err(1, "atexit");
133 if ((sender_msqid = msgget(msgkey, IPC_CREAT | 0640)) == -1)
134 err(1, "msgget");
136 if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
137 err(1, "msgctl IPC_STAT");
139 print_msqid_ds(&m_ds, 0640);
141 m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
143 if (msgctl(sender_msqid, IPC_SET, &m_ds) == -1)
144 err(1, "msgctl IPC_SET");
146 memset(&m_ds, 0, sizeof(m_ds));
148 if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
149 err(1, "msgctl IPC_STAT");
151 if ((m_ds.msg_perm.mode & 0777) != 0600)
152 err(1, "IPC_SET of mode didn't hold");
154 print_msqid_ds(&m_ds, 0600);
156 switch ((child_pid = fork())) {
157 case -1:
158 err(1, "fork");
159 /* NOTREACHED */
161 case 0:
162 receiver();
163 break;
165 default:
166 break;
169 for (loop = 0; loop < maxloop; loop++) {
171 * Send the first message to the receiver and wait for the ACK.
173 m.mtype = MTYPE_1;
174 strcpy(m.mtext, m1_str);
175 if (msgsnd(sender_msqid, &m, sizeof(m), 0) == -1)
176 err(1, "sender: msgsnd 1");
178 if (msgrcv(sender_msqid, &m, sizeof(m), MTYPE_1_ACK, 0) !=
179 sizeof(m))
180 err(1, "sender: msgrcv 1 ack");
182 print_msqid_ds(&m_ds, 0600);
185 * Send the second message to the receiver and wait for the ACK.
187 m.mtype = MTYPE_2;
188 strcpy(m.mtext, m2_str);
189 if (msgsnd(sender_msqid, &m, sizeof(m), 0) == -1)
190 err(1, "sender: msgsnd 2");
192 if (msgrcv(sender_msqid, &m, sizeof(m), MTYPE_2_ACK, 0) !=
193 sizeof(m))
194 err(1, "sender: msgrcv 2 ack");
198 * Suspend forever; when we get SIGCHLD, the handler will exit.
200 sigemptyset(&sigmask);
201 (void) sigsuspend(&sigmask);
204 * ...and any other signal is an unexpected error.
206 errx(1, "sender: received unexpected signal");
209 void
210 sigsys_handler(signo)
211 int signo;
214 errx(1, "System V Message Queue support is not present in the kernel");
217 void
218 sigchld_handler(signo)
219 int signo;
221 struct msqid_ds m_ds;
222 int cstatus;
225 * Reap the child; if it exited successfully, then the test passed!
227 if (waitpid(child_pid, &cstatus, 0) != child_pid)
228 err(1, "waitpid");
230 if (WIFEXITED(cstatus) == 0)
231 errx(1, "receiver exited abnormally");
233 if (WEXITSTATUS(cstatus) != 0)
234 errx(1, "receiver exited with status %d",
235 WEXITSTATUS(cstatus));
238 * If we get here, the child has exited normally, and thus
239 * we should exit normally too. First, tho, we print out
240 * the final stats for the message queue.
243 if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
244 err(1, "msgctl IPC_STAT");
246 print_msqid_ds(&m_ds, 0600);
248 exit(0);
251 void
252 cleanup()
256 * If we're the sender, and it exists, remove the message queue.
258 if (child_pid != 0 && sender_msqid != -1) {
259 if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
260 warn("msgctl IPC_RMID");
264 void
265 print_msqid_ds(mp, mode)
266 struct msqid_ds *mp;
267 mode_t mode;
269 uid_t uid = geteuid();
270 gid_t gid = getegid();
272 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
273 mp->msg_perm.uid, mp->msg_perm.gid,
274 mp->msg_perm.cuid, mp->msg_perm.cgid,
275 mp->msg_perm.mode & 0777);
277 printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
278 mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
279 mp->msg_lrpid);
281 printf("stime: %s", ctime(&mp->msg_stime));
282 printf("rtime: %s", ctime(&mp->msg_rtime));
283 printf("ctime: %s", ctime(&mp->msg_ctime));
286 * Sanity check a few things.
289 if (mp->msg_perm.uid != uid || mp->msg_perm.cuid != uid)
290 errx(1, "uid mismatch");
292 if (mp->msg_perm.gid != gid || mp->msg_perm.cgid != gid)
293 errx(1, "gid mismatch");
295 if ((mp->msg_perm.mode & 0777) != mode)
296 errx(1, "mode mismatch");
299 void
300 usage()
303 fprintf(stderr, "usage: %s keypath\n", getprogname());
304 exit(1);
307 void
308 receiver()
310 struct mymsg m;
311 int msqid, loop;
313 if ((msqid = msgget(msgkey, 0)) == -1)
314 err(1, "receiver: msgget");
316 for (loop = 0; loop < maxloop; loop++) {
318 * Receive the first message, print it, and send an ACK.
320 if (msgrcv(msqid, &m, sizeof(m), MTYPE_1, 0) != sizeof(m))
321 err(1, "receiver: msgrcv 1");
323 printf("%s\n", m.mtext);
324 if (strcmp(m.mtext, m1_str) != 0)
325 err(1, "receiver: message 1 data isn't correct");
327 m.mtype = MTYPE_1_ACK;
329 if (msgsnd(msqid, &m, sizeof(m), 0) == -1)
330 err(1, "receiver: msgsnd ack 1");
333 * Receive the second message, print it, and send an ACK.
336 if (msgrcv(msqid, &m, sizeof(m), MTYPE_2, 0) != sizeof(m))
337 err(1, "receiver: msgrcv 2");
339 printf("%s\n", m.mtext);
340 if (strcmp(m.mtext, m2_str) != 0)
341 err(1, "receiver: message 2 data isn't correct");
343 m.mtype = MTYPE_2_ACK;
345 if (msgsnd(msqid, &m, sizeof(m), 0) == -1)
346 err(1, "receiver: msgsnd ack 2");
349 exit(0);