2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Implementation of SVID semaphores
25 * Author: Daniel Boulet
27 * This software is provided ``AS IS'' without any warranties of any kind.
30 * John Bellardo modified the implementation for Darwin. 12/2000
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <mach/mach_types.h>
41 #include <sys/filedesc.h>
44 /*#include <sys/sysproto.h>*/
45 /*#include <sys/sysent.h>*/
47 /* Uncomment this line to see the debugging output */
48 /* #define SEM_DEBUG */
50 /* Macros to deal with the semaphore subsystem lock. The lock currently uses
51 * the semlock_holder static variable as a mutex. NULL means no lock, any
52 * value other than NULL means locked. semlock_holder is used because it was
53 * present in the code before the Darwin port, and for no other reason.
54 * When the time comes to relax the funnel requirements of the kernel only
55 * these macros should need to be changed. A spin lock would work well.
58 #define SUBSYSTEM_LOCK_AQUIRE(p) { sysv_sem_aquiring_threads++; \
59 while (semlock_holder != NULL) \
60 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "sysvsem", 0); \
62 sysv_sem_aquiring_threads--; }
64 /* Release the lock */
65 #define SUBSYSTEM_LOCK_RELEASE { semlock_holder = NULL; wakeup((caddr_t)&semlock_holder); }
67 /* Release the lock and return a value */
68 #define UNLOCK_AND_RETURN(ret) { SUBSYSTEM_LOCK_RELEASE; return(ret); }
70 #define M_SYSVSEM M_SUBPROC
73 static void seminit
__P((void *));
74 SYSINIT(sysv_sem
, SI_SUB_SYSV_SEM
, SI_ORDER_FIRST
, seminit
, NULL
)
77 /* Hard system limits to avoid resource starvation / DOS attacks.
78 * These are not needed if we can make the semaphore pages swappable.
80 static struct seminfo limitseminfo
= {
81 SEMMAP
, /* # of entries in semaphore map */
82 SEMMNI
, /* # of semaphore identifiers */
83 SEMMNS
, /* # of semaphores in system */
84 SEMMNU
, /* # of undo structures in system */
85 SEMMSL
, /* max # of semaphores per id */
86 SEMOPM
, /* max # of operations per semop call */
87 SEMUME
, /* max # of undo entries per process */
88 SEMUSZ
, /* size in bytes of undo structure */
89 SEMVMX
, /* semaphore maximum value */
90 SEMAEM
/* adjust on exit max value */
93 /* Current system allocations. We use this structure to track how many
94 * resources we have allocated so far. This way we can set large hard limits
95 * and not allocate the memory for them up front.
97 struct seminfo seminfo
= {
98 SEMMAP
, /* Unused, # of entries in semaphore map */
99 0, /* # of semaphore identifiers */
100 0, /* # of semaphores in system */
101 0, /* # of undo entries in system */
102 SEMMSL
, /* max # of semaphores per id */
103 SEMOPM
, /* max # of operations per semop call */
104 SEMUME
, /* max # of undo entries per process */
105 SEMUSZ
, /* size in bytes of undo structure */
106 SEMVMX
, /* semaphore maximum value */
107 SEMAEM
/* adjust on exit max value */
110 /* A counter so the module unload code knows when there are no more processes using
111 * the sysv_sem code */
112 static long sysv_sem_sleeping_threads
= 0;
113 static long sysv_sem_aquiring_threads
= 0;
116 int semctl
__P((struct proc
*p
, struct semctl_args
*uap
, int *));
118 int semget
__P((struct proc
*p
, struct semget_args
*uap
, int *));
120 int semop
__P((struct proc
*p
, struct semop_args
*uap
, int *));
121 struct semconfig_args
;
122 int semconfig
__P((struct proc
*p
, struct semconfig_args
*uap
, int *));
125 static struct sem_undo
*semu_alloc
__P((struct proc
*p
));
126 static int semundo_adjust
__P((struct proc
*p
, struct sem_undo
**supptr
,
127 int semid
, int semnum
, int adjval
));
128 static void semundo_clear
__P((int semid
, int semnum
));
130 typedef int sy_call_t
__P((struct proc
*, void *, int *));
132 /* XXX casting to (sy_call_t *) is bogus, as usual. */
133 static sy_call_t
*semcalls
[] = {
134 (sy_call_t
*)semctl
, (sy_call_t
*)semget
,
135 (sy_call_t
*)semop
, (sy_call_t
*)semconfig
138 static int semtot
= 0; /* # of used semaphores */
139 struct semid_ds
*sema
= NULL
; /* semaphore id pool */
140 struct sem
*sem
= NULL
; /* semaphore pool */
141 static struct sem_undo
*semu_list
= NULL
; /* list of active undo structures */
142 struct sem_undo
*semu
= NULL
; /* semaphore undo pool */
144 static struct proc
*semlock_holder
= NULL
;
146 /* seminit no longer needed. The data structures are grown dynamically */
153 * Entry point for all SEM calls
155 * In Darwin this is no longer the entry point. It will be removed after
156 * the code has been tested better.
166 semsys(p
, uap
, retval
)
168 /* XXX actually varargs. */
169 struct semsys_args
*uap
;
173 /* The individual calls handling the locking now */
174 /*while (semlock_holder != NULL && semlock_holder != p)
175 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semsys", 0);
178 if (uap
->which
>= sizeof(semcalls
)/sizeof(semcalls
[0]))
180 return ((*semcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
184 * Lock or unlock the entire semaphore facility.
186 * This will probably eventually evolve into a general purpose semaphore
187 * facility status enquiry mechanism (I don't like the "read /dev/kmem"
188 * approach currently taken by ipcs and the amount of info that we want
189 * to be able to extract for ipcs is probably beyond what the capability
190 * of the getkerninfo facility.
192 * At the time that the current version of semconfig was written, ipcs is
193 * the only user of the semconfig facility. It uses it to ensure that the
194 * semaphore facility data structures remain static while it fishes around
198 #ifndef _SYS_SYSPROTO_H_
199 struct semconfig_args
{
200 semconfig_ctl_t flag
;
205 semconfig(p
, uap
, retval
)
207 struct semconfig_args
*uap
;
213 case SEM_CONFIG_FREEZE
:
214 SUBSYSTEM_LOCK_AQUIRE(p
);
217 case SEM_CONFIG_THAW
:
218 SUBSYSTEM_LOCK_RELEASE
;
222 printf("semconfig: unknown flag parameter value (%d) - ignored\n",
232 /* Expand the semu array to the given capacity. If the expansion fails
233 * return 0, otherwise return 1.
235 * Assumes we already have the subsystem lock.
238 grow_semu_array(newSize
)
242 register struct sem_undo
*newSemu
;
243 if (newSize
<= seminfo
.semmnu
)
245 if (newSize
> limitseminfo
.semmnu
) /* enforce hard limit */
248 printf("undo structure hard limit of %d reached, requested %d\n",
249 limitseminfo
.semmnu
, newSize
);
253 newSize
= (newSize
/SEMMNU_INC
+ 1) * SEMMNU_INC
;
254 newSize
= newSize
> limitseminfo
.semmnu
? limitseminfo
.semmnu
: newSize
;
257 printf("growing semu[] from %d to %d\n", seminfo
.semmnu
, newSize
);
259 MALLOC(newSemu
, struct sem_undo
*, sizeof(struct sem_undo
)*newSize
,
260 M_SYSVSEM
, M_WAITOK
);
264 printf("allocation failed. no changes made.\n");
269 /* Initialize our structure. */
270 for (i
= 0; i
< seminfo
.semmnu
; i
++)
272 newSemu
[i
] = semu
[i
];
273 for(j
= 0; j
< SEMUME
; j
++) /* Is this really needed? */
274 newSemu
[i
].un_ent
[j
] = semu
[i
].un_ent
[j
];
276 for (i
= seminfo
.semmnu
; i
< newSize
; i
++)
278 newSemu
[i
].un_proc
= NULL
;
281 /* Clean up the old array */
283 FREE(semu
, M_SYSVSEM
);
286 seminfo
.semmnu
= newSize
;
288 printf("expansion successful\n");
294 * Expand the sema array to the given capacity. If the expansion fails
295 * we return 0, otherwise we return 1.
297 * Assumes we already have the subsystem lock.
300 grow_sema_array(newSize
)
303 register struct semid_ds
*newSema
;
306 if (newSize
<= seminfo
.semmni
)
308 if (newSize
> limitseminfo
.semmni
) /* enforce hard limit */
311 printf("identifier hard limit of %d reached, requested %d\n",
312 limitseminfo
.semmni
, newSize
);
316 newSize
= (newSize
/SEMMNI_INC
+ 1) * SEMMNI_INC
;
317 newSize
= newSize
> limitseminfo
.semmni
? limitseminfo
.semmni
: newSize
;
320 printf("growing sema[] from %d to %d\n", seminfo
.semmni
, newSize
);
322 MALLOC(newSema
, struct semid_ds
*, sizeof(struct semid_ds
)*newSize
,
323 M_SYSVSEM
, M_WAITOK
);
327 printf("allocation failed. no changes made.\n");
332 /* Initialize our new ids, and copy over the old ones */
333 for (i
= 0; i
< seminfo
.semmni
; i
++)
335 newSema
[i
] = sema
[i
];
336 /* This is a hack. What we really want to be able to
337 * do is change the value a process is waiting on
338 * without waking it up, but I don't know how to do
339 * this with the existing code, so we wake up the
340 * process and let it do a lot of work to determine the
341 * semaphore set is really not available yet, and then
342 * sleep on the correct, reallocated semid_ds pointer.
344 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
)
345 wakeup((caddr_t
)&sema
[i
]);
348 for (i
= seminfo
.semmni
; i
< newSize
; i
++)
350 newSema
[i
].sem_base
= 0;
351 newSema
[i
].sem_perm
.mode
= 0;
354 /* Clean up the old array */
356 FREE(sema
, M_SYSVSEM
);
359 seminfo
.semmni
= newSize
;
361 printf("expansion successful\n");
367 * Expand the sem array to the given capacity. If the expansion fails
368 * we return 0 (fail), otherwise we return 1 (success).
370 * Assumes we already hold the subsystem lock.
373 grow_sem_array(newSize
)
376 register struct sem
*newSem
= NULL
;
379 if (newSize
< semtot
)
381 if (newSize
> limitseminfo
.semmns
) /* enforce hard limit */
384 printf("semaphore hard limit of %d reached, requested %d\n",
385 limitseminfo
.semmns
, newSize
);
389 newSize
= (newSize
/SEMMNS_INC
+ 1) * SEMMNS_INC
;
390 newSize
= newSize
> limitseminfo
.semmns
? limitseminfo
.semmns
: newSize
;
393 printf("growing sem array from %d to %d\n", seminfo
.semmns
, newSize
);
395 MALLOC(newSem
, struct sem
*, sizeof(struct sem
)*newSize
,
396 M_SYSVSEM
, M_WAITOK
);
400 printf("allocation failed. no changes made.\n");
405 /* We have our new memory, now copy the old contents over */
407 for(i
= 0; i
< seminfo
.semmns
; i
++)
410 /* Update our id structures to point to the new semaphores */
411 for(i
= 0; i
< seminfo
.semmni
; i
++)
412 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
) /* ID in use */
415 sema
[i
].sem_base
+= newSem
- sem
;
417 sema
[i
].sem_base
-= sem
- newSem
;
420 /* clean up the old array */
422 FREE(sem
, M_SYSVSEM
);
425 seminfo
.semmns
= newSize
;
427 printf("expansion complete\n");
433 * Allocate a new sem_undo structure for a process
434 * (returns ptr to structure or NULL if no more room)
436 * Assumes we already hold the subsystem lock.
439 static struct sem_undo
*
444 register struct sem_undo
*suptr
;
445 register struct sem_undo
**supptr
;
449 * Try twice to allocate something.
450 * (we'll purge any empty structures after the first pass so
451 * two passes are always enough)
454 for (attempt
= 0; attempt
< 2; attempt
++) {
456 * Look for a free structure.
457 * Fill it in and return it if we find one.
460 for (i
= 0; i
< seminfo
.semmnu
; i
++) {
462 if (suptr
->un_proc
== NULL
) {
463 suptr
->un_next
= semu_list
;
472 * We didn't find a free one, if this is the first attempt
473 * then try to free some structures.
477 /* All the structures are in use - try to free some */
478 int did_something
= 0;
481 while ((suptr
= *supptr
) != NULL
) {
482 if (suptr
->un_cnt
== 0) {
483 suptr
->un_proc
= NULL
;
484 *supptr
= suptr
->un_next
;
487 supptr
= &(suptr
->un_next
);
490 /* If we didn't free anything. Try expanding
491 * the semu[] array. If that doesn't work
492 * then fail. We expand last to get the
493 * most reuse out of existing resources.
496 if (!grow_semu_array(seminfo
.semmnu
+ 1))
500 * The second pass failed even though we freed
501 * something after the first pass!
502 * This is IMPOSSIBLE!
504 panic("semu_alloc - second attempt failed");
511 * Adjust a particular entry for a particular proc
513 * Assumes we already hold the subsystem lock.
517 semundo_adjust(p
, supptr
, semid
, semnum
, adjval
)
518 register struct proc
*p
;
519 struct sem_undo
**supptr
;
523 register struct sem_undo
*suptr
;
524 register struct undo
*sunptr
;
527 /* Look for and remember the sem_undo if the caller doesn't provide
532 for (suptr
= semu_list
; suptr
!= NULL
;
533 suptr
= suptr
->un_next
) {
534 if (suptr
->un_proc
== p
) {
542 suptr
= semu_alloc(p
);
550 * Look for the requested entry and adjust it (delete if adjval becomes
553 sunptr
= &suptr
->un_ent
[0];
554 for (i
= 0; i
< suptr
->un_cnt
; i
++, sunptr
++) {
555 if (sunptr
->un_id
!= semid
|| sunptr
->un_num
!= semnum
)
558 sunptr
->un_adjval
= 0;
560 sunptr
->un_adjval
+= adjval
;
561 if (sunptr
->un_adjval
== 0) {
563 if (i
< suptr
->un_cnt
)
565 suptr
->un_ent
[suptr
->un_cnt
];
570 /* Didn't find the right entry - create it */
573 if (suptr
->un_cnt
!= seminfo
.semume
) {
574 sunptr
= &suptr
->un_ent
[suptr
->un_cnt
];
576 sunptr
->un_adjval
= adjval
;
577 sunptr
->un_id
= semid
; sunptr
->un_num
= semnum
;
583 /* Assumes we already hold the subsystem lock.
586 semundo_clear(semid
, semnum
)
589 register struct sem_undo
*suptr
;
591 for (suptr
= semu_list
; suptr
!= NULL
; suptr
= suptr
->un_next
) {
592 register struct undo
*sunptr
= &suptr
->un_ent
[0];
595 while (i
< suptr
->un_cnt
) {
596 if (sunptr
->un_id
== semid
) {
597 if (semnum
== -1 || sunptr
->un_num
== semnum
) {
599 if (i
< suptr
->un_cnt
) {
601 suptr
->un_ent
[suptr
->un_cnt
];
614 * Note that the user-mode half of this passes a union, not a pointer
616 #ifndef _SYS_SYSPROTO_H_
626 semctl(p
, uap
, retval
)
628 register struct semctl_args
*uap
;
631 int semid
= uap
->semid
;
632 int semnum
= uap
->semnum
;
634 union semun arg
= uap
->arg
;
635 union semun real_arg
;
636 struct ucred
*cred
= p
->p_ucred
;
638 struct semid_ds sbuf
;
639 register struct semid_ds
*semaptr
;
641 SUBSYSTEM_LOCK_AQUIRE(p
);
643 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid
, semnum
, cmd
, arg
);
646 semid
= IPCID_TO_IX(semid
);
647 if (semid
< 0 || semid
>= seminfo
.semmsl
)
650 printf("Invalid semid\n");
652 UNLOCK_AND_RETURN(EINVAL
);
655 semaptr
= &sema
[semid
];
656 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
657 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
))
658 UNLOCK_AND_RETURN(EINVAL
);
665 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
666 UNLOCK_AND_RETURN(eval
);
667 semaptr
->sem_perm
.cuid
= cred
->cr_uid
;
668 semaptr
->sem_perm
.uid
= cred
->cr_uid
;
669 semtot
-= semaptr
->sem_nsems
;
670 for (i
= semaptr
->sem_base
- sem
; i
< semtot
; i
++)
671 sem
[i
] = sem
[i
+ semaptr
->sem_nsems
];
672 for (i
= 0; i
< seminfo
.semmni
; i
++) {
673 if ((sema
[i
].sem_perm
.mode
& SEM_ALLOC
) &&
674 sema
[i
].sem_base
> semaptr
->sem_base
)
675 sema
[i
].sem_base
-= semaptr
->sem_nsems
;
677 semaptr
->sem_perm
.mode
= 0;
678 semundo_clear(semid
, -1);
679 wakeup((caddr_t
)semaptr
);
683 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
684 UNLOCK_AND_RETURN(eval
);
685 /*if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
686 UNLOCK_AND_RETURN(eval);*/
687 if ((eval
= copyin(arg
.buf
, (caddr_t
)&sbuf
,
689 UNLOCK_AND_RETURN(eval
);
690 semaptr
->sem_perm
.uid
= sbuf
.sem_perm
.uid
;
691 semaptr
->sem_perm
.gid
= sbuf
.sem_perm
.gid
;
692 semaptr
->sem_perm
.mode
= (semaptr
->sem_perm
.mode
& ~0777) |
693 (sbuf
.sem_perm
.mode
& 0777);
694 semaptr
->sem_ctime
= time_second
;
698 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
699 UNLOCK_AND_RETURN(eval
);
700 /*if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
701 UNLOCK_AND_RETURN(eval);*/
702 eval
= copyout((caddr_t
)semaptr
, arg
.buf
,
703 sizeof(struct semid_ds
));
707 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
708 UNLOCK_AND_RETURN(eval
);
709 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
710 UNLOCK_AND_RETURN(EINVAL
);
711 rval
= semaptr
->sem_base
[semnum
].semncnt
;
715 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
716 UNLOCK_AND_RETURN(eval
);
717 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
718 UNLOCK_AND_RETURN(EINVAL
);
719 rval
= semaptr
->sem_base
[semnum
].sempid
;
723 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
724 UNLOCK_AND_RETURN(eval
);
725 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
726 UNLOCK_AND_RETURN(EINVAL
);
727 rval
= semaptr
->sem_base
[semnum
].semval
;
731 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
732 UNLOCK_AND_RETURN(eval
);
733 /*if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
734 UNLOCK_AND_RETURN(eval);*/
735 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
736 eval
= copyout((caddr_t
)&semaptr
->sem_base
[i
].semval
,
737 &arg
.array
[i
], sizeof(arg
.array
[0]));
744 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
745 UNLOCK_AND_RETURN(eval
);
746 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
747 UNLOCK_AND_RETURN(EINVAL
);
748 rval
= semaptr
->sem_base
[semnum
].semzcnt
;
752 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
755 printf("Invalid credentials for write\n");
757 UNLOCK_AND_RETURN(eval
);
759 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
762 printf("Invalid number out of range for set\n");
764 UNLOCK_AND_RETURN(EINVAL
);
766 /*if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
769 printf("Error during value copyin\n");
771 UNLOCK_AND_RETURN(eval);
773 semaptr
->sem_base
[semnum
].semval
= arg
.val
;
774 semundo_clear(semid
, semnum
);
775 wakeup((caddr_t
)semaptr
);
779 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
780 UNLOCK_AND_RETURN(eval
);
781 /*if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
782 UNLOCK_AND_RETURN(eval);*/
783 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
784 eval
= copyin(&arg
.array
[i
],
785 (caddr_t
)&semaptr
->sem_base
[i
].semval
,
786 sizeof(arg
.array
[0]));
790 semundo_clear(semid
, -1);
791 wakeup((caddr_t
)semaptr
);
795 UNLOCK_AND_RETURN(EINVAL
);
800 UNLOCK_AND_RETURN(eval
);
803 #ifndef _SYS_SYSPROTO_H_
812 semget(p
, uap
, retval
)
814 register struct semget_args
*uap
;
819 int nsems
= uap
->nsems
;
820 int semflg
= uap
->semflg
;
821 struct ucred
*cred
= p
->p_ucred
;
823 SUBSYSTEM_LOCK_AQUIRE(p
);
825 if (key
!= IPC_PRIVATE
)
826 printf("semget(0x%x, %d, 0%o)\n", key
, nsems
, semflg
);
828 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems
, semflg
);
831 if (key
!= IPC_PRIVATE
) {
832 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
833 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) &&
834 sema
[semid
].sem_perm
.key
== key
)
837 if (semid
< seminfo
.semmni
) {
839 printf("found public key\n");
841 if ((eval
= ipcperm(cred
, &sema
[semid
].sem_perm
,
843 UNLOCK_AND_RETURN(eval
);
844 if (nsems
> 0 && sema
[semid
].sem_nsems
< nsems
) {
846 printf("too small\n");
848 UNLOCK_AND_RETURN(EINVAL
);
850 if ((semflg
& IPC_CREAT
) && (semflg
& IPC_EXCL
)) {
852 printf("not exclusive\n");
854 UNLOCK_AND_RETURN(EEXIST
);
861 printf("need to allocate an id for the request\n");
863 if (key
== IPC_PRIVATE
|| (semflg
& IPC_CREAT
)) {
864 if (nsems
<= 0 || nsems
> seminfo
.semmsl
) {
866 printf("nsems out of range (0<%d<=%d)\n", nsems
,
869 UNLOCK_AND_RETURN(EINVAL
);
871 if (nsems
> seminfo
.semmns
- semtot
) {
873 printf("not enough semaphores left (need %d, got %d)\n",
874 nsems
, seminfo
.semmns
- semtot
);
876 if (!grow_sem_array(semtot
+ nsems
))
879 printf("failed to grow the sem array\n");
881 UNLOCK_AND_RETURN(ENOSPC
);
884 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
885 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) == 0)
888 if (semid
== seminfo
.semmni
) {
890 printf("no more id's available\n");
892 if (!grow_sema_array(seminfo
.semmni
+ 1))
895 printf("failed to grow sema array\n");
897 UNLOCK_AND_RETURN(ENOSPC
);
901 printf("semid %d is available\n", semid
);
903 sema
[semid
].sem_perm
.key
= key
;
904 sema
[semid
].sem_perm
.cuid
= cred
->cr_uid
;
905 sema
[semid
].sem_perm
.uid
= cred
->cr_uid
;
906 sema
[semid
].sem_perm
.cgid
= cred
->cr_gid
;
907 sema
[semid
].sem_perm
.gid
= cred
->cr_gid
;
908 sema
[semid
].sem_perm
.mode
= (semflg
& 0777) | SEM_ALLOC
;
909 sema
[semid
].sem_perm
.seq
=
910 (sema
[semid
].sem_perm
.seq
+ 1) & 0x7fff;
911 sema
[semid
].sem_nsems
= nsems
;
912 sema
[semid
].sem_otime
= 0;
913 sema
[semid
].sem_ctime
= time_second
;
914 sema
[semid
].sem_base
= &sem
[semtot
];
916 bzero(sema
[semid
].sem_base
,
917 sizeof(sema
[semid
].sem_base
[0])*nsems
);
919 printf("sembase = 0x%x, next = 0x%x\n", sema
[semid
].sem_base
,
924 printf("didn't find it and wasn't asked to create it\n");
926 UNLOCK_AND_RETURN(ENOENT
);
930 *retval
= IXSEQ_TO_IPCID(semid
, sema
[semid
].sem_perm
);
932 printf("semget is done, returning %d\n", *retval
);
934 SUBSYSTEM_LOCK_RELEASE
;
938 #ifndef _SYS_SYSPROTO_H_
947 semop(p
, uap
, retval
)
949 register struct semop_args
*uap
;
952 int semid
= uap
->semid
;
953 int nsops
= uap
->nsops
;
954 struct sembuf sops
[MAX_SOPS
];
955 register struct semid_ds
*semaptr
;
956 register struct sembuf
*sopptr
;
957 register struct sem
*semptr
;
958 struct sem_undo
*suptr
= NULL
;
959 struct ucred
*cred
= p
->p_ucred
;
961 int do_wakeup
, do_undos
;
963 SUBSYSTEM_LOCK_AQUIRE(p
);
965 printf("call to semop(%d, 0x%x, %d)\n", semid
, sops
, nsops
);
968 semid
= IPCID_TO_IX(semid
); /* Convert back to zero origin */
970 if (semid
< 0 || semid
>= seminfo
.semmsl
)
971 UNLOCK_AND_RETURN(EINVAL
);
973 semaptr
= &sema
[semid
];
974 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0)
975 UNLOCK_AND_RETURN(EINVAL
);
976 if (semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
))
977 UNLOCK_AND_RETURN(EINVAL
);
979 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
))) {
981 printf("eval = %d from ipaccess\n", eval
);
983 UNLOCK_AND_RETURN(eval
);
986 if (nsops
> MAX_SOPS
) {
988 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS
, nsops
);
990 UNLOCK_AND_RETURN(E2BIG
);
993 if ((eval
= copyin(uap
->sops
, &sops
, nsops
* sizeof(sops
[0]))) != 0) {
995 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval
,
996 uap
->sops
, &sops
, nsops
* sizeof(sops
[0]));
998 UNLOCK_AND_RETURN(eval
);
1002 * Loop trying to satisfy the vector of requests.
1003 * If we reach a point where we must wait, any requests already
1004 * performed are rolled back and we go to sleep until some other
1005 * process wakes us up. At this point, we start all over again.
1007 * This ensures that from the perspective of other tasks, a set
1008 * of requests is atomic (never partially satisfied).
1015 for (i
= 0; i
< nsops
; i
++) {
1018 if (sopptr
->sem_num
>= semaptr
->sem_nsems
)
1019 UNLOCK_AND_RETURN(EFBIG
);
1021 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1024 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1025 semaptr
, semaptr
->sem_base
, semptr
,
1026 sopptr
->sem_num
, semptr
->semval
, sopptr
->sem_op
,
1027 (sopptr
->sem_flg
& IPC_NOWAIT
) ? "nowait" : "wait");
1030 if (sopptr
->sem_op
< 0) {
1031 if (semptr
->semval
+ sopptr
->sem_op
< 0) {
1033 printf("semop: can't do it now\n");
1037 semptr
->semval
+= sopptr
->sem_op
;
1038 if (semptr
->semval
== 0 &&
1039 semptr
->semzcnt
> 0)
1042 if (sopptr
->sem_flg
& SEM_UNDO
)
1044 } else if (sopptr
->sem_op
== 0) {
1045 if (semptr
->semval
> 0) {
1047 printf("semop: not zero now\n");
1052 if (semptr
->semncnt
> 0)
1054 semptr
->semval
+= sopptr
->sem_op
;
1055 if (sopptr
->sem_flg
& SEM_UNDO
)
1061 * Did we get through the entire vector?
1067 * No ... rollback anything that we've already done
1070 printf("semop: rollback 0 through %d\n", i
-1);
1072 for (j
= 0; j
< i
; j
++)
1073 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1077 * If the request that we couldn't satisfy has the
1078 * NOWAIT flag set then return with EAGAIN.
1080 if (sopptr
->sem_flg
& IPC_NOWAIT
)
1081 UNLOCK_AND_RETURN(EAGAIN
);
1083 if (sopptr
->sem_op
== 0)
1089 printf("semop: good night!\n");
1091 /* Release our lock on the semaphore subsystem so
1092 * another thread can get at the semaphore we are
1093 * waiting for. We will get the lock back after we
1096 SUBSYSTEM_LOCK_RELEASE
;
1097 sysv_sem_sleeping_threads
++;
1098 eval
= tsleep((caddr_t
)semaptr
, (PZERO
- 4) | PCATCH
,
1100 sysv_sem_sleeping_threads
--;
1103 printf("semop: good morning (eval=%d)!\n", eval
);
1105 /* There is no need to get the lock if we are just
1106 * going to return without performing more semaphore
1112 SUBSYSTEM_LOCK_AQUIRE(p
); /* Get it back */
1113 suptr
= NULL
; /* sem_undo may have been reallocated */
1114 semaptr
= &sema
[semid
]; /* sema may have been reallocated */
1118 printf("semop: good morning!\n");
1122 * Make sure that the semaphore still exists
1124 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
1125 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
1126 /* The man page says to return EIDRM. */
1127 /* Unfortunately, BSD doesn't define that code! */
1129 UNLOCK_AND_RETURN(EIDRM
);
1131 UNLOCK_AND_RETURN(EINVAL
);
1136 * The semaphore is still alive. Readjust the count of
1137 * waiting processes. semptr needs to be recomputed
1138 * because the sem[] may have been reallocated while
1139 * we were sleeping, updating our sem_base pointer.
1141 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1142 if (sopptr
->sem_op
== 0)
1150 * Process any SEM_UNDO requests.
1153 for (i
= 0; i
< nsops
; i
++) {
1155 * We only need to deal with SEM_UNDO's for non-zero
1160 if ((sops
[i
].sem_flg
& SEM_UNDO
) == 0)
1162 adjval
= sops
[i
].sem_op
;
1165 eval
= semundo_adjust(p
, &suptr
, semid
,
1166 sops
[i
].sem_num
, -adjval
);
1171 * Oh-Oh! We ran out of either sem_undo's or undo's.
1172 * Rollback the adjustments to this point and then
1173 * rollback the semaphore ups and down so we can return
1174 * with an error with all structures restored. We
1175 * rollback the undo's in the exact reverse order that
1176 * we applied them. This guarantees that we won't run
1177 * out of space as we roll things back out.
1179 for (j
= i
- 1; j
>= 0; j
--) {
1180 if ((sops
[j
].sem_flg
& SEM_UNDO
) == 0)
1182 adjval
= sops
[j
].sem_op
;
1185 if (semundo_adjust(p
, &suptr
, semid
,
1186 sops
[j
].sem_num
, adjval
) != 0)
1187 panic("semop - can't undo undos");
1190 for (j
= 0; j
< nsops
; j
++)
1191 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1195 printf("eval = %d from semundo_adjust\n", eval
);
1197 UNLOCK_AND_RETURN(eval
);
1198 } /* loop through the sops */
1199 } /* if (do_undos) */
1201 /* We're definitely done - set the sempid's */
1202 for (i
= 0; i
< nsops
; i
++) {
1204 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1205 semptr
->sempid
= p
->p_pid
;
1208 /* Do a wakeup if any semaphore was up'd.
1209 * we will release our lock on the semaphore subsystem before
1210 * we wakeup other processes to prevent a little thrashing.
1211 * Note that this is fine because we are done using the
1212 * semaphore structures at this point in time. We only use
1213 * a local variable pointer value, and the retval
1215 * Note 2: Future use of sem_wakeup may reqiure the lock.
1217 SUBSYSTEM_LOCK_RELEASE
;
1220 printf("semop: doing wakeup\n");
1222 sem_wakeup((caddr_t
)semaptr
);
1224 wakeup((caddr_t
)semaptr
);
1226 printf("semop: back from wakeup\n");
1228 wakeup((caddr_t
)semaptr
);
1232 printf("semop: done\n");
1239 * Go through the undo structures for this process and apply the adjustments to
1246 register struct sem_undo
*suptr
;
1247 register struct sem_undo
**supptr
;
1250 /* If we have not allocated our semaphores yet there can't be
1251 * anything to undo, but we need the lock to prevent
1252 * dynamic memory race conditions.
1254 SUBSYSTEM_LOCK_AQUIRE(p
);
1257 SUBSYSTEM_LOCK_RELEASE
;
1263 * Go through the chain of undo vectors looking for one
1264 * associated with this process.
1267 for (supptr
= &semu_list
; (suptr
= *supptr
) != NULL
;
1268 supptr
= &suptr
->un_next
) {
1269 if (suptr
->un_proc
== p
)
1277 printf("proc @%08x has undo structure with %d entries\n", p
,
1282 * If there are any active undo elements then process them.
1284 if (suptr
->un_cnt
> 0) {
1287 for (ix
= 0; ix
< suptr
->un_cnt
; ix
++) {
1288 int semid
= suptr
->un_ent
[ix
].un_id
;
1289 int semnum
= suptr
->un_ent
[ix
].un_num
;
1290 int adjval
= suptr
->un_ent
[ix
].un_adjval
;
1291 struct semid_ds
*semaptr
;
1293 semaptr
= &sema
[semid
];
1294 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0)
1295 panic("semexit - semid not allocated");
1296 if (semnum
>= semaptr
->sem_nsems
)
1297 panic("semexit - semnum out of range");
1300 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1301 suptr
->un_proc
, suptr
->un_ent
[ix
].un_id
,
1302 suptr
->un_ent
[ix
].un_num
,
1303 suptr
->un_ent
[ix
].un_adjval
,
1304 semaptr
->sem_base
[semnum
].semval
);
1308 if (semaptr
->sem_base
[semnum
].semval
< -adjval
)
1309 semaptr
->sem_base
[semnum
].semval
= 0;
1311 semaptr
->sem_base
[semnum
].semval
+=
1314 semaptr
->sem_base
[semnum
].semval
+= adjval
;
1316 /* Maybe we should build a list of semaptr's to wake
1317 * up, finish all access to data structures, release the
1318 * subsystem lock, and wake all the processes. Something
1319 * to think about. It wouldn't buy us anything unless
1320 * wakeup had the potential to block, or the syscall
1321 * funnel state was changed to allow multiple threads
1322 * in the BSD code at once.
1325 sem_wakeup((caddr_t
)semaptr
);
1327 wakeup((caddr_t
)semaptr
);
1330 printf("semexit: back from wakeup\n");
1336 * Deallocate the undo vector.
1339 printf("removing vector\n");
1341 suptr
->un_proc
= NULL
;
1342 *supptr
= suptr
->un_next
;
1346 * There is a semaphore leak (i.e. memory leak) in this code.
1347 * We should be deleting the IPC_PRIVATE semaphores when they are
1348 * no longer needed, and we dont. We would have to track which processes
1349 * know about which IPC_PRIVATE semaphores, updating the list after
1350 * every fork. We can't just delete them semaphore when the process
1351 * that created it dies, because that process may well have forked
1352 * some children. So we need to wait until all of it's children have
1353 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1354 * with the creating group ID, count the number of processes left in
1355 * that group, and delete the semaphore when the group is gone.
1356 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1357 * There is an upper bound on the size of our semaphore array, so
1358 * leaking the semaphores should not work as a DOS attack.
1360 * Please note that the original BSD code this file is based on had the
1361 * same leaky semaphore problem.
1364 SUBSYSTEM_LOCK_RELEASE
;