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_sysvipc.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/sysproto.h> 55 #include <sys/abi_compat.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 = SEMMNI, /* # of semaphore identifiers */ 203 .semmns = SEMMNS, /* # of semaphores in system */ 204 .semmnu = SEMMNU, /* # of undo structures in system */ 205 .semmsl = SEMMSL, /* max # of semaphores per id */ 206 .semopm = SEMOPM, /* max # of operations per semop call */ 207 .semume = SEMUME, /* max # of undo entries per process */ 208 .semusz = SEMUSZ, /* size in bytes of undo structure */ 209 .semvmx = SEMVMX, /* semaphore maximum value */ 210 .semaem = 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 mtx_assert(&sem_mtx, MA_OWNED); 564 semakptr = &sema[semidx]; 565 KASSERT(semakptr->u.__sem_base - sem + semakptr->u.sem_nsems <= semtot, 566 ("sem_remove: sema %d corrupted sem pointer %p %p %d %d", 567 semidx, semakptr->u.__sem_base, sem, semakptr->u.sem_nsems, 568 semtot)); 569 570 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0; 571 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0; 572 semakptr->u.sem_perm.mode = 0; 573 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems); 574 crfree(semakptr->cred); 575 semakptr->cred = NULL; 576 SEMUNDO_LOCK(); 577 semundo_clear(semidx, -1); 578 SEMUNDO_UNLOCK(); 579 #ifdef MAC 580 mac_sysvsem_cleanup(semakptr); 581 #endif 582 wakeup(semakptr); 583 for (i = 0; i < seminfo.semmni; i++) { 584 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 585 sema[i].u.__sem_base > semakptr->u.__sem_base) 586 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK); 587 } 588 for (i = semakptr->u.__sem_base - sem + semakptr->u.sem_nsems; 589 i < semtot; i++) 590 sem[i - semakptr->u.sem_nsems] = sem[i]; 591 for (i = 0; i < seminfo.semmni; i++) { 592 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 593 sema[i].u.__sem_base > semakptr->u.__sem_base) { 594 sema[i].u.__sem_base -= semakptr->u.sem_nsems; 595 mtx_unlock(&sema_mtx[i]); 596 } 597 } 598 semtot -= semakptr->u.sem_nsems; 599 } 600 601 static struct prison * 602 sem_find_prison(struct ucred *cred) 603 { 604 struct prison *pr, *rpr; 605 606 pr = cred->cr_prison; 607 prison_lock(pr); 608 rpr = osd_jail_get(pr, sem_prison_slot); 609 prison_unlock(pr); 610 return rpr; 611 } 612 613 static int 614 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr) 615 { 616 617 if (semakptr->cred == NULL || 618 !(rpr == semakptr->cred->cr_prison || 619 prison_ischild(rpr, semakptr->cred->cr_prison))) 620 return (EINVAL); 621 return (0); 622 } 623 624 /* 625 * Note that the user-mode half of this passes a union, not a pointer. 626 */ 627 #ifndef _SYS_SYSPROTO_H_ 628 struct __semctl_args { 629 int semid; 630 int semnum; 631 int cmd; 632 union semun *arg; 633 }; 634 #endif 635 int 636 sys___semctl(struct thread *td, struct __semctl_args *uap) 637 { 638 struct semid_ds dsbuf; 639 union semun arg, semun; 640 register_t rval; 641 int error; 642 643 switch (uap->cmd) { 644 case SEM_STAT: 645 case IPC_SET: 646 case IPC_STAT: 647 case GETALL: 648 case SETVAL: 649 case SETALL: 650 error = copyin(uap->arg, &arg, sizeof(arg)); 651 if (error) 652 return (error); 653 break; 654 } 655 656 switch (uap->cmd) { 657 case SEM_STAT: 658 case IPC_STAT: 659 semun.buf = &dsbuf; 660 break; 661 case IPC_SET: 662 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf)); 663 if (error) 664 return (error); 665 semun.buf = &dsbuf; 666 break; 667 case GETALL: 668 case SETALL: 669 semun.array = arg.array; 670 break; 671 case SETVAL: 672 semun.val = arg.val; 673 break; 674 } 675 676 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 677 &rval); 678 if (error) 679 return (error); 680 681 switch (uap->cmd) { 682 case SEM_STAT: 683 case IPC_STAT: 684 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf)); 685 break; 686 } 687 688 if (error == 0) 689 td->td_retval[0] = rval; 690 return (error); 691 } 692 693 int 694 kern_semctl(struct thread *td, int semid, int semnum, int cmd, 695 union semun *arg, register_t *rval) 696 { 697 u_short *array; 698 struct ucred *cred = td->td_ucred; 699 int i, error; 700 struct prison *rpr; 701 struct semid_ds *sbuf; 702 struct semid_kernel *semakptr; 703 struct mtx *sema_mtxp; 704 u_short usval, count; 705 int semidx; 706 707 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n", 708 semid, semnum, cmd, arg)); 709 710 AUDIT_ARG_SVIPC_CMD(cmd); 711 AUDIT_ARG_SVIPC_ID(semid); 712 713 rpr = sem_find_prison(td->td_ucred); 714 if (sem == NULL) 715 return (ENOSYS); 716 717 array = NULL; 718 719 switch(cmd) { 720 case SEM_STAT: 721 /* 722 * For this command we assume semid is an array index 723 * rather than an IPC id. 724 */ 725 if (semid < 0 || semid >= seminfo.semmni) 726 return (EINVAL); 727 semakptr = &sema[semid]; 728 sema_mtxp = &sema_mtx[semid]; 729 mtx_lock(sema_mtxp); 730 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 731 error = EINVAL; 732 goto done2; 733 } 734 if ((error = sem_prison_cansee(rpr, semakptr))) 735 goto done2; 736 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 737 goto done2; 738 #ifdef MAC 739 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 740 if (error != 0) 741 goto done2; 742 #endif 743 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 744 if (cred->cr_prison != semakptr->cred->cr_prison) 745 arg->buf->sem_perm.key = IPC_PRIVATE; 746 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm); 747 mtx_unlock(sema_mtxp); 748 return (0); 749 } 750 751 semidx = IPCID_TO_IX(semid); 752 if (semidx < 0 || semidx >= seminfo.semmni) 753 return (EINVAL); 754 755 semakptr = &sema[semidx]; 756 sema_mtxp = &sema_mtx[semidx]; 757 if (cmd == IPC_RMID) 758 mtx_lock(&sem_mtx); 759 mtx_lock(sema_mtxp); 760 761 #ifdef MAC 762 error = mac_sysvsem_check_semctl(cred, semakptr, cmd); 763 if (error != 0) 764 goto done2; 765 #endif 766 767 error = 0; 768 *rval = 0; 769 770 switch (cmd) { 771 case IPC_RMID: 772 if ((error = semvalid(semid, rpr, semakptr)) != 0) 773 goto done2; 774 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 775 goto done2; 776 sem_remove(semidx, cred); 777 break; 778 779 case IPC_SET: 780 AUDIT_ARG_SVIPC_PERM(&arg->buf->sem_perm); 781 if ((error = semvalid(semid, rpr, semakptr)) != 0) 782 goto done2; 783 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M))) 784 goto done2; 785 sbuf = arg->buf; 786 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid; 787 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid; 788 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode & 789 ~0777) | (sbuf->sem_perm.mode & 0777); 790 semakptr->u.sem_ctime = time_second; 791 break; 792 793 case IPC_STAT: 794 if ((error = semvalid(semid, rpr, semakptr)) != 0) 795 goto done2; 796 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 797 goto done2; 798 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds)); 799 if (cred->cr_prison != semakptr->cred->cr_prison) 800 arg->buf->sem_perm.key = IPC_PRIVATE; 801 802 /* 803 * Try to hide the fact that the structure layout is shared by 804 * both the kernel and userland. This pointer is not useful to 805 * userspace. 806 */ 807 arg->buf->__sem_base = NULL; 808 break; 809 810 case GETNCNT: 811 if ((error = semvalid(semid, rpr, semakptr)) != 0) 812 goto done2; 813 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 814 goto done2; 815 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 816 error = EINVAL; 817 goto done2; 818 } 819 *rval = semakptr->u.__sem_base[semnum].semncnt; 820 break; 821 822 case GETPID: 823 if ((error = semvalid(semid, rpr, semakptr)) != 0) 824 goto done2; 825 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 826 goto done2; 827 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 828 error = EINVAL; 829 goto done2; 830 } 831 *rval = semakptr->u.__sem_base[semnum].sempid; 832 break; 833 834 case GETVAL: 835 if ((error = semvalid(semid, rpr, semakptr)) != 0) 836 goto done2; 837 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 838 goto done2; 839 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 840 error = EINVAL; 841 goto done2; 842 } 843 *rval = semakptr->u.__sem_base[semnum].semval; 844 break; 845 846 case GETALL: 847 /* 848 * Unfortunately, callers of this function don't know 849 * in advance how many semaphores are in this set. 850 * While we could just allocate the maximum size array 851 * and pass the actual size back to the caller, that 852 * won't work for SETALL since we can't copyin() more 853 * data than the user specified as we may return a 854 * spurious EFAULT. 855 * 856 * Note that the number of semaphores in a set is 857 * fixed for the life of that set. The only way that 858 * the 'count' could change while are blocked in 859 * malloc() is if this semaphore set were destroyed 860 * and a new one created with the same index. 861 * However, semvalid() will catch that due to the 862 * sequence number unless exactly 0x8000 (or a 863 * multiple thereof) semaphore sets for the same index 864 * are created and destroyed while we are in malloc! 865 * 866 */ 867 count = semakptr->u.sem_nsems; 868 mtx_unlock(sema_mtxp); 869 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 870 mtx_lock(sema_mtxp); 871 if ((error = semvalid(semid, rpr, semakptr)) != 0) 872 goto done2; 873 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 874 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 875 goto done2; 876 for (i = 0; i < semakptr->u.sem_nsems; i++) 877 array[i] = semakptr->u.__sem_base[i].semval; 878 mtx_unlock(sema_mtxp); 879 error = copyout(array, arg->array, count * sizeof(*array)); 880 mtx_lock(sema_mtxp); 881 break; 882 883 case GETZCNT: 884 if ((error = semvalid(semid, rpr, semakptr)) != 0) 885 goto done2; 886 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R))) 887 goto done2; 888 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 889 error = EINVAL; 890 goto done2; 891 } 892 *rval = semakptr->u.__sem_base[semnum].semzcnt; 893 break; 894 895 case SETVAL: 896 if ((error = semvalid(semid, rpr, semakptr)) != 0) 897 goto done2; 898 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 899 goto done2; 900 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) { 901 error = EINVAL; 902 goto done2; 903 } 904 if (arg->val < 0 || arg->val > seminfo.semvmx) { 905 error = ERANGE; 906 goto done2; 907 } 908 semakptr->u.__sem_base[semnum].semval = arg->val; 909 SEMUNDO_LOCK(); 910 semundo_clear(semidx, semnum); 911 SEMUNDO_UNLOCK(); 912 wakeup(semakptr); 913 break; 914 915 case SETALL: 916 /* 917 * See comment on GETALL for why 'count' shouldn't change 918 * and why we require a userland buffer. 919 */ 920 count = semakptr->u.sem_nsems; 921 mtx_unlock(sema_mtxp); 922 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK); 923 error = copyin(arg->array, array, count * sizeof(*array)); 924 mtx_lock(sema_mtxp); 925 if (error) 926 break; 927 if ((error = semvalid(semid, rpr, semakptr)) != 0) 928 goto done2; 929 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed")); 930 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W))) 931 goto done2; 932 for (i = 0; i < semakptr->u.sem_nsems; i++) { 933 usval = array[i]; 934 if (usval > seminfo.semvmx) { 935 error = ERANGE; 936 break; 937 } 938 semakptr->u.__sem_base[i].semval = usval; 939 } 940 SEMUNDO_LOCK(); 941 semundo_clear(semidx, -1); 942 SEMUNDO_UNLOCK(); 943 wakeup(semakptr); 944 break; 945 946 default: 947 error = EINVAL; 948 break; 949 } 950 951 done2: 952 mtx_unlock(sema_mtxp); 953 if (cmd == IPC_RMID) 954 mtx_unlock(&sem_mtx); 955 if (array != NULL) 956 free(array, M_TEMP); 957 return(error); 958 } 959 960 #ifndef _SYS_SYSPROTO_H_ 961 struct semget_args { 962 key_t key; 963 int nsems; 964 int semflg; 965 }; 966 #endif 967 int 968 sys_semget(struct thread *td, struct semget_args *uap) 969 { 970 int semid, error = 0; 971 int key = uap->key; 972 int nsems = uap->nsems; 973 int semflg = uap->semflg; 974 struct ucred *cred = td->td_ucred; 975 976 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg)); 977 978 AUDIT_ARG_VALUE(semflg); 979 980 if (sem_find_prison(cred) == NULL) 981 return (ENOSYS); 982 983 mtx_lock(&sem_mtx); 984 if (key != IPC_PRIVATE) { 985 for (semid = 0; semid < seminfo.semmni; semid++) { 986 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) && 987 sema[semid].cred != NULL && 988 sema[semid].cred->cr_prison == cred->cr_prison && 989 sema[semid].u.sem_perm.key == key) 990 break; 991 } 992 if (semid < seminfo.semmni) { 993 AUDIT_ARG_SVIPC_ID(semid); 994 DPRINTF(("found public key\n")); 995 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) { 996 DPRINTF(("not exclusive\n")); 997 error = EEXIST; 998 goto done2; 999 } 1000 if ((error = ipcperm(td, &sema[semid].u.sem_perm, 1001 semflg & 0700))) { 1002 goto done2; 1003 } 1004 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) { 1005 DPRINTF(("too small\n")); 1006 error = EINVAL; 1007 goto done2; 1008 } 1009 #ifdef MAC 1010 error = mac_sysvsem_check_semget(cred, &sema[semid]); 1011 if (error != 0) 1012 goto done2; 1013 #endif 1014 goto found; 1015 } 1016 } 1017 1018 DPRINTF(("need to allocate the semid_kernel\n")); 1019 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) { 1020 if (nsems <= 0 || nsems > seminfo.semmsl) { 1021 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems, 1022 seminfo.semmsl)); 1023 error = EINVAL; 1024 goto done2; 1025 } 1026 if (nsems > seminfo.semmns - semtot) { 1027 DPRINTF(( 1028 "not enough semaphores left (need %d, got %d)\n", 1029 nsems, seminfo.semmns - semtot)); 1030 error = ENOSPC; 1031 goto done2; 1032 } 1033 for (semid = 0; semid < seminfo.semmni; semid++) { 1034 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0) 1035 break; 1036 } 1037 if (semid == seminfo.semmni) { 1038 DPRINTF(("no more semid_kernel's available\n")); 1039 error = ENOSPC; 1040 goto done2; 1041 } 1042 #ifdef RACCT 1043 if (racct_enable) { 1044 PROC_LOCK(td->td_proc); 1045 error = racct_add(td->td_proc, RACCT_NSEM, nsems); 1046 PROC_UNLOCK(td->td_proc); 1047 if (error != 0) { 1048 error = ENOSPC; 1049 goto done2; 1050 } 1051 } 1052 #endif 1053 DPRINTF(("semid %d is available\n", semid)); 1054 mtx_lock(&sema_mtx[semid]); 1055 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0, 1056 ("Lost semaphore %d", semid)); 1057 sema[semid].u.sem_perm.key = key; 1058 sema[semid].u.sem_perm.cuid = cred->cr_uid; 1059 sema[semid].u.sem_perm.uid = cred->cr_uid; 1060 sema[semid].u.sem_perm.cgid = cred->cr_gid; 1061 sema[semid].u.sem_perm.gid = cred->cr_gid; 1062 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC; 1063 sema[semid].cred = crhold(cred); 1064 sema[semid].u.sem_perm.seq = 1065 (sema[semid].u.sem_perm.seq + 1) & 0x7fff; 1066 sema[semid].u.sem_nsems = nsems; 1067 sema[semid].u.sem_otime = 0; 1068 sema[semid].u.sem_ctime = time_second; 1069 sema[semid].u.__sem_base = &sem[semtot]; 1070 semtot += nsems; 1071 bzero(sema[semid].u.__sem_base, 1072 sizeof(sema[semid].u.__sem_base[0])*nsems); 1073 #ifdef MAC 1074 mac_sysvsem_create(cred, &sema[semid]); 1075 #endif 1076 mtx_unlock(&sema_mtx[semid]); 1077 DPRINTF(("sembase = %p, next = %p\n", 1078 sema[semid].u.__sem_base, &sem[semtot])); 1079 } else { 1080 DPRINTF(("didn't find it and wasn't asked to create it\n")); 1081 error = ENOENT; 1082 goto done2; 1083 } 1084 1085 found: 1086 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm); 1087 done2: 1088 mtx_unlock(&sem_mtx); 1089 return (error); 1090 } 1091 1092 #ifndef _SYS_SYSPROTO_H_ 1093 struct semop_args { 1094 int semid; 1095 struct sembuf *sops; 1096 size_t nsops; 1097 }; 1098 #endif 1099 int 1100 sys_semop(struct thread *td, struct semop_args *uap) 1101 { 1102 #define SMALL_SOPS 8 1103 struct sembuf small_sops[SMALL_SOPS]; 1104 int semid = uap->semid; 1105 size_t nsops = uap->nsops; 1106 struct prison *rpr; 1107 struct sembuf *sops; 1108 struct semid_kernel *semakptr; 1109 struct sembuf *sopptr = NULL; 1110 struct sem *semptr = NULL; 1111 struct sem_undo *suptr; 1112 struct mtx *sema_mtxp; 1113 size_t i, j, k; 1114 int error; 1115 int do_wakeup, do_undos; 1116 unsigned short seq; 1117 1118 #ifdef SEM_DEBUG 1119 sops = NULL; 1120 #endif 1121 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops)); 1122 1123 AUDIT_ARG_SVIPC_ID(semid); 1124 1125 rpr = sem_find_prison(td->td_ucred); 1126 if (sem == NULL) 1127 return (ENOSYS); 1128 1129 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */ 1130 1131 if (semid < 0 || semid >= seminfo.semmni) 1132 return (EINVAL); 1133 1134 /* Allocate memory for sem_ops */ 1135 if (nsops <= SMALL_SOPS) 1136 sops = small_sops; 1137 else if (nsops > seminfo.semopm) { 1138 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm, 1139 nsops)); 1140 return (E2BIG); 1141 } else { 1142 #ifdef RACCT 1143 if (racct_enable) { 1144 PROC_LOCK(td->td_proc); 1145 if (nsops > 1146 racct_get_available(td->td_proc, RACCT_NSEMOP)) { 1147 PROC_UNLOCK(td->td_proc); 1148 return (E2BIG); 1149 } 1150 PROC_UNLOCK(td->td_proc); 1151 } 1152 #endif 1153 1154 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK); 1155 } 1156 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) { 1157 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error, 1158 uap->sops, sops, nsops * sizeof(sops[0]))); 1159 if (sops != small_sops) 1160 free(sops, M_TEMP); 1161 return (error); 1162 } 1163 1164 semakptr = &sema[semid]; 1165 sema_mtxp = &sema_mtx[semid]; 1166 mtx_lock(sema_mtxp); 1167 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) { 1168 error = EINVAL; 1169 goto done2; 1170 } 1171 seq = semakptr->u.sem_perm.seq; 1172 if (seq != IPCID_TO_SEQ(uap->semid)) { 1173 error = EINVAL; 1174 goto done2; 1175 } 1176 if ((error = sem_prison_cansee(rpr, semakptr)) != 0) 1177 goto done2; 1178 /* 1179 * Initial pass through sops to see what permissions are needed. 1180 * Also perform any checks that don't need repeating on each 1181 * attempt to satisfy the request vector. 1182 */ 1183 j = 0; /* permission needed */ 1184 do_undos = 0; 1185 for (i = 0; i < nsops; i++) { 1186 sopptr = &sops[i]; 1187 if (sopptr->sem_num >= semakptr->u.sem_nsems) { 1188 error = EFBIG; 1189 goto done2; 1190 } 1191 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0) 1192 do_undos = 1; 1193 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A; 1194 } 1195 1196 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) { 1197 DPRINTF(("error = %d from ipaccess\n", error)); 1198 goto done2; 1199 } 1200 #ifdef MAC 1201 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j); 1202 if (error != 0) 1203 goto done2; 1204 #endif 1205 1206 /* 1207 * Loop trying to satisfy the vector of requests. 1208 * If we reach a point where we must wait, any requests already 1209 * performed are rolled back and we go to sleep until some other 1210 * process wakes us up. At this point, we start all over again. 1211 * 1212 * This ensures that from the perspective of other tasks, a set 1213 * of requests is atomic (never partially satisfied). 1214 */ 1215 for (;;) { 1216 do_wakeup = 0; 1217 error = 0; /* error return if necessary */ 1218 1219 for (i = 0; i < nsops; i++) { 1220 sopptr = &sops[i]; 1221 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1222 1223 DPRINTF(( 1224 "semop: semakptr=%p, __sem_base=%p, " 1225 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n", 1226 semakptr, semakptr->u.__sem_base, semptr, 1227 sopptr->sem_num, semptr->semval, sopptr->sem_op, 1228 (sopptr->sem_flg & IPC_NOWAIT) ? 1229 "nowait" : "wait")); 1230 1231 if (sopptr->sem_op < 0) { 1232 if (semptr->semval + sopptr->sem_op < 0) { 1233 DPRINTF(("semop: can't do it now\n")); 1234 break; 1235 } else { 1236 semptr->semval += sopptr->sem_op; 1237 if (semptr->semval == 0 && 1238 semptr->semzcnt > 0) 1239 do_wakeup = 1; 1240 } 1241 } else if (sopptr->sem_op == 0) { 1242 if (semptr->semval != 0) { 1243 DPRINTF(("semop: not zero now\n")); 1244 break; 1245 } 1246 } else if (semptr->semval + sopptr->sem_op > 1247 seminfo.semvmx) { 1248 error = ERANGE; 1249 break; 1250 } else { 1251 if (semptr->semncnt > 0) 1252 do_wakeup = 1; 1253 semptr->semval += sopptr->sem_op; 1254 } 1255 } 1256 1257 /* 1258 * Did we get through the entire vector? 1259 */ 1260 if (i >= nsops) 1261 goto done; 1262 1263 /* 1264 * No ... rollback anything that we've already done 1265 */ 1266 DPRINTF(("semop: rollback 0 through %d\n", i-1)); 1267 for (j = 0; j < i; j++) 1268 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1269 sops[j].sem_op; 1270 1271 /* If we detected an error, return it */ 1272 if (error != 0) 1273 goto done2; 1274 1275 /* 1276 * If the request that we couldn't satisfy has the 1277 * NOWAIT flag set then return with EAGAIN. 1278 */ 1279 if (sopptr->sem_flg & IPC_NOWAIT) { 1280 error = EAGAIN; 1281 goto done2; 1282 } 1283 1284 if (sopptr->sem_op == 0) 1285 semptr->semzcnt++; 1286 else 1287 semptr->semncnt++; 1288 1289 DPRINTF(("semop: good night!\n")); 1290 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH, 1291 "semwait", 0); 1292 DPRINTF(("semop: good morning (error=%d)!\n", error)); 1293 /* return code is checked below, after sem[nz]cnt-- */ 1294 1295 /* 1296 * Make sure that the semaphore still exists 1297 */ 1298 seq = semakptr->u.sem_perm.seq; 1299 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1300 seq != IPCID_TO_SEQ(uap->semid)) { 1301 error = EIDRM; 1302 goto done2; 1303 } 1304 1305 /* 1306 * Renew the semaphore's pointer after wakeup since 1307 * during msleep __sem_base may have been modified and semptr 1308 * is not valid any more 1309 */ 1310 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1311 1312 /* 1313 * The semaphore is still alive. Readjust the count of 1314 * waiting processes. 1315 */ 1316 if (sopptr->sem_op == 0) 1317 semptr->semzcnt--; 1318 else 1319 semptr->semncnt--; 1320 1321 /* 1322 * Is it really morning, or was our sleep interrupted? 1323 * (Delayed check of msleep() return code because we 1324 * need to decrement sem[nz]cnt either way.) 1325 */ 1326 if (error != 0) { 1327 error = EINTR; 1328 goto done2; 1329 } 1330 DPRINTF(("semop: good morning!\n")); 1331 } 1332 1333 done: 1334 /* 1335 * Process any SEM_UNDO requests. 1336 */ 1337 if (do_undos) { 1338 SEMUNDO_LOCK(); 1339 suptr = NULL; 1340 for (i = 0; i < nsops; i++) { 1341 /* 1342 * We only need to deal with SEM_UNDO's for non-zero 1343 * op's. 1344 */ 1345 int adjval; 1346 1347 if ((sops[i].sem_flg & SEM_UNDO) == 0) 1348 continue; 1349 adjval = sops[i].sem_op; 1350 if (adjval == 0) 1351 continue; 1352 error = semundo_adjust(td, &suptr, semid, seq, 1353 sops[i].sem_num, -adjval); 1354 if (error == 0) 1355 continue; 1356 1357 /* 1358 * Oh-Oh! We ran out of either sem_undo's or undo's. 1359 * Rollback the adjustments to this point and then 1360 * rollback the semaphore ups and down so we can return 1361 * with an error with all structures restored. We 1362 * rollback the undo's in the exact reverse order that 1363 * we applied them. This guarantees that we won't run 1364 * out of space as we roll things back out. 1365 */ 1366 for (j = 0; j < i; j++) { 1367 k = i - j - 1; 1368 if ((sops[k].sem_flg & SEM_UNDO) == 0) 1369 continue; 1370 adjval = sops[k].sem_op; 1371 if (adjval == 0) 1372 continue; 1373 if (semundo_adjust(td, &suptr, semid, seq, 1374 sops[k].sem_num, adjval) != 0) 1375 panic("semop - can't undo undos"); 1376 } 1377 1378 for (j = 0; j < nsops; j++) 1379 semakptr->u.__sem_base[sops[j].sem_num].semval -= 1380 sops[j].sem_op; 1381 1382 DPRINTF(("error = %d from semundo_adjust\n", error)); 1383 SEMUNDO_UNLOCK(); 1384 goto done2; 1385 } /* loop through the sops */ 1386 SEMUNDO_UNLOCK(); 1387 } /* if (do_undos) */ 1388 1389 /* We're definitely done - set the sempid's and time */ 1390 for (i = 0; i < nsops; i++) { 1391 sopptr = &sops[i]; 1392 semptr = &semakptr->u.__sem_base[sopptr->sem_num]; 1393 semptr->sempid = td->td_proc->p_pid; 1394 } 1395 semakptr->u.sem_otime = time_second; 1396 1397 /* 1398 * Do a wakeup if any semaphore was up'd whilst something was 1399 * sleeping on it. 1400 */ 1401 if (do_wakeup) { 1402 DPRINTF(("semop: doing wakeup\n")); 1403 wakeup(semakptr); 1404 DPRINTF(("semop: back from wakeup\n")); 1405 } 1406 DPRINTF(("semop: done\n")); 1407 td->td_retval[0] = 0; 1408 done2: 1409 mtx_unlock(sema_mtxp); 1410 if (sops != small_sops) 1411 free(sops, M_TEMP); 1412 return (error); 1413 } 1414 1415 /* 1416 * Go through the undo structures for this process and apply the adjustments to 1417 * semaphores. 1418 */ 1419 static void 1420 semexit_myhook(void *arg, struct proc *p) 1421 { 1422 struct sem_undo *suptr; 1423 struct semid_kernel *semakptr; 1424 struct mtx *sema_mtxp; 1425 int semid, semnum, adjval, ix; 1426 unsigned short seq; 1427 1428 /* 1429 * Go through the chain of undo vectors looking for one 1430 * associated with this process. 1431 */ 1432 if (LIST_EMPTY(&semu_list)) 1433 return; 1434 SEMUNDO_LOCK(); 1435 LIST_FOREACH(suptr, &semu_list, un_next) { 1436 if (suptr->un_proc == p) 1437 break; 1438 } 1439 if (suptr == NULL) { 1440 SEMUNDO_UNLOCK(); 1441 return; 1442 } 1443 LIST_REMOVE(suptr, un_next); 1444 1445 DPRINTF(("proc @%p has undo structure with %d entries\n", p, 1446 suptr->un_cnt)); 1447 1448 /* 1449 * If there are any active undo elements then process them. 1450 */ 1451 if (suptr->un_cnt > 0) { 1452 SEMUNDO_UNLOCK(); 1453 for (ix = 0; ix < suptr->un_cnt; ix++) { 1454 semid = suptr->un_ent[ix].un_id; 1455 semnum = suptr->un_ent[ix].un_num; 1456 adjval = suptr->un_ent[ix].un_adjval; 1457 seq = suptr->un_ent[ix].un_seq; 1458 semakptr = &sema[semid]; 1459 sema_mtxp = &sema_mtx[semid]; 1460 1461 mtx_lock(sema_mtxp); 1462 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 || 1463 (semakptr->u.sem_perm.seq != seq)) { 1464 mtx_unlock(sema_mtxp); 1465 continue; 1466 } 1467 if (semnum >= semakptr->u.sem_nsems) 1468 panic("semexit - semnum out of range"); 1469 1470 DPRINTF(( 1471 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n", 1472 suptr->un_proc, suptr->un_ent[ix].un_id, 1473 suptr->un_ent[ix].un_num, 1474 suptr->un_ent[ix].un_adjval, 1475 semakptr->u.__sem_base[semnum].semval)); 1476 1477 if (adjval < 0 && semakptr->u.__sem_base[semnum].semval < 1478 -adjval) 1479 semakptr->u.__sem_base[semnum].semval = 0; 1480 else 1481 semakptr->u.__sem_base[semnum].semval += adjval; 1482 1483 wakeup(semakptr); 1484 DPRINTF(("semexit: back from wakeup\n")); 1485 mtx_unlock(sema_mtxp); 1486 } 1487 SEMUNDO_LOCK(); 1488 } 1489 1490 /* 1491 * Deallocate the undo vector. 1492 */ 1493 DPRINTF(("removing vector\n")); 1494 suptr->un_proc = NULL; 1495 suptr->un_cnt = 0; 1496 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next); 1497 SEMUNDO_UNLOCK(); 1498 } 1499 1500 static int 1501 sysctl_sema(SYSCTL_HANDLER_ARGS) 1502 { 1503 struct prison *pr, *rpr; 1504 struct semid_kernel tsemak; 1505 #ifdef COMPAT_FREEBSD32 1506 struct semid_kernel32 tsemak32; 1507 #endif 1508 void *outaddr; 1509 size_t outsize; 1510 int error, i; 1511 1512 pr = req->td->td_ucred->cr_prison; 1513 rpr = sem_find_prison(req->td->td_ucred); 1514 error = 0; 1515 for (i = 0; i < seminfo.semmni; i++) { 1516 mtx_lock(&sema_mtx[i]); 1517 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 || 1518 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0) 1519 bzero(&tsemak, sizeof(tsemak)); 1520 else { 1521 tsemak = sema[i]; 1522 if (tsemak.cred->cr_prison != pr) 1523 tsemak.u.sem_perm.key = IPC_PRIVATE; 1524 } 1525 mtx_unlock(&sema_mtx[i]); 1526 #ifdef COMPAT_FREEBSD32 1527 if (SV_CURPROC_FLAG(SV_ILP32)) { 1528 bzero(&tsemak32, sizeof(tsemak32)); 1529 freebsd32_ipcperm_out(&tsemak.u.sem_perm, 1530 &tsemak32.u.sem_perm); 1531 /* Don't copy u.__sem_base */ 1532 CP(tsemak, tsemak32, u.sem_nsems); 1533 CP(tsemak, tsemak32, u.sem_otime); 1534 CP(tsemak, tsemak32, u.sem_ctime); 1535 /* Don't copy label or cred */ 1536 outaddr = &tsemak32; 1537 outsize = sizeof(tsemak32); 1538 } else 1539 #endif 1540 { 1541 tsemak.u.__sem_base = NULL; 1542 tsemak.label = NULL; 1543 tsemak.cred = NULL; 1544 outaddr = &tsemak; 1545 outsize = sizeof(tsemak); 1546 } 1547 error = SYSCTL_OUT(req, outaddr, outsize); 1548 if (error != 0) 1549 break; 1550 } 1551 return (error); 1552 } 1553 1554 static int 1555 sem_prison_check(void *obj, void *data) 1556 { 1557 struct prison *pr = obj; 1558 struct prison *prpr; 1559 struct vfsoptlist *opts = data; 1560 int error, jsys; 1561 1562 /* 1563 * sysvsem is a jailsys integer. 1564 * It must be "disable" if the parent jail is disabled. 1565 */ 1566 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1567 if (error != ENOENT) { 1568 if (error != 0) 1569 return (error); 1570 switch (jsys) { 1571 case JAIL_SYS_DISABLE: 1572 break; 1573 case JAIL_SYS_NEW: 1574 case JAIL_SYS_INHERIT: 1575 prison_lock(pr->pr_parent); 1576 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1577 prison_unlock(pr->pr_parent); 1578 if (prpr == NULL) 1579 return (EPERM); 1580 break; 1581 default: 1582 return (EINVAL); 1583 } 1584 } 1585 1586 return (0); 1587 } 1588 1589 static int 1590 sem_prison_set(void *obj, void *data) 1591 { 1592 struct prison *pr = obj; 1593 struct prison *tpr, *orpr, *nrpr, *trpr; 1594 struct vfsoptlist *opts = data; 1595 void *rsv; 1596 int jsys, descend; 1597 1598 /* 1599 * sysvsem controls which jail is the root of the associated sems (this 1600 * jail or same as the parent), or if the feature is available at all. 1601 */ 1602 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT) 1603 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0) 1604 ? JAIL_SYS_INHERIT 1605 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0) 1606 ? JAIL_SYS_DISABLE 1607 : -1; 1608 if (jsys == JAIL_SYS_DISABLE) { 1609 prison_lock(pr); 1610 orpr = osd_jail_get(pr, sem_prison_slot); 1611 if (orpr != NULL) 1612 osd_jail_del(pr, sem_prison_slot); 1613 prison_unlock(pr); 1614 if (orpr != NULL) { 1615 if (orpr == pr) 1616 sem_prison_cleanup(pr); 1617 /* Disable all child jails as well. */ 1618 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1619 prison_lock(tpr); 1620 trpr = osd_jail_get(tpr, sem_prison_slot); 1621 if (trpr != NULL) { 1622 osd_jail_del(tpr, sem_prison_slot); 1623 prison_unlock(tpr); 1624 if (trpr == tpr) 1625 sem_prison_cleanup(tpr); 1626 } else { 1627 prison_unlock(tpr); 1628 descend = 0; 1629 } 1630 } 1631 } 1632 } else if (jsys != -1) { 1633 if (jsys == JAIL_SYS_NEW) 1634 nrpr = pr; 1635 else { 1636 prison_lock(pr->pr_parent); 1637 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot); 1638 prison_unlock(pr->pr_parent); 1639 } 1640 rsv = osd_reserve(sem_prison_slot); 1641 prison_lock(pr); 1642 orpr = osd_jail_get(pr, sem_prison_slot); 1643 if (orpr != nrpr) 1644 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv, 1645 nrpr); 1646 else 1647 osd_free_reserved(rsv); 1648 prison_unlock(pr); 1649 if (orpr != nrpr) { 1650 if (orpr == pr) 1651 sem_prison_cleanup(pr); 1652 if (orpr != NULL) { 1653 /* Change child jails matching the old root, */ 1654 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1655 prison_lock(tpr); 1656 trpr = osd_jail_get(tpr, 1657 sem_prison_slot); 1658 if (trpr == orpr) { 1659 (void)osd_jail_set(tpr, 1660 sem_prison_slot, nrpr); 1661 prison_unlock(tpr); 1662 if (trpr == tpr) 1663 sem_prison_cleanup(tpr); 1664 } else { 1665 prison_unlock(tpr); 1666 descend = 0; 1667 } 1668 } 1669 } 1670 } 1671 } 1672 1673 return (0); 1674 } 1675 1676 static int 1677 sem_prison_get(void *obj, void *data) 1678 { 1679 struct prison *pr = obj; 1680 struct prison *rpr; 1681 struct vfsoptlist *opts = data; 1682 int error, jsys; 1683 1684 /* Set sysvsem based on the jail's root prison. */ 1685 prison_lock(pr); 1686 rpr = osd_jail_get(pr, sem_prison_slot); 1687 prison_unlock(pr); 1688 jsys = rpr == NULL ? JAIL_SYS_DISABLE 1689 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT; 1690 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys)); 1691 if (error == ENOENT) 1692 error = 0; 1693 return (error); 1694 } 1695 1696 static int 1697 sem_prison_remove(void *obj, void *data __unused) 1698 { 1699 struct prison *pr = obj; 1700 struct prison *rpr; 1701 1702 prison_lock(pr); 1703 rpr = osd_jail_get(pr, sem_prison_slot); 1704 prison_unlock(pr); 1705 if (rpr == pr) 1706 sem_prison_cleanup(pr); 1707 return (0); 1708 } 1709 1710 static void 1711 sem_prison_cleanup(struct prison *pr) 1712 { 1713 int i; 1714 1715 /* Remove any sems that belong to this jail. */ 1716 mtx_lock(&sem_mtx); 1717 for (i = 0; i < seminfo.semmni; i++) { 1718 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) && 1719 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) { 1720 mtx_lock(&sema_mtx[i]); 1721 sem_remove(i, NULL); 1722 mtx_unlock(&sema_mtx[i]); 1723 } 1724 } 1725 mtx_unlock(&sem_mtx); 1726 } 1727 1728 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores"); 1729 1730 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1731 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1732 1733 /* XXX casting to (sy_call_t *) is bogus, as usual. */ 1734 static sy_call_t *semcalls[] = { 1735 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget, 1736 (sy_call_t *)sys_semop 1737 }; 1738 1739 /* 1740 * Entry point for all SEM calls. 1741 */ 1742 int 1743 sys_semsys(td, uap) 1744 struct thread *td; 1745 /* XXX actually varargs. */ 1746 struct semsys_args /* { 1747 int which; 1748 int a2; 1749 int a3; 1750 int a4; 1751 int a5; 1752 } */ *uap; 1753 { 1754 int error; 1755 1756 AUDIT_ARG_SVIPC_WHICH(uap->which); 1757 if (uap->which < 0 || uap->which >= nitems(semcalls)) 1758 return (EINVAL); 1759 error = (*semcalls[uap->which])(td, &uap->a2); 1760 return (error); 1761 } 1762 1763 #ifndef _SYS_SYSPROTO_H_ 1764 struct freebsd7___semctl_args { 1765 int semid; 1766 int semnum; 1767 int cmd; 1768 union semun_old *arg; 1769 }; 1770 #endif 1771 int 1772 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap) 1773 { 1774 struct semid_ds_old dsold; 1775 struct semid_ds dsbuf; 1776 union semun_old arg; 1777 union semun semun; 1778 register_t rval; 1779 int error; 1780 1781 switch (uap->cmd) { 1782 case SEM_STAT: 1783 case IPC_SET: 1784 case IPC_STAT: 1785 case GETALL: 1786 case SETVAL: 1787 case SETALL: 1788 error = copyin(uap->arg, &arg, sizeof(arg)); 1789 if (error) 1790 return (error); 1791 break; 1792 } 1793 1794 switch (uap->cmd) { 1795 case SEM_STAT: 1796 case IPC_STAT: 1797 semun.buf = &dsbuf; 1798 break; 1799 case IPC_SET: 1800 error = copyin(arg.buf, &dsold, sizeof(dsold)); 1801 if (error) 1802 return (error); 1803 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm); 1804 CP(dsold, dsbuf, __sem_base); 1805 CP(dsold, dsbuf, sem_nsems); 1806 CP(dsold, dsbuf, sem_otime); 1807 CP(dsold, dsbuf, sem_ctime); 1808 semun.buf = &dsbuf; 1809 break; 1810 case GETALL: 1811 case SETALL: 1812 semun.array = arg.array; 1813 break; 1814 case SETVAL: 1815 semun.val = arg.val; 1816 break; 1817 } 1818 1819 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1820 &rval); 1821 if (error) 1822 return (error); 1823 1824 switch (uap->cmd) { 1825 case SEM_STAT: 1826 case IPC_STAT: 1827 bzero(&dsold, sizeof(dsold)); 1828 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm); 1829 CP(dsbuf, dsold, __sem_base); 1830 CP(dsbuf, dsold, sem_nsems); 1831 CP(dsbuf, dsold, sem_otime); 1832 CP(dsbuf, dsold, sem_ctime); 1833 error = copyout(&dsold, arg.buf, sizeof(dsold)); 1834 break; 1835 } 1836 1837 if (error == 0) 1838 td->td_retval[0] = rval; 1839 return (error); 1840 } 1841 1842 #endif /* COMPAT_FREEBSD{4,5,6,7} */ 1843 1844 #ifdef COMPAT_FREEBSD32 1845 1846 int 1847 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap) 1848 { 1849 1850 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1851 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1852 AUDIT_ARG_SVIPC_WHICH(uap->which); 1853 switch (uap->which) { 1854 case 0: 1855 return (freebsd7_freebsd32_semctl(td, 1856 (struct freebsd7_freebsd32_semctl_args *)&uap->a2)); 1857 default: 1858 return (sys_semsys(td, (struct semsys_args *)uap)); 1859 } 1860 #else 1861 return (nosys(td, NULL)); 1862 #endif 1863 } 1864 1865 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 1866 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) 1867 int 1868 freebsd7_freebsd32_semctl(struct thread *td, 1869 struct freebsd7_freebsd32_semctl_args *uap) 1870 { 1871 struct semid_ds32_old dsbuf32; 1872 struct semid_ds dsbuf; 1873 union semun semun; 1874 union semun32 arg; 1875 register_t rval; 1876 int error; 1877 1878 switch (uap->cmd) { 1879 case SEM_STAT: 1880 case IPC_SET: 1881 case IPC_STAT: 1882 case GETALL: 1883 case SETVAL: 1884 case SETALL: 1885 error = copyin(uap->arg, &arg, sizeof(arg)); 1886 if (error) 1887 return (error); 1888 break; 1889 } 1890 1891 switch (uap->cmd) { 1892 case SEM_STAT: 1893 case IPC_STAT: 1894 semun.buf = &dsbuf; 1895 break; 1896 case IPC_SET: 1897 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1898 if (error) 1899 return (error); 1900 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1901 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1902 CP(dsbuf32, dsbuf, sem_nsems); 1903 CP(dsbuf32, dsbuf, sem_otime); 1904 CP(dsbuf32, dsbuf, sem_ctime); 1905 semun.buf = &dsbuf; 1906 break; 1907 case GETALL: 1908 case SETALL: 1909 semun.array = PTRIN(arg.array); 1910 break; 1911 case SETVAL: 1912 semun.val = arg.val; 1913 break; 1914 } 1915 1916 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1917 &rval); 1918 if (error) 1919 return (error); 1920 1921 switch (uap->cmd) { 1922 case SEM_STAT: 1923 case IPC_STAT: 1924 bzero(&dsbuf32, sizeof(dsbuf32)); 1925 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1926 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1927 CP(dsbuf, dsbuf32, sem_nsems); 1928 CP(dsbuf, dsbuf32, sem_otime); 1929 CP(dsbuf, dsbuf32, sem_ctime); 1930 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 1931 break; 1932 } 1933 1934 if (error == 0) 1935 td->td_retval[0] = rval; 1936 return (error); 1937 } 1938 #endif 1939 1940 int 1941 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap) 1942 { 1943 struct semid_ds32 dsbuf32; 1944 struct semid_ds dsbuf; 1945 union semun semun; 1946 union semun32 arg; 1947 register_t rval; 1948 int error; 1949 1950 switch (uap->cmd) { 1951 case SEM_STAT: 1952 case IPC_SET: 1953 case IPC_STAT: 1954 case GETALL: 1955 case SETVAL: 1956 case SETALL: 1957 error = copyin(uap->arg, &arg, sizeof(arg)); 1958 if (error) 1959 return (error); 1960 break; 1961 } 1962 1963 switch (uap->cmd) { 1964 case SEM_STAT: 1965 case IPC_STAT: 1966 semun.buf = &dsbuf; 1967 break; 1968 case IPC_SET: 1969 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32)); 1970 if (error) 1971 return (error); 1972 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm); 1973 PTRIN_CP(dsbuf32, dsbuf, __sem_base); 1974 CP(dsbuf32, dsbuf, sem_nsems); 1975 CP(dsbuf32, dsbuf, sem_otime); 1976 CP(dsbuf32, dsbuf, sem_ctime); 1977 semun.buf = &dsbuf; 1978 break; 1979 case GETALL: 1980 case SETALL: 1981 semun.array = PTRIN(arg.array); 1982 break; 1983 case SETVAL: 1984 semun.val = arg.val; 1985 break; 1986 } 1987 1988 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun, 1989 &rval); 1990 if (error) 1991 return (error); 1992 1993 switch (uap->cmd) { 1994 case SEM_STAT: 1995 case IPC_STAT: 1996 bzero(&dsbuf32, sizeof(dsbuf32)); 1997 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm); 1998 PTROUT_CP(dsbuf, dsbuf32, __sem_base); 1999 CP(dsbuf, dsbuf32, sem_nsems); 2000 CP(dsbuf, dsbuf32, sem_otime); 2001 CP(dsbuf, dsbuf32, sem_ctime); 2002 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32)); 2003 break; 2004 } 2005 2006 if (error == 0) 2007 td->td_retval[0] = rval; 2008 return (error); 2009 } 2010 2011 #endif /* COMPAT_FREEBSD32 */ 2012