uipc_shm.c revision 205792
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? 49180059Sjhb * 50180059Sjhb * (6) Add MAC support in mac_biba(4) and mac_mls(4). 51180059Sjhb * 52180059Sjhb * (7) Add a MAC check_create() hook for creating new named objects. 53175164Sjhb */ 54175164Sjhb 55175164Sjhb#include <sys/cdefs.h> 56175164Sjhb__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 205792 2010-03-28 13:13:22Z ed $"); 57175164Sjhb 58175164Sjhb#include <sys/param.h> 59175164Sjhb#include <sys/fcntl.h> 60175164Sjhb#include <sys/file.h> 61175164Sjhb#include <sys/filedesc.h> 62175164Sjhb#include <sys/fnv_hash.h> 63175164Sjhb#include <sys/kernel.h> 64175164Sjhb#include <sys/lock.h> 65175164Sjhb#include <sys/malloc.h> 66175164Sjhb#include <sys/mman.h> 67175164Sjhb#include <sys/mutex.h> 68175164Sjhb#include <sys/proc.h> 69175164Sjhb#include <sys/refcount.h> 70175164Sjhb#include <sys/resourcevar.h> 71175164Sjhb#include <sys/stat.h> 72175164Sjhb#include <sys/sysctl.h> 73175164Sjhb#include <sys/sysproto.h> 74175164Sjhb#include <sys/systm.h> 75175164Sjhb#include <sys/sx.h> 76175164Sjhb#include <sys/time.h> 77175164Sjhb#include <sys/vnode.h> 78175164Sjhb 79175164Sjhb#include <security/mac/mac_framework.h> 80175164Sjhb 81175164Sjhb#include <vm/vm.h> 82175164Sjhb#include <vm/vm_param.h> 83175164Sjhb#include <vm/pmap.h> 84175164Sjhb#include <vm/vm_map.h> 85175164Sjhb#include <vm/vm_object.h> 86175164Sjhb#include <vm/vm_page.h> 87175164Sjhb#include <vm/vm_pager.h> 88175164Sjhb#include <vm/swap_pager.h> 89175164Sjhb 90175164Sjhbstruct shm_mapping { 91175164Sjhb char *sm_path; 92175164Sjhb Fnv32_t sm_fnv; 93175164Sjhb struct shmfd *sm_shmfd; 94175164Sjhb LIST_ENTRY(shm_mapping) sm_link; 95175164Sjhb}; 96175164Sjhb 97175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor"); 98175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary; 99175164Sjhbstatic struct sx shm_dict_lock; 100175164Sjhbstatic struct mtx shm_timestamp_lock; 101175164Sjhbstatic u_long shm_hash; 102175164Sjhb 103175164Sjhb#define SHM_HASH(fnv) (&shm_dictionary[(fnv) & shm_hash]) 104175164Sjhb 105175164Sjhbstatic int shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags); 106175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode); 107175164Sjhbstatic void shm_dict_init(void *arg); 108175164Sjhbstatic void shm_drop(struct shmfd *shmfd); 109175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd); 110175164Sjhbstatic void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); 111175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv); 112175164Sjhbstatic int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); 113194766Skibstatic int shm_dotruncate(struct shmfd *shmfd, off_t length); 114175164Sjhb 115175164Sjhbstatic fo_rdwr_t shm_read; 116175164Sjhbstatic fo_rdwr_t shm_write; 117175164Sjhbstatic fo_truncate_t shm_truncate; 118175164Sjhbstatic fo_ioctl_t shm_ioctl; 119175164Sjhbstatic fo_poll_t shm_poll; 120175164Sjhbstatic fo_kqfilter_t shm_kqfilter; 121175164Sjhbstatic fo_stat_t shm_stat; 122175164Sjhbstatic fo_close_t shm_close; 123175164Sjhb 124175164Sjhb/* File descriptor operations. */ 125175164Sjhbstatic struct fileops shm_ops = { 126175164Sjhb .fo_read = shm_read, 127175164Sjhb .fo_write = shm_write, 128175164Sjhb .fo_truncate = shm_truncate, 129175164Sjhb .fo_ioctl = shm_ioctl, 130175164Sjhb .fo_poll = shm_poll, 131175164Sjhb .fo_kqfilter = shm_kqfilter, 132175164Sjhb .fo_stat = shm_stat, 133175164Sjhb .fo_close = shm_close, 134175164Sjhb .fo_flags = DFLAG_PASSABLE 135175164Sjhb}; 136175164Sjhb 137175164SjhbFEATURE(posix_shm, "POSIX shared memory"); 138175164Sjhb 139175164Sjhbstatic int 140175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 141175164Sjhb int flags, struct thread *td) 142175164Sjhb{ 143175164Sjhb 144175164Sjhb return (EOPNOTSUPP); 145175164Sjhb} 146175164Sjhb 147175164Sjhbstatic int 148175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 149175164Sjhb int flags, struct thread *td) 150175164Sjhb{ 151175164Sjhb 152175164Sjhb return (EOPNOTSUPP); 153175164Sjhb} 154175164Sjhb 155175164Sjhbstatic int 156175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred, 157175164Sjhb struct thread *td) 158175164Sjhb{ 159175164Sjhb struct shmfd *shmfd; 160175164Sjhb#ifdef MAC 161175164Sjhb int error; 162175164Sjhb#endif 163175164Sjhb 164175164Sjhb shmfd = fp->f_data; 165175164Sjhb#ifdef MAC 166175164Sjhb error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd); 167175164Sjhb if (error) 168175164Sjhb return (error); 169175164Sjhb#endif 170194766Skib return (shm_dotruncate(shmfd, length)); 171175164Sjhb} 172175164Sjhb 173175164Sjhbstatic int 174175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data, 175175164Sjhb struct ucred *active_cred, struct thread *td) 176175164Sjhb{ 177175164Sjhb 178175164Sjhb return (EOPNOTSUPP); 179175164Sjhb} 180175164Sjhb 181175164Sjhbstatic int 182175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred, 183175164Sjhb struct thread *td) 184175164Sjhb{ 185175164Sjhb 186175164Sjhb return (EOPNOTSUPP); 187175164Sjhb} 188175164Sjhb 189175164Sjhbstatic int 190175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn) 191175164Sjhb{ 192175164Sjhb 193175164Sjhb return (EOPNOTSUPP); 194175164Sjhb} 195175164Sjhb 196175164Sjhbstatic int 197175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 198175164Sjhb struct thread *td) 199175164Sjhb{ 200175164Sjhb struct shmfd *shmfd; 201175164Sjhb#ifdef MAC 202175164Sjhb int error; 203175164Sjhb#endif 204175164Sjhb 205175164Sjhb shmfd = fp->f_data; 206175164Sjhb 207175164Sjhb#ifdef MAC 208175164Sjhb error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd); 209175164Sjhb if (error) 210175164Sjhb return (error); 211175164Sjhb#endif 212175164Sjhb 213175164Sjhb /* 214175164Sjhb * Attempt to return sanish values for fstat() on a memory file 215175164Sjhb * descriptor. 216175164Sjhb */ 217175164Sjhb bzero(sb, sizeof(*sb)); 218175164Sjhb sb->st_mode = S_IFREG | shmfd->shm_mode; /* XXX */ 219175164Sjhb sb->st_blksize = PAGE_SIZE; 220175164Sjhb sb->st_size = shmfd->shm_size; 221175164Sjhb sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize; 222205792Sed sb->st_atim = shmfd->shm_atime; 223205792Sed sb->st_ctim = shmfd->shm_ctime; 224205792Sed sb->st_mtim = shmfd->shm_mtime; 225205792Sed sb->st_birthtim = shmfd->shm_birthtime; 226175164Sjhb sb->st_uid = shmfd->shm_uid; 227175164Sjhb sb->st_gid = shmfd->shm_gid; 228175164Sjhb 229175164Sjhb return (0); 230175164Sjhb} 231175164Sjhb 232175164Sjhbstatic int 233175164Sjhbshm_close(struct file *fp, struct thread *td) 234175164Sjhb{ 235175164Sjhb struct shmfd *shmfd; 236175164Sjhb 237175164Sjhb shmfd = fp->f_data; 238175164Sjhb fp->f_data = NULL; 239175164Sjhb shm_drop(shmfd); 240175164Sjhb 241175164Sjhb return (0); 242175164Sjhb} 243175164Sjhb 244194766Skibstatic int 245175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length) 246175164Sjhb{ 247175164Sjhb vm_object_t object; 248175164Sjhb vm_page_t m; 249175164Sjhb vm_pindex_t nobjsize; 250194766Skib vm_ooffset_t delta; 251175164Sjhb 252175164Sjhb object = shmfd->shm_object; 253175164Sjhb VM_OBJECT_LOCK(object); 254175164Sjhb if (length == shmfd->shm_size) { 255175164Sjhb VM_OBJECT_UNLOCK(object); 256194766Skib return (0); 257175164Sjhb } 258175164Sjhb nobjsize = OFF_TO_IDX(length + PAGE_MASK); 259175164Sjhb 260175164Sjhb /* Are we shrinking? If so, trim the end. */ 261175164Sjhb if (length < shmfd->shm_size) { 262194766Skib delta = ptoa(object->size - nobjsize); 263194766Skib 264175164Sjhb /* Toss in memory pages. */ 265175164Sjhb if (nobjsize < object->size) 266175164Sjhb vm_object_page_remove(object, nobjsize, object->size, 267175164Sjhb FALSE); 268175164Sjhb 269175164Sjhb /* Toss pages from swap. */ 270175164Sjhb if (object->type == OBJT_SWAP) 271194766Skib swap_pager_freespace(object, nobjsize, delta); 272175164Sjhb 273194766Skib /* Free the swap accounted for shm */ 274194766Skib swap_release_by_uid(delta, object->uip); 275194766Skib object->charge -= delta; 276194766Skib 277175164Sjhb /* 278175164Sjhb * If the last page is partially mapped, then zero out 279175164Sjhb * the garbage at the end of the page. See comments 280193303Salc * in vnode_pager_setsize() for more details. 281175164Sjhb * 282175164Sjhb * XXXJHB: This handles in memory pages, but what about 283175164Sjhb * a page swapped out to disk? 284175164Sjhb */ 285175164Sjhb if ((length & PAGE_MASK) && 286175164Sjhb (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL && 287175164Sjhb m->valid != 0) { 288175164Sjhb int base = (int)length & PAGE_MASK; 289175164Sjhb int size = PAGE_SIZE - base; 290175164Sjhb 291175164Sjhb pmap_zero_page_area(m, base, size); 292193303Salc 293193303Salc /* 294193303Salc * Update the valid bits to reflect the blocks that 295193303Salc * have been zeroed. Some of these valid bits may 296193303Salc * have already been set. 297193303Salc */ 298193303Salc vm_page_set_valid(m, base, size); 299193303Salc 300193303Salc /* 301193303Salc * Round "base" to the next block boundary so that the 302193303Salc * dirty bit for a partially zeroed block is not 303193303Salc * cleared. 304193303Salc */ 305193303Salc base = roundup2(base, DEV_BSIZE); 306193303Salc 307175164Sjhb vm_page_lock_queues(); 308193303Salc vm_page_clear_dirty(m, base, PAGE_SIZE - base); 309175164Sjhb vm_page_unlock_queues(); 310176075Salc } else if ((length & PAGE_MASK) && 311176075Salc __predict_false(object->cache != NULL)) { 312176075Salc vm_page_cache_free(object, OFF_TO_IDX(length), 313176075Salc nobjsize); 314175164Sjhb } 315194766Skib } else { 316194766Skib 317194766Skib /* Attempt to reserve the swap */ 318194766Skib delta = ptoa(nobjsize - object->size); 319194766Skib if (!swap_reserve_by_uid(delta, object->uip)) { 320194766Skib VM_OBJECT_UNLOCK(object); 321194766Skib return (ENOMEM); 322194766Skib } 323194766Skib object->charge += delta; 324175164Sjhb } 325175164Sjhb shmfd->shm_size = length; 326175164Sjhb mtx_lock(&shm_timestamp_lock); 327175164Sjhb vfs_timestamp(&shmfd->shm_ctime); 328175164Sjhb shmfd->shm_mtime = shmfd->shm_ctime; 329175164Sjhb mtx_unlock(&shm_timestamp_lock); 330175164Sjhb object->size = nobjsize; 331175164Sjhb VM_OBJECT_UNLOCK(object); 332194766Skib return (0); 333175164Sjhb} 334175164Sjhb 335175164Sjhb/* 336175164Sjhb * shmfd object management including creation and reference counting 337175164Sjhb * routines. 338175164Sjhb */ 339175164Sjhbstatic struct shmfd * 340175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode) 341175164Sjhb{ 342175164Sjhb struct shmfd *shmfd; 343175164Sjhb 344175164Sjhb shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO); 345175164Sjhb shmfd->shm_size = 0; 346175164Sjhb shmfd->shm_uid = ucred->cr_uid; 347175164Sjhb shmfd->shm_gid = ucred->cr_gid; 348175164Sjhb shmfd->shm_mode = mode; 349175164Sjhb shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL, 350194766Skib shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred); 351175164Sjhb KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate")); 352178181Salc VM_OBJECT_LOCK(shmfd->shm_object); 353178181Salc vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING); 354178181Salc vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT); 355178181Salc VM_OBJECT_UNLOCK(shmfd->shm_object); 356175164Sjhb vfs_timestamp(&shmfd->shm_birthtime); 357175164Sjhb shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime = 358175164Sjhb shmfd->shm_birthtime; 359175164Sjhb refcount_init(&shmfd->shm_refs, 1); 360175164Sjhb#ifdef MAC 361175164Sjhb mac_posixshm_init(shmfd); 362175164Sjhb mac_posixshm_create(ucred, shmfd); 363175164Sjhb#endif 364175164Sjhb 365175164Sjhb return (shmfd); 366175164Sjhb} 367175164Sjhb 368175164Sjhbstatic struct shmfd * 369175164Sjhbshm_hold(struct shmfd *shmfd) 370175164Sjhb{ 371175164Sjhb 372175164Sjhb refcount_acquire(&shmfd->shm_refs); 373175164Sjhb return (shmfd); 374175164Sjhb} 375175164Sjhb 376175164Sjhbstatic void 377175164Sjhbshm_drop(struct shmfd *shmfd) 378175164Sjhb{ 379175164Sjhb 380175164Sjhb if (refcount_release(&shmfd->shm_refs)) { 381175164Sjhb#ifdef MAC 382175164Sjhb mac_posixshm_destroy(shmfd); 383175164Sjhb#endif 384175164Sjhb vm_object_deallocate(shmfd->shm_object); 385175164Sjhb free(shmfd, M_SHMFD); 386175164Sjhb } 387175164Sjhb} 388175164Sjhb 389175164Sjhb/* 390175164Sjhb * Determine if the credentials have sufficient permissions for a 391175164Sjhb * specified combination of FREAD and FWRITE. 392175164Sjhb */ 393175164Sjhbstatic int 394175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags) 395175164Sjhb{ 396184413Strasz accmode_t accmode; 397175164Sjhb 398184413Strasz accmode = 0; 399175164Sjhb if (flags & FREAD) 400184413Strasz accmode |= VREAD; 401175164Sjhb if (flags & FWRITE) 402184413Strasz accmode |= VWRITE; 403175164Sjhb return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid, 404184413Strasz accmode, ucred, NULL)); 405175164Sjhb} 406175164Sjhb 407175164Sjhb/* 408175164Sjhb * Dictionary management. We maintain an in-kernel dictionary to map 409175164Sjhb * paths to shmfd objects. We use the FNV hash on the path to store 410175164Sjhb * the mappings in a hash table. 411175164Sjhb */ 412175164Sjhbstatic void 413175164Sjhbshm_dict_init(void *arg) 414175164Sjhb{ 415175164Sjhb 416175164Sjhb mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF); 417175164Sjhb sx_init(&shm_dict_lock, "shm dictionary"); 418175164Sjhb shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash); 419175164Sjhb} 420175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL); 421175164Sjhb 422175164Sjhbstatic struct shmfd * 423175164Sjhbshm_lookup(char *path, Fnv32_t fnv) 424175164Sjhb{ 425175164Sjhb struct shm_mapping *map; 426175164Sjhb 427175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 428175164Sjhb if (map->sm_fnv != fnv) 429175164Sjhb continue; 430175164Sjhb if (strcmp(map->sm_path, path) == 0) 431175164Sjhb return (map->sm_shmfd); 432175164Sjhb } 433175164Sjhb 434175164Sjhb return (NULL); 435175164Sjhb} 436175164Sjhb 437175164Sjhbstatic void 438175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd) 439175164Sjhb{ 440175164Sjhb struct shm_mapping *map; 441175164Sjhb 442175164Sjhb map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK); 443175164Sjhb map->sm_path = path; 444175164Sjhb map->sm_fnv = fnv; 445175164Sjhb map->sm_shmfd = shm_hold(shmfd); 446175164Sjhb LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link); 447175164Sjhb} 448175164Sjhb 449175164Sjhbstatic int 450175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) 451175164Sjhb{ 452175164Sjhb struct shm_mapping *map; 453175164Sjhb int error; 454175164Sjhb 455175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 456175164Sjhb if (map->sm_fnv != fnv) 457175164Sjhb continue; 458175164Sjhb if (strcmp(map->sm_path, path) == 0) { 459175164Sjhb#ifdef MAC 460175164Sjhb error = mac_posixshm_check_unlink(ucred, map->sm_shmfd); 461175164Sjhb if (error) 462175164Sjhb return (error); 463175164Sjhb#endif 464175164Sjhb error = shm_access(map->sm_shmfd, ucred, 465175164Sjhb FREAD | FWRITE); 466175164Sjhb if (error) 467175164Sjhb return (error); 468175164Sjhb LIST_REMOVE(map, sm_link); 469175164Sjhb shm_drop(map->sm_shmfd); 470175164Sjhb free(map->sm_path, M_SHMFD); 471175164Sjhb free(map, M_SHMFD); 472175164Sjhb return (0); 473175164Sjhb } 474175164Sjhb } 475175164Sjhb 476175164Sjhb return (ENOENT); 477175164Sjhb} 478175164Sjhb 479175164Sjhb/* System calls. */ 480175164Sjhbint 481175164Sjhbshm_open(struct thread *td, struct shm_open_args *uap) 482175164Sjhb{ 483175164Sjhb struct filedesc *fdp; 484175164Sjhb struct shmfd *shmfd; 485175164Sjhb struct file *fp; 486175164Sjhb char *path; 487175164Sjhb Fnv32_t fnv; 488175164Sjhb mode_t cmode; 489175164Sjhb int fd, error; 490175164Sjhb 491175164Sjhb if ((uap->flags & O_ACCMODE) != O_RDONLY && 492175164Sjhb (uap->flags & O_ACCMODE) != O_RDWR) 493175164Sjhb return (EINVAL); 494175164Sjhb 495175164Sjhb if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) 496175164Sjhb return (EINVAL); 497175164Sjhb 498175164Sjhb fdp = td->td_proc->p_fd; 499175164Sjhb cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; 500175164Sjhb 501175164Sjhb error = falloc(td, &fp, &fd); 502175164Sjhb if (error) 503175164Sjhb return (error); 504175164Sjhb 505175164Sjhb /* A SHM_ANON path pointer creates an anonymous object. */ 506175164Sjhb if (uap->path == SHM_ANON) { 507175164Sjhb /* A read-only anonymous object is pointless. */ 508175164Sjhb if ((uap->flags & O_ACCMODE) == O_RDONLY) { 509175164Sjhb fdclose(fdp, fp, fd, td); 510175164Sjhb fdrop(fp, td); 511175164Sjhb return (EINVAL); 512175164Sjhb } 513175164Sjhb shmfd = shm_alloc(td->td_ucred, cmode); 514175164Sjhb } else { 515175164Sjhb path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); 516175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 517175164Sjhb 518175164Sjhb /* Require paths to start with a '/' character. */ 519175164Sjhb if (error == 0 && path[0] != '/') 520175164Sjhb error = EINVAL; 521175164Sjhb if (error) { 522175164Sjhb fdclose(fdp, fp, fd, td); 523175164Sjhb fdrop(fp, td); 524175164Sjhb free(path, M_SHMFD); 525175164Sjhb return (error); 526175164Sjhb } 527175164Sjhb 528175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 529175164Sjhb sx_xlock(&shm_dict_lock); 530175164Sjhb shmfd = shm_lookup(path, fnv); 531175164Sjhb if (shmfd == NULL) { 532175164Sjhb /* Object does not yet exist, create it if requested. */ 533175164Sjhb if (uap->flags & O_CREAT) { 534175164Sjhb shmfd = shm_alloc(td->td_ucred, cmode); 535175164Sjhb shm_insert(path, fnv, shmfd); 536175164Sjhb } else { 537175164Sjhb free(path, M_SHMFD); 538175164Sjhb error = ENOENT; 539175164Sjhb } 540175164Sjhb } else { 541175164Sjhb /* 542175164Sjhb * Object already exists, obtain a new 543175164Sjhb * reference if requested and permitted. 544175164Sjhb */ 545175164Sjhb free(path, M_SHMFD); 546175164Sjhb if ((uap->flags & (O_CREAT | O_EXCL)) == 547175164Sjhb (O_CREAT | O_EXCL)) 548175164Sjhb error = EEXIST; 549175164Sjhb else { 550175164Sjhb#ifdef MAC 551175164Sjhb error = mac_posixshm_check_open(td->td_ucred, 552175164Sjhb shmfd); 553175164Sjhb if (error == 0) 554175164Sjhb#endif 555175164Sjhb error = shm_access(shmfd, td->td_ucred, 556175164Sjhb FFLAGS(uap->flags & O_ACCMODE)); 557175164Sjhb } 558175164Sjhb 559175164Sjhb /* 560175164Sjhb * Truncate the file back to zero length if 561175164Sjhb * O_TRUNC was specified and the object was 562175164Sjhb * opened with read/write. 563175164Sjhb */ 564175164Sjhb if (error == 0 && 565175164Sjhb (uap->flags & (O_ACCMODE | O_TRUNC)) == 566175164Sjhb (O_RDWR | O_TRUNC)) { 567175164Sjhb#ifdef MAC 568175164Sjhb error = mac_posixshm_check_truncate( 569175164Sjhb td->td_ucred, fp->f_cred, shmfd); 570175164Sjhb if (error == 0) 571175164Sjhb#endif 572175164Sjhb shm_dotruncate(shmfd, 0); 573175164Sjhb } 574175164Sjhb if (error == 0) 575175164Sjhb shm_hold(shmfd); 576175164Sjhb } 577175164Sjhb sx_xunlock(&shm_dict_lock); 578175164Sjhb 579175164Sjhb if (error) { 580175164Sjhb fdclose(fdp, fp, fd, td); 581175164Sjhb fdrop(fp, td); 582175164Sjhb return (error); 583175164Sjhb } 584175164Sjhb } 585175164Sjhb 586175164Sjhb finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); 587175164Sjhb 588175164Sjhb FILEDESC_XLOCK(fdp); 589175164Sjhb if (fdp->fd_ofiles[fd] == fp) 590175164Sjhb fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 591175164Sjhb FILEDESC_XUNLOCK(fdp); 592175164Sjhb td->td_retval[0] = fd; 593175164Sjhb fdrop(fp, td); 594175164Sjhb 595175164Sjhb return (0); 596175164Sjhb} 597175164Sjhb 598175164Sjhbint 599175164Sjhbshm_unlink(struct thread *td, struct shm_unlink_args *uap) 600175164Sjhb{ 601175164Sjhb char *path; 602175164Sjhb Fnv32_t fnv; 603175164Sjhb int error; 604175164Sjhb 605175164Sjhb path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 606175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 607175164Sjhb if (error) { 608175164Sjhb free(path, M_TEMP); 609175164Sjhb return (error); 610175164Sjhb } 611175164Sjhb 612175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 613175164Sjhb sx_xlock(&shm_dict_lock); 614175164Sjhb error = shm_remove(path, fnv, td->td_ucred); 615175164Sjhb sx_xunlock(&shm_dict_lock); 616175164Sjhb free(path, M_TEMP); 617175164Sjhb 618175164Sjhb return (error); 619175164Sjhb} 620175164Sjhb 621175164Sjhb/* 622175164Sjhb * mmap() helper to validate mmap() requests against shm object state 623175164Sjhb * and give mmap() the vm_object to use for the mapping. 624175164Sjhb */ 625175164Sjhbint 626175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff, 627175164Sjhb vm_object_t *obj) 628175164Sjhb{ 629175164Sjhb 630175164Sjhb /* 631175164Sjhb * XXXRW: This validation is probably insufficient, and subject to 632175164Sjhb * sign errors. It should be fixed. 633175164Sjhb */ 634185533Skan if (foff >= shmfd->shm_size || 635185533Skan foff + objsize > round_page(shmfd->shm_size)) 636175164Sjhb return (EINVAL); 637175164Sjhb 638175164Sjhb mtx_lock(&shm_timestamp_lock); 639175164Sjhb vfs_timestamp(&shmfd->shm_atime); 640175164Sjhb mtx_unlock(&shm_timestamp_lock); 641175164Sjhb vm_object_reference(shmfd->shm_object); 642175164Sjhb *obj = shmfd->shm_object; 643175164Sjhb return (0); 644175164Sjhb} 645