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/sysent.h>
57 #include <sys/sysctl.h>
58 #include <sys/malloc.h>
62 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
65 #define DPRINTF(a) printf a
70 #define MPRINTF(a) printf a
75 static void seminit(void);
76 static int sysvsem_modload(struct module *, int, void *);
77 static int semunload(void);
78 static void semexit_myhook(void *arg, struct proc *p);
79 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
80 static int semvalid(int semid, struct semid_kernel *semakptr);
82 #ifndef _SYS_SYSPROTO_H_
84 int __semctl(struct thread *td, struct __semctl_args *uap);
86 int semget(struct thread *td, struct semget_args *uap);
88 int semop(struct thread *td, struct semop_args *uap);
91 static struct sem_undo *semu_alloc(struct thread *td);
92 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
93 int semid, int semnum, int adjval);
94 static void semundo_clear(int semid, int semnum);
96 /* XXX casting to (sy_call_t *) is bogus, as usual. */
97 static sy_call_t *semcalls[] = {
98 (sy_call_t *)__semctl, (sy_call_t *)semget,
102 static struct mtx sem_mtx; /* semaphore global lock */
103 static int semtot = 0;
104 static struct semid_kernel *sema; /* semaphore id pool */
105 static struct mtx *sema_mtx; /* semaphore id pool mutexes*/
106 static struct sem *sem; /* semaphore pool */
107 SLIST_HEAD(, sem_undo) semu_list; /* list of active undo structures */
108 static int *semu; /* undo structure pool */
109 static eventhandler_tag semexit_tag;
111 #define SEMUNDO_MTX sem_mtx
112 #define SEMUNDO_LOCK() mtx_lock(&SEMUNDO_MTX);
113 #define SEMUNDO_UNLOCK() mtx_unlock(&SEMUNDO_MTX);
114 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
117 u_short semval; /* semaphore value */
118 pid_t sempid; /* pid of last operation */
119 u_short semncnt; /* # awaiting semval > cval */
120 u_short semzcnt; /* # awaiting semval = 0 */
124 * Undo structure (one per process)
127 SLIST_ENTRY(sem_undo) un_next; /* ptr to next active undo structure */
128 struct proc *un_proc; /* owner of this structure */
129 short un_cnt; /* # of active entries */
131 short un_adjval; /* adjust on exit values */
132 short un_num; /* semaphore # */
133 int un_id; /* semid */
134 } un_ent[1]; /* undo entries */
138 * Configuration parameters
141 #define SEMMNI 10 /* # of semaphore identifiers */
144 #define SEMMNS 60 /* # of semaphores in system */
147 #define SEMUME 10 /* max # of undo entries per process */
150 #define SEMMNU 30 /* # of undo structures in system */
153 /* shouldn't need tuning */
155 #define SEMMAP 30 /* # of entries in semaphore map */
158 #define SEMMSL SEMMNS /* max # of semaphores per id */
161 #define SEMOPM 100 /* max # of operations per semop call */
164 #define SEMVMX 32767 /* semaphore maximum value */
165 #define SEMAEM 16384 /* adjust on exit max value */
168 * Due to the way semaphore memory is allocated, we have to ensure that
169 * SEMUSZ is properly aligned.
172 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
174 /* actual size of an undo structure */
175 #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
178 * Macro to find a particular sem_undo vector
181 ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
184 * semaphore info struct
186 struct seminfo seminfo = {
187 SEMMAP, /* # of entries in semaphore map */
188 SEMMNI, /* # of semaphore identifiers */
189 SEMMNS, /* # of semaphores in system */
190 SEMMNU, /* # of undo structures in system */
191 SEMMSL, /* max # of semaphores per id */
192 SEMOPM, /* max # of operations per semop call */
193 SEMUME, /* max # of undo entries per process */
194 SEMUSZ, /* size in bytes of undo structure */
195 SEMVMX, /* semaphore maximum value */
196 SEMAEM /* adjust on exit max value */
199 SYSCTL_DECL(_kern_ipc);
200 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
201 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0, "");
202 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0, "");
203 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0, "");
204 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
205 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0, "");
206 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0, "");
207 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0, "");
208 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
209 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
210 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
211 NULL, 0, sysctl_sema, "", "");
218 TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
219 TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
220 TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
221 TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
222 TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
223 TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
224 TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
225 TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
226 TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
227 TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
229 sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
230 sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
232 sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
234 semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
236 for (i = 0; i < seminfo.semmni; i++) {
237 sema[i].u.sem_base = 0;
238 sema[i].u.sem_perm.mode = 0;
239 sema[i].u.sem_perm.seq = 0;
241 mac_init_sysv_sema(&sema[i]);
244 for (i = 0; i < seminfo.semmni; i++)
245 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
246 for (i = 0; i < seminfo.semmnu; i++) {
247 struct sem_undo *suptr = SEMU(i);
248 suptr->un_proc = NULL;
250 SLIST_INIT(&semu_list);
251 mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
252 semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
253 EVENTHANDLER_PRI_ANY);
264 EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
266 for (i = 0; i < seminfo.semmni; i++)
267 mac_destroy_sysv_sema(&sema[i]);
272 for (i = 0; i < seminfo.semmni; i++)
273 mtx_destroy(&sema_mtx[i]);
274 mtx_destroy(&sem_mtx);
279 sysvsem_modload(struct module *module, int cmd, void *arg)
299 static moduledata_t sysvsem_mod = {
305 SYSCALL_MODULE_HELPER(semsys);
306 SYSCALL_MODULE_HELPER(__semctl);
307 SYSCALL_MODULE_HELPER(semget);
308 SYSCALL_MODULE_HELPER(semop);
310 DECLARE_MODULE(sysvsem, sysvsem_mod,
311 SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
312 MODULE_VERSION(sysvsem, 1);
315 * Entry point for all SEM calls
322 /* XXX actually varargs. */
323 struct semsys_args /* {
333 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
335 if (uap->which < 0 ||
336 uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
338 error = (*semcalls[uap->which])(td, &uap->a2);
343 * Allocate a new sem_undo structure for a process
344 * (returns ptr to structure or NULL if no more room)
347 static struct sem_undo *
352 struct sem_undo *suptr;
353 struct sem_undo **supptr;
356 SEMUNDO_LOCKASSERT(MA_OWNED);
358 * Try twice to allocate something.
359 * (we'll purge an empty structure after the first pass so
360 * two passes are always enough)
363 for (attempt = 0; attempt < 2; attempt++) {
365 * Look for a free structure.
366 * Fill it in and return it if we find one.
369 for (i = 0; i < seminfo.semmnu; i++) {
371 if (suptr->un_proc == NULL) {
372 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
374 suptr->un_proc = td->td_proc;
380 * We didn't find a free one, if this is the first attempt
381 * then try to free a structure.
385 /* All the structures are in use - try to free one */
386 int did_something = 0;
388 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
390 if (suptr->un_cnt == 0) {
391 suptr->un_proc = NULL;
393 *supptr = SLIST_NEXT(suptr, un_next);
398 /* If we didn't free anything then just give-up */
403 * The second pass failed even though we freed
404 * something after the first pass!
405 * This is IMPOSSIBLE!
407 panic("semu_alloc - second attempt failed");
414 * Adjust a particular entry for a particular proc
418 semundo_adjust(td, supptr, semid, semnum, adjval)
420 struct sem_undo **supptr;
424 struct proc *p = td->td_proc;
425 struct sem_undo *suptr;
429 SEMUNDO_LOCKASSERT(MA_OWNED);
430 /* Look for and remember the sem_undo if the caller doesn't provide
435 SLIST_FOREACH(suptr, &semu_list, un_next) {
436 if (suptr->un_proc == p) {
444 suptr = semu_alloc(td);
452 * Look for the requested entry and adjust it (delete if adjval becomes
455 sunptr = &suptr->un_ent[0];
456 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
457 if (sunptr->un_id != semid || sunptr->un_num != semnum)
460 adjval += sunptr->un_adjval;
461 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
464 sunptr->un_adjval = adjval;
465 if (sunptr->un_adjval == 0) {
467 if (i < suptr->un_cnt)
469 suptr->un_ent[suptr->un_cnt];
474 /* Didn't find the right entry - create it */
477 if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
479 if (suptr->un_cnt != seminfo.semume) {
480 sunptr = &suptr->un_ent[suptr->un_cnt];
482 sunptr->un_adjval = adjval;
483 sunptr->un_id = semid; sunptr->un_num = semnum;
490 semundo_clear(semid, semnum)
493 struct sem_undo *suptr;
495 SEMUNDO_LOCKASSERT(MA_OWNED);
496 SLIST_FOREACH(suptr, &semu_list, un_next) {
497 struct undo *sunptr = &suptr->un_ent[0];
500 while (i < suptr->un_cnt) {
501 if (sunptr->un_id == semid) {
502 if (semnum == -1 || sunptr->un_num == semnum) {
504 if (i < suptr->un_cnt) {
506 suptr->un_ent[suptr->un_cnt];
519 semvalid(semid, semakptr)
521 struct semid_kernel *semakptr;
524 return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
525 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
529 * Note that the user-mode half of this passes a union, not a pointer
531 #ifndef _SYS_SYSPROTO_H_
532 struct __semctl_args {
546 struct __semctl_args *uap;
548 int semid = uap->semid;
549 int semnum = uap->semnum;
552 union semun *arg = uap->arg;
553 union semun real_arg;
554 struct ucred *cred = td->td_ucred;
556 struct semid_ds sbuf;
557 struct semid_kernel *semakptr;
558 struct mtx *sema_mtxp;
559 u_short usval, count;
561 DPRINTF(("call to semctl(%d, %d, %d, 0x%x)\n",
562 semid, semnum, cmd, arg));
563 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
570 if (semid < 0 || semid >= seminfo.semmni)
572 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
574 semakptr = &sema[semid];
575 sema_mtxp = &sema_mtx[semid];
577 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
581 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
584 error = mac_check_sysv_semctl(cred, semakptr, cmd);
586 MPRINTF(("mac_check_sysv_semctl returned %d\n",
591 mtx_unlock(sema_mtxp);
592 error = copyout(&semakptr->u, real_arg.buf,
593 sizeof(struct semid_ds));
594 rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
596 td->td_retval[0] = rval;
600 semid = IPCID_TO_IX(semid);
601 if (semid < 0 || semid >= seminfo.semmni)
604 semakptr = &sema[semid];
605 sema_mtxp = &sema_mtx[semid];
608 error = mac_check_sysv_semctl(cred, semakptr, cmd);
610 MPRINTF(("mac_check_sysv_semctl returned %d\n", error));
611 mtx_unlock(sema_mtxp);
614 mtx_unlock(sema_mtxp);
623 if ((error = semvalid(uap->semid, semakptr)) != 0)
625 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
627 semakptr->u.sem_perm.cuid = cred->cr_uid;
628 semakptr->u.sem_perm.uid = cred->cr_uid;
629 semtot -= semakptr->u.sem_nsems;
630 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
631 sem[i] = sem[i + semakptr->u.sem_nsems];
632 for (i = 0; i < seminfo.semmni; i++) {
633 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
634 sema[i].u.sem_base > semakptr->u.sem_base)
635 sema[i].u.sem_base -= semakptr->u.sem_nsems;
637 semakptr->u.sem_perm.mode = 0;
639 mac_cleanup_sysv_sema(semakptr);
642 semundo_clear(semid, -1);
648 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
650 if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
653 if ((error = semvalid(uap->semid, semakptr)) != 0)
655 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
657 semakptr->u.sem_perm.uid = sbuf.sem_perm.uid;
658 semakptr->u.sem_perm.gid = sbuf.sem_perm.gid;
659 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
660 ~0777) | (sbuf.sem_perm.mode & 0777);
661 semakptr->u.sem_ctime = time_second;
665 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
668 if ((error = semvalid(uap->semid, semakptr)) != 0)
670 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
673 mtx_unlock(sema_mtxp);
674 error = copyout(&semakptr->u, real_arg.buf,
675 sizeof(struct semid_ds));
680 if ((error = semvalid(uap->semid, semakptr)) != 0)
682 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
684 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
688 rval = semakptr->u.sem_base[semnum].semncnt;
693 if ((error = semvalid(uap->semid, semakptr)) != 0)
695 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
697 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
701 rval = semakptr->u.sem_base[semnum].sempid;
706 if ((error = semvalid(uap->semid, semakptr)) != 0)
708 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
710 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
714 rval = semakptr->u.sem_base[semnum].semval;
718 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
720 array = malloc(sizeof(*array) * semakptr->u.sem_nsems, M_TEMP,
723 if ((error = semvalid(uap->semid, semakptr)) != 0)
725 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
727 for (i = 0; i < semakptr->u.sem_nsems; i++)
728 array[i] = semakptr->u.sem_base[i].semval;
729 mtx_unlock(sema_mtxp);
730 error = copyout(array, real_arg.array,
731 i * sizeof(real_arg.array[0]));
736 if ((error = semvalid(uap->semid, semakptr)) != 0)
738 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
740 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
744 rval = semakptr->u.sem_base[semnum].semzcnt;
748 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
751 if ((error = semvalid(uap->semid, semakptr)) != 0)
753 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
755 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
759 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
763 semakptr->u.sem_base[semnum].semval = real_arg.val;
765 semundo_clear(semid, semnum);
773 if ((error = semvalid(uap->semid, semakptr)) != 0)
775 count = semakptr->u.sem_nsems;
776 mtx_unlock(sema_mtxp);
777 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
779 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
780 copyin(real_arg.array, array, count * sizeof(*array));
784 if ((error = semvalid(uap->semid, semakptr)) != 0)
786 /* we could have raced? */
787 if (count != semakptr->u.sem_nsems) {
792 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
794 for (i = 0; i < semakptr->u.sem_nsems; i++) {
796 if (usval > seminfo.semvmx) {
800 semakptr->u.sem_base[i].semval = usval;
803 semundo_clear(semid, -1);
814 td->td_retval[0] = rval;
816 if (mtx_owned(sema_mtxp))
817 mtx_unlock(sema_mtxp);
823 #ifndef _SYS_SYSPROTO_H_
837 struct semget_args *uap;
839 int semid, error = 0;
841 int nsems = uap->nsems;
842 int semflg = uap->semflg;
843 struct ucred *cred = td->td_ucred;
845 DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
846 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
850 if (key != IPC_PRIVATE) {
851 for (semid = 0; semid < seminfo.semmni; semid++) {
852 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
853 sema[semid].u.sem_perm.key == key)
856 if (semid < seminfo.semmni) {
857 DPRINTF(("found public key\n"));
858 if ((error = ipcperm(td, &sema[semid].u.sem_perm,
862 if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
863 DPRINTF(("too small\n"));
867 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
868 DPRINTF(("not exclusive\n"));
873 error = mac_check_sysv_semget(cred, &sema[semid]);
875 MPRINTF(("mac_check_sysv_semget returned %d\n",
884 DPRINTF(("need to allocate the semid_kernel\n"));
885 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
886 if (nsems <= 0 || nsems > seminfo.semmsl) {
887 DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
892 if (nsems > seminfo.semmns - semtot) {
894 "not enough semaphores left (need %d, got %d)\n",
895 nsems, seminfo.semmns - semtot));
899 for (semid = 0; semid < seminfo.semmni; semid++) {
900 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
903 if (semid == seminfo.semmni) {
904 DPRINTF(("no more semid_kernel's available\n"));
908 DPRINTF(("semid %d is available\n", semid));
909 sema[semid].u.sem_perm.key = key;
910 sema[semid].u.sem_perm.cuid = cred->cr_uid;
911 sema[semid].u.sem_perm.uid = cred->cr_uid;
912 sema[semid].u.sem_perm.cgid = cred->cr_gid;
913 sema[semid].u.sem_perm.gid = cred->cr_gid;
914 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
915 sema[semid].u.sem_perm.seq =
916 (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
917 sema[semid].u.sem_nsems = nsems;
918 sema[semid].u.sem_otime = 0;
919 sema[semid].u.sem_ctime = time_second;
920 sema[semid].u.sem_base = &sem[semtot];
922 bzero(sema[semid].u.sem_base,
923 sizeof(sema[semid].u.sem_base[0])*nsems);
925 mac_create_sysv_sema(cred, &sema[semid]);
927 DPRINTF(("sembase = 0x%x, next = 0x%x\n",
928 sema[semid].u.sem_base, &sem[semtot]));
930 DPRINTF(("didn't find it and wasn't asked to create it\n"));
936 td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
942 #ifndef _SYS_SYSPROTO_H_
956 struct semop_args *uap;
959 struct sembuf small_sops[SMALL_SOPS];
960 int semid = uap->semid;
961 size_t nsops = uap->nsops;
963 struct semid_kernel *semakptr;
964 struct sembuf *sopptr = 0;
965 struct sem *semptr = 0;
966 struct sem_undo *suptr;
967 struct mtx *sema_mtxp;
970 int do_wakeup, do_undos;
972 DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops));
974 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
977 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
979 if (semid < 0 || semid >= seminfo.semmni)
982 /* Allocate memory for sem_ops */
983 if (nsops <= SMALL_SOPS)
985 else if (nsops <= seminfo.semopm)
986 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
988 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
992 if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
993 DPRINTF(("error = %d from copyin(%08x, %08x, %d)\n", error,
994 uap->sops, sops, nsops * sizeof(sops[0])));
995 if (sops != small_sops)
1000 semakptr = &sema[semid];
1001 sema_mtxp = &sema_mtx[semid];
1002 mtx_lock(sema_mtxp);
1003 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
1007 if (semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1012 * Initial pass thru sops to see what permissions are needed.
1013 * Also perform any checks that don't need repeating on each
1014 * attempt to satisfy the request vector.
1016 j = 0; /* permission needed */
1018 for (i = 0; i < nsops; i++) {
1020 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1024 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1026 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1029 if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1030 DPRINTF(("error = %d from ipaccess\n", error));
1034 error = mac_check_sysv_semop(td->td_ucred, semakptr, j);
1036 MPRINTF(("mac_check_sysv_semop returned %d\n", error));
1042 * Loop trying to satisfy the vector of requests.
1043 * If we reach a point where we must wait, any requests already
1044 * performed are rolled back and we go to sleep until some other
1045 * process wakes us up. At this point, we start all over again.
1047 * This ensures that from the perspective of other tasks, a set
1048 * of requests is atomic (never partially satisfied).
1052 error = 0; /* error return if necessary */
1054 for (i = 0; i < nsops; i++) {
1056 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1059 "semop: semakptr=%x, sem_base=%x, "
1060 "semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1061 semakptr, semakptr->u.sem_base, semptr,
1062 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1063 (sopptr->sem_flg & IPC_NOWAIT) ?
1064 "nowait" : "wait"));
1066 if (sopptr->sem_op < 0) {
1067 if (semptr->semval + sopptr->sem_op < 0) {
1068 DPRINTF(("semop: can't do it now\n"));
1071 semptr->semval += sopptr->sem_op;
1072 if (semptr->semval == 0 &&
1073 semptr->semzcnt > 0)
1076 } else if (sopptr->sem_op == 0) {
1077 if (semptr->semval != 0) {
1078 DPRINTF(("semop: not zero now\n"));
1081 } else if (semptr->semval + sopptr->sem_op >
1086 if (semptr->semncnt > 0)
1088 semptr->semval += sopptr->sem_op;
1093 * Did we get through the entire vector?
1099 * No ... rollback anything that we've already done
1101 DPRINTF(("semop: rollback 0 through %d\n", i-1));
1102 for (j = 0; j < i; j++)
1103 semakptr->u.sem_base[sops[j].sem_num].semval -=
1106 /* If we detected an error, return it */
1111 * If the request that we couldn't satisfy has the
1112 * NOWAIT flag set then return with EAGAIN.
1114 if (sopptr->sem_flg & IPC_NOWAIT) {
1119 if (sopptr->sem_op == 0)
1124 DPRINTF(("semop: good night!\n"));
1125 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1127 DPRINTF(("semop: good morning (error=%d)!\n", error));
1128 /* return code is checked below, after sem[nz]cnt-- */
1131 * Make sure that the semaphore still exists
1133 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1134 semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1140 * The semaphore is still alive. Readjust the count of
1141 * waiting processes.
1143 if (sopptr->sem_op == 0)
1149 * Is it really morning, or was our sleep interrupted?
1150 * (Delayed check of msleep() return code because we
1151 * need to decrement sem[nz]cnt either way.)
1157 DPRINTF(("semop: good morning!\n"));
1162 * Process any SEM_UNDO requests.
1167 for (i = 0; i < nsops; i++) {
1169 * We only need to deal with SEM_UNDO's for non-zero
1174 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1176 adjval = sops[i].sem_op;
1179 error = semundo_adjust(td, &suptr, semid,
1180 sops[i].sem_num, -adjval);
1185 * Oh-Oh! We ran out of either sem_undo's or undo's.
1186 * Rollback the adjustments to this point and then
1187 * rollback the semaphore ups and down so we can return
1188 * with an error with all structures restored. We
1189 * rollback the undo's in the exact reverse order that
1190 * we applied them. This guarantees that we won't run
1191 * out of space as we roll things back out.
1193 for (j = 0; j < i; j++) {
1195 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1197 adjval = sops[k].sem_op;
1200 if (semundo_adjust(td, &suptr, semid,
1201 sops[k].sem_num, adjval) != 0)
1202 panic("semop - can't undo undos");
1205 for (j = 0; j < nsops; j++)
1206 semakptr->u.sem_base[sops[j].sem_num].semval -=
1209 DPRINTF(("error = %d from semundo_adjust\n", error));
1212 } /* loop through the sops */
1214 } /* if (do_undos) */
1216 /* We're definitely done - set the sempid's and time */
1217 for (i = 0; i < nsops; i++) {
1219 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1220 semptr->sempid = td->td_proc->p_pid;
1222 semakptr->u.sem_otime = time_second;
1225 * Do a wakeup if any semaphore was up'd whilst something was
1229 DPRINTF(("semop: doing wakeup\n"));
1231 DPRINTF(("semop: back from wakeup\n"));
1233 DPRINTF(("semop: done\n"));
1234 td->td_retval[0] = 0;
1236 mtx_unlock(sema_mtxp);
1237 if (sops != small_sops)
1243 * Go through the undo structures for this process and apply the adjustments to
1247 semexit_myhook(arg, p)
1251 struct sem_undo *suptr;
1252 struct sem_undo **supptr;
1255 * Go through the chain of undo vectors looking for one
1256 * associated with this process.
1259 SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
1260 if (suptr->un_proc == p)
1268 DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1272 * If there are any active undo elements then process them.
1274 if (suptr->un_cnt > 0) {
1277 for (ix = 0; ix < suptr->un_cnt; ix++) {
1278 int semid = suptr->un_ent[ix].un_id;
1279 int semnum = suptr->un_ent[ix].un_num;
1280 int adjval = suptr->un_ent[ix].un_adjval;
1281 struct semid_kernel *semakptr;
1282 struct mtx *sema_mtxp;
1284 semakptr = &sema[semid];
1285 sema_mtxp = &sema_mtx[semid];
1286 mtx_lock(sema_mtxp);
1288 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0)
1289 panic("semexit - semid not allocated");
1290 if (semnum >= semakptr->u.sem_nsems)
1291 panic("semexit - semnum out of range");
1294 "semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
1295 suptr->un_proc, suptr->un_ent[ix].un_id,
1296 suptr->un_ent[ix].un_num,
1297 suptr->un_ent[ix].un_adjval,
1298 semakptr->u.sem_base[semnum].semval));
1301 if (semakptr->u.sem_base[semnum].semval <
1303 semakptr->u.sem_base[semnum].semval = 0;
1305 semakptr->u.sem_base[semnum].semval +=
1308 semakptr->u.sem_base[semnum].semval += adjval;
1311 DPRINTF(("semexit: back from wakeup\n"));
1312 mtx_unlock(sema_mtxp);
1318 * Deallocate the undo vector.
1320 DPRINTF(("removing vector\n"));
1321 suptr->un_proc = NULL;
1322 *supptr = SLIST_NEXT(suptr, un_next);
1326 sysctl_sema(SYSCTL_HANDLER_ARGS)
1329 return (SYSCTL_OUT(req, sema,
1330 sizeof(struct semid_kernel) * seminfo.semmni));