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/sysv_shm.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD: releng/5.1/sys/kern/sysv_shm.c 125489 2004-02-05 18:01:26Z nectar $ */
    2 /*      $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $      */
    3 
    4 /*
    5  * Copyright (c) 1994 Adam Glass and Charles Hannum.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by Adam Glass and Charles
   18  *      Hannum.
   19  * 4. The names of the authors may not be used to endorse or promote products
   20  *    derived from this software without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
   23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   32  */
   33 
   34 #include "opt_compat.h"
   35 #include "opt_sysvipc.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/lock.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/shm.h>
   43 #include <sys/proc.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mman.h>
   46 #include <sys/mutex.h>
   47 #include <sys/stat.h>
   48 #include <sys/syscall.h>
   49 #include <sys/syscallsubr.h>
   50 #include <sys/sysent.h>
   51 #include <sys/sysproto.h>
   52 #include <sys/jail.h>
   53 
   54 #include <vm/vm.h>
   55 #include <vm/vm_param.h>
   56 #include <vm/pmap.h>
   57 #include <vm/vm_object.h>
   58 #include <vm/vm_map.h>
   59 #include <vm/vm_page.h>
   60 #include <vm/vm_pager.h>
   61 
   62 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
   63 
   64 struct oshmctl_args;
   65 static int oshmctl(struct thread *td, struct oshmctl_args *uap);
   66 
   67 static int shmget_allocate_segment(struct thread *td,
   68     struct shmget_args *uap, int mode);
   69 static int shmget_existing(struct thread *td, struct shmget_args *uap,
   70     int mode, int segnum);
   71 
   72 /* XXX casting to (sy_call_t *) is bogus, as usual. */
   73 static sy_call_t *shmcalls[] = {
   74         (sy_call_t *)shmat, (sy_call_t *)oshmctl,
   75         (sy_call_t *)shmdt, (sy_call_t *)shmget,
   76         (sy_call_t *)shmctl
   77 };
   78 
   79 #define SHMSEG_FREE             0x0200
   80 #define SHMSEG_REMOVED          0x0400
   81 #define SHMSEG_ALLOCATED        0x0800
   82 #define SHMSEG_WANTED           0x1000
   83 
   84 static int shm_last_free, shm_nused, shm_committed, shmalloced;
   85 static struct shmid_ds  *shmsegs;
   86 
   87 struct shm_handle {
   88         /* vm_offset_t kva; */
   89         vm_object_t shm_object;
   90 };
   91 
   92 struct shmmap_state {
   93         vm_offset_t va;
   94         int shmid;
   95 };
   96 
   97 static void shm_deallocate_segment(struct shmid_ds *);
   98 static int shm_find_segment_by_key(key_t);
   99 static struct shmid_ds *shm_find_segment_by_shmid(int, int);
  100 static struct shmid_ds *shm_find_segment_by_shmidx(int, int);
  101 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
  102 static void shmrealloc(void);
  103 static void shminit(void);
  104 static int sysvshm_modload(struct module *, int, void *);
  105 static int shmunload(void);
  106 static void shmexit_myhook(struct vmspace *vm);
  107 static void shmfork_myhook(struct proc *p1, struct proc *p2);
  108 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
  109 
  110 /*
  111  * Tuneable values.
  112  */
  113 #ifndef SHMMAXPGS
  114 #define SHMMAXPGS       8192    /* Note: sysv shared memory is swap backed. */
  115 #endif
  116 #ifndef SHMMAX
  117 #define SHMMAX  (SHMMAXPGS*PAGE_SIZE)
  118 #endif
  119 #ifndef SHMMIN
  120 #define SHMMIN  1
  121 #endif
  122 #ifndef SHMMNI
  123 #define SHMMNI  192
  124 #endif
  125 #ifndef SHMSEG
  126 #define SHMSEG  128
  127 #endif
  128 #ifndef SHMALL
  129 #define SHMALL  (SHMMAXPGS)
  130 #endif
  131 
  132 struct  shminfo shminfo = {
  133         SHMMAX,
  134         SHMMIN,
  135         SHMMNI,
  136         SHMSEG,
  137         SHMALL
  138 };
  139 
  140 static int shm_use_phys;
  141 
  142 SYSCTL_DECL(_kern_ipc);
  143 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
  144 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
  145 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
  146 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RD, &shminfo.shmseg, 0, "");
  147 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
  148 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
  149     &shm_use_phys, 0, "");
  150 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
  151     NULL, 0, sysctl_shmsegs, "", "");
  152 
  153 static int
  154 shm_find_segment_by_key(key)
  155         key_t key;
  156 {
  157         int i;
  158 
  159         for (i = 0; i < shmalloced; i++)
  160                 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
  161                     shmsegs[i].shm_perm.key == key)
  162                         return (i);
  163         return (-1);
  164 }
  165 
  166 static struct shmid_ds *
  167 shm_find_segment_by_shmid(int shmid, int wantrem)
  168 {
  169         int segnum;
  170         struct shmid_ds *shmseg;
  171 
  172         segnum = IPCID_TO_IX(shmid);
  173         if (segnum < 0 || segnum >= shmalloced)
  174                 return (NULL);
  175         shmseg = &shmsegs[segnum];
  176         if (!((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) ||
  177             (wantrem && !(shmseg->shm_perm.mode & SHMSEG_REMOVED))) ||
  178             shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
  179                 return (NULL);
  180         return (shmseg);
  181 }
  182 
  183 static struct shmid_ds *
  184 shm_find_segment_by_shmidx(int segnum, int wantrem)
  185 {
  186         struct shmid_ds *shmseg;
  187 
  188         if (segnum < 0 || segnum >= shmalloced)
  189                 return (NULL);
  190         shmseg = &shmsegs[segnum];
  191         if (!((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) ||
  192             (wantrem && !(shmseg->shm_perm.mode & SHMSEG_REMOVED))))
  193                 return (NULL);
  194         return (shmseg);
  195 }
  196 
  197 static void
  198 shm_deallocate_segment(shmseg)
  199         struct shmid_ds *shmseg;
  200 {
  201         struct shm_handle *shm_handle;
  202         size_t size;
  203 
  204         GIANT_REQUIRED;
  205 
  206         shm_handle = shmseg->shm_internal;
  207         vm_object_deallocate(shm_handle->shm_object);
  208         free(shm_handle, M_SHM);
  209         shmseg->shm_internal = NULL;
  210         size = round_page(shmseg->shm_segsz);
  211         shm_committed -= btoc(size);
  212         shm_nused--;
  213         shmseg->shm_perm.mode = SHMSEG_FREE;
  214 }
  215 
  216 static int
  217 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
  218 {
  219         struct shmid_ds *shmseg;
  220         int segnum, result;
  221         size_t size;
  222 
  223         GIANT_REQUIRED;
  224 
  225         segnum = IPCID_TO_IX(shmmap_s->shmid);
  226         shmseg = &shmsegs[segnum];
  227         size = round_page(shmseg->shm_segsz);
  228         result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
  229         if (result != KERN_SUCCESS)
  230                 return (EINVAL);
  231         shmmap_s->shmid = -1;
  232         shmseg->shm_dtime = time_second;
  233         if ((--shmseg->shm_nattch <= 0) &&
  234             (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
  235                 shm_deallocate_segment(shmseg);
  236                 shm_last_free = segnum;
  237         }
  238         return (0);
  239 }
  240 
  241 #ifndef _SYS_SYSPROTO_H_
  242 struct shmdt_args {
  243         const void *shmaddr;
  244 };
  245 #endif
  246 
  247 /*
  248  * MPSAFE
  249  */
  250 int
  251 shmdt(td, uap)
  252         struct thread *td;
  253         struct shmdt_args *uap;
  254 {
  255         struct proc *p = td->td_proc;
  256         struct shmmap_state *shmmap_s;
  257         int i;
  258         int error = 0;
  259 
  260         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  261                 return (ENOSYS);
  262         mtx_lock(&Giant);
  263         shmmap_s = p->p_vmspace->vm_shm;
  264         if (shmmap_s == NULL) {
  265                 error = EINVAL;
  266                 goto done2;
  267         }
  268         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
  269                 if (shmmap_s->shmid != -1 &&
  270                     shmmap_s->va == (vm_offset_t)uap->shmaddr) {
  271                         break;
  272                 }
  273         }
  274         if (i == shminfo.shmseg) {
  275                 error = EINVAL;
  276                 goto done2;
  277         }
  278         error = shm_delete_mapping(p->p_vmspace, shmmap_s);
  279 done2:
  280         mtx_unlock(&Giant);
  281         return (error);
  282 }
  283 
  284 #ifndef _SYS_SYSPROTO_H_
  285 struct shmat_args {
  286         int shmid;
  287         const void *shmaddr;
  288         int shmflg;
  289 };
  290 #endif
  291 
  292 /*
  293  * MPSAFE
  294  */
  295 int
  296 kern_shmat(td, shmid, shmaddr, shmflg, wantrem)
  297         struct thread *td;
  298         int shmid;
  299         const void *shmaddr;
  300         int shmflg;
  301         int wantrem;
  302 {
  303         struct proc *p = td->td_proc;
  304         int i, flags;
  305         struct shmid_ds *shmseg;
  306         struct shmmap_state *shmmap_s = NULL;
  307         struct shm_handle *shm_handle;
  308         vm_offset_t attach_va;
  309         vm_prot_t prot;
  310         vm_size_t size;
  311         int rv;
  312         int error = 0;
  313 
  314         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  315                 return (ENOSYS);
  316         mtx_lock(&Giant);
  317         shmmap_s = p->p_vmspace->vm_shm;
  318         if (shmmap_s == NULL) {
  319                 size = shminfo.shmseg * sizeof(struct shmmap_state);
  320                 shmmap_s = malloc(size, M_SHM, M_WAITOK);
  321                 for (i = 0; i < shminfo.shmseg; i++)
  322                         shmmap_s[i].shmid = -1;
  323                 p->p_vmspace->vm_shm = shmmap_s;
  324         }
  325         shmseg = shm_find_segment_by_shmid(shmid, wantrem);
  326         if (shmseg == NULL) {
  327                 error = EINVAL;
  328                 goto done2;
  329         }
  330         error = ipcperm(td, &shmseg->shm_perm,
  331             (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
  332         if (error)
  333                 goto done2;
  334         for (i = 0; i < shminfo.shmseg; i++) {
  335                 if (shmmap_s->shmid == -1)
  336                         break;
  337                 shmmap_s++;
  338         }
  339         if (i >= shminfo.shmseg) {
  340                 error = EMFILE;
  341                 goto done2;
  342         }
  343         size = round_page(shmseg->shm_segsz);
  344 #ifdef VM_PROT_READ_IS_EXEC
  345         prot = VM_PROT_READ | VM_PROT_EXECUTE;
  346 #else
  347         prot = VM_PROT_READ;
  348 #endif
  349         if ((shmflg & SHM_RDONLY) == 0)
  350                 prot |= VM_PROT_WRITE;
  351         flags = MAP_ANON | MAP_SHARED;
  352         if (shmaddr) {
  353                 flags |= MAP_FIXED;
  354                 if (shmflg & SHM_RND) {
  355                         attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
  356                 } else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
  357                         attach_va = (vm_offset_t)shmaddr;
  358                 } else {
  359                         error = EINVAL;
  360                         goto done2;
  361                 }
  362         } else {
  363                 /*
  364                  * This is just a hint to vm_map_find() about where to
  365                  * put it.
  366                  */
  367                 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
  368                     + maxtsiz + maxdsiz);
  369         }
  370 
  371         shm_handle = shmseg->shm_internal;
  372         vm_object_reference(shm_handle->shm_object);
  373         rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
  374                 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
  375         if (rv != KERN_SUCCESS) {
  376                 vm_object_deallocate(shm_handle->shm_object);
  377                 error = ENOMEM;
  378                 goto done2;
  379         }
  380         vm_map_inherit(&p->p_vmspace->vm_map,
  381                 attach_va, attach_va + size, VM_INHERIT_SHARE);
  382 
  383         shmmap_s->va = attach_va;
  384         shmmap_s->shmid = shmid;
  385         shmseg->shm_lpid = p->p_pid;
  386         shmseg->shm_atime = time_second;
  387         shmseg->shm_nattch++;
  388         td->td_retval[0] = attach_va;
  389 done2:
  390         mtx_unlock(&Giant);
  391         return (error);
  392 }
  393 
  394 int 
  395 shmat(td, uap)
  396         struct thread *td;
  397         struct shmat_args *uap;
  398 {
  399         return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg, 0);
  400 }
  401 
  402 struct oshmid_ds {
  403         struct  ipc_perm shm_perm;      /* operation perms */
  404         int     shm_segsz;              /* size of segment (bytes) */
  405         ushort  shm_cpid;               /* pid, creator */
  406         ushort  shm_lpid;               /* pid, last operation */
  407         short   shm_nattch;             /* no. of current attaches */
  408         time_t  shm_atime;              /* last attach time */
  409         time_t  shm_dtime;              /* last detach time */
  410         time_t  shm_ctime;              /* last change time */
  411         void    *shm_handle;            /* internal handle for shm segment */
  412 };
  413 
  414 struct oshmctl_args {
  415         int shmid;
  416         int cmd;
  417         struct oshmid_ds *ubuf;
  418 };
  419 
  420 /*
  421  * MPSAFE
  422  */
  423 static int
  424 oshmctl(td, uap)
  425         struct thread *td;
  426         struct oshmctl_args *uap;
  427 {
  428 #ifdef COMPAT_43
  429         int error = 0;
  430         struct shmid_ds *shmseg;
  431         struct oshmid_ds outbuf;
  432 
  433         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  434                 return (ENOSYS);
  435         mtx_lock(&Giant);
  436         shmseg = shm_find_segment_by_shmid(uap->shmid, 0);
  437         if (shmseg == NULL) {
  438                 error = EINVAL;
  439                 goto done2;
  440         }
  441         switch (uap->cmd) {
  442         case IPC_STAT:
  443                 error = ipcperm(td, &shmseg->shm_perm, IPC_R);
  444                 if (error)
  445                         goto done2;
  446                 outbuf.shm_perm = shmseg->shm_perm;
  447                 outbuf.shm_segsz = shmseg->shm_segsz;
  448                 outbuf.shm_cpid = shmseg->shm_cpid;
  449                 outbuf.shm_lpid = shmseg->shm_lpid;
  450                 outbuf.shm_nattch = shmseg->shm_nattch;
  451                 outbuf.shm_atime = shmseg->shm_atime;
  452                 outbuf.shm_dtime = shmseg->shm_dtime;
  453                 outbuf.shm_ctime = shmseg->shm_ctime;
  454                 outbuf.shm_handle = shmseg->shm_internal;
  455                 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
  456                 if (error)
  457                         goto done2;
  458                 break;
  459         default:
  460                 /* XXX casting to (sy_call_t *) is bogus, as usual. */
  461                 error = ((sy_call_t *)shmctl)(td, uap);
  462                 break;
  463         }
  464 done2:
  465         mtx_unlock(&Giant);
  466         return (error);
  467 #else
  468         return (EINVAL);
  469 #endif
  470 }
  471 
  472 #ifndef _SYS_SYSPROTO_H_
  473 struct shmctl_args {
  474         int shmid;
  475         int cmd;
  476         struct shmid_ds *buf;
  477 };
  478 #endif
  479 
  480 /*
  481  * MPSAFE
  482  */
  483 int
  484 kern_shmctl(td, shmid, cmd, buf, bufsz, wantrem)
  485         struct thread *td;
  486         int shmid;
  487         int cmd;
  488         void *buf;
  489         size_t *bufsz;
  490         int wantrem;
  491 {
  492         int error = 0;
  493         struct shmid_ds *shmseg;
  494 
  495         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  496                 return (ENOSYS);
  497 
  498         mtx_lock(&Giant);
  499         switch (cmd) {
  500         case IPC_INFO:
  501                 memcpy(buf, &shminfo, sizeof(shminfo));
  502                 if (bufsz)
  503                         *bufsz = sizeof(shminfo);
  504                 td->td_retval[0] = shmalloced;
  505                 goto done2;
  506         case SHM_INFO: {
  507                 struct shm_info shm_info;
  508                 shm_info.used_ids = shm_nused;
  509                 shm_info.shm_rss = 0;   /*XXX where to get from ? */
  510                 shm_info.shm_tot = 0;   /*XXX where to get from ? */
  511                 shm_info.shm_swp = 0;   /*XXX where to get from ? */
  512                 shm_info.swap_attempts = 0;     /*XXX where to get from ? */
  513                 shm_info.swap_successes = 0;    /*XXX where to get from ? */
  514                 memcpy(buf, &shm_info, sizeof(shm_info));
  515                 if (bufsz)
  516                         *bufsz = sizeof(shm_info);
  517                 td->td_retval[0] = shmalloced;
  518                 goto done2;
  519         }
  520         }
  521         if (cmd == SHM_STAT)
  522                 shmseg = shm_find_segment_by_shmidx(shmid, wantrem);
  523         else
  524                 shmseg = shm_find_segment_by_shmid(shmid, wantrem);
  525         if (shmseg == NULL) {
  526                 error = EINVAL;
  527                 goto done2;
  528         }
  529         switch (cmd) {
  530         case SHM_STAT:
  531         case IPC_STAT:
  532                 error = ipcperm(td, &shmseg->shm_perm, IPC_R);
  533                 if (error)
  534                         goto done2;
  535                 memcpy(buf, shmseg, sizeof(struct shmid_ds));
  536                 if (bufsz)
  537                         *bufsz = sizeof(struct shmid_ds);
  538                 if (cmd == SHM_STAT)
  539                         td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->shm_perm);
  540                 break;
  541         case IPC_SET: {
  542                 struct shmid_ds *shmid;
  543 
  544                 shmid = (struct shmid_ds *)buf;
  545                 error = ipcperm(td, &shmseg->shm_perm, IPC_M);
  546                 if (error)
  547                         goto done2;
  548                 shmseg->shm_perm.uid = shmid->shm_perm.uid;
  549                 shmseg->shm_perm.gid = shmid->shm_perm.gid;
  550                 shmseg->shm_perm.mode =
  551                     (shmseg->shm_perm.mode & ~ACCESSPERMS) |
  552                     (shmid->shm_perm.mode & ACCESSPERMS);
  553                 shmseg->shm_ctime = time_second;
  554                 break;
  555         }
  556         case IPC_RMID:
  557                 error = ipcperm(td, &shmseg->shm_perm, IPC_M);
  558                 if (error)
  559                         goto done2;
  560                 shmseg->shm_perm.key = IPC_PRIVATE;
  561                 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
  562                 if (shmseg->shm_nattch <= 0) {
  563                         shm_deallocate_segment(shmseg);
  564                         shm_last_free = IPCID_TO_IX(shmid);
  565                 }
  566                 break;
  567 #if 0
  568         case SHM_LOCK:
  569         case SHM_UNLOCK:
  570 #endif
  571         default:
  572                 error = EINVAL;
  573                 break;
  574         }
  575 done2:
  576         mtx_unlock(&Giant);
  577         return (error);
  578 }
  579 
  580 int
  581 shmctl(td, uap)
  582         struct thread *td;
  583         struct shmctl_args *uap;
  584 {
  585         int error = 0;
  586         struct shmid_ds buf;
  587         size_t bufsz;
  588         
  589         /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
  590         if (uap->cmd == IPC_SET) {
  591                 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
  592                         goto done;
  593         }
  594         
  595         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz, 0);
  596         if (error)
  597                 goto done;
  598         
  599         /* Cases in which we need to copyout */
  600         switch (uap->cmd) {
  601         case IPC_INFO:
  602         case SHM_INFO:
  603         case SHM_STAT:
  604         case IPC_STAT:
  605                 error = copyout(&buf, uap->buf, bufsz);
  606                 break;
  607         }
  608 
  609 done:
  610         if (error) {
  611                 /* Invalidate the return value */
  612                 td->td_retval[0] = -1;
  613         }
  614         return (error);
  615 }
  616 
  617 
  618 #ifndef _SYS_SYSPROTO_H_
  619 struct shmget_args {
  620         key_t key;
  621         size_t size;
  622         int shmflg;
  623 };
  624 #endif
  625 
  626 static int
  627 shmget_existing(td, uap, mode, segnum)
  628         struct thread *td;
  629         struct shmget_args *uap;
  630         int mode;
  631         int segnum;
  632 {
  633         struct shmid_ds *shmseg;
  634         int error;
  635 
  636         shmseg = &shmsegs[segnum];
  637         if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
  638                 /*
  639                  * This segment is in the process of being allocated.  Wait
  640                  * until it's done, and look the key up again (in case the
  641                  * allocation failed or it was freed).
  642                  */
  643                 shmseg->shm_perm.mode |= SHMSEG_WANTED;
  644                 error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
  645                 if (error)
  646                         return (error);
  647                 return (EAGAIN);
  648         }
  649         if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
  650                 return (EEXIST);
  651         error = ipcperm(td, &shmseg->shm_perm, mode);
  652         if (error)
  653                 return (error);
  654         if (uap->size && uap->size > shmseg->shm_segsz)
  655                 return (EINVAL);
  656         td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
  657         return (0);
  658 }
  659 
  660 static int
  661 shmget_allocate_segment(td, uap, mode)
  662         struct thread *td;
  663         struct shmget_args *uap;
  664         int mode;
  665 {
  666         int i, segnum, shmid, size;
  667         struct ucred *cred = td->td_ucred;
  668         struct shmid_ds *shmseg;
  669         struct shm_handle *shm_handle;
  670 
  671         GIANT_REQUIRED;
  672 
  673         if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
  674                 return (EINVAL);
  675         if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
  676                 return (ENOSPC);
  677         size = round_page(uap->size);
  678         if (shm_committed + btoc(size) > shminfo.shmall)
  679                 return (ENOMEM);
  680         if (shm_last_free < 0) {
  681                 shmrealloc();   /* Maybe expand the shmsegs[] array. */
  682                 for (i = 0; i < shmalloced; i++)
  683                         if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
  684                                 break;
  685                 if (i == shmalloced)
  686                         return (ENOSPC);
  687                 segnum = i;
  688         } else  {
  689                 segnum = shm_last_free;
  690                 shm_last_free = -1;
  691         }
  692         shmseg = &shmsegs[segnum];
  693         /*
  694          * In case we sleep in malloc(), mark the segment present but deleted
  695          * so that noone else tries to create the same key.
  696          */
  697         shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
  698         shmseg->shm_perm.key = uap->key;
  699         shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
  700         shm_handle = (struct shm_handle *)
  701             malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
  702         shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
  703         
  704         /*
  705          * We make sure that we have allocated a pager before we need
  706          * to.
  707          */
  708         if (shm_use_phys) {
  709                 shm_handle->shm_object =
  710                     vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
  711         } else {
  712                 shm_handle->shm_object =
  713                     vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
  714         }
  715         VM_OBJECT_LOCK(shm_handle->shm_object);
  716         vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
  717         vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
  718         VM_OBJECT_UNLOCK(shm_handle->shm_object);
  719 
  720         shmseg->shm_internal = shm_handle;
  721         shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
  722         shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
  723         shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
  724             (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
  725         shmseg->shm_segsz = uap->size;
  726         shmseg->shm_cpid = td->td_proc->p_pid;
  727         shmseg->shm_lpid = shmseg->shm_nattch = 0;
  728         shmseg->shm_atime = shmseg->shm_dtime = 0;
  729         shmseg->shm_ctime = time_second;
  730         shm_committed += btoc(size);
  731         shm_nused++;
  732         if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
  733                 /*
  734                  * Somebody else wanted this key while we were asleep.  Wake
  735                  * them up now.
  736                  */
  737                 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
  738                 wakeup(shmseg);
  739         }
  740         td->td_retval[0] = shmid;
  741         return (0);
  742 }
  743 
  744 /*
  745  * MPSAFE
  746  */
  747 int
  748 shmget(td, uap)
  749         struct thread *td;
  750         struct shmget_args *uap;
  751 {
  752         int segnum, mode;
  753         int error;
  754 
  755         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  756                 return (ENOSYS);
  757         mtx_lock(&Giant);
  758         mode = uap->shmflg & ACCESSPERMS;
  759         if (uap->key != IPC_PRIVATE) {
  760         again:
  761                 segnum = shm_find_segment_by_key(uap->key);
  762                 if (segnum >= 0) {
  763                         error = shmget_existing(td, uap, mode, segnum);
  764                         if (error == EAGAIN)
  765                                 goto again;
  766                         goto done2;
  767                 }
  768                 if ((uap->shmflg & IPC_CREAT) == 0) {
  769                         error = ENOENT;
  770                         goto done2;
  771                 }
  772         }
  773         error = shmget_allocate_segment(td, uap, mode);
  774 done2:
  775         mtx_unlock(&Giant);
  776         return (error);
  777 }
  778 
  779 /*
  780  * MPSAFE
  781  */
  782 int
  783 shmsys(td, uap)
  784         struct thread *td;
  785         /* XXX actually varargs. */
  786         struct shmsys_args /* {
  787                 u_int   which;
  788                 int     a2;
  789                 int     a3;
  790                 int     a4;
  791         } */ *uap;
  792 {
  793         int error;
  794 
  795         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  796                 return (ENOSYS);
  797         if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
  798                 return (EINVAL);
  799         mtx_lock(&Giant);
  800         error = (*shmcalls[uap->which])(td, &uap->a2);
  801         mtx_unlock(&Giant);
  802         return (error);
  803 }
  804 
  805 static void
  806 shmfork_myhook(p1, p2)
  807         struct proc *p1, *p2;
  808 {
  809         struct shmmap_state *shmmap_s;
  810         size_t size;
  811         int i;
  812 
  813         size = shminfo.shmseg * sizeof(struct shmmap_state);
  814         shmmap_s = malloc(size, M_SHM, M_WAITOK);
  815         bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
  816         p2->p_vmspace->vm_shm = shmmap_s;
  817         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
  818                 if (shmmap_s->shmid != -1)
  819                         shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
  820 }
  821 
  822 static void
  823 shmexit_myhook(struct vmspace *vm)
  824 {
  825         struct shmmap_state *base, *shm;
  826         int i;
  827 
  828         GIANT_REQUIRED;
  829 
  830         if ((base = vm->vm_shm) != NULL) {
  831                 vm->vm_shm = NULL;
  832                 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
  833                         if (shm->shmid != -1)
  834                                 shm_delete_mapping(vm, shm);
  835                 }
  836                 free(base, M_SHM);
  837         }
  838 }
  839 
  840 static void
  841 shmrealloc(void)
  842 {
  843         int i;
  844         struct shmid_ds *newsegs;
  845 
  846         if (shmalloced >= shminfo.shmmni)
  847                 return;
  848 
  849         newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
  850         if (newsegs == NULL)
  851                 return;
  852         for (i = 0; i < shmalloced; i++)
  853                 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
  854         for (; i < shminfo.shmmni; i++) {
  855                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
  856                 shmsegs[i].shm_perm.seq = 0;
  857         }
  858         free(shmsegs, M_SHM);
  859         shmsegs = newsegs;
  860         shmalloced = shminfo.shmmni;
  861 }
  862 
  863 static void
  864 shminit()
  865 {
  866         int i;
  867 
  868         TUNABLE_INT_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
  869         for (i = PAGE_SIZE; i > 0; i--) {
  870                 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
  871                 if (shminfo.shmmax >= shminfo.shmall)
  872                         break;
  873         }
  874         TUNABLE_INT_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
  875         TUNABLE_INT_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
  876         TUNABLE_INT_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
  877         TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
  878 
  879         shmalloced = shminfo.shmmni;
  880         shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
  881         if (shmsegs == NULL)
  882                 panic("cannot allocate initial memory for sysvshm");
  883         for (i = 0; i < shmalloced; i++) {
  884                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
  885                 shmsegs[i].shm_perm.seq = 0;
  886         }
  887         shm_last_free = 0;
  888         shm_nused = 0;
  889         shm_committed = 0;
  890         shmexit_hook = &shmexit_myhook;
  891         shmfork_hook = &shmfork_myhook;
  892 }
  893 
  894 static int
  895 shmunload()
  896 {
  897 
  898         if (shm_nused > 0)
  899                 return (EBUSY);
  900 
  901         free(shmsegs, M_SHM);
  902         shmexit_hook = NULL;
  903         shmfork_hook = NULL;
  904         return (0);
  905 }
  906 
  907 static int
  908 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
  909 {
  910 
  911         return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
  912 }
  913 
  914 static int
  915 sysvshm_modload(struct module *module, int cmd, void *arg)
  916 {
  917         int error = 0;
  918 
  919         switch (cmd) {
  920         case MOD_LOAD:
  921                 shminit();
  922                 break;
  923         case MOD_UNLOAD:
  924                 error = shmunload();
  925                 break;
  926         case MOD_SHUTDOWN:
  927                 break;
  928         default:
  929                 error = EINVAL;
  930                 break;
  931         }
  932         return (error);
  933 }
  934 
  935 static moduledata_t sysvshm_mod = {
  936         "sysvshm",
  937         &sysvshm_modload,
  938         NULL
  939 };
  940 
  941 SYSCALL_MODULE_HELPER(shmsys);
  942 SYSCALL_MODULE_HELPER(shmat);
  943 SYSCALL_MODULE_HELPER(shmctl);
  944 SYSCALL_MODULE_HELPER(shmdt);
  945 SYSCALL_MODULE_HELPER(shmget);
  946 
  947 DECLARE_MODULE(sysvshm, sysvshm_mod,
  948         SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
  949 MODULE_VERSION(sysvshm, 1);

Cache object: 0e006a490f78e7283b67b85bb4d030cb


[ 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.