1 /* $FreeBSD: src/sys/kern/sysv_sem.c,v 1.69 2004/03/17 09:37:13 cperciva Exp $ */
4 * Implementation of SVID semaphores
6 * Author: Daniel Boulet
8 * This software is provided ``AS IS'' without any warranties of any kind.
11 #include "opt_sysvipc.h"
13 #include <sys/param.h>
14 #include <sys/systm.h>
15 #include <sys/sysproto.h>
16 #include <sys/kernel.h>
19 #include <sys/sysent.h>
20 #include <sys/sysctl.h>
21 #include <sys/malloc.h>
23 #include <sys/thread.h>
25 #include <sys/thread2.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 struct lwkt_token semu_token = LWKT_TOKEN_INITIALIZER(semu_token);
43 static int semtot = 0;
44 static struct semid_ds *sema; /* semaphore id pool */
45 static struct sem *sem; /* semaphore pool */
46 static struct sem_undo *semu_list; /* list of active undo structures */
47 static int *semu; /* undo structure pool */
50 u_short semval; /* semaphore value */
51 pid_t sempid; /* pid of last operation */
52 u_short semncnt; /* # awaiting semval > cval */
53 u_short semzcnt; /* # awaiting semval = 0 */
57 * Undo structure (one per process)
60 struct sem_undo *un_next; /* ptr to next active undo structure */
61 struct proc *un_proc; /* owner of this structure */
62 short un_cnt; /* # of active entries */
64 short un_adjval; /* adjust on exit values */
65 short un_num; /* semaphore # */
66 int un_id; /* semid */
67 } un_ent[1]; /* undo entries */
71 * Configuration parameters
74 #define SEMMNI 22 /* # of semaphore identifiers */
77 #define SEMMNS 341 /* # of semaphores in system */
80 #define SEMUME 10 /* max # of undo entries per process */
83 #define SEMMNU 30 /* # of undo structures in system */
86 /* shouldn't need tuning */
88 #define SEMMAP 30 /* # of entries in semaphore map */
91 #define SEMMSL SEMMNS /* max # of semaphores per id */
94 #define SEMOPM 100 /* max # of operations per semop call */
97 #define SEMVMX 32767 /* semaphore maximum value */
98 #define SEMAEM 16384 /* adjust on exit max value */
101 * Due to the way semaphore memory is allocated, we have to ensure that
102 * SEMUSZ is properly aligned.
105 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
107 /* actual size of an undo structure */
108 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
111 * Macro to find a particular sem_undo vector
113 #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu) + (ix) * \
117 * semaphore info struct
119 struct seminfo seminfo = {
120 SEMMAP, /* # of entries in semaphore map */
121 SEMMNI, /* # of semaphore identifiers */
122 SEMMNS, /* # of semaphores in system */
123 SEMMNU, /* # of undo structures in system */
124 SEMMSL, /* max # of semaphores per id */
125 SEMOPM, /* max # of operations per semop call */
126 SEMUME, /* max # of undo entries per process */
127 SEMUSZ, /* size in bytes of undo structure */
128 SEMVMX, /* semaphore maximum value */
129 SEMAEM /* adjust on exit max value */
132 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
133 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
134 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
135 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
136 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
137 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
138 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
139 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
140 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
141 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
143 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
144 "Number of entries in semaphore map");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0,
146 "Number of semaphore identifiers");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0,
148 "Total number of semaphores");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0,
150 "Total number of undo structures");
151 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
152 "Max number of semaphores per id");
153 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0,
154 "Max number of operations per semop call");
155 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0,
156 "Max number of undo entries per process");
157 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0,
158 "Size in bytes of undo structure");
159 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
160 "Semaphore maximum value");
161 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
162 "Adjust on exit max value");
165 RO seminfo.semmap /* SEMMAP unused */
168 RO seminfo.semmnu /* undo entries per system */
170 RO seminfo.semopm /* SEMOPM unused */
172 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
173 RO seminfo.semvmx /* SEMVMX unused - user param */
174 RO seminfo.semaem /* SEMAEM unused - user param */
182 sem = kmalloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
183 sema = kmalloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
184 semu = kmalloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
186 for (i = 0; i < seminfo.semmni; i++) {
187 sema[i].sem_base = 0;
188 sema[i].sem_perm.mode = 0;
190 for (i = 0; i < seminfo.semmnu; i++) {
191 struct sem_undo *suptr = SEMU(i);
192 suptr->un_proc = NULL;
196 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
199 * Entry point for all SEM calls
201 * semsys_args(int which, a2, a3, ...) (VARARGS)
206 sys_semsys(struct semsys_args *uap)
208 struct thread *td = curthread;
209 unsigned int which = (unsigned int)uap->which;
212 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
215 if (which >= NELEM(semcalls))
217 bcopy(&uap->a2, &uap->which,
218 sizeof(struct semsys_args) - offsetof(struct semsys_args, a2));
219 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)
227 * semu_token is held by the caller.
229 static struct sem_undo *
230 semu_alloc(struct proc *p)
233 struct sem_undo *suptr;
234 struct sem_undo **supptr;
238 * Try twice to allocate something.
239 * (we'll purge any empty structures after the first pass so
240 * 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.
247 for (i = 0; i < seminfo.semmnu; i++) {
249 if (suptr->un_proc == NULL) {
250 suptr->un_next = semu_list;
259 * We didn't find a free one, if this is the first attempt
260 * then try to free some structures.
264 /* All the structures are in use - try to free some */
265 int did_something = 0;
268 while ((suptr = *supptr) != NULL) {
269 if (suptr->un_cnt == 0) {
270 suptr->un_proc = NULL;
271 *supptr = suptr->un_next;
274 supptr = &(suptr->un_next);
278 /* If we didn't free anything then just give-up */
279 if (!did_something) {
285 * The second pass failed even though we freed
286 * something after the first pass!
287 * This is IMPOSSIBLE!
289 panic("semu_alloc - second attempt failed");
298 * Adjust a particular entry for a particular proc
302 semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
305 struct sem_undo *suptr;
311 * Look for and remember the sem_undo if the caller doesn't
314 lwkt_gettoken(&semu_token);
315 lwkt_gettoken(&p->p_token);
318 for (suptr = semu_list; suptr != NULL;
319 suptr = suptr->un_next) {
320 if (suptr->un_proc == p) {
328 p->p_flags |= P_SYSVSEM;
329 suptr = semu_alloc(p);
339 * Look for the requested entry and adjust it (delete if adjval becomes
342 sunptr = &suptr->un_ent[0];
343 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
344 if (sunptr->un_id != semid || sunptr->un_num != semnum)
347 sunptr->un_adjval = 0;
349 sunptr->un_adjval += adjval;
350 if (sunptr->un_adjval == 0) {
352 if (i < suptr->un_cnt)
354 suptr->un_ent[suptr->un_cnt];
359 /* Didn't find the right entry - create it */
362 if (suptr->un_cnt != seminfo.semume) {
363 sunptr = &suptr->un_ent[suptr->un_cnt];
365 sunptr->un_adjval = adjval;
366 sunptr->un_id = semid; sunptr->un_num = semnum;
371 lwkt_reltoken(&p->p_token);
372 lwkt_reltoken(&semu_token);
377 semundo_clear(int semid, int semnum)
379 struct sem_undo *suptr;
381 lwkt_gettoken(&semu_token);
382 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
383 struct undo *sunptr = &suptr->un_ent[0];
386 while (i < suptr->un_cnt) {
387 if (sunptr->un_id == semid) {
388 if (semnum == -1 || sunptr->un_num == semnum) {
390 if (i < suptr->un_cnt) {
392 suptr->un_ent[suptr->un_cnt];
402 lwkt_reltoken(&semu_token);
406 * Note that the user-mode half of this passes a union, not a pointer
411 sys___semctl(struct __semctl_args *uap)
413 struct thread *td = curthread;
414 int semid = uap->semid;
415 int semnum = uap->semnum;
417 union semun *arg = uap->arg;
418 union semun real_arg;
419 struct ucred *cred = td->td_ucred;
421 struct semid_ds sbuf;
422 struct semid_ds *semaptr;
423 struct semid_ds *semakptr;
426 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
429 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
435 * For this command we assume semid is an array index
436 * rather than an IPC id.
438 if (semid < 0 || semid >= seminfo.semmni) {
442 semakptr = &sema[semid];
443 lwkt_getpooltoken(semakptr);
444 if ((semakptr->sem_perm.mode & SEM_ALLOC) == 0) {
446 lwkt_relpooltoken(semakptr);
449 if ((eval = ipcperm(td->td_proc, &semakptr->sem_perm, IPC_R))) {
450 lwkt_relpooltoken(semakptr);
453 bcopy(&semakptr, arg->buf, sizeof(struct semid_ds));
454 rval = IXSEQ_TO_IPCID(semid, semakptr->sem_perm);
455 lwkt_relpooltoken(semakptr);
459 semid = IPCID_TO_IX(semid);
460 if (semid < 0 || semid >= seminfo.semmni) {
463 semaptr = &sema[semid];
464 lwkt_getpooltoken(semaptr);
466 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
467 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
468 lwkt_relpooltoken(semaptr);
477 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M)) != 0)
479 semaptr->sem_perm.cuid = cred->cr_uid;
480 semaptr->sem_perm.uid = cred->cr_uid;
481 semtot -= semaptr->sem_nsems;
482 for (i = semaptr->sem_base - sem; i < semtot; i++)
483 sem[i] = sem[i + semaptr->sem_nsems];
484 for (i = 0; i < seminfo.semmni; i++) {
485 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
486 sema[i].sem_base > semaptr->sem_base)
487 sema[i].sem_base -= semaptr->sem_nsems;
489 semaptr->sem_perm.mode = 0;
490 semundo_clear(semid, -1);
491 wakeup((caddr_t)semaptr);
495 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M);
498 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
500 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
501 sizeof(sbuf))) != 0) {
504 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
505 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
506 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
507 (sbuf.sem_perm.mode & 0777);
508 semaptr->sem_ctime = time_second;
512 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R)))
514 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
516 eval = copyout(semaptr, real_arg.buf, sizeof(struct semid_ds));
520 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
523 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
527 rval = semaptr->sem_base[semnum].semncnt;
531 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
534 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
538 rval = semaptr->sem_base[semnum].sempid;
542 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
545 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
549 rval = semaptr->sem_base[semnum].semval;
553 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
556 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
558 for (i = 0; i < semaptr->sem_nsems; i++) {
559 eval = copyout(&semaptr->sem_base[i].semval,
561 sizeof(real_arg.array[0]));
568 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R);
571 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
575 rval = semaptr->sem_base[semnum].semzcnt;
579 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
582 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
586 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
588 semaptr->sem_base[semnum].semval = real_arg.val;
589 semundo_clear(semid, semnum);
590 wakeup((caddr_t)semaptr);
594 eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W);
597 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
599 for (i = 0; i < semaptr->sem_nsems; i++) {
600 eval = copyin(&real_arg.array[i],
601 (caddr_t)&semaptr->sem_base[i].semval,
602 sizeof(real_arg.array[0]));
606 semundo_clear(semid, -1);
607 wakeup((caddr_t)semaptr);
614 lwkt_relpooltoken(semaptr);
617 uap->sysmsg_result = rval;
625 sys_semget(struct semget_args *uap)
627 struct thread *td = curthread;
630 int nsems = uap->nsems;
631 int semflg = uap->semflg;
632 struct ucred *cred = td->td_ucred;
635 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
638 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
643 if (key != IPC_PRIVATE) {
644 for (semid = 0; semid < seminfo.semmni; semid++) {
645 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0 ||
646 sema[semid].sem_perm.key != key) {
649 lwkt_getpooltoken(&sema[semid]);
650 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0 ||
651 sema[semid].sem_perm.key != key) {
652 lwkt_relpooltoken(&sema[semid]);
657 if (semid < seminfo.semmni) {
659 kprintf("found public key\n");
661 if ((eval = ipcperm(td->td_proc,
662 &sema[semid].sem_perm,
664 lwkt_relpooltoken(&sema[semid]);
667 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
669 kprintf("too small\n");
672 lwkt_relpooltoken(&sema[semid]);
675 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
677 kprintf("not exclusive\n");
680 lwkt_relpooltoken(&sema[semid]);
688 kprintf("need to allocate the semid_ds\n");
690 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
691 if (nsems <= 0 || nsems > seminfo.semmsl) {
693 kprintf("nsems out of range (0<%d<=%d)\n",
694 nsems, seminfo.semmsl);
699 if (nsems > seminfo.semmns - semtot) {
701 kprintf("not enough semaphores left "
702 "(need %d, got %d)\n",
703 nsems, seminfo.semmns - semtot);
708 for (semid = 0; semid < seminfo.semmni; semid++) {
709 if (sema[semid].sem_perm.mode & SEM_ALLOC)
711 lwkt_getpooltoken(&sema[semid]);
712 if (sema[semid].sem_perm.mode & SEM_ALLOC) {
713 lwkt_relpooltoken(&sema[semid]);
718 if (semid == seminfo.semmni) {
720 kprintf("no more semid_ds's available\n");
726 kprintf("semid %d is available\n", semid);
728 sema[semid].sem_perm.key = key;
729 sema[semid].sem_perm.cuid = cred->cr_uid;
730 sema[semid].sem_perm.uid = cred->cr_uid;
731 sema[semid].sem_perm.cgid = cred->cr_gid;
732 sema[semid].sem_perm.gid = cred->cr_gid;
733 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
734 sema[semid].sem_perm.seq =
735 (sema[semid].sem_perm.seq + 1) & 0x7fff;
736 sema[semid].sem_nsems = nsems;
737 sema[semid].sem_otime = 0;
738 sema[semid].sem_ctime = time_second;
739 sema[semid].sem_base = &sem[semtot];
741 bzero(sema[semid].sem_base,
742 sizeof(sema[semid].sem_base[0])*nsems);
744 kprintf("sembase = 0x%x, next = 0x%x\n",
745 sema[semid].sem_base, &sem[semtot]);
750 kprintf("didn't find it and wasn't asked to create it\n");
758 IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
759 lwkt_relpooltoken(&sema[semid]);
768 sys_semop(struct semop_args *uap)
770 struct thread *td = curthread;
771 int semid = uap->semid;
772 u_int nsops = uap->nsops;
773 struct sembuf sops[MAX_SOPS];
774 struct semid_ds *semaptr;
775 struct sembuf *sopptr;
777 struct sem_undo *suptr = NULL;
779 int do_wakeup, do_undos;
782 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
785 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
788 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
790 if (semid < 0 || semid >= seminfo.semmni) {
794 semaptr = &sema[semid];
795 lwkt_getpooltoken(semaptr);
796 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
800 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
805 if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W))) {
807 kprintf("eval = %d from ipaccess\n", eval);
812 if (nsops > MAX_SOPS) {
814 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
820 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
822 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
823 uap->sops, &sops, nsops * sizeof(sops[0]));
829 * Loop trying to satisfy the vector of requests.
830 * If we reach a point where we must wait, any requests already
831 * performed are rolled back and we go to sleep until some other
832 * process wakes us up. At this point, we start all over again.
834 * This ensures that from the perspective of other tasks, a set
835 * of requests is atomic (never partially satisfied).
842 for (i = 0; i < nsops; i++) {
845 if (sopptr->sem_num >= semaptr->sem_nsems) {
850 semptr = &semaptr->sem_base[sopptr->sem_num];
853 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
854 semaptr, semaptr->sem_base, semptr,
855 sopptr->sem_num, semptr->semval, sopptr->sem_op,
856 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
859 if (sopptr->sem_op < 0) {
860 if (semptr->semval + sopptr->sem_op < 0) {
862 kprintf("semop: can't do it now\n");
866 semptr->semval += sopptr->sem_op;
867 if (semptr->semval == 0 &&
871 if (sopptr->sem_flg & SEM_UNDO)
873 } else if (sopptr->sem_op == 0) {
874 if (semptr->semval > 0) {
876 kprintf("semop: not zero now\n");
881 if (semptr->semncnt > 0)
883 semptr->semval += sopptr->sem_op;
884 if (sopptr->sem_flg & SEM_UNDO)
890 * Did we get through the entire vector?
896 * No ... rollback anything that we've already done
899 kprintf("semop: rollback 0 through %d\n", i-1);
901 for (j = 0; j < i; j++)
902 semaptr->sem_base[sops[j].sem_num].semval -=
906 * If the request that we couldn't satisfy has the
907 * NOWAIT flag set then return with EAGAIN.
909 if (sopptr->sem_flg & IPC_NOWAIT) {
914 if (sopptr->sem_op == 0)
920 kprintf("semop: good night!\n");
922 eval = tsleep((caddr_t)semaptr, PCATCH, "semwait", 0);
924 kprintf("semop: good morning (eval=%d)!\n", eval);
927 suptr = NULL; /* sem_undo may have been reallocated */
929 /* return code is checked below, after sem[nz]cnt-- */
932 * Make sure that the semaphore still exists
934 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
935 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
941 * The semaphore is still alive. Readjust the count of
944 if (sopptr->sem_op == 0)
950 * Is it really morning, or was our sleep interrupted?
951 * (Delayed check of tsleep() return code because we
952 * need to decrement sem[nz]cnt either way.)
959 kprintf("semop: good morning!\n");
965 * Process any SEM_UNDO requests.
968 for (i = 0; i < nsops; i++) {
970 * We only need to deal with SEM_UNDO's for non-zero
975 if ((sops[i].sem_flg & SEM_UNDO) == 0)
977 adjval = sops[i].sem_op;
980 eval = semundo_adjust(td->td_proc, &suptr, semid,
981 sops[i].sem_num, -adjval);
986 * Oh-Oh! We ran out of either sem_undo's or undo's.
987 * Rollback the adjustments to this point and then
988 * rollback the semaphore ups and down so we can return
989 * with an error with all structures restored. We
990 * rollback the undo's in the exact reverse order that
991 * we applied them. This guarantees that we won't run
992 * out of space as we roll things back out.
994 for (j = i - 1; j >= 0; j--) {
995 if ((sops[j].sem_flg & SEM_UNDO) == 0)
997 adjval = sops[j].sem_op;
1000 if (semundo_adjust(td->td_proc, &suptr, semid,
1001 sops[j].sem_num, adjval) != 0)
1002 panic("semop - can't undo undos");
1005 for (j = 0; j < nsops; j++)
1006 semaptr->sem_base[sops[j].sem_num].semval -=
1010 kprintf("eval = %d from semundo_adjust\n", eval);
1013 } /* loop through the sops */
1014 } /* if (do_undos) */
1016 /* We're definitely done - set the sempid's */
1017 for (i = 0; i < nsops; i++) {
1019 semptr = &semaptr->sem_base[sopptr->sem_num];
1020 semptr->sempid = td->td_proc->p_pid;
1023 /* Do a wakeup if any semaphore was up'd. */
1026 kprintf("semop: doing wakeup\n");
1028 wakeup((caddr_t)semaptr);
1030 kprintf("semop: back from wakeup\n");
1034 kprintf("semop: done\n");
1036 uap->sysmsg_result = 0;
1039 lwkt_relpooltoken(semaptr);
1045 * Go through the undo structures for this process and apply the adjustments to
1049 semexit(struct proc *p)
1051 struct sem_undo *suptr;
1052 struct sem_undo **supptr;
1058 * We're getting a global token, don't do it if we couldn't
1059 * possibly have any semaphores.
1061 if ((p->p_flags & P_SYSVSEM) == 0)
1065 * Go through the chain of undo vectors looking for one
1066 * associated with this process. De-link it from the
1067 * list right now, while we have the token, but do not
1068 * clear un_proc until we finish cleaning up the relationship.
1070 lwkt_gettoken(&semu_token);
1071 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1072 supptr = &suptr->un_next) {
1073 if (suptr->un_proc == p) {
1074 *supptr = suptr->un_next;
1078 p->p_flags &= ~P_SYSVSEM;
1079 lwkt_reltoken(&semu_token);
1085 kprintf("proc @%08x has undo structure with %d entries\n", p,
1090 * If there are any active undo elements then process them.
1092 if (suptr->un_cnt > 0) {
1095 for (ix = 0; ix < suptr->un_cnt; ix++) {
1096 int semid = suptr->un_ent[ix].un_id;
1097 int semnum = suptr->un_ent[ix].un_num;
1098 int adjval = suptr->un_ent[ix].un_adjval;
1099 struct semid_ds *semaptr;
1101 semaptr = &sema[semid];
1102 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1103 panic("semexit - semid not allocated");
1104 if (semnum >= semaptr->sem_nsems)
1105 panic("semexit - semnum out of range");
1108 kprintf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1109 suptr->un_proc, suptr->un_ent[ix].un_id,
1110 suptr->un_ent[ix].un_num,
1111 suptr->un_ent[ix].un_adjval,
1112 semaptr->sem_base[semnum].semval);
1116 if (semaptr->sem_base[semnum].semval < -adjval)
1117 semaptr->sem_base[semnum].semval = 0;
1119 semaptr->sem_base[semnum].semval +=
1122 semaptr->sem_base[semnum].semval += adjval;
1124 wakeup((caddr_t)semaptr);
1126 kprintf("semexit: back from wakeup\n");
1132 * Deallocate the undo vector.
1135 suptr->un_proc = NULL;
1137 kprintf("removing vector\n");