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/uipc_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  * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
    3  * Copyright (c) 2003-2005 SPARTA, Inc.
    4  * Copyright (c) 2005 Robert N. M. Watson
    5  * All rights reserved.
    6  *
    7  * This software was developed for the FreeBSD Project in part by Network
    8  * Associates Laboratories, the Security Research Division of Network
    9  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
   10  * as part of the DARPA CHATS research program.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD$");
   36 
   37 #include "opt_compat.h"
   38 #include "opt_posix.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/condvar.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/file.h>
   44 #include <sys/filedesc.h>
   45 #include <sys/fnv_hash.h>
   46 #include <sys/kernel.h>
   47 #include <sys/ksem.h>
   48 #include <sys/lock.h>
   49 #include <sys/malloc.h>
   50 #include <sys/module.h>
   51 #include <sys/mutex.h>
   52 #include <sys/priv.h>
   53 #include <sys/proc.h>
   54 #include <sys/posix4.h>
   55 #include <sys/semaphore.h>
   56 #include <sys/_semaphore.h>
   57 #include <sys/stat.h>
   58 #include <sys/syscall.h>
   59 #include <sys/syscallsubr.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/sysent.h>
   62 #include <sys/sysproto.h>
   63 #include <sys/systm.h>
   64 #include <sys/sx.h>
   65 #include <sys/vnode.h>
   66 
   67 #include <security/mac/mac_framework.h>
   68 
   69 /*
   70  * TODO
   71  *
   72  * - Resource limits?
   73  * - Update fstat(1)
   74  * - Replace global sem_lock with mtx_pool locks?
   75  * - Add a MAC check_create() hook for creating new named semaphores.
   76  */
   77 
   78 #ifndef SEM_MAX
   79 #define SEM_MAX 30
   80 #endif
   81 
   82 #ifdef SEM_DEBUG
   83 #define DP(x)   printf x
   84 #else
   85 #define DP(x)
   86 #endif
   87 
   88 struct ksem_mapping {
   89         char            *km_path;
   90         Fnv32_t         km_fnv;
   91         struct ksem     *km_ksem;
   92         LIST_ENTRY(ksem_mapping) km_link;
   93 };
   94 
   95 static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
   96 static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
   97 static struct sx ksem_dict_lock;
   98 static struct mtx ksem_count_lock;
   99 static struct mtx sem_lock;
  100 static u_long ksem_hash;
  101 static int ksem_dead;
  102 
  103 #define KSEM_HASH(fnv)  (&ksem_dictionary[(fnv) & ksem_hash])
  104 
  105 static int nsems = 0;
  106 SYSCTL_DECL(_p1003_1b);
  107 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
  108     "Number of active kernel POSIX semaphores");
  109 
  110 static int      kern_sem_wait(struct thread *td, semid_t id, int tryflag,
  111                     struct timespec *abstime);
  112 static int      ksem_access(struct ksem *ks, struct ucred *ucred);
  113 static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
  114                     unsigned int value);
  115 static int      ksem_create(struct thread *td, const char *path,
  116                     semid_t *semidp, mode_t mode, unsigned int value,
  117                     int flags, int compat32);
  118 static void     ksem_drop(struct ksem *ks);
  119 static int      ksem_get(struct thread *td, semid_t id, struct file **fpp);
  120 static struct ksem *ksem_hold(struct ksem *ks);
  121 static void     ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
  122 static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
  123 static void     ksem_module_destroy(void);
  124 static int      ksem_module_init(void);
  125 static int      ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
  126 static int      sem_modload(struct module *module, int cmd, void *arg);
  127 
  128 static fo_rdwr_t        ksem_read;
  129 static fo_rdwr_t        ksem_write;
  130 static fo_truncate_t    ksem_truncate;
  131 static fo_ioctl_t       ksem_ioctl;
  132 static fo_poll_t        ksem_poll;
  133 static fo_kqfilter_t    ksem_kqfilter;
  134 static fo_stat_t        ksem_stat;
  135 static fo_close_t       ksem_closef;
  136 
  137 /* File descriptor operations. */
  138 static struct fileops ksem_ops = {
  139         .fo_read = ksem_read,
  140         .fo_write = ksem_write,
  141         .fo_truncate = ksem_truncate,
  142         .fo_ioctl = ksem_ioctl,
  143         .fo_poll = ksem_poll,
  144         .fo_kqfilter = ksem_kqfilter,
  145         .fo_stat = ksem_stat,
  146         .fo_close = ksem_closef,
  147         .fo_flags = DFLAG_PASSABLE
  148 };
  149 
  150 FEATURE(posix_sem, "POSIX semaphores");
  151 
  152 static int
  153 ksem_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
  154     int flags, struct thread *td)
  155 {
  156 
  157         return (EOPNOTSUPP);
  158 }
  159 
  160 static int
  161 ksem_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
  162     int flags, struct thread *td)
  163 {
  164 
  165         return (EOPNOTSUPP);
  166 }
  167 
  168 static int
  169 ksem_truncate(struct file *fp, off_t length, struct ucred *active_cred,
  170     struct thread *td)
  171 {
  172 
  173         return (EINVAL);
  174 }
  175 
  176 static int
  177 ksem_ioctl(struct file *fp, u_long com, void *data,
  178     struct ucred *active_cred, struct thread *td)
  179 {
  180 
  181         return (EOPNOTSUPP);
  182 }
  183 
  184 static int
  185 ksem_poll(struct file *fp, int events, struct ucred *active_cred,
  186     struct thread *td)
  187 {
  188 
  189         return (EOPNOTSUPP);
  190 }
  191 
  192 static int
  193 ksem_kqfilter(struct file *fp, struct knote *kn)
  194 {
  195 
  196         return (EOPNOTSUPP);
  197 }
  198 
  199 static int
  200 ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
  201     struct thread *td)
  202 {
  203         struct ksem *ks;
  204 #ifdef MAC
  205         int error;
  206 #endif
  207 
  208         ks = fp->f_data;
  209 
  210 #ifdef MAC
  211         error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
  212         if (error)
  213                 return (error);
  214 #endif
  215         
  216         /*
  217          * Attempt to return sanish values for fstat() on a semaphore
  218          * file descriptor.
  219          */
  220         bzero(sb, sizeof(*sb));
  221         sb->st_mode = S_IFREG | ks->ks_mode;            /* XXX */
  222 
  223         sb->st_atimespec = ks->ks_atime;
  224         sb->st_ctimespec = ks->ks_ctime;
  225         sb->st_mtimespec = ks->ks_mtime;
  226         sb->st_birthtimespec = ks->ks_birthtime;        
  227         sb->st_uid = ks->ks_uid;
  228         sb->st_gid = ks->ks_gid;
  229 
  230         return (0);
  231 }
  232 
  233 static int
  234 ksem_closef(struct file *fp, struct thread *td)
  235 {
  236         struct ksem *ks;
  237 
  238         ks = fp->f_data;
  239         fp->f_data = NULL;
  240         ksem_drop(ks);
  241 
  242         return (0);
  243 }
  244 
  245 /*
  246  * ksem object management including creation and reference counting
  247  * routines.
  248  */
  249 static struct ksem *
  250 ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
  251 {
  252         struct ksem *ks;
  253 
  254         mtx_lock(&ksem_count_lock);
  255         if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
  256                 mtx_unlock(&ksem_count_lock);
  257                 return (NULL);
  258         }
  259         nsems++;
  260         mtx_unlock(&ksem_count_lock);
  261         ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
  262         ks->ks_uid = ucred->cr_uid;
  263         ks->ks_gid = ucred->cr_gid;
  264         ks->ks_mode = mode;
  265         ks->ks_value = value;
  266         cv_init(&ks->ks_cv, "ksem");
  267         vfs_timestamp(&ks->ks_birthtime);
  268         ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
  269         refcount_init(&ks->ks_ref, 1);
  270 #ifdef MAC
  271         mac_posixsem_init(ks);
  272         mac_posixsem_create(ucred, ks);
  273 #endif
  274 
  275         return (ks);
  276 }
  277 
  278 static struct ksem *
  279 ksem_hold(struct ksem *ks)
  280 {
  281 
  282         refcount_acquire(&ks->ks_ref);
  283         return (ks);
  284 }
  285 
  286 static void
  287 ksem_drop(struct ksem *ks)
  288 {
  289 
  290         if (refcount_release(&ks->ks_ref)) {
  291 #ifdef MAC
  292                 mac_posixsem_destroy(ks);
  293 #endif
  294                 cv_destroy(&ks->ks_cv);
  295                 free(ks, M_KSEM);
  296                 mtx_lock(&ksem_count_lock);
  297                 nsems--;
  298                 mtx_unlock(&ksem_count_lock);
  299         }
  300 }
  301 
  302 /*
  303  * Determine if the credentials have sufficient permissions for read
  304  * and write access.
  305  */
  306 static int
  307 ksem_access(struct ksem *ks, struct ucred *ucred)
  308 {
  309         int error;
  310 
  311         error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
  312             VREAD | VWRITE, ucred, NULL);
  313         if (error)
  314                 error = priv_check_cred(ucred, PRIV_SEM_WRITE, 0);
  315         return (error);
  316 }
  317 
  318 /*
  319  * Dictionary management.  We maintain an in-kernel dictionary to map
  320  * paths to semaphore objects.  We use the FNV hash on the path to
  321  * store the mappings in a hash table.
  322  */
  323 static struct ksem *
  324 ksem_lookup(char *path, Fnv32_t fnv)
  325 {
  326         struct ksem_mapping *map;
  327 
  328         LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
  329                 if (map->km_fnv != fnv)
  330                         continue;
  331                 if (strcmp(map->km_path, path) == 0)
  332                         return (map->km_ksem);
  333         }
  334 
  335         return (NULL);
  336 }
  337 
  338 static void
  339 ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
  340 {
  341         struct ksem_mapping *map;
  342 
  343         map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
  344         map->km_path = path;
  345         map->km_fnv = fnv;
  346         map->km_ksem = ksem_hold(ks);
  347         ks->ks_path = path;
  348         LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
  349 }
  350 
  351 static int
  352 ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
  353 {
  354         struct ksem_mapping *map;
  355         int error;
  356 
  357         LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
  358                 if (map->km_fnv != fnv)
  359                         continue;
  360                 if (strcmp(map->km_path, path) == 0) {
  361 #ifdef MAC
  362                         error = mac_posixsem_check_unlink(ucred, map->km_ksem);
  363                         if (error)
  364                                 return (error);
  365 #endif
  366                         error = ksem_access(map->km_ksem, ucred);
  367                         if (error)
  368                                 return (error);
  369                         map->km_ksem->ks_path = NULL;
  370                         LIST_REMOVE(map, km_link);
  371                         ksem_drop(map->km_ksem);
  372                         free(map->km_path, M_KSEM);
  373                         free(map, M_KSEM);
  374                         return (0);
  375                 }
  376         }
  377 
  378         return (ENOENT);
  379 }
  380 
  381 static void
  382 ksem_info_impl(struct ksem *ks, char *path, size_t size, uint32_t *value)
  383 {
  384 
  385         if (ks->ks_path == NULL)
  386                 return;
  387         sx_slock(&ksem_dict_lock);
  388         if (ks->ks_path != NULL)
  389                 strlcpy(path, ks->ks_path, size);
  390         if (value != NULL)
  391                 *value = ks->ks_value;
  392         sx_sunlock(&ksem_dict_lock);
  393 }
  394 
  395 static int
  396 ksem_create_copyout_semid(struct thread *td, semid_t *semidp, int fd,
  397     int compat32)
  398 {
  399         semid_t semid;
  400 #ifdef COMPAT_FREEBSD32
  401         int32_t semid32;
  402 #endif
  403         void *ptr;
  404         size_t ptrs;
  405 
  406 #ifdef COMPAT_FREEBSD32
  407         if (compat32) {
  408                 semid32 = fd;
  409                 ptr = &semid32;
  410                 ptrs = sizeof(semid32);
  411         } else {
  412 #endif
  413                 semid = fd;
  414                 ptr = &semid;
  415                 ptrs = sizeof(semid);
  416                 compat32 = 0; /* silence gcc */
  417 #ifdef COMPAT_FREEBSD32
  418         }
  419 #endif
  420 
  421         return (copyout(ptr, semidp, ptrs));
  422 }
  423 
  424 /* Other helper routines. */
  425 static int
  426 ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
  427     unsigned int value, int flags, int compat32)
  428 {
  429         struct filedesc *fdp;
  430         struct ksem *ks;
  431         struct file *fp;
  432         char *path;
  433         Fnv32_t fnv;
  434         int error, fd;
  435 
  436         if (value > SEM_VALUE_MAX)
  437                 return (EINVAL);
  438 
  439         fdp = td->td_proc->p_fd;
  440         mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
  441         error = falloc(td, &fp, &fd);
  442         if (error) {
  443                 if (name == NULL)
  444                         error = ENOSPC;
  445                 return (error);
  446         }
  447 
  448         /*
  449          * Go ahead and copyout the file descriptor now.  This is a bit
  450          * premature, but it is a lot easier to handle errors as opposed
  451          * to later when we've possibly created a new semaphore, etc.
  452          */
  453         error = ksem_create_copyout_semid(td, semidp, fd, compat32);
  454         if (error) {
  455                 fdclose(fdp, fp, fd, td);
  456                 fdrop(fp, td);
  457                 return (error);
  458         }
  459 
  460         if (name == NULL) {
  461                 /* Create an anonymous semaphore. */
  462                 ks = ksem_alloc(td->td_ucred, mode, value);
  463                 if (ks == NULL)
  464                         error = ENOSPC;
  465                 else
  466                         ks->ks_flags |= KS_ANONYMOUS;
  467         } else {
  468                 path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
  469                 error = copyinstr(name, path, MAXPATHLEN, NULL);
  470 
  471                 /* Require paths to start with a '/' character. */
  472                 if (error == 0 && path[0] != '/')
  473                         error = EINVAL;
  474                 if (error) {
  475                         fdclose(fdp, fp, fd, td);
  476                         fdrop(fp, td);
  477                         free(path, M_KSEM);
  478                         return (error);
  479                 }
  480 
  481                 fnv = fnv_32_str(path, FNV1_32_INIT);
  482                 sx_xlock(&ksem_dict_lock);
  483                 ks = ksem_lookup(path, fnv);
  484                 if (ks == NULL) {
  485                         /* Object does not exist, create it if requested. */
  486                         if (flags & O_CREAT) {
  487                                 ks = ksem_alloc(td->td_ucred, mode, value);
  488                                 if (ks == NULL)
  489                                         error = ENFILE;
  490                                 else {
  491                                         ksem_insert(path, fnv, ks);
  492                                         path = NULL;
  493                                 }
  494                         } else
  495                                 error = ENOENT;
  496                 } else {
  497                         /*
  498                          * Object already exists, obtain a new
  499                          * reference if requested and permitted.
  500                          */
  501                         if ((flags & (O_CREAT | O_EXCL)) ==
  502                             (O_CREAT | O_EXCL))
  503                                 error = EEXIST;
  504                         else {
  505 #ifdef MAC
  506                                 error = mac_posixsem_check_open(td->td_ucred,
  507                                     ks);
  508                                 if (error == 0)
  509 #endif
  510                                 error = ksem_access(ks, td->td_ucred);
  511                         }
  512                         if (error == 0)
  513                                 ksem_hold(ks);
  514 #ifdef INVARIANTS
  515                         else
  516                                 ks = NULL;
  517 #endif
  518                 }
  519                 sx_xunlock(&ksem_dict_lock);
  520                 if (path)
  521                         free(path, M_KSEM);
  522         }
  523 
  524         if (error) {
  525                 KASSERT(ks == NULL, ("ksem_create error with a ksem"));
  526                 fdclose(fdp, fp, fd, td);
  527                 fdrop(fp, td);
  528                 return (error);
  529         }
  530         KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
  531 
  532         finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
  533 
  534         FILEDESC_XLOCK(fdp);
  535         if (fdp->fd_ofiles[fd] == fp)
  536                 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
  537         FILEDESC_XUNLOCK(fdp);
  538         fdrop(fp, td);
  539 
  540         return (0);
  541 }
  542 
  543 static int
  544 ksem_get(struct thread *td, semid_t id, struct file **fpp)
  545 {
  546         struct ksem *ks;
  547         struct file *fp;
  548         int error;
  549 
  550         error = fget(td, id, &fp);
  551         if (error)
  552                 return (EINVAL);
  553         if (fp->f_type != DTYPE_SEM) {
  554                 fdrop(fp, td);
  555                 return (EINVAL);
  556         }
  557         ks = fp->f_data;
  558         if (ks->ks_flags & KS_DEAD) {
  559                 fdrop(fp, td);
  560                 return (EINVAL);
  561         }
  562         *fpp = fp;
  563         return (0);
  564 }
  565 
  566 /* System calls. */
  567 #ifndef _SYS_SYSPROTO_H_
  568 struct ksem_init_args {
  569         unsigned int    value;
  570         semid_t         *idp;
  571 };
  572 #endif
  573 int
  574 ksem_init(struct thread *td, struct ksem_init_args *uap)
  575 {
  576 
  577         return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
  578             0, 0));
  579 }
  580 
  581 #ifndef _SYS_SYSPROTO_H_
  582 struct ksem_open_args {
  583         char            *name;
  584         int             oflag;
  585         mode_t          mode;
  586         unsigned int    value;
  587         semid_t         *idp;   
  588 };
  589 #endif
  590 int
  591 ksem_open(struct thread *td, struct ksem_open_args *uap)
  592 {
  593 
  594         DP((">>> ksem_open start, pid=%d\n", (int)td->td_proc->p_pid));
  595 
  596         if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
  597                 return (EINVAL);
  598         return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
  599             uap->oflag, 0));
  600 }
  601 
  602 #ifndef _SYS_SYSPROTO_H_
  603 struct ksem_unlink_args {
  604         char            *name;
  605 };
  606 #endif
  607 int
  608 ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
  609 {
  610         char *path;
  611         Fnv32_t fnv;
  612         int error;
  613 
  614         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  615         error = copyinstr(uap->name, path, MAXPATHLEN, NULL);
  616         if (error) {
  617                 free(path, M_TEMP);
  618                 return (error);
  619         }
  620 
  621         fnv = fnv_32_str(path, FNV1_32_INIT);
  622         sx_xlock(&ksem_dict_lock);
  623         error = ksem_remove(path, fnv, td->td_ucred);
  624         sx_xunlock(&ksem_dict_lock);
  625         free(path, M_TEMP);
  626 
  627         return (error);
  628 }
  629 
  630 #ifndef _SYS_SYSPROTO_H_
  631 struct ksem_close_args {
  632         semid_t         id;
  633 };
  634 #endif
  635 int
  636 ksem_close(struct thread *td, struct ksem_close_args *uap)
  637 {
  638         struct ksem *ks;
  639         struct file *fp;
  640         int error;
  641 
  642         error = ksem_get(td, uap->id, &fp);
  643         if (error)
  644                 return (error);
  645         ks = fp->f_data;
  646         if (ks->ks_flags & KS_ANONYMOUS) {
  647                 fdrop(fp, td);
  648                 return (EINVAL);
  649         }
  650         error = kern_close(td, uap->id);
  651         fdrop(fp, td);
  652         return (error);
  653 }
  654 
  655 #ifndef _SYS_SYSPROTO_H_
  656 struct ksem_post_args {
  657         semid_t id;
  658 };
  659 #endif
  660 int
  661 ksem_post(struct thread *td, struct ksem_post_args *uap)
  662 {
  663         struct file *fp;
  664         struct ksem *ks;
  665         int error;
  666 
  667         error = ksem_get(td, uap->id, &fp);
  668         if (error)
  669                 return (error);
  670         ks = fp->f_data;
  671 
  672         mtx_lock(&sem_lock);
  673 #ifdef MAC
  674         error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
  675         if (error)
  676                 goto err;
  677 #endif
  678         if (ks->ks_value == SEM_VALUE_MAX) {
  679                 error = EOVERFLOW;
  680                 goto err;
  681         }
  682         ++ks->ks_value;
  683         if (ks->ks_waiters > 0)
  684                 cv_signal(&ks->ks_cv);
  685         error = 0;
  686         vfs_timestamp(&ks->ks_ctime);
  687 err:
  688         mtx_unlock(&sem_lock);
  689         fdrop(fp, td);
  690         return (error);
  691 }
  692 
  693 #ifndef _SYS_SYSPROTO_H_
  694 struct ksem_wait_args {
  695         semid_t         id;
  696 };
  697 #endif
  698 int
  699 ksem_wait(struct thread *td, struct ksem_wait_args *uap)
  700 {
  701 
  702         return (kern_sem_wait(td, uap->id, 0, NULL));
  703 }
  704 
  705 #ifndef _SYS_SYSPROTO_H_
  706 struct ksem_timedwait_args {
  707         semid_t         id;
  708         const struct timespec *abstime;
  709 };
  710 #endif
  711 int
  712 ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
  713 {
  714         struct timespec abstime;
  715         struct timespec *ts;
  716         int error;
  717 
  718         /*
  719          * We allow a null timespec (wait forever).
  720          */
  721         if (uap->abstime == NULL)
  722                 ts = NULL;
  723         else {
  724                 error = copyin(uap->abstime, &abstime, sizeof(abstime));
  725                 if (error != 0)
  726                         return (error);
  727                 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
  728                         return (EINVAL);
  729                 ts = &abstime;
  730         }
  731         return (kern_sem_wait(td, uap->id, 0, ts));
  732 }
  733 
  734 #ifndef _SYS_SYSPROTO_H_
  735 struct ksem_trywait_args {
  736         semid_t         id;
  737 };
  738 #endif
  739 int
  740 ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
  741 {
  742 
  743         return (kern_sem_wait(td, uap->id, 1, NULL));
  744 }
  745 
  746 static int
  747 kern_sem_wait(struct thread *td, semid_t id, int tryflag,
  748     struct timespec *abstime)
  749 {
  750         struct timespec ts1, ts2;
  751         struct timeval tv;
  752         struct file *fp;
  753         struct ksem *ks;
  754         int error;
  755 
  756         DP((">>> kern_sem_wait entered! pid=%d\n", (int)td->td_proc->p_pid));
  757         error = ksem_get(td, id, &fp);
  758         if (error)
  759                 return (error);
  760         ks = fp->f_data;
  761         mtx_lock(&sem_lock);
  762         DP((">>> kern_sem_wait critical section entered! pid=%d\n",
  763             (int)td->td_proc->p_pid));
  764 #ifdef MAC
  765         error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
  766         if (error) {
  767                 DP(("kern_sem_wait mac failed\n"));
  768                 goto err;
  769         }
  770 #endif
  771         DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
  772         vfs_timestamp(&ks->ks_atime);
  773         while (ks->ks_value == 0) {
  774                 ks->ks_waiters++;
  775                 if (tryflag != 0)
  776                         error = EAGAIN;
  777                 else if (abstime == NULL)
  778                         error = cv_wait_sig(&ks->ks_cv, &sem_lock);
  779                 else {
  780                         for (;;) {
  781                                 ts1 = *abstime;
  782                                 getnanotime(&ts2);
  783                                 timespecsub(&ts1, &ts2);
  784                                 TIMESPEC_TO_TIMEVAL(&tv, &ts1);
  785                                 if (tv.tv_sec < 0) {
  786                                         error = ETIMEDOUT;
  787                                         break;
  788                                 }
  789                                 error = cv_timedwait_sig(&ks->ks_cv,
  790                                     &sem_lock, tvtohz(&tv));
  791                                 if (error != EWOULDBLOCK)
  792                                         break;
  793                         }
  794                 }
  795                 ks->ks_waiters--;
  796                 if (error)
  797                         goto err;
  798         }
  799         ks->ks_value--;
  800         DP(("kern_sem_wait value post-decrement = %d\n", ks->ks_value));
  801         error = 0;
  802 err:
  803         mtx_unlock(&sem_lock);
  804         fdrop(fp, td);
  805         DP(("<<< kern_sem_wait leaving, pid=%d, error = %d\n",
  806             (int)td->td_proc->p_pid, error));
  807         return (error);
  808 }
  809 
  810 #ifndef _SYS_SYSPROTO_H_
  811 struct ksem_getvalue_args {
  812         semid_t         id;
  813         int             *val;
  814 };
  815 #endif
  816 int
  817 ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
  818 {
  819         struct file *fp;
  820         struct ksem *ks;
  821         int error, val;
  822 
  823         error = ksem_get(td, uap->id, &fp);
  824         if (error)
  825                 return (error);
  826         ks = fp->f_data;
  827 
  828         mtx_lock(&sem_lock);
  829 #ifdef MAC
  830         error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
  831         if (error) {
  832                 mtx_unlock(&sem_lock);
  833                 fdrop(fp, td);
  834                 return (error);
  835         }
  836 #endif
  837         val = ks->ks_value;
  838         vfs_timestamp(&ks->ks_atime);
  839         mtx_unlock(&sem_lock);
  840         fdrop(fp, td);
  841         error = copyout(&val, uap->val, sizeof(val));
  842         return (error);
  843 }
  844 
  845 #ifndef _SYS_SYSPROTO_H_
  846 struct ksem_destroy_args {
  847         semid_t         id;
  848 };
  849 #endif
  850 int
  851 ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
  852 {
  853         struct file *fp;
  854         struct ksem *ks;
  855         int error;
  856 
  857         error = ksem_get(td, uap->id, &fp);
  858         if (error)
  859                 return (error);
  860         ks = fp->f_data;
  861         if (!(ks->ks_flags & KS_ANONYMOUS)) {
  862                 fdrop(fp, td);
  863                 return (EINVAL);
  864         }
  865         mtx_lock(&sem_lock);
  866         if (ks->ks_waiters != 0) {
  867                 mtx_unlock(&sem_lock);
  868                 error = EBUSY;
  869                 goto err;
  870         }
  871         ks->ks_flags |= KS_DEAD;
  872         mtx_unlock(&sem_lock);
  873 
  874         error = kern_close(td, uap->id);
  875 err:
  876         fdrop(fp, td);
  877         return (error);
  878 }
  879 
  880 static struct syscall_helper_data ksem_syscalls[] = {
  881         SYSCALL_INIT_HELPER(ksem_init),
  882         SYSCALL_INIT_HELPER(ksem_open),
  883         SYSCALL_INIT_HELPER(ksem_unlink),
  884         SYSCALL_INIT_HELPER(ksem_close),
  885         SYSCALL_INIT_HELPER(ksem_post),
  886         SYSCALL_INIT_HELPER(ksem_wait),
  887         SYSCALL_INIT_HELPER(ksem_timedwait),
  888         SYSCALL_INIT_HELPER(ksem_trywait),
  889         SYSCALL_INIT_HELPER(ksem_getvalue),
  890         SYSCALL_INIT_HELPER(ksem_destroy),
  891         SYSCALL_INIT_LAST
  892 };
  893 
  894 #ifdef COMPAT_FREEBSD32
  895 #include <compat/freebsd32/freebsd32.h>
  896 #include <compat/freebsd32/freebsd32_proto.h>
  897 #include <compat/freebsd32/freebsd32_signal.h>
  898 #include <compat/freebsd32/freebsd32_syscall.h>
  899 #include <compat/freebsd32/freebsd32_util.h>
  900 
  901 int
  902 freebsd32_ksem_init(struct thread *td, struct freebsd32_ksem_init_args *uap)
  903 {
  904 
  905         return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
  906             0, 1));
  907 }
  908 
  909 int
  910 freebsd32_ksem_open(struct thread *td, struct freebsd32_ksem_open_args *uap)
  911 {
  912 
  913         if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
  914                 return (EINVAL);
  915         return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
  916             uap->oflag, 1));
  917 }
  918 
  919 int
  920 freebsd32_ksem_timedwait(struct thread *td,
  921     struct freebsd32_ksem_timedwait_args *uap)
  922 {
  923         struct timespec32 abstime32;
  924         struct timespec *ts, abstime;
  925         int error;
  926 
  927         /*
  928          * We allow a null timespec (wait forever).
  929          */
  930         if (uap->abstime == NULL)
  931                 ts = NULL;
  932         else {
  933                 error = copyin(uap->abstime, &abstime32, sizeof(abstime32));
  934                 if (error != 0)
  935                         return (error);
  936                 CP(abstime32, abstime, tv_sec);
  937                 CP(abstime32, abstime, tv_nsec);
  938                 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
  939                         return (EINVAL);
  940                 ts = &abstime;
  941         }
  942         return (kern_sem_wait(td, uap->id, 0, ts));
  943 }
  944 
  945 static struct syscall_helper_data ksem32_syscalls[] = {
  946         SYSCALL32_INIT_HELPER(freebsd32_ksem_init),
  947         SYSCALL32_INIT_HELPER(freebsd32_ksem_open),
  948         SYSCALL32_INIT_HELPER(ksem_unlink),
  949         SYSCALL32_INIT_HELPER(ksem_close),
  950         SYSCALL32_INIT_HELPER(ksem_post),
  951         SYSCALL32_INIT_HELPER(ksem_wait),
  952         SYSCALL32_INIT_HELPER(freebsd32_ksem_timedwait),
  953         SYSCALL32_INIT_HELPER(ksem_trywait),
  954         SYSCALL32_INIT_HELPER(ksem_getvalue),
  955         SYSCALL32_INIT_HELPER(ksem_destroy),
  956         SYSCALL_INIT_LAST
  957 };
  958 #endif
  959 
  960 static int
  961 ksem_module_init(void)
  962 {
  963         int error;
  964 
  965         mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
  966         mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
  967         sx_init(&ksem_dict_lock, "ksem dictionary");
  968         ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
  969         p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 200112L);
  970         p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
  971         p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
  972         ksem_info = ksem_info_impl;
  973 
  974         error = syscall_helper_register(ksem_syscalls);
  975         if (error)
  976                 return (error);
  977 #ifdef COMPAT_FREEBSD32
  978         error = syscall32_helper_register(ksem32_syscalls);
  979         if (error)
  980                 return (error);
  981 #endif
  982         return (0);
  983 }
  984 
  985 static void
  986 ksem_module_destroy(void)
  987 {
  988 
  989 #ifdef COMPAT_FREEBSD32
  990         syscall32_helper_unregister(ksem32_syscalls);
  991 #endif
  992         syscall_helper_unregister(ksem_syscalls);
  993 
  994         ksem_info = NULL;
  995         p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 0);
  996         hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
  997         sx_destroy(&ksem_dict_lock);
  998         mtx_destroy(&ksem_count_lock);
  999         mtx_destroy(&sem_lock);
 1000         p31b_unsetcfg(CTL_P1003_1B_SEM_VALUE_MAX);
 1001         p31b_unsetcfg(CTL_P1003_1B_SEM_NSEMS_MAX);
 1002 }
 1003 
 1004 static int
 1005 sem_modload(struct module *module, int cmd, void *arg)
 1006 {
 1007         int error = 0;
 1008 
 1009         switch (cmd) {
 1010         case MOD_LOAD:
 1011                 error = ksem_module_init();
 1012                 if (error)
 1013                         ksem_module_destroy();
 1014                 break;
 1015 
 1016         case MOD_UNLOAD:
 1017                 mtx_lock(&ksem_count_lock);
 1018                 if (nsems != 0) {
 1019                         error = EOPNOTSUPP;
 1020                         mtx_unlock(&ksem_count_lock);
 1021                         break;
 1022                 }
 1023                 ksem_dead = 1;
 1024                 mtx_unlock(&ksem_count_lock);
 1025                 ksem_module_destroy();
 1026                 break;
 1027 
 1028         case MOD_SHUTDOWN:
 1029                 break;
 1030         default:
 1031                 error = EINVAL;
 1032                 break;
 1033         }
 1034         return (error);
 1035 }
 1036 
 1037 static moduledata_t sem_mod = {
 1038         "sem",
 1039         &sem_modload,
 1040         NULL
 1041 };
 1042 
 1043 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
 1044 MODULE_VERSION(sem, 1);

Cache object: b809cc229c05a2640fbdb4e174001b9f


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