2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
33 * posix_sem.c : Support for POSIX semaphore APIs
36 * Author: Ananthakrishna Ramesh
44 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
45 * support for mandatory and extensible security protections. This notice
46 * is included in support of clause 2.2 (b) of the Apple Public License,
50 #include <sys/cdefs.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/file_internal.h>
55 #include <sys/filedesc.h>
57 #include <sys/proc_internal.h>
58 #include <sys/kauth.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/vnode.h>
62 #include <sys/ioctl.h>
64 #include <sys/malloc.h>
65 #include <sys/semaphore.h>
66 #include <sys/sysproto.h>
67 #include <sys/proc_info.h>
70 #include <sys/vnode_internal.h>
71 #include <security/mac_framework.h>
74 #include <security/audit/audit.h>
76 #include <mach/mach_types.h>
77 #include <mach/vm_prot.h>
78 #include <mach/semaphore.h>
79 #include <mach/sync_policy.h>
80 #include <mach/task.h>
81 #include <kern/kern_types.h>
82 #include <kern/task.h>
83 #include <kern/clock.h>
84 #include <mach/kern_return.h>
87 #define f_flag f_fglob->fg_flag
88 #define f_type f_fglob->fg_ops->fo_type
89 #define f_msgcount f_fglob->fg_msgcount
90 #define f_cred f_fglob->fg_cred
91 #define f_ops f_fglob->fg_ops
92 #define f_offset f_fglob->fg_offset
93 #define f_data f_fglob->fg_data
94 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
97 unsigned int psem_flags
;
98 unsigned int psem_usecount
;
102 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
103 semaphore_t psem_semobject
;
104 struct label
* psem_label
;
105 pid_t psem_creator_pid
;
106 uint64_t psem_creator_uniqueid
;
108 #define PSEMINFO_NULL (struct pseminfo *)0
111 #define PSEM_DEFINED 2
112 #define PSEM_ALLOCATED 4
113 #define PSEM_MAPPED 8
114 #define PSEM_INUSE 0x10
115 #define PSEM_REMOVED 0x20
116 #define PSEM_INCREATE 0x40
117 #define PSEM_INDELETE 0x80
120 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
121 struct pseminfo
*pseminfo
; /* vnode the name refers to */
122 int psem_nlen
; /* length of name */
123 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
125 #define PSEMCACHE_NULL (struct psemcache *)0
128 long goodhits
; /* hits that we can really use */
129 long neghits
; /* negative hits that we can use */
130 long badhits
; /* hits we must drop */
131 long falsehits
; /* hits with id mismatch */
132 long miss
; /* misses */
133 long longnames
; /* long names that ignore cache */
137 char *psem_nameptr
; /* pointer to looked up name */
138 long psem_namelen
; /* length of looked up component */
139 u_int32_t psem_hash
; /* hash value of looked up name */
143 struct pseminfo
*pinfo
;
145 unsigned int readcnt
;
146 unsigned int writecnt
;
149 #define PSEMNODE_NULL (struct psemnode *)0
152 #define PSEMHASH(pnp) \
153 (&psemhashtbl[(pnp)->psem_hash & psemhash])
154 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
155 u_long psemhash
; /* size of hash table - 1 */
156 long psemnument
; /* number of cache entries allocated */
157 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
158 /* 10000 limits to ~1M of memory */
159 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Posix");
160 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Semaphores");
161 SYSCTL_LONG (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &posix_sem_max
, "max");
163 struct psemstats psemstats
; /* cache effectiveness statistics */
165 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
166 static int psem_cache_search(struct pseminfo
**,
167 struct psemname
*, struct psemcache
**);
168 static int psem_delete(struct pseminfo
* pinfo
);
170 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
171 int flags
, vfs_context_t ctx
);
172 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
173 int flags
, vfs_context_t ctx
);
174 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
175 caddr_t data
, vfs_context_t ctx
);
176 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
);
177 static int psem_closefile (struct fileglob
*fp
, vfs_context_t ctx
);
179 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
);
181 static const struct fileops psemops
= {
192 static lck_grp_t
*psx_sem_subsys_lck_grp
;
193 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
194 static lck_attr_t
*psx_sem_subsys_lck_attr
;
195 static lck_mtx_t psx_sem_subsys_mutex
;
197 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
198 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
201 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
202 /* Initialize the mutex governing access to the posix sem subsystem */
203 __private_extern__
void
204 psem_lock_init( void )
207 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
209 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
211 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
212 lck_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
216 * Lookup an entry in the cache
219 * status of -1 is returned if matches
220 * If the lookup determines that the name does not exist
221 * (negative cacheing), a status of ENOENT is returned. If the lookup
222 * fails, a status of zero is returned.
226 psem_cache_search(struct pseminfo
**psemp
, struct psemname
*pnp
,
227 struct psemcache
**pcache
)
229 struct psemcache
*pcp
, *nnp
;
230 struct psemhashhead
*pcpp
;
232 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
233 psemstats
.longnames
++;
237 pcpp
= PSEMHASH(pnp
);
238 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
239 nnp
= pcp
->psem_hash
.le_next
;
240 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
241 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
250 /* We found a "positive" match, return the vnode */
252 psemstats
.goodhits
++;
254 *psemp
= pcp
->pseminfo
;
260 * We found a "negative" match, ENOENT notifies client of this match.
261 * The nc_vpid field records whether this is a whiteout.
268 * Add an entry to the cache.
271 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
273 struct psemhashhead
*pcpp
;
274 struct pseminfo
*dpinfo
;
275 struct psemcache
*dpcp
;
278 if (pnp
->psem_namelen
> PSEMNAMLEN
)
279 panic("cache_enter: name too long");
283 /* if the entry has already been added by some one else return */
284 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
287 if (psemnument
>= posix_sem_max
)
291 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
292 * For negative entries, we have to record whether it is a whiteout.
293 * the whiteout flag is stored in the nc_vpid field which is
296 pcp
->pseminfo
= psemp
;
297 pcp
->psem_nlen
= pnp
->psem_namelen
;
298 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
299 pcpp
= PSEMHASH(pnp
);
304 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
306 panic("psem:cache_enter duplicate");
309 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
314 * Name cache initialization, from vfs_init() when we are booting
317 psem_cache_init(void)
319 psemhashtbl
= hashinit(posix_sem_max
/ 2, M_SHM
, &psemhash
);
323 psem_cache_delete(struct psemcache
*pcp
)
326 if (pcp
->psem_hash
.le_prev
== 0)
327 panic("psem namecache purge le_prev");
328 if (pcp
->psem_hash
.le_next
== pcp
)
329 panic("namecache purge le_next");
330 #endif /* DIAGNOSTIC */
331 LIST_REMOVE(pcp
, psem_hash
);
332 pcp
->psem_hash
.le_prev
= NULL
;
338 * Invalidate a all entries to particular vnode.
340 * We actually just increment the v_id, that will do it. The entries will
341 * be purged by lookup as they get found. If the v_id wraps around, we
342 * need to ditch the entire cache, to avoid confusion. No valid vnode will
343 * ever have (v_id == 0).
346 psem_cache_purge(void)
348 struct psemcache
*pcp
;
349 struct psemhashhead
*pcpp
;
351 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
352 while ( (pcp
= pcpp
->lh_first
) )
353 psem_cache_delete(pcp
);
356 #endif /* NOT_USED */
359 sem_open(proc_t p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
364 struct pseminfo
*pinfo
;
365 struct fileproc
*fp
= NULL
;
367 struct pseminfo
*new_pinfo
= PSEMINFO_NULL
;
368 struct psemnode
*new_pnode
= PSEMNODE_NULL
;
369 struct psemcache
*pcache
= PSEMCACHE_NULL
;
372 size_t pathlen
, plen
;
374 int cmode
= uap
->mode
;
375 int value
= uap
->value
;
377 struct psemcache
*pcp
= PSEMCACHE_NULL
;
378 kern_return_t kret
= KERN_INVALID_ADDRESS
; /* default fail */
380 AUDIT_ARG(fflags
, uap
->oflag
);
381 AUDIT_ARG(mode
, uap
->mode
);
382 AUDIT_ARG(value32
, uap
->value
);
384 pinfo
= PSEMINFO_NULL
;
387 * Preallocate everything we might need up front to avoid taking
388 * and dropping the lock, opening us up to race conditions.
390 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
396 pathlen
= MAXPATHLEN
;
397 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
401 AUDIT_ARG(text
, pnbuf
);
402 if ( (pathlen
> PSEMNAMLEN
) ) {
403 error
= ENAMETOOLONG
;
407 #ifdef PSXSEM_NAME_RESTRICT
409 if (*nameptr
== '/') {
410 while (*(nameptr
++) == '/') {
419 #endif /* PSXSEM_NAME_RESTRICT */
423 nd
.psem_nameptr
= nameptr
;
424 nd
.psem_namelen
= plen
;
427 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
428 nd
.psem_hash
+= (unsigned char)*cp
* i
;
432 * attempt to allocate a new fp; if unsuccessful, the fp will be
433 * left unmodified (NULL).
435 error
= falloc(p
, &fp
, &indx
, vfs_context_current());
440 * We allocate a new entry if we are less than the maximum
441 * allowed and the one at the front of the LRU list is in use.
442 * Otherwise we use the one at the front of the LRU list.
444 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
445 if (pcp
== PSEMCACHE_NULL
) {
450 MALLOC(new_pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
451 if (new_pinfo
== NULL
) {
456 mac_posixsem_label_init(new_pinfo
);
460 * Provisionally create the semaphore in the new_pinfo; we have to do
461 * this here to prevent locking later. We use the value of kret to
462 * signal success or failure, which is why we set its default value
463 * to KERN_INVALID_ADDRESS, above.
466 fmode
= FFLAGS(uap
->oflag
);
468 if((fmode
& O_CREAT
)) {
470 if((value
< 0) || (value
> SEM_VALUE_MAX
)) {
475 kret
= semaphore_create(kernel_task
, &new_pinfo
->psem_semobject
, SYNC_POLICY_FIFO
, value
);
477 if (kret
!= KERN_SUCCESS
) {
479 case KERN_RESOURCE_SHORTAGE
:
482 case KERN_PROTECTION_FAILURE
:
492 MALLOC(new_pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
493 if (new_pnode
== NULL
) {
499 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
501 if (error
== ENOENT
) {
513 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
514 /* sem exists and opened O_EXCL */
516 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
519 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
520 pinfo
->psem_gid
, pinfo
->psem_mode
);
524 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
525 /* As per POSIX, O_CREAT has no effect */
529 if ( (fmode
& O_CREAT
) ) {
530 /* create a new one (commit the allocation) */
532 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
533 pinfo
->psem_usecount
= 1;
534 pinfo
->psem_mode
= cmode
;
535 pinfo
->psem_uid
= kauth_getuid();
536 pinfo
->psem_gid
= kauth_getgid();
537 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
538 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
539 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
540 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
541 pinfo
->psem_creator_pid
= p
->p_pid
;
542 pinfo
->psem_creator_uniqueid
= p
->p_uniqueid
;
545 error
= mac_posixsem_check_create(kauth_cred_get(), nameptr
);
549 mac_posixsem_label_associate(kauth_cred_get(), pinfo
, nameptr
);
552 /* semaphore should exist as it is without O_CREAT */
557 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
561 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
562 pinfo
->psem_gid
, pinfo
->psem_mode
);
564 error
= mac_posixsem_check_open(kauth_cred_get(), pinfo
);
569 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
575 /* if successful, this will consume the pcp */
576 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
580 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
581 pinfo
->psem_usecount
++;
582 new_pnode
->pinfo
= pinfo
;
583 PSEM_SUBSYS_UNLOCK();
586 * if incache, we did not use the new pcp or the new pcp or the
587 * new . and we must free them.
591 pcp
= PSEMCACHE_NULL
;
592 if (new_pinfo
!= PSEMINFO_NULL
) {
593 /* return value ignored - we can't _not_ do this */
594 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
596 mac_posixsem_label_destroy(new_pinfo
);
598 FREE(new_pinfo
, M_SHM
);
599 new_pinfo
= PSEMINFO_NULL
;
604 fp
->f_flag
= fmode
& FMASK
;
605 fp
->f_ops
= &psemops
;
606 fp
->f_data
= (caddr_t
)new_pnode
;
607 procfdtbl_releasefd(p
, indx
, NULL
);
608 fp_drop(p
, indx
, fp
, 1);
611 *retval
= CAST_USER_ADDR_T(indx
);
612 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
616 PSEM_SUBSYS_UNLOCK();
618 if (pcp
!= PSEMCACHE_NULL
)
621 if (new_pnode
!= PSEMNODE_NULL
)
622 FREE(new_pnode
, M_SHM
);
625 fp_free(p
, indx
, fp
);
627 if (new_pinfo
!= PSEMINFO_NULL
) {
629 * kret signals whether or not we successfully created a
630 * Mach semaphore for this semaphore; if so, we need to
633 if (kret
== KERN_SUCCESS
) {
634 /* return value ignored - we can't _not_ do this */
635 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
638 mac_posixsem_label_destroy(new_pinfo
);
640 FREE(new_pinfo
, M_SHM
);
644 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
649 * XXX This code is repeated in several places
652 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
654 int mode_req
= ((mode
& FREAD
) ? S_IRUSR
: 0) |
655 ((mode
& FWRITE
) ? S_IWUSR
: 0);
657 /* Otherwise, user id 0 always gets access. */
658 if (!suser(cred
, NULL
))
661 return(posix_cred_access(cred
, pinfo
->psem_uid
, pinfo
->psem_gid
, pinfo
->psem_mode
, mode_req
));
665 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused
int32_t *retval
)
670 struct pseminfo
*pinfo
;
674 size_t pathlen
, plen
;
676 struct psemcache
*pcache
= PSEMCACHE_NULL
;
678 pinfo
= PSEMINFO_NULL
;
680 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
682 return(ENOSPC
); /* XXX non-standard */
684 pathlen
= MAXPATHLEN
;
685 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
689 AUDIT_ARG(text
, pnbuf
);
690 if (pathlen
> PSEMNAMLEN
) {
691 error
= ENAMETOOLONG
;
696 #ifdef PSXSEM_NAME_RESTRICT
698 if (*nameptr
== '/') {
699 while (*(nameptr
++) == '/') {
708 #endif /* PSXSEM_NAME_RESTRICT */
712 nd
.psem_nameptr
= nameptr
;
713 nd
.psem_namelen
= plen
;
716 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
717 nd
.psem_hash
+= (unsigned char)*cp
* i
;
721 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
723 if (error
== ENOENT
) {
724 PSEM_SUBSYS_UNLOCK();
730 PSEM_SUBSYS_UNLOCK();
736 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
738 PSEM_SUBSYS_UNLOCK();
742 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
743 PSEM_SUBSYS_UNLOCK();
747 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
748 PSEM_SUBSYS_UNLOCK();
753 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
754 PSEM_SUBSYS_UNLOCK();
759 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
762 pinfo
->psem_flags
|= PSEM_INDELETE
;
763 pinfo
->psem_usecount
--;
765 if (!pinfo
->psem_usecount
) {
769 pinfo
->psem_flags
|= PSEM_REMOVED
;
771 psem_cache_delete(pcache
);
772 PSEM_SUBSYS_UNLOCK();
776 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
781 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused
int32_t *retval
)
783 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
787 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
790 error
= fp_lookup(p
,fd
, &fp
, 1);
795 procfdtbl_markclosefd(p
, fd
);
796 fileproc_drain(p
, fp
);
798 error
= closef_locked(fp
, fp
->f_fglob
, p
);
805 sem_wait(proc_t p
, struct sem_wait_args
*uap
, int32_t *retval
)
807 __pthread_testcancel(1);
808 return(sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
));
812 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused
int32_t *retval
)
814 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
816 struct pseminfo
* pinfo
;
817 struct psemnode
* pnode
;
821 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
824 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
829 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
830 PSEM_SUBSYS_UNLOCK();
834 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
836 PSEM_SUBSYS_UNLOCK();
841 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
843 PSEM_SUBSYS_UNLOCK();
847 PSEM_SUBSYS_UNLOCK();
848 kret
= semaphore_wait(pinfo
->psem_semobject
);
850 case KERN_INVALID_ADDRESS
:
851 case KERN_PROTECTION_FAILURE
:
855 case KERN_OPERATION_TIMED_OUT
:
866 fp_drop(p
, fd
, fp
, 0);
872 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused
int32_t *retval
)
874 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
876 struct pseminfo
* pinfo
;
877 struct psemnode
* pnode
;
879 mach_timespec_t wait_time
;
882 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
885 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
890 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
891 PSEM_SUBSYS_UNLOCK();
895 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
897 PSEM_SUBSYS_UNLOCK();
902 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
904 PSEM_SUBSYS_UNLOCK();
908 PSEM_SUBSYS_UNLOCK();
909 wait_time
.tv_sec
= 0;
910 wait_time
.tv_nsec
= 0;
912 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
914 case KERN_INVALID_ADDRESS
:
915 case KERN_PROTECTION_FAILURE
:
921 case KERN_OPERATION_TIMED_OUT
:
932 fp_drop(p
, fd
, fp
, 0);
937 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused
int32_t *retval
)
939 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
941 struct pseminfo
* pinfo
;
942 struct psemnode
* pnode
;
946 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
949 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
954 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
955 PSEM_SUBSYS_UNLOCK();
959 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
961 PSEM_SUBSYS_UNLOCK();
966 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
968 PSEM_SUBSYS_UNLOCK();
972 PSEM_SUBSYS_UNLOCK();
973 kret
= semaphore_signal(pinfo
->psem_semobject
);
975 case KERN_INVALID_ADDRESS
:
976 case KERN_PROTECTION_FAILURE
:
980 case KERN_OPERATION_TIMED_OUT
:
991 fp_drop(p
, fd
, fp
, 0);
996 sem_init(__unused proc_t p
, __unused
struct sem_init_args
*uap
, __unused
int32_t *retval
)
1002 sem_destroy(__unused proc_t p
, __unused
struct sem_destroy_args
*uap
, __unused
int32_t *retval
)
1008 sem_getvalue(__unused proc_t p
, __unused
struct sem_getvalue_args
*uap
, __unused
int32_t *retval
)
1014 psem_close(struct psemnode
*pnode
, __unused
int flags
)
1017 struct pseminfo
*pinfo
;
1020 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1021 PSEM_SUBSYS_UNLOCK();
1025 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1026 PSEM_SUBSYS_UNLOCK();
1030 if(!pinfo
->psem_usecount
) {
1031 kprintf("negative usecount in psem_close\n");
1033 #endif /* DIAGNOSTIC */
1034 pinfo
->psem_usecount
--;
1036 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1037 PSEM_SUBSYS_UNLOCK();
1038 /* lock dropped as only semaphore is destroyed here */
1039 error
= psem_delete(pinfo
);
1042 PSEM_SUBSYS_UNLOCK();
1044 /* subsystem lock is dropped when we get here */
1050 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1055 * Not locked as psem_close is called only from here and is locked
1058 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
);
1064 psem_delete(struct pseminfo
* pinfo
)
1068 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1070 mac_posixsem_label_destroy(pinfo
);
1074 case KERN_INVALID_ADDRESS
:
1075 case KERN_PROTECTION_FAILURE
:
1078 case KERN_OPERATION_TIMED_OUT
:
1088 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1089 __unused
int flags
, __unused vfs_context_t ctx
)
1095 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1096 __unused
int flags
, __unused vfs_context_t ctx
)
1102 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1103 __unused caddr_t data
, __unused vfs_context_t ctx
)
1109 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1110 __unused
void *wql
, __unused vfs_context_t ctx
)
1116 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1117 __unused vfs_context_t ctx
)
1123 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1125 struct pseminfo
*pinfo
;
1126 struct vinfo_stat
*sb
;
1129 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1130 PSEM_SUBSYS_UNLOCK();
1135 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1136 PSEM_SUBSYS_UNLOCK();
1141 sb
= &info
->psem_stat
;
1142 bzero(sb
, sizeof(struct vinfo_stat
));
1144 sb
->vst_mode
= pinfo
->psem_mode
;
1145 sb
->vst_uid
= pinfo
->psem_uid
;
1146 sb
->vst_gid
= pinfo
->psem_gid
;
1147 sb
->vst_size
= pinfo
->psem_usecount
;
1148 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1150 PSEM_SUBSYS_UNLOCK();
1156 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1158 struct psemnode
*pnode
;
1159 struct pseminfo
*psem
;
1162 pnode
= (struct psemnode
*)fp
->f_fglob
->fg_data
;
1163 if (pnode
!= NULL
) {
1164 psem
= pnode
->pinfo
;
1166 mac_posixsem_vnode_label_associate(
1167 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1170 PSEM_SUBSYS_UNLOCK();