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, "", "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 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
273 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
274 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
275 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
276 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
277 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
278 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
279 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
280 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
282 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
283 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
285 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
287 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
289 for (i = 0; i < seminfo.semmni; i++) {
290 sema[i].u.sem_base = 0;
291 sema[i].u.sem_perm.mode = 0;
292 sema[i].u.sem_perm.seq = 0;
294 mac_sysvsem_init(&sema[i]);
297 for (i = 0; i < seminfo.semmni; i++)
298 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
299 LIST_INIT(&semu_free_list);
300 for (i = 0; i < seminfo.semmnu; i++) {
301 struct sem_undo *suptr = SEMU(i);
302 suptr->un_proc = NULL;
303 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
305 LIST_INIT(&semu_list);
306 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
307 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
308 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
309 EVENTHANDLER_PRI_ANY);
311 /* Set current prisons according to their allow.sysvipc. */
312 sem_prison_slot = osd_jail_register(NULL, methods);
313 rsv = osd_reserve(sem_prison_slot);
314 prison_lock(&prison0);
315 (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
316 prison_unlock(&prison0);
318 sx_slock(&allprison_lock);
319 TAILQ_FOREACH(pr, &allprison, pr_list) {
321 rsv = osd_reserve(sem_prison_slot);
323 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
324 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
331 osd_free_reserved(rsv);
332 sx_sunlock(&allprison_lock);
334 error = syscall_helper_register(sem_syscalls);
337 #ifdef COMPAT_FREEBSD32
338 error = syscall32_helper_register(sem32_syscalls);
354 #ifdef COMPAT_FREEBSD32
355 syscall32_helper_unregister(sem32_syscalls);
357 syscall_helper_unregister(sem_syscalls);
358 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
359 if (sem_prison_slot != 0)
360 osd_jail_deregister(sem_prison_slot);
362 for (i = 0; i < seminfo.semmni; i++)
363 mac_sysvsem_destroy(&sema[i]);
368 for (i = 0; i < seminfo.semmni; i++)
369 mtx_destroy(&sema_mtx[i]);
370 free(sema_mtx, M_SEM);
371 mtx_destroy(&sem_mtx);
372 mtx_destroy(&sem_undo_mtx);
377 sysvsem_modload(struct module *module, int cmd, void *arg)
399 static moduledata_t sysvsem_mod = {
405 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
406 MODULE_VERSION(sysvsem, 1);
409 * Allocate a new sem_undo structure for a process
410 * (returns ptr to structure or NULL if no more room)
413 static struct sem_undo *
414 semu_alloc(struct thread *td)
416 struct sem_undo *suptr;
418 SEMUNDO_LOCKASSERT(MA_OWNED);
419 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
421 LIST_REMOVE(suptr, un_next);
422 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
424 suptr->un_proc = td->td_proc;
429 semu_try_free(struct sem_undo *suptr)
432 SEMUNDO_LOCKASSERT(MA_OWNED);
434 if (suptr->un_cnt != 0)
436 LIST_REMOVE(suptr, un_next);
437 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
442 * Adjust a particular entry for a particular proc
446 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
447 int semseq, int semnum, int adjval)
449 struct proc *p = td->td_proc;
450 struct sem_undo *suptr;
454 SEMUNDO_LOCKASSERT(MA_OWNED);
455 /* Look for and remember the sem_undo if the caller doesn't provide
460 LIST_FOREACH(suptr, &semu_list, un_next) {
461 if (suptr->un_proc == p) {
469 suptr = semu_alloc(td);
477 * Look for the requested entry and adjust it (delete if adjval becomes
480 sunptr = &suptr->un_ent[0];
481 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
482 if (sunptr->un_id != semid || sunptr->un_num != semnum)
485 adjval += sunptr->un_adjval;
486 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
489 sunptr->un_adjval = adjval;
490 if (sunptr->un_adjval == 0) {
492 if (i < suptr->un_cnt)
494 suptr->un_ent[suptr->un_cnt];
495 if (suptr->un_cnt == 0)
496 semu_try_free(suptr);
501 /* Didn't find the right entry - create it */
504 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
506 if (suptr->un_cnt != seminfo.semume) {
507 sunptr = &suptr->un_ent[suptr->un_cnt];
509 sunptr->un_adjval = adjval;
510 sunptr->un_id = semid;
511 sunptr->un_num = semnum;
512 sunptr->un_seq = semseq;
519 semundo_clear(int semid, int semnum)
521 struct sem_undo *suptr, *suptr1;
525 SEMUNDO_LOCKASSERT(MA_OWNED);
526 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
527 sunptr = &suptr->un_ent[0];
528 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
529 if (sunptr->un_id != semid)
531 if (semnum == -1 || sunptr->un_num == semnum) {
533 if (i < suptr->un_cnt) {
535 suptr->un_ent[suptr->un_cnt];
538 semu_try_free(suptr);
547 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
550 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
551 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
552 sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
556 sem_remove(int semidx, struct ucred *cred)
558 struct semid_kernel *semakptr;
561 KASSERT(semidx >= 0 && semidx < seminfo.semmni,
562 ("semidx out of bounds"));
563 semakptr = &sema[semidx];
564 semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
565 semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
566 semakptr->u.sem_perm.mode = 0;
567 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
568 crfree(semakptr->cred);
569 semakptr->cred = NULL;
571 semundo_clear(semidx, -1);
574 mac_sysvsem_cleanup(semakptr);
577 for (i = 0; i < seminfo.semmni; i++) {
578 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
579 sema[i].u.sem_base > semakptr->u.sem_base)
580 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
582 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
583 sem[i] = sem[i + semakptr->u.sem_nsems];
584 for (i = 0; i < seminfo.semmni; i++) {
585 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
586 sema[i].u.sem_base > semakptr->u.sem_base) {
587 sema[i].u.sem_base -= semakptr->u.sem_nsems;
588 mtx_unlock(&sema_mtx[i]);
591 semtot -= semakptr->u.sem_nsems;
594 static struct prison *
595 sem_find_prison(struct ucred *cred)
597 struct prison *pr, *rpr;
599 pr = cred->cr_prison;
601 rpr = osd_jail_get(pr, sem_prison_slot);
607 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
610 if (semakptr->cred == NULL ||
611 !(rpr == semakptr->cred->cr_prison ||
612 prison_ischild(rpr, semakptr->cred->cr_prison)))
618 * Note that the user-mode half of this passes a union, not a pointer.
620 #ifndef _SYS_SYSPROTO_H_
621 struct __semctl_args {
629 sys___semctl(struct thread *td, struct __semctl_args *uap)
631 struct semid_ds dsbuf;
632 union semun arg, semun;
643 error = copyin(uap->arg, &arg, sizeof(arg));
655 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
662 semun.array = arg.array;
669 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
677 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
682 td->td_retval[0] = rval;
687 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
688 union semun *arg, register_t *rval)
691 struct ucred *cred = td->td_ucred;
694 struct semid_ds *sbuf;
695 struct semid_kernel *semakptr;
696 struct mtx *sema_mtxp;
697 u_short usval, count;
700 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
701 semid, semnum, cmd, arg));
703 rpr = sem_find_prison(td->td_ucred);
712 * For this command we assume semid is an array index
713 * rather than an IPC id.
715 if (semid < 0 || semid >= seminfo.semmni)
717 semakptr = &sema[semid];
718 sema_mtxp = &sema_mtx[semid];
720 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
724 if ((error = sem_prison_cansee(rpr, semakptr)))
726 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
729 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
733 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
734 if (cred->cr_prison != semakptr->cred->cr_prison)
735 arg->buf->sem_perm.key = IPC_PRIVATE;
736 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
737 mtx_unlock(sema_mtxp);
741 semidx = IPCID_TO_IX(semid);
742 if (semidx < 0 || semidx >= seminfo.semmni)
745 semakptr = &sema[semidx];
746 sema_mtxp = &sema_mtx[semidx];
752 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
762 if ((error = semvalid(semid, rpr, semakptr)) != 0)
764 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
766 sem_remove(semidx, cred);
770 if ((error = semvalid(semid, rpr, semakptr)) != 0)
772 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
775 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
776 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
777 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
778 ~0777) | (sbuf->sem_perm.mode & 0777);
779 semakptr->u.sem_ctime = time_second;
783 if ((error = semvalid(semid, rpr, semakptr)) != 0)
785 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
787 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
788 if (cred->cr_prison != semakptr->cred->cr_prison)
789 arg->buf->sem_perm.key = IPC_PRIVATE;
793 if ((error = semvalid(semid, rpr, semakptr)) != 0)
795 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
797 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
801 *rval = semakptr->u.sem_base[semnum].semncnt;
805 if ((error = semvalid(semid, rpr, semakptr)) != 0)
807 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
809 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
813 *rval = semakptr->u.sem_base[semnum].sempid;
817 if ((error = semvalid(semid, rpr, semakptr)) != 0)
819 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
821 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
825 *rval = semakptr->u.sem_base[semnum].semval;
830 * Unfortunately, callers of this function don't know
831 * in advance how many semaphores are in this set.
832 * While we could just allocate the maximum size array
833 * and pass the actual size back to the caller, that
834 * won't work for SETALL since we can't copyin() more
835 * data than the user specified as we may return a
838 * Note that the number of semaphores in a set is
839 * fixed for the life of that set. The only way that
840 * the 'count' could change while are blocked in
841 * malloc() is if this semaphore set were destroyed
842 * and a new one created with the same index.
843 * However, semvalid() will catch that due to the
844 * sequence number unless exactly 0x8000 (or a
845 * multiple thereof) semaphore sets for the same index
846 * are created and destroyed while we are in malloc!
849 count = semakptr->u.sem_nsems;
850 mtx_unlock(sema_mtxp);
851 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
853 if ((error = semvalid(semid, rpr, semakptr)) != 0)
855 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
856 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
858 for (i = 0; i < semakptr->u.sem_nsems; i++)
859 array[i] = semakptr->u.sem_base[i].semval;
860 mtx_unlock(sema_mtxp);
861 error = copyout(array, arg->array, count * sizeof(*array));
866 if ((error = semvalid(semid, rpr, semakptr)) != 0)
868 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
870 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
874 *rval = semakptr->u.sem_base[semnum].semzcnt;
878 if ((error = semvalid(semid, rpr, semakptr)) != 0)
880 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
882 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
886 if (arg->val < 0 || arg->val > seminfo.semvmx) {
890 semakptr->u.sem_base[semnum].semval = arg->val;
892 semundo_clear(semidx, semnum);
899 * See comment on GETALL for why 'count' shouldn't change
900 * and why we require a userland buffer.
902 count = semakptr->u.sem_nsems;
903 mtx_unlock(sema_mtxp);
904 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
905 error = copyin(arg->array, array, count * sizeof(*array));
909 if ((error = semvalid(semid, rpr, semakptr)) != 0)
911 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
912 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
914 for (i = 0; i < semakptr->u.sem_nsems; i++) {
916 if (usval > seminfo.semvmx) {
920 semakptr->u.sem_base[i].semval = usval;
923 semundo_clear(semidx, -1);
934 mtx_unlock(sema_mtxp);
936 mtx_unlock(&sem_mtx);
942 #ifndef _SYS_SYSPROTO_H_
950 sys_semget(struct thread *td, struct semget_args *uap)
952 int semid, error = 0;
954 int nsems = uap->nsems;
955 int semflg = uap->semflg;
956 struct ucred *cred = td->td_ucred;
958 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
960 if (sem_find_prison(cred) == NULL)
964 if (key != IPC_PRIVATE) {
965 for (semid = 0; semid < seminfo.semmni; semid++) {
966 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
967 sema[semid].cred != NULL &&
968 sema[semid].cred->cr_prison == cred->cr_prison &&
969 sema[semid].u.sem_perm.key == key)
972 if (semid < seminfo.semmni) {
973 DPRINTF(("found public key\n"));
974 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
975 DPRINTF(("not exclusive\n"));
979 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
983 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
984 DPRINTF(("too small\n"));
989 error = mac_sysvsem_check_semget(cred, &sema[semid]);
997 DPRINTF(("need to allocate the semid_kernel\n"));
998 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
999 if (nsems <= 0 || nsems > seminfo.semmsl) {
1000 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
1005 if (nsems > seminfo.semmns - semtot) {
1007 "not enough semaphores left (need %d, got %d)\n",
1008 nsems, seminfo.semmns - semtot));
1012 for (semid = 0; semid < seminfo.semmni; semid++) {
1013 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1016 if (semid == seminfo.semmni) {
1017 DPRINTF(("no more semid_kernel's available\n"));
1023 PROC_LOCK(td->td_proc);
1024 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
1025 PROC_UNLOCK(td->td_proc);
1032 DPRINTF(("semid %d is available\n", semid));
1033 mtx_lock(&sema_mtx[semid]);
1034 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
1035 ("Lost semaphore %d", semid));
1036 sema[semid].u.sem_perm.key = key;
1037 sema[semid].u.sem_perm.cuid = cred->cr_uid;
1038 sema[semid].u.sem_perm.uid = cred->cr_uid;
1039 sema[semid].u.sem_perm.cgid = cred->cr_gid;
1040 sema[semid].u.sem_perm.gid = cred->cr_gid;
1041 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1042 sema[semid].cred = crhold(cred);
1043 sema[semid].u.sem_perm.seq =
1044 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
1045 sema[semid].u.sem_nsems = nsems;
1046 sema[semid].u.sem_otime = 0;
1047 sema[semid].u.sem_ctime = time_second;
1048 sema[semid].u.sem_base = &sem[semtot];
1050 bzero(sema[semid].u.sem_base,
1051 sizeof(sema[semid].u.sem_base[0])*nsems);
1053 mac_sysvsem_create(cred, &sema[semid]);
1055 mtx_unlock(&sema_mtx[semid]);
1056 DPRINTF(("sembase = %p, next = %p\n",
1057 sema[semid].u.sem_base, &sem[semtot]));
1059 DPRINTF(("didn't find it and wasn't asked to create it\n"));
1065 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
1067 mtx_unlock(&sem_mtx);
1071 #ifndef _SYS_SYSPROTO_H_
1074 struct sembuf *sops;
1079 sys_semop(struct thread *td, struct semop_args *uap)
1081 #define SMALL_SOPS 8
1082 struct sembuf small_sops[SMALL_SOPS];
1083 int semid = uap->semid;
1084 size_t nsops = uap->nsops;
1086 struct sembuf *sops;
1087 struct semid_kernel *semakptr;
1088 struct sembuf *sopptr = 0;
1089 struct sem *semptr = 0;
1090 struct sem_undo *suptr;
1091 struct mtx *sema_mtxp;
1094 int do_wakeup, do_undos;
1100 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1102 rpr = sem_find_prison(td->td_ucred);
1106 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1108 if (semid < 0 || semid >= seminfo.semmni)
1111 /* Allocate memory for sem_ops */
1112 if (nsops <= SMALL_SOPS)
1114 else if (nsops > seminfo.semopm) {
1115 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1121 PROC_LOCK(td->td_proc);
1123 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1124 PROC_UNLOCK(td->td_proc);
1127 PROC_UNLOCK(td->td_proc);
1131 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1133 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1134 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1135 uap->sops, sops, nsops * sizeof(sops[0])));
1136 if (sops != small_sops)
1141 semakptr = &sema[semid];
1142 sema_mtxp = &sema_mtx[semid];
1143 mtx_lock(sema_mtxp);
1144 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1148 seq = semakptr->u.sem_perm.seq;
1149 if (seq != IPCID_TO_SEQ(uap->semid)) {
1153 if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
1156 * Initial pass through sops to see what permissions are needed.
1157 * Also perform any checks that don't need repeating on each
1158 * attempt to satisfy the request vector.
1160 j = 0; /* permission needed */
1162 for (i = 0; i < nsops; i++) {
1164 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1168 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1170 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1173 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1174 DPRINTF(("error = %d from ipaccess\n", error));
1178 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1184 * Loop trying to satisfy the vector of requests.
1185 * If we reach a point where we must wait, any requests already
1186 * performed are rolled back and we go to sleep until some other
1187 * process wakes us up. At this point, we start all over again.
1189 * This ensures that from the perspective of other tasks, a set
1190 * of requests is atomic (never partially satisfied).
1194 error = 0; /* error return if necessary */
1196 for (i = 0; i < nsops; i++) {
1198 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1201 "semop: semakptr=%p, sem_base=%p, "
1202 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1203 semakptr, semakptr->u.sem_base, semptr,
1204 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1205 (sopptr->sem_flg & IPC_NOWAIT) ?
1206 "nowait" : "wait"));
1208 if (sopptr->sem_op < 0) {
1209 if (semptr->semval + sopptr->sem_op < 0) {
1210 DPRINTF(("semop: can't do it now\n"));
1213 semptr->semval += sopptr->sem_op;
1214 if (semptr->semval == 0 &&
1215 semptr->semzcnt > 0)
1218 } else if (sopptr->sem_op == 0) {
1219 if (semptr->semval != 0) {
1220 DPRINTF(("semop: not zero now\n"));
1223 } else if (semptr->semval + sopptr->sem_op >
1228 if (semptr->semncnt > 0)
1230 semptr->semval += sopptr->sem_op;
1235 * Did we get through the entire vector?
1241 * No ... rollback anything that we've already done
1243 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1244 for (j = 0; j < i; j++)
1245 semakptr->u.sem_base[sops[j].sem_num].semval -=
1248 /* If we detected an error, return it */
1253 * If the request that we couldn't satisfy has the
1254 * NOWAIT flag set then return with EAGAIN.
1256 if (sopptr->sem_flg & IPC_NOWAIT) {
1261 if (sopptr->sem_op == 0)
1266 DPRINTF(("semop: good night!\n"));
1267 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1269 DPRINTF(("semop: good morning (error=%d)!\n", error));
1270 /* return code is checked below, after sem[nz]cnt-- */
1273 * Make sure that the semaphore still exists
1275 seq = semakptr->u.sem_perm.seq;
1276 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1277 seq != IPCID_TO_SEQ(uap->semid)) {
1283 * Renew the semaphore's pointer after wakeup since
1284 * during msleep sem_base may have been modified and semptr
1285 * is not valid any more
1287 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1290 * The semaphore is still alive. Readjust the count of
1291 * waiting processes.
1293 if (sopptr->sem_op == 0)
1299 * Is it really morning, or was our sleep interrupted?
1300 * (Delayed check of msleep() return code because we
1301 * need to decrement sem[nz]cnt either way.)
1307 DPRINTF(("semop: good morning!\n"));
1312 * Process any SEM_UNDO requests.
1317 for (i = 0; i < nsops; i++) {
1319 * We only need to deal with SEM_UNDO's for non-zero
1324 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1326 adjval = sops[i].sem_op;
1329 error = semundo_adjust(td, &suptr, semid, seq,
1330 sops[i].sem_num, -adjval);
1335 * Oh-Oh! We ran out of either sem_undo's or undo's.
1336 * Rollback the adjustments to this point and then
1337 * rollback the semaphore ups and down so we can return
1338 * with an error with all structures restored. We
1339 * rollback the undo's in the exact reverse order that
1340 * we applied them. This guarantees that we won't run
1341 * out of space as we roll things back out.
1343 for (j = 0; j < i; j++) {
1345 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1347 adjval = sops[k].sem_op;
1350 if (semundo_adjust(td, &suptr, semid, seq,
1351 sops[k].sem_num, adjval) != 0)
1352 panic("semop - can't undo undos");
1355 for (j = 0; j < nsops; j++)
1356 semakptr->u.sem_base[sops[j].sem_num].semval -=
1359 DPRINTF(("error = %d from semundo_adjust\n", error));
1362 } /* loop through the sops */
1364 } /* if (do_undos) */
1366 /* We're definitely done - set the sempid's and time */
1367 for (i = 0; i < nsops; i++) {
1369 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1370 semptr->sempid = td->td_proc->p_pid;
1372 semakptr->u.sem_otime = time_second;
1375 * Do a wakeup if any semaphore was up'd whilst something was
1379 DPRINTF(("semop: doing wakeup\n"));
1381 DPRINTF(("semop: back from wakeup\n"));
1383 DPRINTF(("semop: done\n"));
1384 td->td_retval[0] = 0;
1386 mtx_unlock(sema_mtxp);
1387 if (sops != small_sops)
1393 * Go through the undo structures for this process and apply the adjustments to
1397 semexit_myhook(void *arg, struct proc *p)
1399 struct sem_undo *suptr;
1400 struct semid_kernel *semakptr;
1401 struct mtx *sema_mtxp;
1402 int semid, semnum, adjval, ix;
1406 * Go through the chain of undo vectors looking for one
1407 * associated with this process.
1410 LIST_FOREACH(suptr, &semu_list, un_next) {
1411 if (suptr->un_proc == p)
1414 if (suptr == NULL) {
1418 LIST_REMOVE(suptr, un_next);
1420 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1424 * If there are any active undo elements then process them.
1426 if (suptr->un_cnt > 0) {
1428 for (ix = 0; ix < suptr->un_cnt; ix++) {
1429 semid = suptr->un_ent[ix].un_id;
1430 semnum = suptr->un_ent[ix].un_num;
1431 adjval = suptr->un_ent[ix].un_adjval;
1432 seq = suptr->un_ent[ix].un_seq;
1433 semakptr = &sema[semid];
1434 sema_mtxp = &sema_mtx[semid];
1436 mtx_lock(sema_mtxp);
1437 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1438 (semakptr->u.sem_perm.seq != seq)) {
1439 mtx_unlock(sema_mtxp);
1442 if (semnum >= semakptr->u.sem_nsems)
1443 panic("semexit - semnum out of range");
1446 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1447 suptr->un_proc, suptr->un_ent[ix].un_id,
1448 suptr->un_ent[ix].un_num,
1449 suptr->un_ent[ix].un_adjval,
1450 semakptr->u.sem_base[semnum].semval));
1452 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1454 semakptr->u.sem_base[semnum].semval = 0;
1456 semakptr->u.sem_base[semnum].semval += adjval;
1459 DPRINTF(("semexit: back from wakeup\n"));
1460 mtx_unlock(sema_mtxp);
1466 * Deallocate the undo vector.
1468 DPRINTF(("removing vector\n"));
1469 suptr->un_proc = NULL;
1471 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1476 sysctl_sema(SYSCTL_HANDLER_ARGS)
1478 struct prison *pr, *rpr;
1479 struct semid_kernel tsemak;
1482 pr = req->td->td_ucred->cr_prison;
1483 rpr = sem_find_prison(req->td->td_ucred);
1485 for (i = 0; i < seminfo.semmni; i++) {
1486 mtx_lock(&sema_mtx[i]);
1487 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
1488 rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
1489 bzero(&tsemak, sizeof(tsemak));
1492 if (tsemak.cred->cr_prison != pr)
1493 tsemak.u.sem_perm.key = IPC_PRIVATE;
1495 mtx_unlock(&sema_mtx[i]);
1496 error = SYSCTL_OUT(req, &tsemak, sizeof(tsemak));
1504 sem_prison_check(void *obj, void *data)
1506 struct prison *pr = obj;
1507 struct prison *prpr;
1508 struct vfsoptlist *opts = data;
1512 * sysvsem is a jailsys integer.
1513 * It must be "disable" if the parent jail is disabled.
1515 error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
1516 if (error != ENOENT) {
1520 case JAIL_SYS_DISABLE:
1523 case JAIL_SYS_INHERIT:
1524 prison_lock(pr->pr_parent);
1525 prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1526 prison_unlock(pr->pr_parent);
1539 sem_prison_set(void *obj, void *data)
1541 struct prison *pr = obj;
1542 struct prison *tpr, *orpr, *nrpr, *trpr;
1543 struct vfsoptlist *opts = data;
1548 * sysvsem controls which jail is the root of the associated sems (this
1549 * jail or same as the parent), or if the feature is available at all.
1551 if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
1552 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1554 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1557 if (jsys == JAIL_SYS_DISABLE) {
1559 orpr = osd_jail_get(pr, sem_prison_slot);
1561 osd_jail_del(pr, sem_prison_slot);
1565 sem_prison_cleanup(pr);
1566 /* Disable all child jails as well. */
1567 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1569 trpr = osd_jail_get(tpr, sem_prison_slot);
1571 osd_jail_del(tpr, sem_prison_slot);
1574 sem_prison_cleanup(tpr);
1581 } else if (jsys != -1) {
1582 if (jsys == JAIL_SYS_NEW)
1585 prison_lock(pr->pr_parent);
1586 nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
1587 prison_unlock(pr->pr_parent);
1589 rsv = osd_reserve(sem_prison_slot);
1591 orpr = osd_jail_get(pr, sem_prison_slot);
1593 (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
1596 osd_free_reserved(rsv);
1600 sem_prison_cleanup(pr);
1602 /* Change child jails matching the old root, */
1603 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1605 trpr = osd_jail_get(tpr,
1608 (void)osd_jail_set(tpr,
1609 sem_prison_slot, nrpr);
1612 sem_prison_cleanup(tpr);
1626 sem_prison_get(void *obj, void *data)
1628 struct prison *pr = obj;
1630 struct vfsoptlist *opts = data;
1633 /* Set sysvsem based on the jail's root prison. */
1635 rpr = osd_jail_get(pr, sem_prison_slot);
1637 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1638 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1639 error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
1640 if (error == ENOENT)
1646 sem_prison_remove(void *obj, void *data __unused)
1648 struct prison *pr = obj;
1652 rpr = osd_jail_get(pr, sem_prison_slot);
1655 sem_prison_cleanup(pr);
1660 sem_prison_cleanup(struct prison *pr)
1664 /* Remove any sems that belong to this jail. */
1666 for (i = 0; i < seminfo.semmni; i++) {
1667 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
1668 sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
1669 mtx_lock(&sema_mtx[i]);
1670 sem_remove(i, NULL);
1671 mtx_unlock(&sema_mtx[i]);
1674 mtx_unlock(&sem_mtx);
1677 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
1679 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1680 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1682 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1683 static sy_call_t *semcalls[] = {
1684 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1685 (sy_call_t *)sys_semop
1689 * Entry point for all SEM calls.
1694 /* XXX actually varargs. */
1695 struct semsys_args /* {
1705 if (uap->which < 0 ||
1706 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1708 error = (*semcalls[uap->which])(td, &uap->a2);
1713 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1716 #ifndef _SYS_SYSPROTO_H_
1717 struct freebsd7___semctl_args {
1721 union semun_old *arg;
1725 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1727 struct semid_ds_old dsold;
1728 struct semid_ds dsbuf;
1729 union semun_old arg;
1741 error = copyin(uap->arg, &arg, sizeof(arg));
1753 error = copyin(arg.buf, &dsold, sizeof(dsold));
1756 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1757 CP(dsold, dsbuf, sem_base);
1758 CP(dsold, dsbuf, sem_nsems);
1759 CP(dsold, dsbuf, sem_otime);
1760 CP(dsold, dsbuf, sem_ctime);
1765 semun.array = arg.array;
1768 semun.val = arg.val;
1772 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1780 bzero(&dsold, sizeof(dsold));
1781 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1782 CP(dsbuf, dsold, sem_base);
1783 CP(dsbuf, dsold, sem_nsems);
1784 CP(dsbuf, dsold, sem_otime);
1785 CP(dsbuf, dsold, sem_ctime);
1786 error = copyout(&dsold, arg.buf, sizeof(dsold));
1791 td->td_retval[0] = rval;
1795 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1797 #ifdef COMPAT_FREEBSD32
1800 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1803 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1804 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1805 switch (uap->which) {
1807 return (freebsd7_freebsd32_semctl(td,
1808 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1810 return (sys_semsys(td, (struct semsys_args *)uap));
1813 return (nosys(td, NULL));
1817 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1818 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1820 freebsd7_freebsd32_semctl(struct thread *td,
1821 struct freebsd7_freebsd32_semctl_args *uap)
1823 struct semid_ds32_old dsbuf32;
1824 struct semid_ds dsbuf;
1837 error = copyin(uap->arg, &arg, sizeof(arg));
1849 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1852 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1853 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1854 CP(dsbuf32, dsbuf, sem_nsems);
1855 CP(dsbuf32, dsbuf, sem_otime);
1856 CP(dsbuf32, dsbuf, sem_ctime);
1861 semun.array = PTRIN(arg.array);
1864 semun.val = arg.val;
1868 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1876 bzero(&dsbuf32, sizeof(dsbuf32));
1877 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1878 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1879 CP(dsbuf, dsbuf32, sem_nsems);
1880 CP(dsbuf, dsbuf32, sem_otime);
1881 CP(dsbuf, dsbuf32, sem_ctime);
1882 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1887 td->td_retval[0] = rval;
1893 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1895 struct semid_ds32 dsbuf32;
1896 struct semid_ds dsbuf;
1909 error = copyin(uap->arg, &arg, sizeof(arg));
1921 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1924 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1925 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1926 CP(dsbuf32, dsbuf, sem_nsems);
1927 CP(dsbuf32, dsbuf, sem_otime);
1928 CP(dsbuf32, dsbuf, sem_ctime);
1933 semun.array = PTRIN(arg.array);
1936 semun.val = arg.val;
1940 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1948 bzero(&dsbuf32, sizeof(dsbuf32));
1949 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1950 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1951 CP(dsbuf, dsbuf32, sem_nsems);
1952 CP(dsbuf, dsbuf32, sem_otime);
1953 CP(dsbuf, dsbuf32, sem_ctime);
1954 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1959 td->td_retval[0] = rval;
1963 #endif /* COMPAT_FREEBSD32 */