2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
27 * All Rights Reserved.
30 * posix_shm.c : Support for POSIX semaphore APIs
33 * Author: Ananthakrishna Ramesh
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/filedesc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/ioctl.h>
55 #include <sys/malloc.h>
56 #include <sys/semaphore.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 struct fileops psemops
=
142 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
};
145 * Lookup an entry in the cache
148 * status of -1 is returned if matches
149 * If the lookup determines that the name does not exist
150 * (negative cacheing), a status of ENOENT is returned. If the lookup
151 * fails, a status of zero is returned.
155 psem_cache_search(psemp
, pnp
, pcache
)
156 struct pseminfo
**psemp
;
157 struct psemname
*pnp
;
158 struct psemcache
**pcache
;
160 register struct psemcache
*pcp
, *nnp
;
161 register struct psemhashhead
*pcpp
;
163 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
164 psemstats
.longnames
++;
168 pcpp
= PSEMHASH(pnp
);
169 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
170 nnp
= pcp
->psem_hash
.le_next
;
171 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
172 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
181 /* We found a "positive" match, return the vnode */
183 psemstats
.goodhits
++;
185 *psemp
= pcp
->pseminfo
;
191 * We found a "negative" match, ENOENT notifies client of this match.
192 * The nc_vpid field records whether this is a whiteout.
199 * Add an entry to the cache.
202 psem_cache_add(psemp
, pnp
)
203 struct pseminfo
*psemp
;
204 struct psemname
*pnp
;
206 register struct psemcache
*pcp
;
207 register struct psemhashhead
*pcpp
;
208 struct pseminfo
*dpinfo
;
209 struct psemcache
*dpcp
;
212 if (pnp
->psem_namelen
> NCHNAMLEN
)
213 panic("cache_enter: name too long");
217 * We allocate a new entry if we are less than the maximum
218 * allowed and the one at the front of the LRU list is in use.
219 * Otherwise we use the one at the front of the LRU list.
221 pcp
= (struct psemcache
*)_MALLOC(sizeof(struct psemcache
), M_SHM
, M_WAITOK
);
222 /* if the entry has already been added by some one else return */
223 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
229 bzero(pcp
, sizeof(struct psemcache
));
231 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
232 * For negative entries, we have to record whether it is a whiteout.
233 * the whiteout flag is stored in the nc_vpid field which is
236 pcp
->pseminfo
= psemp
;
237 pcp
->psem_nlen
= pnp
->psem_namelen
;
238 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
239 pcpp
= PSEMHASH(pnp
);
242 register struct psemcache
*p
;
244 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
246 panic("psem:cache_enter duplicate");
249 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
254 * Name cache initialization, from vfs_init() when we are booting
259 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
263 psem_cache_delete(pcp
)
264 struct psemcache
*pcp
;
267 if (pcp
->psem_hash
.le_prev
== 0)
268 panic("psem namecache purge le_prev");
269 if (pcp
->psem_hash
.le_next
== pcp
)
270 panic("namecache purge le_next");
271 #endif /* DIAGNOSTIC */
272 LIST_REMOVE(pcp
, psem_hash
);
273 pcp
->psem_hash
.le_prev
= 0;
278 * Invalidate a all entries to particular vnode.
280 * We actually just increment the v_id, that will do it. The entries will
281 * be purged by lookup as they get found. If the v_id wraps around, we
282 * need to ditch the entire cache, to avoid confusion. No valid vnode will
283 * ever have (v_id == 0).
286 psem_cache_purge(void)
288 struct psemcache
*pcp
;
289 struct psemhashhead
*pcpp
;
291 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
292 while (pcp
= pcpp
->lh_first
)
293 psem_cache_delete(pcp
);
297 struct sem_open_args
{
305 sem_open(p
, uap
, retval
)
307 register struct sem_open_args
*uap
;
310 register struct filedesc
*fdp
= p
->p_fd
;
311 register struct file
*fp
;
312 register struct vnode
*vp
;
315 int type
, indx
, error
;
317 struct pseminfo
*pinfo
;
318 extern struct fileops psemops
;
322 size_t pathlen
, plen
;
324 int cmode
= uap
->mode
;
325 int value
= uap
->value
;
327 struct psemnode
* pnode
= PSEMNODE_NULL
;
328 struct psemcache
* pcache
= PSEMCACHE_NULL
;
329 kern_return_t kret
= KERN_SUCCESS
;
332 pinfo
= PSEMINFO_NULL
;
334 MALLOC_ZONE(pnbuf
, caddr_t
,
335 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
336 pathlen
= MAXPATHLEN
;
337 error
= copyinstr(uap
->name
, pnbuf
,
338 MAXPATHLEN
, &pathlen
);
342 if (pathlen
> PSEMNAMLEN
) {
343 error
= ENAMETOOLONG
;
347 #ifdef PSXSEM_NAME_RESTRICT
349 if (*nameptr
== '/') {
350 while (*(nameptr
++) == '/') {
359 #endif /* PSXSEM_NAME_RESTRICT */
363 nd
.psem_nameptr
= nameptr
;
364 nd
.psem_namelen
= plen
;
367 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
368 nd
.psem_hash
+= (unsigned char)*cp
* i
;
371 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
373 if (error
== ENOENT
) {
382 fmode
= FFLAGS(uap
->oflag
);
384 if (error
= falloc(p
, &nfp
, &indx
)) {
391 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
392 /* sem exists and opened O_EXCL */
394 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
400 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
401 /* As per POSIX, O_CREAT has no effect */
405 if (fmode
& O_CREAT
) {
406 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
410 pinfo
= (struct pseminfo
*)_MALLOC(sizeof(struct pseminfo
), M_SHM
, M_WAITOK
);
411 bzero(pinfo
, sizeof(struct pseminfo
));
413 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
414 pinfo
->psem_usecount
= 1;
415 pinfo
->psem_mode
= cmode
;
416 pinfo
->psem_uid
= p
->p_ucred
->cr_uid
;
417 pinfo
->psem_gid
= p
->p_ucred
->cr_gid
;
418 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
419 SYNC_POLICY_FIFO
, value
);
420 if(kret
!= KERN_SUCCESS
)
422 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
423 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
426 /* semaphore should exist as it is without O_CREAT */
431 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
435 if (error
= psem_access(pinfo
, fmode
, p
->p_ucred
, p
))
438 pnode
= (struct psemnode
*)_MALLOC(sizeof(struct psemnode
), M_SHM
, M_WAITOK
);
439 bzero(pnode
, sizeof(struct psemnode
));
442 if (error
= psem_cache_add(pinfo
, &nd
)) {
446 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
447 pinfo
->psem_usecount
++;
448 pnode
->pinfo
= pinfo
;
449 fp
->f_flag
= flags
& FMASK
;
450 fp
->f_type
= DTYPE_PSXSEM
;
451 fp
->f_ops
= &psemops
;
452 fp
->f_data
= (caddr_t
)pnode
;
453 *fdflags(p
, indx
) &= ~UF_RESERVED
;
455 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
460 case KERN_RESOURCE_SHORTAGE
:
462 case KERN_PROTECTION_FAILURE
:
476 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
481 psem_access(pinfo
, mode
, cred
, p
)
482 struct pseminfo
*pinfo
;
491 /* Otherwise, user id 0 always gets access. */
492 if (cred
->cr_uid
== 0)
497 /* Otherwise, check the owner. */
498 if (cred
->cr_uid
== pinfo
->psem_uid
) {
503 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
506 /* Otherwise, check the groups. */
507 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
508 if (pinfo
->psem_gid
== *gp
) {
513 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
516 /* Otherwise, check everyone else. */
521 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
524 struct sem_unlink_args
{
529 sem_unlink(p
, uap
, retval
)
531 register struct sem_unlink_args
*uap
;
534 register struct filedesc
*fdp
= p
->p_fd
;
535 register struct file
*fp
;
539 struct pseminfo
*pinfo
;
540 extern struct fileops psemops
;
544 size_t pathlen
, plen
;
547 struct psemnode
* pnode
= PSEMNODE_NULL
;
548 struct psemcache
*pcache
= PSEMCACHE_NULL
;
551 pinfo
= PSEMINFO_NULL
;
553 MALLOC_ZONE(pnbuf
, caddr_t
,
554 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
555 pathlen
= MAXPATHLEN
;
556 error
= copyinstr(uap
->name
, pnbuf
,
557 MAXPATHLEN
, &pathlen
);
561 if (pathlen
> PSEMNAMLEN
) {
562 error
= ENAMETOOLONG
;
567 #ifdef PSXSEM_NAME_RESTRICT
569 if (*nameptr
== '/') {
570 while (*(nameptr
++) == '/') {
579 #endif /* PSXSEM_NAME_RESTRICT */
583 nd
.psem_nameptr
= nameptr
;
584 nd
.psem_namelen
= plen
;
587 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
588 nd
.psem_hash
+= (unsigned char)*cp
* i
;
591 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
593 if (error
== ENOENT
) {
603 if (error
= psem_access(pinfo
, pinfo
->psem_mode
, p
->p_ucred
, p
))
606 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
610 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
614 pinfo
->psem_flags
|= PSEM_INDELETE
;
615 pinfo
->psem_usecount
--;
617 if (!pinfo
->psem_usecount
) {
621 pinfo
->psem_flags
|= PSEM_REMOVED
;
623 psem_cache_delete(pcache
);
624 _FREE(pcache
, M_SHM
);
627 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
631 struct sem_close_args
{
636 sem_close(p
, uap
, retval
)
638 struct sem_close_args
*uap
;
641 int fd
= (int)uap
->sem
;
642 register struct filedesc
*fdp
= p
->p_fd
;
643 register struct file
*fp
;
647 if ((u_int
)fd
>= fdp
->fd_nfiles
||
648 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
649 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
652 if( error
= closef(fp
, p
))
657 struct sem_wait_args
{
662 sem_wait(p
, uap
, retval
)
664 struct sem_wait_args
*uap
;
667 int fd
= (int)uap
->sem
;
668 register struct filedesc
*fdp
= p
->p_fd
;
670 struct pseminfo
* pinfo
;
671 struct psemnode
* pnode
;
675 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
677 if (fp
->f_type
!= DTYPE_PSXSEM
)
679 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
681 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
683 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
688 kret
= semaphore_wait(pinfo
->psem_semobject
);
690 case KERN_INVALID_ADDRESS
:
691 case KERN_PROTECTION_FAILURE
:
694 case KERN_OPERATION_TIMED_OUT
:
703 struct sem_trywait_args
{
708 sem_trywait(p
, uap
, retval
)
710 struct sem_trywait_args
*uap
;
713 int fd
= (int)uap
->sem
;
714 register struct filedesc
*fdp
= p
->p_fd
;
716 struct pseminfo
* pinfo
;
717 struct psemnode
* pnode
;
719 mach_timespec_t wait_time
;
722 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
724 if (fp
->f_type
!= DTYPE_PSXSEM
)
726 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
728 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
730 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
735 wait_time
.tv_sec
= 0;
736 wait_time
.tv_nsec
= 0;
738 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
740 case KERN_INVALID_ADDRESS
:
741 case KERN_PROTECTION_FAILURE
:
745 case KERN_OPERATION_TIMED_OUT
:
754 struct sem_post_args
{
759 sem_post(p
, uap
, retval
)
761 struct sem_post_args
*uap
;
764 int fd
= (int)uap
->sem
;
765 register struct filedesc
*fdp
= p
->p_fd
;
767 struct pseminfo
* pinfo
;
768 struct psemnode
* pnode
;
772 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
774 if (fp
->f_type
!= DTYPE_PSXSEM
)
776 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
778 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
780 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
785 kret
= semaphore_signal(pinfo
->psem_semobject
);
787 case KERN_INVALID_ADDRESS
:
788 case KERN_PROTECTION_FAILURE
:
791 case KERN_OPERATION_TIMED_OUT
:
800 struct sem_init_args
{
807 sem_init(p
, uap
, retval
)
809 struct sem_init_args
*uap
;
815 struct sem_destroy_args
{
820 sem_destroy(p
, uap
, retval
)
822 struct sem_destroy_args
*uap
;
828 struct sem_getvalue_args
{
834 sem_getvalue(p
, uap
, retval
)
836 struct sem_getvalue_args
*uap
;
843 psem_close(pnode
, flags
, cred
, p
)
844 register struct psemnode
*pnode
;
851 register struct pseminfo
*pinfo
;
853 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
856 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
860 if(!pinfo
->psem_usecount
) {
861 kprintf("negative usecount in psem_close\n");
863 #endif /* DIAGNOSTIC */
864 pinfo
->psem_usecount
--;
866 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
867 error
= psem_delete(pinfo
);
875 psem_closefile(fp
, p
)
880 return (psem_close(((struct psemnode
*)fp
->f_data
), fp
->f_flag
,
885 psem_delete(struct pseminfo
* pinfo
)
889 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
892 case KERN_INVALID_ADDRESS
:
893 case KERN_PROTECTION_FAILURE
:
896 case KERN_OPERATION_TIMED_OUT
:
906 psem_read(fp
, uio
, cred
, flags
, p
)
917 psem_write(fp
, uio
, cred
, flags
, p
)
928 psem_ioctl(fp
, com
, data
, p
)
938 psem_select(fp
, which
, wql
, p
)