xref: /freebsd/sys/kern/sysv_shm.c (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
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/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 
62 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
63 
64 struct oshmctl_args;
65 static int oshmctl __P((struct proc *p, struct oshmctl_args *uap));
66 
67 static int shmget_allocate_segment __P((struct proc *p,
68     struct shmget_args *uap, int mode));
69 static 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. */
73 static 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 
84 static int shm_last_free, shm_nused, shm_committed, shmalloced;
85 static struct shmid_ds	*shmsegs;
86 
87 struct shm_handle {
88 	/* vm_offset_t kva; */
89 	vm_object_t shm_object;
90 };
91 
92 struct shmmap_state {
93 	vm_offset_t va;
94 	int shmid;
95 };
96 
97 static void shm_deallocate_segment __P((struct shmid_ds *));
98 static int shm_find_segment_by_key __P((key_t));
99 static struct shmid_ds *shm_find_segment_by_shmid __P((int));
100 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
101 static void shmrealloc __P((void));
102 static void shminit __P((void));
103 static int sysvshm_modload __P((struct module *, int, void *));
104 static int shmunload __P((void));
105 static void shmexit_myhook __P((struct proc *p));
106 static void shmfork_myhook __P((struct proc *p1, struct proc *p2));
107 static 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 
131 struct	shminfo shminfo = {
132 	SHMMAX,
133 	SHMMIN,
134 	SHMMNI,
135 	SHMSEG,
136 	SHMALL
137 };
138 
139 static int shm_use_phys;
140 
141 SYSCTL_DECL(_kern_ipc);
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RD, &shminfo.shmseg, 0, "");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
148     &shm_use_phys, 0, "");
149 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
150     NULL, 0, sysctl_shmsegs, "", "");
151 
152 static int
153 shm_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 
165 static struct shmid_ds *
166 shm_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 
183 static void
184 shm_deallocate_segment(shmseg)
185 	struct shmid_ds *shmseg;
186 {
187 	struct shm_handle *shm_handle;
188 	size_t size;
189 
190 	GIANT_REQUIRED;
191 
192 	shm_handle = shmseg->shm_internal;
193 	vm_object_deallocate(shm_handle->shm_object);
194 	free((caddr_t)shm_handle, M_SHM);
195 	shmseg->shm_internal = NULL;
196 	size = round_page(shmseg->shm_segsz);
197 	shm_committed -= btoc(size);
198 	shm_nused--;
199 	shmseg->shm_perm.mode = SHMSEG_FREE;
200 }
201 
202 static int
203 shm_delete_mapping(p, shmmap_s)
204 	struct proc *p;
205 	struct shmmap_state *shmmap_s;
206 {
207 	struct shmid_ds *shmseg;
208 	int segnum, result;
209 	size_t size;
210 
211 	GIANT_REQUIRED;
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_
231 struct shmdt_args {
232 	void *shmaddr;
233 };
234 #endif
235 
236 /*
237  * MPSAFE
238  */
239 int
240 shmdt(p, uap)
241 	struct proc *p;
242 	struct shmdt_args *uap;
243 {
244 	struct shmmap_state *shmmap_s;
245 	int i;
246 	int error = 0;
247 
248 	mtx_lock(&Giant);
249 
250 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
251 		error = ENOSYS;
252 		goto done2;
253 	}
254 
255 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
256  	if (shmmap_s == NULL) {
257 		error = EINVAL;
258 		goto done2;
259 	}
260 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
261 		if (shmmap_s->shmid != -1 &&
262 		    shmmap_s->va == (vm_offset_t)uap->shmaddr) {
263 			break;
264 		}
265 	}
266 	if (i == shminfo.shmseg) {
267 		error = EINVAL;
268 		goto done2;
269 	}
270 	error = shm_delete_mapping(p, shmmap_s);
271 done2:
272 	mtx_unlock(&Giant);
273 	return (error);
274 }
275 
276 #ifndef _SYS_SYSPROTO_H_
277 struct shmat_args {
278 	int shmid;
279 	void *shmaddr;
280 	int shmflg;
281 };
282 #endif
283 
284 /*
285  * MPSAFE
286  */
287 int
288 shmat(p, uap)
289 	struct proc *p;
290 	struct shmat_args *uap;
291 {
292 	int i, flags;
293 	struct shmid_ds *shmseg;
294 	struct shmmap_state *shmmap_s = NULL;
295 	struct shm_handle *shm_handle;
296 	vm_offset_t attach_va;
297 	vm_prot_t prot;
298 	vm_size_t size;
299 	int rv;
300 	int error = 0;
301 
302 	mtx_lock(&Giant);
303 
304 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
305 		error = ENOSYS;
306 		goto done2;
307 	}
308 
309 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
310 	if (shmmap_s == NULL) {
311 		size = shminfo.shmseg * sizeof(struct shmmap_state);
312 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
313 		for (i = 0; i < shminfo.shmseg; i++)
314 			shmmap_s[i].shmid = -1;
315 		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
316 	}
317 	shmseg = shm_find_segment_by_shmid(uap->shmid);
318 	if (shmseg == NULL) {
319 		error = EINVAL;
320 		goto done2;
321 	}
322 	error = ipcperm(p, &shmseg->shm_perm,
323 	    (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
324 	if (error)
325 		goto done2;
326 	for (i = 0; i < shminfo.shmseg; i++) {
327 		if (shmmap_s->shmid == -1)
328 			break;
329 		shmmap_s++;
330 	}
331 	if (i >= shminfo.shmseg) {
332 		error = EMFILE;
333 		goto done2;
334 	}
335 	size = round_page(shmseg->shm_segsz);
336 #ifdef VM_PROT_READ_IS_EXEC
337 	prot = VM_PROT_READ | VM_PROT_EXECUTE;
338 #else
339 	prot = VM_PROT_READ;
340 #endif
341 	if ((uap->shmflg & SHM_RDONLY) == 0)
342 		prot |= VM_PROT_WRITE;
343 	flags = MAP_ANON | MAP_SHARED;
344 	if (uap->shmaddr) {
345 		flags |= MAP_FIXED;
346 		if (uap->shmflg & SHM_RND) {
347 			attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
348 		} else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
349 			attach_va = (vm_offset_t)uap->shmaddr;
350 		} else {
351 			error = EINVAL;
352 			goto done2;
353 		}
354 	} else {
355 		/*
356 		 * This is just a hint to vm_map_find() about where to
357 		 * put it.
358 		 */
359 		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
360 		    + MAXTSIZ + MAXDSIZ);
361 	}
362 
363 	shm_handle = shmseg->shm_internal;
364 	vm_object_reference(shm_handle->shm_object);
365 	rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
366 		0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
367 	if (rv != KERN_SUCCESS) {
368 		error = ENOMEM;
369 		goto done2;
370 	}
371 	vm_map_inherit(&p->p_vmspace->vm_map,
372 		attach_va, attach_va + size, VM_INHERIT_SHARE);
373 
374 	shmmap_s->va = attach_va;
375 	shmmap_s->shmid = uap->shmid;
376 	shmseg->shm_lpid = p->p_pid;
377 	shmseg->shm_atime = time_second;
378 	shmseg->shm_nattch++;
379 	p->p_retval[0] = attach_va;
380 done2:
381 	mtx_unlock(&Giant);
382 	return (error);
383 }
384 
385 struct oshmid_ds {
386 	struct	ipc_perm shm_perm;	/* operation perms */
387 	int	shm_segsz;		/* size of segment (bytes) */
388 	ushort	shm_cpid;		/* pid, creator */
389 	ushort	shm_lpid;		/* pid, last operation */
390 	short	shm_nattch;		/* no. of current attaches */
391 	time_t	shm_atime;		/* last attach time */
392 	time_t	shm_dtime;		/* last detach time */
393 	time_t	shm_ctime;		/* last change time */
394 	void	*shm_handle;		/* internal handle for shm segment */
395 };
396 
397 struct oshmctl_args {
398 	int shmid;
399 	int cmd;
400 	struct oshmid_ds *ubuf;
401 };
402 
403 /*
404  * MPSAFE
405  */
406 static int
407 oshmctl(p, uap)
408 	struct proc *p;
409 	struct oshmctl_args *uap;
410 {
411 #ifdef COMPAT_43
412 	int error = 0;
413 	struct shmid_ds *shmseg;
414 	struct oshmid_ds outbuf;
415 
416 	mtx_lock(&Giant);
417 
418 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
419 		error = ENOSYS;
420 		goto done2;
421 	}
422 
423 	shmseg = shm_find_segment_by_shmid(uap->shmid);
424 	if (shmseg == NULL) {
425 		error = EINVAL;
426 		goto done2;
427 	}
428 	switch (uap->cmd) {
429 	case IPC_STAT:
430 		error = ipcperm(p, &shmseg->shm_perm, IPC_R);
431 		if (error)
432 			goto done2;
433 		outbuf.shm_perm = shmseg->shm_perm;
434 		outbuf.shm_segsz = shmseg->shm_segsz;
435 		outbuf.shm_cpid = shmseg->shm_cpid;
436 		outbuf.shm_lpid = shmseg->shm_lpid;
437 		outbuf.shm_nattch = shmseg->shm_nattch;
438 		outbuf.shm_atime = shmseg->shm_atime;
439 		outbuf.shm_dtime = shmseg->shm_dtime;
440 		outbuf.shm_ctime = shmseg->shm_ctime;
441 		outbuf.shm_handle = shmseg->shm_internal;
442 		error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
443 		if (error)
444 			goto done2;
445 		break;
446 	default:
447 		/* XXX casting to (sy_call_t *) is bogus, as usual. */
448 		error = ((sy_call_t *)shmctl)(p, uap);
449 		break;
450 	}
451 done2:
452 	mtx_unlock(&Giant);
453 	return (error);
454 #else
455 	return EINVAL;
456 #endif
457 }
458 
459 #ifndef _SYS_SYSPROTO_H_
460 struct shmctl_args {
461 	int shmid;
462 	int cmd;
463 	struct shmid_ds *buf;
464 };
465 #endif
466 
467 /*
468  * MPSAFE
469  */
470 int
471 shmctl(p, uap)
472 	struct proc *p;
473 	struct shmctl_args *uap;
474 {
475 	int error = 0;
476 	struct shmid_ds inbuf;
477 	struct shmid_ds *shmseg;
478 
479 	mtx_lock(&Giant);
480 
481 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
482 		error = ENOSYS;
483 		goto done2;
484 	}
485 
486 	shmseg = shm_find_segment_by_shmid(uap->shmid);
487 	if (shmseg == NULL) {
488 		error = EINVAL;
489 		goto done2;
490 	}
491 	switch (uap->cmd) {
492 	case IPC_STAT:
493 		error = ipcperm(p, &shmseg->shm_perm, IPC_R);
494 		if (error)
495 			goto done2;
496 		error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
497 		if (error)
498 			goto done2;
499 		break;
500 	case IPC_SET:
501 		error = ipcperm(p, &shmseg->shm_perm, IPC_M);
502 		if (error)
503 			goto done2;
504 		error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
505 		if (error)
506 			goto done2;
507 		shmseg->shm_perm.uid = inbuf.shm_perm.uid;
508 		shmseg->shm_perm.gid = inbuf.shm_perm.gid;
509 		shmseg->shm_perm.mode =
510 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
511 		    (inbuf.shm_perm.mode & ACCESSPERMS);
512 		shmseg->shm_ctime = time_second;
513 		break;
514 	case IPC_RMID:
515 		error = ipcperm(p, &shmseg->shm_perm, IPC_M);
516 		if (error)
517 			goto done2;
518 		shmseg->shm_perm.key = IPC_PRIVATE;
519 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
520 		if (shmseg->shm_nattch <= 0) {
521 			shm_deallocate_segment(shmseg);
522 			shm_last_free = IPCID_TO_IX(uap->shmid);
523 		}
524 		break;
525 #if 0
526 	case SHM_LOCK:
527 	case SHM_UNLOCK:
528 #endif
529 	default:
530 		error = EINVAL;
531 		break;
532 	}
533 done2:
534 	mtx_unlock(&Giant);
535 	return (error);
536 }
537 
538 #ifndef _SYS_SYSPROTO_H_
539 struct shmget_args {
540 	key_t key;
541 	size_t size;
542 	int shmflg;
543 };
544 #endif
545 
546 static int
547 shmget_existing(p, uap, mode, segnum)
548 	struct proc *p;
549 	struct shmget_args *uap;
550 	int mode;
551 	int segnum;
552 {
553 	struct shmid_ds *shmseg;
554 	int error;
555 
556 	shmseg = &shmsegs[segnum];
557 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
558 		/*
559 		 * This segment is in the process of being allocated.  Wait
560 		 * until it's done, and look the key up again (in case the
561 		 * allocation failed or it was freed).
562 		 */
563 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
564 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
565 		if (error)
566 			return error;
567 		return EAGAIN;
568 	}
569 	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
570 		return EEXIST;
571 	error = ipcperm(p, &shmseg->shm_perm, mode);
572 	if (error)
573 		return error;
574 	if (uap->size && uap->size > shmseg->shm_segsz)
575 		return EINVAL;
576 	p->p_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
577 	return 0;
578 }
579 
580 static int
581 shmget_allocate_segment(p, uap, mode)
582 	struct proc *p;
583 	struct shmget_args *uap;
584 	int mode;
585 {
586 	int i, segnum, shmid, size;
587 	struct ucred *cred = p->p_ucred;
588 	struct shmid_ds *shmseg;
589 	struct shm_handle *shm_handle;
590 
591 	GIANT_REQUIRED;
592 
593 	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
594 		return EINVAL;
595 	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
596 		return ENOSPC;
597 	size = round_page(uap->size);
598 	if (shm_committed + btoc(size) > shminfo.shmall)
599 		return ENOMEM;
600 	if (shm_last_free < 0) {
601 		shmrealloc();	/* Maybe expand the shmsegs[] array. */
602 		for (i = 0; i < shmalloced; i++)
603 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
604 				break;
605 		if (i == shmalloced)
606 			return ENOSPC;
607 		segnum = i;
608 	} else  {
609 		segnum = shm_last_free;
610 		shm_last_free = -1;
611 	}
612 	shmseg = &shmsegs[segnum];
613 	/*
614 	 * In case we sleep in malloc(), mark the segment present but deleted
615 	 * so that noone else tries to create the same key.
616 	 */
617 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
618 	shmseg->shm_perm.key = uap->key;
619 	shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
620 	shm_handle = (struct shm_handle *)
621 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
622 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
623 
624 	/*
625 	 * We make sure that we have allocated a pager before we need
626 	 * to.
627 	 */
628 	if (shm_use_phys) {
629 		shm_handle->shm_object =
630 		    vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
631 	} else {
632 		shm_handle->shm_object =
633 		    vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
634 	}
635 	vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
636 	vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
637 
638 	shmseg->shm_internal = shm_handle;
639 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
640 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
641 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
642 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
643 	shmseg->shm_segsz = uap->size;
644 	shmseg->shm_cpid = p->p_pid;
645 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
646 	shmseg->shm_atime = shmseg->shm_dtime = 0;
647 	shmseg->shm_ctime = time_second;
648 	shm_committed += btoc(size);
649 	shm_nused++;
650 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
651 		/*
652 		 * Somebody else wanted this key while we were asleep.  Wake
653 		 * them up now.
654 		 */
655 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
656 		wakeup((caddr_t)shmseg);
657 	}
658 	p->p_retval[0] = shmid;
659 	return 0;
660 }
661 
662 /*
663  * MPSAFE
664  */
665 int
666 shmget(p, uap)
667 	struct proc *p;
668 	struct shmget_args *uap;
669 {
670 	int segnum, mode;
671 	int error;
672 
673 	mtx_lock(&Giant);
674 
675 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
676 		error = ENOSYS;
677 		goto done2;
678 	}
679 
680 	mode = uap->shmflg & ACCESSPERMS;
681 	if (uap->key != IPC_PRIVATE) {
682 	again:
683 		segnum = shm_find_segment_by_key(uap->key);
684 		if (segnum >= 0) {
685 			error = shmget_existing(p, uap, mode, segnum);
686 			if (error == EAGAIN)
687 				goto again;
688 			goto done2;
689 		}
690 		if ((uap->shmflg & IPC_CREAT) == 0) {
691 			error = ENOENT;
692 			goto done2;
693 		}
694 	}
695 	error = shmget_allocate_segment(p, uap, mode);
696 done2:
697 	mtx_unlock(&Giant);
698 	return (error);
699 }
700 
701 /*
702  * MPSAFE
703  */
704 int
705 shmsys(p, uap)
706 	struct proc *p;
707 	/* XXX actually varargs. */
708 	struct shmsys_args /* {
709 		u_int	which;
710 		int	a2;
711 		int	a3;
712 		int	a4;
713 	} */ *uap;
714 {
715 	int error;
716 
717 	mtx_lock(&Giant);
718 
719 	if (!jail_sysvipc_allowed && jailed(p->p_ucred)) {
720 		error = ENOSYS;
721 		goto done2;
722 	}
723 
724 	if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0])) {
725 		error = EINVAL;
726 		goto done2;
727 	}
728 	error = (*shmcalls[uap->which])(p, &uap->a2);
729 done2:
730 	mtx_unlock(&Giant);
731 	return (error);
732 }
733 
734 static void
735 shmfork_myhook(p1, p2)
736 	struct proc *p1, *p2;
737 {
738 	struct shmmap_state *shmmap_s;
739 	size_t size;
740 	int i;
741 
742 	size = shminfo.shmseg * sizeof(struct shmmap_state);
743 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
744 	bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
745 	p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
746 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
747 		if (shmmap_s->shmid != -1)
748 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
749 }
750 
751 static void
752 shmexit_myhook(p)
753 	struct proc *p;
754 {
755 	struct shmmap_state *shmmap_s;
756 	int i;
757 
758 	GIANT_REQUIRED;
759 
760 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
761 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
762 		if (shmmap_s->shmid != -1)
763 			shm_delete_mapping(p, shmmap_s);
764 	free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
765 	p->p_vmspace->vm_shm = NULL;
766 }
767 
768 static void
769 shmrealloc(void)
770 {
771 	int i;
772 	struct shmid_ds *newsegs;
773 
774 	if (shmalloced >= shminfo.shmmni)
775 		return;
776 
777 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
778 	if (newsegs == NULL)
779 		return;
780 	for (i = 0; i < shmalloced; i++)
781 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
782 	for (; i < shminfo.shmmni; i++) {
783 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
784 		shmsegs[i].shm_perm.seq = 0;
785 	}
786 	free(shmsegs, M_SHM);
787 	shmsegs = newsegs;
788 	shmalloced = shminfo.shmmni;
789 }
790 
791 static void
792 shminit()
793 {
794 	int i;
795 
796 	shmalloced = shminfo.shmmni;
797 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
798 	if (shmsegs == NULL)
799 		panic("cannot allocate initial memory for sysvshm");
800 	for (i = 0; i < shmalloced; i++) {
801 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
802 		shmsegs[i].shm_perm.seq = 0;
803 	}
804 	shm_last_free = 0;
805 	shm_nused = 0;
806 	shm_committed = 0;
807 	shmexit_hook = &shmexit_myhook;
808 	shmfork_hook = &shmfork_myhook;
809 }
810 
811 static int
812 shmunload()
813 {
814 
815 	if (shm_nused > 0)
816 		return (EBUSY);
817 
818 	free(shmsegs, M_SHM);
819 	shmexit_hook = NULL;
820 	shmfork_hook = NULL;
821 	return (0);
822 }
823 
824 static int
825 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
826 {
827 
828 	return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
829 }
830 
831 static int
832 sysvshm_modload(struct module *module, int cmd, void *arg)
833 {
834 	int error = 0;
835 
836 	switch (cmd) {
837 	case MOD_LOAD:
838 		shminit();
839 		break;
840 	case MOD_UNLOAD:
841 		error = shmunload();
842 		break;
843 	case MOD_SHUTDOWN:
844 		break;
845 	default:
846 		error = EINVAL;
847 		break;
848 	}
849 	return (error);
850 }
851 
852 static moduledata_t sysvshm_mod = {
853 	"sysvshm",
854 	&sysvshm_modload,
855 	NULL
856 };
857 
858 SYSCALL_MODULE_HELPER(shmsys, 4);
859 SYSCALL_MODULE_HELPER(shmat, 3);
860 SYSCALL_MODULE_HELPER(shmctl, 3);
861 SYSCALL_MODULE_HELPER(shmdt, 1);
862 SYSCALL_MODULE_HELPER(shmget, 3);
863 
864 DECLARE_MODULE(sysvshm, sysvshm_mod,
865 	SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
866 MODULE_VERSION(sysvshm, 1);
867