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/11.2/sys/kern/sysv_sem.c 329739 2018-02-21 18:31:21Z brooks $");
   41 
   42 #include "opt_compat.h"
   43 #include "opt_sysvipc.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/racct.h>
   55 #include <sys/sem.h>
   56 #include <sys/sx.h>
   57 #include <sys/syscall.h>
   58 #include <sys/syscallsubr.h>
   59 #include <sys/sysent.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/uio.h>
   62 #include <sys/malloc.h>
   63 #include <sys/jail.h>
   64 
   65 #include <security/mac/mac_framework.h>
   66 
   67 FEATURE(sysv_sem, "System V semaphores support");
   68 
   69 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
   70 
   71 #ifdef SEM_DEBUG
   72 #define DPRINTF(a)      printf a
   73 #else
   74 #define DPRINTF(a)
   75 #endif
   76 
   77 static int 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 prison *rpr,
   83     struct semid_kernel *semakptr);
   84 static void sem_remove(int semidx, struct ucred *cred);
   85 static struct prison *sem_find_prison(struct ucred *);
   86 static int sem_prison_cansee(struct prison *, struct semid_kernel *);
   87 static int sem_prison_check(void *, void *);
   88 static int sem_prison_set(void *, void *);
   89 static int sem_prison_get(void *, void *);
   90 static int sem_prison_remove(void *, void *);
   91 static void sem_prison_cleanup(struct prison *);
   92 
   93 #ifndef _SYS_SYSPROTO_H_
   94 struct __semctl_args;
   95 int __semctl(struct thread *td, struct __semctl_args *uap);
   96 struct semget_args;
   97 int semget(struct thread *td, struct semget_args *uap);
   98 struct semop_args;
   99 int semop(struct thread *td, struct semop_args *uap);
  100 #endif
  101 
  102 static struct sem_undo *semu_alloc(struct thread *td);
  103 static int semundo_adjust(struct thread *td, struct sem_undo **supptr,
  104     int semid, int semseq, int semnum, int adjval);
  105 static void semundo_clear(int semid, int semnum);
  106 
  107 static struct mtx       sem_mtx;        /* semaphore global lock */
  108 static struct mtx sem_undo_mtx;
  109 static int      semtot = 0;
  110 static struct semid_kernel *sema;       /* semaphore id pool */
  111 static struct mtx *sema_mtx;    /* semaphore id pool mutexes*/
  112 static struct sem *sem;         /* semaphore pool */
  113 LIST_HEAD(, sem_undo) semu_list;        /* list of active undo structures */
  114 LIST_HEAD(, sem_undo) semu_free_list;   /* list of free undo structures */
  115 static int      *semu;          /* undo structure pool */
  116 static eventhandler_tag semexit_tag;
  117 static unsigned sem_prison_slot;        /* prison OSD slot */
  118 
  119 #define SEMUNDO_MTX             sem_undo_mtx
  120 #define SEMUNDO_LOCK()          mtx_lock(&SEMUNDO_MTX);
  121 #define SEMUNDO_UNLOCK()        mtx_unlock(&SEMUNDO_MTX);
  122 #define SEMUNDO_LOCKASSERT(how) mtx_assert(&SEMUNDO_MTX, (how));
  123 
  124 struct sem {
  125         u_short semval;         /* semaphore value */
  126         pid_t   sempid;         /* pid of last operation */
  127         u_short semncnt;        /* # awaiting semval > cval */
  128         u_short semzcnt;        /* # awaiting semval = 0 */
  129 };
  130 
  131 /*
  132  * Undo structure (one per process)
  133  */
  134 struct sem_undo {
  135         LIST_ENTRY(sem_undo) un_next;   /* ptr to next active undo structure */
  136         struct  proc *un_proc;          /* owner of this structure */
  137         short   un_cnt;                 /* # of active entries */
  138         struct undo {
  139                 short   un_adjval;      /* adjust on exit values */
  140                 short   un_num;         /* semaphore # */
  141                 int     un_id;          /* semid */
  142                 unsigned short un_seq;
  143         } un_ent[1];                    /* undo entries */
  144 };
  145 
  146 /*
  147  * Configuration parameters
  148  */
  149 #ifndef SEMMNI
  150 #define SEMMNI  50              /* # of semaphore identifiers */
  151 #endif
  152 #ifndef SEMMNS
  153 #define SEMMNS  340             /* # of semaphores in system */
  154 #endif
  155 #ifndef SEMUME
  156 #define SEMUME  50              /* max # of undo entries per process */
  157 #endif
  158 #ifndef SEMMNU
  159 #define SEMMNU  150             /* # of undo structures in system */
  160 #endif
  161 
  162 /* shouldn't need tuning */
  163 #ifndef SEMMSL
  164 #define SEMMSL  SEMMNS          /* max # of semaphores per id */
  165 #endif
  166 #ifndef SEMOPM
  167 #define SEMOPM  100             /* max # of operations per semop call */
  168 #endif
  169 
  170 #define SEMVMX  32767           /* semaphore maximum value */
  171 #define SEMAEM  16384           /* adjust on exit max value */
  172 
  173 /*
  174  * Due to the way semaphore memory is allocated, we have to ensure that
  175  * SEMUSZ is properly aligned.
  176  */
  177 
  178 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long))
  179 
  180 /* actual size of an undo structure */
  181 #define SEMUSZ  SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
  182 
  183 /*
  184  * Macro to find a particular sem_undo vector
  185  */
  186 #define SEMU(ix) \
  187         ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
  188 
  189 /*
  190  * semaphore info struct
  191  */
  192 struct seminfo seminfo = {
  193                 SEMMNI,         /* # of semaphore identifiers */
  194                 SEMMNS,         /* # of semaphores in system */
  195                 SEMMNU,         /* # of undo structures in system */
  196                 SEMMSL,         /* max # of semaphores per id */
  197                 SEMOPM,         /* max # of operations per semop call */
  198                 SEMUME,         /* max # of undo entries per process */
  199                 SEMUSZ,         /* size in bytes of undo structure */
  200                 SEMVMX,         /* semaphore maximum value */
  201                 SEMAEM          /* adjust on exit max value */
  202 };
  203 
  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_RWTUN, &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_RWTUN, &seminfo.semvmx, 0,
  219     "Semaphore maximum value");
  220 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RWTUN, &seminfo.semaem, 0,
  221     "Adjust on exit max value");
  222 SYSCTL_PROC(_kern_ipc, OID_AUTO, sema,
  223     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
  224     NULL, 0, sysctl_sema, "",
  225     "Array of struct semid_kernel for each potential semaphore");
  226 
  227 static struct syscall_helper_data sem_syscalls[] = {
  228         SYSCALL_INIT_HELPER(__semctl),
  229         SYSCALL_INIT_HELPER(semget),
  230         SYSCALL_INIT_HELPER(semop),
  231 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  232     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  233         SYSCALL_INIT_HELPER(semsys),
  234         SYSCALL_INIT_HELPER_COMPAT(freebsd7___semctl),
  235 #endif
  236         SYSCALL_INIT_LAST
  237 };
  238 
  239 #ifdef COMPAT_FREEBSD32
  240 #include <compat/freebsd32/freebsd32.h>
  241 #include <compat/freebsd32/freebsd32_ipc.h>
  242 #include <compat/freebsd32/freebsd32_proto.h>
  243 #include <compat/freebsd32/freebsd32_signal.h>
  244 #include <compat/freebsd32/freebsd32_syscall.h>
  245 #include <compat/freebsd32/freebsd32_util.h>
  246 
  247 static struct syscall_helper_data sem32_syscalls[] = {
  248         SYSCALL32_INIT_HELPER(freebsd32_semctl),
  249         SYSCALL32_INIT_HELPER_COMPAT(semget),
  250         SYSCALL32_INIT_HELPER_COMPAT(semop),
  251         SYSCALL32_INIT_HELPER(freebsd32_semsys),
  252 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
  253     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
  254         SYSCALL32_INIT_HELPER(freebsd7_freebsd32_semctl),
  255 #endif
  256         SYSCALL_INIT_LAST
  257 };
  258 #endif
  259 
  260 static int
  261 seminit(void)
  262 {
  263         struct prison *pr;
  264         void **rsv;
  265         int i, error;
  266         osd_method_t methods[PR_MAXMETHOD] = {
  267             [PR_METHOD_CHECK] =         sem_prison_check,
  268             [PR_METHOD_SET] =           sem_prison_set,
  269             [PR_METHOD_GET] =           sem_prison_get,
  270             [PR_METHOD_REMOVE] =        sem_prison_remove,
  271         };
  272 
  273         sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
  274         sema = malloc(sizeof(struct semid_kernel) * seminfo.semmni, M_SEM,
  275             M_WAITOK | M_ZERO);
  276         sema_mtx = malloc(sizeof(struct mtx) * seminfo.semmni, M_SEM,
  277             M_WAITOK | M_ZERO);
  278         semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
  279 
  280         for (i = 0; i < seminfo.semmni; i++) {
  281                 sema[i].u.sem_base = 0;
  282                 sema[i].u.sem_perm.mode = 0;
  283                 sema[i].u.sem_perm.seq = 0;
  284 #ifdef MAC
  285                 mac_sysvsem_init(&sema[i]);
  286 #endif
  287         }
  288         for (i = 0; i < seminfo.semmni; i++)
  289                 mtx_init(&sema_mtx[i], "semid", NULL, MTX_DEF);
  290         LIST_INIT(&semu_free_list);
  291         for (i = 0; i < seminfo.semmnu; i++) {
  292                 struct sem_undo *suptr = SEMU(i);
  293                 suptr->un_proc = NULL;
  294                 LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
  295         }
  296         LIST_INIT(&semu_list);
  297         mtx_init(&sem_mtx, "sem", NULL, MTX_DEF);
  298         mtx_init(&sem_undo_mtx, "semu", NULL, MTX_DEF);
  299         semexit_tag = EVENTHANDLER_REGISTER(process_exit, semexit_myhook, NULL,
  300             EVENTHANDLER_PRI_ANY);
  301 
  302         /* Set current prisons according to their allow.sysvipc. */
  303         sem_prison_slot = osd_jail_register(NULL, methods);
  304         rsv = osd_reserve(sem_prison_slot);
  305         prison_lock(&prison0);
  306         (void)osd_jail_set_reserved(&prison0, sem_prison_slot, rsv, &prison0);
  307         prison_unlock(&prison0);
  308         rsv = NULL;
  309         sx_slock(&allprison_lock);
  310         TAILQ_FOREACH(pr, &allprison, pr_list) {
  311                 if (rsv == NULL)
  312                         rsv = osd_reserve(sem_prison_slot);
  313                 prison_lock(pr);
  314                 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
  315                         (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
  316                             &prison0);
  317                         rsv = NULL;
  318                 }
  319                 prison_unlock(pr);
  320         }
  321         if (rsv != NULL)
  322                 osd_free_reserved(rsv);
  323         sx_sunlock(&allprison_lock);
  324 
  325         error = syscall_helper_register(sem_syscalls, SY_THR_STATIC_KLD);
  326         if (error != 0)
  327                 return (error);
  328 #ifdef COMPAT_FREEBSD32
  329         error = syscall32_helper_register(sem32_syscalls, SY_THR_STATIC_KLD);
  330         if (error != 0)
  331                 return (error);
  332 #endif
  333         return (0);
  334 }
  335 
  336 static int
  337 semunload(void)
  338 {
  339         int i;
  340 
  341         /* XXXKIB */
  342         if (semtot != 0)
  343                 return (EBUSY);
  344 
  345 #ifdef COMPAT_FREEBSD32
  346         syscall32_helper_unregister(sem32_syscalls);
  347 #endif
  348         syscall_helper_unregister(sem_syscalls);
  349         EVENTHANDLER_DEREGISTER(process_exit, semexit_tag);
  350         if (sem_prison_slot != 0)
  351                 osd_jail_deregister(sem_prison_slot);
  352 #ifdef MAC
  353         for (i = 0; i < seminfo.semmni; i++)
  354                 mac_sysvsem_destroy(&sema[i]);
  355 #endif
  356         free(sem, M_SEM);
  357         free(sema, M_SEM);
  358         free(semu, M_SEM);
  359         for (i = 0; i < seminfo.semmni; i++)
  360                 mtx_destroy(&sema_mtx[i]);
  361         free(sema_mtx, M_SEM);
  362         mtx_destroy(&sem_mtx);
  363         mtx_destroy(&sem_undo_mtx);
  364         return (0);
  365 }
  366 
  367 static int
  368 sysvsem_modload(struct module *module, int cmd, void *arg)
  369 {
  370         int error = 0;
  371 
  372         switch (cmd) {
  373         case MOD_LOAD:
  374                 error = seminit();
  375                 if (error != 0)
  376                         semunload();
  377                 break;
  378         case MOD_UNLOAD:
  379                 error = semunload();
  380                 break;
  381         case MOD_SHUTDOWN:
  382                 break;
  383         default:
  384                 error = EINVAL;
  385                 break;
  386         }
  387         return (error);
  388 }
  389 
  390 static moduledata_t sysvsem_mod = {
  391         "sysvsem",
  392         &sysvsem_modload,
  393         NULL
  394 };
  395 
  396 DECLARE_MODULE(sysvsem, sysvsem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
  397 MODULE_VERSION(sysvsem, 1);
  398 
  399 /*
  400  * Allocate a new sem_undo structure for a process
  401  * (returns ptr to structure or NULL if no more room)
  402  */
  403 
  404 static struct sem_undo *
  405 semu_alloc(struct thread *td)
  406 {
  407         struct sem_undo *suptr;
  408 
  409         SEMUNDO_LOCKASSERT(MA_OWNED);
  410         if ((suptr = LIST_FIRST(&semu_free_list)) == NULL)
  411                 return (NULL);
  412         LIST_REMOVE(suptr, un_next);
  413         LIST_INSERT_HEAD(&semu_list, suptr, un_next);
  414         suptr->un_cnt = 0;
  415         suptr->un_proc = td->td_proc;
  416         return (suptr);
  417 }
  418 
  419 static int
  420 semu_try_free(struct sem_undo *suptr)
  421 {
  422 
  423         SEMUNDO_LOCKASSERT(MA_OWNED);
  424 
  425         if (suptr->un_cnt != 0)
  426                 return (0);
  427         LIST_REMOVE(suptr, un_next);
  428         LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
  429         return (1);
  430 }
  431 
  432 /*
  433  * Adjust a particular entry for a particular proc
  434  */
  435 
  436 static int
  437 semundo_adjust(struct thread *td, struct sem_undo **supptr, int semid,
  438     int semseq, int semnum, int adjval)
  439 {
  440         struct proc *p = td->td_proc;
  441         struct sem_undo *suptr;
  442         struct undo *sunptr;
  443         int i;
  444 
  445         SEMUNDO_LOCKASSERT(MA_OWNED);
  446         /* Look for and remember the sem_undo if the caller doesn't provide
  447            it */
  448 
  449         suptr = *supptr;
  450         if (suptr == NULL) {
  451                 LIST_FOREACH(suptr, &semu_list, un_next) {
  452                         if (suptr->un_proc == p) {
  453                                 *supptr = suptr;
  454                                 break;
  455                         }
  456                 }
  457                 if (suptr == NULL) {
  458                         if (adjval == 0)
  459                                 return(0);
  460                         suptr = semu_alloc(td);
  461                         if (suptr == NULL)
  462                                 return (ENOSPC);
  463                         *supptr = suptr;
  464                 }
  465         }
  466 
  467         /*
  468          * Look for the requested entry and adjust it (delete if adjval becomes
  469          * 0).
  470          */
  471         sunptr = &suptr->un_ent[0];
  472         for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
  473                 if (sunptr->un_id != semid || sunptr->un_num != semnum)
  474                         continue;
  475                 if (adjval != 0) {
  476                         adjval += sunptr->un_adjval;
  477                         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
  478                                 return (ERANGE);
  479                 }
  480                 sunptr->un_adjval = adjval;
  481                 if (sunptr->un_adjval == 0) {
  482                         suptr->un_cnt--;
  483                         if (i < suptr->un_cnt)
  484                                 suptr->un_ent[i] =
  485                                     suptr->un_ent[suptr->un_cnt];
  486                         if (suptr->un_cnt == 0)
  487                                 semu_try_free(suptr);
  488                 }
  489                 return (0);
  490         }
  491 
  492         /* Didn't find the right entry - create it */
  493         if (adjval == 0)
  494                 return (0);
  495         if (adjval > seminfo.semaem || adjval < -seminfo.semaem)
  496                 return (ERANGE);
  497         if (suptr->un_cnt != seminfo.semume) {
  498                 sunptr = &suptr->un_ent[suptr->un_cnt];
  499                 suptr->un_cnt++;
  500                 sunptr->un_adjval = adjval;
  501                 sunptr->un_id = semid;
  502                 sunptr->un_num = semnum;
  503                 sunptr->un_seq = semseq;
  504         } else
  505                 return (EINVAL);
  506         return (0);
  507 }
  508 
  509 static void
  510 semundo_clear(int semid, int semnum)
  511 {
  512         struct sem_undo *suptr, *suptr1;
  513         struct undo *sunptr;
  514         int i;
  515 
  516         SEMUNDO_LOCKASSERT(MA_OWNED);
  517         LIST_FOREACH_SAFE(suptr, &semu_list, un_next, suptr1) {
  518                 sunptr = &suptr->un_ent[0];
  519                 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
  520                         if (sunptr->un_id != semid)
  521                                 continue;
  522                         if (semnum == -1 || sunptr->un_num == semnum) {
  523                                 suptr->un_cnt--;
  524                                 if (i < suptr->un_cnt) {
  525                                         suptr->un_ent[i] =
  526                                             suptr->un_ent[suptr->un_cnt];
  527                                         continue;
  528                                 }
  529                                 semu_try_free(suptr);
  530                         }
  531                         if (semnum != -1)
  532                                 break;
  533                 }
  534         }
  535 }
  536 
  537 static int
  538 semvalid(int semid, struct prison *rpr, struct semid_kernel *semakptr)
  539 {
  540 
  541         return ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
  542             semakptr->u.sem_perm.seq != IPCID_TO_SEQ(semid) ||
  543             sem_prison_cansee(rpr, semakptr) ? EINVAL : 0);
  544 }
  545 
  546 static void
  547 sem_remove(int semidx, struct ucred *cred)
  548 {
  549         struct semid_kernel *semakptr;
  550         int i;
  551 
  552         KASSERT(semidx >= 0 && semidx < seminfo.semmni,
  553                 ("semidx out of bounds"));
  554         semakptr = &sema[semidx];
  555         semakptr->u.sem_perm.cuid = cred ? cred->cr_uid : 0;
  556         semakptr->u.sem_perm.uid = cred ? cred->cr_uid : 0;
  557         semakptr->u.sem_perm.mode = 0;
  558         racct_sub_cred(semakptr->cred, RACCT_NSEM, semakptr->u.sem_nsems);
  559         crfree(semakptr->cred);
  560         semakptr->cred = NULL;
  561         SEMUNDO_LOCK();
  562         semundo_clear(semidx, -1);
  563         SEMUNDO_UNLOCK();
  564 #ifdef MAC
  565         mac_sysvsem_cleanup(semakptr);
  566 #endif
  567         wakeup(semakptr);
  568         for (i = 0; i < seminfo.semmni; i++) {
  569                 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
  570                     sema[i].u.sem_base > semakptr->u.sem_base)
  571                         mtx_lock_flags(&sema_mtx[i], LOP_DUPOK);
  572         }
  573         for (i = semakptr->u.sem_base - sem; i < semtot; i++)
  574                 sem[i] = sem[i + semakptr->u.sem_nsems];
  575         for (i = 0; i < seminfo.semmni; i++) {
  576                 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
  577                     sema[i].u.sem_base > semakptr->u.sem_base) {
  578                         sema[i].u.sem_base -= semakptr->u.sem_nsems;
  579                         mtx_unlock(&sema_mtx[i]);
  580                 }
  581         }
  582         semtot -= semakptr->u.sem_nsems;
  583 }
  584 
  585 static struct prison *
  586 sem_find_prison(struct ucred *cred)
  587 {
  588         struct prison *pr, *rpr;
  589 
  590         pr = cred->cr_prison;
  591         prison_lock(pr);
  592         rpr = osd_jail_get(pr, sem_prison_slot);
  593         prison_unlock(pr);
  594         return rpr;
  595 }
  596 
  597 static int
  598 sem_prison_cansee(struct prison *rpr, struct semid_kernel *semakptr)
  599 {
  600 
  601         if (semakptr->cred == NULL ||
  602             !(rpr == semakptr->cred->cr_prison ||
  603               prison_ischild(rpr, semakptr->cred->cr_prison)))
  604                 return (EINVAL);
  605         return (0);
  606 }
  607 
  608 /*
  609  * Note that the user-mode half of this passes a union, not a pointer.
  610  */
  611 #ifndef _SYS_SYSPROTO_H_
  612 struct __semctl_args {
  613         int     semid;
  614         int     semnum;
  615         int     cmd;
  616         union   semun *arg;
  617 };
  618 #endif
  619 int
  620 sys___semctl(struct thread *td, struct __semctl_args *uap)
  621 {
  622         struct semid_ds dsbuf;
  623         union semun arg, semun;
  624         register_t rval;
  625         int error;
  626 
  627         switch (uap->cmd) {
  628         case SEM_STAT:
  629         case IPC_SET:
  630         case IPC_STAT:
  631         case GETALL:
  632         case SETVAL:
  633         case SETALL:
  634                 error = copyin(uap->arg, &arg, sizeof(arg));
  635                 if (error)
  636                         return (error);
  637                 break;
  638         }
  639 
  640         switch (uap->cmd) {
  641         case SEM_STAT:
  642         case IPC_STAT:
  643                 semun.buf = &dsbuf;
  644                 break;
  645         case IPC_SET:
  646                 error = copyin(arg.buf, &dsbuf, sizeof(dsbuf));
  647                 if (error)
  648                         return (error);
  649                 semun.buf = &dsbuf;
  650                 break;
  651         case GETALL:
  652         case SETALL:
  653                 semun.array = arg.array;
  654                 break;
  655         case SETVAL:
  656                 semun.val = arg.val;
  657                 break;          
  658         }
  659 
  660         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
  661             &rval);
  662         if (error)
  663                 return (error);
  664 
  665         switch (uap->cmd) {
  666         case SEM_STAT:
  667         case IPC_STAT:
  668                 error = copyout(&dsbuf, arg.buf, sizeof(dsbuf));
  669                 break;
  670         }
  671 
  672         if (error == 0)
  673                 td->td_retval[0] = rval;
  674         return (error);
  675 }
  676 
  677 int
  678 kern_semctl(struct thread *td, int semid, int semnum, int cmd,
  679     union semun *arg, register_t *rval)
  680 {
  681         u_short *array;
  682         struct ucred *cred = td->td_ucred;
  683         int i, error;
  684         struct prison *rpr;
  685         struct semid_ds *sbuf;
  686         struct semid_kernel *semakptr;
  687         struct mtx *sema_mtxp;
  688         u_short usval, count;
  689         int semidx;
  690 
  691         DPRINTF(("call to semctl(%d, %d, %d, 0x%p)\n",
  692             semid, semnum, cmd, arg));
  693 
  694         rpr = sem_find_prison(td->td_ucred);
  695         if (sem == NULL)
  696                 return (ENOSYS);
  697 
  698         array = NULL;
  699 
  700         switch(cmd) {
  701         case SEM_STAT:
  702                 /*
  703                  * For this command we assume semid is an array index
  704                  * rather than an IPC id.
  705                  */
  706                 if (semid < 0 || semid >= seminfo.semmni)
  707                         return (EINVAL);
  708                 semakptr = &sema[semid];
  709                 sema_mtxp = &sema_mtx[semid];
  710                 mtx_lock(sema_mtxp);
  711                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
  712                         error = EINVAL;
  713                         goto done2;
  714                 }
  715                 if ((error = sem_prison_cansee(rpr, semakptr)))
  716                         goto done2;
  717                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  718                         goto done2;
  719 #ifdef MAC
  720                 error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
  721                 if (error != 0)
  722                         goto done2;
  723 #endif
  724                 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
  725                 if (cred->cr_prison != semakptr->cred->cr_prison)
  726                         arg->buf->sem_perm.key = IPC_PRIVATE;
  727                 *rval = IXSEQ_TO_IPCID(semid, semakptr->u.sem_perm);
  728                 mtx_unlock(sema_mtxp);
  729                 return (0);
  730         }
  731 
  732         semidx = IPCID_TO_IX(semid);
  733         if (semidx < 0 || semidx >= seminfo.semmni)
  734                 return (EINVAL);
  735 
  736         semakptr = &sema[semidx];
  737         sema_mtxp = &sema_mtx[semidx];
  738         if (cmd == IPC_RMID)
  739                 mtx_lock(&sem_mtx);
  740         mtx_lock(sema_mtxp);
  741 
  742 #ifdef MAC
  743         error = mac_sysvsem_check_semctl(cred, semakptr, cmd);
  744         if (error != 0)
  745                 goto done2;
  746 #endif
  747 
  748         error = 0;
  749         *rval = 0;
  750 
  751         switch (cmd) {
  752         case IPC_RMID:
  753                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  754                         goto done2;
  755                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
  756                         goto done2;
  757                 sem_remove(semidx, cred);
  758                 break;
  759 
  760         case IPC_SET:
  761                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  762                         goto done2;
  763                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_M)))
  764                         goto done2;
  765                 sbuf = arg->buf;
  766                 semakptr->u.sem_perm.uid = sbuf->sem_perm.uid;
  767                 semakptr->u.sem_perm.gid = sbuf->sem_perm.gid;
  768                 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
  769                     ~0777) | (sbuf->sem_perm.mode & 0777);
  770                 semakptr->u.sem_ctime = time_second;
  771                 break;
  772 
  773         case IPC_STAT:
  774                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  775                         goto done2;
  776                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  777                         goto done2;
  778                 bcopy(&semakptr->u, arg->buf, sizeof(struct semid_ds));
  779                 if (cred->cr_prison != semakptr->cred->cr_prison)
  780                         arg->buf->sem_perm.key = IPC_PRIVATE;
  781                 break;
  782 
  783         case GETNCNT:
  784                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  785                         goto done2;
  786                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  787                         goto done2;
  788                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  789                         error = EINVAL;
  790                         goto done2;
  791                 }
  792                 *rval = semakptr->u.sem_base[semnum].semncnt;
  793                 break;
  794 
  795         case GETPID:
  796                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  797                         goto done2;
  798                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  799                         goto done2;
  800                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  801                         error = EINVAL;
  802                         goto done2;
  803                 }
  804                 *rval = semakptr->u.sem_base[semnum].sempid;
  805                 break;
  806 
  807         case GETVAL:
  808                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  809                         goto done2;
  810                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  811                         goto done2;
  812                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  813                         error = EINVAL;
  814                         goto done2;
  815                 }
  816                 *rval = semakptr->u.sem_base[semnum].semval;
  817                 break;
  818 
  819         case GETALL:
  820                 /*
  821                  * Unfortunately, callers of this function don't know
  822                  * in advance how many semaphores are in this set.
  823                  * While we could just allocate the maximum size array
  824                  * and pass the actual size back to the caller, that
  825                  * won't work for SETALL since we can't copyin() more
  826                  * data than the user specified as we may return a
  827                  * spurious EFAULT.
  828                  * 
  829                  * Note that the number of semaphores in a set is
  830                  * fixed for the life of that set.  The only way that
  831                  * the 'count' could change while are blocked in
  832                  * malloc() is if this semaphore set were destroyed
  833                  * and a new one created with the same index.
  834                  * However, semvalid() will catch that due to the
  835                  * sequence number unless exactly 0x8000 (or a
  836                  * multiple thereof) semaphore sets for the same index
  837                  * are created and destroyed while we are in malloc!
  838                  *
  839                  */
  840                 count = semakptr->u.sem_nsems;
  841                 mtx_unlock(sema_mtxp);              
  842                 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
  843                 mtx_lock(sema_mtxp);
  844                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  845                         goto done2;
  846                 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
  847                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  848                         goto done2;
  849                 for (i = 0; i < semakptr->u.sem_nsems; i++)
  850                         array[i] = semakptr->u.sem_base[i].semval;
  851                 mtx_unlock(sema_mtxp);
  852                 error = copyout(array, arg->array, count * sizeof(*array));
  853                 mtx_lock(sema_mtxp);
  854                 break;
  855 
  856         case GETZCNT:
  857                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  858                         goto done2;
  859                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_R)))
  860                         goto done2;
  861                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  862                         error = EINVAL;
  863                         goto done2;
  864                 }
  865                 *rval = semakptr->u.sem_base[semnum].semzcnt;
  866                 break;
  867 
  868         case SETVAL:
  869                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  870                         goto done2;
  871                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
  872                         goto done2;
  873                 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
  874                         error = EINVAL;
  875                         goto done2;
  876                 }
  877                 if (arg->val < 0 || arg->val > seminfo.semvmx) {
  878                         error = ERANGE;
  879                         goto done2;
  880                 }
  881                 semakptr->u.sem_base[semnum].semval = arg->val;
  882                 SEMUNDO_LOCK();
  883                 semundo_clear(semidx, semnum);
  884                 SEMUNDO_UNLOCK();
  885                 wakeup(semakptr);
  886                 break;
  887 
  888         case SETALL:
  889                 /*
  890                  * See comment on GETALL for why 'count' shouldn't change
  891                  * and why we require a userland buffer.
  892                  */
  893                 count = semakptr->u.sem_nsems;
  894                 mtx_unlock(sema_mtxp);              
  895                 array = malloc(sizeof(*array) * count, M_TEMP, M_WAITOK);
  896                 error = copyin(arg->array, array, count * sizeof(*array));
  897                 mtx_lock(sema_mtxp);
  898                 if (error)
  899                         break;
  900                 if ((error = semvalid(semid, rpr, semakptr)) != 0)
  901                         goto done2;
  902                 KASSERT(count == semakptr->u.sem_nsems, ("nsems changed"));
  903                 if ((error = ipcperm(td, &semakptr->u.sem_perm, IPC_W)))
  904                         goto done2;
  905                 for (i = 0; i < semakptr->u.sem_nsems; i++) {
  906                         usval = array[i];
  907                         if (usval > seminfo.semvmx) {
  908                                 error = ERANGE;
  909                                 break;
  910                         }
  911                         semakptr->u.sem_base[i].semval = usval;
  912                 }
  913                 SEMUNDO_LOCK();
  914                 semundo_clear(semidx, -1);
  915                 SEMUNDO_UNLOCK();
  916                 wakeup(semakptr);
  917                 break;
  918 
  919         default:
  920                 error = EINVAL;
  921                 break;
  922         }
  923 
  924 done2:
  925         mtx_unlock(sema_mtxp);
  926         if (cmd == IPC_RMID)
  927                 mtx_unlock(&sem_mtx);
  928         if (array != NULL)
  929                 free(array, M_TEMP);
  930         return(error);
  931 }
  932 
  933 #ifndef _SYS_SYSPROTO_H_
  934 struct semget_args {
  935         key_t   key;
  936         int     nsems;
  937         int     semflg;
  938 };
  939 #endif
  940 int
  941 sys_semget(struct thread *td, struct semget_args *uap)
  942 {
  943         int semid, error = 0;
  944         int key = uap->key;
  945         int nsems = uap->nsems;
  946         int semflg = uap->semflg;
  947         struct ucred *cred = td->td_ucred;
  948 
  949         DPRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
  950 
  951         if (sem_find_prison(cred) == NULL)
  952                 return (ENOSYS);
  953 
  954         mtx_lock(&sem_mtx);
  955         if (key != IPC_PRIVATE) {
  956                 for (semid = 0; semid < seminfo.semmni; semid++) {
  957                         if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
  958                             sema[semid].cred != NULL &&
  959                             sema[semid].cred->cr_prison == cred->cr_prison &&
  960                             sema[semid].u.sem_perm.key == key)
  961                                 break;
  962                 }
  963                 if (semid < seminfo.semmni) {
  964                         DPRINTF(("found public key\n"));
  965                         if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
  966                                 DPRINTF(("not exclusive\n"));
  967                                 error = EEXIST;
  968                                 goto done2;
  969                         }
  970                         if ((error = ipcperm(td, &sema[semid].u.sem_perm,
  971                             semflg & 0700))) {
  972                                 goto done2;
  973                         }
  974                         if (nsems > 0 && sema[semid].u.sem_nsems < nsems) {
  975                                 DPRINTF(("too small\n"));
  976                                 error = EINVAL;
  977                                 goto done2;
  978                         }
  979 #ifdef MAC
  980                         error = mac_sysvsem_check_semget(cred, &sema[semid]);
  981                         if (error != 0)
  982                                 goto done2;
  983 #endif
  984                         goto found;
  985                 }
  986         }
  987 
  988         DPRINTF(("need to allocate the semid_kernel\n"));
  989         if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
  990                 if (nsems <= 0 || nsems > seminfo.semmsl) {
  991                         DPRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
  992                             seminfo.semmsl));
  993                         error = EINVAL;
  994                         goto done2;
  995                 }
  996                 if (nsems > seminfo.semmns - semtot) {
  997                         DPRINTF((
  998                             "not enough semaphores left (need %d, got %d)\n",
  999                             nsems, seminfo.semmns - semtot));
 1000                         error = ENOSPC;
 1001                         goto done2;
 1002                 }
 1003                 for (semid = 0; semid < seminfo.semmni; semid++) {
 1004                         if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
 1005                                 break;
 1006                 }
 1007                 if (semid == seminfo.semmni) {
 1008                         DPRINTF(("no more semid_kernel's available\n"));
 1009                         error = ENOSPC;
 1010                         goto done2;
 1011                 }
 1012 #ifdef RACCT
 1013                 if (racct_enable) {
 1014                         PROC_LOCK(td->td_proc);
 1015                         error = racct_add(td->td_proc, RACCT_NSEM, nsems);
 1016                         PROC_UNLOCK(td->td_proc);
 1017                         if (error != 0) {
 1018                                 error = ENOSPC;
 1019                                 goto done2;
 1020                         }
 1021                 }
 1022 #endif
 1023                 DPRINTF(("semid %d is available\n", semid));
 1024                 mtx_lock(&sema_mtx[semid]);
 1025                 KASSERT((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0,
 1026                     ("Lost semaphore %d", semid));
 1027                 sema[semid].u.sem_perm.key = key;
 1028                 sema[semid].u.sem_perm.cuid = cred->cr_uid;
 1029                 sema[semid].u.sem_perm.uid = cred->cr_uid;
 1030                 sema[semid].u.sem_perm.cgid = cred->cr_gid;
 1031                 sema[semid].u.sem_perm.gid = cred->cr_gid;
 1032                 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
 1033                 sema[semid].cred = crhold(cred);
 1034                 sema[semid].u.sem_perm.seq =
 1035                     (sema[semid].u.sem_perm.seq + 1) & 0x7fff;
 1036                 sema[semid].u.sem_nsems = nsems;
 1037                 sema[semid].u.sem_otime = 0;
 1038                 sema[semid].u.sem_ctime = time_second;
 1039                 sema[semid].u.sem_base = &sem[semtot];
 1040                 semtot += nsems;
 1041                 bzero(sema[semid].u.sem_base,
 1042                     sizeof(sema[semid].u.sem_base[0])*nsems);
 1043 #ifdef MAC
 1044                 mac_sysvsem_create(cred, &sema[semid]);
 1045 #endif
 1046                 mtx_unlock(&sema_mtx[semid]);
 1047                 DPRINTF(("sembase = %p, next = %p\n",
 1048                     sema[semid].u.sem_base, &sem[semtot]));
 1049         } else {
 1050                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
 1051                 error = ENOENT;
 1052                 goto done2;
 1053         }
 1054 
 1055 found:
 1056         td->td_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
 1057 done2:
 1058         mtx_unlock(&sem_mtx);
 1059         return (error);
 1060 }
 1061 
 1062 #ifndef _SYS_SYSPROTO_H_
 1063 struct semop_args {
 1064         int     semid;
 1065         struct  sembuf *sops;
 1066         size_t  nsops;
 1067 };
 1068 #endif
 1069 int
 1070 sys_semop(struct thread *td, struct semop_args *uap)
 1071 {
 1072 #define SMALL_SOPS      8
 1073         struct sembuf small_sops[SMALL_SOPS];
 1074         int semid = uap->semid;
 1075         size_t nsops = uap->nsops;
 1076         struct prison *rpr;
 1077         struct sembuf *sops;
 1078         struct semid_kernel *semakptr;
 1079         struct sembuf *sopptr = NULL;
 1080         struct sem *semptr = NULL;
 1081         struct sem_undo *suptr;
 1082         struct mtx *sema_mtxp;
 1083         size_t i, j, k;
 1084         int error;
 1085         int do_wakeup, do_undos;
 1086         unsigned short seq;
 1087 
 1088 #ifdef SEM_DEBUG
 1089         sops = NULL;
 1090 #endif
 1091         DPRINTF(("call to semop(%d, %p, %u)\n", semid, sops, nsops));
 1092 
 1093         rpr = sem_find_prison(td->td_ucred);
 1094         if (sem == NULL)
 1095                 return (ENOSYS);
 1096 
 1097         semid = IPCID_TO_IX(semid);     /* Convert back to zero origin */
 1098 
 1099         if (semid < 0 || semid >= seminfo.semmni)
 1100                 return (EINVAL);
 1101 
 1102         /* Allocate memory for sem_ops */
 1103         if (nsops <= SMALL_SOPS)
 1104                 sops = small_sops;
 1105         else if (nsops > seminfo.semopm) {
 1106                 DPRINTF(("too many sops (max=%d, nsops=%d)\n", seminfo.semopm,
 1107                     nsops));
 1108                 return (E2BIG);
 1109         } else {
 1110 #ifdef RACCT
 1111                 if (racct_enable) {
 1112                         PROC_LOCK(td->td_proc);
 1113                         if (nsops >
 1114                             racct_get_available(td->td_proc, RACCT_NSEMOP)) {
 1115                                 PROC_UNLOCK(td->td_proc);
 1116                                 return (E2BIG);
 1117                         }
 1118                         PROC_UNLOCK(td->td_proc);
 1119                 }
 1120 #endif
 1121 
 1122                 sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
 1123         }
 1124         if ((error = copyin(uap->sops, sops, nsops * sizeof(sops[0]))) != 0) {
 1125                 DPRINTF(("error = %d from copyin(%p, %p, %d)\n", error,
 1126                     uap->sops, sops, nsops * sizeof(sops[0])));
 1127                 if (sops != small_sops)
 1128                         free(sops, M_SEM);
 1129                 return (error);
 1130         }
 1131 
 1132         semakptr = &sema[semid];
 1133         sema_mtxp = &sema_mtx[semid];
 1134         mtx_lock(sema_mtxp);
 1135         if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
 1136                 error = EINVAL;
 1137                 goto done2;
 1138         }
 1139         seq = semakptr->u.sem_perm.seq;
 1140         if (seq != IPCID_TO_SEQ(uap->semid)) {
 1141                 error = EINVAL;
 1142                 goto done2;
 1143         }
 1144         if ((error = sem_prison_cansee(rpr, semakptr)) != 0)
 1145                 goto done2;
 1146         /*
 1147          * Initial pass through sops to see what permissions are needed.
 1148          * Also perform any checks that don't need repeating on each
 1149          * attempt to satisfy the request vector.
 1150          */
 1151         j = 0;          /* permission needed */
 1152         do_undos = 0;
 1153         for (i = 0; i < nsops; i++) {
 1154                 sopptr = &sops[i];
 1155                 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
 1156                         error = EFBIG;
 1157                         goto done2;
 1158                 }
 1159                 if (sopptr->sem_flg & SEM_UNDO && sopptr->sem_op != 0)
 1160                         do_undos = 1;
 1161                 j |= (sopptr->sem_op == 0) ? SEM_R : SEM_A;
 1162         }
 1163 
 1164         if ((error = ipcperm(td, &semakptr->u.sem_perm, j))) {
 1165                 DPRINTF(("error = %d from ipaccess\n", error));
 1166                 goto done2;
 1167         }
 1168 #ifdef MAC
 1169         error = mac_sysvsem_check_semop(td->td_ucred, semakptr, j);
 1170         if (error != 0)
 1171                 goto done2;
 1172 #endif
 1173 
 1174         /*
 1175          * Loop trying to satisfy the vector of requests.
 1176          * If we reach a point where we must wait, any requests already
 1177          * performed are rolled back and we go to sleep until some other
 1178          * process wakes us up.  At this point, we start all over again.
 1179          *
 1180          * This ensures that from the perspective of other tasks, a set
 1181          * of requests is atomic (never partially satisfied).
 1182          */
 1183         for (;;) {
 1184                 do_wakeup = 0;
 1185                 error = 0;      /* error return if necessary */
 1186 
 1187                 for (i = 0; i < nsops; i++) {
 1188                         sopptr = &sops[i];
 1189                         semptr = &semakptr->u.sem_base[sopptr->sem_num];
 1190 
 1191                         DPRINTF((
 1192                             "semop:  semakptr=%p, sem_base=%p, "
 1193                             "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
 1194                             semakptr, semakptr->u.sem_base, semptr,
 1195                             sopptr->sem_num, semptr->semval, sopptr->sem_op,
 1196                             (sopptr->sem_flg & IPC_NOWAIT) ?
 1197                             "nowait" : "wait"));
 1198 
 1199                         if (sopptr->sem_op < 0) {
 1200                                 if (semptr->semval + sopptr->sem_op < 0) {
 1201                                         DPRINTF(("semop:  can't do it now\n"));
 1202                                         break;
 1203                                 } else {
 1204                                         semptr->semval += sopptr->sem_op;
 1205                                         if (semptr->semval == 0 &&
 1206                                             semptr->semzcnt > 0)
 1207                                                 do_wakeup = 1;
 1208                                 }
 1209                         } else if (sopptr->sem_op == 0) {
 1210                                 if (semptr->semval != 0) {
 1211                                         DPRINTF(("semop:  not zero now\n"));
 1212                                         break;
 1213                                 }
 1214                         } else if (semptr->semval + sopptr->sem_op >
 1215                             seminfo.semvmx) {
 1216                                 error = ERANGE;
 1217                                 break;
 1218                         } else {
 1219                                 if (semptr->semncnt > 0)
 1220                                         do_wakeup = 1;
 1221                                 semptr->semval += sopptr->sem_op;
 1222                         }
 1223                 }
 1224 
 1225                 /*
 1226                  * Did we get through the entire vector?
 1227                  */
 1228                 if (i >= nsops)
 1229                         goto done;
 1230 
 1231                 /*
 1232                  * No ... rollback anything that we've already done
 1233                  */
 1234                 DPRINTF(("semop:  rollback 0 through %d\n", i-1));
 1235                 for (j = 0; j < i; j++)
 1236                         semakptr->u.sem_base[sops[j].sem_num].semval -=
 1237                             sops[j].sem_op;
 1238 
 1239                 /* If we detected an error, return it */
 1240                 if (error != 0)
 1241                         goto done2;
 1242 
 1243                 /*
 1244                  * If the request that we couldn't satisfy has the
 1245                  * NOWAIT flag set then return with EAGAIN.
 1246                  */
 1247                 if (sopptr->sem_flg & IPC_NOWAIT) {
 1248                         error = EAGAIN;
 1249                         goto done2;
 1250                 }
 1251 
 1252                 if (sopptr->sem_op == 0)
 1253                         semptr->semzcnt++;
 1254                 else
 1255                         semptr->semncnt++;
 1256 
 1257                 DPRINTF(("semop:  good night!\n"));
 1258                 error = msleep(semakptr, sema_mtxp, (PZERO - 4) | PCATCH,
 1259                     "semwait", 0);
 1260                 DPRINTF(("semop:  good morning (error=%d)!\n", error));
 1261                 /* return code is checked below, after sem[nz]cnt-- */
 1262 
 1263                 /*
 1264                  * Make sure that the semaphore still exists
 1265                  */
 1266                 seq = semakptr->u.sem_perm.seq;
 1267                 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
 1268                     seq != IPCID_TO_SEQ(uap->semid)) {
 1269                         error = EIDRM;
 1270                         goto done2;
 1271                 }
 1272 
 1273                 /*
 1274                  * Renew the semaphore's pointer after wakeup since
 1275                  * during msleep sem_base may have been modified and semptr
 1276                  * is not valid any more
 1277                  */
 1278                 semptr = &semakptr->u.sem_base[sopptr->sem_num];
 1279 
 1280                 /*
 1281                  * The semaphore is still alive.  Readjust the count of
 1282                  * waiting processes.
 1283                  */
 1284                 if (sopptr->sem_op == 0)
 1285                         semptr->semzcnt--;
 1286                 else
 1287                         semptr->semncnt--;
 1288 
 1289                 /*
 1290                  * Is it really morning, or was our sleep interrupted?
 1291                  * (Delayed check of msleep() return code because we
 1292                  * need to decrement sem[nz]cnt either way.)
 1293                  */
 1294                 if (error != 0) {
 1295                         error = EINTR;
 1296                         goto done2;
 1297                 }
 1298                 DPRINTF(("semop:  good morning!\n"));
 1299         }
 1300 
 1301 done:
 1302         /*
 1303          * Process any SEM_UNDO requests.
 1304          */
 1305         if (do_undos) {
 1306                 SEMUNDO_LOCK();
 1307                 suptr = NULL;
 1308                 for (i = 0; i < nsops; i++) {
 1309                         /*
 1310                          * We only need to deal with SEM_UNDO's for non-zero
 1311                          * op's.
 1312                          */
 1313                         int adjval;
 1314 
 1315                         if ((sops[i].sem_flg & SEM_UNDO) == 0)
 1316                                 continue;
 1317                         adjval = sops[i].sem_op;
 1318                         if (adjval == 0)
 1319                                 continue;
 1320                         error = semundo_adjust(td, &suptr, semid, seq,
 1321                             sops[i].sem_num, -adjval);
 1322                         if (error == 0)
 1323                                 continue;
 1324 
 1325                         /*
 1326                          * Oh-Oh!  We ran out of either sem_undo's or undo's.
 1327                          * Rollback the adjustments to this point and then
 1328                          * rollback the semaphore ups and down so we can return
 1329                          * with an error with all structures restored.  We
 1330                          * rollback the undo's in the exact reverse order that
 1331                          * we applied them.  This guarantees that we won't run
 1332                          * out of space as we roll things back out.
 1333                          */
 1334                         for (j = 0; j < i; j++) {
 1335                                 k = i - j - 1;
 1336                                 if ((sops[k].sem_flg & SEM_UNDO) == 0)
 1337                                         continue;
 1338                                 adjval = sops[k].sem_op;
 1339                                 if (adjval == 0)
 1340                                         continue;
 1341                                 if (semundo_adjust(td, &suptr, semid, seq,
 1342                                     sops[k].sem_num, adjval) != 0)
 1343                                         panic("semop - can't undo undos");
 1344                         }
 1345 
 1346                         for (j = 0; j < nsops; j++)
 1347                                 semakptr->u.sem_base[sops[j].sem_num].semval -=
 1348                                     sops[j].sem_op;
 1349 
 1350                         DPRINTF(("error = %d from semundo_adjust\n", error));
 1351                         SEMUNDO_UNLOCK();
 1352                         goto done2;
 1353                 } /* loop through the sops */
 1354                 SEMUNDO_UNLOCK();
 1355         } /* if (do_undos) */
 1356 
 1357         /* We're definitely done - set the sempid's and time */
 1358         for (i = 0; i < nsops; i++) {
 1359                 sopptr = &sops[i];
 1360                 semptr = &semakptr->u.sem_base[sopptr->sem_num];
 1361                 semptr->sempid = td->td_proc->p_pid;
 1362         }
 1363         semakptr->u.sem_otime = time_second;
 1364 
 1365         /*
 1366          * Do a wakeup if any semaphore was up'd whilst something was
 1367          * sleeping on it.
 1368          */
 1369         if (do_wakeup) {
 1370                 DPRINTF(("semop:  doing wakeup\n"));
 1371                 wakeup(semakptr);
 1372                 DPRINTF(("semop:  back from wakeup\n"));
 1373         }
 1374         DPRINTF(("semop:  done\n"));
 1375         td->td_retval[0] = 0;
 1376 done2:
 1377         mtx_unlock(sema_mtxp);
 1378         if (sops != small_sops)
 1379                 free(sops, M_SEM);
 1380         return (error);
 1381 }
 1382 
 1383 /*
 1384  * Go through the undo structures for this process and apply the adjustments to
 1385  * semaphores.
 1386  */
 1387 static void
 1388 semexit_myhook(void *arg, struct proc *p)
 1389 {
 1390         struct sem_undo *suptr;
 1391         struct semid_kernel *semakptr;
 1392         struct mtx *sema_mtxp;
 1393         int semid, semnum, adjval, ix;
 1394         unsigned short seq;
 1395 
 1396         /*
 1397          * Go through the chain of undo vectors looking for one
 1398          * associated with this process.
 1399          */
 1400         SEMUNDO_LOCK();
 1401         LIST_FOREACH(suptr, &semu_list, un_next) {
 1402                 if (suptr->un_proc == p)
 1403                         break;
 1404         }
 1405         if (suptr == NULL) {
 1406                 SEMUNDO_UNLOCK();
 1407                 return;
 1408         }
 1409         LIST_REMOVE(suptr, un_next);
 1410 
 1411         DPRINTF(("proc @%p has undo structure with %d entries\n", p,
 1412             suptr->un_cnt));
 1413 
 1414         /*
 1415          * If there are any active undo elements then process them.
 1416          */
 1417         if (suptr->un_cnt > 0) {
 1418                 SEMUNDO_UNLOCK();
 1419                 for (ix = 0; ix < suptr->un_cnt; ix++) {
 1420                         semid = suptr->un_ent[ix].un_id;
 1421                         semnum = suptr->un_ent[ix].un_num;
 1422                         adjval = suptr->un_ent[ix].un_adjval;
 1423                         seq = suptr->un_ent[ix].un_seq;
 1424                         semakptr = &sema[semid];
 1425                         sema_mtxp = &sema_mtx[semid];
 1426 
 1427                         mtx_lock(sema_mtxp);
 1428                         if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
 1429                             (semakptr->u.sem_perm.seq != seq)) {
 1430                                 mtx_unlock(sema_mtxp);
 1431                                 continue;
 1432                         }
 1433                         if (semnum >= semakptr->u.sem_nsems)
 1434                                 panic("semexit - semnum out of range");
 1435 
 1436                         DPRINTF((
 1437                             "semexit:  %p id=%d num=%d(adj=%d) ; sem=%d\n",
 1438                             suptr->un_proc, suptr->un_ent[ix].un_id,
 1439                             suptr->un_ent[ix].un_num,
 1440                             suptr->un_ent[ix].un_adjval,
 1441                             semakptr->u.sem_base[semnum].semval));
 1442 
 1443                         if (adjval < 0 && semakptr->u.sem_base[semnum].semval <
 1444                             -adjval)
 1445                                 semakptr->u.sem_base[semnum].semval = 0;
 1446                         else
 1447                                 semakptr->u.sem_base[semnum].semval += adjval;
 1448 
 1449                         wakeup(semakptr);
 1450                         DPRINTF(("semexit:  back from wakeup\n"));
 1451                         mtx_unlock(sema_mtxp);
 1452                 }
 1453                 SEMUNDO_LOCK();
 1454         }
 1455 
 1456         /*
 1457          * Deallocate the undo vector.
 1458          */
 1459         DPRINTF(("removing vector\n"));
 1460         suptr->un_proc = NULL;
 1461         suptr->un_cnt = 0;
 1462         LIST_INSERT_HEAD(&semu_free_list, suptr, un_next);
 1463         SEMUNDO_UNLOCK();
 1464 }
 1465 
 1466 static int
 1467 sysctl_sema(SYSCTL_HANDLER_ARGS)
 1468 {
 1469         struct prison *pr, *rpr;
 1470         struct semid_kernel tsemak;
 1471 #ifdef COMPAT_FREEBSD32
 1472         struct semid_kernel32 tsemak32;
 1473 #endif
 1474         void *outaddr;
 1475         size_t outsize;
 1476         int error, i;
 1477 
 1478         pr = req->td->td_ucred->cr_prison;
 1479         rpr = sem_find_prison(req->td->td_ucred);
 1480         error = 0;
 1481         for (i = 0; i < seminfo.semmni; i++) {
 1482                 mtx_lock(&sema_mtx[i]);
 1483                 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) == 0 ||
 1484                     rpr == NULL || sem_prison_cansee(rpr, &sema[i]) != 0)
 1485                         bzero(&tsemak, sizeof(tsemak));
 1486                 else {
 1487                         tsemak = sema[i];
 1488                         if (tsemak.cred->cr_prison != pr)
 1489                                 tsemak.u.sem_perm.key = IPC_PRIVATE;
 1490                 }
 1491                 mtx_unlock(&sema_mtx[i]);
 1492 #ifdef COMPAT_FREEBSD32
 1493                 if (SV_CURPROC_FLAG(SV_ILP32)) {
 1494                         bzero(&tsemak32, sizeof(tsemak32));
 1495                         freebsd32_ipcperm_out(&tsemak.u.sem_perm,
 1496                             &tsemak32.u.sem_perm);
 1497                         /* Don't copy u.sem_base */
 1498                         CP(tsemak, tsemak32, u.sem_nsems);
 1499                         CP(tsemak, tsemak32, u.sem_otime);
 1500                         CP(tsemak, tsemak32, u.sem_ctime);
 1501                         /* Don't copy label or cred */
 1502                         outaddr = &tsemak32;
 1503                         outsize = sizeof(tsemak32);
 1504                 } else
 1505 #endif
 1506                 {
 1507                         tsemak.u.sem_base = NULL;
 1508                         tsemak.label = NULL;
 1509                         tsemak.cred = NULL;
 1510                         outaddr = &tsemak;
 1511                         outsize = sizeof(tsemak);
 1512                 }
 1513                 error = SYSCTL_OUT(req, outaddr, outsize);
 1514                 if (error != 0)
 1515                         break;
 1516         }
 1517         return (error);
 1518 }
 1519 
 1520 static int
 1521 sem_prison_check(void *obj, void *data)
 1522 {
 1523         struct prison *pr = obj;
 1524         struct prison *prpr;
 1525         struct vfsoptlist *opts = data;
 1526         int error, jsys;
 1527 
 1528         /*
 1529          * sysvsem is a jailsys integer.
 1530          * It must be "disable" if the parent jail is disabled.
 1531          */
 1532         error = vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys));
 1533         if (error != ENOENT) {
 1534                 if (error != 0)
 1535                         return (error);
 1536                 switch (jsys) {
 1537                 case JAIL_SYS_DISABLE:
 1538                         break;
 1539                 case JAIL_SYS_NEW:
 1540                 case JAIL_SYS_INHERIT:
 1541                         prison_lock(pr->pr_parent);
 1542                         prpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
 1543                         prison_unlock(pr->pr_parent);
 1544                         if (prpr == NULL)
 1545                                 return (EPERM);
 1546                         break;
 1547                 default:
 1548                         return (EINVAL);
 1549                 }
 1550         }
 1551 
 1552         return (0);
 1553 }
 1554 
 1555 static int
 1556 sem_prison_set(void *obj, void *data)
 1557 {
 1558         struct prison *pr = obj;
 1559         struct prison *tpr, *orpr, *nrpr, *trpr;
 1560         struct vfsoptlist *opts = data;
 1561         void *rsv;
 1562         int jsys, descend;
 1563 
 1564         /*
 1565          * sysvsem controls which jail is the root of the associated sems (this
 1566          * jail or same as the parent), or if the feature is available at all.
 1567          */
 1568         if (vfs_copyopt(opts, "sysvsem", &jsys, sizeof(jsys)) == ENOENT)
 1569                 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
 1570                     ? JAIL_SYS_INHERIT
 1571                     : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
 1572                     ? JAIL_SYS_DISABLE
 1573                     : -1;
 1574         if (jsys == JAIL_SYS_DISABLE) {
 1575                 prison_lock(pr);
 1576                 orpr = osd_jail_get(pr, sem_prison_slot);
 1577                 if (orpr != NULL)
 1578                         osd_jail_del(pr, sem_prison_slot);
 1579                 prison_unlock(pr);
 1580                 if (orpr != NULL) {
 1581                         if (orpr == pr)
 1582                                 sem_prison_cleanup(pr);
 1583                         /* Disable all child jails as well. */
 1584                         FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
 1585                                 prison_lock(tpr);
 1586                                 trpr = osd_jail_get(tpr, sem_prison_slot);
 1587                                 if (trpr != NULL) {
 1588                                         osd_jail_del(tpr, sem_prison_slot);
 1589                                         prison_unlock(tpr);
 1590                                         if (trpr == tpr)
 1591                                                 sem_prison_cleanup(tpr);
 1592                                 } else {
 1593                                         prison_unlock(tpr);
 1594                                         descend = 0;
 1595                                 }
 1596                         }
 1597                 }
 1598         } else if (jsys != -1) {
 1599                 if (jsys == JAIL_SYS_NEW)
 1600                         nrpr = pr;
 1601                 else {
 1602                         prison_lock(pr->pr_parent);
 1603                         nrpr = osd_jail_get(pr->pr_parent, sem_prison_slot);
 1604                         prison_unlock(pr->pr_parent);
 1605                 }
 1606                 rsv = osd_reserve(sem_prison_slot);
 1607                 prison_lock(pr);
 1608                 orpr = osd_jail_get(pr, sem_prison_slot);
 1609                 if (orpr != nrpr)
 1610                         (void)osd_jail_set_reserved(pr, sem_prison_slot, rsv,
 1611                             nrpr);
 1612                 else
 1613                         osd_free_reserved(rsv);
 1614                 prison_unlock(pr);
 1615                 if (orpr != nrpr) {
 1616                         if (orpr == pr)
 1617                                 sem_prison_cleanup(pr);
 1618                         if (orpr != NULL) {
 1619                                 /* Change child jails matching the old root, */
 1620                                 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
 1621                                         prison_lock(tpr);
 1622                                         trpr = osd_jail_get(tpr,
 1623                                             sem_prison_slot);
 1624                                         if (trpr == orpr) {
 1625                                                 (void)osd_jail_set(tpr,
 1626                                                     sem_prison_slot, nrpr);
 1627                                                 prison_unlock(tpr);
 1628                                                 if (trpr == tpr)
 1629                                                         sem_prison_cleanup(tpr);
 1630                                         } else {
 1631                                                 prison_unlock(tpr);
 1632                                                 descend = 0;
 1633                                         }
 1634                                 }
 1635                         }
 1636                 }
 1637         }
 1638 
 1639         return (0);
 1640 }
 1641 
 1642 static int
 1643 sem_prison_get(void *obj, void *data)
 1644 {
 1645         struct prison *pr = obj;
 1646         struct prison *rpr;
 1647         struct vfsoptlist *opts = data;
 1648         int error, jsys;
 1649 
 1650         /* Set sysvsem based on the jail's root prison. */
 1651         prison_lock(pr);
 1652         rpr = osd_jail_get(pr, sem_prison_slot);
 1653         prison_unlock(pr);
 1654         jsys = rpr == NULL ? JAIL_SYS_DISABLE
 1655             : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
 1656         error = vfs_setopt(opts, "sysvsem", &jsys, sizeof(jsys));
 1657         if (error == ENOENT)
 1658                 error = 0;
 1659         return (error);
 1660 }
 1661 
 1662 static int
 1663 sem_prison_remove(void *obj, void *data __unused)
 1664 {
 1665         struct prison *pr = obj;
 1666         struct prison *rpr;
 1667 
 1668         prison_lock(pr);
 1669         rpr = osd_jail_get(pr, sem_prison_slot);
 1670         prison_unlock(pr);
 1671         if (rpr == pr)
 1672                 sem_prison_cleanup(pr);
 1673         return (0);
 1674 }
 1675 
 1676 static void
 1677 sem_prison_cleanup(struct prison *pr)
 1678 {
 1679         int i;
 1680 
 1681         /* Remove any sems that belong to this jail. */
 1682         mtx_lock(&sem_mtx);
 1683         for (i = 0; i < seminfo.semmni; i++) {
 1684                 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
 1685                     sema[i].cred != NULL && sema[i].cred->cr_prison == pr) {
 1686                         mtx_lock(&sema_mtx[i]);
 1687                         sem_remove(i, NULL);
 1688                         mtx_unlock(&sema_mtx[i]);
 1689                 }
 1690         }
 1691         mtx_unlock(&sem_mtx);
 1692 }
 1693 
 1694 SYSCTL_JAIL_PARAM_SYS_NODE(sysvsem, CTLFLAG_RW, "SYSV semaphores");
 1695 
 1696 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1697     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1698 
 1699 /* XXX casting to (sy_call_t *) is bogus, as usual. */
 1700 static sy_call_t *semcalls[] = {
 1701         (sy_call_t *)freebsd7___semctl, (sy_call_t *)sys_semget,
 1702         (sy_call_t *)sys_semop
 1703 };
 1704 
 1705 /*
 1706  * Entry point for all SEM calls.
 1707  */
 1708 int
 1709 sys_semsys(td, uap)
 1710         struct thread *td;
 1711         /* XXX actually varargs. */
 1712         struct semsys_args /* {
 1713                 int     which;
 1714                 int     a2;
 1715                 int     a3;
 1716                 int     a4;
 1717                 int     a5;
 1718         } */ *uap;
 1719 {
 1720         int error;
 1721 
 1722         if (uap->which < 0 || uap->which >= nitems(semcalls))
 1723                 return (EINVAL);
 1724         error = (*semcalls[uap->which])(td, &uap->a2);
 1725         return (error);
 1726 }
 1727 
 1728 #ifndef CP
 1729 #define CP(src, dst, fld)       do { (dst).fld = (src).fld; } while (0)
 1730 #endif
 1731 
 1732 #ifndef _SYS_SYSPROTO_H_
 1733 struct freebsd7___semctl_args {
 1734         int     semid;
 1735         int     semnum;
 1736         int     cmd;
 1737         union   semun_old *arg;
 1738 };
 1739 #endif
 1740 int
 1741 freebsd7___semctl(struct thread *td, struct freebsd7___semctl_args *uap)
 1742 {
 1743         struct semid_ds_old dsold;
 1744         struct semid_ds dsbuf;
 1745         union semun_old arg;
 1746         union semun semun;
 1747         register_t rval;
 1748         int error;
 1749 
 1750         switch (uap->cmd) {
 1751         case SEM_STAT:
 1752         case IPC_SET:
 1753         case IPC_STAT:
 1754         case GETALL:
 1755         case SETVAL:
 1756         case SETALL:
 1757                 error = copyin(uap->arg, &arg, sizeof(arg));
 1758                 if (error)
 1759                         return (error);
 1760                 break;
 1761         }
 1762 
 1763         switch (uap->cmd) {
 1764         case SEM_STAT:
 1765         case IPC_STAT:
 1766                 semun.buf = &dsbuf;
 1767                 break;
 1768         case IPC_SET:
 1769                 error = copyin(arg.buf, &dsold, sizeof(dsold));
 1770                 if (error)
 1771                         return (error);
 1772                 ipcperm_old2new(&dsold.sem_perm, &dsbuf.sem_perm);
 1773                 CP(dsold, dsbuf, sem_base);
 1774                 CP(dsold, dsbuf, sem_nsems);
 1775                 CP(dsold, dsbuf, sem_otime);
 1776                 CP(dsold, dsbuf, sem_ctime);
 1777                 semun.buf = &dsbuf;
 1778                 break;
 1779         case GETALL:
 1780         case SETALL:
 1781                 semun.array = arg.array;
 1782                 break;
 1783         case SETVAL:
 1784                 semun.val = arg.val;
 1785                 break;          
 1786         }
 1787 
 1788         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1789             &rval);
 1790         if (error)
 1791                 return (error);
 1792 
 1793         switch (uap->cmd) {
 1794         case SEM_STAT:
 1795         case IPC_STAT:
 1796                 bzero(&dsold, sizeof(dsold));
 1797                 ipcperm_new2old(&dsbuf.sem_perm, &dsold.sem_perm);
 1798                 CP(dsbuf, dsold, sem_base);
 1799                 CP(dsbuf, dsold, sem_nsems);
 1800                 CP(dsbuf, dsold, sem_otime);
 1801                 CP(dsbuf, dsold, sem_ctime);
 1802                 error = copyout(&dsold, arg.buf, sizeof(dsold));
 1803                 break;
 1804         }
 1805 
 1806         if (error == 0)
 1807                 td->td_retval[0] = rval;
 1808         return (error);
 1809 }
 1810 
 1811 #endif /* COMPAT_FREEBSD{4,5,6,7} */
 1812 
 1813 #ifdef COMPAT_FREEBSD32
 1814 
 1815 int
 1816 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
 1817 {
 1818 
 1819 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1820     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1821         switch (uap->which) {
 1822         case 0:
 1823                 return (freebsd7_freebsd32_semctl(td,
 1824                     (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
 1825         default:
 1826                 return (sys_semsys(td, (struct semsys_args *)uap));
 1827         }
 1828 #else
 1829         return (nosys(td, NULL));
 1830 #endif
 1831 }
 1832 
 1833 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 1834     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 1835 int
 1836 freebsd7_freebsd32_semctl(struct thread *td,
 1837     struct freebsd7_freebsd32_semctl_args *uap)
 1838 {
 1839         struct semid_ds32_old dsbuf32;
 1840         struct semid_ds dsbuf;
 1841         union semun semun;
 1842         union semun32 arg;
 1843         register_t rval;
 1844         int error;
 1845 
 1846         switch (uap->cmd) {
 1847         case SEM_STAT:
 1848         case IPC_SET:
 1849         case IPC_STAT:
 1850         case GETALL:
 1851         case SETVAL:
 1852         case SETALL:
 1853                 error = copyin(uap->arg, &arg, sizeof(arg));
 1854                 if (error)
 1855                         return (error);         
 1856                 break;
 1857         }
 1858 
 1859         switch (uap->cmd) {
 1860         case SEM_STAT:
 1861         case IPC_STAT:
 1862                 semun.buf = &dsbuf;
 1863                 break;
 1864         case IPC_SET:
 1865                 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
 1866                 if (error)
 1867                         return (error);
 1868                 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
 1869                 PTRIN_CP(dsbuf32, dsbuf, sem_base);
 1870                 CP(dsbuf32, dsbuf, sem_nsems);
 1871                 CP(dsbuf32, dsbuf, sem_otime);
 1872                 CP(dsbuf32, dsbuf, sem_ctime);
 1873                 semun.buf = &dsbuf;
 1874                 break;
 1875         case GETALL:
 1876         case SETALL:
 1877                 semun.array = PTRIN(arg.array);
 1878                 break;
 1879         case SETVAL:
 1880                 semun.val = arg.val;
 1881                 break;
 1882         }
 1883 
 1884         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1885             &rval);
 1886         if (error)
 1887                 return (error);
 1888 
 1889         switch (uap->cmd) {
 1890         case SEM_STAT:
 1891         case IPC_STAT:
 1892                 bzero(&dsbuf32, sizeof(dsbuf32));
 1893                 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
 1894                 PTROUT_CP(dsbuf, dsbuf32, sem_base);
 1895                 CP(dsbuf, dsbuf32, sem_nsems);
 1896                 CP(dsbuf, dsbuf32, sem_otime);
 1897                 CP(dsbuf, dsbuf32, sem_ctime);
 1898                 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
 1899                 break;
 1900         }
 1901 
 1902         if (error == 0)
 1903                 td->td_retval[0] = rval;
 1904         return (error);
 1905 }
 1906 #endif
 1907 
 1908 int
 1909 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
 1910 {
 1911         struct semid_ds32 dsbuf32;
 1912         struct semid_ds dsbuf;
 1913         union semun semun;
 1914         union semun32 arg;
 1915         register_t rval;
 1916         int error;
 1917 
 1918         switch (uap->cmd) {
 1919         case SEM_STAT:
 1920         case IPC_SET:
 1921         case IPC_STAT:
 1922         case GETALL:
 1923         case SETVAL:
 1924         case SETALL:
 1925                 error = copyin(uap->arg, &arg, sizeof(arg));
 1926                 if (error)
 1927                         return (error);         
 1928                 break;
 1929         }
 1930 
 1931         switch (uap->cmd) {
 1932         case SEM_STAT:
 1933         case IPC_STAT:
 1934                 semun.buf = &dsbuf;
 1935                 break;
 1936         case IPC_SET:
 1937                 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
 1938                 if (error)
 1939                         return (error);
 1940                 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
 1941                 PTRIN_CP(dsbuf32, dsbuf, sem_base);
 1942                 CP(dsbuf32, dsbuf, sem_nsems);
 1943                 CP(dsbuf32, dsbuf, sem_otime);
 1944                 CP(dsbuf32, dsbuf, sem_ctime);
 1945                 semun.buf = &dsbuf;
 1946                 break;
 1947         case GETALL:
 1948         case SETALL:
 1949                 semun.array = PTRIN(arg.array);
 1950                 break;
 1951         case SETVAL:
 1952                 semun.val = arg.val;
 1953                 break;          
 1954         }
 1955 
 1956         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1957             &rval);
 1958         if (error)
 1959                 return (error);
 1960 
 1961         switch (uap->cmd) {
 1962         case SEM_STAT:
 1963         case IPC_STAT:
 1964                 bzero(&dsbuf32, sizeof(dsbuf32));
 1965                 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
 1966                 PTROUT_CP(dsbuf, dsbuf32, sem_base);
 1967                 CP(dsbuf, dsbuf32, sem_nsems);
 1968                 CP(dsbuf, dsbuf32, sem_otime);
 1969                 CP(dsbuf, dsbuf32, sem_ctime);
 1970                 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
 1971                 break;
 1972         }
 1973 
 1974         if (error == 0)
 1975                 td->td_retval[0] = rval;
 1976         return (error);
 1977 }
 1978 
 1979 #endif /* COMPAT_FREEBSD32 */

Cache object: 72ffe7e7c9c81a88edcdfb1732e11654


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