2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Implementation of SVID semaphores
26 * Author: Daniel Boulet
28 * This software is provided ``AS IS'' without any warranties of any kind.
31 * John Bellardo modified the implementation for Darwin. 12/2000
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc_internal.h>
38 #include <sys/kauth.h>
39 #include <sys/sem_internal.h>
40 #include <sys/malloc.h>
41 #include <mach/mach_types.h>
43 #include <sys/filedesc.h>
44 #include <sys/file_internal.h>
45 #include <sys/sysctl.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
50 #include <bsm/audit_kernel.h>
53 /* Uncomment this line to see the debugging output */
54 /* #define SEM_DEBUG */
56 #define M_SYSVSEM M_TEMP
59 /* Hard system limits to avoid resource starvation / DOS attacks.
60 * These are not needed if we can make the semaphore pages swappable.
62 static struct seminfo limitseminfo
= {
63 SEMMAP
, /* # of entries in semaphore map */
64 SEMMNI
, /* # of semaphore identifiers */
65 SEMMNS
, /* # of semaphores in system */
66 SEMMNU
, /* # of undo structures in system */
67 SEMMSL
, /* max # of semaphores per id */
68 SEMOPM
, /* max # of operations per semop call */
69 SEMUME
, /* max # of undo entries per process */
70 SEMUSZ
, /* size in bytes of undo structure */
71 SEMVMX
, /* semaphore maximum value */
72 SEMAEM
/* adjust on exit max value */
75 /* Current system allocations. We use this structure to track how many
76 * resources we have allocated so far. This way we can set large hard limits
77 * and not allocate the memory for them up front.
79 struct seminfo seminfo
= {
80 SEMMAP
, /* Unused, # of entries in semaphore map */
81 0, /* # of semaphore identifiers */
82 0, /* # of semaphores in system */
83 0, /* # of undo entries in system */
84 SEMMSL
, /* max # of semaphores per id */
85 SEMOPM
, /* max # of operations per semop call */
86 SEMUME
, /* max # of undo entries per process */
87 SEMUSZ
, /* size in bytes of undo structure */
88 SEMVMX
, /* semaphore maximum value */
89 SEMAEM
/* adjust on exit max value */
93 static struct sem_undo
*semu_alloc(struct proc
*p
);
94 static int semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
,
95 int semid
, int semnum
, int adjval
);
96 static void semundo_clear(int semid
, int semnum
);
98 /* XXX casting to (sy_call_t *) is bogus, as usual. */
99 static sy_call_t
*semcalls
[] = {
100 (sy_call_t
*)semctl
, (sy_call_t
*)semget
,
101 (sy_call_t
*)semop
, (sy_call_t
*)semconfig
104 static int semtot
= 0; /* # of used semaphores */
105 struct user_semid_ds
*sema
= NULL
; /* semaphore id pool */
106 struct sem
*sem_pool
= NULL
; /* semaphore pool */
107 static struct sem_undo
*semu_list
= NULL
; /* active undo structures */
108 struct sem_undo
*semu
= NULL
; /* semaphore undo pool */
111 void sysv_sem_lock_init(void);
112 static lck_grp_t
*sysv_sem_subsys_lck_grp
;
113 static lck_grp_attr_t
*sysv_sem_subsys_lck_grp_attr
;
114 static lck_attr_t
*sysv_sem_subsys_lck_attr
;
115 static lck_mtx_t sysv_sem_subsys_mutex
;
117 #define SYSV_SEM_SUBSYS_LOCK() lck_mtx_lock(&sysv_sem_subsys_mutex)
118 #define SYSV_SEM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_sem_subsys_mutex)
121 __private_extern__
void
122 sysv_sem_lock_init( void )
125 sysv_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
126 lck_grp_attr_setstat(sysv_sem_subsys_lck_grp_attr
);
128 sysv_sem_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_sem_subsys_lck_grp_attr
);
130 sysv_sem_subsys_lck_attr
= lck_attr_alloc_init();
131 lck_attr_setdebug(sysv_sem_subsys_lck_attr
);
132 lck_mtx_init(&sysv_sem_subsys_mutex
, sysv_sem_subsys_lck_grp
, sysv_sem_subsys_lck_attr
);
135 static __inline__ user_time_t
144 * XXX conversion of internal user_time_t to external tume_t loses
145 * XXX precision; not an issue for us now, since we are only ever
146 * XXX setting 32 bits worth of time into it.
148 * pad field contents are not moved correspondingly; contents will be lost
150 * NOTE: Source and target may *NOT* overlap! (target is smaller)
153 semid_ds_64to32(struct user_semid_ds
*in
, struct semid_ds
*out
)
155 out
->sem_perm
= in
->sem_perm
;
156 out
->sem_base
= (__int32_t
)in
->sem_base
;
157 out
->sem_nsems
= in
->sem_nsems
;
158 out
->sem_otime
= in
->sem_otime
; /* XXX loses precision */
159 out
->sem_ctime
= in
->sem_ctime
; /* XXX loses precision */
163 * pad field contents are not moved correspondingly; contents will be lost
165 * NOTE: Source and target may are permitted to overlap! (source is smaller);
166 * this works because we copy fields in order from the end of the struct to
169 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
173 semid_ds_32to64(struct semid_ds
*in
, struct user_semid_ds
*out
)
175 out
->sem_ctime
= in
->sem_ctime
;
176 out
->sem_otime
= in
->sem_otime
;
177 out
->sem_nsems
= in
->sem_nsems
;
178 out
->sem_base
= (void *)in
->sem_base
;
179 out
->sem_perm
= in
->sem_perm
;
184 * Entry point for all SEM calls
186 * In Darwin this is no longer the entry point. It will be removed after
187 * the code has been tested better.
189 /* XXX actually varargs. */
191 semsys(struct proc
*p
, struct semsys_args
*uap
, register_t
*retval
)
194 /* The individual calls handling the locking now */
196 if (uap
->which
>= sizeof(semcalls
)/sizeof(semcalls
[0]))
198 return ((*semcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
202 * Lock or unlock the entire semaphore facility.
204 * This will probably eventually evolve into a general purpose semaphore
205 * facility status enquiry mechanism (I don't like the "read /dev/kmem"
206 * approach currently taken by ipcs and the amount of info that we want
207 * to be able to extract for ipcs is probably beyond what the capability
208 * of the getkerninfo facility.
210 * At the time that the current version of semconfig was written, ipcs is
211 * the only user of the semconfig facility. It uses it to ensure that the
212 * semaphore facility data structures remain static while it fishes around
217 semconfig(__unused
struct proc
*p
, struct semconfig_args
*uap
, register_t
*retval
)
222 case SEM_CONFIG_FREEZE
:
223 SYSV_SEM_SUBSYS_LOCK();
226 case SEM_CONFIG_THAW
:
227 SYSV_SEM_SUBSYS_UNLOCK();
231 printf("semconfig: unknown flag parameter value (%d) - ignored\n",
242 * Expand the semu array to the given capacity. If the expansion fails
243 * return 0, otherwise return 1.
245 * Assumes we already have the subsystem lock.
248 grow_semu_array(int newSize
)
251 register struct sem_undo
*newSemu
;
253 if (newSize
<= seminfo
.semmnu
)
255 if (newSize
> limitseminfo
.semmnu
) /* enforce hard limit */
258 printf("undo structure hard limit of %d reached, requested %d\n",
259 limitseminfo
.semmnu
, newSize
);
263 newSize
= (newSize
/SEMMNU_INC
+ 1) * SEMMNU_INC
;
264 newSize
= newSize
> limitseminfo
.semmnu
? limitseminfo
.semmnu
: newSize
;
267 printf("growing semu[] from %d to %d\n", seminfo
.semmnu
, newSize
);
269 MALLOC(newSemu
, struct sem_undo
*, sizeof (struct sem_undo
) * newSize
,
270 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
274 printf("allocation failed. no changes made.\n");
279 /* copy the old data to the new array */
280 for (i
= 0; i
< seminfo
.semmnu
; i
++)
282 newSemu
[i
] = semu
[i
];
285 * The new elements (from newSemu[i] to newSemu[newSize-1]) have their
286 * "un_proc" set to 0 (i.e. NULL) by the M_ZERO flag to MALLOC() above,
287 * so they're already marked as "not in use".
290 /* Clean up the old array */
292 FREE(semu
, M_SYSVSEM
);
295 seminfo
.semmnu
= newSize
;
297 printf("expansion successful\n");
303 * Expand the sema array to the given capacity. If the expansion fails
304 * we return 0, otherwise we return 1.
306 * Assumes we already have the subsystem lock.
309 grow_sema_array(int newSize
)
311 register struct user_semid_ds
*newSema
;
314 if (newSize
<= seminfo
.semmni
)
316 if (newSize
> limitseminfo
.semmni
) /* enforce hard limit */
319 printf("identifier hard limit of %d reached, requested %d\n",
320 limitseminfo
.semmni
, newSize
);
324 newSize
= (newSize
/SEMMNI_INC
+ 1) * SEMMNI_INC
;
325 newSize
= newSize
> limitseminfo
.semmni
? limitseminfo
.semmni
: newSize
;
328 printf("growing sema[] from %d to %d\n", seminfo
.semmni
, newSize
);
330 MALLOC(newSema
, struct user_semid_ds
*,
331 sizeof (struct user_semid_ds
) * newSize
,
332 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
336 printf("allocation failed. no changes made.\n");
341 /* copy over the old ids */
342 for (i
= 0; i
< seminfo
.semmni
; i
++)
344 newSema
[i
] = sema
[i
];
345 /* This is a hack. What we really want to be able to
346 * do is change the value a process is waiting on
347 * without waking it up, but I don't know how to do
348 * this with the existing code, so we wake up the
349 * process and let it do a lot of work to determine the
350 * semaphore set is really not available yet, and then
351 * sleep on the correct, reallocated user_semid_ds pointer.
353 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
)
354 wakeup((caddr_t
)&sema
[i
]);
357 * The new elements (from newSema[i] to newSema[newSize-1]) have their
358 * "sem_base" and "sem_perm.mode" set to 0 (i.e. NULL) by the M_ZERO
359 * flag to MALLOC() above, so they're already marked as "not in use".
362 /* Clean up the old array */
364 FREE(sema
, M_SYSVSEM
);
367 seminfo
.semmni
= newSize
;
369 printf("expansion successful\n");
375 * Expand the sem_pool array to the given capacity. If the expansion fails
376 * we return 0 (fail), otherwise we return 1 (success).
378 * Assumes we already hold the subsystem lock.
381 grow_sem_pool(int new_pool_size
)
383 struct sem
*new_sem_pool
= NULL
;
384 struct sem
*sem_free
;
387 if (new_pool_size
< semtot
)
389 /* enforce hard limit */
390 if (new_pool_size
> limitseminfo
.semmns
) {
392 printf("semaphore hard limit of %d reached, requested %d\n",
393 limitseminfo
.semmns
, new_pool_size
);
398 new_pool_size
= (new_pool_size
/SEMMNS_INC
+ 1) * SEMMNS_INC
;
399 new_pool_size
= new_pool_size
> limitseminfo
.semmns
? limitseminfo
.semmns
: new_pool_size
;
402 printf("growing sem_pool array from %d to %d\n", seminfo
.semmns
, new_pool_size
);
404 MALLOC(new_sem_pool
, struct sem
*, sizeof (struct sem
) * new_pool_size
,
405 M_SYSVSEM
, M_WAITOK
| M_ZERO
);
406 if (NULL
== new_sem_pool
) {
408 printf("allocation failed. no changes made.\n");
413 /* We have our new memory, now copy the old contents over */
415 for(i
= 0; i
< seminfo
.semmns
; i
++)
416 new_sem_pool
[i
] = sem_pool
[i
];
418 /* Update our id structures to point to the new semaphores */
419 for(i
= 0; i
< seminfo
.semmni
; i
++) {
420 if (sema
[i
].sem_perm
.mode
& SEM_ALLOC
) /* ID in use */
421 sema
[i
].sem_base
+= (new_sem_pool
- sem_pool
);
425 sem_pool
= new_sem_pool
;
427 /* clean up the old array */
428 if (sem_free
!= NULL
)
429 FREE(sem_free
, M_SYSVSEM
);
431 seminfo
.semmns
= new_pool_size
;
433 printf("expansion complete\n");
439 * Allocate a new sem_undo structure for a process
440 * (returns ptr to structure or NULL if no more room)
442 * Assumes we already hold the subsystem lock.
445 static struct sem_undo
*
446 semu_alloc(struct proc
*p
)
449 register struct sem_undo
*suptr
;
450 register struct sem_undo
**supptr
;
454 * Try twice to allocate something.
455 * (we'll purge any empty structures after the first pass so
456 * two passes are always enough)
459 for (attempt
= 0; attempt
< 2; attempt
++) {
461 * Look for a free structure.
462 * Fill it in and return it if we find one.
465 for (i
= 0; i
< seminfo
.semmnu
; i
++) {
467 if (suptr
->un_proc
== NULL
) {
468 suptr
->un_next
= semu_list
;
471 suptr
->un_ent
= NULL
;
478 * We didn't find a free one, if this is the first attempt
479 * then try to free some structures.
483 /* All the structures are in use - try to free some */
484 int did_something
= 0;
487 while ((suptr
= *supptr
) != NULL
) {
488 if (suptr
->un_cnt
== 0) {
489 suptr
->un_proc
= NULL
;
490 *supptr
= suptr
->un_next
;
493 supptr
= &(suptr
->un_next
);
496 /* If we didn't free anything. Try expanding
497 * the semu[] array. If that doesn't work
498 * then fail. We expand last to get the
499 * most reuse out of existing resources.
502 if (!grow_semu_array(seminfo
.semmnu
+ 1))
506 * The second pass failed even though we freed
507 * something after the first pass!
508 * This is IMPOSSIBLE!
510 panic("semu_alloc - second attempt failed");
517 * Adjust a particular entry for a particular proc
519 * Assumes we already hold the subsystem lock.
522 semundo_adjust(struct proc
*p
, struct sem_undo
**supptr
, int semid
,
523 int semnum
, int adjval
)
525 register struct sem_undo
*suptr
;
526 register struct undo
*sueptr
, **suepptr
, *new_sueptr
;
530 * Look for and remember the sem_undo if the caller doesn't provide it
535 for (suptr
= semu_list
; suptr
!= NULL
;
536 suptr
= suptr
->un_next
) {
537 if (suptr
->un_proc
== p
) {
545 suptr
= semu_alloc(p
);
553 * Look for the requested entry and adjust it (delete if adjval becomes
557 for (i
= 0, suepptr
= &suptr
->un_ent
, sueptr
= suptr
->un_ent
;
559 i
++, suepptr
= &sueptr
->une_next
, sueptr
= sueptr
->une_next
) {
560 if (sueptr
->une_id
!= semid
|| sueptr
->une_num
!= semnum
)
563 sueptr
->une_adjval
= 0;
565 sueptr
->une_adjval
+= adjval
;
566 if (sueptr
->une_adjval
== 0) {
568 *suepptr
= sueptr
->une_next
;
569 FREE(sueptr
, M_SYSVSEM
);
575 /* Didn't find the right entry - create it */
577 /* no adjustment: no need for a new entry */
581 if (suptr
->un_cnt
== limitseminfo
.semume
) {
582 /* reached the limit number of semaphore undo entries */
586 /* allocate a new semaphore undo entry */
587 MALLOC(new_sueptr
, struct undo
*, sizeof (struct undo
),
588 M_SYSVSEM
, M_WAITOK
);
589 if (new_sueptr
== NULL
) {
593 /* fill in the new semaphore undo entry */
594 new_sueptr
->une_next
= suptr
->un_ent
;
595 suptr
->un_ent
= new_sueptr
;
597 new_sueptr
->une_adjval
= adjval
;
598 new_sueptr
->une_id
= semid
;
599 new_sueptr
->une_num
= semnum
;
604 /* Assumes we already hold the subsystem lock.
607 semundo_clear(int semid
, int semnum
)
609 struct sem_undo
*suptr
;
611 for (suptr
= semu_list
; suptr
!= NULL
; suptr
= suptr
->un_next
) {
613 struct undo
**suepptr
;
616 sueptr
= suptr
->un_ent
;
617 suepptr
= &suptr
->un_ent
;
618 while (i
< suptr
->un_cnt
) {
619 if (sueptr
->une_id
== semid
) {
620 if (semnum
== -1 || sueptr
->une_num
== semnum
) {
622 *suepptr
= sueptr
->une_next
;
623 FREE(sueptr
, M_SYSVSEM
);
631 suepptr
= &sueptr
->une_next
;
632 sueptr
= sueptr
->une_next
;
638 * Note that the user-mode half of this passes a union coerced to a
639 * user_addr_t. The union contains either an int or a pointer, and
640 * so we have to coerce it back, variant on whether the calling
641 * process is 64 bit or not. The coercion works for the 'val' element
642 * because the alignment is the same in user and kernel space.
645 semctl(struct proc
*p
, struct semctl_args
*uap
, register_t
*retval
)
647 int semid
= uap
->semid
;
648 int semnum
= uap
->semnum
;
650 user_semun_t user_arg
= (user_semun_t
)uap
->arg
;
651 kauth_cred_t cred
= kauth_cred_get();
653 struct user_semid_ds sbuf
;
654 struct user_semid_ds
*semaptr
;
655 struct user_semid_ds uds
;
658 AUDIT_ARG(svipc_cmd
, cmd
);
659 AUDIT_ARG(svipc_id
, semid
);
661 SYSV_SEM_SUBSYS_LOCK();
664 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid
, semnum
, cmd
, user_arg
);
667 semid
= IPCID_TO_IX(semid
);
669 if (semid
< 0 || semid
>= seminfo
.semmni
) {
671 printf("Invalid semid\n");
677 semaptr
= &sema
[semid
];
678 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
679 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
689 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
692 semaptr
->sem_perm
.cuid
= kauth_cred_getuid(cred
);
693 semaptr
->sem_perm
.uid
= kauth_cred_getuid(cred
);
694 semtot
-= semaptr
->sem_nsems
;
695 for (i
= semaptr
->sem_base
- sem_pool
; i
< semtot
; i
++)
696 sem_pool
[i
] = sem_pool
[i
+ semaptr
->sem_nsems
];
697 for (i
= 0; i
< seminfo
.semmni
; i
++) {
698 if ((sema
[i
].sem_perm
.mode
& SEM_ALLOC
) &&
699 sema
[i
].sem_base
> semaptr
->sem_base
)
700 sema
[i
].sem_base
-= semaptr
->sem_nsems
;
702 semaptr
->sem_perm
.mode
= 0;
703 semundo_clear(semid
, -1);
704 wakeup((caddr_t
)semaptr
);
708 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_M
)))
711 if (IS_64BIT_PROCESS(p
)) {
712 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct user_semid_ds
));
714 eval
= copyin(user_arg
.buf
, &sbuf
, sizeof(struct semid_ds
));
715 /* convert in place; ugly, but safe */
716 semid_ds_32to64((struct semid_ds
*)&sbuf
, &sbuf
);
723 semaptr
->sem_perm
.uid
= sbuf
.sem_perm
.uid
;
724 semaptr
->sem_perm
.gid
= sbuf
.sem_perm
.gid
;
725 semaptr
->sem_perm
.mode
= (semaptr
->sem_perm
.mode
& ~0777) |
726 (sbuf
.sem_perm
.mode
& 0777);
727 semaptr
->sem_ctime
= sysv_semtime();
731 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
733 bcopy(semaptr
, &uds
, sizeof(struct user_semid_ds
));
734 if (IS_64BIT_PROCESS(p
)) {
735 eval
= copyout(&uds
, user_arg
.buf
, sizeof(struct user_semid_ds
));
737 struct semid_ds semid_ds32
;
738 semid_ds_64to32(&uds
, &semid_ds32
);
739 eval
= copyout(&semid_ds32
, user_arg
.buf
, sizeof(struct semid_ds
));
744 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
746 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
750 rval
= semaptr
->sem_base
[semnum
].semncnt
;
754 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
756 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
760 rval
= semaptr
->sem_base
[semnum
].sempid
;
764 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
766 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
770 rval
= semaptr
->sem_base
[semnum
].semval
;
774 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
776 /* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
777 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
778 /* XXX could be done in one go... */
779 eval
= copyout((caddr_t
)&semaptr
->sem_base
[i
].semval
,
780 user_arg
.array
+ (i
* sizeof(unsigned short)),
781 sizeof(unsigned short));
788 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_R
)))
790 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
) {
794 rval
= semaptr
->sem_base
[semnum
].semzcnt
;
798 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
801 printf("Invalid credentials for write\n");
805 if (semnum
< 0 || semnum
>= semaptr
->sem_nsems
)
808 printf("Invalid number out of range for set\n");
814 * Cast down a pointer instead of using 'val' member directly
815 * to avoid introducing endieness and a pad field into the
816 * header file. Ugly, but it works.
818 semaptr
->sem_base
[semnum
].semval
= CAST_DOWN(int,user_arg
.buf
);
819 semundo_clear(semid
, semnum
);
820 wakeup((caddr_t
)semaptr
);
824 if ((eval
= ipcperm(cred
, &semaptr
->sem_perm
, IPC_W
)))
826 /*** XXXXXXXXXXXX TBD ********/
827 for (i
= 0; i
< semaptr
->sem_nsems
; i
++) {
828 /* XXX could be done in one go... */
829 eval
= copyin(user_arg
.array
+ (i
* sizeof(unsigned short)),
830 (caddr_t
)&semaptr
->sem_base
[i
].semval
,
831 sizeof(unsigned short));
835 semundo_clear(semid
, -1);
836 wakeup((caddr_t
)semaptr
);
847 SYSV_SEM_SUBSYS_UNLOCK();
852 semget(__unused
struct proc
*p
, struct semget_args
*uap
, register_t
*retval
)
856 int nsems
= uap
->nsems
;
857 int semflg
= uap
->semflg
;
858 kauth_cred_t cred
= kauth_cred_get();
861 if (key
!= IPC_PRIVATE
)
862 printf("semget(0x%x, %d, 0%o)\n", key
, nsems
, semflg
);
864 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems
, semflg
);
868 SYSV_SEM_SUBSYS_LOCK();
871 if (key
!= IPC_PRIVATE
) {
872 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
873 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) &&
874 sema
[semid
].sem_perm
.key
== key
)
877 if (semid
< seminfo
.semmni
) {
879 printf("found public key\n");
881 if ((eval
= ipcperm(cred
, &sema
[semid
].sem_perm
,
884 if (nsems
< 0 || sema
[semid
].sem_nsems
< nsems
) {
886 printf("too small\n");
891 if ((semflg
& IPC_CREAT
) && (semflg
& IPC_EXCL
)) {
893 printf("not exclusive\n");
903 printf("need to allocate an id for the request\n");
905 if (key
== IPC_PRIVATE
|| (semflg
& IPC_CREAT
)) {
906 if (nsems
<= 0 || nsems
> limitseminfo
.semmsl
) {
908 printf("nsems out of range (0<%d<=%d)\n", nsems
,
914 if (nsems
> seminfo
.semmns
- semtot
) {
916 printf("not enough semaphores left (need %d, got %d)\n",
917 nsems
, seminfo
.semmns
- semtot
);
919 if (!grow_sem_pool(semtot
+ nsems
)) {
921 printf("failed to grow the sem array\n");
927 for (semid
= 0; semid
< seminfo
.semmni
; semid
++) {
928 if ((sema
[semid
].sem_perm
.mode
& SEM_ALLOC
) == 0)
931 if (semid
== seminfo
.semmni
) {
933 printf("no more id's available\n");
935 if (!grow_sema_array(seminfo
.semmni
+ 1))
938 printf("failed to grow sema array\n");
945 printf("semid %d is available\n", semid
);
947 sema
[semid
].sem_perm
.key
= key
;
948 sema
[semid
].sem_perm
.cuid
= kauth_cred_getuid(cred
);
949 sema
[semid
].sem_perm
.uid
= kauth_cred_getuid(cred
);
950 sema
[semid
].sem_perm
.cgid
= cred
->cr_gid
;
951 sema
[semid
].sem_perm
.gid
= cred
->cr_gid
;
952 sema
[semid
].sem_perm
.mode
= (semflg
& 0777) | SEM_ALLOC
;
953 sema
[semid
].sem_perm
.seq
=
954 (sema
[semid
].sem_perm
.seq
+ 1) & 0x7fff;
955 sema
[semid
].sem_nsems
= nsems
;
956 sema
[semid
].sem_otime
= 0;
957 sema
[semid
].sem_ctime
= sysv_semtime();
958 sema
[semid
].sem_base
= &sem_pool
[semtot
];
960 bzero(sema
[semid
].sem_base
,
961 sizeof(sema
[semid
].sem_base
[0])*nsems
);
963 printf("sembase = 0x%x, next = 0x%x\n", sema
[semid
].sem_base
,
968 printf("didn't find it and wasn't asked to create it\n");
975 *retval
= IXSEQ_TO_IPCID(semid
, sema
[semid
].sem_perm
);
976 AUDIT_ARG(svipc_id
, *retval
);
978 printf("semget is done, returning %d\n", *retval
);
983 SYSV_SEM_SUBSYS_UNLOCK();
988 semop(struct proc
*p
, struct semop_args
*uap
, register_t
*retval
)
990 int semid
= uap
->semid
;
991 int nsops
= uap
->nsops
;
992 struct sembuf sops
[MAX_SOPS
];
993 register struct user_semid_ds
*semaptr
;
994 register struct sembuf
*sopptr
= NULL
; /* protected by 'semptr' */
995 register struct sem
*semptr
= NULL
; /* protected by 'if' */
996 struct sem_undo
*suptr
= NULL
;
998 int do_wakeup
, do_undos
;
1000 AUDIT_ARG(svipc_id
, uap
->semid
);
1002 SYSV_SEM_SUBSYS_LOCK();
1005 printf("call to semop(%d, 0x%x, %d)\n", semid
, sops
, nsops
);
1008 semid
= IPCID_TO_IX(semid
); /* Convert back to zero origin */
1010 if (semid
< 0 || semid
>= seminfo
.semmni
) {
1015 semaptr
= &sema
[semid
];
1016 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0) {
1020 if (semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
)) {
1025 if ((eval
= ipcperm(kauth_cred_get(), &semaptr
->sem_perm
, IPC_W
))) {
1027 printf("eval = %d from ipaccess\n", eval
);
1032 if (nsops
< 0 || nsops
> MAX_SOPS
) {
1034 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS
, nsops
);
1040 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
1041 if ((eval
= copyin(uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
))) != 0) {
1043 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval
,
1044 uap
->sops
, &sops
, nsops
* sizeof(struct sembuf
));
1050 * Loop trying to satisfy the vector of requests.
1051 * If we reach a point where we must wait, any requests already
1052 * performed are rolled back and we go to sleep until some other
1053 * process wakes us up. At this point, we start all over again.
1055 * This ensures that from the perspective of other tasks, a set
1056 * of requests is atomic (never partially satisfied).
1063 for (i
= 0; i
< nsops
; i
++) {
1066 if (sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1071 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1074 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1075 semaptr
, semaptr
->sem_base
, semptr
,
1076 sopptr
->sem_num
, semptr
->semval
, sopptr
->sem_op
,
1077 (sopptr
->sem_flg
& IPC_NOWAIT
) ? "nowait" : "wait");
1080 if (sopptr
->sem_op
< 0) {
1081 if (semptr
->semval
+ sopptr
->sem_op
< 0) {
1083 printf("semop: can't do it now\n");
1087 semptr
->semval
+= sopptr
->sem_op
;
1088 if (semptr
->semval
== 0 &&
1089 semptr
->semzcnt
> 0)
1092 if (sopptr
->sem_flg
& SEM_UNDO
)
1094 } else if (sopptr
->sem_op
== 0) {
1095 if (semptr
->semval
> 0) {
1097 printf("semop: not zero now\n");
1102 if (semptr
->semncnt
> 0)
1104 semptr
->semval
+= sopptr
->sem_op
;
1105 if (sopptr
->sem_flg
& SEM_UNDO
)
1111 * Did we get through the entire vector?
1117 * No ... rollback anything that we've already done
1120 printf("semop: rollback 0 through %d\n", i
-1);
1122 for (j
= 0; j
< i
; j
++)
1123 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1127 * If the request that we couldn't satisfy has the
1128 * NOWAIT flag set then return with EAGAIN.
1130 if (sopptr
->sem_flg
& IPC_NOWAIT
) {
1135 if (sopptr
->sem_op
== 0)
1141 printf("semop: good night!\n");
1143 /* Release our lock on the semaphore subsystem so
1144 * another thread can get at the semaphore we are
1145 * waiting for. We will get the lock back after we
1148 eval
= msleep((caddr_t
)semaptr
, &sysv_sem_subsys_mutex
, (PZERO
- 4) | PCATCH
,
1152 printf("semop: good morning (eval=%d)!\n", eval
);
1159 * IMPORTANT: while we were asleep, the semaphore array might
1160 * have been reallocated somewhere else (see grow_sema_array()).
1161 * When we wake up, we have to re-lookup the semaphore
1162 * structures and re-validate them.
1165 suptr
= NULL
; /* sem_undo may have been reallocated */
1166 semaptr
= &sema
[semid
]; /* sema may have been reallocated */
1169 * Make sure that the semaphore still exists
1171 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0 ||
1172 semaptr
->sem_perm
.seq
!= IPCID_TO_SEQ(uap
->semid
) ||
1173 sopptr
->sem_num
>= semaptr
->sem_nsems
) {
1174 if (eval
== EINTR
) {
1176 * EINTR takes precedence over the fact that
1177 * the semaphore disappeared while we were
1182 * The man page says to return EIDRM.
1183 * Unfortunately, BSD doesn't define that code!
1195 * The semaphore is still alive. Readjust the count of
1196 * waiting processes. semptr needs to be recomputed
1197 * because the sem[] may have been reallocated while
1198 * we were sleeping, updating our sem_base pointer.
1200 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1201 if (sopptr
->sem_op
== 0)
1206 if (eval
!= 0) { /* EINTR */
1213 * Process any SEM_UNDO requests.
1216 for (i
= 0; i
< nsops
; i
++) {
1218 * We only need to deal with SEM_UNDO's for non-zero
1223 if ((sops
[i
].sem_flg
& SEM_UNDO
) == 0)
1225 adjval
= sops
[i
].sem_op
;
1228 eval
= semundo_adjust(p
, &suptr
, semid
,
1229 sops
[i
].sem_num
, -adjval
);
1234 * Oh-Oh! We ran out of either sem_undo's or undo's.
1235 * Rollback the adjustments to this point and then
1236 * rollback the semaphore ups and down so we can return
1237 * with an error with all structures restored. We
1238 * rollback the undo's in the exact reverse order that
1239 * we applied them. This guarantees that we won't run
1240 * out of space as we roll things back out.
1242 for (j
= i
- 1; j
>= 0; j
--) {
1243 if ((sops
[j
].sem_flg
& SEM_UNDO
) == 0)
1245 adjval
= sops
[j
].sem_op
;
1248 if (semundo_adjust(p
, &suptr
, semid
,
1249 sops
[j
].sem_num
, adjval
) != 0)
1250 panic("semop - can't undo undos");
1253 for (j
= 0; j
< nsops
; j
++)
1254 semaptr
->sem_base
[sops
[j
].sem_num
].semval
-=
1258 printf("eval = %d from semundo_adjust\n", eval
);
1261 } /* loop through the sops */
1262 } /* if (do_undos) */
1264 /* We're definitely done - set the sempid's */
1265 for (i
= 0; i
< nsops
; i
++) {
1267 semptr
= &semaptr
->sem_base
[sopptr
->sem_num
];
1268 semptr
->sempid
= p
->p_pid
;
1273 printf("semop: doing wakeup\n");
1275 sem_wakeup((caddr_t
)semaptr
);
1277 wakeup((caddr_t
)semaptr
);
1279 printf("semop: back from wakeup\n");
1281 wakeup((caddr_t
)semaptr
);
1285 printf("semop: done\n");
1290 SYSV_SEM_SUBSYS_UNLOCK();
1295 * Go through the undo structures for this process and apply the adjustments to
1299 semexit(struct proc
*p
)
1301 register struct sem_undo
*suptr
;
1302 register struct sem_undo
**supptr
;
1305 /* If we have not allocated our semaphores yet there can't be
1306 * anything to undo, but we need the lock to prevent
1307 * dynamic memory race conditions.
1309 SYSV_SEM_SUBSYS_LOCK();
1313 SYSV_SEM_SUBSYS_UNLOCK();
1319 * Go through the chain of undo vectors looking for one
1320 * associated with this process.
1323 for (supptr
= &semu_list
; (suptr
= *supptr
) != NULL
;
1324 supptr
= &suptr
->un_next
) {
1325 if (suptr
->un_proc
== p
)
1333 printf("proc @%08x has undo structure with %d entries\n", p
,
1338 * If there are any active undo elements then process them.
1340 if (suptr
->un_cnt
> 0) {
1341 while (suptr
->un_ent
!= NULL
) {
1342 struct undo
*sueptr
;
1346 struct user_semid_ds
*semaptr
;
1348 sueptr
= suptr
->un_ent
;
1349 semid
= sueptr
->une_id
;
1350 semnum
= sueptr
->une_num
;
1351 adjval
= sueptr
->une_adjval
;
1353 semaptr
= &sema
[semid
];
1354 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) == 0)
1355 panic("semexit - semid not allocated");
1356 if (semnum
>= semaptr
->sem_nsems
)
1357 panic("semexit - semnum out of range");
1360 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1365 semaptr
->sem_base
[semnum
].semval
);
1369 if (semaptr
->sem_base
[semnum
].semval
< -adjval
)
1370 semaptr
->sem_base
[semnum
].semval
= 0;
1372 semaptr
->sem_base
[semnum
].semval
+=
1375 semaptr
->sem_base
[semnum
].semval
+= adjval
;
1377 /* Maybe we should build a list of semaptr's to wake
1378 * up, finish all access to data structures, release the
1379 * subsystem lock, and wake all the processes. Something
1380 * to think about. It wouldn't buy us anything unless
1381 * wakeup had the potential to block, or the syscall
1382 * funnel state was changed to allow multiple threads
1383 * in the BSD code at once.
1386 sem_wakeup((caddr_t
)semaptr
);
1388 wakeup((caddr_t
)semaptr
);
1391 printf("semexit: back from wakeup\n");
1394 suptr
->un_ent
= sueptr
->une_next
;
1395 FREE(sueptr
, M_SYSVSEM
);
1401 * Deallocate the undo vector.
1404 printf("removing vector\n");
1406 suptr
->un_proc
= NULL
;
1407 *supptr
= suptr
->un_next
;
1411 * There is a semaphore leak (i.e. memory leak) in this code.
1412 * We should be deleting the IPC_PRIVATE semaphores when they are
1413 * no longer needed, and we dont. We would have to track which processes
1414 * know about which IPC_PRIVATE semaphores, updating the list after
1415 * every fork. We can't just delete them semaphore when the process
1416 * that created it dies, because that process may well have forked
1417 * some children. So we need to wait until all of it's children have
1418 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1419 * with the creating group ID, count the number of processes left in
1420 * that group, and delete the semaphore when the group is gone.
1421 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1422 * There is an upper bound on the size of our semaphore array, so
1423 * leaking the semaphores should not work as a DOS attack.
1425 * Please note that the original BSD code this file is based on had the
1426 * same leaky semaphore problem.
1429 SYSV_SEM_SUBSYS_UNLOCK();
1433 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1434 struct sysctl_req *req) */
1436 sysctl_seminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
1437 __unused
int arg2
, struct sysctl_req
*req
)
1441 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1442 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1445 SYSV_SEM_SUBSYS_LOCK();
1447 /* Set the values only if shared memory is not initialised */
1448 if ((sem_pool
== NULL
) &&
1451 (semu_list
== NULL
)) {
1452 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int)))) {
1458 SYSV_SEM_SUBSYS_UNLOCK();
1463 /* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1464 extern struct sysctl_oid_list sysctl__kern_sysv_children
;
1465 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNI
, semmni
, CTLTYPE_INT
| CTLFLAG_RW
,
1466 &limitseminfo
.semmni
, 0, &sysctl_seminfo
,"I","semmni");
1468 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNS
, semmns
, CTLTYPE_INT
| CTLFLAG_RW
,
1469 &limitseminfo
.semmns
, 0, &sysctl_seminfo
,"I","semmns");
1471 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMNU
, semmnu
, CTLTYPE_INT
| CTLFLAG_RW
,
1472 &limitseminfo
.semmnu
, 0, &sysctl_seminfo
,"I","semmnu");
1474 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMMSL
, semmsl
, CTLTYPE_INT
| CTLFLAG_RW
,
1475 &limitseminfo
.semmsl
, 0, &sysctl_seminfo
,"I","semmsl");
1477 SYSCTL_PROC(_kern_sysv
, KSYSV_SEMUNE
, semume
, CTLTYPE_INT
| CTLFLAG_RW
,
1478 &limitseminfo
.semume
, 0, &sysctl_seminfo
,"I","semume");
1482 IPCS_sem_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1483 __unused
int arg2
, struct sysctl_req
*req
)
1488 struct IPCS_command u32
;
1489 struct user_IPCS_command u64
;
1491 struct semid_ds semid_ds32
; /* post conversion, 32 bit version */
1493 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1494 size_t semid_ds_sz
= sizeof(struct user_semid_ds
);
1495 struct proc
*p
= current_proc();
1497 /* Copy in the command structure */
1498 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1502 if (!IS_64BIT_PROCESS(p
)) {
1503 ipcs_sz
= sizeof(struct IPCS_command
);
1504 semid_ds_sz
= sizeof(struct semid_ds
);
1507 /* Let us version this interface... */
1508 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1512 SYSV_SEM_SUBSYS_LOCK();
1513 switch(ipcs
.u64
.ipcs_op
) {
1514 case IPCS_SEM_CONF
: /* Obtain global configuration data */
1515 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct seminfo
)) {
1519 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1523 error
= copyout(&seminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1526 case IPCS_SEM_ITER
: /* Iterate over existing segments */
1527 cursor
= ipcs
.u64
.ipcs_cursor
;
1528 if (cursor
< 0 || cursor
>= seminfo
.semmni
) {
1532 if (ipcs
.u64
.ipcs_datalen
!= (int)semid_ds_sz
) {
1536 for( ; cursor
< seminfo
.semmni
; cursor
++) {
1537 if (sema
[cursor
].sem_perm
.mode
& SEM_ALLOC
)
1541 if (cursor
== seminfo
.semmni
) {
1546 semid_dsp
= &sema
[cursor
]; /* default: 64 bit */
1549 * If necessary, convert the 64 bit kernel segment
1550 * descriptor to a 32 bit user one.
1552 if (!IS_64BIT_PROCESS(p
)) {
1553 semid_ds_64to32(semid_dsp
, &semid_ds32
);
1554 semid_dsp
= &semid_ds32
;
1556 error
= copyout(semid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1559 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1560 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1568 SYSV_SEM_SUBSYS_UNLOCK();
1572 SYSCTL_DECL(_kern_sysv_ipcs
);
1573 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, sem
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1574 0, 0, IPCS_sem_sysctl
,
1575 "S,IPCS_sem_command",
1576 "ipcs sem command interface");