2 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7 * NASA Ames Research Center.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * Obtained from: $NetBSD: shmtest.c,v 1.3 2002/07/20 08:36:26 grant Exp $
35 * Test the SVID-compatible Shared Memory facility.
38 #include <sys/param.h>
52 int main __P((int, char *[]));
53 void print_shmid_ds __P((struct shmid_ds *, mode_t));
54 void sigsys_handler __P((int));
55 void sigchld_handler __P((int));
56 void cleanup __P((void));
57 void receiver __P((void));
58 void usage __P((void));
60 const char *m_str = "The quick brown fox jumped over the lazy dog.";
62 int sender_shmid = -1;
83 * Install a SIGSYS handler so that we can exit gracefully if
84 * System V Shared Memory support isn't in the kernel.
86 sa.sa_handler = sigsys_handler;
87 sigemptyset(&sa.sa_mask);
89 if (sigaction(SIGSYS, &sa, NULL) == -1)
90 err(1, "sigaction SIGSYS");
93 * Install and SIGCHLD handler to deal with all possible exit
94 * conditions of the receiver.
96 sa.sa_handler = sigchld_handler;
97 sigemptyset(&sa.sa_mask);
99 if (sigaction(SIGCHLD, &sa, NULL) == -1)
100 err(1, "sigaction SIGCHLD");
102 pgsize = sysconf(_SC_PAGESIZE);
104 shmkey = ftok(argv[1], 4160);
107 * Initialize child_pid to ourselves to that the cleanup function
108 * works before we create the receiver.
110 child_pid = getpid();
113 * Make sure that when the sender exits, the message queue is
116 if (atexit(cleanup) == -1)
119 if ((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) == -1)
122 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
123 err(1, "shmctl IPC_STAT");
125 print_shmid_ds(&s_ds, 0640);
127 s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
129 if (shmctl(sender_shmid, IPC_SET, &s_ds) == -1)
130 err(1, "shmctl IPC_SET");
132 memset(&s_ds, 0, sizeof(s_ds));
134 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
135 err(1, "shmctl IPC_STAT");
137 if ((s_ds.shm_perm.mode & 0777) != 0600)
138 err(1, "IPC_SET of mode didn't hold");
140 print_shmid_ds(&s_ds, 0600);
142 if ((shm_buf = shmat(sender_shmid, NULL, 0)) == (void *) -1)
143 err(1, "sender: shmat");
146 * Write the test pattern into the shared memory buffer.
148 strcpy(shm_buf, m_str);
150 switch ((child_pid = fork())) {
164 * Suspend forever; when we get SIGCHLD, the handler will exit.
166 sigemptyset(&sigmask);
167 (void) sigsuspend(&sigmask);
170 * ...and any other signal is an unexpected error.
172 errx(1, "sender: received unexpected signal");
176 sigsys_handler(signo)
180 errx(1, "System V Shared Memory support is not present in the kernel");
184 sigchld_handler(signo)
187 struct shmid_ds s_ds;
191 * Reap the child; if it exited successfully, then the test passed!
193 if (waitpid(child_pid, &cstatus, 0) != child_pid)
196 if (WIFEXITED(cstatus) == 0)
197 errx(1, "receiver exited abnormally");
199 if (WEXITSTATUS(cstatus) != 0)
200 errx(1, "receiver exited with status %d",
201 WEXITSTATUS(cstatus));
204 * If we get here, the child has exited normally, and thus
205 * we should exit normally too. First, tho, we print out
206 * the final stats for the message queue.
209 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
210 err(1, "shmctl IPC_STAT");
212 print_shmid_ds(&s_ds, 0600);
222 * If we're the sender, and it exists, remove the shared memory area.
224 if (child_pid != 0 && sender_shmid != -1) {
225 if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
226 warn("shmctl IPC_RMID");
231 print_shmid_ds(sp, mode)
235 uid_t uid = geteuid();
236 gid_t gid = getegid();
238 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
239 sp->shm_perm.uid, sp->shm_perm.gid,
240 sp->shm_perm.cuid, sp->shm_perm.cgid,
241 sp->shm_perm.mode & 0777);
243 printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
244 (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
247 printf("atime: %s", ctime(&sp->shm_atime));
248 printf("dtime: %s", ctime(&sp->shm_dtime));
249 printf("ctime: %s", ctime(&sp->shm_ctime));
252 * Sanity check a few things.
255 if (sp->shm_perm.uid != uid || sp->shm_perm.cuid != uid)
256 errx(1, "uid mismatch");
258 if (sp->shm_perm.gid != gid || sp->shm_perm.cgid != gid)
259 errx(1, "gid mismatch");
261 if ((sp->shm_perm.mode & 0777) != mode)
262 errx(1, "mode mismatch");
269 fprintf(stderr, "usage: %s keypath\n", getprogname());
279 if ((shmid = shmget(shmkey, pgsize, 0)) == -1)
280 err(1, "receiver: shmget");
282 if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
283 err(1, "receiver: shmat");
285 printf("%s\n", (const char *)shm_buf);
286 if (strcmp((const char *)shm_buf, m_str) != 0)
287 err(1, "receiver: data isn't correct");