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>
56 #include <sys/syscall.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysent.h>
59 #include <sys/sysctl.h>
61 #include <sys/malloc.h>
64 #include <security/mac/mac_framework.h>
66 FEATURE(sysv_sem, "System V semaphores support");
68 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
71 #define DPRINTF(a) printf a
76 static int seminit(void);
77 static int sysvsem_modload(struct module *, int, void *);
78 static int semunload(void);
79 static void semexit_myhook(void *arg, struct proc *p);
80 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
81 static int semvalid(int semid, struct semid_kernel *semakptr);
83 #ifndef _SYS_SYSPROTO_H_
85 int __semctl(struct thread *td, struct __semctl_args *uap);
87 int semget(struct thread *td, struct semget_args *uap);
89 int semop(struct thread *td, struct semop_args *uap);
92 static struct sem_undo *semu_alloc(struct thread *td);
93 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
94 int semid, int semseq, int semnum, int adjval);
95 static void semundo_clear(int semid, int semnum);
97 static struct mtx sem_mtx; /* semaphore global lock */
98 static struct mtx sem_undo_mtx;
99 static int semtot = 0;
100 static struct semid_kernel *sema; /* semaphore id pool */
101 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
102 static struct sem *sem; /* semaphore pool */
103 LIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
104 LIST_HEAD(, sem_undo) semu_free_list; /* list of free undo structures */
105 static int *semu; /* undo structure pool */
106 static eventhandler_tag semexit_tag;
108 #define SEMUNDO_MTX sem_undo_mtx
109 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
110 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
111 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
114 u_short semval; /* semaphore value */
115 pid_t sempid; /* pid of last operation */
116 u_short semncnt; /* # awaiting semval > cval */
117 u_short semzcnt; /* # awaiting semval = 0 */
121 * Undo structure (one per process)
124 LIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
125 struct proc *un_proc; /* owner of this structure */
126 short un_cnt; /* # of active entries */
128 short un_adjval; /* adjust on exit values */
129 short un_num; /* semaphore # */
130 int un_id; /* semid */
131 unsigned short un_seq;
132 } un_ent[1]; /* undo entries */
136 * Configuration parameters
139 #define SEMMNI 50 /* # of semaphore identifiers */
142 #define SEMMNS 340 /* # of semaphores in system */
145 #define SEMUME 50 /* max # of undo entries per process */
148 #define SEMMNU 150 /* # of undo structures in system */
151 /* shouldn't need tuning */
153 #define SEMMSL SEMMNS /* max # of semaphores per id */
156 #define SEMOPM 100 /* max # of operations per semop call */
159 #define SEMVMX 32767 /* semaphore maximum value */
160 #define SEMAEM 16384 /* adjust on exit max value */
163 * Due to the way semaphore memory is allocated, we have to ensure that
164 * SEMUSZ is properly aligned.
167 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
169 /* actual size of an undo structure */
170 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
173 * Macro to find a particular sem_undo vector
176 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
179 * semaphore info struct
181 struct seminfo seminfo = {
182 SEMMNI, /* # of semaphore identifiers */
183 SEMMNS, /* # of semaphores in system */
184 SEMMNU, /* # of undo structures in system */
185 SEMMSL, /* max # of semaphores per id */
186 SEMOPM, /* max # of operations per semop call */
187 SEMUME, /* max # of undo entries per process */
188 SEMUSZ, /* size in bytes of undo structure */
189 SEMVMX, /* semaphore maximum value */
190 SEMAEM /* adjust on exit max value */
193 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
194 "Number of semaphore identifiers");
195 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
196 "Maximum number of semaphores in the system");
197 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
198 "Maximum number of undo structures in the system");
199 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
200 "Max semaphores per id");
201 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
202 "Max operations per semop call");
203 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
204 "Max undo entries per process");
205 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
206 "Size in bytes of undo structure");
207 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
208 "Semaphore maximum value");
209 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
210 "Adjust on exit max value");
211 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLTYPE_OPAQUE | CTLFLAG_RD,
212 NULL, 0, sysctl_sema, "", "Semaphore id pool");
214 static struct syscall_helper_data sem_syscalls[] = {
215 SYSCALL_INIT_HELPER(__semctl),
216 SYSCALL_INIT_HELPER(semget),
217 SYSCALL_INIT_HELPER(semop),
218 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
219 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
220 SYSCALL_INIT_HELPER(semsys),
221 SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
226 #ifdef COMPAT_FREEBSD32
227 #include <compat/freebsd32/freebsd32.h>
228 #include <compat/freebsd32/freebsd32_ipc.h>
229 #include <compat/freebsd32/freebsd32_proto.h>
230 #include <compat/freebsd32/freebsd32_signal.h>
231 #include <compat/freebsd32/freebsd32_syscall.h>
232 #include <compat/freebsd32/freebsd32_util.h>
234 static struct syscall_helper_data sem32_syscalls[] = {
235 SYSCALL32_INIT_HELPER(freebsd32_semctl),
236 SYSCALL32_INIT_HELPER_COMPAT(semget),
237 SYSCALL32_INIT_HELPER_COMPAT(semop),
238 SYSCALL32_INIT_HELPER(freebsd32_semsys),
239 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
240 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
241 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
252 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
253 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
254 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
255 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
256 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
257 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
258 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
259 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
260 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
262 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
263 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
265 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
267 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
269 for (i = 0; i < seminfo.semmni; i++) {
270 sema[i].u.sem_base = 0;
271 sema[i].u.sem_perm.mode = 0;
272 sema[i].u.sem_perm.seq = 0;
274 mac_sysvsem_init(&sema[i]);
277 for (i = 0; i < seminfo.semmni; i++)
278 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
279 LIST_INIT(&semu_free_list);
280 for (i = 0; i < seminfo.semmnu; i++) {
281 struct sem_undo *suptr = SEMU(i);
282 suptr->un_proc = NULL;
283 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
285 LIST_INIT(&semu_list);
286 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
287 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
288 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
289 EVENTHANDLER_PRI_ANY);
291 error = syscall_helper_register(sem_syscalls);
294 #ifdef COMPAT_FREEBSD32
295 error = syscall32_helper_register(sem32_syscalls);
311 #ifdef COMPAT_FREEBSD32
312 syscall32_helper_unregister(sem32_syscalls);
314 syscall_helper_unregister(sem_syscalls);
315 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
317 for (i = 0; i < seminfo.semmni; i++)
318 mac_sysvsem_destroy(&sema[i]);
323 for (i = 0; i < seminfo.semmni; i++)
324 mtx_destroy(&sema_mtx[i]);
325 free(sema_mtx, M_SEM);
326 mtx_destroy(&sem_mtx);
327 mtx_destroy(&sem_undo_mtx);
332 sysvsem_modload(struct module *module, int cmd, void *arg)
354 static moduledata_t sysvsem_mod = {
360 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
361 MODULE_VERSION(sysvsem, 1);
364 * Allocate a new sem_undo structure for a process
365 * (returns ptr to structure or NULL if no more room)
368 static struct sem_undo *
369 semu_alloc(struct thread *td)
371 struct sem_undo *suptr;
373 SEMUNDO_LOCKASSERT(MA_OWNED);
374 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
376 LIST_REMOVE(suptr, un_next);
377 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
379 suptr->un_proc = td->td_proc;
384 semu_try_free(struct sem_undo *suptr)
387 SEMUNDO_LOCKASSERT(MA_OWNED);
389 if (suptr->un_cnt != 0)
391 LIST_REMOVE(suptr, un_next);
392 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
397 * Adjust a particular entry for a particular proc
401 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
402 int semseq, int semnum, int adjval)
404 struct proc *p = td->td_proc;
405 struct sem_undo *suptr;
409 SEMUNDO_LOCKASSERT(MA_OWNED);
410 /* Look for and remember the sem_undo if the caller doesn't provide
415 LIST_FOREACH(suptr, &semu_list, un_next) {
416 if (suptr->un_proc == p) {
424 suptr = semu_alloc(td);
432 * Look for the requested entry and adjust it (delete if adjval becomes
435 sunptr = &suptr->un_ent[0];
436 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
437 if (sunptr->un_id != semid || sunptr->un_num != semnum)
440 adjval += sunptr->un_adjval;
441 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
444 sunptr->un_adjval = adjval;
445 if (sunptr->un_adjval == 0) {
447 if (i < suptr->un_cnt)
449 suptr->un_ent[suptr->un_cnt];
450 if (suptr->un_cnt == 0)
451 semu_try_free(suptr);
456 /* Didn't find the right entry - create it */
459 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
461 if (suptr->un_cnt != seminfo.semume) {
462 sunptr = &suptr->un_ent[suptr->un_cnt];
464 sunptr->un_adjval = adjval;
465 sunptr->un_id = semid;
466 sunptr->un_num = semnum;
467 sunptr->un_seq = semseq;
474 semundo_clear(int semid, int semnum)
476 struct sem_undo *suptr, *suptr1;
480 SEMUNDO_LOCKASSERT(MA_OWNED);
481 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
482 sunptr = &suptr->un_ent[0];
483 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
484 if (sunptr->un_id != semid)
486 if (semnum == -1 || sunptr->un_num == semnum) {
488 if (i < suptr->un_cnt) {
490 suptr->un_ent[suptr->un_cnt];
493 semu_try_free(suptr);
502 semvalid(int semid, struct semid_kernel *semakptr)
505 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
506 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
510 * Note that the user-mode half of this passes a union, not a pointer.
512 #ifndef _SYS_SYSPROTO_H_
513 struct __semctl_args {
521 sys___semctl(struct thread *td, struct __semctl_args *uap)
523 struct semid_ds dsbuf;
524 union semun arg, semun;
535 error = copyin(uap->arg, &arg, sizeof(arg));
547 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
554 semun.array = arg.array;
561 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
569 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
574 td->td_retval[0] = rval;
579 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
580 union semun *arg, register_t *rval)
583 struct ucred *cred = td->td_ucred;
585 struct semid_ds *sbuf;
586 struct semid_kernel *semakptr;
587 struct mtx *sema_mtxp;
588 u_short usval, count;
591 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
592 semid, semnum, cmd, arg));
593 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
601 * For this command we assume semid is an array index
602 * rather than an IPC id.
604 if (semid < 0 || semid >= seminfo.semmni)
606 semakptr = &sema[semid];
607 sema_mtxp = &sema_mtx[semid];
609 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
613 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
616 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
620 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
621 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
622 mtx_unlock(sema_mtxp);
626 semidx = IPCID_TO_IX(semid);
627 if (semidx < 0 || semidx >= seminfo.semmni)
630 semakptr = &sema[semidx];
631 sema_mtxp = &sema_mtx[semidx];
636 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
646 if ((error = semvalid(semid, semakptr)) != 0)
648 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
650 semakptr->u.sem_perm.cuid = cred->cr_uid;
651 semakptr->u.sem_perm.uid = cred->cr_uid;
652 semakptr->u.sem_perm.mode = 0;
653 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
654 crfree(semakptr->cred);
655 semakptr->cred = NULL;
657 semundo_clear(semidx, -1);
660 mac_sysvsem_cleanup(semakptr);
663 for (i = 0; i < seminfo.semmni; i++) {
664 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
665 sema[i].u.sem_base > semakptr->u.sem_base)
666 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
668 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
669 sem[i] = sem[i + semakptr->u.sem_nsems];
670 for (i = 0; i < seminfo.semmni; i++) {
671 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
672 sema[i].u.sem_base > semakptr->u.sem_base) {
673 sema[i].u.sem_base -= semakptr->u.sem_nsems;
674 mtx_unlock(&sema_mtx[i]);
677 semtot -= semakptr->u.sem_nsems;
681 if ((error = semvalid(semid, semakptr)) != 0)
683 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
686 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
687 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
688 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
689 ~0777) | (sbuf->sem_perm.mode & 0777);
690 semakptr->u.sem_ctime = time_second;
694 if ((error = semvalid(semid, semakptr)) != 0)
696 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
698 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
702 if ((error = semvalid(semid, semakptr)) != 0)
704 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
706 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
710 *rval = semakptr->u.sem_base[semnum].semncnt;
714 if ((error = semvalid(semid, semakptr)) != 0)
716 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
718 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
722 *rval = semakptr->u.sem_base[semnum].sempid;
726 if ((error = semvalid(semid, semakptr)) != 0)
728 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
730 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
734 *rval = semakptr->u.sem_base[semnum].semval;
739 * Unfortunately, callers of this function don't know
740 * in advance how many semaphores are in this set.
741 * While we could just allocate the maximum size array
742 * and pass the actual size back to the caller, that
743 * won't work for SETALL since we can't copyin() more
744 * data than the user specified as we may return a
747 * Note that the number of semaphores in a set is
748 * fixed for the life of that set. The only way that
749 * the 'count' could change while are blocked in
750 * malloc() is if this semaphore set were destroyed
751 * and a new one created with the same index.
752 * However, semvalid() will catch that due to the
753 * sequence number unless exactly 0x8000 (or a
754 * multiple thereof) semaphore sets for the same index
755 * are created and destroyed while we are in malloc!
758 count = semakptr->u.sem_nsems;
759 mtx_unlock(sema_mtxp);
760 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
762 if ((error = semvalid(semid, semakptr)) != 0)
764 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
765 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
767 for (i = 0; i < semakptr->u.sem_nsems; i++)
768 array[i] = semakptr->u.sem_base[i].semval;
769 mtx_unlock(sema_mtxp);
770 error = copyout(array, arg->array, count * sizeof(*array));
775 if ((error = semvalid(semid, semakptr)) != 0)
777 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
779 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
783 *rval = semakptr->u.sem_base[semnum].semzcnt;
787 if ((error = semvalid(semid, semakptr)) != 0)
789 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
791 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
795 if (arg->val < 0 || arg->val > seminfo.semvmx) {
799 semakptr->u.sem_base[semnum].semval = arg->val;
801 semundo_clear(semidx, semnum);
808 * See comment on GETALL for why 'count' shouldn't change
809 * and why we require a userland buffer.
811 count = semakptr->u.sem_nsems;
812 mtx_unlock(sema_mtxp);
813 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
814 error = copyin(arg->array, array, count * sizeof(*array));
818 if ((error = semvalid(semid, semakptr)) != 0)
820 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
821 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
823 for (i = 0; i < semakptr->u.sem_nsems; i++) {
825 if (usval > seminfo.semvmx) {
829 semakptr->u.sem_base[i].semval = usval;
832 semundo_clear(semidx, -1);
843 mtx_unlock(sema_mtxp);
845 mtx_unlock(&sem_mtx);
851 #ifndef _SYS_SYSPROTO_H_
859 sys_semget(struct thread *td, struct semget_args *uap)
861 int semid, error = 0;
863 int nsems = uap->nsems;
864 int semflg = uap->semflg;
865 struct ucred *cred = td->td_ucred;
867 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
868 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
872 if (key != IPC_PRIVATE) {
873 for (semid = 0; semid < seminfo.semmni; semid++) {
874 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
875 sema[semid].u.sem_perm.key == key)
878 if (semid < seminfo.semmni) {
879 DPRINTF(("found public key\n"));
880 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
884 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
885 DPRINTF(("too small\n"));
889 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
890 DPRINTF(("not exclusive\n"));
895 error = mac_sysvsem_check_semget(cred, &sema[semid]);
903 DPRINTF(("need to allocate the semid_kernel\n"));
904 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
905 if (nsems <= 0 || nsems > seminfo.semmsl) {
906 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
911 if (nsems > seminfo.semmns - semtot) {
913 "not enough semaphores left (need %d, got %d)\n",
914 nsems, seminfo.semmns - semtot));
918 for (semid = 0; semid < seminfo.semmni; semid++) {
919 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
922 if (semid == seminfo.semmni) {
923 DPRINTF(("no more semid_kernel's available\n"));
928 PROC_LOCK(td->td_proc);
929 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
930 PROC_UNLOCK(td->td_proc);
936 DPRINTF(("semid %d is available\n", semid));
937 mtx_lock(&sema_mtx[semid]);
938 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
939 ("Lost semaphore %d", semid));
940 sema[semid].u.sem_perm.key = key;
941 sema[semid].u.sem_perm.cuid = cred->cr_uid;
942 sema[semid].u.sem_perm.uid = cred->cr_uid;
943 sema[semid].u.sem_perm.cgid = cred->cr_gid;
944 sema[semid].u.sem_perm.gid = cred->cr_gid;
945 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
946 sema[semid].cred = crhold(cred);
947 sema[semid].u.sem_perm.seq =
948 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
949 sema[semid].u.sem_nsems = nsems;
950 sema[semid].u.sem_otime = 0;
951 sema[semid].u.sem_ctime = time_second;
952 sema[semid].u.sem_base = &sem[semtot];
954 bzero(sema[semid].u.sem_base,
955 sizeof(sema[semid].u.sem_base[0])*nsems);
957 mac_sysvsem_create(cred, &sema[semid]);
959 mtx_unlock(&sema_mtx[semid]);
960 DPRINTF(("sembase = %p, next = %p\n",
961 sema[semid].u.sem_base, &sem[semtot]));
963 DPRINTF(("didn't find it and wasn't asked to create it\n"));
969 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
971 mtx_unlock(&sem_mtx);
975 #ifndef _SYS_SYSPROTO_H_
983 sys_semop(struct thread *td, struct semop_args *uap)
986 struct sembuf small_sops[SMALL_SOPS];
987 int semid = uap->semid;
988 size_t nsops = uap->nsops;
990 struct semid_kernel *semakptr;
991 struct sembuf *sopptr = 0;
992 struct sem *semptr = 0;
993 struct sem_undo *suptr;
994 struct mtx *sema_mtxp;
997 int do_wakeup, do_undos;
1003 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1005 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1008 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1010 if (semid < 0 || semid >= seminfo.semmni)
1013 /* Allocate memory for sem_ops */
1014 if (nsops <= SMALL_SOPS)
1016 else if (nsops > seminfo.semopm) {
1017 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1022 PROC_LOCK(td->td_proc);
1023 if (nsops > racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1024 PROC_UNLOCK(td->td_proc);
1027 PROC_UNLOCK(td->td_proc);
1030 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1032 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1033 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1034 uap->sops, sops, nsops * sizeof(sops[0])));
1035 if (sops != small_sops)
1040 semakptr = &sema[semid];
1041 sema_mtxp = &sema_mtx[semid];
1042 mtx_lock(sema_mtxp);
1043 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1047 seq = semakptr->u.sem_perm.seq;
1048 if (seq != IPCID_TO_SEQ(uap->semid)) {
1053 * Initial pass thru sops to see what permissions are needed.
1054 * Also perform any checks that don't need repeating on each
1055 * attempt to satisfy the request vector.
1057 j = 0; /* permission needed */
1059 for (i = 0; i < nsops; i++) {
1061 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1065 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1067 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1070 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1071 DPRINTF(("error = %d from ipaccess\n", error));
1075 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1081 * Loop trying to satisfy the vector of requests.
1082 * If we reach a point where we must wait, any requests already
1083 * performed are rolled back and we go to sleep until some other
1084 * process wakes us up. At this point, we start all over again.
1086 * This ensures that from the perspective of other tasks, a set
1087 * of requests is atomic (never partially satisfied).
1091 error = 0; /* error return if necessary */
1093 for (i = 0; i < nsops; i++) {
1095 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1098 "semop: semakptr=%p, sem_base=%p, "
1099 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1100 semakptr, semakptr->u.sem_base, semptr,
1101 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1102 (sopptr->sem_flg & IPC_NOWAIT) ?
1103 "nowait" : "wait"));
1105 if (sopptr->sem_op < 0) {
1106 if (semptr->semval + sopptr->sem_op < 0) {
1107 DPRINTF(("semop: can't do it now\n"));
1110 semptr->semval += sopptr->sem_op;
1111 if (semptr->semval == 0 &&
1112 semptr->semzcnt > 0)
1115 } else if (sopptr->sem_op == 0) {
1116 if (semptr->semval != 0) {
1117 DPRINTF(("semop: not zero now\n"));
1120 } else if (semptr->semval + sopptr->sem_op >
1125 if (semptr->semncnt > 0)
1127 semptr->semval += sopptr->sem_op;
1132 * Did we get through the entire vector?
1138 * No ... rollback anything that we've already done
1140 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1141 for (j = 0; j < i; j++)
1142 semakptr->u.sem_base[sops[j].sem_num].semval -=
1145 /* If we detected an error, return it */
1150 * If the request that we couldn't satisfy has the
1151 * NOWAIT flag set then return with EAGAIN.
1153 if (sopptr->sem_flg & IPC_NOWAIT) {
1158 if (sopptr->sem_op == 0)
1163 DPRINTF(("semop: good night!\n"));
1164 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1166 DPRINTF(("semop: good morning (error=%d)!\n", error));
1167 /* return code is checked below, after sem[nz]cnt-- */
1170 * Make sure that the semaphore still exists
1172 seq = semakptr->u.sem_perm.seq;
1173 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1174 seq != IPCID_TO_SEQ(uap->semid)) {
1180 * Renew the semaphore's pointer after wakeup since
1181 * during msleep sem_base may have been modified and semptr
1182 * is not valid any more
1184 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1187 * The semaphore is still alive. Readjust the count of
1188 * waiting processes.
1190 if (sopptr->sem_op == 0)
1196 * Is it really morning, or was our sleep interrupted?
1197 * (Delayed check of msleep() return code because we
1198 * need to decrement sem[nz]cnt either way.)
1204 DPRINTF(("semop: good morning!\n"));
1209 * Process any SEM_UNDO requests.
1214 for (i = 0; i < nsops; i++) {
1216 * We only need to deal with SEM_UNDO's for non-zero
1221 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1223 adjval = sops[i].sem_op;
1226 error = semundo_adjust(td, &suptr, semid, seq,
1227 sops[i].sem_num, -adjval);
1232 * Oh-Oh! We ran out of either sem_undo's or undo's.
1233 * Rollback the adjustments to this point and then
1234 * rollback the semaphore ups and down so we can return
1235 * with an error with all structures restored. We
1236 * rollback the undo's in the exact reverse order that
1237 * we applied them. This guarantees that we won't run
1238 * out of space as we roll things back out.
1240 for (j = 0; j < i; j++) {
1242 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1244 adjval = sops[k].sem_op;
1247 if (semundo_adjust(td, &suptr, semid, seq,
1248 sops[k].sem_num, adjval) != 0)
1249 panic("semop - can't undo undos");
1252 for (j = 0; j < nsops; j++)
1253 semakptr->u.sem_base[sops[j].sem_num].semval -=
1256 DPRINTF(("error = %d from semundo_adjust\n", error));
1259 } /* loop through the sops */
1261 } /* if (do_undos) */
1263 /* We're definitely done - set the sempid's and time */
1264 for (i = 0; i < nsops; i++) {
1266 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1267 semptr->sempid = td->td_proc->p_pid;
1269 semakptr->u.sem_otime = time_second;
1272 * Do a wakeup if any semaphore was up'd whilst something was
1276 DPRINTF(("semop: doing wakeup\n"));
1278 DPRINTF(("semop: back from wakeup\n"));
1280 DPRINTF(("semop: done\n"));
1281 td->td_retval[0] = 0;
1283 mtx_unlock(sema_mtxp);
1284 if (sops != small_sops)
1290 * Go through the undo structures for this process and apply the adjustments to
1294 semexit_myhook(void *arg, struct proc *p)
1296 struct sem_undo *suptr;
1297 struct semid_kernel *semakptr;
1298 struct mtx *sema_mtxp;
1299 int semid, semnum, adjval, ix;
1303 * Go through the chain of undo vectors looking for one
1304 * associated with this process.
1307 LIST_FOREACH(suptr, &semu_list, un_next) {
1308 if (suptr->un_proc == p)
1311 if (suptr == NULL) {
1315 LIST_REMOVE(suptr, un_next);
1317 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1321 * If there are any active undo elements then process them.
1323 if (suptr->un_cnt > 0) {
1325 for (ix = 0; ix < suptr->un_cnt; ix++) {
1326 semid = suptr->un_ent[ix].un_id;
1327 semnum = suptr->un_ent[ix].un_num;
1328 adjval = suptr->un_ent[ix].un_adjval;
1329 seq = suptr->un_ent[ix].un_seq;
1330 semakptr = &sema[semid];
1331 sema_mtxp = &sema_mtx[semid];
1333 mtx_lock(sema_mtxp);
1334 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1335 (semakptr->u.sem_perm.seq != seq)) {
1336 mtx_unlock(sema_mtxp);
1339 if (semnum >= semakptr->u.sem_nsems)
1340 panic("semexit - semnum out of range");
1343 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1344 suptr->un_proc, suptr->un_ent[ix].un_id,
1345 suptr->un_ent[ix].un_num,
1346 suptr->un_ent[ix].un_adjval,
1347 semakptr->u.sem_base[semnum].semval));
1349 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1351 semakptr->u.sem_base[semnum].semval = 0;
1353 semakptr->u.sem_base[semnum].semval += adjval;
1356 DPRINTF(("semexit: back from wakeup\n"));
1357 mtx_unlock(sema_mtxp);
1363 * Deallocate the undo vector.
1365 DPRINTF(("removing vector\n"));
1366 suptr->un_proc = NULL;
1368 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1373 sysctl_sema(SYSCTL_HANDLER_ARGS)
1376 return (SYSCTL_OUT(req, sema,
1377 sizeof(struct semid_kernel) * seminfo.semmni));
1380 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1381 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1383 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1384 static sy_call_t *semcalls[] = {
1385 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1386 (sy_call_t *)sys_semop
1390 * Entry point for all SEM calls.
1395 /* XXX actually varargs. */
1396 struct semsys_args /* {
1406 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1408 if (uap->which < 0 ||
1409 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1411 error = (*semcalls[uap->which])(td, &uap->a2);
1416 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1419 #ifndef _SYS_SYSPROTO_H_
1420 struct freebsd7___semctl_args {
1424 union semun_old *arg;
1428 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1430 struct semid_ds_old dsold;
1431 struct semid_ds dsbuf;
1432 union semun_old arg;
1444 error = copyin(uap->arg, &arg, sizeof(arg));
1456 error = copyin(arg.buf, &dsold, sizeof(dsold));
1459 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1460 CP(dsold, dsbuf, sem_base);
1461 CP(dsold, dsbuf, sem_nsems);
1462 CP(dsold, dsbuf, sem_otime);
1463 CP(dsold, dsbuf, sem_ctime);
1468 semun.array = arg.array;
1471 semun.val = arg.val;
1475 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1483 bzero(&dsold, sizeof(dsold));
1484 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1485 CP(dsbuf, dsold, sem_base);
1486 CP(dsbuf, dsold, sem_nsems);
1487 CP(dsbuf, dsold, sem_otime);
1488 CP(dsbuf, dsold, sem_ctime);
1489 error = copyout(&dsold, arg.buf, sizeof(dsold));
1494 td->td_retval[0] = rval;
1498 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1500 #ifdef COMPAT_FREEBSD32
1503 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1506 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1507 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1508 switch (uap->which) {
1510 return (freebsd7_freebsd32_semctl(td,
1511 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1513 return (sys_semsys(td, (struct semsys_args *)uap));
1516 return (nosys(td, NULL));
1520 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1521 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1523 freebsd7_freebsd32_semctl(struct thread *td,
1524 struct freebsd7_freebsd32_semctl_args *uap)
1526 struct semid_ds32_old dsbuf32;
1527 struct semid_ds dsbuf;
1540 error = copyin(uap->arg, &arg, sizeof(arg));
1552 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1555 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1556 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1557 CP(dsbuf32, dsbuf, sem_nsems);
1558 CP(dsbuf32, dsbuf, sem_otime);
1559 CP(dsbuf32, dsbuf, sem_ctime);
1564 semun.array = PTRIN(arg.array);
1567 semun.val = arg.val;
1571 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1579 bzero(&dsbuf32, sizeof(dsbuf32));
1580 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1581 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1582 CP(dsbuf, dsbuf32, sem_nsems);
1583 CP(dsbuf, dsbuf32, sem_otime);
1584 CP(dsbuf, dsbuf32, sem_ctime);
1585 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1590 td->td_retval[0] = rval;
1596 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1598 struct semid_ds32 dsbuf32;
1599 struct semid_ds dsbuf;
1612 error = copyin(uap->arg, &arg, sizeof(arg));
1624 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1627 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1628 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1629 CP(dsbuf32, dsbuf, sem_nsems);
1630 CP(dsbuf32, dsbuf, sem_otime);
1631 CP(dsbuf32, dsbuf, sem_ctime);
1636 semun.array = PTRIN(arg.array);
1639 semun.val = arg.val;
1643 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1651 bzero(&dsbuf32, sizeof(dsbuf32));
1652 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1653 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1654 CP(dsbuf, dsbuf32, sem_nsems);
1655 CP(dsbuf, dsbuf32, sem_otime);
1656 CP(dsbuf, dsbuf32, sem_ctime);
1657 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1662 td->td_retval[0] = rval;
1666 #endif /* COMPAT_FREEBSD32 */