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 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 pinfo
= PSEMINFO_NULL
;
336 MALLOC_ZONE(pnbuf
, caddr_t
,
337 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
338 pathlen
= MAXPATHLEN
;
339 error
= copyinstr((void *)uap
->name
, pnbuf
,
340 MAXPATHLEN
, &pathlen
);
344 if (pathlen
> PSEMNAMLEN
) {
345 error
= ENAMETOOLONG
;
349 #ifdef PSXSEM_NAME_RESTRICT
351 if (*nameptr
== '/') {
352 while (*(nameptr
++) == '/') {
361 #endif /* PSXSEM_NAME_RESTRICT */
365 nd
.psem_nameptr
= nameptr
;
366 nd
.psem_namelen
= plen
;
369 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
370 nd
.psem_hash
+= (unsigned char)*cp
* i
;
373 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
375 if (error
== ENOENT
) {
384 fmode
= FFLAGS(uap
->oflag
);
386 if (error
= falloc(p
, &nfp
, &indx
)) {
393 if (((fmode
& (O_CREAT
| O_EXCL
))==(O_CREAT
| O_EXCL
)) && incache
) {
394 /* sem exists and opened O_EXCL */
396 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
402 if (((fmode
& (O_CREAT
| O_EXCL
))== O_CREAT
) && incache
) {
403 /* As per POSIX, O_CREAT has no effect */
407 if (fmode
& O_CREAT
) {
408 if((value
< 0) && (value
> SEM_VALUE_MAX
)) {
412 pinfo
= (struct pseminfo
*)_MALLOC(sizeof(struct pseminfo
), M_SHM
, M_WAITOK
);
413 bzero(pinfo
, sizeof(struct pseminfo
));
415 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
416 pinfo
->psem_usecount
= 1;
417 pinfo
->psem_mode
= cmode
;
418 pinfo
->psem_uid
= p
->p_ucred
->cr_uid
;
419 pinfo
->psem_gid
= p
->p_ucred
->cr_gid
;
420 kret
= semaphore_create(kernel_task
, &pinfo
->psem_semobject
,
421 SYNC_POLICY_FIFO
, value
);
422 if(kret
!= KERN_SUCCESS
)
424 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
425 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
428 /* semaphore should exist as it is without O_CREAT */
433 if( pinfo
->psem_flags
& PSEM_INDELETE
) {
437 if (error
= psem_access(pinfo
, fmode
, p
->p_ucred
, p
))
440 pnode
= (struct psemnode
*)_MALLOC(sizeof(struct psemnode
), M_SHM
, M_WAITOK
);
441 bzero(pnode
, sizeof(struct psemnode
));
444 if (error
= psem_cache_add(pinfo
, &nd
)) {
448 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
449 pinfo
->psem_usecount
++;
450 pnode
->pinfo
= pinfo
;
451 fp
->f_flag
= fmode
& FMASK
;
452 fp
->f_type
= DTYPE_PSXSEM
;
453 fp
->f_ops
= &psemops
;
454 fp
->f_data
= (caddr_t
)pnode
;
455 *fdflags(p
, indx
) &= ~UF_RESERVED
;
457 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
462 case KERN_RESOURCE_SHORTAGE
:
464 case KERN_PROTECTION_FAILURE
:
478 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
483 psem_access(pinfo
, mode
, cred
, p
)
484 struct pseminfo
*pinfo
;
493 /* Otherwise, user id 0 always gets access. */
494 if (cred
->cr_uid
== 0)
499 /* Otherwise, check the owner. */
500 if (cred
->cr_uid
== pinfo
->psem_uid
) {
505 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
508 /* Otherwise, check the groups. */
509 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
510 if (pinfo
->psem_gid
== *gp
) {
515 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
518 /* Otherwise, check everyone else. */
523 return ((pinfo
->psem_mode
& mask
) == mask
? 0 : EACCES
);
526 struct sem_unlink_args
{
531 sem_unlink(p
, uap
, retval
)
533 register struct sem_unlink_args
*uap
;
536 register struct filedesc
*fdp
= p
->p_fd
;
537 register struct file
*fp
;
541 struct pseminfo
*pinfo
;
542 extern struct fileops psemops
;
546 size_t pathlen
, plen
;
549 struct psemnode
* pnode
= PSEMNODE_NULL
;
550 struct psemcache
*pcache
= PSEMCACHE_NULL
;
553 pinfo
= PSEMINFO_NULL
;
555 MALLOC_ZONE(pnbuf
, caddr_t
,
556 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
557 pathlen
= MAXPATHLEN
;
558 error
= copyinstr((void *)uap
->name
, pnbuf
,
559 MAXPATHLEN
, &pathlen
);
563 if (pathlen
> PSEMNAMLEN
) {
564 error
= ENAMETOOLONG
;
569 #ifdef PSXSEM_NAME_RESTRICT
571 if (*nameptr
== '/') {
572 while (*(nameptr
++) == '/') {
581 #endif /* PSXSEM_NAME_RESTRICT */
585 nd
.psem_nameptr
= nameptr
;
586 nd
.psem_namelen
= plen
;
589 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
590 nd
.psem_hash
+= (unsigned char)*cp
* i
;
593 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
595 if (error
== ENOENT
) {
605 if (error
= psem_access(pinfo
, pinfo
->psem_mode
, p
->p_ucred
, p
))
608 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))==0) {
612 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
616 pinfo
->psem_flags
|= PSEM_INDELETE
;
617 pinfo
->psem_usecount
--;
619 if (!pinfo
->psem_usecount
) {
623 pinfo
->psem_flags
|= PSEM_REMOVED
;
625 psem_cache_delete(pcache
);
626 _FREE(pcache
, M_SHM
);
629 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
633 struct sem_close_args
{
638 sem_close(p
, uap
, retval
)
640 struct sem_close_args
*uap
;
643 int fd
= (int)uap
->sem
;
644 register struct filedesc
*fdp
= p
->p_fd
;
645 register struct file
*fp
;
649 if ((u_int
)fd
>= fdp
->fd_nfiles
||
650 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
651 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
654 if( error
= closef(fp
, p
))
659 struct sem_wait_args
{
664 sem_wait(p
, uap
, retval
)
666 struct sem_wait_args
*uap
;
669 int fd
= (int)uap
->sem
;
670 register struct filedesc
*fdp
= p
->p_fd
;
672 struct pseminfo
* pinfo
;
673 struct psemnode
* pnode
;
677 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
679 if (fp
->f_type
!= DTYPE_PSXSEM
)
681 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
683 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
685 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
690 kret
= semaphore_wait(pinfo
->psem_semobject
);
692 case KERN_INVALID_ADDRESS
:
693 case KERN_PROTECTION_FAILURE
:
696 case KERN_OPERATION_TIMED_OUT
:
705 struct sem_trywait_args
{
710 sem_trywait(p
, uap
, retval
)
712 struct sem_trywait_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
:
756 struct sem_post_args
{
761 sem_post(p
, uap
, retval
)
763 struct sem_post_args
*uap
;
766 int fd
= (int)uap
->sem
;
767 register struct filedesc
*fdp
= p
->p_fd
;
769 struct pseminfo
* pinfo
;
770 struct psemnode
* pnode
;
774 if (error
= fdgetf(p
, (int)uap
->sem
, &fp
))
776 if (fp
->f_type
!= DTYPE_PSXSEM
)
778 if (((pnode
= (struct psemnode
*)fp
->f_data
)) == PSEMNODE_NULL
)
780 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
782 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
787 kret
= semaphore_signal(pinfo
->psem_semobject
);
789 case KERN_INVALID_ADDRESS
:
790 case KERN_PROTECTION_FAILURE
:
793 case KERN_OPERATION_TIMED_OUT
:
802 struct sem_init_args
{
809 sem_init(p
, uap
, retval
)
811 struct sem_init_args
*uap
;
817 struct sem_destroy_args
{
822 sem_destroy(p
, uap
, retval
)
824 struct sem_destroy_args
*uap
;
830 struct sem_getvalue_args
{
836 sem_getvalue(p
, uap
, retval
)
838 struct sem_getvalue_args
*uap
;
845 psem_close(pnode
, flags
, cred
, p
)
846 register struct psemnode
*pnode
;
853 register struct pseminfo
*pinfo
;
855 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
)
858 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
862 if(!pinfo
->psem_usecount
) {
863 kprintf("negative usecount in psem_close\n");
865 #endif /* DIAGNOSTIC */
866 pinfo
->psem_usecount
--;
868 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
869 error
= psem_delete(pinfo
);
877 psem_closefile(fp
, p
)
882 return (psem_close(((struct psemnode
*)fp
->f_data
), fp
->f_flag
,
887 psem_delete(struct pseminfo
* pinfo
)
891 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
894 case KERN_INVALID_ADDRESS
:
895 case KERN_PROTECTION_FAILURE
:
898 case KERN_OPERATION_TIMED_OUT
:
908 psem_read(fp
, uio
, cred
, flags
, p
)
919 psem_write(fp
, uio
, cred
, flags
, p
)
930 psem_ioctl(fp
, com
, data
, p
)
940 psem_select(fp
, which
, wql
, p
)
950 psem_kqfilter(fp
, kn
, p
)