2 * Copyright (c) 2000-2004 Apple Computer, 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 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/file_internal.h>
49 #include <sys/filedesc.h>
51 #include <sys/proc_internal.h>
52 #include <sys/kauth.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/ioctl.h>
58 #include <sys/malloc.h>
59 #include <sys/semaphore.h>
60 #include <sys/sysproto.h>
62 #include <bsm/audit_kernel.h>
64 #include <mach/mach_types.h>
65 #include <mach/vm_prot.h>
66 #include <mach/semaphore.h>
67 #include <mach/sync_policy.h>
68 #include <mach/task.h>
69 #include <kern/kern_types.h>
70 #include <kern/task.h>
71 #include <kern/clock.h>
72 #include <mach/kern_return.h>
75 #include <sys/ktrace.h>
78 #define f_flag f_fglob->fg_flag
79 #define f_type f_fglob->fg_type
80 #define f_msgcount f_fglob->fg_msgcount
81 #define f_cred f_fglob->fg_cred
82 #define f_ops f_fglob->fg_ops
83 #define f_offset f_fglob->fg_offset
84 #define f_data f_fglob->fg_data
85 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
88 unsigned int psem_flags
;
89 unsigned int psem_usecount
;
93 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
94 semaphore_t psem_semobject
;
95 struct proc
* sem_proc
;
97 #define PSEMINFO_NULL (struct pseminfo *)0
100 #define PSEM_DEFINED 2
101 #define PSEM_ALLOCATED 4
102 #define PSEM_MAPPED 8
103 #define PSEM_INUSE 0x10
104 #define PSEM_REMOVED 0x20
105 #define PSEM_INCREATE 0x40
106 #define PSEM_INDELETE 0x80
109 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
110 struct pseminfo
*pseminfo
; /* vnode the name refers to */
111 int psem_nlen
; /* length of name */
112 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
114 #define PSEMCACHE_NULL (struct psemcache *)0
117 long goodhits
; /* hits that we can really use */
118 long neghits
; /* negative hits that we can use */
119 long badhits
; /* hits we must drop */
120 long falsehits
; /* hits with id mismatch */
121 long miss
; /* misses */
122 long longnames
; /* long names that ignore cache */
126 char *psem_nameptr
; /* pointer to looked up name */
127 long psem_namelen
; /* length of looked up component */
128 u_long psem_hash
; /* hash value of looked up name */
132 struct pseminfo
*pinfo
;
134 unsigned int readcnt
;
135 unsigned int writecnt
;
138 #define PSEMNODE_NULL (struct psemnode *)0
141 #define PSEMHASH(pnp) \
142 (&psemhashtbl[(pnp)->psem_hash & psemhash])
143 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
144 u_long psemhash
; /* size of hash table - 1 */
145 long psemnument
; /* number of cache entries allocated */
146 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
147 /* 10000 limits to ~1M of memory */
148 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
149 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
150 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
152 struct psemstats psemstats
; /* cache effectiveness statistics */
154 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
155 static int psem_cache_search(struct pseminfo
**,
156 struct psemname
*, struct psemcache
**);
157 static int psem_delete(struct pseminfo
* pinfo
);
159 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
160 kauth_cred_t cred
, int flags
, struct proc
*p
);
161 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
162 kauth_cred_t cred
, int flags
, struct proc
*p
);
163 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
164 caddr_t data
, struct proc
*p
);
165 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
166 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
168 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
170 struct fileops psemops
=
171 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
174 static lck_grp_t
*psx_sem_subsys_lck_grp
;
175 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
176 static lck_attr_t
*psx_sem_subsys_lck_attr
;
177 static lck_mtx_t psx_sem_subsys_mutex
;
179 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
180 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
183 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
184 /* Initialize the mutex governing access to the posix sem subsystem */
185 __private_extern__
void
186 psem_lock_init( void )
189 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
190 lck_grp_attr_setstat(psx_sem_subsys_lck_grp_attr
);
192 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
194 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
195 /* lck_attr_setdebug(psx_sem_subsys_lck_attr); */
196 lck_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
200 * Lookup an entry in the cache
203 * status of -1 is returned if matches
204 * If the lookup determines that the name does not exist
205 * (negative cacheing), a status of ENOENT is returned. If the lookup
206 * fails, a status of zero is returned.
210 psem_cache_search(psemp
, pnp
, pcache
)
211 struct pseminfo
**psemp
;
212 struct psemname
*pnp
;
213 struct psemcache
**pcache
;
215 struct psemcache
*pcp
, *nnp
;
216 struct psemhashhead
*pcpp
;
218 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
219 psemstats
.longnames
++;
223 pcpp
= PSEMHASH(pnp
);
224 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
225 nnp
= pcp
->psem_hash
.le_next
;
226 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
227 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
236 /* We found a "positive" match, return the vnode */
238 psemstats
.goodhits
++;
240 *psemp
= pcp
->pseminfo
;
246 * We found a "negative" match, ENOENT notifies client of this match.
247 * The nc_vpid field records whether this is a whiteout.
254 * Add an entry to the cache.
257 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
259 struct psemhashhead
*pcpp
;
260 struct pseminfo
*dpinfo
;
261 struct psemcache
*dpcp
;
264 if (pnp
->psem_namelen
> NCHNAMLEN
)
265 panic("cache_enter: name too long");
269 /* if the entry has already been added by some one else return */
270 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
273 if (psemnument
>= posix_sem_max
)
277 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
278 * For negative entries, we have to record whether it is a whiteout.
279 * the whiteout flag is stored in the nc_vpid field which is
282 pcp
->pseminfo
= psemp
;
283 pcp
->psem_nlen
= pnp
->psem_namelen
;
284 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
285 pcpp
= PSEMHASH(pnp
);
290 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
292 panic("psem:cache_enter duplicate");
295 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
300 * Name cache initialization, from vfs_init() when we are booting
303 psem_cache_init(void)
305 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
309 psem_cache_delete(struct psemcache
*pcp
)
312 if (pcp
->psem_hash
.le_prev
== 0)
313 panic("psem namecache purge le_prev");
314 if (pcp
->psem_hash
.le_next
== pcp
)
315 panic("namecache purge le_next");
316 #endif /* DIAGNOSTIC */
317 LIST_REMOVE(pcp
, psem_hash
);
318 pcp
->psem_hash
.le_prev
= 0;
324 * Invalidate a all entries to particular vnode.
326 * We actually just increment the v_id, that will do it. The entries will
327 * be purged by lookup as they get found. If the v_id wraps around, we
328 * need to ditch the entire cache, to avoid confusion. No valid vnode will
329 * ever have (v_id == 0).
332 psem_cache_purge(void)
334 struct psemcache
*pcp
;
335 struct psemhashhead
*pcpp
;
337 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
338 while ( (pcp
= pcpp
->lh_first
) )
339 psem_cache_delete(pcp
);
342 #endif /* NOT_USED */
345 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
349 struct fileproc
*nfp
;
352 struct pseminfo
*pinfo
;
353 struct psemcache
*pcp
;
357 size_t pathlen
, plen
;
359 int cmode
= uap
->mode
;
360 int value
= uap
->value
;
362 struct psemnode
* pnode
= PSEMNODE_NULL
;
363 struct psemcache
* pcache
= PSEMCACHE_NULL
;
364 kern_return_t kret
= KERN_SUCCESS
;
367 AUDIT_ARG(fflags
, uap
->oflag
);
368 AUDIT_ARG(mode
, uap
->mode
);
369 AUDIT_ARG(value
, uap
->value
);
371 pinfo
= PSEMINFO_NULL
;
373 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
377 pathlen
= MAXPATHLEN
;
378 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
382 AUDIT_ARG(text
, pnbuf
);
383 if ( (pathlen
> PSEMNAMLEN
) ) {
384 error
= ENAMETOOLONG
;
388 #ifdef PSXSEM_NAME_RESTRICT
390 if (*nameptr
== '/') {
391 while (*(nameptr
++) == '/') {
400 #endif /* PSXSEM_NAME_RESTRICT */
404 nd
.psem_nameptr
= nameptr
;
405 nd
.psem_namelen
= plen
;
408 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
409 nd
.psem_hash
+= (unsigned char)*cp
* i
;
413 if (KTRPOINT(p
, KTR_NAMEI
))
414 ktrnamei(p
->p_tracep
, nameptr
);
418 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
420 if (error
== ENOENT
) {
421 PSEM_SUBSYS_UNLOCK();
430 fmode
= FFLAGS(uap
->oflag
);
432 PSEM_SUBSYS_UNLOCK();
433 error
= falloc(p
, &nfp
, &indx
);
441 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
442 /* sem exists and opened O_EXCL */
444 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
447 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
448 pinfo
->psem_gid
, pinfo
->psem_mode
);
449 PSEM_SUBSYS_UNLOCK();
453 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
454 /* As per POSIX, O_CREAT has no effect */
458 if ( (fmode
& O_CREAT
) ) {
459 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
460 PSEM_SUBSYS_UNLOCK();
464 PSEM_SUBSYS_UNLOCK();
465 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
473 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
474 pinfo
->psem_usecount
= 1;
475 pinfo
->psem_mode
= cmode
;
476 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
477 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
478 PSEM_SUBSYS_UNLOCK();
479 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
480 SYNC_POLICY_FIFO
, value
);
481 if(kret
!= KERN_SUCCESS
)
484 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
485 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
488 /* semaphore should exist as it is without O_CREAT */
490 PSEM_SUBSYS_UNLOCK();
494 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
495 PSEM_SUBSYS_UNLOCK();
499 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
500 pinfo
->psem_gid
, pinfo
->psem_mode
);
501 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
502 PSEM_SUBSYS_UNLOCK();
506 PSEM_SUBSYS_UNLOCK();
507 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
514 * We allocate a new entry if we are less than the maximum
515 * allowed and the one at the front of the LRU list is in use.
516 * Otherwise we use the one at the front of the LRU list.
518 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
527 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
528 PSEM_SUBSYS_UNLOCK();
533 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
534 pinfo
->psem_usecount
++;
535 pnode
->pinfo
= pinfo
;
536 PSEM_SUBSYS_UNLOCK();
539 fp
->f_flag
= fmode
& FMASK
;
540 fp
->f_type
= DTYPE_PSXSEM
;
541 fp
->f_ops
= &psemops
;
542 fp
->f_data
= (caddr_t
)pnode
;
543 *fdflags(p
, indx
) &= ~UF_RESERVED
;
544 fp_drop(p
, indx
, fp
, 1);
547 *retval
= CAST_USER_ADDR_T(indx
);
548 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
553 case KERN_RESOURCE_SHORTAGE
:
555 case KERN_PROTECTION_FAILURE
:
566 fp_free(p
, indx
, nfp
);
568 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
573 * XXX This code is repeated in several places
576 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
581 /* Otherwise, user id 0 always gets access. */
582 if (!suser(cred
, NULL
))
587 /* Otherwise, check the owner. */
588 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
593 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
596 /* Otherwise, check the groups. */
597 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
602 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
605 /* Otherwise, check everyone else. */
610 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
614 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
619 struct pseminfo
*pinfo
;
623 size_t pathlen
, plen
;
625 struct psemcache
*pcache
= PSEMCACHE_NULL
;
627 pinfo
= PSEMINFO_NULL
;
629 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
631 return(ENOSPC
); /* XXX non-standard */
633 pathlen
= MAXPATHLEN
;
634 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
638 AUDIT_ARG(text
, pnbuf
);
639 if (pathlen
> PSEMNAMLEN
) {
640 error
= ENAMETOOLONG
;
645 #ifdef PSXSEM_NAME_RESTRICT
647 if (*nameptr
== '/') {
648 while (*(nameptr
++) == '/') {
657 #endif /* PSXSEM_NAME_RESTRICT */
661 nd
.psem_nameptr
= nameptr
;
662 nd
.psem_namelen
= plen
;
665 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
666 nd
.psem_hash
+= (unsigned char)*cp
* i
;
670 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
672 if (error
== ENOENT
) {
673 PSEM_SUBSYS_UNLOCK();
679 PSEM_SUBSYS_UNLOCK();
684 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
685 PSEM_SUBSYS_UNLOCK();
689 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
690 PSEM_SUBSYS_UNLOCK();
694 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
695 PSEM_SUBSYS_UNLOCK();
700 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
703 pinfo
->psem_flags
|= PSEM_INDELETE
;
704 pinfo
->psem_usecount
--;
706 if (!pinfo
->psem_usecount
) {
710 pinfo
->psem_flags
|= PSEM_REMOVED
;
712 psem_cache_delete(pcache
);
713 PSEM_SUBSYS_UNLOCK();
717 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
722 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
724 int fd
= CAST_DOWN(int,uap
->sem
);
728 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
731 error
= fp_lookup(p
,fd
, &fp
, 1);
737 error
= closef_locked(fp
, fp
->f_fglob
, p
);
738 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
744 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
746 int fd
= CAST_DOWN(int,uap
->sem
);
748 struct pseminfo
* pinfo
;
749 struct psemnode
* pnode
;
753 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
756 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
761 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
762 PSEM_SUBSYS_UNLOCK();
766 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
768 PSEM_SUBSYS_UNLOCK();
773 PSEM_SUBSYS_UNLOCK();
774 kret
= semaphore_wait(pinfo
->psem_semobject
);
776 case KERN_INVALID_ADDRESS
:
777 case KERN_PROTECTION_FAILURE
:
781 case KERN_OPERATION_TIMED_OUT
:
792 fp_drop(p
, fd
, fp
, 0);
798 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
800 int fd
= CAST_DOWN(int,uap
->sem
);
802 struct pseminfo
* pinfo
;
803 struct psemnode
* pnode
;
805 mach_timespec_t wait_time
;
808 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
811 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
816 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
817 PSEM_SUBSYS_UNLOCK();
821 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
823 PSEM_SUBSYS_UNLOCK();
828 PSEM_SUBSYS_UNLOCK();
829 wait_time
.tv_sec
= 0;
830 wait_time
.tv_nsec
= 0;
832 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
834 case KERN_INVALID_ADDRESS
:
835 case KERN_PROTECTION_FAILURE
:
841 case KERN_OPERATION_TIMED_OUT
:
852 fp_drop(p
, fd
, fp
, 0);
857 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
859 int fd
= CAST_DOWN(int,uap
->sem
);
861 struct pseminfo
* pinfo
;
862 struct psemnode
* pnode
;
866 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
869 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
874 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
875 PSEM_SUBSYS_UNLOCK();
879 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
881 PSEM_SUBSYS_UNLOCK();
886 PSEM_SUBSYS_UNLOCK();
887 kret
= semaphore_signal(pinfo
->psem_semobject
);
889 case KERN_INVALID_ADDRESS
:
890 case KERN_PROTECTION_FAILURE
:
894 case KERN_OPERATION_TIMED_OUT
:
905 fp_drop(p
, fd
, fp
, 0);
910 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
916 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
922 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
928 psem_close(struct psemnode
*pnode
, __unused
int flags
,
929 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
932 register struct pseminfo
*pinfo
;
935 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
936 PSEM_SUBSYS_UNLOCK();
940 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
941 PSEM_SUBSYS_UNLOCK();
945 if(!pinfo
->psem_usecount
) {
946 kprintf("negative usecount in psem_close\n");
948 #endif /* DIAGNOSTIC */
949 pinfo
->psem_usecount
--;
951 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
952 PSEM_SUBSYS_UNLOCK();
953 /* lock dropped as only semaphore is destroyed here */
954 error
= psem_delete(pinfo
);
957 PSEM_SUBSYS_UNLOCK();
959 /* subsystem lock is dropped when we get here */
965 psem_closefile(fg
, p
)
971 /* Not locked as psem_close is called only from here and is locked properly */
972 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
979 psem_delete(struct pseminfo
* pinfo
)
983 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
986 case KERN_INVALID_ADDRESS
:
987 case KERN_PROTECTION_FAILURE
:
990 case KERN_OPERATION_TIMED_OUT
:
1000 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1001 __unused kauth_cred_t cred
, __unused
int flags
,
1002 __unused
struct proc
*p
)
1008 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1009 __unused kauth_cred_t cred
, __unused
int flags
,
1010 __unused
struct proc
*p
)
1016 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1017 __unused caddr_t data
, __unused
struct proc
*p
)
1023 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1024 __unused
void *wql
, __unused
struct proc
*p
)
1030 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1031 __unused
struct proc
*p
)