2 * Copyright (c) 2000-2002 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>
54 #include <mach/mach_types.h>
55 #include <mach/vm_prot.h>
56 #include <mach/semaphore.h>
57 #include <mach/sync_policy.h>
58 #include <kern/task.h>
59 #include <kern/clock.h>
60 #include <mach/kern_return.h>
62 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
65 unsigned int psem_flags
;
66 unsigned int psem_usecount
;
70 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
71 void * psem_semobject
;
72 struct proc
* sem_proc
;
74 #define PSEMINFO_NULL (struct pseminfo *)0
77 #define PSEM_DEFINED 2
78 #define PSEM_ALLOCATED 4
80 #define PSEM_INUSE 0x10
81 #define PSEM_REMOVED 0x20
82 #define PSEM_INCREATE 0x40
83 #define PSEM_INDELETE 0x80
86 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
87 struct pseminfo
*pseminfo
; /* vnode the name refers to */
88 int psem_nlen
; /* length of name */
89 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
91 #define PSEMCACHE_NULL (struct psemcache *)0
94 long goodhits
; /* hits that we can really use */
95 long neghits
; /* negative hits that we can use */
96 long badhits
; /* hits we must drop */
97 long falsehits
; /* hits with id mismatch */
98 long miss
; /* misses */
99 long longnames
; /* long names that ignore cache */
103 char *psem_nameptr
; /* pointer to looked up name */
104 long psem_namelen
; /* length of looked up component */
105 u_long psem_hash
; /* hash value of looked up name */
109 struct pseminfo
*pinfo
;
111 unsigned int readcnt
;
112 unsigned int writecnt
;
115 #define PSEMNODE_NULL (struct psemnode *)0
118 #define PSEMHASH(pnp) \
119 (&psemhashtbl[(pnp)->psem_hash & psemhash])
120 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
121 u_long psemhash
; /* size of hash table - 1 */
122 long psemnument
; /* number of cache entries allocated */
123 struct psemstats psemstats
; /* cache effectiveness statistics */
125 static int psem_cache_search
__P((struct pseminfo
**,
126 struct psemname
*, struct psemcache
**));
128 static int psem_read
__P((struct file
*fp
, struct uio
*uio
,
129 struct ucred
*cred
, int flags
, struct proc
*p
));
130 static int psem_write
__P((struct file
*fp
, struct uio
*uio
,
131 struct ucred
*cred
, int flags
, struct proc
*p
));
132 static int psem_ioctl
__P((struct file
*fp
, u_long com
,
133 caddr_t data
, struct proc
*p
));
134 static int psem_select
__P((struct file
*fp
, int which
, void *wql
,
136 static int psem_closefile
__P((struct file
*fp
, struct proc
*p
));
138 struct fileops psemops
=
139 { psem_read
, psem_write
, psem_ioctl
, psem_select
, psem_closefile
};
142 * Lookup an entry in the cache
145 * status of -1 is returned if matches
146 * If the lookup determines that the name does not exist
147 * (negative cacheing), a status of ENOENT is returned. If the lookup
148 * fails, a status of zero is returned.
152 psem_cache_search(psemp
, pnp
, pcache
)
153 struct pseminfo
**psemp
;
154 struct psemname
*pnp
;
155 struct psemcache
**pcache
;
157 register struct psemcache
*pcp
, *nnp
;
158 register struct psemhashhead
*pcpp
;
160 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
161 psemstats
.longnames
++;
165 pcpp
= PSEMHASH(pnp
);
166 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
167 nnp
= pcp
->psem_hash
.le_next
;
168 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
169 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, (u_int
)pcp
-> psem_nlen
))
178 /* We found a "positive" match, return the vnode */
180 psemstats
.goodhits
++;
182 *psemp
= pcp
->pseminfo
;
188 * We found a "negative" match, ENOENT notifies client of this match.
189 * The nc_vpid field records whether this is a whiteout.
196 * Add an entry to the cache.
199 psem_cache_add(psemp
, pnp
)
200 struct pseminfo
*psemp
;
201 struct psemname
*pnp
;
203 register struct psemcache
*pcp
;
204 register struct psemhashhead
*pcpp
;
205 struct pseminfo
*dpinfo
;
206 struct psemcache
*dpcp
;
209 if (pnp
->psem_namelen
> NCHNAMLEN
)
210 panic("cache_enter: name too long");
214 * We allocate a new entry if we are less than the maximum
215 * allowed and the one at the front of the LRU list is in use.
216 * Otherwise we use the one at the front of the LRU list.
218 pcp
= (struct psemcache
*)_MALLOC(sizeof(struct psemcache
), M_SHM
, M_WAITOK
);
219 /* if the entry has already been added by some one else return */
220 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
226 bzero(pcp
, sizeof(struct psemcache
));
228 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
229 * For negative entries, we have to record whether it is a whiteout.
230 * the whiteout flag is stored in the nc_vpid field which is
233 pcp
->pseminfo
= psemp
;
234 pcp
->psem_nlen
= pnp
->psem_namelen
;
235 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, (unsigned)pcp
->psem_nlen
);
236 pcpp
= PSEMHASH(pnp
);
239 register struct psemcache
*p
;
241 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
)
243 panic("psem:cache_enter duplicate");
246 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
251 * Name cache initialization, from vfs_init() when we are booting
256 psemhashtbl
= hashinit(desiredvnodes
, M_SHM
, &psemhash
);
260 psem_cache_delete(pcp
)
261 struct psemcache
*pcp
;
264 if (pcp
->psem_hash
.le_prev
== 0)
265 panic("psem namecache purge le_prev");
266 if (pcp
->psem_hash
.le_next
== pcp
)
267 panic("namecache purge le_next");
268 #endif /* DIAGNOSTIC */
269 LIST_REMOVE(pcp
, psem_hash
);
270 pcp
->psem_hash
.le_prev
= 0;
275 * Invalidate a all entries to particular vnode.
277 * We actually just increment the v_id, that will do it. The entries will
278 * be purged by lookup as they get found. If the v_id wraps around, we
279 * need to ditch the entire cache, to avoid confusion. No valid vnode will
280 * ever have (v_id == 0).
283 psem_cache_purge(void)
285 struct psemcache
*pcp
;
286 struct psemhashhead
*pcpp
;
288 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
289 while (pcp
= pcpp
->lh_first
)
290 psem_cache_delete(pcp
);
294 struct sem_open_args
{
302 sem_open(p
, uap
, retval
)
304 register struct sem_open_args
*uap
;
307 register struct filedesc
*fdp
= p
->p_fd
;
308 register struct file
*fp
;
309 register struct vnode
*vp
;
312 int type
, indx
, error
;
314 struct pseminfo
*pinfo
;
315 extern struct fileops psemops
;
319 size_t pathlen
, plen
;
321 int cmode
= uap
->mode
;
322 int value
= uap
->value
;
324 struct psemnode
* pnode
= PSEMNODE_NULL
;
325 struct psemcache
* pcache
= PSEMCACHE_NULL
;
326 kern_return_t kret
= KERN_SUCCESS
;
329 pinfo
= PSEMINFO_NULL
;
331 MALLOC_ZONE(pnbuf
, caddr_t
,
332 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
333 pathlen
= MAXPATHLEN
;
334 error
= copyinstr(uap
->name
, pnbuf
,
335 MAXPATHLEN
, &pathlen
);
339 if (pathlen
> PSEMNAMLEN
) {
340 error
= ENAMETOOLONG
;
344 #ifdef PSXSEM_NAME_RESTRICT
346 if (*nameptr
== '/') {
347 while (*(nameptr
++) == '/') {
356 #endif /* PSXSEM_NAME_RESTRICT */
360 nd
.psem_nameptr
= nameptr
;
361 nd
.psem_namelen
= plen
;
364 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
365 nd
.psem_hash
+= (unsigned char)*cp
* i
;
368 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
370 if (error
== ENOENT
) {
379 fmode
= FFLAGS(uap
->oflag
);
381 if (error
= falloc(p
, &nfp
, &indx
)) {
388 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
389 /* sem exists and opened O_EXCL */
391 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
397 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
398 /* As per POSIX, O_CREAT has no effect */
402 if (fmode
& O_CREAT
) {
403 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
407 pinfo
= (struct pseminfo
*)_MALLOC(sizeof(struct pseminfo
), M_SHM
, M_WAITOK
);
408 bzero(pinfo
, sizeof(struct pseminfo
));
410 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
411 pinfo
->psem_usecount
= 1;
412 pinfo
->psem_mode
= cmode
;
413 pinfo
->psem_uid
= p
->p_ucred
->cr_uid
;
414 pinfo
->psem_gid
= p
->p_ucred
->cr_gid
;
415 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
416 SYNC_POLICY_FIFO
, value
);
417 if(kret
!= KERN_SUCCESS
)
419 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
420 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
423 /* semaphore should exist as it is without O_CREAT */
428 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
432 if (error
= psem_access(pinfo
, fmode
, p
->p_ucred
, p
))
435 pnode
= (struct psemnode
*)_MALLOC(sizeof(struct psemnode
), M_SHM
, M_WAITOK
);
436 bzero(pnode
, sizeof(struct psemnode
));
439 if (error
= psem_cache_add(pinfo
, &nd
)) {
443 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
444 pinfo
->psem_usecount
++;
445 pnode
->pinfo
= pinfo
;
446 fp
->f_flag
= flags
& FMASK
;
447 fp
->f_type
= DTYPE_PSXSEM
;
448 fp
->f_ops
= &psemops
;
449 fp
->f_data
= (caddr_t
)pnode
;
450 *fdflags(p
, indx
) &= ~UF_RESERVED
;
452 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
457 case KERN_RESOURCE_SHORTAGE
:
459 case KERN_PROTECTION_FAILURE
:
473 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
478 psem_access(pinfo
, mode
, cred
, p
)
479 struct pseminfo
*pinfo
;
488 /* Otherwise, user id 0 always gets access. */
489 if (cred
->cr_uid
== 0)
494 /* Otherwise, check the owner. */
495 if (cred
->cr_uid
== pinfo
->psem_uid
) {
500 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
503 /* Otherwise, check the groups. */
504 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
505 if (pinfo
->psem_gid
== *gp
) {
510 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
513 /* Otherwise, check everyone else. */
518 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
521 struct sem_unlink_args
{
526 sem_unlink(p
, uap
, retval
)
528 register struct sem_unlink_args
*uap
;
531 register struct filedesc
*fdp
= p
->p_fd
;
532 register struct file
*fp
;
536 struct pseminfo
*pinfo
;
537 extern struct fileops psemops
;
541 size_t pathlen
, plen
;
544 struct psemnode
* pnode
= PSEMNODE_NULL
;
545 struct psemcache
*pcache
= PSEMCACHE_NULL
;
548 pinfo
= PSEMINFO_NULL
;
550 MALLOC_ZONE(pnbuf
, caddr_t
,
551 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
552 pathlen
= MAXPATHLEN
;
553 error
= copyinstr(uap
->name
, pnbuf
,
554 MAXPATHLEN
, &pathlen
);
558 if (pathlen
> PSEMNAMLEN
) {
559 error
= ENAMETOOLONG
;
564 #ifdef PSXSEM_NAME_RESTRICT
566 if (*nameptr
== '/') {
567 while (*(nameptr
++) == '/') {
576 #endif /* PSXSEM_NAME_RESTRICT */
580 nd
.psem_nameptr
= nameptr
;
581 nd
.psem_namelen
= plen
;
584 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
585 nd
.psem_hash
+= (unsigned char)*cp
* i
;
588 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
590 if (error
== ENOENT
) {
600 if (error
= psem_access(pinfo
, pinfo
->psem_mode
, p
->p_ucred
, p
))
603 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
607 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
611 pinfo
->psem_flags
|= PSEM_INDELETE
;
612 pinfo
->psem_usecount
--;
614 if (!pinfo
->psem_usecount
) {
618 pinfo
->psem_flags
|= PSEM_REMOVED
;
620 psem_cache_delete(pcache
);
621 _FREE(pcache
, M_SHM
);
624 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
628 struct sem_close_args
{
633 sem_close(p
, uap
, retval
)
635 struct sem_close_args
*uap
;
638 int fd
= (int)uap
->sem
;
639 register struct filedesc
*fdp
= p
->p_fd
;
640 register struct file
*fp
;
644 if ((u_int
)fd
>= fdp
->fd_nfiles
||
645 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
646 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
649 if( error
= closef(fp
, p
))
654 struct sem_wait_args
{
659 sem_wait(p
, uap
, retval
)
661 struct sem_wait_args
*uap
;
664 int fd
= (int)uap
->sem
;
665 register struct filedesc
*fdp
= p
->p_fd
;
667 struct pseminfo
* pinfo
;
668 struct psemnode
* pnode
;
672 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
674 if (fp
->f_type
!= DTYPE_PSXSEM
)
676 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
678 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
680 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
685 kret
= semaphore_wait(pinfo
->psem_semobject
);
687 case KERN_INVALID_ADDRESS
:
688 case KERN_PROTECTION_FAILURE
:
691 case KERN_OPERATION_TIMED_OUT
:
700 struct sem_trywait_args
{
705 sem_trywait(p
, uap
, retval
)
707 struct sem_trywait_args
*uap
;
710 int fd
= (int)uap
->sem
;
711 register struct filedesc
*fdp
= p
->p_fd
;
713 struct pseminfo
* pinfo
;
714 struct psemnode
* pnode
;
716 mach_timespec_t wait_time
;
719 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
721 if (fp
->f_type
!= DTYPE_PSXSEM
)
723 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
725 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
727 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
732 wait_time
.tv_sec
= 0;
733 wait_time
.tv_nsec
= 0;
735 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
737 case KERN_INVALID_ADDRESS
:
738 case KERN_PROTECTION_FAILURE
:
742 case KERN_OPERATION_TIMED_OUT
:
751 struct sem_post_args
{
756 sem_post(p
, uap
, retval
)
758 struct sem_post_args
*uap
;
761 int fd
= (int)uap
->sem
;
762 register struct filedesc
*fdp
= p
->p_fd
;
764 struct pseminfo
* pinfo
;
765 struct psemnode
* pnode
;
769 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
771 if (fp
->f_type
!= DTYPE_PSXSEM
)
773 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
775 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
777 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
782 kret
= semaphore_signal(pinfo
->psem_semobject
);
784 case KERN_INVALID_ADDRESS
:
785 case KERN_PROTECTION_FAILURE
:
788 case KERN_OPERATION_TIMED_OUT
:
797 struct sem_init_args
{
804 sem_init(p
, uap
, retval
)
806 struct sem_init_args
*uap
;
812 struct sem_destroy_args
{
817 sem_destroy(p
, uap
, retval
)
819 struct sem_destroy_args
*uap
;
825 struct sem_getvalue_args
{
831 sem_getvalue(p
, uap
, retval
)
833 struct sem_getvalue_args
*uap
;
840 psem_close(pnode
, flags
, cred
, p
)
841 register struct psemnode
*pnode
;
848 register struct pseminfo
*pinfo
;
850 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
853 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
857 if(!pinfo
->psem_usecount
) {
858 kprintf("negative usecount in psem_close\n");
860 #endif /* DIAGNOSTIC */
861 pinfo
->psem_usecount
--;
863 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
864 error
= psem_delete(pinfo
);
872 psem_closefile(fp
, p
)
877 return (psem_close(((struct psemnode
*)fp
->f_data
), fp
->f_flag
,
882 psem_delete(struct pseminfo
* pinfo
)
886 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
889 case KERN_INVALID_ADDRESS
:
890 case KERN_PROTECTION_FAILURE
:
893 case KERN_OPERATION_TIMED_OUT
:
903 psem_read(fp
, uio
, cred
, flags
, p
)
914 psem_write(fp
, uio
, cred
, flags
, p
)
925 psem_ioctl(fp
, com
, data
, p
)
935 psem_select(fp
, which
, wql
, p
)