Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.70
      1 /*	$NetBSD: sysv_sem.c,v 1.70 2007/07/09 21:10:56 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Implementation of SVID semaphores
     42  *
     43  * Author: Daniel Boulet
     44  *
     45  * This software is provided ``AS IS'' without any warranties of any kind.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.70 2007/07/09 21:10:56 ad Exp $");
     50 
     51 #define SYSVSEM
     52 
     53 #include <sys/param.h>
     54 #include <sys/kernel.h>
     55 #include <sys/sem.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/kmem.h>
     58 #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     59 #include <sys/syscallargs.h>
     60 #include <sys/kauth.h>
     61 
     62 static int	semtot = 0;
     63 struct	semid_ds *sema;			/* semaphore id pool */
     64 static struct	__sem *sem;		/* semaphore pool */
     65 static struct	sem_undo *semu_list;	/* list of active undo structures */
     66 static int	*semu;			/* undo structure pool */
     67 static kcondvar_t *semcv;
     68 static kmutex_t semlock;
     69 
     70 #ifdef SEM_DEBUG
     71 #define SEM_PRINTF(a) printf a
     72 #else
     73 #define SEM_PRINTF(a)
     74 #endif
     75 
     76 struct sem_undo *semu_alloc(struct proc *);
     77 int semundo_adjust(struct proc *, struct sem_undo **, int, int, int);
     78 void semundo_clear(int, int);
     79 
     80 /*
     81  * XXXSMP Once we go MP, there needs to be a lock for the semaphore system.
     82  * Until then, we're saved by being a non-preemptive kernel.
     83  */
     84 
     85 void
     86 seminit(void)
     87 {
     88 	int i, sz;
     89 	vaddr_t v;
     90 
     91 	mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE);
     92 
     93 	/* Allocate pageable memory for our structures */
     94 	sz = seminfo.semmni * sizeof(struct semid_ds) +
     95 	    seminfo.semmns * sizeof(struct __sem) +
     96 	    seminfo.semmnu * seminfo.semusz +
     97 	    seminfo.semmni * sizeof(kcondvar_t);
     98 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
     99 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    100 	if (v == 0)
    101 		panic("sysv_sem: cannot allocate memory");
    102 	sema = (void *)v;
    103 	sem = (void *)(sema + seminfo.semmni);
    104 	semu = (void *)(sem + seminfo.semmns);
    105 	semcv = (void *)(semu + seminfo.semmnu);
    106 
    107 	for (i = 0; i < seminfo.semmni; i++) {
    108 		sema[i]._sem_base = 0;
    109 		sema[i].sem_perm.mode = 0;
    110 		cv_init(&semcv[i], "semwait");
    111 	}
    112 	for (i = 0; i < seminfo.semmnu; i++) {
    113 		struct sem_undo *suptr = SEMU(i);
    114 		suptr->un_proc = NULL;
    115 	}
    116 	semu_list = NULL;
    117 	exithook_establish(semexit, NULL);
    118 }
    119 
    120 /*
    121  * Placebo.
    122  */
    123 
    124 int
    125 sys_semconfig(struct lwp *l, void *v, register_t *retval)
    126 {
    127 
    128 	*retval = 0;
    129 	return 0;
    130 }
    131 
    132 /*
    133  * Allocate a new sem_undo structure for a process
    134  * (returns ptr to structure or NULL if no more room)
    135  */
    136 
    137 struct sem_undo *
    138 semu_alloc(struct proc *p)
    139 {
    140 	int i;
    141 	struct sem_undo *suptr;
    142 	struct sem_undo **supptr;
    143 	int attempt;
    144 
    145 	KASSERT(mutex_owned(&semlock));
    146 
    147 	/*
    148 	 * Try twice to allocate something.
    149 	 * (we'll purge any empty structures after the first pass so
    150 	 * two passes are always enough)
    151 	 */
    152 
    153 	for (attempt = 0; attempt < 2; attempt++) {
    154 		/*
    155 		 * Look for a free structure.
    156 		 * Fill it in and return it if we find one.
    157 		 */
    158 
    159 		for (i = 0; i < seminfo.semmnu; i++) {
    160 			suptr = SEMU(i);
    161 			if (suptr->un_proc == NULL) {
    162 				suptr->un_next = semu_list;
    163 				semu_list = suptr;
    164 				suptr->un_cnt = 0;
    165 				suptr->un_proc = p;
    166 				return (suptr);
    167 			}
    168 		}
    169 
    170 		/*
    171 		 * We didn't find a free one, if this is the first attempt
    172 		 * then try to free some structures.
    173 		 */
    174 
    175 		if (attempt == 0) {
    176 			/* All the structures are in use - try to free some */
    177 			int did_something = 0;
    178 
    179 			supptr = &semu_list;
    180 			while ((suptr = *supptr) != NULL) {
    181 				if (suptr->un_cnt == 0)  {
    182 					suptr->un_proc = NULL;
    183 					*supptr = suptr->un_next;
    184 					did_something = 1;
    185 				} else
    186 					supptr = &suptr->un_next;
    187 			}
    188 
    189 			/* If we didn't free anything then just give-up */
    190 			if (!did_something)
    191 				return (NULL);
    192 		} else {
    193 			/*
    194 			 * The second pass failed even though we freed
    195 			 * something after the first pass!
    196 			 * This is IMPOSSIBLE!
    197 			 */
    198 			panic("semu_alloc - second attempt failed");
    199 		}
    200 	}
    201 	return NULL;
    202 }
    203 
    204 /*
    205  * Adjust a particular entry for a particular proc
    206  */
    207 
    208 int
    209 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
    210     int adjval)
    211 {
    212 	struct sem_undo *suptr;
    213 	struct undo *sunptr;
    214 	int i;
    215 
    216 	KASSERT(mutex_owned(&semlock));
    217 
    218 	/*
    219 	 * Look for and remember the sem_undo if the caller doesn't
    220 	 * provide it
    221 	 */
    222 
    223 	suptr = *supptr;
    224 	if (suptr == NULL) {
    225 		for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
    226 			if (suptr->un_proc == p)
    227 				break;
    228 
    229 		if (suptr == NULL) {
    230 			suptr = semu_alloc(p);
    231 			if (suptr == NULL)
    232 				return (ENOSPC);
    233 		}
    234 		*supptr = suptr;
    235 	}
    236 
    237 	/*
    238 	 * Look for the requested entry and adjust it (delete if
    239 	 * adjval becomes 0).
    240 	 */
    241 	sunptr = &suptr->un_ent[0];
    242 	for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    243 		if (sunptr->un_id != semid || sunptr->un_num != semnum)
    244 			continue;
    245 		sunptr->un_adjval += adjval;
    246 		if (sunptr->un_adjval == 0) {
    247 			suptr->un_cnt--;
    248 			if (i < suptr->un_cnt)
    249 				suptr->un_ent[i] =
    250 				    suptr->un_ent[suptr->un_cnt];
    251 		}
    252 		return (0);
    253 	}
    254 
    255 	/* Didn't find the right entry - create it */
    256 	if (suptr->un_cnt == SEMUME)
    257 		return (EINVAL);
    258 
    259 	sunptr = &suptr->un_ent[suptr->un_cnt];
    260 	suptr->un_cnt++;
    261 	sunptr->un_adjval = adjval;
    262 	sunptr->un_id = semid;
    263 	sunptr->un_num = semnum;
    264 	return (0);
    265 }
    266 
    267 void
    268 semundo_clear(int semid, int semnum)
    269 {
    270 	struct sem_undo *suptr;
    271 	struct undo *sunptr, *sunend;
    272 
    273 	KASSERT(mutex_owned(&semlock));
    274 
    275 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
    276 		for (sunptr = &suptr->un_ent[0],
    277 		    sunend = sunptr + suptr->un_cnt; sunptr < sunend;) {
    278 			if (sunptr->un_id == semid) {
    279 				if (semnum == -1 || sunptr->un_num == semnum) {
    280 					suptr->un_cnt--;
    281 					sunend--;
    282 					if (sunptr != sunend)
    283 						*sunptr = *sunend;
    284 					if (semnum != -1)
    285 						break;
    286 					else
    287 						continue;
    288 				}
    289 			}
    290 			sunptr++;
    291 		}
    292 }
    293 
    294 int
    295 sys_____semctl13(struct lwp *l, void *v, register_t *retval)
    296 {
    297 	struct sys_____semctl13_args /* {
    298 		syscallarg(int) semid;
    299 		syscallarg(int) semnum;
    300 		syscallarg(int) cmd;
    301 		syscallarg(union __semun *) arg;
    302 	} */ *uap = v;
    303 	struct semid_ds sembuf;
    304 	int cmd, error;
    305 	void *pass_arg;
    306 	union __semun karg;
    307 
    308 	cmd = SCARG(uap, cmd);
    309 
    310 	pass_arg = get_semctl_arg(cmd, &sembuf, &karg);
    311 
    312 	if (pass_arg) {
    313 		error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
    314 		if (error)
    315 			return error;
    316 		if (cmd == IPC_SET) {
    317 			error = copyin(karg.buf, &sembuf, sizeof(sembuf));
    318 			if (error)
    319 				return (error);
    320 		}
    321 	}
    322 
    323 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    324 	    pass_arg, retval);
    325 
    326 	if (error == 0 && cmd == IPC_STAT)
    327 		error = copyout(&sembuf, karg.buf, sizeof(sembuf));
    328 
    329 	return (error);
    330 }
    331 
    332 int
    333 semctl1(struct lwp *l, int semid, int semnum, int cmd, void *v,
    334     register_t *retval)
    335 {
    336 	kauth_cred_t cred = l->l_cred;
    337 	union __semun *arg = v;
    338 	struct semid_ds *sembuf = v, *semaptr;
    339 	int i, error, ix;
    340 
    341 	SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
    342 	    semid, semnum, cmd, v));
    343 
    344 	mutex_enter(&semlock);
    345 
    346 	ix = IPCID_TO_IX(semid);
    347 	if (ix < 0 || ix >= seminfo.semmni) {
    348 		mutex_exit(&semlock);
    349 		return (EINVAL);
    350 	}
    351 
    352 	semaptr = &sema[ix];
    353 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    354 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(semid)) {
    355 		mutex_exit(&semlock);
    356 		return (EINVAL);
    357 	}
    358 
    359 	switch (cmd) {
    360 	case IPC_RMID:
    361 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
    362 			break;
    363 		semaptr->sem_perm.cuid = kauth_cred_geteuid(cred);
    364 		semaptr->sem_perm.uid = kauth_cred_geteuid(cred);
    365 		semtot -= semaptr->sem_nsems;
    366 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
    367 			sem[i] = sem[i + semaptr->sem_nsems];
    368 		for (i = 0; i < seminfo.semmni; i++) {
    369 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    370 			    sema[i]._sem_base > semaptr->_sem_base)
    371 				sema[i]._sem_base -= semaptr->sem_nsems;
    372 		}
    373 		semaptr->sem_perm.mode = 0;
    374 		semundo_clear(ix, -1);
    375 		cv_broadcast(&semcv[ix]);
    376 		break;
    377 
    378 	case IPC_SET:
    379 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    380 			break;
    381 		KASSERT(sembuf != NULL);
    382 		semaptr->sem_perm.uid = sembuf->sem_perm.uid;
    383 		semaptr->sem_perm.gid = sembuf->sem_perm.gid;
    384 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    385 		    (sembuf->sem_perm.mode & 0777);
    386 		semaptr->sem_ctime = time_second;
    387 		break;
    388 
    389 	case IPC_STAT:
    390 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    391 			break;
    392 		KASSERT(sembuf != NULL);
    393 		memcpy(sembuf, semaptr, sizeof(struct semid_ds));
    394 		break;
    395 
    396 	case GETNCNT:
    397 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    398 			break;
    399 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    400 			error = EINVAL;
    401 			break;
    402 		}
    403 		*retval = semaptr->_sem_base[semnum].semncnt;
    404 		break;
    405 
    406 	case GETPID:
    407 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    408 			break;
    409 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    410 			error = EINVAL;
    411 			break;
    412 		}
    413 		*retval = semaptr->_sem_base[semnum].sempid;
    414 		break;
    415 
    416 	case GETVAL:
    417 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    418 			break;
    419 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    420 			error = EINVAL;
    421 			break;
    422 		}
    423 		*retval = semaptr->_sem_base[semnum].semval;
    424 		break;
    425 
    426 	case GETALL:
    427 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    428 			break;
    429 		KASSERT(arg != NULL);
    430 		for (i = 0; i < semaptr->sem_nsems; i++) {
    431 			error = copyout(&semaptr->_sem_base[i].semval,
    432 			    &arg->array[i], sizeof(arg->array[i]));
    433 			if (error != 0)
    434 				break;
    435 		}
    436 		break;
    437 
    438 	case GETZCNT:
    439 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    440 			break;
    441 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    442 			error = EINVAL;
    443 			break;
    444 		}
    445 		*retval = semaptr->_sem_base[semnum].semzcnt;
    446 		break;
    447 
    448 	case SETVAL:
    449 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    450 			break;
    451 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    452 			error = EINVAL;
    453 			break;
    454 		}
    455 		KASSERT(arg != NULL);
    456 		semaptr->_sem_base[semnum].semval = arg->val;
    457 		semundo_clear(ix, semnum);
    458 		cv_broadcast(&semcv[ix]);
    459 		break;
    460 
    461 	case SETALL:
    462 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    463 			break;
    464 		KASSERT(arg != NULL);
    465 		for (i = 0; i < semaptr->sem_nsems; i++) {
    466 			error = copyin(&arg->array[i],
    467 			    &semaptr->_sem_base[i].semval,
    468 			    sizeof(arg->array[i]));
    469 			if (error != 0)
    470 				break;
    471 		}
    472 		semundo_clear(ix, -1);
    473 		cv_broadcast(&semcv[ix]);
    474 		break;
    475 
    476 	default:
    477 		error = EINVAL;
    478 		break;
    479 	}
    480 
    481 	mutex_exit(&semlock);
    482 	return (error);
    483 }
    484 
    485 int
    486 sys_semget(struct lwp *l, void *v, register_t *retval)
    487 {
    488 	struct sys_semget_args /* {
    489 		syscallarg(key_t) key;
    490 		syscallarg(int) nsems;
    491 		syscallarg(int) semflg;
    492 	} */ *uap = v;
    493 	int semid, error = 0;
    494 	int key = SCARG(uap, key);
    495 	int nsems = SCARG(uap, nsems);
    496 	int semflg = SCARG(uap, semflg);
    497 	kauth_cred_t cred = l->l_cred;
    498 
    499 	SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
    500 
    501 	mutex_enter(&semlock);
    502 
    503 	if (key != IPC_PRIVATE) {
    504 		for (semid = 0; semid < seminfo.semmni; semid++) {
    505 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
    506 			    sema[semid].sem_perm._key == key)
    507 				break;
    508 		}
    509 		if (semid < seminfo.semmni) {
    510 			SEM_PRINTF(("found public key\n"));
    511 			if ((error = ipcperm(cred, &sema[semid].sem_perm,
    512 			    semflg & 0700)))
    513 			    	goto out;
    514 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    515 				SEM_PRINTF(("too small\n"));
    516 				error = EINVAL;
    517 				goto out;
    518 			}
    519 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    520 				SEM_PRINTF(("not exclusive\n"));
    521 				error = EEXIST;
    522 				goto out;
    523 			}
    524 			goto found;
    525 		}
    526 	}
    527 
    528 	SEM_PRINTF(("need to allocate the semid_ds\n"));
    529 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    530 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    531 			SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
    532 			    seminfo.semmsl));
    533 			error = EINVAL;
    534 			goto out;
    535 		}
    536 		if (nsems > seminfo.semmns - semtot) {
    537 			SEM_PRINTF(("not enough semaphores left "
    538 			    "(need %d, got %d)\n",
    539 			    nsems, seminfo.semmns - semtot));
    540 			error = ENOSPC;
    541 			goto out;
    542 		}
    543 		for (semid = 0; semid < seminfo.semmni; semid++) {
    544 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    545 				break;
    546 		}
    547 		if (semid == seminfo.semmni) {
    548 			SEM_PRINTF(("no more semid_ds's available\n"));
    549 			error = ENOSPC;
    550 			goto out;
    551 		}
    552 		SEM_PRINTF(("semid %d is available\n", semid));
    553 		sema[semid].sem_perm._key = key;
    554 		sema[semid].sem_perm.cuid = kauth_cred_geteuid(cred);
    555 		sema[semid].sem_perm.uid = kauth_cred_geteuid(cred);
    556 		sema[semid].sem_perm.cgid = kauth_cred_getegid(cred);
    557 		sema[semid].sem_perm.gid = kauth_cred_getegid(cred);
    558 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    559 		sema[semid].sem_perm._seq =
    560 		    (sema[semid].sem_perm._seq + 1) & 0x7fff;
    561 		sema[semid].sem_nsems = nsems;
    562 		sema[semid].sem_otime = 0;
    563 		sema[semid].sem_ctime = time_second;
    564 		sema[semid]._sem_base = &sem[semtot];
    565 		semtot += nsems;
    566 		memset(sema[semid]._sem_base, 0,
    567 		    sizeof(sema[semid]._sem_base[0]) * nsems);
    568 		SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
    569 		    &sem[semtot]));
    570 	} else {
    571 		SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
    572 		error = ENOENT;
    573 		goto out;
    574 	}
    575 
    576  found:
    577 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    578  out:
    579 	mutex_exit(&semlock);
    580 	return (error);
    581 }
    582 
    583 #define SMALL_SOPS 8
    584 
    585 int
    586 sys_semop(struct lwp *l, void *v, register_t *retval)
    587 {
    588 	struct sys_semop_args /* {
    589 		syscallarg(int) semid;
    590 		syscallarg(struct sembuf *) sops;
    591 		syscallarg(size_t) nsops;
    592 	} */ *uap = v;
    593 	struct proc *p = l->l_proc;
    594 	int semid = SCARG(uap, semid), seq;
    595 	size_t nsops = SCARG(uap, nsops);
    596 	struct sembuf small_sops[SMALL_SOPS];
    597 	struct sembuf *sops;
    598 	struct semid_ds *semaptr;
    599 	struct sembuf *sopptr = NULL;
    600 	struct __sem *semptr = NULL;
    601 	struct sem_undo *suptr = NULL;
    602 	kauth_cred_t cred = l->l_cred;
    603 	int i, error;
    604 	int do_wakeup, do_undos;
    605 
    606 	SEM_PRINTF(("call to semop(%d, %p, %zd)\n", semid, SCARG(uap,sops), nsops));
    607 
    608 	if (nsops <= SMALL_SOPS) {
    609 		sops = small_sops;
    610 	} else if (nsops <= seminfo.semopm)
    611 		sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP);
    612 	else {
    613 		SEM_PRINTF(("too many sops (max=%d, nsops=%zd)\n",
    614 		    seminfo.semopm, nsops));
    615 		return (E2BIG);
    616 	}
    617 
    618 	mutex_enter(&semlock);
    619 
    620 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    621 	if (semid < 0 || semid >= seminfo.semmni) {
    622 		error = EINVAL;
    623 		goto out;
    624 	}
    625 
    626 	semaptr = &sema[semid];
    627 	seq = IPCID_TO_SEQ(SCARG(uap, semid));
    628 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    629 	    semaptr->sem_perm._seq != seq) {
    630 		error = EINVAL;
    631 		goto out;
    632 	}
    633 
    634 	if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    635 		SEM_PRINTF(("error = %d from ipaccess\n", error));
    636 		goto out;
    637 	}
    638 
    639 	if ((error = copyin(SCARG(uap, sops),
    640 	    sops, nsops * sizeof(sops[0]))) != 0) {
    641 		SEM_PRINTF(("error = %d from copyin(%p, %p, %zd)\n", error,
    642 		    SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
    643 		goto out;
    644 	}
    645 
    646 	for (i = 0; i < nsops; i++)
    647 		if (sops[i].sem_num >= semaptr->sem_nsems) {
    648 			error = EFBIG;
    649 			goto out;
    650 		}
    651 
    652 	/*
    653 	 * Loop trying to satisfy the vector of requests.
    654 	 * If we reach a point where we must wait, any requests already
    655 	 * performed are rolled back and we go to sleep until some other
    656 	 * process wakes us up.  At this point, we start all over again.
    657 	 *
    658 	 * This ensures that from the perspective of other tasks, a set
    659 	 * of requests is atomic (never partially satisfied).
    660 	 */
    661 	do_undos = 0;
    662 
    663 	for (;;) {
    664 		do_wakeup = 0;
    665 
    666 		for (i = 0; i < nsops; i++) {
    667 			sopptr = &sops[i];
    668 			semptr = &semaptr->_sem_base[sopptr->sem_num];
    669 
    670 			SEM_PRINTF(("semop:  semaptr=%p, sem_base=%p, "
    671 			    "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
    672 			    semaptr, semaptr->_sem_base, semptr,
    673 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    674 			    (sopptr->sem_flg & IPC_NOWAIT) ?
    675 			    "nowait" : "wait"));
    676 
    677 			if (sopptr->sem_op < 0) {
    678 				if ((int)(semptr->semval +
    679 				    sopptr->sem_op) < 0) {
    680 					SEM_PRINTF(("semop:  "
    681 					    "can't do it now\n"));
    682 					break;
    683 				} else {
    684 					semptr->semval += sopptr->sem_op;
    685 					if (semptr->semval == 0 &&
    686 					    semptr->semzcnt > 0)
    687 						do_wakeup = 1;
    688 				}
    689 				if (sopptr->sem_flg & SEM_UNDO)
    690 					do_undos = 1;
    691 			} else if (sopptr->sem_op == 0) {
    692 				if (semptr->semval > 0) {
    693 					SEM_PRINTF(("semop:  not zero now\n"));
    694 					break;
    695 				}
    696 			} else {
    697 				if (semptr->semncnt > 0)
    698 					do_wakeup = 1;
    699 				semptr->semval += sopptr->sem_op;
    700 				if (sopptr->sem_flg & SEM_UNDO)
    701 					do_undos = 1;
    702 			}
    703 		}
    704 
    705 		/*
    706 		 * Did we get through the entire vector?
    707 		 */
    708 		if (i >= nsops)
    709 			goto done;
    710 
    711 		/*
    712 		 * No ... rollback anything that we've already done
    713 		 */
    714 		SEM_PRINTF(("semop:  rollback 0 through %d\n", i - 1));
    715 		while (i-- > 0)
    716 			semaptr->_sem_base[sops[i].sem_num].semval -=
    717 			    sops[i].sem_op;
    718 
    719 		/*
    720 		 * If the request that we couldn't satisfy has the
    721 		 * NOWAIT flag set then return with EAGAIN.
    722 		 */
    723 		if (sopptr->sem_flg & IPC_NOWAIT) {
    724 			error = EAGAIN;
    725 			goto out;
    726 		}
    727 
    728 		if (sopptr->sem_op == 0)
    729 			semptr->semzcnt++;
    730 		else
    731 			semptr->semncnt++;
    732 
    733 		SEM_PRINTF(("semop:  good night!\n"));
    734 		error = cv_wait_sig(&semcv[semid], &semlock);
    735 		SEM_PRINTF(("semop:  good morning (error=%d)!\n", error));
    736 
    737 		/*
    738 		 * Make sure that the semaphore still exists
    739 		 */
    740 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    741 		    semaptr->sem_perm._seq != seq) {
    742 			error = EIDRM;
    743 			goto out;
    744 		}
    745 
    746 		/*
    747 		 * The semaphore is still alive.  Readjust the count of
    748 		 * waiting processes.
    749 		 */
    750 		semptr = &semaptr->_sem_base[sopptr->sem_num];
    751 		if (sopptr->sem_op == 0)
    752 			semptr->semzcnt--;
    753 		else
    754 			semptr->semncnt--;
    755 		/*
    756 		 * Is it really morning, or was our sleep interrupted?
    757 		 * (Delayed check of tsleep() return code because we
    758 		 * need to decrement sem[nz]cnt either way.)
    759 		 */
    760 		if (error != 0) {
    761 			error = EINTR;
    762 			goto out;
    763 		}
    764 		SEM_PRINTF(("semop:  good morning!\n"));
    765 	}
    766 
    767 done:
    768 	/*
    769 	 * Process any SEM_UNDO requests.
    770 	 */
    771 	if (do_undos) {
    772 		for (i = 0; i < nsops; i++) {
    773 			/*
    774 			 * We only need to deal with SEM_UNDO's for non-zero
    775 			 * op's.
    776 			 */
    777 			int adjval;
    778 
    779 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    780 				continue;
    781 			adjval = sops[i].sem_op;
    782 			if (adjval == 0)
    783 				continue;
    784 			error = semundo_adjust(p, &suptr, semid,
    785 			    sops[i].sem_num, -adjval);
    786 			if (error == 0)
    787 				continue;
    788 
    789 			/*
    790 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    791 			 * Rollback the adjustments to this point and then
    792 			 * rollback the semaphore ups and down so we can return
    793 			 * with an error with all structures restored.  We
    794 			 * rollback the undo's in the exact reverse order that
    795 			 * we applied them.  This guarantees that we won't run
    796 			 * out of space as we roll things back out.
    797 			 */
    798 			while (i-- > 0) {
    799 				if ((sops[i].sem_flg & SEM_UNDO) == 0)
    800 					continue;
    801 				adjval = sops[i].sem_op;
    802 				if (adjval == 0)
    803 					continue;
    804 				if (semundo_adjust(p, &suptr, semid,
    805 				    sops[i].sem_num, adjval) != 0)
    806 					panic("semop - can't undo undos");
    807 			}
    808 
    809 			for (i = 0; i < nsops; i++)
    810 				semaptr->_sem_base[sops[i].sem_num].semval -=
    811 				    sops[i].sem_op;
    812 
    813 			SEM_PRINTF(("error = %d from semundo_adjust\n", error));
    814 			goto out;
    815 		} /* loop through the sops */
    816 	} /* if (do_undos) */
    817 
    818 	/* We're definitely done - set the sempid's */
    819 	for (i = 0; i < nsops; i++) {
    820 		sopptr = &sops[i];
    821 		semptr = &semaptr->_sem_base[sopptr->sem_num];
    822 		semptr->sempid = p->p_pid;
    823 	}
    824 
    825 	/* Update sem_otime */
    826 	semaptr->sem_otime = time_second;
    827 
    828 	/* Do a wakeup if any semaphore was up'd. */
    829 	if (do_wakeup) {
    830 		SEM_PRINTF(("semop:  doing wakeup\n"));
    831 		cv_broadcast(&semcv[semid]);
    832 		SEM_PRINTF(("semop:  back from wakeup\n"));
    833 	}
    834 	SEM_PRINTF(("semop:  done\n"));
    835 	*retval = 0;
    836 
    837  out:
    838 	mutex_exit(&semlock);
    839 	if (sops != small_sops) {
    840 		kmem_free(sops, nsops * sizeof(*sops));
    841 	}
    842 	return error;
    843 }
    844 
    845 /*
    846  * Go through the undo structures for this process and apply the
    847  * adjustments to semaphores.
    848  */
    849 /*ARGSUSED*/
    850 void
    851 semexit(struct proc *p, void *v)
    852 {
    853 	struct sem_undo *suptr;
    854 	struct sem_undo **supptr;
    855 
    856 	mutex_enter(&semlock);
    857 
    858 	/*
    859 	 * Go through the chain of undo vectors looking for one
    860 	 * associated with this process.
    861 	 */
    862 
    863 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
    864 	    supptr = &suptr->un_next) {
    865 		if (suptr->un_proc == p)
    866 			break;
    867 	}
    868 
    869 	/*
    870 	 * If there is no undo vector, skip to the end.
    871 	 */
    872 
    873 	if (suptr == NULL) {
    874 		mutex_exit(&semlock);
    875 		return;
    876 	}
    877 
    878 	/*
    879 	 * We now have an undo vector for this process.
    880 	 */
    881 
    882 	SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
    883 	    suptr->un_cnt));
    884 
    885 	/*
    886 	 * If there are any active undo elements then process them.
    887 	 */
    888 	if (suptr->un_cnt > 0) {
    889 		int ix;
    890 
    891 		for (ix = 0; ix < suptr->un_cnt; ix++) {
    892 			int semid = suptr->un_ent[ix].un_id;
    893 			int semnum = suptr->un_ent[ix].un_num;
    894 			int adjval = suptr->un_ent[ix].un_adjval;
    895 			struct semid_ds *semaptr;
    896 
    897 			semaptr = &sema[semid];
    898 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    899 				panic("semexit - semid not allocated");
    900 			if (semnum >= semaptr->sem_nsems)
    901 				panic("semexit - semnum out of range");
    902 
    903 			SEM_PRINTF(("semexit:  %p id=%d num=%d(adj=%d) ; "
    904 			    "sem=%d\n",
    905 			    suptr->un_proc, suptr->un_ent[ix].un_id,
    906 			    suptr->un_ent[ix].un_num,
    907 			    suptr->un_ent[ix].un_adjval,
    908 			    semaptr->_sem_base[semnum].semval));
    909 
    910 			if (adjval < 0 &&
    911 			    semaptr->_sem_base[semnum].semval < -adjval)
    912 				semaptr->_sem_base[semnum].semval = 0;
    913 			else
    914 				semaptr->_sem_base[semnum].semval += adjval;
    915 
    916 			cv_broadcast(&semcv[semid]);
    917 			SEM_PRINTF(("semexit:  back from wakeup\n"));
    918 		}
    919 	}
    920 
    921 	/*
    922 	 * Deallocate the undo vector.
    923 	 */
    924 	SEM_PRINTF(("removing vector\n"));
    925 	suptr->un_proc = NULL;
    926 	*supptr = suptr->un_next;
    927 	mutex_exit(&semlock);
    928 }
    929