The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_sem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 #include <sys/cdefs.h>
   10 __FBSDID("$FreeBSD: src/sys/kern/sysv_sem.c,v 1.70.2.4 2005/05/05 18:53:13 rwatson Exp $");
   11 
   12 #include "opt_sysvipc.h"
   13 
   14 #include <sys/param.h>
   15 #include <sys/systm.h>
   16 #include <sys/sysproto.h>
   17 #include <sys/eventhandler.h>
   18 #include <sys/kernel.h>
   19 #include <sys/proc.h>
   20 #include <sys/lock.h>
   21 #include <sys/module.h>
   22 #include <sys/mutex.h>
   23 #include <sys/sem.h>
   24 #include <sys/syscall.h>
   25 #include <sys/sysent.h>
   26 #include <sys/sysctl.h>
   27 #include <sys/malloc.h>
   28 #include <sys/jail.h>
   29 
   30 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
   31 
   32 #ifdef SEM_DEBUG
   33 #define DPRINTF(a)      printf a
   34 #else
   35 #define DPRINTF(a)
   36 #endif
   37 
   38 static void seminit(void);
   39 static int sysvsem_modload(struct module *, int, void *);
   40 static int semunload(void);
   41 static void semexit_myhook(void *arg, struct proc *p);
   42 static int sysctl_sema(SYSCTL_HANDLER_ARGS);
   43 static int semvalid(int semid, struct semid_kernel *semakptr);
   44 
   45 #ifndef _SYS_SYSPROTO_H_
   46 struct __semctl_args;
   47 int __semctl(struct thread *td, struct __semctl_args *uap);
   48 struct semget_args;
   49 int semget(struct thread *td, struct semget_args *uap);
   50 struct semop_args;
   51 int semop(struct thread *td, struct semop_args *uap);
   52 #endif
   53 
   54 static struct sem_undo *semu_alloc(struct thread *td);
   55 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
   56                 int semid, int semnum, int adjval);
   57 static void semundo_clear(int semid, int semnum);
   58 
   59 /* XXX casting to (sy_call_t *) is bogus, as usual. */
   60 static sy_call_t *semcalls[] = {
   61         (sy_call_t *)__semctl, (sy_call_t *)semget,
   62         (sy_call_t *)semop
   63 };
   64 
   65 static struct mtx       sem_mtx;        /* semaphore global lock */
   66 static int      semtot = 0;
   67 static struct semid_kernel *sema;       /* semaphore id pool */
   68 static struct mtx *sema_mtx;    /* semaphore id pool mutexes*/
   69 static struct sem *sem;         /* semaphore pool */
   70 SLIST_HEAD(, sem_undo) semu_list;       /* list of active undo structures */
   71 static int      *semu;          /* undo structure pool */
   72 static eventhandler_tag semexit_tag;
   73 
   74 #define SEMUNDO_MTX             sem_mtx
   75 #define SEMUNDO_LOCK()          mtx_lock(&SEMUNDO_MTX);
   76 #define SEMUNDO_UNLOCK()        mtx_unlock(&SEMUNDO_MTX);
   77 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
   78 
   79 struct sem {
   80         u_short semval;         /* semaphore value */
   81         pid_t   sempid;         /* pid of last operation */
   82         u_short semncnt;        /* # awaiting semval > cval */
   83         u_short semzcnt;        /* # awaiting semval = 0 */
   84 };
   85 
   86 /*
   87  * Undo structure (one per process)
   88  */
   89 struct sem_undo {
   90         SLIST_ENTRY(sem_undo) un_next;  /* ptr to next active undo structure */
   91         struct  proc *un_proc;          /* owner of this structure */
   92         short   un_cnt;                 /* # of active entries */
   93         struct undo {
   94                 short   un_adjval;      /* adjust on exit values */
   95                 short   un_num;         /* semaphore # */
   96                 int     un_id;          /* semid */
   97         } un_ent[1];                    /* undo entries */
   98 };
   99 
  100 /*
  101  * Configuration parameters
  102  */
  103 #ifndef SEMMNI
  104 #define SEMMNI  10              /* # of semaphore identifiers */
  105 #endif
  106 #ifndef SEMMNS
  107 #define SEMMNS  60              /* # of semaphores in system */
  108 #endif
  109 #ifndef SEMUME
  110 #define SEMUME  10              /* max # of undo entries per process */
  111 #endif
  112 #ifndef SEMMNU
  113 #define SEMMNU  30              /* # of undo structures in system */
  114 #endif
  115 
  116 /* shouldn't need tuning */
  117 #ifndef SEMMAP
  118 #define SEMMAP  30              /* # of entries in semaphore map */
  119 #endif
  120 #ifndef SEMMSL
  121 #define SEMMSL  SEMMNS          /* max # of semaphores per id */
  122 #endif
  123 #ifndef SEMOPM
  124 #define SEMOPM  100             /* max # of operations per semop call */
  125 #endif
  126 
  127 #define SEMVMX  32767           /* semaphore maximum value */
  128 #define SEMAEM  16384           /* adjust on exit max value */
  129 
  130 /*
  131  * Due to the way semaphore memory is allocated, we have to ensure that
  132  * SEMUSZ is properly aligned.
  133  */
  134 
  135 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
  136 
  137 /* actual size of an undo structure */
  138 #define SEMUSZ  SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
  139 
  140 /*
  141  * Macro to find a particular sem_undo vector
  142  */
  143 #define SEMU(ix) \
  144         ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
  145 
  146 /*
  147  * semaphore info struct
  148  */
  149 struct seminfo seminfo = {
  150                 SEMMAP,         /* # of entries in semaphore map */
  151                 SEMMNI,         /* # of semaphore identifiers */
  152                 SEMMNS,         /* # of semaphores in system */
  153                 SEMMNU,         /* # of undo structures in system */
  154                 SEMMSL,         /* max # of semaphores per id */
  155                 SEMOPM,         /* max # of operations per semop call */
  156                 SEMUME,         /* max # of undo entries per process */
  157                 SEMUSZ,         /* size in bytes of undo structure */
  158                 SEMVMX,         /* semaphore maximum value */
  159                 SEMAEM          /* adjust on exit max value */
  160 };
  161 
  162 SYSCTL_DECL(_kern_ipc);
  163 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
  164     "Number of entries in the semaphore map");
  165 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RDTUN, &seminfo.semmni, 0,
  166     "Number of semaphore identifiers");
  167 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RDTUN, &seminfo.semmns, 0,
  168     "Maximum number of semaphores in the system");
  169 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RDTUN, &seminfo.semmnu, 0,
  170     "Maximum number of undo structures in the system");
  171 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
  172     "Max semaphores per id");
  173 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RDTUN, &seminfo.semopm, 0,
  174     "Max operations per semop call");
  175 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RDTUN, &seminfo.semume, 0,
  176     "Max undo entries per process");
  177 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RDTUN, &seminfo.semusz, 0,
  178     "Size in bytes of undo structure");
  179 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
  180     "Semaphore maximum value");
  181 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
  182     "Adjust on exit max value");
  183 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema, CTLFLAG_RD,
  184     NULL, 0, sysctl_sema, "", "");
  185 
  186 static void
  187 seminit(void)
  188 {
  189         int i;
  190 
  191         TUNABLE_INT_FETCH("kern.ipc.semmap", &seminfo.semmap);
  192         TUNABLE_INT_FETCH("kern.ipc.semmni", &seminfo.semmni);
  193         TUNABLE_INT_FETCH("kern.ipc.semmns", &seminfo.semmns);
  194         TUNABLE_INT_FETCH("kern.ipc.semmnu", &seminfo.semmnu);
  195         TUNABLE_INT_FETCH("kern.ipc.semmsl", &seminfo.semmsl);
  196         TUNABLE_INT_FETCH("kern.ipc.semopm", &seminfo.semopm);
  197         TUNABLE_INT_FETCH("kern.ipc.semume", &seminfo.semume);
  198         TUNABLE_INT_FETCH("kern.ipc.semusz", &seminfo.semusz);
  199         TUNABLE_INT_FETCH("kern.ipc.semvmx", &seminfo.semvmx);
  200         TUNABLE_INT_FETCH("kern.ipc.semaem", &seminfo.semaem);
  201 
  202         sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
  203         sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
  204             M_WAITOK);
  205         sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
  206             M_WAITOK | M_ZERO);
  207         semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
  208 
  209         for (i = 0; i < seminfo.semmni; i++) {
  210                 sema[i].u.sem_base = 0;
  211                 sema[i].u.sem_perm.mode = 0;
  212                 sema[i].u.sem_perm.seq = 0;
  213         }
  214         for (i = 0; i < seminfo.semmni; i++)
  215                 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
  216         for (i = 0; i < seminfo.semmnu; i++) {
  217                 struct sem_undo *suptr = SEMU(i);
  218                 suptr->un_proc = NULL;
  219         }
  220         SLIST_INIT(&semu_list);
  221         mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
  222         semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
  223             EVENTHANDLER_PRI_ANY);
  224 }
  225 
  226 static int
  227 semunload(void)
  228 {
  229         int i;
  230 
  231         if (semtot != 0)
  232                 return (EBUSY);
  233 
  234         EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
  235         free(sem, M_SEM);
  236         free(sema, M_SEM);
  237         free(semu, M_SEM);
  238         for (i = 0; i < seminfo.semmni; i++)
  239                 mtx_destroy(&sema_mtx[i]);
  240         mtx_destroy(&sem_mtx);
  241         return (0);
  242 }
  243 
  244 static int
  245 sysvsem_modload(struct module *module, int cmd, void *arg)
  246 {
  247         int error = 0;
  248 
  249         switch (cmd) {
  250         case MOD_LOAD:
  251                 seminit();
  252                 break;
  253         case MOD_UNLOAD:
  254                 error = semunload();
  255                 break;
  256         case MOD_SHUTDOWN:
  257                 break;
  258         default:
  259                 error = EINVAL;
  260                 break;
  261         }
  262         return (error);
  263 }
  264 
  265 static moduledata_t sysvsem_mod = {
  266         "sysvsem",
  267         &sysvsem_modload,
  268         NULL
  269 };
  270 
  271 SYSCALL_MODULE_HELPER(semsys);
  272 SYSCALL_MODULE_HELPER(__semctl);
  273 SYSCALL_MODULE_HELPER(semget);
  274 SYSCALL_MODULE_HELPER(semop);
  275 
  276 DECLARE_MODULE(sysvsem, sysvsem_mod,
  277         SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
  278 MODULE_VERSION(sysvsem, 1);
  279 
  280 /*
  281  * Entry point for all SEM calls
  282  *
  283  * MPSAFE
  284  */
  285 int
  286 semsys(td, uap)
  287         struct thread *td;
  288         /* XXX actually varargs. */
  289         struct semsys_args /* {
  290                 int     which;
  291                 int     a2;
  292                 int     a3;
  293                 int     a4;
  294                 int     a5;
  295         } */ *uap;
  296 {
  297         int error;
  298 
  299         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  300                 return (ENOSYS);
  301         if (uap->which < 0 ||
  302             uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
  303                 return (EINVAL);
  304         error = (*semcalls[uap->which])(td, &uap->a2);
  305         return (error);
  306 }
  307 
  308 /*
  309  * Allocate a new sem_undo structure for a process
  310  * (returns ptr to structure or NULL if no more room)
  311  */
  312 
  313 static struct sem_undo *
  314 semu_alloc(td)
  315         struct thread *td;
  316 {
  317         int i;
  318         struct sem_undo *suptr;
  319         struct sem_undo **supptr;
  320         int attempt;
  321 
  322         SEMUNDO_LOCKASSERT(MA_OWNED);
  323         /*
  324          * Try twice to allocate something.
  325          * (we'll purge an empty structure after the first pass so
  326          * two passes are always enough)
  327          */
  328 
  329         for (attempt = 0; attempt < 2; attempt++) {
  330                 /*
  331                  * Look for a free structure.
  332                  * Fill it in and return it if we find one.
  333                  */
  334 
  335                 for (i = 0; i < seminfo.semmnu; i++) {
  336                         suptr = SEMU(i);
  337                         if (suptr->un_proc == NULL) {
  338                                 SLIST_INSERT_HEAD(&semu_list, suptr, un_next);
  339                                 suptr->un_cnt = 0;
  340                                 suptr->un_proc = td->td_proc;
  341                                 return(suptr);
  342                         }
  343                 }
  344 
  345                 /*
  346                  * We didn't find a free one, if this is the first attempt
  347                  * then try to free a structure.
  348                  */
  349 
  350                 if (attempt == 0) {
  351                         /* All the structures are in use - try to free one */
  352                         int did_something = 0;
  353 
  354                         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list,
  355                             un_next) {
  356                                 if (suptr->un_cnt == 0) {
  357                                         suptr->un_proc = NULL;
  358                                         did_something = 1;
  359                                         *supptr = SLIST_NEXT(suptr, un_next);
  360                                         break;
  361                                 }
  362                         }
  363 
  364                         /* If we didn't free anything then just give-up */
  365                         if (!did_something)
  366                                 return(NULL);
  367                 } else {
  368                         /*
  369                          * The second pass failed even though we freed
  370                          * something after the first pass!
  371                          * This is IMPOSSIBLE!
  372                          */
  373                         panic("semu_alloc - second attempt failed");
  374                 }
  375         }
  376         return (NULL);
  377 }
  378 
  379 /*
  380  * Adjust a particular entry for a particular proc
  381  */
  382 
  383 static int
  384 semundo_adjust(td, supptr, semid, semnum, adjval)
  385         struct thread *td;
  386         struct sem_undo **supptr;
  387         int semid, semnum;
  388         int adjval;
  389 {
  390         struct proc *p = td->td_proc;
  391         struct sem_undo *suptr;
  392         struct undo *sunptr;
  393         int i;
  394 
  395         SEMUNDO_LOCKASSERT(MA_OWNED);
  396         /* Look for and remember the sem_undo if the caller doesn't provide
  397            it */
  398 
  399         suptr = *supptr;
  400         if (suptr == NULL) {
  401                 SLIST_FOREACH(suptr, &semu_list, un_next) {
  402                         if (suptr->un_proc == p) {
  403                                 *supptr = suptr;
  404                                 break;
  405                         }
  406                 }
  407                 if (suptr == NULL) {
  408                         if (adjval == 0)
  409                                 return(0);
  410                         suptr = semu_alloc(td);
  411                         if (suptr == NULL)
  412                                 return(ENOSPC);
  413                         *supptr = suptr;
  414                 }
  415         }
  416 
  417         /*
  418          * Look for the requested entry and adjust it (delete if adjval becomes
  419          * 0).
  420          */
  421         sunptr = &suptr->un_ent[0];
  422         for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
  423                 if (sunptr->un_id != semid || sunptr->un_num != semnum)
  424                         continue;
  425                 if (adjval != 0) {
  426                         adjval += sunptr->un_adjval;
  427                         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
  428                                 return (ERANGE);
  429                 }
  430                 sunptr->un_adjval = adjval;
  431                 if (sunptr->un_adjval == 0) {
  432                         suptr->un_cnt--;
  433                         if (i < suptr->un_cnt)
  434                                 suptr->un_ent[i] =
  435                                     suptr->un_ent[suptr->un_cnt];
  436                 }
  437                 return(0);
  438         }
  439 
  440         /* Didn't find the right entry - create it */
  441         if (adjval == 0)
  442                 return(0);
  443         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
  444                 return (ERANGE);
  445         if (suptr->un_cnt != seminfo.semume) {
  446                 sunptr = &suptr->un_ent[suptr->un_cnt];
  447                 suptr->un_cnt++;
  448                 sunptr->un_adjval = adjval;
  449                 sunptr->un_id = semid; sunptr->un_num = semnum;
  450         } else
  451                 return(EINVAL);
  452         return(0);
  453 }
  454 
  455 static void
  456 semundo_clear(semid, semnum)
  457         int semid, semnum;
  458 {
  459         struct sem_undo *suptr;
  460 
  461         SEMUNDO_LOCKASSERT(MA_OWNED);
  462         SLIST_FOREACH(suptr, &semu_list, un_next) {
  463                 struct undo *sunptr = &suptr->un_ent[0];
  464                 int i = 0;
  465 
  466                 while (i < suptr->un_cnt) {
  467                         if (sunptr->un_id == semid) {
  468                                 if (semnum == -1 || sunptr->un_num == semnum) {
  469                                         suptr->un_cnt--;
  470                                         if (i < suptr->un_cnt) {
  471                                                 suptr->un_ent[i] =
  472                                                   suptr->un_ent[suptr->un_cnt];
  473                                                 continue;
  474                                         }
  475                                 }
  476                                 if (semnum != -1)
  477                                         break;
  478                         }
  479                         i++, sunptr++;
  480                 }
  481         }
  482 }
  483 
  484 static int
  485 semvalid(semid, semakptr)
  486         int semid;
  487         struct semid_kernel *semakptr;
  488 {
  489 
  490         return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
  491             semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ? EINVAL : 0);
  492 }
  493 
  494 /*
  495  * Note that the user-mode half of this passes a union, not a pointer
  496  */
  497 #ifndef _SYS_SYSPROTO_H_
  498 struct __semctl_args {
  499         int     semid;
  500         int     semnum;
  501         int     cmd;
  502         union   semun *arg;
  503 };
  504 #endif
  505 
  506 /*
  507  * MPSAFE
  508  */
  509 int
  510 __semctl(td, uap)
  511         struct thread *td;
  512         struct __semctl_args *uap;
  513 {
  514         int semid = uap->semid;
  515         int semnum = uap->semnum;
  516         int cmd = uap->cmd;
  517         u_short *array;
  518         union semun *arg = uap->arg;
  519         union semun real_arg;
  520         struct ucred *cred = td->td_ucred;
  521         int i, rval, error;
  522         struct semid_ds sbuf;
  523         struct semid_kernel *semakptr;
  524         struct mtx *sema_mtxp;
  525         u_short usval, count;
  526 
  527         DPRINTF(("call to semctl(%d, %d, %d, 0x%x)\n",
  528             semid, semnum, cmd, arg));
  529         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  530                 return (ENOSYS);
  531 
  532         array = NULL;
  533 
  534         switch(cmd) {
  535         case SEM_STAT:
  536                 if (semid < 0 || semid >= seminfo.semmni)
  537                         return (EINVAL);
  538                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  539                         return (error);
  540                 semakptr = &sema[semid];
  541                 sema_mtxp = &sema_mtx[semid];
  542                 mtx_lock(sema_mtxp);
  543                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
  544                         error = EINVAL;
  545                         goto done2;
  546                 }
  547                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  548                         goto done2;
  549                 mtx_unlock(sema_mtxp);
  550                 error = copyout(&semakptr->u, real_arg.buf,
  551                     sizeof(struct semid_ds));
  552                 rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
  553                 if (error == 0)
  554                         td->td_retval[0] = rval;
  555                 return (error);
  556         }
  557 
  558         semid = IPCID_TO_IX(semid);
  559         if (semid < 0 || semid >= seminfo.semmni)
  560                 return (EINVAL);
  561 
  562         semakptr = &sema[semid];
  563         sema_mtxp = &sema_mtx[semid];
  564                 
  565         error = 0;
  566         rval = 0;
  567 
  568         switch (cmd) {
  569         case IPC_RMID:
  570                 mtx_lock(sema_mtxp);
  571                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  572                         goto done2;
  573                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
  574                         goto done2;
  575                 semakptr->u.sem_perm.cuid = cred->cr_uid;
  576                 semakptr->u.sem_perm.uid = cred->cr_uid;
  577                 semtot -= semakptr->u.sem_nsems;
  578                 for (i = semakptr->u.sem_base - sem; i < semtot; i++)
  579                         sem[i] = sem[i + semakptr->u.sem_nsems];
  580                 for (i = 0; i < seminfo.semmni; i++) {
  581                         if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
  582                             sema[i].u.sem_base > semakptr->u.sem_base)
  583                                 sema[i].u.sem_base -= semakptr->u.sem_nsems;
  584                 }
  585                 semakptr->u.sem_perm.mode = 0;
  586                 SEMUNDO_LOCK();
  587                 semundo_clear(semid, -1);
  588                 SEMUNDO_UNLOCK();
  589                 wakeup(semakptr);
  590                 break;
  591 
  592         case IPC_SET:
  593                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  594                         goto done2;
  595                 if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
  596                         goto done2;
  597                 mtx_lock(sema_mtxp);
  598                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  599                         goto done2;
  600                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
  601                         goto done2;
  602                 semakptr->u.sem_perm.uid = sbuf.sem_perm.uid;
  603                 semakptr->u.sem_perm.gid = sbuf.sem_perm.gid;
  604                 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
  605                     ~0777) | (sbuf.sem_perm.mode & 0777);
  606                 semakptr->u.sem_ctime = time_second;
  607                 break;
  608 
  609         case IPC_STAT:
  610                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  611                         goto done2;
  612                 mtx_lock(sema_mtxp);
  613                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  614                         goto done2;
  615                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  616                         goto done2;
  617                 sbuf = semakptr->u;
  618                 mtx_unlock(sema_mtxp);
  619                 error = copyout(&semakptr->u, real_arg.buf,
  620                                 sizeof(struct semid_ds));
  621                 break;
  622 
  623         case GETNCNT:
  624                 mtx_lock(sema_mtxp);
  625                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  626                         goto done2;
  627                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  628                         goto done2;
  629                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  630                         error = EINVAL;
  631                         goto done2;
  632                 }
  633                 rval = semakptr->u.sem_base[semnum].semncnt;
  634                 break;
  635 
  636         case GETPID:
  637                 mtx_lock(sema_mtxp);
  638                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  639                         goto done2;
  640                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  641                         goto done2;
  642                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  643                         error = EINVAL;
  644                         goto done2;
  645                 }
  646                 rval = semakptr->u.sem_base[semnum].sempid;
  647                 break;
  648 
  649         case GETVAL:
  650                 mtx_lock(sema_mtxp);
  651                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  652                         goto done2;
  653                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  654                         goto done2;
  655                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  656                         error = EINVAL;
  657                         goto done2;
  658                 }
  659                 rval = semakptr->u.sem_base[semnum].semval;
  660                 break;
  661 
  662         case GETALL:
  663                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  664                         goto done2;
  665                 array = malloc(sizeof(*array) * semakptr->u.sem_nsems, M_TEMP,
  666                     M_WAITOK);
  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                 for (i = 0; i < semakptr->u.sem_nsems; i++)
  673                         array[i] = semakptr->u.sem_base[i].semval;
  674                 mtx_unlock(sema_mtxp);
  675                 error = copyout(array, real_arg.array,
  676                     i * sizeof(real_arg.array[0]));
  677                 break;
  678 
  679         case GETZCNT:
  680                 mtx_lock(sema_mtxp);
  681                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  682                         goto done2;
  683                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  684                         goto done2;
  685                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  686                         error = EINVAL;
  687                         goto done2;
  688                 }
  689                 rval = semakptr->u.sem_base[semnum].semzcnt;
  690                 break;
  691 
  692         case SETVAL:
  693                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  694                         goto done2;
  695                 mtx_lock(sema_mtxp);
  696                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  697                         goto done2;
  698                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
  699                         goto done2;
  700                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  701                         error = EINVAL;
  702                         goto done2;
  703                 }
  704                 if (real_arg.val < 0 || real_arg.val > seminfo.semvmx) {
  705                         error = ERANGE;
  706                         goto done2;
  707                 }
  708                 semakptr->u.sem_base[semnum].semval = real_arg.val;
  709                 SEMUNDO_LOCK();
  710                 semundo_clear(semid, semnum);
  711                 SEMUNDO_UNLOCK();
  712                 wakeup(semakptr);
  713                 break;
  714 
  715         case SETALL:
  716                 mtx_lock(sema_mtxp);
  717 raced:
  718                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  719                         goto done2;
  720                 count = semakptr->u.sem_nsems;
  721                 mtx_unlock(sema_mtxp);
  722                 if ((error = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  723                         goto done2;
  724                 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
  725                 error = copyin(real_arg.array, array, count * sizeof(*array));
  726                 if (error)
  727                         break;
  728                 mtx_lock(sema_mtxp);
  729                 if ((error = semvalid(uap->semid, semakptr)) != 0)
  730                         goto done2;
  731                 /* we could have raced? */
  732                 if (count != semakptr->u.sem_nsems) {
  733                         free(array, M_TEMP);
  734                         array = NULL;
  735                         goto raced;
  736                 }
  737                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
  738                         goto done2;
  739                 for (i = 0; i < semakptr->u.sem_nsems; i++) {
  740                         usval = array[i];
  741                         if (usval > seminfo.semvmx) {
  742                                 error = ERANGE;
  743                                 break;
  744                         }
  745                         semakptr->u.sem_base[i].semval = usval;
  746                 }
  747                 SEMUNDO_LOCK();
  748                 semundo_clear(semid, -1);
  749                 SEMUNDO_UNLOCK();
  750                 wakeup(semakptr);
  751                 break;
  752 
  753         default:
  754                 error = EINVAL;
  755                 break;
  756         }
  757 
  758         if (error == 0)
  759                 td->td_retval[0] = rval;
  760 done2:
  761         if (mtx_owned(sema_mtxp))
  762                 mtx_unlock(sema_mtxp);
  763         if (array != NULL)
  764                 free(array, M_TEMP);
  765         return(error);
  766 }
  767 
  768 #ifndef _SYS_SYSPROTO_H_
  769 struct semget_args {
  770         key_t   key;
  771         int     nsems;
  772         int     semflg;
  773 };
  774 #endif
  775 
  776 /*
  777  * MPSAFE
  778  */
  779 int
  780 semget(td, uap)
  781         struct thread *td;
  782         struct semget_args *uap;
  783 {
  784         int semid, error = 0;
  785         int key = uap->key;
  786         int nsems = uap->nsems;
  787         int semflg = uap->semflg;
  788         struct ucred *cred = td->td_ucred;
  789 
  790         DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
  791         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  792                 return (ENOSYS);
  793 
  794         mtx_lock(&Giant);
  795         if (key != IPC_PRIVATE) {
  796                 for (semid = 0; semid < seminfo.semmni; semid++) {
  797                         if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
  798                             sema[semid].u.sem_perm.key == key)
  799                                 break;
  800                 }
  801                 if (semid < seminfo.semmni) {
  802                         DPRINTF(("found public key\n"));
  803                         if ((error = ipcperm(td, &sema[semid].u.sem_perm,
  804                             semflg & 0700))) {
  805                                 goto done2;
  806                         }
  807                         if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
  808                                 DPRINTF(("too small\n"));
  809                                 error = EINVAL;
  810                                 goto done2;
  811                         }
  812                         if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
  813                                 DPRINTF(("not exclusive\n"));
  814                                 error = EEXIST;
  815                                 goto done2;
  816                         }
  817                         goto found;
  818                 }
  819         }
  820 
  821         DPRINTF(("need to allocate the semid_kernel\n"));
  822         if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
  823                 if (nsems <= 0 || nsems > seminfo.semmsl) {
  824                         DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
  825                             seminfo.semmsl));
  826                         error = EINVAL;
  827                         goto done2;
  828                 }
  829                 if (nsems > seminfo.semmns - semtot) {
  830                         DPRINTF((
  831                             "not enough semaphores left (need %d, got %d)\n",
  832                             nsems, seminfo.semmns - semtot));
  833                         error = ENOSPC;
  834                         goto done2;
  835                 }
  836                 for (semid = 0; semid < seminfo.semmni; semid++) {
  837                         if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
  838                                 break;
  839                 }
  840                 if (semid == seminfo.semmni) {
  841                         DPRINTF(("no more semid_kernel's available\n"));
  842                         error = ENOSPC;
  843                         goto done2;
  844                 }
  845                 DPRINTF(("semid %d is available\n", semid));
  846                 sema[semid].u.sem_perm.key = key;
  847                 sema[semid].u.sem_perm.cuid = cred->cr_uid;
  848                 sema[semid].u.sem_perm.uid = cred->cr_uid;
  849                 sema[semid].u.sem_perm.cgid = cred->cr_gid;
  850                 sema[semid].u.sem_perm.gid = cred->cr_gid;
  851                 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
  852                 sema[semid].u.sem_perm.seq =
  853                     (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
  854                 sema[semid].u.sem_nsems = nsems;
  855                 sema[semid].u.sem_otime = 0;
  856                 sema[semid].u.sem_ctime = time_second;
  857                 sema[semid].u.sem_base = &sem[semtot];
  858                 semtot += nsems;
  859                 bzero(sema[semid].u.sem_base,
  860                     sizeof(sema[semid].u.sem_base[0])*nsems);
  861                 DPRINTF(("sembase = 0x%x, next = 0x%x\n",
  862                     sema[semid].u.sem_base, &sem[semtot]));
  863         } else {
  864                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
  865                 error = ENOENT;
  866                 goto done2;
  867         }
  868 
  869 found:
  870         td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
  871 done2:
  872         mtx_unlock(&Giant);
  873         return (error);
  874 }
  875 
  876 #ifndef _SYS_SYSPROTO_H_
  877 struct semop_args {
  878         int     semid;
  879         struct  sembuf *sops;
  880         size_t  nsops;
  881 };
  882 #endif
  883 
  884 /*
  885  * MPSAFE
  886  */
  887 int
  888 semop(td, uap)
  889         struct thread *td;
  890         struct semop_args *uap;
  891 {
  892 #define SMALL_SOPS      8
  893         struct sembuf small_sops[SMALL_SOPS];
  894         int semid = uap->semid;
  895         size_t nsops = uap->nsops;
  896         struct sembuf *sops;
  897         struct semid_kernel *semakptr;
  898         struct sembuf *sopptr = 0;
  899         struct sem *semptr = 0;
  900         struct sem_undo *suptr;
  901         struct mtx *sema_mtxp;
  902         size_t i, j, k;
  903         int error;
  904         int do_wakeup, do_undos;
  905 
  906         DPRINTF(("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops));
  907 
  908         if (!jail_sysvipc_allowed && jailed(td->td_ucred))
  909                 return (ENOSYS);
  910 
  911         semid = IPCID_TO_IX(semid);     /* Convert back to zero origin */
  912 
  913         if (semid < 0 || semid >= seminfo.semmni)
  914                 return (EINVAL);
  915 
  916         /* Allocate memory for sem_ops */
  917         if (nsops <= SMALL_SOPS)
  918                 sops = small_sops;
  919         else if (nsops <= seminfo.semopm)
  920                 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
  921         else {
  922                 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
  923                     nsops));
  924                 return (E2BIG);
  925         }
  926         if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
  927                 DPRINTF(("error = %d from copyin(%08x, %08x, %d)\n", error,
  928                     uap->sops, sops, nsops * sizeof(sops[0])));
  929                 if (sops != small_sops)
  930                         free(sops, M_SEM);
  931                 return (error);
  932         }
  933 
  934         semakptr = &sema[semid];
  935         sema_mtxp = &sema_mtx[semid];
  936         mtx_lock(sema_mtxp);
  937         if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
  938                 error = EINVAL;
  939                 goto done2;
  940         }
  941         if (semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
  942                 error = EINVAL;
  943                 goto done2;
  944         }
  945         /*
  946          * Initial pass thru sops to see what permissions are needed.
  947          * Also perform any checks that don't need repeating on each
  948          * attempt to satisfy the request vector.
  949          */
  950         j = 0;          /* permission needed */
  951         do_undos = 0;
  952         for (i = 0; i < nsops; i++) {
  953                 sopptr = &sops[i];
  954                 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
  955                         error = EFBIG;
  956                         goto done2;
  957                 }
  958                 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
  959                         do_undos = 1;
  960                 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
  961         }
  962 
  963         if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
  964                 DPRINTF(("error = %d from ipaccess\n", error));
  965                 goto done2;
  966         }
  967 
  968         /*
  969          * Loop trying to satisfy the vector of requests.
  970          * If we reach a point where we must wait, any requests already
  971          * performed are rolled back and we go to sleep until some other
  972          * process wakes us up.  At this point, we start all over again.
  973          *
  974          * This ensures that from the perspective of other tasks, a set
  975          * of requests is atomic (never partially satisfied).
  976          */
  977         for (;;) {
  978                 do_wakeup = 0;
  979                 error = 0;      /* error return if necessary */
  980 
  981                 for (i = 0; i < nsops; i++) {
  982                         sopptr = &sops[i];
  983                         semptr = &semakptr->u.sem_base[sopptr->sem_num];
  984 
  985                         DPRINTF((
  986                             "semop:  semakptr=%x, sem_base=%x, "
  987                             "semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
  988                             semakptr, semakptr->u.sem_base, semptr,
  989                             sopptr->sem_num, semptr->semval, sopptr->sem_op,
  990                             (sopptr->sem_flg & IPC_NOWAIT) ?
  991                             "nowait" : "wait"));
  992 
  993                         if (sopptr->sem_op < 0) {
  994                                 if (semptr->semval + sopptr->sem_op < 0) {
  995                                         DPRINTF(("semop:  can't do it now\n"));
  996                                         break;
  997                                 } else {
  998                                         semptr->semval += sopptr->sem_op;
  999                                         if (semptr->semval == 0 &&
 1000                                             semptr->semzcnt > 0)
 1001                                                 do_wakeup = 1;
 1002                                 }
 1003                         } else if (sopptr->sem_op == 0) {
 1004                                 if (semptr->semval != 0) {
 1005                                         DPRINTF(("semop:  not zero now\n"));
 1006                                         break;
 1007                                 }
 1008                         } else if (semptr->semval + sopptr->sem_op >
 1009                             seminfo.semvmx) {
 1010                                 error = ERANGE;
 1011                                 break;
 1012                         } else {
 1013                                 if (semptr->semncnt > 0)
 1014                                         do_wakeup = 1;
 1015                                 semptr->semval += sopptr->sem_op;
 1016                         }
 1017                 }
 1018 
 1019                 /*
 1020                  * Did we get through the entire vector?
 1021                  */
 1022                 if (i >= nsops)
 1023                         goto done;
 1024 
 1025                 /*
 1026                  * No ... rollback anything that we've already done
 1027                  */
 1028                 DPRINTF(("semop:  rollback 0 through %d\n", i-1));
 1029                 for (j = 0; j < i; j++)
 1030                         semakptr->u.sem_base[sops[j].sem_num].semval -=
 1031                             sops[j].sem_op;
 1032 
 1033                 /* If we detected an error, return it */
 1034                 if (error != 0)
 1035                         goto done2;
 1036 
 1037                 /*
 1038                  * If the request that we couldn't satisfy has the
 1039                  * NOWAIT flag set then return with EAGAIN.
 1040                  */
 1041                 if (sopptr->sem_flg & IPC_NOWAIT) {
 1042                         error = EAGAIN;
 1043                         goto done2;
 1044                 }
 1045 
 1046                 if (sopptr->sem_op == 0)
 1047                         semptr->semzcnt++;
 1048                 else
 1049                         semptr->semncnt++;
 1050 
 1051                 DPRINTF(("semop:  good night!\n"));
 1052                 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
 1053                     "semwait", 0);
 1054                 DPRINTF(("semop:  good morning (error=%d)!\n", error));
 1055                 /* return code is checked below, after sem[nz]cnt-- */
 1056 
 1057                 /*
 1058                  * Make sure that the semaphore still exists
 1059                  */
 1060                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
 1061                     semakptr->u.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
 1062                         error = EIDRM;
 1063                         goto done2;
 1064                 }
 1065 
 1066                 /*
 1067                  * The semaphore is still alive.  Readjust the count of
 1068                  * waiting processes.
 1069                  */
 1070                 if (sopptr->sem_op == 0)
 1071                         semptr->semzcnt--;
 1072                 else
 1073                         semptr->semncnt--;
 1074 
 1075                 /*
 1076                  * Is it really morning, or was our sleep interrupted?
 1077                  * (Delayed check of msleep() return code because we
 1078                  * need to decrement sem[nz]cnt either way.)
 1079                  */
 1080                 if (error != 0) {
 1081                         error = EINTR;
 1082                         goto done2;
 1083                 }
 1084                 DPRINTF(("semop:  good morning!\n"));
 1085         }
 1086 
 1087 done:
 1088         /*
 1089          * Process any SEM_UNDO requests.
 1090          */
 1091         if (do_undos) {
 1092                 SEMUNDO_LOCK();
 1093                 suptr = NULL;
 1094                 for (i = 0; i < nsops; i++) {
 1095                         /*
 1096                          * We only need to deal with SEM_UNDO's for non-zero
 1097                          * op's.
 1098                          */
 1099                         int adjval;
 1100 
 1101                         if ((sops[i].sem_flg & SEM_UNDO) == 0)
 1102                                 continue;
 1103                         adjval = sops[i].sem_op;
 1104                         if (adjval == 0)
 1105                                 continue;
 1106                         error = semundo_adjust(td, &suptr, semid,
 1107                             sops[i].sem_num, -adjval);
 1108                         if (error == 0)
 1109                                 continue;
 1110 
 1111                         /*
 1112                          * Oh-Oh!  We ran out of either sem_undo's or undo's.
 1113                          * Rollback the adjustments to this point and then
 1114                          * rollback the semaphore ups and down so we can return
 1115                          * with an error with all structures restored.  We
 1116                          * rollback the undo's in the exact reverse order that
 1117                          * we applied them.  This guarantees that we won't run
 1118                          * out of space as we roll things back out.
 1119                          */
 1120                         for (j = 0; j < i; j++) {
 1121                                 k = i - j - 1;
 1122                                 if ((sops[k].sem_flg & SEM_UNDO) == 0)
 1123                                         continue;
 1124                                 adjval = sops[k].sem_op;
 1125                                 if (adjval == 0)
 1126                                         continue;
 1127                                 if (semundo_adjust(td, &suptr, semid,
 1128                                     sops[k].sem_num, adjval) != 0)
 1129                                         panic("semop - can't undo undos");
 1130                         }
 1131 
 1132                         for (j = 0; j < nsops; j++)
 1133                                 semakptr->u.sem_base[sops[j].sem_num].semval -=
 1134                                     sops[j].sem_op;
 1135 
 1136                         DPRINTF(("error = %d from semundo_adjust\n", error));
 1137                         SEMUNDO_UNLOCK();
 1138                         goto done2;
 1139                 } /* loop through the sops */
 1140                 SEMUNDO_UNLOCK();
 1141         } /* if (do_undos) */
 1142 
 1143         /* We're definitely done - set the sempid's and time */
 1144         for (i = 0; i < nsops; i++) {
 1145                 sopptr = &sops[i];
 1146                 semptr = &semakptr->u.sem_base[sopptr->sem_num];
 1147                 semptr->sempid = td->td_proc->p_pid;
 1148         }
 1149         semakptr->u.sem_otime = time_second;
 1150 
 1151         /*
 1152          * Do a wakeup if any semaphore was up'd whilst something was
 1153          * sleeping on it.
 1154          */
 1155         if (do_wakeup) {
 1156                 DPRINTF(("semop:  doing wakeup\n"));
 1157                 wakeup(semakptr);
 1158                 DPRINTF(("semop:  back from wakeup\n"));
 1159         }
 1160         DPRINTF(("semop:  done\n"));
 1161         td->td_retval[0] = 0;
 1162 done2:
 1163         mtx_unlock(sema_mtxp);
 1164         if (sops != small_sops)
 1165                 free(sops, M_SEM);
 1166         return (error);
 1167 }
 1168 
 1169 /*
 1170  * Go through the undo structures for this process and apply the adjustments to
 1171  * semaphores.
 1172  */
 1173 static void
 1174 semexit_myhook(arg, p)
 1175         void *arg;
 1176         struct proc *p;
 1177 {
 1178         struct sem_undo *suptr;
 1179         struct sem_undo **supptr;
 1180 
 1181         /*
 1182          * Go through the chain of undo vectors looking for one
 1183          * associated with this process.
 1184          */
 1185         SEMUNDO_LOCK();
 1186         SLIST_FOREACH_PREVPTR(suptr, supptr, &semu_list, un_next) {
 1187                 if (suptr->un_proc == p)
 1188                         break;
 1189         }
 1190         SEMUNDO_UNLOCK();
 1191 
 1192         if (suptr == NULL)
 1193                 return;
 1194 
 1195         DPRINTF(("proc @%08x has undo structure with %d entries\n", p,
 1196             suptr->un_cnt));
 1197 
 1198         /*
 1199          * If there are any active undo elements then process them.
 1200          */
 1201         if (suptr->un_cnt > 0) {
 1202                 int ix;
 1203 
 1204                 for (ix = 0; ix < suptr->un_cnt; ix++) {
 1205                         int semid = suptr->un_ent[ix].un_id;
 1206                         int semnum = suptr->un_ent[ix].un_num;
 1207                         int adjval = suptr->un_ent[ix].un_adjval;
 1208                         struct semid_kernel *semakptr;
 1209                         struct mtx *sema_mtxp;
 1210 
 1211                         semakptr = &sema[semid];
 1212                         sema_mtxp = &sema_mtx[semid];
 1213                         mtx_lock(sema_mtxp);
 1214                         SEMUNDO_LOCK();
 1215                         if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0)
 1216                                 panic("semexit - semid not allocated");
 1217                         if (semnum >= semakptr->u.sem_nsems)
 1218                                 panic("semexit - semnum out of range");
 1219 
 1220                         DPRINTF((
 1221                             "semexit:  %08x id=%d num=%d(adj=%d) ; sem=%d\n",
 1222                             suptr->un_proc, suptr->un_ent[ix].un_id,
 1223                             suptr->un_ent[ix].un_num,
 1224                             suptr->un_ent[ix].un_adjval,
 1225                             semakptr->u.sem_base[semnum].semval));
 1226 
 1227                         if (adjval < 0) {
 1228                                 if (semakptr->u.sem_base[semnum].semval <
 1229                                     -adjval)
 1230                                         semakptr->u.sem_base[semnum].semval = 0;
 1231                                 else
 1232                                         semakptr->u.sem_base[semnum].semval +=
 1233                                             adjval;
 1234                         } else
 1235                                 semakptr->u.sem_base[semnum].semval += adjval;
 1236 
 1237                         wakeup(semakptr);
 1238                         DPRINTF(("semexit:  back from wakeup\n"));
 1239                         mtx_unlock(sema_mtxp);
 1240                         SEMUNDO_UNLOCK();
 1241                 }
 1242         }
 1243 
 1244         /*
 1245          * Deallocate the undo vector.
 1246          */
 1247         DPRINTF(("removing vector\n"));
 1248         suptr->un_proc = NULL;
 1249         *supptr = SLIST_NEXT(suptr, un_next);
 1250 }
 1251 
 1252 static int
 1253 sysctl_sema(SYSCTL_HANDLER_ARGS)
 1254 {
 1255 
 1256         return (SYSCTL_OUT(req, sema,
 1257             sizeof(struct semid_kernel) * seminfo.semmni));
 1258 }

Cache object: 922b2b67cacd6ecf2b52f6004aa4f72f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.