The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_shm.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006 Robert N. M. Watson
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * Support for shared swap-backed anonymous memory objects via
   29  * shm_open(2) and shm_unlink(2).  While most of the implementation is
   30  * here, vm_mmap.c contains mapping logic changes.
   31  *
   32  * TODO:
   33  *
   34  * (2) Need to export data to a userland tool via a sysctl.  Should ipcs(1)
   35  *     and ipcrm(1) be expanded or should new tools to manage both POSIX
   36  *     kernel semaphores and POSIX shared memory be written?
   37  *
   38  * (3) Add support for this file type to fstat(1).
   39  *
   40  * (4) Resource limits?  Does this need its own resource limits or are the
   41  *     existing limits in mmap(2) sufficient?
   42  *
   43  * (5) Partial page truncation.  vnode_pager_setsize() will zero any parts
   44  *     of a partially mapped page as a result of ftruncate(2)/truncate(2).
   45  *     We can do the same (with the same pmap evil), but do we need to
   46  *     worry about the bits on disk if the page is swapped out or will the
   47  *     swapper zero the parts of a page that are invalid if the page is
   48  *     swapped back in for us?
   49  *
   50  * (6) Add MAC support in mac_biba(4) and mac_mls(4).
   51  *
   52  * (7) Add a MAC check_create() hook for creating new named objects.
   53  */
   54 
   55 #include <sys/cdefs.h>
   56 __FBSDID("$FreeBSD: releng/8.4/sys/kern/uipc_shm.c 236699 2012-06-06 21:57:03Z jhb $");
   57 
   58 #include <sys/param.h>
   59 #include <sys/fcntl.h>
   60 #include <sys/file.h>
   61 #include <sys/filedesc.h>
   62 #include <sys/fnv_hash.h>
   63 #include <sys/kernel.h>
   64 #include <sys/lock.h>
   65 #include <sys/malloc.h>
   66 #include <sys/mman.h>
   67 #include <sys/mutex.h>
   68 #include <sys/proc.h>
   69 #include <sys/refcount.h>
   70 #include <sys/resourcevar.h>
   71 #include <sys/stat.h>
   72 #include <sys/sysctl.h>
   73 #include <sys/sysproto.h>
   74 #include <sys/systm.h>
   75 #include <sys/sx.h>
   76 #include <sys/time.h>
   77 #include <sys/vnode.h>
   78 
   79 #include <security/mac/mac_framework.h>
   80 
   81 #include <vm/vm.h>
   82 #include <vm/vm_param.h>
   83 #include <vm/pmap.h>
   84 #include <vm/vm_extern.h>
   85 #include <vm/vm_map.h>
   86 #include <vm/vm_kern.h>
   87 #include <vm/vm_object.h>
   88 #include <vm/vm_page.h>
   89 #include <vm/vm_pager.h>
   90 #include <vm/swap_pager.h>
   91 
   92 struct shm_mapping {
   93         char            *sm_path;
   94         Fnv32_t         sm_fnv;
   95         struct shmfd    *sm_shmfd;
   96         LIST_ENTRY(shm_mapping) sm_link;
   97 };
   98 
   99 static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
  100 static LIST_HEAD(, shm_mapping) *shm_dictionary;
  101 static struct sx shm_dict_lock;
  102 static struct mtx shm_timestamp_lock;
  103 static u_long shm_hash;
  104 
  105 #define SHM_HASH(fnv)   (&shm_dictionary[(fnv) & shm_hash])
  106 
  107 static int      shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
  108 static struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
  109 static void     shm_dict_init(void *arg);
  110 static void     shm_drop(struct shmfd *shmfd);
  111 static struct shmfd *shm_hold(struct shmfd *shmfd);
  112 static void     shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
  113 static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
  114 static int      shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
  115 static int      shm_dotruncate(struct shmfd *shmfd, off_t length);
  116 
  117 static fo_rdwr_t        shm_read;
  118 static fo_rdwr_t        shm_write;
  119 static fo_truncate_t    shm_truncate;
  120 static fo_ioctl_t       shm_ioctl;
  121 static fo_poll_t        shm_poll;
  122 static fo_kqfilter_t    shm_kqfilter;
  123 static fo_stat_t        shm_stat;
  124 static fo_close_t       shm_close;
  125 
  126 /* File descriptor operations. */
  127 static struct fileops shm_ops = {
  128         .fo_read = shm_read,
  129         .fo_write = shm_write,
  130         .fo_truncate = shm_truncate,
  131         .fo_ioctl = shm_ioctl,
  132         .fo_poll = shm_poll,
  133         .fo_kqfilter = shm_kqfilter,
  134         .fo_stat = shm_stat,
  135         .fo_close = shm_close,
  136         .fo_flags = DFLAG_PASSABLE
  137 };
  138 
  139 FEATURE(posix_shm, "POSIX shared memory");
  140 
  141 static int
  142 shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
  143     int flags, struct thread *td)
  144 {
  145 
  146         return (EOPNOTSUPP);
  147 }
  148 
  149 static int
  150 shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
  151     int flags, struct thread *td)
  152 {
  153 
  154         return (EOPNOTSUPP);
  155 }
  156 
  157 static int
  158 shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
  159     struct thread *td)
  160 {
  161         struct shmfd *shmfd;
  162 #ifdef MAC
  163         int error;
  164 #endif
  165 
  166         shmfd = fp->f_data;
  167 #ifdef MAC
  168         error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
  169         if (error)
  170                 return (error);
  171 #endif
  172         return (shm_dotruncate(shmfd, length));
  173 }
  174 
  175 static int
  176 shm_ioctl(struct file *fp, u_long com, void *data,
  177     struct ucred *active_cred, struct thread *td)
  178 {
  179 
  180         return (EOPNOTSUPP);
  181 }
  182 
  183 static int
  184 shm_poll(struct file *fp, int events, struct ucred *active_cred,
  185     struct thread *td)
  186 {
  187 
  188         return (EOPNOTSUPP);
  189 }
  190 
  191 static int
  192 shm_kqfilter(struct file *fp, struct knote *kn)
  193 {
  194 
  195         return (EOPNOTSUPP);
  196 }
  197 
  198 static int
  199 shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
  200     struct thread *td)
  201 {
  202         struct shmfd *shmfd;
  203 #ifdef MAC
  204         int error;
  205 #endif
  206 
  207         shmfd = fp->f_data;
  208 
  209 #ifdef MAC
  210         error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
  211         if (error)
  212                 return (error);
  213 #endif
  214         
  215         /*
  216          * Attempt to return sanish values for fstat() on a memory file
  217          * descriptor.
  218          */
  219         bzero(sb, sizeof(*sb));
  220         sb->st_mode = S_IFREG | shmfd->shm_mode;                /* XXX */
  221         sb->st_blksize = PAGE_SIZE;
  222         sb->st_size = shmfd->shm_size;
  223         sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
  224         sb->st_atimespec = shmfd->shm_atime;
  225         sb->st_ctimespec = shmfd->shm_ctime;
  226         sb->st_mtimespec = shmfd->shm_mtime;
  227         sb->st_birthtimespec = shmfd->shm_birthtime;    
  228         sb->st_uid = shmfd->shm_uid;
  229         sb->st_gid = shmfd->shm_gid;
  230 
  231         return (0);
  232 }
  233 
  234 static int
  235 shm_close(struct file *fp, struct thread *td)
  236 {
  237         struct shmfd *shmfd;
  238 
  239         shmfd = fp->f_data;
  240         fp->f_data = NULL;
  241         shm_drop(shmfd);
  242 
  243         return (0);
  244 }
  245 
  246 static int
  247 shm_dotruncate(struct shmfd *shmfd, off_t length)
  248 {
  249         vm_object_t object;
  250         vm_page_t m;
  251         vm_pindex_t nobjsize;
  252         vm_ooffset_t delta;
  253 
  254         object = shmfd->shm_object;
  255         VM_OBJECT_LOCK(object);
  256         if (length == shmfd->shm_size) {
  257                 VM_OBJECT_UNLOCK(object);
  258                 return (0);
  259         }
  260         nobjsize = OFF_TO_IDX(length + PAGE_MASK);
  261 
  262         /* Are we shrinking?  If so, trim the end. */
  263         if (length < shmfd->shm_size) {
  264                 /*
  265                  * Disallow any requests to shrink the size if this
  266                  * object is mapped into the kernel.
  267                  */
  268                 if (shmfd->shm_kmappings > 0) {
  269                         VM_OBJECT_UNLOCK(object);
  270                         return (EBUSY);
  271                 }
  272                 delta = ptoa(object->size - nobjsize);
  273 
  274                 /* Toss in memory pages. */
  275                 if (nobjsize < object->size)
  276                         vm_object_page_remove(object, nobjsize, object->size,
  277                             FALSE);
  278 
  279                 /* Toss pages from swap. */
  280                 if (object->type == OBJT_SWAP)
  281                         swap_pager_freespace(object, nobjsize, delta);
  282 
  283                 /* Free the swap accounted for shm */
  284                 swap_release_by_uid(delta, object->uip);
  285                 object->charge -= delta;
  286 
  287                 /*
  288                  * If the last page is partially mapped, then zero out
  289                  * the garbage at the end of the page.  See comments
  290                  * in vnode_pager_setsize() for more details.
  291                  *
  292                  * XXXJHB: This handles in memory pages, but what about
  293                  * a page swapped out to disk?
  294                  */
  295                 if ((length & PAGE_MASK) &&
  296                     (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
  297                     m->valid != 0) {
  298                         int base = (int)length & PAGE_MASK;
  299                         int size = PAGE_SIZE - base;
  300 
  301                         pmap_zero_page_area(m, base, size);
  302 
  303                         /*
  304                          * Update the valid bits to reflect the blocks that
  305                          * have been zeroed.  Some of these valid bits may
  306                          * have already been set.
  307                          */
  308                         vm_page_set_valid(m, base, size);
  309 
  310                         /*
  311                          * Round "base" to the next block boundary so that the
  312                          * dirty bit for a partially zeroed block is not
  313                          * cleared.
  314                          */
  315                         base = roundup2(base, DEV_BSIZE);
  316 
  317                         vm_page_lock_queues();
  318                         vm_page_clear_dirty(m, base, PAGE_SIZE - base);
  319                         vm_page_unlock_queues();
  320                 } else if ((length & PAGE_MASK) &&
  321                     __predict_false(object->cache != NULL)) {
  322                         vm_page_cache_free(object, OFF_TO_IDX(length),
  323                             nobjsize);
  324                 }
  325         } else {
  326 
  327                 /* Attempt to reserve the swap */
  328                 delta = ptoa(nobjsize - object->size);
  329                 if (!swap_reserve_by_uid(delta, object->uip)) {
  330                         VM_OBJECT_UNLOCK(object);
  331                         return (ENOMEM);
  332                 }
  333                 object->charge += delta;
  334         }
  335         shmfd->shm_size = length;
  336         mtx_lock(&shm_timestamp_lock);
  337         vfs_timestamp(&shmfd->shm_ctime);
  338         shmfd->shm_mtime = shmfd->shm_ctime;
  339         mtx_unlock(&shm_timestamp_lock);
  340         object->size = nobjsize;
  341         VM_OBJECT_UNLOCK(object);
  342         return (0);
  343 }
  344 
  345 /*
  346  * shmfd object management including creation and reference counting
  347  * routines.
  348  */
  349 static struct shmfd *
  350 shm_alloc(struct ucred *ucred, mode_t mode)
  351 {
  352         struct shmfd *shmfd;
  353 
  354         shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
  355         shmfd->shm_size = 0;
  356         shmfd->shm_uid = ucred->cr_uid;
  357         shmfd->shm_gid = ucred->cr_gid;
  358         shmfd->shm_mode = mode;
  359         shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
  360             shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
  361         KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
  362         VM_OBJECT_LOCK(shmfd->shm_object);
  363         vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
  364         vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
  365         VM_OBJECT_UNLOCK(shmfd->shm_object);
  366         vfs_timestamp(&shmfd->shm_birthtime);
  367         shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
  368             shmfd->shm_birthtime;
  369         refcount_init(&shmfd->shm_refs, 1);
  370 #ifdef MAC
  371         mac_posixshm_init(shmfd);
  372         mac_posixshm_create(ucred, shmfd);
  373 #endif
  374 
  375         return (shmfd);
  376 }
  377 
  378 static struct shmfd *
  379 shm_hold(struct shmfd *shmfd)
  380 {
  381 
  382         refcount_acquire(&shmfd->shm_refs);
  383         return (shmfd);
  384 }
  385 
  386 static void
  387 shm_drop(struct shmfd *shmfd)
  388 {
  389 
  390         if (refcount_release(&shmfd->shm_refs)) {
  391 #ifdef MAC
  392                 mac_posixshm_destroy(shmfd);
  393 #endif
  394                 vm_object_deallocate(shmfd->shm_object);
  395                 free(shmfd, M_SHMFD);
  396         }
  397 }
  398 
  399 /*
  400  * Determine if the credentials have sufficient permissions for a
  401  * specified combination of FREAD and FWRITE.
  402  */
  403 static int
  404 shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
  405 {
  406         accmode_t accmode;
  407 
  408         accmode = 0;
  409         if (flags & FREAD)
  410                 accmode |= VREAD;
  411         if (flags & FWRITE)
  412                 accmode |= VWRITE;
  413         return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
  414             accmode, ucred, NULL));
  415 }
  416 
  417 /*
  418  * Dictionary management.  We maintain an in-kernel dictionary to map
  419  * paths to shmfd objects.  We use the FNV hash on the path to store
  420  * the mappings in a hash table.
  421  */
  422 static void
  423 shm_dict_init(void *arg)
  424 {
  425 
  426         mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
  427         sx_init(&shm_dict_lock, "shm dictionary");
  428         shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
  429 }
  430 SYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
  431 
  432 static struct shmfd *
  433 shm_lookup(char *path, Fnv32_t fnv)
  434 {
  435         struct shm_mapping *map;
  436 
  437         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
  438                 if (map->sm_fnv != fnv)
  439                         continue;
  440                 if (strcmp(map->sm_path, path) == 0)
  441                         return (map->sm_shmfd);
  442         }
  443 
  444         return (NULL);
  445 }
  446 
  447 static void
  448 shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
  449 {
  450         struct shm_mapping *map;
  451 
  452         map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
  453         map->sm_path = path;
  454         map->sm_fnv = fnv;
  455         map->sm_shmfd = shm_hold(shmfd);
  456         shmfd->shm_path = path;
  457         LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
  458 }
  459 
  460 static int
  461 shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
  462 {
  463         struct shm_mapping *map;
  464         int error;
  465 
  466         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
  467                 if (map->sm_fnv != fnv)
  468                         continue;
  469                 if (strcmp(map->sm_path, path) == 0) {
  470 #ifdef MAC
  471                         error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
  472                         if (error)
  473                                 return (error);
  474 #endif
  475                         error = shm_access(map->sm_shmfd, ucred,
  476                             FREAD | FWRITE);
  477                         if (error)
  478                                 return (error);
  479                         map->sm_shmfd->shm_path = NULL;
  480                         LIST_REMOVE(map, sm_link);
  481                         shm_drop(map->sm_shmfd);
  482                         free(map->sm_path, M_SHMFD);
  483                         free(map, M_SHMFD);
  484                         return (0);
  485                 }
  486         }
  487 
  488         return (ENOENT);
  489 }
  490 
  491 /* System calls. */
  492 int
  493 shm_open(struct thread *td, struct shm_open_args *uap)
  494 {
  495         struct filedesc *fdp;
  496         struct shmfd *shmfd;
  497         struct file *fp;
  498         char *path;
  499         Fnv32_t fnv;
  500         mode_t cmode;
  501         int fd, error;
  502 
  503         if ((uap->flags & O_ACCMODE) != O_RDONLY &&
  504             (uap->flags & O_ACCMODE) != O_RDWR)
  505                 return (EINVAL);
  506 
  507         if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
  508                 return (EINVAL);
  509 
  510         fdp = td->td_proc->p_fd;
  511         cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
  512 
  513         error = falloc(td, &fp, &fd);
  514         if (error)
  515                 return (error);
  516 
  517         /* A SHM_ANON path pointer creates an anonymous object. */
  518         if (uap->path == SHM_ANON) {
  519                 /* A read-only anonymous object is pointless. */
  520                 if ((uap->flags & O_ACCMODE) == O_RDONLY) {
  521                         fdclose(fdp, fp, fd, td);
  522                         fdrop(fp, td);
  523                         return (EINVAL);
  524                 }
  525                 shmfd = shm_alloc(td->td_ucred, cmode);
  526         } else {
  527                 path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
  528                 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
  529 
  530                 /* Require paths to start with a '/' character. */
  531                 if (error == 0 && path[0] != '/')
  532                         error = EINVAL;
  533                 if (error) {
  534                         fdclose(fdp, fp, fd, td);
  535                         fdrop(fp, td);
  536                         free(path, M_SHMFD);
  537                         return (error);
  538                 }
  539 
  540                 fnv = fnv_32_str(path, FNV1_32_INIT);
  541                 sx_xlock(&shm_dict_lock);
  542                 shmfd = shm_lookup(path, fnv);
  543                 if (shmfd == NULL) {
  544                         /* Object does not yet exist, create it if requested. */
  545                         if (uap->flags & O_CREAT) {
  546                                 shmfd = shm_alloc(td->td_ucred, cmode);
  547                                 shm_insert(path, fnv, shmfd);
  548                         } else {
  549                                 free(path, M_SHMFD);
  550                                 error = ENOENT;
  551                         }
  552                 } else {
  553                         /*
  554                          * Object already exists, obtain a new
  555                          * reference if requested and permitted.
  556                          */
  557                         free(path, M_SHMFD);
  558                         if ((uap->flags & (O_CREAT | O_EXCL)) ==
  559                             (O_CREAT | O_EXCL))
  560                                 error = EEXIST;
  561                         else {
  562 #ifdef MAC
  563                                 error = mac_posixshm_check_open(td->td_ucred,
  564                                     shmfd);
  565                                 if (error == 0)
  566 #endif
  567                                 error = shm_access(shmfd, td->td_ucred,
  568                                     FFLAGS(uap->flags & O_ACCMODE));
  569                         }
  570 
  571                         /*
  572                          * Truncate the file back to zero length if
  573                          * O_TRUNC was specified and the object was
  574                          * opened with read/write.
  575                          */
  576                         if (error == 0 &&
  577                             (uap->flags & (O_ACCMODE | O_TRUNC)) ==
  578                             (O_RDWR | O_TRUNC)) {
  579 #ifdef MAC
  580                                 error = mac_posixshm_check_truncate(
  581                                         td->td_ucred, fp->f_cred, shmfd);
  582                                 if (error == 0)
  583 #endif
  584                                         shm_dotruncate(shmfd, 0);
  585                         }
  586                         if (error == 0)
  587                                 shm_hold(shmfd);
  588                 }
  589                 sx_xunlock(&shm_dict_lock);
  590 
  591                 if (error) {
  592                         fdclose(fdp, fp, fd, td);
  593                         fdrop(fp, td);
  594                         return (error);
  595                 }
  596         }
  597 
  598         finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
  599 
  600         FILEDESC_XLOCK(fdp);
  601         if (fdp->fd_ofiles[fd] == fp)
  602                 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
  603         FILEDESC_XUNLOCK(fdp);
  604         td->td_retval[0] = fd;
  605         fdrop(fp, td);
  606 
  607         return (0);
  608 }
  609 
  610 int
  611 shm_unlink(struct thread *td, struct shm_unlink_args *uap)
  612 {
  613         char *path;
  614         Fnv32_t fnv;
  615         int error;
  616 
  617         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  618         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
  619         if (error) {
  620                 free(path, M_TEMP);
  621                 return (error);
  622         }
  623 
  624         fnv = fnv_32_str(path, FNV1_32_INIT);
  625         sx_xlock(&shm_dict_lock);
  626         error = shm_remove(path, fnv, td->td_ucred);
  627         sx_xunlock(&shm_dict_lock);
  628         free(path, M_TEMP);
  629 
  630         return (error);
  631 }
  632 
  633 /*
  634  * mmap() helper to validate mmap() requests against shm object state
  635  * and give mmap() the vm_object to use for the mapping.
  636  */
  637 int
  638 shm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
  639     vm_object_t *obj)
  640 {
  641 
  642         /*
  643          * XXXRW: This validation is probably insufficient, and subject to
  644          * sign errors.  It should be fixed.
  645          */
  646         if (foff >= shmfd->shm_size ||
  647             foff + objsize > round_page(shmfd->shm_size))
  648                 return (EINVAL);
  649 
  650         mtx_lock(&shm_timestamp_lock);
  651         vfs_timestamp(&shmfd->shm_atime);
  652         mtx_unlock(&shm_timestamp_lock);
  653         vm_object_reference(shmfd->shm_object);
  654         *obj = shmfd->shm_object;
  655         return (0);
  656 }
  657 
  658 /*
  659  * Helper routines to allow the backing object of a shared memory file
  660  * descriptor to be mapped in the kernel.
  661  */
  662 int
  663 shm_map(struct file *fp, size_t size, off_t offset, void **memp)
  664 {
  665         struct shmfd *shmfd;
  666         vm_offset_t kva, ofs;
  667         vm_object_t obj;
  668         int rv;
  669 
  670         if (fp->f_type != DTYPE_SHM)
  671                 return (EINVAL);
  672         shmfd = fp->f_data;
  673         obj = shmfd->shm_object;
  674         VM_OBJECT_LOCK(obj);
  675         /*
  676          * XXXRW: This validation is probably insufficient, and subject to
  677          * sign errors.  It should be fixed.
  678          */
  679         if (offset >= shmfd->shm_size ||
  680             offset + size > round_page(shmfd->shm_size)) {
  681                 VM_OBJECT_UNLOCK(obj);
  682                 return (EINVAL);
  683         }
  684 
  685         shmfd->shm_kmappings++;
  686         vm_object_reference_locked(obj);
  687         VM_OBJECT_UNLOCK(obj);
  688 
  689         /* Map the object into the kernel_map and wire it. */
  690         kva = vm_map_min(kernel_map);
  691         ofs = offset & PAGE_MASK;
  692         offset = trunc_page(offset);
  693         size = round_page(size + ofs);
  694         rv = vm_map_find(kernel_map, obj, offset, &kva, size,
  695             VMFS_ALIGNED_SPACE, VM_PROT_READ | VM_PROT_WRITE,
  696             VM_PROT_READ | VM_PROT_WRITE, 0);
  697         if (rv == KERN_SUCCESS) {
  698                 rv = vm_map_wire(kernel_map, kva, kva + size,
  699                     VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
  700                 if (rv == KERN_SUCCESS) {
  701                         *memp = (void *)(kva + ofs);
  702                         return (0);
  703                 }
  704                 vm_map_remove(kernel_map, kva, kva + size);
  705         } else
  706                 vm_object_deallocate(obj);
  707 
  708         /* On failure, drop our mapping reference. */
  709         VM_OBJECT_LOCK(obj);
  710         shmfd->shm_kmappings--;
  711         VM_OBJECT_UNLOCK(obj);
  712 
  713         return (vm_mmap_to_errno(rv));
  714 }
  715 
  716 /*
  717  * We require the caller to unmap the entire entry.  This allows us to
  718  * safely decrement shm_kmappings when a mapping is removed.
  719  */
  720 int
  721 shm_unmap(struct file *fp, void *mem, size_t size)
  722 {
  723         struct shmfd *shmfd;
  724         vm_map_entry_t entry;
  725         vm_offset_t kva, ofs;
  726         vm_object_t obj;
  727         vm_pindex_t pindex;
  728         vm_prot_t prot;
  729         boolean_t wired;
  730         vm_map_t map;
  731         int rv;
  732 
  733         if (fp->f_type != DTYPE_SHM)
  734                 return (EINVAL);
  735         shmfd = fp->f_data;
  736         kva = (vm_offset_t)mem;
  737         ofs = kva & PAGE_MASK;
  738         kva = trunc_page(kva);
  739         size = round_page(size + ofs);
  740         map = kernel_map;
  741         rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
  742             &obj, &pindex, &prot, &wired);
  743         if (rv != KERN_SUCCESS)
  744                 return (EINVAL);
  745         if (entry->start != kva || entry->end != kva + size) {
  746                 vm_map_lookup_done(map, entry);
  747                 return (EINVAL);
  748         }
  749         vm_map_lookup_done(map, entry);
  750         if (obj != shmfd->shm_object)
  751                 return (EINVAL);
  752         vm_map_remove(map, kva, kva + size);
  753         VM_OBJECT_LOCK(obj);
  754         KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
  755         shmfd->shm_kmappings--;
  756         VM_OBJECT_UNLOCK(obj);
  757         return (0);
  758 }
  759 
  760 void
  761 shm_path(struct shmfd *shmfd, char *path, size_t size)
  762 {
  763 
  764         if (shmfd->shm_path == NULL)
  765                 return;
  766         sx_slock(&shm_dict_lock);
  767         if (shmfd->shm_path != NULL)
  768                 strlcpy(path, shmfd->shm_path, size);
  769         sx_sunlock(&shm_dict_lock);
  770 }

Cache object: 53f036e88592c050fdf46970e887180d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.