xref: /openbsd-src/regress/sys/kern/sysvsem/semtest.c (revision c20d9ad407c19e76b866bffb8304aa598c583016)
1 /*	$NetBSD: semtest.c,v 1.3 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 Semaphore facility.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/ipc.h>
46 #include <sys/sem.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_semid_ds(struct semid_ds *, mode_t);
60 void	sigsys_handler(int);
61 void	sigchld_handler(int);
62 void	cleanup(void);
63 void	waiter(void);
64 
65 int	sender_semid = -1;
66 pid_t	child_pid;
67 int	child_count;
68 volatile sig_atomic_t signal_was_sigchld;
69 
70 key_t	semkey;
71 
72 char keyname[] = "/tmp/msgtestXXXXXXXX";
73 
74 int verbose;
75 
76 union mysemun {
77 	int	val;		/* value for SETVAL */
78 	struct	semid_ds *buf;	/* buffer for IPC_{STAT,SET} */
79 	u_short	*array;		/* array for GETALL & SETALL */
80 };
81 
82 int
83 main(argc, argv)
84 	int argc;
85 	char *argv[];
86 {
87 	struct sigaction sa;
88 	union mysemun sun;
89 	struct semid_ds s_ds;
90 	sigset_t sigmask;
91 	int i;
92 
93 	int fd, ch;
94 
95 	if ((fd = mkstemp(keyname)) < 0)
96 		err(1, "mkstemp");
97 
98 	close(fd);
99 
100 	while ((ch = getopt(argc, argv, "v")) != -1) {
101 		switch (ch) {
102 		case 'v':
103 			verbose = 1;
104 			break;
105 		default:
106 			fprintf(stderr, "Usage: semtest [-v]\n");
107 			exit(1);
108 		}
109 	}
110 
111 	/*
112 	 * Install a SIGSYS handler so that we can exit gracefully if
113 	 * System V Semaphore support isn't in the kernel.
114 	 */
115 	sa.sa_handler = sigsys_handler;
116 	sigemptyset(&sa.sa_mask);
117 	sa.sa_flags = 0;
118 	if (sigaction(SIGSYS, &sa, NULL) == -1)
119 		err(1, "sigaction SIGSYS");
120 
121 	/*
122 	 * Install and SIGCHLD handler to deal with all possible exit
123 	 * conditions of the receiver.
124 	 */
125 	sa.sa_handler = sigchld_handler;
126 	sigemptyset(&sa.sa_mask);
127 	sa.sa_flags = 0;
128 	if (sigaction(SIGCHLD, &sa, NULL) == -1)
129 		err(1, "sigaction SIGCHLD");
130 
131 	semkey = ftok(keyname, 4160);
132 
133 	/*
134 	 * Initialize child_pid to ourselves to that the cleanup function
135 	 * works before we create the receiver.
136 	 */
137 	child_pid = getpid();
138 
139 	/*
140 	 * Make sure that when the sender exits, the message queue is
141 	 * removed.
142 	 */
143 	if (atexit(cleanup) == -1)
144 		err(1, "atexit");
145 
146 	if ((sender_semid = semget(semkey, 1, IPC_CREAT | 0640)) == -1)
147 		err(1, "semget");
148 
149 
150 	sun.buf = &s_ds;
151 	if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
152 		err(1, "semctl IPC_STAT");
153 
154 	if (verbose)
155 		print_semid_ds(&s_ds, 0640);
156 
157 	s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
158 
159 	sun.buf = &s_ds;
160 	if (semctl(sender_semid, 0, IPC_SET, sun) == -1)
161 		err(1, "semctl IPC_SET");
162 
163 	memset(&s_ds, 0, sizeof(s_ds));
164 
165 	sun.buf = &s_ds;
166 	if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
167 		err(1, "semctl IPC_STAT");
168 
169 	if ((s_ds.sem_perm.mode & 0777) != 0600)
170 		err(1, "IPC_SET of mode didn't hold");
171 
172 	if (verbose)
173 		print_semid_ds(&s_ds, 0600);
174 
175 	for (child_count = 0; child_count < 5; child_count++) {
176 		switch ((child_pid = fork())) {
177 		case -1:
178 			err(1, "fork");
179 			/* NOTREACHED */
180 
181 		case 0:
182 			waiter();
183 			break;
184 
185 		default:
186 			break;
187 		}
188 	}
189 
190 	/*
191 	 * Wait for all of the waiters to be attempting to acquire the
192 	 * semaphore.
193 	 */
194 	for (;;) {
195 		i = semctl(sender_semid, 0, GETNCNT);
196 		if (i == -1)
197 			err(1, "semctl GETNCNT");
198 		if (i == 5)
199 			break;
200 	}
201 
202 	/*
203 	 * Now set the thundering herd in motion by initializing the
204 	 * semaphore to the value 1.
205 	 */
206 	sun.val = 1;
207 	if (semctl(sender_semid, 0, SETVAL, sun) == -1)
208 		err(1, "sender: semctl SETVAL to 1");
209 
210 	/*
211 	 * Suspend forever; when we get SIGCHLD, the handler will exit.
212 	 */
213 	sigemptyset(&sigmask);
214 	for (;;) {
215 		(void) sigsuspend(&sigmask);
216 		if (signal_was_sigchld)
217 			signal_was_sigchld = 0;
218 		else
219 			break;
220 	}
221 
222 	/*
223 	 * ...and any other signal is an unexpected error.
224 	 */
225 	errx(1, "sender: received unexpected signal");
226 }
227 
228 void
229 sigsys_handler(signo)
230 	int signo;
231 {
232 
233 	errx(1, "System V Semaphore support is not present in the kernel");
234 }
235 
236 void
237 sigchld_handler(signo)
238 	int signo;
239 {
240 	union mysemun sun;
241 	struct semid_ds s_ds;
242 	int cstatus;
243 
244 	/*
245 	 * Reap the child; if it exited successfully, then we're on the
246 	 * right track!
247 	 */
248 	if (wait(&cstatus) == -1)
249 		err(1, "wait");
250 
251 	if (WIFEXITED(cstatus) == 0)
252 		errx(1, "receiver exited abnormally");
253 
254 	if (WEXITSTATUS(cstatus) != 0)
255 		errx(1, "receiver exited with status %d",
256 		    WEXITSTATUS(cstatus));
257 
258 	/*
259 	 * If we get here, the child has exited normally, and we should
260 	 * decrement the child count.  If the child_count reaches 0, we
261 	 * should exit.
262 	 */
263 
264 	sun.buf = &s_ds;
265 	if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
266 		err(1, "semctl IPC_STAT");
267 
268 	if (verbose)
269 		print_semid_ds(&s_ds, 0600);
270 
271 	if (--child_count != 0) {
272 		signal_was_sigchld = 1;
273 		return;
274 	}
275 
276 	exit(0);
277 }
278 
279 void
280 cleanup()
281 {
282 
283 	/*
284 	 * If we're the sender, and it exists, remove the message queue.
285 	 */
286 	if (child_pid != 0 && sender_semid != -1) {
287 		if (semctl(sender_semid, 0, IPC_RMID) == -1)
288 			warn("semctl IPC_RMID");
289 	}
290 	remove(keyname);
291 }
292 
293 void
294 print_semid_ds(sp, mode)
295 	struct semid_ds *sp;
296 	mode_t mode;
297 {
298 	uid_t uid = geteuid();
299 	gid_t gid = getegid();
300 
301 	printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
302 	    sp->sem_perm.uid, sp->sem_perm.gid,
303 	    sp->sem_perm.cuid, sp->sem_perm.cgid,
304 	    sp->sem_perm.mode & 0777);
305 
306 	printf("nsems %u\n", sp->sem_nsems);
307 
308 	printf("otime: %s", ctime(&sp->sem_otime));
309 	printf("ctime: %s", ctime(&sp->sem_ctime));
310 
311 	/*
312 	 * Sanity check a few things.
313 	 */
314 
315 	if (sp->sem_perm.uid != uid || sp->sem_perm.cuid != uid)
316 		errx(1, "uid mismatch");
317 
318 	if (sp->sem_perm.gid != gid || sp->sem_perm.cgid != gid)
319 		errx(1, "gid mismatch");
320 
321 	if ((sp->sem_perm.mode & 0777) != mode)
322 		errx(1, "mode mismatch %o != %o",
323 		    (sp->sem_perm.mode & 0777), mode);
324 }
325 
326 void
327 waiter()
328 {
329 	struct sembuf s;
330 	int semid;
331 
332 	if ((semid = semget(semkey, 1, 0)) == -1)
333 		err(1, "waiter: semget");
334 
335 	/*
336 	 * Attempt to acquire the semaphore.
337 	 */
338 	s.sem_num = 0;
339 	s.sem_op = -1;
340 	s.sem_flg = SEM_UNDO;
341 
342 	if (semop(semid, &s, 1) == -1)
343 		err(1, "waiter: semop -1");
344 
345 	if (verbose)
346 		printf("WOO!  GOT THE SEMAPHORE!\n");
347 	sleep(1);
348 
349 	/*
350 	 * Release the semaphore and exit.
351 	 */
352 	s.sem_num = 0;
353 	s.sem_op = 1;
354 	s.sem_flg = SEM_UNDO;
355 
356 	if (semop(semid, &s, 1) == -1)
357 		err(1, "waiter: semop +1");
358 
359 	exit(0);
360 }
361