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"));
929 PROC_LOCK(td->td_proc);
930 error = racct_add(td->td_proc, RACCT_NSEM, nsems);
931 PROC_UNLOCK(td->td_proc);
938 DPRINTF(("semid %d is available\n", semid));
939 mtx_lock(&sema_mtx[semid]);
940 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
941 ("Lost semaphore %d", semid));
942 sema[semid].u.sem_perm.key = key;
943 sema[semid].u.sem_perm.cuid = cred->cr_uid;
944 sema[semid].u.sem_perm.uid = cred->cr_uid;
945 sema[semid].u.sem_perm.cgid = cred->cr_gid;
946 sema[semid].u.sem_perm.gid = cred->cr_gid;
947 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
948 sema[semid].cred = crhold(cred);
949 sema[semid].u.sem_perm.seq =
950 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
951 sema[semid].u.sem_nsems = nsems;
952 sema[semid].u.sem_otime = 0;
953 sema[semid].u.sem_ctime = time_second;
954 sema[semid].u.sem_base = &sem[semtot];
956 bzero(sema[semid].u.sem_base,
957 sizeof(sema[semid].u.sem_base[0])*nsems);
959 mac_sysvsem_create(cred, &sema[semid]);
961 mtx_unlock(&sema_mtx[semid]);
962 DPRINTF(("sembase = %p, next = %p\n",
963 sema[semid].u.sem_base, &sem[semtot]));
965 DPRINTF(("didn't find it and wasn't asked to create it\n"));
971 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
973 mtx_unlock(&sem_mtx);
977 #ifndef _SYS_SYSPROTO_H_
985 sys_semop(struct thread *td, struct semop_args *uap)
988 struct sembuf small_sops[SMALL_SOPS];
989 int semid = uap->semid;
990 size_t nsops = uap->nsops;
992 struct semid_kernel *semakptr;
993 struct sembuf *sopptr = 0;
994 struct sem *semptr = 0;
995 struct sem_undo *suptr;
996 struct mtx *sema_mtxp;
999 int do_wakeup, do_undos;
1005 DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
1007 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1010 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1012 if (semid < 0 || semid >= seminfo.semmni)
1015 /* Allocate memory for sem_ops */
1016 if (nsops <= SMALL_SOPS)
1018 else if (nsops > seminfo.semopm) {
1019 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1025 PROC_LOCK(td->td_proc);
1027 racct_get_available(td->td_proc, RACCT_NSEMOP)) {
1028 PROC_UNLOCK(td->td_proc);
1031 PROC_UNLOCK(td->td_proc);
1035 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1037 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1038 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
1039 uap->sops, sops, nsops * sizeof(sops[0])));
1040 if (sops != small_sops)
1045 semakptr = &sema[semid];
1046 sema_mtxp = &sema_mtx[semid];
1047 mtx_lock(sema_mtxp);
1048 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1052 seq = semakptr->u.sem_perm.seq;
1053 if (seq != IPCID_TO_SEQ(uap->semid)) {
1058 * Initial pass thru sops to see what permissions are needed.
1059 * Also perform any checks that don't need repeating on each
1060 * attempt to satisfy the request vector.
1062 j = 0; /* permission needed */
1064 for (i = 0; i < nsops; i++) {
1066 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1070 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1072 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1075 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1076 DPRINTF(("error = %d from ipaccess\n", error));
1080 error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
1086 * Loop trying to satisfy the vector of requests.
1087 * If we reach a point where we must wait, any requests already
1088 * performed are rolled back and we go to sleep until some other
1089 * process wakes us up. At this point, we start all over again.
1091 * This ensures that from the perspective of other tasks, a set
1092 * of requests is atomic (never partially satisfied).
1096 error = 0; /* error return if necessary */
1098 for (i = 0; i < nsops; i++) {
1100 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1103 "semop: semakptr=%p, sem_base=%p, "
1104 "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
1105 semakptr, semakptr->u.sem_base, semptr,
1106 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1107 (sopptr->sem_flg & IPC_NOWAIT) ?
1108 "nowait" : "wait"));
1110 if (sopptr->sem_op < 0) {
1111 if (semptr->semval + sopptr->sem_op < 0) {
1112 DPRINTF(("semop: can't do it now\n"));
1115 semptr->semval += sopptr->sem_op;
1116 if (semptr->semval == 0 &&
1117 semptr->semzcnt > 0)
1120 } else if (sopptr->sem_op == 0) {
1121 if (semptr->semval != 0) {
1122 DPRINTF(("semop: not zero now\n"));
1125 } else if (semptr->semval + sopptr->sem_op >
1130 if (semptr->semncnt > 0)
1132 semptr->semval += sopptr->sem_op;
1137 * Did we get through the entire vector?
1143 * No ... rollback anything that we've already done
1145 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1146 for (j = 0; j < i; j++)
1147 semakptr->u.sem_base[sops[j].sem_num].semval -=
1150 /* If we detected an error, return it */
1155 * If the request that we couldn't satisfy has the
1156 * NOWAIT flag set then return with EAGAIN.
1158 if (sopptr->sem_flg & IPC_NOWAIT) {
1163 if (sopptr->sem_op == 0)
1168 DPRINTF(("semop: good night!\n"));
1169 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1171 DPRINTF(("semop: good morning (error=%d)!\n", error));
1172 /* return code is checked below, after sem[nz]cnt-- */
1175 * Make sure that the semaphore still exists
1177 seq = semakptr->u.sem_perm.seq;
1178 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1179 seq != IPCID_TO_SEQ(uap->semid)) {
1185 * Renew the semaphore's pointer after wakeup since
1186 * during msleep sem_base may have been modified and semptr
1187 * is not valid any more
1189 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1192 * The semaphore is still alive. Readjust the count of
1193 * waiting processes.
1195 if (sopptr->sem_op == 0)
1201 * Is it really morning, or was our sleep interrupted?
1202 * (Delayed check of msleep() return code because we
1203 * need to decrement sem[nz]cnt either way.)
1209 DPRINTF(("semop: good morning!\n"));
1214 * Process any SEM_UNDO requests.
1219 for (i = 0; i < nsops; i++) {
1221 * We only need to deal with SEM_UNDO's for non-zero
1226 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1228 adjval = sops[i].sem_op;
1231 error = semundo_adjust(td, &suptr, semid, seq,
1232 sops[i].sem_num, -adjval);
1237 * Oh-Oh! We ran out of either sem_undo's or undo's.
1238 * Rollback the adjustments to this point and then
1239 * rollback the semaphore ups and down so we can return
1240 * with an error with all structures restored. We
1241 * rollback the undo's in the exact reverse order that
1242 * we applied them. This guarantees that we won't run
1243 * out of space as we roll things back out.
1245 for (j = 0; j < i; j++) {
1247 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1249 adjval = sops[k].sem_op;
1252 if (semundo_adjust(td, &suptr, semid, seq,
1253 sops[k].sem_num, adjval) != 0)
1254 panic("semop - can't undo undos");
1257 for (j = 0; j < nsops; j++)
1258 semakptr->u.sem_base[sops[j].sem_num].semval -=
1261 DPRINTF(("error = %d from semundo_adjust\n", error));
1264 } /* loop through the sops */
1266 } /* if (do_undos) */
1268 /* We're definitely done - set the sempid's and time */
1269 for (i = 0; i < nsops; i++) {
1271 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1272 semptr->sempid = td->td_proc->p_pid;
1274 semakptr->u.sem_otime = time_second;
1277 * Do a wakeup if any semaphore was up'd whilst something was
1281 DPRINTF(("semop: doing wakeup\n"));
1283 DPRINTF(("semop: back from wakeup\n"));
1285 DPRINTF(("semop: done\n"));
1286 td->td_retval[0] = 0;
1288 mtx_unlock(sema_mtxp);
1289 if (sops != small_sops)
1295 * Go through the undo structures for this process and apply the adjustments to
1299 semexit_myhook(void *arg, struct proc *p)
1301 struct sem_undo *suptr;
1302 struct semid_kernel *semakptr;
1303 struct mtx *sema_mtxp;
1304 int semid, semnum, adjval, ix;
1308 * Go through the chain of undo vectors looking for one
1309 * associated with this process.
1312 LIST_FOREACH(suptr, &semu_list, un_next) {
1313 if (suptr->un_proc == p)
1316 if (suptr == NULL) {
1320 LIST_REMOVE(suptr, un_next);
1322 DPRINTF(("proc @%p has undo structure with %d entries\n", p,
1326 * If there are any active undo elements then process them.
1328 if (suptr->un_cnt > 0) {
1330 for (ix = 0; ix < suptr->un_cnt; ix++) {
1331 semid = suptr->un_ent[ix].un_id;
1332 semnum = suptr->un_ent[ix].un_num;
1333 adjval = suptr->un_ent[ix].un_adjval;
1334 seq = suptr->un_ent[ix].un_seq;
1335 semakptr = &sema[semid];
1336 sema_mtxp = &sema_mtx[semid];
1338 mtx_lock(sema_mtxp);
1339 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1340 (semakptr->u.sem_perm.seq != seq)) {
1341 mtx_unlock(sema_mtxp);
1344 if (semnum >= semakptr->u.sem_nsems)
1345 panic("semexit - semnum out of range");
1348 "semexit: %p id=%d num=%d(adj=%d) ; sem=%d\n",
1349 suptr->un_proc, suptr->un_ent[ix].un_id,
1350 suptr->un_ent[ix].un_num,
1351 suptr->un_ent[ix].un_adjval,
1352 semakptr->u.sem_base[semnum].semval));
1354 if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
1356 semakptr->u.sem_base[semnum].semval = 0;
1358 semakptr->u.sem_base[semnum].semval += adjval;
1361 DPRINTF(("semexit: back from wakeup\n"));
1362 mtx_unlock(sema_mtxp);
1368 * Deallocate the undo vector.
1370 DPRINTF(("removing vector\n"));
1371 suptr->un_proc = NULL;
1373 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
1378 sysctl_sema(SYSCTL_HANDLER_ARGS)
1381 return (SYSCTL_OUT(req, sema,
1382 sizeof(struct semid_kernel) * seminfo.semmni));
1385 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1386 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1388 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1389 static sy_call_t *semcalls[] = {
1390 (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
1391 (sy_call_t *)sys_semop
1395 * Entry point for all SEM calls.
1400 /* XXX actually varargs. */
1401 struct semsys_args /* {
1411 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1413 if (uap->which < 0 ||
1414 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
1416 error = (*semcalls[uap->which])(td, &uap->a2);
1421 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1424 #ifndef _SYS_SYSPROTO_H_
1425 struct freebsd7___semctl_args {
1429 union semun_old *arg;
1433 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
1435 struct semid_ds_old dsold;
1436 struct semid_ds dsbuf;
1437 union semun_old arg;
1449 error = copyin(uap->arg, &arg, sizeof(arg));
1461 error = copyin(arg.buf, &dsold, sizeof(dsold));
1464 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
1465 CP(dsold, dsbuf, sem_base);
1466 CP(dsold, dsbuf, sem_nsems);
1467 CP(dsold, dsbuf, sem_otime);
1468 CP(dsold, dsbuf, sem_ctime);
1473 semun.array = arg.array;
1476 semun.val = arg.val;
1480 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1488 bzero(&dsold, sizeof(dsold));
1489 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
1490 CP(dsbuf, dsold, sem_base);
1491 CP(dsbuf, dsold, sem_nsems);
1492 CP(dsbuf, dsold, sem_otime);
1493 CP(dsbuf, dsold, sem_ctime);
1494 error = copyout(&dsold, arg.buf, sizeof(dsold));
1499 td->td_retval[0] = rval;
1503 #endif /* COMPAT_FREEBSD{4,5,6,7} */
1505 #ifdef COMPAT_FREEBSD32
1508 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1511 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1512 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1513 switch (uap->which) {
1515 return (freebsd7_freebsd32_semctl(td,
1516 (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
1518 return (sys_semsys(td, (struct semsys_args *)uap));
1521 return (nosys(td, NULL));
1525 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1526 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1528 freebsd7_freebsd32_semctl(struct thread *td,
1529 struct freebsd7_freebsd32_semctl_args *uap)
1531 struct semid_ds32_old dsbuf32;
1532 struct semid_ds dsbuf;
1545 error = copyin(uap->arg, &arg, sizeof(arg));
1557 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1560 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1561 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1562 CP(dsbuf32, dsbuf, sem_nsems);
1563 CP(dsbuf32, dsbuf, sem_otime);
1564 CP(dsbuf32, dsbuf, sem_ctime);
1569 semun.array = PTRIN(arg.array);
1572 semun.val = arg.val;
1576 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1584 bzero(&dsbuf32, sizeof(dsbuf32));
1585 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1586 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1587 CP(dsbuf, dsbuf32, sem_nsems);
1588 CP(dsbuf, dsbuf32, sem_otime);
1589 CP(dsbuf, dsbuf32, sem_ctime);
1590 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1595 td->td_retval[0] = rval;
1601 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1603 struct semid_ds32 dsbuf32;
1604 struct semid_ds dsbuf;
1617 error = copyin(uap->arg, &arg, sizeof(arg));
1629 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1632 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1633 PTRIN_CP(dsbuf32, dsbuf, sem_base);
1634 CP(dsbuf32, dsbuf, sem_nsems);
1635 CP(dsbuf32, dsbuf, sem_otime);
1636 CP(dsbuf32, dsbuf, sem_ctime);
1641 semun.array = PTRIN(arg.array);
1644 semun.val = arg.val;
1648 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1656 bzero(&dsbuf32, sizeof(dsbuf32));
1657 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1658 PTROUT_CP(dsbuf, dsbuf32, sem_base);
1659 CP(dsbuf, dsbuf32, sem_nsems);
1660 CP(dsbuf, dsbuf32, sem_otime);
1661 CP(dsbuf, dsbuf32, sem_ctime);
1662 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1667 td->td_retval[0] = rval;
1671 #endif /* COMPAT_FREEBSD32 */