Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.59
      1  1.59   thorpej /*	$NetBSD: sysv_shm.c,v 1.59 2000/09/13 15:00:25 thorpej Exp $	*/
      2  1.52   thorpej 
      3  1.52   thorpej /*-
      4  1.52   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.52   thorpej  * All rights reserved.
      6  1.52   thorpej  *
      7  1.52   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.52   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.52   thorpej  * NASA Ames Research Center.
     10  1.52   thorpej  *
     11  1.52   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.52   thorpej  * modification, are permitted provided that the following conditions
     13  1.52   thorpej  * are met:
     14  1.52   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.52   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.52   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.52   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.52   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.52   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.52   thorpej  *    must display the following acknowledgement:
     21  1.52   thorpej  *	This product includes software developed by the NetBSD
     22  1.52   thorpej  *	Foundation, Inc. and its contributors.
     23  1.52   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.52   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.52   thorpej  *    from this software without specific prior written permission.
     26  1.52   thorpej  *
     27  1.52   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.52   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.52   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.52   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.52   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.52   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.52   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.52   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.52   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.52   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.52   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.52   thorpej  */
     39  1.22       cgd 
     40  1.11   hpeyerl /*
     41  1.48   mycroft  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
     42  1.11   hpeyerl  *
     43  1.11   hpeyerl  * Redistribution and use in source and binary forms, with or without
     44  1.11   hpeyerl  * modification, are permitted provided that the following conditions
     45  1.11   hpeyerl  * are met:
     46  1.11   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     47  1.11   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     48  1.17   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.17   mycroft  *    notice, this list of conditions and the following disclaimer in the
     50  1.17   mycroft  *    documentation and/or other materials provided with the distribution.
     51  1.17   mycroft  * 3. All advertising materials mentioning features or use of this software
     52  1.17   mycroft  *    must display the following acknowledgement:
     53  1.48   mycroft  *	This product includes software developed by Adam Glass and Charles M.
     54  1.17   mycroft  *	Hannum.
     55  1.17   mycroft  * 4. The names of the authors may not be used to endorse or promote products
     56  1.11   hpeyerl  *    derived from this software without specific prior written permission.
     57  1.11   hpeyerl  *
     58  1.17   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     59  1.17   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  1.17   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  1.17   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  1.17   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  1.17   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  1.17   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  1.17   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  1.17   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  1.17   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  1.11   hpeyerl  */
     69  1.43       mrg 
     70  1.50      tron #define SYSVSHM
     71  1.11   hpeyerl 
     72  1.11   hpeyerl #include <sys/types.h>
     73  1.11   hpeyerl #include <sys/param.h>
     74  1.11   hpeyerl #include <sys/kernel.h>
     75  1.11   hpeyerl #include <sys/shm.h>
     76  1.11   hpeyerl #include <sys/malloc.h>
     77  1.11   hpeyerl #include <sys/mman.h>
     78  1.12   mycroft #include <sys/stat.h>
     79  1.56    simonb #include <sys/sysctl.h>
     80  1.56    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     81  1.56    simonb #include <sys/syscallargs.h>
     82  1.35  christos 
     83  1.35  christos struct shmid_ds *shm_find_segment_by_shmid __P((int));
     84  1.35  christos 
     85  1.11   hpeyerl /*
     86  1.11   hpeyerl  * Provides the following externally accessible functions:
     87  1.11   hpeyerl  *
     88  1.41   thorpej  * shminit(void);		                 initialization
     89  1.41   thorpej  * shmexit(struct vmspace *)                     cleanup
     90  1.41   thorpej  * shmfork(struct vmspace *, struct vmspace *)   fork handling
     91  1.11   hpeyerl  *
     92  1.11   hpeyerl  * Structures:
     93  1.11   hpeyerl  * shmsegs (an array of 'struct shmid_ds')
     94  1.11   hpeyerl  * per proc array of 'struct shmmap_state'
     95  1.11   hpeyerl  */
     96  1.12   mycroft 
     97  1.16   mycroft #define	SHMSEG_FREE     	0x0200
     98  1.16   mycroft #define	SHMSEG_REMOVED  	0x0400
     99  1.16   mycroft #define	SHMSEG_ALLOCATED	0x0800
    100  1.16   mycroft #define	SHMSEG_WANTED		0x1000
    101  1.11   hpeyerl 
    102  1.55    simonb int	shm_last_free, shm_nused, shm_committed;
    103  1.55    simonb struct	shmid_ds *shmsegs;
    104  1.11   hpeyerl 
    105  1.11   hpeyerl struct shm_handle {
    106  1.42       mrg 	struct uvm_object *shm_object;
    107  1.11   hpeyerl };
    108  1.11   hpeyerl 
    109  1.11   hpeyerl struct shmmap_state {
    110  1.47       eeh 	vaddr_t va;
    111  1.11   hpeyerl 	int shmid;
    112  1.11   hpeyerl };
    113  1.11   hpeyerl 
    114  1.35  christos static int shm_find_segment_by_key __P((key_t));
    115  1.12   mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
    116  1.41   thorpej static int shm_delete_mapping __P((struct vmspace *, struct shmmap_state *));
    117  1.35  christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
    118  1.35  christos 				int, int, register_t *));
    119  1.35  christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
    120  1.35  christos 					int, register_t *));
    121  1.11   hpeyerl 
    122  1.12   mycroft static int
    123  1.12   mycroft shm_find_segment_by_key(key)
    124  1.11   hpeyerl 	key_t key;
    125  1.11   hpeyerl {
    126  1.11   hpeyerl 	int i;
    127  1.11   hpeyerl 
    128  1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    129  1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    130  1.52   thorpej 		    shmsegs[i].shm_perm._key == key)
    131  1.12   mycroft 			return i;
    132  1.11   hpeyerl 	return -1;
    133  1.11   hpeyerl }
    134  1.11   hpeyerl 
    135  1.28  christos struct shmid_ds *
    136  1.15   mycroft shm_find_segment_by_shmid(shmid)
    137  1.11   hpeyerl 	int shmid;
    138  1.11   hpeyerl {
    139  1.11   hpeyerl 	int segnum;
    140  1.11   hpeyerl 	struct shmid_ds *shmseg;
    141  1.11   hpeyerl 
    142  1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    143  1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    144  1.11   hpeyerl 		return NULL;
    145  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    146  1.12   mycroft 	if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
    147  1.12   mycroft 	    != SHMSEG_ALLOCATED ||
    148  1.52   thorpej 	    shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
    149  1.11   hpeyerl 		return NULL;
    150  1.11   hpeyerl 	return shmseg;
    151  1.11   hpeyerl }
    152  1.11   hpeyerl 
    153  1.12   mycroft static void
    154  1.12   mycroft shm_deallocate_segment(shmseg)
    155  1.12   mycroft 	struct shmid_ds *shmseg;
    156  1.12   mycroft {
    157  1.12   mycroft 	struct shm_handle *shm_handle;
    158  1.14   mycroft 	size_t size;
    159  1.12   mycroft 
    160  1.52   thorpej 	shm_handle = shmseg->_shm_internal;
    161  1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    162  1.42       mrg 	uao_detach(shm_handle->shm_object);
    163  1.12   mycroft 	free((caddr_t)shm_handle, M_SHM);
    164  1.52   thorpej 	shmseg->_shm_internal = NULL;
    165  1.14   mycroft 	shm_committed -= btoc(size);
    166  1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    167  1.25   mycroft 	shm_nused--;
    168  1.12   mycroft }
    169  1.12   mycroft 
    170  1.12   mycroft static int
    171  1.41   thorpej shm_delete_mapping(vm, shmmap_s)
    172  1.41   thorpej 	struct vmspace *vm;
    173  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    174  1.11   hpeyerl {
    175  1.12   mycroft 	struct shmid_ds *shmseg;
    176  1.12   mycroft 	int segnum, result;
    177  1.14   mycroft 	size_t size;
    178  1.11   hpeyerl 
    179  1.12   mycroft 	segnum = IPCID_TO_IX(shmmap_s->shmid);
    180  1.12   mycroft 	shmseg = &shmsegs[segnum];
    181  1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    182  1.45   thorpej 	result = uvm_deallocate(&vm->vm_map, shmmap_s->va, size);
    183  1.11   hpeyerl 	if (result != KERN_SUCCESS)
    184  1.11   hpeyerl 		return EINVAL;
    185  1.12   mycroft 	shmmap_s->shmid = -1;
    186  1.11   hpeyerl 	shmseg->shm_dtime = time.tv_sec;
    187  1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    188  1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    189  1.12   mycroft 		shm_deallocate_segment(shmseg);
    190  1.12   mycroft 		shm_last_free = segnum;
    191  1.11   hpeyerl 	}
    192  1.11   hpeyerl 	return 0;
    193  1.11   hpeyerl }
    194  1.11   hpeyerl 
    195  1.12   mycroft int
    196  1.33   mycroft sys_shmdt(p, v, retval)
    197  1.11   hpeyerl 	struct proc *p;
    198  1.32   thorpej 	void *v;
    199  1.32   thorpej 	register_t *retval;
    200  1.32   thorpej {
    201  1.33   mycroft 	struct sys_shmdt_args /* {
    202  1.44    kleink 		syscallarg(const void *) shmaddr;
    203  1.32   thorpej 	} */ *uap = v;
    204  1.12   mycroft 	struct shmmap_state *shmmap_s;
    205  1.11   hpeyerl 	int i;
    206  1.11   hpeyerl 
    207  1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    208  1.38  christos 	if (shmmap_s == NULL)
    209  1.38  christos 		return EINVAL;
    210  1.38  christos 
    211  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    212  1.12   mycroft 		if (shmmap_s->shmid != -1 &&
    213  1.47       eeh 		    shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr))
    214  1.12   mycroft 			break;
    215  1.11   hpeyerl 	if (i == shminfo.shmseg)
    216  1.11   hpeyerl 		return EINVAL;
    217  1.41   thorpej 	return shm_delete_mapping(p->p_vmspace, shmmap_s);
    218  1.11   hpeyerl }
    219  1.11   hpeyerl 
    220  1.12   mycroft int
    221  1.33   mycroft sys_shmat(p, v, retval)
    222  1.11   hpeyerl 	struct proc *p;
    223  1.32   thorpej 	void *v;
    224  1.32   thorpej 	register_t *retval;
    225  1.32   thorpej {
    226  1.33   mycroft 	struct sys_shmat_args /* {
    227  1.26       cgd 		syscallarg(int) shmid;
    228  1.44    kleink 		syscallarg(const void *) shmaddr;
    229  1.35  christos 		syscallarg(int) shmflg;
    230  1.32   thorpej 	} */ *uap = v;
    231  1.12   mycroft 	int error, i, flags;
    232  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    233  1.11   hpeyerl 	struct shmid_ds *shmseg;
    234  1.11   hpeyerl 	struct shmmap_state *shmmap_s = NULL;
    235  1.39  drochner 	struct shm_handle *shm_handle;
    236  1.47       eeh 	vaddr_t attach_va;
    237  1.11   hpeyerl 	vm_prot_t prot;
    238  1.47       eeh 	vsize_t size;
    239  1.39  drochner 	int rv;
    240  1.11   hpeyerl 
    241  1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    242  1.12   mycroft 	if (shmmap_s == NULL) {
    243  1.12   mycroft 		size = shminfo.shmseg * sizeof(struct shmmap_state);
    244  1.12   mycroft 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
    245  1.18       cgd 		for (i = 0; i < shminfo.shmseg; i++)
    246  1.18       cgd 			shmmap_s[i].shmid = -1;
    247  1.12   mycroft 		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    248  1.12   mycroft 	}
    249  1.26       cgd 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    250  1.11   hpeyerl 	if (shmseg == NULL)
    251  1.11   hpeyerl 		return EINVAL;
    252  1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    253  1.35  christos 		    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    254  1.35  christos 	if (error)
    255  1.12   mycroft 		return error;
    256  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++) {
    257  1.12   mycroft 		if (shmmap_s->shmid == -1)
    258  1.12   mycroft 			break;
    259  1.12   mycroft 		shmmap_s++;
    260  1.11   hpeyerl 	}
    261  1.12   mycroft 	if (i >= shminfo.shmseg)
    262  1.12   mycroft 		return EMFILE;
    263  1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    264  1.12   mycroft 	prot = VM_PROT_READ;
    265  1.26       cgd 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    266  1.12   mycroft 		prot |= VM_PROT_WRITE;
    267  1.12   mycroft 	flags = MAP_ANON | MAP_SHARED;
    268  1.26       cgd 	if (SCARG(uap, shmaddr)) {
    269  1.12   mycroft 		flags |= MAP_FIXED;
    270  1.26       cgd 		if (SCARG(uap, shmflg) & SHM_RND)
    271  1.26       cgd 			attach_va =
    272  1.47       eeh 			    (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    273  1.47       eeh 		else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    274  1.47       eeh 			attach_va = (vaddr_t)SCARG(uap, shmaddr);
    275  1.11   hpeyerl 		else
    276  1.14   mycroft 			return EINVAL;
    277  1.12   mycroft 	} else {
    278  1.20   mycroft 		/* This is just a hint to vm_mmap() about where to put it. */
    279  1.31       cgd 		attach_va =
    280  1.54    kleink 		    round_page((vaddr_t)p->p_vmspace->vm_taddr +
    281  1.54    kleink 			MAXTSIZ + MAXDSIZ);
    282  1.11   hpeyerl 	}
    283  1.52   thorpej 	shm_handle = shmseg->_shm_internal;
    284  1.42       mrg 	uao_reference(shm_handle->shm_object);
    285  1.42       mrg 	rv = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
    286  1.59   thorpej 		     shm_handle->shm_object, 0, 0,
    287  1.42       mrg 		     UVM_MAPFLAG(prot, prot, UVM_INH_SHARE,
    288  1.42       mrg 				 UVM_ADV_RANDOM, 0));
    289  1.42       mrg 	if (rv != KERN_SUCCESS) {
    290  1.42       mrg 	    return ENOMEM;
    291  1.42       mrg 	}
    292  1.39  drochner 
    293  1.12   mycroft 	shmmap_s->va = attach_va;
    294  1.26       cgd 	shmmap_s->shmid = SCARG(uap, shmid);
    295  1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    296  1.11   hpeyerl 	shmseg->shm_atime = time.tv_sec;
    297  1.11   hpeyerl 	shmseg->shm_nattch++;
    298  1.12   mycroft 	*retval = attach_va;
    299  1.11   hpeyerl 	return 0;
    300  1.11   hpeyerl }
    301  1.11   hpeyerl 
    302  1.12   mycroft int
    303  1.52   thorpej sys___shmctl13(p, v, retval)
    304  1.11   hpeyerl 	struct proc *p;
    305  1.32   thorpej 	void *v;
    306  1.32   thorpej 	register_t *retval;
    307  1.32   thorpej {
    308  1.52   thorpej 	struct sys___shmctl13_args /* {
    309  1.26       cgd 		syscallarg(int) shmid;
    310  1.26       cgd 		syscallarg(int) cmd;
    311  1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    312  1.52   thorpej 	} */ *uap = v;
    313  1.52   thorpej 	struct shmid_ds shmbuf;
    314  1.52   thorpej 	int cmd, error;
    315  1.52   thorpej 
    316  1.52   thorpej 	cmd = SCARG(uap, cmd);
    317  1.52   thorpej 
    318  1.52   thorpej 	if (cmd == IPC_SET) {
    319  1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    320  1.52   thorpej 		if (error)
    321  1.52   thorpej 			return (error);
    322  1.52   thorpej 	}
    323  1.52   thorpej 
    324  1.52   thorpej 	error = shmctl1(p, SCARG(uap, shmid), cmd,
    325  1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    326  1.52   thorpej 
    327  1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    328  1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    329  1.52   thorpej 
    330  1.52   thorpej 	return (error);
    331  1.52   thorpej }
    332  1.52   thorpej 
    333  1.52   thorpej int
    334  1.52   thorpej shmctl1(p, shmid, cmd, shmbuf)
    335  1.52   thorpej 	struct proc *p;
    336  1.52   thorpej 	int shmid;
    337  1.52   thorpej 	int cmd;
    338  1.52   thorpej 	struct shmid_ds *shmbuf;
    339  1.52   thorpej {
    340  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    341  1.11   hpeyerl 	struct shmid_ds *shmseg;
    342  1.52   thorpej 	int error = 0;
    343  1.11   hpeyerl 
    344  1.52   thorpej 	shmseg = shm_find_segment_by_shmid(shmid);
    345  1.11   hpeyerl 	if (shmseg == NULL)
    346  1.11   hpeyerl 		return EINVAL;
    347  1.52   thorpej 	switch (cmd) {
    348  1.11   hpeyerl 	case IPC_STAT:
    349  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    350  1.11   hpeyerl 			return error;
    351  1.52   thorpej 		memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
    352  1.11   hpeyerl 		break;
    353  1.11   hpeyerl 	case IPC_SET:
    354  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    355  1.13   mycroft 			return error;
    356  1.52   thorpej 		shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
    357  1.52   thorpej 		shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
    358  1.12   mycroft 		shmseg->shm_perm.mode =
    359  1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    360  1.52   thorpej 		    (shmbuf->shm_perm.mode & ACCESSPERMS);
    361  1.12   mycroft 		shmseg->shm_ctime = time.tv_sec;
    362  1.11   hpeyerl 		break;
    363  1.11   hpeyerl 	case IPC_RMID:
    364  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    365  1.13   mycroft 			return error;
    366  1.52   thorpej 		shmseg->shm_perm._key = IPC_PRIVATE;
    367  1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    368  1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    369  1.12   mycroft 			shm_deallocate_segment(shmseg);
    370  1.52   thorpej 			shm_last_free = IPCID_TO_IX(shmid);
    371  1.11   hpeyerl 		}
    372  1.11   hpeyerl 		break;
    373  1.11   hpeyerl 	case SHM_LOCK:
    374  1.11   hpeyerl 	case SHM_UNLOCK:
    375  1.11   hpeyerl 	default:
    376  1.11   hpeyerl 		return EINVAL;
    377  1.11   hpeyerl 	}
    378  1.11   hpeyerl 	return 0;
    379  1.11   hpeyerl }
    380  1.11   hpeyerl 
    381  1.12   mycroft static int
    382  1.12   mycroft shmget_existing(p, uap, mode, segnum, retval)
    383  1.11   hpeyerl 	struct proc *p;
    384  1.33   mycroft 	struct sys_shmget_args /* {
    385  1.26       cgd 		syscallarg(key_t) key;
    386  1.44    kleink 		syscallarg(size_t) size;
    387  1.35  christos 		syscallarg(int) shmflg;
    388  1.26       cgd 	} */ *uap;
    389  1.11   hpeyerl 	int mode;
    390  1.11   hpeyerl 	int segnum;
    391  1.26       cgd 	register_t *retval;
    392  1.11   hpeyerl {
    393  1.12   mycroft 	struct shmid_ds *shmseg;
    394  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    395  1.11   hpeyerl 	int error;
    396  1.11   hpeyerl 
    397  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    398  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    399  1.16   mycroft 		/*
    400  1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    401  1.16   mycroft 		 * until it's done, and look the key up again (in case the
    402  1.16   mycroft 		 * allocation failed or it was freed).
    403  1.16   mycroft 		 */
    404  1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    405  1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    406  1.35  christos 		if (error)
    407  1.16   mycroft 			return error;
    408  1.16   mycroft 		return EAGAIN;
    409  1.16   mycroft 	}
    410  1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    411  1.11   hpeyerl 		return error;
    412  1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    413  1.11   hpeyerl 		return EINVAL;
    414  1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    415  1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    416  1.12   mycroft 		return EEXIST;
    417  1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    418  1.11   hpeyerl 	return 0;
    419  1.11   hpeyerl }
    420  1.11   hpeyerl 
    421  1.14   mycroft static int
    422  1.14   mycroft shmget_allocate_segment(p, uap, mode, retval)
    423  1.14   mycroft 	struct proc *p;
    424  1.33   mycroft 	struct sys_shmget_args /* {
    425  1.26       cgd 		syscallarg(key_t) key;
    426  1.44    kleink 		syscallarg(size_t) size;
    427  1.35  christos 		syscallarg(int) shmflg;
    428  1.26       cgd 	} */ *uap;
    429  1.14   mycroft 	int mode;
    430  1.26       cgd 	register_t *retval;
    431  1.14   mycroft {
    432  1.39  drochner 	int i, segnum, shmid, size;
    433  1.14   mycroft 	struct ucred *cred = p->p_ucred;
    434  1.14   mycroft 	struct shmid_ds *shmseg;
    435  1.14   mycroft 	struct shm_handle *shm_handle;
    436  1.40  drochner 	int error = 0;
    437  1.14   mycroft 
    438  1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    439  1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    440  1.14   mycroft 		return EINVAL;
    441  1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    442  1.14   mycroft 		return ENOSPC;
    443  1.53     ragge 	size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
    444  1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    445  1.14   mycroft 		return ENOMEM;
    446  1.14   mycroft 	if (shm_last_free < 0) {
    447  1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    448  1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    449  1.14   mycroft 				break;
    450  1.14   mycroft 		if (i == shminfo.shmmni)
    451  1.14   mycroft 			panic("shmseg free count inconsistent");
    452  1.14   mycroft 		segnum = i;
    453  1.14   mycroft 	} else  {
    454  1.14   mycroft 		segnum = shm_last_free;
    455  1.14   mycroft 		shm_last_free = -1;
    456  1.14   mycroft 	}
    457  1.14   mycroft 	shmseg = &shmsegs[segnum];
    458  1.14   mycroft 	/*
    459  1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    460  1.14   mycroft 	 * so that noone else tries to create the same key.
    461  1.14   mycroft 	 */
    462  1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    463  1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    464  1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    465  1.14   mycroft 	shm_handle = (struct shm_handle *)
    466  1.14   mycroft 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
    467  1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    468  1.42       mrg 
    469  1.42       mrg 	shm_handle->shm_object = uao_create(size, 0);
    470  1.42       mrg 
    471  1.52   thorpej 	shmseg->_shm_internal = shm_handle;
    472  1.14   mycroft 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
    473  1.14   mycroft 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
    474  1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    475  1.16   mycroft 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
    476  1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    477  1.14   mycroft 	shmseg->shm_cpid = p->p_pid;
    478  1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    479  1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    480  1.14   mycroft 	shmseg->shm_ctime = time.tv_sec;
    481  1.14   mycroft 	shm_committed += btoc(size);
    482  1.14   mycroft 	shm_nused++;
    483  1.40  drochner 
    484  1.51       mrg 	*retval = shmid;
    485  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    486  1.16   mycroft 		/*
    487  1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    488  1.16   mycroft 		 * them up now.
    489  1.16   mycroft 		 */
    490  1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    491  1.16   mycroft 		wakeup((caddr_t)shmseg);
    492  1.16   mycroft 	}
    493  1.40  drochner 	return error;
    494  1.14   mycroft }
    495  1.14   mycroft 
    496  1.12   mycroft int
    497  1.33   mycroft sys_shmget(p, v, retval)
    498  1.11   hpeyerl 	struct proc *p;
    499  1.32   thorpej 	void *v;
    500  1.32   thorpej 	register_t *retval;
    501  1.32   thorpej {
    502  1.33   mycroft 	struct sys_shmget_args /* {
    503  1.26       cgd 		syscallarg(key_t) key;
    504  1.26       cgd 		syscallarg(int) size;
    505  1.35  christos 		syscallarg(int) shmflg;
    506  1.32   thorpej 	} */ *uap = v;
    507  1.11   hpeyerl 	int segnum, mode, error;
    508  1.11   hpeyerl 
    509  1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    510  1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    511  1.16   mycroft 	again:
    512  1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    513  1.16   mycroft 		if (segnum >= 0) {
    514  1.16   mycroft 			error = shmget_existing(p, uap, mode, segnum, retval);
    515  1.16   mycroft 			if (error == EAGAIN)
    516  1.16   mycroft 				goto again;
    517  1.16   mycroft 			return error;
    518  1.16   mycroft 		}
    519  1.26       cgd 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    520  1.14   mycroft 			return ENOENT;
    521  1.11   hpeyerl 	}
    522  1.14   mycroft 	return shmget_allocate_segment(p, uap, mode, retval);
    523  1.11   hpeyerl }
    524  1.11   hpeyerl 
    525  1.12   mycroft void
    526  1.41   thorpej shmfork(vm1, vm2)
    527  1.41   thorpej 	struct vmspace *vm1, *vm2;
    528  1.11   hpeyerl {
    529  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    530  1.12   mycroft 	size_t size;
    531  1.11   hpeyerl 	int i;
    532  1.11   hpeyerl 
    533  1.41   thorpej 	if (vm1->vm_shm == NULL) {
    534  1.41   thorpej 		vm2->vm_shm = NULL;
    535  1.38  christos 		return;
    536  1.38  christos 	}
    537  1.41   thorpej 
    538  1.12   mycroft 	size = shminfo.shmseg * sizeof(struct shmmap_state);
    539  1.12   mycroft 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
    540  1.46     perry 	memcpy(shmmap_s, vm1->vm_shm, size);
    541  1.41   thorpej 	vm2->vm_shm = (caddr_t)shmmap_s;
    542  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    543  1.12   mycroft 		if (shmmap_s->shmid != -1)
    544  1.11   hpeyerl 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
    545  1.11   hpeyerl }
    546  1.11   hpeyerl 
    547  1.12   mycroft void
    548  1.41   thorpej shmexit(vm)
    549  1.41   thorpej 	struct vmspace *vm;
    550  1.11   hpeyerl {
    551  1.12   mycroft 	struct shmmap_state *shmmap_s;
    552  1.11   hpeyerl 	int i;
    553  1.11   hpeyerl 
    554  1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    555  1.38  christos 	if (shmmap_s == NULL)
    556  1.38  christos 		return;
    557  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    558  1.12   mycroft 		if (shmmap_s->shmid != -1)
    559  1.41   thorpej 			shm_delete_mapping(vm, shmmap_s);
    560  1.41   thorpej 	free(vm->vm_shm, M_SHM);
    561  1.41   thorpej 	vm->vm_shm = NULL;
    562  1.11   hpeyerl }
    563  1.11   hpeyerl 
    564  1.12   mycroft void
    565  1.12   mycroft shminit()
    566  1.11   hpeyerl {
    567  1.11   hpeyerl 	int i;
    568  1.24   deraadt 
    569  1.24   deraadt 	shminfo.shmmax *= NBPG;
    570  1.11   hpeyerl 
    571  1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    572  1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    573  1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    574  1.11   hpeyerl 	}
    575  1.11   hpeyerl 	shm_last_free = 0;
    576  1.11   hpeyerl 	shm_nused = 0;
    577  1.11   hpeyerl 	shm_committed = 0;
    578  1.11   hpeyerl }
    579