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, "",
225 "Array of struct semid_kernel for each potential semaphore");
227 static struct syscall_helper_data sem_syscalls[] = {
228 SYSCALL_INIT_HELPER(__semctl),
229 SYSCALL_INIT_HELPER(semget),
230 SYSCALL_INIT_HELPER(semop),
231 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
232 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
233 SYSCALL_INIT_HELPER(semsys),
234 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
239 #ifdef COMPAT_FREEBSD32
240 #include <compat/freebsd32/freebsd32.h>
241 #include <compat/freebsd32/freebsd32_ipc.h>
242 #include <compat/freebsd32/freebsd32_proto.h>
243 #include <compat/freebsd32/freebsd32_signal.h>
244 #include <compat/freebsd32/freebsd32_syscall.h>
245 #include <compat/freebsd32/freebsd32_util.h>
247 static struct syscall_helper_data sem32_syscalls[] = {
248 SYSCALL32_INIT_HELPER(freebsd32_semctl),
249 SYSCALL32_INIT_HELPER_COMPAT(semget),
250 SYSCALL32_INIT_HELPER_COMPAT(semop),
251 SYSCALL32_INIT_HELPER(freebsd32_semsys),
252 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
253 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
254 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
266 osd_method_t methods[PR_MAXMETHOD] = {
267 [PR_METHOD_CHECK] = sem_prison_check,
268 [PR_METHOD_SET] = sem_prison_set,
269 [PR_METHOD_GET] = sem_prison_get,
270 [PR_METHOD_REMOVE] = sem_prison_remove,
273 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
274 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
276 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
278 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
280 for (i = 0; i < seminfo.semmni; i++) {
281 sema[i].u.sem_base = 0;
282 sema[i].u.sem_perm.mode = 0;
283 sema[i].u.sem_perm.seq = 0;
285 mac_sysvsem_init(&sema[i]);
288 for (i = 0; i < seminfo.semmni; i++)
289 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
290 LIST_INIT(&semu_free_list);
291 for (i = 0; i < seminfo.semmnu; i++) {
292 struct sem_undo *suptr = SEMU(i);
293 suptr->un_proc = NULL;
294 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
296 LIST_INIT(&semu_list);
297 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
298 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
299 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
300 EVENTHANDLER_PRI_ANY);
302 /* Set current prisons according to their allow.sysvipc. */
303 sem_prison_slot = osd_jail_register(NULL, methods);
304 rsv = osd_reserve(sem_prison_slot);
305 prison_lock(&prison0);
306 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
307 prison_unlock(&prison0);
309 sx_slock(&allprison_lock);
310 TAILQ_FOREACH(pr, &allprison, pr_list) {
312 rsv = osd_reserve(sem_prison_slot);
314 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
315 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
322 osd_free_reserved(rsv);
323 sx_sunlock(&allprison_lock);
325 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
328 #ifdef COMPAT_FREEBSD32
329 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
345 #ifdef COMPAT_FREEBSD32
346 syscall32_helper_unregister(sem32_syscalls);
348 syscall_helper_unregister(sem_syscalls);
349 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
350 if (sem_prison_slot != 0)
351 osd_jail_deregister(sem_prison_slot);
353 for (i = 0; i < seminfo.semmni; i++)
354 mac_sysvsem_destroy(&sema[i]);
359 for (i = 0; i < seminfo.semmni; i++)
360 mtx_destroy(&sema_mtx[i]);
361 free(sema_mtx, M_SEM);
362 mtx_destroy(&sem_mtx);
363 mtx_destroy(&sem_undo_mtx);
368 sysvsem_modload(struct module *module, int cmd, void *arg)
390 static moduledata_t sysvsem_mod = {
396 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
397 MODULE_VERSION(sysvsem, 1);
400 * Allocate a new sem_undo structure for a process
401 * (returns ptr to structure or NULL if no more room)
404 static struct sem_undo *
405 semu_alloc(struct thread *td)
407 struct sem_undo *suptr;
409 SEMUNDO_LOCKASSERT(MA_OWNED);
410 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
412 LIST_REMOVE(suptr, un_next);
413 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
415 suptr->un_proc = td->td_proc;
420 semu_try_free(struct sem_undo *suptr)
423 SEMUNDO_LOCKASSERT(MA_OWNED);
425 if (suptr->un_cnt != 0)
427 LIST_REMOVE(suptr, un_next);
428 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
433 * Adjust a particular entry for a particular proc
437 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
438 int semseq, int semnum, int adjval)
440 struct proc *p = td->td_proc;
441 struct sem_undo *suptr;
445 SEMUNDO_LOCKASSERT(MA_OWNED);
446 /* Look for and remember the sem_undo if the caller doesn't provide
451 LIST_FOREACH(suptr, &semu_list, un_next) {
452 if (suptr->un_proc == p) {
460 suptr = semu_alloc(td);
468 * Look for the requested entry and adjust it (delete if adjval becomes
471 sunptr = &suptr->un_ent[0];
472 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
473 if (sunptr->un_id != semid || sunptr->un_num != semnum)
476 adjval += sunptr->un_adjval;
477 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
480 sunptr->un_adjval = adjval;
481 if (sunptr->un_adjval == 0) {
483 if (i < suptr->un_cnt)
485 suptr->un_ent[suptr->un_cnt];
486 if (suptr->un_cnt == 0)
487 semu_try_free(suptr);
492 /* Didn't find the right entry - create it */
495 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
497 if (suptr->un_cnt != seminfo.semume) {
498 sunptr = &suptr->un_ent[suptr->un_cnt];
500 sunptr->un_adjval = adjval;
501 sunptr->un_id = semid;
502 sunptr->un_num = semnum;
503 sunptr->un_seq = semseq;
510 semundo_clear(int semid, int semnum)
512 struct sem_undo *suptr, *suptr1;
516 SEMUNDO_LOCKASSERT(MA_OWNED);
517 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
518 sunptr = &suptr->un_ent[0];
519 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
520 if (sunptr->un_id != semid)
522 if (semnum == -1 || sunptr->un_num == semnum) {
524 if (i < suptr->un_cnt) {
526 suptr->un_ent[suptr->un_cnt];
529 semu_try_free(suptr);
538 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
541 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
542 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
543 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
547 sem_remove(int semidx, struct ucred *cred)
549 struct semid_kernel *semakptr;
552 KASSERT(semidx >= 0 && semidx < seminfo.semmni,
553 ("semidx out of bounds"));
554 semakptr = &sema[semidx];
555 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
556 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
557 semakptr->u.sem_perm.mode = 0;
558 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
559 crfree(semakptr->cred);
560 semakptr->cred = NULL;
562 semundo_clear(semidx, -1);
565 mac_sysvsem_cleanup(semakptr);
568 for (i = 0; i < seminfo.semmni; i++) {
569 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
570 sema[i].u.sem_base > semakptr->u.sem_base)
571 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
573 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
574 sem[i] = sem[i + semakptr->u.sem_nsems];
575 for (i = 0; i < seminfo.semmni; i++) {
576 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
577 sema[i].u.sem_base > semakptr->u.sem_base) {
578 sema[i].u.sem_base -= semakptr->u.sem_nsems;
579 mtx_unlock(&sema_mtx[i]);
582 semtot -= semakptr->u.sem_nsems;
585 static struct prison *
586 sem_find_prison(struct ucred *cred)
588 struct prison *pr, *rpr;
590 pr = cred->cr_prison;
592 rpr = osd_jail_get(pr, sem_prison_slot);
598 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
601 if (semakptr->cred == NULL ||
602 !(rpr == semakptr->cred->cr_prison ||
603 prison_ischild(rpr, semakptr->cred->cr_prison)))
609 * Note that the user-mode half of this passes a union, not a pointer.
611 #ifndef _SYS_SYSPROTO_H_
612 struct __semctl_args {
620 sys___semctl(struct thread *td, struct __semctl_args *uap)
622 struct semid_ds dsbuf;
623 union semun arg, semun;
634 error = copyin(uap->arg, &arg, sizeof(arg));
646 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
653 semun.array = arg.array;
660 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
668 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
673 td->td_retval[0] = rval;
678 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
679 union semun *arg, register_t *rval)
682 struct ucred *cred = td->td_ucred;
685 struct semid_ds *sbuf;
686 struct semid_kernel *semakptr;
687 struct mtx *sema_mtxp;
688 u_short usval, count;
691 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
692 semid, semnum, cmd, arg));
694 rpr = sem_find_prison(td->td_ucred);
703 * For this command we assume semid is an array index
704 * rather than an IPC id.
706 if (semid < 0 || semid >= seminfo.semmni)
708 semakptr = &sema[semid];
709 sema_mtxp = &sema_mtx[semid];
711 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
715 if ((error = sem_prison_cansee(rpr, semakptr)))
717 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
720 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
724 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
725 if (cred->cr_prison != semakptr->cred->cr_prison)
726 arg->buf->sem_perm.key = IPC_PRIVATE;
727 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
728 mtx_unlock(sema_mtxp);
732 semidx = IPCID_TO_IX(semid);
733 if (semidx < 0 || semidx >= seminfo.semmni)
736 semakptr = &sema[semidx];
737 sema_mtxp = &sema_mtx[semidx];
743 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
753 if ((error = semvalid(semid, rpr, semakptr)) != 0)
755 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
757 sem_remove(semidx, cred);
761 if ((error = semvalid(semid, rpr, semakptr)) != 0)
763 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
766 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
767 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
768 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
769 ~0777) | (sbuf->sem_perm.mode & 0777);
770 semakptr->u.sem_ctime = time_second;
774 if ((error = semvalid(semid, rpr, semakptr)) != 0)
776 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
778 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
779 if (cred->cr_prison != semakptr->cred->cr_prison)
780 arg->buf->sem_perm.key = IPC_PRIVATE;
784 if ((error = semvalid(semid, rpr, semakptr)) != 0)
786 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
788 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
792 *rval = semakptr->u.sem_base[semnum].semncnt;
796 if ((error = semvalid(semid, rpr, semakptr)) != 0)
798 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
800 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
804 *rval = semakptr->u.sem_base[semnum].sempid;
808 if ((error = semvalid(semid, rpr, semakptr)) != 0)
810 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
812 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
816 *rval = semakptr->u.sem_base[semnum].semval;
821 * Unfortunately, callers of this function don't know
822 * in advance how many semaphores are in this set.
823 * While we could just allocate the maximum size array
824 * and pass the actual size back to the caller, that
825 * won't work for SETALL since we can't copyin() more
826 * data than the user specified as we may return a
829 * Note that the number of semaphores in a set is
830 * fixed for the life of that set. The only way that
831 * the 'count' could change while are blocked in
832 * malloc() is if this semaphore set were destroyed
833 * and a new one created with the same index.
834 * However, semvalid() will catch that due to the
835 * sequence number unless exactly 0x8000 (or a
836 * multiple thereof) semaphore sets for the same index
837 * are created and destroyed while we are in malloc!
840 count = semakptr->u.sem_nsems;
841 mtx_unlock(sema_mtxp);
842 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
844 if ((error = semvalid(semid, rpr, semakptr)) != 0)
846 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
847 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
849 for (i = 0; i < semakptr->u.sem_nsems; i++)
850 array[i] = semakptr->u.sem_base[i].semval;
851 mtx_unlock(sema_mtxp);
852 error = copyout(array, arg->array, count * sizeof(*array));
857 if ((error = semvalid(semid, rpr, semakptr)) != 0)
859 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
861 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
865 *rval = semakptr->u.sem_base[semnum].semzcnt;
869 if ((error = semvalid(semid, rpr, semakptr)) != 0)
871 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
873 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
877 if (arg->val < 0 || arg->val > seminfo.semvmx) {
881 semakptr->u.sem_base[semnum].semval = arg->val;
883 semundo_clear(semidx, semnum);
890 * See comment on GETALL for why 'count' shouldn't change
891 * and why we require a userland buffer.
893 count = semakptr->u.sem_nsems;
894 mtx_unlock(sema_mtxp);
895 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
896 error = copyin(arg->array, array, count * sizeof(*array));
900 if ((error = semvalid(semid, rpr, semakptr)) != 0)
902 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
903 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
905 for (i = 0; i < semakptr->u.sem_nsems; i++) {
907 if (usval > seminfo.semvmx) {
911 semakptr->u.sem_base[i].semval = usval;
914 semundo_clear(semidx, -1);
925 mtx_unlock(sema_mtxp);
927 mtx_unlock(&sem_mtx);
933 #ifndef _SYS_SYSPROTO_H_
941 sys_semget(struct thread *td, struct semget_args *uap)
943 int semid, error = 0;
945 int nsems = uap->nsems;
946 int semflg = uap->semflg;
947 struct ucred *cred = td->td_ucred;
949 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
951 if (sem_find_prison(cred) == NULL)
955 if (key != IPC_PRIVATE) {
956 for (semid = 0; semid < seminfo.semmni; semid++) {
957 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
958 sema[semid].cred != NULL &&
959 sema[semid].cred->cr_prison == cred->cr_prison &&
960 sema[semid].u.sem_perm.key == key)
963 if (semid < seminfo.semmni) {
964 DPRINTF(("found public key\n"));
965 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
966 DPRINTF(("not exclusive\n"));
970 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
974 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
975 DPRINTF(("too small\n"));
980 error = mac_sysvsem_check_semget(cred, &sema[semid]);
988 DPRINTF(("need to allocate the semid_kernel\n"));
989 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
990 if (nsems <= 0 || nsems > seminfo.semmsl) {
991 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
996 if (nsems > seminfo.semmns - semtot) {
998 "not enough semaphores left (need %d, got %d)\n",
999 nsems, seminfo.semmns - semtot));
1003 for (semid = 0; semid < seminfo.semmni; semid++) {
1004 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1007 if (semid == seminfo.semmni) {
1008 DPRINTF(("no more semid_kernel's available\n"));
1014 PROC_LOCK(td->td_proc);
1015 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1016 PROC_UNLOCK(td->td_proc);
1023 DPRINTF(("semid %d is available\n", semid));
1024 mtx_lock(&sema_mtx[semid]);
1025 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1026 ("Lost semaphore %d", semid));
1027 sema[semid].u.sem_perm.key = key;
1028 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1029 sema[semid].u.sem_perm.uid = cred->cr_uid;
1030 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1031 sema[semid].u.sem_perm.gid = cred->cr_gid;
1032 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1033 sema[semid].cred = crhold(cred);
1034 sema[semid].u.sem_perm.seq =
1035 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1036 sema[semid].u.sem_nsems = nsems;
1037 sema[semid].u.sem_otime = 0;
1038 sema[semid].u.sem_ctime = time_second;
1039 sema[semid].u.sem_base = &sem[semtot];
1041 bzero(sema[semid].u.sem_base,
1042 sizeof(sema[semid].u.sem_base[0])*nsems);
1044 mac_sysvsem_create(cred, &sema[semid]);
1046 mtx_unlock(&sema_mtx[semid]);
1047 DPRINTF(("sembase = %p, next = %p\n",
1048 sema[semid].u.sem_base, &sem[semtot]));
1050 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1056 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1058 mtx_unlock(&sem_mtx);
1062 #ifndef _SYS_SYSPROTO_H_
1065 struct sembuf *sops;
1070 sys_semop(struct thread *td, struct semop_args *uap)
1072 #define SMALL_SOPS 8
1073 struct sembuf small_sops[SMALL_SOPS];
1074 int semid = uap->semid;
1075 size_t nsops = uap->nsops;
1077 struct sembuf *sops;
1078 struct semid_kernel *semakptr;
1079 struct sembuf *sopptr = NULL;
1080 struct sem *semptr = NULL;
1081 struct sem_undo *suptr;
1082 struct mtx *sema_mtxp;
1085 int do_wakeup, do_undos;
1091 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1093 rpr = sem_find_prison(td->td_ucred);
1097 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1099 if (semid < 0 || semid >= seminfo.semmni)
1102 /* Allocate memory for sem_ops */
1103 if (nsops <= SMALL_SOPS)
1105 else if (nsops > seminfo.semopm) {
1106 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1112 PROC_LOCK(td->td_proc);
1114 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1115 PROC_UNLOCK(td->td_proc);
1118 PROC_UNLOCK(td->td_proc);
1122 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1124 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1125 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1126 uap->sops, sops, nsops * sizeof(sops[0])));
1127 if (sops != small_sops)
1132 semakptr = &sema[semid];
1133 sema_mtxp = &sema_mtx[semid];
1134 mtx_lock(sema_mtxp);
1135 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1139 seq = semakptr->u.sem_perm.seq;
1140 if (seq != IPCID_TO_SEQ(uap->semid)) {
1144 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1147 * Initial pass through sops to see what permissions are needed.
1148 * Also perform any checks that don't need repeating on each
1149 * attempt to satisfy the request vector.
1151 j = 0; /* permission needed */
1153 for (i = 0; i < nsops; i++) {
1155 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1159 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1161 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1164 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1165 DPRINTF(("error = %d from ipaccess\n", error));
1169 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1175 * Loop trying to satisfy the vector of requests.
1176 * If we reach a point where we must wait, any requests already
1177 * performed are rolled back and we go to sleep until some other
1178 * process wakes us up. At this point, we start all over again.
1180 * This ensures that from the perspective of other tasks, a set
1181 * of requests is atomic (never partially satisfied).
1185 error = 0; /* error return if necessary */
1187 for (i = 0; i < nsops; i++) {
1189 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1192 "semop: semakptr=%p, sem_base=%p, "
1193 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1194 semakptr, semakptr->u.sem_base, semptr,
1195 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1196 (sopptr->sem_flg & IPC_NOWAIT) ?
1197 "nowait" : "wait"));
1199 if (sopptr->sem_op < 0) {
1200 if (semptr->semval + sopptr->sem_op < 0) {
1201 DPRINTF(("semop: can't do it now\n"));
1204 semptr->semval += sopptr->sem_op;
1205 if (semptr->semval == 0 &&
1206 semptr->semzcnt > 0)
1209 } else if (sopptr->sem_op == 0) {
1210 if (semptr->semval != 0) {
1211 DPRINTF(("semop: not zero now\n"));
1214 } else if (semptr->semval + sopptr->sem_op >
1219 if (semptr->semncnt > 0)
1221 semptr->semval += sopptr->sem_op;
1226 * Did we get through the entire vector?
1232 * No ... rollback anything that we've already done
1234 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1235 for (j = 0; j < i; j++)
1236 semakptr->u.sem_base[sops[j].sem_num].semval -=
1239 /* If we detected an error, return it */
1244 * If the request that we couldn't satisfy has the
1245 * NOWAIT flag set then return with EAGAIN.
1247 if (sopptr->sem_flg & IPC_NOWAIT) {
1252 if (sopptr->sem_op == 0)
1257 DPRINTF(("semop: good night!\n"));
1258 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1260 DPRINTF(("semop: good morning (error=%d)!\n", error));
1261 /* return code is checked below, after sem[nz]cnt-- */
1264 * Make sure that the semaphore still exists
1266 seq = semakptr->u.sem_perm.seq;
1267 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1268 seq != IPCID_TO_SEQ(uap->semid)) {
1274 * Renew the semaphore's pointer after wakeup since
1275 * during msleep sem_base may have been modified and semptr
1276 * is not valid any more
1278 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1281 * The semaphore is still alive. Readjust the count of
1282 * waiting processes.
1284 if (sopptr->sem_op == 0)
1290 * Is it really morning, or was our sleep interrupted?
1291 * (Delayed check of msleep() return code because we
1292 * need to decrement sem[nz]cnt either way.)
1298 DPRINTF(("semop: good morning!\n"));
1303 * Process any SEM_UNDO requests.
1308 for (i = 0; i < nsops; i++) {
1310 * We only need to deal with SEM_UNDO's for non-zero
1315 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1317 adjval = sops[i].sem_op;
1320 error = semundo_adjust(td, &suptr, semid, seq,
1321 sops[i].sem_num, -adjval);
1326 * Oh-Oh! We ran out of either sem_undo's or undo's.
1327 * Rollback the adjustments to this point and then
1328 * rollback the semaphore ups and down so we can return
1329 * with an error with all structures restored. We
1330 * rollback the undo's in the exact reverse order that
1331 * we applied them. This guarantees that we won't run
1332 * out of space as we roll things back out.
1334 for (j = 0; j < i; j++) {
1336 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1338 adjval = sops[k].sem_op;
1341 if (semundo_adjust(td, &suptr, semid, seq,
1342 sops[k].sem_num, adjval) != 0)
1343 panic("semop - can't undo undos");
1346 for (j = 0; j < nsops; j++)
1347 semakptr->u.sem_base[sops[j].sem_num].semval -=
1350 DPRINTF(("error = %d from semundo_adjust\n", error));
1353 } /* loop through the sops */
1355 } /* if (do_undos) */
1357 /* We're definitely done - set the sempid's and time */
1358 for (i = 0; i < nsops; i++) {
1360 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1361 semptr->sempid = td->td_proc->p_pid;
1363 semakptr->u.sem_otime = time_second;
1366 * Do a wakeup if any semaphore was up'd whilst something was
1370 DPRINTF(("semop: doing wakeup\n"));
1372 DPRINTF(("semop: back from wakeup\n"));
1374 DPRINTF(("semop: done\n"));
1375 td->td_retval[0] = 0;
1377 mtx_unlock(sema_mtxp);
1378 if (sops != small_sops)
1384 * Go through the undo structures for this process and apply the adjustments to
1388 semexit_myhook(void *arg, struct proc *p)
1390 struct sem_undo *suptr;
1391 struct semid_kernel *semakptr;
1392 struct mtx *sema_mtxp;
1393 int semid, semnum, adjval, ix;
1397 * Go through the chain of undo vectors looking for one
1398 * associated with this process.
1401 LIST_FOREACH(suptr, &semu_list, un_next) {
1402 if (suptr->un_proc == p)
1405 if (suptr == NULL) {
1409 LIST_REMOVE(suptr, un_next);
1411 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1415 * If there are any active undo elements then process them.
1417 if (suptr->un_cnt > 0) {
1419 for (ix = 0; ix < suptr->un_cnt; ix++) {
1420 semid = suptr->un_ent[ix].un_id;
1421 semnum = suptr->un_ent[ix].un_num;
1422 adjval = suptr->un_ent[ix].un_adjval;
1423 seq = suptr->un_ent[ix].un_seq;
1424 semakptr = &sema[semid];
1425 sema_mtxp = &sema_mtx[semid];
1427 mtx_lock(sema_mtxp);
1428 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1429 (semakptr->u.sem_perm.seq != seq)) {
1430 mtx_unlock(sema_mtxp);
1433 if (semnum >= semakptr->u.sem_nsems)
1434 panic("semexit - semnum out of range");
1437 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1438 suptr->un_proc, suptr->un_ent[ix].un_id,
1439 suptr->un_ent[ix].un_num,
1440 suptr->un_ent[ix].un_adjval,
1441 semakptr->u.sem_base[semnum].semval));
1443 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1445 semakptr->u.sem_base[semnum].semval = 0;
1447 semakptr->u.sem_base[semnum].semval += adjval;
1450 DPRINTF(("semexit: back from wakeup\n"));
1451 mtx_unlock(sema_mtxp);
1457 * Deallocate the undo vector.
1459 DPRINTF(("removing vector\n"));
1460 suptr->un_proc = NULL;
1462 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1467 sysctl_sema(SYSCTL_HANDLER_ARGS)
1469 struct prison *pr, *rpr;
1470 struct semid_kernel tsemak;
1471 #ifdef COMPAT_FREEBSD32
1472 struct semid_kernel32 tsemak32;
1478 pr = req->td->td_ucred->cr_prison;
1479 rpr = sem_find_prison(req->td->td_ucred);
1481 for (i = 0; i < seminfo.semmni; i++) {
1482 mtx_lock(&sema_mtx[i]);
1483 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1484 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1485 bzero(&tsemak, sizeof(tsemak));
1488 if (tsemak.cred->cr_prison != pr)
1489 tsemak.u.sem_perm.key = IPC_PRIVATE;
1491 mtx_unlock(&sema_mtx[i]);
1492 #ifdef COMPAT_FREEBSD32
1493 if (SV_CURPROC_FLAG(SV_ILP32)) {
1494 bzero(&tsemak32, sizeof(tsemak32));
1495 freebsd32_ipcperm_out(&tsemak.u.sem_perm,
1496 &tsemak32.u.sem_perm);
1497 /* Don't copy u.sem_base */
1498 CP(tsemak, tsemak32, u.sem_nsems);
1499 CP(tsemak, tsemak32, u.sem_otime);
1500 CP(tsemak, tsemak32, u.sem_ctime);
1501 /* Don't copy label or cred */
1502 outaddr = &tsemak32;
1503 outsize = sizeof(tsemak32);
1507 tsemak.u.sem_base = NULL;
1508 tsemak.label = NULL;
1511 outsize = sizeof(tsemak);
1513 error = SYSCTL_OUT(req, outaddr, outsize);
1521 sem_prison_check(void *obj, void *data)
1523 struct prison *pr = obj;
1524 struct prison *prpr;
1525 struct vfsoptlist *opts = data;
1529 * sysvsem is a jailsys integer.
1530 * It must be "disable" if the parent jail is disabled.
1532 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1533 if (error != ENOENT) {
1537 case JAIL_SYS_DISABLE:
1540 case JAIL_SYS_INHERIT:
1541 prison_lock(pr->pr_parent);
1542 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1543 prison_unlock(pr->pr_parent);
1556 sem_prison_set(void *obj, void *data)
1558 struct prison *pr = obj;
1559 struct prison *tpr, *orpr, *nrpr, *trpr;
1560 struct vfsoptlist *opts = data;
1565 * sysvsem controls which jail is the root of the associated sems (this
1566 * jail or same as the parent), or if the feature is available at all.
1568 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1569 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1571 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1574 if (jsys == JAIL_SYS_DISABLE) {
1576 orpr = osd_jail_get(pr, sem_prison_slot);
1578 osd_jail_del(pr, sem_prison_slot);
1582 sem_prison_cleanup(pr);
1583 /* Disable all child jails as well. */
1584 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1586 trpr = osd_jail_get(tpr, sem_prison_slot);
1588 osd_jail_del(tpr, sem_prison_slot);
1591 sem_prison_cleanup(tpr);
1598 } else if (jsys != -1) {
1599 if (jsys == JAIL_SYS_NEW)
1602 prison_lock(pr->pr_parent);
1603 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1604 prison_unlock(pr->pr_parent);
1606 rsv = osd_reserve(sem_prison_slot);
1608 orpr = osd_jail_get(pr, sem_prison_slot);
1610 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1613 osd_free_reserved(rsv);
1617 sem_prison_cleanup(pr);
1619 /* Change child jails matching the old root, */
1620 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1622 trpr = osd_jail_get(tpr,
1625 (void)osd_jail_set(tpr,
1626 sem_prison_slot, nrpr);
1629 sem_prison_cleanup(tpr);
1643 sem_prison_get(void *obj, void *data)
1645 struct prison *pr = obj;
1647 struct vfsoptlist *opts = data;
1650 /* Set sysvsem based on the jail's root prison. */
1652 rpr = osd_jail_get(pr, sem_prison_slot);
1654 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1655 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1656 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1657 if (error == ENOENT)
1663 sem_prison_remove(void *obj, void *data __unused)
1665 struct prison *pr = obj;
1669 rpr = osd_jail_get(pr, sem_prison_slot);
1672 sem_prison_cleanup(pr);
1677 sem_prison_cleanup(struct prison *pr)
1681 /* Remove any sems that belong to this jail. */
1683 for (i = 0; i < seminfo.semmni; i++) {
1684 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1685 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1686 mtx_lock(&sema_mtx[i]);
1687 sem_remove(i, NULL);
1688 mtx_unlock(&sema_mtx[i]);
1691 mtx_unlock(&sem_mtx);
1694 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1696 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1697 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1699 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1700 static sy_call_t *semcalls[] = {
1701 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1702 (sy_call_t *)sys_semop
1706 * Entry point for all SEM calls.
1711 /* XXX actually varargs. */
1712 struct semsys_args /* {
1722 if (uap->which < 0 || uap->which >= nitems(semcalls))
1724 error = (*semcalls[uap->which])(td, &uap->a2);
1729 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1732 #ifndef _SYS_SYSPROTO_H_
1733 struct freebsd7___semctl_args {
1737 union semun_old *arg;
1741 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1743 struct semid_ds_old dsold;
1744 struct semid_ds dsbuf;
1745 union semun_old arg;
1757 error = copyin(uap->arg, &arg, sizeof(arg));
1769 error = copyin(arg.buf, &dsold, sizeof(dsold));
1772 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1773 CP(dsold, dsbuf, sem_base);
1774 CP(dsold, dsbuf, sem_nsems);
1775 CP(dsold, dsbuf, sem_otime);
1776 CP(dsold, dsbuf, sem_ctime);
1781 semun.array = arg.array;
1784 semun.val = arg.val;
1788 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1796 bzero(&dsold, sizeof(dsold));
1797 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1798 CP(dsbuf, dsold, sem_base);
1799 CP(dsbuf, dsold, sem_nsems);
1800 CP(dsbuf, dsold, sem_otime);
1801 CP(dsbuf, dsold, sem_ctime);
1802 error = copyout(&dsold, arg.buf, sizeof(dsold));
1807 td->td_retval[0] = rval;
1811 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1813 #ifdef COMPAT_FREEBSD32
1816 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1819 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1820 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1821 switch (uap->which) {
1823 return (freebsd7_freebsd32_semctl(td,
1824 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1826 return (sys_semsys(td, (struct semsys_args *)uap));
1829 return (nosys(td, NULL));
1833 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1834 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1836 freebsd7_freebsd32_semctl(struct thread *td,
1837 struct freebsd7_freebsd32_semctl_args *uap)
1839 struct semid_ds32_old dsbuf32;
1840 struct semid_ds dsbuf;
1853 error = copyin(uap->arg, &arg, sizeof(arg));
1865 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1868 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1869 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1870 CP(dsbuf32, dsbuf, sem_nsems);
1871 CP(dsbuf32, dsbuf, sem_otime);
1872 CP(dsbuf32, dsbuf, sem_ctime);
1877 semun.array = PTRIN(arg.array);
1880 semun.val = arg.val;
1884 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1892 bzero(&dsbuf32, sizeof(dsbuf32));
1893 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1894 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1895 CP(dsbuf, dsbuf32, sem_nsems);
1896 CP(dsbuf, dsbuf32, sem_otime);
1897 CP(dsbuf, dsbuf32, sem_ctime);
1898 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1903 td->td_retval[0] = rval;
1909 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1911 struct semid_ds32 dsbuf32;
1912 struct semid_ds dsbuf;
1925 error = copyin(uap->arg, &arg, sizeof(arg));
1937 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1940 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1941 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1942 CP(dsbuf32, dsbuf, sem_nsems);
1943 CP(dsbuf32, dsbuf, sem_otime);
1944 CP(dsbuf32, dsbuf, sem_ctime);
1949 semun.array = PTRIN(arg.array);
1952 semun.val = arg.val;
1956 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1964 bzero(&dsbuf32, sizeof(dsbuf32));
1965 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1966 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1967 CP(dsbuf, dsbuf32, sem_nsems);
1968 CP(dsbuf, dsbuf32, sem_otime);
1969 CP(dsbuf, dsbuf32, sem_ctime);
1970 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1975 td->td_retval[0] = rval;
1979 #endif /* COMPAT_FREEBSD32 */