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