2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
24 * All Rights Reserved.
27 * posix_shm.c : Support for POSIX semaphore APIs
30 * Author: Ananthakrishna Ramesh
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/file_internal.h>
43 #include <sys/filedesc.h>
45 #include <sys/proc_internal.h>
46 #include <sys/kauth.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/ioctl.h>
52 #include <sys/malloc.h>
53 #include <sys/semaphore.h>
54 #include <sys/sysproto.h>
56 #include <bsm/audit_kernel.h>
58 #include <mach/mach_types.h>
59 #include <mach/vm_prot.h>
60 #include <mach/semaphore.h>
61 #include <mach/sync_policy.h>
62 #include <mach/task.h>
63 #include <kern/kern_types.h>
64 #include <kern/task.h>
65 #include <kern/clock.h>
66 #include <mach/kern_return.h>
69 #include <sys/ktrace.h>
72 #define f_flag f_fglob->fg_flag
73 #define f_type f_fglob->fg_type
74 #define f_msgcount f_fglob->fg_msgcount
75 #define f_cred f_fglob->fg_cred
76 #define f_ops f_fglob->fg_ops
77 #define f_offset f_fglob->fg_offset
78 #define f_data f_fglob->fg_data
79 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
82 unsigned int psem_flags
;
83 unsigned int psem_usecount
;
87 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
88 semaphore_t psem_semobject
;
89 struct proc
* sem_proc
;
91 #define PSEMINFO_NULL (struct pseminfo *)0
94 #define PSEM_DEFINED 2
95 #define PSEM_ALLOCATED 4
97 #define PSEM_INUSE 0x10
98 #define PSEM_REMOVED 0x20
99 #define PSEM_INCREATE 0x40
100 #define PSEM_INDELETE 0x80
103 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
104 struct pseminfo
*pseminfo
; /* vnode the name refers to */
105 int psem_nlen
; /* length of name */
106 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
108 #define PSEMCACHE_NULL (struct psemcache *)0
111 long goodhits
; /* hits that we can really use */
112 long neghits
; /* negative hits that we can use */
113 long badhits
; /* hits we must drop */
114 long falsehits
; /* hits with id mismatch */
115 long miss
; /* misses */
116 long longnames
; /* long names that ignore cache */
120 char *psem_nameptr
; /* pointer to looked up name */
121 long psem_namelen
; /* length of looked up component */
122 u_long psem_hash
; /* hash value of looked up name */
126 struct pseminfo
*pinfo
;
128 unsigned int readcnt
;
129 unsigned int writecnt
;
132 #define PSEMNODE_NULL (struct psemnode *)0
135 #define PSEMHASH(pnp) \
136 (&psemhashtbl[(pnp)->psem_hash & psemhash])
137 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
138 u_long psemhash
; /* size of hash table - 1 */
139 long psemnument
; /* number of cache entries allocated */
140 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
141 /* 10000 limits to ~1M of memory */
142 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
143 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
144 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
146 struct psemstats psemstats
; /* cache effectiveness statistics */
148 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
149 static int psem_cache_search(struct pseminfo
**,
150 struct psemname
*, struct psemcache
**);
151 static int psem_delete(struct pseminfo
* pinfo
);
153 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
154 kauth_cred_t cred
, int flags
, struct proc
*p
);
155 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
156 kauth_cred_t cred
, int flags
, struct proc
*p
);
157 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
158 caddr_t data
, struct proc
*p
);
159 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
160 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
162 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
164 struct fileops psemops
=
165 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
168 static lck_grp_t
*psx_sem_subsys_lck_grp
;
169 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
170 static lck_attr_t
*psx_sem_subsys_lck_attr
;
171 static lck_mtx_t psx_sem_subsys_mutex
;
173 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
174 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
177 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
178 /* Initialize the mutex governing access to the posix sem subsystem */
179 __private_extern__
void
180 psem_lock_init( void )
183 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
184 lck_grp_attr_setstat(psx_sem_subsys_lck_grp_attr
);
186 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
188 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
189 /* lck_attr_setdebug(psx_sem_subsys_lck_attr); */
190 lck_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
194 * Lookup an entry in the cache
197 * status of -1 is returned if matches
198 * If the lookup determines that the name does not exist
199 * (negative cacheing), a status of ENOENT is returned. If the lookup
200 * fails, a status of zero is returned.
204 psem_cache_search(psemp
, pnp
, pcache
)
205 struct pseminfo
**psemp
;
206 struct psemname
*pnp
;
207 struct psemcache
**pcache
;
209 struct psemcache
*pcp
, *nnp
;
210 struct psemhashhead
*pcpp
;
212 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
213 psemstats
.longnames
++;
217 pcpp
= PSEMHASH(pnp
);
218 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
219 nnp
= pcp
->psem_hash
.le_next
;
220 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
221 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
230 /* We found a "positive" match, return the vnode */
232 psemstats
.goodhits
++;
234 *psemp
= pcp
->pseminfo
;
240 * We found a "negative" match, ENOENT notifies client of this match.
241 * The nc_vpid field records whether this is a whiteout.
248 * Add an entry to the cache.
251 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
253 struct psemhashhead
*pcpp
;
254 struct pseminfo
*dpinfo
;
255 struct psemcache
*dpcp
;
258 if (pnp
->psem_namelen
> NCHNAMLEN
)
259 panic("cache_enter: name too long");
263 /* if the entry has already been added by some one else return */
264 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
267 if (psemnument
>= posix_sem_max
)
271 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
272 * For negative entries, we have to record whether it is a whiteout.
273 * the whiteout flag is stored in the nc_vpid field which is
276 pcp
->pseminfo
= psemp
;
277 pcp
->psem_nlen
= pnp
->psem_namelen
;
278 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
279 pcpp
= PSEMHASH(pnp
);
284 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
286 panic("psem:cache_enter duplicate");
289 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
294 * Name cache initialization, from vfs_init() when we are booting
297 psem_cache_init(void)
299 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
303 psem_cache_delete(struct psemcache
*pcp
)
306 if (pcp
->psem_hash
.le_prev
== 0)
307 panic("psem namecache purge le_prev");
308 if (pcp
->psem_hash
.le_next
== pcp
)
309 panic("namecache purge le_next");
310 #endif /* DIAGNOSTIC */
311 LIST_REMOVE(pcp
, psem_hash
);
312 pcp
->psem_hash
.le_prev
= 0;
318 * Invalidate a all entries to particular vnode.
320 * We actually just increment the v_id, that will do it. The entries will
321 * be purged by lookup as they get found. If the v_id wraps around, we
322 * need to ditch the entire cache, to avoid confusion. No valid vnode will
323 * ever have (v_id == 0).
326 psem_cache_purge(void)
328 struct psemcache
*pcp
;
329 struct psemhashhead
*pcpp
;
331 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
332 while ( (pcp
= pcpp
->lh_first
) )
333 psem_cache_delete(pcp
);
336 #endif /* NOT_USED */
339 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
343 struct fileproc
*nfp
;
346 struct pseminfo
*pinfo
;
347 struct psemcache
*pcp
;
351 size_t pathlen
, plen
;
353 int cmode
= uap
->mode
;
354 int value
= uap
->value
;
356 struct psemnode
* pnode
= PSEMNODE_NULL
;
357 struct psemcache
* pcache
= PSEMCACHE_NULL
;
358 kern_return_t kret
= KERN_SUCCESS
;
361 AUDIT_ARG(fflags
, uap
->oflag
);
362 AUDIT_ARG(mode
, uap
->mode
);
363 AUDIT_ARG(value
, uap
->value
);
365 pinfo
= PSEMINFO_NULL
;
367 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
371 pathlen
= MAXPATHLEN
;
372 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
376 AUDIT_ARG(text
, pnbuf
);
377 if ( (pathlen
> PSEMNAMLEN
) ) {
378 error
= ENAMETOOLONG
;
382 #ifdef PSXSEM_NAME_RESTRICT
384 if (*nameptr
== '/') {
385 while (*(nameptr
++) == '/') {
394 #endif /* PSXSEM_NAME_RESTRICT */
398 nd
.psem_nameptr
= nameptr
;
399 nd
.psem_namelen
= plen
;
402 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
403 nd
.psem_hash
+= (unsigned char)*cp
* i
;
407 if (KTRPOINT(p
, KTR_NAMEI
))
408 ktrnamei(p
->p_tracep
, nameptr
);
412 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
414 if (error
== ENOENT
) {
415 PSEM_SUBSYS_UNLOCK();
424 fmode
= FFLAGS(uap
->oflag
);
426 PSEM_SUBSYS_UNLOCK();
427 error
= falloc(p
, &nfp
, &indx
);
435 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
436 /* sem exists and opened O_EXCL */
438 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
441 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
442 pinfo
->psem_gid
, pinfo
->psem_mode
);
443 PSEM_SUBSYS_UNLOCK();
447 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
448 /* As per POSIX, O_CREAT has no effect */
452 if ( (fmode
& O_CREAT
) ) {
453 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
454 PSEM_SUBSYS_UNLOCK();
458 PSEM_SUBSYS_UNLOCK();
459 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
467 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
468 pinfo
->psem_usecount
= 1;
469 pinfo
->psem_mode
= cmode
;
470 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
471 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
472 PSEM_SUBSYS_UNLOCK();
473 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
474 SYNC_POLICY_FIFO
, value
);
475 if(kret
!= KERN_SUCCESS
)
478 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
479 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
482 /* semaphore should exist as it is without O_CREAT */
484 PSEM_SUBSYS_UNLOCK();
488 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
489 PSEM_SUBSYS_UNLOCK();
493 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
494 pinfo
->psem_gid
, pinfo
->psem_mode
);
495 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
496 PSEM_SUBSYS_UNLOCK();
500 PSEM_SUBSYS_UNLOCK();
501 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
508 * We allocate a new entry if we are less than the maximum
509 * allowed and the one at the front of the LRU list is in use.
510 * Otherwise we use the one at the front of the LRU list.
512 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
521 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
522 PSEM_SUBSYS_UNLOCK();
527 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
528 pinfo
->psem_usecount
++;
529 pnode
->pinfo
= pinfo
;
530 PSEM_SUBSYS_UNLOCK();
533 fp
->f_flag
= fmode
& FMASK
;
534 fp
->f_type
= DTYPE_PSXSEM
;
535 fp
->f_ops
= &psemops
;
536 fp
->f_data
= (caddr_t
)pnode
;
537 procfdtbl_releasefd(p
, indx
, NULL
);
538 fp_drop(p
, indx
, fp
, 1);
541 *retval
= CAST_USER_ADDR_T(indx
);
542 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
547 case KERN_RESOURCE_SHORTAGE
:
549 case KERN_PROTECTION_FAILURE
:
560 fp_free(p
, indx
, nfp
);
562 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
567 * XXX This code is repeated in several places
570 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
575 /* Otherwise, user id 0 always gets access. */
576 if (!suser(cred
, NULL
))
581 /* Otherwise, check the owner. */
582 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
587 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
590 /* Otherwise, check the groups. */
591 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
596 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
599 /* Otherwise, check everyone else. */
604 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
608 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
613 struct pseminfo
*pinfo
;
617 size_t pathlen
, plen
;
619 struct psemcache
*pcache
= PSEMCACHE_NULL
;
621 pinfo
= PSEMINFO_NULL
;
623 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
625 return(ENOSPC
); /* XXX non-standard */
627 pathlen
= MAXPATHLEN
;
628 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
632 AUDIT_ARG(text
, pnbuf
);
633 if (pathlen
> PSEMNAMLEN
) {
634 error
= ENAMETOOLONG
;
639 #ifdef PSXSEM_NAME_RESTRICT
641 if (*nameptr
== '/') {
642 while (*(nameptr
++) == '/') {
651 #endif /* PSXSEM_NAME_RESTRICT */
655 nd
.psem_nameptr
= nameptr
;
656 nd
.psem_namelen
= plen
;
659 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
660 nd
.psem_hash
+= (unsigned char)*cp
* i
;
664 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
666 if (error
== ENOENT
) {
667 PSEM_SUBSYS_UNLOCK();
673 PSEM_SUBSYS_UNLOCK();
678 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
679 PSEM_SUBSYS_UNLOCK();
683 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
684 PSEM_SUBSYS_UNLOCK();
688 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
689 PSEM_SUBSYS_UNLOCK();
694 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
697 pinfo
->psem_flags
|= PSEM_INDELETE
;
698 pinfo
->psem_usecount
--;
700 if (!pinfo
->psem_usecount
) {
704 pinfo
->psem_flags
|= PSEM_REMOVED
;
706 psem_cache_delete(pcache
);
707 PSEM_SUBSYS_UNLOCK();
711 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
716 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
718 int fd
= CAST_DOWN(int,uap
->sem
);
722 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
725 error
= fp_lookup(p
,fd
, &fp
, 1);
731 error
= closef_locked(fp
, fp
->f_fglob
, p
);
732 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
738 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
740 int fd
= CAST_DOWN(int,uap
->sem
);
742 struct pseminfo
* pinfo
;
743 struct psemnode
* pnode
;
747 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
750 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
755 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
756 PSEM_SUBSYS_UNLOCK();
760 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
762 PSEM_SUBSYS_UNLOCK();
767 PSEM_SUBSYS_UNLOCK();
768 kret
= semaphore_wait(pinfo
->psem_semobject
);
770 case KERN_INVALID_ADDRESS
:
771 case KERN_PROTECTION_FAILURE
:
775 case KERN_OPERATION_TIMED_OUT
:
786 fp_drop(p
, fd
, fp
, 0);
792 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
794 int fd
= CAST_DOWN(int,uap
->sem
);
796 struct pseminfo
* pinfo
;
797 struct psemnode
* pnode
;
799 mach_timespec_t wait_time
;
802 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
805 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
810 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
811 PSEM_SUBSYS_UNLOCK();
815 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
817 PSEM_SUBSYS_UNLOCK();
822 PSEM_SUBSYS_UNLOCK();
823 wait_time
.tv_sec
= 0;
824 wait_time
.tv_nsec
= 0;
826 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
828 case KERN_INVALID_ADDRESS
:
829 case KERN_PROTECTION_FAILURE
:
835 case KERN_OPERATION_TIMED_OUT
:
846 fp_drop(p
, fd
, fp
, 0);
851 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
853 int fd
= CAST_DOWN(int,uap
->sem
);
855 struct pseminfo
* pinfo
;
856 struct psemnode
* pnode
;
860 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
863 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
868 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
869 PSEM_SUBSYS_UNLOCK();
873 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
875 PSEM_SUBSYS_UNLOCK();
880 PSEM_SUBSYS_UNLOCK();
881 kret
= semaphore_signal(pinfo
->psem_semobject
);
883 case KERN_INVALID_ADDRESS
:
884 case KERN_PROTECTION_FAILURE
:
888 case KERN_OPERATION_TIMED_OUT
:
899 fp_drop(p
, fd
, fp
, 0);
904 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
910 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
916 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
922 psem_close(struct psemnode
*pnode
, __unused
int flags
,
923 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
926 register struct pseminfo
*pinfo
;
929 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
930 PSEM_SUBSYS_UNLOCK();
934 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
935 PSEM_SUBSYS_UNLOCK();
939 if(!pinfo
->psem_usecount
) {
940 kprintf("negative usecount in psem_close\n");
942 #endif /* DIAGNOSTIC */
943 pinfo
->psem_usecount
--;
945 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
946 PSEM_SUBSYS_UNLOCK();
947 /* lock dropped as only semaphore is destroyed here */
948 error
= psem_delete(pinfo
);
951 PSEM_SUBSYS_UNLOCK();
953 /* subsystem lock is dropped when we get here */
959 psem_closefile(fg
, p
)
965 /* Not locked as psem_close is called only from here and is locked properly */
966 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
973 psem_delete(struct pseminfo
* pinfo
)
977 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
980 case KERN_INVALID_ADDRESS
:
981 case KERN_PROTECTION_FAILURE
:
984 case KERN_OPERATION_TIMED_OUT
:
994 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
995 __unused kauth_cred_t cred
, __unused
int flags
,
996 __unused
struct proc
*p
)
1002 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1003 __unused kauth_cred_t cred
, __unused
int flags
,
1004 __unused
struct proc
*p
)
1010 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1011 __unused caddr_t data
, __unused
struct proc
*p
)
1017 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1018 __unused
void *wql
, __unused
struct proc
*p
)
1024 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1025 __unused
struct proc
*p
)