10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52677Sml93401 * Common Development and Distribution License (the "License").
62677Sml93401 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11861SMarek.Pospisil@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Inter-Process Communication Semaphore Facility.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * See os/ipc.c for a description of common IPC functionality.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * Resource controls
350Sstevel@tonic-gate * -----------------
360Sstevel@tonic-gate *
372677Sml93401 * Control: zone.max-sem-ids (rc_zone_semmni)
382677Sml93401 * Description: Maximum number of semaphore ids allowed a zone.
392677Sml93401 *
402677Sml93401 * When semget() is used to allocate a semaphore set, one id is
412677Sml93401 * allocated. If the id allocation doesn't succeed, semget() fails
422677Sml93401 * and errno is set to ENOSPC. Upon successful semctl(, IPC_RMID)
432677Sml93401 * the id is deallocated.
442677Sml93401 *
450Sstevel@tonic-gate * Control: project.max-sem-ids (rc_project_semmni)
460Sstevel@tonic-gate * Description: Maximum number of semaphore ids allowed a project.
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * When semget() is used to allocate a semaphore set, one id is
490Sstevel@tonic-gate * allocated. If the id allocation doesn't succeed, semget() fails
500Sstevel@tonic-gate * and errno is set to ENOSPC. Upon successful semctl(, IPC_RMID)
510Sstevel@tonic-gate * the id is deallocated.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * Control: process.max-sem-nsems (rc_process_semmsl)
540Sstevel@tonic-gate * Description: Maximum number of semaphores allowed per semaphore set.
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * When semget() is used to allocate a semaphore set, the size of the
570Sstevel@tonic-gate * set is compared with this limit. If the number of semaphores
580Sstevel@tonic-gate * exceeds the limit, semget() fails and errno is set to EINVAL.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * Control: process.max-sem-ops (rc_process_semopm)
610Sstevel@tonic-gate * Description: Maximum number of semaphore operations allowed per
620Sstevel@tonic-gate * semop call.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * When semget() successfully allocates a semaphore set, the minimum
650Sstevel@tonic-gate * enforced value of this limit is used to initialize the
660Sstevel@tonic-gate * "system-imposed maximum" number of operations a semop() call for
670Sstevel@tonic-gate * this set can perform.
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * Undo structures
700Sstevel@tonic-gate * ---------------
710Sstevel@tonic-gate *
720Sstevel@tonic-gate * Removing the undo structure tunables involved a serious redesign of
730Sstevel@tonic-gate * how they were implemented. There is now one undo structure for
740Sstevel@tonic-gate * every process/semaphore array combination (lazily allocated, of
750Sstevel@tonic-gate * course), and each is equal in size to the semaphore it corresponds
760Sstevel@tonic-gate * to. To avoid scalability and performance problems, the undo
770Sstevel@tonic-gate * structures are stored in two places: a per-process AVL tree sorted
780Sstevel@tonic-gate * by ksemid pointer (p_semacct, protected by p_lock) and an unsorted
790Sstevel@tonic-gate * per-semaphore linked list (sem_undos, protected by the semaphore's
800Sstevel@tonic-gate * ID lock). The former is used by semop, where a lookup is performed
810Sstevel@tonic-gate * once and cached if SEM_UNDO is specified for any of the operations,
820Sstevel@tonic-gate * and at process exit where the undoable operations are rolled back.
830Sstevel@tonic-gate * The latter is used when removing the semaphore, so the undo
840Sstevel@tonic-gate * structures can be removed from the appropriate processes' trees.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * The undo structure itself contains pointers to the ksemid and proc
870Sstevel@tonic-gate * to which it corresponds, a list node, an AVL node, and an array of
880Sstevel@tonic-gate * adjust-on-exit (AOE) values. When an undo structure is allocated it
890Sstevel@tonic-gate * is immediately added to both the process's tree and the semaphore's
900Sstevel@tonic-gate * list. Lastly, the reference count on the semaphore is increased.
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * Avoiding a lock ordering violation between p_lock and the ID lock,
930Sstevel@tonic-gate * wont to occur when there is a race between a process exiting and the
940Sstevel@tonic-gate * removal of a semaphore, mandates the delicate dance that exists
950Sstevel@tonic-gate * between semexit and sem_rmid.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate * sem_rmid, holding the ID lock, iterates through all undo structures
980Sstevel@tonic-gate * and for each takes the appropriate process's p_lock and checks to
990Sstevel@tonic-gate * see if p_semacct is NULL. If it is, it skips that undo structure
1000Sstevel@tonic-gate * and continues to the next. Otherwise, it removes the undo structure
1010Sstevel@tonic-gate * from both the AVL tree and the semaphore's list, and releases the
1020Sstevel@tonic-gate * hold that the undo structure had on the semaphore.
1030Sstevel@tonic-gate *
1040Sstevel@tonic-gate * The important other half of this is semexit, which will immediately
1050Sstevel@tonic-gate * take p_lock, obtain the AVL pointer, clear p_semacct, and drop
1060Sstevel@tonic-gate * p_lock. From this point on it is semexit's responsibility to clean
1070Sstevel@tonic-gate * up all undo structures found in the tree -- a coexecuting sem_rmid
1080Sstevel@tonic-gate * will see the NULL p_semacct and skip that undo structure. It walks
1090Sstevel@tonic-gate * the AVL tree (using avl_destroy_nodes) and for each undo structure
1100Sstevel@tonic-gate * takes the appropriate semaphore's ID lock (always legal since the
1110Sstevel@tonic-gate * undo structure has a hold on the semaphore), updates all semaphores
1120Sstevel@tonic-gate * with non-zero AOE values, and removes the structure from the
1130Sstevel@tonic-gate * semaphore's list. It then drops the structure's reference on the
1140Sstevel@tonic-gate * semaphore, drops the ID lock, and frees the undo structure.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate #include <sys/types.h>
1180Sstevel@tonic-gate #include <sys/t_lock.h>
1190Sstevel@tonic-gate #include <sys/param.h>
1200Sstevel@tonic-gate #include <sys/systm.h>
1210Sstevel@tonic-gate #include <sys/sysmacros.h>
1220Sstevel@tonic-gate #include <sys/cred.h>
1230Sstevel@tonic-gate #include <sys/vmem.h>
1240Sstevel@tonic-gate #include <sys/kmem.h>
1250Sstevel@tonic-gate #include <sys/errno.h>
1260Sstevel@tonic-gate #include <sys/time.h>
1270Sstevel@tonic-gate #include <sys/ipc.h>
1280Sstevel@tonic-gate #include <sys/ipc_impl.h>
1290Sstevel@tonic-gate #include <sys/sem.h>
1300Sstevel@tonic-gate #include <sys/sem_impl.h>
1310Sstevel@tonic-gate #include <sys/user.h>
1320Sstevel@tonic-gate #include <sys/proc.h>
1330Sstevel@tonic-gate #include <sys/cpuvar.h>
1340Sstevel@tonic-gate #include <sys/debug.h>
1350Sstevel@tonic-gate #include <sys/var.h>
1360Sstevel@tonic-gate #include <sys/cmn_err.h>
1370Sstevel@tonic-gate #include <sys/modctl.h>
1380Sstevel@tonic-gate #include <sys/syscall.h>
1390Sstevel@tonic-gate #include <sys/avl.h>
1400Sstevel@tonic-gate #include <sys/list.h>
1410Sstevel@tonic-gate #include <sys/zone.h>
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate #include <c2/audit.h>
1440Sstevel@tonic-gate
1452677Sml93401 extern rctl_hndl_t rc_zone_semmni;
1460Sstevel@tonic-gate extern rctl_hndl_t rc_project_semmni;
1470Sstevel@tonic-gate extern rctl_hndl_t rc_process_semmsl;
1480Sstevel@tonic-gate extern rctl_hndl_t rc_process_semopm;
1490Sstevel@tonic-gate static ipc_service_t *sem_svc;
1500Sstevel@tonic-gate static zone_key_t sem_zone_key;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate * The following tunables are obsolete. Though for compatibility we
1540Sstevel@tonic-gate * still read and interpret seminfo_semmsl, seminfo_semopm and
1550Sstevel@tonic-gate * seminfo_semmni (see os/project.c and os/rctl_proc.c), the preferred
1560Sstevel@tonic-gate * mechanism for administrating the IPC Semaphore facility is through
1570Sstevel@tonic-gate * the resource controls described at the top of this file.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate int seminfo_semaem = 16384; /* (obsolete) */
1600Sstevel@tonic-gate int seminfo_semmap = 10; /* (obsolete) */
1610Sstevel@tonic-gate int seminfo_semmni = 10; /* (obsolete) */
1620Sstevel@tonic-gate int seminfo_semmns = 60; /* (obsolete) */
1630Sstevel@tonic-gate int seminfo_semmnu = 30; /* (obsolete) */
1640Sstevel@tonic-gate int seminfo_semmsl = 25; /* (obsolete) */
1650Sstevel@tonic-gate int seminfo_semopm = 10; /* (obsolete) */
1660Sstevel@tonic-gate int seminfo_semume = 10; /* (obsolete) */
1670Sstevel@tonic-gate int seminfo_semusz = 96; /* (obsolete) */
1680Sstevel@tonic-gate int seminfo_semvmx = 32767; /* (obsolete) */
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate #define SEM_MAXUCOPS 4096 /* max # of unchecked ops per semop call */
1710Sstevel@tonic-gate #define SEM_UNDOSZ(n) (sizeof (struct sem_undo) + (n - 1) * sizeof (int))
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate static int semsys(int opcode, uintptr_t a0, uintptr_t a1,
1740Sstevel@tonic-gate uintptr_t a2, uintptr_t a3);
1750Sstevel@tonic-gate static void sem_dtor(kipc_perm_t *);
1760Sstevel@tonic-gate static void sem_rmid(kipc_perm_t *);
1770Sstevel@tonic-gate static void sem_remove_zone(zoneid_t, void *);
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate static struct sysent ipcsem_sysent = {
1800Sstevel@tonic-gate 5,
1810Sstevel@tonic-gate SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
1820Sstevel@tonic-gate semsys
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * Module linkage information for the kernel.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate static struct modlsys modlsys = {
1890Sstevel@tonic-gate &mod_syscallops, "System V semaphore facility", &ipcsem_sysent
1900Sstevel@tonic-gate };
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1930Sstevel@tonic-gate static struct modlsys modlsys32 = {
1940Sstevel@tonic-gate &mod_syscallops32, "32-bit System V semaphore facility", &ipcsem_sysent
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate static struct modlinkage modlinkage = {
1990Sstevel@tonic-gate MODREV_1,
2000Sstevel@tonic-gate &modlsys,
2010Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2020Sstevel@tonic-gate &modlsys32,
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate NULL
2050Sstevel@tonic-gate };
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate int
_init(void)2090Sstevel@tonic-gate _init(void)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate int result;
2120Sstevel@tonic-gate
2132677Sml93401 sem_svc = ipcs_create("semids", rc_project_semmni, rc_zone_semmni,
2142677Sml93401 sizeof (ksemid_t), sem_dtor, sem_rmid, AT_IPC_SEM,
2152677Sml93401 offsetof(ipc_rqty_t, ipcq_semmni));
2160Sstevel@tonic-gate zone_key_create(&sem_zone_key, NULL, sem_remove_zone, NULL);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if ((result = mod_install(&modlinkage)) == 0)
2190Sstevel@tonic-gate return (0);
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate (void) zone_key_delete(sem_zone_key);
2220Sstevel@tonic-gate ipcs_destroy(sem_svc);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate return (result);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate int
_fini(void)2280Sstevel@tonic-gate _fini(void)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate return (EBUSY);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2340Sstevel@tonic-gate _info(struct modinfo *modinfop)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate static void
sem_dtor(kipc_perm_t * perm)2400Sstevel@tonic-gate sem_dtor(kipc_perm_t *perm)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate ksemid_t *sp = (ksemid_t *)perm;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate kmem_free(sp->sem_base,
2450Sstevel@tonic-gate P2ROUNDUP(sp->sem_nsems * sizeof (struct sem), 64));
2460Sstevel@tonic-gate list_destroy(&sp->sem_undos);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * sem_undo_add - Create or update adjust on exit entry.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate static int
sem_undo_add(short val,ushort_t num,struct sem_undo * undo)2530Sstevel@tonic-gate sem_undo_add(short val, ushort_t num, struct sem_undo *undo)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate int newval = undo->un_aoe[num] - val;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (newval > USHRT_MAX || newval < -USHRT_MAX)
2580Sstevel@tonic-gate return (ERANGE);
2590Sstevel@tonic-gate undo->un_aoe[num] = newval;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * sem_undo_clear - clears all undo entries for specified semaphores
2660Sstevel@tonic-gate *
2670Sstevel@tonic-gate * Used when semaphores are reset by SETVAL or SETALL.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate static void
sem_undo_clear(ksemid_t * sp,ushort_t low,ushort_t high)2700Sstevel@tonic-gate sem_undo_clear(ksemid_t *sp, ushort_t low, ushort_t high)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate struct sem_undo *undo;
2730Sstevel@tonic-gate int i;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate ASSERT(low <= high);
2760Sstevel@tonic-gate ASSERT(high < sp->sem_nsems);
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate for (undo = list_head(&sp->sem_undos); undo;
2790Sstevel@tonic-gate undo = list_next(&sp->sem_undos, undo))
2800Sstevel@tonic-gate for (i = low; i <= high; i++)
2810Sstevel@tonic-gate undo->un_aoe[i] = 0;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * sem_rollback - roll back work done so far if unable to complete operation
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate static void
sem_rollback(ksemid_t * sp,struct sembuf * op,int n,struct sem_undo * undo)2880Sstevel@tonic-gate sem_rollback(ksemid_t *sp, struct sembuf *op, int n, struct sem_undo *undo)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate struct sem *semp; /* semaphore ptr */
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate for (op += n - 1; n--; op--) {
2930Sstevel@tonic-gate if (op->sem_op == 0)
2940Sstevel@tonic-gate continue;
2950Sstevel@tonic-gate semp = &sp->sem_base[op->sem_num];
2960Sstevel@tonic-gate semp->semval -= op->sem_op;
2970Sstevel@tonic-gate if (op->sem_flg & SEM_UNDO) {
2980Sstevel@tonic-gate ASSERT(undo != NULL);
2990Sstevel@tonic-gate (void) sem_undo_add(-op->sem_op, op->sem_num, undo);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate static void
sem_rmid(kipc_perm_t * perm)3050Sstevel@tonic-gate sem_rmid(kipc_perm_t *perm)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate ksemid_t *sp = (ksemid_t *)perm;
3080Sstevel@tonic-gate struct sem *semp;
3090Sstevel@tonic-gate struct sem_undo *undo;
3100Sstevel@tonic-gate size_t size = SEM_UNDOSZ(sp->sem_nsems);
3110Sstevel@tonic-gate int i;
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /*LINTED*/
3140Sstevel@tonic-gate while (undo = list_head(&sp->sem_undos)) {
3150Sstevel@tonic-gate list_remove(&sp->sem_undos, undo);
3160Sstevel@tonic-gate mutex_enter(&undo->un_proc->p_lock);
3170Sstevel@tonic-gate if (undo->un_proc->p_semacct == NULL) {
3180Sstevel@tonic-gate mutex_exit(&undo->un_proc->p_lock);
3190Sstevel@tonic-gate continue;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate avl_remove(undo->un_proc->p_semacct, undo);
3220Sstevel@tonic-gate mutex_exit(&undo->un_proc->p_lock);
3230Sstevel@tonic-gate kmem_free(undo, size);
3240Sstevel@tonic-gate ipc_rele_locked(sem_svc, (kipc_perm_t *)sp);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate for (i = 0; i < sp->sem_nsems; i++) {
3280Sstevel@tonic-gate semp = &sp->sem_base[i];
3290Sstevel@tonic-gate semp->semval = semp->sempid = 0;
3300Sstevel@tonic-gate if (semp->semncnt) {
3310Sstevel@tonic-gate cv_broadcast(&semp->semncnt_cv);
3320Sstevel@tonic-gate semp->semncnt = 0;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate if (semp->semzcnt) {
3350Sstevel@tonic-gate cv_broadcast(&semp->semzcnt_cv);
3360Sstevel@tonic-gate semp->semzcnt = 0;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate * semctl - Semctl system call.
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate static int
semctl(int semid,uint_t semnum,int cmd,uintptr_t arg)3450Sstevel@tonic-gate semctl(int semid, uint_t semnum, int cmd, uintptr_t arg)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate ksemid_t *sp; /* ptr to semaphore header */
3480Sstevel@tonic-gate struct sem *p; /* ptr to semaphore */
3490Sstevel@tonic-gate unsigned int i; /* loop control */
3500Sstevel@tonic-gate ushort_t *vals, *vp;
3510Sstevel@tonic-gate size_t vsize = 0;
3520Sstevel@tonic-gate int error = 0;
3530Sstevel@tonic-gate int retval = 0;
3540Sstevel@tonic-gate struct cred *cr;
3550Sstevel@tonic-gate kmutex_t *lock;
3560Sstevel@tonic-gate model_t mdl = get_udatamodel();
3570Sstevel@tonic-gate STRUCT_DECL(semid_ds, sid);
3580Sstevel@tonic-gate struct semid_ds64 ds64;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate STRUCT_INIT(sid, mdl);
3610Sstevel@tonic-gate cr = CRED();
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate * Perform pre- or non-lookup actions (e.g. copyins, RMID).
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate switch (cmd) {
3670Sstevel@tonic-gate case IPC_SET:
3680Sstevel@tonic-gate if (copyin((void *)arg, STRUCT_BUF(sid), STRUCT_SIZE(sid)))
3690Sstevel@tonic-gate return (set_errno(EFAULT));
3700Sstevel@tonic-gate break;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate case IPC_SET64:
3730Sstevel@tonic-gate if (copyin((void *)arg, &ds64, sizeof (struct semid_ds64)))
3740Sstevel@tonic-gate return (set_errno(EFAULT));
3750Sstevel@tonic-gate break;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate case SETALL:
3780Sstevel@tonic-gate if ((lock = ipc_lookup(sem_svc, semid,
3790Sstevel@tonic-gate (kipc_perm_t **)&sp)) == NULL)
3800Sstevel@tonic-gate return (set_errno(EINVAL));
3810Sstevel@tonic-gate vsize = sp->sem_nsems * sizeof (*vals);
3820Sstevel@tonic-gate mutex_exit(lock);
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate /* allocate space to hold all semaphore values */
3850Sstevel@tonic-gate vals = kmem_alloc(vsize, KM_SLEEP);
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (copyin((void *)arg, vals, vsize)) {
3880Sstevel@tonic-gate kmem_free(vals, vsize);
3890Sstevel@tonic-gate return (set_errno(EFAULT));
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate break;
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate case IPC_RMID:
3940Sstevel@tonic-gate if (error = ipc_rmid(sem_svc, semid, cr))
3950Sstevel@tonic-gate return (set_errno(error));
3960Sstevel@tonic-gate return (0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate if ((lock = ipc_lookup(sem_svc, semid, (kipc_perm_t **)&sp)) == NULL) {
4000Sstevel@tonic-gate if (vsize != 0)
4010Sstevel@tonic-gate kmem_free(vals, vsize);
4020Sstevel@tonic-gate return (set_errno(EINVAL));
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate switch (cmd) {
4050Sstevel@tonic-gate /* Set ownership and permissions. */
4060Sstevel@tonic-gate case IPC_SET:
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate if (error = ipcperm_set(sem_svc, cr, &sp->sem_perm,
4090Sstevel@tonic-gate &STRUCT_BUF(sid)->sem_perm, mdl)) {
4100Sstevel@tonic-gate mutex_exit(lock);
4110Sstevel@tonic-gate return (set_errno(error));
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate sp->sem_ctime = gethrestime_sec();
4140Sstevel@tonic-gate mutex_exit(lock);
4150Sstevel@tonic-gate return (0);
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate /* Get semaphore data structure. */
4180Sstevel@tonic-gate case IPC_STAT:
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
4210Sstevel@tonic-gate mutex_exit(lock);
4220Sstevel@tonic-gate return (set_errno(error));
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate ipcperm_stat(&STRUCT_BUF(sid)->sem_perm, &sp->sem_perm, mdl);
4260Sstevel@tonic-gate STRUCT_FSETP(sid, sem_base, NULL); /* kernel addr */
4270Sstevel@tonic-gate STRUCT_FSET(sid, sem_nsems, sp->sem_nsems);
4280Sstevel@tonic-gate STRUCT_FSET(sid, sem_otime, sp->sem_otime);
4290Sstevel@tonic-gate STRUCT_FSET(sid, sem_ctime, sp->sem_ctime);
4300Sstevel@tonic-gate STRUCT_FSET(sid, sem_binary, sp->sem_binary);
4310Sstevel@tonic-gate mutex_exit(lock);
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate if (copyout(STRUCT_BUF(sid), (void *)arg, STRUCT_SIZE(sid)))
4340Sstevel@tonic-gate return (set_errno(EFAULT));
4350Sstevel@tonic-gate return (0);
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate case IPC_SET64:
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate if (error = ipcperm_set64(sem_svc, cr, &sp->sem_perm,
4400Sstevel@tonic-gate &ds64.semx_perm)) {
4410Sstevel@tonic-gate mutex_exit(lock);
4420Sstevel@tonic-gate return (set_errno(error));
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate sp->sem_ctime = gethrestime_sec();
4450Sstevel@tonic-gate mutex_exit(lock);
4460Sstevel@tonic-gate return (0);
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate case IPC_STAT64:
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate ipcperm_stat64(&ds64.semx_perm, &sp->sem_perm);
4510Sstevel@tonic-gate ds64.semx_nsems = sp->sem_nsems;
4520Sstevel@tonic-gate ds64.semx_otime = sp->sem_otime;
4530Sstevel@tonic-gate ds64.semx_ctime = sp->sem_ctime;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate mutex_exit(lock);
4560Sstevel@tonic-gate if (copyout(&ds64, (void *)arg, sizeof (struct semid_ds64)))
4570Sstevel@tonic-gate return (set_errno(EFAULT));
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate return (0);
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate /* Get # of processes sleeping for greater semval. */
4620Sstevel@tonic-gate case GETNCNT:
4630Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
4640Sstevel@tonic-gate mutex_exit(lock);
4650Sstevel@tonic-gate return (set_errno(error));
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate if (semnum >= sp->sem_nsems) {
4680Sstevel@tonic-gate mutex_exit(lock);
4690Sstevel@tonic-gate return (set_errno(EINVAL));
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate retval = sp->sem_base[semnum].semncnt;
4720Sstevel@tonic-gate mutex_exit(lock);
4730Sstevel@tonic-gate return (retval);
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate /* Get pid of last process to operate on semaphore. */
4760Sstevel@tonic-gate case GETPID:
4770Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
4780Sstevel@tonic-gate mutex_exit(lock);
4790Sstevel@tonic-gate return (set_errno(error));
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate if (semnum >= sp->sem_nsems) {
4820Sstevel@tonic-gate mutex_exit(lock);
4830Sstevel@tonic-gate return (set_errno(EINVAL));
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate retval = sp->sem_base[semnum].sempid;
4860Sstevel@tonic-gate mutex_exit(lock);
4870Sstevel@tonic-gate return (retval);
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate /* Get semval of one semaphore. */
4900Sstevel@tonic-gate case GETVAL:
4910Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
4920Sstevel@tonic-gate mutex_exit(lock);
4930Sstevel@tonic-gate return (set_errno(error));
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate if (semnum >= sp->sem_nsems) {
4960Sstevel@tonic-gate mutex_exit(lock);
4970Sstevel@tonic-gate return (set_errno(EINVAL));
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate retval = sp->sem_base[semnum].semval;
5000Sstevel@tonic-gate mutex_exit(lock);
5010Sstevel@tonic-gate return (retval);
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /* Get all semvals in set. */
5040Sstevel@tonic-gate case GETALL:
5050Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
5060Sstevel@tonic-gate mutex_exit(lock);
5070Sstevel@tonic-gate return (set_errno(error));
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate /* allocate space to hold all semaphore values */
5110Sstevel@tonic-gate vsize = sp->sem_nsems * sizeof (*vals);
5120Sstevel@tonic-gate vals = vp = kmem_alloc(vsize, KM_SLEEP);
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate for (i = sp->sem_nsems, p = sp->sem_base; i--; p++, vp++)
5150Sstevel@tonic-gate bcopy(&p->semval, vp, sizeof (p->semval));
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate mutex_exit(lock);
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate if (copyout((void *)vals, (void *)arg, vsize)) {
5200Sstevel@tonic-gate kmem_free(vals, vsize);
5210Sstevel@tonic-gate return (set_errno(EFAULT));
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate kmem_free(vals, vsize);
5250Sstevel@tonic-gate return (0);
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate /* Get # of processes sleeping for semval to become zero. */
5280Sstevel@tonic-gate case GETZCNT:
5290Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
5300Sstevel@tonic-gate mutex_exit(lock);
5310Sstevel@tonic-gate return (set_errno(error));
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate if (semnum >= sp->sem_nsems) {
5340Sstevel@tonic-gate mutex_exit(lock);
5350Sstevel@tonic-gate return (set_errno(EINVAL));
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate retval = sp->sem_base[semnum].semzcnt;
5380Sstevel@tonic-gate mutex_exit(lock);
5390Sstevel@tonic-gate return (retval);
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate /* Set semval of one semaphore. */
5420Sstevel@tonic-gate case SETVAL:
5430Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_A, cr)) {
5440Sstevel@tonic-gate mutex_exit(lock);
5450Sstevel@tonic-gate return (set_errno(error));
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate if (semnum >= sp->sem_nsems) {
5480Sstevel@tonic-gate mutex_exit(lock);
5490Sstevel@tonic-gate return (set_errno(EINVAL));
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate if ((uint_t)arg > USHRT_MAX) {
5520Sstevel@tonic-gate mutex_exit(lock);
5530Sstevel@tonic-gate return (set_errno(ERANGE));
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate p = &sp->sem_base[semnum];
5560Sstevel@tonic-gate if ((p->semval = (ushort_t)arg) != 0) {
5570Sstevel@tonic-gate if (p->semncnt) {
5580Sstevel@tonic-gate cv_broadcast(&p->semncnt_cv);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate } else if (p->semzcnt) {
5610Sstevel@tonic-gate cv_broadcast(&p->semzcnt_cv);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate p->sempid = curproc->p_pid;
5640Sstevel@tonic-gate sem_undo_clear(sp, (ushort_t)semnum, (ushort_t)semnum);
5650Sstevel@tonic-gate mutex_exit(lock);
5660Sstevel@tonic-gate return (0);
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate /* Set semvals of all semaphores in set. */
5690Sstevel@tonic-gate case SETALL:
5700Sstevel@tonic-gate /* Check if semaphore set has been deleted and reallocated. */
5710Sstevel@tonic-gate if (sp->sem_nsems * sizeof (*vals) != vsize) {
5720Sstevel@tonic-gate error = set_errno(EINVAL);
5730Sstevel@tonic-gate goto seterr;
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, SEM_A, cr)) {
5760Sstevel@tonic-gate error = set_errno(error);
5770Sstevel@tonic-gate goto seterr;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate sem_undo_clear(sp, 0, sp->sem_nsems - 1);
5800Sstevel@tonic-gate for (i = 0, p = sp->sem_base; i < sp->sem_nsems;
5810Sstevel@tonic-gate (p++)->sempid = curproc->p_pid) {
5820Sstevel@tonic-gate if ((p->semval = vals[i++]) != 0) {
5830Sstevel@tonic-gate if (p->semncnt) {
5840Sstevel@tonic-gate cv_broadcast(&p->semncnt_cv);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate } else if (p->semzcnt) {
5870Sstevel@tonic-gate cv_broadcast(&p->semzcnt_cv);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate seterr:
5910Sstevel@tonic-gate mutex_exit(lock);
5920Sstevel@tonic-gate kmem_free(vals, vsize);
5930Sstevel@tonic-gate return (error);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate default:
5960Sstevel@tonic-gate mutex_exit(lock);
5970Sstevel@tonic-gate return (set_errno(EINVAL));
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /* NOTREACHED */
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate * semexit - Called by exit() to clean up on process exit.
6050Sstevel@tonic-gate */
6060Sstevel@tonic-gate void
semexit(proc_t * pp)6070Sstevel@tonic-gate semexit(proc_t *pp)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate avl_tree_t *tree;
6100Sstevel@tonic-gate struct sem_undo *undo;
6110Sstevel@tonic-gate void *cookie = NULL;
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate mutex_enter(&pp->p_lock);
6140Sstevel@tonic-gate tree = pp->p_semacct;
6150Sstevel@tonic-gate pp->p_semacct = NULL;
6160Sstevel@tonic-gate mutex_exit(&pp->p_lock);
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate while (undo = avl_destroy_nodes(tree, &cookie)) {
6190Sstevel@tonic-gate ksemid_t *sp = undo->un_sp;
6200Sstevel@tonic-gate size_t size = SEM_UNDOSZ(sp->sem_nsems);
6210Sstevel@tonic-gate int i;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate (void) ipc_lock(sem_svc, sp->sem_perm.ipc_id);
6240Sstevel@tonic-gate if (!IPC_FREE(&sp->sem_perm)) {
6250Sstevel@tonic-gate for (i = 0; i < sp->sem_nsems; i++) {
6260Sstevel@tonic-gate int adj = undo->un_aoe[i];
6270Sstevel@tonic-gate if (adj) {
6280Sstevel@tonic-gate struct sem *semp = &sp->sem_base[i];
6290Sstevel@tonic-gate int v = (int)semp->semval + adj;
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate if (v < 0 || v > USHRT_MAX)
6320Sstevel@tonic-gate continue;
6330Sstevel@tonic-gate semp->semval = (ushort_t)v;
6340Sstevel@tonic-gate if (v == 0 && semp->semzcnt)
6350Sstevel@tonic-gate cv_broadcast(&semp->semzcnt_cv);
6360Sstevel@tonic-gate if (adj > 0 && semp->semncnt)
6370Sstevel@tonic-gate cv_broadcast(&semp->semncnt_cv);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate list_remove(&sp->sem_undos, undo);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate ipc_rele(sem_svc, (kipc_perm_t *)sp);
6430Sstevel@tonic-gate kmem_free(undo, size);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate avl_destroy(tree);
6470Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t));
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate * Remove all semaphores associated with a given zone. Called by
6520Sstevel@tonic-gate * zone_shutdown when the zone is halted.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate /*ARGSUSED1*/
6550Sstevel@tonic-gate static void
sem_remove_zone(zoneid_t zoneid,void * arg)6560Sstevel@tonic-gate sem_remove_zone(zoneid_t zoneid, void *arg)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate ipc_remove_zone(sem_svc, zoneid);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate * semget - Semget system call.
6630Sstevel@tonic-gate */
6640Sstevel@tonic-gate static int
semget(key_t key,int nsems,int semflg)6650Sstevel@tonic-gate semget(key_t key, int nsems, int semflg)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate ksemid_t *sp;
6680Sstevel@tonic-gate kmutex_t *lock;
6690Sstevel@tonic-gate int id, error;
6700Sstevel@tonic-gate proc_t *pp = curproc;
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate top:
6730Sstevel@tonic-gate if (error = ipc_get(sem_svc, key, semflg, (kipc_perm_t **)&sp, &lock))
6740Sstevel@tonic-gate return (set_errno(error));
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate if (!IPC_FREE(&sp->sem_perm)) {
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate * A semaphore with the requested key exists.
6790Sstevel@tonic-gate */
6800Sstevel@tonic-gate if (!((nsems >= 0) && (nsems <= sp->sem_nsems))) {
6810Sstevel@tonic-gate mutex_exit(lock);
6820Sstevel@tonic-gate return (set_errno(EINVAL));
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate } else {
6850Sstevel@tonic-gate /*
6860Sstevel@tonic-gate * This is a new semaphore set. Finish initialization.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate if (nsems <= 0 || (rctl_test(rc_process_semmsl, pp->p_rctls, pp,
6890Sstevel@tonic-gate nsems, RCA_SAFE) & RCT_DENY)) {
6900Sstevel@tonic-gate mutex_exit(lock);
6910Sstevel@tonic-gate mutex_exit(&pp->p_lock);
6920Sstevel@tonic-gate ipc_cleanup(sem_svc, (kipc_perm_t *)sp);
6930Sstevel@tonic-gate return (set_errno(EINVAL));
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate mutex_exit(lock);
6960Sstevel@tonic-gate mutex_exit(&pp->p_lock);
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate * We round the allocation up to coherency granularity
7000Sstevel@tonic-gate * so that multiple semaphore allocations won't result
7010Sstevel@tonic-gate * in the false sharing of their sem structures.
7020Sstevel@tonic-gate */
7030Sstevel@tonic-gate sp->sem_base =
7040Sstevel@tonic-gate kmem_zalloc(P2ROUNDUP(nsems * sizeof (struct sem), 64),
7050Sstevel@tonic-gate KM_SLEEP);
7060Sstevel@tonic-gate sp->sem_binary = (nsems == 1);
7070Sstevel@tonic-gate sp->sem_nsems = (ushort_t)nsems;
7080Sstevel@tonic-gate sp->sem_ctime = gethrestime_sec();
7090Sstevel@tonic-gate sp->sem_otime = 0;
7100Sstevel@tonic-gate list_create(&sp->sem_undos, sizeof (struct sem_undo),
7110Sstevel@tonic-gate offsetof(struct sem_undo, un_list));
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate if (error = ipc_commit_begin(sem_svc, key, semflg,
7140Sstevel@tonic-gate (kipc_perm_t *)sp)) {
7150Sstevel@tonic-gate if (error == EAGAIN)
7160Sstevel@tonic-gate goto top;
7170Sstevel@tonic-gate return (set_errno(error));
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate sp->sem_maxops =
7200Sstevel@tonic-gate rctl_enforced_value(rc_process_semopm, pp->p_rctls, pp);
7210Sstevel@tonic-gate if (rctl_test(rc_process_semmsl, pp->p_rctls, pp, nsems,
7220Sstevel@tonic-gate RCA_SAFE) & RCT_DENY) {
7230Sstevel@tonic-gate ipc_cleanup(sem_svc, (kipc_perm_t *)sp);
7240Sstevel@tonic-gate return (set_errno(EINVAL));
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate lock = ipc_commit_end(sem_svc, &sp->sem_perm);
7270Sstevel@tonic-gate }
728*11861SMarek.Pospisil@Sun.COM
729*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING())
7300Sstevel@tonic-gate audit_ipcget(AT_IPC_SEM, (void *)sp);
731*11861SMarek.Pospisil@Sun.COM
7320Sstevel@tonic-gate id = sp->sem_perm.ipc_id;
7330Sstevel@tonic-gate mutex_exit(lock);
7340Sstevel@tonic-gate return (id);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate * semids system call.
7390Sstevel@tonic-gate */
7400Sstevel@tonic-gate static int
semids(int * buf,uint_t nids,uint_t * pnids)7410Sstevel@tonic-gate semids(int *buf, uint_t nids, uint_t *pnids)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate int error;
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate if (error = ipc_ids(sem_svc, buf, nids, pnids))
7460Sstevel@tonic-gate return (set_errno(error));
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate return (0);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate * Helper function for semop - copies in the provided timespec and
7540Sstevel@tonic-gate * computes the absolute future time after which we must return.
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate static int
compute_timeout(timespec_t ** tsp,timespec_t * ts,timespec_t * now,timespec_t * timeout)7570Sstevel@tonic-gate compute_timeout(timespec_t **tsp, timespec_t *ts, timespec_t *now,
7580Sstevel@tonic-gate timespec_t *timeout)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate model_t datamodel = get_udatamodel();
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) {
7630Sstevel@tonic-gate if (copyin(timeout, ts, sizeof (timespec_t)))
7640Sstevel@tonic-gate return (EFAULT);
7650Sstevel@tonic-gate } else {
7660Sstevel@tonic-gate timespec32_t ts32;
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate if (copyin(timeout, &ts32, sizeof (timespec32_t)))
7690Sstevel@tonic-gate return (EFAULT);
7700Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(ts, &ts32)
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate if (itimerspecfix(ts))
7740Sstevel@tonic-gate return (EINVAL);
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate /*
7770Sstevel@tonic-gate * Convert the timespec value into absolute time.
7780Sstevel@tonic-gate */
7790Sstevel@tonic-gate timespecadd(ts, now);
7800Sstevel@tonic-gate *tsp = ts;
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate return (0);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate * Undo structure comparator. We sort based on ksemid_t pointer.
7870Sstevel@tonic-gate */
7880Sstevel@tonic-gate static int
sem_undo_compar(const void * x,const void * y)7890Sstevel@tonic-gate sem_undo_compar(const void *x, const void *y)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate struct sem_undo *undo1 = (struct sem_undo *)x;
7920Sstevel@tonic-gate struct sem_undo *undo2 = (struct sem_undo *)y;
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate if (undo1->un_sp < undo2->un_sp)
7950Sstevel@tonic-gate return (-1);
7960Sstevel@tonic-gate if (undo1->un_sp > undo2->un_sp)
7970Sstevel@tonic-gate return (1);
7980Sstevel@tonic-gate return (0);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate /*
8020Sstevel@tonic-gate * Helper function for semop - creates an undo structure and adds it to
8030Sstevel@tonic-gate * the process's avl tree and the semaphore's list.
8040Sstevel@tonic-gate */
8050Sstevel@tonic-gate static int
sem_undo_alloc(proc_t * pp,ksemid_t * sp,kmutex_t ** lock,struct sem_undo * template,struct sem_undo ** un)8060Sstevel@tonic-gate sem_undo_alloc(proc_t *pp, ksemid_t *sp, kmutex_t **lock,
8070Sstevel@tonic-gate struct sem_undo *template, struct sem_undo **un)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate size_t size;
8100Sstevel@tonic-gate struct sem_undo *undo;
8110Sstevel@tonic-gate avl_tree_t *tree = NULL;
8120Sstevel@tonic-gate avl_index_t where;
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate mutex_exit(*lock);
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate size = SEM_UNDOSZ(sp->sem_nsems);
8170Sstevel@tonic-gate undo = kmem_zalloc(size, KM_SLEEP);
8180Sstevel@tonic-gate undo->un_proc = pp;
8190Sstevel@tonic-gate undo->un_sp = sp;
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate if (pp->p_semacct == NULL)
8220Sstevel@tonic-gate tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate *lock = ipc_lock(sem_svc, sp->sem_perm.ipc_id);
8250Sstevel@tonic-gate if (IPC_FREE(&sp->sem_perm)) {
8260Sstevel@tonic-gate kmem_free(undo, size);
8270Sstevel@tonic-gate if (tree)
8280Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t));
8290Sstevel@tonic-gate return (EIDRM);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate mutex_enter(&pp->p_lock);
8330Sstevel@tonic-gate if (tree) {
8340Sstevel@tonic-gate if (pp->p_semacct == NULL) {
8350Sstevel@tonic-gate avl_create(tree, sem_undo_compar,
8360Sstevel@tonic-gate sizeof (struct sem_undo),
8370Sstevel@tonic-gate offsetof(struct sem_undo, un_avl));
8380Sstevel@tonic-gate pp->p_semacct = tree;
8390Sstevel@tonic-gate } else {
8400Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t));
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate if (*un = avl_find(pp->p_semacct, template, &where)) {
8450Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8460Sstevel@tonic-gate kmem_free(undo, size);
8470Sstevel@tonic-gate } else {
8480Sstevel@tonic-gate *un = undo;
8490Sstevel@tonic-gate avl_insert(pp->p_semacct, undo, where);
8500Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8510Sstevel@tonic-gate list_insert_head(&sp->sem_undos, undo);
8520Sstevel@tonic-gate ipc_hold(sem_svc, (kipc_perm_t *)sp);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate return (0);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate /*
8600Sstevel@tonic-gate * semop - Semop system call.
8610Sstevel@tonic-gate */
8620Sstevel@tonic-gate static int
semop(int semid,struct sembuf * sops,size_t nsops,timespec_t * timeout)8630Sstevel@tonic-gate semop(int semid, struct sembuf *sops, size_t nsops, timespec_t *timeout)
8640Sstevel@tonic-gate {
8650Sstevel@tonic-gate ksemid_t *sp = NULL;
8660Sstevel@tonic-gate kmutex_t *lock;
8670Sstevel@tonic-gate struct sembuf *op; /* ptr to operation */
8680Sstevel@tonic-gate int i; /* loop control */
8690Sstevel@tonic-gate struct sem *semp; /* ptr to semaphore */
8700Sstevel@tonic-gate int error = 0;
8710Sstevel@tonic-gate struct sembuf *uops; /* ptr to copy of user ops */
8720Sstevel@tonic-gate struct sembuf x_sem; /* avoid kmem_alloc's */
8730Sstevel@tonic-gate timespec_t now, ts, *tsp = NULL;
8744123Sdm120769 int timecheck = 0;
8750Sstevel@tonic-gate int cvres, needundo, mode;
8760Sstevel@tonic-gate struct sem_undo *undo;
8770Sstevel@tonic-gate proc_t *pp = curproc;
8780Sstevel@tonic-gate int held = 0;
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, sys, sema, 1); /* bump semaphore op count */
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate * To avoid the cost of copying in 'timeout' in the common
8840Sstevel@tonic-gate * case, we could only grab the time here and defer the copyin
8850Sstevel@tonic-gate * and associated computations until we are about to block.
8860Sstevel@tonic-gate *
8870Sstevel@tonic-gate * The down side to this is that we would then have to spin
8880Sstevel@tonic-gate * some goto top nonsense to avoid the copyin behind the semid
8890Sstevel@tonic-gate * lock. As a common use of timed semaphores is as an explicit
8900Sstevel@tonic-gate * blocking mechanism, this could incur a greater penalty.
8910Sstevel@tonic-gate *
8920Sstevel@tonic-gate * If we eventually decide that this would be a wise route to
8930Sstevel@tonic-gate * take, the deferrable functionality is completely contained
8940Sstevel@tonic-gate * in 'compute_timeout', and the interface is defined such that
8950Sstevel@tonic-gate * we can legally not validate 'timeout' if it is unused.
8960Sstevel@tonic-gate */
8970Sstevel@tonic-gate if (timeout != NULL) {
8984123Sdm120769 timecheck = timechanged;
8990Sstevel@tonic-gate gethrestime(&now);
9000Sstevel@tonic-gate if (error = compute_timeout(&tsp, &ts, &now, timeout))
9010Sstevel@tonic-gate return (set_errno(error));
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate /*
9050Sstevel@tonic-gate * Allocate space to hold the vector of semaphore ops. If
9060Sstevel@tonic-gate * there is only 1 operation we use a preallocated buffer on
9070Sstevel@tonic-gate * the stack for speed.
9080Sstevel@tonic-gate *
9090Sstevel@tonic-gate * Since we don't want to allow the user to allocate an
9100Sstevel@tonic-gate * arbitrary amount of kernel memory, we need to check against
9110Sstevel@tonic-gate * the number of operations allowed by the semaphore. We only
9120Sstevel@tonic-gate * bother doing this if the number of operations is larger than
9130Sstevel@tonic-gate * SEM_MAXUCOPS.
9140Sstevel@tonic-gate */
9150Sstevel@tonic-gate if (nsops == 1)
9160Sstevel@tonic-gate uops = &x_sem;
9170Sstevel@tonic-gate else if (nsops == 0)
9180Sstevel@tonic-gate return (0);
9190Sstevel@tonic-gate else if (nsops <= SEM_MAXUCOPS)
9200Sstevel@tonic-gate uops = kmem_alloc(nsops * sizeof (*uops), KM_SLEEP);
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate if (nsops > SEM_MAXUCOPS) {
9230Sstevel@tonic-gate if ((lock = ipc_lookup(sem_svc, semid,
9240Sstevel@tonic-gate (kipc_perm_t **)&sp)) == NULL)
9250Sstevel@tonic-gate return (set_errno(EFAULT));
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate if (nsops > sp->sem_maxops) {
9280Sstevel@tonic-gate mutex_exit(lock);
9290Sstevel@tonic-gate return (set_errno(E2BIG));
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate held = 1;
9320Sstevel@tonic-gate ipc_hold(sem_svc, (kipc_perm_t *)sp);
9330Sstevel@tonic-gate mutex_exit(lock);
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate uops = kmem_alloc(nsops * sizeof (*uops), KM_SLEEP);
9360Sstevel@tonic-gate if (copyin(sops, uops, nsops * sizeof (*op))) {
9370Sstevel@tonic-gate error = EFAULT;
9380Sstevel@tonic-gate (void) ipc_lock(sem_svc, sp->sem_perm.ipc_id);
9390Sstevel@tonic-gate goto semoperr;
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate lock = ipc_lock(sem_svc, sp->sem_perm.ipc_id);
9430Sstevel@tonic-gate if (IPC_FREE(&sp->sem_perm)) {
9440Sstevel@tonic-gate error = EIDRM;
9450Sstevel@tonic-gate goto semoperr;
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate } else {
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate * This could be interleaved with the above code, but
9500Sstevel@tonic-gate * keeping them separate improves readability.
9510Sstevel@tonic-gate */
9520Sstevel@tonic-gate if (copyin(sops, uops, nsops * sizeof (*op))) {
9530Sstevel@tonic-gate error = EFAULT;
9540Sstevel@tonic-gate goto semoperr_unlocked;
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate if ((lock = ipc_lookup(sem_svc, semid,
9580Sstevel@tonic-gate (kipc_perm_t **)&sp)) == NULL) {
9590Sstevel@tonic-gate error = EINVAL;
9600Sstevel@tonic-gate goto semoperr_unlocked;
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate if (nsops > sp->sem_maxops) {
9640Sstevel@tonic-gate error = E2BIG;
9650Sstevel@tonic-gate goto semoperr;
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /*
9700Sstevel@tonic-gate * Scan all operations. Verify that sem #s are in range and
9710Sstevel@tonic-gate * this process is allowed the requested operations. If any
9720Sstevel@tonic-gate * operations are marked SEM_UNDO, find (or allocate) the undo
9730Sstevel@tonic-gate * structure for this process and semaphore.
9740Sstevel@tonic-gate */
9750Sstevel@tonic-gate needundo = 0;
9760Sstevel@tonic-gate mode = 0;
9770Sstevel@tonic-gate for (i = 0, op = uops; i++ < nsops; op++) {
9780Sstevel@tonic-gate mode |= op->sem_op ? SEM_A : SEM_R;
9790Sstevel@tonic-gate if (op->sem_num >= sp->sem_nsems) {
9800Sstevel@tonic-gate error = EFBIG;
9810Sstevel@tonic-gate goto semoperr;
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate if ((op->sem_flg & SEM_UNDO) && op->sem_op)
9840Sstevel@tonic-gate needundo = 1;
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, mode, CRED()))
9870Sstevel@tonic-gate goto semoperr;
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate if (needundo) {
9900Sstevel@tonic-gate struct sem_undo template;
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate template.un_sp = sp;
9930Sstevel@tonic-gate mutex_enter(&pp->p_lock);
9940Sstevel@tonic-gate if (pp->p_semacct)
9950Sstevel@tonic-gate undo = avl_find(pp->p_semacct, &template, NULL);
9960Sstevel@tonic-gate else
9970Sstevel@tonic-gate undo = NULL;
9980Sstevel@tonic-gate mutex_exit(&pp->p_lock);
9990Sstevel@tonic-gate if (undo == NULL) {
10008136SAmrita.Sadhukhan@Sun.COM if (!held) {
10018136SAmrita.Sadhukhan@Sun.COM held = 1;
10028136SAmrita.Sadhukhan@Sun.COM ipc_hold(sem_svc, (kipc_perm_t *)sp);
10038136SAmrita.Sadhukhan@Sun.COM }
10040Sstevel@tonic-gate if (error = sem_undo_alloc(pp, sp, &lock, &template,
10050Sstevel@tonic-gate &undo))
10060Sstevel@tonic-gate goto semoperr;
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate /* sem_undo_alloc unlocks the semaphore */
10090Sstevel@tonic-gate if (error = ipcperm_access(&sp->sem_perm, mode, CRED()))
10100Sstevel@tonic-gate goto semoperr;
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate check:
10150Sstevel@tonic-gate /*
10160Sstevel@tonic-gate * Loop waiting for the operations to be satisfied atomically.
10170Sstevel@tonic-gate * Actually, do the operations and undo them if a wait is needed
10180Sstevel@tonic-gate * or an error is detected.
10190Sstevel@tonic-gate */
10200Sstevel@tonic-gate for (i = 0; i < nsops; i++) {
10210Sstevel@tonic-gate op = &uops[i];
10220Sstevel@tonic-gate semp = &sp->sem_base[op->sem_num];
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate * Raise the semaphore (i.e. sema_v)
10260Sstevel@tonic-gate */
10270Sstevel@tonic-gate if (op->sem_op > 0) {
10280Sstevel@tonic-gate if (op->sem_op + (int)semp->semval > USHRT_MAX ||
10290Sstevel@tonic-gate ((op->sem_flg & SEM_UNDO) &&
10300Sstevel@tonic-gate (error = sem_undo_add(op->sem_op, op->sem_num,
10310Sstevel@tonic-gate undo)))) {
10320Sstevel@tonic-gate if (i)
10330Sstevel@tonic-gate sem_rollback(sp, uops, i, undo);
10340Sstevel@tonic-gate if (error == 0)
10350Sstevel@tonic-gate error = ERANGE;
10360Sstevel@tonic-gate goto semoperr;
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate semp->semval += op->sem_op;
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate * If we are only incrementing the semaphore value
10410Sstevel@tonic-gate * by one on a binary semaphore, we can cv_signal.
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate if (semp->semncnt) {
10440Sstevel@tonic-gate if (op->sem_op == 1 && sp->sem_binary)
10450Sstevel@tonic-gate cv_signal(&semp->semncnt_cv);
10460Sstevel@tonic-gate else
10470Sstevel@tonic-gate cv_broadcast(&semp->semncnt_cv);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate if (semp->semzcnt && !semp->semval)
10500Sstevel@tonic-gate cv_broadcast(&semp->semzcnt_cv);
10510Sstevel@tonic-gate continue;
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate /*
10550Sstevel@tonic-gate * Lower the semaphore (i.e. sema_p)
10560Sstevel@tonic-gate */
10570Sstevel@tonic-gate if (op->sem_op < 0) {
10580Sstevel@tonic-gate if (semp->semval >= (unsigned)(-op->sem_op)) {
10590Sstevel@tonic-gate if ((op->sem_flg & SEM_UNDO) &&
10600Sstevel@tonic-gate (error = sem_undo_add(op->sem_op,
10610Sstevel@tonic-gate op->sem_num, undo))) {
10620Sstevel@tonic-gate if (i)
10630Sstevel@tonic-gate sem_rollback(sp, uops, i, undo);
10640Sstevel@tonic-gate goto semoperr;
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate semp->semval += op->sem_op;
10670Sstevel@tonic-gate if (semp->semzcnt && !semp->semval)
10680Sstevel@tonic-gate cv_broadcast(&semp->semzcnt_cv);
10690Sstevel@tonic-gate continue;
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate if (i)
10720Sstevel@tonic-gate sem_rollback(sp, uops, i, undo);
10730Sstevel@tonic-gate if (op->sem_flg & IPC_NOWAIT) {
10740Sstevel@tonic-gate error = EAGAIN;
10750Sstevel@tonic-gate goto semoperr;
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate * Mark the semaphore set as not a binary type
10800Sstevel@tonic-gate * if we are decrementing the value by more than 1.
10810Sstevel@tonic-gate *
10820Sstevel@tonic-gate * V operations will resort to cv_broadcast
10830Sstevel@tonic-gate * for this set because there are too many weird
10840Sstevel@tonic-gate * cases that have to be caught.
10850Sstevel@tonic-gate */
10860Sstevel@tonic-gate if (op->sem_op < -1)
10870Sstevel@tonic-gate sp->sem_binary = 0;
10880Sstevel@tonic-gate if (!held) {
10890Sstevel@tonic-gate held = 1;
10900Sstevel@tonic-gate ipc_hold(sem_svc, (kipc_perm_t *)sp);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate semp->semncnt++;
10934123Sdm120769 cvres = cv_waituntil_sig(&semp->semncnt_cv, lock,
10945753Sgww tsp, timecheck);
10950Sstevel@tonic-gate lock = ipc_relock(sem_svc, sp->sem_perm.ipc_id, lock);
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate if (!IPC_FREE(&sp->sem_perm)) {
10980Sstevel@tonic-gate ASSERT(semp->semncnt != 0);
10990Sstevel@tonic-gate semp->semncnt--;
11000Sstevel@tonic-gate if (cvres > 0) /* normal wakeup */
11010Sstevel@tonic-gate goto check;
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate /* EINTR or EAGAIN overrides EIDRM */
11050Sstevel@tonic-gate if (cvres == 0)
11060Sstevel@tonic-gate error = EINTR;
11070Sstevel@tonic-gate else if (cvres < 0)
11080Sstevel@tonic-gate error = EAGAIN;
11090Sstevel@tonic-gate else
11100Sstevel@tonic-gate error = EIDRM;
11110Sstevel@tonic-gate goto semoperr;
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate * Wait for zero value
11160Sstevel@tonic-gate */
11170Sstevel@tonic-gate if (semp->semval) {
11180Sstevel@tonic-gate if (i)
11190Sstevel@tonic-gate sem_rollback(sp, uops, i, undo);
11200Sstevel@tonic-gate if (op->sem_flg & IPC_NOWAIT) {
11210Sstevel@tonic-gate error = EAGAIN;
11220Sstevel@tonic-gate goto semoperr;
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate if (!held) {
11260Sstevel@tonic-gate held = 1;
11270Sstevel@tonic-gate ipc_hold(sem_svc, (kipc_perm_t *)sp);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate semp->semzcnt++;
11304123Sdm120769 cvres = cv_waituntil_sig(&semp->semzcnt_cv, lock,
11315753Sgww tsp, timecheck);
11320Sstevel@tonic-gate lock = ipc_relock(sem_svc, sp->sem_perm.ipc_id, lock);
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate * Don't touch semp if the semaphores have been removed.
11360Sstevel@tonic-gate */
11370Sstevel@tonic-gate if (!IPC_FREE(&sp->sem_perm)) {
11380Sstevel@tonic-gate ASSERT(semp->semzcnt != 0);
11390Sstevel@tonic-gate semp->semzcnt--;
11400Sstevel@tonic-gate if (cvres > 0) /* normal wakeup */
11410Sstevel@tonic-gate goto check;
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate /* EINTR or EAGAIN overrides EIDRM */
11450Sstevel@tonic-gate if (cvres == 0)
11460Sstevel@tonic-gate error = EINTR;
11470Sstevel@tonic-gate else if (cvres < 0)
11480Sstevel@tonic-gate error = EAGAIN;
11490Sstevel@tonic-gate else
11500Sstevel@tonic-gate error = EIDRM;
11510Sstevel@tonic-gate goto semoperr;
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate /* All operations succeeded. Update sempid for accessed semaphores. */
11560Sstevel@tonic-gate for (i = 0, op = uops; i++ < nsops;
11570Sstevel@tonic-gate sp->sem_base[(op++)->sem_num].sempid = pp->p_pid)
11580Sstevel@tonic-gate ;
11590Sstevel@tonic-gate sp->sem_otime = gethrestime_sec();
11600Sstevel@tonic-gate if (held)
11610Sstevel@tonic-gate ipc_rele(sem_svc, (kipc_perm_t *)sp);
11620Sstevel@tonic-gate else
11630Sstevel@tonic-gate mutex_exit(lock);
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate /* Before leaving, deallocate the buffer that held the user semops */
11660Sstevel@tonic-gate if (nsops != 1)
11670Sstevel@tonic-gate kmem_free(uops, sizeof (*uops) * nsops);
11680Sstevel@tonic-gate return (0);
11690Sstevel@tonic-gate
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate * Error return labels
11720Sstevel@tonic-gate */
11730Sstevel@tonic-gate semoperr:
11740Sstevel@tonic-gate if (held)
11750Sstevel@tonic-gate ipc_rele(sem_svc, (kipc_perm_t *)sp);
11760Sstevel@tonic-gate else
11770Sstevel@tonic-gate mutex_exit(lock);
11780Sstevel@tonic-gate
11790Sstevel@tonic-gate semoperr_unlocked:
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate /* Before leaving, deallocate the buffer that held the user semops */
11820Sstevel@tonic-gate if (nsops != 1)
11830Sstevel@tonic-gate kmem_free(uops, sizeof (*uops) * nsops);
11840Sstevel@tonic-gate return (set_errno(error));
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate * semsys - System entry point for semctl, semget, and semop system calls.
11890Sstevel@tonic-gate */
11900Sstevel@tonic-gate static int
semsys(int opcode,uintptr_t a1,uintptr_t a2,uintptr_t a3,uintptr_t a4)11910Sstevel@tonic-gate semsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate int error;
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate switch (opcode) {
11960Sstevel@tonic-gate case SEMCTL:
11970Sstevel@tonic-gate error = semctl((int)a1, (uint_t)a2, (int)a3, a4);
11980Sstevel@tonic-gate break;
11990Sstevel@tonic-gate case SEMGET:
12000Sstevel@tonic-gate error = semget((key_t)a1, (int)a2, (int)a3);
12010Sstevel@tonic-gate break;
12020Sstevel@tonic-gate case SEMOP:
12030Sstevel@tonic-gate error = semop((int)a1, (struct sembuf *)a2, (size_t)a3, 0);
12040Sstevel@tonic-gate break;
12050Sstevel@tonic-gate case SEMIDS:
12060Sstevel@tonic-gate error = semids((int *)a1, (uint_t)a2, (uint_t *)a3);
12070Sstevel@tonic-gate break;
12080Sstevel@tonic-gate case SEMTIMEDOP:
12090Sstevel@tonic-gate error = semop((int)a1, (struct sembuf *)a2, (size_t)a3,
12100Sstevel@tonic-gate (timespec_t *)a4);
12110Sstevel@tonic-gate break;
12120Sstevel@tonic-gate default:
12130Sstevel@tonic-gate error = set_errno(EINVAL);
12140Sstevel@tonic-gate break;
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate return (error);
12170Sstevel@tonic-gate }
1218