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_RWTUN, &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_RWTUN, &seminfo.semvmx, 0,
208 "Semaphore maximum value");
209 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &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 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
253 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
255 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
257 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
259 for (i = 0; i < seminfo.semmni; i++) {
260 sema[i].u.sem_base = 0;
261 sema[i].u.sem_perm.mode = 0;
262 sema[i].u.sem_perm.seq = 0;
264 mac_sysvsem_init(&sema[i]);
267 for (i = 0; i < seminfo.semmni; i++)
268 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
269 LIST_INIT(&semu_free_list);
270 for (i = 0; i < seminfo.semmnu; i++) {
271 struct sem_undo *suptr = SEMU(i);
272 suptr->un_proc = NULL;
273 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
275 LIST_INIT(&semu_list);
276 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
277 mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
278 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
279 EVENTHANDLER_PRI_ANY);
281 error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
284 #ifdef COMPAT_FREEBSD32
285 error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
301 #ifdef COMPAT_FREEBSD32
302 syscall32_helper_unregister(sem32_syscalls);
304 syscall_helper_unregister(sem_syscalls);
305 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
307 for (i = 0; i < seminfo.semmni; i++)
308 mac_sysvsem_destroy(&sema[i]);
313 for (i = 0; i < seminfo.semmni; i++)
314 mtx_destroy(&sema_mtx[i]);
315 free(sema_mtx, M_SEM);
316 mtx_destroy(&sem_mtx);
317 mtx_destroy(&sem_undo_mtx);
322 sysvsem_modload(struct module *module, int cmd, void *arg)
344 static moduledata_t sysvsem_mod = {
350 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
351 MODULE_VERSION(sysvsem, 1);
354 * Allocate a new sem_undo structure for a process
355 * (returns ptr to structure or NULL if no more room)
358 static struct sem_undo *
359 semu_alloc(struct thread *td)
361 struct sem_undo *suptr;
363 SEMUNDO_LOCKASSERT(MA_OWNED);
364 if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
366 LIST_REMOVE(suptr, un_next);
367 LIST_INSERT_HEAD(&semu_list, suptr, un_next);
369 suptr->un_proc = td->td_proc;
374 semu_try_free(struct sem_undo *suptr)
377 SEMUNDO_LOCKASSERT(MA_OWNED);
379 if (suptr->un_cnt != 0)
381 LIST_REMOVE(suptr, un_next);
382 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
387 * Adjust a particular entry for a particular proc
391 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
392 int semseq, int semnum, int adjval)
394 struct proc *p = td->td_proc;
395 struct sem_undo *suptr;
399 SEMUNDO_LOCKASSERT(MA_OWNED);
400 /* Look for and remember the sem_undo if the caller doesn't provide
405 LIST_FOREACH(suptr, &semu_list, un_next) {
406 if (suptr->un_proc == p) {
414 suptr = semu_alloc(td);
422 * Look for the requested entry and adjust it (delete if adjval becomes
425 sunptr = &suptr->un_ent[0];
426 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
427 if (sunptr->un_id != semid || sunptr->un_num != semnum)
430 adjval += sunptr->un_adjval;
431 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
434 sunptr->un_adjval = adjval;
435 if (sunptr->un_adjval == 0) {
437 if (i < suptr->un_cnt)
439 suptr->un_ent[suptr->un_cnt];
440 if (suptr->un_cnt == 0)
441 semu_try_free(suptr);
446 /* Didn't find the right entry - create it */
449 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
451 if (suptr->un_cnt != seminfo.semume) {
452 sunptr = &suptr->un_ent[suptr->un_cnt];
454 sunptr->un_adjval = adjval;
455 sunptr->un_id = semid;
456 sunptr->un_num = semnum;
457 sunptr->un_seq = semseq;
464 semundo_clear(int semid, int semnum)
466 struct sem_undo *suptr, *suptr1;
470 SEMUNDO_LOCKASSERT(MA_OWNED);
471 LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
472 sunptr = &suptr->un_ent[0];
473 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
474 if (sunptr->un_id != semid)
476 if (semnum == -1 || sunptr->un_num == semnum) {
478 if (i < suptr->un_cnt) {
480 suptr->un_ent[suptr->un_cnt];
483 semu_try_free(suptr);
492 semvalid(int semid, struct semid_kernel *semakptr)
495 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
496 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
500 * Note that the user-mode half of this passes a union, not a pointer.
502 #ifndef _SYS_SYSPROTO_H_
503 struct __semctl_args {
511 sys___semctl(struct thread *td, struct __semctl_args *uap)
513 struct semid_ds dsbuf;
514 union semun arg, semun;
525 error = copyin(uap->arg, &arg, sizeof(arg));
537 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
544 semun.array = arg.array;
551 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
559 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
564 td->td_retval[0] = rval;
569 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
570 union semun *arg, register_t *rval)
573 struct ucred *cred = td->td_ucred;
575 struct semid_ds *sbuf;
576 struct semid_kernel *semakptr;
577 struct mtx *sema_mtxp;
578 u_short usval, count;
581 DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
582 semid, semnum, cmd, arg));
583 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
591 * For this command we assume semid is an array index
592 * rather than an IPC id.
594 if (semid < 0 || semid >= seminfo.semmni)
596 semakptr = &sema[semid];
597 sema_mtxp = &sema_mtx[semid];
599 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
603 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
606 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
610 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
611 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
612 mtx_unlock(sema_mtxp);
616 semidx = IPCID_TO_IX(semid);
617 if (semidx < 0 || semidx >= seminfo.semmni)
620 semakptr = &sema[semidx];
621 sema_mtxp = &sema_mtx[semidx];
626 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
636 if ((error = semvalid(semid, semakptr)) != 0)
638 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
640 semakptr->u.sem_perm.cuid = cred->cr_uid;
641 semakptr->u.sem_perm.uid = cred->cr_uid;
642 semakptr->u.sem_perm.mode = 0;
643 racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
644 crfree(semakptr->cred);
645 semakptr->cred = NULL;
647 semundo_clear(semidx, -1);
650 mac_sysvsem_cleanup(semakptr);
653 for (i = 0; i < seminfo.semmni; i++) {
654 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
655 sema[i].u.sem_base > semakptr->u.sem_base)
656 mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
658 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
659 sem[i] = sem[i + semakptr->u.sem_nsems];
660 for (i = 0; i < seminfo.semmni; i++) {
661 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
662 sema[i].u.sem_base > semakptr->u.sem_base) {
663 sema[i].u.sem_base -= semakptr->u.sem_nsems;
664 mtx_unlock(&sema_mtx[i]);
667 semtot -= semakptr->u.sem_nsems;
671 if ((error = semvalid(semid, semakptr)) != 0)
673 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
676 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
677 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
678 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
679 ~0777) | (sbuf->sem_perm.mode & 0777);
680 semakptr->u.sem_ctime = time_second;
684 if ((error = semvalid(semid, semakptr)) != 0)
686 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
688 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
692 if ((error = semvalid(semid, semakptr)) != 0)
694 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
696 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
700 *rval = semakptr->u.sem_base[semnum].semncnt;
704 if ((error = semvalid(semid, semakptr)) != 0)
706 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
708 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
712 *rval = semakptr->u.sem_base[semnum].sempid;
716 if ((error = semvalid(semid, semakptr)) != 0)
718 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
720 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
724 *rval = semakptr->u.sem_base[semnum].semval;
729 * Unfortunately, callers of this function don't know
730 * in advance how many semaphores are in this set.
731 * While we could just allocate the maximum size array
732 * and pass the actual size back to the caller, that
733 * won't work for SETALL since we can't copyin() more
734 * data than the user specified as we may return a
737 * Note that the number of semaphores in a set is
738 * fixed for the life of that set. The only way that
739 * the 'count' could change while are blocked in
740 * malloc() is if this semaphore set were destroyed
741 * and a new one created with the same index.
742 * However, semvalid() will catch that due to the
743 * sequence number unless exactly 0x8000 (or a
744 * multiple thereof) semaphore sets for the same index
745 * are created and destroyed while we are in malloc!
748 count = semakptr->u.sem_nsems;
749 mtx_unlock(sema_mtxp);
750 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
752 if ((error = semvalid(semid, semakptr)) != 0)
754 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
755 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
757 for (i = 0; i < semakptr->u.sem_nsems; i++)
758 array[i] = semakptr->u.sem_base[i].semval;
759 mtx_unlock(sema_mtxp);
760 error = copyout(array, arg->array, count * sizeof(*array));
765 if ((error = semvalid(semid, semakptr)) != 0)
767 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
769 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
773 *rval = semakptr->u.sem_base[semnum].semzcnt;
777 if ((error = semvalid(semid, semakptr)) != 0)
779 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
781 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
785 if (arg->val < 0 || arg->val > seminfo.semvmx) {
789 semakptr->u.sem_base[semnum].semval = arg->val;
791 semundo_clear(semidx, semnum);
798 * See comment on GETALL for why 'count' shouldn't change
799 * and why we require a userland buffer.
801 count = semakptr->u.sem_nsems;
802 mtx_unlock(sema_mtxp);
803 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
804 error = copyin(arg->array, array, count * sizeof(*array));
808 if ((error = semvalid(semid, semakptr)) != 0)
810 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
811 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
813 for (i = 0; i < semakptr->u.sem_nsems; i++) {
815 if (usval > seminfo.semvmx) {
819 semakptr->u.sem_base[i].semval = usval;
822 semundo_clear(semidx, -1);
833 mtx_unlock(sema_mtxp);
835 mtx_unlock(&sem_mtx);
841 #ifndef _SYS_SYSPROTO_H_
849 sys_semget(struct thread *td, struct semget_args *uap)
851 int semid, error = 0;
853 int nsems = uap->nsems;
854 int semflg = uap->semflg;
855 struct ucred *cred = td->td_ucred;
857 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
858 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
862 if (key != IPC_PRIVATE) {
863 for (semid = 0; semid < seminfo.semmni; semid++) {
864 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
865 sema[semid].u.sem_perm.key == key)
868 if (semid < seminfo.semmni) {
869 DPRINTF(("found public key\n"));
870 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
874 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
875 DPRINTF(("too small\n"));
879 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
880 DPRINTF(("not exclusive\n"));
885 error = mac_sysvsem_check_semget(cred, &sema[semid]);
893 DPRINTF(("need to allocate the semid_kernel\n"));
894 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
895 if (nsems <= 0 || nsems > seminfo.semmsl) {
896 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
901 if (nsems > seminfo.semmns - semtot) {
903 "not enough semaphores left (need %d, got %d)\n",
904 nsems, seminfo.semmns - semtot));
908 for (semid = 0; semid < seminfo.semmni; semid++) {
909 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
912 if (semid == seminfo.semmni) {
913 DPRINTF(("no more semid_kernel's available\n"));
918 PROC_LOCK(td->td_proc);
919 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
920 PROC_UNLOCK(td->td_proc);
926 DPRINTF(("semid %d is available\n", semid));
927 mtx_lock(&sema_mtx[semid]);
928 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
929 ("Lost semaphore %d", semid));
930 sema[semid].u.sem_perm.key = key;
931 sema[semid].u.sem_perm.cuid = cred->cr_uid;
932 sema[semid].u.sem_perm.uid = cred->cr_uid;
933 sema[semid].u.sem_perm.cgid = cred->cr_gid;
934 sema[semid].u.sem_perm.gid = cred->cr_gid;
935 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
936 sema[semid].cred = crhold(cred);
937 sema[semid].u.sem_perm.seq =
938 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
939 sema[semid].u.sem_nsems = nsems;
940 sema[semid].u.sem_otime = 0;
941 sema[semid].u.sem_ctime = time_second;
942 sema[semid].u.sem_base = &sem[semtot];
944 bzero(sema[semid].u.sem_base,
945 sizeof(sema[semid].u.sem_base[0])*nsems);
947 mac_sysvsem_create(cred, &sema[semid]);
949 mtx_unlock(&sema_mtx[semid]);
950 DPRINTF(("sembase = %p, next = %p\n",
951 sema[semid].u.sem_base, &sem[semtot]));
953 DPRINTF(("didn't find it and wasn't asked to create it\n"));
959 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
961 mtx_unlock(&sem_mtx);
965 #ifndef _SYS_SYSPROTO_H_
973 sys_semop(struct thread *td, struct semop_args *uap)
976 struct sembuf small_sops[SMALL_SOPS];
977 int semid = uap->semid;
978 size_t nsops = uap->nsops;
980 struct semid_kernel *semakptr;
981 struct sembuf *sopptr = 0;
982 struct sem *semptr = 0;
983 struct sem_undo *suptr;
984 struct mtx *sema_mtxp;
987 int do_wakeup, do_undos;
993 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
995 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
998 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1000 if (semid < 0 || semid >= seminfo.semmni)
1003 /* Allocate memory for sem_ops */
1004 if (nsops <= SMALL_SOPS)
1006 else if (nsops > seminfo.semopm) {
1007 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1012 PROC_LOCK(td->td_proc);
1013 if (nsops > racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1014 PROC_UNLOCK(td->td_proc);
1017 PROC_UNLOCK(td->td_proc);
1020 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1022 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1023 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1024 uap->sops, sops, nsops * sizeof(sops[0])));
1025 if (sops != small_sops)
1030 semakptr = &sema[semid];
1031 sema_mtxp = &sema_mtx[semid];
1032 mtx_lock(sema_mtxp);
1033 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1037 seq = semakptr->u.sem_perm.seq;
1038 if (seq != IPCID_TO_SEQ(uap->semid)) {
1043 * Initial pass thru sops to see what permissions are needed.
1044 * Also perform any checks that don't need repeating on each
1045 * attempt to satisfy the request vector.
1047 j = 0; /* permission needed */
1049 for (i = 0; i < nsops; i++) {
1051 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1055 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1057 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1060 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1061 DPRINTF(("error = %d from ipaccess\n", error));
1065 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1071 * Loop trying to satisfy the vector of requests.
1072 * If we reach a point where we must wait, any requests already
1073 * performed are rolled back and we go to sleep until some other
1074 * process wakes us up. At this point, we start all over again.
1076 * This ensures that from the perspective of other tasks, a set
1077 * of requests is atomic (never partially satisfied).
1081 error = 0; /* error return if necessary */
1083 for (i = 0; i < nsops; i++) {
1085 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1088 "semop: semakptr=%p, sem_base=%p, "
1089 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1090 semakptr, semakptr->u.sem_base, semptr,
1091 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1092 (sopptr->sem_flg & IPC_NOWAIT) ?
1093 "nowait" : "wait"));
1095 if (sopptr->sem_op < 0) {
1096 if (semptr->semval + sopptr->sem_op < 0) {
1097 DPRINTF(("semop: can't do it now\n"));
1100 semptr->semval += sopptr->sem_op;
1101 if (semptr->semval == 0 &&
1102 semptr->semzcnt > 0)
1105 } else if (sopptr->sem_op == 0) {
1106 if (semptr->semval != 0) {
1107 DPRINTF(("semop: not zero now\n"));
1110 } else if (semptr->semval + sopptr->sem_op >
1115 if (semptr->semncnt > 0)
1117 semptr->semval += sopptr->sem_op;
1122 * Did we get through the entire vector?
1128 * No ... rollback anything that we've already done
1130 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1131 for (j = 0; j < i; j++)
1132 semakptr->u.sem_base[sops[j].sem_num].semval -=
1135 /* If we detected an error, return it */
1140 * If the request that we couldn't satisfy has the
1141 * NOWAIT flag set then return with EAGAIN.
1143 if (sopptr->sem_flg & IPC_NOWAIT) {
1148 if (sopptr->sem_op == 0)
1153 DPRINTF(("semop: good night!\n"));
1154 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1156 DPRINTF(("semop: good morning (error=%d)!\n", error));
1157 /* return code is checked below, after sem[nz]cnt-- */
1160 * Make sure that the semaphore still exists
1162 seq = semakptr->u.sem_perm.seq;
1163 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1164 seq != IPCID_TO_SEQ(uap->semid)) {
1170 * Renew the semaphore's pointer after wakeup since
1171 * during msleep sem_base may have been modified and semptr
1172 * is not valid any more
1174 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1177 * The semaphore is still alive. Readjust the count of
1178 * waiting processes.
1180 if (sopptr->sem_op == 0)
1186 * Is it really morning, or was our sleep interrupted?
1187 * (Delayed check of msleep() return code because we
1188 * need to decrement sem[nz]cnt either way.)
1194 DPRINTF(("semop: good morning!\n"));
1199 * Process any SEM_UNDO requests.
1204 for (i = 0; i < nsops; i++) {
1206 * We only need to deal with SEM_UNDO's for non-zero
1211 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1213 adjval = sops[i].sem_op;
1216 error = semundo_adjust(td, &suptr, semid, seq,
1217 sops[i].sem_num, -adjval);
1222 * Oh-Oh! We ran out of either sem_undo's or undo's.
1223 * Rollback the adjustments to this point and then
1224 * rollback the semaphore ups and down so we can return
1225 * with an error with all structures restored. We
1226 * rollback the undo's in the exact reverse order that
1227 * we applied them. This guarantees that we won't run
1228 * out of space as we roll things back out.
1230 for (j = 0; j < i; j++) {
1232 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1234 adjval = sops[k].sem_op;
1237 if (semundo_adjust(td, &suptr, semid, seq,
1238 sops[k].sem_num, adjval) != 0)
1239 panic("semop - can't undo undos");
1242 for (j = 0; j < nsops; j++)
1243 semakptr->u.sem_base[sops[j].sem_num].semval -=
1246 DPRINTF(("error = %d from semundo_adjust\n", error));
1249 } /* loop through the sops */
1251 } /* if (do_undos) */
1253 /* We're definitely done - set the sempid's and time */
1254 for (i = 0; i < nsops; i++) {
1256 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1257 semptr->sempid = td->td_proc->p_pid;
1259 semakptr->u.sem_otime = time_second;
1262 * Do a wakeup if any semaphore was up'd whilst something was
1266 DPRINTF(("semop: doing wakeup\n"));
1268 DPRINTF(("semop: back from wakeup\n"));
1270 DPRINTF(("semop: done\n"));
1271 td->td_retval[0] = 0;
1273 mtx_unlock(sema_mtxp);
1274 if (sops != small_sops)
1280 * Go through the undo structures for this process and apply the adjustments to
1284 semexit_myhook(void *arg, struct proc *p)
1286 struct sem_undo *suptr;
1287 struct semid_kernel *semakptr;
1288 struct mtx *sema_mtxp;
1289 int semid, semnum, adjval, ix;
1293 * Go through the chain of undo vectors looking for one
1294 * associated with this process.
1297 LIST_FOREACH(suptr, &semu_list, un_next) {
1298 if (suptr->un_proc == p)
1301 if (suptr == NULL) {
1305 LIST_REMOVE(suptr, un_next);
1307 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1311 * If there are any active undo elements then process them.
1313 if (suptr->un_cnt > 0) {
1315 for (ix = 0; ix < suptr->un_cnt; ix++) {
1316 semid = suptr->un_ent[ix].un_id;
1317 semnum = suptr->un_ent[ix].un_num;
1318 adjval = suptr->un_ent[ix].un_adjval;
1319 seq = suptr->un_ent[ix].un_seq;
1320 semakptr = &sema[semid];
1321 sema_mtxp = &sema_mtx[semid];
1323 mtx_lock(sema_mtxp);
1324 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1325 (semakptr->u.sem_perm.seq != seq)) {
1326 mtx_unlock(sema_mtxp);
1329 if (semnum >= semakptr->u.sem_nsems)
1330 panic("semexit - semnum out of range");
1333 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1334 suptr->un_proc, suptr->un_ent[ix].un_id,
1335 suptr->un_ent[ix].un_num,
1336 suptr->un_ent[ix].un_adjval,
1337 semakptr->u.sem_base[semnum].semval));
1339 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1341 semakptr->u.sem_base[semnum].semval = 0;
1343 semakptr->u.sem_base[semnum].semval += adjval;
1346 DPRINTF(("semexit: back from wakeup\n"));
1347 mtx_unlock(sema_mtxp);
1353 * Deallocate the undo vector.
1355 DPRINTF(("removing vector\n"));
1356 suptr->un_proc = NULL;
1358 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1363 sysctl_sema(SYSCTL_HANDLER_ARGS)
1366 return (SYSCTL_OUT(req, sema,
1367 sizeof(struct semid_kernel) * seminfo.semmni));
1370 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1371 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1373 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1374 static sy_call_t *semcalls[] = {
1375 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1376 (sy_call_t *)sys_semop
1380 * Entry point for all SEM calls.
1385 /* XXX actually varargs. */
1386 struct semsys_args /* {
1396 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1398 if (uap->which < 0 ||
1399 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1401 error = (*semcalls[uap->which])(td, &uap->a2);
1406 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1409 #ifndef _SYS_SYSPROTO_H_
1410 struct freebsd7___semctl_args {
1414 union semun_old *arg;
1418 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1420 struct semid_ds_old dsold;
1421 struct semid_ds dsbuf;
1422 union semun_old arg;
1434 error = copyin(uap->arg, &arg, sizeof(arg));
1446 error = copyin(arg.buf, &dsold, sizeof(dsold));
1449 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1450 CP(dsold, dsbuf, sem_base);
1451 CP(dsold, dsbuf, sem_nsems);
1452 CP(dsold, dsbuf, sem_otime);
1453 CP(dsold, dsbuf, sem_ctime);
1458 semun.array = arg.array;
1461 semun.val = arg.val;
1465 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1473 bzero(&dsold, sizeof(dsold));
1474 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1475 CP(dsbuf, dsold, sem_base);
1476 CP(dsbuf, dsold, sem_nsems);
1477 CP(dsbuf, dsold, sem_otime);
1478 CP(dsbuf, dsold, sem_ctime);
1479 error = copyout(&dsold, arg.buf, sizeof(dsold));
1484 td->td_retval[0] = rval;
1488 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1490 #ifdef COMPAT_FREEBSD32
1493 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1496 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1497 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1498 switch (uap->which) {
1500 return (freebsd7_freebsd32_semctl(td,
1501 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1503 return (sys_semsys(td, (struct semsys_args *)uap));
1506 return (nosys(td, NULL));
1510 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1511 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1513 freebsd7_freebsd32_semctl(struct thread *td,
1514 struct freebsd7_freebsd32_semctl_args *uap)
1516 struct semid_ds32_old dsbuf32;
1517 struct semid_ds dsbuf;
1530 error = copyin(uap->arg, &arg, sizeof(arg));
1542 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1545 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1546 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1547 CP(dsbuf32, dsbuf, sem_nsems);
1548 CP(dsbuf32, dsbuf, sem_otime);
1549 CP(dsbuf32, dsbuf, sem_ctime);
1554 semun.array = PTRIN(arg.array);
1557 semun.val = arg.val;
1561 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1569 bzero(&dsbuf32, sizeof(dsbuf32));
1570 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1571 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1572 CP(dsbuf, dsbuf32, sem_nsems);
1573 CP(dsbuf, dsbuf32, sem_otime);
1574 CP(dsbuf, dsbuf32, sem_ctime);
1575 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1580 td->td_retval[0] = rval;
1586 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1588 struct semid_ds32 dsbuf32;
1589 struct semid_ds dsbuf;
1602 error = copyin(uap->arg, &arg, sizeof(arg));
1614 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1617 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1618 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1619 CP(dsbuf32, dsbuf, sem_nsems);
1620 CP(dsbuf32, dsbuf, sem_otime);
1621 CP(dsbuf32, dsbuf, sem_ctime);
1626 semun.array = PTRIN(arg.array);
1629 semun.val = arg.val;
1633 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1641 bzero(&dsbuf32, sizeof(dsbuf32));
1642 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1643 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1644 CP(dsbuf, dsbuf32, sem_nsems);
1645 CP(dsbuf, dsbuf32, sem_otime);
1646 CP(dsbuf, dsbuf32, sem_ctime);
1647 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1652 td->td_retval[0] = rval;
1656 #endif /* COMPAT_FREEBSD32 */