xref: /titanic_50/usr/src/lib/libc/port/sys/semsys.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #pragma weak semctl = _semctl
34 #pragma weak semctl64 = _semctl64
35 #pragma weak semget = _semget
36 #pragma weak semop = _semop
37 #pragma weak semids = _semids
38 #pragma weak semtimedop = _semtimedop
39 
40 #include "synonyms.h"
41 #include <sys/types.h>
42 #include <sys/ipc.h>
43 #include <sys/ipc_impl.h>
44 #include <sys/sem.h>
45 #include <sys/sem_impl.h>
46 #include <sys/syscall.h>
47 #include <stdarg.h>
48 #include <errno.h>
49 
50 union semun {
51 	int val;
52 	struct semid_ds *buf;
53 	struct semid_ds64 *buf64;
54 	ushort_t *array;
55 };
56 
57 /*
58  * The kernel implementation of semsys expects an argument containing the
59  * value of the semun argument, but the Sparc compiler passes a pointer
60  * to it, since it is a union.  So, we convert here and pass the value,
61  * but to keep the naive user from being penalized for the counterintuitive
62  * behaviour of the Sparc compiler, we ignore the union if it will not be
63  * used by the system call (to protect the caller from SIGSEGVs, e.g.
64  * semctl(semid, semnum, cmd, NULL);  which would otherwise always result
65  * in a segmentation violation).  We do this partly for consistency, since
66  * the ICL port did it.  This all works just fine for the Intel compiler,
67  * which actually does pass the union by value.
68  */
69 int
70 semctl(int semid, int semnum, int cmd, ...)
71 {
72 	uintptr_t arg;
73 	va_list ap;
74 
75 	switch (cmd) {
76 	case SETVAL:
77 		va_start(ap, cmd);
78 		arg = (uintptr_t)va_arg(ap, union semun).val;
79 		va_end(ap);
80 		break;
81 	case GETALL:
82 	case SETALL:
83 		va_start(ap, cmd);
84 		arg = (uintptr_t)va_arg(ap, union semun).array;
85 		va_end(ap);
86 		break;
87 	case IPC_STAT:
88 	case IPC_SET:
89 		va_start(ap, cmd);
90 		arg = (uintptr_t)va_arg(ap, union semun).buf;
91 		va_end(ap);
92 		break;
93 	case IPC_SET64:
94 	case IPC_STAT64:
95 		(void) __set_errno(EINVAL);
96 		return (-1);
97 	default:
98 		arg = 0;
99 		break;
100 	}
101 
102 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, arg));
103 }
104 
105 int
106 semctl64(int semid, int semnum, int cmd, ...)
107 {
108 	struct semid_ds64 *buf;
109 	va_list ap;
110 
111 	if (cmd != IPC_SET64 && cmd != IPC_STAT64) {
112 		(void) __set_errno(EINVAL);
113 		return (-1);
114 	}
115 
116 	va_start(ap, cmd);
117 	buf = va_arg(ap, union semun).buf64;
118 	va_end(ap);
119 
120 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, buf));
121 }
122 
123 int
124 semget(key_t key, int nsems, int semflg)
125 {
126 	return (syscall(SYS_semsys, SEMGET, key, nsems, semflg));
127 }
128 
129 int
130 semop(int semid, struct sembuf *sops, size_t nsops)
131 {
132 	return (syscall(SYS_semsys, SEMOP, semid, sops, nsops));
133 }
134 
135 int
136 semids(int *buf, uint_t nids, uint_t *pnids)
137 {
138 	return (syscall(SYS_semsys, SEMIDS, buf, nids, pnids));
139 }
140 
141 int
142 semtimedop(int semid, struct sembuf *sops, size_t nsops,
143     const timespec_t *timeout)
144 {
145 	return (syscall(SYS_semsys, SEMTIMEDOP, semid, sops, nsops,
146 	    timeout));
147 }
148