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(void);
30 static int sysvsem_modload(struct module *, int, void *);
31 static int semunload(void);
32 static void semexit_myhook(struct proc *p);
33 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
35 #ifndef _SYS_SYSPROTO_H_
37 int __semctl(struct thread *td, struct __semctl_args *uap);
39 int semget(struct thread *td, struct semget_args *uap);
41 int semop(struct thread *td, struct semop_args *uap);
44 static struct sem_undo *semu_alloc(struct thread *td);
45 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
46 int semid, int semnum, int adjval);
47 static void semundo_clear(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 /* {
264 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
266 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
269 error = (*semcalls[uap->which])(td, &uap->a2);
275 * Allocate a new sem_undo structure for a process
276 * (returns ptr to structure or NULL if no more room)
279 static struct sem_undo *
284 register struct sem_undo *suptr;
285 register struct sem_undo **supptr;
289 * Try twice to allocate something.
290 * (we'll purge any empty structures after the first pass so
291 * two passes are always enough)
294 for (attempt = 0; attempt < 2; attempt++) {
296 * Look for a free structure.
297 * Fill it in and return it if we find one.
300 for (i = 0; i < seminfo.semmnu; i++) {
302 if (suptr->un_proc == NULL) {
303 suptr->un_next = semu_list;
306 suptr->un_proc = td->td_proc;
312 * We didn't find a free one, if this is the first attempt
313 * then try to free some structures.
317 /* All the structures are in use - try to free some */
318 int did_something = 0;
321 while ((suptr = *supptr) != NULL) {
322 if (suptr->un_cnt == 0) {
323 suptr->un_proc = NULL;
324 *supptr = suptr->un_next;
327 supptr = &(suptr->un_next);
330 /* If we didn't free anything then just give-up */
335 * The second pass failed even though we freed
336 * something after the first pass!
337 * This is IMPOSSIBLE!
339 panic("semu_alloc - second attempt failed");
346 * Adjust a particular entry for a particular proc
350 semundo_adjust(td, supptr, semid, semnum, adjval)
351 register struct thread *td;
352 struct sem_undo **supptr;
356 struct proc *p = td->td_proc;
357 register struct sem_undo *suptr;
358 register struct undo *sunptr;
361 /* Look for and remember the sem_undo if the caller doesn't provide
366 for (suptr = semu_list; suptr != NULL;
367 suptr = suptr->un_next) {
368 if (suptr->un_proc == p) {
376 suptr = semu_alloc(td);
384 * Look for the requested entry and adjust it (delete if adjval becomes
387 sunptr = &suptr->un_ent[0];
388 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
389 if (sunptr->un_id != semid || sunptr->un_num != semnum)
392 adjval += sunptr->un_adjval;
393 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
396 sunptr->un_adjval = adjval;
397 if (sunptr->un_adjval == 0) {
399 if (i < suptr->un_cnt)
401 suptr->un_ent[suptr->un_cnt];
406 /* Didn't find the right entry - create it */
409 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
411 if (suptr->un_cnt != seminfo.semume) {
412 sunptr = &suptr->un_ent[suptr->un_cnt];
414 sunptr->un_adjval = adjval;
415 sunptr->un_id = semid; sunptr->un_num = semnum;
422 semundo_clear(semid, semnum)
425 register struct sem_undo *suptr;
427 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
428 register struct undo *sunptr = &suptr->un_ent[0];
431 while (i < suptr->un_cnt) {
432 if (sunptr->un_id == semid) {
433 if (semnum == -1 || sunptr->un_num == semnum) {
435 if (i < suptr->un_cnt) {
437 suptr->un_ent[suptr->un_cnt];
450 * Note that the user-mode half of this passes a union, not a pointer
452 #ifndef _SYS_SYSPROTO_H_
453 struct __semctl_args {
467 register struct __semctl_args *uap;
469 int semid = uap->semid;
470 int semnum = uap->semnum;
472 union semun *arg = uap->arg;
473 union semun real_arg;
474 struct ucred *cred = td->td_ucred;
476 struct semid_ds sbuf;
477 register struct semid_ds *semaptr;
481 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
483 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
489 if (semid < 0 || semid >= seminfo.semmni)
491 semaptr = &sema[semid];
492 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 )
494 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
496 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
498 error = copyout((caddr_t)semaptr, real_arg.buf,
499 sizeof(struct semid_ds));
500 rval = IXSEQ_TO_IPCID(semid,semaptr->sem_perm);
502 td->td_retval[0] = rval;
506 semid = IPCID_TO_IX(semid);
507 if (semid < 0 || semid >= seminfo.semmni) {
512 semaptr = &sema[semid];
513 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
514 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
524 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
526 semaptr->sem_perm.cuid = cred->cr_uid;
527 semaptr->sem_perm.uid = cred->cr_uid;
528 semtot -= semaptr->sem_nsems;
529 for (i = semaptr->sem_base - sem; i < semtot; i++)
530 sem[i] = sem[i + semaptr->sem_nsems];
531 for (i = 0; i < seminfo.semmni; i++) {
532 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
533 sema[i].sem_base > semaptr->sem_base)
534 sema[i].sem_base -= semaptr->sem_nsems;
536 semaptr->sem_perm.mode = 0;
537 semundo_clear(semid, -1);
538 wakeup((caddr_t)semaptr);
542 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_M)))
544 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
546 if ((error = copyin(real_arg.buf, (caddr_t)&sbuf,
547 sizeof(sbuf))) != 0) {
550 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
551 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
552 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
553 (sbuf.sem_perm.mode & 0777);
554 semaptr->sem_ctime = time_second;
558 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
560 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
562 error = copyout((caddr_t)semaptr, real_arg.buf,
563 sizeof(struct semid_ds));
567 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
569 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
573 rval = semaptr->sem_base[semnum].semncnt;
577 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
579 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
583 rval = semaptr->sem_base[semnum].sempid;
587 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
589 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
593 rval = semaptr->sem_base[semnum].semval;
597 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
599 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
601 for (i = 0; i < semaptr->sem_nsems; i++) {
602 error = copyout((caddr_t)&semaptr->sem_base[i].semval,
603 &real_arg.array[i], sizeof(real_arg.array[0]));
610 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_R)))
612 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
616 rval = semaptr->sem_base[semnum].semzcnt;
620 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
622 if (semnum < 0 || semnum >= semaptr->sem_nsems) {
626 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
628 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
632 semaptr->sem_base[semnum].semval = real_arg.val;
633 semundo_clear(semid, semnum);
634 wakeup((caddr_t)semaptr);
638 if ((error = ipcperm(td, &semaptr->sem_perm, IPC_W)))
640 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
642 for (i = 0; i < semaptr->sem_nsems; i++) {
643 error = copyin(&real_arg.array[i],
644 (caddr_t)&usval, sizeof(real_arg.array[0]));
647 if (usval > seminfo.semvmx) {
651 semaptr->sem_base[i].semval = usval;
653 semundo_clear(semid, -1);
654 wakeup((caddr_t)semaptr);
663 td->td_retval[0] = rval;
669 #ifndef _SYS_SYSPROTO_H_
683 register struct semget_args *uap;
685 int semid, error = 0;
687 int nsems = uap->nsems;
688 int semflg = uap->semflg;
689 struct ucred *cred = td->td_ucred;
692 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
694 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
698 if (key != IPC_PRIVATE) {
699 for (semid = 0; semid < seminfo.semmni; semid++) {
700 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
701 sema[semid].sem_perm.key == key)
704 if (semid < seminfo.semmni) {
706 printf("found public key\n");
708 if ((error = ipcperm(td, &sema[semid].sem_perm,
712 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
714 printf("too small\n");
719 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
721 printf("not exclusive\n");
731 printf("need to allocate the semid_ds\n");
733 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
734 if (nsems <= 0 || nsems > seminfo.semmsl) {
736 printf("nsems out of range (0<%d<=%d)\n", nsems,
742 if (nsems > seminfo.semmns - semtot) {
744 printf("not enough semaphores left (need %d, got %d)\n",
745 nsems, seminfo.semmns - semtot);
750 for (semid = 0; semid < seminfo.semmni; semid++) {
751 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
754 if (semid == seminfo.semmni) {
756 printf("no more semid_ds's available\n");
762 printf("semid %d is available\n", semid);
764 sema[semid].sem_perm.key = key;
765 sema[semid].sem_perm.cuid = cred->cr_uid;
766 sema[semid].sem_perm.uid = cred->cr_uid;
767 sema[semid].sem_perm.cgid = cred->cr_gid;
768 sema[semid].sem_perm.gid = cred->cr_gid;
769 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
770 sema[semid].sem_perm.seq =
771 (sema[semid].sem_perm.seq + 1) & 0x7fff;
772 sema[semid].sem_nsems = nsems;
773 sema[semid].sem_otime = 0;
774 sema[semid].sem_ctime = time_second;
775 sema[semid].sem_base = &sem[semtot];
777 bzero(sema[semid].sem_base,
778 sizeof(sema[semid].sem_base[0])*nsems);
780 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
785 printf("didn't find it and wasn't asked to create it\n");
792 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
798 #ifndef _SYS_SYSPROTO_H_
812 register struct semop_args *uap;
814 int semid = uap->semid;
815 u_int nsops = uap->nsops;
816 struct sembuf *sops = NULL;
817 register struct semid_ds *semaptr;
818 register struct sembuf *sopptr = 0;
819 register struct sem *semptr = 0;
820 struct sem_undo *suptr;
822 int do_wakeup, do_undos;
825 printf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
828 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
832 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
834 if (semid < 0 || semid >= seminfo.semmni) {
839 semaptr = &sema[semid];
840 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0) {
844 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
848 if (nsops > seminfo.semopm) {
850 printf("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
857 /* Allocate memory for sem_ops */
858 sops = malloc(nsops * sizeof(sops[0]), M_SEM, M_WAITOK);
860 panic("Failed to allocate %d sem_ops", nsops);
862 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
864 printf("error = %d from copyin(%08x, %08x, %d)\n", error,
865 uap->sops, sops, nsops * sizeof(sops[0]));
871 * Initial pass thru sops to see what permissions are needed.
872 * Also perform any checks that don't need repeating on each
873 * attempt to satisfy the request vector.
875 j = 0; /* permission needed */
877 for (i = 0; i < nsops; i++) {
879 if (sopptr->sem_num >= semaptr->sem_nsems) {
883 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
885 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
888 if ((error = ipcperm(td, &semaptr->sem_perm, j))) {
890 printf("error = %d from ipaccess\n", error);
896 * Loop trying to satisfy the vector of requests.
897 * If we reach a point where we must wait, any requests already
898 * performed are rolled back and we go to sleep until some other
899 * process wakes us up. At this point, we start all over again.
901 * This ensures that from the perspective of other tasks, a set
902 * of requests is atomic (never partially satisfied).
906 error = 0; /* error return if necessary */
908 for (i = 0; i < nsops; i++) {
910 semptr = &semaptr->sem_base[sopptr->sem_num];
913 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
914 semaptr, semaptr->sem_base, semptr,
915 sopptr->sem_num, semptr->semval, sopptr->sem_op,
916 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
919 if (sopptr->sem_op < 0) {
920 if (semptr->semval + sopptr->sem_op < 0) {
922 printf("semop: can't do it now\n");
926 semptr->semval += sopptr->sem_op;
927 if (semptr->semval == 0 &&
931 } else if (sopptr->sem_op == 0) {
932 if (semptr->semval != 0) {
934 printf("semop: not zero now\n");
938 } else if (semptr->semval + sopptr->sem_op >
943 if (semptr->semncnt > 0)
945 semptr->semval += sopptr->sem_op;
950 * Did we get through the entire vector?
956 * No ... rollback anything that we've already done
959 printf("semop: rollback 0 through %d\n", i-1);
961 for (j = 0; j < i; j++)
962 semaptr->sem_base[sops[j].sem_num].semval -=
965 /* If we detected an error, return it */
970 * If the request that we couldn't satisfy has the
971 * NOWAIT flag set then return with EAGAIN.
973 if (sopptr->sem_flg & IPC_NOWAIT) {
978 if (sopptr->sem_op == 0)
984 printf("semop: good night!\n");
986 error = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
989 printf("semop: good morning (error=%d)!\n", error);
997 printf("semop: good morning!\n");
1001 * Make sure that the semaphore still exists
1003 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
1004 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1010 * The semaphore is still alive. Readjust the count of
1011 * waiting processes.
1013 if (sopptr->sem_op == 0)
1021 * Process any SEM_UNDO requests.
1025 for (i = 0; i < nsops; i++) {
1027 * We only need to deal with SEM_UNDO's for non-zero
1032 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1034 adjval = sops[i].sem_op;
1037 error = semundo_adjust(td, &suptr, semid,
1038 sops[i].sem_num, -adjval);
1043 * Oh-Oh! We ran out of either sem_undo's or undo's.
1044 * Rollback the adjustments to this point and then
1045 * rollback the semaphore ups and down so we can return
1046 * with an error with all structures restored. We
1047 * rollback the undo's in the exact reverse order that
1048 * we applied them. This guarantees that we won't run
1049 * out of space as we roll things back out.
1051 for (j = i - 1; j >= 0; j--) {
1052 if ((sops[j].sem_flg & SEM_UNDO) == 0)
1054 adjval = sops[j].sem_op;
1057 if (semundo_adjust(td, &suptr, semid,
1058 sops[j].sem_num, adjval) != 0)
1059 panic("semop - can't undo undos");
1062 for (j = 0; j < nsops; j++)
1063 semaptr->sem_base[sops[j].sem_num].semval -=
1067 printf("error = %d from semundo_adjust\n", error);
1070 } /* loop through the sops */
1071 } /* if (do_undos) */
1073 /* We're definitely done - set the sempid's and time */
1074 for (i = 0; i < nsops; i++) {
1076 semptr = &semaptr->sem_base[sopptr->sem_num];
1077 semptr->sempid = td->td_proc->p_pid;
1079 semaptr->sem_otime = time_second;
1082 * Do a wakeup if any semaphore was up'd whilst something was
1087 printf("semop: doing wakeup\n");
1089 wakeup((caddr_t)semaptr);
1091 printf("semop: back from wakeup\n");
1095 printf("semop: done\n");
1097 td->td_retval[0] = 0;
1106 * Go through the undo structures for this process and apply the adjustments to
1113 register struct sem_undo *suptr;
1114 register struct sem_undo **supptr;
1117 * Go through the chain of undo vectors looking for one
1118 * associated with this process.
1121 for (supptr = &semu_list; (suptr = *supptr) != NULL;
1122 supptr = &suptr->un_next) {
1123 if (suptr->un_proc == p)
1131 printf("proc @%08x has undo structure with %d entries\n", p,
1136 * If there are any active undo elements then process them.
1138 if (suptr->un_cnt > 0) {
1141 for (ix = 0; ix < suptr->un_cnt; ix++) {
1142 int semid = suptr->un_ent[ix].un_id;
1143 int semnum = suptr->un_ent[ix].un_num;
1144 int adjval = suptr->un_ent[ix].un_adjval;
1145 struct semid_ds *semaptr;
1147 semaptr = &sema[semid];
1148 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1149 panic("semexit - semid not allocated");
1150 if (semnum >= semaptr->sem_nsems)
1151 panic("semexit - semnum out of range");
1154 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1155 suptr->un_proc, suptr->un_ent[ix].un_id,
1156 suptr->un_ent[ix].un_num,
1157 suptr->un_ent[ix].un_adjval,
1158 semaptr->sem_base[semnum].semval);
1162 if (semaptr->sem_base[semnum].semval < -adjval)
1163 semaptr->sem_base[semnum].semval = 0;
1165 semaptr->sem_base[semnum].semval +=
1168 semaptr->sem_base[semnum].semval += adjval;
1170 wakeup((caddr_t)semaptr);
1172 printf("semexit: back from wakeup\n");
1178 * Deallocate the undo vector.
1181 printf("removing vector\n");
1183 suptr->un_proc = NULL;
1184 *supptr = suptr->un_next;
1188 sysctl_sema(SYSCTL_HANDLER_ARGS)
1191 return (SYSCTL_OUT(req, sema,
1192 sizeof(struct semid_ds) * seminfo.semmni));