]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/posix_shm.c
9fe9b778500bac814565f23c1480135fa7744ab8
[apple/xnu.git] / bsd / kern / posix_shm.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
24 * All Rights Reserved.
25 */
26 /*
27 * posix_shm.c : Support for POSIX shared memory APIs
28 *
29 * File: posix_shm.c
30 * Author: Ananthakrishna Ramesh
31 *
32 * HISTORY
33 * 2-Sep-1999 A.Ramesh
34 * Created for MacOSX
35 *
36 */
37
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>
44 #include <sys/stat.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>
51 #include <sys/tty.h>
52 #include <sys/malloc.h>
53 #include <sys/mman.h>
54 #include <sys/stat.h>
55 #include <sys/sysproto.h>
56 #include <sys/proc_info.h>
57
58 #include <bsm/audit_kernel.h>
59
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>
67
68 #include <vm/vm_map.h>
69 #include <vm/vm_protos.h>
70 #include <vm/vm_shared_memory_server.h>
71
72 #if KTRACE
73 #include <sys/ktrace.h>
74 #endif
75
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 */
84
85
86 struct pshminfo {
87 unsigned int pshm_flags;
88 unsigned int pshm_usecount;
89 off_t pshm_length;
90 mode_t pshm_mode;
91 uid_t pshm_uid;
92 gid_t pshm_gid;
93 char pshm_name[PSHMNAMLEN + 1]; /* segment name */
94 void * pshm_memobject;
95 #if DIAGNOSTIC
96 unsigned int pshm_readcount;
97 unsigned int pshm_writecount;
98 struct proc * pshm_proc;
99 #endif /* DIAGNOSTIC */
100 };
101 #define PSHMINFO_NULL (struct pshminfo *)0
102
103 #define PSHM_NONE 1
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
111
112 struct pshmcache {
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 */
117 };
118 #define PSHMCACHE_NULL (struct pshmcache *)0
119
120 struct pshmstats {
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 */
127 };
128
129 struct pshmname {
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 */
133 };
134
135 struct pshmnode {
136 off_t mapp_addr;
137 user_size_t map_size;
138 struct pshminfo *pinfo;
139 unsigned int pshm_usecount;
140 #if DIAGNOSTIC
141 unsigned int readcnt;
142 unsigned int writecnt;
143 #endif
144 };
145 #define PSHMNODE_NULL (struct pshmnode *)0
146
147
148 #define PSHMHASH(pnp) \
149 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
150
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 */
155
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);
165
166 static int pshm_kqfilter(struct fileproc *fp, struct knote *kn, struct proc *p);
167
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);
171 #if NOT_USED
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);
176
177 struct fileops pshmops =
178 { pshm_read, pshm_write, pshm_ioctl, pshm_select, pshm_closefile, pshm_kqfilter, 0 };
179
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;
184
185 #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex)
186 #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex)
187
188
189 /* Initialize the mutex governing access to the posix shm subsystem */
190 __private_extern__ void
191 pshm_lock_init( void )
192 {
193
194 psx_shm_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
195
196 psx_shm_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr);
197
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);
200 }
201
202 /*
203 * Lookup an entry in the cache
204 *
205 *
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.
210 */
211
212 static int
213 pshm_cache_search(struct pshminfo **pshmp, struct pshmname *pnp,
214 struct pshmcache **pcache)
215 {
216 struct pshmcache *pcp, *nnp;
217 struct pshmhashhead *pcpp;
218
219 if (pnp->pshm_namelen > PSHMNAMLEN) {
220 pshmstats.longnames++;
221 return (0);
222 }
223
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))
229 break;
230 }
231
232 if (pcp == 0) {
233 pshmstats.miss++;
234 return (0);
235 }
236
237 /* We found a "positive" match, return the vnode */
238 if (pcp->pshminfo) {
239 pshmstats.goodhits++;
240 /* TOUCH(ncp); */
241 *pshmp = pcp->pshminfo;
242 *pcache = pcp;
243 return (-1);
244 }
245
246 /*
247 * We found a "negative" match, ENOENT notifies client of this match.
248 * The nc_vpid field records whether this is a whiteout.
249 */
250 pshmstats.neghits++;
251 return (ENOENT);
252 }
253
254 /*
255 * Add an entry to the cache.
256 * XXX should be static?
257 */
258 static int
259 pshm_cache_add(struct pshminfo *pshmp, struct pshmname *pnp, struct pshmcache *pcp)
260 {
261 struct pshmhashhead *pcpp;
262 struct pshminfo *dpinfo;
263 struct pshmcache *dpcp;
264
265 #if DIAGNOSTIC
266 if (pnp->pshm_namelen > NCHNAMLEN)
267 panic("cache_enter: name too long");
268 #endif
269
270
271 /* if the entry has already been added by some one else return */
272 if (pshm_cache_search(&dpinfo, pnp, &dpcp) == -1) {
273 return(EEXIST);
274 }
275 pshmnument++;
276
277 /*
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
281 * otherwise unused.
282 */
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);
287 #if DIAGNOSTIC
288 {
289 struct pshmcache *p;
290
291 for (p = pcpp->lh_first; p != 0; p = p->pshm_hash.le_next)
292 if (p == pcp)
293 panic("cache_enter: duplicate");
294 }
295 #endif
296 LIST_INSERT_HEAD(pcpp, pcp, pshm_hash);
297 return(0);
298 }
299
300 /*
301 * Name cache initialization, from vfs_init() when we are booting
302 */
303 void
304 pshm_cache_init(void)
305 {
306 pshmhashtbl = hashinit(desiredvnodes, M_SHM, &pshmhash);
307 }
308
309 #if NOT_USED
310 /*
311 * Invalidate a all entries to particular vnode.
312 *
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).
317 */
318 static void
319 pshm_cache_purge(void)
320 {
321 struct pshmcache *pcp;
322 struct pshmhashhead *pcpp;
323
324 for (pcpp = &pshmhashtbl[pshmhash]; pcpp >= pshmhashtbl; pcpp--) {
325 while ( (pcp = pcpp->lh_first) )
326 pshm_cache_delete(pcp);
327 }
328 }
329 #endif /* NOT_USED */
330
331 static void
332 pshm_cache_delete(struct pshmcache *pcp)
333 {
334 #if DIAGNOSTIC
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;
342 pshmnument--;
343 }
344
345
346 int
347 shm_open(struct proc *p, struct shm_open_args *uap, register_t *retval)
348 {
349 struct fileproc *fp;
350 size_t i;
351 struct fileproc *nfp;
352 int indx, error;
353 struct pshmname nd;
354 struct pshminfo *pinfo;
355 char * pnbuf;
356 char * nameptr;
357 char * cp;
358 size_t pathlen, plen;
359 int fmode ;
360 int cmode = uap->mode;
361 int incache = 0;
362 struct pshmnode * pnode = PSHMNODE_NULL;
363 struct pshmcache * pcache = PSHMCACHE_NULL;
364 struct pshmcache *pcp;
365 int pinfo_alloc=0;
366
367 AUDIT_ARG(fflags, uap->oflag);
368 AUDIT_ARG(mode, uap->mode);
369
370 pinfo = PSHMINFO_NULL;
371
372 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
373 if (pnbuf == NULL) {
374 return(ENOSPC);
375 }
376
377 pathlen = MAXPATHLEN;
378 error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen);
379 if (error) {
380 goto bad;
381 }
382 AUDIT_ARG(text, pnbuf);
383 if (pathlen > PSHMNAMLEN) {
384 error = ENAMETOOLONG;
385 goto bad;
386 }
387
388
389 #ifdef PSXSHM_NAME_RESTRICT
390 nameptr = pnbuf;
391 if (*nameptr == '/') {
392 while (*(nameptr++) == '/') {
393 plen--;
394 error = EINVAL;
395 goto bad;
396 }
397 } else {
398 error = EINVAL;
399 goto bad;
400 }
401 #endif /* PSXSHM_NAME_RESTRICT */
402
403 plen = pathlen;
404 nameptr = pnbuf;
405 nd.pshm_nameptr = nameptr;
406 nd.pshm_namelen = plen;
407 nd. pshm_hash =0;
408
409 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
410 nd.pshm_hash += (unsigned char)*cp * i;
411 }
412
413 #if KTRACE
414 if (KTRPOINT(p, KTR_NAMEI))
415 ktrnamei(p->p_tracep, nameptr);
416 #endif
417
418 PSHM_SUBSYS_LOCK();
419 error = pshm_cache_search(&pinfo, &nd, &pcache);
420
421 if (error == ENOENT) {
422 PSHM_SUBSYS_UNLOCK();
423 error = EINVAL;
424 goto bad;
425
426 }
427 if (!error) {
428 incache = 0;
429 } else
430 incache = 1;
431 fmode = FFLAGS(uap->oflag);
432 if ((fmode & (FREAD | FWRITE))==0) {
433 PSHM_SUBSYS_UNLOCK();
434 error = EINVAL;
435 goto bad;
436 }
437
438 /*
439 * XXXXXXXXXX TBD XXXXXXXXXX
440 * There is a race that existed with the funnels as well.
441 * Need to be fixed later
442 */
443 PSHM_SUBSYS_UNLOCK();
444 error = falloc(p, &nfp, &indx);
445 if (error )
446 goto bad;
447 PSHM_SUBSYS_LOCK();
448
449 fp = nfp;
450
451 cmode &= ALLPERMS;
452
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);
457
458 /* shm obj exists and opened O_EXCL */
459 #if notyet
460 if (pinfo->pshm_flags & PSHM_INDELETE) {
461 }
462 #endif
463 error = EEXIST;
464 PSHM_SUBSYS_UNLOCK();
465 goto bad1;
466 }
467 if (!incache) {
468 PSHM_SUBSYS_UNLOCK();
469 /* create a new one */
470 MALLOC(pinfo, struct pshminfo *, sizeof(struct pshminfo), M_SHM, M_WAITOK|M_ZERO);
471 if (pinfo == NULL) {
472 error = ENOSPC;
473 goto bad1;
474 }
475 PSHM_SUBSYS_LOCK();
476 pinfo_alloc = 1;
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;
484 } else {
485 /* already exists */
486 if( pinfo->pshm_flags & PSHM_INDELETE) {
487 PSHM_SUBSYS_UNLOCK();
488 error = ENOENT;
489 goto bad1;
490 }
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();
495 goto bad1;
496 }
497 }
498 } else {
499 if (!incache) {
500 /* O_CREAT is not set and the shm obecj does not exist */
501 PSHM_SUBSYS_UNLOCK();
502 error = ENOENT;
503 goto bad1;
504 }
505 if( pinfo->pshm_flags & PSHM_INDELETE) {
506 PSHM_SUBSYS_UNLOCK();
507 error = ENOENT;
508 goto bad1;
509 }
510 if ( (error = pshm_access(pinfo, fmode, kauth_cred_get(), p)) ) {
511 PSHM_SUBSYS_UNLOCK();
512 goto bad1;
513 }
514 }
515 if (fmode & O_TRUNC) {
516 PSHM_SUBSYS_UNLOCK();
517 error = EINVAL;
518 goto bad2;
519 }
520 #if DIAGNOSTIC
521 if (fmode & FWRITE)
522 pinfo->pshm_writecount++;
523 if (fmode & FREAD)
524 pinfo->pshm_readcount++;
525 #endif
526 PSHM_SUBSYS_UNLOCK();
527 MALLOC(pnode, struct pshmnode *, sizeof(struct pshmnode), M_SHM, M_WAITOK|M_ZERO);
528 if (pnode == NULL) {
529 error = ENOSPC;
530 goto bad2;
531 }
532 if (!incache) {
533 /*
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.
537 */
538 MALLOC(pcp, struct pshmcache *, sizeof(struct pshmcache), M_SHM, M_WAITOK|M_ZERO);
539 if (pcp == NULL) {
540 error = ENOSPC;
541 goto bad2;
542 }
543
544 }
545 PSHM_SUBSYS_LOCK();
546
547 if (!incache) {
548 if ( (error = pshm_cache_add(pinfo, &nd, pcp)) ) {
549 PSHM_SUBSYS_UNLOCK();
550 FREE(pcp, M_SHM);
551 goto bad3;
552 }
553 }
554 pinfo->pshm_flags &= ~PSHM_INCREATE;
555 pinfo->pshm_usecount++; /* extra reference for the new fd */
556 pnode->pinfo = pinfo;
557
558 PSHM_SUBSYS_UNLOCK();
559 proc_fdlock(p);
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);
566 proc_fdunlock(p);
567
568 *retval = indx;
569 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
570 return (0);
571 bad3:
572 FREE(pnode, M_SHM);
573
574 bad2:
575 if (pinfo_alloc)
576 FREE(pinfo, M_SHM);
577 bad1:
578 fp_free(p, indx, fp);
579 bad:
580 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
581 return (error);
582 }
583
584
585 int
586 pshm_truncate(__unused struct proc *p, struct fileproc *fp, __unused int fd,
587 off_t length, __unused register_t *retval)
588 {
589 struct pshminfo * pinfo;
590 struct pshmnode * pnode ;
591 kern_return_t kret;
592 mach_vm_offset_t user_addr;
593 mem_entry_name_port_t mem_object;
594 mach_vm_size_t size;
595
596 if (fp->f_type != DTYPE_PSXSHM) {
597 return(EINVAL);
598 }
599
600
601 if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL )
602 return(EINVAL);
603
604 PSHM_SUBSYS_LOCK();
605 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) {
606 PSHM_SUBSYS_UNLOCK();
607 return(EINVAL);
608 }
609 if ((pinfo->pshm_flags & (PSHM_DEFINED | PSHM_ALLOCATED))
610 != PSHM_DEFINED) {
611 PSHM_SUBSYS_UNLOCK();
612 return(EINVAL);
613 }
614
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)
619 goto out;
620
621 kret = mach_make_memory_entry_64 (current_map(), &size,
622 user_addr, VM_PROT_DEFAULT, &mem_object, 0);
623
624 if (kret != KERN_SUCCESS)
625 goto out;
626
627 mach_vm_deallocate(current_map(), user_addr, size);
628
629 PSHM_SUBSYS_LOCK();
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();
635 return(0);
636
637 out:
638 switch (kret) {
639 case KERN_INVALID_ADDRESS:
640 case KERN_NO_SPACE:
641 return (ENOMEM);
642 case KERN_PROTECTION_FAILURE:
643 return (EACCES);
644 default:
645 return (EINVAL);
646
647 }
648 }
649
650 int
651 pshm_stat(struct pshmnode *pnode, struct stat *sb)
652 {
653 struct pshminfo *pinfo;
654
655 PSHM_SUBSYS_LOCK();
656 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL){
657 PSHM_SUBSYS_UNLOCK();
658 return(EINVAL);
659 }
660
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();
667
668 return(0);
669 }
670
671 /*
672 * This is called only from shm_open which holds pshm_lock();
673 * XXX This code is repeated many times
674 */
675 int
676 pshm_access(struct pshminfo *pinfo, int mode, kauth_cred_t cred, __unused struct proc *p)
677 {
678 mode_t mask;
679 int is_member;
680
681 /* Otherwise, user id 0 always gets access. */
682 if (!suser(cred, NULL))
683 return (0);
684
685 mask = 0;
686
687 /* Otherwise, check the owner. */
688 if (kauth_cred_getuid(cred) == pinfo->pshm_uid) {
689 if (mode & FREAD)
690 mask |= S_IRUSR;
691 if (mode & FWRITE)
692 mask |= S_IWUSR;
693 return ((pinfo->pshm_mode & mask) == mask ? 0 : EACCES);
694 }
695
696 /* Otherwise, check the groups. */
697 if (kauth_cred_ismember_gid(cred, pinfo->pshm_gid, &is_member) == 0 && is_member) {
698 if (mode & FREAD)
699 mask |= S_IRGRP;
700 if (mode & FWRITE)
701 mask |= S_IWGRP;
702 return ((pinfo->pshm_mode & mask) == mask ? 0 : EACCES);
703 }
704
705 /* Otherwise, check everyone else. */
706 if (mode & FREAD)
707 mask |= S_IROTH;
708 if (mode & FWRITE)
709 mask |= S_IWOTH;
710 return ((pinfo->pshm_mode & mask) == mask ? 0 : EACCES);
711 }
712
713 int
714 pshm_mmap(struct proc *p, struct mmap_args *uap, user_addr_t *retval, struct fileproc *fp, off_t pageoff)
715 {
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;
721 int fd = uap->fd;
722 vm_map_t user_map;
723 int alloc_flags;
724 boolean_t docow;
725 kern_return_t kret;
726 struct pshminfo * pinfo;
727 struct pshmnode * pnode;
728 void * mem_object;
729
730 if (user_size == 0)
731 return(0);
732
733 if ((flags & MAP_SHARED) == 0)
734 return(EINVAL);
735
736
737 if ((prot & PROT_WRITE) && ((fp->f_flag & FWRITE) == 0)) {
738 return(EPERM);
739 }
740
741 if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL )
742 return(EINVAL);
743
744 PSHM_SUBSYS_LOCK();
745 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) {
746 PSHM_SUBSYS_UNLOCK();
747 return(EINVAL);
748 }
749
750 if ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED) {
751 PSHM_SUBSYS_UNLOCK();
752 return(EINVAL);
753 }
754 if ((off_t)user_size > pinfo->pshm_length) {
755 PSHM_SUBSYS_UNLOCK();
756 return(EINVAL);
757 }
758 if ((off_t)(user_size + file_pos) > pinfo->pshm_length) {
759 PSHM_SUBSYS_UNLOCK();
760 return(EINVAL);
761 }
762 if ((mem_object = pinfo->pshm_memobject) == NULL) {
763 PSHM_SUBSYS_UNLOCK();
764 return(EINVAL);
765 }
766
767
768 PSHM_SUBSYS_UNLOCK();
769 user_map = current_map();
770
771 if ((flags & MAP_FIXED) == 0) {
772 alloc_flags = VM_FLAGS_ANYWHERE;
773 user_addr = mach_vm_round_page(user_addr);
774 } else {
775 if (user_addr != mach_vm_trunc_page(user_addr))
776 return (EINVAL);
777 /*
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.
782 */
783 alloc_flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE;
784 }
785 docow = FALSE;
786
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,
790 VM_INHERIT_SHARE);
791 if (kret != KERN_SUCCESS)
792 goto out;
793 /* LP64todo - this should be superfluous at this point */
794 kret = mach_vm_inherit(user_map, user_addr, user_size,
795 VM_INHERIT_SHARE);
796 if (kret != KERN_SUCCESS) {
797 (void) mach_vm_deallocate(user_map, user_addr, user_size);
798 goto out;
799 }
800 PSHM_SUBSYS_LOCK();
801 pnode->mapp_addr = user_addr;
802 pnode->map_size = user_size;
803 pinfo->pshm_flags |= (PSHM_MAPPED | PSHM_INUSE);
804 PSHM_SUBSYS_UNLOCK();
805 out:
806 switch (kret) {
807 case KERN_SUCCESS:
808 *retval = (user_addr + pageoff);
809 return (0);
810 case KERN_INVALID_ADDRESS:
811 case KERN_NO_SPACE:
812 return (ENOMEM);
813 case KERN_PROTECTION_FAILURE:
814 return (EACCES);
815 default:
816 return (EINVAL);
817 }
818
819 }
820
821 int
822 shm_unlink(__unused struct proc *p, struct shm_unlink_args *uap,
823 __unused register_t *retval)
824 {
825 size_t i;
826 int error=0;
827 struct pshmname nd;
828 struct pshminfo *pinfo;
829 char * pnbuf;
830 char * nameptr;
831 char * cp;
832 size_t pathlen, plen;
833 int incache = 0;
834 struct pshmcache *pcache = PSHMCACHE_NULL;
835
836 pinfo = PSHMINFO_NULL;
837
838 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
839 if (pnbuf == NULL) {
840 return(ENOSPC); /* XXX non-standard */
841 }
842 pathlen = MAXPATHLEN;
843 error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen);
844 if (error) {
845 goto bad;
846 }
847 AUDIT_ARG(text, pnbuf);
848 if (pathlen > PSHMNAMLEN) {
849 error = ENAMETOOLONG;
850 goto bad;
851 }
852
853
854 #ifdef PSXSHM_NAME_RESTRICT
855 nameptr = pnbuf;
856 if (*nameptr == '/') {
857 while (*(nameptr++) == '/') {
858 plen--;
859 error = EINVAL;
860 goto bad;
861 }
862 } else {
863 error = EINVAL;
864 goto bad;
865 }
866 #endif /* PSXSHM_NAME_RESTRICT */
867
868 plen = pathlen;
869 nameptr = pnbuf;
870 nd.pshm_nameptr = nameptr;
871 nd.pshm_namelen = plen;
872 nd. pshm_hash =0;
873
874 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
875 nd.pshm_hash += (unsigned char)*cp * i;
876 }
877
878 PSHM_SUBSYS_LOCK();
879 error = pshm_cache_search(&pinfo, &nd, &pcache);
880
881 if (error == ENOENT) {
882 PSHM_SUBSYS_UNLOCK();
883 error = EINVAL;
884 goto bad;
885
886 }
887 if (!error) {
888 PSHM_SUBSYS_UNLOCK();
889 error = EINVAL;
890 goto bad;
891 } else
892 incache = 1;
893
894 if ((pinfo->pshm_flags & (PSHM_DEFINED | PSHM_ALLOCATED))==0) {
895 PSHM_SUBSYS_UNLOCK();
896 return (EINVAL);
897 }
898
899 if (pinfo->pshm_flags & PSHM_INDELETE) {
900 PSHM_SUBSYS_UNLOCK();
901 error = 0;
902 goto bad;
903 }
904
905 AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid, pinfo->pshm_gid,
906 pinfo->pshm_mode);
907
908 /*
909 * JMM - How should permissions be checked?
910 */
911
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();
918 /*
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.
924 */
925 if (pinfo->pshm_memobject != NULL)
926 mach_memory_entry_port_release(pinfo->pshm_memobject);
927 PSHM_SUBSYS_LOCK();
928 FREE(pinfo,M_SHM);
929 }
930 PSHM_SUBSYS_UNLOCK();
931 FREE(pcache, M_SHM);
932 error = 0;
933 bad:
934 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
935 return (error);
936 }
937
938 /* already called locked */
939 static int
940 pshm_close(struct pshmnode *pnode)
941 {
942 int error=0;
943 struct pshminfo *pinfo;
944
945 if ((pinfo = pnode->pinfo) == PSHMINFO_NULL)
946 return(EINVAL);
947
948 if ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED) {
949 return(EINVAL);
950 }
951 #if DIAGNOSTIC
952 if(!pinfo->pshm_usecount) {
953 kprintf("negative usecount in pshm_close\n");
954 }
955 #endif /* DIAGNOSTIC */
956 pinfo->pshm_usecount--; /* release this fd's reference */
957
958 if ((pinfo->pshm_flags & PSHM_REMOVED) && !pinfo->pshm_usecount) {
959 PSHM_SUBSYS_UNLOCK();
960 /*
961 * If this is the last reference going away on the object,
962 * then we need to destroy the backing object.
963 */
964 if (pinfo->pshm_memobject != NULL)
965 mach_memory_entry_port_release(pinfo->pshm_memobject);
966 PSHM_SUBSYS_LOCK();
967 FREE(pinfo,M_SHM);
968 }
969 FREE(pnode, M_SHM);
970 return (error);
971 }
972
973 /* struct proc passed to match prototype for struct fileops */
974 static int
975 pshm_closefile(struct fileglob *fg, __unused struct proc *p)
976 {
977 int error;
978
979 PSHM_SUBSYS_LOCK();
980 error = pshm_close(((struct pshmnode *)fg->fg_data));
981 PSHM_SUBSYS_UNLOCK();
982 return(error);
983 }
984
985 static int
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)
989 {
990 return(ENOTSUP);
991 }
992
993 static int
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)
997 {
998 return(ENOTSUP);
999 }
1000
1001 static int
1002 pshm_ioctl(__unused struct fileproc *fp, __unused u_long com,
1003 __unused caddr_t data, __unused struct proc *p)
1004 {
1005 return(ENOTSUP);
1006 }
1007
1008 static int
1009 pshm_select(__unused struct fileproc *fp, __unused int which, __unused void *wql,
1010 __unused struct proc *p)
1011 {
1012 return(ENOTSUP);
1013 }
1014
1015 static int
1016 pshm_kqfilter(__unused struct fileproc *fp, __unused struct knote *kn,
1017 __unused struct proc *p)
1018 {
1019 return(ENOTSUP);
1020 }
1021
1022 int
1023 fill_pshminfo(struct pshmnode * pshm, struct pshm_info * info)
1024 {
1025 struct pshminfo *pinfo;
1026 struct stat *sb;
1027
1028 PSHM_SUBSYS_LOCK();
1029 if ((pinfo = pshm->pinfo) == PSHMINFO_NULL){
1030 PSHM_SUBSYS_UNLOCK();
1031 return(EINVAL);
1032 }
1033
1034 sb = &info->pshm_stat;
1035
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;
1041
1042 info->pshm_mappaddr = pshm->mapp_addr;
1043 bcopy(&pinfo->pshm_name[0], &info->pshm_name[0], PSHMNAMLEN+1);
1044
1045 PSHM_SUBSYS_UNLOCK();
1046 return(0);
1047 }
1048
1049