2 * Copyright (c) 2000 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>
63 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
66 unsigned int psem_flags
;
67 unsigned int psem_usecount
;
71 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
72 void * psem_semobject
;
73 struct proc
* sem_proc
;
75 #define PSEMINFO_NULL (struct pseminfo *)0
78 #define PSEM_DEFINED 2
79 #define PSEM_ALLOCATED 4
81 #define PSEM_INUSE 0x10
82 #define PSEM_REMOVED 0x20
83 #define PSEM_INCREATE 0x40
84 #define PSEM_INDELETE 0x80
87 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
88 struct pseminfo
*pseminfo
; /* vnode the name refers to */
89 int psem_nlen
; /* length of name */
90 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
92 #define PSEMCACHE_NULL (struct psemcache *)0
95 long goodhits
; /* hits that we can really use */
96 long neghits
; /* negative hits that we can use */
97 long badhits
; /* hits we must drop */
98 long falsehits
; /* hits with id mismatch */
99 long miss
; /* misses */
100 long longnames
; /* long names that ignore cache */
104 char *psem_nameptr
; /* pointer to looked up name */
105 long psem_namelen
; /* length of looked up component */
106 u_long psem_hash
; /* hash value of looked up name */
110 struct pseminfo
*pinfo
;
112 unsigned int readcnt
;
113 unsigned int writecnt
;
116 #define PSEMNODE_NULL (struct psemnode *)0
119 #define PSEMHASH(pnp) \
120 (&psemhashtbl[(pnp)->psem_hash & psemhash])
121 LIST_HEAD(psemhashhead
, psemcache
) *psemhashtbl
; /* Hash Table */
122 u_long psemhash
; /* size of hash table - 1 */
123 long psemnument
; /* number of cache entries allocated */
124 struct psemstats psemstats
; /* cache effectiveness statistics */
126 int psem_cache_search
__P((struct pseminfo
**, struct psemname
*, struct psemcache
**));
128 int psem_read
__P((struct file
*fp
, struct uio
*uio
,
129 struct ucred
*cred
));
130 int psem_write
__P((struct file
*fp
, struct uio
*uio
,
131 struct ucred
*cred
));
132 int psem_ioctl
__P((struct file
*fp
, u_long com
,
133 caddr_t data
, struct proc
*p
));
134 int psem_select
__P((struct file
*fp
, int which
,
136 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 * Invalidate a all entries to particular vnode.
262 * We actually just increment the v_id, that will do it. The entries will
263 * be purged by lookup as they get found. If the v_id wraps around, we
264 * need to ditch the entire cache, to avoid confusion. No valid vnode will
265 * ever have (v_id == 0).
268 psem_cache_purge(void)
270 struct psemcache
*pcp
;
271 struct psemhashhead
*pcpp
;
273 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
274 while (pcp
= pcpp
->lh_first
)
275 psem_cache_delete(pcp
);
279 psem_cache_delete(pcp
)
280 struct psemcache
*pcp
;
283 if (pcp
->psem_hash
.le_prev
== 0)
284 panic("psem namecache purge le_prev");
285 if (pcp
->psem_hash
.le_next
== pcp
)
286 panic("namecache purge le_next");
287 #endif /* DIAGNOSTIC */
288 LIST_REMOVE(pcp
, psem_hash
);
289 pcp
->psem_hash
.le_prev
= 0;
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
;
345 #ifdef PSXSEM_NAME_RESTRICT
347 if (*nameptr
== '/') {
348 while (*(nameptr
++) == '/') {
357 #endif /* PSXSEM_NAME_RESTRICT */
361 nd
.psem_nameptr
= nameptr
;
362 nd
.psem_namelen
= plen
;
365 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
366 nd
.psem_hash
+= (unsigned char)*cp
* i
;
369 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
371 if (error
== ENOENT
) {
380 fmode
= FFLAGS(uap
->oflag
);
382 if (error
= falloc(p
, &nfp
, &indx
)) {
389 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
390 /* sem exists and opened O_EXCL */
392 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
398 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
399 /* As per POSIX, O_CREAT has no effect */
403 if (fmode
& O_CREAT
) {
404 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
408 pinfo
= (struct pseminfo
*)_MALLOC(sizeof(struct pseminfo
), M_SHM
, M_WAITOK
);
409 bzero(pinfo
, sizeof(struct pseminfo
));
411 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
412 pinfo
->psem_usecount
= 1;
413 pinfo
->psem_mode
= cmode
;
414 pinfo
->psem_uid
= p
->p_ucred
->cr_uid
;
415 pinfo
->psem_gid
= p
->p_ucred
->cr_gid
;
416 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
417 SYNC_POLICY_FIFO
, value
);
418 if(kret
!= KERN_SUCCESS
)
420 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
421 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
424 /* semaphore should exist as it is without O_CREAT */
429 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
433 if (error
= psem_access(pinfo
, fmode
, p
->p_ucred
, p
))
436 pnode
= (struct psemnode
*)_MALLOC(sizeof(struct psemnode
), M_SHM
, M_WAITOK
);
437 bzero(pnode
, sizeof(struct psemnode
));
440 if (error
= psem_cache_add(pinfo
, &nd
)) {
444 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
445 pinfo
->psem_usecount
++;
446 pnode
->pinfo
= pinfo
;
447 fp
->f_flag
= flags
& FMASK
;
448 fp
->f_type
= DTYPE_PSXSEM
;
449 fp
->f_ops
= &psemops
;
450 fp
->f_data
= (caddr_t
)pnode
;
451 *fdflags(p
, indx
) &= ~UF_RESERVED
;
453 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
458 case KERN_RESOURCE_SHORTAGE
:
460 case KERN_PROTECTION_FAILURE
:
474 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
481 psem_access(struct pseminfo
*pinfo
, int mode
, struct ucred
*cred
, struct proc
*p
)
487 /* Otherwise, user id 0 always gets access. */
488 if (cred
->cr_uid
== 0)
493 /* Otherwise, check the owner. */
494 if (cred
->cr_uid
== pinfo
->psem_uid
) {
499 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
502 /* Otherwise, check the groups. */
503 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
504 if (pinfo
->psem_gid
== *gp
) {
509 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
512 /* Otherwise, check everyone else. */
517 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
523 struct sem_unlink_args
{
528 sem_unlink(p
, uap
, retval
)
530 register struct sem_unlink_args
*uap
;
533 register struct filedesc
*fdp
= p
->p_fd
;
534 register struct file
*fp
;
538 struct pseminfo
*pinfo
;
539 extern struct fileops psemops
;
543 size_t pathlen
, plen
;
546 struct psemnode
* pnode
= PSEMNODE_NULL
;
547 struct psemcache
*pcache
= PSEMCACHE_NULL
;
550 pinfo
= PSEMINFO_NULL
;
552 MALLOC_ZONE(pnbuf
, caddr_t
,
553 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
554 pathlen
= MAXPATHLEN
;
555 error
= copyinstr(uap
->name
, pnbuf
,
556 MAXPATHLEN
, &pathlen
);
560 if (pathlen
> PSEMNAMLEN
) {
561 error
= ENAMETOOLONG
;
566 #ifdef PSXSEM_NAME_RESTRICT
568 if (*nameptr
== '/') {
569 while (*(nameptr
++) == '/') {
578 #endif /* PSXSEM_NAME_RESTRICT */
582 nd
.psem_nameptr
= nameptr
;
583 nd
.psem_namelen
= plen
;
586 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
587 nd
.psem_hash
+= (unsigned char)*cp
* i
;
590 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
592 if (error
== ENOENT
) {
602 if (error
= psem_access(pinfo
, pinfo
->psem_mode
, p
->p_ucred
, p
))
605 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
609 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
613 pinfo
->psem_flags
|= PSEM_INDELETE
;
614 pinfo
->psem_usecount
--;
616 if (!pinfo
->psem_usecount
) {
620 pinfo
->psem_flags
|= PSEM_REMOVED
;
622 psem_cache_delete(pcache
);
623 _FREE(pcache
, M_SHM
);
626 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
630 struct sem_close_args
{
635 sem_close(p
, uap
, retval
)
637 struct sem_close_args
*uap
;
640 int fd
= (int)uap
->sem
;
641 register struct filedesc
*fdp
= p
->p_fd
;
642 register struct file
*fp
;
646 if ((u_int
)fd
>= fdp
->fd_nfiles
||
647 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
648 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
651 if( error
= closef(fp
, p
))
658 struct sem_wait_args
{
663 sem_wait(p
, uap
, retval
)
665 struct sem_wait_args
*uap
;
668 int fd
= (int)uap
->sem
;
669 register struct filedesc
*fdp
= p
->p_fd
;
671 struct pseminfo
* pinfo
;
672 struct psemnode
* pnode
;
676 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
678 if (fp
->f_type
!= DTYPE_PSXSEM
)
680 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
682 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
684 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
689 kret
= semaphore_wait(pinfo
->psem_semobject
);
691 case KERN_INVALID_ADDRESS
:
692 case KERN_PROTECTION_FAILURE
:
695 case KERN_OPERATION_TIMED_OUT
:
705 struct sem_trywait_args
{
710 sem_trywait(p
, uap
, retval
)
712 struct sem_wait_args
*uap
;
715 int fd
= (int)uap
->sem
;
716 register struct filedesc
*fdp
= p
->p_fd
;
718 struct pseminfo
* pinfo
;
719 struct psemnode
* pnode
;
721 mach_timespec_t wait_time
;
724 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
726 if (fp
->f_type
!= DTYPE_PSXSEM
)
728 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
730 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
732 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
737 wait_time
.tv_sec
= 0;
738 wait_time
.tv_nsec
= 0;
740 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
742 case KERN_INVALID_ADDRESS
:
743 case KERN_PROTECTION_FAILURE
:
747 case KERN_OPERATION_TIMED_OUT
:
757 struct sem_post_args
{
762 sem_post(p
, uap
, retval
)
764 struct sem_wait_args
*uap
;
767 int fd
= (int)uap
->sem
;
768 register struct filedesc
*fdp
= p
->p_fd
;
770 struct pseminfo
* pinfo
;
771 struct psemnode
* pnode
;
775 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
777 if (fp
->f_type
!= DTYPE_PSXSEM
)
779 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
781 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
783 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
788 kret
= semaphore_signal(pinfo
->psem_semobject
);
790 case KERN_INVALID_ADDRESS
:
791 case KERN_PROTECTION_FAILURE
:
794 case KERN_OPERATION_TIMED_OUT
:
804 struct sem_init_args
{
811 sem_init(p
, uap
, retval
)
813 struct sem_init_args
*uap
;
819 struct sem_destroy_args
{
824 sem_destroy(p
, uap
, retval
)
826 struct sem_destroy_args
*uap
;
832 struct sem_getvalue_args
{
838 sem_getvalue(p
, uap
, retval
)
840 struct sem_getvalue_args
*uap
;
847 psem_closefile(fp
, p
)
852 return (psem_close(((struct psemnode
*)fp
->f_data
), fp
->f_flag
,
858 psem_close(pnode
, flags
, cred
, p
)
859 register struct psemnode
*pnode
;
866 register struct pseminfo
*pinfo
;
868 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
871 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
875 if(!pinfo
->psem_usecount
) {
876 kprintf("negative usecount in psem_close\n");
878 #endif /* DIAGNOSTIC */
879 pinfo
->psem_usecount
--;
881 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
882 error
= psem_delete(pinfo
);
890 psem_delete(struct pseminfo
* pinfo
)
894 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
897 case KERN_INVALID_ADDRESS
:
898 case KERN_PROTECTION_FAILURE
:
901 case KERN_OPERATION_TIMED_OUT
:
913 psem_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
)
918 psem_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
)
923 psem_ioctl(struct file
*fp
, u_long com
, caddr_t data
, struct proc
*p
)
928 psem_select(struct file
*fp
, int which
, struct proc
*p
)