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_shm.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
, &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_cred_getuid(kauth_cred_get());
528 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
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
)
649 /* Otherwise, user id 0 always gets access. */
650 if (!suser(cred
, NULL
))
655 /* Otherwise, check the owner. */
656 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
661 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
664 /* Otherwise, check the groups. */
665 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
670 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
673 /* Otherwise, check everyone else. */
678 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
682 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused
int32_t *retval
)
687 struct pseminfo
*pinfo
;
691 size_t pathlen
, plen
;
693 struct psemcache
*pcache
= PSEMCACHE_NULL
;
695 pinfo
= PSEMINFO_NULL
;
697 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
699 return(ENOSPC
); /* XXX non-standard */
701 pathlen
= MAXPATHLEN
;
702 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
706 AUDIT_ARG(text
, pnbuf
);
707 if (pathlen
> PSEMNAMLEN
) {
708 error
= ENAMETOOLONG
;
713 #ifdef PSXSEM_NAME_RESTRICT
715 if (*nameptr
== '/') {
716 while (*(nameptr
++) == '/') {
725 #endif /* PSXSEM_NAME_RESTRICT */
729 nd
.psem_nameptr
= nameptr
;
730 nd
.psem_namelen
= plen
;
733 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
734 nd
.psem_hash
+= (unsigned char)*cp
* i
;
738 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
740 if (error
== ENOENT
) {
741 PSEM_SUBSYS_UNLOCK();
747 PSEM_SUBSYS_UNLOCK();
753 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
755 PSEM_SUBSYS_UNLOCK();
759 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
760 PSEM_SUBSYS_UNLOCK();
764 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
765 PSEM_SUBSYS_UNLOCK();
770 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
771 PSEM_SUBSYS_UNLOCK();
776 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
779 pinfo
->psem_flags
|= PSEM_INDELETE
;
780 pinfo
->psem_usecount
--;
782 if (!pinfo
->psem_usecount
) {
786 pinfo
->psem_flags
|= PSEM_REMOVED
;
788 psem_cache_delete(pcache
);
789 PSEM_SUBSYS_UNLOCK();
793 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
798 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused
int32_t *retval
)
800 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
804 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
807 error
= fp_lookup(p
,fd
, &fp
, 1);
812 fileproc_drain(p
, fp
);
814 error
= closef_locked(fp
, fp
->f_fglob
, p
);
815 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
821 sem_wait(proc_t p
, struct sem_wait_args
*uap
, int32_t *retval
)
823 __pthread_testcancel(1);
824 return(sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
));
828 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused
int32_t *retval
)
830 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
832 struct pseminfo
* pinfo
;
833 struct psemnode
* pnode
;
837 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
840 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
845 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
846 PSEM_SUBSYS_UNLOCK();
850 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
852 PSEM_SUBSYS_UNLOCK();
857 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
859 PSEM_SUBSYS_UNLOCK();
863 PSEM_SUBSYS_UNLOCK();
864 kret
= semaphore_wait(pinfo
->psem_semobject
);
866 case KERN_INVALID_ADDRESS
:
867 case KERN_PROTECTION_FAILURE
:
871 case KERN_OPERATION_TIMED_OUT
:
882 fp_drop(p
, fd
, fp
, 0);
888 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused
int32_t *retval
)
890 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
892 struct pseminfo
* pinfo
;
893 struct psemnode
* pnode
;
895 mach_timespec_t wait_time
;
898 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
901 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
906 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
907 PSEM_SUBSYS_UNLOCK();
911 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
913 PSEM_SUBSYS_UNLOCK();
918 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
920 PSEM_SUBSYS_UNLOCK();
924 PSEM_SUBSYS_UNLOCK();
925 wait_time
.tv_sec
= 0;
926 wait_time
.tv_nsec
= 0;
928 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
930 case KERN_INVALID_ADDRESS
:
931 case KERN_PROTECTION_FAILURE
:
937 case KERN_OPERATION_TIMED_OUT
:
948 fp_drop(p
, fd
, fp
, 0);
953 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused
int32_t *retval
)
955 int fd
= CAST_DOWN_EXPLICIT(int,uap
->sem
);
957 struct pseminfo
* pinfo
;
958 struct psemnode
* pnode
;
962 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
965 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
970 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
971 PSEM_SUBSYS_UNLOCK();
975 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
977 PSEM_SUBSYS_UNLOCK();
982 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
984 PSEM_SUBSYS_UNLOCK();
988 PSEM_SUBSYS_UNLOCK();
989 kret
= semaphore_signal(pinfo
->psem_semobject
);
991 case KERN_INVALID_ADDRESS
:
992 case KERN_PROTECTION_FAILURE
:
996 case KERN_OPERATION_TIMED_OUT
:
1007 fp_drop(p
, fd
, fp
, 0);
1012 sem_init(__unused proc_t p
, __unused
struct sem_init_args
*uap
, __unused
int32_t *retval
)
1018 sem_destroy(__unused proc_t p
, __unused
struct sem_destroy_args
*uap
, __unused
int32_t *retval
)
1024 sem_getvalue(__unused proc_t p
, __unused
struct sem_getvalue_args
*uap
, __unused
int32_t *retval
)
1030 psem_close(struct psemnode
*pnode
, __unused
int flags
)
1033 struct pseminfo
*pinfo
;
1036 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1037 PSEM_SUBSYS_UNLOCK();
1041 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1042 PSEM_SUBSYS_UNLOCK();
1046 if(!pinfo
->psem_usecount
) {
1047 kprintf("negative usecount in psem_close\n");
1049 #endif /* DIAGNOSTIC */
1050 pinfo
->psem_usecount
--;
1052 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1053 PSEM_SUBSYS_UNLOCK();
1054 /* lock dropped as only semaphore is destroyed here */
1055 error
= psem_delete(pinfo
);
1058 PSEM_SUBSYS_UNLOCK();
1060 /* subsystem lock is dropped when we get here */
1066 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1071 * Not locked as psem_close is called only from here and is locked
1074 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
);
1080 psem_delete(struct pseminfo
* pinfo
)
1084 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1086 mac_posixsem_label_destroy(pinfo
);
1090 case KERN_INVALID_ADDRESS
:
1091 case KERN_PROTECTION_FAILURE
:
1094 case KERN_OPERATION_TIMED_OUT
:
1104 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1105 __unused
int flags
, __unused vfs_context_t ctx
)
1111 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1112 __unused
int flags
, __unused vfs_context_t ctx
)
1118 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1119 __unused caddr_t data
, __unused vfs_context_t ctx
)
1125 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1126 __unused
void *wql
, __unused vfs_context_t ctx
)
1132 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1133 __unused vfs_context_t ctx
)
1139 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1141 struct pseminfo
*pinfo
;
1142 struct vinfo_stat
*sb
;
1145 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1146 PSEM_SUBSYS_UNLOCK();
1151 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1152 PSEM_SUBSYS_UNLOCK();
1157 sb
= &info
->psem_stat
;
1158 bzero(sb
, sizeof(struct vinfo_stat
));
1160 sb
->vst_mode
= pinfo
->psem_mode
;
1161 sb
->vst_uid
= pinfo
->psem_uid
;
1162 sb
->vst_gid
= pinfo
->psem_gid
;
1163 sb
->vst_size
= pinfo
->psem_usecount
;
1164 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1166 PSEM_SUBSYS_UNLOCK();
1172 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1174 struct psemnode
*pnode
;
1175 struct pseminfo
*psem
;
1178 pnode
= (struct psemnode
*)fp
->f_fglob
->fg_data
;
1179 if (pnode
!= NULL
) {
1180 psem
= pnode
->pinfo
;
1182 mac_posixsem_vnode_label_associate(
1183 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1186 PSEM_SUBSYS_UNLOCK();