xref: /illumos-gate/usr/src/lib/libc/port/sys/semsys.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma weak _semctl = semctl
31 #pragma weak _semctl64 = semctl64
32 #pragma weak _semget = semget
33 #pragma weak _semop = semop
34 #pragma weak _semids = semids
35 #pragma weak _semtimedop = semtimedop
36 
37 #include "lint.h"
38 #include <sys/types.h>
39 #include <sys/ipc.h>
40 #include <sys/ipc_impl.h>
41 #include <sys/sem.h>
42 #include <sys/sem_impl.h>
43 #include <sys/syscall.h>
44 #include <stdarg.h>
45 #include <errno.h>
46 
47 union semun {
48 	int val;
49 	struct semid_ds *buf;
50 	struct semid_ds64 *buf64;
51 	ushort_t *array;
52 };
53 
54 /*
55  * The kernel implementation of semsys expects an argument containing the
56  * value of the semun argument, but the Sparc compiler passes a pointer
57  * to it, since it is a union.  So, we convert here and pass the value,
58  * but to keep the naive user from being penalized for the counterintuitive
59  * behaviour of the Sparc compiler, we ignore the union if it will not be
60  * used by the system call (to protect the caller from SIGSEGVs, e.g.
61  * semctl(semid, semnum, cmd, NULL);  which would otherwise always result
62  * in a segmentation violation).  We do this partly for consistency, since
63  * the ICL port did it.  This all works just fine for the Intel compiler,
64  * which actually does pass the union by value.
65  */
66 int
semctl(int semid,int semnum,int cmd,...)67 semctl(int semid, int semnum, int cmd, ...)
68 {
69 	uintptr_t arg;
70 	va_list ap;
71 
72 	switch (cmd) {
73 	case SETVAL:
74 		va_start(ap, cmd);
75 		arg = (uintptr_t)va_arg(ap, union semun).val;
76 		va_end(ap);
77 		break;
78 	case GETALL:
79 	case SETALL:
80 		va_start(ap, cmd);
81 		arg = (uintptr_t)va_arg(ap, union semun).array;
82 		va_end(ap);
83 		break;
84 	case IPC_STAT:
85 	case IPC_SET:
86 		va_start(ap, cmd);
87 		arg = (uintptr_t)va_arg(ap, union semun).buf;
88 		va_end(ap);
89 		break;
90 	case IPC_SET64:
91 	case IPC_STAT64:
92 		(void) __set_errno(EINVAL);
93 		return (-1);
94 	default:
95 		arg = 0;
96 		break;
97 	}
98 
99 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, arg));
100 }
101 
102 int
semctl64(int semid,int semnum,int cmd,...)103 semctl64(int semid, int semnum, int cmd, ...)
104 {
105 	struct semid_ds64 *buf;
106 	va_list ap;
107 
108 	if (cmd != IPC_SET64 && cmd != IPC_STAT64) {
109 		(void) __set_errno(EINVAL);
110 		return (-1);
111 	}
112 
113 	va_start(ap, cmd);
114 	buf = va_arg(ap, union semun).buf64;
115 	va_end(ap);
116 
117 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, buf));
118 }
119 
120 int
semget(key_t key,int nsems,int semflg)121 semget(key_t key, int nsems, int semflg)
122 {
123 	return (syscall(SYS_semsys, SEMGET, key, nsems, semflg));
124 }
125 
126 int
semop(int semid,struct sembuf * sops,size_t nsops)127 semop(int semid, struct sembuf *sops, size_t nsops)
128 {
129 	return (syscall(SYS_semsys, SEMOP, semid, sops, nsops));
130 }
131 
132 int
semids(int * buf,uint_t nids,uint_t * pnids)133 semids(int *buf, uint_t nids, uint_t *pnids)
134 {
135 	return (syscall(SYS_semsys, SEMIDS, buf, nids, pnids));
136 }
137 
138 int
semtimedop(int semid,struct sembuf * sops,size_t nsops,const timespec_t * timeout)139 semtimedop(int semid, struct sembuf *sops, size_t nsops,
140     const timespec_t *timeout)
141 {
142 	return (syscall(SYS_semsys, SEMTIMEDOP, semid, sops, nsops,
143 	    timeout));
144 }
145