17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307257d1braf#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327257d1braf#pragma weak _semctl = semctl
337257d1braf#pragma weak _semctl64 = semctl64
347257d1braf#pragma weak _semget = semget
357257d1braf#pragma weak _semop = semop
367257d1braf#pragma weak _semids = semids
377257d1braf#pragma weak _semtimedop = semtimedop
387c478bdstevel@tonic-gate
397257d1braf#include "lint.h"
407c478bdstevel@tonic-gate#include <sys/types.h>
417c478bdstevel@tonic-gate#include <sys/ipc.h>
427c478bdstevel@tonic-gate#include <sys/ipc_impl.h>
437c478bdstevel@tonic-gate#include <sys/sem.h>
447c478bdstevel@tonic-gate#include <sys/sem_impl.h>
457c478bdstevel@tonic-gate#include <sys/syscall.h>
467c478bdstevel@tonic-gate#include <stdarg.h>
477c478bdstevel@tonic-gate#include <errno.h>
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gateunion semun {
507c478bdstevel@tonic-gate	int val;
517c478bdstevel@tonic-gate	struct semid_ds *buf;
527c478bdstevel@tonic-gate	struct semid_ds64 *buf64;
537c478bdstevel@tonic-gate	ushort_t *array;
547c478bdstevel@tonic-gate};
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate/*
577c478bdstevel@tonic-gate * The kernel implementation of semsys expects an argument containing the
587c478bdstevel@tonic-gate * value of the semun argument, but the Sparc compiler passes a pointer
597c478bdstevel@tonic-gate * to it, since it is a union.  So, we convert here and pass the value,
607c478bdstevel@tonic-gate * but to keep the naive user from being penalized for the counterintuitive
617c478bdstevel@tonic-gate * behaviour of the Sparc compiler, we ignore the union if it will not be
627c478bdstevel@tonic-gate * used by the system call (to protect the caller from SIGSEGVs, e.g.
637c478bdstevel@tonic-gate * semctl(semid, semnum, cmd, NULL);  which would otherwise always result
647c478bdstevel@tonic-gate * in a segmentation violation).  We do this partly for consistency, since
657c478bdstevel@tonic-gate * the ICL port did it.  This all works just fine for the Intel compiler,
667c478bdstevel@tonic-gate * which actually does pass the union by value.
677c478bdstevel@tonic-gate */
687c478bdstevel@tonic-gateint
697c478bdstevel@tonic-gatesemctl(int semid, int semnum, int cmd, ...)
707c478bdstevel@tonic-gate{
717c478bdstevel@tonic-gate	uintptr_t arg;
727c478bdstevel@tonic-gate	va_list ap;
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate	switch (cmd) {
757c478bdstevel@tonic-gate	case SETVAL:
767c478bdstevel@tonic-gate		va_start(ap, cmd);
777c478bdstevel@tonic-gate		arg = (uintptr_t)va_arg(ap, union semun).val;
787c478bdstevel@tonic-gate		va_end(ap);
797c478bdstevel@tonic-gate		break;
807c478bdstevel@tonic-gate	case GETALL:
817c478bdstevel@tonic-gate	case SETALL:
827c478bdstevel@tonic-gate		va_start(ap, cmd);
837c478bdstevel@tonic-gate		arg = (uintptr_t)va_arg(ap, union semun).array;
847c478bdstevel@tonic-gate		va_end(ap);
857c478bdstevel@tonic-gate		break;
867c478bdstevel@tonic-gate	case IPC_STAT:
877c478bdstevel@tonic-gate	case IPC_SET:
887c478bdstevel@tonic-gate		va_start(ap, cmd);
897c478bdstevel@tonic-gate		arg = (uintptr_t)va_arg(ap, union semun).buf;
907c478bdstevel@tonic-gate		va_end(ap);
917c478bdstevel@tonic-gate		break;
927c478bdstevel@tonic-gate	case IPC_SET64:
937c478bdstevel@tonic-gate	case IPC_STAT64:
947c478bdstevel@tonic-gate		(void) __set_errno(EINVAL);
957c478bdstevel@tonic-gate		return (-1);
967c478bdstevel@tonic-gate	default:
977c478bdstevel@tonic-gate		arg = 0;
987c478bdstevel@tonic-gate		break;
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, arg));
1027c478bdstevel@tonic-gate}
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gateint
1057c478bdstevel@tonic-gatesemctl64(int semid, int semnum, int cmd, ...)
1067c478bdstevel@tonic-gate{
1077c478bdstevel@tonic-gate	struct semid_ds64 *buf;
1087c478bdstevel@tonic-gate	va_list ap;
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate	if (cmd != IPC_SET64 && cmd != IPC_STAT64) {
1117c478bdstevel@tonic-gate		(void) __set_errno(EINVAL);
1127c478bdstevel@tonic-gate		return (-1);
1137c478bdstevel@tonic-gate	}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	va_start(ap, cmd);
1167c478bdstevel@tonic-gate	buf = va_arg(ap, union semun).buf64;
1177c478bdstevel@tonic-gate	va_end(ap);
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMCTL, semid, semnum, cmd, buf));
1207c478bdstevel@tonic-gate}
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gateint
1237c478bdstevel@tonic-gatesemget(key_t key, int nsems, int semflg)
1247c478bdstevel@tonic-gate{
1257c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMGET, key, nsems, semflg));
1267c478bdstevel@tonic-gate}
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gateint
1297c478bdstevel@tonic-gatesemop(int semid, struct sembuf *sops, size_t nsops)
1307c478bdstevel@tonic-gate{
1317c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMOP, semid, sops, nsops));
1327c478bdstevel@tonic-gate}
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gateint
1357c478bdstevel@tonic-gatesemids(int *buf, uint_t nids, uint_t *pnids)
1367c478bdstevel@tonic-gate{
1377c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMIDS, buf, nids, pnids));
1387c478bdstevel@tonic-gate}
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gateint
1417c478bdstevel@tonic-gatesemtimedop(int semid, struct sembuf *sops, size_t nsops,
1427c478bdstevel@tonic-gate    const timespec_t *timeout)
1437c478bdstevel@tonic-gate{
1447c478bdstevel@tonic-gate	return (syscall(SYS_semsys, SEMTIMEDOP, semid, sops, nsops,
1457c478bdstevel@tonic-gate	    timeout));
1467c478bdstevel@tonic-gate}
147