2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
33 * posix_shm.c : Support for POSIX shared memory APIs
36 * Author: Ananthakrishna Ramesh
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/file_internal.h>
49 #include <sys/filedesc.h>
51 #include <sys/proc_internal.h>
52 #include <sys/kauth.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/ioctl.h>
58 #include <sys/malloc.h>
61 #include <sys/sysproto.h>
63 #include <bsm/audit_kernel.h>
65 #include <mach/mach_types.h>
66 #include <mach/mach_vm.h>
67 #include <mach/vm_map.h>
68 #include <mach/vm_prot.h>
69 #include <mach/vm_inherit.h>
70 #include <mach/kern_return.h>
71 #include <mach/memory_object_control.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_protos.h>
75 #include <vm/vm_shared_memory_server.h>
78 #include <sys/ktrace.h>
81 #define f_flag f_fglob->fg_flag
82 #define f_type f_fglob->fg_type
83 #define f_msgcount f_fglob->fg_msgcount
84 #define f_cred f_fglob->fg_cred
85 #define f_ops f_fglob->fg_ops
86 #define f_offset f_fglob->fg_offset
87 #define f_data f_fglob->fg_data
88 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
91 unsigned int pshm_flags
;
92 unsigned int pshm_usecount
;
97 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
98 void * pshm_memobject
;
100 unsigned int pshm_readcount
;
101 unsigned int pshm_writecount
;
102 struct proc
* pshm_proc
;
103 #endif /* DIAGNOSTIC */
105 #define PSHMINFO_NULL (struct pshminfo *)0
108 #define PSHM_DEFINED 2
109 #define PSHM_ALLOCATED 4
110 #define PSHM_MAPPED 8
111 #define PSHM_INUSE 0x10
112 #define PSHM_REMOVED 0x20
113 #define PSHM_INCREATE 0x40
114 #define PSHM_INDELETE 0x80
117 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
118 struct pshminfo
*pshminfo
; /* vnode the name refers to */
119 int pshm_nlen
; /* length of name */
120 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
122 #define PSHMCACHE_NULL (struct pshmcache *)0
125 long goodhits
; /* hits that we can really use */
126 long neghits
; /* negative hits that we can use */
127 long badhits
; /* hits we must drop */
128 long falsehits
; /* hits with id mismatch */
129 long miss
; /* misses */
130 long longnames
; /* long names that ignore cache */
134 char *pshm_nameptr
; /* pointer to looked up name */
135 long pshm_namelen
; /* length of looked up component */
136 u_long pshm_hash
; /* hash value of looked up name */
141 user_size_t map_size
;
142 struct pshminfo
*pinfo
;
143 unsigned int pshm_usecount
;
145 unsigned int readcnt
;
146 unsigned int writecnt
;
149 #define PSHMNODE_NULL (struct pshmnode *)0
152 #define PSHMHASH(pnp) \
153 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
155 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
156 u_long pshmhash
; /* size of hash table - 1 */
157 long pshmnument
; /* number of cache entries allocated */
158 struct pshmstats pshmstats
; /* cache effectiveness statistics */
160 static int pshm_read (struct fileproc
*fp
, struct uio
*uio
,
161 kauth_cred_t cred
, int flags
, struct proc
*p
);
162 static int pshm_write (struct fileproc
*fp
, struct uio
*uio
,
163 kauth_cred_t cred
, int flags
, struct proc
*p
);
164 static int pshm_ioctl (struct fileproc
*fp
, u_long com
,
165 caddr_t data
, struct proc
*p
);
166 static int pshm_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
167 static int pshm_close(struct pshmnode
*pnode
);
168 static int pshm_closefile (struct fileglob
*fg
, struct proc
*p
);
170 static int pshm_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
172 int pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, struct proc
*p
);
173 static int pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
);
174 static void pshm_cache_delete(struct pshmcache
*pcp
);
176 static void pshm_cache_purge(void);
177 #endif /* NOT_USED */
178 static int pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
179 struct pshmcache
**pcache
);
181 struct fileops pshmops
=
182 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
, 0 };
184 static lck_grp_t
*psx_shm_subsys_lck_grp
;
185 static lck_grp_attr_t
*psx_shm_subsys_lck_grp_attr
;
186 static lck_attr_t
*psx_shm_subsys_lck_attr
;
187 static lck_mtx_t psx_shm_subsys_mutex
;
189 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
190 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
193 /* Initialize the mutex governing access to the posix shm subsystem */
194 __private_extern__
void
195 pshm_lock_init( void )
198 psx_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
199 lck_grp_attr_setstat(psx_shm_subsys_lck_grp_attr
);
201 psx_shm_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr
);
203 psx_shm_subsys_lck_attr
= lck_attr_alloc_init();
204 /* lck_attr_setdebug(psx_shm_subsys_lck_attr); */
205 lck_mtx_init(& psx_shm_subsys_mutex
, psx_shm_subsys_lck_grp
, psx_shm_subsys_lck_attr
);
209 * Lookup an entry in the cache
212 * status of -1 is returned if matches
213 * If the lookup determines that the name does not exist
214 * (negative cacheing), a status of ENOENT is returned. If the lookup
215 * fails, a status of zero is returned.
219 pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
220 struct pshmcache
**pcache
)
222 struct pshmcache
*pcp
, *nnp
;
223 struct pshmhashhead
*pcpp
;
225 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
226 pshmstats
.longnames
++;
230 pcpp
= PSHMHASH(pnp
);
231 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
232 nnp
= pcp
->pshm_hash
.le_next
;
233 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
234 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
243 /* We found a "positive" match, return the vnode */
245 pshmstats
.goodhits
++;
247 *pshmp
= pcp
->pshminfo
;
253 * We found a "negative" match, ENOENT notifies client of this match.
254 * The nc_vpid field records whether this is a whiteout.
261 * Add an entry to the cache.
262 * XXX should be static?
265 pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
)
267 struct pshmhashhead
*pcpp
;
268 struct pshminfo
*dpinfo
;
269 struct pshmcache
*dpcp
;
272 if (pnp
->pshm_namelen
> NCHNAMLEN
)
273 panic("cache_enter: name too long");
277 /* if the entry has already been added by some one else return */
278 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
284 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
285 * For negative entries, we have to record whether it is a whiteout.
286 * the whiteout flag is stored in the nc_vpid field which is
289 pcp
->pshminfo
= pshmp
;
290 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
291 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
292 pcpp
= PSHMHASH(pnp
);
297 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
299 panic("cache_enter: duplicate");
302 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
307 * Name cache initialization, from vfs_init() when we are booting
310 pshm_cache_init(void)
312 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
317 * Invalidate a all entries to particular vnode.
319 * We actually just increment the v_id, that will do it. The entries will
320 * be purged by lookup as they get found. If the v_id wraps around, we
321 * need to ditch the entire cache, to avoid confusion. No valid vnode will
322 * ever have (v_id == 0).
325 pshm_cache_purge(void)
327 struct pshmcache
*pcp
;
328 struct pshmhashhead
*pcpp
;
330 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
331 while ( (pcp
= pcpp
->lh_first
) )
332 pshm_cache_delete(pcp
);
335 #endif /* NOT_USED */
338 pshm_cache_delete(struct pshmcache
*pcp
)
341 if (pcp
->pshm_hash
.le_prev
== 0)
342 panic("namecache purge le_prev");
343 if (pcp
->pshm_hash
.le_next
== pcp
)
344 panic("namecache purge le_next");
345 #endif /* DIAGNOSTIC */
346 LIST_REMOVE(pcp
, pshm_hash
);
347 pcp
->pshm_hash
.le_prev
= 0;
353 shm_open(struct proc
*p
, struct shm_open_args
*uap
, register_t
*retval
)
357 struct fileproc
*nfp
;
360 struct pshminfo
*pinfo
;
364 size_t pathlen
, plen
;
366 int cmode
= uap
->mode
;
368 struct pshmnode
* pnode
= PSHMNODE_NULL
;
369 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
370 struct pshmcache
*pcp
;
373 AUDIT_ARG(fflags
, uap
->oflag
);
374 AUDIT_ARG(mode
, uap
->mode
);
376 pinfo
= PSHMINFO_NULL
;
378 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
383 pathlen
= MAXPATHLEN
;
384 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
388 AUDIT_ARG(text
, pnbuf
);
389 if (pathlen
> PSHMNAMLEN
) {
390 error
= ENAMETOOLONG
;
395 #ifdef PSXSHM_NAME_RESTRICT
397 if (*nameptr
== '/') {
398 while (*(nameptr
++) == '/') {
407 #endif /* PSXSHM_NAME_RESTRICT */
411 nd
.pshm_nameptr
= nameptr
;
412 nd
.pshm_namelen
= plen
;
415 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
416 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
420 if (KTRPOINT(p
, KTR_NAMEI
))
421 ktrnamei(p
->p_tracep
, nameptr
);
425 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
427 if (error
== ENOENT
) {
428 PSHM_SUBSYS_UNLOCK();
437 fmode
= FFLAGS(uap
->oflag
);
438 if ((fmode
& (FREAD
| FWRITE
))==0) {
439 PSHM_SUBSYS_UNLOCK();
445 * XXXXXXXXXX TBD XXXXXXXXXX
446 * There is a race that existed with the funnels as well.
447 * Need to be fixed later
449 PSHM_SUBSYS_UNLOCK();
450 error
= falloc(p
, &nfp
, &indx
);
459 if (fmode
& O_CREAT
) {
460 if ((fmode
& O_EXCL
) && incache
) {
461 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
462 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
464 /* shm obj exists and opened O_EXCL */
466 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
470 PSHM_SUBSYS_UNLOCK();
474 PSHM_SUBSYS_UNLOCK();
475 /* create a new one */
476 MALLOC(pinfo
, struct pshminfo
*, sizeof(struct pshminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
483 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
484 pinfo
->pshm_usecount
= 1; /* existence reference */
485 pinfo
->pshm_mode
= cmode
;
486 pinfo
->pshm_uid
= kauth_cred_getuid(kauth_cred_get());
487 pinfo
->pshm_gid
= kauth_cred_get()->cr_gid
;
490 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
491 PSHM_SUBSYS_UNLOCK();
495 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
496 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
497 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
498 PSHM_SUBSYS_UNLOCK();
504 /* O_CREAT is not set and the shm obecj does not exist */
505 PSHM_SUBSYS_UNLOCK();
509 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
510 PSHM_SUBSYS_UNLOCK();
514 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
515 PSHM_SUBSYS_UNLOCK();
519 if (fmode
& O_TRUNC
) {
520 PSHM_SUBSYS_UNLOCK();
526 pinfo
->pshm_writecount
++;
528 pinfo
->pshm_readcount
++;
530 PSHM_SUBSYS_UNLOCK();
531 MALLOC(pnode
, struct pshmnode
*, sizeof(struct pshmnode
), M_SHM
, M_WAITOK
|M_ZERO
);
538 * We allocate a new entry if we are less than the maximum
539 * allowed and the one at the front of the LRU list is in use.
540 * Otherwise we use the one at the front of the LRU list.
542 MALLOC(pcp
, struct pshmcache
*, sizeof(struct pshmcache
), M_SHM
, M_WAITOK
|M_ZERO
);
552 if ( (error
= pshm_cache_add(pinfo
, &nd
, pcp
)) ) {
553 PSHM_SUBSYS_UNLOCK();
558 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
559 pinfo
->pshm_usecount
++; /* extra reference for the new fd */
560 pnode
->pinfo
= pinfo
;
562 PSHM_SUBSYS_UNLOCK();
564 fp
->f_flag
= fmode
& FMASK
;
565 fp
->f_type
= DTYPE_PSXSHM
;
566 fp
->f_ops
= &pshmops
;
567 fp
->f_data
= (caddr_t
)pnode
;
568 *fdflags(p
, indx
) &= ~UF_RESERVED
;
569 fp_drop(p
, indx
, fp
, 1);
573 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
582 fp_free(p
, indx
, fp
);
584 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
590 pshm_truncate(__unused
struct proc
*p
, struct fileproc
*fp
, __unused
int fd
,
591 off_t length
, __unused register_t
*retval
)
593 struct pshminfo
* pinfo
;
594 struct pshmnode
* pnode
;
596 vm_offset_t user_addr
;
597 mem_entry_name_port_t mem_object
;
600 if (fp
->f_type
!= DTYPE_PSXSHM
) {
605 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
609 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
610 PSHM_SUBSYS_UNLOCK();
613 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
615 PSHM_SUBSYS_UNLOCK();
619 PSHM_SUBSYS_UNLOCK();
620 size
= round_page_64(length
);
621 kret
= vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
622 if (kret
!= KERN_SUCCESS
)
625 kret
= mach_make_memory_entry (current_map(), &size
,
626 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
628 if (kret
!= KERN_SUCCESS
)
631 vm_deallocate(current_map(), user_addr
, size
);
634 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
635 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
636 pinfo
->pshm_memobject
= (void *)mem_object
;
637 pinfo
->pshm_length
= size
;
638 PSHM_SUBSYS_UNLOCK();
643 case KERN_INVALID_ADDRESS
:
646 case KERN_PROTECTION_FAILURE
:
655 pshm_stat(struct pshmnode
*pnode
, struct stat
*sb
)
657 struct pshminfo
*pinfo
;
660 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
){
661 PSHM_SUBSYS_UNLOCK();
665 bzero(sb
, sizeof(struct stat
));
666 sb
->st_mode
= pinfo
->pshm_mode
;
667 sb
->st_uid
= pinfo
->pshm_uid
;
668 sb
->st_gid
= pinfo
->pshm_gid
;
669 sb
->st_size
= pinfo
->pshm_length
;
670 PSHM_SUBSYS_UNLOCK();
676 * This is called only from shm_open which holds pshm_lock();
677 * XXX This code is repeated many times
680 pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, __unused
struct proc
*p
)
685 /* Otherwise, user id 0 always gets access. */
686 if (!suser(cred
, NULL
))
691 /* Otherwise, check the owner. */
692 if (kauth_cred_getuid(cred
) == pinfo
->pshm_uid
) {
697 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
700 /* Otherwise, check the groups. */
701 if (kauth_cred_ismember_gid(cred
, pinfo
->pshm_gid
, &is_member
) == 0 && is_member
) {
706 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
709 /* Otherwise, check everyone else. */
714 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
718 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
, struct fileproc
*fp
, off_t pageoff
)
720 mach_vm_offset_t user_addr
= (mach_vm_offset_t
)uap
->addr
;
721 mach_vm_size_t user_size
= (mach_vm_size_t
)uap
->len
;
722 int prot
= uap
->prot
;
723 int flags
= uap
->flags
;
724 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
730 struct pshminfo
* pinfo
;
731 struct pshmnode
* pnode
;
737 if ((flags
& MAP_SHARED
) == 0)
741 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
745 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
749 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
750 PSHM_SUBSYS_UNLOCK();
754 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
755 PSHM_SUBSYS_UNLOCK();
758 if ((off_t
)user_size
> pinfo
->pshm_length
) {
759 PSHM_SUBSYS_UNLOCK();
762 if ((off_t
)(user_size
+ file_pos
) > pinfo
->pshm_length
) {
763 PSHM_SUBSYS_UNLOCK();
766 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
767 PSHM_SUBSYS_UNLOCK();
772 PSHM_SUBSYS_UNLOCK();
773 user_map
= current_map();
775 if ((flags
& MAP_FIXED
) == 0) {
776 alloc_flags
= VM_FLAGS_ANYWHERE
;
777 user_addr
= mach_vm_round_page(user_addr
);
779 if (user_addr
!= mach_vm_trunc_page(user_addr
))
782 * We do not get rid of the existing mappings here because
783 * it wouldn't be atomic (see comment in mmap()). We let
784 * Mach VM know that we want it to replace any existing
785 * mapping with the new one.
787 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
791 kret
= mach_vm_map(user_map
, &user_addr
, user_size
,
792 0, alloc_flags
, pinfo
->pshm_memobject
, file_pos
, docow
,
793 prot
, VM_PROT_DEFAULT
,
795 if (kret
!= KERN_SUCCESS
)
797 /* LP64todo - this should be superfluous at this point */
798 kret
= mach_vm_inherit(user_map
, user_addr
, user_size
,
800 if (kret
!= KERN_SUCCESS
) {
801 (void) mach_vm_deallocate(user_map
, user_addr
, user_size
);
805 pnode
->mapp_addr
= user_addr
;
806 pnode
->map_size
= user_size
;
807 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
808 PSHM_SUBSYS_UNLOCK();
812 *retval
= (user_addr
+ pageoff
);
814 case KERN_INVALID_ADDRESS
:
817 case KERN_PROTECTION_FAILURE
:
826 shm_unlink(__unused
struct proc
*p
, struct shm_unlink_args
*uap
,
827 __unused register_t
*retval
)
832 struct pshminfo
*pinfo
;
836 size_t pathlen
, plen
;
838 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
840 pinfo
= PSHMINFO_NULL
;
842 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
844 return(ENOSPC
); /* XXX non-standard */
846 pathlen
= MAXPATHLEN
;
847 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
851 AUDIT_ARG(text
, pnbuf
);
852 if (pathlen
> PSHMNAMLEN
) {
853 error
= ENAMETOOLONG
;
858 #ifdef PSXSHM_NAME_RESTRICT
860 if (*nameptr
== '/') {
861 while (*(nameptr
++) == '/') {
870 #endif /* PSXSHM_NAME_RESTRICT */
874 nd
.pshm_nameptr
= nameptr
;
875 nd
.pshm_namelen
= plen
;
878 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
879 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
883 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
885 if (error
== ENOENT
) {
886 PSHM_SUBSYS_UNLOCK();
892 PSHM_SUBSYS_UNLOCK();
898 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
899 PSHM_SUBSYS_UNLOCK();
903 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
904 PSHM_SUBSYS_UNLOCK();
909 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
, pinfo
->pshm_gid
,
913 * JMM - How should permissions be checked?
916 pinfo
->pshm_flags
|= PSHM_INDELETE
;
917 pshm_cache_delete(pcache
);
918 pinfo
->pshm_flags
|= PSHM_REMOVED
;
919 /* release the existence reference */
920 if (!--pinfo
->pshm_usecount
) {
921 PSHM_SUBSYS_UNLOCK();
923 * If this is the last reference going away on the object,
924 * then we need to destroy the backing object. The name
925 * has an implied but uncounted reference on the object,
926 * once it's created, since it's used as a rendesvous, and
927 * therefore may be subsequently reopened.
929 if (pinfo
->pshm_memobject
!= NULL
)
930 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
934 PSHM_SUBSYS_UNLOCK();
938 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
942 /* already called locked */
944 pshm_close(struct pshmnode
*pnode
)
947 struct pshminfo
*pinfo
;
949 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
952 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
956 if(!pinfo
->pshm_usecount
) {
957 kprintf("negative usecount in pshm_close\n");
959 #endif /* DIAGNOSTIC */
960 pinfo
->pshm_usecount
--; /* release this fd's reference */
962 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
963 PSHM_SUBSYS_UNLOCK();
965 * If this is the last reference going away on the object,
966 * then we need to destroy the backing object.
968 if (pinfo
->pshm_memobject
!= NULL
)
969 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
977 /* struct proc passed to match prototype for struct fileops */
979 pshm_closefile(struct fileglob
*fg
, __unused
struct proc
*p
)
984 error
= pshm_close(((struct pshmnode
*)fg
->fg_data
));
985 PSHM_SUBSYS_UNLOCK();
990 pshm_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
991 __unused kauth_cred_t cred
, __unused
int flags
,
992 __unused
struct proc
*p
)
998 pshm_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
999 __unused kauth_cred_t cred
, __unused
int flags
,
1000 __unused
struct proc
*p
)
1006 pshm_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1007 __unused caddr_t data
, __unused
struct proc
*p
)
1013 pshm_select(__unused
struct fileproc
*fp
, __unused
int which
, __unused
void *wql
,
1014 __unused
struct proc
*p
)
1020 pshm_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1021 __unused
struct proc
*p
)