xref: /titanic_51/usr/src/lib/libbc/libc/sys/common/semsys.c (revision d4188195113bc7f546b026033c66ea3e12de0e02)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d4188195Sraf  * Common Development and Distribution License (the "License").
6*d4188195Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*d4188195Sraf 
227c478bd9Sstevel@tonic-gate /*
23*d4188195Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T */
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
32*d4188195Sraf #include <sys/syscall.h>
335d54f3d8Smuffin #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
367c478bd9Sstevel@tonic-gate #include <sys/sem.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* semsys dispatch argument */
397c478bd9Sstevel@tonic-gate #define	SEMCTL	0
407c478bd9Sstevel@tonic-gate #define	SEMGET	1
417c478bd9Sstevel@tonic-gate #define	SEMOP	2
427c478bd9Sstevel@tonic-gate 
435d54f3d8Smuffin int
445d54f3d8Smuffin semctl(int semid, int semnum, int cmd, union semun *arg)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	switch (cmd) {
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	case IPC_STAT:
497c478bd9Sstevel@tonic-gate 	case IPC_SET:
507c478bd9Sstevel@tonic-gate 		cmd += 10;
517c478bd9Sstevel@tonic-gate 		/* fall-through */
527c478bd9Sstevel@tonic-gate 	case SETVAL:
537c478bd9Sstevel@tonic-gate 	case GETALL:
547c478bd9Sstevel@tonic-gate 	case SETALL:
55*d4188195Sraf 		return (_syscall(SYS_semsys, SEMCTL,
56*d4188195Sraf 		    semid, semnum, cmd, arg->val));
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	case IPC_RMID:
597c478bd9Sstevel@tonic-gate 		cmd += 10;
607c478bd9Sstevel@tonic-gate 		/* fall-through */
617c478bd9Sstevel@tonic-gate 	default:
62*d4188195Sraf 		return (_syscall(SYS_semsys, SEMCTL,
63*d4188195Sraf 		    semid, semnum, cmd, 0));
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
675d54f3d8Smuffin int
685d54f3d8Smuffin semget(key_t key, int nsems, int semflg)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	return (_syscall(SYS_semsys, SEMGET, key, nsems, semflg));
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
735d54f3d8Smuffin int
745d54f3d8Smuffin semop(int semid, struct sembuf *sops, int nsops)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	return (_syscall(SYS_semsys, SEMOP, semid, sops, nsops));
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
795d54f3d8Smuffin int
805d54f3d8Smuffin semsys(int sysnum, ...)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	va_list ap;
837c478bd9Sstevel@tonic-gate 	int semid, cmd;
847c478bd9Sstevel@tonic-gate 	int semnum, val;
857c478bd9Sstevel@tonic-gate 	union semun arg;
867c478bd9Sstevel@tonic-gate 	key_t key;
877c478bd9Sstevel@tonic-gate 	int nsems, semflg;
887c478bd9Sstevel@tonic-gate 	struct sembuf *sops;
897c478bd9Sstevel@tonic-gate 	int nsops;
907c478bd9Sstevel@tonic-gate 
915d54f3d8Smuffin 	va_start(ap, sysnum);
927c478bd9Sstevel@tonic-gate 	switch (sysnum) {
937c478bd9Sstevel@tonic-gate 	case SEMCTL:
947c478bd9Sstevel@tonic-gate 		semid = va_arg(ap, int);
957c478bd9Sstevel@tonic-gate 		semnum = va_arg(ap, int);
967c478bd9Sstevel@tonic-gate 		cmd = va_arg(ap, int);
977c478bd9Sstevel@tonic-gate 		val = va_arg(ap, int);
987c478bd9Sstevel@tonic-gate 		if ((cmd == IPC_STAT) || (cmd == IPC_SET) || (cmd == IPC_RMID))
997c478bd9Sstevel@tonic-gate 			cmd += 10;
1005d54f3d8Smuffin 		va_end(ap);
1017c478bd9Sstevel@tonic-gate 		return (_syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, val));
1027c478bd9Sstevel@tonic-gate 	case SEMGET:
1037c478bd9Sstevel@tonic-gate 		key = va_arg(ap, key_t);
1047c478bd9Sstevel@tonic-gate 		nsems = va_arg(ap, int);
1057c478bd9Sstevel@tonic-gate 		semflg = va_arg(ap, int);
1065d54f3d8Smuffin 		va_end(ap);
1077c478bd9Sstevel@tonic-gate 		return (semget(key, nsems, semflg));
1087c478bd9Sstevel@tonic-gate 	case SEMOP:
1097c478bd9Sstevel@tonic-gate 		semid = va_arg(ap, int);
1107c478bd9Sstevel@tonic-gate 		sops = va_arg(ap, struct sembuf *);
1117c478bd9Sstevel@tonic-gate 		nsops = va_arg(ap, int);
1125d54f3d8Smuffin 		va_end(ap);
1137c478bd9Sstevel@tonic-gate 		return (semop(semid, sops, nsops));
1147c478bd9Sstevel@tonic-gate 	}
1155d54f3d8Smuffin 	va_end(ap);
1165d54f3d8Smuffin 	return (-1);
1177c478bd9Sstevel@tonic-gate }
118