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