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_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
;
105 struct label
* psem_label
;
107 #define PSEMINFO_NULL (struct pseminfo *)0
110 #define PSEM_DEFINED 2
111 #define PSEM_ALLOCATED 4
112 #define PSEM_MAPPED 8
113 #define PSEM_INUSE 0x10
114 #define PSEM_REMOVED 0x20
115 #define PSEM_INCREATE 0x40
116 #define PSEM_INDELETE 0x80
119 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
120 struct pseminfo
*pseminfo
; /* vnode the name refers to */
121 int psem_nlen
; /* length of name */
122 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
124 #define PSEMCACHE_NULL (struct psemcache *)0
127 long goodhits
; /* hits that we can really use */
128 long neghits
; /* negative hits that we can use */
129 long badhits
; /* hits we must drop */
130 long falsehits
; /* hits with id mismatch */
131 long miss
; /* misses */
132 long longnames
; /* long names that ignore cache */
136 char *psem_nameptr
; /* pointer to looked up name */
137 long psem_namelen
; /* length of looked up component */
138 u_int32_t psem_hash
; /* hash value of looked up name */
142 struct pseminfo
*pinfo
;
144 unsigned int readcnt
;
145 unsigned int writecnt
;
148 #define PSEMNODE_NULL (struct psemnode *)0
151 #define PSEMHASH(pnp) \
152 (&psemhashtbl[(pnp)->psem_hash & psemhash])
153 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
154 u_long psemhash
; /* size of hash table - 1 */
155 long psemnument
; /* number of cache entries allocated */
156 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
157 /* 10000 limits to ~1M of memory */
158 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Posix");
159 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Semaphores");
160 SYSCTL_LONG (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &posix_sem_max
, "max");
162 struct psemstats psemstats
; /* cache effectiveness statistics */
164 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
165 static int psem_cache_search(struct pseminfo
**,
166 struct psemname
*, struct psemcache
**);
167 static int psem_delete(struct pseminfo
* pinfo
);
169 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
170 int flags
, vfs_context_t ctx
);
171 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
172 int flags
, vfs_context_t ctx
);
173 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
174 caddr_t data
, vfs_context_t ctx
);
175 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
);
176 static int psem_closefile (struct fileglob
*fp
, vfs_context_t ctx
);
178 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
);
180 struct fileops psemops
=
181 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, NULL
};
184 static lck_grp_t
*psx_sem_subsys_lck_grp
;
185 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
186 static lck_attr_t
*psx_sem_subsys_lck_attr
;
187 static lck_mtx_t psx_sem_subsys_mutex
;
189 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
190 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
193 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
194 /* Initialize the mutex governing access to the posix sem subsystem */
195 __private_extern__
void
196 psem_lock_init( void )
199 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
201 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
203 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
204 lck_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
208 * Lookup an entry in the cache
211 * status of -1 is returned if matches
212 * If the lookup determines that the name does not exist
213 * (negative cacheing), a status of ENOENT is returned. If the lookup
214 * fails, a status of zero is returned.
218 psem_cache_search(struct pseminfo
**psemp
, struct psemname
*pnp
,
219 struct psemcache
**pcache
)
221 struct psemcache
*pcp
, *nnp
;
222 struct psemhashhead
*pcpp
;
224 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
225 psemstats
.longnames
++;
229 pcpp
= PSEMHASH(pnp
);
230 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
231 nnp
= pcp
->psem_hash
.le_next
;
232 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
233 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
242 /* We found a "positive" match, return the vnode */
244 psemstats
.goodhits
++;
246 *psemp
= pcp
->pseminfo
;
252 * We found a "negative" match, ENOENT notifies client of this match.
253 * The nc_vpid field records whether this is a whiteout.
260 * Add an entry to the cache.
263 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
265 struct psemhashhead
*pcpp
;
266 struct pseminfo
*dpinfo
;
267 struct psemcache
*dpcp
;
270 if (pnp
->psem_namelen
> PSEMNAMLEN
)
271 panic("cache_enter: name too long");
275 /* if the entry has already been added by some one else return */
276 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
279 if (psemnument
>= posix_sem_max
)
283 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
284 * For negative entries, we have to record whether it is a whiteout.
285 * the whiteout flag is stored in the nc_vpid field which is
288 pcp
->pseminfo
= psemp
;
289 pcp
->psem_nlen
= pnp
->psem_namelen
;
290 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
291 pcpp
= PSEMHASH(pnp
);
296 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
298 panic("psem:cache_enter duplicate");
301 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
306 * Name cache initialization, from vfs_init() when we are booting
309 psem_cache_init(void)
311 psemhashtbl
= hashinit(posix_sem_max
/ 2, M_SHM
, &psemhash
);
315 psem_cache_delete(struct psemcache
*pcp
)
318 if (pcp
->psem_hash
.le_prev
== 0)
319 panic("psem namecache purge le_prev");
320 if (pcp
->psem_hash
.le_next
== pcp
)
321 panic("namecache purge le_next");
322 #endif /* DIAGNOSTIC */
323 LIST_REMOVE(pcp
, psem_hash
);
324 pcp
->psem_hash
.le_prev
= NULL
;
330 * Invalidate a all entries to particular vnode.
332 * We actually just increment the v_id, that will do it. The entries will
333 * be purged by lookup as they get found. If the v_id wraps around, we
334 * need to ditch the entire cache, to avoid confusion. No valid vnode will
335 * ever have (v_id == 0).
338 psem_cache_purge(void)
340 struct psemcache
*pcp
;
341 struct psemhashhead
*pcpp
;
343 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
344 while ( (pcp
= pcpp
->lh_first
) )
345 psem_cache_delete(pcp
);
348 #endif /* NOT_USED */
351 sem_open(proc_t p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
356 struct pseminfo
*pinfo
;
357 struct fileproc
*fp
= NULL
;
359 struct pseminfo
*new_pinfo
= PSEMINFO_NULL
;
360 struct psemnode
*new_pnode
= PSEMNODE_NULL
;
361 struct psemcache
*pcache
= PSEMCACHE_NULL
;
364 size_t pathlen
, plen
;
366 int cmode
= uap
->mode
;
367 int value
= uap
->value
;
369 struct psemcache
*pcp
= PSEMCACHE_NULL
;
370 kern_return_t kret
= KERN_INVALID_ADDRESS
; /* default fail */
372 AUDIT_ARG(fflags
, uap
->oflag
);
373 AUDIT_ARG(mode
, uap
->mode
);
374 AUDIT_ARG(value32
, uap
->value
);
376 pinfo
= PSEMINFO_NULL
;
379 * Preallocate everything we might need up front to avoid taking
380 * and dropping the lock, opening us up to race conditions.
382 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
388 pathlen
= MAXPATHLEN
;
389 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
393 AUDIT_ARG(text
, pnbuf
);
394 if ( (pathlen
> PSEMNAMLEN
) ) {
395 error
= ENAMETOOLONG
;
399 #ifdef PSXSEM_NAME_RESTRICT
401 if (*nameptr
== '/') {
402 while (*(nameptr
++) == '/') {
411 #endif /* PSXSEM_NAME_RESTRICT */
415 nd
.psem_nameptr
= nameptr
;
416 nd
.psem_namelen
= plen
;
419 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
420 nd
.psem_hash
+= (unsigned char)*cp
* i
;
424 * attempt to allocate a new fp; if unsuccessful, the fp will be
425 * left unmodified (NULL).
427 error
= falloc(p
, &fp
, &indx
, vfs_context_current());
432 * We allocate a new entry if we are less than the maximum
433 * allowed and the one at the front of the LRU list is in use.
434 * Otherwise we use the one at the front of the LRU list.
436 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
437 if (pcp
== PSEMCACHE_NULL
) {
442 MALLOC(new_pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
443 if (new_pinfo
== NULL
) {
448 mac_posixsem_label_init(new_pinfo
);
452 * Provisionally create the semaphore in the new_pinfo; we have to do
453 * this here to prevent locking later. We use the value of kret to
454 * signal success or failure, which is why we set its default value
455 * to KERN_INVALID_ADDRESS, above.
458 fmode
= FFLAGS(uap
->oflag
);
460 if((fmode
& O_CREAT
)) {
462 if((value
< 0) || (value
> SEM_VALUE_MAX
)) {
467 kret
= semaphore_create(kernel_task
, &new_pinfo
->psem_semobject
, SYNC_POLICY_FIFO
, value
);
469 if (kret
!= KERN_SUCCESS
) {
471 case KERN_RESOURCE_SHORTAGE
:
474 case KERN_PROTECTION_FAILURE
:
484 MALLOC(new_pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
485 if (new_pnode
== NULL
) {
491 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
493 if (error
== ENOENT
) {
505 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
506 /* sem exists and opened O_EXCL */
508 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
511 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
512 pinfo
->psem_gid
, pinfo
->psem_mode
);
516 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
517 /* As per POSIX, O_CREAT has no effect */
521 if ( (fmode
& O_CREAT
) ) {
522 /* create a new one (commit the allocation) */
524 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
525 pinfo
->psem_usecount
= 1;
526 pinfo
->psem_mode
= cmode
;
527 pinfo
->psem_uid
= kauth_getuid();
528 pinfo
->psem_gid
= kauth_getgid();
529 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
530 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
531 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
532 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
536 error
= mac_posixsem_check_create(kauth_cred_get(), nameptr
);
540 mac_posixsem_label_associate(kauth_cred_get(), pinfo
, nameptr
);
543 /* semaphore should exist as it is without O_CREAT */
548 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
552 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
553 pinfo
->psem_gid
, pinfo
->psem_mode
);
555 error
= mac_posixsem_check_open(kauth_cred_get(), pinfo
);
560 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
566 /* if successful, this will consume the pcp */
567 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
571 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
572 pinfo
->psem_usecount
++;
573 new_pnode
->pinfo
= pinfo
;
574 PSEM_SUBSYS_UNLOCK();
577 * if incache, we did not use the new pcp or the new pcp or the
578 * new . and we must free them.
582 pcp
= PSEMCACHE_NULL
;
583 if (new_pinfo
!= PSEMINFO_NULL
) {
584 /* return value ignored - we can't _not_ do this */
585 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
587 mac_posixsem_label_destroy(new_pinfo
);
589 FREE(new_pinfo
, M_SHM
);
590 new_pinfo
= PSEMINFO_NULL
;
595 fp
->f_flag
= fmode
& FMASK
;
596 fp
->f_type
= DTYPE_PSXSEM
;
597 fp
->f_ops
= &psemops
;
598 fp
->f_data
= (caddr_t
)new_pnode
;
599 procfdtbl_releasefd(p
, indx
, NULL
);
600 fp_drop(p
, indx
, fp
, 1);
603 *retval
= CAST_USER_ADDR_T(indx
);
604 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
608 PSEM_SUBSYS_UNLOCK();
610 if (pcp
!= PSEMCACHE_NULL
)
613 if (new_pnode
!= PSEMNODE_NULL
)
614 FREE(new_pnode
, M_SHM
);
617 fp_free(p
, indx
, fp
);
619 if (new_pinfo
!= PSEMINFO_NULL
) {
621 * kret signals whether or not we successfully created a
622 * Mach semaphore for this semaphore; if so, we need to
625 if (kret
== KERN_SUCCESS
) {
626 /* return value ignored - we can't _not_ do this */
627 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
630 mac_posixsem_label_destroy(new_pinfo
);
632 FREE(new_pinfo
, M_SHM
);
636 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
641 * XXX This code is repeated in several places
644 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
646 int mode_req
= ((mode
& FREAD
) ? S_IRUSR
: 0) |
647 ((mode
& FWRITE
) ? S_IWUSR
: 0);
649 /* Otherwise, user id 0 always gets access. */
650 if (!suser(cred
, NULL
))
653 return(posix_cred_access(cred
, pinfo
->psem_uid
, pinfo
->psem_gid
, pinfo
->psem_mode
, mode_req
));
657 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused
int32_t *retval
)
662 struct pseminfo
*pinfo
;
666 size_t pathlen
, plen
;
668 struct psemcache
*pcache
= PSEMCACHE_NULL
;
670 pinfo
= PSEMINFO_NULL
;
672 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
674 return(ENOSPC
); /* XXX non-standard */
676 pathlen
= MAXPATHLEN
;
677 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
681 AUDIT_ARG(text
, pnbuf
);
682 if (pathlen
> PSEMNAMLEN
) {
683 error
= ENAMETOOLONG
;
688 #ifdef PSXSEM_NAME_RESTRICT
690 if (*nameptr
== '/') {
691 while (*(nameptr
++) == '/') {
700 #endif /* PSXSEM_NAME_RESTRICT */
704 nd
.psem_nameptr
= nameptr
;
705 nd
.psem_namelen
= plen
;
708 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
709 nd
.psem_hash
+= (unsigned char)*cp
* i
;
713 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
715 if (error
== ENOENT
) {
716 PSEM_SUBSYS_UNLOCK();
722 PSEM_SUBSYS_UNLOCK();
728 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
730 PSEM_SUBSYS_UNLOCK();
734 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
735 PSEM_SUBSYS_UNLOCK();
739 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
740 PSEM_SUBSYS_UNLOCK();
745 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
746 PSEM_SUBSYS_UNLOCK();
751 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
754 pinfo
->psem_flags
|= PSEM_INDELETE
;
755 pinfo
->psem_usecount
--;
757 if (!pinfo
->psem_usecount
) {
761 pinfo
->psem_flags
|= PSEM_REMOVED
;
763 psem_cache_delete(pcache
);
764 PSEM_SUBSYS_UNLOCK();
768 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
773 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused
int32_t *retval
)
775 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
779 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
782 error
= fp_lookup(p
,fd
, &fp
, 1);
787 procfdtbl_markclosefd(p
, fd
);
788 fileproc_drain(p
, fp
);
790 error
= closef_locked(fp
, fp
->f_fglob
, p
);
791 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
797 sem_wait(proc_t p
, struct sem_wait_args
*uap
, int32_t *retval
)
799 __pthread_testcancel(1);
800 return(sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
));
804 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused
int32_t *retval
)
806 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
808 struct pseminfo
* pinfo
;
809 struct psemnode
* pnode
;
813 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
816 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
821 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
822 PSEM_SUBSYS_UNLOCK();
826 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
828 PSEM_SUBSYS_UNLOCK();
833 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
835 PSEM_SUBSYS_UNLOCK();
839 PSEM_SUBSYS_UNLOCK();
840 kret
= semaphore_wait(pinfo
->psem_semobject
);
842 case KERN_INVALID_ADDRESS
:
843 case KERN_PROTECTION_FAILURE
:
847 case KERN_OPERATION_TIMED_OUT
:
858 fp_drop(p
, fd
, fp
, 0);
864 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused
int32_t *retval
)
866 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
868 struct pseminfo
* pinfo
;
869 struct psemnode
* pnode
;
871 mach_timespec_t wait_time
;
874 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
877 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
882 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
883 PSEM_SUBSYS_UNLOCK();
887 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
889 PSEM_SUBSYS_UNLOCK();
894 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
896 PSEM_SUBSYS_UNLOCK();
900 PSEM_SUBSYS_UNLOCK();
901 wait_time
.tv_sec
= 0;
902 wait_time
.tv_nsec
= 0;
904 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
906 case KERN_INVALID_ADDRESS
:
907 case KERN_PROTECTION_FAILURE
:
913 case KERN_OPERATION_TIMED_OUT
:
924 fp_drop(p
, fd
, fp
, 0);
929 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused
int32_t *retval
)
931 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
933 struct pseminfo
* pinfo
;
934 struct psemnode
* pnode
;
938 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
941 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
946 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
947 PSEM_SUBSYS_UNLOCK();
951 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
953 PSEM_SUBSYS_UNLOCK();
958 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
960 PSEM_SUBSYS_UNLOCK();
964 PSEM_SUBSYS_UNLOCK();
965 kret
= semaphore_signal(pinfo
->psem_semobject
);
967 case KERN_INVALID_ADDRESS
:
968 case KERN_PROTECTION_FAILURE
:
972 case KERN_OPERATION_TIMED_OUT
:
983 fp_drop(p
, fd
, fp
, 0);
988 sem_init(__unused proc_t p
, __unused
struct sem_init_args
*uap
, __unused
int32_t *retval
)
994 sem_destroy(__unused proc_t p
, __unused
struct sem_destroy_args
*uap
, __unused
int32_t *retval
)
1000 sem_getvalue(__unused proc_t p
, __unused
struct sem_getvalue_args
*uap
, __unused
int32_t *retval
)
1006 psem_close(struct psemnode
*pnode
, __unused
int flags
)
1009 struct pseminfo
*pinfo
;
1012 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1013 PSEM_SUBSYS_UNLOCK();
1017 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1018 PSEM_SUBSYS_UNLOCK();
1022 if(!pinfo
->psem_usecount
) {
1023 kprintf("negative usecount in psem_close\n");
1025 #endif /* DIAGNOSTIC */
1026 pinfo
->psem_usecount
--;
1028 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1029 PSEM_SUBSYS_UNLOCK();
1030 /* lock dropped as only semaphore is destroyed here */
1031 error
= psem_delete(pinfo
);
1034 PSEM_SUBSYS_UNLOCK();
1036 /* subsystem lock is dropped when we get here */
1042 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1047 * Not locked as psem_close is called only from here and is locked
1050 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
);
1056 psem_delete(struct pseminfo
* pinfo
)
1060 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1062 mac_posixsem_label_destroy(pinfo
);
1066 case KERN_INVALID_ADDRESS
:
1067 case KERN_PROTECTION_FAILURE
:
1070 case KERN_OPERATION_TIMED_OUT
:
1080 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1081 __unused
int flags
, __unused vfs_context_t ctx
)
1087 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1088 __unused
int flags
, __unused vfs_context_t ctx
)
1094 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1095 __unused caddr_t data
, __unused vfs_context_t ctx
)
1101 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1102 __unused
void *wql
, __unused vfs_context_t ctx
)
1108 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1109 __unused vfs_context_t ctx
)
1115 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1117 struct pseminfo
*pinfo
;
1118 struct vinfo_stat
*sb
;
1121 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1122 PSEM_SUBSYS_UNLOCK();
1127 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1128 PSEM_SUBSYS_UNLOCK();
1133 sb
= &info
->psem_stat
;
1134 bzero(sb
, sizeof(struct vinfo_stat
));
1136 sb
->vst_mode
= pinfo
->psem_mode
;
1137 sb
->vst_uid
= pinfo
->psem_uid
;
1138 sb
->vst_gid
= pinfo
->psem_gid
;
1139 sb
->vst_size
= pinfo
->psem_usecount
;
1140 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1142 PSEM_SUBSYS_UNLOCK();
1148 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1150 struct psemnode
*pnode
;
1151 struct pseminfo
*psem
;
1154 pnode
= (struct psemnode
*)fp
->f_fglob
->fg_data
;
1155 if (pnode
!= NULL
) {
1156 psem
= pnode
->pinfo
;
1158 mac_posixsem_vnode_label_associate(
1159 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1162 PSEM_SUBSYS_UNLOCK();