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>
56 #include <sys/proc_info.h>
58 #include <bsm/audit_kernel.h>
60 #include <mach/mach_types.h>
61 #include <mach/mach_vm.h>
62 #include <mach/vm_map.h>
63 #include <mach/vm_prot.h>
64 #include <mach/vm_inherit.h>
65 #include <mach/kern_return.h>
66 #include <mach/memory_object_control.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_protos.h>
70 #include <vm/vm_shared_memory_server.h>
73 #include <sys/ktrace.h>
76 #define f_flag f_fglob->fg_flag
77 #define f_type f_fglob->fg_type
78 #define f_msgcount f_fglob->fg_msgcount
79 #define f_cred f_fglob->fg_cred
80 #define f_ops f_fglob->fg_ops
81 #define f_offset f_fglob->fg_offset
82 #define f_data f_fglob->fg_data
83 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
87 unsigned int pshm_flags
;
88 unsigned int pshm_usecount
;
93 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
94 void * pshm_memobject
;
96 unsigned int pshm_readcount
;
97 unsigned int pshm_writecount
;
98 struct proc
* pshm_proc
;
99 #endif /* DIAGNOSTIC */
101 #define PSHMINFO_NULL (struct pshminfo *)0
104 #define PSHM_DEFINED 2
105 #define PSHM_ALLOCATED 4
106 #define PSHM_MAPPED 8
107 #define PSHM_INUSE 0x10
108 #define PSHM_REMOVED 0x20
109 #define PSHM_INCREATE 0x40
110 #define PSHM_INDELETE 0x80
113 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
114 struct pshminfo
*pshminfo
; /* vnode the name refers to */
115 int pshm_nlen
; /* length of name */
116 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
118 #define PSHMCACHE_NULL (struct pshmcache *)0
121 long goodhits
; /* hits that we can really use */
122 long neghits
; /* negative hits that we can use */
123 long badhits
; /* hits we must drop */
124 long falsehits
; /* hits with id mismatch */
125 long miss
; /* misses */
126 long longnames
; /* long names that ignore cache */
130 char *pshm_nameptr
; /* pointer to looked up name */
131 long pshm_namelen
; /* length of looked up component */
132 u_long pshm_hash
; /* hash value of looked up name */
137 user_size_t map_size
;
138 struct pshminfo
*pinfo
;
139 unsigned int pshm_usecount
;
141 unsigned int readcnt
;
142 unsigned int writecnt
;
145 #define PSHMNODE_NULL (struct pshmnode *)0
148 #define PSHMHASH(pnp) \
149 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
151 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
152 u_long pshmhash
; /* size of hash table - 1 */
153 long pshmnument
; /* number of cache entries allocated */
154 struct pshmstats pshmstats
; /* cache effectiveness statistics */
156 static int pshm_read (struct fileproc
*fp
, struct uio
*uio
,
157 kauth_cred_t cred
, int flags
, struct proc
*p
);
158 static int pshm_write (struct fileproc
*fp
, struct uio
*uio
,
159 kauth_cred_t cred
, int flags
, struct proc
*p
);
160 static int pshm_ioctl (struct fileproc
*fp
, u_long com
,
161 caddr_t data
, struct proc
*p
);
162 static int pshm_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
163 static int pshm_close(struct pshmnode
*pnode
);
164 static int pshm_closefile (struct fileglob
*fg
, struct proc
*p
);
166 static int pshm_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
168 int pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, struct proc
*p
);
169 static int pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
);
170 static void pshm_cache_delete(struct pshmcache
*pcp
);
172 static void pshm_cache_purge(void);
173 #endif /* NOT_USED */
174 static int pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
175 struct pshmcache
**pcache
);
177 struct fileops pshmops
=
178 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
, 0 };
180 static lck_grp_t
*psx_shm_subsys_lck_grp
;
181 static lck_grp_attr_t
*psx_shm_subsys_lck_grp_attr
;
182 static lck_attr_t
*psx_shm_subsys_lck_attr
;
183 static lck_mtx_t psx_shm_subsys_mutex
;
185 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
186 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
189 /* Initialize the mutex governing access to the posix shm subsystem */
190 __private_extern__
void
191 pshm_lock_init( void )
194 psx_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
196 psx_shm_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr
);
198 psx_shm_subsys_lck_attr
= lck_attr_alloc_init();
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
;
482 bcopy(pnbuf
, &pinfo
->pshm_name
[0], PSHMNAMLEN
);
483 pinfo
->pshm_name
[PSHMNAMLEN
]=0;
486 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
487 PSHM_SUBSYS_UNLOCK();
491 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
492 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
493 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
494 PSHM_SUBSYS_UNLOCK();
500 /* O_CREAT is not set and the shm obecj does not exist */
501 PSHM_SUBSYS_UNLOCK();
505 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
506 PSHM_SUBSYS_UNLOCK();
510 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
511 PSHM_SUBSYS_UNLOCK();
515 if (fmode
& O_TRUNC
) {
516 PSHM_SUBSYS_UNLOCK();
522 pinfo
->pshm_writecount
++;
524 pinfo
->pshm_readcount
++;
526 PSHM_SUBSYS_UNLOCK();
527 MALLOC(pnode
, struct pshmnode
*, sizeof(struct pshmnode
), M_SHM
, M_WAITOK
|M_ZERO
);
534 * We allocate a new entry if we are less than the maximum
535 * allowed and the one at the front of the LRU list is in use.
536 * Otherwise we use the one at the front of the LRU list.
538 MALLOC(pcp
, struct pshmcache
*, sizeof(struct pshmcache
), M_SHM
, M_WAITOK
|M_ZERO
);
548 if ( (error
= pshm_cache_add(pinfo
, &nd
, pcp
)) ) {
549 PSHM_SUBSYS_UNLOCK();
554 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
555 pinfo
->pshm_usecount
++; /* extra reference for the new fd */
556 pnode
->pinfo
= pinfo
;
558 PSHM_SUBSYS_UNLOCK();
560 fp
->f_flag
= fmode
& FMASK
;
561 fp
->f_type
= DTYPE_PSXSHM
;
562 fp
->f_ops
= &pshmops
;
563 fp
->f_data
= (caddr_t
)pnode
;
564 *fdflags(p
, indx
) &= ~UF_RESERVED
;
565 fp_drop(p
, indx
, fp
, 1);
569 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
578 fp_free(p
, indx
, fp
);
580 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
586 pshm_truncate(__unused
struct proc
*p
, struct fileproc
*fp
, __unused
int fd
,
587 off_t length
, __unused register_t
*retval
)
589 struct pshminfo
* pinfo
;
590 struct pshmnode
* pnode
;
592 mach_vm_offset_t user_addr
;
593 mem_entry_name_port_t mem_object
;
596 if (fp
->f_type
!= DTYPE_PSXSHM
) {
601 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
605 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
606 PSHM_SUBSYS_UNLOCK();
609 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
611 PSHM_SUBSYS_UNLOCK();
615 PSHM_SUBSYS_UNLOCK();
616 size
= round_page_64(length
);
617 kret
= mach_vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
618 if (kret
!= KERN_SUCCESS
)
621 kret
= mach_make_memory_entry_64 (current_map(), &size
,
622 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
624 if (kret
!= KERN_SUCCESS
)
627 mach_vm_deallocate(current_map(), user_addr
, size
);
630 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
631 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
632 pinfo
->pshm_memobject
= (void *)mem_object
;
633 pinfo
->pshm_length
= size
;
634 PSHM_SUBSYS_UNLOCK();
639 case KERN_INVALID_ADDRESS
:
642 case KERN_PROTECTION_FAILURE
:
651 pshm_stat(struct pshmnode
*pnode
, struct stat
*sb
)
653 struct pshminfo
*pinfo
;
656 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
){
657 PSHM_SUBSYS_UNLOCK();
661 bzero(sb
, sizeof(struct stat
));
662 sb
->st_mode
= pinfo
->pshm_mode
;
663 sb
->st_uid
= pinfo
->pshm_uid
;
664 sb
->st_gid
= pinfo
->pshm_gid
;
665 sb
->st_size
= pinfo
->pshm_length
;
666 PSHM_SUBSYS_UNLOCK();
672 * This is called only from shm_open which holds pshm_lock();
673 * XXX This code is repeated many times
676 pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, __unused
struct proc
*p
)
681 /* Otherwise, user id 0 always gets access. */
682 if (!suser(cred
, NULL
))
687 /* Otherwise, check the owner. */
688 if (kauth_cred_getuid(cred
) == pinfo
->pshm_uid
) {
693 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
696 /* Otherwise, check the groups. */
697 if (kauth_cred_ismember_gid(cred
, pinfo
->pshm_gid
, &is_member
) == 0 && is_member
) {
702 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
705 /* Otherwise, check everyone else. */
710 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
714 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
, struct fileproc
*fp
, off_t pageoff
)
716 mach_vm_offset_t user_addr
= (mach_vm_offset_t
)uap
->addr
;
717 mach_vm_size_t user_size
= (mach_vm_size_t
)uap
->len
;
718 int prot
= uap
->prot
;
719 int flags
= uap
->flags
;
720 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
726 struct pshminfo
* pinfo
;
727 struct pshmnode
* pnode
;
733 if ((flags
& MAP_SHARED
) == 0)
737 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
741 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
745 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
746 PSHM_SUBSYS_UNLOCK();
750 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
751 PSHM_SUBSYS_UNLOCK();
754 if ((off_t
)user_size
> pinfo
->pshm_length
) {
755 PSHM_SUBSYS_UNLOCK();
758 if ((off_t
)(user_size
+ file_pos
) > pinfo
->pshm_length
) {
759 PSHM_SUBSYS_UNLOCK();
762 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
763 PSHM_SUBSYS_UNLOCK();
768 PSHM_SUBSYS_UNLOCK();
769 user_map
= current_map();
771 if ((flags
& MAP_FIXED
) == 0) {
772 alloc_flags
= VM_FLAGS_ANYWHERE
;
773 user_addr
= mach_vm_round_page(user_addr
);
775 if (user_addr
!= mach_vm_trunc_page(user_addr
))
778 * We do not get rid of the existing mappings here because
779 * it wouldn't be atomic (see comment in mmap()). We let
780 * Mach VM know that we want it to replace any existing
781 * mapping with the new one.
783 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
787 kret
= mach_vm_map(user_map
, &user_addr
, user_size
,
788 0, alloc_flags
, pinfo
->pshm_memobject
, file_pos
, docow
,
789 prot
, VM_PROT_DEFAULT
,
791 if (kret
!= KERN_SUCCESS
)
793 /* LP64todo - this should be superfluous at this point */
794 kret
= mach_vm_inherit(user_map
, user_addr
, user_size
,
796 if (kret
!= KERN_SUCCESS
) {
797 (void) mach_vm_deallocate(user_map
, user_addr
, user_size
);
801 pnode
->mapp_addr
= user_addr
;
802 pnode
->map_size
= user_size
;
803 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
804 PSHM_SUBSYS_UNLOCK();
808 *retval
= (user_addr
+ pageoff
);
810 case KERN_INVALID_ADDRESS
:
813 case KERN_PROTECTION_FAILURE
:
822 shm_unlink(__unused
struct proc
*p
, struct shm_unlink_args
*uap
,
823 __unused register_t
*retval
)
828 struct pshminfo
*pinfo
;
832 size_t pathlen
, plen
;
834 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
836 pinfo
= PSHMINFO_NULL
;
838 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
840 return(ENOSPC
); /* XXX non-standard */
842 pathlen
= MAXPATHLEN
;
843 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
847 AUDIT_ARG(text
, pnbuf
);
848 if (pathlen
> PSHMNAMLEN
) {
849 error
= ENAMETOOLONG
;
854 #ifdef PSXSHM_NAME_RESTRICT
856 if (*nameptr
== '/') {
857 while (*(nameptr
++) == '/') {
866 #endif /* PSXSHM_NAME_RESTRICT */
870 nd
.pshm_nameptr
= nameptr
;
871 nd
.pshm_namelen
= plen
;
874 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
875 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
879 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
881 if (error
== ENOENT
) {
882 PSHM_SUBSYS_UNLOCK();
888 PSHM_SUBSYS_UNLOCK();
894 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
895 PSHM_SUBSYS_UNLOCK();
899 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
900 PSHM_SUBSYS_UNLOCK();
905 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
, pinfo
->pshm_gid
,
909 * JMM - How should permissions be checked?
912 pinfo
->pshm_flags
|= PSHM_INDELETE
;
913 pshm_cache_delete(pcache
);
914 pinfo
->pshm_flags
|= PSHM_REMOVED
;
915 /* release the existence reference */
916 if (!--pinfo
->pshm_usecount
) {
917 PSHM_SUBSYS_UNLOCK();
919 * If this is the last reference going away on the object,
920 * then we need to destroy the backing object. The name
921 * has an implied but uncounted reference on the object,
922 * once it's created, since it's used as a rendesvous, and
923 * therefore may be subsequently reopened.
925 if (pinfo
->pshm_memobject
!= NULL
)
926 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
930 PSHM_SUBSYS_UNLOCK();
934 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
938 /* already called locked */
940 pshm_close(struct pshmnode
*pnode
)
943 struct pshminfo
*pinfo
;
945 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
948 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
952 if(!pinfo
->pshm_usecount
) {
953 kprintf("negative usecount in pshm_close\n");
955 #endif /* DIAGNOSTIC */
956 pinfo
->pshm_usecount
--; /* release this fd's reference */
958 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
959 PSHM_SUBSYS_UNLOCK();
961 * If this is the last reference going away on the object,
962 * then we need to destroy the backing object.
964 if (pinfo
->pshm_memobject
!= NULL
)
965 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
973 /* struct proc passed to match prototype for struct fileops */
975 pshm_closefile(struct fileglob
*fg
, __unused
struct proc
*p
)
980 error
= pshm_close(((struct pshmnode
*)fg
->fg_data
));
981 PSHM_SUBSYS_UNLOCK();
986 pshm_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
987 __unused kauth_cred_t cred
, __unused
int flags
,
988 __unused
struct proc
*p
)
994 pshm_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
995 __unused kauth_cred_t cred
, __unused
int flags
,
996 __unused
struct proc
*p
)
1002 pshm_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1003 __unused caddr_t data
, __unused
struct proc
*p
)
1009 pshm_select(__unused
struct fileproc
*fp
, __unused
int which
, __unused
void *wql
,
1010 __unused
struct proc
*p
)
1016 pshm_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1017 __unused
struct proc
*p
)
1023 fill_pshminfo(struct pshmnode
* pshm
, struct pshm_info
* info
)
1025 struct pshminfo
*pinfo
;
1029 if ((pinfo
= pshm
->pinfo
) == PSHMINFO_NULL
){
1030 PSHM_SUBSYS_UNLOCK();
1034 sb
= &info
->pshm_stat
;
1036 bzero(sb
, sizeof(struct stat
));
1037 sb
->st_mode
= pinfo
->pshm_mode
;
1038 sb
->st_uid
= pinfo
->pshm_uid
;
1039 sb
->st_gid
= pinfo
->pshm_gid
;
1040 sb
->st_size
= pinfo
->pshm_length
;
1042 info
->pshm_mappaddr
= pshm
->mapp_addr
;
1043 bcopy(&pinfo
->pshm_name
[0], &info
->pshm_name
[0], PSHMNAMLEN
+1);
1045 PSHM_SUBSYS_UNLOCK();