uipc_shm.c revision 178181
1175164Sjhb/*- 2175164Sjhb * Copyright (c) 2006 Robert N. M. Watson 3175164Sjhb * All rights reserved. 4175164Sjhb * 5175164Sjhb * Redistribution and use in source and binary forms, with or without 6175164Sjhb * modification, are permitted provided that the following conditions 7175164Sjhb * are met: 8175164Sjhb * 1. Redistributions of source code must retain the above copyright 9175164Sjhb * notice, this list of conditions and the following disclaimer. 10175164Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11175164Sjhb * notice, this list of conditions and the following disclaimer in the 12175164Sjhb * documentation and/or other materials provided with the distribution. 13175164Sjhb * 14175164Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15175164Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16175164Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17175164Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18175164Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19175164Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20175164Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21175164Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22175164Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23175164Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24175164Sjhb * SUCH DAMAGE. 25175164Sjhb */ 26175164Sjhb 27175164Sjhb/* 28175164Sjhb * Support for shared swap-backed anonymous memory objects via 29175164Sjhb * shm_open(2) and shm_unlink(2). While most of the implementation is 30175164Sjhb * here, vm_mmap.c contains mapping logic changes. 31175164Sjhb * 32175164Sjhb * TODO: 33175164Sjhb * 34175164Sjhb * (2) Need to export data to a userland tool via a sysctl. Should ipcs(1) 35175164Sjhb * and ipcrm(1) be expanded or should new tools to manage both POSIX 36175164Sjhb * kernel semaphores and POSIX shared memory be written? 37175164Sjhb * 38175164Sjhb * (3) Add support for this file type to fstat(1). 39175164Sjhb * 40175164Sjhb * (4) Resource limits? Does this need its own resource limits or are the 41175164Sjhb * existing limits in mmap(2) sufficient? 42175164Sjhb * 43175164Sjhb * (5) Partial page truncation. vnode_pager_setsize() will zero any parts 44175164Sjhb * of a partially mapped page as a result of ftruncate(2)/truncate(2). 45175164Sjhb * We can do the same (with the same pmap evil), but do we need to 46175164Sjhb * worry about the bits on disk if the page is swapped out or will the 47175164Sjhb * swapper zero the parts of a page that are invalid if the page is 48175164Sjhb * swapped back in for us? 49175164Sjhb */ 50175164Sjhb 51175164Sjhb#include <sys/cdefs.h> 52175164Sjhb__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 178181 2008-04-13 21:08:34Z alc $"); 53175164Sjhb 54175164Sjhb#include "opt_mac.h" 55175164Sjhb 56175164Sjhb#include <sys/param.h> 57175164Sjhb#include <sys/fcntl.h> 58175164Sjhb#include <sys/file.h> 59175164Sjhb#include <sys/filedesc.h> 60175164Sjhb#include <sys/fnv_hash.h> 61175164Sjhb#include <sys/kernel.h> 62175164Sjhb#include <sys/lock.h> 63175164Sjhb#include <sys/malloc.h> 64175164Sjhb#include <sys/mman.h> 65175164Sjhb#include <sys/mutex.h> 66175164Sjhb#include <sys/proc.h> 67175164Sjhb#include <sys/refcount.h> 68175164Sjhb#include <sys/resourcevar.h> 69175164Sjhb#include <sys/stat.h> 70175164Sjhb#include <sys/sysctl.h> 71175164Sjhb#include <sys/sysproto.h> 72175164Sjhb#include <sys/systm.h> 73175164Sjhb#include <sys/sx.h> 74175164Sjhb#include <sys/time.h> 75175164Sjhb#include <sys/vnode.h> 76175164Sjhb 77175164Sjhb#include <security/mac/mac_framework.h> 78175164Sjhb 79175164Sjhb#include <vm/vm.h> 80175164Sjhb#include <vm/vm_param.h> 81175164Sjhb#include <vm/pmap.h> 82175164Sjhb#include <vm/vm_map.h> 83175164Sjhb#include <vm/vm_object.h> 84175164Sjhb#include <vm/vm_page.h> 85175164Sjhb#include <vm/vm_pager.h> 86175164Sjhb#include <vm/swap_pager.h> 87175164Sjhb 88175164Sjhbstruct shm_mapping { 89175164Sjhb char *sm_path; 90175164Sjhb Fnv32_t sm_fnv; 91175164Sjhb struct shmfd *sm_shmfd; 92175164Sjhb LIST_ENTRY(shm_mapping) sm_link; 93175164Sjhb}; 94175164Sjhb 95175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor"); 96175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary; 97175164Sjhbstatic struct sx shm_dict_lock; 98175164Sjhbstatic struct mtx shm_timestamp_lock; 99175164Sjhbstatic u_long shm_hash; 100175164Sjhb 101175164Sjhb#define SHM_HASH(fnv) (&shm_dictionary[(fnv) & shm_hash]) 102175164Sjhb 103175164Sjhbstatic int shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags); 104175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode); 105175164Sjhbstatic void shm_dict_init(void *arg); 106175164Sjhbstatic void shm_drop(struct shmfd *shmfd); 107175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd); 108175164Sjhbstatic void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); 109175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv); 110175164Sjhbstatic int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); 111175164Sjhbstatic void shm_dotruncate(struct shmfd *shmfd, off_t length); 112175164Sjhb 113175164Sjhbstatic fo_rdwr_t shm_read; 114175164Sjhbstatic fo_rdwr_t shm_write; 115175164Sjhbstatic fo_truncate_t shm_truncate; 116175164Sjhbstatic fo_ioctl_t shm_ioctl; 117175164Sjhbstatic fo_poll_t shm_poll; 118175164Sjhbstatic fo_kqfilter_t shm_kqfilter; 119175164Sjhbstatic fo_stat_t shm_stat; 120175164Sjhbstatic fo_close_t shm_close; 121175164Sjhb 122175164Sjhb/* File descriptor operations. */ 123175164Sjhbstatic struct fileops shm_ops = { 124175164Sjhb .fo_read = shm_read, 125175164Sjhb .fo_write = shm_write, 126175164Sjhb .fo_truncate = shm_truncate, 127175164Sjhb .fo_ioctl = shm_ioctl, 128175164Sjhb .fo_poll = shm_poll, 129175164Sjhb .fo_kqfilter = shm_kqfilter, 130175164Sjhb .fo_stat = shm_stat, 131175164Sjhb .fo_close = shm_close, 132175164Sjhb .fo_flags = DFLAG_PASSABLE 133175164Sjhb}; 134175164Sjhb 135175164SjhbFEATURE(posix_shm, "POSIX shared memory"); 136175164Sjhb 137175164Sjhbstatic int 138175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 139175164Sjhb int flags, struct thread *td) 140175164Sjhb{ 141175164Sjhb 142175164Sjhb return (EOPNOTSUPP); 143175164Sjhb} 144175164Sjhb 145175164Sjhbstatic int 146175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 147175164Sjhb int flags, struct thread *td) 148175164Sjhb{ 149175164Sjhb 150175164Sjhb return (EOPNOTSUPP); 151175164Sjhb} 152175164Sjhb 153175164Sjhbstatic int 154175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred, 155175164Sjhb struct thread *td) 156175164Sjhb{ 157175164Sjhb struct shmfd *shmfd; 158175164Sjhb#ifdef MAC 159175164Sjhb int error; 160175164Sjhb#endif 161175164Sjhb 162175164Sjhb shmfd = fp->f_data; 163175164Sjhb#ifdef MAC 164175164Sjhb error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd); 165175164Sjhb if (error) 166175164Sjhb return (error); 167175164Sjhb#endif 168175164Sjhb shm_dotruncate(shmfd, length); 169175164Sjhb return (0); 170175164Sjhb} 171175164Sjhb 172175164Sjhbstatic int 173175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data, 174175164Sjhb struct ucred *active_cred, struct thread *td) 175175164Sjhb{ 176175164Sjhb 177175164Sjhb return (EOPNOTSUPP); 178175164Sjhb} 179175164Sjhb 180175164Sjhbstatic int 181175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred, 182175164Sjhb struct thread *td) 183175164Sjhb{ 184175164Sjhb 185175164Sjhb return (EOPNOTSUPP); 186175164Sjhb} 187175164Sjhb 188175164Sjhbstatic int 189175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn) 190175164Sjhb{ 191175164Sjhb 192175164Sjhb return (EOPNOTSUPP); 193175164Sjhb} 194175164Sjhb 195175164Sjhbstatic int 196175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 197175164Sjhb struct thread *td) 198175164Sjhb{ 199175164Sjhb struct shmfd *shmfd; 200175164Sjhb#ifdef MAC 201175164Sjhb int error; 202175164Sjhb#endif 203175164Sjhb 204175164Sjhb shmfd = fp->f_data; 205175164Sjhb 206175164Sjhb#ifdef MAC 207175164Sjhb error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd); 208175164Sjhb if (error) 209175164Sjhb return (error); 210175164Sjhb#endif 211175164Sjhb 212175164Sjhb /* 213175164Sjhb * Attempt to return sanish values for fstat() on a memory file 214175164Sjhb * descriptor. 215175164Sjhb */ 216175164Sjhb bzero(sb, sizeof(*sb)); 217175164Sjhb sb->st_mode = S_IFREG | shmfd->shm_mode; /* XXX */ 218175164Sjhb sb->st_blksize = PAGE_SIZE; 219175164Sjhb sb->st_size = shmfd->shm_size; 220175164Sjhb sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize; 221175164Sjhb sb->st_atimespec = shmfd->shm_atime; 222175164Sjhb sb->st_ctimespec = shmfd->shm_ctime; 223175164Sjhb sb->st_mtimespec = shmfd->shm_mtime; 224175164Sjhb sb->st_birthtimespec = shmfd->shm_birthtime; 225175164Sjhb sb->st_uid = shmfd->shm_uid; 226175164Sjhb sb->st_gid = shmfd->shm_gid; 227175164Sjhb 228175164Sjhb return (0); 229175164Sjhb} 230175164Sjhb 231175164Sjhbstatic int 232175164Sjhbshm_close(struct file *fp, struct thread *td) 233175164Sjhb{ 234175164Sjhb struct shmfd *shmfd; 235175164Sjhb 236175164Sjhb shmfd = fp->f_data; 237175164Sjhb fp->f_data = NULL; 238175164Sjhb shm_drop(shmfd); 239175164Sjhb 240175164Sjhb return (0); 241175164Sjhb} 242175164Sjhb 243175164Sjhbstatic void 244175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length) 245175164Sjhb{ 246175164Sjhb vm_object_t object; 247175164Sjhb vm_page_t m; 248175164Sjhb vm_pindex_t nobjsize; 249175164Sjhb 250175164Sjhb object = shmfd->shm_object; 251175164Sjhb VM_OBJECT_LOCK(object); 252175164Sjhb if (length == shmfd->shm_size) { 253175164Sjhb VM_OBJECT_UNLOCK(object); 254175164Sjhb return; 255175164Sjhb } 256175164Sjhb nobjsize = OFF_TO_IDX(length + PAGE_MASK); 257175164Sjhb 258175164Sjhb /* Are we shrinking? If so, trim the end. */ 259175164Sjhb if (length < shmfd->shm_size) { 260175164Sjhb /* Toss in memory pages. */ 261175164Sjhb if (nobjsize < object->size) 262175164Sjhb vm_object_page_remove(object, nobjsize, object->size, 263175164Sjhb FALSE); 264175164Sjhb 265175164Sjhb /* Toss pages from swap. */ 266175164Sjhb if (object->type == OBJT_SWAP) 267175164Sjhb swap_pager_freespace(object, nobjsize, 268175164Sjhb object->size - nobjsize); 269175164Sjhb 270175164Sjhb /* 271175164Sjhb * If the last page is partially mapped, then zero out 272175164Sjhb * the garbage at the end of the page. See comments 273175164Sjhb * in vnode_page_setsize() for more details. 274175164Sjhb * 275175164Sjhb * XXXJHB: This handles in memory pages, but what about 276175164Sjhb * a page swapped out to disk? 277175164Sjhb */ 278175164Sjhb if ((length & PAGE_MASK) && 279175164Sjhb (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL && 280175164Sjhb m->valid != 0) { 281175164Sjhb int base = (int)length & PAGE_MASK; 282175164Sjhb int size = PAGE_SIZE - base; 283175164Sjhb 284175164Sjhb pmap_zero_page_area(m, base, size); 285175164Sjhb vm_page_lock_queues(); 286175164Sjhb vm_page_set_validclean(m, base, size); 287175164Sjhb if (m->dirty != 0) 288175164Sjhb m->dirty = VM_PAGE_BITS_ALL; 289175164Sjhb vm_page_unlock_queues(); 290176075Salc } else if ((length & PAGE_MASK) && 291176075Salc __predict_false(object->cache != NULL)) { 292176075Salc vm_page_cache_free(object, OFF_TO_IDX(length), 293176075Salc nobjsize); 294175164Sjhb } 295175164Sjhb } 296175164Sjhb shmfd->shm_size = length; 297175164Sjhb mtx_lock(&shm_timestamp_lock); 298175164Sjhb vfs_timestamp(&shmfd->shm_ctime); 299175164Sjhb shmfd->shm_mtime = shmfd->shm_ctime; 300175164Sjhb mtx_unlock(&shm_timestamp_lock); 301175164Sjhb object->size = nobjsize; 302175164Sjhb VM_OBJECT_UNLOCK(object); 303175164Sjhb} 304175164Sjhb 305175164Sjhb/* 306175164Sjhb * shmfd object management including creation and reference counting 307175164Sjhb * routines. 308175164Sjhb */ 309175164Sjhbstatic struct shmfd * 310175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode) 311175164Sjhb{ 312175164Sjhb struct shmfd *shmfd; 313175164Sjhb 314175164Sjhb shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO); 315175164Sjhb shmfd->shm_size = 0; 316175164Sjhb shmfd->shm_uid = ucred->cr_uid; 317175164Sjhb shmfd->shm_gid = ucred->cr_gid; 318175164Sjhb shmfd->shm_mode = mode; 319175164Sjhb shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL, 320175164Sjhb shmfd->shm_size, VM_PROT_DEFAULT, 0); 321175164Sjhb KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate")); 322178181Salc VM_OBJECT_LOCK(shmfd->shm_object); 323178181Salc vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING); 324178181Salc vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT); 325178181Salc VM_OBJECT_UNLOCK(shmfd->shm_object); 326175164Sjhb vfs_timestamp(&shmfd->shm_birthtime); 327175164Sjhb shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime = 328175164Sjhb shmfd->shm_birthtime; 329175164Sjhb refcount_init(&shmfd->shm_refs, 1); 330175164Sjhb#ifdef MAC 331175164Sjhb mac_posixshm_init(shmfd); 332175164Sjhb mac_posixshm_create(ucred, shmfd); 333175164Sjhb#endif 334175164Sjhb 335175164Sjhb return (shmfd); 336175164Sjhb} 337175164Sjhb 338175164Sjhbstatic struct shmfd * 339175164Sjhbshm_hold(struct shmfd *shmfd) 340175164Sjhb{ 341175164Sjhb 342175164Sjhb refcount_acquire(&shmfd->shm_refs); 343175164Sjhb return (shmfd); 344175164Sjhb} 345175164Sjhb 346175164Sjhbstatic void 347175164Sjhbshm_drop(struct shmfd *shmfd) 348175164Sjhb{ 349175164Sjhb 350175164Sjhb if (refcount_release(&shmfd->shm_refs)) { 351175164Sjhb#ifdef MAC 352175164Sjhb mac_posixshm_destroy(shmfd); 353175164Sjhb#endif 354175164Sjhb vm_object_deallocate(shmfd->shm_object); 355175164Sjhb free(shmfd, M_SHMFD); 356175164Sjhb } 357175164Sjhb} 358175164Sjhb 359175164Sjhb/* 360175164Sjhb * Determine if the credentials have sufficient permissions for a 361175164Sjhb * specified combination of FREAD and FWRITE. 362175164Sjhb */ 363175164Sjhbstatic int 364175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags) 365175164Sjhb{ 366175164Sjhb int acc_mode; 367175164Sjhb 368175164Sjhb acc_mode = 0; 369175164Sjhb if (flags & FREAD) 370175164Sjhb acc_mode |= VREAD; 371175164Sjhb if (flags & FWRITE) 372175164Sjhb acc_mode |= VWRITE; 373175164Sjhb return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid, 374175164Sjhb acc_mode, ucred, NULL)); 375175164Sjhb} 376175164Sjhb 377175164Sjhb/* 378175164Sjhb * Dictionary management. We maintain an in-kernel dictionary to map 379175164Sjhb * paths to shmfd objects. We use the FNV hash on the path to store 380175164Sjhb * the mappings in a hash table. 381175164Sjhb */ 382175164Sjhbstatic void 383175164Sjhbshm_dict_init(void *arg) 384175164Sjhb{ 385175164Sjhb 386175164Sjhb mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF); 387175164Sjhb sx_init(&shm_dict_lock, "shm dictionary"); 388175164Sjhb shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash); 389175164Sjhb} 390175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL); 391175164Sjhb 392175164Sjhbstatic struct shmfd * 393175164Sjhbshm_lookup(char *path, Fnv32_t fnv) 394175164Sjhb{ 395175164Sjhb struct shm_mapping *map; 396175164Sjhb 397175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 398175164Sjhb if (map->sm_fnv != fnv) 399175164Sjhb continue; 400175164Sjhb if (strcmp(map->sm_path, path) == 0) 401175164Sjhb return (map->sm_shmfd); 402175164Sjhb } 403175164Sjhb 404175164Sjhb return (NULL); 405175164Sjhb} 406175164Sjhb 407175164Sjhbstatic void 408175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd) 409175164Sjhb{ 410175164Sjhb struct shm_mapping *map; 411175164Sjhb 412175164Sjhb map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK); 413175164Sjhb map->sm_path = path; 414175164Sjhb map->sm_fnv = fnv; 415175164Sjhb map->sm_shmfd = shm_hold(shmfd); 416175164Sjhb LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link); 417175164Sjhb} 418175164Sjhb 419175164Sjhbstatic int 420175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) 421175164Sjhb{ 422175164Sjhb struct shm_mapping *map; 423175164Sjhb int error; 424175164Sjhb 425175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 426175164Sjhb if (map->sm_fnv != fnv) 427175164Sjhb continue; 428175164Sjhb if (strcmp(map->sm_path, path) == 0) { 429175164Sjhb#ifdef MAC 430175164Sjhb error = mac_posixshm_check_unlink(ucred, map->sm_shmfd); 431175164Sjhb if (error) 432175164Sjhb return (error); 433175164Sjhb#endif 434175164Sjhb error = shm_access(map->sm_shmfd, ucred, 435175164Sjhb FREAD | FWRITE); 436175164Sjhb if (error) 437175164Sjhb return (error); 438175164Sjhb LIST_REMOVE(map, sm_link); 439175164Sjhb shm_drop(map->sm_shmfd); 440175164Sjhb free(map->sm_path, M_SHMFD); 441175164Sjhb free(map, M_SHMFD); 442175164Sjhb return (0); 443175164Sjhb } 444175164Sjhb } 445175164Sjhb 446175164Sjhb return (ENOENT); 447175164Sjhb} 448175164Sjhb 449175164Sjhb/* System calls. */ 450175164Sjhbint 451175164Sjhbshm_open(struct thread *td, struct shm_open_args *uap) 452175164Sjhb{ 453175164Sjhb struct filedesc *fdp; 454175164Sjhb struct shmfd *shmfd; 455175164Sjhb struct file *fp; 456175164Sjhb char *path; 457175164Sjhb Fnv32_t fnv; 458175164Sjhb mode_t cmode; 459175164Sjhb int fd, error; 460175164Sjhb 461175164Sjhb if ((uap->flags & O_ACCMODE) != O_RDONLY && 462175164Sjhb (uap->flags & O_ACCMODE) != O_RDWR) 463175164Sjhb return (EINVAL); 464175164Sjhb 465175164Sjhb if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) 466175164Sjhb return (EINVAL); 467175164Sjhb 468175164Sjhb fdp = td->td_proc->p_fd; 469175164Sjhb cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; 470175164Sjhb 471175164Sjhb error = falloc(td, &fp, &fd); 472175164Sjhb if (error) 473175164Sjhb return (error); 474175164Sjhb 475175164Sjhb /* A SHM_ANON path pointer creates an anonymous object. */ 476175164Sjhb if (uap->path == SHM_ANON) { 477175164Sjhb /* A read-only anonymous object is pointless. */ 478175164Sjhb if ((uap->flags & O_ACCMODE) == O_RDONLY) { 479175164Sjhb fdclose(fdp, fp, fd, td); 480175164Sjhb fdrop(fp, td); 481175164Sjhb return (EINVAL); 482175164Sjhb } 483175164Sjhb shmfd = shm_alloc(td->td_ucred, cmode); 484175164Sjhb } else { 485175164Sjhb path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); 486175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 487175164Sjhb 488175164Sjhb /* Require paths to start with a '/' character. */ 489175164Sjhb if (error == 0 && path[0] != '/') 490175164Sjhb error = EINVAL; 491175164Sjhb if (error) { 492175164Sjhb fdclose(fdp, fp, fd, td); 493175164Sjhb fdrop(fp, td); 494175164Sjhb free(path, M_SHMFD); 495175164Sjhb return (error); 496175164Sjhb } 497175164Sjhb 498175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 499175164Sjhb sx_xlock(&shm_dict_lock); 500175164Sjhb shmfd = shm_lookup(path, fnv); 501175164Sjhb if (shmfd == NULL) { 502175164Sjhb /* Object does not yet exist, create it if requested. */ 503175164Sjhb if (uap->flags & O_CREAT) { 504175164Sjhb shmfd = shm_alloc(td->td_ucred, cmode); 505175164Sjhb shm_insert(path, fnv, shmfd); 506175164Sjhb } else { 507175164Sjhb free(path, M_SHMFD); 508175164Sjhb error = ENOENT; 509175164Sjhb } 510175164Sjhb } else { 511175164Sjhb /* 512175164Sjhb * Object already exists, obtain a new 513175164Sjhb * reference if requested and permitted. 514175164Sjhb */ 515175164Sjhb free(path, M_SHMFD); 516175164Sjhb if ((uap->flags & (O_CREAT | O_EXCL)) == 517175164Sjhb (O_CREAT | O_EXCL)) 518175164Sjhb error = EEXIST; 519175164Sjhb else { 520175164Sjhb#ifdef MAC 521175164Sjhb error = mac_posixshm_check_open(td->td_ucred, 522175164Sjhb shmfd); 523175164Sjhb if (error == 0) 524175164Sjhb#endif 525175164Sjhb error = shm_access(shmfd, td->td_ucred, 526175164Sjhb FFLAGS(uap->flags & O_ACCMODE)); 527175164Sjhb } 528175164Sjhb 529175164Sjhb /* 530175164Sjhb * Truncate the file back to zero length if 531175164Sjhb * O_TRUNC was specified and the object was 532175164Sjhb * opened with read/write. 533175164Sjhb */ 534175164Sjhb if (error == 0 && 535175164Sjhb (uap->flags & (O_ACCMODE | O_TRUNC)) == 536175164Sjhb (O_RDWR | O_TRUNC)) { 537175164Sjhb#ifdef MAC 538175164Sjhb error = mac_posixshm_check_truncate( 539175164Sjhb td->td_ucred, fp->f_cred, shmfd); 540175164Sjhb if (error == 0) 541175164Sjhb#endif 542175164Sjhb shm_dotruncate(shmfd, 0); 543175164Sjhb } 544175164Sjhb if (error == 0) 545175164Sjhb shm_hold(shmfd); 546175164Sjhb } 547175164Sjhb sx_xunlock(&shm_dict_lock); 548175164Sjhb 549175164Sjhb if (error) { 550175164Sjhb fdclose(fdp, fp, fd, td); 551175164Sjhb fdrop(fp, td); 552175164Sjhb return (error); 553175164Sjhb } 554175164Sjhb } 555175164Sjhb 556175164Sjhb finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); 557175164Sjhb 558175164Sjhb FILEDESC_XLOCK(fdp); 559175164Sjhb if (fdp->fd_ofiles[fd] == fp) 560175164Sjhb fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 561175164Sjhb FILEDESC_XUNLOCK(fdp); 562175164Sjhb td->td_retval[0] = fd; 563175164Sjhb fdrop(fp, td); 564175164Sjhb 565175164Sjhb return (0); 566175164Sjhb} 567175164Sjhb 568175164Sjhbint 569175164Sjhbshm_unlink(struct thread *td, struct shm_unlink_args *uap) 570175164Sjhb{ 571175164Sjhb char *path; 572175164Sjhb Fnv32_t fnv; 573175164Sjhb int error; 574175164Sjhb 575175164Sjhb path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 576175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 577175164Sjhb if (error) { 578175164Sjhb free(path, M_TEMP); 579175164Sjhb return (error); 580175164Sjhb } 581175164Sjhb 582175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 583175164Sjhb sx_xlock(&shm_dict_lock); 584175164Sjhb error = shm_remove(path, fnv, td->td_ucred); 585175164Sjhb sx_xunlock(&shm_dict_lock); 586175164Sjhb free(path, M_TEMP); 587175164Sjhb 588175164Sjhb return (error); 589175164Sjhb} 590175164Sjhb 591175164Sjhb/* 592175164Sjhb * mmap() helper to validate mmap() requests against shm object state 593175164Sjhb * and give mmap() the vm_object to use for the mapping. 594175164Sjhb */ 595175164Sjhbint 596175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff, 597175164Sjhb vm_object_t *obj) 598175164Sjhb{ 599175164Sjhb 600175164Sjhb /* 601175164Sjhb * XXXRW: This validation is probably insufficient, and subject to 602175164Sjhb * sign errors. It should be fixed. 603175164Sjhb */ 604175164Sjhb if (foff >= shmfd->shm_size || foff + objsize > shmfd->shm_size) 605175164Sjhb return (EINVAL); 606175164Sjhb 607175164Sjhb mtx_lock(&shm_timestamp_lock); 608175164Sjhb vfs_timestamp(&shmfd->shm_atime); 609175164Sjhb mtx_unlock(&shm_timestamp_lock); 610175164Sjhb vm_object_reference(shmfd->shm_object); 611175164Sjhb *obj = shmfd->shm_object; 612175164Sjhb return (0); 613175164Sjhb} 614