2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
35 * posix_shm.c : Support for POSIX shared memory APIs
38 * Author: Ananthakrishna Ramesh
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/file_internal.h>
51 #include <sys/filedesc.h>
53 #include <sys/proc_internal.h>
54 #include <sys/kauth.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/ioctl.h>
60 #include <sys/malloc.h>
63 #include <sys/sysproto.h>
64 #include <sys/proc_info.h>
66 #include <bsm/audit_kernel.h>
68 #include <mach/mach_types.h>
69 #include <mach/mach_vm.h>
70 #include <mach/vm_map.h>
71 #include <mach/vm_prot.h>
72 #include <mach/vm_inherit.h>
73 #include <mach/kern_return.h>
74 #include <mach/memory_object_control.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_protos.h>
78 #include <vm/vm_shared_memory_server.h>
81 #include <sys/ktrace.h>
84 #define f_flag f_fglob->fg_flag
85 #define f_type f_fglob->fg_type
86 #define f_msgcount f_fglob->fg_msgcount
87 #define f_cred f_fglob->fg_cred
88 #define f_ops f_fglob->fg_ops
89 #define f_offset f_fglob->fg_offset
90 #define f_data f_fglob->fg_data
91 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
95 unsigned int pshm_flags
;
96 unsigned int pshm_usecount
;
101 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
102 void * pshm_memobject
;
104 unsigned int pshm_readcount
;
105 unsigned int pshm_writecount
;
106 struct proc
* pshm_proc
;
107 #endif /* DIAGNOSTIC */
109 #define PSHMINFO_NULL (struct pshminfo *)0
112 #define PSHM_DEFINED 2
113 #define PSHM_ALLOCATED 4
114 #define PSHM_MAPPED 8
115 #define PSHM_INUSE 0x10
116 #define PSHM_REMOVED 0x20
117 #define PSHM_INCREATE 0x40
118 #define PSHM_INDELETE 0x80
121 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
122 struct pshminfo
*pshminfo
; /* vnode the name refers to */
123 int pshm_nlen
; /* length of name */
124 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
126 #define PSHMCACHE_NULL (struct pshmcache *)0
129 long goodhits
; /* hits that we can really use */
130 long neghits
; /* negative hits that we can use */
131 long badhits
; /* hits we must drop */
132 long falsehits
; /* hits with id mismatch */
133 long miss
; /* misses */
134 long longnames
; /* long names that ignore cache */
138 char *pshm_nameptr
; /* pointer to looked up name */
139 long pshm_namelen
; /* length of looked up component */
140 u_long pshm_hash
; /* hash value of looked up name */
145 user_size_t map_size
;
146 struct pshminfo
*pinfo
;
147 unsigned int pshm_usecount
;
149 unsigned int readcnt
;
150 unsigned int writecnt
;
153 #define PSHMNODE_NULL (struct pshmnode *)0
156 #define PSHMHASH(pnp) \
157 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
159 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
160 u_long pshmhash
; /* size of hash table - 1 */
161 long pshmnument
; /* number of cache entries allocated */
162 struct pshmstats pshmstats
; /* cache effectiveness statistics */
164 static int pshm_read (struct fileproc
*fp
, struct uio
*uio
,
165 kauth_cred_t cred
, int flags
, struct proc
*p
);
166 static int pshm_write (struct fileproc
*fp
, struct uio
*uio
,
167 kauth_cred_t cred
, int flags
, struct proc
*p
);
168 static int pshm_ioctl (struct fileproc
*fp
, u_long com
,
169 caddr_t data
, struct proc
*p
);
170 static int pshm_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
171 static int pshm_close(struct pshmnode
*pnode
);
172 static int pshm_closefile (struct fileglob
*fg
, struct proc
*p
);
174 static int pshm_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
176 int pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, struct proc
*p
);
177 static int pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
);
178 static void pshm_cache_delete(struct pshmcache
*pcp
);
180 static void pshm_cache_purge(void);
181 #endif /* NOT_USED */
182 static int pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
183 struct pshmcache
**pcache
);
185 struct fileops pshmops
=
186 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
, 0 };
188 static lck_grp_t
*psx_shm_subsys_lck_grp
;
189 static lck_grp_attr_t
*psx_shm_subsys_lck_grp_attr
;
190 static lck_attr_t
*psx_shm_subsys_lck_attr
;
191 static lck_mtx_t psx_shm_subsys_mutex
;
193 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
194 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
197 /* Initialize the mutex governing access to the posix shm subsystem */
198 __private_extern__
void
199 pshm_lock_init( void )
202 psx_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
204 psx_shm_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr
);
206 psx_shm_subsys_lck_attr
= lck_attr_alloc_init();
207 lck_mtx_init(& psx_shm_subsys_mutex
, psx_shm_subsys_lck_grp
, psx_shm_subsys_lck_attr
);
211 * Lookup an entry in the cache
214 * status of -1 is returned if matches
215 * If the lookup determines that the name does not exist
216 * (negative cacheing), a status of ENOENT is returned. If the lookup
217 * fails, a status of zero is returned.
221 pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
222 struct pshmcache
**pcache
)
224 struct pshmcache
*pcp
, *nnp
;
225 struct pshmhashhead
*pcpp
;
227 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
228 pshmstats
.longnames
++;
232 pcpp
= PSHMHASH(pnp
);
233 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
234 nnp
= pcp
->pshm_hash
.le_next
;
235 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
236 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
245 /* We found a "positive" match, return the vnode */
247 pshmstats
.goodhits
++;
249 *pshmp
= pcp
->pshminfo
;
255 * We found a "negative" match, ENOENT notifies client of this match.
256 * The nc_vpid field records whether this is a whiteout.
263 * Add an entry to the cache.
264 * XXX should be static?
267 pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
)
269 struct pshmhashhead
*pcpp
;
270 struct pshminfo
*dpinfo
;
271 struct pshmcache
*dpcp
;
274 if (pnp
->pshm_namelen
> NCHNAMLEN
)
275 panic("cache_enter: name too long");
279 /* if the entry has already been added by some one else return */
280 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
286 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
287 * For negative entries, we have to record whether it is a whiteout.
288 * the whiteout flag is stored in the nc_vpid field which is
291 pcp
->pshminfo
= pshmp
;
292 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
293 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
294 pcpp
= PSHMHASH(pnp
);
299 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
301 panic("cache_enter: duplicate");
304 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
309 * Name cache initialization, from vfs_init() when we are booting
312 pshm_cache_init(void)
314 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
319 * Invalidate a all entries to particular vnode.
321 * We actually just increment the v_id, that will do it. The entries will
322 * be purged by lookup as they get found. If the v_id wraps around, we
323 * need to ditch the entire cache, to avoid confusion. No valid vnode will
324 * ever have (v_id == 0).
327 pshm_cache_purge(void)
329 struct pshmcache
*pcp
;
330 struct pshmhashhead
*pcpp
;
332 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
333 while ( (pcp
= pcpp
->lh_first
) )
334 pshm_cache_delete(pcp
);
337 #endif /* NOT_USED */
340 pshm_cache_delete(struct pshmcache
*pcp
)
343 if (pcp
->pshm_hash
.le_prev
== 0)
344 panic("namecache purge le_prev");
345 if (pcp
->pshm_hash
.le_next
== pcp
)
346 panic("namecache purge le_next");
347 #endif /* DIAGNOSTIC */
348 LIST_REMOVE(pcp
, pshm_hash
);
349 pcp
->pshm_hash
.le_prev
= 0;
355 shm_open(struct proc
*p
, struct shm_open_args
*uap
, register_t
*retval
)
359 struct fileproc
*nfp
;
362 struct pshminfo
*pinfo
;
366 size_t pathlen
, plen
;
368 int cmode
= uap
->mode
;
370 struct pshmnode
* pnode
= PSHMNODE_NULL
;
371 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
372 struct pshmcache
*pcp
;
375 AUDIT_ARG(fflags
, uap
->oflag
);
376 AUDIT_ARG(mode
, uap
->mode
);
378 pinfo
= PSHMINFO_NULL
;
380 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
385 pathlen
= MAXPATHLEN
;
386 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
390 AUDIT_ARG(text
, pnbuf
);
391 if (pathlen
> PSHMNAMLEN
) {
392 error
= ENAMETOOLONG
;
397 #ifdef PSXSHM_NAME_RESTRICT
399 if (*nameptr
== '/') {
400 while (*(nameptr
++) == '/') {
409 #endif /* PSXSHM_NAME_RESTRICT */
413 nd
.pshm_nameptr
= nameptr
;
414 nd
.pshm_namelen
= plen
;
417 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
418 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
422 if (KTRPOINT(p
, KTR_NAMEI
))
423 ktrnamei(p
->p_tracep
, nameptr
);
427 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
429 if (error
== ENOENT
) {
430 PSHM_SUBSYS_UNLOCK();
439 fmode
= FFLAGS(uap
->oflag
);
440 if ((fmode
& (FREAD
| FWRITE
))==0) {
441 PSHM_SUBSYS_UNLOCK();
447 * XXXXXXXXXX TBD XXXXXXXXXX
448 * There is a race that existed with the funnels as well.
449 * Need to be fixed later
451 PSHM_SUBSYS_UNLOCK();
452 error
= falloc(p
, &nfp
, &indx
);
461 if (fmode
& O_CREAT
) {
462 if ((fmode
& O_EXCL
) && incache
) {
463 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
464 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
466 /* shm obj exists and opened O_EXCL */
468 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
472 PSHM_SUBSYS_UNLOCK();
476 PSHM_SUBSYS_UNLOCK();
477 /* create a new one */
478 MALLOC(pinfo
, struct pshminfo
*, sizeof(struct pshminfo
), M_SHM
, M_WAITOK
|M_ZERO
);
485 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
486 pinfo
->pshm_usecount
= 1; /* existence reference */
487 pinfo
->pshm_mode
= cmode
;
488 pinfo
->pshm_uid
= kauth_cred_getuid(kauth_cred_get());
489 pinfo
->pshm_gid
= kauth_cred_get()->cr_gid
;
490 bcopy(pnbuf
, &pinfo
->pshm_name
[0], PSHMNAMLEN
);
491 pinfo
->pshm_name
[PSHMNAMLEN
]=0;
494 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
495 PSHM_SUBSYS_UNLOCK();
499 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
500 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
501 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
502 PSHM_SUBSYS_UNLOCK();
508 /* O_CREAT is not set and the shm obecj does not exist */
509 PSHM_SUBSYS_UNLOCK();
513 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
514 PSHM_SUBSYS_UNLOCK();
518 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
519 PSHM_SUBSYS_UNLOCK();
523 if (fmode
& O_TRUNC
) {
524 PSHM_SUBSYS_UNLOCK();
530 pinfo
->pshm_writecount
++;
532 pinfo
->pshm_readcount
++;
534 PSHM_SUBSYS_UNLOCK();
535 MALLOC(pnode
, struct pshmnode
*, sizeof(struct pshmnode
), M_SHM
, M_WAITOK
|M_ZERO
);
542 * We allocate a new entry if we are less than the maximum
543 * allowed and the one at the front of the LRU list is in use.
544 * Otherwise we use the one at the front of the LRU list.
546 MALLOC(pcp
, struct pshmcache
*, sizeof(struct pshmcache
), M_SHM
, M_WAITOK
|M_ZERO
);
556 if ( (error
= pshm_cache_add(pinfo
, &nd
, pcp
)) ) {
557 PSHM_SUBSYS_UNLOCK();
562 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
563 pinfo
->pshm_usecount
++; /* extra reference for the new fd */
564 pnode
->pinfo
= pinfo
;
566 PSHM_SUBSYS_UNLOCK();
568 fp
->f_flag
= fmode
& FMASK
;
569 fp
->f_type
= DTYPE_PSXSHM
;
570 fp
->f_ops
= &pshmops
;
571 fp
->f_data
= (caddr_t
)pnode
;
572 *fdflags(p
, indx
) &= ~UF_RESERVED
;
573 fp_drop(p
, indx
, fp
, 1);
577 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
586 fp_free(p
, indx
, fp
);
588 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
594 pshm_truncate(__unused
struct proc
*p
, struct fileproc
*fp
, __unused
int fd
,
595 off_t length
, __unused register_t
*retval
)
597 struct pshminfo
* pinfo
;
598 struct pshmnode
* pnode
;
600 mach_vm_offset_t user_addr
;
601 mem_entry_name_port_t mem_object
;
604 if (fp
->f_type
!= DTYPE_PSXSHM
) {
609 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
613 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
614 PSHM_SUBSYS_UNLOCK();
617 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
619 PSHM_SUBSYS_UNLOCK();
623 PSHM_SUBSYS_UNLOCK();
624 size
= round_page_64(length
);
625 kret
= mach_vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
626 if (kret
!= KERN_SUCCESS
)
629 kret
= mach_make_memory_entry_64 (current_map(), &size
,
630 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
632 if (kret
!= KERN_SUCCESS
)
635 mach_vm_deallocate(current_map(), user_addr
, size
);
638 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
639 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
640 pinfo
->pshm_memobject
= (void *)mem_object
;
641 pinfo
->pshm_length
= size
;
642 PSHM_SUBSYS_UNLOCK();
647 case KERN_INVALID_ADDRESS
:
650 case KERN_PROTECTION_FAILURE
:
659 pshm_stat(struct pshmnode
*pnode
, struct stat
*sb
)
661 struct pshminfo
*pinfo
;
664 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
){
665 PSHM_SUBSYS_UNLOCK();
669 bzero(sb
, sizeof(struct stat
));
670 sb
->st_mode
= pinfo
->pshm_mode
;
671 sb
->st_uid
= pinfo
->pshm_uid
;
672 sb
->st_gid
= pinfo
->pshm_gid
;
673 sb
->st_size
= pinfo
->pshm_length
;
674 PSHM_SUBSYS_UNLOCK();
680 * This is called only from shm_open which holds pshm_lock();
681 * XXX This code is repeated many times
684 pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, __unused
struct proc
*p
)
689 /* Otherwise, user id 0 always gets access. */
690 if (!suser(cred
, NULL
))
695 /* Otherwise, check the owner. */
696 if (kauth_cred_getuid(cred
) == pinfo
->pshm_uid
) {
701 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
704 /* Otherwise, check the groups. */
705 if (kauth_cred_ismember_gid(cred
, pinfo
->pshm_gid
, &is_member
) == 0 && is_member
) {
710 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
713 /* Otherwise, check everyone else. */
718 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
722 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
, struct fileproc
*fp
, off_t pageoff
)
724 mach_vm_offset_t user_addr
= (mach_vm_offset_t
)uap
->addr
;
725 mach_vm_size_t user_size
= (mach_vm_size_t
)uap
->len
;
726 int prot
= uap
->prot
;
727 int flags
= uap
->flags
;
728 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
734 struct pshminfo
* pinfo
;
735 struct pshmnode
* pnode
;
741 if ((flags
& MAP_SHARED
) == 0)
745 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
749 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
753 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
754 PSHM_SUBSYS_UNLOCK();
758 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
759 PSHM_SUBSYS_UNLOCK();
762 if ((off_t
)user_size
> pinfo
->pshm_length
) {
763 PSHM_SUBSYS_UNLOCK();
766 if ((off_t
)(user_size
+ file_pos
) > pinfo
->pshm_length
) {
767 PSHM_SUBSYS_UNLOCK();
770 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
771 PSHM_SUBSYS_UNLOCK();
776 PSHM_SUBSYS_UNLOCK();
777 user_map
= current_map();
779 if ((flags
& MAP_FIXED
) == 0) {
780 alloc_flags
= VM_FLAGS_ANYWHERE
;
781 user_addr
= mach_vm_round_page(user_addr
);
783 if (user_addr
!= mach_vm_trunc_page(user_addr
))
786 * We do not get rid of the existing mappings here because
787 * it wouldn't be atomic (see comment in mmap()). We let
788 * Mach VM know that we want it to replace any existing
789 * mapping with the new one.
791 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
795 kret
= mach_vm_map(user_map
, &user_addr
, user_size
,
796 0, alloc_flags
, pinfo
->pshm_memobject
, file_pos
, docow
,
797 prot
, VM_PROT_DEFAULT
,
799 if (kret
!= KERN_SUCCESS
)
801 /* LP64todo - this should be superfluous at this point */
802 kret
= mach_vm_inherit(user_map
, user_addr
, user_size
,
804 if (kret
!= KERN_SUCCESS
) {
805 (void) mach_vm_deallocate(user_map
, user_addr
, user_size
);
809 pnode
->mapp_addr
= user_addr
;
810 pnode
->map_size
= user_size
;
811 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
812 PSHM_SUBSYS_UNLOCK();
816 *retval
= (user_addr
+ pageoff
);
818 case KERN_INVALID_ADDRESS
:
821 case KERN_PROTECTION_FAILURE
:
830 shm_unlink(__unused
struct proc
*p
, struct shm_unlink_args
*uap
,
831 __unused register_t
*retval
)
836 struct pshminfo
*pinfo
;
840 size_t pathlen
, plen
;
842 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
844 pinfo
= PSHMINFO_NULL
;
846 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
848 return(ENOSPC
); /* XXX non-standard */
850 pathlen
= MAXPATHLEN
;
851 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
855 AUDIT_ARG(text
, pnbuf
);
856 if (pathlen
> PSHMNAMLEN
) {
857 error
= ENAMETOOLONG
;
862 #ifdef PSXSHM_NAME_RESTRICT
864 if (*nameptr
== '/') {
865 while (*(nameptr
++) == '/') {
874 #endif /* PSXSHM_NAME_RESTRICT */
878 nd
.pshm_nameptr
= nameptr
;
879 nd
.pshm_namelen
= plen
;
882 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
883 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
887 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
889 if (error
== ENOENT
) {
890 PSHM_SUBSYS_UNLOCK();
896 PSHM_SUBSYS_UNLOCK();
902 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
903 PSHM_SUBSYS_UNLOCK();
907 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
908 PSHM_SUBSYS_UNLOCK();
913 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
, pinfo
->pshm_gid
,
917 * JMM - How should permissions be checked?
920 pinfo
->pshm_flags
|= PSHM_INDELETE
;
921 pshm_cache_delete(pcache
);
922 pinfo
->pshm_flags
|= PSHM_REMOVED
;
923 /* release the existence reference */
924 if (!--pinfo
->pshm_usecount
) {
925 PSHM_SUBSYS_UNLOCK();
927 * If this is the last reference going away on the object,
928 * then we need to destroy the backing object. The name
929 * has an implied but uncounted reference on the object,
930 * once it's created, since it's used as a rendesvous, and
931 * therefore may be subsequently reopened.
933 if (pinfo
->pshm_memobject
!= NULL
)
934 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
938 PSHM_SUBSYS_UNLOCK();
942 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
946 /* already called locked */
948 pshm_close(struct pshmnode
*pnode
)
951 struct pshminfo
*pinfo
;
953 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
956 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
960 if(!pinfo
->pshm_usecount
) {
961 kprintf("negative usecount in pshm_close\n");
963 #endif /* DIAGNOSTIC */
964 pinfo
->pshm_usecount
--; /* release this fd's reference */
966 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
967 PSHM_SUBSYS_UNLOCK();
969 * If this is the last reference going away on the object,
970 * then we need to destroy the backing object.
972 if (pinfo
->pshm_memobject
!= NULL
)
973 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
981 /* struct proc passed to match prototype for struct fileops */
983 pshm_closefile(struct fileglob
*fg
, __unused
struct proc
*p
)
988 error
= pshm_close(((struct pshmnode
*)fg
->fg_data
));
989 PSHM_SUBSYS_UNLOCK();
994 pshm_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
995 __unused kauth_cred_t cred
, __unused
int flags
,
996 __unused
struct proc
*p
)
1002 pshm_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1003 __unused kauth_cred_t cred
, __unused
int flags
,
1004 __unused
struct proc
*p
)
1010 pshm_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1011 __unused caddr_t data
, __unused
struct proc
*p
)
1017 pshm_select(__unused
struct fileproc
*fp
, __unused
int which
, __unused
void *wql
,
1018 __unused
struct proc
*p
)
1024 pshm_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1025 __unused
struct proc
*p
)
1031 fill_pshminfo(struct pshmnode
* pshm
, struct pshm_info
* info
)
1033 struct pshminfo
*pinfo
;
1037 if ((pinfo
= pshm
->pinfo
) == PSHMINFO_NULL
){
1038 PSHM_SUBSYS_UNLOCK();
1042 sb
= &info
->pshm_stat
;
1044 bzero(sb
, sizeof(struct stat
));
1045 sb
->st_mode
= pinfo
->pshm_mode
;
1046 sb
->st_uid
= pinfo
->pshm_uid
;
1047 sb
->st_gid
= pinfo
->pshm_gid
;
1048 sb
->st_size
= pinfo
->pshm_length
;
1050 info
->pshm_mappaddr
= pshm
->mapp_addr
;
1051 bcopy(&pinfo
->pshm_name
[0], &info
->pshm_name
[0], PSHMNAMLEN
+1);
1053 PSHM_SUBSYS_UNLOCK();