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>
62 #include <sys/proc_info.h>
64 #include <bsm/audit_kernel.h>
66 #include <mach/mach_types.h>
67 #include <mach/mach_vm.h>
68 #include <mach/vm_map.h>
69 #include <mach/vm_prot.h>
70 #include <mach/vm_inherit.h>
71 #include <mach/kern_return.h>
72 #include <mach/memory_object_control.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_protos.h>
76 #include <vm/vm_shared_memory_server.h>
79 #include <sys/ktrace.h>
82 #define f_flag f_fglob->fg_flag
83 #define f_type f_fglob->fg_type
84 #define f_msgcount f_fglob->fg_msgcount
85 #define f_cred f_fglob->fg_cred
86 #define f_ops f_fglob->fg_ops
87 #define f_offset f_fglob->fg_offset
88 #define f_data f_fglob->fg_data
89 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
93 unsigned int pshm_flags
;
94 unsigned int pshm_usecount
;
99 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
100 void * pshm_memobject
;
102 unsigned int pshm_readcount
;
103 unsigned int pshm_writecount
;
104 struct proc
* pshm_proc
;
105 #endif /* DIAGNOSTIC */
107 #define PSHMINFO_NULL (struct pshminfo *)0
110 #define PSHM_DEFINED 2
111 #define PSHM_ALLOCATED 4
112 #define PSHM_MAPPED 8
113 #define PSHM_INUSE 0x10
114 #define PSHM_REMOVED 0x20
115 #define PSHM_INCREATE 0x40
116 #define PSHM_INDELETE 0x80
119 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
120 struct pshminfo
*pshminfo
; /* vnode the name refers to */
121 int pshm_nlen
; /* length of name */
122 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
124 #define PSHMCACHE_NULL (struct pshmcache *)0
127 long goodhits
; /* hits that we can really use */
128 long neghits
; /* negative hits that we can use */
129 long badhits
; /* hits we must drop */
130 long falsehits
; /* hits with id mismatch */
131 long miss
; /* misses */
132 long longnames
; /* long names that ignore cache */
136 char *pshm_nameptr
; /* pointer to looked up name */
137 long pshm_namelen
; /* length of looked up component */
138 u_long pshm_hash
; /* hash value of looked up name */
143 user_size_t map_size
;
144 struct pshminfo
*pinfo
;
145 unsigned int pshm_usecount
;
147 unsigned int readcnt
;
148 unsigned int writecnt
;
151 #define PSHMNODE_NULL (struct pshmnode *)0
154 #define PSHMHASH(pnp) \
155 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
157 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
158 u_long pshmhash
; /* size of hash table - 1 */
159 long pshmnument
; /* number of cache entries allocated */
160 struct pshmstats pshmstats
; /* cache effectiveness statistics */
162 static int pshm_read (struct fileproc
*fp
, struct uio
*uio
,
163 kauth_cred_t cred
, int flags
, struct proc
*p
);
164 static int pshm_write (struct fileproc
*fp
, struct uio
*uio
,
165 kauth_cred_t cred
, int flags
, struct proc
*p
);
166 static int pshm_ioctl (struct fileproc
*fp
, u_long com
,
167 caddr_t data
, struct proc
*p
);
168 static int pshm_select (struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
);
169 static int pshm_close(struct pshmnode
*pnode
);
170 static int pshm_closefile (struct fileglob
*fg
, struct proc
*p
);
172 static int pshm_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
174 int pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, struct proc
*p
);
175 static int pshm_cache_add(struct pshminfo
*pshmp
, struct pshmname
*pnp
, struct pshmcache
*pcp
);
176 static void pshm_cache_delete(struct pshmcache
*pcp
);
178 static void pshm_cache_purge(void);
179 #endif /* NOT_USED */
180 static int pshm_cache_search(struct pshminfo
**pshmp
, struct pshmname
*pnp
,
181 struct pshmcache
**pcache
);
183 struct fileops pshmops
=
184 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
, 0 };
186 static lck_grp_t
*psx_shm_subsys_lck_grp
;
187 static lck_grp_attr_t
*psx_shm_subsys_lck_grp_attr
;
188 static lck_attr_t
*psx_shm_subsys_lck_attr
;
189 static lck_mtx_t psx_shm_subsys_mutex
;
191 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
192 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
195 /* Initialize the mutex governing access to the posix shm subsystem */
196 __private_extern__
void
197 pshm_lock_init( void )
200 psx_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
202 psx_shm_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr
);
204 psx_shm_subsys_lck_attr
= lck_attr_alloc_init();
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
;
488 bcopy(pnbuf
, &pinfo
->pshm_name
[0], PSHMNAMLEN
);
489 pinfo
->pshm_name
[PSHMNAMLEN
]=0;
492 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
493 PSHM_SUBSYS_UNLOCK();
497 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
,
498 pinfo
->pshm_gid
, pinfo
->pshm_mode
);
499 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
500 PSHM_SUBSYS_UNLOCK();
506 /* O_CREAT is not set and the shm obecj does not exist */
507 PSHM_SUBSYS_UNLOCK();
511 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
512 PSHM_SUBSYS_UNLOCK();
516 if ( (error
= pshm_access(pinfo
, fmode
, kauth_cred_get(), p
)) ) {
517 PSHM_SUBSYS_UNLOCK();
521 if (fmode
& O_TRUNC
) {
522 PSHM_SUBSYS_UNLOCK();
528 pinfo
->pshm_writecount
++;
530 pinfo
->pshm_readcount
++;
532 PSHM_SUBSYS_UNLOCK();
533 MALLOC(pnode
, struct pshmnode
*, sizeof(struct pshmnode
), M_SHM
, M_WAITOK
|M_ZERO
);
540 * We allocate a new entry if we are less than the maximum
541 * allowed and the one at the front of the LRU list is in use.
542 * Otherwise we use the one at the front of the LRU list.
544 MALLOC(pcp
, struct pshmcache
*, sizeof(struct pshmcache
), M_SHM
, M_WAITOK
|M_ZERO
);
554 if ( (error
= pshm_cache_add(pinfo
, &nd
, pcp
)) ) {
555 PSHM_SUBSYS_UNLOCK();
560 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
561 pinfo
->pshm_usecount
++; /* extra reference for the new fd */
562 pnode
->pinfo
= pinfo
;
564 PSHM_SUBSYS_UNLOCK();
566 fp
->f_flag
= fmode
& FMASK
;
567 fp
->f_type
= DTYPE_PSXSHM
;
568 fp
->f_ops
= &pshmops
;
569 fp
->f_data
= (caddr_t
)pnode
;
570 *fdflags(p
, indx
) &= ~UF_RESERVED
;
571 fp_drop(p
, indx
, fp
, 1);
575 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
584 fp_free(p
, indx
, fp
);
586 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
592 pshm_truncate(__unused
struct proc
*p
, struct fileproc
*fp
, __unused
int fd
,
593 off_t length
, __unused register_t
*retval
)
595 struct pshminfo
* pinfo
;
596 struct pshmnode
* pnode
;
598 mach_vm_offset_t user_addr
;
599 mem_entry_name_port_t mem_object
;
602 if (fp
->f_type
!= DTYPE_PSXSHM
) {
607 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
611 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
612 PSHM_SUBSYS_UNLOCK();
615 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
617 PSHM_SUBSYS_UNLOCK();
621 PSHM_SUBSYS_UNLOCK();
622 size
= round_page_64(length
);
623 kret
= mach_vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
624 if (kret
!= KERN_SUCCESS
)
627 kret
= mach_make_memory_entry_64 (current_map(), &size
,
628 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
630 if (kret
!= KERN_SUCCESS
)
633 mach_vm_deallocate(current_map(), user_addr
, size
);
636 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
637 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
638 pinfo
->pshm_memobject
= (void *)mem_object
;
639 pinfo
->pshm_length
= size
;
640 PSHM_SUBSYS_UNLOCK();
645 case KERN_INVALID_ADDRESS
:
648 case KERN_PROTECTION_FAILURE
:
657 pshm_stat(struct pshmnode
*pnode
, struct stat
*sb
)
659 struct pshminfo
*pinfo
;
662 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
){
663 PSHM_SUBSYS_UNLOCK();
667 bzero(sb
, sizeof(struct stat
));
668 sb
->st_mode
= pinfo
->pshm_mode
;
669 sb
->st_uid
= pinfo
->pshm_uid
;
670 sb
->st_gid
= pinfo
->pshm_gid
;
671 sb
->st_size
= pinfo
->pshm_length
;
672 PSHM_SUBSYS_UNLOCK();
678 * This is called only from shm_open which holds pshm_lock();
679 * XXX This code is repeated many times
682 pshm_access(struct pshminfo
*pinfo
, int mode
, kauth_cred_t cred
, __unused
struct proc
*p
)
687 /* Otherwise, user id 0 always gets access. */
688 if (!suser(cred
, NULL
))
693 /* Otherwise, check the owner. */
694 if (kauth_cred_getuid(cred
) == pinfo
->pshm_uid
) {
699 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
702 /* Otherwise, check the groups. */
703 if (kauth_cred_ismember_gid(cred
, pinfo
->pshm_gid
, &is_member
) == 0 && is_member
) {
708 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
711 /* Otherwise, check everyone else. */
716 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
720 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, user_addr_t
*retval
, struct fileproc
*fp
, off_t pageoff
)
722 mach_vm_offset_t user_addr
= (mach_vm_offset_t
)uap
->addr
;
723 mach_vm_size_t user_size
= (mach_vm_size_t
)uap
->len
;
724 int prot
= uap
->prot
;
725 int flags
= uap
->flags
;
726 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
732 struct pshminfo
* pinfo
;
733 struct pshmnode
* pnode
;
739 if ((flags
& MAP_SHARED
) == 0)
743 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
747 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
751 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
) {
752 PSHM_SUBSYS_UNLOCK();
756 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
757 PSHM_SUBSYS_UNLOCK();
760 if ((off_t
)user_size
> pinfo
->pshm_length
) {
761 PSHM_SUBSYS_UNLOCK();
764 if ((off_t
)(user_size
+ file_pos
) > pinfo
->pshm_length
) {
765 PSHM_SUBSYS_UNLOCK();
768 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
769 PSHM_SUBSYS_UNLOCK();
774 PSHM_SUBSYS_UNLOCK();
775 user_map
= current_map();
777 if ((flags
& MAP_FIXED
) == 0) {
778 alloc_flags
= VM_FLAGS_ANYWHERE
;
779 user_addr
= mach_vm_round_page(user_addr
);
781 if (user_addr
!= mach_vm_trunc_page(user_addr
))
784 * We do not get rid of the existing mappings here because
785 * it wouldn't be atomic (see comment in mmap()). We let
786 * Mach VM know that we want it to replace any existing
787 * mapping with the new one.
789 alloc_flags
= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
793 kret
= mach_vm_map(user_map
, &user_addr
, user_size
,
794 0, alloc_flags
, pinfo
->pshm_memobject
, file_pos
, docow
,
795 prot
, VM_PROT_DEFAULT
,
797 if (kret
!= KERN_SUCCESS
)
799 /* LP64todo - this should be superfluous at this point */
800 kret
= mach_vm_inherit(user_map
, user_addr
, user_size
,
802 if (kret
!= KERN_SUCCESS
) {
803 (void) mach_vm_deallocate(user_map
, user_addr
, user_size
);
807 pnode
->mapp_addr
= user_addr
;
808 pnode
->map_size
= user_size
;
809 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
810 PSHM_SUBSYS_UNLOCK();
814 *retval
= (user_addr
+ pageoff
);
816 case KERN_INVALID_ADDRESS
:
819 case KERN_PROTECTION_FAILURE
:
828 shm_unlink(__unused
struct proc
*p
, struct shm_unlink_args
*uap
,
829 __unused register_t
*retval
)
834 struct pshminfo
*pinfo
;
838 size_t pathlen
, plen
;
840 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
842 pinfo
= PSHMINFO_NULL
;
844 MALLOC_ZONE(pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
846 return(ENOSPC
); /* XXX non-standard */
848 pathlen
= MAXPATHLEN
;
849 error
= copyinstr(uap
->name
, (void *)pnbuf
, MAXPATHLEN
, &pathlen
);
853 AUDIT_ARG(text
, pnbuf
);
854 if (pathlen
> PSHMNAMLEN
) {
855 error
= ENAMETOOLONG
;
860 #ifdef PSXSHM_NAME_RESTRICT
862 if (*nameptr
== '/') {
863 while (*(nameptr
++) == '/') {
872 #endif /* PSXSHM_NAME_RESTRICT */
876 nd
.pshm_nameptr
= nameptr
;
877 nd
.pshm_namelen
= plen
;
880 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
881 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
885 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
887 if (error
== ENOENT
) {
888 PSHM_SUBSYS_UNLOCK();
894 PSHM_SUBSYS_UNLOCK();
900 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
901 PSHM_SUBSYS_UNLOCK();
905 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
906 PSHM_SUBSYS_UNLOCK();
911 AUDIT_ARG(posix_ipc_perm
, pinfo
->pshm_uid
, pinfo
->pshm_gid
,
915 * JMM - How should permissions be checked?
918 pinfo
->pshm_flags
|= PSHM_INDELETE
;
919 pshm_cache_delete(pcache
);
920 pinfo
->pshm_flags
|= PSHM_REMOVED
;
921 /* release the existence reference */
922 if (!--pinfo
->pshm_usecount
) {
923 PSHM_SUBSYS_UNLOCK();
925 * If this is the last reference going away on the object,
926 * then we need to destroy the backing object. The name
927 * has an implied but uncounted reference on the object,
928 * once it's created, since it's used as a rendesvous, and
929 * therefore may be subsequently reopened.
931 if (pinfo
->pshm_memobject
!= NULL
)
932 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
936 PSHM_SUBSYS_UNLOCK();
940 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
944 /* already called locked */
946 pshm_close(struct pshmnode
*pnode
)
949 struct pshminfo
*pinfo
;
951 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
954 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
958 if(!pinfo
->pshm_usecount
) {
959 kprintf("negative usecount in pshm_close\n");
961 #endif /* DIAGNOSTIC */
962 pinfo
->pshm_usecount
--; /* release this fd's reference */
964 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
965 PSHM_SUBSYS_UNLOCK();
967 * If this is the last reference going away on the object,
968 * then we need to destroy the backing object.
970 if (pinfo
->pshm_memobject
!= NULL
)
971 mach_memory_entry_port_release(pinfo
->pshm_memobject
);
979 /* struct proc passed to match prototype for struct fileops */
981 pshm_closefile(struct fileglob
*fg
, __unused
struct proc
*p
)
986 error
= pshm_close(((struct pshmnode
*)fg
->fg_data
));
987 PSHM_SUBSYS_UNLOCK();
992 pshm_read(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
993 __unused kauth_cred_t cred
, __unused
int flags
,
994 __unused
struct proc
*p
)
1000 pshm_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1001 __unused kauth_cred_t cred
, __unused
int flags
,
1002 __unused
struct proc
*p
)
1008 pshm_ioctl(__unused
struct fileproc
*fp
, __unused u_long com
,
1009 __unused caddr_t data
, __unused
struct proc
*p
)
1015 pshm_select(__unused
struct fileproc
*fp
, __unused
int which
, __unused
void *wql
,
1016 __unused
struct proc
*p
)
1022 pshm_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
,
1023 __unused
struct proc
*p
)
1029 fill_pshminfo(struct pshmnode
* pshm
, struct pshm_info
* info
)
1031 struct pshminfo
*pinfo
;
1035 if ((pinfo
= pshm
->pinfo
) == PSHMINFO_NULL
){
1036 PSHM_SUBSYS_UNLOCK();
1040 sb
= &info
->pshm_stat
;
1042 bzero(sb
, sizeof(struct stat
));
1043 sb
->st_mode
= pinfo
->pshm_mode
;
1044 sb
->st_uid
= pinfo
->pshm_uid
;
1045 sb
->st_gid
= pinfo
->pshm_gid
;
1046 sb
->st_size
= pinfo
->pshm_length
;
1048 info
->pshm_mappaddr
= pshm
->mapp_addr
;
1049 bcopy(&pinfo
->pshm_name
[0], &info
->pshm_name
[0], PSHMNAMLEN
+1);
1051 PSHM_SUBSYS_UNLOCK();