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

Cache object: 7d0fad110103fce7aad9df1597150c81


[ 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.