1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 1999 The NetBSD Foundation, Inc.
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * This code is derived from software contributed to The NetBSD Foundation
6*86d7f5d3SJohn Marino * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7*86d7f5d3SJohn Marino * NASA Ames Research Center.
8*86d7f5d3SJohn Marino *
9*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
10*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
11*86d7f5d3SJohn Marino * are met:
12*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
13*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
14*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
15*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
16*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
17*86d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
18*86d7f5d3SJohn Marino * must display the following acknowledgement:
19*86d7f5d3SJohn Marino * This product includes software developed by the NetBSD
20*86d7f5d3SJohn Marino * Foundation, Inc. and its contributors.
21*86d7f5d3SJohn Marino * 4. Neither the name of The NetBSD Foundation nor the names of its
22*86d7f5d3SJohn Marino * contributors may be used to endorse or promote products derived
23*86d7f5d3SJohn Marino * from this software without specific prior written permission.
24*86d7f5d3SJohn Marino *
25*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26*86d7f5d3SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27*86d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*86d7f5d3SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29*86d7f5d3SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*86d7f5d3SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31*86d7f5d3SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*86d7f5d3SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*86d7f5d3SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*86d7f5d3SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35*86d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
36*86d7f5d3SJohn Marino *
37*86d7f5d3SJohn Marino * Obtained from: $NetBSD: msgtest.c,v 1.7 2002/07/20 08:36:25 grant Exp $
38*86d7f5d3SJohn Marino * $DragonFly: src/tools/regression/sysvmsg/msgtest.c,v 1.1 2003/10/18 12:13:01 hmp Exp $
39*86d7f5d3SJohn Marino */
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino /*
42*86d7f5d3SJohn Marino * Test the SVID-compatible Message Queue facility.
43*86d7f5d3SJohn Marino */
44*86d7f5d3SJohn Marino
45*86d7f5d3SJohn Marino #include <sys/param.h>
46*86d7f5d3SJohn Marino #include <sys/ipc.h>
47*86d7f5d3SJohn Marino #include <sys/msg.h>
48*86d7f5d3SJohn Marino #include <sys/wait.h>
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino #include <err.h>
51*86d7f5d3SJohn Marino #include <errno.h>
52*86d7f5d3SJohn Marino #include <signal.h>
53*86d7f5d3SJohn Marino #include <stdio.h>
54*86d7f5d3SJohn Marino #include <stdlib.h>
55*86d7f5d3SJohn Marino #include <string.h>
56*86d7f5d3SJohn Marino #include <time.h>
57*86d7f5d3SJohn Marino #include <unistd.h>
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino void print_msqid_ds (struct msqid_ds *, mode_t);
60*86d7f5d3SJohn Marino void sigsys_handler(int);
61*86d7f5d3SJohn Marino void sigchld_handler (int);
62*86d7f5d3SJohn Marino void cleanup (void);
63*86d7f5d3SJohn Marino void receiver (void);
64*86d7f5d3SJohn Marino void usage (void);
65*86d7f5d3SJohn Marino
66*86d7f5d3SJohn Marino #define MESSAGE_TEXT_LEN 255
67*86d7f5d3SJohn Marino
68*86d7f5d3SJohn Marino /*
69*86d7f5d3SJohn Marino * Define it as test_mymsg because we already have struct mymsg and we dont
70*86d7f5d3SJohn Marino * want to conflict with it. Also, regression fails when the default mymsg
71*86d7f5d3SJohn Marino * struct is used, because mtext[] array is '1', so the passed string cannot
72*86d7f5d3SJohn Marino * be processed.
73*86d7f5d3SJohn Marino */
74*86d7f5d3SJohn Marino struct test_mymsg {
75*86d7f5d3SJohn Marino long mtype;
76*86d7f5d3SJohn Marino char mtext[MESSAGE_TEXT_LEN];
77*86d7f5d3SJohn Marino };
78*86d7f5d3SJohn Marino
79*86d7f5d3SJohn Marino const char *m1_str = "California is overrated.";
80*86d7f5d3SJohn Marino const char *m2_str = "The quick brown fox jumped over the lazy dog.";
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino #define MTYPE_1 1
83*86d7f5d3SJohn Marino #define MTYPE_1_ACK 2
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino #define MTYPE_2 3
86*86d7f5d3SJohn Marino #define MTYPE_2_ACK 4
87*86d7f5d3SJohn Marino
88*86d7f5d3SJohn Marino int sender_msqid = -1;
89*86d7f5d3SJohn Marino pid_t child_pid;
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino key_t msgkey;
92*86d7f5d3SJohn Marino
93*86d7f5d3SJohn Marino int
main(int argc,char * argv[])94*86d7f5d3SJohn Marino main(int argc, char *argv[])
95*86d7f5d3SJohn Marino {
96*86d7f5d3SJohn Marino struct sigaction sa;
97*86d7f5d3SJohn Marino struct msqid_ds m_ds;
98*86d7f5d3SJohn Marino struct test_mymsg m;
99*86d7f5d3SJohn Marino sigset_t sigmask;
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino if (argc != 2)
102*86d7f5d3SJohn Marino usage();
103*86d7f5d3SJohn Marino
104*86d7f5d3SJohn Marino /*
105*86d7f5d3SJohn Marino * Install a SIGSYS handler so that we can exit gracefully if
106*86d7f5d3SJohn Marino * System V Message Queue support isn't in the kernel.
107*86d7f5d3SJohn Marino */
108*86d7f5d3SJohn Marino sa.sa_handler = sigsys_handler;
109*86d7f5d3SJohn Marino sigemptyset(&sa.sa_mask);
110*86d7f5d3SJohn Marino sa.sa_flags = 0;
111*86d7f5d3SJohn Marino if (sigaction(SIGSYS, &sa, NULL) == -1)
112*86d7f5d3SJohn Marino err(1, "sigaction SIGSYS");
113*86d7f5d3SJohn Marino
114*86d7f5d3SJohn Marino /*
115*86d7f5d3SJohn Marino * Install and SIGCHLD handler to deal with all possible exit
116*86d7f5d3SJohn Marino * conditions of the receiver.
117*86d7f5d3SJohn Marino */
118*86d7f5d3SJohn Marino sa.sa_handler = sigchld_handler;
119*86d7f5d3SJohn Marino sigemptyset(&sa.sa_mask);
120*86d7f5d3SJohn Marino sa.sa_flags = 0;
121*86d7f5d3SJohn Marino if (sigaction(SIGCHLD, &sa, NULL) == -1)
122*86d7f5d3SJohn Marino err(1, "sigaction SIGCHLD");
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino msgkey = ftok(argv[1], 4160);
125*86d7f5d3SJohn Marino
126*86d7f5d3SJohn Marino /*
127*86d7f5d3SJohn Marino * Initialize child_pid to ourselves to that the cleanup function
128*86d7f5d3SJohn Marino * works before we create the receiver.
129*86d7f5d3SJohn Marino */
130*86d7f5d3SJohn Marino child_pid = getpid();
131*86d7f5d3SJohn Marino
132*86d7f5d3SJohn Marino /*
133*86d7f5d3SJohn Marino * Make sure that when the sender exits, the message queue is
134*86d7f5d3SJohn Marino * removed.
135*86d7f5d3SJohn Marino */
136*86d7f5d3SJohn Marino if (atexit(cleanup) == -1)
137*86d7f5d3SJohn Marino err(1, "atexit");
138*86d7f5d3SJohn Marino
139*86d7f5d3SJohn Marino if ((sender_msqid = msgget(msgkey, IPC_CREAT | 0640)) == -1)
140*86d7f5d3SJohn Marino err(1, "msgget");
141*86d7f5d3SJohn Marino
142*86d7f5d3SJohn Marino if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
143*86d7f5d3SJohn Marino err(1, "msgctl IPC_STAT");
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino print_msqid_ds(&m_ds, 0640);
146*86d7f5d3SJohn Marino
147*86d7f5d3SJohn Marino m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
148*86d7f5d3SJohn Marino
149*86d7f5d3SJohn Marino if (msgctl(sender_msqid, IPC_SET, &m_ds) == -1)
150*86d7f5d3SJohn Marino err(1, "msgctl IPC_SET");
151*86d7f5d3SJohn Marino
152*86d7f5d3SJohn Marino bzero(&m_ds, sizeof m_ds);
153*86d7f5d3SJohn Marino
154*86d7f5d3SJohn Marino if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
155*86d7f5d3SJohn Marino err(1, "msgctl IPC_STAT");
156*86d7f5d3SJohn Marino
157*86d7f5d3SJohn Marino if ((m_ds.msg_perm.mode & 0777) != 0600)
158*86d7f5d3SJohn Marino err(1, "IPC_SET of mode didn't hold");
159*86d7f5d3SJohn Marino
160*86d7f5d3SJohn Marino print_msqid_ds(&m_ds, 0600);
161*86d7f5d3SJohn Marino
162*86d7f5d3SJohn Marino switch ((child_pid = fork())) {
163*86d7f5d3SJohn Marino case -1:
164*86d7f5d3SJohn Marino err(1, "fork");
165*86d7f5d3SJohn Marino /* NOTREACHED */
166*86d7f5d3SJohn Marino
167*86d7f5d3SJohn Marino case 0:
168*86d7f5d3SJohn Marino receiver();
169*86d7f5d3SJohn Marino break;
170*86d7f5d3SJohn Marino
171*86d7f5d3SJohn Marino default:
172*86d7f5d3SJohn Marino break;
173*86d7f5d3SJohn Marino }
174*86d7f5d3SJohn Marino
175*86d7f5d3SJohn Marino /*
176*86d7f5d3SJohn Marino * Send the first message to the receiver and wait for the ACK.
177*86d7f5d3SJohn Marino */
178*86d7f5d3SJohn Marino m.mtype = MTYPE_1;
179*86d7f5d3SJohn Marino strcpy(m.mtext, m1_str);
180*86d7f5d3SJohn Marino if (msgsnd(sender_msqid, &m, sizeof(m), 0) == -1)
181*86d7f5d3SJohn Marino err(1, "sender: msgsnd 1");
182*86d7f5d3SJohn Marino
183*86d7f5d3SJohn Marino if (msgrcv(sender_msqid, &m, sizeof(m), MTYPE_1_ACK, 0) != sizeof(m))
184*86d7f5d3SJohn Marino err(1, "sender: msgrcv 1 ack");
185*86d7f5d3SJohn Marino
186*86d7f5d3SJohn Marino print_msqid_ds(&m_ds, 0600);
187*86d7f5d3SJohn Marino
188*86d7f5d3SJohn Marino /*
189*86d7f5d3SJohn Marino * Send the second message to the receiver and wait for the ACK.
190*86d7f5d3SJohn Marino */
191*86d7f5d3SJohn Marino m.mtype = MTYPE_2;
192*86d7f5d3SJohn Marino strcpy(m.mtext, m2_str);
193*86d7f5d3SJohn Marino if (msgsnd(sender_msqid, &m, sizeof(m), 0) == -1)
194*86d7f5d3SJohn Marino err(1, "sender: msgsnd 2");
195*86d7f5d3SJohn Marino
196*86d7f5d3SJohn Marino if (msgrcv(sender_msqid, &m, sizeof(m), MTYPE_2_ACK, 0) != sizeof(m))
197*86d7f5d3SJohn Marino err(1, "sender: msgrcv 2 ack");
198*86d7f5d3SJohn Marino
199*86d7f5d3SJohn Marino /*
200*86d7f5d3SJohn Marino * Suspend forever; when we get SIGCHLD, the handler will exit.
201*86d7f5d3SJohn Marino */
202*86d7f5d3SJohn Marino sigemptyset(&sigmask);
203*86d7f5d3SJohn Marino (void) sigsuspend(&sigmask);
204*86d7f5d3SJohn Marino
205*86d7f5d3SJohn Marino /*
206*86d7f5d3SJohn Marino * ...and any other signal is an unexpected error.
207*86d7f5d3SJohn Marino */
208*86d7f5d3SJohn Marino errx(1, "sender: received unexpected signal");
209*86d7f5d3SJohn Marino }
210*86d7f5d3SJohn Marino
211*86d7f5d3SJohn Marino void
sigsys_handler(int signo)212*86d7f5d3SJohn Marino sigsys_handler(int signo)
213*86d7f5d3SJohn Marino {
214*86d7f5d3SJohn Marino
215*86d7f5d3SJohn Marino errx(1, "System V Message Queue support is not present in the kernel");
216*86d7f5d3SJohn Marino }
217*86d7f5d3SJohn Marino
218*86d7f5d3SJohn Marino void
sigchld_handler(int signo)219*86d7f5d3SJohn Marino sigchld_handler(int signo)
220*86d7f5d3SJohn Marino {
221*86d7f5d3SJohn Marino struct msqid_ds m_ds;
222*86d7f5d3SJohn Marino int cstatus;
223*86d7f5d3SJohn Marino
224*86d7f5d3SJohn Marino /*
225*86d7f5d3SJohn Marino * Reap the child; if it exited successfully, then the test passed!
226*86d7f5d3SJohn Marino */
227*86d7f5d3SJohn Marino if (waitpid(child_pid, &cstatus, 0) != child_pid)
228*86d7f5d3SJohn Marino err(1, "waitpid");
229*86d7f5d3SJohn Marino
230*86d7f5d3SJohn Marino if (WIFEXITED(cstatus) == 0)
231*86d7f5d3SJohn Marino errx(1, "receiver exited abnormally");
232*86d7f5d3SJohn Marino
233*86d7f5d3SJohn Marino if (WEXITSTATUS(cstatus) != 0)
234*86d7f5d3SJohn Marino errx(1, "receiver exited with status %d",
235*86d7f5d3SJohn Marino WEXITSTATUS(cstatus));
236*86d7f5d3SJohn Marino
237*86d7f5d3SJohn Marino /*
238*86d7f5d3SJohn Marino * If we get here, the child has exited normally, and thus
239*86d7f5d3SJohn Marino * we should exit normally too. First, tho, we print out
240*86d7f5d3SJohn Marino * the final stats for the message queue.
241*86d7f5d3SJohn Marino */
242*86d7f5d3SJohn Marino
243*86d7f5d3SJohn Marino if (msgctl(sender_msqid, IPC_STAT, &m_ds) == -1)
244*86d7f5d3SJohn Marino err(1, "msgctl IPC_STAT");
245*86d7f5d3SJohn Marino
246*86d7f5d3SJohn Marino print_msqid_ds(&m_ds, 0600);
247*86d7f5d3SJohn Marino
248*86d7f5d3SJohn Marino exit(0);
249*86d7f5d3SJohn Marino }
250*86d7f5d3SJohn Marino
251*86d7f5d3SJohn Marino void
cleanup()252*86d7f5d3SJohn Marino cleanup()
253*86d7f5d3SJohn Marino {
254*86d7f5d3SJohn Marino
255*86d7f5d3SJohn Marino /*
256*86d7f5d3SJohn Marino * If we're the sender, and it exists, remove the message queue.
257*86d7f5d3SJohn Marino */
258*86d7f5d3SJohn Marino if (child_pid != 0 && sender_msqid != -1) {
259*86d7f5d3SJohn Marino if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
260*86d7f5d3SJohn Marino warn("msgctl IPC_RMID");
261*86d7f5d3SJohn Marino }
262*86d7f5d3SJohn Marino }
263*86d7f5d3SJohn Marino
264*86d7f5d3SJohn Marino void
print_msqid_ds(struct msqid_ds * mp,mode_t mode)265*86d7f5d3SJohn Marino print_msqid_ds(struct msqid_ds *mp, mode_t mode)
266*86d7f5d3SJohn Marino {
267*86d7f5d3SJohn Marino uid_t uid = geteuid();
268*86d7f5d3SJohn Marino gid_t gid = getegid();
269*86d7f5d3SJohn Marino
270*86d7f5d3SJohn Marino printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
271*86d7f5d3SJohn Marino mp->msg_perm.uid, mp->msg_perm.gid,
272*86d7f5d3SJohn Marino mp->msg_perm.cuid, mp->msg_perm.cgid,
273*86d7f5d3SJohn Marino mp->msg_perm.mode & 0777);
274*86d7f5d3SJohn Marino
275*86d7f5d3SJohn Marino printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
276*86d7f5d3SJohn Marino mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
277*86d7f5d3SJohn Marino mp->msg_lrpid);
278*86d7f5d3SJohn Marino
279*86d7f5d3SJohn Marino printf("stime: %s", ctime(&mp->msg_stime));
280*86d7f5d3SJohn Marino printf("rtime: %s", ctime(&mp->msg_rtime));
281*86d7f5d3SJohn Marino printf("ctime: %s", ctime(&mp->msg_ctime));
282*86d7f5d3SJohn Marino
283*86d7f5d3SJohn Marino /*
284*86d7f5d3SJohn Marino * Sanity check a few things.
285*86d7f5d3SJohn Marino */
286*86d7f5d3SJohn Marino
287*86d7f5d3SJohn Marino if (mp->msg_perm.uid != uid || mp->msg_perm.cuid != uid)
288*86d7f5d3SJohn Marino errx(1, "uid mismatch");
289*86d7f5d3SJohn Marino
290*86d7f5d3SJohn Marino if (mp->msg_perm.gid != gid || mp->msg_perm.cgid != gid)
291*86d7f5d3SJohn Marino errx(1, "gid mismatch");
292*86d7f5d3SJohn Marino
293*86d7f5d3SJohn Marino if ((mp->msg_perm.mode & 0777) != mode)
294*86d7f5d3SJohn Marino errx(1, "mode mismatch");
295*86d7f5d3SJohn Marino }
296*86d7f5d3SJohn Marino
297*86d7f5d3SJohn Marino void
usage()298*86d7f5d3SJohn Marino usage()
299*86d7f5d3SJohn Marino {
300*86d7f5d3SJohn Marino
301*86d7f5d3SJohn Marino fprintf(stderr, "usage: %s keypath\n", getprogname());
302*86d7f5d3SJohn Marino exit(1);
303*86d7f5d3SJohn Marino }
304*86d7f5d3SJohn Marino
305*86d7f5d3SJohn Marino void
receiver()306*86d7f5d3SJohn Marino receiver()
307*86d7f5d3SJohn Marino {
308*86d7f5d3SJohn Marino struct test_mymsg m;
309*86d7f5d3SJohn Marino int msqid;
310*86d7f5d3SJohn Marino
311*86d7f5d3SJohn Marino if ((msqid = msgget(msgkey, 0)) == -1)
312*86d7f5d3SJohn Marino err(1, "receiver: msgget");
313*86d7f5d3SJohn Marino
314*86d7f5d3SJohn Marino /*
315*86d7f5d3SJohn Marino * Receive the first message, print it, and send an ACK.
316*86d7f5d3SJohn Marino */
317*86d7f5d3SJohn Marino
318*86d7f5d3SJohn Marino if (msgrcv(msqid, &m, sizeof(m), MTYPE_1, 0) != sizeof(m))
319*86d7f5d3SJohn Marino err(1, "receiver: msgrcv 1");
320*86d7f5d3SJohn Marino
321*86d7f5d3SJohn Marino printf("%s\n", m.mtext);
322*86d7f5d3SJohn Marino if (strcmp(m.mtext, m1_str) != 0)
323*86d7f5d3SJohn Marino err(1, "receiver: message 1 data isn't correct");
324*86d7f5d3SJohn Marino
325*86d7f5d3SJohn Marino m.mtype = MTYPE_1_ACK;
326*86d7f5d3SJohn Marino
327*86d7f5d3SJohn Marino if (msgsnd(msqid, &m, sizeof(m), 0) == -1)
328*86d7f5d3SJohn Marino err(1, "receiver: msgsnd ack 1");
329*86d7f5d3SJohn Marino
330*86d7f5d3SJohn Marino /*
331*86d7f5d3SJohn Marino * Receive the second message, print it, and send an ACK.
332*86d7f5d3SJohn Marino */
333*86d7f5d3SJohn Marino
334*86d7f5d3SJohn Marino if (msgrcv(msqid, &m, sizeof(m), MTYPE_2, 0) != sizeof(m))
335*86d7f5d3SJohn Marino err(1, "receiver: msgrcv 2");
336*86d7f5d3SJohn Marino
337*86d7f5d3SJohn Marino printf("%s\n", m.mtext);
338*86d7f5d3SJohn Marino if (strcmp(m.mtext, m2_str) != 0)
339*86d7f5d3SJohn Marino err(1, "receiver: message 2 data isn't correct");
340*86d7f5d3SJohn Marino
341*86d7f5d3SJohn Marino m.mtype = MTYPE_2_ACK;
342*86d7f5d3SJohn Marino
343*86d7f5d3SJohn Marino if (msgsnd(msqid, &m, sizeof(m), 0) == -1)
344*86d7f5d3SJohn Marino err(1, "receiver: msgsnd ack 2");
345*86d7f5d3SJohn Marino
346*86d7f5d3SJohn Marino exit(0);
347*86d7f5d3SJohn Marino }
348