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