xref: /openbsd/regress/sys/kern/sysvshm/shmtest.c (revision 49a6e16f)
1 /*	$NetBSD: shmtest.c,v 1.2 2001/02/19 22:44:41 cgd Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
10  *
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.
19  *
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.
31  */
32 
33 /*
34  * Test the SVID-compatible Shared Memory facility.
35  */
36 
37 #include <sys/ipc.h>
38 #include <sys/mman.h>
39 #include <sys/shm.h>
40 #include <sys/wait.h>
41 
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>
50 
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 
58 const char *m_str = "The quick brown fox jumped over the lazy dog.";
59 
60 int	sender_shmid = -1;
61 pid_t	child_pid;
62 
63 key_t	shmkey;
64 
65 char keyname[] = "/tmp/msgtestXXXXXXXX";
66 
67 int verbose;
68 
69 size_t	pgsize;
70 
71 int
main(argc,argv)72 main(argc, argv)
73 	int argc;
74 	char *argv[];
75 {
76 	struct sigaction sa;
77 	struct shmid_ds s_ds;
78 	sigset_t sigmask;
79 	char *shm_buf;
80 	int fd, ch;
81 
82 	if ((fd = mkstemp(keyname)) < 0)
83 		err(1, "mkstemp");
84 
85 	close(fd);
86 
87 	while ((ch = getopt(argc, argv, "v")) != -1) {
88 		switch (ch) {
89 		case 'v':
90 			verbose = 1;
91 			break;
92 		default:
93 			fprintf(stderr, "Usage: shmtest [-v]\n");
94 			exit(1);
95 		}
96 	}
97 
98 	/*
99 	 * Install a SIGSYS handler so that we can exit gracefully if
100 	 * System V Shared Memory support isn't in the kernel.
101 	 */
102 	sa.sa_handler = sigsys_handler;
103 	sigemptyset(&sa.sa_mask);
104 	sa.sa_flags = 0;
105 	if (sigaction(SIGSYS, &sa, NULL) == -1)
106 		err(1, "sigaction SIGSYS");
107 
108 	/*
109 	 * Install and SIGCHLD handler to deal with all possible exit
110 	 * conditions of the receiver.
111 	 */
112 	sa.sa_handler = sigchld_handler;
113 	sigemptyset(&sa.sa_mask);
114 	sa.sa_flags = 0;
115 	if (sigaction(SIGCHLD, &sa, NULL) == -1)
116 		err(1, "sigaction SIGCHLD");
117 
118 	pgsize = sysconf(_SC_PAGESIZE);
119 
120 	shmkey = ftok(argv[1], 4160);
121 
122 	/*
123 	 * Initialize child_pid to ourselves to that the cleanup function
124 	 * works before we create the receiver.
125 	 */
126 	child_pid = getpid();
127 
128 	/*
129 	 * Make sure that when the sender exits, the message queue is
130 	 * removed.
131 	 */
132 	if (atexit(cleanup) == -1)
133 		err(1, "atexit");
134 
135 	if ((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) == -1)
136 		err(1, "shmget");
137 
138 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
139 		err(1, "shmctl IPC_STAT");
140 
141 	if (verbose)
142 		print_shmid_ds(&s_ds, 0640);
143 
144 	s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
145 
146 	if (shmctl(sender_shmid, IPC_SET, &s_ds) == -1)
147 		err(1, "shmctl IPC_SET");
148 
149 	memset(&s_ds, 0, sizeof(s_ds));
150 
151 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
152 		err(1, "shmctl IPC_STAT");
153 
154 	if ((s_ds.shm_perm.mode & 0777) != 0600)
155 		err(1, "IPC_SET of mode didn't hold");
156 
157 	if (verbose)
158 		print_shmid_ds(&s_ds, 0600);
159 
160 	if ((shm_buf = shmat(sender_shmid, NULL, 0)) == (void *) -1)
161 		err(1, "sender: shmat");
162 
163 	/*
164 	 * Write the test pattern into the shared memory buffer.
165 	 */
166 	strlcpy(shm_buf, m_str, pgsize);
167 
168 	switch ((child_pid = fork())) {
169 	case -1:
170 		err(1, "fork");
171 		/* NOTREACHED */
172 
173 	case 0:
174 		receiver();
175 		break;
176 
177 	default:
178 		break;
179 	}
180 
181 	/*
182 	 * Suspend forever; when we get SIGCHLD, the handler will exit.
183 	 */
184 	sigemptyset(&sigmask);
185 	(void) sigsuspend(&sigmask);
186 
187 	/*
188 	 * ...and any other signal is an unexpected error.
189 	 */
190 	errx(1, "sender: received unexpected signal");
191 }
192 
193 void
sigsys_handler(signo)194 sigsys_handler(signo)
195 	int signo;
196 {
197 
198 	errx(1, "System V Shared Memory support is not present in the kernel");
199 }
200 
201 void
sigchld_handler(signo)202 sigchld_handler(signo)
203 	int signo;
204 {
205 	struct shmid_ds s_ds;
206 	int cstatus;
207 
208 	/*
209 	 * Reap the child; if it exited successfully, then the test passed!
210 	 */
211 	if (waitpid(child_pid, &cstatus, 0) != child_pid)
212 		err(1, "waitpid");
213 
214 	if (WIFEXITED(cstatus) == 0)
215 		errx(1, "receiver exited abnormally");
216 
217 	if (WEXITSTATUS(cstatus) != 0)
218 		errx(1, "receiver exited with status %d",
219 		    WEXITSTATUS(cstatus));
220 
221 	/*
222 	 * If we get here, the child has exited normally, and thus
223 	 * we should exit normally too.  First, tho, we print out
224 	 * the final stats for the message queue.
225 	 */
226 
227 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
228 		err(1, "shmctl IPC_STAT");
229 
230 	if (verbose)
231 		print_shmid_ds(&s_ds, 0600);
232 
233 	exit(0);
234 }
235 
236 void
cleanup()237 cleanup()
238 {
239 
240 	/*
241 	 * If we're the sender, and it exists, remove the shared memory area.
242 	 */
243 	if (child_pid != 0 && sender_shmid != -1) {
244 		if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
245 			warn("shmctl IPC_RMID");
246 	}
247 
248 	remove(keyname);
249 }
250 
251 void
print_shmid_ds(sp,mode)252 print_shmid_ds(sp, mode)
253 	struct shmid_ds *sp;
254 	mode_t mode;
255 {
256 	uid_t uid = geteuid();
257 	gid_t gid = getegid();
258 
259 	printf("PERM: uid %u, gid %u, cuid %u, cgid %u, mode 0%o\n",
260 	    sp->shm_perm.uid, sp->shm_perm.gid,
261 	    sp->shm_perm.cuid, sp->shm_perm.cgid,
262 	    sp->shm_perm.mode & 0777);
263 
264 	printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
265 	    (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
266 	    sp->shm_nattch);
267 
268 	printf("atime: %s", ctime(&sp->shm_atime));
269 	printf("dtime: %s", ctime(&sp->shm_dtime));
270 	printf("ctime: %s", ctime(&sp->shm_ctime));
271 
272 	/*
273 	 * Sanity check a few things.
274 	 */
275 
276 	if (sp->shm_perm.uid != uid || sp->shm_perm.cuid != uid)
277 		errx(1, "uid mismatch");
278 
279 	if (sp->shm_perm.gid != gid || sp->shm_perm.cgid != gid)
280 		errx(1, "gid mismatch");
281 
282 	if ((sp->shm_perm.mode & 0777) != mode)
283 		errx(1, "mode mismatch");
284 }
285 
286 void
receiver()287 receiver()
288 {
289 	int shmid;
290 	void *shm_buf;
291 	void *block;
292 
293 	if ((shmid = shmget(shmkey, pgsize, 0)) == -1)
294 		err(1, "receiver: shmget");
295 
296 	if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
297 		err(1, "receiver: shmat");
298 
299 	if (verbose)
300 		printf("%.*s\n", (int)pgsize, (const char *)shm_buf);
301 	if (strcmp((const char *)shm_buf, m_str) != 0)
302 		errx(1, "receiver: data isn't correct");
303 
304 	/* mmap() a page to get a distinct, freeable address */
305 	block = mmap(NULL, pgsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON,
306 	    -1, 0);
307 	if (block == MAP_FAILED)
308 		err(1, "receiver: mmap");
309 
310 	/* detach, then try to attach, conflicting with the mmap() */
311 	if (shmdt(shm_buf) == -1)
312 		err(1, "receiver: shmdt");
313 	if ((shm_buf = shmat(shmid, block, 0)) != (void *) -1)
314 		errx(1, "receiver: shmat(conflict) succeeded!");
315 	if (errno != ENOMEM)
316 		err(1, "receiver: shmat(conflict) wrong error");
317 
318 	/* free up that address and try again */
319 	if (munmap(block, pgsize) == -1)
320 		err(1, "receiver: munmap");
321 	if ((shm_buf = shmat(shmid, block, 0)) == (void *) -1)
322 		err(1, "receiver: shmat(fixed)");
323 
324 	if (shm_buf != block)
325 		errx(1, "receiver: shmat not at expected address: %p != %p",
326 		    shm_buf, block);
327 
328 	if (verbose)
329 		printf("%.*s\n", (int)pgsize, (const char *)shm_buf);
330 	if (strcmp((const char *)shm_buf, m_str) != 0)
331 		errx(1, "receiver: data isn't correct second time");
332 
333 	exit(0);
334 }
335