xref: /titanic_50/usr/src/lib/libbc/libc/sys/common/semsys.c (revision 1886f67eb9de99edbcda88765f2c603a30c87fc2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984 AT&T */
28 /*	  All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/syscall.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <sys/ipc.h>
36 #include <sys/sem.h>
37 
38 /* semsys dispatch argument */
39 #define	SEMCTL	0
40 #define	SEMGET	1
41 #define	SEMOP	2
42 
43 int
44 semctl(int semid, int semnum, int cmd, union semun *arg)
45 {
46 	switch (cmd) {
47 
48 	case IPC_STAT:
49 	case IPC_SET:
50 		cmd += 10;
51 		/* fall-through */
52 	case SETVAL:
53 	case GETALL:
54 	case SETALL:
55 		return (_syscall(SYS_semsys, SEMCTL,
56 		    semid, semnum, cmd, arg->val));
57 
58 	case IPC_RMID:
59 		cmd += 10;
60 		/* fall-through */
61 	default:
62 		return (_syscall(SYS_semsys, SEMCTL,
63 		    semid, semnum, cmd, 0));
64 	}
65 }
66 
67 int
68 semget(key_t key, int nsems, int semflg)
69 {
70 	return (_syscall(SYS_semsys, SEMGET, key, nsems, semflg));
71 }
72 
73 int
74 semop(int semid, struct sembuf *sops, int nsops)
75 {
76 	return (_syscall(SYS_semsys, SEMOP, semid, sops, nsops));
77 }
78 
79 int
80 semsys(int sysnum, ...)
81 {
82 	va_list ap;
83 	int semid, cmd;
84 	int semnum, val;
85 	union semun arg;
86 	key_t key;
87 	int nsems, semflg;
88 	struct sembuf *sops;
89 	int nsops;
90 
91 	va_start(ap, sysnum);
92 	switch (sysnum) {
93 	case SEMCTL:
94 		semid = va_arg(ap, int);
95 		semnum = va_arg(ap, int);
96 		cmd = va_arg(ap, int);
97 		val = va_arg(ap, int);
98 		if ((cmd == IPC_STAT) || (cmd == IPC_SET) || (cmd == IPC_RMID))
99 			cmd += 10;
100 		va_end(ap);
101 		return (_syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, val));
102 	case SEMGET:
103 		key = va_arg(ap, key_t);
104 		nsems = va_arg(ap, int);
105 		semflg = va_arg(ap, int);
106 		va_end(ap);
107 		return (semget(key, nsems, semflg));
108 	case SEMOP:
109 		semid = va_arg(ap, int);
110 		sops = va_arg(ap, struct sembuf *);
111 		nsops = va_arg(ap, int);
112 		va_end(ap);
113 		return (semop(semid, sops, nsops));
114 	}
115 	va_end(ap);
116 	return (-1);
117 }
118