xref: /freebsd/sys/kern/sysv_shm.c (revision 45f48220bb95a27195cdf63fb3cae4c09c053cde)
1 /*	$NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $	*/
2 /*-
3  * Copyright (c) 1994 Adam Glass and Charles Hannum.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Adam Glass and Charles
16  *	Hannum.
17  * 4. The names of the authors may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 2003-2005 McAfee, Inc.
33  * All rights reserved.
34  *
35  * This software was developed for the FreeBSD Project in part by McAfee
36  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
37  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
38  * program.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64 
65 #include "opt_compat.h"
66 #include "opt_sysvipc.h"
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/lock.h>
72 #include <sys/sysctl.h>
73 #include <sys/shm.h>
74 #include <sys/proc.h>
75 #include <sys/malloc.h>
76 #include <sys/mman.h>
77 #include <sys/module.h>
78 #include <sys/mutex.h>
79 #include <sys/resourcevar.h>
80 #include <sys/stat.h>
81 #include <sys/syscall.h>
82 #include <sys/syscallsubr.h>
83 #include <sys/sysent.h>
84 #include <sys/sysproto.h>
85 #include <sys/jail.h>
86 
87 #include <security/mac/mac_framework.h>
88 
89 #include <vm/vm.h>
90 #include <vm/vm_param.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pager.h>
96 
97 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
98 
99 static int shmget_allocate_segment(struct thread *td,
100     struct shmget_args *uap, int mode);
101 static int shmget_existing(struct thread *td, struct shmget_args *uap,
102     int mode, int segnum);
103 
104 #define	SHMSEG_FREE     	0x0200
105 #define	SHMSEG_REMOVED  	0x0400
106 #define	SHMSEG_ALLOCATED	0x0800
107 #define	SHMSEG_WANTED		0x1000
108 
109 static int shm_last_free, shm_nused, shmalloced;
110 vm_size_t shm_committed;
111 static struct shmid_kernel	*shmsegs;
112 
113 struct shmmap_state {
114 	vm_offset_t va;
115 	int shmid;
116 };
117 
118 static void shm_deallocate_segment(struct shmid_kernel *);
119 static int shm_find_segment_by_key(key_t);
120 static struct shmid_kernel *shm_find_segment_by_shmid(int);
121 static struct shmid_kernel *shm_find_segment_by_shmidx(int);
122 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
123 static void shmrealloc(void);
124 static void shminit(void);
125 static int sysvshm_modload(struct module *, int, void *);
126 static int shmunload(void);
127 static void shmexit_myhook(struct vmspace *vm);
128 static void shmfork_myhook(struct proc *p1, struct proc *p2);
129 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
130 
131 /*
132  * Tuneable values.
133  */
134 #ifndef SHMMAXPGS
135 #define	SHMMAXPGS	8192	/* Note: sysv shared memory is swap backed. */
136 #endif
137 #ifndef SHMMAX
138 #define	SHMMAX	(SHMMAXPGS*PAGE_SIZE)
139 #endif
140 #ifndef SHMMIN
141 #define	SHMMIN	1
142 #endif
143 #ifndef SHMMNI
144 #define	SHMMNI	192
145 #endif
146 #ifndef SHMSEG
147 #define	SHMSEG	128
148 #endif
149 #ifndef SHMALL
150 #define	SHMALL	(SHMMAXPGS)
151 #endif
152 
153 struct	shminfo shminfo = {
154 	SHMMAX,
155 	SHMMIN,
156 	SHMMNI,
157 	SHMSEG,
158 	SHMALL
159 };
160 
161 static int shm_use_phys;
162 static int shm_allow_removed;
163 
164 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
165     "Maximum shared memory segment size");
166 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
167     "Minimum shared memory segment size");
168 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
169     "Number of shared memory identifiers");
170 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
171     "Number of segments per process");
172 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
173     "Maximum number of pages available for shared memory");
174 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
175     &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
176 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
177     &shm_allow_removed, 0,
178     "Enable/Disable attachment to attached segments marked for removal");
179 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
180     NULL, 0, sysctl_shmsegs, "",
181     "Current number of shared memory segments allocated");
182 
183 static int
184 shm_find_segment_by_key(key)
185 	key_t key;
186 {
187 	int i;
188 
189 	for (i = 0; i < shmalloced; i++)
190 		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
191 		    shmsegs[i].u.shm_perm.key == key)
192 			return (i);
193 	return (-1);
194 }
195 
196 static struct shmid_kernel *
197 shm_find_segment_by_shmid(int shmid)
198 {
199 	int segnum;
200 	struct shmid_kernel *shmseg;
201 
202 	segnum = IPCID_TO_IX(shmid);
203 	if (segnum < 0 || segnum >= shmalloced)
204 		return (NULL);
205 	shmseg = &shmsegs[segnum];
206 	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
207 	    (!shm_allow_removed &&
208 	     (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
209 	    shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid))
210 		return (NULL);
211 	return (shmseg);
212 }
213 
214 static struct shmid_kernel *
215 shm_find_segment_by_shmidx(int segnum)
216 {
217 	struct shmid_kernel *shmseg;
218 
219 	if (segnum < 0 || segnum >= shmalloced)
220 		return (NULL);
221 	shmseg = &shmsegs[segnum];
222 	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
223 	    (!shm_allow_removed &&
224 	     (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0))
225 		return (NULL);
226 	return (shmseg);
227 }
228 
229 static void
230 shm_deallocate_segment(shmseg)
231 	struct shmid_kernel *shmseg;
232 {
233 	vm_size_t size;
234 
235 	GIANT_REQUIRED;
236 
237 	vm_object_deallocate(shmseg->u.shm_internal);
238 	shmseg->u.shm_internal = NULL;
239 	size = round_page(shmseg->shm_bsegsz);
240 	shm_committed -= btoc(size);
241 	shm_nused--;
242 	shmseg->u.shm_perm.mode = SHMSEG_FREE;
243 #ifdef MAC
244 	mac_sysvshm_cleanup(shmseg);
245 #endif
246 }
247 
248 static int
249 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
250 {
251 	struct shmid_kernel *shmseg;
252 	int segnum, result;
253 	vm_size_t size;
254 
255 	GIANT_REQUIRED;
256 
257 	segnum = IPCID_TO_IX(shmmap_s->shmid);
258 	shmseg = &shmsegs[segnum];
259 	size = round_page(shmseg->shm_bsegsz);
260 	result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
261 	if (result != KERN_SUCCESS)
262 		return (EINVAL);
263 	shmmap_s->shmid = -1;
264 	shmseg->u.shm_dtime = time_second;
265 	if ((--shmseg->u.shm_nattch <= 0) &&
266 	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
267 		shm_deallocate_segment(shmseg);
268 		shm_last_free = segnum;
269 	}
270 	return (0);
271 }
272 
273 #ifndef _SYS_SYSPROTO_H_
274 struct shmdt_args {
275 	const void *shmaddr;
276 };
277 #endif
278 int
279 shmdt(td, uap)
280 	struct thread *td;
281 	struct shmdt_args *uap;
282 {
283 	struct proc *p = td->td_proc;
284 	struct shmmap_state *shmmap_s;
285 #ifdef MAC
286 	struct shmid_kernel *shmsegptr;
287 #endif
288 	int i;
289 	int error = 0;
290 
291 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
292 		return (ENOSYS);
293 	mtx_lock(&Giant);
294 	shmmap_s = p->p_vmspace->vm_shm;
295  	if (shmmap_s == NULL) {
296 		error = EINVAL;
297 		goto done2;
298 	}
299 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
300 		if (shmmap_s->shmid != -1 &&
301 		    shmmap_s->va == (vm_offset_t)uap->shmaddr) {
302 			break;
303 		}
304 	}
305 	if (i == shminfo.shmseg) {
306 		error = EINVAL;
307 		goto done2;
308 	}
309 #ifdef MAC
310 	shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
311 	error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
312 	if (error != 0)
313 		goto done2;
314 #endif
315 	error = shm_delete_mapping(p->p_vmspace, shmmap_s);
316 done2:
317 	mtx_unlock(&Giant);
318 	return (error);
319 }
320 
321 #ifndef _SYS_SYSPROTO_H_
322 struct shmat_args {
323 	int shmid;
324 	const void *shmaddr;
325 	int shmflg;
326 };
327 #endif
328 int
329 kern_shmat(td, shmid, shmaddr, shmflg)
330 	struct thread *td;
331 	int shmid;
332 	const void *shmaddr;
333 	int shmflg;
334 {
335 	struct proc *p = td->td_proc;
336 	int i, flags;
337 	struct shmid_kernel *shmseg;
338 	struct shmmap_state *shmmap_s = NULL;
339 	vm_offset_t attach_va;
340 	vm_prot_t prot;
341 	vm_size_t size;
342 	int rv;
343 	int error = 0;
344 
345 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
346 		return (ENOSYS);
347 	mtx_lock(&Giant);
348 	shmmap_s = p->p_vmspace->vm_shm;
349 	if (shmmap_s == NULL) {
350 		shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
351 		    M_SHM, M_WAITOK);
352 		for (i = 0; i < shminfo.shmseg; i++)
353 			shmmap_s[i].shmid = -1;
354 		p->p_vmspace->vm_shm = shmmap_s;
355 	}
356 	shmseg = shm_find_segment_by_shmid(shmid);
357 	if (shmseg == NULL) {
358 		error = EINVAL;
359 		goto done2;
360 	}
361 	error = ipcperm(td, &shmseg->u.shm_perm,
362 	    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
363 	if (error)
364 		goto done2;
365 #ifdef MAC
366 	error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
367 	if (error != 0)
368 		goto done2;
369 #endif
370 	for (i = 0; i < shminfo.shmseg; i++) {
371 		if (shmmap_s->shmid == -1)
372 			break;
373 		shmmap_s++;
374 	}
375 	if (i >= shminfo.shmseg) {
376 		error = EMFILE;
377 		goto done2;
378 	}
379 	size = round_page(shmseg->shm_bsegsz);
380 	prot = VM_PROT_READ;
381 	if ((shmflg & SHM_RDONLY) == 0)
382 		prot |= VM_PROT_WRITE;
383 	flags = MAP_ANON | MAP_SHARED;
384 	if (shmaddr) {
385 		flags |= MAP_FIXED;
386 		if (shmflg & SHM_RND) {
387 			attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
388 		} else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
389 			attach_va = (vm_offset_t)shmaddr;
390 		} else {
391 			error = EINVAL;
392 			goto done2;
393 		}
394 	} else {
395 		/*
396 		 * This is just a hint to vm_map_find() about where to
397 		 * put it.
398 		 */
399 		PROC_LOCK(p);
400 		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
401 		    lim_max(p, RLIMIT_DATA));
402 		PROC_UNLOCK(p);
403 	}
404 
405 	vm_object_reference(shmseg->u.shm_internal);
406 	rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->u.shm_internal,
407 	    0, &attach_va, size, (flags & MAP_FIXED) ? VMFS_NO_SPACE :
408 	    VMFS_ANY_SPACE, prot, prot, 0);
409 	if (rv != KERN_SUCCESS) {
410 		vm_object_deallocate(shmseg->u.shm_internal);
411 		error = ENOMEM;
412 		goto done2;
413 	}
414 	vm_map_inherit(&p->p_vmspace->vm_map,
415 		attach_va, attach_va + size, VM_INHERIT_SHARE);
416 
417 	shmmap_s->va = attach_va;
418 	shmmap_s->shmid = shmid;
419 	shmseg->u.shm_lpid = p->p_pid;
420 	shmseg->u.shm_atime = time_second;
421 	shmseg->u.shm_nattch++;
422 	td->td_retval[0] = attach_va;
423 done2:
424 	mtx_unlock(&Giant);
425 	return (error);
426 }
427 
428 int
429 shmat(td, uap)
430 	struct thread *td;
431 	struct shmat_args *uap;
432 {
433 	return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
434 }
435 
436 int
437 kern_shmctl(td, shmid, cmd, buf, bufsz)
438 	struct thread *td;
439 	int shmid;
440 	int cmd;
441 	void *buf;
442 	size_t *bufsz;
443 {
444 	int error = 0;
445 	struct shmid_kernel *shmseg;
446 
447 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
448 		return (ENOSYS);
449 
450 	mtx_lock(&Giant);
451 	switch (cmd) {
452 	/*
453 	 * It is possible that kern_shmctl is being called from the Linux ABI
454 	 * layer, in which case, we will need to implement IPC_INFO.  It should
455 	 * be noted that other shmctl calls will be funneled through here for
456 	 * Linix binaries as well.
457 	 *
458 	 * NB: The Linux ABI layer will convert this data to structure(s) more
459 	 * consistent with the Linux ABI.
460 	 */
461 	case IPC_INFO:
462 		memcpy(buf, &shminfo, sizeof(shminfo));
463 		if (bufsz)
464 			*bufsz = sizeof(shminfo);
465 		td->td_retval[0] = shmalloced;
466 		goto done2;
467 	case SHM_INFO: {
468 		struct shm_info shm_info;
469 		shm_info.used_ids = shm_nused;
470 		shm_info.shm_rss = 0;	/*XXX where to get from ? */
471 		shm_info.shm_tot = 0;	/*XXX where to get from ? */
472 		shm_info.shm_swp = 0;	/*XXX where to get from ? */
473 		shm_info.swap_attempts = 0;	/*XXX where to get from ? */
474 		shm_info.swap_successes = 0;	/*XXX where to get from ? */
475 		memcpy(buf, &shm_info, sizeof(shm_info));
476 		if (bufsz)
477 			*bufsz = sizeof(shm_info);
478 		td->td_retval[0] = shmalloced;
479 		goto done2;
480 	}
481 	}
482 	if (cmd == SHM_STAT)
483 		shmseg = shm_find_segment_by_shmidx(shmid);
484 	else
485 		shmseg = shm_find_segment_by_shmid(shmid);
486 	if (shmseg == NULL) {
487 		error = EINVAL;
488 		goto done2;
489 	}
490 #ifdef MAC
491 	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
492 	if (error != 0)
493 		goto done2;
494 #endif
495 	switch (cmd) {
496 	case SHM_STAT:
497 	case IPC_STAT:
498 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
499 		if (error)
500 			goto done2;
501 		memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
502 		if (bufsz)
503 			*bufsz = sizeof(struct shmid_ds);
504 		if (cmd == SHM_STAT)
505 			td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm);
506 		break;
507 	case IPC_SET: {
508 		struct shmid_ds *shmid;
509 
510 		shmid = (struct shmid_ds *)buf;
511 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
512 		if (error)
513 			goto done2;
514 		shmseg->u.shm_perm.uid = shmid->shm_perm.uid;
515 		shmseg->u.shm_perm.gid = shmid->shm_perm.gid;
516 		shmseg->u.shm_perm.mode =
517 		    (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
518 		    (shmid->shm_perm.mode & ACCESSPERMS);
519 		shmseg->u.shm_ctime = time_second;
520 		break;
521 	}
522 	case IPC_RMID:
523 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
524 		if (error)
525 			goto done2;
526 		shmseg->u.shm_perm.key = IPC_PRIVATE;
527 		shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
528 		if (shmseg->u.shm_nattch <= 0) {
529 			shm_deallocate_segment(shmseg);
530 			shm_last_free = IPCID_TO_IX(shmid);
531 		}
532 		break;
533 #if 0
534 	case SHM_LOCK:
535 	case SHM_UNLOCK:
536 #endif
537 	default:
538 		error = EINVAL;
539 		break;
540 	}
541 done2:
542 	mtx_unlock(&Giant);
543 	return (error);
544 }
545 
546 #ifndef _SYS_SYSPROTO_H_
547 struct shmctl_args {
548 	int shmid;
549 	int cmd;
550 	struct shmid_ds *buf;
551 };
552 #endif
553 int
554 shmctl(td, uap)
555 	struct thread *td;
556 	struct shmctl_args *uap;
557 {
558 	int error = 0;
559 	struct shmid_ds buf;
560 	size_t bufsz;
561 
562 	/*
563 	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
564 	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
565 	 * return an error back to the user since we do not to support this.
566 	 */
567 	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
568 	    uap->cmd == SHM_STAT)
569 		return (EINVAL);
570 
571 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
572 	if (uap->cmd == IPC_SET) {
573 		if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
574 			goto done;
575 	}
576 
577 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
578 	if (error)
579 		goto done;
580 
581 	/* Cases in which we need to copyout */
582 	switch (uap->cmd) {
583 	case IPC_STAT:
584 		error = copyout(&buf, uap->buf, bufsz);
585 		break;
586 	}
587 
588 done:
589 	if (error) {
590 		/* Invalidate the return value */
591 		td->td_retval[0] = -1;
592 	}
593 	return (error);
594 }
595 
596 
597 static int
598 shmget_existing(td, uap, mode, segnum)
599 	struct thread *td;
600 	struct shmget_args *uap;
601 	int mode;
602 	int segnum;
603 {
604 	struct shmid_kernel *shmseg;
605 	int error;
606 
607 	shmseg = &shmsegs[segnum];
608 	if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) {
609 		/*
610 		 * This segment is in the process of being allocated.  Wait
611 		 * until it's done, and look the key up again (in case the
612 		 * allocation failed or it was freed).
613 		 */
614 		shmseg->u.shm_perm.mode |= SHMSEG_WANTED;
615 		error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
616 		if (error)
617 			return (error);
618 		return (EAGAIN);
619 	}
620 	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
621 		return (EEXIST);
622 #ifdef MAC
623 	error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
624 	if (error != 0)
625 		return (error);
626 #endif
627 	if (uap->size != 0 && uap->size > shmseg->shm_bsegsz)
628 		return (EINVAL);
629 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
630 	return (0);
631 }
632 
633 static int
634 shmget_allocate_segment(td, uap, mode)
635 	struct thread *td;
636 	struct shmget_args *uap;
637 	int mode;
638 {
639 	int i, segnum, shmid;
640 	size_t size;
641 	struct ucred *cred = td->td_ucred;
642 	struct shmid_kernel *shmseg;
643 	vm_object_t shm_object;
644 
645 	GIANT_REQUIRED;
646 
647 	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
648 		return (EINVAL);
649 	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
650 		return (ENOSPC);
651 	size = round_page(uap->size);
652 	if (shm_committed + btoc(size) > shminfo.shmall)
653 		return (ENOMEM);
654 	if (shm_last_free < 0) {
655 		shmrealloc();	/* Maybe expand the shmsegs[] array. */
656 		for (i = 0; i < shmalloced; i++)
657 			if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
658 				break;
659 		if (i == shmalloced)
660 			return (ENOSPC);
661 		segnum = i;
662 	} else  {
663 		segnum = shm_last_free;
664 		shm_last_free = -1;
665 	}
666 	shmseg = &shmsegs[segnum];
667 	/*
668 	 * In case we sleep in malloc(), mark the segment present but deleted
669 	 * so that noone else tries to create the same key.
670 	 */
671 	shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
672 	shmseg->u.shm_perm.key = uap->key;
673 	shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
674 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
675 
676 	/*
677 	 * We make sure that we have allocated a pager before we need
678 	 * to.
679 	 */
680 	shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
681 	    0, size, VM_PROT_DEFAULT, 0, cred);
682 	if (shm_object == NULL)
683 		return (ENOMEM);
684 	VM_OBJECT_LOCK(shm_object);
685 	vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
686 	vm_object_set_flag(shm_object, OBJ_NOSPLIT);
687 	VM_OBJECT_UNLOCK(shm_object);
688 
689 	shmseg->u.shm_internal = shm_object;
690 	shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
691 	shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
692 	shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) |
693 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
694 	shmseg->u.shm_segsz = uap->size;
695 	shmseg->shm_bsegsz = uap->size;
696 	shmseg->u.shm_cpid = td->td_proc->p_pid;
697 	shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
698 	shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
699 #ifdef MAC
700 	mac_sysvshm_create(cred, shmseg);
701 #endif
702 	shmseg->u.shm_ctime = time_second;
703 	shm_committed += btoc(size);
704 	shm_nused++;
705 	if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) {
706 		/*
707 		 * Somebody else wanted this key while we were asleep.  Wake
708 		 * them up now.
709 		 */
710 		shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED;
711 		wakeup(shmseg);
712 	}
713 	td->td_retval[0] = shmid;
714 	return (0);
715 }
716 
717 #ifndef _SYS_SYSPROTO_H_
718 struct shmget_args {
719 	key_t key;
720 	size_t size;
721 	int shmflg;
722 };
723 #endif
724 int
725 shmget(td, uap)
726 	struct thread *td;
727 	struct shmget_args *uap;
728 {
729 	int segnum, mode;
730 	int error;
731 
732 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
733 		return (ENOSYS);
734 	mtx_lock(&Giant);
735 	mode = uap->shmflg & ACCESSPERMS;
736 	if (uap->key != IPC_PRIVATE) {
737 	again:
738 		segnum = shm_find_segment_by_key(uap->key);
739 		if (segnum >= 0) {
740 			error = shmget_existing(td, uap, mode, segnum);
741 			if (error == EAGAIN)
742 				goto again;
743 			goto done2;
744 		}
745 		if ((uap->shmflg & IPC_CREAT) == 0) {
746 			error = ENOENT;
747 			goto done2;
748 		}
749 	}
750 	error = shmget_allocate_segment(td, uap, mode);
751 done2:
752 	mtx_unlock(&Giant);
753 	return (error);
754 }
755 
756 static void
757 shmfork_myhook(p1, p2)
758 	struct proc *p1, *p2;
759 {
760 	struct shmmap_state *shmmap_s;
761 	size_t size;
762 	int i;
763 
764 	mtx_lock(&Giant);
765 	size = shminfo.shmseg * sizeof(struct shmmap_state);
766 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
767 	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
768 	p2->p_vmspace->vm_shm = shmmap_s;
769 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
770 		if (shmmap_s->shmid != -1)
771 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
772 	mtx_unlock(&Giant);
773 }
774 
775 static void
776 shmexit_myhook(struct vmspace *vm)
777 {
778 	struct shmmap_state *base, *shm;
779 	int i;
780 
781 	if ((base = vm->vm_shm) != NULL) {
782 		vm->vm_shm = NULL;
783 		mtx_lock(&Giant);
784 		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
785 			if (shm->shmid != -1)
786 				shm_delete_mapping(vm, shm);
787 		}
788 		mtx_unlock(&Giant);
789 		free(base, M_SHM);
790 	}
791 }
792 
793 static void
794 shmrealloc(void)
795 {
796 	int i;
797 	struct shmid_kernel *newsegs;
798 
799 	if (shmalloced >= shminfo.shmmni)
800 		return;
801 
802 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
803 	if (newsegs == NULL)
804 		return;
805 	for (i = 0; i < shmalloced; i++)
806 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
807 	for (; i < shminfo.shmmni; i++) {
808 		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
809 		shmsegs[i].u.shm_perm.seq = 0;
810 #ifdef MAC
811 		mac_sysvshm_init(&shmsegs[i]);
812 #endif
813 	}
814 	free(shmsegs, M_SHM);
815 	shmsegs = newsegs;
816 	shmalloced = shminfo.shmmni;
817 }
818 
819 static void
820 shminit()
821 {
822 	int i;
823 
824 	TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
825 	for (i = PAGE_SIZE; i > 0; i--) {
826 		shminfo.shmmax = shminfo.shmall * i;
827 		if (shminfo.shmmax >= shminfo.shmall)
828 			break;
829 	}
830 	TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
831 	TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
832 	TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
833 	TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
834 
835 	shmalloced = shminfo.shmmni;
836 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
837 	if (shmsegs == NULL)
838 		panic("cannot allocate initial memory for sysvshm");
839 	for (i = 0; i < shmalloced; i++) {
840 		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
841 		shmsegs[i].u.shm_perm.seq = 0;
842 #ifdef MAC
843 		mac_sysvshm_init(&shmsegs[i]);
844 #endif
845 	}
846 	shm_last_free = 0;
847 	shm_nused = 0;
848 	shm_committed = 0;
849 	shmexit_hook = &shmexit_myhook;
850 	shmfork_hook = &shmfork_myhook;
851 }
852 
853 static int
854 shmunload()
855 {
856 #ifdef MAC
857 	int i;
858 #endif
859 
860 	if (shm_nused > 0)
861 		return (EBUSY);
862 
863 #ifdef MAC
864 	for (i = 0; i < shmalloced; i++)
865 		mac_sysvshm_destroy(&shmsegs[i]);
866 #endif
867 	free(shmsegs, M_SHM);
868 	shmexit_hook = NULL;
869 	shmfork_hook = NULL;
870 	return (0);
871 }
872 
873 static int
874 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
875 {
876 
877 	return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
878 }
879 
880 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
881 struct oshmid_ds {
882 	struct	ipc_perm_old shm_perm;	/* operation perms */
883 	int	shm_segsz;		/* size of segment (bytes) */
884 	u_short	shm_cpid;		/* pid, creator */
885 	u_short	shm_lpid;		/* pid, last operation */
886 	short	shm_nattch;		/* no. of current attaches */
887 	time_t	shm_atime;		/* last attach time */
888 	time_t	shm_dtime;		/* last detach time */
889 	time_t	shm_ctime;		/* last change time */
890 	void	*shm_handle;		/* internal handle for shm segment */
891 };
892 
893 struct oshmctl_args {
894 	int shmid;
895 	int cmd;
896 	struct oshmid_ds *ubuf;
897 };
898 
899 static int
900 oshmctl(td, uap)
901 	struct thread *td;
902 	struct oshmctl_args *uap;
903 {
904 #ifdef COMPAT_43
905 	int error = 0;
906 	struct shmid_kernel *shmseg;
907 	struct oshmid_ds outbuf;
908 
909 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
910 		return (ENOSYS);
911 	mtx_lock(&Giant);
912 	shmseg = shm_find_segment_by_shmid(uap->shmid);
913 	if (shmseg == NULL) {
914 		error = EINVAL;
915 		goto done2;
916 	}
917 	switch (uap->cmd) {
918 	case IPC_STAT:
919 		error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
920 		if (error)
921 			goto done2;
922 #ifdef MAC
923 		error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
924 		if (error != 0)
925 			goto done2;
926 #endif
927 		ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
928 		outbuf.shm_segsz = shmseg->u.shm_segsz;
929 		outbuf.shm_cpid = shmseg->u.shm_cpid;
930 		outbuf.shm_lpid = shmseg->u.shm_lpid;
931 		outbuf.shm_nattch = shmseg->u.shm_nattch;
932 		outbuf.shm_atime = shmseg->u.shm_atime;
933 		outbuf.shm_dtime = shmseg->u.shm_dtime;
934 		outbuf.shm_ctime = shmseg->u.shm_ctime;
935 		outbuf.shm_handle = shmseg->object;
936 		error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
937 		if (error)
938 			goto done2;
939 		break;
940 	default:
941 		error = freebsd7_shmctl(td, (struct shmctl_args *)uap);
942 		break;
943 	}
944 done2:
945 	mtx_unlock(&Giant);
946 	return (error);
947 #else
948 	return (EINVAL);
949 #endif
950 }
951 
952 /* XXX casting to (sy_call_t *) is bogus, as usual. */
953 static sy_call_t *shmcalls[] = {
954 	(sy_call_t *)shmat, (sy_call_t *)oshmctl,
955 	(sy_call_t *)shmdt, (sy_call_t *)shmget,
956 	(sy_call_t *)shmctl
957 };
958 
959 int
960 shmsys(td, uap)
961 	struct thread *td;
962 	/* XXX actually varargs. */
963 	struct shmsys_args /* {
964 		int	which;
965 		int	a2;
966 		int	a3;
967 		int	a4;
968 	} */ *uap;
969 {
970 	int error;
971 
972 	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
973 		return (ENOSYS);
974 	if (uap->which < 0 ||
975 	    uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
976 		return (EINVAL);
977 	mtx_lock(&Giant);
978 	error = (*shmcalls[uap->which])(td, &uap->a2);
979 	mtx_unlock(&Giant);
980 	return (error);
981 }
982 
983 SYSCALL_MODULE_HELPER(shmsys);
984 #endif	/* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
985 
986 static int
987 sysvshm_modload(struct module *module, int cmd, void *arg)
988 {
989 	int error = 0;
990 
991 	switch (cmd) {
992 	case MOD_LOAD:
993 		shminit();
994 		break;
995 	case MOD_UNLOAD:
996 		error = shmunload();
997 		break;
998 	case MOD_SHUTDOWN:
999 		break;
1000 	default:
1001 		error = EINVAL;
1002 		break;
1003 	}
1004 	return (error);
1005 }
1006 
1007 static moduledata_t sysvshm_mod = {
1008 	"sysvshm",
1009 	&sysvshm_modload,
1010 	NULL
1011 };
1012 
1013 SYSCALL_MODULE_HELPER(shmat);
1014 SYSCALL_MODULE_HELPER(shmctl);
1015 SYSCALL_MODULE_HELPER(shmdt);
1016 SYSCALL_MODULE_HELPER(shmget);
1017 
1018 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1019 MODULE_VERSION(sysvshm, 1);
1020