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_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>
55 #include <sys/syscall.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysent.h>
58 #include <sys/sysctl.h>
60 #include <sys/malloc.h>
64 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
67 #define DPRINTF(a) printf a
72 #define MPRINTF(a) printf a
77 static void seminit(void);
78 static int sysvsem_modload(struct module *, int, void *);
79 static int semunload(void);
80 static void semexit_myhook(void *arg, struct proc *p);
81 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
82 static int semvalid(int semid, struct semid_kernel *semakptr);
84 #ifndef _SYS_SYSPROTO_H_
86 int __semctl(struct thread *td, struct __semctl_args *uap);
88 int semget(struct thread *td, struct semget_args *uap);
90 int semop(struct thread *td, struct semop_args *uap);
93 static struct sem_undo *semu_alloc(struct thread *td);
94 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
95 int semid, int semnum, int adjval);
96 static void semundo_clear(int semid, int semnum);
98 /* XXX casting to (sy_call_t *) is bogus, as usual. */
99 static sy_call_t *semcalls[] = {
100 (sy_call_t *)__semctl, (sy_call_t *)semget,
104 static struct mtx sem_mtx; /* semaphore global lock */
105 static int semtot = 0;
106 static struct semid_kernel *sema; /* semaphore id pool */
107 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
108 static struct sem *sem; /* semaphore pool */
109 SLIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
110 static int *semu; /* undo structure pool */
111 static eventhandler_tag semexit_tag;
113 #define SEMUNDO_MTX sem_mtx
114 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
115 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
116 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
119 u_short semval; /* semaphore value */
120 pid_t sempid; /* pid of last operation */
121 u_short semncnt; /* # awaiting semval > cval */
122 u_short semzcnt; /* # awaiting semval = 0 */
126 * Undo structure (one per process)
129 SLIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
130 struct proc *un_proc; /* owner of this structure */
131 short un_cnt; /* # of active entries */
133 short un_adjval; /* adjust on exit values */
134 short un_num; /* semaphore # */
135 int un_id; /* semid */
136 } un_ent[1]; /* undo entries */
140 * Configuration parameters
143 #define SEMMNI 10 /* # of semaphore identifiers */
146 #define SEMMNS 60 /* # of semaphores in system */
149 #define SEMUME 10 /* max # of undo entries per process */
152 #define SEMMNU 30 /* # of undo structures in system */
155 /* shouldn't need tuning */
157 #define SEMMAP 30 /* # of entries in semaphore map */
160 #define SEMMSL SEMMNS /* max # of semaphores per id */
163 #define SEMOPM 100 /* max # of operations per semop call */
166 #define SEMVMX 32767 /* semaphore maximum value */
167 #define SEMAEM 16384 /* adjust on exit max value */
170 * Due to the way semaphore memory is allocated, we have to ensure that
171 * SEMUSZ is properly aligned.
174 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
176 /* actual size of an undo structure */
177 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
180 * Macro to find a particular sem_undo vector
183 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
186 * semaphore info struct
188 struct seminfo seminfo = {
189 SEMMAP, /* # of entries in semaphore map */
190 SEMMNI, /* # of semaphore identifiers */
191 SEMMNS, /* # of semaphores in system */
192 SEMMNU, /* # of undo structures in system */
193 SEMMSL, /* max # of semaphores per id */
194 SEMOPM, /* max # of operations per semop call */
195 SEMUME, /* max # of undo entries per process */
196 SEMUSZ, /* size in bytes of undo structure */
197 SEMVMX, /* semaphore maximum value */
198 SEMAEM /* adjust on exit max value */
201 SYSCTL_DECL(_kern_ipc);
202 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
203 "Number of entries in the semaphore map");
204 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
205 "Number of semaphore identifiers");
206 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
207 "Maximum number of semaphores in the system");
208 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
209 "Maximum number of undo structures in the system");
210 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
211 "Max semaphores per id");
212 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
213 "Max operations per semop call");
214 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
215 "Max undo entries per process");
216 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
217 "Size in bytes of undo structure");
218 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
219 "Semaphore maximum value");
220 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
221 "Adjust on exit max value");
222 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
223 NULL, 0, sysctl_sema, "", "");
230 TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
231 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
232 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
233 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
234 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
235 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
236 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
237 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
238 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
239 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
241 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
242 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
244 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
246 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
248 for (i = 0; i < seminfo.semmni; i++) {
249 sema[i].u.sem_base = 0;
250 sema[i].u.sem_perm.mode = 0;
251 sema[i].u.sem_perm.seq = 0;
253 mac_init_sysv_sem(&sema[i]);
256 for (i = 0; i < seminfo.semmni; i++)
257 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
258 for (i = 0; i < seminfo.semmnu; i++) {
259 struct sem_undo *suptr = SEMU(i);
260 suptr->un_proc = NULL;
262 SLIST_INIT(&semu_list);
263 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
264 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
265 EVENTHANDLER_PRI_ANY);
276 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
278 for (i = 0; i < seminfo.semmni; i++)
279 mac_destroy_sysv_sem(&sema[i]);
284 for (i = 0; i < seminfo.semmni; i++)
285 mtx_destroy(&sema_mtx[i]);
286 mtx_destroy(&sem_mtx);
291 sysvsem_modload(struct module *module, int cmd, void *arg)
311 static moduledata_t sysvsem_mod = {
317 SYSCALL_MODULE_HELPER(semsys);
318 SYSCALL_MODULE_HELPER(__semctl);
319 SYSCALL_MODULE_HELPER(semget);
320 SYSCALL_MODULE_HELPER(semop);
322 DECLARE_MODULE(sysvsem, sysvsem_mod,
323 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
324 MODULE_VERSION(sysvsem, 1);
327 * Entry point for all SEM calls
334 /* XXX actually varargs. */
335 struct semsys_args /* {
345 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
347 if (uap->which < 0 ||
348 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
350 error = (*semcalls[uap->which])(td, &uap->a2);
355 * Allocate a new sem_undo structure for a process
356 * (returns ptr to structure or NULL if no more room)
359 static struct sem_undo *
364 struct sem_undo *suptr;
365 struct sem_undo **supptr;
368 SEMUNDO_LOCKASSERT(MA_OWNED);
370 * Try twice to allocate something.
371 * (we'll purge an empty structure after the first pass so
372 * two passes are always enough)
375 for (attempt = 0; attempt < 2; attempt++) {
377 * Look for a free structure.
378 * Fill it in and return it if we find one.
381 for (i = 0; i < seminfo.semmnu; i++) {
383 if (suptr->un_proc == NULL) {
384 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
386 suptr->un_proc = td->td_proc;
392 * We didn't find a free one, if this is the first attempt
393 * then try to free a structure.
397 /* All the structures are in use - try to free one */
398 int did_something = 0;
400 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
402 if (suptr->un_cnt == 0) {
403 suptr->un_proc = NULL;
405 *supptr = SLIST_NEXT(suptr, un_next);
410 /* If we didn't free anything then just give-up */
415 * The second pass failed even though we freed
416 * something after the first pass!
417 * This is IMPOSSIBLE!
419 panic("semu_alloc - second attempt failed");
426 * Adjust a particular entry for a particular proc
430 semundo_adjust(td, supptr, semid, semnum, adjval)
432 struct sem_undo **supptr;
436 struct proc *p = td->td_proc;
437 struct sem_undo *suptr;
441 SEMUNDO_LOCKASSERT(MA_OWNED);
442 /* Look for and remember the sem_undo if the caller doesn't provide
447 SLIST_FOREACH(suptr, &semu_list, un_next) {
448 if (suptr->un_proc == p) {
456 suptr = semu_alloc(td);
464 * Look for the requested entry and adjust it (delete if adjval becomes
467 sunptr = &suptr->un_ent[0];
468 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
469 if (sunptr->un_id != semid || sunptr->un_num != semnum)
472 adjval += sunptr->un_adjval;
473 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
476 sunptr->un_adjval = adjval;
477 if (sunptr->un_adjval == 0) {
479 if (i < suptr->un_cnt)
481 suptr->un_ent[suptr->un_cnt];
486 /* Didn't find the right entry - create it */
489 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
491 if (suptr->un_cnt != seminfo.semume) {
492 sunptr = &suptr->un_ent[suptr->un_cnt];
494 sunptr->un_adjval = adjval;
495 sunptr->un_id = semid; sunptr->un_num = semnum;
502 semundo_clear(semid, semnum)
505 struct sem_undo *suptr;
507 SEMUNDO_LOCKASSERT(MA_OWNED);
508 SLIST_FOREACH(suptr, &semu_list, un_next) {
509 struct undo *sunptr = &suptr->un_ent[0];
512 while (i < suptr->un_cnt) {
513 if (sunptr->un_id == semid) {
514 if (semnum == -1 || sunptr->un_num == semnum) {
516 if (i < suptr->un_cnt) {
518 suptr->un_ent[suptr->un_cnt];
531 semvalid(semid, semakptr)
533 struct semid_kernel *semakptr;
536 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
537 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
541 * Note that the user-mode half of this passes a union, not a pointer
543 #ifndef _SYS_SYSPROTO_H_
544 struct __semctl_args {
558 struct __semctl_args *uap;
560 struct semid_ds dsbuf;
561 union semun arg, semun;
572 error = copyin(uap->arg, &arg, sizeof(arg));
584 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
591 semun.array = arg.array;
598 error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
606 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
611 td->td_retval[0] = rval;
616 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
617 union semun *arg, register_t *rval)
620 struct ucred *cred = td->td_ucred;
622 struct semid_ds *sbuf;
623 struct semid_kernel *semakptr;
624 struct mtx *sema_mtxp;
625 u_short usval, count;
628 DPRINTF(("call to semctl(%d, %d, %d, 0x%x)\n",
629 semid, semnum, cmd, arg));
630 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
638 * For this command we assume semid is an array index
639 * rather than an IPC id.
641 if (semid < 0 || semid >= seminfo.semmni)
643 semakptr = &sema[semid];
644 sema_mtxp = &sema_mtx[semid];
646 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
650 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
653 error = mac_check_sysv_semctl(cred, semakptr, cmd);
655 MPRINTF(("mac_check_sysv_semctl returned %d\n",
660 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
661 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
662 mtx_unlock(sema_mtxp);
666 semidx = IPCID_TO_IX(semid);
667 if (semidx < 0 || semidx >= seminfo.semmni)
670 semakptr = &sema[semidx];
671 sema_mtxp = &sema_mtx[semidx];
674 error = mac_check_sysv_semctl(cred, semakptr, cmd);
676 MPRINTF(("mac_check_sysv_semctl returned %d\n", error));
686 if ((error = semvalid(semid, semakptr)) != 0)
688 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
690 semakptr->u.sem_perm.cuid = cred->cr_uid;
691 semakptr->u.sem_perm.uid = cred->cr_uid;
692 semtot -= semakptr->u.sem_nsems;
693 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
694 sem[i] = sem[i + semakptr->u.sem_nsems];
695 for (i = 0; i < seminfo.semmni; i++) {
696 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
697 sema[i].u.sem_base > semakptr->u.sem_base)
698 sema[i].u.sem_base -= semakptr->u.sem_nsems;
700 semakptr->u.sem_perm.mode = 0;
702 mac_cleanup_sysv_sem(semakptr);
705 semundo_clear(semidx, -1);
711 if ((error = semvalid(semid, semakptr)) != 0)
713 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
716 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
717 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
718 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
719 ~0777) | (sbuf->sem_perm.mode & 0777);
720 semakptr->u.sem_ctime = time_second;
724 if ((error = semvalid(semid, semakptr)) != 0)
726 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
728 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
732 if ((error = semvalid(semid, semakptr)) != 0)
734 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
736 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
740 *rval = semakptr->u.sem_base[semnum].semncnt;
744 if ((error = semvalid(semid, semakptr)) != 0)
746 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
748 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
752 *rval = semakptr->u.sem_base[semnum].sempid;
756 if ((error = semvalid(semid, semakptr)) != 0)
758 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
760 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
764 *rval = semakptr->u.sem_base[semnum].semval;
769 * Unfortunately, callers of this function don't know
770 * in advance how many semaphores are in this set.
771 * While we could just allocate the maximum size array
772 * and pass the actual size back to the caller, that
773 * won't work for SETALL since we can't copyin() more
774 * data than the user specified as we may return a
777 * Note that the number of semaphores in a set is
778 * fixed for the life of that set. The only way that
779 * the 'count' could change while are blocked in
780 * malloc() is if this semaphore set were destroyed
781 * and a new one created with the same index.
782 * However, semvalid() will catch that due to the
783 * sequence number unless exactly 0x8000 (or a
784 * multiple thereof) semaphore sets for the same index
785 * are created and destroyed while we are in malloc!
788 count = semakptr->u.sem_nsems;
789 mtx_unlock(sema_mtxp);
790 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
792 if ((error = semvalid(semid, semakptr)) != 0)
794 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
795 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
797 for (i = 0; i < semakptr->u.sem_nsems; i++)
798 array[i] = semakptr->u.sem_base[i].semval;
799 mtx_unlock(sema_mtxp);
800 error = copyout(array, arg->array, count * sizeof(*array));
805 if ((error = semvalid(semid, semakptr)) != 0)
807 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
809 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
813 *rval = semakptr->u.sem_base[semnum].semzcnt;
817 if ((error = semvalid(semid, semakptr)) != 0)
819 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
821 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
825 if (arg->val < 0 || arg->val > seminfo.semvmx) {
829 semakptr->u.sem_base[semnum].semval = arg->val;
831 semundo_clear(semidx, semnum);
838 * See comment on GETALL for why 'count' shouldn't change
839 * and why we require a userland buffer.
841 count = semakptr->u.sem_nsems;
842 mtx_unlock(sema_mtxp);
843 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
844 error = copyin(arg->array, array, count * sizeof(*array));
848 if ((error = semvalid(semid, semakptr)) != 0)
850 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
851 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
853 for (i = 0; i < semakptr->u.sem_nsems; i++) {
855 if (usval > seminfo.semvmx) {
859 semakptr->u.sem_base[i].semval = usval;
862 semundo_clear(semidx, -1);
873 mtx_unlock(sema_mtxp);
879 #ifndef _SYS_SYSPROTO_H_
893 struct semget_args *uap;
895 int semid, error = 0;
897 int nsems = uap->nsems;
898 int semflg = uap->semflg;
899 struct ucred *cred = td->td_ucred;
901 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
902 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
906 if (key != IPC_PRIVATE) {
907 for (semid = 0; semid < seminfo.semmni; semid++) {
908 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
909 sema[semid].u.sem_perm.key == key)
912 if (semid < seminfo.semmni) {
913 DPRINTF(("found public key\n"));
914 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
918 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
919 DPRINTF(("too small\n"));
923 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
924 DPRINTF(("not exclusive\n"));
929 error = mac_check_sysv_semget(cred, &sema[semid]);
931 MPRINTF(("mac_check_sysv_semget returned %d\n",
940 DPRINTF(("need to allocate the semid_kernel\n"));
941 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
942 if (nsems <= 0 || nsems > seminfo.semmsl) {
943 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
948 if (nsems > seminfo.semmns - semtot) {
950 "not enough semaphores left (need %d, got %d)\n",
951 nsems, seminfo.semmns - semtot));
955 for (semid = 0; semid < seminfo.semmni; semid++) {
956 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
959 if (semid == seminfo.semmni) {
960 DPRINTF(("no more semid_kernel's available\n"));
964 DPRINTF(("semid %d is available\n", semid));
965 sema[semid].u.sem_perm.key = key;
966 sema[semid].u.sem_perm.cuid = cred->cr_uid;
967 sema[semid].u.sem_perm.uid = cred->cr_uid;
968 sema[semid].u.sem_perm.cgid = cred->cr_gid;
969 sema[semid].u.sem_perm.gid = cred->cr_gid;
970 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
971 sema[semid].u.sem_perm.seq =
972 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
973 sema[semid].u.sem_nsems = nsems;
974 sema[semid].u.sem_otime = 0;
975 sema[semid].u.sem_ctime = time_second;
976 sema[semid].u.sem_base = &sem[semtot];
978 bzero(sema[semid].u.sem_base,
979 sizeof(sema[semid].u.sem_base[0])*nsems);
981 mac_create_sysv_sem(cred, &sema[semid]);
983 DPRINTF(("sembase = 0x%x, next = 0x%x\n",
984 sema[semid].u.sem_base, &sem[semtot]));
986 DPRINTF(("didn't find it and wasn't asked to create it\n"));
992 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
998 #ifndef _SYS_SYSPROTO_H_
1001 struct sembuf *sops;
1012 struct semop_args *uap;
1014 #define SMALL_SOPS 8
1015 struct sembuf small_sops[SMALL_SOPS];
1016 int semid = uap->semid;
1017 size_t nsops = uap->nsops;
1018 struct sembuf *sops;
1019 struct semid_kernel *semakptr;
1020 struct sembuf *sopptr = 0;
1021 struct sem *semptr = 0;
1022 struct sem_undo *suptr;
1023 struct mtx *sema_mtxp;
1026 int do_wakeup, do_undos;
1028 DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops));
1030 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1033 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1035 if (semid < 0 || semid >= seminfo.semmni)
1038 /* Allocate memory for sem_ops */
1039 if (nsops <= SMALL_SOPS)
1041 else if (nsops <= seminfo.semopm)
1042 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
1044 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
1048 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
1049 DPRINTF(("error = %d from copyin(%08x, %08x, %d)\n", error,
1050 uap->sops, sops, nsops * sizeof(sops[0])));
1051 if (sops != small_sops)
1056 semakptr = &sema[semid];
1057 sema_mtxp = &sema_mtx[semid];
1058 mtx_lock(sema_mtxp);
1059 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1063 if (semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1068 * Initial pass thru sops to see what permissions are needed.
1069 * Also perform any checks that don't need repeating on each
1070 * attempt to satisfy the request vector.
1072 j = 0; /* permission needed */
1074 for (i = 0; i < nsops; i++) {
1076 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1080 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1082 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1085 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1086 DPRINTF(("error = %d from ipaccess\n", error));
1090 error = mac_check_sysv_semop(td->td_ucred, semakptr, j);
1092 MPRINTF(("mac_check_sysv_semop returned %d\n", error));
1098 * Loop trying to satisfy the vector of requests.
1099 * If we reach a point where we must wait, any requests already
1100 * performed are rolled back and we go to sleep until some other
1101 * process wakes us up. At this point, we start all over again.
1103 * This ensures that from the perspective of other tasks, a set
1104 * of requests is atomic (never partially satisfied).
1108 error = 0; /* error return if necessary */
1110 for (i = 0; i < nsops; i++) {
1112 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1115 "semop: semakptr=%x, sem_base=%x, "
1116 "semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1117 semakptr, semakptr->u.sem_base, semptr,
1118 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1119 (sopptr->sem_flg & IPC_NOWAIT) ?
1120 "nowait" : "wait"));
1122 if (sopptr->sem_op < 0) {
1123 if (semptr->semval + sopptr->sem_op < 0) {
1124 DPRINTF(("semop: can't do it now\n"));
1127 semptr->semval += sopptr->sem_op;
1128 if (semptr->semval == 0 &&
1129 semptr->semzcnt > 0)
1132 } else if (sopptr->sem_op == 0) {
1133 if (semptr->semval != 0) {
1134 DPRINTF(("semop: not zero now\n"));
1137 } else if (semptr->semval + sopptr->sem_op >
1142 if (semptr->semncnt > 0)
1144 semptr->semval += sopptr->sem_op;
1149 * Did we get through the entire vector?
1155 * No ... rollback anything that we've already done
1157 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1158 for (j = 0; j < i; j++)
1159 semakptr->u.sem_base[sops[j].sem_num].semval -=
1162 /* If we detected an error, return it */
1167 * If the request that we couldn't satisfy has the
1168 * NOWAIT flag set then return with EAGAIN.
1170 if (sopptr->sem_flg & IPC_NOWAIT) {
1175 if (sopptr->sem_op == 0)
1180 DPRINTF(("semop: good night!\n"));
1181 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1183 DPRINTF(("semop: good morning (error=%d)!\n", error));
1184 /* return code is checked below, after sem[nz]cnt-- */
1187 * Make sure that the semaphore still exists
1189 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1190 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1196 * Renew the semaphore's pointer after wakeup since
1197 * during msleep sem_base may have been modified and semptr
1198 * is not valid any more
1200 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1203 * The semaphore is still alive. Readjust the count of
1204 * waiting processes.
1206 if (sopptr->sem_op == 0)
1212 * Is it really morning, or was our sleep interrupted?
1213 * (Delayed check of msleep() return code because we
1214 * need to decrement sem[nz]cnt either way.)
1220 DPRINTF(("semop: good morning!\n"));
1225 * Process any SEM_UNDO requests.
1230 for (i = 0; i < nsops; i++) {
1232 * We only need to deal with SEM_UNDO's for non-zero
1237 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1239 adjval = sops[i].sem_op;
1242 error = semundo_adjust(td, &suptr, semid,
1243 sops[i].sem_num, -adjval);
1248 * Oh-Oh! We ran out of either sem_undo's or undo's.
1249 * Rollback the adjustments to this point and then
1250 * rollback the semaphore ups and down so we can return
1251 * with an error with all structures restored. We
1252 * rollback the undo's in the exact reverse order that
1253 * we applied them. This guarantees that we won't run
1254 * out of space as we roll things back out.
1256 for (j = 0; j < i; j++) {
1258 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1260 adjval = sops[k].sem_op;
1263 if (semundo_adjust(td, &suptr, semid,
1264 sops[k].sem_num, adjval) != 0)
1265 panic("semop - can't undo undos");
1268 for (j = 0; j < nsops; j++)
1269 semakptr->u.sem_base[sops[j].sem_num].semval -=
1272 DPRINTF(("error = %d from semundo_adjust\n", error));
1275 } /* loop through the sops */
1277 } /* if (do_undos) */
1279 /* We're definitely done - set the sempid's and time */
1280 for (i = 0; i < nsops; i++) {
1282 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1283 semptr->sempid = td->td_proc->p_pid;
1285 semakptr->u.sem_otime = time_second;
1288 * Do a wakeup if any semaphore was up'd whilst something was
1292 DPRINTF(("semop: doing wakeup\n"));
1294 DPRINTF(("semop: back from wakeup\n"));
1296 DPRINTF(("semop: done\n"));
1297 td->td_retval[0] = 0;
1299 mtx_unlock(sema_mtxp);
1300 if (sops != small_sops)
1306 * Go through the undo structures for this process and apply the adjustments to
1310 semexit_myhook(arg, p)
1314 struct sem_undo *suptr;
1315 struct sem_undo **supptr;
1318 * Go through the chain of undo vectors looking for one
1319 * associated with this process.
1322 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
1323 if (suptr->un_proc == p) {
1324 *supptr = SLIST_NEXT(suptr, un_next);
1333 DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1337 * If there are any active undo elements then process them.
1339 if (suptr->un_cnt > 0) {
1342 for (ix = 0; ix < suptr->un_cnt; ix++) {
1343 int semid = suptr->un_ent[ix].un_id;
1344 int semnum = suptr->un_ent[ix].un_num;
1345 int adjval = suptr->un_ent[ix].un_adjval;
1346 struct semid_kernel *semakptr;
1347 struct mtx *sema_mtxp;
1349 semakptr = &sema[semid];
1350 sema_mtxp = &sema_mtx[semid];
1351 mtx_lock(sema_mtxp);
1353 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0)
1354 panic("semexit - semid not allocated");
1355 if (semnum >= semakptr->u.sem_nsems)
1356 panic("semexit - semnum out of range");
1359 "semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1360 suptr->un_proc, suptr->un_ent[ix].un_id,
1361 suptr->un_ent[ix].un_num,
1362 suptr->un_ent[ix].un_adjval,
1363 semakptr->u.sem_base[semnum].semval));
1366 if (semakptr->u.sem_base[semnum].semval <
1368 semakptr->u.sem_base[semnum].semval = 0;
1370 semakptr->u.sem_base[semnum].semval +=
1373 semakptr->u.sem_base[semnum].semval += adjval;
1376 DPRINTF(("semexit: back from wakeup\n"));
1377 mtx_unlock(sema_mtxp);
1383 * Deallocate the undo vector.
1385 DPRINTF(("removing vector\n"));
1387 suptr->un_proc = NULL;
1392 sysctl_sema(SYSCTL_HANDLER_ARGS)
1395 return (SYSCTL_OUT(req, sema,
1396 sizeof(struct semid_kernel) * seminfo.semmni));