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  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  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 /*      $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $      */
    2 /*-
    3  * Copyright (c) 1994 Adam Glass and Charles Hannum.  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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by Adam Glass and Charles
   16  *      Hannum.
   17  * 4. The names of the authors may not be used to endorse or promote products
   18  *    derived from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
   21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 /*-
   32  * Copyright (c) 2003-2005 McAfee, Inc.
   33  * All rights reserved.
   34  *
   35  * This software was developed for the FreeBSD Project in part by McAfee
   36  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
   37  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
   38  * program.
   39  *
   40  * Redistribution and use in source and binary forms, with or without
   41  * modification, are permitted provided that the following conditions
   42  * are met:
   43  * 1. Redistributions of source code must retain the above copyright
   44  *    notice, this list of conditions and the following disclaimer.
   45  * 2. Redistributions in binary form must reproduce the above copyright
   46  *    notice, this list of conditions and the following disclaimer in the
   47  *    documentation and/or other materials provided with the distribution.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  */
   61 
   62 #include <sys/cdefs.h>
   63 __FBSDID("$FreeBSD: stable/10/sys/kern/sysv_shm.c 329741 2018-02-21 18:32:57Z brooks $");
   64 
   65 #include "opt_compat.h"
   66 #include "opt_sysvipc.h"
   67 
   68 #include <sys/param.h>
   69 #include <sys/systm.h>
   70 #include <sys/kernel.h>
   71 #include <sys/limits.h>
   72 #include <sys/lock.h>
   73 #include <sys/sysctl.h>
   74 #include <sys/shm.h>
   75 #include <sys/proc.h>
   76 #include <sys/malloc.h>
   77 #include <sys/mman.h>
   78 #include <sys/module.h>
   79 #include <sys/mutex.h>
   80 #include <sys/racct.h>
   81 #include <sys/resourcevar.h>
   82 #include <sys/rwlock.h>
   83 #include <sys/stat.h>
   84 #include <sys/syscall.h>
   85 #include <sys/syscallsubr.h>
   86 #include <sys/sysent.h>
   87 #include <sys/sysproto.h>
   88 #include <sys/jail.h>
   89 
   90 #include <security/mac/mac_framework.h>
   91 
   92 #include <vm/vm.h>
   93 #include <vm/vm_param.h>
   94 #include <vm/pmap.h>
   95 #include <vm/vm_object.h>
   96 #include <vm/vm_map.h>
   97 #include <vm/vm_page.h>
   98 #include <vm/vm_pager.h>
   99 
  100 FEATURE(sysv_shm, "System V shared memory segments support");
  101 
  102 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
  103 
  104 static int shmget_allocate_segment(struct thread *td,
  105     struct shmget_args *uap, int mode);
  106 static int shmget_existing(struct thread *td, struct shmget_args *uap,
  107     int mode, int segnum);
  108 
  109 #define SHMSEG_FREE             0x0200
  110 #define SHMSEG_REMOVED          0x0400
  111 #define SHMSEG_ALLOCATED        0x0800
  112 
  113 static int shm_last_free, shm_nused, shmalloced;
  114 vm_size_t shm_committed;
  115 static struct shmid_kernel *shmsegs;
  116 static unsigned shm_prison_slot;
  117 
  118 struct shmmap_state {
  119         vm_offset_t va;
  120         int shmid;
  121 };
  122 
  123 static void shm_deallocate_segment(struct shmid_kernel *);
  124 static int shm_find_segment_by_key(struct prison *, key_t);
  125 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
  126 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
  127 static void shmrealloc(void);
  128 static int shminit(void);
  129 static int sysvshm_modload(struct module *, int, void *);
  130 static int shmunload(void);
  131 static void shmexit_myhook(struct vmspace *vm);
  132 static void shmfork_myhook(struct proc *p1, struct proc *p2);
  133 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
  134 static void shm_remove(struct shmid_kernel *, int);
  135 static struct prison *shm_find_prison(struct ucred *);
  136 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
  137 static int shm_prison_check(void *, void *);
  138 static int shm_prison_set(void *, void *);
  139 static int shm_prison_get(void *, void *);
  140 static int shm_prison_remove(void *, void *);
  141 static void shm_prison_cleanup(struct prison *);
  142 
  143 /*
  144  * Tuneable values.
  145  */
  146 #ifndef SHMMAXPGS
  147 #define SHMMAXPGS       131072  /* Note: sysv shared memory is swap backed. */
  148 #endif
  149 #ifndef SHMMAX
  150 #define SHMMAX  (SHMMAXPGS*PAGE_SIZE)
  151 #endif
  152 #ifndef SHMMIN
  153 #define SHMMIN  1
  154 #endif
  155 #ifndef SHMMNI
  156 #define SHMMNI  192
  157 #endif
  158 #ifndef SHMSEG
  159 #define SHMSEG  128
  160 #endif
  161 #ifndef SHMALL
  162 #define SHMALL  (SHMMAXPGS)
  163 #endif
  164 
  165 struct  shminfo shminfo = {
  166         SHMMAX,
  167         SHMMIN,
  168         SHMMNI,
  169         SHMSEG,
  170         SHMALL
  171 };
  172 
  173 static int shm_use_phys;
  174 static int shm_allow_removed;
  175 
  176 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
  177     "Maximum shared memory segment size");
  178 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
  179     "Minimum shared memory segment size");
  180 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
  181     "Number of shared memory identifiers");
  182 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
  183     "Number of segments per process");
  184 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
  185     "Maximum number of pages available for shared memory");
  186 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
  187     &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
  188 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
  189     &shm_allow_removed, 0,
  190     "Enable/Disable attachment to attached segments marked for removal");
  191 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
  192     CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
  193     "Array of struct shmid_kernel for each potential shared memory segment");
  194 
  195 static struct sx sysvshmsx;
  196 #define SYSVSHM_LOCK()          sx_xlock(&sysvshmsx)
  197 #define SYSVSHM_UNLOCK()        sx_xunlock(&sysvshmsx)
  198 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
  199 
  200 static int
  201 shm_find_segment_by_key(struct prison *pr, key_t key)
  202 {
  203         int i;
  204 
  205         for (i = 0; i < shmalloced; i++)
  206                 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
  207                     shmsegs[i].cred != NULL &&
  208                     shmsegs[i].cred->cr_prison == pr &&
  209                     shmsegs[i].u.shm_perm.key == key)
  210                         return (i);
  211         return (-1);
  212 }
  213 
  214 /*
  215  * Finds segment either by shmid if is_shmid is true, or by segnum if
  216  * is_shmid is false.
  217  */
  218 static struct shmid_kernel *
  219 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
  220 {
  221         struct shmid_kernel *shmseg;
  222         int segnum;
  223 
  224         segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
  225         if (segnum < 0 || segnum >= shmalloced)
  226                 return (NULL);
  227         shmseg = &shmsegs[segnum];
  228         if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
  229             (!shm_allow_removed &&
  230              (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
  231             (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
  232             shm_prison_cansee(rpr, shmseg) != 0)
  233                 return (NULL);
  234         return (shmseg);
  235 }
  236 
  237 static void
  238 shm_deallocate_segment(struct shmid_kernel *shmseg)
  239 {
  240         vm_size_t size;
  241 
  242         SYSVSHM_ASSERT_LOCKED();
  243 
  244         vm_object_deallocate(shmseg->object);
  245         shmseg->object = NULL;
  246         size = round_page(shmseg->u.shm_segsz);
  247         shm_committed -= btoc(size);
  248         shm_nused--;
  249         shmseg->u.shm_perm.mode = SHMSEG_FREE;
  250 #ifdef MAC
  251         mac_sysvshm_cleanup(shmseg);
  252 #endif
  253         racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
  254         racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
  255         crfree(shmseg->cred);
  256         shmseg->cred = NULL;
  257 }
  258 
  259 static int
  260 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
  261 {
  262         struct shmid_kernel *shmseg;
  263         int segnum, result;
  264         vm_size_t size;
  265 
  266         SYSVSHM_ASSERT_LOCKED();
  267         segnum = IPCID_TO_IX(shmmap_s->shmid);
  268         KASSERT(segnum >= 0 && segnum < shmalloced,
  269             ("segnum %d shmalloced %d", segnum, shmalloced));
  270 
  271         shmseg = &shmsegs[segnum];
  272         size = round_page(shmseg->u.shm_segsz);
  273         result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
  274         if (result != KERN_SUCCESS)
  275                 return (EINVAL);
  276         shmmap_s->shmid = -1;
  277         shmseg->u.shm_dtime = time_second;
  278         if ((--shmseg->u.shm_nattch <= 0) &&
  279             (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
  280                 shm_deallocate_segment(shmseg);
  281                 shm_last_free = segnum;
  282         }
  283         return (0);
  284 }
  285 
  286 static void
  287 shm_remove(struct shmid_kernel *shmseg, int segnum)
  288 {
  289 
  290         shmseg->u.shm_perm.key = IPC_PRIVATE;
  291         shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
  292         if (shmseg->u.shm_nattch <= 0) {
  293                 shm_deallocate_segment(shmseg);
  294                 shm_last_free = segnum;
  295         }
  296 }
  297 
  298 static struct prison *
  299 shm_find_prison(struct ucred *cred)
  300 {
  301         struct prison *pr, *rpr;
  302 
  303         pr = cred->cr_prison;
  304         prison_lock(pr);
  305         rpr = osd_jail_get(pr, shm_prison_slot);
  306         prison_unlock(pr);
  307         return rpr;
  308 }
  309 
  310 static int
  311 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
  312 {
  313 
  314         if (shmseg->cred == NULL ||
  315             !(rpr == shmseg->cred->cr_prison ||
  316               prison_ischild(rpr, shmseg->cred->cr_prison)))
  317                 return (EINVAL);
  318         return (0);
  319 }
  320 
  321 static int
  322 kern_shmdt_locked(struct thread *td, const void *shmaddr)
  323 {
  324         struct proc *p = td->td_proc;
  325         struct shmmap_state *shmmap_s;
  326 #ifdef MAC
  327         struct shmid_kernel *shmsegptr;
  328 #endif
  329         int error, i;
  330 
  331         SYSVSHM_ASSERT_LOCKED();
  332         if (shm_find_prison(td->td_ucred) == NULL)
  333                 return (ENOSYS);
  334         shmmap_s = p->p_vmspace->vm_shm;
  335         if (shmmap_s == NULL)
  336                 return (EINVAL);
  337         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
  338                 if (shmmap_s->shmid != -1 &&
  339                     shmmap_s->va == (vm_offset_t)shmaddr) {
  340                         break;
  341                 }
  342         }
  343         if (i == shminfo.shmseg)
  344                 return (EINVAL);
  345 #ifdef MAC
  346         shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
  347         error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
  348         if (error != 0)
  349                 return (error);
  350 #endif
  351         error = shm_delete_mapping(p->p_vmspace, shmmap_s);
  352         return (error);
  353 }
  354 
  355 #ifndef _SYS_SYSPROTO_H_
  356 struct shmdt_args {
  357         const void *shmaddr;
  358 };
  359 #endif
  360 int
  361 sys_shmdt(struct thread *td, struct shmdt_args *uap)
  362 {
  363         int error;
  364 
  365         SYSVSHM_LOCK();
  366         error = kern_shmdt_locked(td, uap->shmaddr);
  367         SYSVSHM_UNLOCK();
  368         return (error);
  369 }
  370 
  371 static int
  372 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
  373     int shmflg)
  374 {
  375         struct prison *rpr;
  376         struct proc *p = td->td_proc;
  377         struct shmid_kernel *shmseg;
  378         struct shmmap_state *shmmap_s;
  379         vm_offset_t attach_va;
  380         vm_prot_t prot;
  381         vm_size_t size;
  382         int error, i, rv;
  383 
  384         SYSVSHM_ASSERT_LOCKED();
  385         rpr = shm_find_prison(td->td_ucred);
  386         if (rpr == NULL)
  387                 return (ENOSYS);
  388         shmmap_s = p->p_vmspace->vm_shm;
  389         if (shmmap_s == NULL) {
  390                 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
  391                     M_SHM, M_WAITOK);
  392                 for (i = 0; i < shminfo.shmseg; i++)
  393                         shmmap_s[i].shmid = -1;
  394                 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
  395                 p->p_vmspace->vm_shm = shmmap_s;
  396         }
  397         shmseg = shm_find_segment(rpr, shmid, true);
  398         if (shmseg == NULL)
  399                 return (EINVAL);
  400         error = ipcperm(td, &shmseg->u.shm_perm,
  401             (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
  402         if (error != 0)
  403                 return (error);
  404 #ifdef MAC
  405         error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
  406         if (error != 0)
  407                 return (error);
  408 #endif
  409         for (i = 0; i < shminfo.shmseg; i++) {
  410                 if (shmmap_s->shmid == -1)
  411                         break;
  412                 shmmap_s++;
  413         }
  414         if (i >= shminfo.shmseg)
  415                 return (EMFILE);
  416         size = round_page(shmseg->u.shm_segsz);
  417         prot = VM_PROT_READ;
  418         if ((shmflg & SHM_RDONLY) == 0)
  419                 prot |= VM_PROT_WRITE;
  420         if (shmaddr != NULL) {
  421                 if ((shmflg & SHM_RND) != 0)
  422                         attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
  423                 else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
  424                         attach_va = (vm_offset_t)shmaddr;
  425                 else
  426                         return (EINVAL);
  427         } else {
  428                 /*
  429                  * This is just a hint to vm_map_find() about where to
  430                  * put it.
  431                  */
  432                 PROC_LOCK(p);
  433                 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
  434                     lim_max(p, RLIMIT_DATA));
  435                 PROC_UNLOCK(p);
  436         }
  437 
  438         vm_object_reference(shmseg->object);
  439         rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object,
  440             0, &attach_va, size, 0, shmaddr != NULL ? VMFS_NO_SPACE :
  441             VMFS_OPTIMAL_SPACE, prot, prot, MAP_INHERIT_SHARE);
  442         if (rv != KERN_SUCCESS) {
  443                 vm_object_deallocate(shmseg->object);
  444                 return (ENOMEM);
  445         }
  446 
  447         shmmap_s->va = attach_va;
  448         shmmap_s->shmid = shmid;
  449         shmseg->u.shm_lpid = p->p_pid;
  450         shmseg->u.shm_atime = time_second;
  451         shmseg->u.shm_nattch++;
  452         td->td_retval[0] = attach_va;
  453         return (error);
  454 }
  455 
  456 int
  457 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
  458 {
  459         int error;
  460 
  461         SYSVSHM_LOCK();
  462         error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
  463         SYSVSHM_UNLOCK();
  464         return (error);
  465 }
  466 
  467 #ifndef _SYS_SYSPROTO_H_
  468 struct shmat_args {
  469         int shmid;
  470         const void *shmaddr;
  471         int shmflg;
  472 };
  473 #endif
  474 int
  475 sys_shmat(struct thread *td, struct shmat_args *uap)
  476 {
  477 
  478         return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
  479 }
  480 
  481 static int
  482 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
  483     size_t *bufsz)
  484 {
  485         struct prison *rpr;
  486         struct shmid_kernel *shmseg;
  487         struct shmid_ds *shmidp;
  488         struct shm_info shm_info;
  489         int error;
  490 
  491         SYSVSHM_ASSERT_LOCKED();
  492 
  493         rpr = shm_find_prison(td->td_ucred);
  494         if (rpr == NULL)
  495                 return (ENOSYS);
  496 
  497         error = 0;
  498         switch (cmd) {
  499         /*
  500          * It is possible that kern_shmctl is being called from the Linux ABI
  501          * layer, in which case, we will need to implement IPC_INFO.  It should
  502          * be noted that other shmctl calls will be funneled through here for
  503          * Linix binaries as well.
  504          *
  505          * NB: The Linux ABI layer will convert this data to structure(s) more
  506          * consistent with the Linux ABI.
  507          */
  508         case IPC_INFO:
  509                 memcpy(buf, &shminfo, sizeof(shminfo));
  510                 if (bufsz)
  511                         *bufsz = sizeof(shminfo);
  512                 td->td_retval[0] = shmalloced;
  513                 return (0);
  514         case SHM_INFO: {
  515                 shm_info.used_ids = shm_nused;
  516                 shm_info.shm_rss = 0;   /*XXX where to get from ? */
  517                 shm_info.shm_tot = 0;   /*XXX where to get from ? */
  518                 shm_info.shm_swp = 0;   /*XXX where to get from ? */
  519                 shm_info.swap_attempts = 0;     /*XXX where to get from ? */
  520                 shm_info.swap_successes = 0;    /*XXX where to get from ? */
  521                 memcpy(buf, &shm_info, sizeof(shm_info));
  522                 if (bufsz != NULL)
  523                         *bufsz = sizeof(shm_info);
  524                 td->td_retval[0] = shmalloced;
  525                 return (0);
  526         }
  527         }
  528         shmseg = shm_find_segment(rpr, shmid, cmd != SHM_STAT);
  529         if (shmseg == NULL)
  530                 return (EINVAL);
  531 #ifdef MAC
  532         error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
  533         if (error != 0)
  534                 return (error);
  535 #endif
  536         switch (cmd) {
  537         case SHM_STAT:
  538         case IPC_STAT:
  539                 shmidp = (struct shmid_ds *)buf;
  540                 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
  541                 if (error != 0)
  542                         return (error);
  543                 memcpy(shmidp, &shmseg->u, sizeof(struct shmid_ds));
  544                 if (td->td_ucred->cr_prison != shmseg->cred->cr_prison)
  545                         shmidp->shm_perm.key = IPC_PRIVATE;
  546                 if (bufsz != NULL)
  547                         *bufsz = sizeof(struct shmid_ds);
  548                 if (cmd == SHM_STAT) {
  549                         td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
  550                             shmseg->u.shm_perm);
  551                 }
  552                 break;
  553         case IPC_SET:
  554                 shmidp = (struct shmid_ds *)buf;
  555                 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
  556                 if (error != 0)
  557                         return (error);
  558                 shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
  559                 shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
  560                 shmseg->u.shm_perm.mode =
  561                     (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
  562                     (shmidp->shm_perm.mode & ACCESSPERMS);
  563                 shmseg->u.shm_ctime = time_second;
  564                 break;
  565         case IPC_RMID:
  566                 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
  567                 if (error != 0)
  568                         return (error);
  569                 shm_remove(shmseg, IPCID_TO_IX(shmid));
  570                 break;
  571 #if 0
  572         case SHM_LOCK:
  573         case SHM_UNLOCK:
  574 #endif
  575         default:
  576                 error = EINVAL;
  577                 break;
  578         }
  579         return (error);
  580 }
  581 
  582 int
  583 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
  584 {
  585         int error;
  586 
  587         SYSVSHM_LOCK();
  588         error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
  589         SYSVSHM_UNLOCK();
  590         return (error);
  591 }
  592 
  593 
  594 #ifndef _SYS_SYSPROTO_H_
  595 struct shmctl_args {
  596         int shmid;
  597         int cmd;
  598         struct shmid_ds *buf;
  599 };
  600 #endif
  601 int
  602 sys_shmctl(struct thread *td, struct shmctl_args *uap)
  603 {
  604         int error = 0;
  605         struct shmid_ds buf;
  606         size_t bufsz;
  607         
  608         /*
  609          * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
  610          * Linux binaries.  If we see the call come through the FreeBSD ABI,
  611          * return an error back to the user since we do not to support this.
  612          */
  613         if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
  614             uap->cmd == SHM_STAT)
  615                 return (EINVAL);
  616 
  617         /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
  618         if (uap->cmd == IPC_SET) {
  619                 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
  620                         goto done;
  621         }
  622         
  623         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
  624         if (error)
  625                 goto done;
  626         
  627         /* Cases in which we need to copyout */
  628         switch (uap->cmd) {
  629         case IPC_STAT:
  630                 error = copyout(&buf, uap->buf, bufsz);
  631                 break;
  632         }
  633 
  634 done:
  635         if (error) {
  636                 /* Invalidate the return value */
  637                 td->td_retval[0] = -1;
  638         }
  639         return (error);
  640 }
  641 
  642 
  643 static int
  644 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
  645     int segnum)
  646 {
  647         struct shmid_kernel *shmseg;
  648 #ifdef MAC
  649         int error;
  650 #endif
  651 
  652         SYSVSHM_ASSERT_LOCKED();
  653         KASSERT(segnum >= 0 && segnum < shmalloced,
  654             ("segnum %d shmalloced %d", segnum, shmalloced));
  655         shmseg = &shmsegs[segnum];
  656         if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
  657                 return (EEXIST);
  658 #ifdef MAC
  659         error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
  660         if (error != 0)
  661                 return (error);
  662 #endif
  663         if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
  664                 return (EINVAL);
  665         td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
  666         return (0);
  667 }
  668 
  669 static int
  670 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
  671 {
  672         struct ucred *cred = td->td_ucred;
  673         struct shmid_kernel *shmseg;
  674         vm_object_t shm_object;
  675         int i, segnum;
  676         size_t size;
  677 
  678         SYSVSHM_ASSERT_LOCKED();
  679 
  680         if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
  681                 return (EINVAL);
  682         if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
  683                 return (ENOSPC);
  684         size = round_page(uap->size);
  685         if (shm_committed + btoc(size) > shminfo.shmall)
  686                 return (ENOMEM);
  687         if (shm_last_free < 0) {
  688                 shmrealloc();   /* Maybe expand the shmsegs[] array. */
  689                 for (i = 0; i < shmalloced; i++)
  690                         if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
  691                                 break;
  692                 if (i == shmalloced)
  693                         return (ENOSPC);
  694                 segnum = i;
  695         } else  {
  696                 segnum = shm_last_free;
  697                 shm_last_free = -1;
  698         }
  699         KASSERT(segnum >= 0 && segnum < shmalloced,
  700             ("segnum %d shmalloced %d", segnum, shmalloced));
  701         shmseg = &shmsegs[segnum];
  702 #ifdef RACCT
  703         if (racct_enable) {
  704                 PROC_LOCK(td->td_proc);
  705                 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
  706                         PROC_UNLOCK(td->td_proc);
  707                         return (ENOSPC);
  708                 }
  709                 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
  710                         racct_sub(td->td_proc, RACCT_NSHM, 1);
  711                         PROC_UNLOCK(td->td_proc);
  712                         return (ENOMEM);
  713                 }
  714                 PROC_UNLOCK(td->td_proc);
  715         }
  716 #endif
  717 
  718         /*
  719          * We make sure that we have allocated a pager before we need
  720          * to.
  721          */
  722         shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
  723             0, size, VM_PROT_DEFAULT, 0, cred);
  724         if (shm_object == NULL) {
  725 #ifdef RACCT
  726                 if (racct_enable) {
  727                         PROC_LOCK(td->td_proc);
  728                         racct_sub(td->td_proc, RACCT_NSHM, 1);
  729                         racct_sub(td->td_proc, RACCT_SHMSIZE, size);
  730                         PROC_UNLOCK(td->td_proc);
  731                 }
  732 #endif
  733                 return (ENOMEM);
  734         }
  735         shm_object->pg_color = 0;
  736         VM_OBJECT_WLOCK(shm_object);
  737         vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
  738         vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
  739         VM_OBJECT_WUNLOCK(shm_object);
  740 
  741         shmseg->object = shm_object;
  742         shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
  743         shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
  744         shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
  745         shmseg->u.shm_perm.key = uap->key;
  746         shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
  747         shmseg->cred = crhold(cred);
  748         shmseg->u.shm_segsz = uap->size;
  749         shmseg->u.shm_cpid = td->td_proc->p_pid;
  750         shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
  751         shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
  752 #ifdef MAC
  753         mac_sysvshm_create(cred, shmseg);
  754 #endif
  755         shmseg->u.shm_ctime = time_second;
  756         shm_committed += btoc(size);
  757         shm_nused++;
  758         td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
  759 
  760         return (0);
  761 }
  762 
  763 #ifndef _SYS_SYSPROTO_H_
  764 struct shmget_args {
  765         key_t key;
  766         size_t size;
  767         int shmflg;
  768 };
  769 #endif
  770 int
  771 sys_shmget(struct thread *td, struct shmget_args *uap)
  772 {
  773         int segnum, mode;
  774         int error;
  775 
  776         if (shm_find_prison(td->td_ucred) == NULL)
  777                 return (ENOSYS);
  778         mode = uap->shmflg & ACCESSPERMS;
  779         SYSVSHM_LOCK();
  780         if (uap->key == IPC_PRIVATE) {
  781                 error = shmget_allocate_segment(td, uap, mode);
  782         } else {
  783                 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
  784                     uap->key);
  785                 if (segnum >= 0)
  786                         error = shmget_existing(td, uap, mode, segnum);
  787                 else if ((uap->shmflg & IPC_CREAT) == 0)
  788                         error = ENOENT;
  789                 else
  790                         error = shmget_allocate_segment(td, uap, mode);
  791         }
  792         SYSVSHM_UNLOCK();
  793         return (error);
  794 }
  795 
  796 static void
  797 shmfork_myhook(struct proc *p1, struct proc *p2)
  798 {
  799         struct shmmap_state *shmmap_s;
  800         size_t size;
  801         int i;
  802 
  803         SYSVSHM_LOCK();
  804         size = shminfo.shmseg * sizeof(struct shmmap_state);
  805         shmmap_s = malloc(size, M_SHM, M_WAITOK);
  806         bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
  807         p2->p_vmspace->vm_shm = shmmap_s;
  808         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
  809                 if (shmmap_s->shmid != -1) {
  810                         KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
  811                             IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
  812                             ("segnum %d shmalloced %d",
  813                             IPCID_TO_IX(shmmap_s->shmid), shmalloced));
  814                         shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
  815                 }
  816         }
  817         SYSVSHM_UNLOCK();
  818 }
  819 
  820 static void
  821 shmexit_myhook(struct vmspace *vm)
  822 {
  823         struct shmmap_state *base, *shm;
  824         int i;
  825 
  826         base = vm->vm_shm;
  827         if (base != NULL) {
  828                 vm->vm_shm = NULL;
  829                 SYSVSHM_LOCK();
  830                 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
  831                         if (shm->shmid != -1)
  832                                 shm_delete_mapping(vm, shm);
  833                 }
  834                 SYSVSHM_UNLOCK();
  835                 free(base, M_SHM);
  836         }
  837 }
  838 
  839 static void
  840 shmrealloc(void)
  841 {
  842         struct shmid_kernel *newsegs;
  843         int i;
  844 
  845         SYSVSHM_ASSERT_LOCKED();
  846 
  847         if (shmalloced >= shminfo.shmmni)
  848                 return;
  849 
  850         newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
  851         for (i = 0; i < shmalloced; i++)
  852                 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
  853         for (; i < shminfo.shmmni; i++) {
  854                 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
  855                 shmsegs[i].u.shm_perm.seq = 0;
  856 #ifdef MAC
  857                 mac_sysvshm_init(&shmsegs[i]);
  858 #endif
  859         }
  860         free(shmsegs, M_SHM);
  861         shmsegs = newsegs;
  862         shmalloced = shminfo.shmmni;
  863 }
  864 
  865 static struct syscall_helper_data shm_syscalls[] = {
  866         SYSCALL_INIT_HELPER(shmat),
  867         SYSCALL_INIT_HELPER(shmctl),
  868         SYSCALL_INIT_HELPER(shmdt),
  869         SYSCALL_INIT_HELPER(shmget),
  870 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  871     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  872         SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
  873 #endif
  874 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
  875         SYSCALL_INIT_HELPER(shmsys),
  876 #endif
  877         SYSCALL_INIT_LAST
  878 };
  879 
  880 #ifdef COMPAT_FREEBSD32
  881 #include <compat/freebsd32/freebsd32.h>
  882 #include <compat/freebsd32/freebsd32_ipc.h>
  883 #include <compat/freebsd32/freebsd32_proto.h>
  884 #include <compat/freebsd32/freebsd32_signal.h>
  885 #include <compat/freebsd32/freebsd32_syscall.h>
  886 #include <compat/freebsd32/freebsd32_util.h>
  887 
  888 static struct syscall_helper_data shm32_syscalls[] = {
  889         SYSCALL32_INIT_HELPER_COMPAT(shmat),
  890         SYSCALL32_INIT_HELPER_COMPAT(shmdt),
  891         SYSCALL32_INIT_HELPER_COMPAT(shmget),
  892         SYSCALL32_INIT_HELPER(freebsd32_shmsys),
  893         SYSCALL32_INIT_HELPER(freebsd32_shmctl),
  894 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  895     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  896         SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
  897 #endif
  898         SYSCALL_INIT_LAST
  899 };
  900 #endif
  901 
  902 static int
  903 shminit(void)
  904 {
  905         struct prison *pr;
  906         void *rsv;
  907         int i, error;
  908         osd_method_t methods[PR_MAXMETHOD] = {
  909             [PR_METHOD_CHECK] =         shm_prison_check,
  910             [PR_METHOD_SET] =           shm_prison_set,
  911             [PR_METHOD_GET] =           shm_prison_get,
  912             [PR_METHOD_REMOVE] =        shm_prison_remove,
  913         };
  914 
  915 #ifndef BURN_BRIDGES
  916         if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
  917                 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
  918 #endif
  919         TUNABLE_ULONG_FETCH("kern.ipc.shmall", &shminfo.shmall);
  920         if (!TUNABLE_ULONG_FETCH("kern.ipc.shmmax", &shminfo.shmmax)) {
  921                 /* Initialize shmmax dealing with possible overflow. */
  922                 for (i = PAGE_SIZE; i > 0; i--) {
  923                         shminfo.shmmax = shminfo.shmall * i;
  924                         if (shminfo.shmmax >= shminfo.shmall)
  925                                 break;
  926                 }
  927         }
  928         TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
  929         TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
  930         TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
  931         TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
  932 
  933         shmalloced = shminfo.shmmni;
  934         shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
  935         for (i = 0; i < shmalloced; i++) {
  936                 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
  937                 shmsegs[i].u.shm_perm.seq = 0;
  938 #ifdef MAC
  939                 mac_sysvshm_init(&shmsegs[i]);
  940 #endif
  941         }
  942         shm_last_free = 0;
  943         shm_nused = 0;
  944         shm_committed = 0;
  945         sx_init(&sysvshmsx, "sysvshmsx");
  946         shmexit_hook = &shmexit_myhook;
  947         shmfork_hook = &shmfork_myhook;
  948 
  949         /* Set current prisons according to their allow.sysvipc. */
  950         shm_prison_slot = osd_jail_register(NULL, methods);
  951         rsv = osd_reserve(shm_prison_slot);
  952         prison_lock(&prison0);
  953         (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
  954         prison_unlock(&prison0);
  955         rsv = NULL;
  956         sx_slock(&allprison_lock);
  957         TAILQ_FOREACH(pr, &allprison, pr_list) {
  958                 if (rsv == NULL)
  959                         rsv = osd_reserve(shm_prison_slot);
  960                 prison_lock(pr);
  961                 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
  962                         (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
  963                             &prison0);
  964                         rsv = NULL;
  965                 }
  966                 prison_unlock(pr);
  967         }
  968         if (rsv != NULL)
  969                 osd_free_reserved(rsv);
  970         sx_sunlock(&allprison_lock);
  971 
  972         error = syscall_helper_register(shm_syscalls);
  973         if (error != 0)
  974                 return (error);
  975 #ifdef COMPAT_FREEBSD32
  976         error = syscall32_helper_register(shm32_syscalls);
  977         if (error != 0)
  978                 return (error);
  979 #endif
  980         return (0);
  981 }
  982 
  983 static int
  984 shmunload(void)
  985 {
  986         int i;  
  987 
  988         if (shm_nused > 0)
  989                 return (EBUSY);
  990 
  991 #ifdef COMPAT_FREEBSD32
  992         syscall32_helper_unregister(shm32_syscalls);
  993 #endif
  994         syscall_helper_unregister(shm_syscalls);
  995         if (shm_prison_slot != 0)
  996                 osd_jail_deregister(shm_prison_slot);
  997 
  998         for (i = 0; i < shmalloced; i++) {
  999 #ifdef MAC
 1000                 mac_sysvshm_destroy(&shmsegs[i]);
 1001 #endif
 1002                 /*
 1003                  * Objects might be still mapped into the processes
 1004                  * address spaces.  Actual free would happen on the
 1005                  * last mapping destruction.
 1006                  */
 1007                 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
 1008                         vm_object_deallocate(shmsegs[i].object);
 1009         }
 1010         free(shmsegs, M_SHM);
 1011         shmexit_hook = NULL;
 1012         shmfork_hook = NULL;
 1013         sx_destroy(&sysvshmsx);
 1014         return (0);
 1015 }
 1016 
 1017 static int
 1018 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
 1019 {
 1020         struct shmid_kernel tshmseg;
 1021         struct prison *pr, *rpr;
 1022         int error, i;
 1023 
 1024         SYSVSHM_LOCK();
 1025         pr = req->td->td_ucred->cr_prison;
 1026         rpr = shm_find_prison(req->td->td_ucred);
 1027         error = 0;
 1028         for (i = 0; i < shmalloced; i++) {
 1029                 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
 1030                     rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
 1031                         bzero(&tshmseg, sizeof(tshmseg));
 1032                         tshmseg.u.shm_perm.mode = SHMSEG_FREE;
 1033                 } else {
 1034                         tshmseg = shmsegs[i];
 1035                         if (tshmseg.cred->cr_prison != pr)
 1036                                 tshmseg.u.shm_perm.key = IPC_PRIVATE;
 1037                 }
 1038                 error = SYSCTL_OUT(req, &tshmseg, sizeof(tshmseg));
 1039                 if (error != 0)
 1040                         break;
 1041         }
 1042         SYSVSHM_UNLOCK();
 1043         return (error);
 1044 }
 1045 
 1046 static int
 1047 shm_prison_check(void *obj, void *data)
 1048 {
 1049         struct prison *pr = obj;
 1050         struct prison *prpr;
 1051         struct vfsoptlist *opts = data;
 1052         int error, jsys;
 1053 
 1054         /*
 1055          * sysvshm is a jailsys integer.
 1056          * It must be "disable" if the parent jail is disabled.
 1057          */
 1058         error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
 1059         if (error != ENOENT) {
 1060                 if (error != 0)
 1061                         return (error);
 1062                 switch (jsys) {
 1063                 case JAIL_SYS_DISABLE:
 1064                         break;
 1065                 case JAIL_SYS_NEW:
 1066                 case JAIL_SYS_INHERIT:
 1067                         prison_lock(pr->pr_parent);
 1068                         prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
 1069                         prison_unlock(pr->pr_parent);
 1070                         if (prpr == NULL)
 1071                                 return (EPERM);
 1072                         break;
 1073                 default:
 1074                         return (EINVAL);
 1075                 }
 1076         }
 1077 
 1078         return (0);
 1079 }
 1080 
 1081 static int
 1082 shm_prison_set(void *obj, void *data)
 1083 {
 1084         struct prison *pr = obj;
 1085         struct prison *tpr, *orpr, *nrpr, *trpr;
 1086         struct vfsoptlist *opts = data;
 1087         void *rsv;
 1088         int jsys, descend;
 1089 
 1090         /*
 1091          * sysvshm controls which jail is the root of the associated segments
 1092          * (this jail or same as the parent), or if the feature is available
 1093          * at all.
 1094          */
 1095         if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
 1096                 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
 1097                     ? JAIL_SYS_INHERIT
 1098                     : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
 1099                     ? JAIL_SYS_DISABLE
 1100                     : -1;
 1101         if (jsys == JAIL_SYS_DISABLE) {
 1102                 prison_lock(pr);
 1103                 orpr = osd_jail_get(pr, shm_prison_slot);
 1104                 if (orpr != NULL)
 1105                         osd_jail_del(pr, shm_prison_slot);
 1106                 prison_unlock(pr);
 1107                 if (orpr != NULL) {
 1108                         if (orpr == pr)
 1109                                 shm_prison_cleanup(pr);
 1110                         /* Disable all child jails as well. */
 1111                         FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
 1112                                 prison_lock(tpr);
 1113                                 trpr = osd_jail_get(tpr, shm_prison_slot);
 1114                                 if (trpr != NULL) {
 1115                                         osd_jail_del(tpr, shm_prison_slot);
 1116                                         prison_unlock(tpr);
 1117                                         if (trpr == tpr)
 1118                                                 shm_prison_cleanup(tpr);
 1119                                 } else {
 1120                                         prison_unlock(tpr);
 1121                                         descend = 0;
 1122                                 }
 1123                         }
 1124                 }
 1125         } else if (jsys != -1) {
 1126                 if (jsys == JAIL_SYS_NEW)
 1127                         nrpr = pr;
 1128                 else {
 1129                         prison_lock(pr->pr_parent);
 1130                         nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
 1131                         prison_unlock(pr->pr_parent);
 1132                 }
 1133                 rsv = osd_reserve(shm_prison_slot);
 1134                 prison_lock(pr);
 1135                 orpr = osd_jail_get(pr, shm_prison_slot);
 1136                 if (orpr != nrpr)
 1137                         (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
 1138                             nrpr);
 1139                 else
 1140                         osd_free_reserved(rsv);
 1141                 prison_unlock(pr);
 1142                 if (orpr != nrpr) {
 1143                         if (orpr == pr)
 1144                                 shm_prison_cleanup(pr);
 1145                         if (orpr != NULL) {
 1146                                 /* Change child jails matching the old root, */
 1147                                 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
 1148                                         prison_lock(tpr);
 1149                                         trpr = osd_jail_get(tpr,
 1150                                             shm_prison_slot);
 1151                                         if (trpr == orpr) {
 1152                                                 (void)osd_jail_set(tpr,
 1153                                                     shm_prison_slot, nrpr);
 1154                                                 prison_unlock(tpr);
 1155                                                 if (trpr == tpr)
 1156                                                         shm_prison_cleanup(tpr);
 1157                                         } else {
 1158                                                 prison_unlock(tpr);
 1159                                                 descend = 0;
 1160                                         }
 1161                                 }
 1162                         }
 1163                 }
 1164         }
 1165 
 1166         return (0);
 1167 }
 1168 
 1169 static int
 1170 shm_prison_get(void *obj, void *data)
 1171 {
 1172         struct prison *pr = obj;
 1173         struct prison *rpr;
 1174         struct vfsoptlist *opts = data;
 1175         int error, jsys;
 1176 
 1177         /* Set sysvshm based on the jail's root prison. */
 1178         prison_lock(pr);
 1179         rpr = osd_jail_get(pr, shm_prison_slot);
 1180         prison_unlock(pr);
 1181         jsys = rpr == NULL ? JAIL_SYS_DISABLE
 1182             : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
 1183         error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
 1184         if (error == ENOENT)
 1185                 error = 0;
 1186         return (error);
 1187 }
 1188 
 1189 static int
 1190 shm_prison_remove(void *obj, void *data __unused)
 1191 {
 1192         struct prison *pr = obj;
 1193         struct prison *rpr;
 1194 
 1195         SYSVSHM_LOCK();
 1196         prison_lock(pr);
 1197         rpr = osd_jail_get(pr, shm_prison_slot);
 1198         prison_unlock(pr);
 1199         if (rpr == pr)
 1200                 shm_prison_cleanup(pr);
 1201         SYSVSHM_UNLOCK();
 1202         return (0);
 1203 }
 1204 
 1205 static void
 1206 shm_prison_cleanup(struct prison *pr)
 1207 {
 1208         struct shmid_kernel *shmseg;
 1209         int i;
 1210 
 1211         /* Remove any segments that belong to this jail. */
 1212         for (i = 0; i < shmalloced; i++) {
 1213                 shmseg = &shmsegs[i];
 1214                 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
 1215                     shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
 1216                         shm_remove(shmseg, i);
 1217                 }
 1218         }
 1219 }
 1220 
 1221 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
 1222 
 1223 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
 1224 struct oshmid_ds {
 1225         struct  ipc_perm_old shm_perm;  /* operation perms */
 1226         int     shm_segsz;              /* size of segment (bytes) */
 1227         u_short shm_cpid;               /* pid, creator */
 1228         u_short shm_lpid;               /* pid, last operation */
 1229         short   shm_nattch;             /* no. of current attaches */
 1230         time_t  shm_atime;              /* last attach time */
 1231         time_t  shm_dtime;              /* last detach time */
 1232         time_t  shm_ctime;              /* last change time */
 1233         void    *shm_handle;            /* internal handle for shm segment */
 1234 };
 1235 
 1236 struct oshmctl_args {
 1237         int shmid;
 1238         int cmd;
 1239         struct oshmid_ds *ubuf;
 1240 };
 1241 
 1242 static int
 1243 oshmctl(struct thread *td, struct oshmctl_args *uap)
 1244 {
 1245 #ifdef COMPAT_43
 1246         int error = 0;
 1247         struct prison *rpr;
 1248         struct shmid_kernel *shmseg;
 1249         struct oshmid_ds outbuf;
 1250 
 1251         rpr = shm_find_prison(td->td_ucred);
 1252         if (rpr == NULL)
 1253                 return (ENOSYS);
 1254         if (uap->cmd != IPC_STAT) {
 1255                 return (freebsd7_shmctl(td,
 1256                     (struct freebsd7_shmctl_args *)uap));
 1257         }
 1258         SYSVSHM_LOCK();
 1259         shmseg = shm_find_segment(rpr, uap->shmid, true);
 1260         if (shmseg == NULL) {
 1261                 SYSVSHM_UNLOCK();
 1262                 return (EINVAL);
 1263         }
 1264         error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
 1265         if (error != 0) {
 1266                 SYSVSHM_UNLOCK();
 1267                 return (error);
 1268         }
 1269 #ifdef MAC
 1270         error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
 1271         if (error != 0) {
 1272                 SYSVSHM_UNLOCK();
 1273                 return (error);
 1274         }
 1275 #endif
 1276         ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
 1277         outbuf.shm_segsz = shmseg->u.shm_segsz;
 1278         outbuf.shm_cpid = shmseg->u.shm_cpid;
 1279         outbuf.shm_lpid = shmseg->u.shm_lpid;
 1280         outbuf.shm_nattch = shmseg->u.shm_nattch;
 1281         outbuf.shm_atime = shmseg->u.shm_atime;
 1282         outbuf.shm_dtime = shmseg->u.shm_dtime;
 1283         outbuf.shm_ctime = shmseg->u.shm_ctime;
 1284         outbuf.shm_handle = shmseg->object;
 1285         SYSVSHM_UNLOCK();
 1286         error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
 1287         return (error);
 1288 #else
 1289         return (EINVAL);
 1290 #endif
 1291 }
 1292 
 1293 /* XXX casting to (sy_call_t *) is bogus, as usual. */
 1294 static sy_call_t *shmcalls[] = {
 1295         (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
 1296         (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
 1297         (sy_call_t *)freebsd7_shmctl
 1298 };
 1299 
 1300 #ifndef _SYS_SYSPROTO_H_
 1301 /* XXX actually varargs. */
 1302 struct shmsys_args {
 1303         int     which;
 1304         int     a2;
 1305         int     a3;
 1306         int     a4;
 1307 };
 1308 #endif
 1309 int
 1310 sys_shmsys(struct thread *td, struct shmsys_args *uap)
 1311 {
 1312         int error;
 1313 
 1314         if (uap->which < 0 || uap->which >= nitems(shmcalls))
 1315                 return (EINVAL);
 1316         error = (*shmcalls[uap->which])(td, &uap->a2);
 1317         return (error);
 1318 }
 1319 
 1320 #endif  /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
 1321 
 1322 #ifdef COMPAT_FREEBSD32
 1323 
 1324 int
 1325 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
 1326 {
 1327 
 1328 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1329     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1330         switch (uap->which) {
 1331         case 0: {       /* shmat */
 1332                 struct shmat_args ap;
 1333 
 1334                 ap.shmid = uap->a2;
 1335                 ap.shmaddr = PTRIN(uap->a3);
 1336                 ap.shmflg = uap->a4;
 1337                 return (sysent[SYS_shmat].sy_call(td, &ap));
 1338         }
 1339         case 2: {       /* shmdt */
 1340                 struct shmdt_args ap;
 1341 
 1342                 ap.shmaddr = PTRIN(uap->a2);
 1343                 return (sysent[SYS_shmdt].sy_call(td, &ap));
 1344         }
 1345         case 3: {       /* shmget */
 1346                 struct shmget_args ap;
 1347 
 1348                 ap.key = uap->a2;
 1349                 ap.size = uap->a3;
 1350                 ap.shmflg = uap->a4;
 1351                 return (sysent[SYS_shmget].sy_call(td, &ap));
 1352         }
 1353         case 4: {       /* shmctl */
 1354                 struct freebsd7_freebsd32_shmctl_args ap;
 1355 
 1356                 ap.shmid = uap->a2;
 1357                 ap.cmd = uap->a3;
 1358                 ap.buf = PTRIN(uap->a4);
 1359                 return (freebsd7_freebsd32_shmctl(td, &ap));
 1360         }
 1361         case 1:         /* oshmctl */
 1362         default:
 1363                 return (EINVAL);
 1364         }
 1365 #else
 1366         return (nosys(td, NULL));
 1367 #endif
 1368 }
 1369 
 1370 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1371     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1372 int
 1373 freebsd7_freebsd32_shmctl(struct thread *td,
 1374     struct freebsd7_freebsd32_shmctl_args *uap)
 1375 {
 1376         int error = 0;
 1377         union {
 1378                 struct shmid_ds shmid_ds;
 1379                 struct shm_info shm_info;
 1380                 struct shminfo shminfo;
 1381         } u;
 1382         union {
 1383                 struct shmid_ds32_old shmid_ds32;
 1384                 struct shm_info32 shm_info32;
 1385                 struct shminfo32 shminfo32;
 1386         } u32;
 1387         size_t sz;
 1388 
 1389         if (uap->cmd == IPC_SET) {
 1390                 if ((error = copyin(uap->buf, &u32.shmid_ds32,
 1391                     sizeof(u32.shmid_ds32))))
 1392                         goto done;
 1393                 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
 1394                     &u.shmid_ds.shm_perm);
 1395                 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
 1396                 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
 1397                 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
 1398                 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
 1399                 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
 1400                 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
 1401                 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
 1402         }
 1403         
 1404         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
 1405         if (error)
 1406                 goto done;
 1407         
 1408         /* Cases in which we need to copyout */
 1409         switch (uap->cmd) {
 1410         case IPC_INFO:
 1411                 CP(u.shminfo, u32.shminfo32, shmmax);
 1412                 CP(u.shminfo, u32.shminfo32, shmmin);
 1413                 CP(u.shminfo, u32.shminfo32, shmmni);
 1414                 CP(u.shminfo, u32.shminfo32, shmseg);
 1415                 CP(u.shminfo, u32.shminfo32, shmall);
 1416                 error = copyout(&u32.shminfo32, uap->buf,
 1417                     sizeof(u32.shminfo32));
 1418                 break;
 1419         case SHM_INFO:
 1420                 CP(u.shm_info, u32.shm_info32, used_ids);
 1421                 CP(u.shm_info, u32.shm_info32, shm_rss);
 1422                 CP(u.shm_info, u32.shm_info32, shm_tot);
 1423                 CP(u.shm_info, u32.shm_info32, shm_swp);
 1424                 CP(u.shm_info, u32.shm_info32, swap_attempts);
 1425                 CP(u.shm_info, u32.shm_info32, swap_successes);
 1426                 error = copyout(&u32.shm_info32, uap->buf,
 1427                     sizeof(u32.shm_info32));
 1428                 break;
 1429         case SHM_STAT:
 1430         case IPC_STAT:
 1431                 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
 1432                     &u32.shmid_ds32.shm_perm);
 1433                 if (u.shmid_ds.shm_segsz > INT32_MAX)
 1434                         u32.shmid_ds32.shm_segsz = INT32_MAX;
 1435                 else
 1436                         CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
 1437                 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
 1438                 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
 1439                 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
 1440                 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
 1441                 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
 1442                 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
 1443                 u32.shmid_ds32.shm_internal = 0;
 1444                 error = copyout(&u32.shmid_ds32, uap->buf,
 1445                     sizeof(u32.shmid_ds32));
 1446                 break;
 1447         }
 1448 
 1449 done:
 1450         if (error) {
 1451                 /* Invalidate the return value */
 1452                 td->td_retval[0] = -1;
 1453         }
 1454         return (error);
 1455 }
 1456 #endif
 1457 
 1458 int
 1459 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
 1460 {
 1461         int error = 0;
 1462         union {
 1463                 struct shmid_ds shmid_ds;
 1464                 struct shm_info shm_info;
 1465                 struct shminfo shminfo;
 1466         } u;
 1467         union {
 1468                 struct shmid_ds32 shmid_ds32;
 1469                 struct shm_info32 shm_info32;
 1470                 struct shminfo32 shminfo32;
 1471         } u32;
 1472         size_t sz;
 1473         
 1474         if (uap->cmd == IPC_SET) {
 1475                 if ((error = copyin(uap->buf, &u32.shmid_ds32,
 1476                     sizeof(u32.shmid_ds32))))
 1477                         goto done;
 1478                 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
 1479                     &u.shmid_ds.shm_perm);
 1480                 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
 1481                 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
 1482                 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
 1483                 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
 1484                 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
 1485                 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
 1486                 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
 1487         }
 1488         
 1489         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
 1490         if (error)
 1491                 goto done;
 1492         
 1493         /* Cases in which we need to copyout */
 1494         switch (uap->cmd) {
 1495         case IPC_INFO:
 1496                 CP(u.shminfo, u32.shminfo32, shmmax);
 1497                 CP(u.shminfo, u32.shminfo32, shmmin);
 1498                 CP(u.shminfo, u32.shminfo32, shmmni);
 1499                 CP(u.shminfo, u32.shminfo32, shmseg);
 1500                 CP(u.shminfo, u32.shminfo32, shmall);
 1501                 error = copyout(&u32.shminfo32, uap->buf,
 1502                     sizeof(u32.shminfo32));
 1503                 break;
 1504         case SHM_INFO:
 1505                 CP(u.shm_info, u32.shm_info32, used_ids);
 1506                 CP(u.shm_info, u32.shm_info32, shm_rss);
 1507                 CP(u.shm_info, u32.shm_info32, shm_tot);
 1508                 CP(u.shm_info, u32.shm_info32, shm_swp);
 1509                 CP(u.shm_info, u32.shm_info32, swap_attempts);
 1510                 CP(u.shm_info, u32.shm_info32, swap_successes);
 1511                 error = copyout(&u32.shm_info32, uap->buf,
 1512                     sizeof(u32.shm_info32));
 1513                 break;
 1514         case SHM_STAT:
 1515         case IPC_STAT:
 1516                 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
 1517                     &u32.shmid_ds32.shm_perm);
 1518                 if (u.shmid_ds.shm_segsz > INT32_MAX)
 1519                         u32.shmid_ds32.shm_segsz = INT32_MAX;
 1520                 else
 1521                         CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
 1522                 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
 1523                 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
 1524                 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
 1525                 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
 1526                 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
 1527                 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
 1528                 error = copyout(&u32.shmid_ds32, uap->buf,
 1529                     sizeof(u32.shmid_ds32));
 1530                 break;
 1531         }
 1532 
 1533 done:
 1534         if (error) {
 1535                 /* Invalidate the return value */
 1536                 td->td_retval[0] = -1;
 1537         }
 1538         return (error);
 1539 }
 1540 #endif
 1541 
 1542 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1543     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1544 
 1545 #ifndef CP
 1546 #define CP(src, dst, fld)       do { (dst).fld = (src).fld; } while (0)
 1547 #endif
 1548 
 1549 #ifndef _SYS_SYSPROTO_H_
 1550 struct freebsd7_shmctl_args {
 1551         int shmid;
 1552         int cmd;
 1553         struct shmid_ds_old *buf;
 1554 };
 1555 #endif
 1556 int
 1557 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
 1558 {
 1559         int error = 0;
 1560         struct shmid_ds_old old;
 1561         struct shmid_ds buf;
 1562         size_t bufsz;
 1563         
 1564         /*
 1565          * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
 1566          * Linux binaries.  If we see the call come through the FreeBSD ABI,
 1567          * return an error back to the user since we do not to support this.
 1568          */
 1569         if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
 1570             uap->cmd == SHM_STAT)
 1571                 return (EINVAL);
 1572 
 1573         /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
 1574         if (uap->cmd == IPC_SET) {
 1575                 if ((error = copyin(uap->buf, &old, sizeof(old))))
 1576                         goto done;
 1577                 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
 1578                 CP(old, buf, shm_segsz);
 1579                 CP(old, buf, shm_lpid);
 1580                 CP(old, buf, shm_cpid);
 1581                 CP(old, buf, shm_nattch);
 1582                 CP(old, buf, shm_atime);
 1583                 CP(old, buf, shm_dtime);
 1584                 CP(old, buf, shm_ctime);
 1585         }
 1586         
 1587         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
 1588         if (error)
 1589                 goto done;
 1590 
 1591         /* Cases in which we need to copyout */
 1592         switch (uap->cmd) {
 1593         case IPC_STAT:
 1594                 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
 1595                 if (buf.shm_segsz > INT_MAX)
 1596                         old.shm_segsz = INT_MAX;
 1597                 else
 1598                         CP(buf, old, shm_segsz);
 1599                 CP(buf, old, shm_lpid);
 1600                 CP(buf, old, shm_cpid);
 1601                 if (buf.shm_nattch > SHRT_MAX)
 1602                         old.shm_nattch = SHRT_MAX;
 1603                 else
 1604                         CP(buf, old, shm_nattch);
 1605                 CP(buf, old, shm_atime);
 1606                 CP(buf, old, shm_dtime);
 1607                 CP(buf, old, shm_ctime);
 1608                 old.shm_internal = NULL;
 1609                 error = copyout(&old, uap->buf, sizeof(old));
 1610                 break;
 1611         }
 1612 
 1613 done:
 1614         if (error) {
 1615                 /* Invalidate the return value */
 1616                 td->td_retval[0] = -1;
 1617         }
 1618         return (error);
 1619 }
 1620 
 1621 #endif  /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
 1622            COMPAT_FREEBSD7 */
 1623 
 1624 static int
 1625 sysvshm_modload(struct module *module, int cmd, void *arg)
 1626 {
 1627         int error = 0;
 1628 
 1629         switch (cmd) {
 1630         case MOD_LOAD:
 1631                 error = shminit();
 1632                 if (error != 0)
 1633                         shmunload();
 1634                 break;
 1635         case MOD_UNLOAD:
 1636                 error = shmunload();
 1637                 break;
 1638         case MOD_SHUTDOWN:
 1639                 break;
 1640         default:
 1641                 error = EINVAL;
 1642                 break;
 1643         }
 1644         return (error);
 1645 }
 1646 
 1647 static moduledata_t sysvshm_mod = {
 1648         "sysvshm",
 1649         &sysvshm_modload,
 1650         NULL
 1651 };
 1652 
 1653 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
 1654 MODULE_VERSION(sysvshm, 1);

Cache object: a36c45a099666d58a606c8c566e43fa8


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