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