xref: /freebsd/sys/kern/sysv_sem.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1 /*	$Id: sysv_sem.c,v 1.21 1998/03/30 09:50:41 phk Exp $ */
2 
3 /*
4  * Implementation of SVID semaphores
5  *
6  * Author:  Daniel Boulet
7  *
8  * This software is provided ``AS IS'' without any warranties of any kind.
9  */
10 
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/sysproto.h>
14 #include <sys/kernel.h>
15 #include <sys/proc.h>
16 #include <sys/sem.h>
17 #include <sys/sysent.h>
18 
19 static void seminit __P((void *));
20 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
21 
22 #ifndef _SYS_SYSPROTO_H_
23 struct __semctl_args;
24 int __semctl __P((struct proc *p, struct __semctl_args *uap));
25 struct semget_args;
26 int semget __P((struct proc *p, struct semget_args *uap));
27 struct semop_args;
28 int semop __P((struct proc *p, struct semop_args *uap));
29 struct semconfig_args;
30 int semconfig __P((struct proc *p, struct semconfig_args *uap));
31 #endif
32 
33 static struct sem_undo *semu_alloc __P((struct proc *p));
34 static int semundo_adjust __P((struct proc *p, struct sem_undo **supptr,
35 		int semid, int semnum, int adjval));
36 static void semundo_clear __P((int semid, int semnum));
37 
38 /* XXX casting to (sy_call_t *) is bogus, as usual. */
39 static sy_call_t *semcalls[] = {
40 	(sy_call_t *)__semctl, (sy_call_t *)semget,
41 	(sy_call_t *)semop, (sy_call_t *)semconfig
42 };
43 
44 static int	semtot = 0;
45 struct semid_ds *sema;		/* semaphore id pool */
46 struct sem *sem;		/* semaphore pool */
47 static struct sem_undo *semu_list; 	/* list of active undo structures */
48 int	*semu;			/* undo structure pool */
49 
50 static struct proc *semlock_holder = NULL;
51 
52 void
53 seminit(dummy)
54 	void *dummy;
55 {
56 	register int i;
57 
58 	if (sema == NULL)
59 		panic("sema is NULL");
60 	if (semu == NULL)
61 		panic("semu is NULL");
62 
63 	for (i = 0; i < seminfo.semmni; i++) {
64 		sema[i].sem_base = 0;
65 		sema[i].sem_perm.mode = 0;
66 	}
67 	for (i = 0; i < seminfo.semmnu; i++) {
68 		register struct sem_undo *suptr = SEMU(i);
69 		suptr->un_proc = NULL;
70 	}
71 	semu_list = NULL;
72 }
73 
74 /*
75  * Entry point for all SEM calls
76  */
77 int
78 semsys(p, uap)
79 	struct proc *p;
80 	/* XXX actually varargs. */
81 	struct semsys_args /* {
82 		u_int	which;
83 		int	a2;
84 		int	a3;
85 		int	a4;
86 		int	a5;
87 	} */ *uap;
88 {
89 
90 	while (semlock_holder != NULL && semlock_holder != p)
91 		(void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semsys", 0);
92 
93 	if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
94 		return (EINVAL);
95 	return ((*semcalls[uap->which])(p, &uap->a2));
96 }
97 
98 /*
99  * Lock or unlock the entire semaphore facility.
100  *
101  * This will probably eventually evolve into a general purpose semaphore
102  * facility status enquiry mechanism (I don't like the "read /dev/kmem"
103  * approach currently taken by ipcs and the amount of info that we want
104  * to be able to extract for ipcs is probably beyond what the capability
105  * of the getkerninfo facility.
106  *
107  * At the time that the current version of semconfig was written, ipcs is
108  * the only user of the semconfig facility.  It uses it to ensure that the
109  * semaphore facility data structures remain static while it fishes around
110  * in /dev/kmem.
111  */
112 
113 #ifndef _SYS_SYSPROTO_H_
114 struct semconfig_args {
115 	semconfig_ctl_t	flag;
116 };
117 #endif
118 
119 int
120 semconfig(p, uap)
121 	struct proc *p;
122 	struct semconfig_args *uap;
123 {
124 	int eval = 0;
125 
126 	switch (uap->flag) {
127 	case SEM_CONFIG_FREEZE:
128 		semlock_holder = p;
129 		break;
130 
131 	case SEM_CONFIG_THAW:
132 		semlock_holder = NULL;
133 		wakeup((caddr_t)&semlock_holder);
134 		break;
135 
136 	default:
137 		printf("semconfig: unknown flag parameter value (%d) - ignored\n",
138 		    uap->flag);
139 		eval = EINVAL;
140 		break;
141 	}
142 
143 	p->p_retval[0] = 0;
144 	return(eval);
145 }
146 
147 /*
148  * Allocate a new sem_undo structure for a process
149  * (returns ptr to structure or NULL if no more room)
150  */
151 
152 static struct sem_undo *
153 semu_alloc(p)
154 	struct proc *p;
155 {
156 	register int i;
157 	register struct sem_undo *suptr;
158 	register struct sem_undo **supptr;
159 	int attempt;
160 
161 	/*
162 	 * Try twice to allocate something.
163 	 * (we'll purge any empty structures after the first pass so
164 	 * two passes are always enough)
165 	 */
166 
167 	for (attempt = 0; attempt < 2; attempt++) {
168 		/*
169 		 * Look for a free structure.
170 		 * Fill it in and return it if we find one.
171 		 */
172 
173 		for (i = 0; i < seminfo.semmnu; i++) {
174 			suptr = SEMU(i);
175 			if (suptr->un_proc == NULL) {
176 				suptr->un_next = semu_list;
177 				semu_list = suptr;
178 				suptr->un_cnt = 0;
179 				suptr->un_proc = p;
180 				return(suptr);
181 			}
182 		}
183 
184 		/*
185 		 * We didn't find a free one, if this is the first attempt
186 		 * then try to free some structures.
187 		 */
188 
189 		if (attempt == 0) {
190 			/* All the structures are in use - try to free some */
191 			int did_something = 0;
192 
193 			supptr = &semu_list;
194 			while ((suptr = *supptr) != NULL) {
195 				if (suptr->un_cnt == 0)  {
196 					suptr->un_proc = NULL;
197 					*supptr = suptr->un_next;
198 					did_something = 1;
199 				} else
200 					supptr = &(suptr->un_next);
201 			}
202 
203 			/* If we didn't free anything then just give-up */
204 			if (!did_something)
205 				return(NULL);
206 		} else {
207 			/*
208 			 * The second pass failed even though we freed
209 			 * something after the first pass!
210 			 * This is IMPOSSIBLE!
211 			 */
212 			panic("semu_alloc - second attempt failed");
213 		}
214 	}
215 	return (NULL);
216 }
217 
218 /*
219  * Adjust a particular entry for a particular proc
220  */
221 
222 static int
223 semundo_adjust(p, supptr, semid, semnum, adjval)
224 	register struct proc *p;
225 	struct sem_undo **supptr;
226 	int semid, semnum;
227 	int adjval;
228 {
229 	register struct sem_undo *suptr;
230 	register struct undo *sunptr;
231 	int i;
232 
233 	/* Look for and remember the sem_undo if the caller doesn't provide
234 	   it */
235 
236 	suptr = *supptr;
237 	if (suptr == NULL) {
238 		for (suptr = semu_list; suptr != NULL;
239 		    suptr = suptr->un_next) {
240 			if (suptr->un_proc == p) {
241 				*supptr = suptr;
242 				break;
243 			}
244 		}
245 		if (suptr == NULL) {
246 			if (adjval == 0)
247 				return(0);
248 			suptr = semu_alloc(p);
249 			if (suptr == NULL)
250 				return(ENOSPC);
251 			*supptr = suptr;
252 		}
253 	}
254 
255 	/*
256 	 * Look for the requested entry and adjust it (delete if adjval becomes
257 	 * 0).
258 	 */
259 	sunptr = &suptr->un_ent[0];
260 	for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
261 		if (sunptr->un_id != semid || sunptr->un_num != semnum)
262 			continue;
263 		if (adjval == 0)
264 			sunptr->un_adjval = 0;
265 		else
266 			sunptr->un_adjval += adjval;
267 		if (sunptr->un_adjval == 0) {
268 			suptr->un_cnt--;
269 			if (i < suptr->un_cnt)
270 				suptr->un_ent[i] =
271 				    suptr->un_ent[suptr->un_cnt];
272 		}
273 		return(0);
274 	}
275 
276 	/* Didn't find the right entry - create it */
277 	if (adjval == 0)
278 		return(0);
279 	if (suptr->un_cnt != seminfo.semume) {
280 		sunptr = &suptr->un_ent[suptr->un_cnt];
281 		suptr->un_cnt++;
282 		sunptr->un_adjval = adjval;
283 		sunptr->un_id = semid; sunptr->un_num = semnum;
284 	} else
285 		return(EINVAL);
286 	return(0);
287 }
288 
289 static void
290 semundo_clear(semid, semnum)
291 	int semid, semnum;
292 {
293 	register struct sem_undo *suptr;
294 
295 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
296 		register struct undo *sunptr = &suptr->un_ent[0];
297 		register int i = 0;
298 
299 		while (i < suptr->un_cnt) {
300 			if (sunptr->un_id == semid) {
301 				if (semnum == -1 || sunptr->un_num == semnum) {
302 					suptr->un_cnt--;
303 					if (i < suptr->un_cnt) {
304 						suptr->un_ent[i] =
305 						  suptr->un_ent[suptr->un_cnt];
306 						continue;
307 					}
308 				}
309 				if (semnum != -1)
310 					break;
311 			}
312 			i++, sunptr++;
313 		}
314 	}
315 }
316 
317 /*
318  * Note that the user-mode half of this passes a union, not a pointer
319  */
320 #ifndef _SYS_SYSPROTO_H_
321 struct __semctl_args {
322 	int	semid;
323 	int	semnum;
324 	int	cmd;
325 	union	semun *arg;
326 };
327 #endif
328 
329 int
330 __semctl(p, uap)
331 	struct proc *p;
332 	register struct __semctl_args *uap;
333 {
334 	int semid = uap->semid;
335 	int semnum = uap->semnum;
336 	int cmd = uap->cmd;
337 	union semun *arg = uap->arg;
338 	union semun real_arg;
339 	struct ucred *cred = p->p_ucred;
340 	int i, rval, eval;
341 	struct semid_ds sbuf;
342 	register struct semid_ds *semaptr;
343 
344 #ifdef SEM_DEBUG
345 	printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
346 #endif
347 
348 	semid = IPCID_TO_IX(semid);
349 	if (semid < 0 || semid >= seminfo.semmsl)
350 		return(EINVAL);
351 
352 	semaptr = &sema[semid];
353 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
354 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
355 		return(EINVAL);
356 
357 	eval = 0;
358 	rval = 0;
359 
360 	switch (cmd) {
361 	case IPC_RMID:
362 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
363 			return(eval);
364 		semaptr->sem_perm.cuid = cred->cr_uid;
365 		semaptr->sem_perm.uid = cred->cr_uid;
366 		semtot -= semaptr->sem_nsems;
367 		for (i = semaptr->sem_base - sem; i < semtot; i++)
368 			sem[i] = sem[i + semaptr->sem_nsems];
369 		for (i = 0; i < seminfo.semmni; i++) {
370 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
371 			    sema[i].sem_base > semaptr->sem_base)
372 				sema[i].sem_base -= semaptr->sem_nsems;
373 		}
374 		semaptr->sem_perm.mode = 0;
375 		semundo_clear(semid, -1);
376 		wakeup((caddr_t)semaptr);
377 		break;
378 
379 	case IPC_SET:
380 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
381 			return(eval);
382 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
383 			return(eval);
384 		if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
385 		    sizeof(sbuf))) != 0)
386 			return(eval);
387 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
388 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
389 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
390 		    (sbuf.sem_perm.mode & 0777);
391 		semaptr->sem_ctime = time_second;
392 		break;
393 
394 	case IPC_STAT:
395 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
396 			return(eval);
397 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
398 			return(eval);
399 		eval = copyout((caddr_t)semaptr, real_arg.buf,
400 		    sizeof(struct semid_ds));
401 		break;
402 
403 	case GETNCNT:
404 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
405 			return(eval);
406 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
407 			return(EINVAL);
408 		rval = semaptr->sem_base[semnum].semncnt;
409 		break;
410 
411 	case GETPID:
412 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
413 			return(eval);
414 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
415 			return(EINVAL);
416 		rval = semaptr->sem_base[semnum].sempid;
417 		break;
418 
419 	case GETVAL:
420 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
421 			return(eval);
422 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
423 			return(EINVAL);
424 		rval = semaptr->sem_base[semnum].semval;
425 		break;
426 
427 	case GETALL:
428 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
429 			return(eval);
430 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
431 			return(eval);
432 		for (i = 0; i < semaptr->sem_nsems; i++) {
433 			eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
434 			    &real_arg.array[i], sizeof(real_arg.array[0]));
435 			if (eval != 0)
436 				break;
437 		}
438 		break;
439 
440 	case GETZCNT:
441 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
442 			return(eval);
443 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
444 			return(EINVAL);
445 		rval = semaptr->sem_base[semnum].semzcnt;
446 		break;
447 
448 	case SETVAL:
449 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
450 			return(eval);
451 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
452 			return(EINVAL);
453 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
454 			return(eval);
455 		semaptr->sem_base[semnum].semval = real_arg.val;
456 		semundo_clear(semid, semnum);
457 		wakeup((caddr_t)semaptr);
458 		break;
459 
460 	case SETALL:
461 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
462 			return(eval);
463 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
464 			return(eval);
465 		for (i = 0; i < semaptr->sem_nsems; i++) {
466 			eval = copyin(&real_arg.array[i],
467 			    (caddr_t)&semaptr->sem_base[i].semval,
468 			    sizeof(real_arg.array[0]));
469 			if (eval != 0)
470 				break;
471 		}
472 		semundo_clear(semid, -1);
473 		wakeup((caddr_t)semaptr);
474 		break;
475 
476 	default:
477 		return(EINVAL);
478 	}
479 
480 	if (eval == 0)
481 		p->p_retval[0] = rval;
482 	return(eval);
483 }
484 
485 #ifndef _SYS_SYSPROTO_H_
486 struct semget_args {
487 	key_t	key;
488 	int	nsems;
489 	int	semflg;
490 };
491 #endif
492 
493 int
494 semget(p, uap)
495 	struct proc *p;
496 	register struct semget_args *uap;
497 {
498 	int semid, eval;
499 	int key = uap->key;
500 	int nsems = uap->nsems;
501 	int semflg = uap->semflg;
502 	struct ucred *cred = p->p_ucred;
503 
504 #ifdef SEM_DEBUG
505 	printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
506 #endif
507 
508 	if (key != IPC_PRIVATE) {
509 		for (semid = 0; semid < seminfo.semmni; semid++) {
510 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
511 			    sema[semid].sem_perm.key == key)
512 				break;
513 		}
514 		if (semid < seminfo.semmni) {
515 #ifdef SEM_DEBUG
516 			printf("found public key\n");
517 #endif
518 			if ((eval = ipcperm(cred, &sema[semid].sem_perm,
519 			    semflg & 0700)))
520 				return(eval);
521 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
522 #ifdef SEM_DEBUG
523 				printf("too small\n");
524 #endif
525 				return(EINVAL);
526 			}
527 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
528 #ifdef SEM_DEBUG
529 				printf("not exclusive\n");
530 #endif
531 				return(EEXIST);
532 			}
533 			goto found;
534 		}
535 	}
536 
537 #ifdef SEM_DEBUG
538 	printf("need to allocate the semid_ds\n");
539 #endif
540 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
541 		if (nsems <= 0 || nsems > seminfo.semmsl) {
542 #ifdef SEM_DEBUG
543 			printf("nsems out of range (0<%d<=%d)\n", nsems,
544 			    seminfo.semmsl);
545 #endif
546 			return(EINVAL);
547 		}
548 		if (nsems > seminfo.semmns - semtot) {
549 #ifdef SEM_DEBUG
550 			printf("not enough semaphores left (need %d, got %d)\n",
551 			    nsems, seminfo.semmns - semtot);
552 #endif
553 			return(ENOSPC);
554 		}
555 		for (semid = 0; semid < seminfo.semmni; semid++) {
556 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
557 				break;
558 		}
559 		if (semid == seminfo.semmni) {
560 #ifdef SEM_DEBUG
561 			printf("no more semid_ds's available\n");
562 #endif
563 			return(ENOSPC);
564 		}
565 #ifdef SEM_DEBUG
566 		printf("semid %d is available\n", semid);
567 #endif
568 		sema[semid].sem_perm.key = key;
569 		sema[semid].sem_perm.cuid = cred->cr_uid;
570 		sema[semid].sem_perm.uid = cred->cr_uid;
571 		sema[semid].sem_perm.cgid = cred->cr_gid;
572 		sema[semid].sem_perm.gid = cred->cr_gid;
573 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
574 		sema[semid].sem_perm.seq =
575 		    (sema[semid].sem_perm.seq + 1) & 0x7fff;
576 		sema[semid].sem_nsems = nsems;
577 		sema[semid].sem_otime = 0;
578 		sema[semid].sem_ctime = time_second;
579 		sema[semid].sem_base = &sem[semtot];
580 		semtot += nsems;
581 		bzero(sema[semid].sem_base,
582 		    sizeof(sema[semid].sem_base[0])*nsems);
583 #ifdef SEM_DEBUG
584 		printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
585 		    &sem[semtot]);
586 #endif
587 	} else {
588 #ifdef SEM_DEBUG
589 		printf("didn't find it and wasn't asked to create it\n");
590 #endif
591 		return(ENOENT);
592 	}
593 
594 found:
595 	p->p_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
596 	return(0);
597 }
598 
599 #ifndef _SYS_SYSPROTO_H_
600 struct semop_args {
601 	int	semid;
602 	struct	sembuf *sops;
603 	int	nsops;
604 };
605 #endif
606 
607 int
608 semop(p, uap)
609 	struct proc *p;
610 	register struct semop_args *uap;
611 {
612 	int semid = uap->semid;
613 	int nsops = uap->nsops;
614 	struct sembuf sops[MAX_SOPS];
615 	register struct semid_ds *semaptr;
616 	register struct sembuf *sopptr;
617 	register struct sem *semptr;
618 	struct sem_undo *suptr = NULL;
619 	struct ucred *cred = p->p_ucred;
620 	int i, j, eval;
621 	int do_wakeup, do_undos;
622 
623 #ifdef SEM_DEBUG
624 	printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
625 #endif
626 
627 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
628 
629 	if (semid < 0 || semid >= seminfo.semmsl)
630 		return(EINVAL);
631 
632 	semaptr = &sema[semid];
633 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
634 		return(EINVAL);
635 	if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
636 		return(EINVAL);
637 
638 	if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
639 #ifdef SEM_DEBUG
640 		printf("eval = %d from ipaccess\n", eval);
641 #endif
642 		return(eval);
643 	}
644 
645 	if (nsops > MAX_SOPS) {
646 #ifdef SEM_DEBUG
647 		printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
648 #endif
649 		return(E2BIG);
650 	}
651 
652 	if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
653 #ifdef SEM_DEBUG
654 		printf("eval = %d from copyin(%08x, %08x, %d)\n", eval,
655 		    uap->sops, &sops, nsops * sizeof(sops[0]));
656 #endif
657 		return(eval);
658 	}
659 
660 	/*
661 	 * Loop trying to satisfy the vector of requests.
662 	 * If we reach a point where we must wait, any requests already
663 	 * performed are rolled back and we go to sleep until some other
664 	 * process wakes us up.  At this point, we start all over again.
665 	 *
666 	 * This ensures that from the perspective of other tasks, a set
667 	 * of requests is atomic (never partially satisfied).
668 	 */
669 	do_undos = 0;
670 
671 	for (;;) {
672 		do_wakeup = 0;
673 
674 		for (i = 0; i < nsops; i++) {
675 			sopptr = &sops[i];
676 
677 			if (sopptr->sem_num >= semaptr->sem_nsems)
678 				return(EFBIG);
679 
680 			semptr = &semaptr->sem_base[sopptr->sem_num];
681 
682 #ifdef SEM_DEBUG
683 			printf("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
684 			    semaptr, semaptr->sem_base, semptr,
685 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
686 			    (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
687 #endif
688 
689 			if (sopptr->sem_op < 0) {
690 				if (semptr->semval + sopptr->sem_op < 0) {
691 #ifdef SEM_DEBUG
692 					printf("semop:  can't do it now\n");
693 #endif
694 					break;
695 				} else {
696 					semptr->semval += sopptr->sem_op;
697 					if (semptr->semval == 0 &&
698 					    semptr->semzcnt > 0)
699 						do_wakeup = 1;
700 				}
701 				if (sopptr->sem_flg & SEM_UNDO)
702 					do_undos = 1;
703 			} else if (sopptr->sem_op == 0) {
704 				if (semptr->semval > 0) {
705 #ifdef SEM_DEBUG
706 					printf("semop:  not zero now\n");
707 #endif
708 					break;
709 				}
710 			} else {
711 				if (semptr->semncnt > 0)
712 					do_wakeup = 1;
713 				semptr->semval += sopptr->sem_op;
714 				if (sopptr->sem_flg & SEM_UNDO)
715 					do_undos = 1;
716 			}
717 		}
718 
719 		/*
720 		 * Did we get through the entire vector?
721 		 */
722 		if (i >= nsops)
723 			goto done;
724 
725 		/*
726 		 * No ... rollback anything that we've already done
727 		 */
728 #ifdef SEM_DEBUG
729 		printf("semop:  rollback 0 through %d\n", i-1);
730 #endif
731 		for (j = 0; j < i; j++)
732 			semaptr->sem_base[sops[j].sem_num].semval -=
733 			    sops[j].sem_op;
734 
735 		/*
736 		 * If the request that we couldn't satisfy has the
737 		 * NOWAIT flag set then return with EAGAIN.
738 		 */
739 		if (sopptr->sem_flg & IPC_NOWAIT)
740 			return(EAGAIN);
741 
742 		if (sopptr->sem_op == 0)
743 			semptr->semzcnt++;
744 		else
745 			semptr->semncnt++;
746 
747 #ifdef SEM_DEBUG
748 		printf("semop:  good night!\n");
749 #endif
750 		eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
751 		    "semwait", 0);
752 #ifdef SEM_DEBUG
753 		printf("semop:  good morning (eval=%d)!\n", eval);
754 #endif
755 
756 		suptr = NULL;	/* sem_undo may have been reallocated */
757 
758 		if (eval != 0)
759 			return(EINTR);
760 #ifdef SEM_DEBUG
761 		printf("semop:  good morning!\n");
762 #endif
763 
764 		/*
765 		 * Make sure that the semaphore still exists
766 		 */
767 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
768 		    semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
769 			/* The man page says to return EIDRM. */
770 			/* Unfortunately, BSD doesn't define that code! */
771 #ifdef EIDRM
772 			return(EIDRM);
773 #else
774 			return(EINVAL);
775 #endif
776 		}
777 
778 		/*
779 		 * The semaphore is still alive.  Readjust the count of
780 		 * waiting processes.
781 		 */
782 		if (sopptr->sem_op == 0)
783 			semptr->semzcnt--;
784 		else
785 			semptr->semncnt--;
786 	}
787 
788 done:
789 	/*
790 	 * Process any SEM_UNDO requests.
791 	 */
792 	if (do_undos) {
793 		for (i = 0; i < nsops; i++) {
794 			/*
795 			 * We only need to deal with SEM_UNDO's for non-zero
796 			 * op's.
797 			 */
798 			int adjval;
799 
800 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
801 				continue;
802 			adjval = sops[i].sem_op;
803 			if (adjval == 0)
804 				continue;
805 			eval = semundo_adjust(p, &suptr, semid,
806 			    sops[i].sem_num, -adjval);
807 			if (eval == 0)
808 				continue;
809 
810 			/*
811 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
812 			 * Rollback the adjustments to this point and then
813 			 * rollback the semaphore ups and down so we can return
814 			 * with an error with all structures restored.  We
815 			 * rollback the undo's in the exact reverse order that
816 			 * we applied them.  This guarantees that we won't run
817 			 * out of space as we roll things back out.
818 			 */
819 			for (j = i - 1; j >= 0; j--) {
820 				if ((sops[j].sem_flg & SEM_UNDO) == 0)
821 					continue;
822 				adjval = sops[j].sem_op;
823 				if (adjval == 0)
824 					continue;
825 				if (semundo_adjust(p, &suptr, semid,
826 				    sops[j].sem_num, adjval) != 0)
827 					panic("semop - can't undo undos");
828 			}
829 
830 			for (j = 0; j < nsops; j++)
831 				semaptr->sem_base[sops[j].sem_num].semval -=
832 				    sops[j].sem_op;
833 
834 #ifdef SEM_DEBUG
835 			printf("eval = %d from semundo_adjust\n", eval);
836 #endif
837 			return(eval);
838 		} /* loop through the sops */
839 	} /* if (do_undos) */
840 
841 	/* We're definitely done - set the sempid's */
842 	for (i = 0; i < nsops; i++) {
843 		sopptr = &sops[i];
844 		semptr = &semaptr->sem_base[sopptr->sem_num];
845 		semptr->sempid = p->p_pid;
846 	}
847 
848 	/* Do a wakeup if any semaphore was up'd. */
849 	if (do_wakeup) {
850 #ifdef SEM_DEBUG
851 		printf("semop:  doing wakeup\n");
852 #ifdef SEM_WAKEUP
853 		sem_wakeup((caddr_t)semaptr);
854 #else
855 		wakeup((caddr_t)semaptr);
856 #endif
857 		printf("semop:  back from wakeup\n");
858 #else
859 		wakeup((caddr_t)semaptr);
860 #endif
861 	}
862 #ifdef SEM_DEBUG
863 	printf("semop:  done\n");
864 #endif
865 	p->p_retval[0] = 0;
866 	return(0);
867 }
868 
869 /*
870  * Go through the undo structures for this process and apply the adjustments to
871  * semaphores.
872  */
873 void
874 semexit(p)
875 	struct proc *p;
876 {
877 	register struct sem_undo *suptr;
878 	register struct sem_undo **supptr;
879 	int did_something;
880 
881 	/*
882 	 * If somebody else is holding the global semaphore facility lock
883 	 * then sleep until it is released.
884 	 */
885 	while (semlock_holder != NULL && semlock_holder != p) {
886 #ifdef SEM_DEBUG
887 		printf("semaphore facility locked - sleeping ...\n");
888 #endif
889 		(void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semext", 0);
890 	}
891 
892 	did_something = 0;
893 
894 	/*
895 	 * Go through the chain of undo vectors looking for one
896 	 * associated with this process.
897 	 */
898 
899 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
900 	    supptr = &suptr->un_next) {
901 		if (suptr->un_proc == p)
902 			break;
903 	}
904 
905 	if (suptr == NULL)
906 		goto unlock;
907 
908 #ifdef SEM_DEBUG
909 	printf("proc @%08x has undo structure with %d entries\n", p,
910 	    suptr->un_cnt);
911 #endif
912 
913 	/*
914 	 * If there are any active undo elements then process them.
915 	 */
916 	if (suptr->un_cnt > 0) {
917 		int ix;
918 
919 		for (ix = 0; ix < suptr->un_cnt; ix++) {
920 			int semid = suptr->un_ent[ix].un_id;
921 			int semnum = suptr->un_ent[ix].un_num;
922 			int adjval = suptr->un_ent[ix].un_adjval;
923 			struct semid_ds *semaptr;
924 
925 			semaptr = &sema[semid];
926 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
927 				panic("semexit - semid not allocated");
928 			if (semnum >= semaptr->sem_nsems)
929 				panic("semexit - semnum out of range");
930 
931 #ifdef SEM_DEBUG
932 			printf("semexit:  %08x id=%d num=%d(adj=%d) ; sem=%d\n",
933 			    suptr->un_proc, suptr->un_ent[ix].un_id,
934 			    suptr->un_ent[ix].un_num,
935 			    suptr->un_ent[ix].un_adjval,
936 			    semaptr->sem_base[semnum].semval);
937 #endif
938 
939 			if (adjval < 0) {
940 				if (semaptr->sem_base[semnum].semval < -adjval)
941 					semaptr->sem_base[semnum].semval = 0;
942 				else
943 					semaptr->sem_base[semnum].semval +=
944 					    adjval;
945 			} else
946 				semaptr->sem_base[semnum].semval += adjval;
947 
948 #ifdef SEM_WAKEUP
949 			sem_wakeup((caddr_t)semaptr);
950 #else
951 			wakeup((caddr_t)semaptr);
952 #endif
953 #ifdef SEM_DEBUG
954 			printf("semexit:  back from wakeup\n");
955 #endif
956 		}
957 	}
958 
959 	/*
960 	 * Deallocate the undo vector.
961 	 */
962 #ifdef SEM_DEBUG
963 	printf("removing vector\n");
964 #endif
965 	suptr->un_proc = NULL;
966 	*supptr = suptr->un_next;
967 
968 unlock:
969 	/*
970 	 * If the exiting process is holding the global semaphore facility
971 	 * lock then release it.
972 	 */
973 	if (semlock_holder == p) {
974 		semlock_holder = NULL;
975 		wakeup((caddr_t)&semlock_holder);
976 	}
977 }
978