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>
43 #include <sys/filedesc.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>
55 #include <bsm/audit_kernel.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_prot.h>
59 #include <mach/semaphore.h>
60 #include <mach/sync_policy.h>
61 #include <kern/task.h>
62 #include <kern/clock.h>
63 #include <mach/kern_return.h>
65 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
68 unsigned int psem_flags
;
69 unsigned int psem_usecount
;
73 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
74 void * psem_semobject
;
75 struct proc
* sem_proc
;
77 #define PSEMINFO_NULL (struct pseminfo *)0
80 #define PSEM_DEFINED 2
81 #define PSEM_ALLOCATED 4
83 #define PSEM_INUSE 0x10
84 #define PSEM_REMOVED 0x20
85 #define PSEM_INCREATE 0x40
86 #define PSEM_INDELETE 0x80
89 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
90 struct pseminfo
*pseminfo
; /* vnode the name refers to */
91 int psem_nlen
; /* length of name */
92 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
94 #define PSEMCACHE_NULL (struct psemcache *)0
97 long goodhits
; /* hits that we can really use */
98 long neghits
; /* negative hits that we can use */
99 long badhits
; /* hits we must drop */
100 long falsehits
; /* hits with id mismatch */
101 long miss
; /* misses */
102 long longnames
; /* long names that ignore cache */
106 char *psem_nameptr
; /* pointer to looked up name */
107 long psem_namelen
; /* length of looked up component */
108 u_long psem_hash
; /* hash value of looked up name */
112 struct pseminfo
*pinfo
;
114 unsigned int readcnt
;
115 unsigned int writecnt
;
118 #define PSEMNODE_NULL (struct psemnode *)0
121 #define PSEMHASH(pnp) \
122 (&psemhashtbl[(pnp)->psem_hash & psemhash])
123 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
124 u_long psemhash
; /* size of hash table - 1 */
125 long psemnument
; /* number of cache entries allocated */
126 struct psemstats psemstats
; /* cache effectiveness statistics */
128 static int psem_cache_search
__P((struct pseminfo
**,
129 struct psemname
*, struct psemcache
**));
131 static int psem_read
__P((struct file
*fp
, struct uio
*uio
,
132 struct ucred
*cred
, int flags
, struct proc
*p
));
133 static int psem_write
__P((struct file
*fp
, struct uio
*uio
,
134 struct ucred
*cred
, int flags
, struct proc
*p
));
135 static int psem_ioctl
__P((struct file
*fp
, u_long com
,
136 caddr_t data
, struct proc
*p
));
137 static int psem_select
__P((struct file
*fp
, int which
, void *wql
,
139 static int psem_closefile
__P((struct file
*fp
, struct proc
*p
));
141 static int psem_kqfilter
__P((struct file
*fp
, struct knote
*kn
, struct proc
*p
));
143 struct fileops psemops
=
144 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
, psem_kqfilter
};
147 * Lookup an entry in the cache
150 * status of -1 is returned if matches
151 * If the lookup determines that the name does not exist
152 * (negative cacheing), a status of ENOENT is returned. If the lookup
153 * fails, a status of zero is returned.
157 psem_cache_search(psemp
, pnp
, pcache
)
158 struct pseminfo
**psemp
;
159 struct psemname
*pnp
;
160 struct psemcache
**pcache
;
162 register struct psemcache
*pcp
, *nnp
;
163 register struct psemhashhead
*pcpp
;
165 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
166 psemstats
.longnames
++;
170 pcpp
= PSEMHASH(pnp
);
171 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
172 nnp
= pcp
->psem_hash
.le_next
;
173 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
174 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
183 /* We found a "positive" match, return the vnode */
185 psemstats
.goodhits
++;
187 *psemp
= pcp
->pseminfo
;
193 * We found a "negative" match, ENOENT notifies client of this match.
194 * The nc_vpid field records whether this is a whiteout.
201 * Add an entry to the cache.
204 psem_cache_add(psemp
, pnp
)
205 struct pseminfo
*psemp
;
206 struct psemname
*pnp
;
208 register struct psemcache
*pcp
;
209 register struct psemhashhead
*pcpp
;
210 struct pseminfo
*dpinfo
;
211 struct psemcache
*dpcp
;
214 if (pnp
->psem_namelen
> NCHNAMLEN
)
215 panic("cache_enter: name too long");
219 * We allocate a new entry if we are less than the maximum
220 * allowed and the one at the front of the LRU list is in use.
221 * Otherwise we use the one at the front of the LRU list.
223 pcp
= (struct psemcache
*)_MALLOC(sizeof(struct psemcache
), M_SHM
, M_WAITOK
);
224 /* if the entry has already been added by some one else return */
225 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
231 bzero(pcp
, sizeof(struct psemcache
));
233 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
234 * For negative entries, we have to record whether it is a whiteout.
235 * the whiteout flag is stored in the nc_vpid field which is
238 pcp
->pseminfo
= psemp
;
239 pcp
->psem_nlen
= pnp
->psem_namelen
;
240 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
241 pcpp
= PSEMHASH(pnp
);
244 register struct psemcache
*p
;
246 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
248 panic("psem:cache_enter duplicate");
251 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
256 * Name cache initialization, from vfs_init() when we are booting
261 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
265 psem_cache_delete(pcp
)
266 struct psemcache
*pcp
;
269 if (pcp
->psem_hash
.le_prev
== 0)
270 panic("psem namecache purge le_prev");
271 if (pcp
->psem_hash
.le_next
== pcp
)
272 panic("namecache purge le_next");
273 #endif /* DIAGNOSTIC */
274 LIST_REMOVE(pcp
, psem_hash
);
275 pcp
->psem_hash
.le_prev
= 0;
280 * Invalidate a all entries to particular vnode.
282 * We actually just increment the v_id, that will do it. The entries will
283 * be purged by lookup as they get found. If the v_id wraps around, we
284 * need to ditch the entire cache, to avoid confusion. No valid vnode will
285 * ever have (v_id == 0).
288 psem_cache_purge(void)
290 struct psemcache
*pcp
;
291 struct psemhashhead
*pcpp
;
293 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
294 while (pcp
= pcpp
->lh_first
)
295 psem_cache_delete(pcp
);
299 struct sem_open_args
{
307 sem_open(p
, uap
, retval
)
309 register struct sem_open_args
*uap
;
312 register struct filedesc
*fdp
= p
->p_fd
;
313 register struct file
*fp
;
314 register struct vnode
*vp
;
317 int type
, indx
, error
;
319 struct pseminfo
*pinfo
;
320 extern struct fileops psemops
;
324 size_t pathlen
, plen
;
326 int cmode
= uap
->mode
;
327 int value
= uap
->value
;
329 struct psemnode
* pnode
= PSEMNODE_NULL
;
330 struct psemcache
* pcache
= PSEMCACHE_NULL
;
331 kern_return_t kret
= KERN_SUCCESS
;
334 AUDIT_ARG(fflags
, uap
->oflag
);
335 AUDIT_ARG(mode
, uap
->mode
);
336 AUDIT_ARG(value
, uap
->value
);
337 pinfo
= PSEMINFO_NULL
;
339 MALLOC_ZONE(pnbuf
, caddr_t
,
340 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
341 pathlen
= MAXPATHLEN
;
342 error
= copyinstr((void *)uap
->name
, pnbuf
,
343 MAXPATHLEN
, &pathlen
);
347 AUDIT_ARG(text
, pnbuf
);
348 if (pathlen
> PSEMNAMLEN
) {
349 error
= ENAMETOOLONG
;
353 #ifdef PSXSEM_NAME_RESTRICT
355 if (*nameptr
== '/') {
356 while (*(nameptr
++) == '/') {
365 #endif /* PSXSEM_NAME_RESTRICT */
369 nd
.psem_nameptr
= nameptr
;
370 nd
.psem_namelen
= plen
;
373 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
374 nd
.psem_hash
+= (unsigned char)*cp
* i
;
377 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
379 if (error
== ENOENT
) {
388 fmode
= FFLAGS(uap
->oflag
);
390 if (error
= falloc(p
, &nfp
, &indx
)) {
397 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
398 /* sem exists and opened O_EXCL */
400 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
403 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
404 pinfo
->psem_gid
, pinfo
->psem_mode
);
408 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
409 /* As per POSIX, O_CREAT has no effect */
413 if (fmode
& O_CREAT
) {
414 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
418 pinfo
= (struct pseminfo
*)_MALLOC(sizeof(struct pseminfo
), M_SHM
, M_WAITOK
);
419 bzero(pinfo
, sizeof(struct pseminfo
));
421 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
422 pinfo
->psem_usecount
= 1;
423 pinfo
->psem_mode
= cmode
;
424 pinfo
->psem_uid
= p
->p_ucred
->cr_uid
;
425 pinfo
->psem_gid
= p
->p_ucred
->cr_gid
;
426 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
427 SYNC_POLICY_FIFO
, value
);
428 if(kret
!= KERN_SUCCESS
)
430 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
431 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
434 /* semaphore should exist as it is without O_CREAT */
439 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
443 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
444 pinfo
->psem_gid
, pinfo
->psem_mode
);
445 if (error
= psem_access(pinfo
, fmode
, p
->p_ucred
, p
))
448 pnode
= (struct psemnode
*)_MALLOC(sizeof(struct psemnode
), M_SHM
, M_WAITOK
);
449 bzero(pnode
, sizeof(struct psemnode
));
452 if (error
= psem_cache_add(pinfo
, &nd
)) {
456 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
457 pinfo
->psem_usecount
++;
458 pnode
->pinfo
= pinfo
;
459 fp
->f_flag
= fmode
& FMASK
;
460 fp
->f_type
= DTYPE_PSXSEM
;
461 fp
->f_ops
= &psemops
;
462 fp
->f_data
= (caddr_t
)pnode
;
463 *fdflags(p
, indx
) &= ~UF_RESERVED
;
465 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
470 case KERN_RESOURCE_SHORTAGE
:
472 case KERN_PROTECTION_FAILURE
:
486 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
491 psem_access(pinfo
, mode
, cred
, p
)
492 struct pseminfo
*pinfo
;
501 /* Otherwise, user id 0 always gets access. */
502 if (cred
->cr_uid
== 0)
507 /* Otherwise, check the owner. */
508 if (cred
->cr_uid
== pinfo
->psem_uid
) {
513 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
516 /* Otherwise, check the groups. */
517 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
518 if (pinfo
->psem_gid
== *gp
) {
523 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
526 /* Otherwise, check everyone else. */
531 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
534 struct sem_unlink_args
{
539 sem_unlink(p
, uap
, retval
)
541 register struct sem_unlink_args
*uap
;
544 register struct filedesc
*fdp
= p
->p_fd
;
545 register struct file
*fp
;
549 struct pseminfo
*pinfo
;
550 extern struct fileops psemops
;
554 size_t pathlen
, plen
;
557 struct psemnode
* pnode
= PSEMNODE_NULL
;
558 struct psemcache
*pcache
= PSEMCACHE_NULL
;
561 pinfo
= PSEMINFO_NULL
;
563 MALLOC_ZONE(pnbuf
, caddr_t
,
564 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
565 pathlen
= MAXPATHLEN
;
566 error
= copyinstr((void *)uap
->name
, pnbuf
,
567 MAXPATHLEN
, &pathlen
);
571 AUDIT_ARG(text
, pnbuf
);
572 if (pathlen
> PSEMNAMLEN
) {
573 error
= ENAMETOOLONG
;
578 #ifdef PSXSEM_NAME_RESTRICT
580 if (*nameptr
== '/') {
581 while (*(nameptr
++) == '/') {
590 #endif /* PSXSEM_NAME_RESTRICT */
594 nd
.psem_nameptr
= nameptr
;
595 nd
.psem_namelen
= plen
;
598 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
599 nd
.psem_hash
+= (unsigned char)*cp
* i
;
602 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
604 if (error
== ENOENT
) {
614 if (error
= psem_access(pinfo
, pinfo
->psem_mode
, p
->p_ucred
, p
))
617 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
621 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
625 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
628 pinfo
->psem_flags
|= PSEM_INDELETE
;
629 pinfo
->psem_usecount
--;
631 if (!pinfo
->psem_usecount
) {
635 pinfo
->psem_flags
|= PSEM_REMOVED
;
637 psem_cache_delete(pcache
);
638 _FREE(pcache
, M_SHM
);
641 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
645 struct sem_close_args
{
650 sem_close(p
, uap
, retval
)
652 struct sem_close_args
*uap
;
655 int fd
= (int)uap
->sem
;
656 register struct filedesc
*fdp
= p
->p_fd
;
657 register struct file
*fp
;
660 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
661 if ((u_int
)fd
>= fdp
->fd_nfiles
||
662 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
663 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
666 if( error
= closef(fp
, p
))
671 struct sem_wait_args
{
676 sem_wait(p
, uap
, retval
)
678 struct sem_wait_args
*uap
;
681 int fd
= (int)uap
->sem
;
682 register struct filedesc
*fdp
= p
->p_fd
;
684 struct pseminfo
* pinfo
;
685 struct psemnode
* pnode
;
689 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
691 if (fp
->f_type
!= DTYPE_PSXSEM
)
693 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
695 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
697 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
702 kret
= semaphore_wait(pinfo
->psem_semobject
);
704 case KERN_INVALID_ADDRESS
:
705 case KERN_PROTECTION_FAILURE
:
708 case KERN_OPERATION_TIMED_OUT
:
717 struct sem_trywait_args
{
722 sem_trywait(p
, uap
, retval
)
724 struct sem_trywait_args
*uap
;
727 int fd
= (int)uap
->sem
;
728 register struct filedesc
*fdp
= p
->p_fd
;
730 struct pseminfo
* pinfo
;
731 struct psemnode
* pnode
;
733 mach_timespec_t wait_time
;
736 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
738 if (fp
->f_type
!= DTYPE_PSXSEM
)
740 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
742 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
744 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
749 wait_time
.tv_sec
= 0;
750 wait_time
.tv_nsec
= 0;
752 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
754 case KERN_INVALID_ADDRESS
:
755 case KERN_PROTECTION_FAILURE
:
759 case KERN_OPERATION_TIMED_OUT
:
768 struct sem_post_args
{
773 sem_post(p
, uap
, retval
)
775 struct sem_post_args
*uap
;
778 int fd
= (int)uap
->sem
;
779 register struct filedesc
*fdp
= p
->p_fd
;
781 struct pseminfo
* pinfo
;
782 struct psemnode
* pnode
;
786 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
788 if (fp
->f_type
!= DTYPE_PSXSEM
)
790 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
792 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
794 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
799 kret
= semaphore_signal(pinfo
->psem_semobject
);
801 case KERN_INVALID_ADDRESS
:
802 case KERN_PROTECTION_FAILURE
:
805 case KERN_OPERATION_TIMED_OUT
:
814 struct sem_init_args
{
821 sem_init(p
, uap
, retval
)
823 struct sem_init_args
*uap
;
829 struct sem_destroy_args
{
834 sem_destroy(p
, uap
, retval
)
836 struct sem_destroy_args
*uap
;
842 struct sem_getvalue_args
{
848 sem_getvalue(p
, uap
, retval
)
850 struct sem_getvalue_args
*uap
;
857 psem_close(pnode
, flags
, cred
, p
)
858 register struct psemnode
*pnode
;
865 register struct pseminfo
*pinfo
;
867 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
870 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
874 if(!pinfo
->psem_usecount
) {
875 kprintf("negative usecount in psem_close\n");
877 #endif /* DIAGNOSTIC */
878 pinfo
->psem_usecount
--;
880 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
881 error
= psem_delete(pinfo
);
889 psem_closefile(fp
, p
)
894 return (psem_close(((struct psemnode
*)fp
->f_data
), fp
->f_flag
,
899 psem_delete(struct pseminfo
* pinfo
)
903 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
906 case KERN_INVALID_ADDRESS
:
907 case KERN_PROTECTION_FAILURE
:
910 case KERN_OPERATION_TIMED_OUT
:
920 psem_read(fp
, uio
, cred
, flags
, p
)
931 psem_write(fp
, uio
, cred
, flags
, p
)
942 psem_ioctl(fp
, com
, data
, p
)
952 psem_select(fp
, which
, wql
, p
)
962 psem_kqfilter(fp
, kn
, p
)