2 * Copyright (c) 2000-2004 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 shared memory 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>
42 #include <sys/file_internal.h>
43 #include <sys/filedesc.h>
45 #include <sys/proc_internal.h>
46 #include <sys/kauth.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>
55 #include <sys/sysproto.h>
57 #include <bsm/audit_kernel.h>
59 #include <mach/mach_types.h>
60 #include <mach/mach_vm.h>
61 #include <mach/vm_map.h>
62 #include <mach/vm_prot.h>
63 #include <mach/vm_inherit.h>
64 #include <mach/kern_return.h>
65 #include <mach/memory_object_control.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_protos.h>
69 #include <vm/vm_shared_memory_server.h>
72 #include <sys/ktrace.h>
75 #define f_flag f_fglob->fg_flag
76 #define f_type f_fglob->fg_type
77 #define f_msgcount f_fglob->fg_msgcount
78 #define f_cred f_fglob->fg_cred
79 #define f_ops f_fglob->fg_ops
80 #define f_offset f_fglob->fg_offset
81 #define f_data f_fglob->fg_data
82 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
85 unsigned int pshm_flags
;
86 unsigned int pshm_usecount
;
91 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
92 void * pshm_memobject
;
94 unsigned int pshm_readcount
;
95 unsigned int pshm_writecount
;
96 struct proc
* pshm_proc
;
97 #endif /* DIAGNOSTIC */
99 #define PSHMINFO_NULL (struct pshminfo *)0
102 #define PSHM_DEFINED 2
103 #define PSHM_ALLOCATED 4
104 #define PSHM_MAPPED 8
105 #define PSHM_INUSE 0x10
106 #define PSHM_REMOVED 0x20
107 #define PSHM_INCREATE 0x40
108 #define PSHM_INDELETE 0x80
111 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
112 struct pshminfo
*pshminfo
; /* vnode the name refers to */
113 int pshm_nlen
; /* length of name */
114 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
116 #define PSHMCACHE_NULL (struct pshmcache *)0
119 long goodhits
; /* hits that we can really use */
120 long neghits
; /* negative hits that we can use */
121 long badhits
; /* hits we must drop */
122 long falsehits
; /* hits with id mismatch */
123 long miss
; /* misses */
124 long longnames
; /* long names that ignore cache */
128 char *pshm_nameptr
; /* pointer to looked up name */
129 long pshm_namelen
; /* length of looked up component */
130 u_long pshm_hash
; /* hash value of looked up name */
135 user_size_t map_size
;
136 struct pshminfo
*pinfo
;
137 unsigned int pshm_usecount
;
139 unsigned int readcnt
;
140 unsigned int writecnt
;
143 #define PSHMNODE_NULL (struct pshmnode *)0
146 #define PSHMHASH(pnp) \
147 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
149 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
150 u_long pshmhash
; /* size of hash table - 1 */
151 long pshmnument
; /* number of cache entries allocated */
152 struct pshmstats pshmstats
; /* cache effectiveness statistics */
154 static int pshm_read (struct fileproc
*fp
, struct uio
*uio
,
155 kauth_cred_t cred
, int flags
, struct proc
*p
);
156 static int pshm_write (struct fileproc
*fp
, struct uio
*uio
,
157 kauth_cred_t cred
, int flags
, struct proc
*p
);
158 static int pshm_ioctl (struct fileproc
*fp
, u_long com
,
159 caddr_t data
, struct proc
*p
);
160 static int pshm_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
161 static int pshm_close(struct pshmnode
*pnode
);
162 static int pshm_closefile (struct fileglob
*fg
, struct proc
*p
);
164 static int pshm_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
166 int pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, struct proc
*p
);
167 static int pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
);
168 static void pshm_cache_delete(struct pshmcache
*pcp
);
170 static void pshm_cache_purge(void);
171 #endif /* NOT_USED */
172 static int pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
173 struct pshmcache
**pcache
);
175 struct fileops pshmops
=
176 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
, 0 };
178 static lck_grp_t
*psx_shm_subsys_lck_grp
;
179 static lck_grp_attr_t
*psx_shm_subsys_lck_grp_attr
;
180 static lck_attr_t
*psx_shm_subsys_lck_attr
;
181 static lck_mtx_t psx_shm_subsys_mutex
;
183 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
184 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
187 /* Initialize the mutex governing access to the posix shm subsystem */
188 __private_extern__
void
189 pshm_lock_init( void )
192 psx_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
193 lck_grp_attr_setstat(psx_shm_subsys_lck_grp_attr
);
195 psx_shm_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr
);
197 psx_shm_subsys_lck_attr
= lck_attr_alloc_init();
198 /* lck_attr_setdebug(psx_shm_subsys_lck_attr); */
199 lck_mtx_init(& psx_shm_subsys_mutex
, psx_shm_subsys_lck_grp
, psx_shm_subsys_lck_attr
);
203 * Lookup an entry in the cache
206 * status of -1 is returned if matches
207 * If the lookup determines that the name does not exist
208 * (negative cacheing), a status of ENOENT is returned. If the lookup
209 * fails, a status of zero is returned.
213 pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
214 struct pshmcache
**pcache
)
216 struct pshmcache
*pcp
, *nnp
;
217 struct pshmhashhead
*pcpp
;
219 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
220 pshmstats
.longnames
++;
224 pcpp
= PSHMHASH(pnp
);
225 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
226 nnp
= pcp
->pshm_hash
.le_next
;
227 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
228 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
237 /* We found a "positive" match, return the vnode */
239 pshmstats
.goodhits
++;
241 *pshmp
= pcp
->pshminfo
;
247 * We found a "negative" match, ENOENT notifies client of this match.
248 * The nc_vpid field records whether this is a whiteout.
255 * Add an entry to the cache.
256 * XXX should be static?
259 pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
)
261 struct pshmhashhead
*pcpp
;
262 struct pshminfo
*dpinfo
;
263 struct pshmcache
*dpcp
;
266 if (pnp
->pshm_namelen
> NCHNAMLEN
)
267 panic("cache_enter: name too long");
271 /* if the entry has already been added by some one else return */
272 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
278 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
279 * For negative entries, we have to record whether it is a whiteout.
280 * the whiteout flag is stored in the nc_vpid field which is
283 pcp
->pshminfo
= pshmp
;
284 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
285 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
286 pcpp
= PSHMHASH(pnp
);
291 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
293 panic("cache_enter: duplicate");
296 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
301 * Name cache initialization, from vfs_init() when we are booting
304 pshm_cache_init(void)
306 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
311 * Invalidate a all entries to particular vnode.
313 * We actually just increment the v_id, that will do it. The entries will
314 * be purged by lookup as they get found. If the v_id wraps around, we
315 * need to ditch the entire cache, to avoid confusion. No valid vnode will
316 * ever have (v_id == 0).
319 pshm_cache_purge(void)
321 struct pshmcache
*pcp
;
322 struct pshmhashhead
*pcpp
;
324 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
325 while ( (pcp
= pcpp
->lh_first
) )
326 pshm_cache_delete(pcp
);
329 #endif /* NOT_USED */
332 pshm_cache_delete(struct pshmcache
*pcp
)
335 if (pcp
->pshm_hash
.le_prev
== 0)
336 panic("namecache purge le_prev");
337 if (pcp
->pshm_hash
.le_next
== pcp
)
338 panic("namecache purge le_next");
339 #endif /* DIAGNOSTIC */
340 LIST_REMOVE(pcp
, pshm_hash
);
341 pcp
->pshm_hash
.le_prev
= 0;
347 shm_open(struct proc
*p
, struct shm_open_args
*uap
, register_t
*retval
)
351 struct fileproc
*nfp
;
354 struct pshminfo
*pinfo
;
358 size_t pathlen
, plen
;
360 int cmode
= uap
->mode
;
362 struct pshmnode
* pnode
= PSHMNODE_NULL
;
363 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
364 struct pshmcache
*pcp
;
367 AUDIT_ARG(fflags
, uap
->oflag
);
368 AUDIT_ARG(mode
, uap
->mode
);
370 pinfo
= PSHMINFO_NULL
;
372 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
377 pathlen
= MAXPATHLEN
;
378 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
382 AUDIT_ARG(text
, pnbuf
);
383 if (pathlen
> PSHMNAMLEN
) {
384 error
= ENAMETOOLONG
;
389 #ifdef PSXSHM_NAME_RESTRICT
391 if (*nameptr
== '/') {
392 while (*(nameptr
++) == '/') {
401 #endif /* PSXSHM_NAME_RESTRICT */
405 nd
.pshm_nameptr
= nameptr
;
406 nd
.pshm_namelen
= plen
;
409 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
410 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
414 if (KTRPOINT(p
, KTR_NAMEI
))
415 ktrnamei(p
->p_tracep
, nameptr
);
419 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
421 if (error
== ENOENT
) {
422 PSHM_SUBSYS_UNLOCK();
431 fmode
= FFLAGS(uap
->oflag
);
432 if ((fmode
& (FREAD
| FWRITE
))==0) {
433 PSHM_SUBSYS_UNLOCK();
439 * XXXXXXXXXX TBD XXXXXXXXXX
440 * There is a race that existed with the funnels as well.
441 * Need to be fixed later
443 PSHM_SUBSYS_UNLOCK();
444 error
= falloc(p
, &nfp
, &indx
);
453 if (fmode
& O_CREAT
) {
454 if ((fmode
& O_EXCL
) && incache
) {
455 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
456 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
458 /* shm obj exists and opened O_EXCL */
460 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
464 PSHM_SUBSYS_UNLOCK();
468 PSHM_SUBSYS_UNLOCK();
469 /* create a new one */
470 MALLOC(pinfo
, struct pshminfo
*, sizeof(struct pshminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
477 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
478 pinfo
->pshm_usecount
= 1; /* existence reference */
479 pinfo
->pshm_mode
= cmode
;
480 pinfo
->pshm_uid
= kauth_cred_getuid(kauth_cred_get());
481 pinfo
->pshm_gid
= kauth_cred_get()->cr_gid
;
484 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
485 PSHM_SUBSYS_UNLOCK();
489 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
490 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
491 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
492 PSHM_SUBSYS_UNLOCK();
498 /* O_CREAT is not set and the shm obecj does not exist */
499 PSHM_SUBSYS_UNLOCK();
503 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
504 PSHM_SUBSYS_UNLOCK();
508 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
509 PSHM_SUBSYS_UNLOCK();
513 if (fmode
& O_TRUNC
) {
514 PSHM_SUBSYS_UNLOCK();
520 pinfo
->pshm_writecount
++;
522 pinfo
->pshm_readcount
++;
524 PSHM_SUBSYS_UNLOCK();
525 MALLOC(pnode
, struct pshmnode
*, sizeof(struct pshmnode
), M_SHM
, M_WAITOK
|M_ZERO
);
532 * We allocate a new entry if we are less than the maximum
533 * allowed and the one at the front of the LRU list is in use.
534 * Otherwise we use the one at the front of the LRU list.
536 MALLOC(pcp
, struct pshmcache
*, sizeof(struct pshmcache
), M_SHM
, M_WAITOK
|M_ZERO
);
546 if ( (error
= pshm_cache_add(pinfo
, &nd
, pcp
)) ) {
547 PSHM_SUBSYS_UNLOCK();
552 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
553 pinfo
->pshm_usecount
++; /* extra reference for the new fd */
554 pnode
->pinfo
= pinfo
;
556 PSHM_SUBSYS_UNLOCK();
558 fp
->f_flag
= fmode
& FMASK
;
559 fp
->f_type
= DTYPE_PSXSHM
;
560 fp
->f_ops
= &pshmops
;
561 fp
->f_data
= (caddr_t
)pnode
;
562 *fdflags(p
, indx
) &= ~UF_RESERVED
;
563 fp_drop(p
, indx
, fp
, 1);
567 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
576 fp_free(p
, indx
, fp
);
578 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
584 pshm_truncate(__unused
struct proc
*p
, struct fileproc
*fp
, __unused
int fd
,
585 off_t length
, __unused register_t
*retval
)
587 struct pshminfo
* pinfo
;
588 struct pshmnode
* pnode
;
590 vm_offset_t user_addr
;
591 mem_entry_name_port_t mem_object
;
594 if (fp
->f_type
!= DTYPE_PSXSHM
) {
599 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
603 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
604 PSHM_SUBSYS_UNLOCK();
607 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
609 PSHM_SUBSYS_UNLOCK();
613 PSHM_SUBSYS_UNLOCK();
614 size
= round_page_64(length
);
615 kret
= vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
616 if (kret
!= KERN_SUCCESS
)
619 kret
= mach_make_memory_entry (current_map(), &size
,
620 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
622 if (kret
!= KERN_SUCCESS
)
625 vm_deallocate(current_map(), user_addr
, size
);
628 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
629 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
630 pinfo
->pshm_memobject
= (void *)mem_object
;
631 pinfo
->pshm_length
= size
;
632 PSHM_SUBSYS_UNLOCK();
637 case KERN_INVALID_ADDRESS
:
640 case KERN_PROTECTION_FAILURE
:
649 pshm_stat(struct pshmnode
*pnode
, struct stat
*sb
)
651 struct pshminfo
*pinfo
;
654 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
){
655 PSHM_SUBSYS_UNLOCK();
659 bzero(sb
, sizeof(struct stat
));
660 sb
->st_mode
= pinfo
->pshm_mode
;
661 sb
->st_uid
= pinfo
->pshm_uid
;
662 sb
->st_gid
= pinfo
->pshm_gid
;
663 sb
->st_size
= pinfo
->pshm_length
;
664 PSHM_SUBSYS_UNLOCK();
670 * This is called only from shm_open which holds pshm_lock();
671 * XXX This code is repeated many times
674 pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, __unused
struct proc
*p
)
679 /* Otherwise, user id 0 always gets access. */
680 if (!suser(cred
, NULL
))
685 /* Otherwise, check the owner. */
686 if (kauth_cred_getuid(cred
) == pinfo
->pshm_uid
) {
691 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
694 /* Otherwise, check the groups. */
695 if (kauth_cred_ismember_gid(cred
, pinfo
->pshm_gid
, &is_member
) == 0 && is_member
) {
700 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
703 /* Otherwise, check everyone else. */
708 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
712 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
, struct fileproc
*fp
, off_t pageoff
)
714 mach_vm_offset_t user_addr
= (mach_vm_offset_t
)uap
->addr
;
715 mach_vm_size_t user_size
= (mach_vm_size_t
)uap
->len
;
716 int prot
= uap
->prot
;
717 int flags
= uap
->flags
;
718 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
724 struct pshminfo
* pinfo
;
725 struct pshmnode
* pnode
;
731 if ((flags
& MAP_SHARED
) == 0)
735 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
739 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
743 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
744 PSHM_SUBSYS_UNLOCK();
748 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
749 PSHM_SUBSYS_UNLOCK();
752 if ((off_t
)user_size
> pinfo
->pshm_length
) {
753 PSHM_SUBSYS_UNLOCK();
756 if ((off_t
)(user_size
+ file_pos
) > pinfo
->pshm_length
) {
757 PSHM_SUBSYS_UNLOCK();
760 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
761 PSHM_SUBSYS_UNLOCK();
766 PSHM_SUBSYS_UNLOCK();
767 user_map
= current_map();
769 if ((flags
& MAP_FIXED
) == 0) {
770 alloc_flags
= VM_FLAGS_ANYWHERE
;
771 user_addr
= mach_vm_round_page(user_addr
);
773 if (user_addr
!= mach_vm_trunc_page(user_addr
))
776 * We do not get rid of the existing mappings here because
777 * it wouldn't be atomic (see comment in mmap()). We let
778 * Mach VM know that we want it to replace any existing
779 * mapping with the new one.
781 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
785 kret
= mach_vm_map(user_map
, &user_addr
, user_size
,
786 0, alloc_flags
, pinfo
->pshm_memobject
, file_pos
, docow
,
787 prot
, VM_PROT_DEFAULT
,
789 if (kret
!= KERN_SUCCESS
)
791 /* LP64todo - this should be superfluous at this point */
792 kret
= mach_vm_inherit(user_map
, user_addr
, user_size
,
794 if (kret
!= KERN_SUCCESS
) {
795 (void) mach_vm_deallocate(user_map
, user_addr
, user_size
);
799 pnode
->mapp_addr
= user_addr
;
800 pnode
->map_size
= user_size
;
801 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
802 PSHM_SUBSYS_UNLOCK();
806 *retval
= (user_addr
+ pageoff
);
808 case KERN_INVALID_ADDRESS
:
811 case KERN_PROTECTION_FAILURE
:
820 shm_unlink(__unused
struct proc
*p
, struct shm_unlink_args
*uap
,
821 __unused register_t
*retval
)
826 struct pshminfo
*pinfo
;
830 size_t pathlen
, plen
;
832 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
834 pinfo
= PSHMINFO_NULL
;
836 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
838 return(ENOSPC
); /* XXX non-standard */
840 pathlen
= MAXPATHLEN
;
841 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
845 AUDIT_ARG(text
, pnbuf
);
846 if (pathlen
> PSHMNAMLEN
) {
847 error
= ENAMETOOLONG
;
852 #ifdef PSXSHM_NAME_RESTRICT
854 if (*nameptr
== '/') {
855 while (*(nameptr
++) == '/') {
864 #endif /* PSXSHM_NAME_RESTRICT */
868 nd
.pshm_nameptr
= nameptr
;
869 nd
.pshm_namelen
= plen
;
872 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
873 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
877 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
879 if (error
== ENOENT
) {
880 PSHM_SUBSYS_UNLOCK();
886 PSHM_SUBSYS_UNLOCK();
892 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
893 PSHM_SUBSYS_UNLOCK();
897 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
898 PSHM_SUBSYS_UNLOCK();
903 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
, pinfo
->pshm_gid
,
907 * JMM - How should permissions be checked?
910 pinfo
->pshm_flags
|= PSHM_INDELETE
;
911 pshm_cache_delete(pcache
);
912 pinfo
->pshm_flags
|= PSHM_REMOVED
;
913 /* release the existence reference */
914 if (!--pinfo
->pshm_usecount
) {
915 PSHM_SUBSYS_UNLOCK();
917 * If this is the last reference going away on the object,
918 * then we need to destroy the backing object. The name
919 * has an implied but uncounted reference on the object,
920 * once it's created, since it's used as a rendesvous, and
921 * therefore may be subsequently reopened.
923 if (pinfo
->pshm_memobject
!= NULL
)
924 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
928 PSHM_SUBSYS_UNLOCK();
932 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
936 /* already called locked */
938 pshm_close(struct pshmnode
*pnode
)
941 struct pshminfo
*pinfo
;
943 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
946 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
950 if(!pinfo
->pshm_usecount
) {
951 kprintf("negative usecount in pshm_close\n");
953 #endif /* DIAGNOSTIC */
954 pinfo
->pshm_usecount
--; /* release this fd's reference */
956 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
957 PSHM_SUBSYS_UNLOCK();
959 * If this is the last reference going away on the object,
960 * then we need to destroy the backing object.
962 if (pinfo
->pshm_memobject
!= NULL
)
963 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
971 /* struct proc passed to match prototype for struct fileops */
973 pshm_closefile(struct fileglob
*fg
, __unused
struct proc
*p
)
978 error
= pshm_close(((struct pshmnode
*)fg
->fg_data
));
979 PSHM_SUBSYS_UNLOCK();
984 pshm_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
985 __unused kauth_cred_t cred
, __unused
int flags
,
986 __unused
struct proc
*p
)
992 pshm_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
993 __unused kauth_cred_t cred
, __unused
int flags
,
994 __unused
struct proc
*p
)
1000 pshm_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1001 __unused caddr_t data
, __unused
struct proc
*p
)
1007 pshm_select(__unused
struct fileproc
*fp
, __unused
int which
, __unused
void *wql
,
1008 __unused
struct proc
*p
)
1014 pshm_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1015 __unused
struct proc
*p
)