Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / kern / sysvshm / shmtest.c
blobe1de04ff13994cfe2004e8596df3930a96f2c038
1 /* $NetBSD: shmtest.c,v 1.4 2005/02/06 06:05:20 perry Exp $ */
3 /*-
4 * Copyright (c) 1999 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.
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 Shared Memory facility.
37 #include <sys/param.h>
38 #include <sys/ipc.h>
39 #include <sys/shm.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_shmid_ds(struct shmid_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 const char *m_str = "The quick brown fox jumped over the lazy dog.";
61 int sender_shmid = -1;
62 pid_t child_pid;
64 key_t shmkey;
66 size_t pgsize;
68 int
69 main(argc, argv)
70 int argc;
71 char *argv[];
73 struct sigaction sa;
74 struct shmid_ds s_ds;
75 sigset_t sigmask;
76 char *shm_buf;
78 if (argc != 2)
79 usage();
82 * Install a SIGSYS handler so that we can exit gracefully if
83 * System V Shared Memory support isn't in the kernel.
85 sa.sa_handler = sigsys_handler;
86 sigemptyset(&sa.sa_mask);
87 sa.sa_flags = 0;
88 if (sigaction(SIGSYS, &sa, NULL) == -1)
89 err(1, "sigaction SIGSYS");
92 * Install and SIGCHLD handler to deal with all possible exit
93 * conditions of the receiver.
95 sa.sa_handler = sigchld_handler;
96 sigemptyset(&sa.sa_mask);
97 sa.sa_flags = 0;
98 if (sigaction(SIGCHLD, &sa, NULL) == -1)
99 err(1, "sigaction SIGCHLD");
101 pgsize = sysconf(_SC_PAGESIZE);
103 shmkey = ftok(argv[1], 4160);
106 * Initialize child_pid to ourselves to that the cleanup function
107 * works before we create the receiver.
109 child_pid = getpid();
112 * Make sure that when the sender exits, the message queue is
113 * removed.
115 if (atexit(cleanup) == -1)
116 err(1, "atexit");
118 if ((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) == -1)
119 err(1, "shmget");
121 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
122 err(1, "shmctl IPC_STAT");
124 print_shmid_ds(&s_ds, 0640);
126 s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
128 if (shmctl(sender_shmid, IPC_SET, &s_ds) == -1)
129 err(1, "shmctl IPC_SET");
131 memset(&s_ds, 0, sizeof(s_ds));
133 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
134 err(1, "shmctl IPC_STAT");
136 if ((s_ds.shm_perm.mode & 0777) != 0600)
137 err(1, "IPC_SET of mode didn't hold");
139 print_shmid_ds(&s_ds, 0600);
141 if ((shm_buf = shmat(sender_shmid, NULL, 0)) == (void *) -1)
142 err(1, "sender: shmat");
145 * Write the test pattern into the shared memory buffer.
147 strcpy(shm_buf, m_str);
149 switch ((child_pid = fork())) {
150 case -1:
151 err(1, "fork");
152 /* NOTREACHED */
154 case 0:
155 receiver();
156 break;
158 default:
159 break;
163 * Suspend forever; when we get SIGCHLD, the handler will exit.
165 sigemptyset(&sigmask);
166 (void) sigsuspend(&sigmask);
169 * ...and any other signal is an unexpected error.
171 errx(1, "sender: received unexpected signal");
174 void
175 sigsys_handler(signo)
176 int signo;
179 errx(1, "System V Shared Memory support is not present in the kernel");
182 void
183 sigchld_handler(signo)
184 int signo;
186 struct shmid_ds s_ds;
187 int cstatus;
190 * Reap the child; if it exited successfully, then the test passed!
192 if (waitpid(child_pid, &cstatus, 0) != child_pid)
193 err(1, "waitpid");
195 if (WIFEXITED(cstatus) == 0)
196 errx(1, "receiver exited abnormally");
198 if (WEXITSTATUS(cstatus) != 0)
199 errx(1, "receiver exited with status %d",
200 WEXITSTATUS(cstatus));
203 * If we get here, the child has exited normally, and thus
204 * we should exit normally too. First, tho, we print out
205 * the final stats for the message queue.
208 if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
209 err(1, "shmctl IPC_STAT");
211 print_shmid_ds(&s_ds, 0600);
213 exit(0);
216 void
217 cleanup()
221 * If we're the sender, and it exists, remove the shared memory area.
223 if (child_pid != 0 && sender_shmid != -1) {
224 if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
225 warn("shmctl IPC_RMID");
229 void
230 print_shmid_ds(sp, mode)
231 struct shmid_ds *sp;
232 mode_t mode;
234 uid_t uid = geteuid();
235 gid_t gid = getegid();
237 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
238 sp->shm_perm.uid, sp->shm_perm.gid,
239 sp->shm_perm.cuid, sp->shm_perm.cgid,
240 sp->shm_perm.mode & 0777);
242 printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
243 (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
244 sp->shm_nattch);
246 printf("atime: %s", ctime(&sp->shm_atime));
247 printf("dtime: %s", ctime(&sp->shm_dtime));
248 printf("ctime: %s", ctime(&sp->shm_ctime));
251 * Sanity check a few things.
254 if (sp->shm_perm.uid != uid || sp->shm_perm.cuid != uid)
255 errx(1, "uid mismatch");
257 if (sp->shm_perm.gid != gid || sp->shm_perm.cgid != gid)
258 errx(1, "gid mismatch");
260 if ((sp->shm_perm.mode & 0777) != mode)
261 errx(1, "mode mismatch");
264 void
265 usage()
268 fprintf(stderr, "usage: %s keypath\n", getprogname());
269 exit(1);
272 void
273 receiver()
275 int shmid;
276 void *shm_buf;
278 if ((shmid = shmget(shmkey, pgsize, 0)) == -1)
279 err(1, "receiver: shmget");
281 if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
282 err(1, "receiver: shmat");
284 printf("%s\n", (const char *)shm_buf);
285 if (strcmp((const char *)shm_buf, m_str) != 0)
286 err(1, "receiver: data isn't correct");
288 exit(0);