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>
61 #include <sys/proc_info.h>
63 #include <bsm/audit_kernel.h>
65 #include <mach/mach_types.h>
66 #include <mach/vm_prot.h>
67 #include <mach/semaphore.h>
68 #include <mach/sync_policy.h>
69 #include <mach/task.h>
70 #include <kern/kern_types.h>
71 #include <kern/task.h>
72 #include <kern/clock.h>
73 #include <mach/kern_return.h>
76 #include <sys/ktrace.h>
79 #define f_flag f_fglob->fg_flag
80 #define f_type f_fglob->fg_type
81 #define f_msgcount f_fglob->fg_msgcount
82 #define f_cred f_fglob->fg_cred
83 #define f_ops f_fglob->fg_ops
84 #define f_offset f_fglob->fg_offset
85 #define f_data f_fglob->fg_data
86 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
89 unsigned int psem_flags
;
90 unsigned int psem_usecount
;
94 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
95 semaphore_t psem_semobject
;
96 struct proc
* sem_proc
;
98 #define PSEMINFO_NULL (struct pseminfo *)0
101 #define PSEM_DEFINED 2
102 #define PSEM_ALLOCATED 4
103 #define PSEM_MAPPED 8
104 #define PSEM_INUSE 0x10
105 #define PSEM_REMOVED 0x20
106 #define PSEM_INCREATE 0x40
107 #define PSEM_INDELETE 0x80
110 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
111 struct pseminfo
*pseminfo
; /* vnode the name refers to */
112 int psem_nlen
; /* length of name */
113 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
115 #define PSEMCACHE_NULL (struct psemcache *)0
118 long goodhits
; /* hits that we can really use */
119 long neghits
; /* negative hits that we can use */
120 long badhits
; /* hits we must drop */
121 long falsehits
; /* hits with id mismatch */
122 long miss
; /* misses */
123 long longnames
; /* long names that ignore cache */
127 char *psem_nameptr
; /* pointer to looked up name */
128 long psem_namelen
; /* length of looked up component */
129 u_long psem_hash
; /* hash value of looked up name */
133 struct pseminfo
*pinfo
;
135 unsigned int readcnt
;
136 unsigned int writecnt
;
139 #define PSEMNODE_NULL (struct psemnode *)0
142 #define PSEMHASH(pnp) \
143 (&psemhashtbl[(pnp)->psem_hash & psemhash])
144 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
145 u_long psemhash
; /* size of hash table - 1 */
146 long psemnument
; /* number of cache entries allocated */
147 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
148 /* 10000 limits to ~1M of memory */
149 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
150 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
151 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
153 struct psemstats psemstats
; /* cache effectiveness statistics */
155 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
156 static int psem_cache_search(struct pseminfo
**,
157 struct psemname
*, struct psemcache
**);
158 static int psem_delete(struct pseminfo
* pinfo
);
160 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
161 kauth_cred_t cred
, int flags
, struct proc
*p
);
162 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
163 kauth_cred_t cred
, int flags
, struct proc
*p
);
164 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
165 caddr_t data
, struct proc
*p
);
166 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
167 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
169 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
171 struct fileops psemops
=
172 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
175 static lck_grp_t
*psx_sem_subsys_lck_grp
;
176 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
177 static lck_attr_t
*psx_sem_subsys_lck_attr
;
178 static lck_mtx_t psx_sem_subsys_mutex
;
180 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
181 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
184 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
185 /* Initialize the mutex governing access to the posix sem subsystem */
186 __private_extern__
void
187 psem_lock_init( void )
190 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
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_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
199 * Lookup an entry in the cache
202 * status of -1 is returned if matches
203 * If the lookup determines that the name does not exist
204 * (negative cacheing), a status of ENOENT is returned. If the lookup
205 * fails, a status of zero is returned.
209 psem_cache_search(psemp
, pnp
, pcache
)
210 struct pseminfo
**psemp
;
211 struct psemname
*pnp
;
212 struct psemcache
**pcache
;
214 struct psemcache
*pcp
, *nnp
;
215 struct psemhashhead
*pcpp
;
217 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
218 psemstats
.longnames
++;
222 pcpp
= PSEMHASH(pnp
);
223 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
224 nnp
= pcp
->psem_hash
.le_next
;
225 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
226 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
235 /* We found a "positive" match, return the vnode */
237 psemstats
.goodhits
++;
239 *psemp
= pcp
->pseminfo
;
245 * We found a "negative" match, ENOENT notifies client of this match.
246 * The nc_vpid field records whether this is a whiteout.
253 * Add an entry to the cache.
256 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
258 struct psemhashhead
*pcpp
;
259 struct pseminfo
*dpinfo
;
260 struct psemcache
*dpcp
;
263 if (pnp
->psem_namelen
> NCHNAMLEN
)
264 panic("cache_enter: name too long");
268 /* if the entry has already been added by some one else return */
269 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
272 if (psemnument
>= posix_sem_max
)
276 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
277 * For negative entries, we have to record whether it is a whiteout.
278 * the whiteout flag is stored in the nc_vpid field which is
281 pcp
->pseminfo
= psemp
;
282 pcp
->psem_nlen
= pnp
->psem_namelen
;
283 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
284 pcpp
= PSEMHASH(pnp
);
289 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
291 panic("psem:cache_enter duplicate");
294 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
299 * Name cache initialization, from vfs_init() when we are booting
302 psem_cache_init(void)
304 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
308 psem_cache_delete(struct psemcache
*pcp
)
311 if (pcp
->psem_hash
.le_prev
== 0)
312 panic("psem namecache purge le_prev");
313 if (pcp
->psem_hash
.le_next
== pcp
)
314 panic("namecache purge le_next");
315 #endif /* DIAGNOSTIC */
316 LIST_REMOVE(pcp
, psem_hash
);
317 pcp
->psem_hash
.le_prev
= 0;
323 * Invalidate a all entries to particular vnode.
325 * We actually just increment the v_id, that will do it. The entries will
326 * be purged by lookup as they get found. If the v_id wraps around, we
327 * need to ditch the entire cache, to avoid confusion. No valid vnode will
328 * ever have (v_id == 0).
331 psem_cache_purge(void)
333 struct psemcache
*pcp
;
334 struct psemhashhead
*pcpp
;
336 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
337 while ( (pcp
= pcpp
->lh_first
) )
338 psem_cache_delete(pcp
);
341 #endif /* NOT_USED */
344 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
348 struct fileproc
*nfp
;
351 struct pseminfo
*pinfo
;
352 struct psemcache
*pcp
;
356 size_t pathlen
, plen
;
358 int cmode
= uap
->mode
;
359 int value
= uap
->value
;
361 struct psemnode
* pnode
= PSEMNODE_NULL
;
362 struct psemcache
* pcache
= PSEMCACHE_NULL
;
363 kern_return_t kret
= KERN_SUCCESS
;
366 AUDIT_ARG(fflags
, uap
->oflag
);
367 AUDIT_ARG(mode
, uap
->mode
);
368 AUDIT_ARG(value
, uap
->value
);
370 pinfo
= PSEMINFO_NULL
;
372 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
376 pathlen
= MAXPATHLEN
;
377 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
381 AUDIT_ARG(text
, pnbuf
);
382 if ( (pathlen
> PSEMNAMLEN
) ) {
383 error
= ENAMETOOLONG
;
387 #ifdef PSXSEM_NAME_RESTRICT
389 if (*nameptr
== '/') {
390 while (*(nameptr
++) == '/') {
399 #endif /* PSXSEM_NAME_RESTRICT */
403 nd
.psem_nameptr
= nameptr
;
404 nd
.psem_namelen
= plen
;
407 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
408 nd
.psem_hash
+= (unsigned char)*cp
* i
;
412 if (KTRPOINT(p
, KTR_NAMEI
))
413 ktrnamei(p
->p_tracep
, nameptr
);
417 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
419 if (error
== ENOENT
) {
420 PSEM_SUBSYS_UNLOCK();
429 fmode
= FFLAGS(uap
->oflag
);
431 PSEM_SUBSYS_UNLOCK();
432 error
= falloc(p
, &nfp
, &indx
);
440 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
441 /* sem exists and opened O_EXCL */
443 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
446 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
447 pinfo
->psem_gid
, pinfo
->psem_mode
);
448 PSEM_SUBSYS_UNLOCK();
452 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
453 /* As per POSIX, O_CREAT has no effect */
457 if ( (fmode
& O_CREAT
) ) {
458 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
459 PSEM_SUBSYS_UNLOCK();
463 PSEM_SUBSYS_UNLOCK();
464 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
472 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
473 pinfo
->psem_usecount
= 1;
474 pinfo
->psem_mode
= cmode
;
475 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
476 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
477 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
478 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
479 PSEM_SUBSYS_UNLOCK();
480 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
481 SYNC_POLICY_FIFO
, value
);
482 if(kret
!= KERN_SUCCESS
)
485 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
486 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
489 /* semaphore should exist as it is without O_CREAT */
491 PSEM_SUBSYS_UNLOCK();
495 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
496 PSEM_SUBSYS_UNLOCK();
500 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
501 pinfo
->psem_gid
, pinfo
->psem_mode
);
502 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
503 PSEM_SUBSYS_UNLOCK();
507 PSEM_SUBSYS_UNLOCK();
508 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
515 * We allocate a new entry if we are less than the maximum
516 * allowed and the one at the front of the LRU list is in use.
517 * Otherwise we use the one at the front of the LRU list.
519 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
528 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
529 PSEM_SUBSYS_UNLOCK();
534 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
535 pinfo
->psem_usecount
++;
536 pnode
->pinfo
= pinfo
;
537 PSEM_SUBSYS_UNLOCK();
540 fp
->f_flag
= fmode
& FMASK
;
541 fp
->f_type
= DTYPE_PSXSEM
;
542 fp
->f_ops
= &psemops
;
543 fp
->f_data
= (caddr_t
)pnode
;
544 *fdflags(p
, indx
) &= ~UF_RESERVED
;
545 fp_drop(p
, indx
, fp
, 1);
548 *retval
= CAST_USER_ADDR_T(indx
);
549 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
554 case KERN_RESOURCE_SHORTAGE
:
556 case KERN_PROTECTION_FAILURE
:
567 fp_free(p
, indx
, nfp
);
569 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
574 * XXX This code is repeated in several places
577 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
582 /* Otherwise, user id 0 always gets access. */
583 if (!suser(cred
, NULL
))
588 /* Otherwise, check the owner. */
589 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
594 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
597 /* Otherwise, check the groups. */
598 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
603 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
606 /* Otherwise, check everyone else. */
611 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
615 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
620 struct pseminfo
*pinfo
;
624 size_t pathlen
, plen
;
626 struct psemcache
*pcache
= PSEMCACHE_NULL
;
628 pinfo
= PSEMINFO_NULL
;
630 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
632 return(ENOSPC
); /* XXX non-standard */
634 pathlen
= MAXPATHLEN
;
635 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
639 AUDIT_ARG(text
, pnbuf
);
640 if (pathlen
> PSEMNAMLEN
) {
641 error
= ENAMETOOLONG
;
646 #ifdef PSXSEM_NAME_RESTRICT
648 if (*nameptr
== '/') {
649 while (*(nameptr
++) == '/') {
658 #endif /* PSXSEM_NAME_RESTRICT */
662 nd
.psem_nameptr
= nameptr
;
663 nd
.psem_namelen
= plen
;
666 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
667 nd
.psem_hash
+= (unsigned char)*cp
* i
;
671 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
673 if (error
== ENOENT
) {
674 PSEM_SUBSYS_UNLOCK();
680 PSEM_SUBSYS_UNLOCK();
685 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
686 PSEM_SUBSYS_UNLOCK();
690 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
691 PSEM_SUBSYS_UNLOCK();
695 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
696 PSEM_SUBSYS_UNLOCK();
701 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
704 pinfo
->psem_flags
|= PSEM_INDELETE
;
705 pinfo
->psem_usecount
--;
707 if (!pinfo
->psem_usecount
) {
711 pinfo
->psem_flags
|= PSEM_REMOVED
;
713 psem_cache_delete(pcache
);
714 PSEM_SUBSYS_UNLOCK();
718 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
723 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
725 int fd
= CAST_DOWN(int,uap
->sem
);
729 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
732 error
= fp_lookup(p
,fd
, &fp
, 1);
738 error
= closef_locked(fp
, fp
->f_fglob
, p
);
739 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
745 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
747 int fd
= CAST_DOWN(int,uap
->sem
);
749 struct pseminfo
* pinfo
;
750 struct psemnode
* pnode
;
754 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
757 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
762 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
763 PSEM_SUBSYS_UNLOCK();
767 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
769 PSEM_SUBSYS_UNLOCK();
774 PSEM_SUBSYS_UNLOCK();
775 kret
= semaphore_wait(pinfo
->psem_semobject
);
777 case KERN_INVALID_ADDRESS
:
778 case KERN_PROTECTION_FAILURE
:
782 case KERN_OPERATION_TIMED_OUT
:
793 fp_drop(p
, fd
, fp
, 0);
799 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
801 int fd
= CAST_DOWN(int,uap
->sem
);
803 struct pseminfo
* pinfo
;
804 struct psemnode
* pnode
;
806 mach_timespec_t wait_time
;
809 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
812 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
817 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
818 PSEM_SUBSYS_UNLOCK();
822 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
824 PSEM_SUBSYS_UNLOCK();
829 PSEM_SUBSYS_UNLOCK();
830 wait_time
.tv_sec
= 0;
831 wait_time
.tv_nsec
= 0;
833 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
835 case KERN_INVALID_ADDRESS
:
836 case KERN_PROTECTION_FAILURE
:
842 case KERN_OPERATION_TIMED_OUT
:
853 fp_drop(p
, fd
, fp
, 0);
858 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
860 int fd
= CAST_DOWN(int,uap
->sem
);
862 struct pseminfo
* pinfo
;
863 struct psemnode
* pnode
;
867 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
870 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
875 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
876 PSEM_SUBSYS_UNLOCK();
880 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
882 PSEM_SUBSYS_UNLOCK();
887 PSEM_SUBSYS_UNLOCK();
888 kret
= semaphore_signal(pinfo
->psem_semobject
);
890 case KERN_INVALID_ADDRESS
:
891 case KERN_PROTECTION_FAILURE
:
895 case KERN_OPERATION_TIMED_OUT
:
906 fp_drop(p
, fd
, fp
, 0);
911 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
917 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
923 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
929 psem_close(struct psemnode
*pnode
, __unused
int flags
,
930 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
933 register struct pseminfo
*pinfo
;
936 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
937 PSEM_SUBSYS_UNLOCK();
941 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
942 PSEM_SUBSYS_UNLOCK();
946 if(!pinfo
->psem_usecount
) {
947 kprintf("negative usecount in psem_close\n");
949 #endif /* DIAGNOSTIC */
950 pinfo
->psem_usecount
--;
952 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
953 PSEM_SUBSYS_UNLOCK();
954 /* lock dropped as only semaphore is destroyed here */
955 error
= psem_delete(pinfo
);
958 PSEM_SUBSYS_UNLOCK();
960 /* subsystem lock is dropped when we get here */
966 psem_closefile(fg
, p
)
972 /* Not locked as psem_close is called only from here and is locked properly */
973 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
980 psem_delete(struct pseminfo
* pinfo
)
984 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
987 case KERN_INVALID_ADDRESS
:
988 case KERN_PROTECTION_FAILURE
:
991 case KERN_OPERATION_TIMED_OUT
:
1001 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1002 __unused kauth_cred_t cred
, __unused
int flags
,
1003 __unused
struct proc
*p
)
1009 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1010 __unused kauth_cred_t cred
, __unused
int flags
,
1011 __unused
struct proc
*p
)
1017 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1018 __unused caddr_t data
, __unused
struct proc
*p
)
1024 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1025 __unused
void *wql
, __unused
struct proc
*p
)
1031 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1032 __unused
struct proc
*p
)
1038 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1040 register struct pseminfo
*pinfo
;
1044 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1045 PSEM_SUBSYS_UNLOCK();
1050 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1051 PSEM_SUBSYS_UNLOCK();
1056 sb
= &info
->psem_stat
;
1057 bzero(sb
, sizeof(struct stat
));
1059 sb
->st_mode
= pinfo
->psem_mode
;
1060 sb
->st_uid
= pinfo
->psem_uid
;
1061 sb
->st_gid
= pinfo
->psem_gid
;
1062 sb
->st_size
= pinfo
->psem_usecount
;
1063 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1065 PSEM_SUBSYS_UNLOCK();