]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/posix_shm.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / bsd / kern / posix_shm.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
31 */
32/*
9bccf70c 33 * posix_shm.c : Support for POSIX shared memory APIs
1c79356b
A
34 *
35 * File: posix_shm.c
36 * Author: Ananthakrishna Ramesh
37 *
38 * HISTORY
39 * 2-Sep-1999 A.Ramesh
40 * Created for MacOSX
41 *
42 */
2d21ac55
A
43/*
44 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
45 * support for mandatory and extensible security protections. This notice
46 * is included in support of clause 2.2 (b) of the Apple Public License,
47 * Version 2.0.
48 */
1c79356b
A
49
50#include <sys/cdefs.h>
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
91447636 54#include <sys/file_internal.h>
1c79356b
A
55#include <sys/filedesc.h>
56#include <sys/stat.h>
91447636
A
57#include <sys/proc_internal.h>
58#include <sys/kauth.h>
1c79356b
A
59#include <sys/mount.h>
60#include <sys/namei.h>
61#include <sys/vnode.h>
2d21ac55 62#include <sys/vnode_internal.h>
1c79356b
A
63#include <sys/ioctl.h>
64#include <sys/tty.h>
65#include <sys/malloc.h>
66#include <sys/mman.h>
91447636
A
67#include <sys/stat.h>
68#include <sys/sysproto.h>
0c530ab8 69#include <sys/proc_info.h>
b0d623f7 70#include <security/audit/audit.h>
e5568f75 71
2d21ac55
A
72#if CONFIG_MACF
73#include <security/mac_framework.h>
74#endif
75
1c79356b 76#include <mach/mach_types.h>
91447636
A
77#include <mach/mach_vm.h>
78#include <mach/vm_map.h>
1c79356b
A
79#include <mach/vm_prot.h>
80#include <mach/vm_inherit.h>
81#include <mach/kern_return.h>
82#include <mach/memory_object_control.h>
83
91447636
A
84#include <vm/vm_map.h>
85#include <vm/vm_protos.h>
1c79356b 86
91447636
A
87#define f_flag f_fglob->fg_flag
88#define f_type f_fglob->fg_type
89#define f_msgcount f_fglob->fg_msgcount
90#define f_cred f_fglob->fg_cred
91#define f_ops f_fglob->fg_ops
92#define f_offset f_fglob->fg_offset
93#define f_data f_fglob->fg_data
1c79356b
A
94#define PSHMNAMLEN 31 /* maximum name segment length we bother with */
95
b0d623f7
A
96struct pshmobj {
97 void * pshmo_memobject;
98 memory_object_size_t pshmo_size;
99 struct pshmobj * pshmo_next;
100};
101
1c79356b
A
102struct pshminfo {
103 unsigned int pshm_flags;
104 unsigned int pshm_usecount;
105 off_t pshm_length;
106 mode_t pshm_mode;
107 uid_t pshm_uid;
108 gid_t pshm_gid;
109 char pshm_name[PSHMNAMLEN + 1]; /* segment name */
b0d623f7 110 struct pshmobj *pshm_memobjects;
1c79356b
A
111#if DIAGNOSTIC
112 unsigned int pshm_readcount;
113 unsigned int pshm_writecount;
2d21ac55 114 proc_t pshm_proc;
1c79356b 115#endif /* DIAGNOSTIC */
2d21ac55 116 struct label* pshm_label;
1c79356b
A
117};
118#define PSHMINFO_NULL (struct pshminfo *)0
119
b0d623f7
A
120#define PSHM_NONE 0x001
121#define PSHM_DEFINED 0x002
122#define PSHM_ALLOCATED 0x004
123#define PSHM_MAPPED 0x008
124#define PSHM_INUSE 0x010
125#define PSHM_REMOVED 0x020
126#define PSHM_INCREATE 0x040
127#define PSHM_INDELETE 0x080
128#define PSHM_ALLOCATING 0x100
1c79356b
A
129
130struct pshmcache {
131 LIST_ENTRY(pshmcache) pshm_hash; /* hash chain */
132 struct pshminfo *pshminfo; /* vnode the name refers to */
133 int pshm_nlen; /* length of name */
134 char pshm_name[PSHMNAMLEN + 1]; /* segment name */
135};
136#define PSHMCACHE_NULL (struct pshmcache *)0
137
138struct pshmstats {
139 long goodhits; /* hits that we can really use */
140 long neghits; /* negative hits that we can use */
141 long badhits; /* hits we must drop */
142 long falsehits; /* hits with id mismatch */
143 long miss; /* misses */
144 long longnames; /* long names that ignore cache */
145};
146
147struct pshmname {
148 char *pshm_nameptr; /* pointer to looked up name */
149 long pshm_namelen; /* length of looked up component */
150 u_long pshm_hash; /* hash value of looked up name */
151};
152
153struct pshmnode {
91447636 154 off_t mapp_addr;
b0d623f7 155 user_size_t map_size; /* XXX unused ? */
1c79356b
A
156 struct pshminfo *pinfo;
157 unsigned int pshm_usecount;
158#if DIAGNOSTIC
159 unsigned int readcnt;
160 unsigned int writecnt;
161#endif
162};
163#define PSHMNODE_NULL (struct pshmnode *)0
164
165
166#define PSHMHASH(pnp) \
167 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
91447636 168
1c79356b
A
169LIST_HEAD(pshmhashhead, pshmcache) *pshmhashtbl; /* Hash Table */
170u_long pshmhash; /* size of hash table - 1 */
171long pshmnument; /* number of cache entries allocated */
172struct pshmstats pshmstats; /* cache effectiveness statistics */
173
91447636 174static int pshm_read (struct fileproc *fp, struct uio *uio,
2d21ac55 175 int flags, vfs_context_t ctx);
91447636 176static int pshm_write (struct fileproc *fp, struct uio *uio,
2d21ac55 177 int flags, vfs_context_t ctx);
91447636 178static int pshm_ioctl (struct fileproc *fp, u_long com,
2d21ac55
A
179 caddr_t data, vfs_context_t ctx);
180static int pshm_select (struct fileproc *fp, int which, void *wql, vfs_context_t ctx);
6d2010ae 181static int pshm_close(struct pshminfo *pinfo, int dropref);
2d21ac55 182static int pshm_closefile (struct fileglob *fg, vfs_context_t ctx);
91447636 183
2d21ac55 184static int pshm_kqfilter(struct fileproc *fp, struct knote *kn, vfs_context_t ctx);
91447636 185
2d21ac55 186int pshm_access(struct pshminfo *pinfo, int mode, kauth_cred_t cred, proc_t p);
91447636
A
187static int pshm_cache_add(struct pshminfo *pshmp, struct pshmname *pnp, struct pshmcache *pcp);
188static void pshm_cache_delete(struct pshmcache *pcp);
189#if NOT_USED
190static void pshm_cache_purge(void);
191#endif /* NOT_USED */
192static int pshm_cache_search(struct pshminfo **pshmp, struct pshmname *pnp,
6d2010ae 193 struct pshmcache **pcache, int addref);
55e303ae 194
1c79356b 195struct fileops pshmops =
91447636
A
196 { pshm_read, pshm_write, pshm_ioctl, pshm_select, pshm_closefile, pshm_kqfilter, 0 };
197
198static lck_grp_t *psx_shm_subsys_lck_grp;
199static lck_grp_attr_t *psx_shm_subsys_lck_grp_attr;
200static lck_attr_t *psx_shm_subsys_lck_attr;
201static lck_mtx_t psx_shm_subsys_mutex;
202
203#define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
204#define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
205
206
207/* Initialize the mutex governing access to the posix shm subsystem */
208__private_extern__ void
209pshm_lock_init( void )
210{
211
212 psx_shm_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
91447636
A
213
214 psx_shm_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr);
215
216 psx_shm_subsys_lck_attr = lck_attr_alloc_init();
91447636
A
217 lck_mtx_init(& psx_shm_subsys_mutex, psx_shm_subsys_lck_grp, psx_shm_subsys_lck_attr);
218}
1c79356b 219
1c79356b
A
220/*
221 * Lookup an entry in the cache
222 *
223 *
224 * status of -1 is returned if matches
225 * If the lookup determines that the name does not exist
226 * (negative cacheing), a status of ENOENT is returned. If the lookup
227 * fails, a status of zero is returned.
228 */
229
91447636
A
230static int
231pshm_cache_search(struct pshminfo **pshmp, struct pshmname *pnp,
6d2010ae 232 struct pshmcache **pcache, int addref)
1c79356b 233{
91447636
A
234 struct pshmcache *pcp, *nnp;
235 struct pshmhashhead *pcpp;
1c79356b
A
236
237 if (pnp->pshm_namelen > PSHMNAMLEN) {
238 pshmstats.longnames++;
239 return (0);
240 }
241
242 pcpp = PSHMHASH(pnp);
243 for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) {
244 nnp = pcp->pshm_hash.le_next;
245 if (pcp->pshm_nlen == pnp->pshm_namelen &&
246 !bcmp(pcp->pshm_name, pnp->pshm_nameptr, (u_int)pcp-> pshm_nlen))
247 break;
248 }
249
250 if (pcp == 0) {
251 pshmstats.miss++;
252 return (0);
253 }
254
255 /* We found a "positive" match, return the vnode */
256 if (pcp->pshminfo) {
257 pshmstats.goodhits++;
258 /* TOUCH(ncp); */
259 *pshmp = pcp->pshminfo;
260 *pcache = pcp;
6d2010ae
A
261 if (addref)
262 pcp->pshminfo->pshm_usecount++;
1c79356b
A
263 return (-1);
264 }
265
266 /*
267 * We found a "negative" match, ENOENT notifies client of this match.
268 * The nc_vpid field records whether this is a whiteout.
269 */
270 pshmstats.neghits++;
271 return (ENOENT);
272}
273
274/*
275 * Add an entry to the cache.
91447636 276 * XXX should be static?
1c79356b 277 */
91447636
A
278static int
279pshm_cache_add(struct pshminfo *pshmp, struct pshmname *pnp, struct pshmcache *pcp)
1c79356b 280{
91447636 281 struct pshmhashhead *pcpp;
55e303ae
A
282 struct pshminfo *dpinfo;
283 struct pshmcache *dpcp;
1c79356b
A
284
285#if DIAGNOSTIC
2d21ac55 286 if (pnp->pshm_namelen > PSHMNAMLEN)
1c79356b
A
287 panic("cache_enter: name too long");
288#endif
289
91447636 290
1c79356b 291 /* if the entry has already been added by some one else return */
6d2010ae 292 if (pshm_cache_search(&dpinfo, pnp, &dpcp, 0) == -1) {
1c79356b
A
293 return(EEXIST);
294 }
295 pshmnument++;
296
1c79356b
A
297 /*
298 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
299 * For negative entries, we have to record whether it is a whiteout.
300 * the whiteout flag is stored in the nc_vpid field which is
301 * otherwise unused.
302 */
303 pcp->pshminfo = pshmp;
304 pcp->pshm_nlen = pnp->pshm_namelen;
305 bcopy(pnp->pshm_nameptr, pcp->pshm_name, (unsigned)pcp->pshm_nlen);
306 pcpp = PSHMHASH(pnp);
307#if DIAGNOSTIC
308 {
91447636 309 struct pshmcache *p;
1c79356b
A
310
311 for (p = pcpp->lh_first; p != 0; p = p->pshm_hash.le_next)
312 if (p == pcp)
313 panic("cache_enter: duplicate");
314 }
315#endif
316 LIST_INSERT_HEAD(pcpp, pcp, pshm_hash);
317 return(0);
318}
319
320/*
321 * Name cache initialization, from vfs_init() when we are booting
322 */
323void
91447636 324pshm_cache_init(void)
1c79356b 325{
2d21ac55 326 pshmhashtbl = hashinit(desiredvnodes / 8, M_SHM, &pshmhash);
1c79356b
A
327}
328
91447636 329#if NOT_USED
1c79356b
A
330/*
331 * Invalidate a all entries to particular vnode.
332 *
333 * We actually just increment the v_id, that will do it. The entries will
334 * be purged by lookup as they get found. If the v_id wraps around, we
335 * need to ditch the entire cache, to avoid confusion. No valid vnode will
336 * ever have (v_id == 0).
337 */
91447636 338static void
1c79356b
A
339pshm_cache_purge(void)
340{
341 struct pshmcache *pcp;
342 struct pshmhashhead *pcpp;
343
344 for (pcpp = &pshmhashtbl[pshmhash]; pcpp >= pshmhashtbl; pcpp--) {
91447636 345 while ( (pcp = pcpp->lh_first) )
1c79356b
A
346 pshm_cache_delete(pcp);
347 }
348}
91447636 349#endif /* NOT_USED */
1c79356b 350
91447636
A
351static void
352pshm_cache_delete(struct pshmcache *pcp)
1c79356b
A
353{
354#if DIAGNOSTIC
355 if (pcp->pshm_hash.le_prev == 0)
356 panic("namecache purge le_prev");
357 if (pcp->pshm_hash.le_next == pcp)
358 panic("namecache purge le_next");
359#endif /* DIAGNOSTIC */
360 LIST_REMOVE(pcp, pshm_hash);
361 pcp->pshm_hash.le_prev = 0;
362 pshmnument--;
363}
364
365
1c79356b 366int
b0d623f7 367shm_open(proc_t p, struct shm_open_args *uap, int32_t *retval)
1c79356b 368{
91447636 369 size_t i;
91447636 370 int indx, error;
1c79356b
A
371 struct pshmname nd;
372 struct pshminfo *pinfo;
b0d623f7
A
373 struct fileproc *fp = NULL;
374 char *pnbuf = NULL;
375 struct pshminfo *new_pinfo = PSHMINFO_NULL;
376 struct pshmnode *new_pnode = PSHMNODE_NULL;
377 struct pshmcache *pcache = PSHMCACHE_NULL; /* ignored on return */
1c79356b
A
378 char * nameptr;
379 char * cp;
380 size_t pathlen, plen;
381 int fmode ;
382 int cmode = uap->mode;
383 int incache = 0;
b0d623f7 384 struct pshmcache *pcp = NULL;
1c79356b 385
e5568f75
A
386 AUDIT_ARG(fflags, uap->oflag);
387 AUDIT_ARG(mode, uap->mode);
91447636 388
1c79356b
A
389 pinfo = PSHMINFO_NULL;
390
b0d623f7
A
391 /*
392 * Preallocate everything we might need up front to avoid taking
393 * and dropping the lock, opening us up to race conditions.
394 */
91447636
A
395 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
396 if (pnbuf == NULL) {
b0d623f7
A
397 error = ENOSPC;
398 goto bad;
91447636
A
399 }
400
1c79356b 401 pathlen = MAXPATHLEN;
91447636 402 error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen);
1c79356b
A
403 if (error) {
404 goto bad;
405 }
e5568f75 406 AUDIT_ARG(text, pnbuf);
1c79356b
A
407 if (pathlen > PSHMNAMLEN) {
408 error = ENAMETOOLONG;
409 goto bad;
410 }
1c79356b
A
411#ifdef PSXSHM_NAME_RESTRICT
412 nameptr = pnbuf;
413 if (*nameptr == '/') {
414 while (*(nameptr++) == '/') {
415 plen--;
416 error = EINVAL;
417 goto bad;
418 }
2d21ac55 419 } else {
1c79356b
A
420 error = EINVAL;
421 goto bad;
422 }
423#endif /* PSXSHM_NAME_RESTRICT */
424
425 plen = pathlen;
426 nameptr = pnbuf;
427 nd.pshm_nameptr = nameptr;
428 nd.pshm_namelen = plen;
429 nd. pshm_hash =0;
430
2d21ac55
A
431 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
432 nd.pshm_hash += (unsigned char)*cp * i;
1c79356b
A
433 }
434
b0d623f7
A
435 /*
436 * attempt to allocate a new fp; if unsuccessful, the fp will be
437 * left unmodified (NULL).
438 */
439 error = falloc(p, &fp, &indx, vfs_context_current());
440 if (error)
441 goto bad;
442
6d2010ae
A
443 cmode &= ALLPERMS;
444
445 fmode = FFLAGS(uap->oflag);
446 if ((fmode & (FREAD | FWRITE)) == 0) {
447 error = EINVAL;
448 goto bad;
449 }
450
b0d623f7
A
451 /*
452 * We allocate a new entry if we are less than the maximum
453 * allowed and the one at the front of the LRU list is in use.
454 * Otherwise we use the one at the front of the LRU list.
455 */
456 MALLOC(pcp, struct pshmcache *, sizeof(struct pshmcache), M_SHM, M_WAITOK|M_ZERO);
457 if (pcp == NULL) {
458 error = ENOSPC;
459 goto bad;
460 }
461
462 MALLOC(new_pinfo, struct pshminfo *, sizeof(struct pshminfo), M_SHM, M_WAITOK|M_ZERO);
463 if (new_pinfo == PSHMINFO_NULL) {
464 error = ENOSPC;
465 goto bad;
466 }
467#if CONFIG_MACF
468 mac_posixshm_label_init(new_pinfo);
469#endif
470
471 MALLOC(new_pnode, struct pshmnode *, sizeof(struct pshmnode), M_SHM, M_WAITOK|M_ZERO);
472 if (new_pnode == PSHMNODE_NULL) {
473 error = ENOSPC;
474 goto bad;
475 }
476
91447636 477 PSHM_SUBSYS_LOCK();
b0d623f7 478
6d2010ae
A
479 /*
480 * If we find the entry in the cache, this will take a reference,
481 * allowing us to unlock it for the permissions check.
482 */
483 error = pshm_cache_search(&pinfo, &nd, &pcache, 1);
484
485 PSHM_SUBSYS_UNLOCK();
1c79356b
A
486
487 if (error == ENOENT) {
488 error = EINVAL;
6d2010ae 489 goto bad;
1c79356b 490 }
6d2010ae 491
1c79356b
A
492 if (!error) {
493 incache = 0;
6d2010ae
A
494 if (fmode & O_CREAT) {
495 /* create a new one (commit the allocation) */
496 pinfo = new_pinfo;
497 pinfo->pshm_flags = PSHM_DEFINED | PSHM_INCREATE;
498 pinfo->pshm_usecount = 1; /* existence reference */
499 pinfo->pshm_mode = cmode;
500 pinfo->pshm_uid = kauth_getuid();
501 pinfo->pshm_gid = kauth_getgid();
502 bcopy(pnbuf, &pinfo->pshm_name[0], PSHMNAMLEN);
503 pinfo->pshm_name[PSHMNAMLEN]=0;
504#if CONFIG_MACF
505 error = mac_posixshm_check_create(kauth_cred_get(), nameptr);
506 if (error) {
507 goto bad;
508 }
509 mac_posixshm_label_associate(kauth_cred_get(), pinfo, nameptr);
510#endif
511 }
512 } else {
1c79356b 513 incache = 1;
6d2010ae 514 if (fmode & O_CREAT) {
b0d623f7
A
515 /* already exists */
516 if ((fmode & O_EXCL)) {
517 AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid,
518 pinfo->pshm_gid,
519 pinfo->pshm_mode);
520
521 /* shm obj exists and opened O_EXCL */
522 error = EEXIST;
6d2010ae 523 goto bad;
b0d623f7
A
524 }
525
526 if( pinfo->pshm_flags & PSHM_INDELETE) {
527 error = ENOENT;
6d2010ae 528 goto bad;
b0d623f7 529 }
e5568f75 530 AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid,
2d21ac55 531 pinfo->pshm_gid, pinfo->pshm_mode);
b0d623f7
A
532#if CONFIG_MACF
533 if ((error = mac_posixshm_check_open(kauth_cred_get(), pinfo))) {
6d2010ae 534 goto bad;
2d21ac55 535 }
b0d623f7
A
536#endif
537 if ( (error = pshm_access(pinfo, fmode, kauth_cred_get(), p)) ) {
6d2010ae 538 goto bad;
2d21ac55 539 }
2d21ac55 540 }
6d2010ae
A
541 }
542 if (!(fmode & O_CREAT)) {
1c79356b 543 if (!incache) {
b0d623f7 544 /* O_CREAT is not set and the object does not exist */
1c79356b 545 error = ENOENT;
6d2010ae 546 goto bad;
1c79356b
A
547 }
548 if( pinfo->pshm_flags & PSHM_INDELETE) {
549 error = ENOENT;
6d2010ae 550 goto bad;
1c79356b 551 }
2d21ac55 552#if CONFIG_MACF
b0d623f7 553 if ((error = mac_posixshm_check_open(kauth_cred_get(), pinfo))) {
6d2010ae 554 goto bad;
2d21ac55
A
555 }
556#endif
557
b0d623f7 558 if ((error = pshm_access(pinfo, fmode, kauth_cred_get(), p))) {
6d2010ae 559 goto bad;
91447636 560 }
1c79356b
A
561 }
562 if (fmode & O_TRUNC) {
563 error = EINVAL;
6d2010ae 564 goto bad;
1c79356b 565 }
6d2010ae
A
566
567
568 PSHM_SUBSYS_LOCK();
569
1c79356b
A
570#if DIAGNOSTIC
571 if (fmode & FWRITE)
572 pinfo->pshm_writecount++;
573 if (fmode & FREAD)
574 pinfo->pshm_readcount++;
575#endif
1c79356b 576 if (!incache) {
b0d623f7 577 /* if successful, this will consume the pcp */
91447636 578 if ( (error = pshm_cache_add(pinfo, &nd, pcp)) ) {
b0d623f7 579 goto bad_locked;
1c79356b 580 }
6d2010ae
A
581 /*
582 * add reference for the new entry; otherwise, we obtained
583 * one from the cache hit earlier.
584 */
585 pinfo->pshm_usecount++;
1c79356b
A
586 }
587 pinfo->pshm_flags &= ~PSHM_INCREATE;
b0d623f7 588 new_pnode->pinfo = pinfo;
91447636
A
589
590 PSHM_SUBSYS_UNLOCK();
b0d623f7
A
591
592 /*
593 * if incache, we did not use the new pcp or new_pinfo and must
594 * free them
595 */
596 if (incache) {
597 FREE(pcp, M_SHM);
598
599 if (new_pinfo != PSHMINFO_NULL) {
600#if CONFIG_MACF
601 mac_posixshm_label_destroy(new_pinfo);
602#endif
603 FREE(new_pinfo, M_SHM);
604 }
605 }
606
91447636 607 proc_fdlock(p);
1c79356b
A
608 fp->f_flag = fmode & FMASK;
609 fp->f_type = DTYPE_PSXSHM;
610 fp->f_ops = &pshmops;
b0d623f7 611 fp->f_data = (caddr_t)new_pnode;
2d21ac55 612 *fdflags(p, indx) |= UF_EXCLOSE;
6601e61a 613 procfdtbl_releasefd(p, indx, NULL);
91447636
A
614 fp_drop(p, indx, fp, 1);
615 proc_fdunlock(p);
616
1c79356b 617 *retval = indx;
55e303ae 618 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
1c79356b 619 return (0);
2d21ac55 620
b0d623f7
A
621bad_locked:
622 PSHM_SUBSYS_UNLOCK();
623bad:
6d2010ae
A
624 /*
625 * If we obtained the entry from the cache, we need to drop the
626 * reference; holding the reference may have prevented unlinking,
627 * so we need to call pshm_close() to get the full effect.
628 */
629 if (incache) {
630 PSHM_SUBSYS_LOCK();
631 pshm_close(pinfo, 1);
632 PSHM_SUBSYS_UNLOCK();
633 }
634
b0d623f7
A
635 if (pcp != NULL)
636 FREE(pcp, M_SHM);
637
638 if (new_pnode != PSHMNODE_NULL)
639 FREE(new_pnode, M_SHM);
640
641 if (fp != NULL)
642 fp_free(p, indx, fp);
643
644 if (new_pinfo != PSHMINFO_NULL) {
2d21ac55 645#if CONFIG_MACF
b0d623f7 646 mac_posixshm_label_destroy(new_pinfo);
2d21ac55 647#endif
b0d623f7 648 FREE(new_pinfo, M_SHM);
2d21ac55 649 }
b0d623f7
A
650 if (pnbuf != NULL)
651 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
1c79356b
A
652 return (error);
653}
654
655
1c79356b 656int
2d21ac55 657pshm_truncate(__unused proc_t p, struct fileproc *fp, __unused int fd,
b0d623f7 658 off_t length, __unused int32_t *retval)
1c79356b
A
659{
660 struct pshminfo * pinfo;
661 struct pshmnode * pnode ;
662 kern_return_t kret;
91447636 663 mem_entry_name_port_t mem_object;
6d2010ae
A
664 mach_vm_size_t total_size, alloc_size;
665 memory_object_size_t mosize;
b0d623f7 666 struct pshmobj *pshmobj, *pshmobj_next, **pshmobj_next_p;
2d21ac55
A
667#if CONFIG_MACF
668 int error;
669#endif
1c79356b
A
670
671 if (fp->f_type != DTYPE_PSXSHM) {
672 return(EINVAL);
673 }
674
675
676 if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL )
677 return(EINVAL);
678
91447636
A
679 PSHM_SUBSYS_LOCK();
680 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) {
681 PSHM_SUBSYS_UNLOCK();
1c79356b 682 return(EINVAL);
91447636 683 }
b0d623f7 684 if ((pinfo->pshm_flags & (PSHM_DEFINED|PSHM_ALLOCATING|PSHM_ALLOCATED))
1c79356b 685 != PSHM_DEFINED) {
91447636 686 PSHM_SUBSYS_UNLOCK();
1c79356b
A
687 return(EINVAL);
688 }
2d21ac55 689#if CONFIG_MACF
6d2010ae 690 error = mac_posixshm_check_truncate(kauth_cred_get(), pinfo, length);
2d21ac55
A
691 if (error) {
692 PSHM_SUBSYS_UNLOCK();
693 return(error);
694 }
695#endif
1c79356b 696
b0d623f7
A
697 pinfo->pshm_flags |= PSHM_ALLOCATING;
698 total_size = round_page_64(length);
699 pshmobj_next_p = &pinfo->pshm_memobjects;
1c79356b 700
b0d623f7
A
701 for (alloc_size = 0;
702 alloc_size < total_size;
6d2010ae 703 alloc_size += mosize) {
1c79356b 704
b0d623f7
A
705 PSHM_SUBSYS_UNLOCK();
706
6d2010ae 707 mosize = MIN(total_size - alloc_size, ANON_MAX_SIZE);
b0d623f7
A
708 kret = mach_make_memory_entry_64(
709 VM_MAP_NULL,
6d2010ae 710 &mosize,
b0d623f7
A
711 0,
712 MAP_MEM_NAMED_CREATE | VM_PROT_DEFAULT,
713 &mem_object,
714 0);
715
716 if (kret != KERN_SUCCESS)
717 goto out;
718
719 MALLOC(pshmobj, struct pshmobj *, sizeof (struct pshmobj),
720 M_SHM, M_WAITOK);
721 if (pshmobj == NULL) {
722 kret = KERN_NO_SPACE;
723 mach_memory_entry_port_release(mem_object);
724 mem_object = NULL;
725 goto out;
726 }
727
728 PSHM_SUBSYS_LOCK();
729
730 pshmobj->pshmo_memobject = (void *) mem_object;
6d2010ae 731 pshmobj->pshmo_size = mosize;
b0d623f7
A
732 pshmobj->pshmo_next = NULL;
733
734 *pshmobj_next_p = pshmobj;
735 pshmobj_next_p = &pshmobj->pshmo_next;
736 }
737
1c79356b 738 pinfo->pshm_flags = PSHM_ALLOCATED;
b0d623f7 739 pinfo->pshm_length = total_size;
91447636 740 PSHM_SUBSYS_UNLOCK();
1c79356b
A
741 return(0);
742
743out:
b0d623f7
A
744 PSHM_SUBSYS_LOCK();
745 for (pshmobj = pinfo->pshm_memobjects;
746 pshmobj != NULL;
747 pshmobj = pshmobj_next) {
748 pshmobj_next = pshmobj->pshmo_next;
749 mach_memory_entry_port_release(pshmobj->pshmo_memobject);
750 FREE(pshmobj, M_SHM);
751 }
752 pinfo->pshm_memobjects = NULL;
753 pinfo->pshm_flags &= ~PSHM_ALLOCATING;
754 PSHM_SUBSYS_UNLOCK();
755
1c79356b
A
756 switch (kret) {
757 case KERN_INVALID_ADDRESS:
758 case KERN_NO_SPACE:
759 return (ENOMEM);
760 case KERN_PROTECTION_FAILURE:
761 return (EACCES);
762 default:
763 return (EINVAL);
764
765 }
766}
767
768int
2d21ac55 769pshm_stat(struct pshmnode *pnode, void *ub, int isstat64)
1c79356b 770{
2d21ac55
A
771 struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */
772 struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */
1c79356b 773 struct pshminfo *pinfo;
2d21ac55
A
774#if CONFIG_MACF
775 int error;
776#endif
1c79356b 777
91447636
A
778 PSHM_SUBSYS_LOCK();
779 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL){
780 PSHM_SUBSYS_UNLOCK();
1c79356b 781 return(EINVAL);
91447636 782 }
1c79356b 783
2d21ac55
A
784#if CONFIG_MACF
785 error = mac_posixshm_check_stat(kauth_cred_get(), pinfo);
786 if (error) {
787 PSHM_SUBSYS_UNLOCK();
788 return(error);
789 }
790#endif
791
792 if (isstat64 != 0) {
793 sb64 = (struct stat64 *)ub;
794 bzero(sb64, sizeof(struct stat64));
795 sb64->st_mode = pinfo->pshm_mode;
796 sb64->st_uid = pinfo->pshm_uid;
797 sb64->st_gid = pinfo->pshm_gid;
798 sb64->st_size = pinfo->pshm_length;
799 } else {
800 sb = (struct stat *)ub;
801 bzero(sb, sizeof(struct stat));
802 sb->st_mode = pinfo->pshm_mode;
803 sb->st_uid = pinfo->pshm_uid;
804 sb->st_gid = pinfo->pshm_gid;
805 sb->st_size = pinfo->pshm_length;
806 }
91447636 807 PSHM_SUBSYS_UNLOCK();
1c79356b
A
808
809 return(0);
810}
811
91447636
A
812/*
813 * This is called only from shm_open which holds pshm_lock();
814 * XXX This code is repeated many times
815 */
1c79356b 816int
2d21ac55 817pshm_access(struct pshminfo *pinfo, int mode, kauth_cred_t cred, __unused proc_t p)
1c79356b 818{
6d2010ae
A
819 int mode_req = ((mode & FREAD) ? S_IRUSR : 0) |
820 ((mode & FWRITE) ? S_IWUSR : 0);
1c79356b
A
821
822 /* Otherwise, user id 0 always gets access. */
91447636 823 if (!suser(cred, NULL))
1c79356b
A
824 return (0);
825
6d2010ae 826 return(posix_cred_access(cred, pinfo->pshm_uid, pinfo->pshm_gid, pinfo->pshm_mode, mode_req));
1c79356b 827}
9bccf70c 828
1c79356b 829int
2d21ac55 830pshm_mmap(__unused proc_t p, struct mmap_args *uap, user_addr_t *retval, struct fileproc *fp, off_t pageoff)
1c79356b 831{
91447636
A
832 mach_vm_offset_t user_addr = (mach_vm_offset_t)uap->addr;
833 mach_vm_size_t user_size = (mach_vm_size_t)uap->len ;
b0d623f7
A
834 mach_vm_offset_t user_start_addr;
835 mach_vm_size_t map_size, mapped_size;
1c79356b
A
836 int prot = uap->prot;
837 int flags = uap->flags;
838 vm_object_offset_t file_pos = (vm_object_offset_t)uap->pos;
b0d623f7 839 vm_object_offset_t map_pos;
1c79356b 840 vm_map_t user_map;
91447636
A
841 int alloc_flags;
842 boolean_t docow;
1c79356b
A
843 kern_return_t kret;
844 struct pshminfo * pinfo;
845 struct pshmnode * pnode;
b0d623f7 846 struct pshmobj * pshmobj;
2d21ac55
A
847#if CONFIG_MACF
848 int error;
849#endif
1c79356b
A
850
851 if (user_size == 0)
852 return(0);
853
854 if ((flags & MAP_SHARED) == 0)
855 return(EINVAL);
856
857
858 if ((prot & PROT_WRITE) && ((fp->f_flag & FWRITE) == 0)) {
859 return(EPERM);
860 }
861
862 if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL )
863 return(EINVAL);
864
91447636
A
865 PSHM_SUBSYS_LOCK();
866 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) {
867 PSHM_SUBSYS_UNLOCK();
1c79356b 868 return(EINVAL);
91447636 869 }
1c79356b
A
870
871 if ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED) {
91447636 872 PSHM_SUBSYS_UNLOCK();
1c79356b
A
873 return(EINVAL);
874 }
91447636
A
875 if ((off_t)user_size > pinfo->pshm_length) {
876 PSHM_SUBSYS_UNLOCK();
1c79356b
A
877 return(EINVAL);
878 }
91447636
A
879 if ((off_t)(user_size + file_pos) > pinfo->pshm_length) {
880 PSHM_SUBSYS_UNLOCK();
1c79356b
A
881 return(EINVAL);
882 }
b0d623f7 883 if ((pshmobj = pinfo->pshm_memobjects) == NULL) {
91447636 884 PSHM_SUBSYS_UNLOCK();
1c79356b
A
885 return(EINVAL);
886 }
887
2d21ac55
A
888#if CONFIG_MACF
889 error = mac_posixshm_check_mmap(kauth_cred_get(), pinfo, prot, flags);
890 if (error) {
891 PSHM_SUBSYS_UNLOCK();
892 return(error);
893 }
894#endif
91447636
A
895
896 PSHM_SUBSYS_UNLOCK();
1c79356b
A
897 user_map = current_map();
898
899 if ((flags & MAP_FIXED) == 0) {
91447636
A
900 alloc_flags = VM_FLAGS_ANYWHERE;
901 user_addr = mach_vm_round_page(user_addr);
1c79356b 902 } else {
91447636 903 if (user_addr != mach_vm_trunc_page(user_addr))
1c79356b 904 return (EINVAL);
91447636
A
905 /*
906 * We do not get rid of the existing mappings here because
907 * it wouldn't be atomic (see comment in mmap()). We let
908 * Mach VM know that we want it to replace any existing
909 * mapping with the new one.
910 */
911 alloc_flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE;
1c79356b
A
912 }
913 docow = FALSE;
914
b0d623f7
A
915 mapped_size = 0;
916
917 /* reserver the entire space first... */
918 kret = vm_map_enter_mem_object(user_map,
919 &user_addr,
920 user_size,
921 0,
922 alloc_flags,
923 IPC_PORT_NULL,
924 0,
925 FALSE,
926 VM_PROT_NONE,
927 VM_PROT_NONE,
928 VM_INHERIT_NONE);
929 user_start_addr = user_addr;
1c79356b 930 if (kret != KERN_SUCCESS) {
1c79356b
A
931 goto out;
932 }
b0d623f7
A
933
934 /* ... and overwrite with the real mappings */
935 for (map_pos = 0, pshmobj = pinfo->pshm_memobjects;
936 user_size != 0;
937 map_pos += pshmobj->pshmo_size, pshmobj = pshmobj->pshmo_next) {
938 if (pshmobj == NULL) {
939 /* nothing there to map !? */
940 goto out;
941 }
942 if (file_pos >= map_pos + pshmobj->pshmo_size) {
943 continue;
944 }
945 map_size = pshmobj->pshmo_size - (file_pos - map_pos);
946 if (map_size > user_size) {
947 map_size = user_size;
948 }
949 kret = vm_map_enter_mem_object(
950 user_map,
951 &user_addr,
952 map_size,
953 0,
954 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
955 pshmobj->pshmo_memobject,
956 file_pos - map_pos,
957 docow,
958 prot,
959 VM_PROT_DEFAULT,
960 VM_INHERIT_SHARE);
961 if (kret != KERN_SUCCESS)
962 goto out;
963
964 user_addr += map_size;
965 user_size -= map_size;
966 mapped_size += map_size;
967 file_pos += map_size;
968 }
969
91447636 970 PSHM_SUBSYS_LOCK();
b0d623f7
A
971 pnode->mapp_addr = user_start_addr;
972 pnode->map_size = mapped_size;
1c79356b 973 pinfo->pshm_flags |= (PSHM_MAPPED | PSHM_INUSE);
91447636 974 PSHM_SUBSYS_UNLOCK();
1c79356b 975out:
b0d623f7
A
976 if (kret != KERN_SUCCESS) {
977 if (mapped_size != 0) {
978 (void) mach_vm_deallocate(current_map(),
979 user_start_addr,
980 mapped_size);
981 }
982 }
983
1c79356b
A
984 switch (kret) {
985 case KERN_SUCCESS:
b0d623f7 986 *retval = (user_start_addr + pageoff);
1c79356b
A
987 return (0);
988 case KERN_INVALID_ADDRESS:
989 case KERN_NO_SPACE:
990 return (ENOMEM);
991 case KERN_PROTECTION_FAILURE:
992 return (EACCES);
993 default:
994 return (EINVAL);
995 }
996
997}
998
1c79356b 999int
2d21ac55 1000shm_unlink(__unused proc_t p, struct shm_unlink_args *uap,
b0d623f7 1001 __unused int32_t *retval)
1c79356b 1002{
91447636 1003 size_t i;
1c79356b
A
1004 int error=0;
1005 struct pshmname nd;
1006 struct pshminfo *pinfo;
1c79356b
A
1007 char * pnbuf;
1008 char * nameptr;
1009 char * cp;
1010 size_t pathlen, plen;
1c79356b 1011 int incache = 0;
1c79356b 1012 struct pshmcache *pcache = PSHMCACHE_NULL;
b0d623f7 1013 struct pshmobj *pshmobj, *pshmobj_next;
1c79356b
A
1014
1015 pinfo = PSHMINFO_NULL;
1016
91447636
A
1017 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
1018 if (pnbuf == NULL) {
1019 return(ENOSPC); /* XXX non-standard */
1020 }
1c79356b 1021 pathlen = MAXPATHLEN;
91447636 1022 error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen);
1c79356b
A
1023 if (error) {
1024 goto bad;
1025 }
e5568f75 1026 AUDIT_ARG(text, pnbuf);
1c79356b
A
1027 if (pathlen > PSHMNAMLEN) {
1028 error = ENAMETOOLONG;
1029 goto bad;
1030 }
1031
1032
1033#ifdef PSXSHM_NAME_RESTRICT
1034 nameptr = pnbuf;
1035 if (*nameptr == '/') {
1036 while (*(nameptr++) == '/') {
1037 plen--;
1038 error = EINVAL;
1039 goto bad;
1040 }
1041 } else {
1042 error = EINVAL;
1043 goto bad;
1044 }
1045#endif /* PSXSHM_NAME_RESTRICT */
1046
1047 plen = pathlen;
1048 nameptr = pnbuf;
1049 nd.pshm_nameptr = nameptr;
1050 nd.pshm_namelen = plen;
1051 nd. pshm_hash =0;
1052
1053 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
1054 nd.pshm_hash += (unsigned char)*cp * i;
1055 }
1056
91447636 1057 PSHM_SUBSYS_LOCK();
6d2010ae 1058 error = pshm_cache_search(&pinfo, &nd, &pcache, 0);
1c79356b
A
1059
1060 if (error == ENOENT) {
91447636 1061 PSHM_SUBSYS_UNLOCK();
1c79356b
A
1062 error = EINVAL;
1063 goto bad;
1064
1065 }
1066 if (!error) {
91447636 1067 PSHM_SUBSYS_UNLOCK();
1c79356b
A
1068 error = EINVAL;
1069 goto bad;
1070 } else
1071 incache = 1;
1072
1073 if ((pinfo->pshm_flags & (PSHM_DEFINED | PSHM_ALLOCATED))==0) {
91447636 1074 PSHM_SUBSYS_UNLOCK();
2d21ac55
A
1075 error = EINVAL;
1076 goto bad;
1c79356b
A
1077 }
1078
b0d623f7
A
1079 if (pinfo->pshm_flags & PSHM_ALLOCATING) {
1080 /* XXX should we wait for flag to clear and then proceed ? */
1081 PSHM_SUBSYS_UNLOCK();
1082 error = EAGAIN;
1083 goto bad;
1084 }
1085
1c79356b 1086 if (pinfo->pshm_flags & PSHM_INDELETE) {
91447636 1087 PSHM_SUBSYS_UNLOCK();
1c79356b
A
1088 error = 0;
1089 goto bad;
1090 }
2d21ac55
A
1091#if CONFIG_MACF
1092 error = mac_posixshm_check_unlink(kauth_cred_get(), pinfo, nameptr);
1093 if (error) {
1094 PSHM_SUBSYS_UNLOCK();
1095 goto bad;
1096 }
1097#endif
1c79356b 1098
e5568f75
A
1099 AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid, pinfo->pshm_gid,
1100 pinfo->pshm_mode);
91447636
A
1101
1102 /*
1103 * JMM - How should permissions be checked?
1104 */
1105
1c79356b 1106 pinfo->pshm_flags |= PSHM_INDELETE;
1c79356b 1107 pshm_cache_delete(pcache);
1c79356b 1108 pinfo->pshm_flags |= PSHM_REMOVED;
91447636
A
1109 /* release the existence reference */
1110 if (!--pinfo->pshm_usecount) {
1111 PSHM_SUBSYS_UNLOCK();
1112 /*
1113 * If this is the last reference going away on the object,
1114 * then we need to destroy the backing object. The name
1115 * has an implied but uncounted reference on the object,
b0d623f7 1116 * once it's created, since it's used as a rendezvous, and
91447636
A
1117 * therefore may be subsequently reopened.
1118 */
b0d623f7
A
1119 for (pshmobj = pinfo->pshm_memobjects;
1120 pshmobj != NULL;
1121 pshmobj = pshmobj_next) {
1122 mach_memory_entry_port_release(pshmobj->pshmo_memobject);
1123 pshmobj_next = pshmobj->pshmo_next;
1124 FREE(pshmobj, M_SHM);
1125 }
91447636 1126 FREE(pinfo,M_SHM);
b0d623f7
A
1127 } else {
1128 PSHM_SUBSYS_UNLOCK();
91447636 1129 }
91447636 1130 FREE(pcache, M_SHM);
1c79356b
A
1131 error = 0;
1132bad:
55e303ae 1133 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
1c79356b 1134 return (error);
1c79356b 1135}
1c79356b 1136
91447636
A
1137/* already called locked */
1138static int
6d2010ae 1139pshm_close(struct pshminfo *pinfo, int dropref)
1c79356b 1140{
6d2010ae 1141 int error = 0;
b0d623f7 1142 struct pshmobj *pshmobj, *pshmobj_next;
1c79356b 1143
6d2010ae
A
1144 /*
1145 * If we are dropping the reference we took on the cache object, don't
1146 * enforce the allocation requirement.
1147 */
1148 if ( !dropref && ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED)) {
1c79356b
A
1149 return(EINVAL);
1150 }
1151#if DIAGNOSTIC
1152 if(!pinfo->pshm_usecount) {
1153 kprintf("negative usecount in pshm_close\n");
1154 }
1155#endif /* DIAGNOSTIC */
91447636 1156 pinfo->pshm_usecount--; /* release this fd's reference */
1c79356b
A
1157
1158 if ((pinfo->pshm_flags & PSHM_REMOVED) && !pinfo->pshm_usecount) {
b0d623f7
A
1159#if CONFIG_MACF
1160 mac_posixshm_label_destroy(pinfo);
1161#endif
91447636
A
1162 PSHM_SUBSYS_UNLOCK();
1163 /*
1164 * If this is the last reference going away on the object,
1165 * then we need to destroy the backing object.
1166 */
b0d623f7
A
1167 for (pshmobj = pinfo->pshm_memobjects;
1168 pshmobj != NULL;
1169 pshmobj = pshmobj_next) {
1170 mach_memory_entry_port_release(pshmobj->pshmo_memobject);
1171 pshmobj_next = pshmobj->pshmo_next;
1172 FREE(pshmobj, M_SHM);
1173 }
91447636
A
1174 PSHM_SUBSYS_LOCK();
1175 FREE(pinfo,M_SHM);
1176 }
1c79356b
A
1177 return (error);
1178}
9bccf70c 1179
2d21ac55 1180/* vfs_context_t passed to match prototype for struct fileops */
9bccf70c 1181static int
2d21ac55 1182pshm_closefile(struct fileglob *fg, __unused vfs_context_t ctx)
9bccf70c 1183{
6d2010ae
A
1184 int error = EINVAL;
1185 struct pshmnode *pnode;
91447636
A
1186
1187 PSHM_SUBSYS_LOCK();
6d2010ae
A
1188
1189 if ((pnode = (struct pshmnode *)fg->fg_data) != NULL) {
1190 if (pnode->pinfo != PSHMINFO_NULL) {
1191 error = pshm_close(pnode->pinfo, 0);
1192 }
1193 FREE(pnode, M_SHM);
1194 }
1195
91447636 1196 PSHM_SUBSYS_UNLOCK();
6d2010ae 1197
91447636 1198 return(error);
9bccf70c
A
1199}
1200
1201static int
91447636 1202pshm_read(__unused struct fileproc *fp, __unused struct uio *uio,
2d21ac55 1203 __unused int flags, __unused vfs_context_t ctx)
1c79356b 1204{
91447636 1205 return(ENOTSUP);
1c79356b 1206}
9bccf70c
A
1207
1208static int
91447636 1209pshm_write(__unused struct fileproc *fp, __unused struct uio *uio,
2d21ac55 1210 __unused int flags, __unused vfs_context_t ctx)
1c79356b 1211{
91447636 1212 return(ENOTSUP);
1c79356b 1213}
9bccf70c
A
1214
1215static int
91447636 1216pshm_ioctl(__unused struct fileproc *fp, __unused u_long com,
2d21ac55 1217 __unused caddr_t data, __unused vfs_context_t ctx)
1c79356b 1218{
91447636 1219 return(ENOTSUP);
1c79356b 1220}
9bccf70c
A
1221
1222static int
91447636 1223pshm_select(__unused struct fileproc *fp, __unused int which, __unused void *wql,
2d21ac55 1224 __unused vfs_context_t ctx)
1c79356b 1225{
91447636 1226 return(ENOTSUP);
1c79356b 1227}
55e303ae
A
1228
1229static int
91447636 1230pshm_kqfilter(__unused struct fileproc *fp, __unused struct knote *kn,
2d21ac55 1231 __unused vfs_context_t ctx)
55e303ae 1232{
91447636 1233 return(ENOTSUP);
55e303ae 1234}
0c530ab8
A
1235
1236int
1237fill_pshminfo(struct pshmnode * pshm, struct pshm_info * info)
1238{
1239 struct pshminfo *pinfo;
2d21ac55 1240 struct vinfo_stat *sb;
0c530ab8
A
1241
1242 PSHM_SUBSYS_LOCK();
1243 if ((pinfo = pshm->pinfo) == PSHMINFO_NULL){
1244 PSHM_SUBSYS_UNLOCK();
1245 return(EINVAL);
1246 }
1247
1248 sb = &info->pshm_stat;
1249
2d21ac55
A
1250 bzero(sb, sizeof(struct vinfo_stat));
1251 sb->vst_mode = pinfo->pshm_mode;
1252 sb->vst_uid = pinfo->pshm_uid;
1253 sb->vst_gid = pinfo->pshm_gid;
1254 sb->vst_size = pinfo->pshm_length;
0c530ab8
A
1255
1256 info->pshm_mappaddr = pshm->mapp_addr;
1257 bcopy(&pinfo->pshm_name[0], &info->pshm_name[0], PSHMNAMLEN+1);
1258
1259 PSHM_SUBSYS_UNLOCK();
1260 return(0);
1261}
1262
2d21ac55
A
1263#if CONFIG_MACF
1264void
1265pshm_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx)
1266{
1267 struct pshmnode *pnode;
1268 struct pshminfo *pshm;
0c530ab8 1269
2d21ac55
A
1270 PSHM_SUBSYS_LOCK();
1271 pnode = (struct pshmnode *)fp->f_fglob->fg_data;
1272 if (pnode != NULL) {
1273 pshm = pnode->pinfo;
1274 if (pshm != NULL)
1275 mac_posixshm_vnode_label_associate(
1276 vfs_context_ucred(ctx), pshm, pshm->pshm_label,
1277 vp, vp->v_label);
1278 }
1279 PSHM_SUBSYS_UNLOCK();
1280}
1281#endif