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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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: releng/7.3/sys/kern/uipc_sem.c 189781 2009-03-14 01:12:35Z bms $");
   36 
   37 #include "opt_mac.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/refcount.h>
   56 #include <sys/semaphore.h>
   57 #include <sys/_semaphore.h>
   58 #include <sys/stat.h>
   59 #include <sys/syscall.h>
   60 #include <sys/syscallsubr.h>
   61 #include <sys/sysctl.h>
   62 #include <sys/sysent.h>
   63 #include <sys/sysproto.h>
   64 #include <sys/systm.h>
   65 #include <sys/sx.h>
   66 #include <sys/vnode.h>
   67 
   68 #include <security/mac/mac_framework.h>
   69 
   70 /*
   71  * TODO
   72  *
   73  * - Resource limits?
   74  * - Update fstat(1)
   75  * - Replace global sem_lock with mtx_pool locks?
   76  * - Add a MAC check_create() hook for creating new named semaphores.
   77  */
   78 
   79 #ifndef SEM_MAX
   80 #define SEM_MAX 30
   81 #endif
   82 
   83 #ifdef SEM_DEBUG
   84 #define DP(x)   printf x
   85 #else
   86 #define DP(x)
   87 #endif
   88 
   89 struct ksem_mapping {
   90         char            *km_path;
   91         Fnv32_t         km_fnv;
   92         struct ksem     *km_ksem;
   93         LIST_ENTRY(ksem_mapping) km_link;
   94 };
   95 
   96 static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
   97 static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
   98 static struct sx ksem_dict_lock;
   99 static struct mtx ksem_count_lock;
  100 static struct mtx sem_lock;
  101 static u_long ksem_hash;
  102 static int ksem_dead;
  103 
  104 #define KSEM_HASH(fnv)  (&ksem_dictionary[(fnv) & ksem_hash])
  105 
  106 static int nsems = 0;
  107 SYSCTL_DECL(_p1003_1b);
  108 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
  109     "Number of active kernel POSIX semaphores");
  110 
  111 static int      kern_sem_wait(struct thread *td, semid_t id, int tryflag,
  112                     struct timespec *abstime);
  113 static int      ksem_access(struct ksem *ks, struct ucred *ucred);
  114 static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
  115                     unsigned int value);
  116 static int      ksem_create(struct thread *td, const char *path,
  117                     semid_t *semidp, mode_t mode, unsigned int value,
  118                     int flags);
  119 static void     ksem_drop(struct ksem *ks);
  120 static int      ksem_get(struct thread *td, semid_t id, struct file **fpp);
  121 static struct ksem *ksem_hold(struct ksem *ks);
  122 static void     ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
  123 static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
  124 static void     ksem_module_destroy(void);
  125 static int      ksem_module_init(void);
  126 static int      ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
  127 static int      sem_modload(struct module *module, int cmd, void *arg);
  128 
  129 static fo_rdwr_t        ksem_read;
  130 static fo_rdwr_t        ksem_write;
  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_ioctl = ksem_ioctl,
  142         .fo_poll = ksem_poll,
  143         .fo_kqfilter = ksem_kqfilter,
  144         .fo_stat = ksem_stat,
  145         .fo_close = ksem_closef,
  146         .fo_flags = DFLAG_PASSABLE
  147 };
  148 
  149 FEATURE(posix_sem, "POSIX semaphores");
  150 
  151 static int
  152 ksem_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
  153     int flags, struct thread *td)
  154 {
  155 
  156         return (EOPNOTSUPP);
  157 }
  158 
  159 static int
  160 ksem_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
  161     int flags, struct thread *td)
  162 {
  163 
  164         return (EOPNOTSUPP);
  165 }
  166 
  167 static int
  168 ksem_ioctl(struct file *fp, u_long com, void *data,
  169     struct ucred *active_cred, struct thread *td)
  170 {
  171 
  172         return (EOPNOTSUPP);
  173 }
  174 
  175 static int
  176 ksem_poll(struct file *fp, int events, struct ucred *active_cred,
  177     struct thread *td)
  178 {
  179 
  180         return (EOPNOTSUPP);
  181 }
  182 
  183 static int
  184 ksem_kqfilter(struct file *fp, struct knote *kn)
  185 {
  186 
  187         return (EOPNOTSUPP);
  188 }
  189 
  190 static int
  191 ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
  192     struct thread *td)
  193 {
  194         struct ksem *ks;
  195 #ifdef MAC
  196         int error;
  197 #endif
  198 
  199         ks = fp->f_data;
  200 
  201 #ifdef MAC
  202         /*
  203          * XXX: This isn't quite right, but adding a new stat() hook
  204          * would break the ABI.  This seems to be the closest match of
  205          * the existing MAC hooks for POSIX semaphores in 7.
  206          */
  207         error = mac_check_posix_sem_getvalue(active_cred, ks);
  208         if (error)
  209                 return (error);
  210 #endif
  211         
  212         /*
  213          * Attempt to return sanish values for fstat() on a semaphore
  214          * file descriptor.
  215          */
  216         bzero(sb, sizeof(*sb));
  217         sb->st_mode = S_IFREG | ks->ks_mode;            /* XXX */
  218 
  219         sb->st_atimespec = ks->ks_atime;
  220         sb->st_ctimespec = ks->ks_ctime;
  221         sb->st_mtimespec = ks->ks_mtime;
  222         sb->st_birthtimespec = ks->ks_birthtime;        
  223         sb->st_uid = ks->ks_uid;
  224         sb->st_gid = ks->ks_gid;
  225 
  226         return (0);
  227 }
  228 
  229 static int
  230 ksem_closef(struct file *fp, struct thread *td)
  231 {
  232         struct ksem *ks;
  233 
  234         ks = fp->f_data;
  235         fp->f_data = NULL;
  236         ksem_drop(ks);
  237 
  238         return (0);
  239 }
  240 
  241 /*
  242  * ksem object management including creation and reference counting
  243  * routines.
  244  */
  245 static struct ksem *
  246 ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
  247 {
  248         struct ksem *ks;
  249 
  250         mtx_lock(&ksem_count_lock);
  251         if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
  252                 mtx_unlock(&ksem_count_lock);
  253                 return (NULL);
  254         }
  255         nsems++;
  256         mtx_unlock(&ksem_count_lock);
  257         ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
  258         ks->ks_uid = ucred->cr_uid;
  259         ks->ks_gid = ucred->cr_gid;
  260         ks->ks_mode = mode;
  261         ks->ks_value = value;
  262         cv_init(&ks->ks_cv, "ksem");
  263         vfs_timestamp(&ks->ks_birthtime);
  264         ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
  265         refcount_init(&ks->ks_ref, 1);
  266 #ifdef MAC
  267         mac_init_posix_sem(ks);
  268         mac_create_posix_sem(ucred, ks);
  269 #endif
  270 
  271         return (ks);
  272 }
  273 
  274 static struct ksem *
  275 ksem_hold(struct ksem *ks)
  276 {
  277 
  278         refcount_acquire(&ks->ks_ref);
  279         return (ks);
  280 }
  281 
  282 static void
  283 ksem_drop(struct ksem *ks)
  284 {
  285 
  286         if (refcount_release(&ks->ks_ref)) {
  287 #ifdef MAC
  288                 mac_destroy_posix_sem(ks);
  289 #endif
  290                 cv_destroy(&ks->ks_cv);
  291                 free(ks, M_KSEM);
  292                 mtx_lock(&ksem_count_lock);
  293                 nsems--;
  294                 mtx_unlock(&ksem_count_lock);
  295         }
  296 }
  297 
  298 /*
  299  * Determine if the credentials have sufficient permissions for read
  300  * and write access.
  301  */
  302 static int
  303 ksem_access(struct ksem *ks, struct ucred *ucred)
  304 {
  305         int error;
  306 
  307         error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
  308             VREAD | VWRITE, ucred, NULL);
  309         if (error)
  310                 error = priv_check_cred(ucred, PRIV_SEM_WRITE, 0);
  311         return (error);
  312 }
  313 
  314 /*
  315  * Dictionary management.  We maintain an in-kernel dictionary to map
  316  * paths to semaphore objects.  We use the FNV hash on the path to
  317  * store the mappings in a hash table.
  318  */
  319 static struct ksem *
  320 ksem_lookup(char *path, Fnv32_t fnv)
  321 {
  322         struct ksem_mapping *map;
  323 
  324         LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
  325                 if (map->km_fnv != fnv)
  326                         continue;
  327                 if (strcmp(map->km_path, path) == 0)
  328                         return (map->km_ksem);
  329         }
  330 
  331         return (NULL);
  332 }
  333 
  334 static void
  335 ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
  336 {
  337         struct ksem_mapping *map;
  338 
  339         map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
  340         map->km_path = path;
  341         map->km_fnv = fnv;
  342         map->km_ksem = ksem_hold(ks);
  343         LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
  344 }
  345 
  346 static int
  347 ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
  348 {
  349         struct ksem_mapping *map;
  350         int error;
  351 
  352         LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
  353                 if (map->km_fnv != fnv)
  354                         continue;
  355                 if (strcmp(map->km_path, path) == 0) {
  356 #ifdef MAC
  357                         error = mac_check_posix_sem_unlink(ucred, map->km_ksem);
  358                         if (error)
  359                                 return (error);
  360 #endif
  361                         error = ksem_access(map->km_ksem, ucred);
  362                         if (error)
  363                                 return (error);
  364                         LIST_REMOVE(map, km_link);
  365                         ksem_drop(map->km_ksem);
  366                         free(map->km_path, M_KSEM);
  367                         free(map, M_KSEM);
  368                         return (0);
  369                 }
  370         }
  371 
  372         return (ENOENT);
  373 }
  374 
  375 /* Other helper routines. */
  376 static int
  377 ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
  378     unsigned int value, int flags)
  379 {
  380         struct filedesc *fdp;
  381         struct ksem *ks;
  382         struct file *fp;
  383         char *path;
  384         semid_t semid;
  385         Fnv32_t fnv;
  386         int error, fd;
  387 
  388         if (value > SEM_VALUE_MAX)
  389                 return (EINVAL);
  390 
  391         fdp = td->td_proc->p_fd;
  392         mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
  393         error = falloc(td, &fp, &fd);
  394         if (error) {
  395                 if (name == NULL)
  396                         error = ENOSPC;
  397                 return (error);
  398         }
  399 
  400         /*
  401          * Go ahead and copyout the file descriptor now.  This is a bit
  402          * premature, but it is a lot easier to handle errors as opposed
  403          * to later when we've possibly created a new semaphore, etc.
  404          */
  405         semid = fd;
  406         error = copyout(&semid, semidp, sizeof(semid));
  407         if (error) {
  408                 fdclose(fdp, fp, fd, td);
  409                 fdrop(fp, td);
  410                 return (error);
  411         }
  412 
  413         if (name == NULL) {
  414                 /* Create an anonymous semaphore. */
  415                 ks = ksem_alloc(td->td_ucred, mode, value);
  416                 if (ks == NULL)
  417                         error = ENOSPC;
  418                 else
  419                         ks->ks_flags |= KS_ANONYMOUS;
  420         } else {
  421                 path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
  422                 error = copyinstr(name, path, MAXPATHLEN, NULL);
  423 
  424                 /* Require paths to start with a '/' character. */
  425                 if (error == 0 && path[0] != '/')
  426                         error = EINVAL;
  427                 if (error) {
  428                         fdclose(fdp, fp, fd, td);
  429                         fdrop(fp, td);
  430                         free(path, M_KSEM);
  431                         return (error);
  432                 }
  433 
  434                 fnv = fnv_32_str(path, FNV1_32_INIT);
  435                 sx_xlock(&ksem_dict_lock);
  436                 ks = ksem_lookup(path, fnv);
  437                 if (ks == NULL) {
  438                         /* Object does not exist, create it if requested. */
  439                         if (flags & O_CREAT) {
  440                                 ks = ksem_alloc(td->td_ucred, mode, value);
  441                                 if (ks == NULL)
  442                                         error = ENFILE;
  443                                 else {
  444                                         ksem_insert(path, fnv, ks);
  445                                         path = NULL;
  446                                 }
  447                         } else
  448                                 error = ENOENT;
  449                 } else {
  450                         /*
  451                          * Object already exists, obtain a new
  452                          * reference if requested and permitted.
  453                          */
  454                         if ((flags & (O_CREAT | O_EXCL)) ==
  455                             (O_CREAT | O_EXCL))
  456                                 error = EEXIST;
  457                         else {
  458 #ifdef MAC
  459                                 error = mac_check_posix_sem_open(td->td_ucred,
  460                                     ks);
  461                                 if (error == 0)
  462 #endif
  463                                 error = ksem_access(ks, td->td_ucred);
  464                         }
  465                         if (error == 0)
  466                                 ksem_hold(ks);
  467 #ifdef INVARIANTS
  468                         else
  469                                 ks = NULL;
  470 #endif
  471                 }
  472                 sx_xunlock(&ksem_dict_lock);
  473                 if (path)
  474                         free(path, M_KSEM);
  475         }
  476 
  477         if (error) {
  478                 KASSERT(ks == NULL, ("ksem_create error with a ksem"));
  479                 fdclose(fdp, fp, fd, td);
  480                 fdrop(fp, td);
  481                 return (error);
  482         }
  483         KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
  484 
  485         fp->f_data = ks;
  486         fp->f_flag = FREAD | FWRITE;
  487         fp->f_type = DTYPE_SEM;
  488         fp->f_ops = &ksem_ops;
  489 
  490         FILEDESC_XLOCK(fdp);
  491         if (fdp->fd_ofiles[fd] == fp)
  492                 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
  493         FILEDESC_XUNLOCK(fdp);
  494         fdrop(fp, td);
  495 
  496         return (0);
  497 }
  498 
  499 static int
  500 ksem_get(struct thread *td, semid_t id, struct file **fpp)
  501 {
  502         struct ksem *ks;
  503         struct file *fp;
  504         int error;
  505 
  506         error = fget(td, id, &fp);
  507         if (error)
  508                 return (EINVAL);
  509         if (fp->f_type != DTYPE_SEM) {
  510                 fdrop(fp, td);
  511                 return (EINVAL);
  512         }
  513         ks = fp->f_data;
  514         if (ks->ks_flags & KS_DEAD) {
  515                 fdrop(fp, td);
  516                 return (EINVAL);
  517         }
  518         *fpp = fp;
  519         return (0);
  520 }
  521 
  522 /* System calls. */
  523 #ifndef _SYS_SYSPROTO_H_
  524 struct ksem_init_args {
  525         unsigned int    value;
  526         semid_t         *idp;
  527 };
  528 #endif
  529 int
  530 ksem_init(struct thread *td, struct ksem_init_args *uap)
  531 {
  532 
  533         return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
  534             0));
  535 }
  536 
  537 #ifndef _SYS_SYSPROTO_H_
  538 struct ksem_open_args {
  539         char            *name;
  540         int             oflag;
  541         mode_t          mode;
  542         unsigned int    value;
  543         semid_t         *idp;   
  544 };
  545 #endif
  546 int
  547 ksem_open(struct thread *td, struct ksem_open_args *uap)
  548 {
  549 
  550         if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
  551                 return (EINVAL);
  552         return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
  553             uap->oflag));
  554 }
  555 
  556 #ifndef _SYS_SYSPROTO_H_
  557 struct ksem_unlink_args {
  558         char            *name;
  559 };
  560 #endif
  561 int
  562 ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
  563 {
  564         char *path;
  565         Fnv32_t fnv;
  566         int error;
  567 
  568         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  569         error = copyinstr(uap->name, path, MAXPATHLEN, NULL);
  570         if (error) {
  571                 free(path, M_TEMP);
  572                 return (error);
  573         }
  574 
  575         fnv = fnv_32_str(path, FNV1_32_INIT);
  576         sx_xlock(&ksem_dict_lock);
  577         error = ksem_remove(path, fnv, td->td_ucred);
  578         sx_xunlock(&ksem_dict_lock);
  579         free(path, M_TEMP);
  580 
  581         return (error);
  582 }
  583 
  584 #ifndef _SYS_SYSPROTO_H_
  585 struct ksem_close_args {
  586         semid_t         id;
  587 };
  588 #endif
  589 int
  590 ksem_close(struct thread *td, struct ksem_close_args *uap)
  591 {
  592         struct ksem *ks;
  593         struct file *fp;
  594         int error;
  595 
  596         error = ksem_get(td, uap->id, &fp);
  597         if (error)
  598                 return (error);
  599         ks = fp->f_data;
  600         if (ks->ks_flags & KS_ANONYMOUS) {
  601                 fdrop(fp, td);
  602                 return (EINVAL);
  603         }
  604         error = kern_close(td, uap->id);
  605         fdrop(fp, td);
  606         return (error);
  607 }
  608 
  609 #ifndef _SYS_SYSPROTO_H_
  610 struct ksem_post_args {
  611         semid_t id;
  612 };
  613 #endif
  614 int
  615 ksem_post(struct thread *td, struct ksem_post_args *uap)
  616 {
  617         struct file *fp;
  618         struct ksem *ks;
  619         int error;
  620 
  621         error = ksem_get(td, uap->id, &fp);
  622         if (error)
  623                 return (error);
  624         ks = fp->f_data;
  625 
  626         mtx_lock(&sem_lock);
  627 #ifdef MAC
  628         error = mac_check_posix_sem_post(td->td_ucred, ks);
  629         if (error)
  630                 goto err;
  631 #endif
  632         if (ks->ks_value == SEM_VALUE_MAX) {
  633                 error = EOVERFLOW;
  634                 goto err;
  635         }
  636         ++ks->ks_value;
  637         if (ks->ks_waiters > 0)
  638                 cv_signal(&ks->ks_cv);
  639         error = 0;
  640         vfs_timestamp(&ks->ks_ctime);
  641 err:
  642         mtx_unlock(&sem_lock);
  643         fdrop(fp, td);
  644         return (error);
  645 }
  646 
  647 #ifndef _SYS_SYSPROTO_H_
  648 struct ksem_wait_args {
  649         semid_t         id;
  650 };
  651 #endif
  652 int
  653 ksem_wait(struct thread *td, struct ksem_wait_args *uap)
  654 {
  655 
  656         return (kern_sem_wait(td, uap->id, 0, NULL));
  657 }
  658 
  659 #ifndef _SYS_SYSPROTO_H_
  660 struct ksem_timedwait_args {
  661         semid_t         id;
  662         const struct timespec *abstime;
  663 };
  664 #endif
  665 int
  666 ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
  667 {
  668         struct timespec abstime;
  669         struct timespec *ts;
  670         int error;
  671 
  672         /*
  673          * We allow a null timespec (wait forever).
  674          */
  675         if (uap->abstime == NULL)
  676                 ts = NULL;
  677         else {
  678                 error = copyin(uap->abstime, &abstime, sizeof(abstime));
  679                 if (error != 0)
  680                         return (error);
  681                 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
  682                         return (EINVAL);
  683                 ts = &abstime;
  684         }
  685         return (kern_sem_wait(td, uap->id, 0, ts));
  686 }
  687 
  688 #ifndef _SYS_SYSPROTO_H_
  689 struct ksem_trywait_args {
  690         semid_t         id;
  691 };
  692 #endif
  693 int
  694 ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
  695 {
  696 
  697         return (kern_sem_wait(td, uap->id, 1, NULL));
  698 }
  699 
  700 static int
  701 kern_sem_wait(struct thread *td, semid_t id, int tryflag,
  702     struct timespec *abstime)
  703 {
  704         struct timespec ts1, ts2;
  705         struct timeval tv;
  706         struct file *fp;
  707         struct ksem *ks;
  708         int error;
  709 
  710         DP((">>> kern_sem_wait entered!\n"));
  711         error = ksem_get(td, id, &fp);
  712         if (error)
  713                 return (error);
  714         ks = fp->f_data;
  715         mtx_lock(&sem_lock);
  716 #ifdef MAC
  717         error = mac_check_posix_sem_wait(td->td_ucred, ks);
  718         if (error) {
  719                 DP(("kern_sem_wait mac failed\n"));
  720                 goto err;
  721         }
  722 #endif
  723         DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
  724         vfs_timestamp(&ks->ks_atime);
  725         while (ks->ks_value == 0) {
  726                 ks->ks_waiters++;
  727                 if (tryflag != 0)
  728                         error = EAGAIN;
  729                 else if (abstime == NULL)
  730                         error = cv_wait_sig(&ks->ks_cv, &sem_lock);
  731                 else {
  732                         for (;;) {
  733                                 ts1 = *abstime;
  734                                 getnanotime(&ts2);
  735                                 timespecsub(&ts1, &ts2);
  736                                 TIMESPEC_TO_TIMEVAL(&tv, &ts1);
  737                                 if (tv.tv_sec < 0) {
  738                                         error = ETIMEDOUT;
  739                                         break;
  740                                 }
  741                                 error = cv_timedwait_sig(&ks->ks_cv,
  742                                     &sem_lock, tvtohz(&tv));
  743                                 if (error != EWOULDBLOCK)
  744                                         break;
  745                         }
  746                 }
  747                 ks->ks_waiters--;
  748                 if (error)
  749                         goto err;
  750         }
  751         ks->ks_value--;
  752         error = 0;
  753 err:
  754         mtx_unlock(&sem_lock);
  755         fdrop(fp, td);
  756         DP(("<<< kern_sem_wait leaving, error = %d\n", error));
  757         return (error);
  758 }
  759 
  760 #ifndef _SYS_SYSPROTO_H_
  761 struct ksem_getvalue_args {
  762         semid_t         id;
  763         int             *val;
  764 };
  765 #endif
  766 int
  767 ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
  768 {
  769         struct file *fp;
  770         struct ksem *ks;
  771         int error, val;
  772 
  773         error = ksem_get(td, uap->id, &fp);
  774         if (error)
  775                 return (error);
  776         ks = fp->f_data;
  777 
  778         mtx_lock(&sem_lock);
  779 #ifdef MAC
  780         error = mac_check_posix_sem_getvalue(td->td_ucred, ks);
  781         if (error) {
  782                 mtx_unlock(&sem_lock);
  783                 fdrop(fp, td);
  784                 return (error);
  785         }
  786 #endif
  787         val = ks->ks_value;
  788         vfs_timestamp(&ks->ks_atime);
  789         mtx_unlock(&sem_lock);
  790         fdrop(fp, td);
  791         error = copyout(&val, uap->val, sizeof(val));
  792         return (error);
  793 }
  794 
  795 #ifndef _SYS_SYSPROTO_H_
  796 struct ksem_destroy_args {
  797         semid_t         id;
  798 };
  799 #endif
  800 int
  801 ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
  802 {
  803         struct file *fp;
  804         struct ksem *ks;
  805         int error;
  806 
  807         error = ksem_get(td, uap->id, &fp);
  808         if (error)
  809                 return (error);
  810         ks = fp->f_data;
  811         if (!(ks->ks_flags & KS_ANONYMOUS)) {
  812                 fdrop(fp, td);
  813                 return (EINVAL);
  814         }
  815         mtx_lock(&sem_lock);
  816         if (ks->ks_waiters != 0) {
  817                 mtx_unlock(&sem_lock);
  818                 error = EBUSY;
  819                 goto err;
  820         }
  821         ks->ks_flags |= KS_DEAD;
  822         mtx_unlock(&sem_lock);
  823 
  824         error = kern_close(td, uap->id);
  825 err:
  826         fdrop(fp, td);
  827         return (error);
  828 }
  829 
  830 #define SYSCALL_DATA(syscallname)                               \
  831 static int syscallname##_syscall = SYS_##syscallname;           \
  832 static int syscallname##_registered;                            \
  833 static struct sysent syscallname##_old_sysent;                  \
  834 MAKE_SYSENT(syscallname);
  835 
  836 #define SYSCALL_REGISTER(syscallname) do {                              \
  837         error = syscall_register(& syscallname##_syscall,               \
  838             & syscallname##_sysent, & syscallname##_old_sysent);        \
  839         if (error)                                                      \
  840                 return (error);                                         \
  841         syscallname##_registered = 1;                                   \
  842 } while(0)
  843 
  844 #define SYSCALL_DEREGISTER(syscallname) do {                            \
  845         if (syscallname##_registered) {                                 \
  846                 syscallname##_registered = 0;                           \
  847                 syscall_deregister(& syscallname##_syscall,             \
  848                     & syscallname##_old_sysent);                        \
  849         }                                                               \
  850 } while(0)
  851 
  852 SYSCALL_DATA(ksem_init);
  853 SYSCALL_DATA(ksem_open);
  854 SYSCALL_DATA(ksem_unlink);
  855 SYSCALL_DATA(ksem_close);
  856 SYSCALL_DATA(ksem_post);
  857 SYSCALL_DATA(ksem_wait);
  858 SYSCALL_DATA(ksem_timedwait);
  859 SYSCALL_DATA(ksem_trywait);
  860 SYSCALL_DATA(ksem_getvalue);
  861 SYSCALL_DATA(ksem_destroy);
  862 
  863 static int
  864 ksem_module_init(void)
  865 {
  866         int error;
  867 
  868         mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
  869         mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
  870         sx_init(&ksem_dict_lock, "ksem dictionary");
  871         ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
  872         p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
  873         p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
  874 
  875         SYSCALL_REGISTER(ksem_init);
  876         SYSCALL_REGISTER(ksem_open);
  877         SYSCALL_REGISTER(ksem_unlink);
  878         SYSCALL_REGISTER(ksem_close);
  879         SYSCALL_REGISTER(ksem_post);
  880         SYSCALL_REGISTER(ksem_wait);
  881         SYSCALL_REGISTER(ksem_timedwait);
  882         SYSCALL_REGISTER(ksem_trywait);
  883         SYSCALL_REGISTER(ksem_getvalue);
  884         SYSCALL_REGISTER(ksem_destroy);
  885         return (0);
  886 }
  887 
  888 static void
  889 ksem_module_destroy(void)
  890 {
  891 
  892         SYSCALL_DEREGISTER(ksem_init);
  893         SYSCALL_DEREGISTER(ksem_open);
  894         SYSCALL_DEREGISTER(ksem_unlink);
  895         SYSCALL_DEREGISTER(ksem_close);
  896         SYSCALL_DEREGISTER(ksem_post);
  897         SYSCALL_DEREGISTER(ksem_wait);
  898         SYSCALL_DEREGISTER(ksem_timedwait);
  899         SYSCALL_DEREGISTER(ksem_trywait);
  900         SYSCALL_DEREGISTER(ksem_getvalue);
  901         SYSCALL_DEREGISTER(ksem_destroy);
  902 
  903         hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
  904         sx_destroy(&ksem_dict_lock);
  905         mtx_destroy(&ksem_count_lock);
  906         mtx_destroy(&sem_lock);
  907 }
  908 
  909 static int
  910 sem_modload(struct module *module, int cmd, void *arg)
  911 {
  912         int error = 0;
  913 
  914         switch (cmd) {
  915         case MOD_LOAD:
  916                 error = ksem_module_init();
  917                 if (error)
  918                         ksem_module_destroy();
  919                 break;
  920 
  921         case MOD_UNLOAD:
  922                 mtx_lock(&ksem_count_lock);
  923                 if (nsems != 0) {
  924                         error = EOPNOTSUPP;
  925                         mtx_unlock(&ksem_count_lock);
  926                         break;
  927                 }
  928                 ksem_dead = 1;
  929                 mtx_unlock(&ksem_count_lock);
  930                 ksem_module_destroy();
  931                 break;
  932 
  933         case MOD_SHUTDOWN:
  934                 break;
  935         default:
  936                 error = EINVAL;
  937                 break;
  938         }
  939         return (error);
  940 }
  941 
  942 static moduledata_t sem_mod = {
  943         "sem",
  944         &sem_modload,
  945         NULL
  946 };
  947 
  948 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
  949 MODULE_VERSION(sem, 1);

Cache object: 7a3a3825dd1d46fe200984e20ce73812


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