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/mutex.h>
21 #include <sys/syscall.h>
22 #include <sys/sysent.h>
23 #include <sys/sysctl.h>
24 #include <sys/malloc.h>
27 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
29 static void seminit __P((void));
30 static int sysvsem_modload __P((struct module *, int, void *));
31 static int semunload __P((void));
32 static void semexit_myhook __P((struct proc *p));
33 static int sysctl_sema __P((SYSCTL_HANDLER_ARGS));
35 #ifndef _SYS_SYSPROTO_H_
37 int __semctl __P((struct thread *td, struct __semctl_args *uap));
39 int semget __P((struct thread *td, struct semget_args *uap));
41 int semop __P((struct thread *td, struct semop_args *uap));
44 static struct sem_undo *semu_alloc __P((struct thread *td));
45 static int semundo_adjust __P((struct thread *td, struct sem_undo **supptr,
46 int semid, int semnum, int adjval));
47 static void semundo_clear __P((int semid, int semnum));
49 /* XXX casting to (sy_call_t *) is bogus, as usual. */
50 static sy_call_t *semcalls[] = {
51 (sy_call_t *)__semctl, (sy_call_t *)semget,
55 static int semtot = 0;
56 static struct semid_ds *sema; /* semaphore id pool */
57 static struct sem *sem; /* semaphore pool */
58 static struct sem_undo *semu_list; /* list of active undo structures */
59 static int *semu; /* undo structure pool */
62 u_short semval; /* semaphore value */
63 pid_t sempid; /* pid of last operation */
64 u_short semncnt; /* # awaiting semval > cval */
65 u_short semzcnt; /* # awaiting semval = 0 */
69 * Undo structure (one per process)
72 struct sem_undo *un_next; /* ptr to next active undo structure */
73 struct proc *un_proc; /* owner of this structure */
74 short un_cnt; /* # of active entries */
76 short un_adjval; /* adjust on exit values */
77 short un_num; /* semaphore # */
78 int un_id; /* semid */
79 } un_ent[1]; /* undo entries */
83 * Configuration parameters
86 #define SEMMNI 10 /* # of semaphore identifiers */
89 #define SEMMNS 60 /* # of semaphores in system */
92 #define SEMUME 10 /* max # of undo entries per process */
95 #define SEMMNU 30 /* # of undo structures in system */
98 /* shouldn't need tuning */
100 #define SEMMAP 30 /* # of entries in semaphore map */
103 #define SEMMSL SEMMNS /* max # of semaphores per id */
106 #define SEMOPM 100 /* max # of operations per semop call */
109 #define SEMVMX 32767 /* semaphore maximum value */
110 #define SEMAEM 16384 /* adjust on exit max value */
113 * Due to the way semaphore memory is allocated, we have to ensure that
114 * SEMUSZ is properly aligned.
117 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
119 /* actual size of an undo structure */
120 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
123 * Macro to find a particular sem_undo vector
125 #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
128 * semaphore info struct
130 struct seminfo seminfo = {
131 SEMMAP, /* # of entries in semaphore map */
132 SEMMNI, /* # of semaphore identifiers */
133 SEMMNS, /* # of semaphores in system */
134 SEMMNU, /* # of undo structures in system */
135 SEMMSL, /* max # of semaphores per id */
136 SEMOPM, /* max # of operations per semop call */
137 SEMUME, /* max # of undo entries per process */
138 SEMUSZ, /* size in bytes of undo structure */
139 SEMVMX, /* semaphore maximum value */
140 SEMAEM /* adjust on exit max value */
143 SYSCTL_DECL(_kern_ipc);
144 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0, "");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0, "");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0, "");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0, "");
150 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0, "");
151 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0, "");
152 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
153 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
154 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
155 NULL, 0, sysctl_sema, "", "");
158 RO seminfo.semmap /* SEMMAP unused */
161 RO seminfo.semmnu /* undo entries per system */
163 RO seminfo.semopm /* SEMOPM unused */
165 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
166 RO seminfo.semvmx /* SEMVMX unused - user param */
167 RO seminfo.semaem /* SEMAEM unused - user param */
175 TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
176 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
177 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
178 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
179 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
180 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
181 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
182 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
183 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
184 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
186 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
188 panic("sem is NULL");
189 sema = malloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
191 panic("sema is NULL");
192 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
194 panic("semu is NULL");
196 for (i = 0; i < seminfo.semmni; i++) {
197 sema[i].sem_base = 0;
198 sema[i].sem_perm.mode = 0;
200 for (i = 0; i < seminfo.semmnu; i++) {
201 register struct sem_undo *suptr = SEMU(i);
202 suptr->un_proc = NULL;
205 at_exit(semexit_myhook);
218 rm_at_exit(semexit_myhook);
223 sysvsem_modload(struct module *module, int cmd, void *arg)
243 static moduledata_t sysvsem_mod = {
249 SYSCALL_MODULE_HELPER(semsys);
250 SYSCALL_MODULE_HELPER(__semctl);
251 SYSCALL_MODULE_HELPER(semget);
252 SYSCALL_MODULE_HELPER(semop);
254 DECLARE_MODULE(sysvsem, sysvsem_mod,
255 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
256 MODULE_VERSION(sysvsem, 1);
259 * Entry point for all SEM calls
266 /* XXX actually varargs. */
267 struct semsys_args /* {
278 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
282 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0])) {
286 error = (*semcalls[uap->which])(td, &uap->a2);
293 * Allocate a new sem_undo structure for a process
294 * (returns ptr to structure or NULL if no more room)
297 static struct sem_undo *
302 register struct sem_undo *suptr;
303 register struct sem_undo **supptr;
307 * Try twice to allocate something.
308 * (we'll purge any empty structures after the first pass so
309 * two passes are always enough)
312 for (attempt = 0; attempt < 2; attempt++) {
314 * Look for a free structure.
315 * Fill it in and return it if we find one.
318 for (i = 0; i < seminfo.semmnu; i++) {
320 if (suptr->un_proc == NULL) {
321 suptr->un_next = semu_list;
324 suptr->un_proc = td->td_proc;
330 * We didn't find a free one, if this is the first attempt
331 * then try to free some structures.
335 /* All the structures are in use - try to free some */
336 int did_something = 0;
339 while ((suptr = *supptr) != NULL) {
340 if (suptr->un_cnt == 0) {
341 suptr->un_proc = NULL;
342 *supptr = suptr->un_next;
345 supptr = &(suptr->un_next);
348 /* If we didn't free anything then just give-up */
353 * The second pass failed even though we freed
354 * something after the first pass!
355 * This is IMPOSSIBLE!
357 panic("semu_alloc - second attempt failed");
364 * Adjust a particular entry for a particular proc
368 semundo_adjust(td, supptr, semid, semnum, adjval)
369 register struct thread *td;
370 struct sem_undo **supptr;
374 struct proc *p = td->td_proc;
375 register struct sem_undo *suptr;
376 register struct undo *sunptr;
379 /* Look for and remember the sem_undo if the caller doesn't provide
384 for (suptr = semu_list; suptr != NULL;
385 suptr = suptr->un_next) {
386 if (suptr->un_proc == p) {
394 suptr = semu_alloc(td);
402 * Look for the requested entry and adjust it (delete if adjval becomes
405 sunptr = &suptr->un_ent[0];
406 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
407 if (sunptr->un_id != semid || sunptr->un_num != semnum)
410 adjval += sunptr->un_adjval;
411 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
414 sunptr->un_adjval = adjval;
415 if (sunptr->un_adjval == 0) {
417 if (i < suptr->un_cnt)
419 suptr->un_ent[suptr->un_cnt];
424 /* Didn't find the right entry - create it */
427 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
429 if (suptr->un_cnt != seminfo.semume) {
430 sunptr = &suptr->un_ent[suptr->un_cnt];
432 sunptr->un_adjval = adjval;
433 sunptr->un_id = semid; sunptr->un_num = semnum;
440 semundo_clear(semid, semnum)
443 register struct sem_undo *suptr;
445 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
446 register struct undo *sunptr = &suptr->un_ent[0];
449 while (i < suptr->un_cnt) {
450 if (sunptr->un_id == semid) {
451 if (semnum == -1 || sunptr->un_num == semnum) {
453 if (i < suptr->un_cnt) {
455 suptr->un_ent[suptr->un_cnt];
468 * Note that the user-mode half of this passes a union, not a pointer
470 #ifndef _SYS_SYSPROTO_H_
471 struct __semctl_args {
485 register struct __semctl_args *uap;
487 int semid = uap->semid;
488 int semnum = uap->semnum;
490 union semun *arg = uap->arg;
491 union semun real_arg;
492 struct ucred *cred = td->td_proc->p_ucred;
494 struct semid_ds sbuf;
495 register struct semid_ds *semaptr;
499 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
502 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
509 if (semid < 0 || semid >= seminfo.semmsl)
511 semaptr = &sema[semid];
512 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 )
514 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
516 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
518 error = copyout((caddr_t)semaptr, real_arg.buf,
519 sizeof(struct semid_ds));
520 rval = IXSEQ_TO_IPCID(semid,semaptr->sem_perm);
522 td->td_retval[0] = rval;
526 semid = IPCID_TO_IX(semid);
527 if (semid < 0 || semid >= seminfo.semmsl) {
532 semaptr = &sema[semid];
533 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
534 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
544 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
546 semaptr->sem_perm.cuid = cred->cr_uid;
547 semaptr->sem_perm.uid = cred->cr_uid;
548 semtot -= semaptr->sem_nsems;
549 for (i = semaptr->sem_base - sem; i < semtot; i++)
550 sem[i] = sem[i + semaptr->sem_nsems];
551 for (i = 0; i < seminfo.semmni; i++) {
552 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
553 sema[i].sem_base > semaptr->sem_base)
554 sema[i].sem_base -= semaptr->sem_nsems;
556 semaptr->sem_perm.mode = 0;
557 semundo_clear(semid, -1);
558 wakeup((caddr_t)semaptr);
562 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
564 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
566 if ((error = copyin(real_arg.buf, (caddr_t)&sbuf,
567 sizeof(sbuf))) != 0) {
570 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
571 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
572 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
573 (sbuf.sem_perm.mode & 0777);
574 semaptr->sem_ctime = time_second;
578 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
580 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
582 error = copyout((caddr_t)semaptr, real_arg.buf,
583 sizeof(struct semid_ds));
587 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
589 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
593 rval = semaptr->sem_base[semnum].semncnt;
597 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
599 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
603 rval = semaptr->sem_base[semnum].sempid;
607 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
609 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
613 rval = semaptr->sem_base[semnum].semval;
617 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
619 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
621 for (i = 0; i < semaptr->sem_nsems; i++) {
622 error = copyout((caddr_t)&semaptr->sem_base[i].semval,
623 &real_arg.array[i], sizeof(real_arg.array[0]));
630 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
632 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
636 rval = semaptr->sem_base[semnum].semzcnt;
640 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
642 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
646 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
648 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
652 semaptr->sem_base[semnum].semval = real_arg.val;
653 semundo_clear(semid, semnum);
654 wakeup((caddr_t)semaptr);
658 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
660 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
662 for (i = 0; i < semaptr->sem_nsems; i++) {
663 error = copyin(&real_arg.array[i],
664 (caddr_t)&usval, sizeof(real_arg.array[0]));
667 if (usval > seminfo.semvmx) {
671 semaptr->sem_base[i].semval = usval;
673 semundo_clear(semid, -1);
674 wakeup((caddr_t)semaptr);
683 td->td_retval[0] = rval;
689 #ifndef _SYS_SYSPROTO_H_
703 register struct semget_args *uap;
705 int semid, error = 0;
707 int nsems = uap->nsems;
708 int semflg = uap->semflg;
709 struct ucred *cred = td->td_proc->p_ucred;
712 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
715 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
720 if (key != IPC_PRIVATE) {
721 for (semid = 0; semid < seminfo.semmni; semid++) {
722 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
723 sema[semid].sem_perm.key == key)
726 if (semid < seminfo.semmni) {
728 printf("found public key\n");
730 if ((error = ipcperm(td, &sema[semid].sem_perm,
734 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
736 printf("too small\n");
741 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
743 printf("not exclusive\n");
753 printf("need to allocate the semid_ds\n");
755 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
756 if (nsems <= 0 || nsems > seminfo.semmsl) {
758 printf("nsems out of range (0<%d<=%d)\n", nsems,
764 if (nsems > seminfo.semmns - semtot) {
766 printf("not enough semaphores left (need %d, got %d)\n",
767 nsems, seminfo.semmns - semtot);
772 for (semid = 0; semid < seminfo.semmni; semid++) {
773 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
776 if (semid == seminfo.semmni) {
778 printf("no more semid_ds's available\n");
784 printf("semid %d is available\n", semid);
786 sema[semid].sem_perm.key = key;
787 sema[semid].sem_perm.cuid = cred->cr_uid;
788 sema[semid].sem_perm.uid = cred->cr_uid;
789 sema[semid].sem_perm.cgid = cred->cr_gid;
790 sema[semid].sem_perm.gid = cred->cr_gid;
791 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
792 sema[semid].sem_perm.seq =
793 (sema[semid].sem_perm.seq + 1) & 0x7fff;
794 sema[semid].sem_nsems = nsems;
795 sema[semid].sem_otime = 0;
796 sema[semid].sem_ctime = time_second;
797 sema[semid].sem_base = &sem[semtot];
799 bzero(sema[semid].sem_base,
800 sizeof(sema[semid].sem_base[0])*nsems);
802 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
807 printf("didn't find it and wasn't asked to create it\n");
814 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
820 #ifndef _SYS_SYSPROTO_H_
834 register struct semop_args *uap;
836 int semid = uap->semid;
837 u_int nsops = uap->nsops;
838 struct sembuf *sops = NULL;
839 register struct semid_ds *semaptr;
840 register struct sembuf *sopptr;
841 register struct sem *semptr;
842 struct sem_undo *suptr;
844 int do_wakeup, do_undos;
847 printf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
851 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
856 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
858 if (semid < 0 || semid >= seminfo.semmsl) {
863 semaptr = &sema[semid];
864 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
868 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
872 if (nsops > seminfo.semopm) {
874 printf("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
881 /* Allocate memory for sem_ops */
882 sops = malloc(nsops * sizeof(sops[0]), M_SEM, M_WAITOK);
884 panic("Failed to allocate %d sem_ops", nsops);
886 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
888 printf("error = %d from copyin(%08x, %08x, %d)\n", error,
889 uap->sops, sops, nsops * sizeof(sops[0]));
895 * Initial pass thru sops to see what permissions are needed.
896 * Also perform any checks that don't need repeating on each
897 * attempt to satisfy the request vector.
899 j = 0; /* permission needed */
901 for (i = 0; i < nsops; i++) {
903 if (sopptr->sem_num >= semaptr->sem_nsems) {
907 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
909 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
912 if ((error = ipcperm(td, &semaptr->sem_perm, j))) {
914 printf("error = %d from ipaccess\n", error);
920 * Loop trying to satisfy the vector of requests.
921 * If we reach a point where we must wait, any requests already
922 * performed are rolled back and we go to sleep until some other
923 * process wakes us up. At this point, we start all over again.
925 * This ensures that from the perspective of other tasks, a set
926 * of requests is atomic (never partially satisfied).
930 error = 0; /* error return if necessary */
932 for (i = 0; i < nsops; i++) {
934 semptr = &semaptr->sem_base[sopptr->sem_num];
937 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
938 semaptr, semaptr->sem_base, semptr,
939 sopptr->sem_num, semptr->semval, sopptr->sem_op,
940 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
943 if (sopptr->sem_op < 0) {
944 if (semptr->semval + sopptr->sem_op < 0) {
946 printf("semop: can't do it now\n");
950 semptr->semval += sopptr->sem_op;
951 if (semptr->semval == 0 &&
955 } else if (sopptr->sem_op == 0) {
956 if (semptr->semval != 0) {
958 printf("semop: not zero now\n");
962 } else if (semptr->semval + sopptr->sem_op >
967 if (semptr->semncnt > 0)
969 semptr->semval += sopptr->sem_op;
974 * Did we get through the entire vector?
980 * No ... rollback anything that we've already done
983 printf("semop: rollback 0 through %d\n", i-1);
985 for (j = 0; j < i; j++)
986 semaptr->sem_base[sops[j].sem_num].semval -=
989 /* If we detected an error, return it */
994 * If the request that we couldn't satisfy has the
995 * NOWAIT flag set then return with EAGAIN.
997 if (sopptr->sem_flg & IPC_NOWAIT) {
1002 if (sopptr->sem_op == 0)
1008 printf("semop: good night!\n");
1010 error = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
1013 printf("semop: good morning (error=%d)!\n", error);
1021 printf("semop: good morning!\n");
1025 * Make sure that the semaphore still exists
1027 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1028 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1034 * The semaphore is still alive. Readjust the count of
1035 * waiting processes.
1037 if (sopptr->sem_op == 0)
1045 * Process any SEM_UNDO requests.
1049 for (i = 0; i < nsops; i++) {
1051 * We only need to deal with SEM_UNDO's for non-zero
1056 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1058 adjval = sops[i].sem_op;
1061 error = semundo_adjust(td, &suptr, semid,
1062 sops[i].sem_num, -adjval);
1067 * Oh-Oh! We ran out of either sem_undo's or undo's.
1068 * Rollback the adjustments to this point and then
1069 * rollback the semaphore ups and down so we can return
1070 * with an error with all structures restored. We
1071 * rollback the undo's in the exact reverse order that
1072 * we applied them. This guarantees that we won't run
1073 * out of space as we roll things back out.
1075 for (j = i - 1; j >= 0; j--) {
1076 if ((sops[j].sem_flg & SEM_UNDO) == 0)
1078 adjval = sops[j].sem_op;
1081 if (semundo_adjust(td, &suptr, semid,
1082 sops[j].sem_num, adjval) != 0)
1083 panic("semop - can't undo undos");
1086 for (j = 0; j < nsops; j++)
1087 semaptr->sem_base[sops[j].sem_num].semval -=
1091 printf("error = %d from semundo_adjust\n", error);
1094 } /* loop through the sops */
1095 } /* if (do_undos) */
1097 /* We're definitely done - set the sempid's and time */
1098 for (i = 0; i < nsops; i++) {
1100 semptr = &semaptr->sem_base[sopptr->sem_num];
1101 semptr->sempid = td->td_proc->p_pid;
1103 semaptr->sem_otime = time_second;
1106 * Do a wakeup if any semaphore was up'd whilst something was
1111 printf("semop: doing wakeup\n");
1113 wakeup((caddr_t)semaptr);
1115 printf("semop: back from wakeup\n");
1119 printf("semop: done\n");
1121 td->td_retval[0] = 0;
1130 * Go through the undo structures for this process and apply the adjustments to
1137 register struct sem_undo *suptr;
1138 register struct sem_undo **supptr;
1141 * Go through the chain of undo vectors looking for one
1142 * associated with this process.
1145 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1146 supptr = &suptr->un_next) {
1147 if (suptr->un_proc == p)
1155 printf("proc @%08x has undo structure with %d entries\n", p,
1160 * If there are any active undo elements then process them.
1162 if (suptr->un_cnt > 0) {
1165 for (ix = 0; ix < suptr->un_cnt; ix++) {
1166 int semid = suptr->un_ent[ix].un_id;
1167 int semnum = suptr->un_ent[ix].un_num;
1168 int adjval = suptr->un_ent[ix].un_adjval;
1169 struct semid_ds *semaptr;
1171 semaptr = &sema[semid];
1172 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1173 panic("semexit - semid not allocated");
1174 if (semnum >= semaptr->sem_nsems)
1175 panic("semexit - semnum out of range");
1178 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1179 suptr->un_proc, suptr->un_ent[ix].un_id,
1180 suptr->un_ent[ix].un_num,
1181 suptr->un_ent[ix].un_adjval,
1182 semaptr->sem_base[semnum].semval);
1186 if (semaptr->sem_base[semnum].semval < -adjval)
1187 semaptr->sem_base[semnum].semval = 0;
1189 semaptr->sem_base[semnum].semval +=
1192 semaptr->sem_base[semnum].semval += adjval;
1194 wakeup((caddr_t)semaptr);
1196 printf("semexit: back from wakeup\n");
1202 * Deallocate the undo vector.
1205 printf("removing vector\n");
1207 suptr->un_proc = NULL;
1208 *supptr = suptr->un_next;
1212 sysctl_sema(SYSCTL_HANDLER_ARGS)
1215 return (SYSCTL_OUT(req, sema,
1216 sizeof(struct semid_ds) * seminfo.semmni));