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) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
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_RW, &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_RW, &seminfo.semvmx, 0,
219 "Semaphore maximum value");
220 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &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 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
274 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
275 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
276 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
277 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
278 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
279 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
280 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
281 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
283 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
284 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
286 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
288 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
290 for (i = 0; i < seminfo.semmni; i++) {
291 sema[i].u.sem_base = 0;
292 sema[i].u.sem_perm.mode = 0;
293 sema[i].u.sem_perm.seq = 0;
295 mac_sysvsem_init(&sema[i]);
298 for (i = 0; i < seminfo.semmni; i++)
299 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
300 LIST_INIT(&semu_free_list);
301 for (i = 0; i < seminfo.semmnu; i++) {
302 struct sem_undo *suptr = SEMU(i);
303 suptr->un_proc = NULL;
304 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
306 LIST_INIT(&semu_list);
307 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
308 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
309 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
310 EVENTHANDLER_PRI_ANY);
312 /* Set current prisons according to their allow.sysvipc. */
313 sem_prison_slot = osd_jail_register(NULL, methods);
314 rsv = osd_reserve(sem_prison_slot);
315 prison_lock(&prison0);
316 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
317 prison_unlock(&prison0);
319 sx_slock(&allprison_lock);
320 TAILQ_FOREACH(pr, &allprison, pr_list) {
322 rsv = osd_reserve(sem_prison_slot);
324 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
325 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
332 osd_free_reserved(rsv);
333 sx_sunlock(&allprison_lock);
335 error = syscall_helper_register(sem_syscalls);
338 #ifdef COMPAT_FREEBSD32
339 error = syscall32_helper_register(sem32_syscalls);
355 #ifdef COMPAT_FREEBSD32
356 syscall32_helper_unregister(sem32_syscalls);
358 syscall_helper_unregister(sem_syscalls);
359 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
360 if (sem_prison_slot != 0)
361 osd_jail_deregister(sem_prison_slot);
363 for (i = 0; i < seminfo.semmni; i++)
364 mac_sysvsem_destroy(&sema[i]);
369 for (i = 0; i < seminfo.semmni; i++)
370 mtx_destroy(&sema_mtx[i]);
371 free(sema_mtx, M_SEM);
372 mtx_destroy(&sem_mtx);
373 mtx_destroy(&sem_undo_mtx);
378 sysvsem_modload(struct module *module, int cmd, void *arg)
400 static moduledata_t sysvsem_mod = {
406 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
407 MODULE_VERSION(sysvsem, 1);
410 * Allocate a new sem_undo structure for a process
411 * (returns ptr to structure or NULL if no more room)
414 static struct sem_undo *
415 semu_alloc(struct thread *td)
417 struct sem_undo *suptr;
419 SEMUNDO_LOCKASSERT(MA_OWNED);
420 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
422 LIST_REMOVE(suptr, un_next);
423 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
425 suptr->un_proc = td->td_proc;
430 semu_try_free(struct sem_undo *suptr)
433 SEMUNDO_LOCKASSERT(MA_OWNED);
435 if (suptr->un_cnt != 0)
437 LIST_REMOVE(suptr, un_next);
438 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
443 * Adjust a particular entry for a particular proc
447 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
448 int semseq, int semnum, int adjval)
450 struct proc *p = td->td_proc;
451 struct sem_undo *suptr;
455 SEMUNDO_LOCKASSERT(MA_OWNED);
456 /* Look for and remember the sem_undo if the caller doesn't provide
461 LIST_FOREACH(suptr, &semu_list, un_next) {
462 if (suptr->un_proc == p) {
470 suptr = semu_alloc(td);
478 * Look for the requested entry and adjust it (delete if adjval becomes
481 sunptr = &suptr->un_ent[0];
482 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
483 if (sunptr->un_id != semid || sunptr->un_num != semnum)
486 adjval += sunptr->un_adjval;
487 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
490 sunptr->un_adjval = adjval;
491 if (sunptr->un_adjval == 0) {
493 if (i < suptr->un_cnt)
495 suptr->un_ent[suptr->un_cnt];
496 if (suptr->un_cnt == 0)
497 semu_try_free(suptr);
502 /* Didn't find the right entry - create it */
505 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
507 if (suptr->un_cnt != seminfo.semume) {
508 sunptr = &suptr->un_ent[suptr->un_cnt];
510 sunptr->un_adjval = adjval;
511 sunptr->un_id = semid;
512 sunptr->un_num = semnum;
513 sunptr->un_seq = semseq;
520 semundo_clear(int semid, int semnum)
522 struct sem_undo *suptr, *suptr1;
526 SEMUNDO_LOCKASSERT(MA_OWNED);
527 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
528 sunptr = &suptr->un_ent[0];
529 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
530 if (sunptr->un_id != semid)
532 if (semnum == -1 || sunptr->un_num == semnum) {
534 if (i < suptr->un_cnt) {
536 suptr->un_ent[suptr->un_cnt];
539 semu_try_free(suptr);
548 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
551 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
552 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
553 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
557 sem_remove(int semidx, struct ucred *cred)
559 struct semid_kernel *semakptr;
562 KASSERT(semidx >= 0 && semidx < seminfo.semmni,
563 ("semidx out of bounds"));
564 semakptr = &sema[semidx];
565 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
566 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
567 semakptr->u.sem_perm.mode = 0;
568 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
569 crfree(semakptr->cred);
570 semakptr->cred = NULL;
572 semundo_clear(semidx, -1);
575 mac_sysvsem_cleanup(semakptr);
578 for (i = 0; i < seminfo.semmni; i++) {
579 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
580 sema[i].u.sem_base > semakptr->u.sem_base)
581 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
583 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
584 sem[i] = sem[i + semakptr->u.sem_nsems];
585 for (i = 0; i < seminfo.semmni; i++) {
586 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
587 sema[i].u.sem_base > semakptr->u.sem_base) {
588 sema[i].u.sem_base -= semakptr->u.sem_nsems;
589 mtx_unlock(&sema_mtx[i]);
592 semtot -= semakptr->u.sem_nsems;
595 static struct prison *
596 sem_find_prison(struct ucred *cred)
598 struct prison *pr, *rpr;
600 pr = cred->cr_prison;
602 rpr = osd_jail_get(pr, sem_prison_slot);
608 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
611 if (semakptr->cred == NULL ||
612 !(rpr == semakptr->cred->cr_prison ||
613 prison_ischild(rpr, semakptr->cred->cr_prison)))
619 * Note that the user-mode half of this passes a union, not a pointer.
621 #ifndef _SYS_SYSPROTO_H_
622 struct __semctl_args {
630 sys___semctl(struct thread *td, struct __semctl_args *uap)
632 struct semid_ds dsbuf;
633 union semun arg, semun;
644 error = copyin(uap->arg, &arg, sizeof(arg));
656 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
663 semun.array = arg.array;
670 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
678 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
683 td->td_retval[0] = rval;
688 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
689 union semun *arg, register_t *rval)
692 struct ucred *cred = td->td_ucred;
695 struct semid_ds *sbuf;
696 struct semid_kernel *semakptr;
697 struct mtx *sema_mtxp;
698 u_short usval, count;
701 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
702 semid, semnum, cmd, arg));
704 rpr = sem_find_prison(td->td_ucred);
713 * For this command we assume semid is an array index
714 * rather than an IPC id.
716 if (semid < 0 || semid >= seminfo.semmni)
718 semakptr = &sema[semid];
719 sema_mtxp = &sema_mtx[semid];
721 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
725 if ((error = sem_prison_cansee(rpr, semakptr)))
727 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
730 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
734 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
735 if (cred->cr_prison != semakptr->cred->cr_prison)
736 arg->buf->sem_perm.key = IPC_PRIVATE;
737 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
738 mtx_unlock(sema_mtxp);
742 semidx = IPCID_TO_IX(semid);
743 if (semidx < 0 || semidx >= seminfo.semmni)
746 semakptr = &sema[semidx];
747 sema_mtxp = &sema_mtx[semidx];
753 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
763 if ((error = semvalid(semid, rpr, semakptr)) != 0)
765 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
767 sem_remove(semidx, cred);
771 if ((error = semvalid(semid, rpr, semakptr)) != 0)
773 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
776 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
777 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
778 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
779 ~0777) | (sbuf->sem_perm.mode & 0777);
780 semakptr->u.sem_ctime = time_second;
784 if ((error = semvalid(semid, rpr, semakptr)) != 0)
786 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
788 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
789 if (cred->cr_prison != semakptr->cred->cr_prison)
790 arg->buf->sem_perm.key = IPC_PRIVATE;
794 if ((error = semvalid(semid, rpr, semakptr)) != 0)
796 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
798 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
802 *rval = semakptr->u.sem_base[semnum].semncnt;
806 if ((error = semvalid(semid, rpr, semakptr)) != 0)
808 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
810 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
814 *rval = semakptr->u.sem_base[semnum].sempid;
818 if ((error = semvalid(semid, rpr, semakptr)) != 0)
820 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
822 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
826 *rval = semakptr->u.sem_base[semnum].semval;
831 * Unfortunately, callers of this function don't know
832 * in advance how many semaphores are in this set.
833 * While we could just allocate the maximum size array
834 * and pass the actual size back to the caller, that
835 * won't work for SETALL since we can't copyin() more
836 * data than the user specified as we may return a
839 * Note that the number of semaphores in a set is
840 * fixed for the life of that set. The only way that
841 * the 'count' could change while are blocked in
842 * malloc() is if this semaphore set were destroyed
843 * and a new one created with the same index.
844 * However, semvalid() will catch that due to the
845 * sequence number unless exactly 0x8000 (or a
846 * multiple thereof) semaphore sets for the same index
847 * are created and destroyed while we are in malloc!
850 count = semakptr->u.sem_nsems;
851 mtx_unlock(sema_mtxp);
852 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
854 if ((error = semvalid(semid, rpr, semakptr)) != 0)
856 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
857 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
859 for (i = 0; i < semakptr->u.sem_nsems; i++)
860 array[i] = semakptr->u.sem_base[i].semval;
861 mtx_unlock(sema_mtxp);
862 error = copyout(array, arg->array, count * sizeof(*array));
867 if ((error = semvalid(semid, rpr, semakptr)) != 0)
869 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
871 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
875 *rval = semakptr->u.sem_base[semnum].semzcnt;
879 if ((error = semvalid(semid, rpr, semakptr)) != 0)
881 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
883 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
887 if (arg->val < 0 || arg->val > seminfo.semvmx) {
891 semakptr->u.sem_base[semnum].semval = arg->val;
893 semundo_clear(semidx, semnum);
900 * See comment on GETALL for why 'count' shouldn't change
901 * and why we require a userland buffer.
903 count = semakptr->u.sem_nsems;
904 mtx_unlock(sema_mtxp);
905 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
906 error = copyin(arg->array, array, count * sizeof(*array));
910 if ((error = semvalid(semid, rpr, semakptr)) != 0)
912 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
913 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
915 for (i = 0; i < semakptr->u.sem_nsems; i++) {
917 if (usval > seminfo.semvmx) {
921 semakptr->u.sem_base[i].semval = usval;
924 semundo_clear(semidx, -1);
935 mtx_unlock(sema_mtxp);
937 mtx_unlock(&sem_mtx);
943 #ifndef _SYS_SYSPROTO_H_
951 sys_semget(struct thread *td, struct semget_args *uap)
953 int semid, error = 0;
955 int nsems = uap->nsems;
956 int semflg = uap->semflg;
957 struct ucred *cred = td->td_ucred;
959 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
961 if (sem_find_prison(cred) == NULL)
965 if (key != IPC_PRIVATE) {
966 for (semid = 0; semid < seminfo.semmni; semid++) {
967 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
968 sema[semid].cred != NULL &&
969 sema[semid].cred->cr_prison == cred->cr_prison &&
970 sema[semid].u.sem_perm.key == key)
973 if (semid < seminfo.semmni) {
974 DPRINTF(("found public key\n"));
975 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
976 DPRINTF(("not exclusive\n"));
980 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
984 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
985 DPRINTF(("too small\n"));
990 error = mac_sysvsem_check_semget(cred, &sema[semid]);
998 DPRINTF(("need to allocate the semid_kernel\n"));
999 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
1000 if (nsems <= 0 || nsems > seminfo.semmsl) {
1001 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
1006 if (nsems > seminfo.semmns - semtot) {
1008 "not enough semaphores left (need %d, got %d)\n",
1009 nsems, seminfo.semmns - semtot));
1013 for (semid = 0; semid < seminfo.semmni; semid++) {
1014 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1017 if (semid == seminfo.semmni) {
1018 DPRINTF(("no more semid_kernel's available\n"));
1024 PROC_LOCK(td->td_proc);
1025 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1026 PROC_UNLOCK(td->td_proc);
1033 DPRINTF(("semid %d is available\n", semid));
1034 mtx_lock(&sema_mtx[semid]);
1035 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1036 ("Lost semaphore %d", semid));
1037 sema[semid].u.sem_perm.key = key;
1038 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1039 sema[semid].u.sem_perm.uid = cred->cr_uid;
1040 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1041 sema[semid].u.sem_perm.gid = cred->cr_gid;
1042 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1043 sema[semid].cred = crhold(cred);
1044 sema[semid].u.sem_perm.seq =
1045 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1046 sema[semid].u.sem_nsems = nsems;
1047 sema[semid].u.sem_otime = 0;
1048 sema[semid].u.sem_ctime = time_second;
1049 sema[semid].u.sem_base = &sem[semtot];
1051 bzero(sema[semid].u.sem_base,
1052 sizeof(sema[semid].u.sem_base[0])*nsems);
1054 mac_sysvsem_create(cred, &sema[semid]);
1056 mtx_unlock(&sema_mtx[semid]);
1057 DPRINTF(("sembase = %p, next = %p\n",
1058 sema[semid].u.sem_base, &sem[semtot]));
1060 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1066 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1068 mtx_unlock(&sem_mtx);
1072 #ifndef _SYS_SYSPROTO_H_
1075 struct sembuf *sops;
1080 sys_semop(struct thread *td, struct semop_args *uap)
1082 #define SMALL_SOPS 8
1083 struct sembuf small_sops[SMALL_SOPS];
1084 int semid = uap->semid;
1085 size_t nsops = uap->nsops;
1087 struct sembuf *sops;
1088 struct semid_kernel *semakptr;
1089 struct sembuf *sopptr = 0;
1090 struct sem *semptr = 0;
1091 struct sem_undo *suptr;
1092 struct mtx *sema_mtxp;
1095 int do_wakeup, do_undos;
1101 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1103 rpr = sem_find_prison(td->td_ucred);
1107 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1109 if (semid < 0 || semid >= seminfo.semmni)
1112 /* Allocate memory for sem_ops */
1113 if (nsops <= SMALL_SOPS)
1115 else if (nsops > seminfo.semopm) {
1116 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1122 PROC_LOCK(td->td_proc);
1124 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1125 PROC_UNLOCK(td->td_proc);
1128 PROC_UNLOCK(td->td_proc);
1132 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1134 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1135 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1136 uap->sops, sops, nsops * sizeof(sops[0])));
1137 if (sops != small_sops)
1142 semakptr = &sema[semid];
1143 sema_mtxp = &sema_mtx[semid];
1144 mtx_lock(sema_mtxp);
1145 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1149 seq = semakptr->u.sem_perm.seq;
1150 if (seq != IPCID_TO_SEQ(uap->semid)) {
1154 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1157 * Initial pass through sops to see what permissions are needed.
1158 * Also perform any checks that don't need repeating on each
1159 * attempt to satisfy the request vector.
1161 j = 0; /* permission needed */
1163 for (i = 0; i < nsops; i++) {
1165 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1169 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1171 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1174 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1175 DPRINTF(("error = %d from ipaccess\n", error));
1179 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1185 * Loop trying to satisfy the vector of requests.
1186 * If we reach a point where we must wait, any requests already
1187 * performed are rolled back and we go to sleep until some other
1188 * process wakes us up. At this point, we start all over again.
1190 * This ensures that from the perspective of other tasks, a set
1191 * of requests is atomic (never partially satisfied).
1195 error = 0; /* error return if necessary */
1197 for (i = 0; i < nsops; i++) {
1199 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1202 "semop: semakptr=%p, sem_base=%p, "
1203 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1204 semakptr, semakptr->u.sem_base, semptr,
1205 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1206 (sopptr->sem_flg & IPC_NOWAIT) ?
1207 "nowait" : "wait"));
1209 if (sopptr->sem_op < 0) {
1210 if (semptr->semval + sopptr->sem_op < 0) {
1211 DPRINTF(("semop: can't do it now\n"));
1214 semptr->semval += sopptr->sem_op;
1215 if (semptr->semval == 0 &&
1216 semptr->semzcnt > 0)
1219 } else if (sopptr->sem_op == 0) {
1220 if (semptr->semval != 0) {
1221 DPRINTF(("semop: not zero now\n"));
1224 } else if (semptr->semval + sopptr->sem_op >
1229 if (semptr->semncnt > 0)
1231 semptr->semval += sopptr->sem_op;
1236 * Did we get through the entire vector?
1242 * No ... rollback anything that we've already done
1244 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1245 for (j = 0; j < i; j++)
1246 semakptr->u.sem_base[sops[j].sem_num].semval -=
1249 /* If we detected an error, return it */
1254 * If the request that we couldn't satisfy has the
1255 * NOWAIT flag set then return with EAGAIN.
1257 if (sopptr->sem_flg & IPC_NOWAIT) {
1262 if (sopptr->sem_op == 0)
1267 DPRINTF(("semop: good night!\n"));
1268 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1270 DPRINTF(("semop: good morning (error=%d)!\n", error));
1271 /* return code is checked below, after sem[nz]cnt-- */
1274 * Make sure that the semaphore still exists
1276 seq = semakptr->u.sem_perm.seq;
1277 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1278 seq != IPCID_TO_SEQ(uap->semid)) {
1284 * Renew the semaphore's pointer after wakeup since
1285 * during msleep sem_base may have been modified and semptr
1286 * is not valid any more
1288 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1291 * The semaphore is still alive. Readjust the count of
1292 * waiting processes.
1294 if (sopptr->sem_op == 0)
1300 * Is it really morning, or was our sleep interrupted?
1301 * (Delayed check of msleep() return code because we
1302 * need to decrement sem[nz]cnt either way.)
1308 DPRINTF(("semop: good morning!\n"));
1313 * Process any SEM_UNDO requests.
1318 for (i = 0; i < nsops; i++) {
1320 * We only need to deal with SEM_UNDO's for non-zero
1325 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1327 adjval = sops[i].sem_op;
1330 error = semundo_adjust(td, &suptr, semid, seq,
1331 sops[i].sem_num, -adjval);
1336 * Oh-Oh! We ran out of either sem_undo's or undo's.
1337 * Rollback the adjustments to this point and then
1338 * rollback the semaphore ups and down so we can return
1339 * with an error with all structures restored. We
1340 * rollback the undo's in the exact reverse order that
1341 * we applied them. This guarantees that we won't run
1342 * out of space as we roll things back out.
1344 for (j = 0; j < i; j++) {
1346 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1348 adjval = sops[k].sem_op;
1351 if (semundo_adjust(td, &suptr, semid, seq,
1352 sops[k].sem_num, adjval) != 0)
1353 panic("semop - can't undo undos");
1356 for (j = 0; j < nsops; j++)
1357 semakptr->u.sem_base[sops[j].sem_num].semval -=
1360 DPRINTF(("error = %d from semundo_adjust\n", error));
1363 } /* loop through the sops */
1365 } /* if (do_undos) */
1367 /* We're definitely done - set the sempid's and time */
1368 for (i = 0; i < nsops; i++) {
1370 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1371 semptr->sempid = td->td_proc->p_pid;
1373 semakptr->u.sem_otime = time_second;
1376 * Do a wakeup if any semaphore was up'd whilst something was
1380 DPRINTF(("semop: doing wakeup\n"));
1382 DPRINTF(("semop: back from wakeup\n"));
1384 DPRINTF(("semop: done\n"));
1385 td->td_retval[0] = 0;
1387 mtx_unlock(sema_mtxp);
1388 if (sops != small_sops)
1394 * Go through the undo structures for this process and apply the adjustments to
1398 semexit_myhook(void *arg, struct proc *p)
1400 struct sem_undo *suptr;
1401 struct semid_kernel *semakptr;
1402 struct mtx *sema_mtxp;
1403 int semid, semnum, adjval, ix;
1407 * Go through the chain of undo vectors looking for one
1408 * associated with this process.
1411 LIST_FOREACH(suptr, &semu_list, un_next) {
1412 if (suptr->un_proc == p)
1415 if (suptr == NULL) {
1419 LIST_REMOVE(suptr, un_next);
1421 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1425 * If there are any active undo elements then process them.
1427 if (suptr->un_cnt > 0) {
1429 for (ix = 0; ix < suptr->un_cnt; ix++) {
1430 semid = suptr->un_ent[ix].un_id;
1431 semnum = suptr->un_ent[ix].un_num;
1432 adjval = suptr->un_ent[ix].un_adjval;
1433 seq = suptr->un_ent[ix].un_seq;
1434 semakptr = &sema[semid];
1435 sema_mtxp = &sema_mtx[semid];
1437 mtx_lock(sema_mtxp);
1438 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1439 (semakptr->u.sem_perm.seq != seq)) {
1440 mtx_unlock(sema_mtxp);
1443 if (semnum >= semakptr->u.sem_nsems)
1444 panic("semexit - semnum out of range");
1447 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1448 suptr->un_proc, suptr->un_ent[ix].un_id,
1449 suptr->un_ent[ix].un_num,
1450 suptr->un_ent[ix].un_adjval,
1451 semakptr->u.sem_base[semnum].semval));
1453 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1455 semakptr->u.sem_base[semnum].semval = 0;
1457 semakptr->u.sem_base[semnum].semval += adjval;
1460 DPRINTF(("semexit: back from wakeup\n"));
1461 mtx_unlock(sema_mtxp);
1467 * Deallocate the undo vector.
1469 DPRINTF(("removing vector\n"));
1470 suptr->un_proc = NULL;
1472 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1477 sysctl_sema(SYSCTL_HANDLER_ARGS)
1479 struct prison *pr, *rpr;
1480 struct semid_kernel tsemak;
1483 pr = req->td->td_ucred->cr_prison;
1484 rpr = sem_find_prison(req->td->td_ucred);
1486 for (i = 0; i < seminfo.semmni; i++) {
1487 mtx_lock(&sema_mtx[i]);
1488 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1489 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1490 bzero(&tsemak, sizeof(tsemak));
1493 if (tsemak.cred->cr_prison != pr)
1494 tsemak.u.sem_perm.key = IPC_PRIVATE;
1496 mtx_unlock(&sema_mtx[i]);
1497 error = SYSCTL_OUT(req, &tsemak, sizeof(tsemak));
1505 sem_prison_check(void *obj, void *data)
1507 struct prison *pr = obj;
1508 struct prison *prpr;
1509 struct vfsoptlist *opts = data;
1513 * sysvsem is a jailsys integer.
1514 * It must be "disable" if the parent jail is disabled.
1516 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1517 if (error != ENOENT) {
1521 case JAIL_SYS_DISABLE:
1524 case JAIL_SYS_INHERIT:
1525 prison_lock(pr->pr_parent);
1526 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1527 prison_unlock(pr->pr_parent);
1540 sem_prison_set(void *obj, void *data)
1542 struct prison *pr = obj;
1543 struct prison *tpr, *orpr, *nrpr, *trpr;
1544 struct vfsoptlist *opts = data;
1549 * sysvsem controls which jail is the root of the associated sems (this
1550 * jail or same as the parent), or if the feature is available at all.
1552 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1553 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1555 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1558 if (jsys == JAIL_SYS_DISABLE) {
1560 orpr = osd_jail_get(pr, sem_prison_slot);
1562 osd_jail_del(pr, sem_prison_slot);
1566 sem_prison_cleanup(pr);
1567 /* Disable all child jails as well. */
1568 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1570 trpr = osd_jail_get(tpr, sem_prison_slot);
1572 osd_jail_del(tpr, sem_prison_slot);
1575 sem_prison_cleanup(tpr);
1582 } else if (jsys != -1) {
1583 if (jsys == JAIL_SYS_NEW)
1586 prison_lock(pr->pr_parent);
1587 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1588 prison_unlock(pr->pr_parent);
1590 rsv = osd_reserve(sem_prison_slot);
1592 orpr = osd_jail_get(pr, sem_prison_slot);
1594 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1597 osd_free_reserved(rsv);
1601 sem_prison_cleanup(pr);
1603 /* Change child jails matching the old root, */
1604 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1606 trpr = osd_jail_get(tpr,
1609 (void)osd_jail_set(tpr,
1610 sem_prison_slot, nrpr);
1613 sem_prison_cleanup(tpr);
1627 sem_prison_get(void *obj, void *data)
1629 struct prison *pr = obj;
1631 struct vfsoptlist *opts = data;
1634 /* Set sysvsem based on the jail's root prison. */
1636 rpr = osd_jail_get(pr, sem_prison_slot);
1638 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1639 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1640 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1641 if (error == ENOENT)
1647 sem_prison_remove(void *obj, void *data __unused)
1649 struct prison *pr = obj;
1653 rpr = osd_jail_get(pr, sem_prison_slot);
1656 sem_prison_cleanup(pr);
1661 sem_prison_cleanup(struct prison *pr)
1665 /* Remove any sems that belong to this jail. */
1667 for (i = 0; i < seminfo.semmni; i++) {
1668 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1669 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1670 mtx_lock(&sema_mtx[i]);
1671 sem_remove(i, NULL);
1672 mtx_unlock(&sema_mtx[i]);
1675 mtx_unlock(&sem_mtx);
1678 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1680 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1681 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1683 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1684 static sy_call_t *semcalls[] = {
1685 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1686 (sy_call_t *)sys_semop
1690 * Entry point for all SEM calls.
1695 /* XXX actually varargs. */
1696 struct semsys_args /* {
1706 if (uap->which < 0 ||
1707 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1709 error = (*semcalls[uap->which])(td, &uap->a2);
1714 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1717 #ifndef _SYS_SYSPROTO_H_
1718 struct freebsd7___semctl_args {
1722 union semun_old *arg;
1726 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1728 struct semid_ds_old dsold;
1729 struct semid_ds dsbuf;
1730 union semun_old arg;
1742 error = copyin(uap->arg, &arg, sizeof(arg));
1754 error = copyin(arg.buf, &dsold, sizeof(dsold));
1757 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1758 CP(dsold, dsbuf, sem_base);
1759 CP(dsold, dsbuf, sem_nsems);
1760 CP(dsold, dsbuf, sem_otime);
1761 CP(dsold, dsbuf, sem_ctime);
1766 semun.array = arg.array;
1769 semun.val = arg.val;
1773 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1781 bzero(&dsold, sizeof(dsold));
1782 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1783 CP(dsbuf, dsold, sem_base);
1784 CP(dsbuf, dsold, sem_nsems);
1785 CP(dsbuf, dsold, sem_otime);
1786 CP(dsbuf, dsold, sem_ctime);
1787 error = copyout(&dsold, arg.buf, sizeof(dsold));
1792 td->td_retval[0] = rval;
1796 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1798 #ifdef COMPAT_FREEBSD32
1801 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1804 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1805 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1806 switch (uap->which) {
1808 return (freebsd7_freebsd32_semctl(td,
1809 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1811 return (sys_semsys(td, (struct semsys_args *)uap));
1814 return (nosys(td, NULL));
1818 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1819 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1821 freebsd7_freebsd32_semctl(struct thread *td,
1822 struct freebsd7_freebsd32_semctl_args *uap)
1824 struct semid_ds32_old dsbuf32;
1825 struct semid_ds dsbuf;
1838 error = copyin(uap->arg, &arg, sizeof(arg));
1850 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1853 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1854 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1855 CP(dsbuf32, dsbuf, sem_nsems);
1856 CP(dsbuf32, dsbuf, sem_otime);
1857 CP(dsbuf32, dsbuf, sem_ctime);
1862 semun.array = PTRIN(arg.array);
1865 semun.val = arg.val;
1869 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1877 bzero(&dsbuf32, sizeof(dsbuf32));
1878 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1879 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1880 CP(dsbuf, dsbuf32, sem_nsems);
1881 CP(dsbuf, dsbuf32, sem_otime);
1882 CP(dsbuf, dsbuf32, sem_ctime);
1883 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1888 td->td_retval[0] = rval;
1894 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1896 struct semid_ds32 dsbuf32;
1897 struct semid_ds dsbuf;
1910 error = copyin(uap->arg, &arg, sizeof(arg));
1922 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1925 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1926 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1927 CP(dsbuf32, dsbuf, sem_nsems);
1928 CP(dsbuf32, dsbuf, sem_otime);
1929 CP(dsbuf32, dsbuf, sem_ctime);
1934 semun.array = PTRIN(arg.array);
1937 semun.val = arg.val;
1941 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1949 bzero(&dsbuf32, sizeof(dsbuf32));
1950 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1951 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1952 CP(dsbuf, dsbuf32, sem_nsems);
1953 CP(dsbuf, dsbuf32, sem_otime);
1954 CP(dsbuf, dsbuf32, sem_ctime);
1955 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1960 td->td_retval[0] = rval;
1964 #endif /* COMPAT_FREEBSD32 */