2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
35 * posix_shm.c : Support for POSIX semaphore APIs
38 * Author: Ananthakrishna Ramesh
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/file_internal.h>
51 #include <sys/filedesc.h>
53 #include <sys/proc_internal.h>
54 #include <sys/kauth.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/ioctl.h>
60 #include <sys/malloc.h>
61 #include <sys/semaphore.h>
62 #include <sys/sysproto.h>
64 #include <bsm/audit_kernel.h>
66 #include <mach/mach_types.h>
67 #include <mach/vm_prot.h>
68 #include <mach/semaphore.h>
69 #include <mach/sync_policy.h>
70 #include <mach/task.h>
71 #include <kern/kern_types.h>
72 #include <kern/task.h>
73 #include <kern/clock.h>
74 #include <mach/kern_return.h>
77 #include <sys/ktrace.h>
80 #define f_flag f_fglob->fg_flag
81 #define f_type f_fglob->fg_type
82 #define f_msgcount f_fglob->fg_msgcount
83 #define f_cred f_fglob->fg_cred
84 #define f_ops f_fglob->fg_ops
85 #define f_offset f_fglob->fg_offset
86 #define f_data f_fglob->fg_data
87 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
90 unsigned int psem_flags
;
91 unsigned int psem_usecount
;
95 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
96 semaphore_t psem_semobject
;
97 struct proc
* sem_proc
;
99 #define PSEMINFO_NULL (struct pseminfo *)0
102 #define PSEM_DEFINED 2
103 #define PSEM_ALLOCATED 4
104 #define PSEM_MAPPED 8
105 #define PSEM_INUSE 0x10
106 #define PSEM_REMOVED 0x20
107 #define PSEM_INCREATE 0x40
108 #define PSEM_INDELETE 0x80
111 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
112 struct pseminfo
*pseminfo
; /* vnode the name refers to */
113 int psem_nlen
; /* length of name */
114 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
116 #define PSEMCACHE_NULL (struct psemcache *)0
119 long goodhits
; /* hits that we can really use */
120 long neghits
; /* negative hits that we can use */
121 long badhits
; /* hits we must drop */
122 long falsehits
; /* hits with id mismatch */
123 long miss
; /* misses */
124 long longnames
; /* long names that ignore cache */
128 char *psem_nameptr
; /* pointer to looked up name */
129 long psem_namelen
; /* length of looked up component */
130 u_long psem_hash
; /* hash value of looked up name */
134 struct pseminfo
*pinfo
;
136 unsigned int readcnt
;
137 unsigned int writecnt
;
140 #define PSEMNODE_NULL (struct psemnode *)0
143 #define PSEMHASH(pnp) \
144 (&psemhashtbl[(pnp)->psem_hash & psemhash])
145 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
146 u_long psemhash
; /* size of hash table - 1 */
147 long psemnument
; /* number of cache entries allocated */
148 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
149 /* 10000 limits to ~1M of memory */
150 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
151 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
152 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
154 struct psemstats psemstats
; /* cache effectiveness statistics */
156 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
157 static int psem_cache_search(struct pseminfo
**,
158 struct psemname
*, struct psemcache
**);
159 static int psem_delete(struct pseminfo
* pinfo
);
161 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
162 kauth_cred_t cred
, int flags
, struct proc
*p
);
163 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
164 kauth_cred_t cred
, int flags
, struct proc
*p
);
165 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
166 caddr_t data
, struct proc
*p
);
167 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
168 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
170 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
172 struct fileops psemops
=
173 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
176 static lck_grp_t
*psx_sem_subsys_lck_grp
;
177 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
178 static lck_attr_t
*psx_sem_subsys_lck_attr
;
179 static lck_mtx_t psx_sem_subsys_mutex
;
181 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
182 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
185 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
186 /* Initialize the mutex governing access to the posix sem subsystem */
187 __private_extern__
void
188 psem_lock_init( void )
191 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
192 lck_grp_attr_setstat(psx_sem_subsys_lck_grp_attr
);
194 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
196 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
197 /* lck_attr_setdebug(psx_sem_subsys_lck_attr); */
198 lck_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
202 * Lookup an entry in the cache
205 * status of -1 is returned if matches
206 * If the lookup determines that the name does not exist
207 * (negative cacheing), a status of ENOENT is returned. If the lookup
208 * fails, a status of zero is returned.
212 psem_cache_search(psemp
, pnp
, pcache
)
213 struct pseminfo
**psemp
;
214 struct psemname
*pnp
;
215 struct psemcache
**pcache
;
217 struct psemcache
*pcp
, *nnp
;
218 struct psemhashhead
*pcpp
;
220 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
221 psemstats
.longnames
++;
225 pcpp
= PSEMHASH(pnp
);
226 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
227 nnp
= pcp
->psem_hash
.le_next
;
228 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
229 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
238 /* We found a "positive" match, return the vnode */
240 psemstats
.goodhits
++;
242 *psemp
= pcp
->pseminfo
;
248 * We found a "negative" match, ENOENT notifies client of this match.
249 * The nc_vpid field records whether this is a whiteout.
256 * Add an entry to the cache.
259 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
261 struct psemhashhead
*pcpp
;
262 struct pseminfo
*dpinfo
;
263 struct psemcache
*dpcp
;
266 if (pnp
->psem_namelen
> NCHNAMLEN
)
267 panic("cache_enter: name too long");
271 /* if the entry has already been added by some one else return */
272 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
275 if (psemnument
>= posix_sem_max
)
279 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
280 * For negative entries, we have to record whether it is a whiteout.
281 * the whiteout flag is stored in the nc_vpid field which is
284 pcp
->pseminfo
= psemp
;
285 pcp
->psem_nlen
= pnp
->psem_namelen
;
286 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
287 pcpp
= PSEMHASH(pnp
);
292 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
294 panic("psem:cache_enter duplicate");
297 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
302 * Name cache initialization, from vfs_init() when we are booting
305 psem_cache_init(void)
307 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
311 psem_cache_delete(struct psemcache
*pcp
)
314 if (pcp
->psem_hash
.le_prev
== 0)
315 panic("psem namecache purge le_prev");
316 if (pcp
->psem_hash
.le_next
== pcp
)
317 panic("namecache purge le_next");
318 #endif /* DIAGNOSTIC */
319 LIST_REMOVE(pcp
, psem_hash
);
320 pcp
->psem_hash
.le_prev
= 0;
326 * Invalidate a all entries to particular vnode.
328 * We actually just increment the v_id, that will do it. The entries will
329 * be purged by lookup as they get found. If the v_id wraps around, we
330 * need to ditch the entire cache, to avoid confusion. No valid vnode will
331 * ever have (v_id == 0).
334 psem_cache_purge(void)
336 struct psemcache
*pcp
;
337 struct psemhashhead
*pcpp
;
339 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
340 while ( (pcp
= pcpp
->lh_first
) )
341 psem_cache_delete(pcp
);
344 #endif /* NOT_USED */
347 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
351 struct fileproc
*nfp
;
354 struct pseminfo
*pinfo
;
355 struct psemcache
*pcp
;
359 size_t pathlen
, plen
;
361 int cmode
= uap
->mode
;
362 int value
= uap
->value
;
364 struct psemnode
* pnode
= PSEMNODE_NULL
;
365 struct psemcache
* pcache
= PSEMCACHE_NULL
;
366 kern_return_t kret
= KERN_SUCCESS
;
369 AUDIT_ARG(fflags
, uap
->oflag
);
370 AUDIT_ARG(mode
, uap
->mode
);
371 AUDIT_ARG(value
, uap
->value
);
373 pinfo
= PSEMINFO_NULL
;
375 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
379 pathlen
= MAXPATHLEN
;
380 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
384 AUDIT_ARG(text
, pnbuf
);
385 if ( (pathlen
> PSEMNAMLEN
) ) {
386 error
= ENAMETOOLONG
;
390 #ifdef PSXSEM_NAME_RESTRICT
392 if (*nameptr
== '/') {
393 while (*(nameptr
++) == '/') {
402 #endif /* PSXSEM_NAME_RESTRICT */
406 nd
.psem_nameptr
= nameptr
;
407 nd
.psem_namelen
= plen
;
410 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
411 nd
.psem_hash
+= (unsigned char)*cp
* i
;
415 if (KTRPOINT(p
, KTR_NAMEI
))
416 ktrnamei(p
->p_tracep
, nameptr
);
420 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
422 if (error
== ENOENT
) {
423 PSEM_SUBSYS_UNLOCK();
432 fmode
= FFLAGS(uap
->oflag
);
434 PSEM_SUBSYS_UNLOCK();
435 error
= falloc(p
, &nfp
, &indx
);
443 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
444 /* sem exists and opened O_EXCL */
446 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
449 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
450 pinfo
->psem_gid
, pinfo
->psem_mode
);
451 PSEM_SUBSYS_UNLOCK();
455 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
456 /* As per POSIX, O_CREAT has no effect */
460 if ( (fmode
& O_CREAT
) ) {
461 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
462 PSEM_SUBSYS_UNLOCK();
466 PSEM_SUBSYS_UNLOCK();
467 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
475 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
476 pinfo
->psem_usecount
= 1;
477 pinfo
->psem_mode
= cmode
;
478 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
479 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
480 PSEM_SUBSYS_UNLOCK();
481 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
482 SYNC_POLICY_FIFO
, value
);
483 if(kret
!= KERN_SUCCESS
)
486 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
487 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
490 /* semaphore should exist as it is without O_CREAT */
492 PSEM_SUBSYS_UNLOCK();
496 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
497 PSEM_SUBSYS_UNLOCK();
501 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
502 pinfo
->psem_gid
, pinfo
->psem_mode
);
503 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
504 PSEM_SUBSYS_UNLOCK();
508 PSEM_SUBSYS_UNLOCK();
509 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
516 * We allocate a new entry if we are less than the maximum
517 * allowed and the one at the front of the LRU list is in use.
518 * Otherwise we use the one at the front of the LRU list.
520 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
529 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
530 PSEM_SUBSYS_UNLOCK();
535 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
536 pinfo
->psem_usecount
++;
537 pnode
->pinfo
= pinfo
;
538 PSEM_SUBSYS_UNLOCK();
541 fp
->f_flag
= fmode
& FMASK
;
542 fp
->f_type
= DTYPE_PSXSEM
;
543 fp
->f_ops
= &psemops
;
544 fp
->f_data
= (caddr_t
)pnode
;
545 *fdflags(p
, indx
) &= ~UF_RESERVED
;
546 fp_drop(p
, indx
, fp
, 1);
549 *retval
= CAST_USER_ADDR_T(indx
);
550 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
555 case KERN_RESOURCE_SHORTAGE
:
557 case KERN_PROTECTION_FAILURE
:
568 fp_free(p
, indx
, nfp
);
570 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
575 * XXX This code is repeated in several places
578 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
583 /* Otherwise, user id 0 always gets access. */
584 if (!suser(cred
, NULL
))
589 /* Otherwise, check the owner. */
590 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
595 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
598 /* Otherwise, check the groups. */
599 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
604 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
607 /* Otherwise, check everyone else. */
612 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
616 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
621 struct pseminfo
*pinfo
;
625 size_t pathlen
, plen
;
627 struct psemcache
*pcache
= PSEMCACHE_NULL
;
629 pinfo
= PSEMINFO_NULL
;
631 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
633 return(ENOSPC
); /* XXX non-standard */
635 pathlen
= MAXPATHLEN
;
636 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
640 AUDIT_ARG(text
, pnbuf
);
641 if (pathlen
> PSEMNAMLEN
) {
642 error
= ENAMETOOLONG
;
647 #ifdef PSXSEM_NAME_RESTRICT
649 if (*nameptr
== '/') {
650 while (*(nameptr
++) == '/') {
659 #endif /* PSXSEM_NAME_RESTRICT */
663 nd
.psem_nameptr
= nameptr
;
664 nd
.psem_namelen
= plen
;
667 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
668 nd
.psem_hash
+= (unsigned char)*cp
* i
;
672 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
674 if (error
== ENOENT
) {
675 PSEM_SUBSYS_UNLOCK();
681 PSEM_SUBSYS_UNLOCK();
686 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
687 PSEM_SUBSYS_UNLOCK();
691 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
692 PSEM_SUBSYS_UNLOCK();
696 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
697 PSEM_SUBSYS_UNLOCK();
702 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
705 pinfo
->psem_flags
|= PSEM_INDELETE
;
706 pinfo
->psem_usecount
--;
708 if (!pinfo
->psem_usecount
) {
712 pinfo
->psem_flags
|= PSEM_REMOVED
;
714 psem_cache_delete(pcache
);
715 PSEM_SUBSYS_UNLOCK();
719 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
724 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
726 int fd
= CAST_DOWN(int,uap
->sem
);
730 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
733 error
= fp_lookup(p
,fd
, &fp
, 1);
739 error
= closef_locked(fp
, fp
->f_fglob
, p
);
740 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
746 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
748 int fd
= CAST_DOWN(int,uap
->sem
);
750 struct pseminfo
* pinfo
;
751 struct psemnode
* pnode
;
755 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
758 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
763 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
764 PSEM_SUBSYS_UNLOCK();
768 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
770 PSEM_SUBSYS_UNLOCK();
775 PSEM_SUBSYS_UNLOCK();
776 kret
= semaphore_wait(pinfo
->psem_semobject
);
778 case KERN_INVALID_ADDRESS
:
779 case KERN_PROTECTION_FAILURE
:
783 case KERN_OPERATION_TIMED_OUT
:
794 fp_drop(p
, fd
, fp
, 0);
800 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
802 int fd
= CAST_DOWN(int,uap
->sem
);
804 struct pseminfo
* pinfo
;
805 struct psemnode
* pnode
;
807 mach_timespec_t wait_time
;
810 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
813 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
818 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
819 PSEM_SUBSYS_UNLOCK();
823 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
825 PSEM_SUBSYS_UNLOCK();
830 PSEM_SUBSYS_UNLOCK();
831 wait_time
.tv_sec
= 0;
832 wait_time
.tv_nsec
= 0;
834 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
836 case KERN_INVALID_ADDRESS
:
837 case KERN_PROTECTION_FAILURE
:
843 case KERN_OPERATION_TIMED_OUT
:
854 fp_drop(p
, fd
, fp
, 0);
859 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
861 int fd
= CAST_DOWN(int,uap
->sem
);
863 struct pseminfo
* pinfo
;
864 struct psemnode
* pnode
;
868 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
871 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
876 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
877 PSEM_SUBSYS_UNLOCK();
881 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
883 PSEM_SUBSYS_UNLOCK();
888 PSEM_SUBSYS_UNLOCK();
889 kret
= semaphore_signal(pinfo
->psem_semobject
);
891 case KERN_INVALID_ADDRESS
:
892 case KERN_PROTECTION_FAILURE
:
896 case KERN_OPERATION_TIMED_OUT
:
907 fp_drop(p
, fd
, fp
, 0);
912 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
918 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
924 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
930 psem_close(struct psemnode
*pnode
, __unused
int flags
,
931 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
934 register struct pseminfo
*pinfo
;
937 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
938 PSEM_SUBSYS_UNLOCK();
942 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
943 PSEM_SUBSYS_UNLOCK();
947 if(!pinfo
->psem_usecount
) {
948 kprintf("negative usecount in psem_close\n");
950 #endif /* DIAGNOSTIC */
951 pinfo
->psem_usecount
--;
953 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
954 PSEM_SUBSYS_UNLOCK();
955 /* lock dropped as only semaphore is destroyed here */
956 error
= psem_delete(pinfo
);
959 PSEM_SUBSYS_UNLOCK();
961 /* subsystem lock is dropped when we get here */
967 psem_closefile(fg
, p
)
973 /* Not locked as psem_close is called only from here and is locked properly */
974 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
981 psem_delete(struct pseminfo
* pinfo
)
985 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
988 case KERN_INVALID_ADDRESS
:
989 case KERN_PROTECTION_FAILURE
:
992 case KERN_OPERATION_TIMED_OUT
:
1002 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1003 __unused kauth_cred_t cred
, __unused
int flags
,
1004 __unused
struct proc
*p
)
1010 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1011 __unused kauth_cred_t cred
, __unused
int flags
,
1012 __unused
struct proc
*p
)
1018 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1019 __unused caddr_t data
, __unused
struct proc
*p
)
1025 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1026 __unused
void *wql
, __unused
struct proc
*p
)
1032 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1033 __unused
struct proc
*p
)