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/limits.h> 72 #include <sys/lock.h> 73 #include <sys/sysctl.h> 74 #include <sys/shm.h> 75 #include <sys/proc.h> 76 #include <sys/malloc.h> 77 #include <sys/mman.h> 78 #include <sys/module.h> 79 #include <sys/mutex.h> 80 #include <sys/racct.h> 81 #include <sys/resourcevar.h> 82 #include <sys/rwlock.h> 83 #include <sys/stat.h> 84 #include <sys/syscall.h> 85 #include <sys/syscallsubr.h> 86 #include <sys/sysent.h> 87 #include <sys/sysproto.h> 88 #include <sys/jail.h> 89 90 #include <security/mac/mac_framework.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_param.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_object.h> 96 #include <vm/vm_map.h> 97 #include <vm/vm_page.h> 98 #include <vm/vm_pager.h> 99 100 FEATURE(sysv_shm, "System V shared memory segments support"); 101 102 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments"); 103 104 static int shmget_allocate_segment(struct thread *td, 105 struct shmget_args *uap, int mode); 106 static int shmget_existing(struct thread *td, struct shmget_args *uap, 107 int mode, int segnum); 108 109 #define SHMSEG_FREE 0x0200 110 #define SHMSEG_REMOVED 0x0400 111 #define SHMSEG_ALLOCATED 0x0800 112 #define SHMSEG_WANTED 0x1000 113 114 static int shm_last_free, shm_nused, shmalloced; 115 vm_size_t shm_committed; 116 static struct shmid_kernel *shmsegs; 117 118 struct shmmap_state { 119 vm_offset_t va; 120 int shmid; 121 }; 122 123 static void shm_deallocate_segment(struct shmid_kernel *); 124 static int shm_find_segment_by_key(key_t); 125 static struct shmid_kernel *shm_find_segment_by_shmid(int); 126 static struct shmid_kernel *shm_find_segment_by_shmidx(int); 127 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *); 128 static void shmrealloc(void); 129 static int shminit(void); 130 static int sysvshm_modload(struct module *, int, void *); 131 static int shmunload(void); 132 static void shmexit_myhook(struct vmspace *vm); 133 static void shmfork_myhook(struct proc *p1, struct proc *p2); 134 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS); 135 136 /* 137 * Tuneable values. 138 */ 139 #ifndef SHMMAXPGS 140 #define SHMMAXPGS 131072 /* Note: sysv shared memory is swap backed. */ 141 #endif 142 #ifndef SHMMAX 143 #define SHMMAX (SHMMAXPGS*PAGE_SIZE) 144 #endif 145 #ifndef SHMMIN 146 #define SHMMIN 1 147 #endif 148 #ifndef SHMMNI 149 #define SHMMNI 192 150 #endif 151 #ifndef SHMSEG 152 #define SHMSEG 128 153 #endif 154 #ifndef SHMALL 155 #define SHMALL (SHMMAXPGS) 156 #endif 157 158 struct shminfo shminfo = { 159 .shmmax = SHMMAX, 160 .shmmin = SHMMIN, 161 .shmmni = SHMMNI, 162 .shmseg = SHMSEG, 163 .shmall = SHMALL 164 }; 165 166 static int shm_use_phys; 167 static int shm_allow_removed; 168 169 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0, 170 "Maximum shared memory segment size"); 171 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0, 172 "Minimum shared memory segment size"); 173 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0, 174 "Number of shared memory identifiers"); 175 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0, 176 "Number of segments per process"); 177 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0, 178 "Maximum number of pages available for shared memory"); 179 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN, 180 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core"); 181 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN, 182 &shm_allow_removed, 0, 183 "Enable/Disable attachment to attached segments marked for removal"); 184 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD, 185 NULL, 0, sysctl_shmsegs, "", 186 "Current number of shared memory segments allocated"); 187 188 static int 189 shm_find_segment_by_key(key) 190 key_t key; 191 { 192 int i; 193 194 for (i = 0; i < shmalloced; i++) 195 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) && 196 shmsegs[i].u.shm_perm.key == key) 197 return (i); 198 return (-1); 199 } 200 201 static struct shmid_kernel * 202 shm_find_segment_by_shmid(int shmid) 203 { 204 int segnum; 205 struct shmid_kernel *shmseg; 206 207 segnum = IPCID_TO_IX(shmid); 208 if (segnum < 0 || segnum >= shmalloced) 209 return (NULL); 210 shmseg = &shmsegs[segnum]; 211 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 || 212 (!shm_allow_removed && 213 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) || 214 shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid)) 215 return (NULL); 216 return (shmseg); 217 } 218 219 static struct shmid_kernel * 220 shm_find_segment_by_shmidx(int segnum) 221 { 222 struct shmid_kernel *shmseg; 223 224 if (segnum < 0 || segnum >= shmalloced) 225 return (NULL); 226 shmseg = &shmsegs[segnum]; 227 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 || 228 (!shm_allow_removed && 229 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0)) 230 return (NULL); 231 return (shmseg); 232 } 233 234 static void 235 shm_deallocate_segment(shmseg) 236 struct shmid_kernel *shmseg; 237 { 238 vm_size_t size; 239 240 GIANT_REQUIRED; 241 242 vm_object_deallocate(shmseg->object); 243 shmseg->object = NULL; 244 size = round_page(shmseg->u.shm_segsz); 245 shm_committed -= btoc(size); 246 shm_nused--; 247 shmseg->u.shm_perm.mode = SHMSEG_FREE; 248 #ifdef MAC 249 mac_sysvshm_cleanup(shmseg); 250 #endif 251 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1); 252 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size); 253 crfree(shmseg->cred); 254 shmseg->cred = NULL; 255 } 256 257 static int 258 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s) 259 { 260 struct shmid_kernel *shmseg; 261 int segnum, result; 262 vm_size_t size; 263 264 GIANT_REQUIRED; 265 266 segnum = IPCID_TO_IX(shmmap_s->shmid); 267 shmseg = &shmsegs[segnum]; 268 size = round_page(shmseg->u.shm_segsz); 269 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size); 270 if (result != KERN_SUCCESS) 271 return (EINVAL); 272 shmmap_s->shmid = -1; 273 shmseg->u.shm_dtime = time_second; 274 if ((--shmseg->u.shm_nattch <= 0) && 275 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) { 276 shm_deallocate_segment(shmseg); 277 shm_last_free = segnum; 278 } 279 return (0); 280 } 281 282 #ifndef _SYS_SYSPROTO_H_ 283 struct shmdt_args { 284 const void *shmaddr; 285 }; 286 #endif 287 int 288 sys_shmdt(td, uap) 289 struct thread *td; 290 struct shmdt_args *uap; 291 { 292 struct proc *p = td->td_proc; 293 struct shmmap_state *shmmap_s; 294 #ifdef MAC 295 struct shmid_kernel *shmsegptr; 296 #endif 297 int i; 298 int error = 0; 299 300 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 301 return (ENOSYS); 302 mtx_lock(&Giant); 303 shmmap_s = p->p_vmspace->vm_shm; 304 if (shmmap_s == NULL) { 305 error = EINVAL; 306 goto done2; 307 } 308 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) { 309 if (shmmap_s->shmid != -1 && 310 shmmap_s->va == (vm_offset_t)uap->shmaddr) { 311 break; 312 } 313 } 314 if (i == shminfo.shmseg) { 315 error = EINVAL; 316 goto done2; 317 } 318 #ifdef MAC 319 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)]; 320 error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr); 321 if (error != 0) 322 goto done2; 323 #endif 324 error = shm_delete_mapping(p->p_vmspace, shmmap_s); 325 done2: 326 mtx_unlock(&Giant); 327 return (error); 328 } 329 330 #ifndef _SYS_SYSPROTO_H_ 331 struct shmat_args { 332 int shmid; 333 const void *shmaddr; 334 int shmflg; 335 }; 336 #endif 337 int 338 kern_shmat(td, shmid, shmaddr, shmflg) 339 struct thread *td; 340 int shmid; 341 const void *shmaddr; 342 int shmflg; 343 { 344 struct proc *p = td->td_proc; 345 int i; 346 struct shmid_kernel *shmseg; 347 struct shmmap_state *shmmap_s = NULL; 348 vm_offset_t attach_va; 349 vm_prot_t prot; 350 vm_size_t size; 351 int rv; 352 int error = 0; 353 354 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 355 return (ENOSYS); 356 mtx_lock(&Giant); 357 shmmap_s = p->p_vmspace->vm_shm; 358 if (shmmap_s == NULL) { 359 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state), 360 M_SHM, M_WAITOK); 361 for (i = 0; i < shminfo.shmseg; i++) 362 shmmap_s[i].shmid = -1; 363 p->p_vmspace->vm_shm = shmmap_s; 364 } 365 shmseg = shm_find_segment_by_shmid(shmid); 366 if (shmseg == NULL) { 367 error = EINVAL; 368 goto done2; 369 } 370 error = ipcperm(td, &shmseg->u.shm_perm, 371 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); 372 if (error) 373 goto done2; 374 #ifdef MAC 375 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg); 376 if (error != 0) 377 goto done2; 378 #endif 379 for (i = 0; i < shminfo.shmseg; i++) { 380 if (shmmap_s->shmid == -1) 381 break; 382 shmmap_s++; 383 } 384 if (i >= shminfo.shmseg) { 385 error = EMFILE; 386 goto done2; 387 } 388 size = round_page(shmseg->u.shm_segsz); 389 prot = VM_PROT_READ; 390 if ((shmflg & SHM_RDONLY) == 0) 391 prot |= VM_PROT_WRITE; 392 if (shmaddr) { 393 if (shmflg & SHM_RND) { 394 attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1); 395 } else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) { 396 attach_va = (vm_offset_t)shmaddr; 397 } else { 398 error = EINVAL; 399 goto done2; 400 } 401 } else { 402 /* 403 * This is just a hint to vm_map_find() about where to 404 * put it. 405 */ 406 PROC_LOCK(p); 407 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr + 408 lim_max(p, RLIMIT_DATA)); 409 PROC_UNLOCK(p); 410 } 411 412 vm_object_reference(shmseg->object); 413 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va, 414 size, 0, shmaddr != NULL ? VMFS_NO_SPACE : VMFS_OPTIMAL_SPACE, 415 prot, prot, MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL); 416 if (rv != KERN_SUCCESS) { 417 vm_object_deallocate(shmseg->object); 418 error = ENOMEM; 419 goto done2; 420 } 421 422 shmmap_s->va = attach_va; 423 shmmap_s->shmid = shmid; 424 shmseg->u.shm_lpid = p->p_pid; 425 shmseg->u.shm_atime = time_second; 426 shmseg->u.shm_nattch++; 427 td->td_retval[0] = attach_va; 428 done2: 429 mtx_unlock(&Giant); 430 return (error); 431 } 432 433 int 434 sys_shmat(td, uap) 435 struct thread *td; 436 struct shmat_args *uap; 437 { 438 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg); 439 } 440 441 int 442 kern_shmctl(td, shmid, cmd, buf, bufsz) 443 struct thread *td; 444 int shmid; 445 int cmd; 446 void *buf; 447 size_t *bufsz; 448 { 449 int error = 0; 450 struct shmid_kernel *shmseg; 451 452 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 453 return (ENOSYS); 454 455 mtx_lock(&Giant); 456 switch (cmd) { 457 /* 458 * It is possible that kern_shmctl is being called from the Linux ABI 459 * layer, in which case, we will need to implement IPC_INFO. It should 460 * be noted that other shmctl calls will be funneled through here for 461 * Linix binaries as well. 462 * 463 * NB: The Linux ABI layer will convert this data to structure(s) more 464 * consistent with the Linux ABI. 465 */ 466 case IPC_INFO: 467 memcpy(buf, &shminfo, sizeof(shminfo)); 468 if (bufsz) 469 *bufsz = sizeof(shminfo); 470 td->td_retval[0] = shmalloced; 471 goto done2; 472 case SHM_INFO: { 473 struct shm_info shm_info; 474 shm_info.used_ids = shm_nused; 475 shm_info.shm_rss = 0; /*XXX where to get from ? */ 476 shm_info.shm_tot = 0; /*XXX where to get from ? */ 477 shm_info.shm_swp = 0; /*XXX where to get from ? */ 478 shm_info.swap_attempts = 0; /*XXX where to get from ? */ 479 shm_info.swap_successes = 0; /*XXX where to get from ? */ 480 memcpy(buf, &shm_info, sizeof(shm_info)); 481 if (bufsz) 482 *bufsz = sizeof(shm_info); 483 td->td_retval[0] = shmalloced; 484 goto done2; 485 } 486 } 487 if (cmd == SHM_STAT) 488 shmseg = shm_find_segment_by_shmidx(shmid); 489 else 490 shmseg = shm_find_segment_by_shmid(shmid); 491 if (shmseg == NULL) { 492 error = EINVAL; 493 goto done2; 494 } 495 #ifdef MAC 496 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd); 497 if (error != 0) 498 goto done2; 499 #endif 500 switch (cmd) { 501 case SHM_STAT: 502 case IPC_STAT: 503 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R); 504 if (error) 505 goto done2; 506 memcpy(buf, &shmseg->u, sizeof(struct shmid_ds)); 507 if (bufsz) 508 *bufsz = sizeof(struct shmid_ds); 509 if (cmd == SHM_STAT) 510 td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm); 511 break; 512 case IPC_SET: { 513 struct shmid_ds *shmid; 514 515 shmid = (struct shmid_ds *)buf; 516 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M); 517 if (error) 518 goto done2; 519 shmseg->u.shm_perm.uid = shmid->shm_perm.uid; 520 shmseg->u.shm_perm.gid = shmid->shm_perm.gid; 521 shmseg->u.shm_perm.mode = 522 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) | 523 (shmid->shm_perm.mode & ACCESSPERMS); 524 shmseg->u.shm_ctime = time_second; 525 break; 526 } 527 case IPC_RMID: 528 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M); 529 if (error) 530 goto done2; 531 shmseg->u.shm_perm.key = IPC_PRIVATE; 532 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED; 533 if (shmseg->u.shm_nattch <= 0) { 534 shm_deallocate_segment(shmseg); 535 shm_last_free = IPCID_TO_IX(shmid); 536 } 537 break; 538 #if 0 539 case SHM_LOCK: 540 case SHM_UNLOCK: 541 #endif 542 default: 543 error = EINVAL; 544 break; 545 } 546 done2: 547 mtx_unlock(&Giant); 548 return (error); 549 } 550 551 #ifndef _SYS_SYSPROTO_H_ 552 struct shmctl_args { 553 int shmid; 554 int cmd; 555 struct shmid_ds *buf; 556 }; 557 #endif 558 int 559 sys_shmctl(td, uap) 560 struct thread *td; 561 struct shmctl_args *uap; 562 { 563 int error = 0; 564 struct shmid_ds buf; 565 size_t bufsz; 566 567 /* 568 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support 569 * Linux binaries. If we see the call come through the FreeBSD ABI, 570 * return an error back to the user since we do not to support this. 571 */ 572 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO || 573 uap->cmd == SHM_STAT) 574 return (EINVAL); 575 576 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */ 577 if (uap->cmd == IPC_SET) { 578 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds)))) 579 goto done; 580 } 581 582 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz); 583 if (error) 584 goto done; 585 586 /* Cases in which we need to copyout */ 587 switch (uap->cmd) { 588 case IPC_STAT: 589 error = copyout(&buf, uap->buf, bufsz); 590 break; 591 } 592 593 done: 594 if (error) { 595 /* Invalidate the return value */ 596 td->td_retval[0] = -1; 597 } 598 return (error); 599 } 600 601 602 static int 603 shmget_existing(td, uap, mode, segnum) 604 struct thread *td; 605 struct shmget_args *uap; 606 int mode; 607 int segnum; 608 { 609 struct shmid_kernel *shmseg; 610 int error; 611 612 shmseg = &shmsegs[segnum]; 613 if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) { 614 /* 615 * This segment is in the process of being allocated. Wait 616 * until it's done, and look the key up again (in case the 617 * allocation failed or it was freed). 618 */ 619 shmseg->u.shm_perm.mode |= SHMSEG_WANTED; 620 error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0); 621 if (error) 622 return (error); 623 return (EAGAIN); 624 } 625 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) 626 return (EEXIST); 627 #ifdef MAC 628 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg); 629 if (error != 0) 630 return (error); 631 #endif 632 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz) 633 return (EINVAL); 634 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm); 635 return (0); 636 } 637 638 static int 639 shmget_allocate_segment(td, uap, mode) 640 struct thread *td; 641 struct shmget_args *uap; 642 int mode; 643 { 644 int i, segnum, shmid; 645 size_t size; 646 struct ucred *cred = td->td_ucred; 647 struct shmid_kernel *shmseg; 648 vm_object_t shm_object; 649 650 GIANT_REQUIRED; 651 652 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax) 653 return (EINVAL); 654 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */ 655 return (ENOSPC); 656 size = round_page(uap->size); 657 if (shm_committed + btoc(size) > shminfo.shmall) 658 return (ENOMEM); 659 if (shm_last_free < 0) { 660 shmrealloc(); /* Maybe expand the shmsegs[] array. */ 661 for (i = 0; i < shmalloced; i++) 662 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE) 663 break; 664 if (i == shmalloced) 665 return (ENOSPC); 666 segnum = i; 667 } else { 668 segnum = shm_last_free; 669 shm_last_free = -1; 670 } 671 shmseg = &shmsegs[segnum]; 672 #ifdef RACCT 673 PROC_LOCK(td->td_proc); 674 if (racct_add(td->td_proc, RACCT_NSHM, 1)) { 675 PROC_UNLOCK(td->td_proc); 676 return (ENOSPC); 677 } 678 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) { 679 racct_sub(td->td_proc, RACCT_NSHM, 1); 680 PROC_UNLOCK(td->td_proc); 681 return (ENOMEM); 682 } 683 PROC_UNLOCK(td->td_proc); 684 #endif 685 /* 686 * In case we sleep in malloc(), mark the segment present but deleted 687 * so that noone else tries to create the same key. 688 */ 689 shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 690 shmseg->u.shm_perm.key = uap->key; 691 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff; 692 shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm); 693 694 /* 695 * We make sure that we have allocated a pager before we need 696 * to. 697 */ 698 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP, 699 0, size, VM_PROT_DEFAULT, 0, cred); 700 if (shm_object == NULL) { 701 #ifdef RACCT 702 PROC_LOCK(td->td_proc); 703 racct_sub(td->td_proc, RACCT_NSHM, 1); 704 racct_sub(td->td_proc, RACCT_SHMSIZE, size); 705 PROC_UNLOCK(td->td_proc); 706 #endif 707 return (ENOMEM); 708 } 709 shm_object->pg_color = 0; 710 VM_OBJECT_WLOCK(shm_object); 711 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING); 712 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT); 713 VM_OBJECT_WUNLOCK(shm_object); 714 715 shmseg->object = shm_object; 716 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid; 717 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid; 718 shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) | 719 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; 720 shmseg->cred = crhold(cred); 721 shmseg->u.shm_segsz = uap->size; 722 shmseg->u.shm_cpid = td->td_proc->p_pid; 723 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0; 724 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0; 725 #ifdef MAC 726 mac_sysvshm_create(cred, shmseg); 727 #endif 728 shmseg->u.shm_ctime = time_second; 729 shm_committed += btoc(size); 730 shm_nused++; 731 if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) { 732 /* 733 * Somebody else wanted this key while we were asleep. Wake 734 * them up now. 735 */ 736 shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED; 737 wakeup(shmseg); 738 } 739 td->td_retval[0] = shmid; 740 return (0); 741 } 742 743 #ifndef _SYS_SYSPROTO_H_ 744 struct shmget_args { 745 key_t key; 746 size_t size; 747 int shmflg; 748 }; 749 #endif 750 int 751 sys_shmget(td, uap) 752 struct thread *td; 753 struct shmget_args *uap; 754 { 755 int segnum, mode; 756 int error; 757 758 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 759 return (ENOSYS); 760 mtx_lock(&Giant); 761 mode = uap->shmflg & ACCESSPERMS; 762 if (uap->key != IPC_PRIVATE) { 763 again: 764 segnum = shm_find_segment_by_key(uap->key); 765 if (segnum >= 0) { 766 error = shmget_existing(td, uap, mode, segnum); 767 if (error == EAGAIN) 768 goto again; 769 goto done2; 770 } 771 if ((uap->shmflg & IPC_CREAT) == 0) { 772 error = ENOENT; 773 goto done2; 774 } 775 } 776 error = shmget_allocate_segment(td, uap, mode); 777 done2: 778 mtx_unlock(&Giant); 779 return (error); 780 } 781 782 static void 783 shmfork_myhook(p1, p2) 784 struct proc *p1, *p2; 785 { 786 struct shmmap_state *shmmap_s; 787 size_t size; 788 int i; 789 790 mtx_lock(&Giant); 791 size = shminfo.shmseg * sizeof(struct shmmap_state); 792 shmmap_s = malloc(size, M_SHM, M_WAITOK); 793 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size); 794 p2->p_vmspace->vm_shm = shmmap_s; 795 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 796 if (shmmap_s->shmid != -1) 797 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++; 798 mtx_unlock(&Giant); 799 } 800 801 static void 802 shmexit_myhook(struct vmspace *vm) 803 { 804 struct shmmap_state *base, *shm; 805 int i; 806 807 if ((base = vm->vm_shm) != NULL) { 808 vm->vm_shm = NULL; 809 mtx_lock(&Giant); 810 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) { 811 if (shm->shmid != -1) 812 shm_delete_mapping(vm, shm); 813 } 814 mtx_unlock(&Giant); 815 free(base, M_SHM); 816 } 817 } 818 819 static void 820 shmrealloc(void) 821 { 822 int i; 823 struct shmid_kernel *newsegs; 824 825 if (shmalloced >= shminfo.shmmni) 826 return; 827 828 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK); 829 if (newsegs == NULL) 830 return; 831 for (i = 0; i < shmalloced; i++) 832 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0])); 833 for (; i < shminfo.shmmni; i++) { 834 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE; 835 shmsegs[i].u.shm_perm.seq = 0; 836 #ifdef MAC 837 mac_sysvshm_init(&shmsegs[i]); 838 #endif 839 } 840 free(shmsegs, M_SHM); 841 shmsegs = newsegs; 842 shmalloced = shminfo.shmmni; 843 } 844 845 static struct syscall_helper_data shm_syscalls[] = { 846 SYSCALL_INIT_HELPER(shmat), 847 SYSCALL_INIT_HELPER(shmctl), 848 SYSCALL_INIT_HELPER(shmdt), 849 SYSCALL_INIT_HELPER(shmget), 850 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 851 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 852 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl), 853 #endif 854 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43)) 855 SYSCALL_INIT_HELPER(shmsys), 856 #endif 857 SYSCALL_INIT_LAST 858 }; 859 860 #ifdef COMPAT_FREEBSD32 861 #include <compat/freebsd32/freebsd32.h> 862 #include <compat/freebsd32/freebsd32_ipc.h> 863 #include <compat/freebsd32/freebsd32_proto.h> 864 #include <compat/freebsd32/freebsd32_signal.h> 865 #include <compat/freebsd32/freebsd32_syscall.h> 866 #include <compat/freebsd32/freebsd32_util.h> 867 868 static struct syscall_helper_data shm32_syscalls[] = { 869 SYSCALL32_INIT_HELPER_COMPAT(shmat), 870 SYSCALL32_INIT_HELPER_COMPAT(shmdt), 871 SYSCALL32_INIT_HELPER_COMPAT(shmget), 872 SYSCALL32_INIT_HELPER(freebsd32_shmsys), 873 SYSCALL32_INIT_HELPER(freebsd32_shmctl), 874 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 875 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 876 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl), 877 #endif 878 SYSCALL_INIT_LAST 879 }; 880 #endif 881 882 static int 883 shminit() 884 { 885 int i, error; 886 887 #ifndef BURN_BRIDGES 888 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0) 889 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n"); 890 #endif 891 if (shminfo.shmmax == SHMMAX) { 892 /* Initialize shmmax dealing with possible overflow. */ 893 for (i = PAGE_SIZE; i != 0; i--) { 894 shminfo.shmmax = shminfo.shmall * i; 895 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i) 896 break; 897 } 898 } 899 shmalloced = shminfo.shmmni; 900 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK); 901 for (i = 0; i < shmalloced; i++) { 902 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE; 903 shmsegs[i].u.shm_perm.seq = 0; 904 #ifdef MAC 905 mac_sysvshm_init(&shmsegs[i]); 906 #endif 907 } 908 shm_last_free = 0; 909 shm_nused = 0; 910 shm_committed = 0; 911 shmexit_hook = &shmexit_myhook; 912 shmfork_hook = &shmfork_myhook; 913 914 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD); 915 if (error != 0) 916 return (error); 917 #ifdef COMPAT_FREEBSD32 918 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD); 919 if (error != 0) 920 return (error); 921 #endif 922 return (0); 923 } 924 925 static int 926 shmunload() 927 { 928 int i; 929 930 if (shm_nused > 0) 931 return (EBUSY); 932 933 #ifdef COMPAT_FREEBSD32 934 syscall32_helper_unregister(shm32_syscalls); 935 #endif 936 syscall_helper_unregister(shm_syscalls); 937 938 for (i = 0; i < shmalloced; i++) { 939 #ifdef MAC 940 mac_sysvshm_destroy(&shmsegs[i]); 941 #endif 942 /* 943 * Objects might be still mapped into the processes 944 * address spaces. Actual free would happen on the 945 * last mapping destruction. 946 */ 947 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE) 948 vm_object_deallocate(shmsegs[i].object); 949 } 950 free(shmsegs, M_SHM); 951 shmexit_hook = NULL; 952 shmfork_hook = NULL; 953 return (0); 954 } 955 956 static int 957 sysctl_shmsegs(SYSCTL_HANDLER_ARGS) 958 { 959 960 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0]))); 961 } 962 963 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43)) 964 struct oshmid_ds { 965 struct ipc_perm_old shm_perm; /* operation perms */ 966 int shm_segsz; /* size of segment (bytes) */ 967 u_short shm_cpid; /* pid, creator */ 968 u_short shm_lpid; /* pid, last operation */ 969 short shm_nattch; /* no. of current attaches */ 970 time_t shm_atime; /* last attach time */ 971 time_t shm_dtime; /* last detach time */ 972 time_t shm_ctime; /* last change time */ 973 void *shm_handle; /* internal handle for shm segment */ 974 }; 975 976 struct oshmctl_args { 977 int shmid; 978 int cmd; 979 struct oshmid_ds *ubuf; 980 }; 981 982 static int 983 oshmctl(struct thread *td, struct oshmctl_args *uap) 984 { 985 #ifdef COMPAT_43 986 int error = 0; 987 struct shmid_kernel *shmseg; 988 struct oshmid_ds outbuf; 989 990 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 991 return (ENOSYS); 992 mtx_lock(&Giant); 993 shmseg = shm_find_segment_by_shmid(uap->shmid); 994 if (shmseg == NULL) { 995 error = EINVAL; 996 goto done2; 997 } 998 switch (uap->cmd) { 999 case IPC_STAT: 1000 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R); 1001 if (error) 1002 goto done2; 1003 #ifdef MAC 1004 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd); 1005 if (error != 0) 1006 goto done2; 1007 #endif 1008 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm); 1009 outbuf.shm_segsz = shmseg->u.shm_segsz; 1010 outbuf.shm_cpid = shmseg->u.shm_cpid; 1011 outbuf.shm_lpid = shmseg->u.shm_lpid; 1012 outbuf.shm_nattch = shmseg->u.shm_nattch; 1013 outbuf.shm_atime = shmseg->u.shm_atime; 1014 outbuf.shm_dtime = shmseg->u.shm_dtime; 1015 outbuf.shm_ctime = shmseg->u.shm_ctime; 1016 outbuf.shm_handle = shmseg->object; 1017 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf)); 1018 if (error) 1019 goto done2; 1020 break; 1021 default: 1022 error = freebsd7_shmctl(td, (struct freebsd7_shmctl_args *)uap); 1023 break; 1024 } 1025 done2: 1026 mtx_unlock(&Giant); 1027 return (error); 1028 #else 1029 return (EINVAL); 1030 #endif 1031 } 1032 1033 /* XXX casting to (sy_call_t *) is bogus, as usual. */ 1034 static sy_call_t *shmcalls[] = { 1035 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl, 1036 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget, 1037 (sy_call_t *)freebsd7_shmctl 1038 }; 1039 1040 int 1041 sys_shmsys(td, uap) 1042 struct thread *td; 1043 /* XXX actually varargs. */ 1044 struct shmsys_args /* { 1045 int which; 1046 int a2; 1047 int a3; 1048 int a4; 1049 } */ *uap; 1050 { 1051 int error; 1052 1053 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC)) 1054 return (ENOSYS); 1055 if (uap->which < 0 || 1056 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0])) 1057 return (EINVAL); 1058 mtx_lock(&Giant); 1059 error = (*shmcalls[uap->which])(td, &uap->a2); 1060 mtx_unlock(&Giant); 1061 return (error); 1062 } 1063 1064 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */ 1065 1066 #ifdef COMPAT_FREEBSD32 1067 1068 int 1069 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap) 1070 { 1071 1072 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1073 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1074 switch (uap->which) { 1075 case 0: { /* shmat */ 1076 struct shmat_args ap; 1077 1078 ap.shmid = uap->a2; 1079 ap.shmaddr = PTRIN(uap->a3); 1080 ap.shmflg = uap->a4; 1081 return (sysent[SYS_shmat].sy_call(td, &ap)); 1082 } 1083 case 2: { /* shmdt */ 1084 struct shmdt_args ap; 1085 1086 ap.shmaddr = PTRIN(uap->a2); 1087 return (sysent[SYS_shmdt].sy_call(td, &ap)); 1088 } 1089 case 3: { /* shmget */ 1090 struct shmget_args ap; 1091 1092 ap.key = uap->a2; 1093 ap.size = uap->a3; 1094 ap.shmflg = uap->a4; 1095 return (sysent[SYS_shmget].sy_call(td, &ap)); 1096 } 1097 case 4: { /* shmctl */ 1098 struct freebsd7_freebsd32_shmctl_args ap; 1099 1100 ap.shmid = uap->a2; 1101 ap.cmd = uap->a3; 1102 ap.buf = PTRIN(uap->a4); 1103 return (freebsd7_freebsd32_shmctl(td, &ap)); 1104 } 1105 case 1: /* oshmctl */ 1106 default: 1107 return (EINVAL); 1108 } 1109 #else 1110 return (nosys(td, NULL)); 1111 #endif 1112 } 1113 1114 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1115 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1116 int 1117 freebsd7_freebsd32_shmctl(struct thread *td, 1118 struct freebsd7_freebsd32_shmctl_args *uap) 1119 { 1120 int error = 0; 1121 union { 1122 struct shmid_ds shmid_ds; 1123 struct shm_info shm_info; 1124 struct shminfo shminfo; 1125 } u; 1126 union { 1127 struct shmid_ds32_old shmid_ds32; 1128 struct shm_info32 shm_info32; 1129 struct shminfo32 shminfo32; 1130 } u32; 1131 size_t sz; 1132 1133 if (uap->cmd == IPC_SET) { 1134 if ((error = copyin(uap->buf, &u32.shmid_ds32, 1135 sizeof(u32.shmid_ds32)))) 1136 goto done; 1137 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm, 1138 &u.shmid_ds.shm_perm); 1139 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz); 1140 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid); 1141 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid); 1142 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch); 1143 CP(u32.shmid_ds32, u.shmid_ds, shm_atime); 1144 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime); 1145 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime); 1146 } 1147 1148 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz); 1149 if (error) 1150 goto done; 1151 1152 /* Cases in which we need to copyout */ 1153 switch (uap->cmd) { 1154 case IPC_INFO: 1155 CP(u.shminfo, u32.shminfo32, shmmax); 1156 CP(u.shminfo, u32.shminfo32, shmmin); 1157 CP(u.shminfo, u32.shminfo32, shmmni); 1158 CP(u.shminfo, u32.shminfo32, shmseg); 1159 CP(u.shminfo, u32.shminfo32, shmall); 1160 error = copyout(&u32.shminfo32, uap->buf, 1161 sizeof(u32.shminfo32)); 1162 break; 1163 case SHM_INFO: 1164 CP(u.shm_info, u32.shm_info32, used_ids); 1165 CP(u.shm_info, u32.shm_info32, shm_rss); 1166 CP(u.shm_info, u32.shm_info32, shm_tot); 1167 CP(u.shm_info, u32.shm_info32, shm_swp); 1168 CP(u.shm_info, u32.shm_info32, swap_attempts); 1169 CP(u.shm_info, u32.shm_info32, swap_successes); 1170 error = copyout(&u32.shm_info32, uap->buf, 1171 sizeof(u32.shm_info32)); 1172 break; 1173 case SHM_STAT: 1174 case IPC_STAT: 1175 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm, 1176 &u32.shmid_ds32.shm_perm); 1177 if (u.shmid_ds.shm_segsz > INT32_MAX) 1178 u32.shmid_ds32.shm_segsz = INT32_MAX; 1179 else 1180 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz); 1181 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid); 1182 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid); 1183 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch); 1184 CP(u.shmid_ds, u32.shmid_ds32, shm_atime); 1185 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime); 1186 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime); 1187 u32.shmid_ds32.shm_internal = 0; 1188 error = copyout(&u32.shmid_ds32, uap->buf, 1189 sizeof(u32.shmid_ds32)); 1190 break; 1191 } 1192 1193 done: 1194 if (error) { 1195 /* Invalidate the return value */ 1196 td->td_retval[0] = -1; 1197 } 1198 return (error); 1199 } 1200 #endif 1201 1202 int 1203 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap) 1204 { 1205 int error = 0; 1206 union { 1207 struct shmid_ds shmid_ds; 1208 struct shm_info shm_info; 1209 struct shminfo shminfo; 1210 } u; 1211 union { 1212 struct shmid_ds32 shmid_ds32; 1213 struct shm_info32 shm_info32; 1214 struct shminfo32 shminfo32; 1215 } u32; 1216 size_t sz; 1217 1218 if (uap->cmd == IPC_SET) { 1219 if ((error = copyin(uap->buf, &u32.shmid_ds32, 1220 sizeof(u32.shmid_ds32)))) 1221 goto done; 1222 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm, 1223 &u.shmid_ds.shm_perm); 1224 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz); 1225 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid); 1226 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid); 1227 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch); 1228 CP(u32.shmid_ds32, u.shmid_ds, shm_atime); 1229 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime); 1230 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime); 1231 } 1232 1233 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz); 1234 if (error) 1235 goto done; 1236 1237 /* Cases in which we need to copyout */ 1238 switch (uap->cmd) { 1239 case IPC_INFO: 1240 CP(u.shminfo, u32.shminfo32, shmmax); 1241 CP(u.shminfo, u32.shminfo32, shmmin); 1242 CP(u.shminfo, u32.shminfo32, shmmni); 1243 CP(u.shminfo, u32.shminfo32, shmseg); 1244 CP(u.shminfo, u32.shminfo32, shmall); 1245 error = copyout(&u32.shminfo32, uap->buf, 1246 sizeof(u32.shminfo32)); 1247 break; 1248 case SHM_INFO: 1249 CP(u.shm_info, u32.shm_info32, used_ids); 1250 CP(u.shm_info, u32.shm_info32, shm_rss); 1251 CP(u.shm_info, u32.shm_info32, shm_tot); 1252 CP(u.shm_info, u32.shm_info32, shm_swp); 1253 CP(u.shm_info, u32.shm_info32, swap_attempts); 1254 CP(u.shm_info, u32.shm_info32, swap_successes); 1255 error = copyout(&u32.shm_info32, uap->buf, 1256 sizeof(u32.shm_info32)); 1257 break; 1258 case SHM_STAT: 1259 case IPC_STAT: 1260 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm, 1261 &u32.shmid_ds32.shm_perm); 1262 if (u.shmid_ds.shm_segsz > INT32_MAX) 1263 u32.shmid_ds32.shm_segsz = INT32_MAX; 1264 else 1265 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz); 1266 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid); 1267 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid); 1268 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch); 1269 CP(u.shmid_ds, u32.shmid_ds32, shm_atime); 1270 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime); 1271 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime); 1272 error = copyout(&u32.shmid_ds32, uap->buf, 1273 sizeof(u32.shmid_ds32)); 1274 break; 1275 } 1276 1277 done: 1278 if (error) { 1279 /* Invalidate the return value */ 1280 td->td_retval[0] = -1; 1281 } 1282 return (error); 1283 } 1284 #endif 1285 1286 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1287 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1288 1289 #ifndef CP 1290 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0) 1291 #endif 1292 1293 #ifndef _SYS_SYSPROTO_H_ 1294 struct freebsd7_shmctl_args { 1295 int shmid; 1296 int cmd; 1297 struct shmid_ds_old *buf; 1298 }; 1299 #endif 1300 int 1301 freebsd7_shmctl(td, uap) 1302 struct thread *td; 1303 struct freebsd7_shmctl_args *uap; 1304 { 1305 int error = 0; 1306 struct shmid_ds_old old; 1307 struct shmid_ds buf; 1308 size_t bufsz; 1309 1310 /* 1311 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support 1312 * Linux binaries. If we see the call come through the FreeBSD ABI, 1313 * return an error back to the user since we do not to support this. 1314 */ 1315 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO || 1316 uap->cmd == SHM_STAT) 1317 return (EINVAL); 1318 1319 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */ 1320 if (uap->cmd == IPC_SET) { 1321 if ((error = copyin(uap->buf, &old, sizeof(old)))) 1322 goto done; 1323 ipcperm_old2new(&old.shm_perm, &buf.shm_perm); 1324 CP(old, buf, shm_segsz); 1325 CP(old, buf, shm_lpid); 1326 CP(old, buf, shm_cpid); 1327 CP(old, buf, shm_nattch); 1328 CP(old, buf, shm_atime); 1329 CP(old, buf, shm_dtime); 1330 CP(old, buf, shm_ctime); 1331 } 1332 1333 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz); 1334 if (error) 1335 goto done; 1336 1337 /* Cases in which we need to copyout */ 1338 switch (uap->cmd) { 1339 case IPC_STAT: 1340 ipcperm_new2old(&buf.shm_perm, &old.shm_perm); 1341 if (buf.shm_segsz > INT_MAX) 1342 old.shm_segsz = INT_MAX; 1343 else 1344 CP(buf, old, shm_segsz); 1345 CP(buf, old, shm_lpid); 1346 CP(buf, old, shm_cpid); 1347 if (buf.shm_nattch > SHRT_MAX) 1348 old.shm_nattch = SHRT_MAX; 1349 else 1350 CP(buf, old, shm_nattch); 1351 CP(buf, old, shm_atime); 1352 CP(buf, old, shm_dtime); 1353 CP(buf, old, shm_ctime); 1354 old.shm_internal = NULL; 1355 error = copyout(&old, uap->buf, sizeof(old)); 1356 break; 1357 } 1358 1359 done: 1360 if (error) { 1361 /* Invalidate the return value */ 1362 td->td_retval[0] = -1; 1363 } 1364 return (error); 1365 } 1366 1367 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 || 1368 COMPAT_FREEBSD7 */ 1369 1370 static int 1371 sysvshm_modload(struct module *module, int cmd, void *arg) 1372 { 1373 int error = 0; 1374 1375 switch (cmd) { 1376 case MOD_LOAD: 1377 error = shminit(); 1378 if (error != 0) 1379 shmunload(); 1380 break; 1381 case MOD_UNLOAD: 1382 error = shmunload(); 1383 break; 1384 case MOD_SHUTDOWN: 1385 break; 1386 default: 1387 error = EINVAL; 1388 break; 1389 } 1390 return (error); 1391 } 1392 1393 static moduledata_t sysvshm_mod = { 1394 "sysvshm", 1395 &sysvshm_modload, 1396 NULL 1397 }; 1398 1399 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST); 1400 MODULE_VERSION(sysvshm, 1); 1401