]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/sysv_sem.c
This commit was generated by cvs2svn to compensate for changes in r141261,
[FreeBSD/FreeBSD.git] / sys / kern / sysv_sem.c
1 /*-
2  * Implementation of SVID semaphores
3  *
4  * Author:  Daniel Boulet
5  *
6  * This software is provided ``AS IS'' without any warranties of any kind.
7  */
8 /*-
9  * Copyright (c) 2003-2005 McAfee, Inc.
10  * All rights reserved.
11  *
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
15  * program.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_sysvipc.h"
43 #include "opt_mac.h"
44
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>
50 #include <sys/proc.h>
51 #include <sys/lock.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/sem.h>
55 #include <sys/syscall.h>
56 #include <sys/sysent.h>
57 #include <sys/sysctl.h>
58 #include <sys/malloc.h>
59 #include <sys/jail.h>
60 #include <sys/mac.h>
61
62 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
63
64 #ifdef SEM_DEBUG
65 #define DPRINTF(a)      printf a
66 #else
67 #define DPRINTF(a)
68 #endif
69 #ifdef MAC_DEBUG
70 #define MPRINTF(a)      printf a
71 #else
72 #define MPRINTF(a)
73 #endif
74
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);
81
82 #ifndef _SYS_SYSPROTO_H_
83 struct __semctl_args;
84 int __semctl(struct thread *td, struct __semctl_args *uap);
85 struct semget_args;
86 int semget(struct thread *td, struct semget_args *uap);
87 struct semop_args;
88 int semop(struct thread *td, struct semop_args *uap);
89 #endif
90
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);
95
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,
99         (sy_call_t *)semop
100 };
101
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;
110
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));
115
116 struct sem {
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 */
121 };
122
123 /*
124  * Undo structure (one per process)
125  */
126 struct sem_undo {
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 */
130         struct undo {
131                 short   un_adjval;      /* adjust on exit values */
132                 short   un_num;         /* semaphore # */
133                 int     un_id;          /* semid */
134         } un_ent[1];                    /* undo entries */
135 };
136
137 /*
138  * Configuration parameters
139  */
140 #ifndef SEMMNI
141 #define SEMMNI  10              /* # of semaphore identifiers */
142 #endif
143 #ifndef SEMMNS
144 #define SEMMNS  60              /* # of semaphores in system */
145 #endif
146 #ifndef SEMUME
147 #define SEMUME  10              /* max # of undo entries per process */
148 #endif
149 #ifndef SEMMNU
150 #define SEMMNU  30              /* # of undo structures in system */
151 #endif
152
153 /* shouldn't need tuning */
154 #ifndef SEMMAP
155 #define SEMMAP  30              /* # of entries in semaphore map */
156 #endif
157 #ifndef SEMMSL
158 #define SEMMSL  SEMMNS          /* max # of semaphores per id */
159 #endif
160 #ifndef SEMOPM
161 #define SEMOPM  100             /* max # of operations per semop call */
162 #endif
163
164 #define SEMVMX  32767           /* semaphore maximum value */
165 #define SEMAEM  16384           /* adjust on exit max value */
166
167 /*
168  * Due to the way semaphore memory is allocated, we have to ensure that
169  * SEMUSZ is properly aligned.
170  */
171
172 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
173
174 /* actual size of an undo structure */
175 #define SEMUSZ  SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
176
177 /*
178  * Macro to find a particular sem_undo vector
179  */
180 #define SEMU(ix) \
181         ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
182
183 /*
184  * semaphore info struct
185  */
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 */
197 };
198
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, "", "");
212
213 static void
214 seminit(void)
215 {
216         int i;
217
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);
228
229         sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
230         sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
231             M_WAITOK);
232         sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
233             M_WAITOK | M_ZERO);
234         semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
235
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;
240 #ifdef MAC
241                 mac_init_sysv_sema(&sema[i]);
242 #endif
243         }
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;
249         }
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);
254 }
255
256 static int
257 semunload(void)
258 {
259         int i;
260
261         if (semtot != 0)
262                 return (EBUSY);
263
264         EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
265 #ifdef MAC
266         for (i = 0; i < seminfo.semmni; i++)
267                 mac_destroy_sysv_sema(&sema[i]);
268 #endif
269         free(sem, M_SEM);
270         free(sema, M_SEM);
271         free(semu, M_SEM);
272         for (i = 0; i < seminfo.semmni; i++)
273                 mtx_destroy(&sema_mtx[i]);
274         mtx_destroy(&sem_mtx);
275         return (0);
276 }
277
278 static int
279 sysvsem_modload(struct module *module, int cmd, void *arg)
280 {
281         int error = 0;
282
283         switch (cmd) {
284         case MOD_LOAD:
285                 seminit();
286                 break;
287         case MOD_UNLOAD:
288                 error = semunload();
289                 break;
290         case MOD_SHUTDOWN:
291                 break;
292         default:
293                 error = EINVAL;
294                 break;
295         }
296         return (error);
297 }
298
299 static moduledata_t sysvsem_mod = {
300         "sysvsem",
301         &sysvsem_modload,
302         NULL
303 };
304
305 SYSCALL_MODULE_HELPER(semsys);
306 SYSCALL_MODULE_HELPER(__semctl);
307 SYSCALL_MODULE_HELPER(semget);
308 SYSCALL_MODULE_HELPER(semop);
309
310 DECLARE_MODULE(sysvsem, sysvsem_mod,
311         SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
312 MODULE_VERSION(sysvsem, 1);
313
314 /*
315  * Entry point for all SEM calls
316  *
317  * MPSAFE
318  */
319 int
320 semsys(td, uap)
321         struct thread *td;
322         /* XXX actually varargs. */
323         struct semsys_args /* {
324                 int     which;
325                 int     a2;
326                 int     a3;
327                 int     a4;
328                 int     a5;
329         } */ *uap;
330 {
331         int error;
332
333         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
334                 return (ENOSYS);
335         if (uap->which < 0 ||
336             uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
337                 return (EINVAL);
338         error = (*semcalls[uap->which])(td, &uap->a2);
339         return (error);
340 }
341
342 /*
343  * Allocate a new sem_undo structure for a process
344  * (returns ptr to structure or NULL if no more room)
345  */
346
347 static struct sem_undo *
348 semu_alloc(td)
349         struct thread *td;
350 {
351         int i;
352         struct sem_undo *suptr;
353         struct sem_undo **supptr;
354         int attempt;
355
356         SEMUNDO_LOCKASSERT(MA_OWNED);
357         /*
358          * Try twice to allocate something.
359          * (we'll purge an empty structure after the first pass so
360          * two passes are always enough)
361          */
362
363         for (attempt = 0; attempt < 2; attempt++) {
364                 /*
365                  * Look for a free structure.
366                  * Fill it in and return it if we find one.
367                  */
368
369                 for (i = 0; i < seminfo.semmnu; i++) {
370                         suptr = SEMU(i);
371                         if (suptr->un_proc == NULL) {
372                                 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
373                                 suptr->un_cnt = 0;
374                                 suptr->un_proc = td->td_proc;
375                                 return(suptr);
376                         }
377                 }
378
379                 /*
380                  * We didn't find a free one, if this is the first attempt
381                  * then try to free a structure.
382                  */
383
384                 if (attempt == 0) {
385                         /* All the structures are in use - try to free one */
386                         int did_something = 0;
387
388                         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
389                             un_next) {
390                                 if (suptr->un_cnt == 0) {
391                                         suptr->un_proc = NULL;
392                                         did_something = 1;
393                                         *supptr = SLIST_NEXT(suptr, un_next);
394                                         break;
395                                 }
396                         }
397
398                         /* If we didn't free anything then just give-up */
399                         if (!did_something)
400                                 return(NULL);
401                 } else {
402                         /*
403                          * The second pass failed even though we freed
404                          * something after the first pass!
405                          * This is IMPOSSIBLE!
406                          */
407                         panic("semu_alloc - second attempt failed");
408                 }
409         }
410         return (NULL);
411 }
412
413 /*
414  * Adjust a particular entry for a particular proc
415  */
416
417 static int
418 semundo_adjust(td, supptr, semid, semnum, adjval)
419         struct thread *td;
420         struct sem_undo **supptr;
421         int semid, semnum;
422         int adjval;
423 {
424         struct proc *p = td->td_proc;
425         struct sem_undo *suptr;
426         struct undo *sunptr;
427         int i;
428
429         SEMUNDO_LOCKASSERT(MA_OWNED);
430         /* Look for and remember the sem_undo if the caller doesn't provide
431            it */
432
433         suptr = *supptr;
434         if (suptr == NULL) {
435                 SLIST_FOREACH(suptr, &semu_list, un_next) {
436                         if (suptr->un_proc == p) {
437                                 *supptr = suptr;
438                                 break;
439                         }
440                 }
441                 if (suptr == NULL) {
442                         if (adjval == 0)
443                                 return(0);
444                         suptr = semu_alloc(td);
445                         if (suptr == NULL)
446                                 return(ENOSPC);
447                         *supptr = suptr;
448                 }
449         }
450
451         /*
452          * Look for the requested entry and adjust it (delete if adjval becomes
453          * 0).
454          */
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)
458                         continue;
459                 if (adjval != 0) {
460                         adjval += sunptr->un_adjval;
461                         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
462                                 return (ERANGE);
463                 }
464                 sunptr->un_adjval = adjval;
465                 if (sunptr->un_adjval == 0) {
466                         suptr->un_cnt--;
467                         if (i < suptr->un_cnt)
468                                 suptr->un_ent[i] =
469                                     suptr->un_ent[suptr->un_cnt];
470                 }
471                 return(0);
472         }
473
474         /* Didn't find the right entry - create it */
475         if (adjval == 0)
476                 return(0);
477         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
478                 return (ERANGE);
479         if (suptr->un_cnt != seminfo.semume) {
480                 sunptr = &suptr->un_ent[suptr->un_cnt];
481                 suptr->un_cnt++;
482                 sunptr->un_adjval = adjval;
483                 sunptr->un_id = semid; sunptr->un_num = semnum;
484         } else
485                 return(EINVAL);
486         return(0);
487 }
488
489 static void
490 semundo_clear(semid, semnum)
491         int semid, semnum;
492 {
493         struct sem_undo *suptr;
494
495         SEMUNDO_LOCKASSERT(MA_OWNED);
496         SLIST_FOREACH(suptr, &semu_list, un_next) {
497                 struct undo *sunptr = &suptr->un_ent[0];
498                 int i = 0;
499
500                 while (i < suptr->un_cnt) {
501                         if (sunptr->un_id == semid) {
502                                 if (semnum == -1 || sunptr->un_num == semnum) {
503                                         suptr->un_cnt--;
504                                         if (i < suptr->un_cnt) {
505                                                 suptr->un_ent[i] =
506                                                   suptr->un_ent[suptr->un_cnt];
507                                                 continue;
508                                         }
509                                 }
510                                 if (semnum != -1)
511                                         break;
512                         }
513                         i++, sunptr++;
514                 }
515         }
516 }
517
518 static int
519 semvalid(semid, semakptr)
520         int semid;
521         struct semid_kernel *semakptr;
522 {
523
524         return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
525             semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
526 }
527
528 /*
529  * Note that the user-mode half of this passes a union, not a pointer
530  */
531 #ifndef _SYS_SYSPROTO_H_
532 struct __semctl_args {
533         int     semid;
534         int     semnum;
535         int     cmd;
536         union   semun *arg;
537 };
538 #endif
539
540 /*
541  * MPSAFE
542  */
543 int
544 __semctl(td, uap)
545         struct thread *td;
546         struct __semctl_args *uap;
547 {
548         int semid = uap->semid;
549         int semnum = uap->semnum;
550         int cmd = uap->cmd;
551         u_short *array;
552         union semun *arg = uap->arg;
553         union semun real_arg;
554         struct ucred *cred = td->td_ucred;
555         int i, rval, error;
556         struct semid_ds sbuf;
557         struct semid_kernel *semakptr;
558         struct mtx *sema_mtxp;
559         u_short usval, count;
560
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))
564                 return (ENOSYS);
565
566         array = NULL;
567
568         switch(cmd) {
569         case SEM_STAT:
570                 if (semid < 0 || semid >= seminfo.semmni)
571                         return (EINVAL);
572                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
573                         return (error);
574                 semakptr = &sema[semid];
575                 sema_mtxp = &sema_mtx[semid];
576                 mtx_lock(sema_mtxp);
577                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
578                         error = EINVAL;
579                         goto done2;
580                 }
581                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
582                         goto done2;
583 #ifdef MAC
584                 error = mac_check_sysv_semctl(cred, semakptr, cmd);
585                 if (error != 0) {
586                         MPRINTF(("mac_check_sysv_semctl returned %d\n",
587                             error));
588                         goto done2;
589                 }
590 #endif
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);
595                 if (error == 0)
596                         td->td_retval[0] = rval;
597                 return (error);
598         }
599
600         semid = IPCID_TO_IX(semid);
601         if (semid < 0 || semid >= seminfo.semmni)
602                 return (EINVAL);
603
604         semakptr = &sema[semid];
605         sema_mtxp = &sema_mtx[semid];
606 #ifdef MAC
607         mtx_lock(sema_mtxp);
608         error = mac_check_sysv_semctl(cred, semakptr, cmd);
609         if (error != 0) {
610                 MPRINTF(("mac_check_sysv_semctl returned %d\n", error));
611                 mtx_unlock(sema_mtxp);
612                 return (error);
613         }
614         mtx_unlock(sema_mtxp);
615 #endif
616                 
617         error = 0;
618         rval = 0;
619
620         switch (cmd) {
621         case IPC_RMID:
622                 mtx_lock(sema_mtxp);
623                 if ((error = semvalid(uap->semid, semakptr)) != 0)
624                         goto done2;
625                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
626                         goto done2;
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;
636                 }
637                 semakptr->u.sem_perm.mode = 0;
638 #ifdef MAC
639                 mac_cleanup_sysv_sema(semakptr);
640 #endif
641                 SEMUNDO_LOCK();
642                 semundo_clear(semid, -1);
643                 SEMUNDO_UNLOCK();
644                 wakeup(semakptr);
645                 break;
646
647         case IPC_SET:
648                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
649                         goto done2;
650                 if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
651                         goto done2;
652                 mtx_lock(sema_mtxp);
653                 if ((error = semvalid(uap->semid, semakptr)) != 0)
654                         goto done2;
655                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
656                         goto done2;
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;
662                 break;
663
664         case IPC_STAT:
665                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
666                         goto done2;
667                 mtx_lock(sema_mtxp);
668                 if ((error = semvalid(uap->semid, semakptr)) != 0)
669                         goto done2;
670                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
671                         goto done2;
672                 sbuf = semakptr->u;
673                 mtx_unlock(sema_mtxp);
674                 error = copyout(&semakptr->u, real_arg.buf,
675                                 sizeof(struct semid_ds));
676                 break;
677
678         case GETNCNT:
679                 mtx_lock(sema_mtxp);
680                 if ((error = semvalid(uap->semid, semakptr)) != 0)
681                         goto done2;
682                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
683                         goto done2;
684                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
685                         error = EINVAL;
686                         goto done2;
687                 }
688                 rval = semakptr->u.sem_base[semnum].semncnt;
689                 break;
690
691         case GETPID:
692                 mtx_lock(sema_mtxp);
693                 if ((error = semvalid(uap->semid, semakptr)) != 0)
694                         goto done2;
695                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
696                         goto done2;
697                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
698                         error = EINVAL;
699                         goto done2;
700                 }
701                 rval = semakptr->u.sem_base[semnum].sempid;
702                 break;
703
704         case GETVAL:
705                 mtx_lock(sema_mtxp);
706                 if ((error = semvalid(uap->semid, semakptr)) != 0)
707                         goto done2;
708                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
709                         goto done2;
710                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
711                         error = EINVAL;
712                         goto done2;
713                 }
714                 rval = semakptr->u.sem_base[semnum].semval;
715                 break;
716
717         case GETALL:
718                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
719                         goto done2;
720                 array = malloc(sizeof(*array) * semakptr->u.sem_nsems, M_TEMP,
721                     M_WAITOK);
722                 mtx_lock(sema_mtxp);
723                 if ((error = semvalid(uap->semid, semakptr)) != 0)
724                         goto done2;
725                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
726                         goto done2;
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]));
732                 break;
733
734         case GETZCNT:
735                 mtx_lock(sema_mtxp);
736                 if ((error = semvalid(uap->semid, semakptr)) != 0)
737                         goto done2;
738                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
739                         goto done2;
740                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
741                         error = EINVAL;
742                         goto done2;
743                 }
744                 rval = semakptr->u.sem_base[semnum].semzcnt;
745                 break;
746
747         case SETVAL:
748                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
749                         goto done2;
750                 mtx_lock(sema_mtxp);
751                 if ((error = semvalid(uap->semid, semakptr)) != 0)
752                         goto done2;
753                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
754                         goto done2;
755                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
756                         error = EINVAL;
757                         goto done2;
758                 }
759                 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
760                         error = ERANGE;
761                         goto done2;
762                 }
763                 semakptr->u.sem_base[semnum].semval = real_arg.val;
764                 SEMUNDO_LOCK();
765                 semundo_clear(semid, semnum);
766                 SEMUNDO_UNLOCK();
767                 wakeup(semakptr);
768                 break;
769
770         case SETALL:
771                 mtx_lock(sema_mtxp);
772 raced:
773                 if ((error = semvalid(uap->semid, semakptr)) != 0)
774                         goto done2;
775                 count = semakptr->u.sem_nsems;
776                 mtx_unlock(sema_mtxp);
777                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
778                         goto done2;
779                 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
780                 copyin(real_arg.array, array, count * sizeof(*array));
781                 if (error)
782                         break;
783                 mtx_lock(sema_mtxp);
784                 if ((error = semvalid(uap->semid, semakptr)) != 0)
785                         goto done2;
786                 /* we could have raced? */
787                 if (count != semakptr->u.sem_nsems) {
788                         free(array, M_TEMP);
789                         array = NULL;
790                         goto raced;
791                 }
792                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
793                         goto done2;
794                 for (i = 0; i < semakptr->u.sem_nsems; i++) {
795                         usval = array[i];
796                         if (usval > seminfo.semvmx) {
797                                 error = ERANGE;
798                                 break;
799                         }
800                         semakptr->u.sem_base[i].semval = usval;
801                 }
802                 SEMUNDO_LOCK();
803                 semundo_clear(semid, -1);
804                 SEMUNDO_UNLOCK();
805                 wakeup(semakptr);
806                 break;
807
808         default:
809                 error = EINVAL;
810                 break;
811         }
812
813         if (error == 0)
814                 td->td_retval[0] = rval;
815 done2:
816         if (mtx_owned(sema_mtxp))
817                 mtx_unlock(sema_mtxp);
818         if (array != NULL)
819                 free(array, M_TEMP);
820         return(error);
821 }
822
823 #ifndef _SYS_SYSPROTO_H_
824 struct semget_args {
825         key_t   key;
826         int     nsems;
827         int     semflg;
828 };
829 #endif
830
831 /*
832  * MPSAFE
833  */
834 int
835 semget(td, uap)
836         struct thread *td;
837         struct semget_args *uap;
838 {
839         int semid, error = 0;
840         int key = uap->key;
841         int nsems = uap->nsems;
842         int semflg = uap->semflg;
843         struct ucred *cred = td->td_ucred;
844
845         DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
846         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
847                 return (ENOSYS);
848
849         mtx_lock(&Giant);
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)
854                                 break;
855                 }
856                 if (semid < seminfo.semmni) {
857                         DPRINTF(("found public key\n"));
858                         if ((error = ipcperm(td, &sema[semid].u.sem_perm,
859                             semflg & 0700))) {
860                                 goto done2;
861                         }
862                         if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
863                                 DPRINTF(("too small\n"));
864                                 error = EINVAL;
865                                 goto done2;
866                         }
867                         if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
868                                 DPRINTF(("not exclusive\n"));
869                                 error = EEXIST;
870                                 goto done2;
871                         }
872 #ifdef MAC
873                         error = mac_check_sysv_semget(cred, &sema[semid]);
874                         if (error != 0) {
875                                 MPRINTF(("mac_check_sysv_semget returned %d\n",
876                                     error));
877                                 goto done2;
878                         }
879 #endif
880                         goto found;
881                 }
882         }
883
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,
888                             seminfo.semmsl));
889                         error = EINVAL;
890                         goto done2;
891                 }
892                 if (nsems > seminfo.semmns - semtot) {
893                         DPRINTF((
894                             "not enough semaphores left (need %d, got %d)\n",
895                             nsems, seminfo.semmns - semtot));
896                         error = ENOSPC;
897                         goto done2;
898                 }
899                 for (semid = 0; semid < seminfo.semmni; semid++) {
900                         if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
901                                 break;
902                 }
903                 if (semid == seminfo.semmni) {
904                         DPRINTF(("no more semid_kernel's available\n"));
905                         error = ENOSPC;
906                         goto done2;
907                 }
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];
921                 semtot += nsems;
922                 bzero(sema[semid].u.sem_base,
923                     sizeof(sema[semid].u.sem_base[0])*nsems);
924 #ifdef MAC
925                 mac_create_sysv_sema(cred, &sema[semid]);
926 #endif
927                 DPRINTF(("sembase = 0x%x, next = 0x%x\n",
928                     sema[semid].u.sem_base, &sem[semtot]));
929         } else {
930                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
931                 error = ENOENT;
932                 goto done2;
933         }
934
935 found:
936         td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
937 done2:
938         mtx_unlock(&Giant);
939         return (error);
940 }
941
942 #ifndef _SYS_SYSPROTO_H_
943 struct semop_args {
944         int     semid;
945         struct  sembuf *sops;
946         size_t  nsops;
947 };
948 #endif
949
950 /*
951  * MPSAFE
952  */
953 int
954 semop(td, uap)
955         struct thread *td;
956         struct semop_args *uap;
957 {
958 #define SMALL_SOPS      8
959         struct sembuf small_sops[SMALL_SOPS];
960         int semid = uap->semid;
961         size_t nsops = uap->nsops;
962         struct sembuf *sops;
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;
968         size_t i, j, k;
969         int error;
970         int do_wakeup, do_undos;
971
972         DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops));
973
974         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
975                 return (ENOSYS);
976
977         semid = IPCID_TO_IX(semid);     /* Convert back to zero origin */
978
979         if (semid < 0 || semid >= seminfo.semmni)
980                 return (EINVAL);
981
982         /* Allocate memory for sem_ops */
983         if (nsops <= SMALL_SOPS)
984                 sops = small_sops;
985         else if (nsops <= seminfo.semopm)
986                 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
987         else {
988                 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
989                     nsops));
990                 return (E2BIG);
991         }
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)
996                         free(sops, M_SEM);
997                 return (error);
998         }
999
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) {
1004                 error = EINVAL;
1005                 goto done2;
1006         }
1007         if (semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1008                 error = EINVAL;
1009                 goto done2;
1010         }
1011         /*
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.
1015          */
1016         j = 0;          /* permission needed */
1017         do_undos = 0;
1018         for (i = 0; i < nsops; i++) {
1019                 sopptr = &sops[i];
1020                 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
1021                         error = EFBIG;
1022                         goto done2;
1023                 }
1024                 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
1025                         do_undos = 1;
1026                 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
1027         }
1028
1029         if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
1030                 DPRINTF(("error = %d from ipaccess\n", error));
1031                 goto done2;
1032         }
1033 #ifdef MAC
1034         error = mac_check_sysv_semop(td->td_ucred, semakptr, j);
1035         if (error != 0) {
1036                 MPRINTF(("mac_check_sysv_semop returned %d\n", error));
1037                 goto done2;
1038         }
1039 #endif
1040
1041         /*
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.
1046          *
1047          * This ensures that from the perspective of other tasks, a set
1048          * of requests is atomic (never partially satisfied).
1049          */
1050         for (;;) {
1051                 do_wakeup = 0;
1052                 error = 0;      /* error return if necessary */
1053
1054                 for (i = 0; i < nsops; i++) {
1055                         sopptr = &sops[i];
1056                         semptr = &semakptr->u.sem_base[sopptr->sem_num];
1057
1058                         DPRINTF((
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"));
1065
1066                         if (sopptr->sem_op < 0) {
1067                                 if (semptr->semval + sopptr->sem_op < 0) {
1068                                         DPRINTF(("semop:  can't do it now\n"));
1069                                         break;
1070                                 } else {
1071                                         semptr->semval += sopptr->sem_op;
1072                                         if (semptr->semval == 0 &&
1073                                             semptr->semzcnt > 0)
1074                                                 do_wakeup = 1;
1075                                 }
1076                         } else if (sopptr->sem_op == 0) {
1077                                 if (semptr->semval != 0) {
1078                                         DPRINTF(("semop:  not zero now\n"));
1079                                         break;
1080                                 }
1081                         } else if (semptr->semval + sopptr->sem_op >
1082                             seminfo.semvmx) {
1083                                 error = ERANGE;
1084                                 break;
1085                         } else {
1086                                 if (semptr->semncnt > 0)
1087                                         do_wakeup = 1;
1088                                 semptr->semval += sopptr->sem_op;
1089                         }
1090                 }
1091
1092                 /*
1093                  * Did we get through the entire vector?
1094                  */
1095                 if (i >= nsops)
1096                         goto done;
1097
1098                 /*
1099                  * No ... rollback anything that we've already done
1100                  */
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 -=
1104                             sops[j].sem_op;
1105
1106                 /* If we detected an error, return it */
1107                 if (error != 0)
1108                         goto done2;
1109
1110                 /*
1111                  * If the request that we couldn't satisfy has the
1112                  * NOWAIT flag set then return with EAGAIN.
1113                  */
1114                 if (sopptr->sem_flg & IPC_NOWAIT) {
1115                         error = EAGAIN;
1116                         goto done2;
1117                 }
1118
1119                 if (sopptr->sem_op == 0)
1120                         semptr->semzcnt++;
1121                 else
1122                         semptr->semncnt++;
1123
1124                 DPRINTF(("semop:  good night!\n"));
1125                 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
1126                     "semwait", 0);
1127                 DPRINTF(("semop:  good morning (error=%d)!\n", error));
1128                 /* return code is checked below, after sem[nz]cnt-- */
1129
1130                 /*
1131                  * Make sure that the semaphore still exists
1132                  */
1133                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1134                     semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
1135                         error = EIDRM;
1136                         goto done2;
1137                 }
1138
1139                 /*
1140                  * The semaphore is still alive.  Readjust the count of
1141                  * waiting processes.
1142                  */
1143                 if (sopptr->sem_op == 0)
1144                         semptr->semzcnt--;
1145                 else
1146                         semptr->semncnt--;
1147
1148                 /*
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.)
1152                  */
1153                 if (error != 0) {
1154                         error = EINTR;
1155                         goto done2;
1156                 }
1157                 DPRINTF(("semop:  good morning!\n"));
1158         }
1159
1160 done:
1161         /*
1162          * Process any SEM_UNDO requests.
1163          */
1164         if (do_undos) {
1165                 SEMUNDO_LOCK();
1166                 suptr = NULL;
1167                 for (i = 0; i < nsops; i++) {
1168                         /*
1169                          * We only need to deal with SEM_UNDO's for non-zero
1170                          * op's.
1171                          */
1172                         int adjval;
1173
1174                         if ((sops[i].sem_flg & SEM_UNDO) == 0)
1175                                 continue;
1176                         adjval = sops[i].sem_op;
1177                         if (adjval == 0)
1178                                 continue;
1179                         error = semundo_adjust(td, &suptr, semid,
1180                             sops[i].sem_num, -adjval);
1181                         if (error == 0)
1182                                 continue;
1183
1184                         /*
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.
1192                          */
1193                         for (j = 0; j < i; j++) {
1194                                 k = i - j - 1;
1195                                 if ((sops[k].sem_flg & SEM_UNDO) == 0)
1196                                         continue;
1197                                 adjval = sops[k].sem_op;
1198                                 if (adjval == 0)
1199                                         continue;
1200                                 if (semundo_adjust(td, &suptr, semid,
1201                                     sops[k].sem_num, adjval) != 0)
1202                                         panic("semop - can't undo undos");
1203                         }
1204
1205                         for (j = 0; j < nsops; j++)
1206                                 semakptr->u.sem_base[sops[j].sem_num].semval -=
1207                                     sops[j].sem_op;
1208
1209                         DPRINTF(("error = %d from semundo_adjust\n", error));
1210                         SEMUNDO_UNLOCK();
1211                         goto done2;
1212                 } /* loop through the sops */
1213                 SEMUNDO_UNLOCK();
1214         } /* if (do_undos) */
1215
1216         /* We're definitely done - set the sempid's and time */
1217         for (i = 0; i < nsops; i++) {
1218                 sopptr = &sops[i];
1219                 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1220                 semptr->sempid = td->td_proc->p_pid;
1221         }
1222         semakptr->u.sem_otime = time_second;
1223
1224         /*
1225          * Do a wakeup if any semaphore was up'd whilst something was
1226          * sleeping on it.
1227          */
1228         if (do_wakeup) {
1229                 DPRINTF(("semop:  doing wakeup\n"));
1230                 wakeup(semakptr);
1231                 DPRINTF(("semop:  back from wakeup\n"));
1232         }
1233         DPRINTF(("semop:  done\n"));
1234         td->td_retval[0] = 0;
1235 done2:
1236         mtx_unlock(sema_mtxp);
1237         if (sops != small_sops)
1238                 free(sops, M_SEM);
1239         return (error);
1240 }
1241
1242 /*
1243  * Go through the undo structures for this process and apply the adjustments to
1244  * semaphores.
1245  */
1246 static void
1247 semexit_myhook(arg, p)
1248         void *arg;
1249         struct proc *p;
1250 {
1251         struct sem_undo *suptr;
1252         struct sem_undo **supptr;
1253
1254         /*
1255          * Go through the chain of undo vectors looking for one
1256          * associated with this process.
1257          */
1258         SEMUNDO_LOCK();
1259         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
1260                 if (suptr->un_proc == p)
1261                         break;
1262         }
1263         SEMUNDO_UNLOCK();
1264
1265         if (suptr == NULL)
1266                 return;
1267
1268         DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
1269             suptr->un_cnt));
1270
1271         /*
1272          * If there are any active undo elements then process them.
1273          */
1274         if (suptr->un_cnt > 0) {
1275                 int ix;
1276
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;
1283
1284                         semakptr = &sema[semid];
1285                         sema_mtxp = &sema_mtx[semid];
1286                         mtx_lock(sema_mtxp);
1287                         SEMUNDO_LOCK();
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");
1292
1293                         DPRINTF((
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));
1299
1300                         if (adjval < 0) {
1301                                 if (semakptr->u.sem_base[semnum].semval <
1302                                     -adjval)
1303                                         semakptr->u.sem_base[semnum].semval = 0;
1304                                 else
1305                                         semakptr->u.sem_base[semnum].semval +=
1306                                             adjval;
1307                         } else
1308                                 semakptr->u.sem_base[semnum].semval += adjval;
1309
1310                         wakeup(semakptr);
1311                         DPRINTF(("semexit:  back from wakeup\n"));
1312                         mtx_unlock(sema_mtxp);
1313                         SEMUNDO_UNLOCK();
1314                 }
1315         }
1316
1317         /*
1318          * Deallocate the undo vector.
1319          */
1320         DPRINTF(("removing vector\n"));
1321         suptr->un_proc = NULL;
1322         *supptr = SLIST_NEXT(suptr, un_next);
1323 }
1324
1325 static int
1326 sysctl_sema(SYSCTL_HANDLER_ARGS)
1327 {
1328
1329         return (SYSCTL_OUT(req, sema,
1330             sizeof(struct semid_kernel) * seminfo.semmni));
1331 }