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 <bsm/audit_kernel.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_long 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_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "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
)
355 struct fileproc
*nfp
;
358 struct pseminfo
*pinfo
;
359 struct psemcache
*pcp
= NULL
; /* protected by !incache */
363 size_t pathlen
, plen
;
365 int cmode
= uap
->mode
;
366 int value
= uap
->value
;
368 struct psemnode
* pnode
= PSEMNODE_NULL
;
369 struct psemcache
* pcache
= PSEMCACHE_NULL
;
370 kern_return_t kret
= KERN_SUCCESS
;
373 AUDIT_ARG(fflags
, uap
->oflag
);
374 AUDIT_ARG(mode
, uap
->mode
);
375 AUDIT_ARG(value
, uap
->value
);
377 pinfo
= PSEMINFO_NULL
;
379 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
383 pathlen
= MAXPATHLEN
;
384 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
388 AUDIT_ARG(text
, pnbuf
);
389 if ( (pathlen
> PSEMNAMLEN
) ) {
390 error
= ENAMETOOLONG
;
394 #ifdef PSXSEM_NAME_RESTRICT
396 if (*nameptr
== '/') {
397 while (*(nameptr
++) == '/') {
406 #endif /* PSXSEM_NAME_RESTRICT */
410 nd
.psem_nameptr
= nameptr
;
411 nd
.psem_namelen
= plen
;
414 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
415 nd
.psem_hash
+= (unsigned char)*cp
* i
;
419 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
421 if (error
== ENOENT
) {
422 PSEM_SUBSYS_UNLOCK();
431 fmode
= FFLAGS(uap
->oflag
);
433 PSEM_SUBSYS_UNLOCK();
434 error
= falloc(p
, &nfp
, &indx
, vfs_context_current());
442 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
443 /* sem exists and opened O_EXCL */
445 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
448 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
449 pinfo
->psem_gid
, pinfo
->psem_mode
);
450 PSEM_SUBSYS_UNLOCK();
454 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
455 /* As per POSIX, O_CREAT has no effect */
459 if ( (fmode
& O_CREAT
) ) {
460 if((value
< 0) || (value
> SEM_VALUE_MAX
)) {
461 PSEM_SUBSYS_UNLOCK();
465 PSEM_SUBSYS_UNLOCK();
466 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
472 mac_posixsem_label_init(pinfo
);
477 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
478 pinfo
->psem_usecount
= 1;
479 pinfo
->psem_mode
= cmode
;
480 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
481 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
482 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
483 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
484 PSEM_SUBSYS_UNLOCK();
485 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
486 SYNC_POLICY_FIFO
, value
);
487 if(kret
!= KERN_SUCCESS
)
490 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
491 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
494 error
= mac_posixsem_check_create(kauth_cred_get(), nameptr
);
496 PSEM_SUBSYS_UNLOCK();
499 mac_posixsem_label_associate(kauth_cred_get(), pinfo
, nameptr
);
502 /* semaphore should exist as it is without O_CREAT */
504 PSEM_SUBSYS_UNLOCK();
508 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
509 PSEM_SUBSYS_UNLOCK();
513 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
514 pinfo
->psem_gid
, pinfo
->psem_mode
);
516 error
= mac_posixsem_check_open(kauth_cred_get(), pinfo
);
518 PSEM_SUBSYS_UNLOCK();
522 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
523 PSEM_SUBSYS_UNLOCK();
527 PSEM_SUBSYS_UNLOCK();
528 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
535 * We allocate a new entry if we are less than the maximum
536 * allowed and the one at the front of the LRU list is in use.
537 * Otherwise we use the one at the front of the LRU list.
539 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
548 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
549 PSEM_SUBSYS_UNLOCK();
554 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
555 pinfo
->psem_usecount
++;
556 pnode
->pinfo
= pinfo
;
557 PSEM_SUBSYS_UNLOCK();
560 fp
->f_flag
= fmode
& FMASK
;
561 fp
->f_type
= DTYPE_PSXSEM
;
562 fp
->f_ops
= &psemops
;
563 fp
->f_data
= (caddr_t
)pnode
;
564 procfdtbl_releasefd(p
, indx
, NULL
);
565 fp_drop(p
, indx
, fp
, 1);
568 *retval
= CAST_USER_ADDR_T(indx
);
569 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
574 case KERN_RESOURCE_SHORTAGE
:
577 case KERN_PROTECTION_FAILURE
:
589 mac_posixsem_label_destroy(pinfo
);
593 fp_free(p
, indx
, nfp
);
595 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
600 * XXX This code is repeated in several places
603 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
608 /* Otherwise, user id 0 always gets access. */
609 if (!suser(cred
, NULL
))
614 /* Otherwise, check the owner. */
615 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
620 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
623 /* Otherwise, check the groups. */
624 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
629 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
632 /* Otherwise, check everyone else. */
637 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
641 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
646 struct pseminfo
*pinfo
;
650 size_t pathlen
, plen
;
652 struct psemcache
*pcache
= PSEMCACHE_NULL
;
654 pinfo
= PSEMINFO_NULL
;
656 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
658 return(ENOSPC
); /* XXX non-standard */
660 pathlen
= MAXPATHLEN
;
661 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
665 AUDIT_ARG(text
, pnbuf
);
666 if (pathlen
> PSEMNAMLEN
) {
667 error
= ENAMETOOLONG
;
672 #ifdef PSXSEM_NAME_RESTRICT
674 if (*nameptr
== '/') {
675 while (*(nameptr
++) == '/') {
684 #endif /* PSXSEM_NAME_RESTRICT */
688 nd
.psem_nameptr
= nameptr
;
689 nd
.psem_namelen
= plen
;
692 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
693 nd
.psem_hash
+= (unsigned char)*cp
* i
;
697 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
699 if (error
== ENOENT
) {
700 PSEM_SUBSYS_UNLOCK();
706 PSEM_SUBSYS_UNLOCK();
712 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
714 PSEM_SUBSYS_UNLOCK();
718 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
719 PSEM_SUBSYS_UNLOCK();
723 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
724 PSEM_SUBSYS_UNLOCK();
729 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
730 PSEM_SUBSYS_UNLOCK();
735 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
738 pinfo
->psem_flags
|= PSEM_INDELETE
;
739 pinfo
->psem_usecount
--;
741 if (!pinfo
->psem_usecount
) {
745 pinfo
->psem_flags
|= PSEM_REMOVED
;
747 psem_cache_delete(pcache
);
748 PSEM_SUBSYS_UNLOCK();
752 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
757 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
759 int fd
= CAST_DOWN(int,uap
->sem
);
763 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
766 error
= fp_lookup(p
,fd
, &fp
, 1);
771 fileproc_drain(p
, fp
);
773 error
= closef_locked(fp
, fp
->f_fglob
, p
);
774 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
780 sem_wait(proc_t p
, struct sem_wait_args
*uap
, register_t
*retval
)
782 __pthread_testcancel(1);
783 return(sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
));
787 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused register_t
*retval
)
789 int fd
= CAST_DOWN(int,uap
->sem
);
791 struct pseminfo
* pinfo
;
792 struct psemnode
* pnode
;
796 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
799 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
804 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
805 PSEM_SUBSYS_UNLOCK();
809 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
811 PSEM_SUBSYS_UNLOCK();
816 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
818 PSEM_SUBSYS_UNLOCK();
822 PSEM_SUBSYS_UNLOCK();
823 kret
= semaphore_wait(pinfo
->psem_semobject
);
825 case KERN_INVALID_ADDRESS
:
826 case KERN_PROTECTION_FAILURE
:
830 case KERN_OPERATION_TIMED_OUT
:
841 fp_drop(p
, fd
, fp
, 0);
847 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
849 int fd
= CAST_DOWN(int,uap
->sem
);
851 struct pseminfo
* pinfo
;
852 struct psemnode
* pnode
;
854 mach_timespec_t wait_time
;
857 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
860 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
865 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
866 PSEM_SUBSYS_UNLOCK();
870 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
872 PSEM_SUBSYS_UNLOCK();
877 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
879 PSEM_SUBSYS_UNLOCK();
883 PSEM_SUBSYS_UNLOCK();
884 wait_time
.tv_sec
= 0;
885 wait_time
.tv_nsec
= 0;
887 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
889 case KERN_INVALID_ADDRESS
:
890 case KERN_PROTECTION_FAILURE
:
896 case KERN_OPERATION_TIMED_OUT
:
907 fp_drop(p
, fd
, fp
, 0);
912 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
914 int fd
= CAST_DOWN(int,uap
->sem
);
916 struct pseminfo
* pinfo
;
917 struct psemnode
* pnode
;
921 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
924 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
929 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
930 PSEM_SUBSYS_UNLOCK();
934 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
936 PSEM_SUBSYS_UNLOCK();
941 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
943 PSEM_SUBSYS_UNLOCK();
947 PSEM_SUBSYS_UNLOCK();
948 kret
= semaphore_signal(pinfo
->psem_semobject
);
950 case KERN_INVALID_ADDRESS
:
951 case KERN_PROTECTION_FAILURE
:
955 case KERN_OPERATION_TIMED_OUT
:
966 fp_drop(p
, fd
, fp
, 0);
971 sem_init(__unused proc_t p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
977 sem_destroy(__unused proc_t p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
983 sem_getvalue(__unused proc_t p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
989 psem_close(struct psemnode
*pnode
, __unused
int flags
)
992 struct pseminfo
*pinfo
;
995 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
996 PSEM_SUBSYS_UNLOCK();
1000 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1001 PSEM_SUBSYS_UNLOCK();
1005 if(!pinfo
->psem_usecount
) {
1006 kprintf("negative usecount in psem_close\n");
1008 #endif /* DIAGNOSTIC */
1009 pinfo
->psem_usecount
--;
1011 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1012 PSEM_SUBSYS_UNLOCK();
1013 /* lock dropped as only semaphore is destroyed here */
1014 error
= psem_delete(pinfo
);
1017 PSEM_SUBSYS_UNLOCK();
1019 /* subsystem lock is dropped when we get here */
1025 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1030 * Not locked as psem_close is called only from here and is locked
1033 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
);
1039 psem_delete(struct pseminfo
* pinfo
)
1043 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1045 mac_posixsem_label_destroy(pinfo
);
1049 case KERN_INVALID_ADDRESS
:
1050 case KERN_PROTECTION_FAILURE
:
1053 case KERN_OPERATION_TIMED_OUT
:
1063 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1064 __unused
int flags
, __unused vfs_context_t ctx
)
1070 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1071 __unused
int flags
, __unused vfs_context_t ctx
)
1077 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1078 __unused caddr_t data
, __unused vfs_context_t ctx
)
1084 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1085 __unused
void *wql
, __unused vfs_context_t ctx
)
1091 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1092 __unused vfs_context_t ctx
)
1098 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1100 struct pseminfo
*pinfo
;
1101 struct vinfo_stat
*sb
;
1104 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1105 PSEM_SUBSYS_UNLOCK();
1110 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1111 PSEM_SUBSYS_UNLOCK();
1116 sb
= &info
->psem_stat
;
1117 bzero(sb
, sizeof(struct vinfo_stat
));
1119 sb
->vst_mode
= pinfo
->psem_mode
;
1120 sb
->vst_uid
= pinfo
->psem_uid
;
1121 sb
->vst_gid
= pinfo
->psem_gid
;
1122 sb
->vst_size
= pinfo
->psem_usecount
;
1123 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1125 PSEM_SUBSYS_UNLOCK();
1131 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1133 struct psemnode
*pnode
;
1134 struct pseminfo
*psem
;
1137 pnode
= (struct psemnode
*)fp
->f_fglob
->fg_data
;
1138 if (pnode
!= NULL
) {
1139 psem
= pnode
->pinfo
;
1141 mac_posixsem_vnode_label_associate(
1142 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1145 PSEM_SUBSYS_UNLOCK();