2 * Copyright (c) 2000-2004 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>
63 #include <sys/proc_info.h>
65 #include <bsm/audit_kernel.h>
67 #include <mach/mach_types.h>
68 #include <mach/vm_prot.h>
69 #include <mach/semaphore.h>
70 #include <mach/sync_policy.h>
71 #include <mach/task.h>
72 #include <kern/kern_types.h>
73 #include <kern/task.h>
74 #include <kern/clock.h>
75 #include <mach/kern_return.h>
78 #include <sys/ktrace.h>
81 #define f_flag f_fglob->fg_flag
82 #define f_type f_fglob->fg_type
83 #define f_msgcount f_fglob->fg_msgcount
84 #define f_cred f_fglob->fg_cred
85 #define f_ops f_fglob->fg_ops
86 #define f_offset f_fglob->fg_offset
87 #define f_data f_fglob->fg_data
88 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
91 unsigned int psem_flags
;
92 unsigned int psem_usecount
;
96 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
97 semaphore_t psem_semobject
;
98 struct proc
* sem_proc
;
100 #define PSEMINFO_NULL (struct pseminfo *)0
103 #define PSEM_DEFINED 2
104 #define PSEM_ALLOCATED 4
105 #define PSEM_MAPPED 8
106 #define PSEM_INUSE 0x10
107 #define PSEM_REMOVED 0x20
108 #define PSEM_INCREATE 0x40
109 #define PSEM_INDELETE 0x80
112 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
113 struct pseminfo
*pseminfo
; /* vnode the name refers to */
114 int psem_nlen
; /* length of name */
115 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
117 #define PSEMCACHE_NULL (struct psemcache *)0
120 long goodhits
; /* hits that we can really use */
121 long neghits
; /* negative hits that we can use */
122 long badhits
; /* hits we must drop */
123 long falsehits
; /* hits with id mismatch */
124 long miss
; /* misses */
125 long longnames
; /* long names that ignore cache */
129 char *psem_nameptr
; /* pointer to looked up name */
130 long psem_namelen
; /* length of looked up component */
131 u_long psem_hash
; /* hash value of looked up name */
135 struct pseminfo
*pinfo
;
137 unsigned int readcnt
;
138 unsigned int writecnt
;
141 #define PSEMNODE_NULL (struct psemnode *)0
144 #define PSEMHASH(pnp) \
145 (&psemhashtbl[(pnp)->psem_hash & psemhash])
146 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
147 u_long psemhash
; /* size of hash table - 1 */
148 long psemnument
; /* number of cache entries allocated */
149 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
150 /* 10000 limits to ~1M of memory */
151 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
152 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
153 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
155 struct psemstats psemstats
; /* cache effectiveness statistics */
157 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
158 static int psem_cache_search(struct pseminfo
**,
159 struct psemname
*, struct psemcache
**);
160 static int psem_delete(struct pseminfo
* pinfo
);
162 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
163 kauth_cred_t cred
, int flags
, struct proc
*p
);
164 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
165 kauth_cred_t cred
, int flags
, struct proc
*p
);
166 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
167 caddr_t data
, struct proc
*p
);
168 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
169 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
171 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
173 struct fileops psemops
=
174 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
177 static lck_grp_t
*psx_sem_subsys_lck_grp
;
178 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
179 static lck_attr_t
*psx_sem_subsys_lck_attr
;
180 static lck_mtx_t psx_sem_subsys_mutex
;
182 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
183 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
186 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
187 /* Initialize the mutex governing access to the posix sem subsystem */
188 __private_extern__
void
189 psem_lock_init( void )
192 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
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_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
201 * Lookup an entry in the cache
204 * status of -1 is returned if matches
205 * If the lookup determines that the name does not exist
206 * (negative cacheing), a status of ENOENT is returned. If the lookup
207 * fails, a status of zero is returned.
211 psem_cache_search(psemp
, pnp
, pcache
)
212 struct pseminfo
**psemp
;
213 struct psemname
*pnp
;
214 struct psemcache
**pcache
;
216 struct psemcache
*pcp
, *nnp
;
217 struct psemhashhead
*pcpp
;
219 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
220 psemstats
.longnames
++;
224 pcpp
= PSEMHASH(pnp
);
225 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
226 nnp
= pcp
->psem_hash
.le_next
;
227 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
228 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
237 /* We found a "positive" match, return the vnode */
239 psemstats
.goodhits
++;
241 *psemp
= pcp
->pseminfo
;
247 * We found a "negative" match, ENOENT notifies client of this match.
248 * The nc_vpid field records whether this is a whiteout.
255 * Add an entry to the cache.
258 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
260 struct psemhashhead
*pcpp
;
261 struct pseminfo
*dpinfo
;
262 struct psemcache
*dpcp
;
265 if (pnp
->psem_namelen
> NCHNAMLEN
)
266 panic("cache_enter: name too long");
270 /* if the entry has already been added by some one else return */
271 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
274 if (psemnument
>= posix_sem_max
)
278 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
279 * For negative entries, we have to record whether it is a whiteout.
280 * the whiteout flag is stored in the nc_vpid field which is
283 pcp
->pseminfo
= psemp
;
284 pcp
->psem_nlen
= pnp
->psem_namelen
;
285 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
286 pcpp
= PSEMHASH(pnp
);
291 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
293 panic("psem:cache_enter duplicate");
296 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
301 * Name cache initialization, from vfs_init() when we are booting
304 psem_cache_init(void)
306 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
310 psem_cache_delete(struct psemcache
*pcp
)
313 if (pcp
->psem_hash
.le_prev
== 0)
314 panic("psem namecache purge le_prev");
315 if (pcp
->psem_hash
.le_next
== pcp
)
316 panic("namecache purge le_next");
317 #endif /* DIAGNOSTIC */
318 LIST_REMOVE(pcp
, psem_hash
);
319 pcp
->psem_hash
.le_prev
= 0;
325 * Invalidate a all entries to particular vnode.
327 * We actually just increment the v_id, that will do it. The entries will
328 * be purged by lookup as they get found. If the v_id wraps around, we
329 * need to ditch the entire cache, to avoid confusion. No valid vnode will
330 * ever have (v_id == 0).
333 psem_cache_purge(void)
335 struct psemcache
*pcp
;
336 struct psemhashhead
*pcpp
;
338 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
339 while ( (pcp
= pcpp
->lh_first
) )
340 psem_cache_delete(pcp
);
343 #endif /* NOT_USED */
346 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
350 struct fileproc
*nfp
;
353 struct pseminfo
*pinfo
;
354 struct psemcache
*pcp
;
358 size_t pathlen
, plen
;
360 int cmode
= uap
->mode
;
361 int value
= uap
->value
;
363 struct psemnode
* pnode
= PSEMNODE_NULL
;
364 struct psemcache
* pcache
= PSEMCACHE_NULL
;
365 kern_return_t kret
= KERN_SUCCESS
;
368 AUDIT_ARG(fflags
, uap
->oflag
);
369 AUDIT_ARG(mode
, uap
->mode
);
370 AUDIT_ARG(value
, uap
->value
);
372 pinfo
= PSEMINFO_NULL
;
374 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
378 pathlen
= MAXPATHLEN
;
379 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
383 AUDIT_ARG(text
, pnbuf
);
384 if ( (pathlen
> PSEMNAMLEN
) ) {
385 error
= ENAMETOOLONG
;
389 #ifdef PSXSEM_NAME_RESTRICT
391 if (*nameptr
== '/') {
392 while (*(nameptr
++) == '/') {
401 #endif /* PSXSEM_NAME_RESTRICT */
405 nd
.psem_nameptr
= nameptr
;
406 nd
.psem_namelen
= plen
;
409 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
410 nd
.psem_hash
+= (unsigned char)*cp
* i
;
414 if (KTRPOINT(p
, KTR_NAMEI
))
415 ktrnamei(p
->p_tracep
, nameptr
);
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
);
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
);
474 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
475 pinfo
->psem_usecount
= 1;
476 pinfo
->psem_mode
= cmode
;
477 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
478 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
479 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
480 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
481 PSEM_SUBSYS_UNLOCK();
482 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
483 SYNC_POLICY_FIFO
, value
);
484 if(kret
!= KERN_SUCCESS
)
487 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
488 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
491 /* semaphore should exist as it is without O_CREAT */
493 PSEM_SUBSYS_UNLOCK();
497 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
498 PSEM_SUBSYS_UNLOCK();
502 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
503 pinfo
->psem_gid
, pinfo
->psem_mode
);
504 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
505 PSEM_SUBSYS_UNLOCK();
509 PSEM_SUBSYS_UNLOCK();
510 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
517 * We allocate a new entry if we are less than the maximum
518 * allowed and the one at the front of the LRU list is in use.
519 * Otherwise we use the one at the front of the LRU list.
521 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
530 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
531 PSEM_SUBSYS_UNLOCK();
536 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
537 pinfo
->psem_usecount
++;
538 pnode
->pinfo
= pinfo
;
539 PSEM_SUBSYS_UNLOCK();
542 fp
->f_flag
= fmode
& FMASK
;
543 fp
->f_type
= DTYPE_PSXSEM
;
544 fp
->f_ops
= &psemops
;
545 fp
->f_data
= (caddr_t
)pnode
;
546 *fdflags(p
, indx
) &= ~UF_RESERVED
;
547 fp_drop(p
, indx
, fp
, 1);
550 *retval
= CAST_USER_ADDR_T(indx
);
551 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
556 case KERN_RESOURCE_SHORTAGE
:
558 case KERN_PROTECTION_FAILURE
:
569 fp_free(p
, indx
, nfp
);
571 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
576 * XXX This code is repeated in several places
579 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
584 /* Otherwise, user id 0 always gets access. */
585 if (!suser(cred
, NULL
))
590 /* Otherwise, check the owner. */
591 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
596 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
599 /* Otherwise, check the groups. */
600 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
605 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
608 /* Otherwise, check everyone else. */
613 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
617 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
622 struct pseminfo
*pinfo
;
626 size_t pathlen
, plen
;
628 struct psemcache
*pcache
= PSEMCACHE_NULL
;
630 pinfo
= PSEMINFO_NULL
;
632 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
634 return(ENOSPC
); /* XXX non-standard */
636 pathlen
= MAXPATHLEN
;
637 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
641 AUDIT_ARG(text
, pnbuf
);
642 if (pathlen
> PSEMNAMLEN
) {
643 error
= ENAMETOOLONG
;
648 #ifdef PSXSEM_NAME_RESTRICT
650 if (*nameptr
== '/') {
651 while (*(nameptr
++) == '/') {
660 #endif /* PSXSEM_NAME_RESTRICT */
664 nd
.psem_nameptr
= nameptr
;
665 nd
.psem_namelen
= plen
;
668 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
669 nd
.psem_hash
+= (unsigned char)*cp
* i
;
673 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
675 if (error
== ENOENT
) {
676 PSEM_SUBSYS_UNLOCK();
682 PSEM_SUBSYS_UNLOCK();
687 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
688 PSEM_SUBSYS_UNLOCK();
692 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
693 PSEM_SUBSYS_UNLOCK();
697 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
698 PSEM_SUBSYS_UNLOCK();
703 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
706 pinfo
->psem_flags
|= PSEM_INDELETE
;
707 pinfo
->psem_usecount
--;
709 if (!pinfo
->psem_usecount
) {
713 pinfo
->psem_flags
|= PSEM_REMOVED
;
715 psem_cache_delete(pcache
);
716 PSEM_SUBSYS_UNLOCK();
720 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
725 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
727 int fd
= CAST_DOWN(int,uap
->sem
);
731 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
734 error
= fp_lookup(p
,fd
, &fp
, 1);
740 error
= closef_locked(fp
, fp
->f_fglob
, p
);
741 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
747 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
749 int fd
= CAST_DOWN(int,uap
->sem
);
751 struct pseminfo
* pinfo
;
752 struct psemnode
* pnode
;
756 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
759 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
764 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
765 PSEM_SUBSYS_UNLOCK();
769 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
771 PSEM_SUBSYS_UNLOCK();
776 PSEM_SUBSYS_UNLOCK();
777 kret
= semaphore_wait(pinfo
->psem_semobject
);
779 case KERN_INVALID_ADDRESS
:
780 case KERN_PROTECTION_FAILURE
:
784 case KERN_OPERATION_TIMED_OUT
:
795 fp_drop(p
, fd
, fp
, 0);
801 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
803 int fd
= CAST_DOWN(int,uap
->sem
);
805 struct pseminfo
* pinfo
;
806 struct psemnode
* pnode
;
808 mach_timespec_t wait_time
;
811 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
814 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
819 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
820 PSEM_SUBSYS_UNLOCK();
824 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
826 PSEM_SUBSYS_UNLOCK();
831 PSEM_SUBSYS_UNLOCK();
832 wait_time
.tv_sec
= 0;
833 wait_time
.tv_nsec
= 0;
835 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
837 case KERN_INVALID_ADDRESS
:
838 case KERN_PROTECTION_FAILURE
:
844 case KERN_OPERATION_TIMED_OUT
:
855 fp_drop(p
, fd
, fp
, 0);
860 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
862 int fd
= CAST_DOWN(int,uap
->sem
);
864 struct pseminfo
* pinfo
;
865 struct psemnode
* pnode
;
869 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
872 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
877 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
878 PSEM_SUBSYS_UNLOCK();
882 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
884 PSEM_SUBSYS_UNLOCK();
889 PSEM_SUBSYS_UNLOCK();
890 kret
= semaphore_signal(pinfo
->psem_semobject
);
892 case KERN_INVALID_ADDRESS
:
893 case KERN_PROTECTION_FAILURE
:
897 case KERN_OPERATION_TIMED_OUT
:
908 fp_drop(p
, fd
, fp
, 0);
913 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
919 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
925 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
931 psem_close(struct psemnode
*pnode
, __unused
int flags
,
932 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
935 register struct pseminfo
*pinfo
;
938 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
939 PSEM_SUBSYS_UNLOCK();
943 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
944 PSEM_SUBSYS_UNLOCK();
948 if(!pinfo
->psem_usecount
) {
949 kprintf("negative usecount in psem_close\n");
951 #endif /* DIAGNOSTIC */
952 pinfo
->psem_usecount
--;
954 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
955 PSEM_SUBSYS_UNLOCK();
956 /* lock dropped as only semaphore is destroyed here */
957 error
= psem_delete(pinfo
);
960 PSEM_SUBSYS_UNLOCK();
962 /* subsystem lock is dropped when we get here */
968 psem_closefile(fg
, p
)
974 /* Not locked as psem_close is called only from here and is locked properly */
975 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
982 psem_delete(struct pseminfo
* pinfo
)
986 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
989 case KERN_INVALID_ADDRESS
:
990 case KERN_PROTECTION_FAILURE
:
993 case KERN_OPERATION_TIMED_OUT
:
1003 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1004 __unused kauth_cred_t cred
, __unused
int flags
,
1005 __unused
struct proc
*p
)
1011 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1012 __unused kauth_cred_t cred
, __unused
int flags
,
1013 __unused
struct proc
*p
)
1019 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1020 __unused caddr_t data
, __unused
struct proc
*p
)
1026 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1027 __unused
void *wql
, __unused
struct proc
*p
)
1033 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1034 __unused
struct proc
*p
)
1040 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1042 register struct pseminfo
*pinfo
;
1046 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1047 PSEM_SUBSYS_UNLOCK();
1052 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1053 PSEM_SUBSYS_UNLOCK();
1058 sb
= &info
->psem_stat
;
1059 bzero(sb
, sizeof(struct stat
));
1061 sb
->st_mode
= pinfo
->psem_mode
;
1062 sb
->st_uid
= pinfo
->psem_uid
;
1063 sb
->st_gid
= pinfo
->psem_gid
;
1064 sb
->st_size
= pinfo
->psem_usecount
;
1065 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1067 PSEM_SUBSYS_UNLOCK();