1 /* $NetBSD: msgtest.c,v 1.9 2007/02/06 15:08:17 ad Exp $ */
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
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
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>
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);
59 #define MESSAGE_TEXT_LEN 256
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.";
75 int sender_msqid
= -1;
94 maxloop
= atoi(argv
[2]);
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
);
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
);
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
130 if (atexit(cleanup
) == -1)
133 if ((sender_msqid
= msgget(msgkey
, IPC_CREAT
| 0640)) == -1)
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())) {
169 for (loop
= 0; loop
< maxloop
; loop
++) {
171 * Send the first message to the receiver and wait for the ACK.
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) !=
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.
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) !=
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");
210 sigsys_handler(signo
)
214 errx(1, "System V Message Queue support is not present in the kernel");
218 sigchld_handler(signo
)
221 struct msqid_ds m_ds
;
225 * Reap the child; if it exited successfully, then the test passed!
227 if (waitpid(child_pid
, &cstatus
, 0) != child_pid
)
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);
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");
265 print_msqid_ds(mp
, 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
,
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");
303 fprintf(stderr
, "usage: %s keypath\n", getprogname());
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");