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