1 /* $FreeBSD: src/sys/kern/sysv_sem.c,v 1.69 2004/03/17 09:37:13 cperciva Exp $ */
2 /* $DragonFly: src/sys/kern/sysv_sem.c,v 1.19 2008/01/06 16:55:51 swildner Exp $ */
5 * Implementation of SVID semaphores
7 * Author: Daniel Boulet
9 * This software is provided ``AS IS'' without any warranties of any kind.
12 #include "opt_sysvipc.h"
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/sysproto.h>
17 #include <sys/kernel.h>
20 #include <sys/sysent.h>
21 #include <sys/sysctl.h>
22 #include <sys/malloc.h>
25 #include <sys/mplock2.h>
27 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
29 static void seminit (void *);
31 static struct sem_undo *semu_alloc (struct proc *p);
32 static int semundo_adjust (struct proc *p, struct sem_undo **supptr,
33 int semid, int semnum, int adjval);
34 static void semundo_clear (int semid, int semnum);
36 /* XXX casting to (sy_call_t *) is bogus, as usual. */
37 static sy_call_t *semcalls[] = {
38 (sy_call_t *)sys___semctl, (sy_call_t *)sys_semget,
39 (sy_call_t *)sys_semop
42 static int semtot = 0;
43 static struct semid_ds *sema; /* semaphore id pool */
44 static struct sem *sem; /* semaphore pool */
45 static struct sem_undo *semu_list; /* list of active undo structures */
46 static int *semu; /* undo structure pool */
49 u_short semval; /* semaphore value */
50 pid_t sempid; /* pid of last operation */
51 u_short semncnt; /* # awaiting semval > cval */
52 u_short semzcnt; /* # awaiting semval = 0 */
56 * Undo structure (one per process)
59 struct sem_undo *un_next; /* ptr to next active undo structure */
60 struct proc *un_proc; /* owner of this structure */
61 short un_cnt; /* # of active entries */
63 short un_adjval; /* adjust on exit values */
64 short un_num; /* semaphore # */
65 int un_id; /* semid */
66 } un_ent[1]; /* undo entries */
70 * Configuration parameters
73 #define SEMMNI 10 /* # of semaphore identifiers */
76 #define SEMMNS 60 /* # of semaphores in system */
79 #define SEMUME 10 /* max # of undo entries per process */
82 #define SEMMNU 30 /* # of undo structures in system */
85 /* shouldn't need tuning */
87 #define SEMMAP 30 /* # of entries in semaphore map */
90 #define SEMMSL SEMMNS /* max # of semaphores per id */
93 #define SEMOPM 100 /* max # of operations per semop call */
96 #define SEMVMX 32767 /* semaphore maximum value */
97 #define SEMAEM 16384 /* adjust on exit max value */
100 * Due to the way semaphore memory is allocated, we have to ensure that
101 * SEMUSZ is properly aligned.
104 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
106 /* actual size of an undo structure */
107 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
110 * Macro to find a particular sem_undo vector
112 #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
115 * semaphore info struct
117 struct seminfo seminfo = {
118 SEMMAP, /* # of entries in semaphore map */
119 SEMMNI, /* # of semaphore identifiers */
120 SEMMNS, /* # of semaphores in system */
121 SEMMNU, /* # of undo structures in system */
122 SEMMSL, /* max # of semaphores per id */
123 SEMOPM, /* max # of operations per semop call */
124 SEMUME, /* max # of undo entries per process */
125 SEMUSZ, /* size in bytes of undo structure */
126 SEMVMX, /* semaphore maximum value */
127 SEMAEM /* adjust on exit max value */
130 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
131 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
132 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
133 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
134 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
135 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
136 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
137 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
138 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
139 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
141 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
142 "Number of entries in semaphore map");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0,
144 "Number of semaphore identifiers");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0,
146 "Total number of semaphores");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0,
148 "Total number of undo structures");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
150 "Max number of semaphores per id");
151 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0,
152 "Max number of operations per semop call");
153 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0,
154 "Max number of undo entries per process");
155 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0,
156 "Size in bytes of undo structure");
157 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
158 "Semaphore maximum value");
159 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
160 "Adjust on exit max value");
163 RO seminfo.semmap /* SEMMAP unused */
166 RO seminfo.semmnu /* undo entries per system */
168 RO seminfo.semopm /* SEMOPM unused */
170 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
171 RO seminfo.semvmx /* SEMVMX unused - user param */
172 RO seminfo.semaem /* SEMAEM unused - user param */
180 sem = kmalloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
181 sema = kmalloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
182 semu = kmalloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
184 for (i = 0; i < seminfo.semmni; i++) {
185 sema[i].sem_base = 0;
186 sema[i].sem_perm.mode = 0;
188 for (i = 0; i < seminfo.semmnu; i++) {
189 struct sem_undo *suptr = SEMU(i);
190 suptr->un_proc = NULL;
194 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
197 * Entry point for all SEM calls
199 * semsys_args(int which, a2, a3, ...) (VARARGS)
204 sys_semsys(struct semsys_args *uap)
206 struct thread *td = curthread;
207 unsigned int which = (unsigned int)uap->which;
210 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
213 if (which >= sizeof(semcalls)/sizeof(semcalls[0]))
215 bcopy(&uap->a2, &uap->which,
216 sizeof(struct semsys_args) - offsetof(struct semsys_args, a2));
218 error = (*semcalls[which])(uap);
224 * Allocate a new sem_undo structure for a process
225 * (returns ptr to structure or NULL if no more room)
228 static struct sem_undo *
229 semu_alloc(struct proc *p)
232 struct sem_undo *suptr;
233 struct sem_undo **supptr;
237 * Try twice to allocate something.
238 * (we'll purge any empty structures after the first pass so
239 * two passes are always enough)
242 for (attempt = 0; attempt < 2; attempt++) {
244 * Look for a free structure.
245 * Fill it in and return it if we find one.
248 for (i = 0; i < seminfo.semmnu; i++) {
250 if (suptr->un_proc == NULL) {
251 suptr->un_next = semu_list;
260 * We didn't find a free one, if this is the first attempt
261 * then try to free some structures.
265 /* All the structures are in use - try to free some */
266 int did_something = 0;
269 while ((suptr = *supptr) != NULL) {
270 if (suptr->un_cnt == 0) {
271 suptr->un_proc = NULL;
272 *supptr = suptr->un_next;
275 supptr = &(suptr->un_next);
278 /* If we didn't free anything then just give-up */
283 * The second pass failed even though we freed
284 * something after the first pass!
285 * This is IMPOSSIBLE!
287 panic("semu_alloc - second attempt failed");
294 * Adjust a particular entry for a particular proc
298 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
301 struct sem_undo *suptr;
305 /* Look for and remember the sem_undo if the caller doesn't provide
310 for (suptr = semu_list; suptr != NULL;
311 suptr = suptr->un_next) {
312 if (suptr->un_proc == p) {
320 suptr = semu_alloc(p);
328 * Look for the requested entry and adjust it (delete if adjval becomes
331 sunptr = &suptr->un_ent[0];
332 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
333 if (sunptr->un_id != semid || sunptr->un_num != semnum)
336 sunptr->un_adjval = 0;
338 sunptr->un_adjval += adjval;
339 if (sunptr->un_adjval == 0) {
341 if (i < suptr->un_cnt)
343 suptr->un_ent[suptr->un_cnt];
348 /* Didn't find the right entry - create it */
351 if (suptr->un_cnt != seminfo.semume) {
352 sunptr = &suptr->un_ent[suptr->un_cnt];
354 sunptr->un_adjval = adjval;
355 sunptr->un_id = semid; sunptr->un_num = semnum;
362 semundo_clear(int semid, int semnum)
364 struct sem_undo *suptr;
366 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
367 struct undo *sunptr = &suptr->un_ent[0];
370 while (i < suptr->un_cnt) {
371 if (sunptr->un_id == semid) {
372 if (semnum == -1 || sunptr->un_num == semnum) {
374 if (i < suptr->un_cnt) {
376 suptr->un_ent[suptr->un_cnt];
389 * Note that the user-mode half of this passes a union, not a pointer
394 sys___semctl(struct __semctl_args *uap)
396 struct thread *td = curthread;
397 int semid = uap->semid;
398 int semnum = uap->semnum;
400 union semun *arg = uap->arg;
401 union semun real_arg;
402 struct ucred *cred = td->td_ucred;
404 struct semid_ds sbuf;
405 struct semid_ds *semaptr;
406 struct semid_ds *semakptr;
409 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
412 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
419 * For this command we assume semid is an array index
420 * rather than an IPC id.
422 if (semid < 0 || semid >= seminfo.semmni) {
426 semakptr = &sema[semid];
427 if ((semakptr->sem_perm.mode & SEM_ALLOC) == 0) {
431 if ((eval = ipcperm(td->td_proc, &semakptr->sem_perm, IPC_R)))
434 bcopy(&semakptr, arg->buf, sizeof(struct semid_ds));
435 rval = IXSEQ_TO_IPCID(semid, semakptr->sem_perm);
439 semid = IPCID_TO_IX(semid);
440 if (semid < 0 || semid >= seminfo.semmni) {
445 semaptr = &sema[semid];
446 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
447 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
457 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M)) != 0)
459 semaptr->sem_perm.cuid = cred->cr_uid;
460 semaptr->sem_perm.uid = cred->cr_uid;
461 semtot -= semaptr->sem_nsems;
462 for (i = semaptr->sem_base - sem; i < semtot; i++)
463 sem[i] = sem[i + semaptr->sem_nsems];
464 for (i = 0; i < seminfo.semmni; i++) {
465 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
466 sema[i].sem_base > semaptr->sem_base)
467 sema[i].sem_base -= semaptr->sem_nsems;
469 semaptr->sem_perm.mode = 0;
470 semundo_clear(semid, -1);
471 wakeup((caddr_t)semaptr);
475 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M);
478 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
480 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
481 sizeof(sbuf))) != 0) {
484 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
485 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
486 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
487 (sbuf.sem_perm.mode & 0777);
488 semaptr->sem_ctime = time_second;
492 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R)))
494 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
496 eval = copyout(semaptr, real_arg.buf, sizeof(struct semid_ds));
500 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
503 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
507 rval = semaptr->sem_base[semnum].semncnt;
511 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
514 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
518 rval = semaptr->sem_base[semnum].sempid;
522 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
525 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
529 rval = semaptr->sem_base[semnum].semval;
533 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
536 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
538 for (i = 0; i < semaptr->sem_nsems; i++) {
539 eval = copyout(&semaptr->sem_base[i].semval,
541 sizeof(real_arg.array[0]));
548 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
551 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
555 rval = semaptr->sem_base[semnum].semzcnt;
559 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
562 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
566 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
568 semaptr->sem_base[semnum].semval = real_arg.val;
569 semundo_clear(semid, semnum);
570 wakeup((caddr_t)semaptr);
574 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
577 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
579 for (i = 0; i < semaptr->sem_nsems; i++) {
580 eval = copyin(&real_arg.array[i],
581 (caddr_t)&semaptr->sem_base[i].semval,
582 sizeof(real_arg.array[0]));
586 semundo_clear(semid, -1);
587 wakeup((caddr_t)semaptr);
597 uap->sysmsg_result = rval;
605 sys_semget(struct semget_args *uap)
607 struct thread *td = curthread;
610 int nsems = uap->nsems;
611 int semflg = uap->semflg;
612 struct ucred *cred = td->td_ucred;
615 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
618 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
624 if (key != IPC_PRIVATE) {
625 for (semid = 0; semid < seminfo.semmni; semid++) {
626 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
627 sema[semid].sem_perm.key == key)
630 if (semid < seminfo.semmni) {
632 kprintf("found public key\n");
634 if ((eval = ipcperm(td->td_proc,
635 &sema[semid].sem_perm,
639 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
641 kprintf("too small\n");
646 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
648 kprintf("not exclusive\n");
658 kprintf("need to allocate the semid_ds\n");
660 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
661 if (nsems <= 0 || nsems > seminfo.semmsl) {
663 kprintf("nsems out of range (0<%d<=%d)\n", nsems,
669 if (nsems > seminfo.semmns - semtot) {
671 kprintf("not enough semaphores left (need %d, got %d)\n",
672 nsems, seminfo.semmns - semtot);
677 for (semid = 0; semid < seminfo.semmni; semid++) {
678 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
681 if (semid == seminfo.semmni) {
683 kprintf("no more semid_ds's available\n");
689 kprintf("semid %d is available\n", semid);
691 sema[semid].sem_perm.key = key;
692 sema[semid].sem_perm.cuid = cred->cr_uid;
693 sema[semid].sem_perm.uid = cred->cr_uid;
694 sema[semid].sem_perm.cgid = cred->cr_gid;
695 sema[semid].sem_perm.gid = cred->cr_gid;
696 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
697 sema[semid].sem_perm.seq =
698 (sema[semid].sem_perm.seq + 1) & 0x7fff;
699 sema[semid].sem_nsems = nsems;
700 sema[semid].sem_otime = 0;
701 sema[semid].sem_ctime = time_second;
702 sema[semid].sem_base = &sem[semtot];
704 bzero(sema[semid].sem_base,
705 sizeof(sema[semid].sem_base[0])*nsems);
707 kprintf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
712 kprintf("didn't find it and wasn't asked to create it\n");
719 uap->sysmsg_result = IXSEQ_TO_IPCID(semid,
720 sema[semid].sem_perm);
730 sys_semop(struct semop_args *uap)
732 struct thread *td = curthread;
733 int semid = uap->semid;
734 u_int nsops = uap->nsops;
735 struct sembuf sops[MAX_SOPS];
736 struct semid_ds *semaptr;
737 struct sembuf *sopptr;
739 struct sem_undo *suptr = NULL;
741 int do_wakeup, do_undos;
744 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
747 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
751 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
753 if (semid < 0 || semid >= seminfo.semmni) {
758 semaptr = &sema[semid];
759 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
763 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
768 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W))) {
770 kprintf("eval = %d from ipaccess\n", eval);
775 if (nsops > MAX_SOPS) {
777 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
783 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
785 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
786 uap->sops, &sops, nsops * sizeof(sops[0]));
792 * Loop trying to satisfy the vector of requests.
793 * If we reach a point where we must wait, any requests already
794 * performed are rolled back and we go to sleep until some other
795 * process wakes us up. At this point, we start all over again.
797 * This ensures that from the perspective of other tasks, a set
798 * of requests is atomic (never partially satisfied).
805 for (i = 0; i < nsops; i++) {
808 if (sopptr->sem_num >= semaptr->sem_nsems) {
813 semptr = &semaptr->sem_base[sopptr->sem_num];
816 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
817 semaptr, semaptr->sem_base, semptr,
818 sopptr->sem_num, semptr->semval, sopptr->sem_op,
819 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
822 if (sopptr->sem_op < 0) {
823 if (semptr->semval + sopptr->sem_op < 0) {
825 kprintf("semop: can't do it now\n");
829 semptr->semval += sopptr->sem_op;
830 if (semptr->semval == 0 &&
834 if (sopptr->sem_flg & SEM_UNDO)
836 } else if (sopptr->sem_op == 0) {
837 if (semptr->semval > 0) {
839 kprintf("semop: not zero now\n");
844 if (semptr->semncnt > 0)
846 semptr->semval += sopptr->sem_op;
847 if (sopptr->sem_flg & SEM_UNDO)
853 * Did we get through the entire vector?
859 * No ... rollback anything that we've already done
862 kprintf("semop: rollback 0 through %d\n", i-1);
864 for (j = 0; j < i; j++)
865 semaptr->sem_base[sops[j].sem_num].semval -=
869 * If the request that we couldn't satisfy has the
870 * NOWAIT flag set then return with EAGAIN.
872 if (sopptr->sem_flg & IPC_NOWAIT) {
877 if (sopptr->sem_op == 0)
883 kprintf("semop: good night!\n");
885 eval = tsleep((caddr_t)semaptr, PCATCH, "semwait", 0);
887 kprintf("semop: good morning (eval=%d)!\n", eval);
890 suptr = NULL; /* sem_undo may have been reallocated */
892 /* return code is checked below, after sem[nz]cnt-- */
895 * Make sure that the semaphore still exists
897 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
898 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
904 * The semaphore is still alive. Readjust the count of
907 if (sopptr->sem_op == 0)
913 * Is it really morning, or was our sleep interrupted?
914 * (Delayed check of tsleep() return code because we
915 * need to decrement sem[nz]cnt either way.)
922 kprintf("semop: good morning!\n");
928 * Process any SEM_UNDO requests.
931 for (i = 0; i < nsops; i++) {
933 * We only need to deal with SEM_UNDO's for non-zero
938 if ((sops[i].sem_flg & SEM_UNDO) == 0)
940 adjval = sops[i].sem_op;
943 eval = semundo_adjust(td->td_proc, &suptr, semid,
944 sops[i].sem_num, -adjval);
949 * Oh-Oh! We ran out of either sem_undo's or undo's.
950 * Rollback the adjustments to this point and then
951 * rollback the semaphore ups and down so we can return
952 * with an error with all structures restored. We
953 * rollback the undo's in the exact reverse order that
954 * we applied them. This guarantees that we won't run
955 * out of space as we roll things back out.
957 for (j = i - 1; j >= 0; j--) {
958 if ((sops[j].sem_flg & SEM_UNDO) == 0)
960 adjval = sops[j].sem_op;
963 if (semundo_adjust(td->td_proc, &suptr, semid,
964 sops[j].sem_num, adjval) != 0)
965 panic("semop - can't undo undos");
968 for (j = 0; j < nsops; j++)
969 semaptr->sem_base[sops[j].sem_num].semval -=
973 kprintf("eval = %d from semundo_adjust\n", eval);
976 } /* loop through the sops */
977 } /* if (do_undos) */
979 /* We're definitely done - set the sempid's */
980 for (i = 0; i < nsops; i++) {
982 semptr = &semaptr->sem_base[sopptr->sem_num];
983 semptr->sempid = td->td_proc->p_pid;
986 /* Do a wakeup if any semaphore was up'd. */
989 kprintf("semop: doing wakeup\n");
991 wakeup((caddr_t)semaptr);
993 kprintf("semop: back from wakeup\n");
997 kprintf("semop: done\n");
999 uap->sysmsg_result = 0;
1007 * Go through the undo structures for this process and apply the adjustments to
1011 semexit(struct proc *p)
1013 struct sem_undo *suptr;
1014 struct sem_undo **supptr;
1020 * Go through the chain of undo vectors looking for one
1021 * associated with this process.
1024 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1025 supptr = &suptr->un_next) {
1026 if (suptr->un_proc == p)
1034 kprintf("proc @%08x has undo structure with %d entries\n", p,
1039 * If there are any active undo elements then process them.
1041 if (suptr->un_cnt > 0) {
1044 for (ix = 0; ix < suptr->un_cnt; ix++) {
1045 int semid = suptr->un_ent[ix].un_id;
1046 int semnum = suptr->un_ent[ix].un_num;
1047 int adjval = suptr->un_ent[ix].un_adjval;
1048 struct semid_ds *semaptr;
1050 semaptr = &sema[semid];
1051 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1052 panic("semexit - semid not allocated");
1053 if (semnum >= semaptr->sem_nsems)
1054 panic("semexit - semnum out of range");
1057 kprintf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1058 suptr->un_proc, suptr->un_ent[ix].un_id,
1059 suptr->un_ent[ix].un_num,
1060 suptr->un_ent[ix].un_adjval,
1061 semaptr->sem_base[semnum].semval);
1065 if (semaptr->sem_base[semnum].semval < -adjval)
1066 semaptr->sem_base[semnum].semval = 0;
1068 semaptr->sem_base[semnum].semval +=
1071 semaptr->sem_base[semnum].semval += adjval;
1073 wakeup((caddr_t)semaptr);
1075 kprintf("semexit: back from wakeup\n");
1081 * Deallocate the undo vector.
1084 kprintf("removing vector\n");
1086 suptr->un_proc = NULL;
1087 *supptr = suptr->un_next;