sysv_shm.c revision 76166
1/* $FreeBSD: head/sys/kern/sysv_shm.c 76166 2001-05-01 08:13:21Z markm $ */ 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/stat.h> 48#include <sys/syscall.h> 49#include <sys/sysent.h> 50#include <sys/sysproto.h> 51#include <sys/jail.h> 52 53#include <vm/vm.h> 54#include <vm/vm_param.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 61static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments"); 62 63struct oshmctl_args; 64static int oshmctl __P((struct proc *p, struct oshmctl_args *uap)); 65 66static int shmget_allocate_segment __P((struct proc *p, struct shmget_args *uap, int mode)); 67static 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. */ 70static 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 81static int shm_last_free, shm_nused, shm_committed, shmalloced; 82static struct shmid_ds *shmsegs; 83 84struct shm_handle { 85 /* vm_offset_t kva; */ 86 vm_object_t shm_object; 87}; 88 89struct shmmap_state { 90 vm_offset_t va; 91 int shmid; 92}; 93 94static void shm_deallocate_segment __P((struct shmid_ds *)); 95static int shm_find_segment_by_key __P((key_t)); 96static struct shmid_ds *shm_find_segment_by_shmid __P((int)); 97static int shm_delete_mapping __P((struct proc *, struct shmmap_state *)); 98static void shmrealloc __P((void)); 99static void shminit __P((void)); 100static int sysvshm_modload __P((struct module *, int, void *)); 101static int shmunload __P((void)); 102static void shmexit_myhook __P((struct proc *p)); 103static 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 127struct shminfo shminfo = { 128 SHMMAX, 129 SHMMIN, 130 SHMMNI, 131 SHMSEG, 132 SHMALL 133}; 134 135static int shm_use_phys; 136 137SYSCTL_DECL(_kern_ipc); 138SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, ""); 139SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, ""); 140SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, ""); 141SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RD, &shminfo.shmseg, 0, ""); 142SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, ""); 143SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, ""); 144 145static int 146shm_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 158static struct shmid_ds * 159shm_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 176static void 177shm_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 193static int 194shm_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_ 219struct shmdt_args { 220 void *shmaddr; 221}; 222#endif 223 224int 225shmdt(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 && jailed(p->p_ucred)) 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_ 248struct shmat_args { 249 int shmid; 250 void *shmaddr; 251 int shmflg; 252}; 253#endif 254 255int 256shmat(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 && jailed(p->p_ucred)) 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 335struct 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 347struct oshmctl_args { 348 int shmid; 349 int cmd; 350 struct oshmid_ds *ubuf; 351}; 352 353static int 354oshmctl(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 && jailed(p->p_ucred)) 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_ 398struct shmctl_args { 399 int shmid; 400 int cmd; 401 struct shmid_ds *buf; 402}; 403#endif 404 405int 406shmctl(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 && jailed(p->p_ucred)) 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_ 465struct shmget_args { 466 key_t key; 467 size_t size; 468 int shmflg; 469}; 470#endif 471 472static int 473shmget_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 506static int 507shmget_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 586int 587shmget(p, uap) 588 struct proc *p; 589 struct shmget_args *uap; 590{ 591 int segnum, mode, error; 592 593 if (!jail_sysvipc_allowed && jailed(p->p_ucred)) 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 612int 613shmsys(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 && jailed(p->p_ucred)) 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 632static void 633shmfork_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 649static void 650shmexit_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 664static void 665shmrealloc(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 687static void 688shminit() 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 707static int 708shmunload() 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 720static int 721sysvshm_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 741static moduledata_t sysvshm_mod = { 742 "sysvshm", 743 &sysvshm_modload, 744 NULL 745}; 746 747SYSCALL_MODULE_HELPER(shmsys, 4); 748SYSCALL_MODULE_HELPER(shmat, 3); 749SYSCALL_MODULE_HELPER(shmctl, 3); 750SYSCALL_MODULE_HELPER(shmdt, 1); 751SYSCALL_MODULE_HELPER(shmget, 3); 752 753DECLARE_MODULE(sysvshm, sysvshm_mod, 754 SI_SUB_SYSV_SHM, SI_ORDER_FIRST); 755MODULE_VERSION(sysvshm, 1); 756