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>
55 #include <sys/proc_info.h>
57 #include <bsm/audit_kernel.h>
59 #include <mach/mach_types.h>
60 #include <mach/vm_prot.h>
61 #include <mach/semaphore.h>
62 #include <mach/sync_policy.h>
63 #include <mach/task.h>
64 #include <kern/kern_types.h>
65 #include <kern/task.h>
66 #include <kern/clock.h>
67 #include <mach/kern_return.h>
70 #include <sys/ktrace.h>
73 #define f_flag f_fglob->fg_flag
74 #define f_type f_fglob->fg_type
75 #define f_msgcount f_fglob->fg_msgcount
76 #define f_cred f_fglob->fg_cred
77 #define f_ops f_fglob->fg_ops
78 #define f_offset f_fglob->fg_offset
79 #define f_data f_fglob->fg_data
80 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
83 unsigned int psem_flags
;
84 unsigned int psem_usecount
;
88 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
89 semaphore_t psem_semobject
;
90 struct proc
* sem_proc
;
92 #define PSEMINFO_NULL (struct pseminfo *)0
95 #define PSEM_DEFINED 2
96 #define PSEM_ALLOCATED 4
98 #define PSEM_INUSE 0x10
99 #define PSEM_REMOVED 0x20
100 #define PSEM_INCREATE 0x40
101 #define PSEM_INDELETE 0x80
104 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
105 struct pseminfo
*pseminfo
; /* vnode the name refers to */
106 int psem_nlen
; /* length of name */
107 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
109 #define PSEMCACHE_NULL (struct psemcache *)0
112 long goodhits
; /* hits that we can really use */
113 long neghits
; /* negative hits that we can use */
114 long badhits
; /* hits we must drop */
115 long falsehits
; /* hits with id mismatch */
116 long miss
; /* misses */
117 long longnames
; /* long names that ignore cache */
121 char *psem_nameptr
; /* pointer to looked up name */
122 long psem_namelen
; /* length of looked up component */
123 u_long psem_hash
; /* hash value of looked up name */
127 struct pseminfo
*pinfo
;
129 unsigned int readcnt
;
130 unsigned int writecnt
;
133 #define PSEMNODE_NULL (struct psemnode *)0
136 #define PSEMHASH(pnp) \
137 (&psemhashtbl[(pnp)->psem_hash & psemhash])
138 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
139 u_long psemhash
; /* size of hash table - 1 */
140 long psemnument
; /* number of cache entries allocated */
141 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
142 /* 10000 limits to ~1M of memory */
143 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
, 0, "Posix");
144 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
, 0, "Semaphores");
145 SYSCTL_INT (_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
, &posix_sem_max
, 0, "max");
147 struct psemstats psemstats
; /* cache effectiveness statistics */
149 static int psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
);
150 static int psem_cache_search(struct pseminfo
**,
151 struct psemname
*, struct psemcache
**);
152 static int psem_delete(struct pseminfo
* pinfo
);
154 static int psem_read (struct fileproc
*fp
, struct uio
*uio
,
155 kauth_cred_t cred
, int flags
, struct proc
*p
);
156 static int psem_write (struct fileproc
*fp
, struct uio
*uio
,
157 kauth_cred_t cred
, int flags
, struct proc
*p
);
158 static int psem_ioctl (struct fileproc
*fp
, u_long com
,
159 caddr_t data
, struct proc
*p
);
160 static int psem_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
161 static int psem_closefile (struct fileglob
*fp
, struct proc
*p
);
163 static int psem_kqfilter (struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
165 struct fileops psemops
=
166 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
, 0 };
169 static lck_grp_t
*psx_sem_subsys_lck_grp
;
170 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
171 static lck_attr_t
*psx_sem_subsys_lck_attr
;
172 static lck_mtx_t psx_sem_subsys_mutex
;
174 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
175 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
178 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
179 /* Initialize the mutex governing access to the posix sem subsystem */
180 __private_extern__
void
181 psem_lock_init( void )
184 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
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_mtx_init(& psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
193 * Lookup an entry in the cache
196 * status of -1 is returned if matches
197 * If the lookup determines that the name does not exist
198 * (negative cacheing), a status of ENOENT is returned. If the lookup
199 * fails, a status of zero is returned.
203 psem_cache_search(psemp
, pnp
, pcache
)
204 struct pseminfo
**psemp
;
205 struct psemname
*pnp
;
206 struct psemcache
**pcache
;
208 struct psemcache
*pcp
, *nnp
;
209 struct psemhashhead
*pcpp
;
211 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
212 psemstats
.longnames
++;
216 pcpp
= PSEMHASH(pnp
);
217 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
218 nnp
= pcp
->psem_hash
.le_next
;
219 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
220 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
229 /* We found a "positive" match, return the vnode */
231 psemstats
.goodhits
++;
233 *psemp
= pcp
->pseminfo
;
239 * We found a "negative" match, ENOENT notifies client of this match.
240 * The nc_vpid field records whether this is a whiteout.
247 * Add an entry to the cache.
250 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
252 struct psemhashhead
*pcpp
;
253 struct pseminfo
*dpinfo
;
254 struct psemcache
*dpcp
;
257 if (pnp
->psem_namelen
> NCHNAMLEN
)
258 panic("cache_enter: name too long");
262 /* if the entry has already been added by some one else return */
263 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
266 if (psemnument
>= posix_sem_max
)
270 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
271 * For negative entries, we have to record whether it is a whiteout.
272 * the whiteout flag is stored in the nc_vpid field which is
275 pcp
->pseminfo
= psemp
;
276 pcp
->psem_nlen
= pnp
->psem_namelen
;
277 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
278 pcpp
= PSEMHASH(pnp
);
283 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
285 panic("psem:cache_enter duplicate");
288 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
293 * Name cache initialization, from vfs_init() when we are booting
296 psem_cache_init(void)
298 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
302 psem_cache_delete(struct psemcache
*pcp
)
305 if (pcp
->psem_hash
.le_prev
== 0)
306 panic("psem namecache purge le_prev");
307 if (pcp
->psem_hash
.le_next
== pcp
)
308 panic("namecache purge le_next");
309 #endif /* DIAGNOSTIC */
310 LIST_REMOVE(pcp
, psem_hash
);
311 pcp
->psem_hash
.le_prev
= 0;
317 * Invalidate a all entries to particular vnode.
319 * We actually just increment the v_id, that will do it. The entries will
320 * be purged by lookup as they get found. If the v_id wraps around, we
321 * need to ditch the entire cache, to avoid confusion. No valid vnode will
322 * ever have (v_id == 0).
325 psem_cache_purge(void)
327 struct psemcache
*pcp
;
328 struct psemhashhead
*pcpp
;
330 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
331 while ( (pcp
= pcpp
->lh_first
) )
332 psem_cache_delete(pcp
);
335 #endif /* NOT_USED */
338 sem_open(struct proc
*p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
342 struct fileproc
*nfp
;
345 struct pseminfo
*pinfo
;
346 struct psemcache
*pcp
;
350 size_t pathlen
, plen
;
352 int cmode
= uap
->mode
;
353 int value
= uap
->value
;
355 struct psemnode
* pnode
= PSEMNODE_NULL
;
356 struct psemcache
* pcache
= PSEMCACHE_NULL
;
357 kern_return_t kret
= KERN_SUCCESS
;
360 AUDIT_ARG(fflags
, uap
->oflag
);
361 AUDIT_ARG(mode
, uap
->mode
);
362 AUDIT_ARG(value
, uap
->value
);
364 pinfo
= PSEMINFO_NULL
;
366 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
370 pathlen
= MAXPATHLEN
;
371 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
375 AUDIT_ARG(text
, pnbuf
);
376 if ( (pathlen
> PSEMNAMLEN
) ) {
377 error
= ENAMETOOLONG
;
381 #ifdef PSXSEM_NAME_RESTRICT
383 if (*nameptr
== '/') {
384 while (*(nameptr
++) == '/') {
393 #endif /* PSXSEM_NAME_RESTRICT */
397 nd
.psem_nameptr
= nameptr
;
398 nd
.psem_namelen
= plen
;
401 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
402 nd
.psem_hash
+= (unsigned char)*cp
* i
;
406 if (KTRPOINT(p
, KTR_NAMEI
))
407 ktrnamei(p
->p_tracep
, nameptr
);
411 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
413 if (error
== ENOENT
) {
414 PSEM_SUBSYS_UNLOCK();
423 fmode
= FFLAGS(uap
->oflag
);
425 PSEM_SUBSYS_UNLOCK();
426 error
= falloc(p
, &nfp
, &indx
);
434 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
435 /* sem exists and opened O_EXCL */
437 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
440 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
441 pinfo
->psem_gid
, pinfo
->psem_mode
);
442 PSEM_SUBSYS_UNLOCK();
446 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
447 /* As per POSIX, O_CREAT has no effect */
451 if ( (fmode
& O_CREAT
) ) {
452 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
453 PSEM_SUBSYS_UNLOCK();
457 PSEM_SUBSYS_UNLOCK();
458 MALLOC(pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
466 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
467 pinfo
->psem_usecount
= 1;
468 pinfo
->psem_mode
= cmode
;
469 pinfo
->psem_uid
= kauth_cred_getuid(kauth_cred_get());
470 pinfo
->psem_gid
= kauth_cred_get()->cr_gid
;
471 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
472 pinfo
->psem_name
[PSEMNAMLEN
]= 0;
473 PSEM_SUBSYS_UNLOCK();
474 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
475 SYNC_POLICY_FIFO
, value
);
476 if(kret
!= KERN_SUCCESS
)
479 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
480 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
483 /* semaphore should exist as it is without O_CREAT */
485 PSEM_SUBSYS_UNLOCK();
489 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
490 PSEM_SUBSYS_UNLOCK();
494 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
495 pinfo
->psem_gid
, pinfo
->psem_mode
);
496 if ( (error
= psem_access(pinfo
, fmode
, kauth_cred_get())) ) {
497 PSEM_SUBSYS_UNLOCK();
501 PSEM_SUBSYS_UNLOCK();
502 MALLOC(pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
|M_ZERO
);
509 * We allocate a new entry if we are less than the maximum
510 * allowed and the one at the front of the LRU list is in use.
511 * Otherwise we use the one at the front of the LRU list.
513 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
|M_ZERO
);
522 if ( (error
= psem_cache_add(pinfo
, &nd
, pcp
)) ) {
523 PSEM_SUBSYS_UNLOCK();
528 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
529 pinfo
->psem_usecount
++;
530 pnode
->pinfo
= pinfo
;
531 PSEM_SUBSYS_UNLOCK();
534 fp
->f_flag
= fmode
& FMASK
;
535 fp
->f_type
= DTYPE_PSXSEM
;
536 fp
->f_ops
= &psemops
;
537 fp
->f_data
= (caddr_t
)pnode
;
538 *fdflags(p
, indx
) &= ~UF_RESERVED
;
539 fp_drop(p
, indx
, fp
, 1);
542 *retval
= CAST_USER_ADDR_T(indx
);
543 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
548 case KERN_RESOURCE_SHORTAGE
:
550 case KERN_PROTECTION_FAILURE
:
561 fp_free(p
, indx
, nfp
);
563 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
568 * XXX This code is repeated in several places
571 psem_access(struct pseminfo
*pinfo
, int mode
, kauth_cred_t cred
)
576 /* Otherwise, user id 0 always gets access. */
577 if (!suser(cred
, NULL
))
582 /* Otherwise, check the owner. */
583 if (kauth_cred_getuid(cred
) == pinfo
->psem_uid
) {
588 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
591 /* Otherwise, check the groups. */
592 if (kauth_cred_ismember_gid(cred
, pinfo
->psem_gid
, &is_member
) == 0 && is_member
) {
597 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
600 /* Otherwise, check everyone else. */
605 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
609 sem_unlink(__unused
struct proc
*p
, struct sem_unlink_args
*uap
, __unused register_t
*retval
)
614 struct pseminfo
*pinfo
;
618 size_t pathlen
, plen
;
620 struct psemcache
*pcache
= PSEMCACHE_NULL
;
622 pinfo
= PSEMINFO_NULL
;
624 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
626 return(ENOSPC
); /* XXX non-standard */
628 pathlen
= MAXPATHLEN
;
629 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
633 AUDIT_ARG(text
, pnbuf
);
634 if (pathlen
> PSEMNAMLEN
) {
635 error
= ENAMETOOLONG
;
640 #ifdef PSXSEM_NAME_RESTRICT
642 if (*nameptr
== '/') {
643 while (*(nameptr
++) == '/') {
652 #endif /* PSXSEM_NAME_RESTRICT */
656 nd
.psem_nameptr
= nameptr
;
657 nd
.psem_namelen
= plen
;
660 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
661 nd
.psem_hash
+= (unsigned char)*cp
* i
;
665 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
667 if (error
== ENOENT
) {
668 PSEM_SUBSYS_UNLOCK();
674 PSEM_SUBSYS_UNLOCK();
679 if ( (error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get())) ) {
680 PSEM_SUBSYS_UNLOCK();
684 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
685 PSEM_SUBSYS_UNLOCK();
689 if ( (pinfo
->psem_flags
& PSEM_INDELETE
) ) {
690 PSEM_SUBSYS_UNLOCK();
695 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
698 pinfo
->psem_flags
|= PSEM_INDELETE
;
699 pinfo
->psem_usecount
--;
701 if (!pinfo
->psem_usecount
) {
705 pinfo
->psem_flags
|= PSEM_REMOVED
;
707 psem_cache_delete(pcache
);
708 PSEM_SUBSYS_UNLOCK();
712 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
717 sem_close(struct proc
*p
, struct sem_close_args
*uap
, __unused register_t
*retval
)
719 int fd
= CAST_DOWN(int,uap
->sem
);
723 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
726 error
= fp_lookup(p
,fd
, &fp
, 1);
732 error
= closef_locked(fp
, fp
->f_fglob
, p
);
733 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
739 sem_wait(struct proc
*p
, struct sem_wait_args
*uap
, __unused register_t
*retval
)
741 int fd
= CAST_DOWN(int,uap
->sem
);
743 struct pseminfo
* pinfo
;
744 struct psemnode
* pnode
;
748 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
751 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
756 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
757 PSEM_SUBSYS_UNLOCK();
761 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
763 PSEM_SUBSYS_UNLOCK();
768 PSEM_SUBSYS_UNLOCK();
769 kret
= semaphore_wait(pinfo
->psem_semobject
);
771 case KERN_INVALID_ADDRESS
:
772 case KERN_PROTECTION_FAILURE
:
776 case KERN_OPERATION_TIMED_OUT
:
787 fp_drop(p
, fd
, fp
, 0);
793 sem_trywait(struct proc
*p
, struct sem_trywait_args
*uap
, __unused register_t
*retval
)
795 int fd
= CAST_DOWN(int,uap
->sem
);
797 struct pseminfo
* pinfo
;
798 struct psemnode
* pnode
;
800 mach_timespec_t wait_time
;
803 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
806 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
811 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
812 PSEM_SUBSYS_UNLOCK();
816 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
818 PSEM_SUBSYS_UNLOCK();
823 PSEM_SUBSYS_UNLOCK();
824 wait_time
.tv_sec
= 0;
825 wait_time
.tv_nsec
= 0;
827 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
829 case KERN_INVALID_ADDRESS
:
830 case KERN_PROTECTION_FAILURE
:
836 case KERN_OPERATION_TIMED_OUT
:
847 fp_drop(p
, fd
, fp
, 0);
852 sem_post(struct proc
*p
, struct sem_post_args
*uap
, __unused register_t
*retval
)
854 int fd
= CAST_DOWN(int,uap
->sem
);
856 struct pseminfo
* pinfo
;
857 struct psemnode
* pnode
;
861 error
= fp_getfpsem(p
, fd
, &fp
, &pnode
);
864 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
) {
869 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
870 PSEM_SUBSYS_UNLOCK();
874 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
876 PSEM_SUBSYS_UNLOCK();
881 PSEM_SUBSYS_UNLOCK();
882 kret
= semaphore_signal(pinfo
->psem_semobject
);
884 case KERN_INVALID_ADDRESS
:
885 case KERN_PROTECTION_FAILURE
:
889 case KERN_OPERATION_TIMED_OUT
:
900 fp_drop(p
, fd
, fp
, 0);
905 sem_init(__unused
struct proc
*p
, __unused
struct sem_init_args
*uap
, __unused register_t
*retval
)
911 sem_destroy(__unused
struct proc
*p
, __unused
struct sem_destroy_args
*uap
, __unused register_t
*retval
)
917 sem_getvalue(__unused
struct proc
*p
, __unused
struct sem_getvalue_args
*uap
, __unused register_t
*retval
)
923 psem_close(struct psemnode
*pnode
, __unused
int flags
,
924 __unused kauth_cred_t cred
, __unused
struct proc
*p
)
927 register struct pseminfo
*pinfo
;
930 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
931 PSEM_SUBSYS_UNLOCK();
935 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
936 PSEM_SUBSYS_UNLOCK();
940 if(!pinfo
->psem_usecount
) {
941 kprintf("negative usecount in psem_close\n");
943 #endif /* DIAGNOSTIC */
944 pinfo
->psem_usecount
--;
946 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
947 PSEM_SUBSYS_UNLOCK();
948 /* lock dropped as only semaphore is destroyed here */
949 error
= psem_delete(pinfo
);
952 PSEM_SUBSYS_UNLOCK();
954 /* subsystem lock is dropped when we get here */
960 psem_closefile(fg
, p
)
966 /* Not locked as psem_close is called only from here and is locked properly */
967 error
= psem_close(((struct psemnode
*)fg
->fg_data
), fg
->fg_flag
,
974 psem_delete(struct pseminfo
* pinfo
)
978 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
981 case KERN_INVALID_ADDRESS
:
982 case KERN_PROTECTION_FAILURE
:
985 case KERN_OPERATION_TIMED_OUT
:
995 psem_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
996 __unused kauth_cred_t cred
, __unused
int flags
,
997 __unused
struct proc
*p
)
1003 psem_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1004 __unused kauth_cred_t cred
, __unused
int flags
,
1005 __unused
struct proc
*p
)
1011 psem_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1012 __unused caddr_t data
, __unused
struct proc
*p
)
1018 psem_select(__unused
struct fileproc
*fp
, __unused
int which
,
1019 __unused
void *wql
, __unused
struct proc
*p
)
1025 psem_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1026 __unused
struct proc
*p
)
1032 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1034 register struct pseminfo
*pinfo
;
1038 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
){
1039 PSEM_SUBSYS_UNLOCK();
1044 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1045 PSEM_SUBSYS_UNLOCK();
1050 sb
= &info
->psem_stat
;
1051 bzero(sb
, sizeof(struct stat
));
1053 sb
->st_mode
= pinfo
->psem_mode
;
1054 sb
->st_uid
= pinfo
->psem_uid
;
1055 sb
->st_gid
= pinfo
->psem_gid
;
1056 sb
->st_size
= pinfo
->psem_usecount
;
1057 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+1);
1059 PSEM_SUBSYS_UNLOCK();