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, "", "");
162 TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
163 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
164 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
165 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
166 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
167 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
168 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
169 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
170 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
171 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
173 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
175 panic("sem is NULL");
176 sema = malloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
178 panic("sema is NULL");
179 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
181 panic("semu is NULL");
183 for (i = 0; i < seminfo.semmni; i++) {
184 sema[i].sem_base = 0;
185 sema[i].sem_perm.mode = 0;
187 for (i = 0; i < seminfo.semmnu; i++) {
188 register struct sem_undo *suptr = SEMU(i);
189 suptr->un_proc = NULL;
192 at_exit(semexit_myhook);
205 rm_at_exit(semexit_myhook);
210 sysvsem_modload(struct module *module, int cmd, void *arg)
230 static moduledata_t sysvsem_mod = {
236 SYSCALL_MODULE_HELPER(semsys);
237 SYSCALL_MODULE_HELPER(__semctl);
238 SYSCALL_MODULE_HELPER(semget);
239 SYSCALL_MODULE_HELPER(semop);
241 DECLARE_MODULE(sysvsem, sysvsem_mod,
242 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
243 MODULE_VERSION(sysvsem, 1);
246 * Entry point for all SEM calls
253 /* XXX actually varargs. */
254 struct semsys_args /* {
265 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
269 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0])) {
273 error = (*semcalls[uap->which])(td, &uap->a2);
280 * Allocate a new sem_undo structure for a process
281 * (returns ptr to structure or NULL if no more room)
284 static struct sem_undo *
289 register struct sem_undo *suptr;
290 register struct sem_undo **supptr;
294 * Try twice to allocate something.
295 * (we'll purge any empty structures after the first pass so
296 * two passes are always enough)
299 for (attempt = 0; attempt < 2; attempt++) {
301 * Look for a free structure.
302 * Fill it in and return it if we find one.
305 for (i = 0; i < seminfo.semmnu; i++) {
307 if (suptr->un_proc == NULL) {
308 suptr->un_next = semu_list;
311 suptr->un_proc = td->td_proc;
317 * We didn't find a free one, if this is the first attempt
318 * then try to free some structures.
322 /* All the structures are in use - try to free some */
323 int did_something = 0;
326 while ((suptr = *supptr) != NULL) {
327 if (suptr->un_cnt == 0) {
328 suptr->un_proc = NULL;
329 *supptr = suptr->un_next;
332 supptr = &(suptr->un_next);
335 /* If we didn't free anything then just give-up */
340 * The second pass failed even though we freed
341 * something after the first pass!
342 * This is IMPOSSIBLE!
344 panic("semu_alloc - second attempt failed");
351 * Adjust a particular entry for a particular proc
355 semundo_adjust(td, supptr, semid, semnum, adjval)
356 register struct thread *td;
357 struct sem_undo **supptr;
361 struct proc *p = td->td_proc;
362 register struct sem_undo *suptr;
363 register struct undo *sunptr;
366 /* Look for and remember the sem_undo if the caller doesn't provide
371 for (suptr = semu_list; suptr != NULL;
372 suptr = suptr->un_next) {
373 if (suptr->un_proc == p) {
381 suptr = semu_alloc(td);
389 * Look for the requested entry and adjust it (delete if adjval becomes
392 sunptr = &suptr->un_ent[0];
393 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
394 if (sunptr->un_id != semid || sunptr->un_num != semnum)
397 adjval += sunptr->un_adjval;
398 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
401 sunptr->un_adjval = adjval;
402 if (sunptr->un_adjval == 0) {
404 if (i < suptr->un_cnt)
406 suptr->un_ent[suptr->un_cnt];
411 /* Didn't find the right entry - create it */
414 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
416 if (suptr->un_cnt != seminfo.semume) {
417 sunptr = &suptr->un_ent[suptr->un_cnt];
419 sunptr->un_adjval = adjval;
420 sunptr->un_id = semid; sunptr->un_num = semnum;
427 semundo_clear(semid, semnum)
430 register struct sem_undo *suptr;
432 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
433 register struct undo *sunptr = &suptr->un_ent[0];
436 while (i < suptr->un_cnt) {
437 if (sunptr->un_id == semid) {
438 if (semnum == -1 || sunptr->un_num == semnum) {
440 if (i < suptr->un_cnt) {
442 suptr->un_ent[suptr->un_cnt];
455 * Note that the user-mode half of this passes a union, not a pointer
457 #ifndef _SYS_SYSPROTO_H_
458 struct __semctl_args {
472 register struct __semctl_args *uap;
474 int semid = uap->semid;
475 int semnum = uap->semnum;
477 union semun *arg = uap->arg;
478 union semun real_arg;
479 struct ucred *cred = td->td_proc->p_ucred;
481 struct semid_ds sbuf;
482 register struct semid_ds *semaptr;
486 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
489 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
496 if (semid < 0 || semid >= seminfo.semmsl)
498 semaptr = &sema[semid];
499 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 )
501 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
503 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
505 error = copyout((caddr_t)semaptr, real_arg.buf,
506 sizeof(struct semid_ds));
507 rval = IXSEQ_TO_IPCID(semid,semaptr->sem_perm);
509 td->td_retval[0] = rval;
513 semid = IPCID_TO_IX(semid);
514 if (semid < 0 || semid >= seminfo.semmsl) {
519 semaptr = &sema[semid];
520 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
521 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
531 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
533 semaptr->sem_perm.cuid = cred->cr_uid;
534 semaptr->sem_perm.uid = cred->cr_uid;
535 semtot -= semaptr->sem_nsems;
536 for (i = semaptr->sem_base - sem; i < semtot; i++)
537 sem[i] = sem[i + semaptr->sem_nsems];
538 for (i = 0; i < seminfo.semmni; i++) {
539 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
540 sema[i].sem_base > semaptr->sem_base)
541 sema[i].sem_base -= semaptr->sem_nsems;
543 semaptr->sem_perm.mode = 0;
544 semundo_clear(semid, -1);
545 wakeup((caddr_t)semaptr);
549 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
551 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
553 if ((error = copyin(real_arg.buf, (caddr_t)&sbuf,
554 sizeof(sbuf))) != 0) {
557 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
558 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
559 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
560 (sbuf.sem_perm.mode & 0777);
561 semaptr->sem_ctime = time_second;
565 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
567 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
569 error = copyout((caddr_t)semaptr, real_arg.buf,
570 sizeof(struct semid_ds));
574 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
576 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
580 rval = semaptr->sem_base[semnum].semncnt;
584 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
586 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
590 rval = semaptr->sem_base[semnum].sempid;
594 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
596 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
600 rval = semaptr->sem_base[semnum].semval;
604 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
606 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
608 for (i = 0; i < semaptr->sem_nsems; i++) {
609 error = copyout((caddr_t)&semaptr->sem_base[i].semval,
610 &real_arg.array[i], sizeof(real_arg.array[0]));
617 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
619 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
623 rval = semaptr->sem_base[semnum].semzcnt;
627 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
629 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
633 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
635 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
639 semaptr->sem_base[semnum].semval = real_arg.val;
640 semundo_clear(semid, semnum);
641 wakeup((caddr_t)semaptr);
645 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
647 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
649 for (i = 0; i < semaptr->sem_nsems; i++) {
650 error = copyin(&real_arg.array[i],
651 (caddr_t)&usval, sizeof(real_arg.array[0]));
654 if (usval > seminfo.semvmx) {
658 semaptr->sem_base[i].semval = usval;
660 semundo_clear(semid, -1);
661 wakeup((caddr_t)semaptr);
670 td->td_retval[0] = rval;
676 #ifndef _SYS_SYSPROTO_H_
690 register struct semget_args *uap;
692 int semid, error = 0;
694 int nsems = uap->nsems;
695 int semflg = uap->semflg;
696 struct ucred *cred = td->td_proc->p_ucred;
699 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
702 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
707 if (key != IPC_PRIVATE) {
708 for (semid = 0; semid < seminfo.semmni; semid++) {
709 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
710 sema[semid].sem_perm.key == key)
713 if (semid < seminfo.semmni) {
715 printf("found public key\n");
717 if ((error = ipcperm(td, &sema[semid].sem_perm,
721 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
723 printf("too small\n");
728 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
730 printf("not exclusive\n");
740 printf("need to allocate the semid_ds\n");
742 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
743 if (nsems <= 0 || nsems > seminfo.semmsl) {
745 printf("nsems out of range (0<%d<=%d)\n", nsems,
751 if (nsems > seminfo.semmns - semtot) {
753 printf("not enough semaphores left (need %d, got %d)\n",
754 nsems, seminfo.semmns - semtot);
759 for (semid = 0; semid < seminfo.semmni; semid++) {
760 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
763 if (semid == seminfo.semmni) {
765 printf("no more semid_ds's available\n");
771 printf("semid %d is available\n", semid);
773 sema[semid].sem_perm.key = key;
774 sema[semid].sem_perm.cuid = cred->cr_uid;
775 sema[semid].sem_perm.uid = cred->cr_uid;
776 sema[semid].sem_perm.cgid = cred->cr_gid;
777 sema[semid].sem_perm.gid = cred->cr_gid;
778 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
779 sema[semid].sem_perm.seq =
780 (sema[semid].sem_perm.seq + 1) & 0x7fff;
781 sema[semid].sem_nsems = nsems;
782 sema[semid].sem_otime = 0;
783 sema[semid].sem_ctime = time_second;
784 sema[semid].sem_base = &sem[semtot];
786 bzero(sema[semid].sem_base,
787 sizeof(sema[semid].sem_base[0])*nsems);
789 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
794 printf("didn't find it and wasn't asked to create it\n");
801 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
807 #ifndef _SYS_SYSPROTO_H_
821 register struct semop_args *uap;
823 int semid = uap->semid;
824 u_int nsops = uap->nsops;
825 struct sembuf *sops = NULL;
826 register struct semid_ds *semaptr;
827 register struct sembuf *sopptr;
828 register struct sem *semptr;
829 struct sem_undo *suptr;
831 int do_wakeup, do_undos;
834 printf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
838 if (!jail_sysvipc_allowed && jailed(td->td_proc->p_ucred)) {
843 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
845 if (semid < 0 || semid >= seminfo.semmsl) {
850 semaptr = &sema[semid];
851 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
855 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
859 if (nsops > seminfo.semopm) {
861 printf("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
868 /* Allocate memory for sem_ops */
869 sops = malloc(nsops * sizeof(sops[0]), M_SEM, M_WAITOK);
871 panic("Failed to allocate %d sem_ops", nsops);
873 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
875 printf("error = %d from copyin(%08x, %08x, %d)\n", error,
876 uap->sops, sops, nsops * sizeof(sops[0]));
882 * Initial pass thru sops to see what permissions are needed.
883 * Also perform any checks that don't need repeating on each
884 * attempt to satisfy the request vector.
886 j = 0; /* permission needed */
888 for (i = 0; i < nsops; i++) {
890 if (sopptr->sem_num >= semaptr->sem_nsems) {
894 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
896 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
899 if ((error = ipcperm(td, &semaptr->sem_perm, j))) {
901 printf("error = %d from ipaccess\n", error);
907 * Loop trying to satisfy the vector of requests.
908 * If we reach a point where we must wait, any requests already
909 * performed are rolled back and we go to sleep until some other
910 * process wakes us up. At this point, we start all over again.
912 * This ensures that from the perspective of other tasks, a set
913 * of requests is atomic (never partially satisfied).
917 error = 0; /* error return if necessary */
919 for (i = 0; i < nsops; i++) {
921 semptr = &semaptr->sem_base[sopptr->sem_num];
924 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
925 semaptr, semaptr->sem_base, semptr,
926 sopptr->sem_num, semptr->semval, sopptr->sem_op,
927 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
930 if (sopptr->sem_op < 0) {
931 if (semptr->semval + sopptr->sem_op < 0) {
933 printf("semop: can't do it now\n");
937 semptr->semval += sopptr->sem_op;
938 if (semptr->semval == 0 &&
942 } else if (sopptr->sem_op == 0) {
943 if (semptr->semval != 0) {
945 printf("semop: not zero now\n");
949 } else if (semptr->semval + sopptr->sem_op >
954 if (semptr->semncnt > 0)
956 semptr->semval += sopptr->sem_op;
961 * Did we get through the entire vector?
967 * No ... rollback anything that we've already done
970 printf("semop: rollback 0 through %d\n", i-1);
972 for (j = 0; j < i; j++)
973 semaptr->sem_base[sops[j].sem_num].semval -=
976 /* If we detected an error, return it */
981 * If the request that we couldn't satisfy has the
982 * NOWAIT flag set then return with EAGAIN.
984 if (sopptr->sem_flg & IPC_NOWAIT) {
989 if (sopptr->sem_op == 0)
995 printf("semop: good night!\n");
997 error = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
1000 printf("semop: good morning (error=%d)!\n", error);
1008 printf("semop: good morning!\n");
1012 * Make sure that the semaphore still exists
1014 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1015 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1021 * The semaphore is still alive. Readjust the count of
1022 * waiting processes.
1024 if (sopptr->sem_op == 0)
1032 * Process any SEM_UNDO requests.
1036 for (i = 0; i < nsops; i++) {
1038 * We only need to deal with SEM_UNDO's for non-zero
1043 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1045 adjval = sops[i].sem_op;
1048 error = semundo_adjust(td, &suptr, semid,
1049 sops[i].sem_num, -adjval);
1054 * Oh-Oh! We ran out of either sem_undo's or undo's.
1055 * Rollback the adjustments to this point and then
1056 * rollback the semaphore ups and down so we can return
1057 * with an error with all structures restored. We
1058 * rollback the undo's in the exact reverse order that
1059 * we applied them. This guarantees that we won't run
1060 * out of space as we roll things back out.
1062 for (j = i - 1; j >= 0; j--) {
1063 if ((sops[j].sem_flg & SEM_UNDO) == 0)
1065 adjval = sops[j].sem_op;
1068 if (semundo_adjust(td, &suptr, semid,
1069 sops[j].sem_num, adjval) != 0)
1070 panic("semop - can't undo undos");
1073 for (j = 0; j < nsops; j++)
1074 semaptr->sem_base[sops[j].sem_num].semval -=
1078 printf("error = %d from semundo_adjust\n", error);
1081 } /* loop through the sops */
1082 } /* if (do_undos) */
1084 /* We're definitely done - set the sempid's and time */
1085 for (i = 0; i < nsops; i++) {
1087 semptr = &semaptr->sem_base[sopptr->sem_num];
1088 semptr->sempid = td->td_proc->p_pid;
1090 semaptr->sem_otime = time_second;
1093 * Do a wakeup if any semaphore was up'd whilst something was
1098 printf("semop: doing wakeup\n");
1100 wakeup((caddr_t)semaptr);
1102 printf("semop: back from wakeup\n");
1106 printf("semop: done\n");
1108 td->td_retval[0] = 0;
1117 * Go through the undo structures for this process and apply the adjustments to
1124 register struct sem_undo *suptr;
1125 register struct sem_undo **supptr;
1128 * Go through the chain of undo vectors looking for one
1129 * associated with this process.
1132 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1133 supptr = &suptr->un_next) {
1134 if (suptr->un_proc == p)
1142 printf("proc @%08x has undo structure with %d entries\n", p,
1147 * If there are any active undo elements then process them.
1149 if (suptr->un_cnt > 0) {
1152 for (ix = 0; ix < suptr->un_cnt; ix++) {
1153 int semid = suptr->un_ent[ix].un_id;
1154 int semnum = suptr->un_ent[ix].un_num;
1155 int adjval = suptr->un_ent[ix].un_adjval;
1156 struct semid_ds *semaptr;
1158 semaptr = &sema[semid];
1159 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1160 panic("semexit - semid not allocated");
1161 if (semnum >= semaptr->sem_nsems)
1162 panic("semexit - semnum out of range");
1165 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1166 suptr->un_proc, suptr->un_ent[ix].un_id,
1167 suptr->un_ent[ix].un_num,
1168 suptr->un_ent[ix].un_adjval,
1169 semaptr->sem_base[semnum].semval);
1173 if (semaptr->sem_base[semnum].semval < -adjval)
1174 semaptr->sem_base[semnum].semval = 0;
1176 semaptr->sem_base[semnum].semval +=
1179 semaptr->sem_base[semnum].semval += adjval;
1181 wakeup((caddr_t)semaptr);
1183 printf("semexit: back from wakeup\n");
1189 * Deallocate the undo vector.
1192 printf("removing vector\n");
1194 suptr->un_proc = NULL;
1195 *supptr = suptr->un_next;
1199 sysctl_sema(SYSCTL_HANDLER_ARGS)
1202 return (SYSCTL_OUT(req, sema,
1203 sizeof(struct semid_ds) * seminfo.semmni));