2 * Implementation of SVID semaphores
4 * Author: Daniel Boulet
6 * This software is provided ``AS IS'' without any warranties of any kind.
9 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
11 * Copyright (c) 2003-2005 McAfee, Inc.
12 * Copyright (c) 2016-2017 Robert N. M. Watson
13 * All rights reserved.
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
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.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
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.
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
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include "opt_sysvipc.h"
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>
60 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #include <sys/racct.h>
65 #include <sys/syscall.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysent.h>
68 #include <sys/sysctl.h>
70 #include <sys/malloc.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
76 FEATURE(sysv_sem, "System V semaphores support");
78 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
81 #define DPRINTF(a) printf a
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 *);
102 #ifndef _SYS_SYSPROTO_H_
103 struct __semctl_args;
104 int __semctl(struct thread *td, struct __semctl_args *uap);
106 int semget(struct thread *td, struct semget_args *uap);
108 int semop(struct thread *td, struct semop_args *uap);
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);
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 */
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));
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 */
141 * Undo structure (one per process)
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 */
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 */
156 * Configuration parameters
159 #define SEMMNI 50 /* # of semaphore identifiers */
162 #define SEMMNS 340 /* # of semaphores in system */
165 #define SEMUME 50 /* max # of undo entries per process */
168 #define SEMMNU 150 /* # of undo structures in system */
171 /* shouldn't need tuning */
173 #define SEMMSL SEMMNS /* max # of semaphores per id */
176 #define SEMOPM 100 /* max # of operations per semop call */
179 #define SEMVMX 32767 /* semaphore maximum value */
180 #define SEMAEM 16384 /* adjust on exit max value */
183 * Due to the way semaphore memory is allocated, we have to ensure that
184 * SEMUSZ is properly aligned.
187 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long))
189 /* actual size of an undo structure */
190 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
193 * Macro to find a particular sem_undo vector
196 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
199 * semaphore info struct
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 */
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");
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),
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>
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),
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,
282 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
283 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
285 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
287 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
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;
294 mac_sysvsem_init(&sema[i]);
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);
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);
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);
318 sx_slock(&allprison_lock);
319 TAILQ_FOREACH(pr, &allprison, pr_list) {
321 rsv = osd_reserve(sem_prison_slot);
323 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
324 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
331 osd_free_reserved(rsv);
332 sx_sunlock(&allprison_lock);
334 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
337 #ifdef COMPAT_FREEBSD32
338 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
354 #ifdef COMPAT_FREEBSD32
355 syscall32_helper_unregister(sem32_syscalls);
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);
362 for (i = 0; i < seminfo.semmni; i++)
363 mac_sysvsem_destroy(&sema[i]);
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);
377 sysvsem_modload(struct module *module, int cmd, void *arg)
399 static moduledata_t sysvsem_mod = {
405 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
406 MODULE_VERSION(sysvsem, 1);
409 * Allocate a new sem_undo structure for a process
410 * (returns ptr to structure or NULL if no more room)
413 static struct sem_undo *
414 semu_alloc(struct thread *td)
416 struct sem_undo *suptr;
418 SEMUNDO_LOCKASSERT(MA_OWNED);
419 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
421 LIST_REMOVE(suptr, un_next);
422 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
424 suptr->un_proc = td->td_proc;
429 semu_try_free(struct sem_undo *suptr)
432 SEMUNDO_LOCKASSERT(MA_OWNED);
434 if (suptr->un_cnt != 0)
436 LIST_REMOVE(suptr, un_next);
437 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
442 * Adjust a particular entry for a particular proc
446 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
447 int semseq, int semnum, int adjval)
449 struct proc *p = td->td_proc;
450 struct sem_undo *suptr;
454 SEMUNDO_LOCKASSERT(MA_OWNED);
455 /* Look for and remember the sem_undo if the caller doesn't provide
460 LIST_FOREACH(suptr, &semu_list, un_next) {
461 if (suptr->un_proc == p) {
469 suptr = semu_alloc(td);
477 * Look for the requested entry and adjust it (delete if adjval becomes
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)
485 adjval += sunptr->un_adjval;
486 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
489 sunptr->un_adjval = adjval;
490 if (sunptr->un_adjval == 0) {
492 if (i < suptr->un_cnt)
494 suptr->un_ent[suptr->un_cnt];
495 if (suptr->un_cnt == 0)
496 semu_try_free(suptr);
501 /* Didn't find the right entry - create it */
504 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
506 if (suptr->un_cnt != seminfo.semume) {
507 sunptr = &suptr->un_ent[suptr->un_cnt];
509 sunptr->un_adjval = adjval;
510 sunptr->un_id = semid;
511 sunptr->un_num = semnum;
512 sunptr->un_seq = semseq;
519 semundo_clear(int semid, int semnum)
521 struct sem_undo *suptr, *suptr1;
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)
531 if (semnum == -1 || sunptr->un_num == semnum) {
533 if (i < suptr->un_cnt) {
535 suptr->un_ent[suptr->un_cnt];
538 semu_try_free(suptr);
547 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
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);
556 sem_remove(int semidx, struct ucred *cred)
558 struct semid_kernel *semakptr;
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,
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;
577 semundo_clear(semidx, -1);
580 mac_sysvsem_cleanup(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);
588 for (i = semakptr->u.__sem_base - sem + semakptr->u.sem_nsems;
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]);
598 semtot -= semakptr->u.sem_nsems;
601 static struct prison *
602 sem_find_prison(struct ucred *cred)
604 struct prison *pr, *rpr;
606 pr = cred->cr_prison;
608 rpr = osd_jail_get(pr, sem_prison_slot);
614 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
617 if (semakptr->cred == NULL ||
618 !(rpr == semakptr->cred->cr_prison ||
619 prison_ischild(rpr, semakptr->cred->cr_prison)))
625 * Note that the user-mode half of this passes a union, not a pointer.
627 #ifndef _SYS_SYSPROTO_H_
628 struct __semctl_args {
636 sys___semctl(struct thread *td, struct __semctl_args *uap)
638 struct semid_ds dsbuf;
639 union semun arg, semun;
650 error = copyin(uap->arg, &arg, sizeof(arg));
662 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
669 semun.array = arg.array;
676 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
684 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
689 td->td_retval[0] = rval;
694 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
695 union semun *arg, register_t *rval)
698 struct ucred *cred = td->td_ucred;
701 struct semid_ds *sbuf;
702 struct semid_kernel *semakptr;
703 struct mtx *sema_mtxp;
704 u_short usval, count;
707 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
708 semid, semnum, cmd, arg));
710 AUDIT_ARG_SVIPC_CMD(cmd);
711 AUDIT_ARG_SVIPC_ID(semid);
713 rpr = sem_find_prison(td->td_ucred);
722 * For this command we assume semid is an array index
723 * rather than an IPC id.
725 if (semid < 0 || semid >= seminfo.semmni)
727 semakptr = &sema[semid];
728 sema_mtxp = &sema_mtx[semid];
730 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
734 if ((error = sem_prison_cansee(rpr, semakptr)))
736 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
739 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
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);
751 semidx = IPCID_TO_IX(semid);
752 if (semidx < 0 || semidx >= seminfo.semmni)
755 semakptr = &sema[semidx];
756 sema_mtxp = &sema_mtx[semidx];
762 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
772 if ((error = semvalid(semid, rpr, semakptr)) != 0)
774 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
776 sem_remove(semidx, cred);
780 AUDIT_ARG_SVIPC_PERM(&arg->buf->sem_perm);
781 if ((error = semvalid(semid, rpr, semakptr)) != 0)
783 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
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;
794 if ((error = semvalid(semid, rpr, semakptr)) != 0)
796 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
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;
804 if ((error = semvalid(semid, rpr, semakptr)) != 0)
806 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
808 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
812 *rval = semakptr->u.__sem_base[semnum].semncnt;
816 if ((error = semvalid(semid, rpr, semakptr)) != 0)
818 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
820 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
824 *rval = semakptr->u.__sem_base[semnum].sempid;
828 if ((error = semvalid(semid, rpr, semakptr)) != 0)
830 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
832 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
836 *rval = semakptr->u.__sem_base[semnum].semval;
841 * Unfortunately, callers of this function don't know
842 * in advance how many semaphores are in this set.
843 * While we could just allocate the maximum size array
844 * and pass the actual size back to the caller, that
845 * won't work for SETALL since we can't copyin() more
846 * data than the user specified as we may return a
849 * Note that the number of semaphores in a set is
850 * fixed for the life of that set. The only way that
851 * the 'count' could change while are blocked in
852 * malloc() is if this semaphore set were destroyed
853 * and a new one created with the same index.
854 * However, semvalid() will catch that due to the
855 * sequence number unless exactly 0x8000 (or a
856 * multiple thereof) semaphore sets for the same index
857 * are created and destroyed while we are in malloc!
860 count = semakptr->u.sem_nsems;
861 mtx_unlock(sema_mtxp);
862 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
864 if ((error = semvalid(semid, rpr, semakptr)) != 0)
866 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
867 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
869 for (i = 0; i < semakptr->u.sem_nsems; i++)
870 array[i] = semakptr->u.__sem_base[i].semval;
871 mtx_unlock(sema_mtxp);
872 error = copyout(array, arg->array, count * sizeof(*array));
877 if ((error = semvalid(semid, rpr, semakptr)) != 0)
879 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
881 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
885 *rval = semakptr->u.__sem_base[semnum].semzcnt;
889 if ((error = semvalid(semid, rpr, semakptr)) != 0)
891 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
893 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
897 if (arg->val < 0 || arg->val > seminfo.semvmx) {
901 semakptr->u.__sem_base[semnum].semval = arg->val;
903 semundo_clear(semidx, semnum);
910 * See comment on GETALL for why 'count' shouldn't change
911 * and why we require a userland buffer.
913 count = semakptr->u.sem_nsems;
914 mtx_unlock(sema_mtxp);
915 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
916 error = copyin(arg->array, array, count * sizeof(*array));
920 if ((error = semvalid(semid, rpr, semakptr)) != 0)
922 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
923 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
925 for (i = 0; i < semakptr->u.sem_nsems; i++) {
927 if (usval > seminfo.semvmx) {
931 semakptr->u.__sem_base[i].semval = usval;
934 semundo_clear(semidx, -1);
945 mtx_unlock(sema_mtxp);
947 mtx_unlock(&sem_mtx);
953 #ifndef _SYS_SYSPROTO_H_
961 sys_semget(struct thread *td, struct semget_args *uap)
963 int semid, error = 0;
965 int nsems = uap->nsems;
966 int semflg = uap->semflg;
967 struct ucred *cred = td->td_ucred;
969 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
971 AUDIT_ARG_VALUE(semflg);
973 if (sem_find_prison(cred) == NULL)
977 if (key != IPC_PRIVATE) {
978 for (semid = 0; semid < seminfo.semmni; semid++) {
979 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
980 sema[semid].cred != NULL &&
981 sema[semid].cred->cr_prison == cred->cr_prison &&
982 sema[semid].u.sem_perm.key == key)
985 if (semid < seminfo.semmni) {
986 AUDIT_ARG_SVIPC_ID(semid);
987 DPRINTF(("found public key\n"));
988 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
989 DPRINTF(("not exclusive\n"));
993 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
997 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
998 DPRINTF(("too small\n"));
1003 error = mac_sysvsem_check_semget(cred, &sema[semid]);
1011 DPRINTF(("need to allocate the semid_kernel\n"));
1012 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
1013 if (nsems <= 0 || nsems > seminfo.semmsl) {
1014 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
1019 if (nsems > seminfo.semmns - semtot) {
1021 "not enough semaphores left (need %d, got %d)\n",
1022 nsems, seminfo.semmns - semtot));
1026 for (semid = 0; semid < seminfo.semmni; semid++) {
1027 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1030 if (semid == seminfo.semmni) {
1031 DPRINTF(("no more semid_kernel's available\n"));
1037 PROC_LOCK(td->td_proc);
1038 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1039 PROC_UNLOCK(td->td_proc);
1046 DPRINTF(("semid %d is available\n", semid));
1047 mtx_lock(&sema_mtx[semid]);
1048 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1049 ("Lost semaphore %d", semid));
1050 sema[semid].u.sem_perm.key = key;
1051 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1052 sema[semid].u.sem_perm.uid = cred->cr_uid;
1053 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1054 sema[semid].u.sem_perm.gid = cred->cr_gid;
1055 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1056 sema[semid].cred = crhold(cred);
1057 sema[semid].u.sem_perm.seq =
1058 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1059 sema[semid].u.sem_nsems = nsems;
1060 sema[semid].u.sem_otime = 0;
1061 sema[semid].u.sem_ctime = time_second;
1062 sema[semid].u.__sem_base = &sem[semtot];
1064 bzero(sema[semid].u.__sem_base,
1065 sizeof(sema[semid].u.__sem_base[0])*nsems);
1067 mac_sysvsem_create(cred, &sema[semid]);
1069 mtx_unlock(&sema_mtx[semid]);
1070 DPRINTF(("sembase = %p, next = %p\n",
1071 sema[semid].u.__sem_base, &sem[semtot]));
1073 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1079 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1081 mtx_unlock(&sem_mtx);
1085 #ifndef _SYS_SYSPROTO_H_
1088 struct sembuf *sops;
1093 sys_semop(struct thread *td, struct semop_args *uap)
1095 #define SMALL_SOPS 8
1096 struct sembuf small_sops[SMALL_SOPS];
1097 int semid = uap->semid;
1098 size_t nsops = uap->nsops;
1100 struct sembuf *sops;
1101 struct semid_kernel *semakptr;
1102 struct sembuf *sopptr = NULL;
1103 struct sem *semptr = NULL;
1104 struct sem_undo *suptr;
1105 struct mtx *sema_mtxp;
1108 int do_wakeup, do_undos;
1114 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1116 AUDIT_ARG_SVIPC_ID(semid);
1118 rpr = sem_find_prison(td->td_ucred);
1122 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1124 if (semid < 0 || semid >= seminfo.semmni)
1127 /* Allocate memory for sem_ops */
1128 if (nsops <= SMALL_SOPS)
1130 else if (nsops > seminfo.semopm) {
1131 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1137 PROC_LOCK(td->td_proc);
1139 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1140 PROC_UNLOCK(td->td_proc);
1143 PROC_UNLOCK(td->td_proc);
1147 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1149 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1150 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1151 uap->sops, sops, nsops * sizeof(sops[0])));
1152 if (sops != small_sops)
1157 semakptr = &sema[semid];
1158 sema_mtxp = &sema_mtx[semid];
1159 mtx_lock(sema_mtxp);
1160 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1164 seq = semakptr->u.sem_perm.seq;
1165 if (seq != IPCID_TO_SEQ(uap->semid)) {
1169 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1172 * Initial pass through sops to see what permissions are needed.
1173 * Also perform any checks that don't need repeating on each
1174 * attempt to satisfy the request vector.
1176 j = 0; /* permission needed */
1178 for (i = 0; i < nsops; i++) {
1180 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1184 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1186 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1189 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1190 DPRINTF(("error = %d from ipaccess\n", error));
1194 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1200 * Loop trying to satisfy the vector of requests.
1201 * If we reach a point where we must wait, any requests already
1202 * performed are rolled back and we go to sleep until some other
1203 * process wakes us up. At this point, we start all over again.
1205 * This ensures that from the perspective of other tasks, a set
1206 * of requests is atomic (never partially satisfied).
1210 error = 0; /* error return if necessary */
1212 for (i = 0; i < nsops; i++) {
1214 semptr = &semakptr->u.__sem_base[sopptr->sem_num];
1217 "semop: semakptr=%p, __sem_base=%p, "
1218 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1219 semakptr, semakptr->u.__sem_base, semptr,
1220 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1221 (sopptr->sem_flg & IPC_NOWAIT) ?
1222 "nowait" : "wait"));
1224 if (sopptr->sem_op < 0) {
1225 if (semptr->semval + sopptr->sem_op < 0) {
1226 DPRINTF(("semop: can't do it now\n"));
1229 semptr->semval += sopptr->sem_op;
1230 if (semptr->semval == 0 &&
1231 semptr->semzcnt > 0)
1234 } else if (sopptr->sem_op == 0) {
1235 if (semptr->semval != 0) {
1236 DPRINTF(("semop: not zero now\n"));
1239 } else if (semptr->semval + sopptr->sem_op >
1244 if (semptr->semncnt > 0)
1246 semptr->semval += sopptr->sem_op;
1251 * Did we get through the entire vector?
1257 * No ... rollback anything that we've already done
1259 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1260 for (j = 0; j < i; j++)
1261 semakptr->u.__sem_base[sops[j].sem_num].semval -=
1264 /* If we detected an error, return it */
1269 * If the request that we couldn't satisfy has the
1270 * NOWAIT flag set then return with EAGAIN.
1272 if (sopptr->sem_flg & IPC_NOWAIT) {
1277 if (sopptr->sem_op == 0)
1282 DPRINTF(("semop: good night!\n"));
1283 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1285 DPRINTF(("semop: good morning (error=%d)!\n", error));
1286 /* return code is checked below, after sem[nz]cnt-- */
1289 * Make sure that the semaphore still exists
1291 seq = semakptr->u.sem_perm.seq;
1292 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1293 seq != IPCID_TO_SEQ(uap->semid)) {
1299 * Renew the semaphore's pointer after wakeup since
1300 * during msleep __sem_base may have been modified and semptr
1301 * is not valid any more
1303 semptr = &semakptr->u.__sem_base[sopptr->sem_num];
1306 * The semaphore is still alive. Readjust the count of
1307 * waiting processes.
1309 if (sopptr->sem_op == 0)
1315 * Is it really morning, or was our sleep interrupted?
1316 * (Delayed check of msleep() return code because we
1317 * need to decrement sem[nz]cnt either way.)
1323 DPRINTF(("semop: good morning!\n"));
1328 * Process any SEM_UNDO requests.
1333 for (i = 0; i < nsops; i++) {
1335 * We only need to deal with SEM_UNDO's for non-zero
1340 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1342 adjval = sops[i].sem_op;
1345 error = semundo_adjust(td, &suptr, semid, seq,
1346 sops[i].sem_num, -adjval);
1351 * Oh-Oh! We ran out of either sem_undo's or undo's.
1352 * Rollback the adjustments to this point and then
1353 * rollback the semaphore ups and down so we can return
1354 * with an error with all structures restored. We
1355 * rollback the undo's in the exact reverse order that
1356 * we applied them. This guarantees that we won't run
1357 * out of space as we roll things back out.
1359 for (j = 0; j < i; j++) {
1361 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1363 adjval = sops[k].sem_op;
1366 if (semundo_adjust(td, &suptr, semid, seq,
1367 sops[k].sem_num, adjval) != 0)
1368 panic("semop - can't undo undos");
1371 for (j = 0; j < nsops; j++)
1372 semakptr->u.__sem_base[sops[j].sem_num].semval -=
1375 DPRINTF(("error = %d from semundo_adjust\n", error));
1378 } /* loop through the sops */
1380 } /* if (do_undos) */
1382 /* We're definitely done - set the sempid's and time */
1383 for (i = 0; i < nsops; i++) {
1385 semptr = &semakptr->u.__sem_base[sopptr->sem_num];
1386 semptr->sempid = td->td_proc->p_pid;
1388 semakptr->u.sem_otime = time_second;
1391 * Do a wakeup if any semaphore was up'd whilst something was
1395 DPRINTF(("semop: doing wakeup\n"));
1397 DPRINTF(("semop: back from wakeup\n"));
1399 DPRINTF(("semop: done\n"));
1400 td->td_retval[0] = 0;
1402 mtx_unlock(sema_mtxp);
1403 if (sops != small_sops)
1409 * Go through the undo structures for this process and apply the adjustments to
1413 semexit_myhook(void *arg, struct proc *p)
1415 struct sem_undo *suptr;
1416 struct semid_kernel *semakptr;
1417 struct mtx *sema_mtxp;
1418 int semid, semnum, adjval, ix;
1422 * Go through the chain of undo vectors looking for one
1423 * associated with this process.
1425 if (LIST_EMPTY(&semu_list))
1428 LIST_FOREACH(suptr, &semu_list, un_next) {
1429 if (suptr->un_proc == p)
1432 if (suptr == NULL) {
1436 LIST_REMOVE(suptr, un_next);
1438 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1442 * If there are any active undo elements then process them.
1444 if (suptr->un_cnt > 0) {
1446 for (ix = 0; ix < suptr->un_cnt; ix++) {
1447 semid = suptr->un_ent[ix].un_id;
1448 semnum = suptr->un_ent[ix].un_num;
1449 adjval = suptr->un_ent[ix].un_adjval;
1450 seq = suptr->un_ent[ix].un_seq;
1451 semakptr = &sema[semid];
1452 sema_mtxp = &sema_mtx[semid];
1454 mtx_lock(sema_mtxp);
1455 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1456 (semakptr->u.sem_perm.seq != seq)) {
1457 mtx_unlock(sema_mtxp);
1460 if (semnum >= semakptr->u.sem_nsems)
1461 panic("semexit - semnum out of range");
1464 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1465 suptr->un_proc, suptr->un_ent[ix].un_id,
1466 suptr->un_ent[ix].un_num,
1467 suptr->un_ent[ix].un_adjval,
1468 semakptr->u.__sem_base[semnum].semval));
1470 if (adjval < 0 && semakptr->u.__sem_base[semnum].semval <
1472 semakptr->u.__sem_base[semnum].semval = 0;
1474 semakptr->u.__sem_base[semnum].semval += adjval;
1477 DPRINTF(("semexit: back from wakeup\n"));
1478 mtx_unlock(sema_mtxp);
1484 * Deallocate the undo vector.
1486 DPRINTF(("removing vector\n"));
1487 suptr->un_proc = NULL;
1489 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1494 sysctl_sema(SYSCTL_HANDLER_ARGS)
1496 struct prison *pr, *rpr;
1497 struct semid_kernel tsemak;
1498 #ifdef COMPAT_FREEBSD32
1499 struct semid_kernel32 tsemak32;
1505 pr = req->td->td_ucred->cr_prison;
1506 rpr = sem_find_prison(req->td->td_ucred);
1508 for (i = 0; i < seminfo.semmni; i++) {
1509 mtx_lock(&sema_mtx[i]);
1510 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1511 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1512 bzero(&tsemak, sizeof(tsemak));
1515 if (tsemak.cred->cr_prison != pr)
1516 tsemak.u.sem_perm.key = IPC_PRIVATE;
1518 mtx_unlock(&sema_mtx[i]);
1519 #ifdef COMPAT_FREEBSD32
1520 if (SV_CURPROC_FLAG(SV_ILP32)) {
1521 bzero(&tsemak32, sizeof(tsemak32));
1522 freebsd32_ipcperm_out(&tsemak.u.sem_perm,
1523 &tsemak32.u.sem_perm);
1524 /* Don't copy u.__sem_base */
1525 CP(tsemak, tsemak32, u.sem_nsems);
1526 CP(tsemak, tsemak32, u.sem_otime);
1527 CP(tsemak, tsemak32, u.sem_ctime);
1528 /* Don't copy label or cred */
1529 outaddr = &tsemak32;
1530 outsize = sizeof(tsemak32);
1534 tsemak.u.__sem_base = NULL;
1535 tsemak.label = NULL;
1538 outsize = sizeof(tsemak);
1540 error = SYSCTL_OUT(req, outaddr, outsize);
1548 sem_prison_check(void *obj, void *data)
1550 struct prison *pr = obj;
1551 struct prison *prpr;
1552 struct vfsoptlist *opts = data;
1556 * sysvsem is a jailsys integer.
1557 * It must be "disable" if the parent jail is disabled.
1559 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1560 if (error != ENOENT) {
1564 case JAIL_SYS_DISABLE:
1567 case JAIL_SYS_INHERIT:
1568 prison_lock(pr->pr_parent);
1569 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1570 prison_unlock(pr->pr_parent);
1583 sem_prison_set(void *obj, void *data)
1585 struct prison *pr = obj;
1586 struct prison *tpr, *orpr, *nrpr, *trpr;
1587 struct vfsoptlist *opts = data;
1592 * sysvsem controls which jail is the root of the associated sems (this
1593 * jail or same as the parent), or if the feature is available at all.
1595 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1596 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1598 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1601 if (jsys == JAIL_SYS_DISABLE) {
1603 orpr = osd_jail_get(pr, sem_prison_slot);
1605 osd_jail_del(pr, sem_prison_slot);
1609 sem_prison_cleanup(pr);
1610 /* Disable all child jails as well. */
1611 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1613 trpr = osd_jail_get(tpr, sem_prison_slot);
1615 osd_jail_del(tpr, sem_prison_slot);
1618 sem_prison_cleanup(tpr);
1625 } else if (jsys != -1) {
1626 if (jsys == JAIL_SYS_NEW)
1629 prison_lock(pr->pr_parent);
1630 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1631 prison_unlock(pr->pr_parent);
1633 rsv = osd_reserve(sem_prison_slot);
1635 orpr = osd_jail_get(pr, sem_prison_slot);
1637 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1640 osd_free_reserved(rsv);
1644 sem_prison_cleanup(pr);
1646 /* Change child jails matching the old root, */
1647 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1649 trpr = osd_jail_get(tpr,
1652 (void)osd_jail_set(tpr,
1653 sem_prison_slot, nrpr);
1656 sem_prison_cleanup(tpr);
1670 sem_prison_get(void *obj, void *data)
1672 struct prison *pr = obj;
1674 struct vfsoptlist *opts = data;
1677 /* Set sysvsem based on the jail's root prison. */
1679 rpr = osd_jail_get(pr, sem_prison_slot);
1681 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1682 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1683 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1684 if (error == ENOENT)
1690 sem_prison_remove(void *obj, void *data __unused)
1692 struct prison *pr = obj;
1696 rpr = osd_jail_get(pr, sem_prison_slot);
1699 sem_prison_cleanup(pr);
1704 sem_prison_cleanup(struct prison *pr)
1708 /* Remove any sems that belong to this jail. */
1710 for (i = 0; i < seminfo.semmni; i++) {
1711 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1712 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1713 mtx_lock(&sema_mtx[i]);
1714 sem_remove(i, NULL);
1715 mtx_unlock(&sema_mtx[i]);
1718 mtx_unlock(&sem_mtx);
1721 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1723 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1724 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1726 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1727 static sy_call_t *semcalls[] = {
1728 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1729 (sy_call_t *)sys_semop
1733 * Entry point for all SEM calls.
1738 /* XXX actually varargs. */
1739 struct semsys_args /* {
1749 AUDIT_ARG_SVIPC_WHICH(uap->which);
1750 if (uap->which < 0 || uap->which >= nitems(semcalls))
1752 error = (*semcalls[uap->which])(td, &uap->a2);
1756 #ifndef _SYS_SYSPROTO_H_
1757 struct freebsd7___semctl_args {
1761 union semun_old *arg;
1765 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1767 struct semid_ds_old dsold;
1768 struct semid_ds dsbuf;
1769 union semun_old arg;
1781 error = copyin(uap->arg, &arg, sizeof(arg));
1793 error = copyin(arg.buf, &dsold, sizeof(dsold));
1796 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1797 CP(dsold, dsbuf, __sem_base);
1798 CP(dsold, dsbuf, sem_nsems);
1799 CP(dsold, dsbuf, sem_otime);
1800 CP(dsold, dsbuf, sem_ctime);
1805 semun.array = arg.array;
1808 semun.val = arg.val;
1812 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1820 bzero(&dsold, sizeof(dsold));
1821 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1822 CP(dsbuf, dsold, __sem_base);
1823 CP(dsbuf, dsold, sem_nsems);
1824 CP(dsbuf, dsold, sem_otime);
1825 CP(dsbuf, dsold, sem_ctime);
1826 error = copyout(&dsold, arg.buf, sizeof(dsold));
1831 td->td_retval[0] = rval;
1835 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1837 #ifdef COMPAT_FREEBSD32
1840 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1843 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1844 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1845 AUDIT_ARG_SVIPC_WHICH(uap->which);
1846 switch (uap->which) {
1848 return (freebsd7_freebsd32_semctl(td,
1849 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1851 return (sys_semsys(td, (struct semsys_args *)uap));
1854 return (nosys(td, NULL));
1858 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1859 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1861 freebsd7_freebsd32_semctl(struct thread *td,
1862 struct freebsd7_freebsd32_semctl_args *uap)
1864 struct semid_ds32_old dsbuf32;
1865 struct semid_ds dsbuf;
1878 error = copyin(uap->arg, &arg, sizeof(arg));
1890 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1893 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1894 PTRIN_CP(dsbuf32, dsbuf, __sem_base);
1895 CP(dsbuf32, dsbuf, sem_nsems);
1896 CP(dsbuf32, dsbuf, sem_otime);
1897 CP(dsbuf32, dsbuf, sem_ctime);
1902 semun.array = PTRIN(arg.array);
1905 semun.val = arg.val;
1909 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1917 bzero(&dsbuf32, sizeof(dsbuf32));
1918 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1919 PTROUT_CP(dsbuf, dsbuf32, __sem_base);
1920 CP(dsbuf, dsbuf32, sem_nsems);
1921 CP(dsbuf, dsbuf32, sem_otime);
1922 CP(dsbuf, dsbuf32, sem_ctime);
1923 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1928 td->td_retval[0] = rval;
1934 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1936 struct semid_ds32 dsbuf32;
1937 struct semid_ds dsbuf;
1950 error = copyin(uap->arg, &arg, sizeof(arg));
1962 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1965 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1966 PTRIN_CP(dsbuf32, dsbuf, __sem_base);
1967 CP(dsbuf32, dsbuf, sem_nsems);
1968 CP(dsbuf32, dsbuf, sem_otime);
1969 CP(dsbuf32, dsbuf, sem_ctime);
1974 semun.array = PTRIN(arg.array);
1977 semun.val = arg.val;
1981 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1989 bzero(&dsbuf32, sizeof(dsbuf32));
1990 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1991 PTROUT_CP(dsbuf, dsbuf32, __sem_base);
1992 CP(dsbuf, dsbuf32, sem_nsems);
1993 CP(dsbuf, dsbuf32, sem_otime);
1994 CP(dsbuf, dsbuf32, sem_ctime);
1995 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
2000 td->td_retval[0] = rval;
2004 #endif /* COMPAT_FREEBSD32 */