2 * Implementation of SVID semaphores
4 * Author: Daniel Boulet
6 * This software is provided ``AS IS'' without any warranties of any kind.
9 * Copyright (c) 2003-2005 McAfee, Inc.
10 * All rights reserved.
12 * This software was developed for the FreeBSD Project in part by McAfee
13 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
14 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_compat.h"
43 #include "opt_sysvipc.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/eventhandler.h>
49 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
57 #include <sys/syscall.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysent.h>
60 #include <sys/sysctl.h>
62 #include <sys/malloc.h>
65 #include <security/mac/mac_framework.h>
67 FEATURE(sysv_sem, "System V semaphores support");
69 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
72 #define DPRINTF(a) printf a
77 static int seminit(void);
78 static int sysvsem_modload(struct module *, int, void *);
79 static int semunload(void);
80 static void semexit_myhook(void *arg, struct proc *p);
81 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
82 static int semvalid(int semid, struct prison *rpr,
83 struct semid_kernel *semakptr);
84 static void sem_remove(int semidx, struct ucred *cred);
85 static struct prison *sem_find_prison(struct ucred *);
86 static int sem_prison_cansee(struct prison *, struct semid_kernel *);
87 static int sem_prison_check(void *, void *);
88 static int sem_prison_set(void *, void *);
89 static int sem_prison_get(void *, void *);
90 static int sem_prison_remove(void *, void *);
91 static void sem_prison_cleanup(struct prison *);
93 #ifndef _SYS_SYSPROTO_H_
95 int __semctl(struct thread *td, struct __semctl_args *uap);
97 int semget(struct thread *td, struct semget_args *uap);
99 int semop(struct thread *td, struct semop_args *uap);
102 static struct sem_undo *semu_alloc(struct thread *td);
103 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
104 int semid, int semseq, int semnum, int adjval);
105 static void semundo_clear(int semid, int semnum);
107 static struct mtx sem_mtx; /* semaphore global lock */
108 static struct mtx sem_undo_mtx;
109 static int semtot = 0;
110 static struct semid_kernel *sema; /* semaphore id pool */
111 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
112 static struct sem *sem; /* semaphore pool */
113 LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
114 LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */
115 static int *semu; /* undo structure pool */
116 static eventhandler_tag semexit_tag;
117 static unsigned sem_prison_slot; /* prison OSD slot */
119 #define SEMUNDO_MTX sem_undo_mtx
120 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
121 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
122 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
125 u_short semval; /* semaphore value */
126 pid_t sempid; /* pid of last operation */
127 u_short semncnt; /* # awaiting semval > cval */
128 u_short semzcnt; /* # awaiting semval = 0 */
132 * Undo structure (one per process)
135 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
136 struct proc *un_proc; /* owner of this structure */
137 short un_cnt; /* # of active entries */
139 short un_adjval; /* adjust on exit values */
140 short un_num; /* semaphore # */
141 int un_id; /* semid */
142 unsigned short un_seq;
143 } un_ent[1]; /* undo entries */
147 * Configuration parameters
150 #define SEMMNI 50 /* # of semaphore identifiers */
153 #define SEMMNS 340 /* # of semaphores in system */
156 #define SEMUME 50 /* max # of undo entries per process */
159 #define SEMMNU 150 /* # of undo structures in system */
162 /* shouldn't need tuning */
164 #define SEMMSL SEMMNS /* max # of semaphores per id */
167 #define SEMOPM 100 /* max # of operations per semop call */
170 #define SEMVMX 32767 /* semaphore maximum value */
171 #define SEMAEM 16384 /* adjust on exit max value */
174 * Due to the way semaphore memory is allocated, we have to ensure that
175 * SEMUSZ is properly aligned.
178 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long))
180 /* actual size of an undo structure */
181 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
184 * Macro to find a particular sem_undo vector
187 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
190 * semaphore info struct
192 struct seminfo seminfo = {
193 SEMMNI, /* # of semaphore identifiers */
194 SEMMNS, /* # of semaphores in system */
195 SEMMNU, /* # of undo structures in system */
196 SEMMSL, /* max # of semaphores per id */
197 SEMOPM, /* max # of operations per semop call */
198 SEMUME, /* max # of undo entries per process */
199 SEMUSZ, /* size in bytes of undo structure */
200 SEMVMX, /* semaphore maximum value */
201 SEMAEM /* adjust on exit max value */
204 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
205 "Number of semaphore identifiers");
206 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
207 "Maximum number of semaphores in the system");
208 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
209 "Maximum number of undo structures in the system");
210 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RWTUN, &seminfo.semmsl, 0,
211 "Max semaphores per id");
212 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
213 "Max operations per semop call");
214 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
215 "Max undo entries per process");
216 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
217 "Size in bytes of undo structure");
218 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RWTUN, &seminfo.semvmx, 0,
219 "Semaphore maximum value");
220 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &seminfo.semaem, 0,
221 "Adjust on exit max value");
222 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema,
223 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
224 NULL, 0, sysctl_sema, "", "Semaphore id pool");
226 static struct syscall_helper_data sem_syscalls[] = {
227 SYSCALL_INIT_HELPER(__semctl),
228 SYSCALL_INIT_HELPER(semget),
229 SYSCALL_INIT_HELPER(semop),
230 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
231 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
232 SYSCALL_INIT_HELPER(semsys),
233 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
238 #ifdef COMPAT_FREEBSD32
239 #include <compat/freebsd32/freebsd32.h>
240 #include <compat/freebsd32/freebsd32_ipc.h>
241 #include <compat/freebsd32/freebsd32_proto.h>
242 #include <compat/freebsd32/freebsd32_signal.h>
243 #include <compat/freebsd32/freebsd32_syscall.h>
244 #include <compat/freebsd32/freebsd32_util.h>
246 static struct syscall_helper_data sem32_syscalls[] = {
247 SYSCALL32_INIT_HELPER(freebsd32_semctl),
248 SYSCALL32_INIT_HELPER_COMPAT(semget),
249 SYSCALL32_INIT_HELPER_COMPAT(semop),
250 SYSCALL32_INIT_HELPER(freebsd32_semsys),
251 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
252 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
253 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
265 osd_method_t methods[PR_MAXMETHOD] = {
266 [PR_METHOD_CHECK] = sem_prison_check,
267 [PR_METHOD_SET] = sem_prison_set,
268 [PR_METHOD_GET] = sem_prison_get,
269 [PR_METHOD_REMOVE] = sem_prison_remove,
272 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
273 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
275 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
277 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
279 for (i = 0; i < seminfo.semmni; i++) {
280 sema[i].u.sem_base = 0;
281 sema[i].u.sem_perm.mode = 0;
282 sema[i].u.sem_perm.seq = 0;
284 mac_sysvsem_init(&sema[i]);
287 for (i = 0; i < seminfo.semmni; i++)
288 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
289 LIST_INIT(&semu_free_list);
290 for (i = 0; i < seminfo.semmnu; i++) {
291 struct sem_undo *suptr = SEMU(i);
292 suptr->un_proc = NULL;
293 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
295 LIST_INIT(&semu_list);
296 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
297 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
298 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
299 EVENTHANDLER_PRI_ANY);
301 /* Set current prisons according to their allow.sysvipc. */
302 sem_prison_slot = osd_jail_register(NULL, methods);
303 rsv = osd_reserve(sem_prison_slot);
304 prison_lock(&prison0);
305 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
306 prison_unlock(&prison0);
308 sx_slock(&allprison_lock);
309 TAILQ_FOREACH(pr, &allprison, pr_list) {
311 rsv = osd_reserve(sem_prison_slot);
313 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
314 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
321 osd_free_reserved(rsv);
322 sx_sunlock(&allprison_lock);
324 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
327 #ifdef COMPAT_FREEBSD32
328 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
344 #ifdef COMPAT_FREEBSD32
345 syscall32_helper_unregister(sem32_syscalls);
347 syscall_helper_unregister(sem_syscalls);
348 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
349 if (sem_prison_slot != 0)
350 osd_jail_deregister(sem_prison_slot);
352 for (i = 0; i < seminfo.semmni; i++)
353 mac_sysvsem_destroy(&sema[i]);
358 for (i = 0; i < seminfo.semmni; i++)
359 mtx_destroy(&sema_mtx[i]);
360 free(sema_mtx, M_SEM);
361 mtx_destroy(&sem_mtx);
362 mtx_destroy(&sem_undo_mtx);
367 sysvsem_modload(struct module *module, int cmd, void *arg)
389 static moduledata_t sysvsem_mod = {
395 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
396 MODULE_VERSION(sysvsem, 1);
399 * Allocate a new sem_undo structure for a process
400 * (returns ptr to structure or NULL if no more room)
403 static struct sem_undo *
404 semu_alloc(struct thread *td)
406 struct sem_undo *suptr;
408 SEMUNDO_LOCKASSERT(MA_OWNED);
409 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
411 LIST_REMOVE(suptr, un_next);
412 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
414 suptr->un_proc = td->td_proc;
419 semu_try_free(struct sem_undo *suptr)
422 SEMUNDO_LOCKASSERT(MA_OWNED);
424 if (suptr->un_cnt != 0)
426 LIST_REMOVE(suptr, un_next);
427 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
432 * Adjust a particular entry for a particular proc
436 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
437 int semseq, int semnum, int adjval)
439 struct proc *p = td->td_proc;
440 struct sem_undo *suptr;
444 SEMUNDO_LOCKASSERT(MA_OWNED);
445 /* Look for and remember the sem_undo if the caller doesn't provide
450 LIST_FOREACH(suptr, &semu_list, un_next) {
451 if (suptr->un_proc == p) {
459 suptr = semu_alloc(td);
467 * Look for the requested entry and adjust it (delete if adjval becomes
470 sunptr = &suptr->un_ent[0];
471 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
472 if (sunptr->un_id != semid || sunptr->un_num != semnum)
475 adjval += sunptr->un_adjval;
476 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
479 sunptr->un_adjval = adjval;
480 if (sunptr->un_adjval == 0) {
482 if (i < suptr->un_cnt)
484 suptr->un_ent[suptr->un_cnt];
485 if (suptr->un_cnt == 0)
486 semu_try_free(suptr);
491 /* Didn't find the right entry - create it */
494 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
496 if (suptr->un_cnt != seminfo.semume) {
497 sunptr = &suptr->un_ent[suptr->un_cnt];
499 sunptr->un_adjval = adjval;
500 sunptr->un_id = semid;
501 sunptr->un_num = semnum;
502 sunptr->un_seq = semseq;
509 semundo_clear(int semid, int semnum)
511 struct sem_undo *suptr, *suptr1;
515 SEMUNDO_LOCKASSERT(MA_OWNED);
516 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
517 sunptr = &suptr->un_ent[0];
518 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
519 if (sunptr->un_id != semid)
521 if (semnum == -1 || sunptr->un_num == semnum) {
523 if (i < suptr->un_cnt) {
525 suptr->un_ent[suptr->un_cnt];
528 semu_try_free(suptr);
537 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
540 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
541 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
542 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
546 sem_remove(int semidx, struct ucred *cred)
548 struct semid_kernel *semakptr;
551 KASSERT(semidx >= 0 && semidx < seminfo.semmni,
552 ("semidx out of bounds"));
553 semakptr = &sema[semidx];
554 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
555 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
556 semakptr->u.sem_perm.mode = 0;
557 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
558 crfree(semakptr->cred);
559 semakptr->cred = NULL;
561 semundo_clear(semidx, -1);
564 mac_sysvsem_cleanup(semakptr);
567 for (i = 0; i < seminfo.semmni; i++) {
568 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
569 sema[i].u.sem_base > semakptr->u.sem_base)
570 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
572 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
573 sem[i] = sem[i + semakptr->u.sem_nsems];
574 for (i = 0; i < seminfo.semmni; i++) {
575 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
576 sema[i].u.sem_base > semakptr->u.sem_base) {
577 sema[i].u.sem_base -= semakptr->u.sem_nsems;
578 mtx_unlock(&sema_mtx[i]);
581 semtot -= semakptr->u.sem_nsems;
584 static struct prison *
585 sem_find_prison(struct ucred *cred)
587 struct prison *pr, *rpr;
589 pr = cred->cr_prison;
591 rpr = osd_jail_get(pr, sem_prison_slot);
597 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
600 if (semakptr->cred == NULL ||
601 !(rpr == semakptr->cred->cr_prison ||
602 prison_ischild(rpr, semakptr->cred->cr_prison)))
608 * Note that the user-mode half of this passes a union, not a pointer.
610 #ifndef _SYS_SYSPROTO_H_
611 struct __semctl_args {
619 sys___semctl(struct thread *td, struct __semctl_args *uap)
621 struct semid_ds dsbuf;
622 union semun arg, semun;
633 error = copyin(uap->arg, &arg, sizeof(arg));
645 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
652 semun.array = arg.array;
659 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
667 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
672 td->td_retval[0] = rval;
677 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
678 union semun *arg, register_t *rval)
681 struct ucred *cred = td->td_ucred;
684 struct semid_ds *sbuf;
685 struct semid_kernel *semakptr;
686 struct mtx *sema_mtxp;
687 u_short usval, count;
690 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
691 semid, semnum, cmd, arg));
693 rpr = sem_find_prison(td->td_ucred);
702 * For this command we assume semid is an array index
703 * rather than an IPC id.
705 if (semid < 0 || semid >= seminfo.semmni)
707 semakptr = &sema[semid];
708 sema_mtxp = &sema_mtx[semid];
710 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
714 if ((error = sem_prison_cansee(rpr, semakptr)))
716 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
719 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
723 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
724 if (cred->cr_prison != semakptr->cred->cr_prison)
725 arg->buf->sem_perm.key = IPC_PRIVATE;
726 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
727 mtx_unlock(sema_mtxp);
731 semidx = IPCID_TO_IX(semid);
732 if (semidx < 0 || semidx >= seminfo.semmni)
735 semakptr = &sema[semidx];
736 sema_mtxp = &sema_mtx[semidx];
742 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
752 if ((error = semvalid(semid, rpr, semakptr)) != 0)
754 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
756 sem_remove(semidx, cred);
760 if ((error = semvalid(semid, rpr, semakptr)) != 0)
762 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
765 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
766 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
767 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
768 ~0777) | (sbuf->sem_perm.mode & 0777);
769 semakptr->u.sem_ctime = time_second;
773 if ((error = semvalid(semid, rpr, semakptr)) != 0)
775 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
777 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
778 if (cred->cr_prison != semakptr->cred->cr_prison)
779 arg->buf->sem_perm.key = IPC_PRIVATE;
783 if ((error = semvalid(semid, rpr, semakptr)) != 0)
785 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
787 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
791 *rval = semakptr->u.sem_base[semnum].semncnt;
795 if ((error = semvalid(semid, rpr, semakptr)) != 0)
797 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
799 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
803 *rval = semakptr->u.sem_base[semnum].sempid;
807 if ((error = semvalid(semid, rpr, semakptr)) != 0)
809 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
811 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
815 *rval = semakptr->u.sem_base[semnum].semval;
820 * Unfortunately, callers of this function don't know
821 * in advance how many semaphores are in this set.
822 * While we could just allocate the maximum size array
823 * and pass the actual size back to the caller, that
824 * won't work for SETALL since we can't copyin() more
825 * data than the user specified as we may return a
828 * Note that the number of semaphores in a set is
829 * fixed for the life of that set. The only way that
830 * the 'count' could change while are blocked in
831 * malloc() is if this semaphore set were destroyed
832 * and a new one created with the same index.
833 * However, semvalid() will catch that due to the
834 * sequence number unless exactly 0x8000 (or a
835 * multiple thereof) semaphore sets for the same index
836 * are created and destroyed while we are in malloc!
839 count = semakptr->u.sem_nsems;
840 mtx_unlock(sema_mtxp);
841 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
843 if ((error = semvalid(semid, rpr, semakptr)) != 0)
845 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
846 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
848 for (i = 0; i < semakptr->u.sem_nsems; i++)
849 array[i] = semakptr->u.sem_base[i].semval;
850 mtx_unlock(sema_mtxp);
851 error = copyout(array, arg->array, count * sizeof(*array));
856 if ((error = semvalid(semid, rpr, semakptr)) != 0)
858 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
860 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
864 *rval = semakptr->u.sem_base[semnum].semzcnt;
868 if ((error = semvalid(semid, rpr, semakptr)) != 0)
870 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
872 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
876 if (arg->val < 0 || arg->val > seminfo.semvmx) {
880 semakptr->u.sem_base[semnum].semval = arg->val;
882 semundo_clear(semidx, semnum);
889 * See comment on GETALL for why 'count' shouldn't change
890 * and why we require a userland buffer.
892 count = semakptr->u.sem_nsems;
893 mtx_unlock(sema_mtxp);
894 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
895 error = copyin(arg->array, array, count * sizeof(*array));
899 if ((error = semvalid(semid, rpr, semakptr)) != 0)
901 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
902 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
904 for (i = 0; i < semakptr->u.sem_nsems; i++) {
906 if (usval > seminfo.semvmx) {
910 semakptr->u.sem_base[i].semval = usval;
913 semundo_clear(semidx, -1);
924 mtx_unlock(sema_mtxp);
926 mtx_unlock(&sem_mtx);
932 #ifndef _SYS_SYSPROTO_H_
940 sys_semget(struct thread *td, struct semget_args *uap)
942 int semid, error = 0;
944 int nsems = uap->nsems;
945 int semflg = uap->semflg;
946 struct ucred *cred = td->td_ucred;
948 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
950 if (sem_find_prison(cred) == NULL)
954 if (key != IPC_PRIVATE) {
955 for (semid = 0; semid < seminfo.semmni; semid++) {
956 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
957 sema[semid].cred != NULL &&
958 sema[semid].cred->cr_prison == cred->cr_prison &&
959 sema[semid].u.sem_perm.key == key)
962 if (semid < seminfo.semmni) {
963 DPRINTF(("found public key\n"));
964 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
965 DPRINTF(("not exclusive\n"));
969 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
973 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
974 DPRINTF(("too small\n"));
979 error = mac_sysvsem_check_semget(cred, &sema[semid]);
987 DPRINTF(("need to allocate the semid_kernel\n"));
988 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
989 if (nsems <= 0 || nsems > seminfo.semmsl) {
990 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
995 if (nsems > seminfo.semmns - semtot) {
997 "not enough semaphores left (need %d, got %d)\n",
998 nsems, seminfo.semmns - semtot));
1002 for (semid = 0; semid < seminfo.semmni; semid++) {
1003 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1006 if (semid == seminfo.semmni) {
1007 DPRINTF(("no more semid_kernel's available\n"));
1013 PROC_LOCK(td->td_proc);
1014 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1015 PROC_UNLOCK(td->td_proc);
1022 DPRINTF(("semid %d is available\n", semid));
1023 mtx_lock(&sema_mtx[semid]);
1024 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1025 ("Lost semaphore %d", semid));
1026 sema[semid].u.sem_perm.key = key;
1027 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1028 sema[semid].u.sem_perm.uid = cred->cr_uid;
1029 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1030 sema[semid].u.sem_perm.gid = cred->cr_gid;
1031 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1032 sema[semid].cred = crhold(cred);
1033 sema[semid].u.sem_perm.seq =
1034 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1035 sema[semid].u.sem_nsems = nsems;
1036 sema[semid].u.sem_otime = 0;
1037 sema[semid].u.sem_ctime = time_second;
1038 sema[semid].u.sem_base = &sem[semtot];
1040 bzero(sema[semid].u.sem_base,
1041 sizeof(sema[semid].u.sem_base[0])*nsems);
1043 mac_sysvsem_create(cred, &sema[semid]);
1045 mtx_unlock(&sema_mtx[semid]);
1046 DPRINTF(("sembase = %p, next = %p\n",
1047 sema[semid].u.sem_base, &sem[semtot]));
1049 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1055 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1057 mtx_unlock(&sem_mtx);
1061 #ifndef _SYS_SYSPROTO_H_
1064 struct sembuf *sops;
1069 sys_semop(struct thread *td, struct semop_args *uap)
1071 #define SMALL_SOPS 8
1072 struct sembuf small_sops[SMALL_SOPS];
1073 int semid = uap->semid;
1074 size_t nsops = uap->nsops;
1076 struct sembuf *sops;
1077 struct semid_kernel *semakptr;
1078 struct sembuf *sopptr = NULL;
1079 struct sem *semptr = NULL;
1080 struct sem_undo *suptr;
1081 struct mtx *sema_mtxp;
1084 int do_wakeup, do_undos;
1090 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1092 rpr = sem_find_prison(td->td_ucred);
1096 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1098 if (semid < 0 || semid >= seminfo.semmni)
1101 /* Allocate memory for sem_ops */
1102 if (nsops <= SMALL_SOPS)
1104 else if (nsops > seminfo.semopm) {
1105 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1111 PROC_LOCK(td->td_proc);
1113 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1114 PROC_UNLOCK(td->td_proc);
1117 PROC_UNLOCK(td->td_proc);
1121 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1123 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1124 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1125 uap->sops, sops, nsops * sizeof(sops[0])));
1126 if (sops != small_sops)
1131 semakptr = &sema[semid];
1132 sema_mtxp = &sema_mtx[semid];
1133 mtx_lock(sema_mtxp);
1134 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1138 seq = semakptr->u.sem_perm.seq;
1139 if (seq != IPCID_TO_SEQ(uap->semid)) {
1143 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1146 * Initial pass through sops to see what permissions are needed.
1147 * Also perform any checks that don't need repeating on each
1148 * attempt to satisfy the request vector.
1150 j = 0; /* permission needed */
1152 for (i = 0; i < nsops; i++) {
1154 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1158 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1160 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1163 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1164 DPRINTF(("error = %d from ipaccess\n", error));
1168 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1174 * Loop trying to satisfy the vector of requests.
1175 * If we reach a point where we must wait, any requests already
1176 * performed are rolled back and we go to sleep until some other
1177 * process wakes us up. At this point, we start all over again.
1179 * This ensures that from the perspective of other tasks, a set
1180 * of requests is atomic (never partially satisfied).
1184 error = 0; /* error return if necessary */
1186 for (i = 0; i < nsops; i++) {
1188 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1191 "semop: semakptr=%p, sem_base=%p, "
1192 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1193 semakptr, semakptr->u.sem_base, semptr,
1194 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1195 (sopptr->sem_flg & IPC_NOWAIT) ?
1196 "nowait" : "wait"));
1198 if (sopptr->sem_op < 0) {
1199 if (semptr->semval + sopptr->sem_op < 0) {
1200 DPRINTF(("semop: can't do it now\n"));
1203 semptr->semval += sopptr->sem_op;
1204 if (semptr->semval == 0 &&
1205 semptr->semzcnt > 0)
1208 } else if (sopptr->sem_op == 0) {
1209 if (semptr->semval != 0) {
1210 DPRINTF(("semop: not zero now\n"));
1213 } else if (semptr->semval + sopptr->sem_op >
1218 if (semptr->semncnt > 0)
1220 semptr->semval += sopptr->sem_op;
1225 * Did we get through the entire vector?
1231 * No ... rollback anything that we've already done
1233 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1234 for (j = 0; j < i; j++)
1235 semakptr->u.sem_base[sops[j].sem_num].semval -=
1238 /* If we detected an error, return it */
1243 * If the request that we couldn't satisfy has the
1244 * NOWAIT flag set then return with EAGAIN.
1246 if (sopptr->sem_flg & IPC_NOWAIT) {
1251 if (sopptr->sem_op == 0)
1256 DPRINTF(("semop: good night!\n"));
1257 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1259 DPRINTF(("semop: good morning (error=%d)!\n", error));
1260 /* return code is checked below, after sem[nz]cnt-- */
1263 * Make sure that the semaphore still exists
1265 seq = semakptr->u.sem_perm.seq;
1266 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1267 seq != IPCID_TO_SEQ(uap->semid)) {
1273 * Renew the semaphore's pointer after wakeup since
1274 * during msleep sem_base may have been modified and semptr
1275 * is not valid any more
1277 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1280 * The semaphore is still alive. Readjust the count of
1281 * waiting processes.
1283 if (sopptr->sem_op == 0)
1289 * Is it really morning, or was our sleep interrupted?
1290 * (Delayed check of msleep() return code because we
1291 * need to decrement sem[nz]cnt either way.)
1297 DPRINTF(("semop: good morning!\n"));
1302 * Process any SEM_UNDO requests.
1307 for (i = 0; i < nsops; i++) {
1309 * We only need to deal with SEM_UNDO's for non-zero
1314 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1316 adjval = sops[i].sem_op;
1319 error = semundo_adjust(td, &suptr, semid, seq,
1320 sops[i].sem_num, -adjval);
1325 * Oh-Oh! We ran out of either sem_undo's or undo's.
1326 * Rollback the adjustments to this point and then
1327 * rollback the semaphore ups and down so we can return
1328 * with an error with all structures restored. We
1329 * rollback the undo's in the exact reverse order that
1330 * we applied them. This guarantees that we won't run
1331 * out of space as we roll things back out.
1333 for (j = 0; j < i; j++) {
1335 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1337 adjval = sops[k].sem_op;
1340 if (semundo_adjust(td, &suptr, semid, seq,
1341 sops[k].sem_num, adjval) != 0)
1342 panic("semop - can't undo undos");
1345 for (j = 0; j < nsops; j++)
1346 semakptr->u.sem_base[sops[j].sem_num].semval -=
1349 DPRINTF(("error = %d from semundo_adjust\n", error));
1352 } /* loop through the sops */
1354 } /* if (do_undos) */
1356 /* We're definitely done - set the sempid's and time */
1357 for (i = 0; i < nsops; i++) {
1359 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1360 semptr->sempid = td->td_proc->p_pid;
1362 semakptr->u.sem_otime = time_second;
1365 * Do a wakeup if any semaphore was up'd whilst something was
1369 DPRINTF(("semop: doing wakeup\n"));
1371 DPRINTF(("semop: back from wakeup\n"));
1373 DPRINTF(("semop: done\n"));
1374 td->td_retval[0] = 0;
1376 mtx_unlock(sema_mtxp);
1377 if (sops != small_sops)
1383 * Go through the undo structures for this process and apply the adjustments to
1387 semexit_myhook(void *arg, struct proc *p)
1389 struct sem_undo *suptr;
1390 struct semid_kernel *semakptr;
1391 struct mtx *sema_mtxp;
1392 int semid, semnum, adjval, ix;
1396 * Go through the chain of undo vectors looking for one
1397 * associated with this process.
1400 LIST_FOREACH(suptr, &semu_list, un_next) {
1401 if (suptr->un_proc == p)
1404 if (suptr == NULL) {
1408 LIST_REMOVE(suptr, un_next);
1410 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1414 * If there are any active undo elements then process them.
1416 if (suptr->un_cnt > 0) {
1418 for (ix = 0; ix < suptr->un_cnt; ix++) {
1419 semid = suptr->un_ent[ix].un_id;
1420 semnum = suptr->un_ent[ix].un_num;
1421 adjval = suptr->un_ent[ix].un_adjval;
1422 seq = suptr->un_ent[ix].un_seq;
1423 semakptr = &sema[semid];
1424 sema_mtxp = &sema_mtx[semid];
1426 mtx_lock(sema_mtxp);
1427 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1428 (semakptr->u.sem_perm.seq != seq)) {
1429 mtx_unlock(sema_mtxp);
1432 if (semnum >= semakptr->u.sem_nsems)
1433 panic("semexit - semnum out of range");
1436 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1437 suptr->un_proc, suptr->un_ent[ix].un_id,
1438 suptr->un_ent[ix].un_num,
1439 suptr->un_ent[ix].un_adjval,
1440 semakptr->u.sem_base[semnum].semval));
1442 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1444 semakptr->u.sem_base[semnum].semval = 0;
1446 semakptr->u.sem_base[semnum].semval += adjval;
1449 DPRINTF(("semexit: back from wakeup\n"));
1450 mtx_unlock(sema_mtxp);
1456 * Deallocate the undo vector.
1458 DPRINTF(("removing vector\n"));
1459 suptr->un_proc = NULL;
1461 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1466 sysctl_sema(SYSCTL_HANDLER_ARGS)
1468 struct prison *pr, *rpr;
1469 struct semid_kernel tsemak;
1472 pr = req->td->td_ucred->cr_prison;
1473 rpr = sem_find_prison(req->td->td_ucred);
1475 for (i = 0; i < seminfo.semmni; i++) {
1476 mtx_lock(&sema_mtx[i]);
1477 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1478 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1479 bzero(&tsemak, sizeof(tsemak));
1482 if (tsemak.cred->cr_prison != pr)
1483 tsemak.u.sem_perm.key = IPC_PRIVATE;
1485 mtx_unlock(&sema_mtx[i]);
1486 error = SYSCTL_OUT(req, &tsemak, sizeof(tsemak));
1494 sem_prison_check(void *obj, void *data)
1496 struct prison *pr = obj;
1497 struct prison *prpr;
1498 struct vfsoptlist *opts = data;
1502 * sysvsem is a jailsys integer.
1503 * It must be "disable" if the parent jail is disabled.
1505 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1506 if (error != ENOENT) {
1510 case JAIL_SYS_DISABLE:
1513 case JAIL_SYS_INHERIT:
1514 prison_lock(pr->pr_parent);
1515 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1516 prison_unlock(pr->pr_parent);
1529 sem_prison_set(void *obj, void *data)
1531 struct prison *pr = obj;
1532 struct prison *tpr, *orpr, *nrpr, *trpr;
1533 struct vfsoptlist *opts = data;
1538 * sysvsem controls which jail is the root of the associated sems (this
1539 * jail or same as the parent), or if the feature is available at all.
1541 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1542 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1544 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1547 if (jsys == JAIL_SYS_DISABLE) {
1549 orpr = osd_jail_get(pr, sem_prison_slot);
1551 osd_jail_del(pr, sem_prison_slot);
1555 sem_prison_cleanup(pr);
1556 /* Disable all child jails as well. */
1557 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1559 trpr = osd_jail_get(tpr, sem_prison_slot);
1561 osd_jail_del(tpr, sem_prison_slot);
1564 sem_prison_cleanup(tpr);
1571 } else if (jsys != -1) {
1572 if (jsys == JAIL_SYS_NEW)
1575 prison_lock(pr->pr_parent);
1576 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1577 prison_unlock(pr->pr_parent);
1579 rsv = osd_reserve(sem_prison_slot);
1581 orpr = osd_jail_get(pr, sem_prison_slot);
1583 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1586 osd_free_reserved(rsv);
1590 sem_prison_cleanup(pr);
1592 /* Change child jails matching the old root, */
1593 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1595 trpr = osd_jail_get(tpr,
1598 (void)osd_jail_set(tpr,
1599 sem_prison_slot, nrpr);
1602 sem_prison_cleanup(tpr);
1616 sem_prison_get(void *obj, void *data)
1618 struct prison *pr = obj;
1620 struct vfsoptlist *opts = data;
1623 /* Set sysvsem based on the jail's root prison. */
1625 rpr = osd_jail_get(pr, sem_prison_slot);
1627 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1628 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1629 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1630 if (error == ENOENT)
1636 sem_prison_remove(void *obj, void *data __unused)
1638 struct prison *pr = obj;
1642 rpr = osd_jail_get(pr, sem_prison_slot);
1645 sem_prison_cleanup(pr);
1650 sem_prison_cleanup(struct prison *pr)
1654 /* Remove any sems that belong to this jail. */
1656 for (i = 0; i < seminfo.semmni; i++) {
1657 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1658 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1659 mtx_lock(&sema_mtx[i]);
1660 sem_remove(i, NULL);
1661 mtx_unlock(&sema_mtx[i]);
1664 mtx_unlock(&sem_mtx);
1667 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1669 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1670 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1672 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1673 static sy_call_t *semcalls[] = {
1674 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1675 (sy_call_t *)sys_semop
1679 * Entry point for all SEM calls.
1684 /* XXX actually varargs. */
1685 struct semsys_args /* {
1695 if (uap->which < 0 || uap->which >= nitems(semcalls))
1697 error = (*semcalls[uap->which])(td, &uap->a2);
1702 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1705 #ifndef _SYS_SYSPROTO_H_
1706 struct freebsd7___semctl_args {
1710 union semun_old *arg;
1714 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1716 struct semid_ds_old dsold;
1717 struct semid_ds dsbuf;
1718 union semun_old arg;
1730 error = copyin(uap->arg, &arg, sizeof(arg));
1742 error = copyin(arg.buf, &dsold, sizeof(dsold));
1745 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1746 CP(dsold, dsbuf, sem_base);
1747 CP(dsold, dsbuf, sem_nsems);
1748 CP(dsold, dsbuf, sem_otime);
1749 CP(dsold, dsbuf, sem_ctime);
1754 semun.array = arg.array;
1757 semun.val = arg.val;
1761 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1769 bzero(&dsold, sizeof(dsold));
1770 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1771 CP(dsbuf, dsold, sem_base);
1772 CP(dsbuf, dsold, sem_nsems);
1773 CP(dsbuf, dsold, sem_otime);
1774 CP(dsbuf, dsold, sem_ctime);
1775 error = copyout(&dsold, arg.buf, sizeof(dsold));
1780 td->td_retval[0] = rval;
1784 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1786 #ifdef COMPAT_FREEBSD32
1789 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1792 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1793 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1794 switch (uap->which) {
1796 return (freebsd7_freebsd32_semctl(td,
1797 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1799 return (sys_semsys(td, (struct semsys_args *)uap));
1802 return (nosys(td, NULL));
1806 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1807 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1809 freebsd7_freebsd32_semctl(struct thread *td,
1810 struct freebsd7_freebsd32_semctl_args *uap)
1812 struct semid_ds32_old dsbuf32;
1813 struct semid_ds dsbuf;
1826 error = copyin(uap->arg, &arg, sizeof(arg));
1838 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1841 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1842 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1843 CP(dsbuf32, dsbuf, sem_nsems);
1844 CP(dsbuf32, dsbuf, sem_otime);
1845 CP(dsbuf32, dsbuf, sem_ctime);
1850 semun.array = PTRIN(arg.array);
1853 semun.val = arg.val;
1857 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1865 bzero(&dsbuf32, sizeof(dsbuf32));
1866 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1867 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1868 CP(dsbuf, dsbuf32, sem_nsems);
1869 CP(dsbuf, dsbuf32, sem_otime);
1870 CP(dsbuf, dsbuf32, sem_ctime);
1871 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1876 td->td_retval[0] = rval;
1882 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1884 struct semid_ds32 dsbuf32;
1885 struct semid_ds dsbuf;
1898 error = copyin(uap->arg, &arg, sizeof(arg));
1910 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1913 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1914 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1915 CP(dsbuf32, dsbuf, sem_nsems);
1916 CP(dsbuf32, dsbuf, sem_otime);
1917 CP(dsbuf32, dsbuf, sem_ctime);
1922 semun.array = PTRIN(arg.array);
1925 semun.val = arg.val;
1929 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1937 bzero(&dsbuf32, sizeof(dsbuf32));
1938 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1939 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1940 CP(dsbuf, dsbuf32, sem_nsems);
1941 CP(dsbuf, dsbuf32, sem_otime);
1942 CP(dsbuf, dsbuf32, sem_ctime);
1943 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1948 td->td_retval[0] = rval;
1952 #endif /* COMPAT_FREEBSD32 */