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