1 /* $NetBSD: semtest.c,v 1.5 2005/02/06 06:05:20 perry Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
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
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>
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);
59 int sender_semid
= -1;
62 int signal_was_sigchld
;
67 int val
; /* value for SETVAL */
68 struct semid_ds
*buf
; /* buffer for IPC_{STAT,SET} */
69 u_short
*array
; /* array for GETALL & SETALL */
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
);
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
);
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
118 if (atexit(cleanup
) == -1)
121 if ((sender_semid
= semget(semkey
, 1, IPC_CREAT
| 0640)) == -1)
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;
134 if (semctl(sender_semid
, 0, IPC_SET
, sun
) == -1)
135 err(1, "semctl IPC_SET");
137 memset(&s_ds
, 0, sizeof(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())) {
164 * Wait for all of the waiters to be attempting to acquire the
168 i
= semctl(sender_semid
, 0, GETNCNT
);
170 err(1, "semctl GETNCNT");
176 * Now set the thundering herd in motion by initializing the
177 * semaphore to the value 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
);
188 (void) sigsuspend(&sigmask
);
189 if (signal_was_sigchld
)
190 signal_was_sigchld
= 0;
196 * ...and any other signal is an unexpected error.
198 errx(1, "sender: received unexpected signal");
202 sigsys_handler(signo
)
206 errx(1, "System V Semaphore support is not present in the kernel");
210 sigchld_handler(signo
)
214 struct semid_ds s_ds
;
218 * Reap the child; if it exited successfully, then we're on the
221 if (wait(&cstatus
) == -1)
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
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;
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");
265 print_semid_ds(sp
, 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
);
301 fprintf(stderr
, "usage: %s keypath\n", getprogname());
311 if ((semid
= semget(semkey
, 1, 0)) == -1)
312 err(1, "waiter: semget");
315 * Attempt to acquire the semaphore.
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");
328 * Release the semaphore and exit.
332 s
.sem_flg
= SEM_UNDO
;
334 if (semop(semid
, &s
, 1) == -1)
335 err(1, "waiter: semop +1");