xref: /illumos-gate/usr/src/lib/libc/port/sys/semsys.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * 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*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 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) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
32*7257d1b4Sraf #pragma weak _semctl = semctl
33*7257d1b4Sraf #pragma weak _semctl64 = semctl64
34*7257d1b4Sraf #pragma weak _semget = semget
35*7257d1b4Sraf #pragma weak _semop = semop
36*7257d1b4Sraf #pragma weak _semids = semids
37*7257d1b4Sraf #pragma weak _semtimedop = semtimedop
387c478bd9Sstevel@tonic-gate 
39*7257d1b4Sraf #include "lint.h"
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
427c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/sem.h>
447c478bd9Sstevel@tonic-gate #include <sys/sem_impl.h>
457c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
467c478bd9Sstevel@tonic-gate #include <stdarg.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate union semun {
507c478bd9Sstevel@tonic-gate 	int val;
517c478bd9Sstevel@tonic-gate 	struct semid_ds *buf;
527c478bd9Sstevel@tonic-gate 	struct semid_ds64 *buf64;
537c478bd9Sstevel@tonic-gate 	ushort_t *array;
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * The kernel implementation of semsys expects an argument containing the
587c478bd9Sstevel@tonic-gate  * value of the semun argument, but the Sparc compiler passes a pointer
597c478bd9Sstevel@tonic-gate  * to it, since it is a union.  So, we convert here and pass the value,
607c478bd9Sstevel@tonic-gate  * but to keep the naive user from being penalized for the counterintuitive
617c478bd9Sstevel@tonic-gate  * behaviour of the Sparc compiler, we ignore the union if it will not be
627c478bd9Sstevel@tonic-gate  * used by the system call (to protect the caller from SIGSEGVs, e.g.
637c478bd9Sstevel@tonic-gate  * semctl(semid, semnum, cmd, NULL);  which would otherwise always result
647c478bd9Sstevel@tonic-gate  * in a segmentation violation).  We do this partly for consistency, since
657c478bd9Sstevel@tonic-gate  * the ICL port did it.  This all works just fine for the Intel compiler,
667c478bd9Sstevel@tonic-gate  * which actually does pass the union by value.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate semctl(int semid, int semnum, int cmd, ...)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	uintptr_t arg;
727c478bd9Sstevel@tonic-gate 	va_list ap;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	switch (cmd) {
757c478bd9Sstevel@tonic-gate 	case SETVAL:
767c478bd9Sstevel@tonic-gate 		va_start(ap, cmd);
777c478bd9Sstevel@tonic-gate 		arg = (uintptr_t)va_arg(ap, union semun).val;
787c478bd9Sstevel@tonic-gate 		va_end(ap);
797c478bd9Sstevel@tonic-gate 		break;
807c478bd9Sstevel@tonic-gate 	case GETALL:
817c478bd9Sstevel@tonic-gate 	case SETALL:
827c478bd9Sstevel@tonic-gate 		va_start(ap, cmd);
837c478bd9Sstevel@tonic-gate 		arg = (uintptr_t)va_arg(ap, union semun).array;
847c478bd9Sstevel@tonic-gate 		va_end(ap);
857c478bd9Sstevel@tonic-gate 		break;
867c478bd9Sstevel@tonic-gate 	case IPC_STAT:
877c478bd9Sstevel@tonic-gate 	case IPC_SET:
887c478bd9Sstevel@tonic-gate 		va_start(ap, cmd);
897c478bd9Sstevel@tonic-gate 		arg = (uintptr_t)va_arg(ap, union semun).buf;
907c478bd9Sstevel@tonic-gate 		va_end(ap);
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 	case IPC_SET64:
937c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
947c478bd9Sstevel@tonic-gate 		(void) __set_errno(EINVAL);
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	default:
977c478bd9Sstevel@tonic-gate 		arg = 0;
987c478bd9Sstevel@tonic-gate 		break;
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, arg));
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate int
1057c478bd9Sstevel@tonic-gate semctl64(int semid, int semnum, int cmd, ...)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	struct semid_ds64 *buf;
1087c478bd9Sstevel@tonic-gate 	va_list ap;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (cmd != IPC_SET64 && cmd != IPC_STAT64) {
1117c478bd9Sstevel@tonic-gate 		(void) __set_errno(EINVAL);
1127c478bd9Sstevel@tonic-gate 		return (-1);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	va_start(ap, cmd);
1167c478bd9Sstevel@tonic-gate 	buf = va_arg(ap, union semun).buf64;
1177c478bd9Sstevel@tonic-gate 	va_end(ap);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, buf));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
1237c478bd9Sstevel@tonic-gate semget(key_t key, int nsems, int semflg)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMGET, key, nsems, semflg));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate int
1297c478bd9Sstevel@tonic-gate semop(int semid, struct sembuf *sops, size_t nsops)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMOP, semid, sops, nsops));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate int
1357c478bd9Sstevel@tonic-gate semids(int *buf, uint_t nids, uint_t *pnids)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMIDS, buf, nids, pnids));
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
1417c478bd9Sstevel@tonic-gate semtimedop(int semid, struct sembuf *sops, size_t nsops,
1427c478bd9Sstevel@tonic-gate     const timespec_t *timeout)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	return (syscall(SYS_semsys, SEMTIMEDOP, semid, sops, nsops,
1457c478bd9Sstevel@tonic-gate 	    timeout));
1467c478bd9Sstevel@tonic-gate }
147