Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / kern / sysvsem / semtest.c
blob3754538bb5816f51f32991d73a7f45ef76fb24b8
1 /* $NetBSD: semtest.c,v 1.5 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 Semaphore facility.
37 #include <sys/param.h>
38 #include <sys/ipc.h>
39 #include <sys/sem.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_semid_ds(struct semid_ds *, mode_t);
53 void sigsys_handler(int);
54 void sigchld_handler(int);
55 void cleanup(void);
56 void waiter(void);
57 void usage(void);
59 int sender_semid = -1;
60 pid_t child_pid;
61 int child_count;
62 int signal_was_sigchld;
64 key_t semkey;
66 union semun {
67 int val; /* value for SETVAL */
68 struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */
69 u_short *array; /* array for GETALL & SETALL */
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
77 struct sigaction sa;
78 union semun sun;
79 struct semid_ds s_ds;
80 sigset_t sigmask;
81 int i;
83 if (argc != 2)
84 usage();
87 * Install a SIGSYS handler so that we can exit gracefully if
88 * System V Semaphore support isn't in the kernel.
90 sa.sa_handler = sigsys_handler;
91 sigemptyset(&sa.sa_mask);
92 sa.sa_flags = 0;
93 if (sigaction(SIGSYS, &sa, NULL) == -1)
94 err(1, "sigaction SIGSYS");
97 * Install and SIGCHLD handler to deal with all possible exit
98 * conditions of the receiver.
100 sa.sa_handler = sigchld_handler;
101 sigemptyset(&sa.sa_mask);
102 sa.sa_flags = 0;
103 if (sigaction(SIGCHLD, &sa, NULL) == -1)
104 err(1, "sigaction SIGCHLD");
106 semkey = ftok(argv[1], 4160);
109 * Initialize child_pid to ourselves to that the cleanup function
110 * works before we create the receiver.
112 child_pid = getpid();
115 * Make sure that when the sender exits, the message queue is
116 * removed.
118 if (atexit(cleanup) == -1)
119 err(1, "atexit");
121 if ((sender_semid = semget(semkey, 1, IPC_CREAT | 0640)) == -1)
122 err(1, "semget");
125 sun.buf = &s_ds;
126 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
127 err(1, "semctl IPC_STAT");
129 print_semid_ds(&s_ds, 0640);
131 s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
133 sun.buf = &s_ds;
134 if (semctl(sender_semid, 0, IPC_SET, sun) == -1)
135 err(1, "semctl IPC_SET");
137 memset(&s_ds, 0, sizeof(s_ds));
139 sun.buf = &s_ds;
140 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
141 err(1, "semctl IPC_STAT");
143 if ((s_ds.sem_perm.mode & 0777) != 0600)
144 err(1, "IPC_SET of mode didn't hold");
146 print_semid_ds(&s_ds, 0600);
148 for (child_count = 0; child_count < 5; child_count++) {
149 switch ((child_pid = fork())) {
150 case -1:
151 err(1, "fork");
152 /* NOTREACHED */
154 case 0:
155 waiter();
156 break;
158 default:
159 break;
164 * Wait for all of the waiters to be attempting to acquire the
165 * semaphore.
167 for (;;) {
168 i = semctl(sender_semid, 0, GETNCNT);
169 if (i == -1)
170 err(1, "semctl GETNCNT");
171 if (i == 5)
172 break;
176 * Now set the thundering herd in motion by initializing the
177 * semaphore to the value 1.
179 sun.val = 1;
180 if (semctl(sender_semid, 0, SETVAL, sun) == -1)
181 err(1, "sender: semctl SETVAL to 1");
184 * Suspend forever; when we get SIGCHLD, the handler will exit.
186 sigemptyset(&sigmask);
187 for (;;) {
188 (void) sigsuspend(&sigmask);
189 if (signal_was_sigchld)
190 signal_was_sigchld = 0;
191 else
192 break;
196 * ...and any other signal is an unexpected error.
198 errx(1, "sender: received unexpected signal");
201 void
202 sigsys_handler(signo)
203 int signo;
206 errx(1, "System V Semaphore support is not present in the kernel");
209 void
210 sigchld_handler(signo)
211 int signo;
213 union semun sun;
214 struct semid_ds s_ds;
215 int cstatus;
218 * Reap the child; if it exited successfully, then we're on the
219 * right track!
221 if (wait(&cstatus) == -1)
222 err(1, "wait");
224 if (WIFEXITED(cstatus) == 0)
225 errx(1, "receiver exited abnormally");
227 if (WEXITSTATUS(cstatus) != 0)
228 errx(1, "receiver exited with status %d",
229 WEXITSTATUS(cstatus));
232 * If we get here, the child has exited normally, and we should
233 * decrement the child count. If the child_count reaches 0, we
234 * should exit.
237 sun.buf = &s_ds;
238 if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
239 err(1, "semctl IPC_STAT");
241 print_semid_ds(&s_ds, 0600);
243 if (--child_count != 0) {
244 signal_was_sigchld = 1;
245 return;
248 exit(0);
251 void
252 cleanup()
256 * If we're the sender, and it exists, remove the message queue.
258 if (child_pid != 0 && sender_semid != -1) {
259 if (semctl(sender_semid, 0, IPC_RMID) == -1)
260 warn("semctl IPC_RMID");
264 void
265 print_semid_ds(sp, mode)
266 struct semid_ds *sp;
267 mode_t mode;
269 uid_t uid = geteuid();
270 gid_t gid = getegid();
272 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
273 sp->sem_perm.uid, sp->sem_perm.gid,
274 sp->sem_perm.cuid, sp->sem_perm.cgid,
275 sp->sem_perm.mode & 0777);
277 printf("nsems %u\n", sp->sem_nsems);
279 printf("otime: %s", ctime(&sp->sem_otime));
280 printf("ctime: %s", ctime(&sp->sem_ctime));
283 * Sanity check a few things.
286 if (sp->sem_perm.uid != uid || sp->sem_perm.cuid != uid)
287 errx(1, "uid mismatch");
289 if (sp->sem_perm.gid != gid || sp->sem_perm.cgid != gid)
290 errx(1, "gid mismatch");
292 if ((sp->sem_perm.mode & 0777) != mode)
293 errx(1, "mode mismatch %o != %o",
294 (sp->sem_perm.mode & 0777), mode);
297 void
298 usage()
301 fprintf(stderr, "usage: %s keypath\n", getprogname());
302 exit(1);
305 void
306 waiter()
308 struct sembuf s;
309 int semid;
311 if ((semid = semget(semkey, 1, 0)) == -1)
312 err(1, "waiter: semget");
315 * Attempt to acquire the semaphore.
317 s.sem_num = 0;
318 s.sem_op = -1;
319 s.sem_flg = SEM_UNDO;
321 if (semop(semid, &s, 1) == -1)
322 err(1, "waiter: semop -1");
324 printf("WOO! GOT THE SEMAPHORE!\n");
325 sleep(1);
328 * Release the semaphore and exit.
330 s.sem_num = 0;
331 s.sem_op = 1;
332 s.sem_flg = SEM_UNDO;
334 if (semop(semid, &s, 1) == -1)
335 err(1, "waiter: semop +1");
337 exit(0);