xref: /openbsd-src/regress/sys/kern/sysvshm/shmtest.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Test the SVID-compatible Shared Memory facility.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/ipc.h>
46 #include <sys/shm.h>
47 #include <sys/wait.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 
58 int	main(int, char *[]);
59 void	print_shmid_ds(struct shmid_ds *, mode_t);
60 void	sigsys_handler(int);
61 void	sigchld_handler(int);
62 void	cleanup(void);
63 void	receiver(void);
64 
65 const char *m_str = "The quick brown fox jumped over the lazy dog.";
66 
67 int	sender_shmid = -1;
68 pid_t	child_pid;
69 
70 key_t	shmkey;
71 
72 char keyname[] = "/tmp/msgtestXXXXXXXX";
73 
74 int verbose;
75 
76 size_t	pgsize;
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char *argv[];
82 {
83 	struct sigaction sa;
84 	struct shmid_ds s_ds;
85 	sigset_t sigmask;
86 	char *shm_buf;
87 	int fd, ch;
88 
89 	if ((fd = mkstemp(keyname)) < 0)
90 		err(1, "mkstemp");
91 
92 	close(fd);
93 
94 	while ((ch = getopt(argc, argv, "v")) != -1) {
95 		switch (ch) {
96 		case 'v':
97 			verbose = 1;
98 			break;
99 		default:
100 			fprintf(stderr, "Usage: shmtest [-v]\n");
101 			exit(1);
102 		}
103 	}
104 
105 	/*
106 	 * Install a SIGSYS handler so that we can exit gracefully if
107 	 * System V Shared Memory support isn't in the kernel.
108 	 */
109 	sa.sa_handler = sigsys_handler;
110 	sigemptyset(&sa.sa_mask);
111 	sa.sa_flags = 0;
112 	if (sigaction(SIGSYS, &sa, NULL) == -1)
113 		err(1, "sigaction SIGSYS");
114 
115 	/*
116 	 * Install and SIGCHLD handler to deal with all possible exit
117 	 * conditions of the receiver.
118 	 */
119 	sa.sa_handler = sigchld_handler;
120 	sigemptyset(&sa.sa_mask);
121 	sa.sa_flags = 0;
122 	if (sigaction(SIGCHLD, &sa, NULL) == -1)
123 		err(1, "sigaction SIGCHLD");
124 
125 	pgsize = sysconf(_SC_PAGESIZE);
126 
127 	shmkey = ftok(argv[1], 4160);
128 
129 	/*
130 	 * Initialize child_pid to ourselves to that the cleanup function
131 	 * works before we create the receiver.
132 	 */
133 	child_pid = getpid();
134 
135 	/*
136 	 * Make sure that when the sender exits, the message queue is
137 	 * removed.
138 	 */
139 	if (atexit(cleanup) == -1)
140 		err(1, "atexit");
141 
142 	if ((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) == -1)
143 		err(1, "shmget");
144 
145 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
146 		err(1, "shmctl IPC_STAT");
147 
148 	if (verbose)
149 		print_shmid_ds(&s_ds, 0640);
150 
151 	s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
152 
153 	if (shmctl(sender_shmid, IPC_SET, &s_ds) == -1)
154 		err(1, "shmctl IPC_SET");
155 
156 	memset(&s_ds, 0, sizeof(s_ds));
157 
158 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
159 		err(1, "shmctl IPC_STAT");
160 
161 	if ((s_ds.shm_perm.mode & 0777) != 0600)
162 		err(1, "IPC_SET of mode didn't hold");
163 
164 	if (verbose)
165 		print_shmid_ds(&s_ds, 0600);
166 
167 	if ((shm_buf = shmat(sender_shmid, NULL, 0)) == (void *) -1)
168 		err(1, "sender: shmat");
169 
170 	/*
171 	 * Write the test pattern into the shared memory buffer.
172 	 */
173 	strcpy(shm_buf, m_str);
174 
175 	switch ((child_pid = fork())) {
176 	case -1:
177 		err(1, "fork");
178 		/* NOTREACHED */
179 
180 	case 0:
181 		receiver();
182 		break;
183 
184 	default:
185 		break;
186 	}
187 
188 	/*
189 	 * Suspend forever; when we get SIGCHLD, the handler will exit.
190 	 */
191 	sigemptyset(&sigmask);
192 	(void) sigsuspend(&sigmask);
193 
194 	/*
195 	 * ...and any other signal is an unexpected error.
196 	 */
197 	errx(1, "sender: received unexpected signal");
198 }
199 
200 void
201 sigsys_handler(signo)
202 	int signo;
203 {
204 
205 	errx(1, "System V Shared Memory support is not present in the kernel");
206 }
207 
208 void
209 sigchld_handler(signo)
210 	int signo;
211 {
212 	struct shmid_ds s_ds;
213 	int cstatus;
214 
215 	/*
216 	 * Reap the child; if it exited successfully, then the test passed!
217 	 */
218 	if (waitpid(child_pid, &cstatus, 0) != child_pid)
219 		err(1, "waitpid");
220 
221 	if (WIFEXITED(cstatus) == 0)
222 		errx(1, "receiver exited abnormally");
223 
224 	if (WEXITSTATUS(cstatus) != 0)
225 		errx(1, "receiver exited with status %d",
226 		    WEXITSTATUS(cstatus));
227 
228 	/*
229 	 * If we get here, the child has exited normally, and thus
230 	 * we should exit normally too.  First, tho, we print out
231 	 * the final stats for the message queue.
232 	 */
233 
234 	if (shmctl(sender_shmid, IPC_STAT, &s_ds) == -1)
235 		err(1, "shmctl IPC_STAT");
236 
237 	if (verbose)
238 		print_shmid_ds(&s_ds, 0600);
239 
240 	exit(0);
241 }
242 
243 void
244 cleanup()
245 {
246 
247 	/*
248 	 * If we're the sender, and it exists, remove the shared memory area.
249 	 */
250 	if (child_pid != 0 && sender_shmid != -1) {
251 		if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
252 			warn("shmctl IPC_RMID");
253 	}
254 
255 	remove(keyname);
256 }
257 
258 void
259 print_shmid_ds(sp, mode)
260 	struct shmid_ds *sp;
261 	mode_t mode;
262 {
263 	uid_t uid = geteuid();
264 	gid_t gid = getegid();
265 
266 	printf("PERM: uid %u, gid %u, cuid %u, cgid %u, mode 0%o\n",
267 	    sp->shm_perm.uid, sp->shm_perm.gid,
268 	    sp->shm_perm.cuid, sp->shm_perm.cgid,
269 	    sp->shm_perm.mode & 0777);
270 
271 	printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
272 	    (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
273 	    sp->shm_nattch);
274 
275 	printf("atime: %s", ctime(&sp->shm_atime));
276 	printf("dtime: %s", ctime(&sp->shm_dtime));
277 	printf("ctime: %s", ctime(&sp->shm_ctime));
278 
279 	/*
280 	 * Sanity check a few things.
281 	 */
282 
283 	if (sp->shm_perm.uid != uid || sp->shm_perm.cuid != uid)
284 		errx(1, "uid mismatch");
285 
286 	if (sp->shm_perm.gid != gid || sp->shm_perm.cgid != gid)
287 		errx(1, "gid mismatch");
288 
289 	if ((sp->shm_perm.mode & 0777) != mode)
290 		errx(1, "mode mismatch");
291 }
292 
293 void
294 receiver()
295 {
296 	int shmid;
297 	void *shm_buf;
298 
299 	if ((shmid = shmget(shmkey, pgsize, 0)) == -1)
300 		err(1, "receiver: shmget");
301 
302 	if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
303 		err(1, "receiver: shmat");
304 
305 	if (verbose)
306 		printf("%s\n", (const char *)shm_buf);
307 	if (strcmp((const char *)shm_buf, m_str) != 0)
308 		err(1, "receiver: data isn't correct");
309 
310 	exit(0);
311 }
312