1 /*- 2 * Implementation of SVID semaphores 3 * 4 * Author: Daniel Boulet 5 * 6 * This software is provided ``AS IS'' without any warranties of any kind. 7 */ 8 /*- 9 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 10 * 11 * Copyright (c) 2003-2005 McAfee, Inc. 12 * Copyright (c) 2016-2017 Robert N. M. Watson 13 * All rights reserved. 14 * 15 * This software was developed for the FreeBSD Project in part by McAfee 16 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR 17 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research 18 * program. 19 * 20 * Portions of this software were developed by BAE Systems, the University of 21 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 22 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 23 * Computing (TC) research program. 24 * 25 * Redistribution and use in source and binary forms, with or without 26 * modification, are permitted provided that the following conditions 27 * are met: 28 * 1. Redistributions of source code must retain the above copyright 29 * notice, this list of conditions and the following disclaimer. 30 * 2. Redistributions in binary form must reproduce the above copyright 31 * notice, this list of conditions and the following disclaimer in the 32 * documentation and/or other materials provided with the distribution. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include "opt_compat.h" 51 #include "opt_sysvipc.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/sysproto.h> 56 #include <sys/eventhandler.h> 57 #include <sys/kernel.h> 58 #include <sys/proc.h> 59 #include <sys/lock.h> 60 #include <sys/module.h> 61 #include <sys/mutex.h> 62 #include <sys/racct.h> 63 #include <sys/sem.h> 64 #include <sys/sx.h> 65 #include <sys/syscall.h> 66 #include <sys/syscallsubr.h> 67 #include <sys/sysent.h> 68 #include <sys/sysctl.h> 69 #include <sys/uio.h> 70 #include <sys/malloc.h> 71 #include <sys/jail.h> 72 73 #include <security/audit/audit.h> 74 #include <security/mac/mac_framework.h> 75 76 FEATURE(sysv_sem, "System V semaphores support"); 77 78 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores"); 79 80 #ifdef SEM_DEBUG 81 #define DPRINTF(a) printf a 82 #else 83 #define DPRINTF(a) 84 #endif 85 86 static int seminit(void); 87 static int sysvsem_modload(struct module *, int, void *); 88 static int semunload(void); 89 static void semexit_myhook(void *arg, struct proc *p); 90 static int sysctl_sema(SYSCTL_HANDLER_ARGS); 91 static int semvalid(int semid, struct prison *rpr, 92 struct semid_kernel *semakptr); 93 static void sem_remove(int semidx, struct ucred *cred); 94 static struct prison *sem_find_prison(struct ucred *); 95 static int sem_prison_cansee(struct prison *, struct semid_kernel *); 96 static int sem_prison_check(void *, void *); 97 static int sem_prison_set(void *, void *); 98 static int sem_prison_get(void *, void *); 99 static int sem_prison_remove(void *, void *); 100 static void sem_prison_cleanup(struct prison *); 101 102 #ifndef _SYS_SYSPROTO_H_ 103 struct __semctl_args; 104 int __semctl(struct thread *td, struct __semctl_args *uap); 105 struct semget_args; 106 int semget(struct thread *td, struct semget_args *uap); 107 struct semop_args; 108 int semop(struct thread *td, struct semop_args *uap); 109 #endif 110 111 static struct sem_undo *semu_alloc(struct thread *td); 112 static int semundo_adjust(struct thread *td, struct sem_undo **supptr, 113 int semid, int semseq, int semnum, int adjval); 114 static void semundo_clear(int semid, int semnum); 115 116 static struct mtx sem_mtx; /* semaphore global lock */ 117 static struct mtx sem_undo_mtx; 118 static int semtot = 0; 119 static struct semid_kernel *sema; /* semaphore id pool */ 120 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/ 121 static struct sem *sem; /* semaphore pool */ 122 LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */ 123 LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */ 124 static int *semu; /* undo structure pool */ 125 static eventhandler_tag semexit_tag; 126 static unsigned sem_prison_slot; /* prison OSD slot */ 127 128 #define SEMUNDO_MTX sem_undo_mtx 129 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX); 130 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX); 131 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how)); 132 133 struct sem { 134 u_short semval; /* semaphore value */ 135 pid_t sempid; /* pid of last operation */ 136 u_short semncnt; /* # awaiting semval > cval */ 137 u_short semzcnt; /* # awaiting semval = 0 */ 138 }; 139 140 /* 141 * Undo structure (one per process) 142 */ 143 struct sem_undo { 144 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */ 145 struct proc *un_proc; /* owner of this structure */ 146 short un_cnt; /* # of active entries */ 147 struct undo { 148 short un_adjval; /* adjust on exit values */ 149 short un_num; /* semaphore # */ 150 int un_id; /* semid */ 151 unsigned short un_seq; 152 } un_ent[1]; /* undo entries */ 153 }; 154 155 /* 156 * Configuration parameters 157 */ 158 #ifndef SEMMNI 159 #define SEMMNI 50 /* # of semaphore identifiers */ 160 #endif 161 #ifndef SEMMNS 162 #define SEMMNS 340 /* # of semaphores in system */ 163 #endif 164 #ifndef SEMUME 165 #define SEMUME 50 /* max # of undo entries per process */ 166 #endif 167 #ifndef SEMMNU 168 #define SEMMNU 150 /* # of undo structures in system */ 169 #endif 170 171 /* shouldn't need tuning */ 172 #ifndef SEMMSL 173 #define SEMMSL SEMMNS /* max # of semaphores per id */ 174 #endif 175 #ifndef SEMOPM 176 #define SEMOPM 100 /* max # of operations per semop call */ 177 #endif 178 179 #define SEMVMX 32767 /* semaphore maximum value */ 180 #define SEMAEM 16384 /* adjust on exit max value */ 181 182 /* 183 * Due to the way semaphore memory is allocated, we have to ensure that 184 * SEMUSZ is properly aligned. 185 */ 186 187 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long)) 188 189 /* actual size of an undo structure */ 190 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME])) 191 192 /* 193 * Macro to find a particular sem_undo vector 194 */ 195 #define SEMU(ix) \ 196 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz)) 197 198 /* 199 * semaphore info struct 200 */ 201 struct seminfo seminfo = { 202 SEMMNI, /* # of semaphore identifiers */ 203 SEMMNS, /* # of semaphores in system */ 204 SEMMNU, /* # of undo structures in system */ 205 SEMMSL, /* max # of semaphores per id */ 206 SEMOPM, /* max # of operations per semop call */ 207 SEMUME, /* max # of undo entries per process */ 208 SEMUSZ, /* size in bytes of undo structure */ 209 SEMVMX, /* semaphore maximum value */ 210 SEMAEM /* adjust on exit max value */ 211 }; 212 213 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0, 214 "Number of semaphore identifiers"); 215 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0, 216 "Maximum number of semaphores in the system"); 217 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0, 218 "Maximum number of undo structures in the system"); 219 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RWTUN, &seminfo.semmsl, 0, 220 "Max semaphores per id"); 221 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0, 222 "Max operations per semop call"); 223 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0, 224 "Max undo entries per process"); 225 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0, 226 "Size in bytes of undo structure"); 227 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RWTUN, &seminfo.semvmx, 0, 228 "Semaphore maximum value"); 229 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &seminfo.semaem, 0, 230 "Adjust on exit max value"); 231 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, 232 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 233 NULL, 0, sysctl_sema, "", 234 "Array of struct semid_kernel for each potential semaphore"); 235 236 static struct syscall_helper_data sem_syscalls[] = { 237 SYSCALL_INIT_HELPER(__semctl), 238 SYSCALL_INIT_HELPER(semget), 239 SYSCALL_INIT_HELPER(semop), 240 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 241 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 242 SYSCALL_INIT_HELPER(semsys), 243 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl), 244 #endif 245 SYSCALL_INIT_LAST 246 }; 247 248 #ifdef COMPAT_FREEBSD32 249 #include <compat/freebsd32/freebsd32.h> 250 #include <compat/freebsd32/freebsd32_ipc.h> 251 #include <compat/freebsd32/freebsd32_proto.h> 252 #include <compat/freebsd32/freebsd32_signal.h> 253 #include <compat/freebsd32/freebsd32_syscall.h> 254 #include <compat/freebsd32/freebsd32_util.h> 255 256 static struct syscall_helper_data sem32_syscalls[] = { 257 SYSCALL32_INIT_HELPER(freebsd32_semctl), 258 SYSCALL32_INIT_HELPER_COMPAT(semget), 259 SYSCALL32_INIT_HELPER_COMPAT(semop), 260 SYSCALL32_INIT_HELPER(freebsd32_semsys), 261 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 262 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 263 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl), 264 #endif 265 SYSCALL_INIT_LAST 266 }; 267 #endif 268 269 static int 270 seminit(void) 271 { 272 struct prison *pr; 273 void **rsv; 274 int i, error; 275 osd_method_t methods[PR_MAXMETHOD] = { 276 [PR_METHOD_CHECK] = sem_prison_check, 277 [PR_METHOD_SET] = sem_prison_set, 278 [PR_METHOD_GET] = sem_prison_get, 279 [PR_METHOD_REMOVE] = sem_prison_remove, 280 }; 281 282 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK); 283 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM, 284 M_WAITOK | M_ZERO); 285 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM, 286 M_WAITOK | M_ZERO); 287 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK); 288 289 for (i = 0; i < seminfo.semmni; i++) { 290 sema[i].u.__sem_base = 0; 291 sema[i].u.sem_perm.mode = 0; 292 sema[i].u.sem_perm.seq = 0; 293 #ifdef MAC 294 mac_sysvsem_init(&sema[i]); 295 #endif 296 } 297 for (i = 0; i < seminfo.semmni; i++) 298 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF); 299 LIST_INIT(&semu_free_list); 300 for (i = 0; i < seminfo.semmnu; i++) { 301 struct sem_undo *suptr = SEMU(i); 302 suptr->un_proc = NULL; 303 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 304 } 305 LIST_INIT(&semu_list); 306 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF); 307 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF); 308 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL, 309 EVENTHANDLER_PRI_ANY); 310 311 /* Set current prisons according to their allow.sysvipc. */ 312 sem_prison_slot = osd_jail_register(NULL, methods); 313 rsv = osd_reserve(sem_prison_slot); 314 prison_lock(&prison0); 315 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0); 316 prison_unlock(&prison0); 317 rsv = NULL; 318 sx_slock(&allprison_lock); 319 TAILQ_FOREACH(pr, &allprison, pr_list) { 320 if (rsv == NULL) 321 rsv = osd_reserve(sem_prison_slot); 322 prison_lock(pr); 323 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) { 324 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv, 325 &prison0); 326 rsv = NULL; 327 } 328 prison_unlock(pr); 329 } 330 if (rsv != NULL) 331 osd_free_reserved(rsv); 332 sx_sunlock(&allprison_lock); 333 334 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD); 335 if (error != 0) 336 return (error); 337 #ifdef COMPAT_FREEBSD32 338 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD); 339 if (error != 0) 340 return (error); 341 #endif 342 return (0); 343 } 344 345 static int 346 semunload(void) 347 { 348 int i; 349 350 /* XXXKIB */ 351 if (semtot != 0) 352 return (EBUSY); 353 354 #ifdef COMPAT_FREEBSD32 355 syscall32_helper_unregister(sem32_syscalls); 356 #endif 357 syscall_helper_unregister(sem_syscalls); 358 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag); 359 if (sem_prison_slot != 0) 360 osd_jail_deregister(sem_prison_slot); 361 #ifdef MAC 362 for (i = 0; i < seminfo.semmni; i++) 363 mac_sysvsem_destroy(&sema[i]); 364 #endif 365 free(sem, M_SEM); 366 free(sema, M_SEM); 367 free(semu, M_SEM); 368 for (i = 0; i < seminfo.semmni; i++) 369 mtx_destroy(&sema_mtx[i]); 370 free(sema_mtx, M_SEM); 371 mtx_destroy(&sem_mtx); 372 mtx_destroy(&sem_undo_mtx); 373 return (0); 374 } 375 376 static int 377 sysvsem_modload(struct module *module, int cmd, void *arg) 378 { 379 int error = 0; 380 381 switch (cmd) { 382 case MOD_LOAD: 383 error = seminit(); 384 if (error != 0) 385 semunload(); 386 break; 387 case MOD_UNLOAD: 388 error = semunload(); 389 break; 390 case MOD_SHUTDOWN: 391 break; 392 default: 393 error = EINVAL; 394 break; 395 } 396 return (error); 397 } 398 399 static moduledata_t sysvsem_mod = { 400 "sysvsem", 401 &sysvsem_modload, 402 NULL 403 }; 404 405 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST); 406 MODULE_VERSION(sysvsem, 1); 407 408 /* 409 * Allocate a new sem_undo structure for a process 410 * (returns ptr to structure or NULL if no more room) 411 */ 412 413 static struct sem_undo * 414 semu_alloc(struct thread *td) 415 { 416 struct sem_undo *suptr; 417 418 SEMUNDO_LOCKASSERT(MA_OWNED); 419 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL) 420 return (NULL); 421 LIST_REMOVE(suptr, un_next); 422 LIST_INSERT_HEAD(&semu_list, suptr, un_next); 423 suptr->un_cnt = 0; 424 suptr->un_proc = td->td_proc; 425 return (suptr); 426 } 427 428 static int 429 semu_try_free(struct sem_undo *suptr) 430 { 431 432 SEMUNDO_LOCKASSERT(MA_OWNED); 433 434 if (suptr->un_cnt != 0) 435 return (0); 436 LIST_REMOVE(suptr, un_next); 437 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 438 return (1); 439 } 440 441 /* 442 * Adjust a particular entry for a particular proc 443 */ 444 445 static int 446 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid, 447 int semseq, int semnum, int adjval) 448 { 449 struct proc *p = td->td_proc; 450 struct sem_undo *suptr; 451 struct undo *sunptr; 452 int i; 453 454 SEMUNDO_LOCKASSERT(MA_OWNED); 455 /* Look for and remember the sem_undo if the caller doesn't provide 456 it */ 457 458 suptr = *supptr; 459 if (suptr == NULL) { 460 LIST_FOREACH(suptr, &semu_list, un_next) { 461 if (suptr->un_proc == p) { 462 *supptr = suptr; 463 break; 464 } 465 } 466 if (suptr == NULL) { 467 if (adjval == 0) 468 return(0); 469 suptr = semu_alloc(td); 470 if (suptr == NULL) 471 return (ENOSPC); 472 *supptr = suptr; 473 } 474 } 475 476 /* 477 * Look for the requested entry and adjust it (delete if adjval becomes 478 * 0). 479 */ 480 sunptr = &suptr->un_ent[0]; 481 for (i = 0; i < suptr->un_cnt; i++, sunptr++) { 482 if (sunptr->un_id != semid || sunptr->un_num != semnum) 483 continue; 484 if (adjval != 0) { 485 adjval += sunptr->un_adjval; 486 if (adjval > seminfo.semaem || adjval < -seminfo.semaem) 487 return (ERANGE); 488 } 489 sunptr->un_adjval = adjval; 490 if (sunptr->un_adjval == 0) { 491 suptr->un_cnt--; 492 if (i < suptr->un_cnt) 493 suptr->un_ent[i] = 494 suptr->un_ent[suptr->un_cnt]; 495 if (suptr->un_cnt == 0) 496 semu_try_free(suptr); 497 } 498 return (0); 499 } 500 501 /* Didn't find the right entry - create it */ 502 if (adjval == 0) 503 return (0); 504 if (adjval > seminfo.semaem || adjval < -seminfo.semaem) 505 return (ERANGE); 506 if (suptr->un_cnt != seminfo.semume) { 507 sunptr = &suptr->un_ent[suptr->un_cnt]; 508 suptr->un_cnt++; 509 sunptr->un_adjval = adjval; 510 sunptr->un_id = semid; 511 sunptr->un_num = semnum; 512 sunptr->un_seq = semseq; 513 } else 514 return (EINVAL); 515 return (0); 516 } 517 518 static void 519 semundo_clear(int semid, int semnum) 520 { 521 struct sem_undo *suptr, *suptr1; 522 struct undo *sunptr; 523 int i; 524 525 SEMUNDO_LOCKASSERT(MA_OWNED); 526 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) { 527 sunptr = &suptr->un_ent[0]; 528 for (i = 0; i < suptr->un_cnt; i++, sunptr++) { 529 if (sunptr->un_id != semid) 530 continue; 531 if (semnum == -1 || sunptr->un_num == semnum) { 532 suptr->un_cnt--; 533 if (i < suptr->un_cnt) { 534 suptr->un_ent[i] = 535 suptr->un_ent[suptr->un_cnt]; 536 continue; 537 } 538 semu_try_free(suptr); 539 } 540 if (semnum != -1) 541 break; 542 } 543 } 544 } 545 546 static int 547 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr) 548 { 549 550 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 551 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) || 552 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0); 553 } 554 555 static void 556 sem_remove(int semidx, struct ucred *cred) 557 { 558 struct semid_kernel *semakptr; 559 int i; 560 561 KASSERT(semidx >= 0 && semidx < seminfo.semmni, 562 ("semidx out of bounds")); 563 semakptr = &sema[semidx]; 564 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0; 565 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0; 566 semakptr->u.sem_perm.mode = 0; 567 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems); 568 crfree(semakptr->cred); 569 semakptr->cred = NULL; 570 SEMUNDO_LOCK(); 571 semundo_clear(semidx, -1); 572 SEMUNDO_UNLOCK(); 573 #ifdef MAC 574 mac_sysvsem_cleanup(semakptr); 575 #endif 576 wakeup(semakptr); 577 for (i = 0; i < seminfo.semmni; i++) { 578 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 579 sema[i].u.__sem_base > semakptr->u.__sem_base) 580 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK); 581 } 582 for (i = semakptr->u.__sem_base - sem; i < semtot; i++) 583 sem[i] = sem[i + semakptr->u.sem_nsems]; 584 for (i = 0; i < seminfo.semmni; i++) { 585 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 586 sema[i].u.__sem_base > semakptr->u.__sem_base) { 587 sema[i].u.__sem_base -= semakptr->u.sem_nsems; 588 mtx_unlock(&sema_mtx[i]); 589 } 590 } 591 semtot -= semakptr->u.sem_nsems; 592 } 593 594 static struct prison * 595 sem_find_prison(struct ucred *cred) 596 { 597 struct prison *pr, *rpr; 598 599 pr = cred->cr_prison; 600 prison_lock(pr); 601 rpr = osd_jail_get(pr, sem_prison_slot); 602 prison_unlock(pr); 603 return rpr; 604 } 605 606 static int 607 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr) 608 { 609 610 if (semakptr->cred == NULL || 611 !(rpr == semakptr->cred->cr_prison || 612 prison_ischild(rpr, semakptr->cred->cr_prison))) 613 return (EINVAL); 614 return (0); 615 } 616 617 /* 618 * Note that the user-mode half of this passes a union, not a pointer. 619 */ 620 #ifndef _SYS_SYSPROTO_H_ 621 struct __semctl_args { 622 int semid; 623 int semnum; 624 int cmd; 625 union semun *arg; 626 }; 627 #endif 628 int 629 sys___semctl(struct thread *td, struct __semctl_args *uap) 630 { 631 struct semid_ds dsbuf; 632 union semun arg, semun; 633 register_t rval; 634 int error; 635 636 switch (uap->cmd) { 637 case SEM_STAT: 638 case IPC_SET: 639 case IPC_STAT: 640 case GETALL: 641 case SETVAL: 642 case SETALL: 643 error = copyin(uap->arg, &arg, sizeof(arg)); 644 if (error) 645 return (error); 646 break; 647 } 648 649 switch (uap->cmd) { 650 case SEM_STAT: 651 case IPC_STAT: 652 semun.buf = &dsbuf; 653 break; 654 case IPC_SET: 655 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf)); 656 if (error) 657 return (error); 658 semun.buf = &dsbuf; 659 break; 660 case GETALL: 661 case SETALL: 662 semun.array = arg.array; 663 break; 664 case SETVAL: 665 semun.val = arg.val; 666 break; 667 } 668 669 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 670 &rval); 671 if (error) 672 return (error); 673 674 switch (uap->cmd) { 675 case SEM_STAT: 676 case IPC_STAT: 677 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf)); 678 break; 679 } 680 681 if (error == 0) 682 td->td_retval[0] = rval; 683 return (error); 684 } 685 686 int 687 kern_semctl(struct thread *td, int semid, int semnum, int cmd, 688 union semun *arg, register_t *rval) 689 { 690 u_short *array; 691 struct ucred *cred = td->td_ucred; 692 int i, error; 693 struct prison *rpr; 694 struct semid_ds *sbuf; 695 struct semid_kernel *semakptr; 696 struct mtx *sema_mtxp; 697 u_short usval, count; 698 int semidx; 699 700 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n", 701 semid, semnum, cmd, arg)); 702 703 AUDIT_ARG_SVIPC_CMD(cmd); 704 AUDIT_ARG_SVIPC_ID(semid); 705 706 rpr = sem_find_prison(td->td_ucred); 707 if (sem == NULL) 708 return (ENOSYS); 709 710 array = NULL; 711 712 switch(cmd) { 713 case SEM_STAT: 714 /* 715 * For this command we assume semid is an array index 716 * rather than an IPC id. 717 */ 718 if (semid < 0 || semid >= seminfo.semmni) 719 return (EINVAL); 720 semakptr = &sema[semid]; 721 sema_mtxp = &sema_mtx[semid]; 722 mtx_lock(sema_mtxp); 723 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 724 error = EINVAL; 725 goto done2; 726 } 727 if ((error = sem_prison_cansee(rpr, semakptr))) 728 goto done2; 729 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 730 goto done2; 731 #ifdef MAC 732 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 733 if (error != 0) 734 goto done2; 735 #endif 736 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 737 if (cred->cr_prison != semakptr->cred->cr_prison) 738 arg->buf->sem_perm.key = IPC_PRIVATE; 739 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm); 740 mtx_unlock(sema_mtxp); 741 return (0); 742 } 743 744 semidx = IPCID_TO_IX(semid); 745 if (semidx < 0 || semidx >= seminfo.semmni) 746 return (EINVAL); 747 748 semakptr = &sema[semidx]; 749 sema_mtxp = &sema_mtx[semidx]; 750 if (cmd == IPC_RMID) 751 mtx_lock(&sem_mtx); 752 mtx_lock(sema_mtxp); 753 754 #ifdef MAC 755 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 756 if (error != 0) 757 goto done2; 758 #endif 759 760 error = 0; 761 *rval = 0; 762 763 switch (cmd) { 764 case IPC_RMID: 765 if ((error = semvalid(semid, rpr, semakptr)) != 0) 766 goto done2; 767 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 768 goto done2; 769 sem_remove(semidx, cred); 770 break; 771 772 case IPC_SET: 773 AUDIT_ARG_SVIPC_PERM(&arg->buf->sem_perm); 774 if ((error = semvalid(semid, rpr, semakptr)) != 0) 775 goto done2; 776 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 777 goto done2; 778 sbuf = arg->buf; 779 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid; 780 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid; 781 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode & 782 ~0777) | (sbuf->sem_perm.mode & 0777); 783 semakptr->u.sem_ctime = time_second; 784 break; 785 786 case IPC_STAT: 787 if ((error = semvalid(semid, rpr, semakptr)) != 0) 788 goto done2; 789 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 790 goto done2; 791 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 792 if (cred->cr_prison != semakptr->cred->cr_prison) 793 arg->buf->sem_perm.key = IPC_PRIVATE; 794 break; 795 796 case GETNCNT: 797 if ((error = semvalid(semid, rpr, semakptr)) != 0) 798 goto done2; 799 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 800 goto done2; 801 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 802 error = EINVAL; 803 goto done2; 804 } 805 *rval = semakptr->u.__sem_base[semnum].semncnt; 806 break; 807 808 case GETPID: 809 if ((error = semvalid(semid, rpr, semakptr)) != 0) 810 goto done2; 811 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 812 goto done2; 813 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 814 error = EINVAL; 815 goto done2; 816 } 817 *rval = semakptr->u.__sem_base[semnum].sempid; 818 break; 819 820 case GETVAL: 821 if ((error = semvalid(semid, rpr, semakptr)) != 0) 822 goto done2; 823 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 824 goto done2; 825 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 826 error = EINVAL; 827 goto done2; 828 } 829 *rval = semakptr->u.__sem_base[semnum].semval; 830 break; 831 832 case GETALL: 833 /* 834 * Unfortunately, callers of this function don't know 835 * in advance how many semaphores are in this set. 836 * While we could just allocate the maximum size array 837 * and pass the actual size back to the caller, that 838 * won't work for SETALL since we can't copyin() more 839 * data than the user specified as we may return a 840 * spurious EFAULT. 841 * 842 * Note that the number of semaphores in a set is 843 * fixed for the life of that set. The only way that 844 * the 'count' could change while are blocked in 845 * malloc() is if this semaphore set were destroyed 846 * and a new one created with the same index. 847 * However, semvalid() will catch that due to the 848 * sequence number unless exactly 0x8000 (or a 849 * multiple thereof) semaphore sets for the same index 850 * are created and destroyed while we are in malloc! 851 * 852 */ 853 count = semakptr->u.sem_nsems; 854 mtx_unlock(sema_mtxp); 855 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 856 mtx_lock(sema_mtxp); 857 if ((error = semvalid(semid, rpr, semakptr)) != 0) 858 goto done2; 859 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 860 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 861 goto done2; 862 for (i = 0; i < semakptr->u.sem_nsems; i++) 863 array[i] = semakptr->u.__sem_base[i].semval; 864 mtx_unlock(sema_mtxp); 865 error = copyout(array, arg->array, count * sizeof(*array)); 866 mtx_lock(sema_mtxp); 867 break; 868 869 case GETZCNT: 870 if ((error = semvalid(semid, rpr, semakptr)) != 0) 871 goto done2; 872 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 873 goto done2; 874 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 875 error = EINVAL; 876 goto done2; 877 } 878 *rval = semakptr->u.__sem_base[semnum].semzcnt; 879 break; 880 881 case SETVAL: 882 if ((error = semvalid(semid, rpr, semakptr)) != 0) 883 goto done2; 884 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 885 goto done2; 886 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 887 error = EINVAL; 888 goto done2; 889 } 890 if (arg->val < 0 || arg->val > seminfo.semvmx) { 891 error = ERANGE; 892 goto done2; 893 } 894 semakptr->u.__sem_base[semnum].semval = arg->val; 895 SEMUNDO_LOCK(); 896 semundo_clear(semidx, semnum); 897 SEMUNDO_UNLOCK(); 898 wakeup(semakptr); 899 break; 900 901 case SETALL: 902 /* 903 * See comment on GETALL for why 'count' shouldn't change 904 * and why we require a userland buffer. 905 */ 906 count = semakptr->u.sem_nsems; 907 mtx_unlock(sema_mtxp); 908 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 909 error = copyin(arg->array, array, count * sizeof(*array)); 910 mtx_lock(sema_mtxp); 911 if (error) 912 break; 913 if ((error = semvalid(semid, rpr, semakptr)) != 0) 914 goto done2; 915 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 916 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 917 goto done2; 918 for (i = 0; i < semakptr->u.sem_nsems; i++) { 919 usval = array[i]; 920 if (usval > seminfo.semvmx) { 921 error = ERANGE; 922 break; 923 } 924 semakptr->u.__sem_base[i].semval = usval; 925 } 926 SEMUNDO_LOCK(); 927 semundo_clear(semidx, -1); 928 SEMUNDO_UNLOCK(); 929 wakeup(semakptr); 930 break; 931 932 default: 933 error = EINVAL; 934 break; 935 } 936 937 done2: 938 mtx_unlock(sema_mtxp); 939 if (cmd == IPC_RMID) 940 mtx_unlock(&sem_mtx); 941 if (array != NULL) 942 free(array, M_TEMP); 943 return(error); 944 } 945 946 #ifndef _SYS_SYSPROTO_H_ 947 struct semget_args { 948 key_t key; 949 int nsems; 950 int semflg; 951 }; 952 #endif 953 int 954 sys_semget(struct thread *td, struct semget_args *uap) 955 { 956 int semid, error = 0; 957 int key = uap->key; 958 int nsems = uap->nsems; 959 int semflg = uap->semflg; 960 struct ucred *cred = td->td_ucred; 961 962 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg)); 963 964 AUDIT_ARG_VALUE(semflg); 965 966 if (sem_find_prison(cred) == NULL) 967 return (ENOSYS); 968 969 mtx_lock(&sem_mtx); 970 if (key != IPC_PRIVATE) { 971 for (semid = 0; semid < seminfo.semmni; semid++) { 972 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) && 973 sema[semid].cred != NULL && 974 sema[semid].cred->cr_prison == cred->cr_prison && 975 sema[semid].u.sem_perm.key == key) 976 break; 977 } 978 if (semid < seminfo.semmni) { 979 AUDIT_ARG_SVIPC_ID(semid); 980 DPRINTF(("found public key\n")); 981 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) { 982 DPRINTF(("not exclusive\n")); 983 error = EEXIST; 984 goto done2; 985 } 986 if ((error = ipcperm(td, &sema[semid].u.sem_perm, 987 semflg & 0700))) { 988 goto done2; 989 } 990 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) { 991 DPRINTF(("too small\n")); 992 error = EINVAL; 993 goto done2; 994 } 995 #ifdef MAC 996 error = mac_sysvsem_check_semget(cred, &sema[semid]); 997 if (error != 0) 998 goto done2; 999 #endif 1000 goto found; 1001 } 1002 } 1003 1004 DPRINTF(("need to allocate the semid_kernel\n")); 1005 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) { 1006 if (nsems <= 0 || nsems > seminfo.semmsl) { 1007 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems, 1008 seminfo.semmsl)); 1009 error = EINVAL; 1010 goto done2; 1011 } 1012 if (nsems > seminfo.semmns - semtot) { 1013 DPRINTF(( 1014 "not enough semaphores left (need %d, got %d)\n", 1015 nsems, seminfo.semmns - semtot)); 1016 error = ENOSPC; 1017 goto done2; 1018 } 1019 for (semid = 0; semid < seminfo.semmni; semid++) { 1020 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0) 1021 break; 1022 } 1023 if (semid == seminfo.semmni) { 1024 DPRINTF(("no more semid_kernel's available\n")); 1025 error = ENOSPC; 1026 goto done2; 1027 } 1028 #ifdef RACCT 1029 if (racct_enable) { 1030 PROC_LOCK(td->td_proc); 1031 error = racct_add(td->td_proc, RACCT_NSEM, nsems); 1032 PROC_UNLOCK(td->td_proc); 1033 if (error != 0) { 1034 error = ENOSPC; 1035 goto done2; 1036 } 1037 } 1038 #endif 1039 DPRINTF(("semid %d is available\n", semid)); 1040 mtx_lock(&sema_mtx[semid]); 1041 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0, 1042 ("Lost semaphore %d", semid)); 1043 sema[semid].u.sem_perm.key = key; 1044 sema[semid].u.sem_perm.cuid = cred->cr_uid; 1045 sema[semid].u.sem_perm.uid = cred->cr_uid; 1046 sema[semid].u.sem_perm.cgid = cred->cr_gid; 1047 sema[semid].u.sem_perm.gid = cred->cr_gid; 1048 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC; 1049 sema[semid].cred = crhold(cred); 1050 sema[semid].u.sem_perm.seq = 1051 (sema[semid].u.sem_perm.seq + 1) & 0x7fff; 1052 sema[semid].u.sem_nsems = nsems; 1053 sema[semid].u.sem_otime = 0; 1054 sema[semid].u.sem_ctime = time_second; 1055 sema[semid].u.__sem_base = &sem[semtot]; 1056 semtot += nsems; 1057 bzero(sema[semid].u.__sem_base, 1058 sizeof(sema[semid].u.__sem_base[0])*nsems); 1059 #ifdef MAC 1060 mac_sysvsem_create(cred, &sema[semid]); 1061 #endif 1062 mtx_unlock(&sema_mtx[semid]); 1063 DPRINTF(("sembase = %p, next = %p\n", 1064 sema[semid].u.__sem_base, &sem[semtot])); 1065 } else { 1066 DPRINTF(("didn't find it and wasn't asked to create it\n")); 1067 error = ENOENT; 1068 goto done2; 1069 } 1070 1071 found: 1072 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm); 1073 done2: 1074 mtx_unlock(&sem_mtx); 1075 return (error); 1076 } 1077 1078 #ifndef _SYS_SYSPROTO_H_ 1079 struct semop_args { 1080 int semid; 1081 struct sembuf *sops; 1082 size_t nsops; 1083 }; 1084 #endif 1085 int 1086 sys_semop(struct thread *td, struct semop_args *uap) 1087 { 1088 #define SMALL_SOPS 8 1089 struct sembuf small_sops[SMALL_SOPS]; 1090 int semid = uap->semid; 1091 size_t nsops = uap->nsops; 1092 struct prison *rpr; 1093 struct sembuf *sops; 1094 struct semid_kernel *semakptr; 1095 struct sembuf *sopptr = NULL; 1096 struct sem *semptr = NULL; 1097 struct sem_undo *suptr; 1098 struct mtx *sema_mtxp; 1099 size_t i, j, k; 1100 int error; 1101 int do_wakeup, do_undos; 1102 unsigned short seq; 1103 1104 #ifdef SEM_DEBUG 1105 sops = NULL; 1106 #endif 1107 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops)); 1108 1109 AUDIT_ARG_SVIPC_ID(semid); 1110 1111 rpr = sem_find_prison(td->td_ucred); 1112 if (sem == NULL) 1113 return (ENOSYS); 1114 1115 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */ 1116 1117 if (semid < 0 || semid >= seminfo.semmni) 1118 return (EINVAL); 1119 1120 /* Allocate memory for sem_ops */ 1121 if (nsops <= SMALL_SOPS) 1122 sops = small_sops; 1123 else if (nsops > seminfo.semopm) { 1124 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm, 1125 nsops)); 1126 return (E2BIG); 1127 } else { 1128 #ifdef RACCT 1129 if (racct_enable) { 1130 PROC_LOCK(td->td_proc); 1131 if (nsops > 1132 racct_get_available(td->td_proc, RACCT_NSEMOP)) { 1133 PROC_UNLOCK(td->td_proc); 1134 return (E2BIG); 1135 } 1136 PROC_UNLOCK(td->td_proc); 1137 } 1138 #endif 1139 1140 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK); 1141 } 1142 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) { 1143 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error, 1144 uap->sops, sops, nsops * sizeof(sops[0]))); 1145 if (sops != small_sops) 1146 free(sops, M_SEM); 1147 return (error); 1148 } 1149 1150 semakptr = &sema[semid]; 1151 sema_mtxp = &sema_mtx[semid]; 1152 mtx_lock(sema_mtxp); 1153 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 1154 error = EINVAL; 1155 goto done2; 1156 } 1157 seq = semakptr->u.sem_perm.seq; 1158 if (seq != IPCID_TO_SEQ(uap->semid)) { 1159 error = EINVAL; 1160 goto done2; 1161 } 1162 if ((error = sem_prison_cansee(rpr, semakptr)) != 0) 1163 goto done2; 1164 /* 1165 * Initial pass through sops to see what permissions are needed. 1166 * Also perform any checks that don't need repeating on each 1167 * attempt to satisfy the request vector. 1168 */ 1169 j = 0; /* permission needed */ 1170 do_undos = 0; 1171 for (i = 0; i < nsops; i++) { 1172 sopptr = &sops[i]; 1173 if (sopptr->sem_num >= semakptr->u.sem_nsems) { 1174 error = EFBIG; 1175 goto done2; 1176 } 1177 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0) 1178 do_undos = 1; 1179 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A; 1180 } 1181 1182 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) { 1183 DPRINTF(("error = %d from ipaccess\n", error)); 1184 goto done2; 1185 } 1186 #ifdef MAC 1187 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j); 1188 if (error != 0) 1189 goto done2; 1190 #endif 1191 1192 /* 1193 * Loop trying to satisfy the vector of requests. 1194 * If we reach a point where we must wait, any requests already 1195 * performed are rolled back and we go to sleep until some other 1196 * process wakes us up. At this point, we start all over again. 1197 * 1198 * This ensures that from the perspective of other tasks, a set 1199 * of requests is atomic (never partially satisfied). 1200 */ 1201 for (;;) { 1202 do_wakeup = 0; 1203 error = 0; /* error return if necessary */ 1204 1205 for (i = 0; i < nsops; i++) { 1206 sopptr = &sops[i]; 1207 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1208 1209 DPRINTF(( 1210 "semop: semakptr=%p, __sem_base=%p, " 1211 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n", 1212 semakptr, semakptr->u.__sem_base, semptr, 1213 sopptr->sem_num, semptr->semval, sopptr->sem_op, 1214 (sopptr->sem_flg & IPC_NOWAIT) ? 1215 "nowait" : "wait")); 1216 1217 if (sopptr->sem_op < 0) { 1218 if (semptr->semval + sopptr->sem_op < 0) { 1219 DPRINTF(("semop: can't do it now\n")); 1220 break; 1221 } else { 1222 semptr->semval += sopptr->sem_op; 1223 if (semptr->semval == 0 && 1224 semptr->semzcnt > 0) 1225 do_wakeup = 1; 1226 } 1227 } else if (sopptr->sem_op == 0) { 1228 if (semptr->semval != 0) { 1229 DPRINTF(("semop: not zero now\n")); 1230 break; 1231 } 1232 } else if (semptr->semval + sopptr->sem_op > 1233 seminfo.semvmx) { 1234 error = ERANGE; 1235 break; 1236 } else { 1237 if (semptr->semncnt > 0) 1238 do_wakeup = 1; 1239 semptr->semval += sopptr->sem_op; 1240 } 1241 } 1242 1243 /* 1244 * Did we get through the entire vector? 1245 */ 1246 if (i >= nsops) 1247 goto done; 1248 1249 /* 1250 * No ... rollback anything that we've already done 1251 */ 1252 DPRINTF(("semop: rollback 0 through %d\n", i-1)); 1253 for (j = 0; j < i; j++) 1254 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1255 sops[j].sem_op; 1256 1257 /* If we detected an error, return it */ 1258 if (error != 0) 1259 goto done2; 1260 1261 /* 1262 * If the request that we couldn't satisfy has the 1263 * NOWAIT flag set then return with EAGAIN. 1264 */ 1265 if (sopptr->sem_flg & IPC_NOWAIT) { 1266 error = EAGAIN; 1267 goto done2; 1268 } 1269 1270 if (sopptr->sem_op == 0) 1271 semptr->semzcnt++; 1272 else 1273 semptr->semncnt++; 1274 1275 DPRINTF(("semop: good night!\n")); 1276 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH, 1277 "semwait", 0); 1278 DPRINTF(("semop: good morning (error=%d)!\n", error)); 1279 /* return code is checked below, after sem[nz]cnt-- */ 1280 1281 /* 1282 * Make sure that the semaphore still exists 1283 */ 1284 seq = semakptr->u.sem_perm.seq; 1285 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1286 seq != IPCID_TO_SEQ(uap->semid)) { 1287 error = EIDRM; 1288 goto done2; 1289 } 1290 1291 /* 1292 * Renew the semaphore's pointer after wakeup since 1293 * during msleep __sem_base may have been modified and semptr 1294 * is not valid any more 1295 */ 1296 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1297 1298 /* 1299 * The semaphore is still alive. Readjust the count of 1300 * waiting processes. 1301 */ 1302 if (sopptr->sem_op == 0) 1303 semptr->semzcnt--; 1304 else 1305 semptr->semncnt--; 1306 1307 /* 1308 * Is it really morning, or was our sleep interrupted? 1309 * (Delayed check of msleep() return code because we 1310 * need to decrement sem[nz]cnt either way.) 1311 */ 1312 if (error != 0) { 1313 error = EINTR; 1314 goto done2; 1315 } 1316 DPRINTF(("semop: good morning!\n")); 1317 } 1318 1319 done: 1320 /* 1321 * Process any SEM_UNDO requests. 1322 */ 1323 if (do_undos) { 1324 SEMUNDO_LOCK(); 1325 suptr = NULL; 1326 for (i = 0; i < nsops; i++) { 1327 /* 1328 * We only need to deal with SEM_UNDO's for non-zero 1329 * op's. 1330 */ 1331 int adjval; 1332 1333 if ((sops[i].sem_flg & SEM_UNDO) == 0) 1334 continue; 1335 adjval = sops[i].sem_op; 1336 if (adjval == 0) 1337 continue; 1338 error = semundo_adjust(td, &suptr, semid, seq, 1339 sops[i].sem_num, -adjval); 1340 if (error == 0) 1341 continue; 1342 1343 /* 1344 * Oh-Oh! We ran out of either sem_undo's or undo's. 1345 * Rollback the adjustments to this point and then 1346 * rollback the semaphore ups and down so we can return 1347 * with an error with all structures restored. We 1348 * rollback the undo's in the exact reverse order that 1349 * we applied them. This guarantees that we won't run 1350 * out of space as we roll things back out. 1351 */ 1352 for (j = 0; j < i; j++) { 1353 k = i - j - 1; 1354 if ((sops[k].sem_flg & SEM_UNDO) == 0) 1355 continue; 1356 adjval = sops[k].sem_op; 1357 if (adjval == 0) 1358 continue; 1359 if (semundo_adjust(td, &suptr, semid, seq, 1360 sops[k].sem_num, adjval) != 0) 1361 panic("semop - can't undo undos"); 1362 } 1363 1364 for (j = 0; j < nsops; j++) 1365 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1366 sops[j].sem_op; 1367 1368 DPRINTF(("error = %d from semundo_adjust\n", error)); 1369 SEMUNDO_UNLOCK(); 1370 goto done2; 1371 } /* loop through the sops */ 1372 SEMUNDO_UNLOCK(); 1373 } /* if (do_undos) */ 1374 1375 /* We're definitely done - set the sempid's and time */ 1376 for (i = 0; i < nsops; i++) { 1377 sopptr = &sops[i]; 1378 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1379 semptr->sempid = td->td_proc->p_pid; 1380 } 1381 semakptr->u.sem_otime = time_second; 1382 1383 /* 1384 * Do a wakeup if any semaphore was up'd whilst something was 1385 * sleeping on it. 1386 */ 1387 if (do_wakeup) { 1388 DPRINTF(("semop: doing wakeup\n")); 1389 wakeup(semakptr); 1390 DPRINTF(("semop: back from wakeup\n")); 1391 } 1392 DPRINTF(("semop: done\n")); 1393 td->td_retval[0] = 0; 1394 done2: 1395 mtx_unlock(sema_mtxp); 1396 if (sops != small_sops) 1397 free(sops, M_SEM); 1398 return (error); 1399 } 1400 1401 /* 1402 * Go through the undo structures for this process and apply the adjustments to 1403 * semaphores. 1404 */ 1405 static void 1406 semexit_myhook(void *arg, struct proc *p) 1407 { 1408 struct sem_undo *suptr; 1409 struct semid_kernel *semakptr; 1410 struct mtx *sema_mtxp; 1411 int semid, semnum, adjval, ix; 1412 unsigned short seq; 1413 1414 /* 1415 * Go through the chain of undo vectors looking for one 1416 * associated with this process. 1417 */ 1418 if (LIST_EMPTY(&semu_list)) 1419 return; 1420 SEMUNDO_LOCK(); 1421 LIST_FOREACH(suptr, &semu_list, un_next) { 1422 if (suptr->un_proc == p) 1423 break; 1424 } 1425 if (suptr == NULL) { 1426 SEMUNDO_UNLOCK(); 1427 return; 1428 } 1429 LIST_REMOVE(suptr, un_next); 1430 1431 DPRINTF(("proc @%p has undo structure with %d entries\n", p, 1432 suptr->un_cnt)); 1433 1434 /* 1435 * If there are any active undo elements then process them. 1436 */ 1437 if (suptr->un_cnt > 0) { 1438 SEMUNDO_UNLOCK(); 1439 for (ix = 0; ix < suptr->un_cnt; ix++) { 1440 semid = suptr->un_ent[ix].un_id; 1441 semnum = suptr->un_ent[ix].un_num; 1442 adjval = suptr->un_ent[ix].un_adjval; 1443 seq = suptr->un_ent[ix].un_seq; 1444 semakptr = &sema[semid]; 1445 sema_mtxp = &sema_mtx[semid]; 1446 1447 mtx_lock(sema_mtxp); 1448 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1449 (semakptr->u.sem_perm.seq != seq)) { 1450 mtx_unlock(sema_mtxp); 1451 continue; 1452 } 1453 if (semnum >= semakptr->u.sem_nsems) 1454 panic("semexit - semnum out of range"); 1455 1456 DPRINTF(( 1457 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n", 1458 suptr->un_proc, suptr->un_ent[ix].un_id, 1459 suptr->un_ent[ix].un_num, 1460 suptr->un_ent[ix].un_adjval, 1461 semakptr->u.__sem_base[semnum].semval)); 1462 1463 if (adjval < 0 && semakptr->u.__sem_base[semnum].semval < 1464 -adjval) 1465 semakptr->u.__sem_base[semnum].semval = 0; 1466 else 1467 semakptr->u.__sem_base[semnum].semval += adjval; 1468 1469 wakeup(semakptr); 1470 DPRINTF(("semexit: back from wakeup\n")); 1471 mtx_unlock(sema_mtxp); 1472 } 1473 SEMUNDO_LOCK(); 1474 } 1475 1476 /* 1477 * Deallocate the undo vector. 1478 */ 1479 DPRINTF(("removing vector\n")); 1480 suptr->un_proc = NULL; 1481 suptr->un_cnt = 0; 1482 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 1483 SEMUNDO_UNLOCK(); 1484 } 1485 1486 static int 1487 sysctl_sema(SYSCTL_HANDLER_ARGS) 1488 { 1489 struct prison *pr, *rpr; 1490 struct semid_kernel tsemak; 1491 #ifdef COMPAT_FREEBSD32 1492 struct semid_kernel32 tsemak32; 1493 #endif 1494 void *outaddr; 1495 size_t outsize; 1496 int error, i; 1497 1498 pr = req->td->td_ucred->cr_prison; 1499 rpr = sem_find_prison(req->td->td_ucred); 1500 error = 0; 1501 for (i = 0; i < seminfo.semmni; i++) { 1502 mtx_lock(&sema_mtx[i]); 1503 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 || 1504 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0) 1505 bzero(&tsemak, sizeof(tsemak)); 1506 else { 1507 tsemak = sema[i]; 1508 if (tsemak.cred->cr_prison != pr) 1509 tsemak.u.sem_perm.key = IPC_PRIVATE; 1510 } 1511 mtx_unlock(&sema_mtx[i]); 1512 #ifdef COMPAT_FREEBSD32 1513 if (SV_CURPROC_FLAG(SV_ILP32)) { 1514 bzero(&tsemak32, sizeof(tsemak32)); 1515 freebsd32_ipcperm_out(&tsemak.u.sem_perm, 1516 &tsemak32.u.sem_perm); 1517 /* Don't copy u.__sem_base */ 1518 CP(tsemak, tsemak32, u.sem_nsems); 1519 CP(tsemak, tsemak32, u.sem_otime); 1520 CP(tsemak, tsemak32, u.sem_ctime); 1521 /* Don't copy label or cred */ 1522 outaddr = &tsemak32; 1523 outsize = sizeof(tsemak32); 1524 } else 1525 #endif 1526 { 1527 tsemak.u.__sem_base = NULL; 1528 tsemak.label = NULL; 1529 tsemak.cred = NULL; 1530 outaddr = &tsemak; 1531 outsize = sizeof(tsemak); 1532 } 1533 error = SYSCTL_OUT(req, outaddr, outsize); 1534 if (error != 0) 1535 break; 1536 } 1537 return (error); 1538 } 1539 1540 static int 1541 sem_prison_check(void *obj, void *data) 1542 { 1543 struct prison *pr = obj; 1544 struct prison *prpr; 1545 struct vfsoptlist *opts = data; 1546 int error, jsys; 1547 1548 /* 1549 * sysvsem is a jailsys integer. 1550 * It must be "disable" if the parent jail is disabled. 1551 */ 1552 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1553 if (error != ENOENT) { 1554 if (error != 0) 1555 return (error); 1556 switch (jsys) { 1557 case JAIL_SYS_DISABLE: 1558 break; 1559 case JAIL_SYS_NEW: 1560 case JAIL_SYS_INHERIT: 1561 prison_lock(pr->pr_parent); 1562 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1563 prison_unlock(pr->pr_parent); 1564 if (prpr == NULL) 1565 return (EPERM); 1566 break; 1567 default: 1568 return (EINVAL); 1569 } 1570 } 1571 1572 return (0); 1573 } 1574 1575 static int 1576 sem_prison_set(void *obj, void *data) 1577 { 1578 struct prison *pr = obj; 1579 struct prison *tpr, *orpr, *nrpr, *trpr; 1580 struct vfsoptlist *opts = data; 1581 void *rsv; 1582 int jsys, descend; 1583 1584 /* 1585 * sysvsem controls which jail is the root of the associated sems (this 1586 * jail or same as the parent), or if the feature is available at all. 1587 */ 1588 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT) 1589 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0) 1590 ? JAIL_SYS_INHERIT 1591 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0) 1592 ? JAIL_SYS_DISABLE 1593 : -1; 1594 if (jsys == JAIL_SYS_DISABLE) { 1595 prison_lock(pr); 1596 orpr = osd_jail_get(pr, sem_prison_slot); 1597 if (orpr != NULL) 1598 osd_jail_del(pr, sem_prison_slot); 1599 prison_unlock(pr); 1600 if (orpr != NULL) { 1601 if (orpr == pr) 1602 sem_prison_cleanup(pr); 1603 /* Disable all child jails as well. */ 1604 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1605 prison_lock(tpr); 1606 trpr = osd_jail_get(tpr, sem_prison_slot); 1607 if (trpr != NULL) { 1608 osd_jail_del(tpr, sem_prison_slot); 1609 prison_unlock(tpr); 1610 if (trpr == tpr) 1611 sem_prison_cleanup(tpr); 1612 } else { 1613 prison_unlock(tpr); 1614 descend = 0; 1615 } 1616 } 1617 } 1618 } else if (jsys != -1) { 1619 if (jsys == JAIL_SYS_NEW) 1620 nrpr = pr; 1621 else { 1622 prison_lock(pr->pr_parent); 1623 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1624 prison_unlock(pr->pr_parent); 1625 } 1626 rsv = osd_reserve(sem_prison_slot); 1627 prison_lock(pr); 1628 orpr = osd_jail_get(pr, sem_prison_slot); 1629 if (orpr != nrpr) 1630 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv, 1631 nrpr); 1632 else 1633 osd_free_reserved(rsv); 1634 prison_unlock(pr); 1635 if (orpr != nrpr) { 1636 if (orpr == pr) 1637 sem_prison_cleanup(pr); 1638 if (orpr != NULL) { 1639 /* Change child jails matching the old root, */ 1640 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1641 prison_lock(tpr); 1642 trpr = osd_jail_get(tpr, 1643 sem_prison_slot); 1644 if (trpr == orpr) { 1645 (void)osd_jail_set(tpr, 1646 sem_prison_slot, nrpr); 1647 prison_unlock(tpr); 1648 if (trpr == tpr) 1649 sem_prison_cleanup(tpr); 1650 } else { 1651 prison_unlock(tpr); 1652 descend = 0; 1653 } 1654 } 1655 } 1656 } 1657 } 1658 1659 return (0); 1660 } 1661 1662 static int 1663 sem_prison_get(void *obj, void *data) 1664 { 1665 struct prison *pr = obj; 1666 struct prison *rpr; 1667 struct vfsoptlist *opts = data; 1668 int error, jsys; 1669 1670 /* Set sysvsem based on the jail's root prison. */ 1671 prison_lock(pr); 1672 rpr = osd_jail_get(pr, sem_prison_slot); 1673 prison_unlock(pr); 1674 jsys = rpr == NULL ? JAIL_SYS_DISABLE 1675 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT; 1676 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1677 if (error == ENOENT) 1678 error = 0; 1679 return (error); 1680 } 1681 1682 static int 1683 sem_prison_remove(void *obj, void *data __unused) 1684 { 1685 struct prison *pr = obj; 1686 struct prison *rpr; 1687 1688 prison_lock(pr); 1689 rpr = osd_jail_get(pr, sem_prison_slot); 1690 prison_unlock(pr); 1691 if (rpr == pr) 1692 sem_prison_cleanup(pr); 1693 return (0); 1694 } 1695 1696 static void 1697 sem_prison_cleanup(struct prison *pr) 1698 { 1699 int i; 1700 1701 /* Remove any sems that belong to this jail. */ 1702 mtx_lock(&sem_mtx); 1703 for (i = 0; i < seminfo.semmni; i++) { 1704 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 1705 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) { 1706 mtx_lock(&sema_mtx[i]); 1707 sem_remove(i, NULL); 1708 mtx_unlock(&sema_mtx[i]); 1709 } 1710 } 1711 mtx_unlock(&sem_mtx); 1712 } 1713 1714 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores"); 1715 1716 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1717 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1718 1719 /* XXX casting to (sy_call_t *) is bogus, as usual. */ 1720 static sy_call_t *semcalls[] = { 1721 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget, 1722 (sy_call_t *)sys_semop 1723 }; 1724 1725 /* 1726 * Entry point for all SEM calls. 1727 */ 1728 int 1729 sys_semsys(td, uap) 1730 struct thread *td; 1731 /* XXX actually varargs. */ 1732 struct semsys_args /* { 1733 int which; 1734 int a2; 1735 int a3; 1736 int a4; 1737 int a5; 1738 } */ *uap; 1739 { 1740 int error; 1741 1742 AUDIT_ARG_SVIPC_WHICH(uap->which); 1743 if (uap->which < 0 || uap->which >= nitems(semcalls)) 1744 return (EINVAL); 1745 error = (*semcalls[uap->which])(td, &uap->a2); 1746 return (error); 1747 } 1748 1749 #ifndef CP 1750 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0) 1751 #endif 1752 1753 #ifndef _SYS_SYSPROTO_H_ 1754 struct freebsd7___semctl_args { 1755 int semid; 1756 int semnum; 1757 int cmd; 1758 union semun_old *arg; 1759 }; 1760 #endif 1761 int 1762 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap) 1763 { 1764 struct semid_ds_old dsold; 1765 struct semid_ds dsbuf; 1766 union semun_old arg; 1767 union semun semun; 1768 register_t rval; 1769 int error; 1770 1771 switch (uap->cmd) { 1772 case SEM_STAT: 1773 case IPC_SET: 1774 case IPC_STAT: 1775 case GETALL: 1776 case SETVAL: 1777 case SETALL: 1778 error = copyin(uap->arg, &arg, sizeof(arg)); 1779 if (error) 1780 return (error); 1781 break; 1782 } 1783 1784 switch (uap->cmd) { 1785 case SEM_STAT: 1786 case IPC_STAT: 1787 semun.buf = &dsbuf; 1788 break; 1789 case IPC_SET: 1790 error = copyin(arg.buf, &dsold, sizeof(dsold)); 1791 if (error) 1792 return (error); 1793 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm); 1794 CP(dsold, dsbuf, __sem_base); 1795 CP(dsold, dsbuf, sem_nsems); 1796 CP(dsold, dsbuf, sem_otime); 1797 CP(dsold, dsbuf, sem_ctime); 1798 semun.buf = &dsbuf; 1799 break; 1800 case GETALL: 1801 case SETALL: 1802 semun.array = arg.array; 1803 break; 1804 case SETVAL: 1805 semun.val = arg.val; 1806 break; 1807 } 1808 1809 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1810 &rval); 1811 if (error) 1812 return (error); 1813 1814 switch (uap->cmd) { 1815 case SEM_STAT: 1816 case IPC_STAT: 1817 bzero(&dsold, sizeof(dsold)); 1818 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm); 1819 CP(dsbuf, dsold, __sem_base); 1820 CP(dsbuf, dsold, sem_nsems); 1821 CP(dsbuf, dsold, sem_otime); 1822 CP(dsbuf, dsold, sem_ctime); 1823 error = copyout(&dsold, arg.buf, sizeof(dsold)); 1824 break; 1825 } 1826 1827 if (error == 0) 1828 td->td_retval[0] = rval; 1829 return (error); 1830 } 1831 1832 #endif /* COMPAT_FREEBSD{4,5,6,7} */ 1833 1834 #ifdef COMPAT_FREEBSD32 1835 1836 int 1837 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap) 1838 { 1839 1840 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1841 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1842 AUDIT_ARG_SVIPC_WHICH(uap->which); 1843 switch (uap->which) { 1844 case 0: 1845 return (freebsd7_freebsd32_semctl(td, 1846 (struct freebsd7_freebsd32_semctl_args *)&uap->a2)); 1847 default: 1848 return (sys_semsys(td, (struct semsys_args *)uap)); 1849 } 1850 #else 1851 return (nosys(td, NULL)); 1852 #endif 1853 } 1854 1855 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1856 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1857 int 1858 freebsd7_freebsd32_semctl(struct thread *td, 1859 struct freebsd7_freebsd32_semctl_args *uap) 1860 { 1861 struct semid_ds32_old dsbuf32; 1862 struct semid_ds dsbuf; 1863 union semun semun; 1864 union semun32 arg; 1865 register_t rval; 1866 int error; 1867 1868 switch (uap->cmd) { 1869 case SEM_STAT: 1870 case IPC_SET: 1871 case IPC_STAT: 1872 case GETALL: 1873 case SETVAL: 1874 case SETALL: 1875 error = copyin(uap->arg, &arg, sizeof(arg)); 1876 if (error) 1877 return (error); 1878 break; 1879 } 1880 1881 switch (uap->cmd) { 1882 case SEM_STAT: 1883 case IPC_STAT: 1884 semun.buf = &dsbuf; 1885 break; 1886 case IPC_SET: 1887 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1888 if (error) 1889 return (error); 1890 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1891 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1892 CP(dsbuf32, dsbuf, sem_nsems); 1893 CP(dsbuf32, dsbuf, sem_otime); 1894 CP(dsbuf32, dsbuf, sem_ctime); 1895 semun.buf = &dsbuf; 1896 break; 1897 case GETALL: 1898 case SETALL: 1899 semun.array = PTRIN(arg.array); 1900 break; 1901 case SETVAL: 1902 semun.val = arg.val; 1903 break; 1904 } 1905 1906 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1907 &rval); 1908 if (error) 1909 return (error); 1910 1911 switch (uap->cmd) { 1912 case SEM_STAT: 1913 case IPC_STAT: 1914 bzero(&dsbuf32, sizeof(dsbuf32)); 1915 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1916 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1917 CP(dsbuf, dsbuf32, sem_nsems); 1918 CP(dsbuf, dsbuf32, sem_otime); 1919 CP(dsbuf, dsbuf32, sem_ctime); 1920 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 1921 break; 1922 } 1923 1924 if (error == 0) 1925 td->td_retval[0] = rval; 1926 return (error); 1927 } 1928 #endif 1929 1930 int 1931 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap) 1932 { 1933 struct semid_ds32 dsbuf32; 1934 struct semid_ds dsbuf; 1935 union semun semun; 1936 union semun32 arg; 1937 register_t rval; 1938 int error; 1939 1940 switch (uap->cmd) { 1941 case SEM_STAT: 1942 case IPC_SET: 1943 case IPC_STAT: 1944 case GETALL: 1945 case SETVAL: 1946 case SETALL: 1947 error = copyin(uap->arg, &arg, sizeof(arg)); 1948 if (error) 1949 return (error); 1950 break; 1951 } 1952 1953 switch (uap->cmd) { 1954 case SEM_STAT: 1955 case IPC_STAT: 1956 semun.buf = &dsbuf; 1957 break; 1958 case IPC_SET: 1959 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1960 if (error) 1961 return (error); 1962 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1963 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1964 CP(dsbuf32, dsbuf, sem_nsems); 1965 CP(dsbuf32, dsbuf, sem_otime); 1966 CP(dsbuf32, dsbuf, sem_ctime); 1967 semun.buf = &dsbuf; 1968 break; 1969 case GETALL: 1970 case SETALL: 1971 semun.array = PTRIN(arg.array); 1972 break; 1973 case SETVAL: 1974 semun.val = arg.val; 1975 break; 1976 } 1977 1978 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1979 &rval); 1980 if (error) 1981 return (error); 1982 1983 switch (uap->cmd) { 1984 case SEM_STAT: 1985 case IPC_STAT: 1986 bzero(&dsbuf32, sizeof(dsbuf32)); 1987 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1988 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1989 CP(dsbuf, dsbuf32, sem_nsems); 1990 CP(dsbuf, dsbuf32, sem_otime); 1991 CP(dsbuf, dsbuf32, sem_ctime); 1992 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 1993 break; 1994 } 1995 1996 if (error == 0) 1997 td->td_retval[0] = rval; 1998 return (error); 1999 } 2000 2001 #endif /* COMPAT_FREEBSD32 */ 2002