1ba05755cSAlfred Perlstein /*-
2ba05755cSAlfred Perlstein * Copyright (c) 1999 The NetBSD Foundation, Inc.
3ba05755cSAlfred Perlstein * All rights reserved.
4ba05755cSAlfred Perlstein *
5ba05755cSAlfred Perlstein * This code is derived from software contributed to The NetBSD Foundation
6ba05755cSAlfred Perlstein * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7ba05755cSAlfred Perlstein * NASA Ames Research Center.
8ba05755cSAlfred Perlstein *
9ba05755cSAlfred Perlstein * Redistribution and use in source and binary forms, with or without
10ba05755cSAlfred Perlstein * modification, are permitted provided that the following conditions
11ba05755cSAlfred Perlstein * are met:
12ba05755cSAlfred Perlstein * 1. Redistributions of source code must retain the above copyright
13ba05755cSAlfred Perlstein * notice, this list of conditions and the following disclaimer.
14ba05755cSAlfred Perlstein * 2. Redistributions in binary form must reproduce the above copyright
15ba05755cSAlfred Perlstein * notice, this list of conditions and the following disclaimer in the
16ba05755cSAlfred Perlstein * documentation and/or other materials provided with the distribution.
17ba05755cSAlfred Perlstein *
18ba05755cSAlfred Perlstein * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19ba05755cSAlfred Perlstein * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20ba05755cSAlfred Perlstein * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21ba05755cSAlfred Perlstein * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22ba05755cSAlfred Perlstein * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23ba05755cSAlfred Perlstein * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24ba05755cSAlfred Perlstein * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25ba05755cSAlfred Perlstein * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26ba05755cSAlfred Perlstein * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ba05755cSAlfred Perlstein * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28ba05755cSAlfred Perlstein * POSSIBILITY OF SUCH DAMAGE.
29ba05755cSAlfred Perlstein *
30ba05755cSAlfred Perlstein * Obtained from: $NetBSD: semtest.c,v 1.4 2002/07/20 08:36:25 grant Exp $
31ba05755cSAlfred Perlstein */
32ba05755cSAlfred Perlstein
33ba05755cSAlfred Perlstein /*
34ba05755cSAlfred Perlstein * Test the SVID-compatible Semaphore facility.
35ba05755cSAlfred Perlstein */
36ba05755cSAlfred Perlstein
378b0349c8SKevin Lo #include <sys/types.h>
38ba05755cSAlfred Perlstein #include <sys/ipc.h>
39ba05755cSAlfred Perlstein #include <sys/sem.h>
40ba05755cSAlfred Perlstein #include <sys/wait.h>
41ba05755cSAlfred Perlstein
42ba05755cSAlfred Perlstein #include <err.h>
43ba05755cSAlfred Perlstein #include <errno.h>
44ba05755cSAlfred Perlstein #include <signal.h>
45ba05755cSAlfred Perlstein #include <stdio.h>
46ba05755cSAlfred Perlstein #include <stdlib.h>
47ba05755cSAlfred Perlstein #include <string.h>
48ba05755cSAlfred Perlstein #include <time.h>
49ba05755cSAlfred Perlstein #include <unistd.h>
50ba05755cSAlfred Perlstein
51ba05755cSAlfred Perlstein int main (int, char *[]);
52ba05755cSAlfred Perlstein void print_semid_ds (struct semid_ds *, mode_t);
53ba05755cSAlfred Perlstein void sigsys_handler (int);
54ba05755cSAlfred Perlstein void sigchld_handler(int);
55ba05755cSAlfred Perlstein void cleanup (void);
56ba05755cSAlfred Perlstein void waiter (void);
57ba05755cSAlfred Perlstein void usage (void);
58ba05755cSAlfred Perlstein
59ba05755cSAlfred Perlstein int sender_semid = -1;
60ba05755cSAlfred Perlstein pid_t child_pid;
61ba05755cSAlfred Perlstein int child_count;
62ba05755cSAlfred Perlstein int signal_was_sigchld;
63ba05755cSAlfred Perlstein
64ba05755cSAlfred Perlstein key_t semkey;
65ba05755cSAlfred Perlstein
66ba05755cSAlfred Perlstein /*
67ba05755cSAlfred Perlstein * This is the original semun union used by the sysvsem utility.
68ba05755cSAlfred Perlstein * It is deliberately kept here under #if 0'ed condition for future
69ba05755cSAlfred Perlstein * reference. PLEASE DO NOT REMOVE. The {SET,GET}ALL in FreeBSD
70ba05755cSAlfred Perlstein * are signed values, so the default version in sys/sem.h suffices.
71ba05755cSAlfred Perlstein */
72ba05755cSAlfred Perlstein #if 0
73ba05755cSAlfred Perlstein union semun {
74ba05755cSAlfred Perlstein int val; /* value for SETVAL */
75ba05755cSAlfred Perlstein struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */
76ba05755cSAlfred Perlstein u_short *array; /* array for GETALL & SETALL */
77ba05755cSAlfred Perlstein };
78ba05755cSAlfred Perlstein #endif
79ba05755cSAlfred Perlstein
80ba05755cSAlfred Perlstein int
main(int argc,char * argv[])81ba05755cSAlfred Perlstein main(int argc, char *argv[])
82ba05755cSAlfred Perlstein {
83ba05755cSAlfred Perlstein struct sigaction sa;
84ba05755cSAlfred Perlstein union semun sun;
85ba05755cSAlfred Perlstein struct semid_ds s_ds;
86ba05755cSAlfred Perlstein sigset_t sigmask;
87ba05755cSAlfred Perlstein int i;
88ba05755cSAlfred Perlstein
89ba05755cSAlfred Perlstein if (argc != 2)
90ba05755cSAlfred Perlstein usage();
91ba05755cSAlfred Perlstein
92ba05755cSAlfred Perlstein /*
93ba05755cSAlfred Perlstein * Install a SIGSYS handler so that we can exit gracefully if
94ba05755cSAlfred Perlstein * System V Semaphore support isn't in the kernel.
95ba05755cSAlfred Perlstein */
96ba05755cSAlfred Perlstein sa.sa_handler = sigsys_handler;
97ba05755cSAlfred Perlstein sigemptyset(&sa.sa_mask);
98ba05755cSAlfred Perlstein sa.sa_flags = 0;
99ba05755cSAlfred Perlstein if (sigaction(SIGSYS, &sa, NULL) == -1)
100ba05755cSAlfred Perlstein err(1, "sigaction SIGSYS");
101ba05755cSAlfred Perlstein
102ba05755cSAlfred Perlstein /*
103ba05755cSAlfred Perlstein * Install and SIGCHLD handler to deal with all possible exit
104ba05755cSAlfred Perlstein * conditions of the receiver.
105ba05755cSAlfred Perlstein */
106ba05755cSAlfred Perlstein sa.sa_handler = sigchld_handler;
107ba05755cSAlfred Perlstein sigemptyset(&sa.sa_mask);
108ba05755cSAlfred Perlstein sa.sa_flags = 0;
109ba05755cSAlfred Perlstein if (sigaction(SIGCHLD, &sa, NULL) == -1)
110ba05755cSAlfred Perlstein err(1, "sigaction SIGCHLD");
111ba05755cSAlfred Perlstein
112ba05755cSAlfred Perlstein semkey = ftok(argv[1], 4160);
113ba05755cSAlfred Perlstein
114ba05755cSAlfred Perlstein /*
115ba05755cSAlfred Perlstein * Initialize child_pid to ourselves to that the cleanup function
116ba05755cSAlfred Perlstein * works before we create the receiver.
117ba05755cSAlfred Perlstein */
118ba05755cSAlfred Perlstein child_pid = getpid();
119ba05755cSAlfred Perlstein
120ba05755cSAlfred Perlstein /*
121ba05755cSAlfred Perlstein * Make sure that when the sender exits, the message queue is
122ba05755cSAlfred Perlstein * removed.
123ba05755cSAlfred Perlstein */
124ba05755cSAlfred Perlstein if (atexit(cleanup) == -1)
125ba05755cSAlfred Perlstein err(1, "atexit");
126ba05755cSAlfred Perlstein
127ba05755cSAlfred Perlstein if ((sender_semid = semget(semkey, 1, IPC_CREAT | 0640)) == -1)
128ba05755cSAlfred Perlstein err(1, "semget");
129ba05755cSAlfred Perlstein
130ba05755cSAlfred Perlstein
131ba05755cSAlfred Perlstein sun.buf = &s_ds;
132ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
133ba05755cSAlfred Perlstein err(1, "semctl IPC_STAT");
134ba05755cSAlfred Perlstein
135ba05755cSAlfred Perlstein print_semid_ds(&s_ds, 0640);
136ba05755cSAlfred Perlstein
137ba05755cSAlfred Perlstein s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
138ba05755cSAlfred Perlstein
139ba05755cSAlfred Perlstein sun.buf = &s_ds;
140ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, IPC_SET, sun) == -1)
141ba05755cSAlfred Perlstein err(1, "semctl IPC_SET");
142ba05755cSAlfred Perlstein
143ba05755cSAlfred Perlstein memset(&s_ds, 0, sizeof(s_ds));
144ba05755cSAlfred Perlstein
145ba05755cSAlfred Perlstein sun.buf = &s_ds;
146ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
147ba05755cSAlfred Perlstein err(1, "semctl IPC_STAT");
148ba05755cSAlfred Perlstein
149ba05755cSAlfred Perlstein if ((s_ds.sem_perm.mode & 0777) != 0600)
150ba05755cSAlfred Perlstein err(1, "IPC_SET of mode didn't hold");
151ba05755cSAlfred Perlstein
152ba05755cSAlfred Perlstein print_semid_ds(&s_ds, 0600);
153ba05755cSAlfred Perlstein
154*f00fb545SJilles Tjoelker errno = 0;
155*f00fb545SJilles Tjoelker if (semget(semkey, 1, IPC_CREAT | IPC_EXCL | 0600) != -1 ||
156*f00fb545SJilles Tjoelker errno != EEXIST)
157*f00fb545SJilles Tjoelker err(1, "semget IPC_EXCL 1 did not fail with [EEXIST]");
158*f00fb545SJilles Tjoelker errno = 0;
159*f00fb545SJilles Tjoelker if (semget(semkey, 2, IPC_CREAT | IPC_EXCL | 0600) != -1 ||
160*f00fb545SJilles Tjoelker errno != EEXIST)
161*f00fb545SJilles Tjoelker err(1, "semget IPC_EXCL 2 did not fail with [EEXIST]");
162*f00fb545SJilles Tjoelker
163ba05755cSAlfred Perlstein for (child_count = 0; child_count < 5; child_count++) {
164ba05755cSAlfred Perlstein switch ((child_pid = fork())) {
165ba05755cSAlfred Perlstein case -1:
166ba05755cSAlfred Perlstein err(1, "fork");
167ba05755cSAlfred Perlstein /* NOTREACHED */
168ba05755cSAlfred Perlstein
169ba05755cSAlfred Perlstein case 0:
170ba05755cSAlfred Perlstein waiter();
171ba05755cSAlfred Perlstein break;
172ba05755cSAlfred Perlstein
173ba05755cSAlfred Perlstein default:
174ba05755cSAlfred Perlstein break;
175ba05755cSAlfred Perlstein }
176ba05755cSAlfred Perlstein }
177ba05755cSAlfred Perlstein
178ba05755cSAlfred Perlstein /*
179ba05755cSAlfred Perlstein * Wait for all of the waiters to be attempting to acquire the
180ba05755cSAlfred Perlstein * semaphore.
181ba05755cSAlfred Perlstein */
182ba05755cSAlfred Perlstein for (;;) {
183ba05755cSAlfred Perlstein i = semctl(sender_semid, 0, GETNCNT);
184ba05755cSAlfred Perlstein if (i == -1)
185ba05755cSAlfred Perlstein err(1, "semctl GETNCNT");
186ba05755cSAlfred Perlstein if (i == 5)
187ba05755cSAlfred Perlstein break;
188ba05755cSAlfred Perlstein }
189ba05755cSAlfred Perlstein
190ba05755cSAlfred Perlstein /*
191ba05755cSAlfred Perlstein * Now set the thundering herd in motion by initializing the
192ba05755cSAlfred Perlstein * semaphore to the value 1.
193ba05755cSAlfred Perlstein */
194ba05755cSAlfred Perlstein sun.val = 1;
195ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, SETVAL, sun) == -1)
196ba05755cSAlfred Perlstein err(1, "sender: semctl SETVAL to 1");
197ba05755cSAlfred Perlstein
198ba05755cSAlfred Perlstein /*
199ba05755cSAlfred Perlstein * Suspend forever; when we get SIGCHLD, the handler will exit.
200ba05755cSAlfred Perlstein */
201ba05755cSAlfred Perlstein sigemptyset(&sigmask);
202ba05755cSAlfred Perlstein for (;;) {
203ba05755cSAlfred Perlstein (void) sigsuspend(&sigmask);
204ba05755cSAlfred Perlstein if (signal_was_sigchld)
205ba05755cSAlfred Perlstein signal_was_sigchld = 0;
206ba05755cSAlfred Perlstein else
207ba05755cSAlfred Perlstein break;
208ba05755cSAlfred Perlstein }
209ba05755cSAlfred Perlstein
210ba05755cSAlfred Perlstein /*
211ba05755cSAlfred Perlstein * ...and any other signal is an unexpected error.
212ba05755cSAlfred Perlstein */
213ba05755cSAlfred Perlstein errx(1, "sender: received unexpected signal");
214ba05755cSAlfred Perlstein }
215ba05755cSAlfred Perlstein
216ba05755cSAlfred Perlstein void
sigsys_handler(int signo)217ba05755cSAlfred Perlstein sigsys_handler(int signo)
218ba05755cSAlfred Perlstein {
219ba05755cSAlfred Perlstein
220ba05755cSAlfred Perlstein errx(1, "System V Semaphore support is not present in the kernel");
221ba05755cSAlfred Perlstein }
222ba05755cSAlfred Perlstein
223ba05755cSAlfred Perlstein void
sigchld_handler(int signo)224ba05755cSAlfred Perlstein sigchld_handler(int signo)
225ba05755cSAlfred Perlstein {
226ba05755cSAlfred Perlstein union semun sun;
227ba05755cSAlfred Perlstein struct semid_ds s_ds;
228ba05755cSAlfred Perlstein int cstatus;
229ba05755cSAlfred Perlstein
230ba05755cSAlfred Perlstein /*
231ba05755cSAlfred Perlstein * Reap the child; if it exited successfully, then we're on the
232ba05755cSAlfred Perlstein * right track!
233ba05755cSAlfred Perlstein */
234ba05755cSAlfred Perlstein if (wait(&cstatus) == -1)
235ba05755cSAlfred Perlstein err(1, "wait");
236ba05755cSAlfred Perlstein
237ba05755cSAlfred Perlstein if (WIFEXITED(cstatus) == 0)
238ba05755cSAlfred Perlstein errx(1, "receiver exited abnormally");
239ba05755cSAlfred Perlstein
240ba05755cSAlfred Perlstein if (WEXITSTATUS(cstatus) != 0)
241ba05755cSAlfred Perlstein errx(1, "receiver exited with status %d",
242ba05755cSAlfred Perlstein WEXITSTATUS(cstatus));
243ba05755cSAlfred Perlstein
244ba05755cSAlfred Perlstein /*
245ba05755cSAlfred Perlstein * If we get here, the child has exited normally, and we should
246ba05755cSAlfred Perlstein * decrement the child count. If the child_count reaches 0, we
247ba05755cSAlfred Perlstein * should exit.
248ba05755cSAlfred Perlstein */
249ba05755cSAlfred Perlstein
250ba05755cSAlfred Perlstein sun.buf = &s_ds;
251ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, IPC_STAT, sun) == -1)
252ba05755cSAlfred Perlstein err(1, "semctl IPC_STAT");
253ba05755cSAlfred Perlstein
254ba05755cSAlfred Perlstein print_semid_ds(&s_ds, 0600);
255ba05755cSAlfred Perlstein
256ba05755cSAlfred Perlstein if (--child_count != 0) {
257ba05755cSAlfred Perlstein signal_was_sigchld = 1;
258ba05755cSAlfred Perlstein return;
259ba05755cSAlfred Perlstein }
260ba05755cSAlfred Perlstein
261ba05755cSAlfred Perlstein exit(0);
262ba05755cSAlfred Perlstein }
263ba05755cSAlfred Perlstein
264ba05755cSAlfred Perlstein void
cleanup()265ba05755cSAlfred Perlstein cleanup()
266ba05755cSAlfred Perlstein {
267ba05755cSAlfred Perlstein
268ba05755cSAlfred Perlstein /*
269ba05755cSAlfred Perlstein * If we're the sender, and it exists, remove the message queue.
270ba05755cSAlfred Perlstein */
271ba05755cSAlfred Perlstein if (child_pid != 0 && sender_semid != -1) {
272ba05755cSAlfred Perlstein if (semctl(sender_semid, 0, IPC_RMID) == -1)
273ba05755cSAlfred Perlstein warn("semctl IPC_RMID");
274ba05755cSAlfred Perlstein }
275ba05755cSAlfred Perlstein }
276ba05755cSAlfred Perlstein
277ba05755cSAlfred Perlstein void
print_semid_ds(struct semid_ds * sp,mode_t mode)278ba05755cSAlfred Perlstein print_semid_ds(struct semid_ds *sp, mode_t mode)
279ba05755cSAlfred Perlstein {
280ba05755cSAlfred Perlstein uid_t uid = geteuid();
281ba05755cSAlfred Perlstein gid_t gid = getegid();
282ba05755cSAlfred Perlstein
283ba05755cSAlfred Perlstein printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
284ba05755cSAlfred Perlstein sp->sem_perm.uid, sp->sem_perm.gid,
285ba05755cSAlfred Perlstein sp->sem_perm.cuid, sp->sem_perm.cgid,
286ba05755cSAlfred Perlstein sp->sem_perm.mode & 0777);
287ba05755cSAlfred Perlstein
288ba05755cSAlfred Perlstein printf("nsems %u\n", sp->sem_nsems);
289ba05755cSAlfred Perlstein
290ba05755cSAlfred Perlstein printf("otime: %s", ctime(&sp->sem_otime));
291ba05755cSAlfred Perlstein printf("ctime: %s", ctime(&sp->sem_ctime));
292ba05755cSAlfred Perlstein
293ba05755cSAlfred Perlstein /*
294ba05755cSAlfred Perlstein * Sanity check a few things.
295ba05755cSAlfred Perlstein */
296ba05755cSAlfred Perlstein
297ba05755cSAlfred Perlstein if (sp->sem_perm.uid != uid || sp->sem_perm.cuid != uid)
298ba05755cSAlfred Perlstein errx(1, "uid mismatch");
299ba05755cSAlfred Perlstein
300ba05755cSAlfred Perlstein if (sp->sem_perm.gid != gid || sp->sem_perm.cgid != gid)
301ba05755cSAlfred Perlstein errx(1, "gid mismatch");
302ba05755cSAlfred Perlstein
303ba05755cSAlfred Perlstein if ((sp->sem_perm.mode & 0777) != mode)
304ba05755cSAlfred Perlstein errx(1, "mode mismatch %o != %o",
305ba05755cSAlfred Perlstein (sp->sem_perm.mode & 0777), mode);
306ba05755cSAlfred Perlstein }
307ba05755cSAlfred Perlstein
308ba05755cSAlfred Perlstein void
usage()309ba05755cSAlfred Perlstein usage()
310ba05755cSAlfred Perlstein {
311ba05755cSAlfred Perlstein
312ba05755cSAlfred Perlstein fprintf(stderr, "usage: %s keypath\n", getprogname());
313ba05755cSAlfred Perlstein exit(1);
314ba05755cSAlfred Perlstein }
315ba05755cSAlfred Perlstein
316ba05755cSAlfred Perlstein void
waiter()317ba05755cSAlfred Perlstein waiter()
318ba05755cSAlfred Perlstein {
319ba05755cSAlfred Perlstein struct sembuf s;
320ba05755cSAlfred Perlstein int semid;
321ba05755cSAlfred Perlstein
322ba05755cSAlfred Perlstein if ((semid = semget(semkey, 1, 0)) == -1)
323ba05755cSAlfred Perlstein err(1, "waiter: semget");
324ba05755cSAlfred Perlstein
325ba05755cSAlfred Perlstein /*
326ba05755cSAlfred Perlstein * Attempt to acquire the semaphore.
327ba05755cSAlfred Perlstein */
328ba05755cSAlfred Perlstein s.sem_num = 0;
329ba05755cSAlfred Perlstein s.sem_op = -1;
330ba05755cSAlfred Perlstein s.sem_flg = SEM_UNDO;
331ba05755cSAlfred Perlstein
332ba05755cSAlfred Perlstein if (semop(semid, &s, 1) == -1)
333ba05755cSAlfred Perlstein err(1, "waiter: semop -1");
334ba05755cSAlfred Perlstein
335ba05755cSAlfred Perlstein printf("WOO! GOT THE SEMAPHORE!\n");
336ba05755cSAlfred Perlstein sleep(1);
337ba05755cSAlfred Perlstein
338ba05755cSAlfred Perlstein /*
339ba05755cSAlfred Perlstein * Release the semaphore and exit.
340ba05755cSAlfred Perlstein */
341ba05755cSAlfred Perlstein s.sem_num = 0;
342ba05755cSAlfred Perlstein s.sem_op = 1;
343ba05755cSAlfred Perlstein s.sem_flg = SEM_UNDO;
344ba05755cSAlfred Perlstein
345ba05755cSAlfred Perlstein if (semop(semid, &s, 1) == -1)
346ba05755cSAlfred Perlstein err(1, "waiter: semop +1");
347ba05755cSAlfred Perlstein
348ba05755cSAlfred Perlstein exit(0);
349ba05755cSAlfred Perlstein }
350