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