19454b2d8SWarner Losh /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
4efaa6588SAlfred Perlstein * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
552648411SRobert Watson * Copyright (c) 2003-2005 SPARTA, Inc.
615bcf785SRobert Watson * Copyright (c) 2005, 2016-2017 Robert N. M. Watson
7efaa6588SAlfred Perlstein * All rights reserved.
8efaa6588SAlfred Perlstein *
952648411SRobert Watson * This software was developed for the FreeBSD Project in part by Network
1052648411SRobert Watson * Associates Laboratories, the Security Research Division of Network
1152648411SRobert Watson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
1252648411SRobert Watson * as part of the DARPA CHATS research program.
1352648411SRobert Watson *
1415bcf785SRobert Watson * Portions of this software were developed by BAE Systems, the University of
1515bcf785SRobert Watson * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
1615bcf785SRobert Watson * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
1715bcf785SRobert Watson * Computing (TC) research program.
1815bcf785SRobert Watson *
19efaa6588SAlfred Perlstein * Redistribution and use in source and binary forms, with or without
20efaa6588SAlfred Perlstein * modification, are permitted provided that the following conditions
21efaa6588SAlfred Perlstein * are met:
22efaa6588SAlfred Perlstein * 1. Redistributions of source code must retain the above copyright
23efaa6588SAlfred Perlstein * notice, this list of conditions and the following disclaimer.
24efaa6588SAlfred Perlstein * 2. Redistributions in binary form must reproduce the above copyright
25efaa6588SAlfred Perlstein * notice, this list of conditions and the following disclaimer in the
26efaa6588SAlfred Perlstein * documentation and/or other materials provided with the distribution.
27efaa6588SAlfred Perlstein *
28efaa6588SAlfred Perlstein * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29efaa6588SAlfred Perlstein * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30efaa6588SAlfred Perlstein * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31efaa6588SAlfred Perlstein * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32efaa6588SAlfred Perlstein * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33efaa6588SAlfred Perlstein * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34efaa6588SAlfred Perlstein * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35efaa6588SAlfred Perlstein * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36efaa6588SAlfred Perlstein * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37efaa6588SAlfred Perlstein * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38efaa6588SAlfred Perlstein * SUCH DAMAGE.
39efaa6588SAlfred Perlstein */
40efaa6588SAlfred Perlstein
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42efaa6588SAlfred Perlstein #include "opt_posix.h"
43efaa6588SAlfred Perlstein
44efaa6588SAlfred Perlstein #include <sys/param.h>
454a144410SRobert Watson #include <sys/capsicum.h>
466bc1e9cdSJohn Baldwin #include <sys/condvar.h>
476bc1e9cdSJohn Baldwin #include <sys/fcntl.h>
486bc1e9cdSJohn Baldwin #include <sys/file.h>
496bc1e9cdSJohn Baldwin #include <sys/filedesc.h>
506bc1e9cdSJohn Baldwin #include <sys/fnv_hash.h>
51cc7b259aSJamie Gritton #include <sys/jail.h>
52efaa6588SAlfred Perlstein #include <sys/kernel.h>
536aeb05d7STom Rhodes #include <sys/ksem.h>
546bc1e9cdSJohn Baldwin #include <sys/lock.h>
556bc1e9cdSJohn Baldwin #include <sys/malloc.h>
566bc1e9cdSJohn Baldwin #include <sys/module.h>
576bc1e9cdSJohn Baldwin #include <sys/mutex.h>
58acd3428bSRobert Watson #include <sys/priv.h>
59efaa6588SAlfred Perlstein #include <sys/proc.h>
606aeb05d7STom Rhodes #include <sys/posix4.h>
616aeb05d7STom Rhodes #include <sys/_semaphore.h>
626bc1e9cdSJohn Baldwin #include <sys/stat.h>
636bc1e9cdSJohn Baldwin #include <sys/syscall.h>
646bc1e9cdSJohn Baldwin #include <sys/syscallsubr.h>
656bc1e9cdSJohn Baldwin #include <sys/sysctl.h>
666bc1e9cdSJohn Baldwin #include <sys/sysent.h>
676bc1e9cdSJohn Baldwin #include <sys/sysproto.h>
686bc1e9cdSJohn Baldwin #include <sys/systm.h>
696bc1e9cdSJohn Baldwin #include <sys/sx.h>
709696feebSJohn Baldwin #include <sys/user.h>
716bc1e9cdSJohn Baldwin #include <sys/vnode.h>
72efaa6588SAlfred Perlstein
7315bcf785SRobert Watson #include <security/audit/audit.h>
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson
769a253c10SAlexander Leidinger FEATURE(p1003_1b_semaphores, "POSIX P1003.1B semaphores support");
776bc1e9cdSJohn Baldwin /*
786bc1e9cdSJohn Baldwin * TODO
796bc1e9cdSJohn Baldwin *
806bc1e9cdSJohn Baldwin * - Resource limits?
816bc1e9cdSJohn Baldwin * - Replace global sem_lock with mtx_pool locks?
826bc1e9cdSJohn Baldwin * - Add a MAC check_create() hook for creating new named semaphores.
836bc1e9cdSJohn Baldwin */
84efaa6588SAlfred Perlstein
85efaa6588SAlfred Perlstein #ifndef SEM_MAX
86efaa6588SAlfred Perlstein #define SEM_MAX 30
87efaa6588SAlfred Perlstein #endif
88efaa6588SAlfred Perlstein
89c814aa3fSAlfred Perlstein #ifdef SEM_DEBUG
90c814aa3fSAlfred Perlstein #define DP(x) printf x
91c814aa3fSAlfred Perlstein #else
92c814aa3fSAlfred Perlstein #define DP(x)
93c814aa3fSAlfred Perlstein #endif
94c814aa3fSAlfred Perlstein
956bc1e9cdSJohn Baldwin struct ksem_mapping {
966bc1e9cdSJohn Baldwin char *km_path;
976bc1e9cdSJohn Baldwin Fnv32_t km_fnv;
986bc1e9cdSJohn Baldwin struct ksem *km_ksem;
996bc1e9cdSJohn Baldwin LIST_ENTRY(ksem_mapping) km_link;
1006bc1e9cdSJohn Baldwin };
1016bc1e9cdSJohn Baldwin
1026bc1e9cdSJohn Baldwin static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
1036bc1e9cdSJohn Baldwin static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
1046bc1e9cdSJohn Baldwin static struct sx ksem_dict_lock;
1056bc1e9cdSJohn Baldwin static struct mtx ksem_count_lock;
1066bc1e9cdSJohn Baldwin static struct mtx sem_lock;
1076bc1e9cdSJohn Baldwin static u_long ksem_hash;
1086bc1e9cdSJohn Baldwin static int ksem_dead;
1096bc1e9cdSJohn Baldwin
1106bc1e9cdSJohn Baldwin #define KSEM_HASH(fnv) (&ksem_dictionary[(fnv) & ksem_hash])
1116bc1e9cdSJohn Baldwin
1126bc1e9cdSJohn Baldwin static int nsems = 0;
1136bc1e9cdSJohn Baldwin SYSCTL_DECL(_p1003_1b);
1146bc1e9cdSJohn Baldwin SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
1156bc1e9cdSJohn Baldwin "Number of active kernel POSIX semaphores");
1166bc1e9cdSJohn Baldwin
1176bc1e9cdSJohn Baldwin static int kern_sem_wait(struct thread *td, semid_t id, int tryflag,
1186bc1e9cdSJohn Baldwin struct timespec *abstime);
1196bc1e9cdSJohn Baldwin static int ksem_access(struct ksem *ks, struct ucred *ucred);
1206bc1e9cdSJohn Baldwin static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
1216bc1e9cdSJohn Baldwin unsigned int value);
1226bc1e9cdSJohn Baldwin static int ksem_create(struct thread *td, const char *path,
1236bc1e9cdSJohn Baldwin semid_t *semidp, mode_t mode, unsigned int value,
1240e5d5bc2SKonstantin Belousov int flags, int compat32);
1256bc1e9cdSJohn Baldwin static void ksem_drop(struct ksem *ks);
1267008be5bSPawel Jakub Dawidek static int ksem_get(struct thread *td, semid_t id, cap_rights_t *rightsp,
127a9d2f8d8SRobert Watson struct file **fpp);
1286bc1e9cdSJohn Baldwin static struct ksem *ksem_hold(struct ksem *ks);
1296bc1e9cdSJohn Baldwin static void ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
1306bc1e9cdSJohn Baldwin static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
1316bc1e9cdSJohn Baldwin static void ksem_module_destroy(void);
1326bc1e9cdSJohn Baldwin static int ksem_module_init(void);
1336bc1e9cdSJohn Baldwin static int ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
1346bc1e9cdSJohn Baldwin static int sem_modload(struct module *module, int cmd, void *arg);
1356bc1e9cdSJohn Baldwin
1366bc1e9cdSJohn Baldwin static fo_stat_t ksem_stat;
1376bc1e9cdSJohn Baldwin static fo_close_t ksem_closef;
1389c00bb91SKonstantin Belousov static fo_chmod_t ksem_chmod;
1399c00bb91SKonstantin Belousov static fo_chown_t ksem_chown;
1409696feebSJohn Baldwin static fo_fill_kinfo_t ksem_fill_kinfo;
1416bc1e9cdSJohn Baldwin
1426bc1e9cdSJohn Baldwin /* File descriptor operations. */
143*ef9ffb85SMark Johnston static const struct fileops ksem_ops = {
1442d69d0dcSJohn Baldwin .fo_read = invfo_rdwr,
1452d69d0dcSJohn Baldwin .fo_write = invfo_rdwr,
1462d69d0dcSJohn Baldwin .fo_truncate = invfo_truncate,
1472d69d0dcSJohn Baldwin .fo_ioctl = invfo_ioctl,
1482d69d0dcSJohn Baldwin .fo_poll = invfo_poll,
1492d69d0dcSJohn Baldwin .fo_kqfilter = invfo_kqfilter,
1506bc1e9cdSJohn Baldwin .fo_stat = ksem_stat,
1516bc1e9cdSJohn Baldwin .fo_close = ksem_closef,
1529c00bb91SKonstantin Belousov .fo_chmod = ksem_chmod,
1539c00bb91SKonstantin Belousov .fo_chown = ksem_chown,
154ca04d21dSGleb Smirnoff .fo_sendfile = invfo_sendfile,
1559696feebSJohn Baldwin .fo_fill_kinfo = ksem_fill_kinfo,
156f28526e9SKonstantin Belousov .fo_cmp = file_kcmp_generic,
1576bc1e9cdSJohn Baldwin .fo_flags = DFLAG_PASSABLE
1586bc1e9cdSJohn Baldwin };
1596bc1e9cdSJohn Baldwin
1606bc1e9cdSJohn Baldwin FEATURE(posix_sem, "POSIX semaphores");
1616bc1e9cdSJohn Baldwin
1626bc1e9cdSJohn Baldwin static int
ksem_stat(struct file * fp,struct stat * sb,struct ucred * active_cred)1632b68eb8eSMateusz Guzik ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
1646bc1e9cdSJohn Baldwin {
1656bc1e9cdSJohn Baldwin struct ksem *ks;
1666bc1e9cdSJohn Baldwin #ifdef MAC
1676bc1e9cdSJohn Baldwin int error;
1686bc1e9cdSJohn Baldwin #endif
1696bc1e9cdSJohn Baldwin
1706bc1e9cdSJohn Baldwin ks = fp->f_data;
1716bc1e9cdSJohn Baldwin
1726bc1e9cdSJohn Baldwin #ifdef MAC
1736bc1e9cdSJohn Baldwin error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
1746bc1e9cdSJohn Baldwin if (error)
1756bc1e9cdSJohn Baldwin return (error);
1766bc1e9cdSJohn Baldwin #endif
1776bc1e9cdSJohn Baldwin
1786bc1e9cdSJohn Baldwin /*
1796bc1e9cdSJohn Baldwin * Attempt to return sanish values for fstat() on a semaphore
1806bc1e9cdSJohn Baldwin * file descriptor.
1816bc1e9cdSJohn Baldwin */
1826bc1e9cdSJohn Baldwin bzero(sb, sizeof(*sb));
1836bc1e9cdSJohn Baldwin
1849c00bb91SKonstantin Belousov mtx_lock(&sem_lock);
185510ea843SEd Schouten sb->st_atim = ks->ks_atime;
186510ea843SEd Schouten sb->st_ctim = ks->ks_ctime;
187510ea843SEd Schouten sb->st_mtim = ks->ks_mtime;
188510ea843SEd Schouten sb->st_birthtim = ks->ks_birthtime;
1896bc1e9cdSJohn Baldwin sb->st_uid = ks->ks_uid;
1906bc1e9cdSJohn Baldwin sb->st_gid = ks->ks_gid;
1919c00bb91SKonstantin Belousov sb->st_mode = S_IFREG | ks->ks_mode; /* XXX */
1929c00bb91SKonstantin Belousov mtx_unlock(&sem_lock);
1936bc1e9cdSJohn Baldwin
1946bc1e9cdSJohn Baldwin return (0);
1956bc1e9cdSJohn Baldwin }
1966bc1e9cdSJohn Baldwin
1976bc1e9cdSJohn Baldwin static int
ksem_chmod(struct file * fp,mode_t mode,struct ucred * active_cred,struct thread * td)1989c00bb91SKonstantin Belousov ksem_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1999c00bb91SKonstantin Belousov struct thread *td)
2009c00bb91SKonstantin Belousov {
2019c00bb91SKonstantin Belousov struct ksem *ks;
2029c00bb91SKonstantin Belousov int error;
2039c00bb91SKonstantin Belousov
2049c00bb91SKonstantin Belousov error = 0;
2059c00bb91SKonstantin Belousov ks = fp->f_data;
2069c00bb91SKonstantin Belousov mtx_lock(&sem_lock);
2079c00bb91SKonstantin Belousov #ifdef MAC
2089c00bb91SKonstantin Belousov error = mac_posixsem_check_setmode(active_cred, ks, mode);
2099c00bb91SKonstantin Belousov if (error != 0)
2109c00bb91SKonstantin Belousov goto out;
2119c00bb91SKonstantin Belousov #endif
2129c00bb91SKonstantin Belousov error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid, VADMIN,
213d292b194SMateusz Guzik active_cred);
2149c00bb91SKonstantin Belousov if (error != 0)
2159c00bb91SKonstantin Belousov goto out;
2169c00bb91SKonstantin Belousov ks->ks_mode = mode & ACCESSPERMS;
2179c00bb91SKonstantin Belousov out:
2189c00bb91SKonstantin Belousov mtx_unlock(&sem_lock);
2199c00bb91SKonstantin Belousov return (error);
2209c00bb91SKonstantin Belousov }
2219c00bb91SKonstantin Belousov
2229c00bb91SKonstantin Belousov static int
ksem_chown(struct file * fp,uid_t uid,gid_t gid,struct ucred * active_cred,struct thread * td)2239c00bb91SKonstantin Belousov ksem_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2249c00bb91SKonstantin Belousov struct thread *td)
2259c00bb91SKonstantin Belousov {
2269c00bb91SKonstantin Belousov struct ksem *ks;
2279c00bb91SKonstantin Belousov int error;
2289c00bb91SKonstantin Belousov
22968889ed6SKonstantin Belousov error = 0;
2309c00bb91SKonstantin Belousov ks = fp->f_data;
2319c00bb91SKonstantin Belousov mtx_lock(&sem_lock);
2329c00bb91SKonstantin Belousov #ifdef MAC
2339c00bb91SKonstantin Belousov error = mac_posixsem_check_setowner(active_cred, ks, uid, gid);
2349c00bb91SKonstantin Belousov if (error != 0)
2359c00bb91SKonstantin Belousov goto out;
2369c00bb91SKonstantin Belousov #endif
2379c00bb91SKonstantin Belousov if (uid == (uid_t)-1)
2389c00bb91SKonstantin Belousov uid = ks->ks_uid;
2399c00bb91SKonstantin Belousov if (gid == (gid_t)-1)
2409c00bb91SKonstantin Belousov gid = ks->ks_gid;
2419c00bb91SKonstantin Belousov if (((uid != ks->ks_uid && uid != active_cred->cr_uid) ||
2429c00bb91SKonstantin Belousov (gid != ks->ks_gid && !groupmember(gid, active_cred))) &&
243cc426dd3SMateusz Guzik (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
2449c00bb91SKonstantin Belousov goto out;
2459c00bb91SKonstantin Belousov ks->ks_uid = uid;
2469c00bb91SKonstantin Belousov ks->ks_gid = gid;
2479c00bb91SKonstantin Belousov out:
2489c00bb91SKonstantin Belousov mtx_unlock(&sem_lock);
2499c00bb91SKonstantin Belousov return (error);
2509c00bb91SKonstantin Belousov }
2519c00bb91SKonstantin Belousov
2529c00bb91SKonstantin Belousov static int
ksem_closef(struct file * fp,struct thread * td)2536bc1e9cdSJohn Baldwin ksem_closef(struct file *fp, struct thread *td)
2546bc1e9cdSJohn Baldwin {
2556bc1e9cdSJohn Baldwin struct ksem *ks;
2566bc1e9cdSJohn Baldwin
2576bc1e9cdSJohn Baldwin ks = fp->f_data;
2586bc1e9cdSJohn Baldwin fp->f_data = NULL;
2596bc1e9cdSJohn Baldwin ksem_drop(ks);
2606bc1e9cdSJohn Baldwin
2616bc1e9cdSJohn Baldwin return (0);
2626bc1e9cdSJohn Baldwin }
2636bc1e9cdSJohn Baldwin
2649696feebSJohn Baldwin static int
ksem_fill_kinfo(struct file * fp,struct kinfo_file * kif,struct filedesc * fdp)2659696feebSJohn Baldwin ksem_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2669696feebSJohn Baldwin {
267cc7b259aSJamie Gritton const char *path, *pr_path;
2689696feebSJohn Baldwin struct ksem *ks;
269cc7b259aSJamie Gritton size_t pr_pathlen;
2709696feebSJohn Baldwin
2719696feebSJohn Baldwin kif->kf_type = KF_TYPE_SEM;
2729696feebSJohn Baldwin ks = fp->f_data;
2739696feebSJohn Baldwin mtx_lock(&sem_lock);
2749696feebSJohn Baldwin kif->kf_un.kf_sem.kf_sem_value = ks->ks_value;
2759696feebSJohn Baldwin kif->kf_un.kf_sem.kf_sem_mode = S_IFREG | ks->ks_mode; /* XXX */
2769696feebSJohn Baldwin mtx_unlock(&sem_lock);
2779696feebSJohn Baldwin if (ks->ks_path != NULL) {
2789696feebSJohn Baldwin sx_slock(&ksem_dict_lock);
27944c16975SJamie Gritton if (ks->ks_path != NULL) {
280cc7b259aSJamie Gritton path = ks->ks_path;
281cc7b259aSJamie Gritton pr_path = curthread->td_ucred->cr_prison->pr_path;
28244c16975SJamie Gritton if (strcmp(pr_path, "/") != 0) {
28344c16975SJamie Gritton /* Return the jail-rooted pathname. */
284cc7b259aSJamie Gritton pr_pathlen = strlen(pr_path);
285cc7b259aSJamie Gritton if (strncmp(path, pr_path, pr_pathlen) == 0 &&
286cc7b259aSJamie Gritton path[pr_pathlen] == '/')
287cc7b259aSJamie Gritton path += pr_pathlen;
288cc7b259aSJamie Gritton }
289cc7b259aSJamie Gritton strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
290cc7b259aSJamie Gritton }
2919696feebSJohn Baldwin sx_sunlock(&ksem_dict_lock);
2929696feebSJohn Baldwin }
2939696feebSJohn Baldwin return (0);
2949696feebSJohn Baldwin }
2959696feebSJohn Baldwin
2966bc1e9cdSJohn Baldwin /*
2976bc1e9cdSJohn Baldwin * ksem object management including creation and reference counting
2986bc1e9cdSJohn Baldwin * routines.
2996bc1e9cdSJohn Baldwin */
3006bc1e9cdSJohn Baldwin static struct ksem *
ksem_alloc(struct ucred * ucred,mode_t mode,unsigned int value)3016bc1e9cdSJohn Baldwin ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
3026bc1e9cdSJohn Baldwin {
3036bc1e9cdSJohn Baldwin struct ksem *ks;
3046bc1e9cdSJohn Baldwin
3056bc1e9cdSJohn Baldwin mtx_lock(&ksem_count_lock);
3066bc1e9cdSJohn Baldwin if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
3076bc1e9cdSJohn Baldwin mtx_unlock(&ksem_count_lock);
3086bc1e9cdSJohn Baldwin return (NULL);
3096bc1e9cdSJohn Baldwin }
3106bc1e9cdSJohn Baldwin nsems++;
3116bc1e9cdSJohn Baldwin mtx_unlock(&ksem_count_lock);
3126bc1e9cdSJohn Baldwin ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
3136bc1e9cdSJohn Baldwin ks->ks_uid = ucred->cr_uid;
3146bc1e9cdSJohn Baldwin ks->ks_gid = ucred->cr_gid;
3156bc1e9cdSJohn Baldwin ks->ks_mode = mode;
3166bc1e9cdSJohn Baldwin ks->ks_value = value;
3176bc1e9cdSJohn Baldwin cv_init(&ks->ks_cv, "ksem");
3186bc1e9cdSJohn Baldwin vfs_timestamp(&ks->ks_birthtime);
3196bc1e9cdSJohn Baldwin ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
3206bc1e9cdSJohn Baldwin refcount_init(&ks->ks_ref, 1);
3216bc1e9cdSJohn Baldwin #ifdef MAC
3226bc1e9cdSJohn Baldwin mac_posixsem_init(ks);
3236bc1e9cdSJohn Baldwin mac_posixsem_create(ucred, ks);
3246bc1e9cdSJohn Baldwin #endif
3256bc1e9cdSJohn Baldwin
3266bc1e9cdSJohn Baldwin return (ks);
3276bc1e9cdSJohn Baldwin }
3286bc1e9cdSJohn Baldwin
3296bc1e9cdSJohn Baldwin static struct ksem *
ksem_hold(struct ksem * ks)3306bc1e9cdSJohn Baldwin ksem_hold(struct ksem *ks)
3316bc1e9cdSJohn Baldwin {
3326bc1e9cdSJohn Baldwin
3336bc1e9cdSJohn Baldwin refcount_acquire(&ks->ks_ref);
3346bc1e9cdSJohn Baldwin return (ks);
3356bc1e9cdSJohn Baldwin }
3366bc1e9cdSJohn Baldwin
3376bc1e9cdSJohn Baldwin static void
ksem_drop(struct ksem * ks)3386bc1e9cdSJohn Baldwin ksem_drop(struct ksem *ks)
3396bc1e9cdSJohn Baldwin {
3406bc1e9cdSJohn Baldwin
3416bc1e9cdSJohn Baldwin if (refcount_release(&ks->ks_ref)) {
3426bc1e9cdSJohn Baldwin #ifdef MAC
3436bc1e9cdSJohn Baldwin mac_posixsem_destroy(ks);
3446bc1e9cdSJohn Baldwin #endif
3456bc1e9cdSJohn Baldwin cv_destroy(&ks->ks_cv);
3466bc1e9cdSJohn Baldwin free(ks, M_KSEM);
3476bc1e9cdSJohn Baldwin mtx_lock(&ksem_count_lock);
3486bc1e9cdSJohn Baldwin nsems--;
3496bc1e9cdSJohn Baldwin mtx_unlock(&ksem_count_lock);
3506bc1e9cdSJohn Baldwin }
3516bc1e9cdSJohn Baldwin }
3526bc1e9cdSJohn Baldwin
3536bc1e9cdSJohn Baldwin /*
3546bc1e9cdSJohn Baldwin * Determine if the credentials have sufficient permissions for read
3556bc1e9cdSJohn Baldwin * and write access.
3566bc1e9cdSJohn Baldwin */
3576bc1e9cdSJohn Baldwin static int
ksem_access(struct ksem * ks,struct ucred * ucred)3586bc1e9cdSJohn Baldwin ksem_access(struct ksem *ks, struct ucred *ucred)
3596bc1e9cdSJohn Baldwin {
360efaa6588SAlfred Perlstein int error;
361efaa6588SAlfred Perlstein
3626bc1e9cdSJohn Baldwin error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
363d292b194SMateusz Guzik VREAD | VWRITE, ucred);
3646bc1e9cdSJohn Baldwin if (error)
365cc426dd3SMateusz Guzik error = priv_check_cred(ucred, PRIV_SEM_WRITE);
366efaa6588SAlfred Perlstein return (error);
367efaa6588SAlfred Perlstein }
368efaa6588SAlfred Perlstein
3696bc1e9cdSJohn Baldwin /*
3706bc1e9cdSJohn Baldwin * Dictionary management. We maintain an in-kernel dictionary to map
3716bc1e9cdSJohn Baldwin * paths to semaphore objects. We use the FNV hash on the path to
3726bc1e9cdSJohn Baldwin * store the mappings in a hash table.
3736bc1e9cdSJohn Baldwin */
3746bc1e9cdSJohn Baldwin static struct ksem *
ksem_lookup(char * path,Fnv32_t fnv)3756bc1e9cdSJohn Baldwin ksem_lookup(char *path, Fnv32_t fnv)
3766bc1e9cdSJohn Baldwin {
3776bc1e9cdSJohn Baldwin struct ksem_mapping *map;
3786bc1e9cdSJohn Baldwin
3796bc1e9cdSJohn Baldwin LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
3806bc1e9cdSJohn Baldwin if (map->km_fnv != fnv)
3816bc1e9cdSJohn Baldwin continue;
3826bc1e9cdSJohn Baldwin if (strcmp(map->km_path, path) == 0)
3836bc1e9cdSJohn Baldwin return (map->km_ksem);
3846bc1e9cdSJohn Baldwin }
3856bc1e9cdSJohn Baldwin
3866bc1e9cdSJohn Baldwin return (NULL);
3876bc1e9cdSJohn Baldwin }
3886bc1e9cdSJohn Baldwin
3896bc1e9cdSJohn Baldwin static void
ksem_insert(char * path,Fnv32_t fnv,struct ksem * ks)3906bc1e9cdSJohn Baldwin ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
3916bc1e9cdSJohn Baldwin {
3926bc1e9cdSJohn Baldwin struct ksem_mapping *map;
3936bc1e9cdSJohn Baldwin
3946bc1e9cdSJohn Baldwin map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
3956bc1e9cdSJohn Baldwin map->km_path = path;
3966bc1e9cdSJohn Baldwin map->km_fnv = fnv;
3976bc1e9cdSJohn Baldwin map->km_ksem = ksem_hold(ks);
398958aa575SJohn Baldwin ks->ks_path = path;
3996bc1e9cdSJohn Baldwin LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
4006bc1e9cdSJohn Baldwin }
4016bc1e9cdSJohn Baldwin
4026bc1e9cdSJohn Baldwin static int
ksem_remove(char * path,Fnv32_t fnv,struct ucred * ucred)4036bc1e9cdSJohn Baldwin ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
4046bc1e9cdSJohn Baldwin {
4056bc1e9cdSJohn Baldwin struct ksem_mapping *map;
4066bc1e9cdSJohn Baldwin int error;
4076bc1e9cdSJohn Baldwin
4086bc1e9cdSJohn Baldwin LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
4096bc1e9cdSJohn Baldwin if (map->km_fnv != fnv)
4106bc1e9cdSJohn Baldwin continue;
4116bc1e9cdSJohn Baldwin if (strcmp(map->km_path, path) == 0) {
4126bc1e9cdSJohn Baldwin #ifdef MAC
4136bc1e9cdSJohn Baldwin error = mac_posixsem_check_unlink(ucred, map->km_ksem);
4146bc1e9cdSJohn Baldwin if (error)
4156bc1e9cdSJohn Baldwin return (error);
4166bc1e9cdSJohn Baldwin #endif
4176bc1e9cdSJohn Baldwin error = ksem_access(map->km_ksem, ucred);
4186bc1e9cdSJohn Baldwin if (error)
4196bc1e9cdSJohn Baldwin return (error);
420958aa575SJohn Baldwin map->km_ksem->ks_path = NULL;
4216bc1e9cdSJohn Baldwin LIST_REMOVE(map, km_link);
4226bc1e9cdSJohn Baldwin ksem_drop(map->km_ksem);
4236bc1e9cdSJohn Baldwin free(map->km_path, M_KSEM);
4246bc1e9cdSJohn Baldwin free(map, M_KSEM);
4256bc1e9cdSJohn Baldwin return (0);
4266bc1e9cdSJohn Baldwin }
4276bc1e9cdSJohn Baldwin }
4286bc1e9cdSJohn Baldwin
4296bc1e9cdSJohn Baldwin return (ENOENT);
4306bc1e9cdSJohn Baldwin }
4316bc1e9cdSJohn Baldwin
4320e5d5bc2SKonstantin Belousov static int
ksem_create_copyout_semid(struct thread * td,semid_t * semidp,int fd,int compat32)4330e5d5bc2SKonstantin Belousov ksem_create_copyout_semid(struct thread *td, semid_t *semidp, int fd,
4340e5d5bc2SKonstantin Belousov int compat32)
4350e5d5bc2SKonstantin Belousov {
4360e5d5bc2SKonstantin Belousov semid_t semid;
4370e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
4380e5d5bc2SKonstantin Belousov int32_t semid32;
4390e5d5bc2SKonstantin Belousov #endif
4400e5d5bc2SKonstantin Belousov void *ptr;
4410e5d5bc2SKonstantin Belousov size_t ptrs;
4420e5d5bc2SKonstantin Belousov
4430e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
4440e5d5bc2SKonstantin Belousov if (compat32) {
4450e5d5bc2SKonstantin Belousov semid32 = fd;
4460e5d5bc2SKonstantin Belousov ptr = &semid32;
4470e5d5bc2SKonstantin Belousov ptrs = sizeof(semid32);
4480e5d5bc2SKonstantin Belousov } else {
4490e5d5bc2SKonstantin Belousov #endif
4500e5d5bc2SKonstantin Belousov semid = fd;
4510e5d5bc2SKonstantin Belousov ptr = &semid;
4520e5d5bc2SKonstantin Belousov ptrs = sizeof(semid);
4530e5d5bc2SKonstantin Belousov compat32 = 0; /* silence gcc */
4540e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
4550e5d5bc2SKonstantin Belousov }
4560e5d5bc2SKonstantin Belousov #endif
4570e5d5bc2SKonstantin Belousov
4580e5d5bc2SKonstantin Belousov return (copyout(ptr, semidp, ptrs));
4590e5d5bc2SKonstantin Belousov }
4600e5d5bc2SKonstantin Belousov
4616bc1e9cdSJohn Baldwin /* Other helper routines. */
4626bc1e9cdSJohn Baldwin static int
ksem_create(struct thread * td,const char * name,semid_t * semidp,mode_t mode,unsigned int value,int flags,int compat32)4636bc1e9cdSJohn Baldwin ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
4640e5d5bc2SKonstantin Belousov unsigned int value, int flags, int compat32)
4656bc1e9cdSJohn Baldwin {
46685078b85SConrad Meyer struct pwddesc *pdp;
4676bc1e9cdSJohn Baldwin struct ksem *ks;
4686bc1e9cdSJohn Baldwin struct file *fp;
4696bc1e9cdSJohn Baldwin char *path;
470cc7b259aSJamie Gritton const char *pr_path;
471cc7b259aSJamie Gritton size_t pr_pathlen;
4726bc1e9cdSJohn Baldwin Fnv32_t fnv;
4736bc1e9cdSJohn Baldwin int error, fd;
4746bc1e9cdSJohn Baldwin
47515bcf785SRobert Watson AUDIT_ARG_FFLAGS(flags);
47615bcf785SRobert Watson AUDIT_ARG_MODE(mode);
47715bcf785SRobert Watson AUDIT_ARG_VALUE(value);
47815bcf785SRobert Watson
4796bc1e9cdSJohn Baldwin if (value > SEM_VALUE_MAX)
4806bc1e9cdSJohn Baldwin return (EINVAL);
4816bc1e9cdSJohn Baldwin
48285078b85SConrad Meyer pdp = td->td_proc->p_pd;
48385078b85SConrad Meyer mode = (mode & ~pdp->pd_cmask) & ACCESSPERMS;
484b68cf25fSJilles Tjoelker error = falloc(td, &fp, &fd, O_CLOEXEC);
4856bc1e9cdSJohn Baldwin if (error) {
4866bc1e9cdSJohn Baldwin if (name == NULL)
4876bc1e9cdSJohn Baldwin error = ENOSPC;
4886bc1e9cdSJohn Baldwin return (error);
4896bc1e9cdSJohn Baldwin }
4906bc1e9cdSJohn Baldwin
4916bc1e9cdSJohn Baldwin /*
4926bc1e9cdSJohn Baldwin * Go ahead and copyout the file descriptor now. This is a bit
4936bc1e9cdSJohn Baldwin * premature, but it is a lot easier to handle errors as opposed
4946bc1e9cdSJohn Baldwin * to later when we've possibly created a new semaphore, etc.
4956bc1e9cdSJohn Baldwin */
4960e5d5bc2SKonstantin Belousov error = ksem_create_copyout_semid(td, semidp, fd, compat32);
4976bc1e9cdSJohn Baldwin if (error) {
49890f54cbfSMateusz Guzik fdclose(td, fp, fd);
4996bc1e9cdSJohn Baldwin fdrop(fp, td);
5006bc1e9cdSJohn Baldwin return (error);
5016bc1e9cdSJohn Baldwin }
5026bc1e9cdSJohn Baldwin
5036bc1e9cdSJohn Baldwin if (name == NULL) {
5046bc1e9cdSJohn Baldwin /* Create an anonymous semaphore. */
5056bc1e9cdSJohn Baldwin ks = ksem_alloc(td->td_ucred, mode, value);
5066bc1e9cdSJohn Baldwin if (ks == NULL)
5076bc1e9cdSJohn Baldwin error = ENOSPC;
5086bc1e9cdSJohn Baldwin else
5096bc1e9cdSJohn Baldwin ks->ks_flags |= KS_ANONYMOUS;
5106bc1e9cdSJohn Baldwin } else {
5116bc1e9cdSJohn Baldwin path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
512cc7b259aSJamie Gritton pr_path = td->td_ucred->cr_prison->pr_path;
51344c16975SJamie Gritton
51444c16975SJamie Gritton /* Construct a full pathname for jailed callers. */
515cc7b259aSJamie Gritton pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
516cc7b259aSJamie Gritton : strlcpy(path, pr_path, MAXPATHLEN);
517cc7b259aSJamie Gritton error = copyinstr(name, path + pr_pathlen,
518cc7b259aSJamie Gritton MAXPATHLEN - pr_pathlen, NULL);
5196bc1e9cdSJohn Baldwin
5206bc1e9cdSJohn Baldwin /* Require paths to start with a '/' character. */
521cc7b259aSJamie Gritton if (error == 0 && path[pr_pathlen] != '/')
5226bc1e9cdSJohn Baldwin error = EINVAL;
5236bc1e9cdSJohn Baldwin if (error) {
52490f54cbfSMateusz Guzik fdclose(td, fp, fd);
5256bc1e9cdSJohn Baldwin fdrop(fp, td);
5266bc1e9cdSJohn Baldwin free(path, M_KSEM);
5276bc1e9cdSJohn Baldwin return (error);
5286bc1e9cdSJohn Baldwin }
5296bc1e9cdSJohn Baldwin
53015bcf785SRobert Watson AUDIT_ARG_UPATH1_CANON(path);
5316bc1e9cdSJohn Baldwin fnv = fnv_32_str(path, FNV1_32_INIT);
5326bc1e9cdSJohn Baldwin sx_xlock(&ksem_dict_lock);
5336bc1e9cdSJohn Baldwin ks = ksem_lookup(path, fnv);
5346bc1e9cdSJohn Baldwin if (ks == NULL) {
5356bc1e9cdSJohn Baldwin /* Object does not exist, create it if requested. */
5366bc1e9cdSJohn Baldwin if (flags & O_CREAT) {
5376bc1e9cdSJohn Baldwin ks = ksem_alloc(td->td_ucred, mode, value);
5386bc1e9cdSJohn Baldwin if (ks == NULL)
5396bc1e9cdSJohn Baldwin error = ENFILE;
5406bc1e9cdSJohn Baldwin else {
5416bc1e9cdSJohn Baldwin ksem_insert(path, fnv, ks);
5426bc1e9cdSJohn Baldwin path = NULL;
5436bc1e9cdSJohn Baldwin }
5446bc1e9cdSJohn Baldwin } else
5456bc1e9cdSJohn Baldwin error = ENOENT;
5466bc1e9cdSJohn Baldwin } else {
5476bc1e9cdSJohn Baldwin /*
5486bc1e9cdSJohn Baldwin * Object already exists, obtain a new
5496bc1e9cdSJohn Baldwin * reference if requested and permitted.
5506bc1e9cdSJohn Baldwin */
5516bc1e9cdSJohn Baldwin if ((flags & (O_CREAT | O_EXCL)) ==
5526bc1e9cdSJohn Baldwin (O_CREAT | O_EXCL))
5536bc1e9cdSJohn Baldwin error = EEXIST;
5546bc1e9cdSJohn Baldwin else {
5556bc1e9cdSJohn Baldwin #ifdef MAC
5566bc1e9cdSJohn Baldwin error = mac_posixsem_check_open(td->td_ucred,
5576bc1e9cdSJohn Baldwin ks);
5586bc1e9cdSJohn Baldwin if (error == 0)
5596bc1e9cdSJohn Baldwin #endif
5606bc1e9cdSJohn Baldwin error = ksem_access(ks, td->td_ucred);
5616bc1e9cdSJohn Baldwin }
5626bc1e9cdSJohn Baldwin if (error == 0)
5636bc1e9cdSJohn Baldwin ksem_hold(ks);
5646bc1e9cdSJohn Baldwin #ifdef INVARIANTS
5656bc1e9cdSJohn Baldwin else
5666bc1e9cdSJohn Baldwin ks = NULL;
5676bc1e9cdSJohn Baldwin #endif
5686bc1e9cdSJohn Baldwin }
5696bc1e9cdSJohn Baldwin sx_xunlock(&ksem_dict_lock);
5706bc1e9cdSJohn Baldwin if (path)
5716bc1e9cdSJohn Baldwin free(path, M_KSEM);
5726bc1e9cdSJohn Baldwin }
5736bc1e9cdSJohn Baldwin
5746bc1e9cdSJohn Baldwin if (error) {
5756bc1e9cdSJohn Baldwin KASSERT(ks == NULL, ("ksem_create error with a ksem"));
57690f54cbfSMateusz Guzik fdclose(td, fp, fd);
5776bc1e9cdSJohn Baldwin fdrop(fp, td);
5786bc1e9cdSJohn Baldwin return (error);
5796bc1e9cdSJohn Baldwin }
5806bc1e9cdSJohn Baldwin KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
5816bc1e9cdSJohn Baldwin
5826bc1e9cdSJohn Baldwin finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
5836bc1e9cdSJohn Baldwin
5846bc1e9cdSJohn Baldwin fdrop(fp, td);
5856bc1e9cdSJohn Baldwin
5866bc1e9cdSJohn Baldwin return (0);
5876bc1e9cdSJohn Baldwin }
5886bc1e9cdSJohn Baldwin
5896bc1e9cdSJohn Baldwin static int
ksem_get(struct thread * td,semid_t id,cap_rights_t * rightsp,struct file ** fpp)5907008be5bSPawel Jakub Dawidek ksem_get(struct thread *td, semid_t id, cap_rights_t *rightsp,
5917008be5bSPawel Jakub Dawidek struct file **fpp)
5926bc1e9cdSJohn Baldwin {
5936bc1e9cdSJohn Baldwin struct ksem *ks;
5946bc1e9cdSJohn Baldwin struct file *fp;
5956bc1e9cdSJohn Baldwin int error;
5966bc1e9cdSJohn Baldwin
5977008be5bSPawel Jakub Dawidek error = fget(td, id, rightsp, &fp);
5986bc1e9cdSJohn Baldwin if (error)
5996bc1e9cdSJohn Baldwin return (EINVAL);
6006bc1e9cdSJohn Baldwin if (fp->f_type != DTYPE_SEM) {
6016bc1e9cdSJohn Baldwin fdrop(fp, td);
6026bc1e9cdSJohn Baldwin return (EINVAL);
6036bc1e9cdSJohn Baldwin }
6046bc1e9cdSJohn Baldwin ks = fp->f_data;
6056bc1e9cdSJohn Baldwin if (ks->ks_flags & KS_DEAD) {
6066bc1e9cdSJohn Baldwin fdrop(fp, td);
6076bc1e9cdSJohn Baldwin return (EINVAL);
6086bc1e9cdSJohn Baldwin }
6096bc1e9cdSJohn Baldwin *fpp = fp;
6106bc1e9cdSJohn Baldwin return (0);
6116bc1e9cdSJohn Baldwin }
6126bc1e9cdSJohn Baldwin
6136bc1e9cdSJohn Baldwin /* System calls. */
614efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
615efaa6588SAlfred Perlstein struct ksem_init_args {
616efaa6588SAlfred Perlstein unsigned int value;
617efaa6588SAlfred Perlstein semid_t *idp;
618efaa6588SAlfred Perlstein };
619efaa6588SAlfred Perlstein #endif
620efaa6588SAlfred Perlstein int
sys_ksem_init(struct thread * td,struct ksem_init_args * uap)6218451d0ddSKip Macy sys_ksem_init(struct thread *td, struct ksem_init_args *uap)
622efaa6588SAlfred Perlstein {
623efaa6588SAlfred Perlstein
6246bc1e9cdSJohn Baldwin return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
6250e5d5bc2SKonstantin Belousov 0, 0));
626efaa6588SAlfred Perlstein }
627efaa6588SAlfred Perlstein
628efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
629efaa6588SAlfred Perlstein struct ksem_open_args {
630efaa6588SAlfred Perlstein char *name;
631efaa6588SAlfred Perlstein int oflag;
632efaa6588SAlfred Perlstein mode_t mode;
633efaa6588SAlfred Perlstein unsigned int value;
634efaa6588SAlfred Perlstein semid_t *idp;
635efaa6588SAlfred Perlstein };
636efaa6588SAlfred Perlstein #endif
637efaa6588SAlfred Perlstein int
sys_ksem_open(struct thread * td,struct ksem_open_args * uap)6388451d0ddSKip Macy sys_ksem_open(struct thread *td, struct ksem_open_args *uap)
639efaa6588SAlfred Perlstein {
640efaa6588SAlfred Perlstein
641b2966a5aSBruce M Simpson DP((">>> ksem_open start, pid=%d\n", (int)td->td_proc->p_pid));
642b2966a5aSBruce M Simpson
6436bc1e9cdSJohn Baldwin if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
644b3890a1cSAlfred Perlstein return (EINVAL);
6456bc1e9cdSJohn Baldwin return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
6460e5d5bc2SKonstantin Belousov uap->oflag, 0));
647efaa6588SAlfred Perlstein }
648efaa6588SAlfred Perlstein
649efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
650efaa6588SAlfred Perlstein struct ksem_unlink_args {
651efaa6588SAlfred Perlstein char *name;
652efaa6588SAlfred Perlstein };
653efaa6588SAlfred Perlstein #endif
654efaa6588SAlfred Perlstein int
sys_ksem_unlink(struct thread * td,struct ksem_unlink_args * uap)6558451d0ddSKip Macy sys_ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
656efaa6588SAlfred Perlstein {
6576bc1e9cdSJohn Baldwin char *path;
658cc7b259aSJamie Gritton const char *pr_path;
659cc7b259aSJamie Gritton size_t pr_pathlen;
6606bc1e9cdSJohn Baldwin Fnv32_t fnv;
661efaa6588SAlfred Perlstein int error;
662efaa6588SAlfred Perlstein
6636bc1e9cdSJohn Baldwin path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
664cc7b259aSJamie Gritton pr_path = td->td_ucred->cr_prison->pr_path;
665cc7b259aSJamie Gritton pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
666cc7b259aSJamie Gritton : strlcpy(path, pr_path, MAXPATHLEN);
667cc7b259aSJamie Gritton error = copyinstr(uap->name, path + pr_pathlen, MAXPATHLEN - pr_pathlen,
668cc7b259aSJamie Gritton NULL);
66952648411SRobert Watson if (error) {
6706bc1e9cdSJohn Baldwin free(path, M_TEMP);
67152648411SRobert Watson return (error);
67252648411SRobert Watson }
6736bc1e9cdSJohn Baldwin
67415bcf785SRobert Watson AUDIT_ARG_UPATH1_CANON(path);
6756bc1e9cdSJohn Baldwin fnv = fnv_32_str(path, FNV1_32_INIT);
6766bc1e9cdSJohn Baldwin sx_xlock(&ksem_dict_lock);
6776bc1e9cdSJohn Baldwin error = ksem_remove(path, fnv, td->td_ucred);
6786bc1e9cdSJohn Baldwin sx_xunlock(&ksem_dict_lock);
6796bc1e9cdSJohn Baldwin free(path, M_TEMP);
6806bc1e9cdSJohn Baldwin
681efaa6588SAlfred Perlstein return (error);
682efaa6588SAlfred Perlstein }
683efaa6588SAlfred Perlstein
684efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
685efaa6588SAlfred Perlstein struct ksem_close_args {
686efaa6588SAlfred Perlstein semid_t id;
687efaa6588SAlfred Perlstein };
688efaa6588SAlfred Perlstein #endif
689efaa6588SAlfred Perlstein int
sys_ksem_close(struct thread * td,struct ksem_close_args * uap)6908451d0ddSKip Macy sys_ksem_close(struct thread *td, struct ksem_close_args *uap)
691efaa6588SAlfred Perlstein {
692efaa6588SAlfred Perlstein struct ksem *ks;
6936bc1e9cdSJohn Baldwin struct file *fp;
694efaa6588SAlfred Perlstein int error;
695efaa6588SAlfred Perlstein
696a9d2f8d8SRobert Watson /* No capability rights required to close a semaphore. */
69715bcf785SRobert Watson AUDIT_ARG_FD(uap->id);
698cbd92ce6SMatt Macy error = ksem_get(td, uap->id, &cap_no_rights, &fp);
6996bc1e9cdSJohn Baldwin if (error)
7006bc1e9cdSJohn Baldwin return (error);
7016bc1e9cdSJohn Baldwin ks = fp->f_data;
7026bc1e9cdSJohn Baldwin if (ks->ks_flags & KS_ANONYMOUS) {
7036bc1e9cdSJohn Baldwin fdrop(fp, td);
7046bc1e9cdSJohn Baldwin return (EINVAL);
7056bc1e9cdSJohn Baldwin }
7066bc1e9cdSJohn Baldwin error = kern_close(td, uap->id);
7076bc1e9cdSJohn Baldwin fdrop(fp, td);
708b3890a1cSAlfred Perlstein return (error);
709efaa6588SAlfred Perlstein }
710efaa6588SAlfred Perlstein
711efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
712efaa6588SAlfred Perlstein struct ksem_post_args {
713efaa6588SAlfred Perlstein semid_t id;
714efaa6588SAlfred Perlstein };
715efaa6588SAlfred Perlstein #endif
716efaa6588SAlfred Perlstein int
sys_ksem_post(struct thread * td,struct ksem_post_args * uap)7178451d0ddSKip Macy sys_ksem_post(struct thread *td, struct ksem_post_args *uap)
718efaa6588SAlfred Perlstein {
7197008be5bSPawel Jakub Dawidek cap_rights_t rights;
7206bc1e9cdSJohn Baldwin struct file *fp;
721efaa6588SAlfred Perlstein struct ksem *ks;
722efaa6588SAlfred Perlstein int error;
723efaa6588SAlfred Perlstein
72415bcf785SRobert Watson AUDIT_ARG_FD(uap->id);
7257008be5bSPawel Jakub Dawidek error = ksem_get(td, uap->id,
7266b3a9a0fSMateusz Guzik cap_rights_init_one(&rights, CAP_SEM_POST), &fp);
7276bc1e9cdSJohn Baldwin if (error)
7286bc1e9cdSJohn Baldwin return (error);
7296bc1e9cdSJohn Baldwin ks = fp->f_data;
7306bc1e9cdSJohn Baldwin
731efaa6588SAlfred Perlstein mtx_lock(&sem_lock);
73252648411SRobert Watson #ifdef MAC
7336bc1e9cdSJohn Baldwin error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
73452648411SRobert Watson if (error)
73552648411SRobert Watson goto err;
73652648411SRobert Watson #endif
737efaa6588SAlfred Perlstein if (ks->ks_value == SEM_VALUE_MAX) {
738efaa6588SAlfred Perlstein error = EOVERFLOW;
739efaa6588SAlfred Perlstein goto err;
740efaa6588SAlfred Perlstein }
741efaa6588SAlfred Perlstein ++ks->ks_value;
742efaa6588SAlfred Perlstein if (ks->ks_waiters > 0)
743efaa6588SAlfred Perlstein cv_signal(&ks->ks_cv);
744efaa6588SAlfred Perlstein error = 0;
7456bc1e9cdSJohn Baldwin vfs_timestamp(&ks->ks_ctime);
746efaa6588SAlfred Perlstein err:
747efaa6588SAlfred Perlstein mtx_unlock(&sem_lock);
7486bc1e9cdSJohn Baldwin fdrop(fp, td);
749efaa6588SAlfred Perlstein return (error);
750efaa6588SAlfred Perlstein }
751efaa6588SAlfred Perlstein
752efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
753efaa6588SAlfred Perlstein struct ksem_wait_args {
754efaa6588SAlfred Perlstein semid_t id;
755efaa6588SAlfred Perlstein };
756efaa6588SAlfred Perlstein #endif
757efaa6588SAlfred Perlstein int
sys_ksem_wait(struct thread * td,struct ksem_wait_args * uap)7588451d0ddSKip Macy sys_ksem_wait(struct thread *td, struct ksem_wait_args *uap)
759efaa6588SAlfred Perlstein {
760efaa6588SAlfred Perlstein
761aae94fbbSDaniel Eischen return (kern_sem_wait(td, uap->id, 0, NULL));
762aae94fbbSDaniel Eischen }
763aae94fbbSDaniel Eischen
764aae94fbbSDaniel Eischen #ifndef _SYS_SYSPROTO_H_
765aae94fbbSDaniel Eischen struct ksem_timedwait_args {
766aae94fbbSDaniel Eischen semid_t id;
767d60e86c8SStefan Farfeleder const struct timespec *abstime;
768aae94fbbSDaniel Eischen };
769aae94fbbSDaniel Eischen #endif
770aae94fbbSDaniel Eischen int
sys_ksem_timedwait(struct thread * td,struct ksem_timedwait_args * uap)7718451d0ddSKip Macy sys_ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
772aae94fbbSDaniel Eischen {
773aae94fbbSDaniel Eischen struct timespec abstime;
774aae94fbbSDaniel Eischen struct timespec *ts;
775aae94fbbSDaniel Eischen int error;
776aae94fbbSDaniel Eischen
7778e230e30SRobert Watson /*
7788e230e30SRobert Watson * We allow a null timespec (wait forever).
7798e230e30SRobert Watson */
780aae94fbbSDaniel Eischen if (uap->abstime == NULL)
781aae94fbbSDaniel Eischen ts = NULL;
782aae94fbbSDaniel Eischen else {
783aae94fbbSDaniel Eischen error = copyin(uap->abstime, &abstime, sizeof(abstime));
784aae94fbbSDaniel Eischen if (error != 0)
785aae94fbbSDaniel Eischen return (error);
786aae94fbbSDaniel Eischen if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
787aae94fbbSDaniel Eischen return (EINVAL);
788aae94fbbSDaniel Eischen ts = &abstime;
789aae94fbbSDaniel Eischen }
790aae94fbbSDaniel Eischen return (kern_sem_wait(td, uap->id, 0, ts));
791efaa6588SAlfred Perlstein }
792efaa6588SAlfred Perlstein
793efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
794efaa6588SAlfred Perlstein struct ksem_trywait_args {
795efaa6588SAlfred Perlstein semid_t id;
796efaa6588SAlfred Perlstein };
797efaa6588SAlfred Perlstein #endif
798efaa6588SAlfred Perlstein int
sys_ksem_trywait(struct thread * td,struct ksem_trywait_args * uap)7998451d0ddSKip Macy sys_ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
800efaa6588SAlfred Perlstein {
801efaa6588SAlfred Perlstein
802aae94fbbSDaniel Eischen return (kern_sem_wait(td, uap->id, 1, NULL));
803efaa6588SAlfred Perlstein }
804efaa6588SAlfred Perlstein
805c3053131SPoul-Henning Kamp static int
kern_sem_wait(struct thread * td,semid_t id,int tryflag,struct timespec * abstime)806c1250af6SRobert Watson kern_sem_wait(struct thread *td, semid_t id, int tryflag,
807c1250af6SRobert Watson struct timespec *abstime)
808efaa6588SAlfred Perlstein {
809aae94fbbSDaniel Eischen struct timespec ts1, ts2;
810aae94fbbSDaniel Eischen struct timeval tv;
8117008be5bSPawel Jakub Dawidek cap_rights_t rights;
8126bc1e9cdSJohn Baldwin struct file *fp;
813efaa6588SAlfred Perlstein struct ksem *ks;
814efaa6588SAlfred Perlstein int error;
815efaa6588SAlfred Perlstein
816b2966a5aSBruce M Simpson DP((">>> kern_sem_wait entered! pid=%d\n", (int)td->td_proc->p_pid));
81715bcf785SRobert Watson AUDIT_ARG_FD(id);
8186b3a9a0fSMateusz Guzik error = ksem_get(td, id, cap_rights_init_one(&rights, CAP_SEM_WAIT),
8196b3a9a0fSMateusz Guzik &fp);
8206bc1e9cdSJohn Baldwin if (error)
8216bc1e9cdSJohn Baldwin return (error);
8226bc1e9cdSJohn Baldwin ks = fp->f_data;
823efaa6588SAlfred Perlstein mtx_lock(&sem_lock);
824b2966a5aSBruce M Simpson DP((">>> kern_sem_wait critical section entered! pid=%d\n",
825b2966a5aSBruce M Simpson (int)td->td_proc->p_pid));
82652648411SRobert Watson #ifdef MAC
8276bc1e9cdSJohn Baldwin error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
82852648411SRobert Watson if (error) {
82952648411SRobert Watson DP(("kern_sem_wait mac failed\n"));
83052648411SRobert Watson goto err;
83152648411SRobert Watson }
83252648411SRobert Watson #endif
833c814aa3fSAlfred Perlstein DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
8346bc1e9cdSJohn Baldwin vfs_timestamp(&ks->ks_atime);
83577d8bf9cSBruce M Simpson while (ks->ks_value == 0) {
836efaa6588SAlfred Perlstein ks->ks_waiters++;
837aae94fbbSDaniel Eischen if (tryflag != 0)
838aae94fbbSDaniel Eischen error = EAGAIN;
839aae94fbbSDaniel Eischen else if (abstime == NULL)
840aae94fbbSDaniel Eischen error = cv_wait_sig(&ks->ks_cv, &sem_lock);
841aae94fbbSDaniel Eischen else {
842aae94fbbSDaniel Eischen for (;;) {
843aae94fbbSDaniel Eischen ts1 = *abstime;
844aae94fbbSDaniel Eischen getnanotime(&ts2);
8456040822cSAlan Somers timespecsub(&ts1, &ts2, &ts1);
846aae94fbbSDaniel Eischen TIMESPEC_TO_TIMEVAL(&tv, &ts1);
847aae94fbbSDaniel Eischen if (tv.tv_sec < 0) {
848aae94fbbSDaniel Eischen error = ETIMEDOUT;
849aae94fbbSDaniel Eischen break;
850aae94fbbSDaniel Eischen }
851aae94fbbSDaniel Eischen error = cv_timedwait_sig(&ks->ks_cv,
852aae94fbbSDaniel Eischen &sem_lock, tvtohz(&tv));
853aae94fbbSDaniel Eischen if (error != EWOULDBLOCK)
854aae94fbbSDaniel Eischen break;
855aae94fbbSDaniel Eischen }
856aae94fbbSDaniel Eischen }
857efaa6588SAlfred Perlstein ks->ks_waiters--;
858efaa6588SAlfred Perlstein if (error)
859efaa6588SAlfred Perlstein goto err;
860efaa6588SAlfred Perlstein }
861efaa6588SAlfred Perlstein ks->ks_value--;
862b2966a5aSBruce M Simpson DP(("kern_sem_wait value post-decrement = %d\n", ks->ks_value));
863efaa6588SAlfred Perlstein error = 0;
864efaa6588SAlfred Perlstein err:
865efaa6588SAlfred Perlstein mtx_unlock(&sem_lock);
8666bc1e9cdSJohn Baldwin fdrop(fp, td);
867b2966a5aSBruce M Simpson DP(("<<< kern_sem_wait leaving, pid=%d, error = %d\n",
868b2966a5aSBruce M Simpson (int)td->td_proc->p_pid, error));
869efaa6588SAlfred Perlstein return (error);
870efaa6588SAlfred Perlstein }
871efaa6588SAlfred Perlstein
872efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
873efaa6588SAlfred Perlstein struct ksem_getvalue_args {
874efaa6588SAlfred Perlstein semid_t id;
875efaa6588SAlfred Perlstein int *val;
876efaa6588SAlfred Perlstein };
877efaa6588SAlfred Perlstein #endif
878efaa6588SAlfred Perlstein int
sys_ksem_getvalue(struct thread * td,struct ksem_getvalue_args * uap)8798451d0ddSKip Macy sys_ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
880efaa6588SAlfred Perlstein {
8817008be5bSPawel Jakub Dawidek cap_rights_t rights;
8826bc1e9cdSJohn Baldwin struct file *fp;
883efaa6588SAlfred Perlstein struct ksem *ks;
884efaa6588SAlfred Perlstein int error, val;
885efaa6588SAlfred Perlstein
88615bcf785SRobert Watson AUDIT_ARG_FD(uap->id);
8877008be5bSPawel Jakub Dawidek error = ksem_get(td, uap->id,
8886b3a9a0fSMateusz Guzik cap_rights_init_one(&rights, CAP_SEM_GETVALUE), &fp);
8896bc1e9cdSJohn Baldwin if (error)
8906bc1e9cdSJohn Baldwin return (error);
8916bc1e9cdSJohn Baldwin ks = fp->f_data;
8926bc1e9cdSJohn Baldwin
893efaa6588SAlfred Perlstein mtx_lock(&sem_lock);
89452648411SRobert Watson #ifdef MAC
8956bc1e9cdSJohn Baldwin error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
89652648411SRobert Watson if (error) {
89752648411SRobert Watson mtx_unlock(&sem_lock);
8986bc1e9cdSJohn Baldwin fdrop(fp, td);
89952648411SRobert Watson return (error);
90052648411SRobert Watson }
90152648411SRobert Watson #endif
902efaa6588SAlfred Perlstein val = ks->ks_value;
9036bc1e9cdSJohn Baldwin vfs_timestamp(&ks->ks_atime);
904efaa6588SAlfred Perlstein mtx_unlock(&sem_lock);
9056bc1e9cdSJohn Baldwin fdrop(fp, td);
906efaa6588SAlfred Perlstein error = copyout(&val, uap->val, sizeof(val));
907efaa6588SAlfred Perlstein return (error);
908efaa6588SAlfred Perlstein }
909efaa6588SAlfred Perlstein
910efaa6588SAlfred Perlstein #ifndef _SYS_SYSPROTO_H_
911efaa6588SAlfred Perlstein struct ksem_destroy_args {
912efaa6588SAlfred Perlstein semid_t id;
913efaa6588SAlfred Perlstein };
914efaa6588SAlfred Perlstein #endif
915efaa6588SAlfred Perlstein int
sys_ksem_destroy(struct thread * td,struct ksem_destroy_args * uap)9168451d0ddSKip Macy sys_ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
917efaa6588SAlfred Perlstein {
9186bc1e9cdSJohn Baldwin struct file *fp;
919efaa6588SAlfred Perlstein struct ksem *ks;
920efaa6588SAlfred Perlstein int error;
921efaa6588SAlfred Perlstein
922a9d2f8d8SRobert Watson /* No capability rights required to close a semaphore. */
92315bcf785SRobert Watson AUDIT_ARG_FD(uap->id);
924cbd92ce6SMatt Macy error = ksem_get(td, uap->id, &cap_no_rights, &fp);
9256bc1e9cdSJohn Baldwin if (error)
9266bc1e9cdSJohn Baldwin return (error);
9276bc1e9cdSJohn Baldwin ks = fp->f_data;
9286bc1e9cdSJohn Baldwin if (!(ks->ks_flags & KS_ANONYMOUS)) {
9296bc1e9cdSJohn Baldwin fdrop(fp, td);
9306bc1e9cdSJohn Baldwin return (EINVAL);
931efaa6588SAlfred Perlstein }
9326bc1e9cdSJohn Baldwin mtx_lock(&sem_lock);
933efaa6588SAlfred Perlstein if (ks->ks_waiters != 0) {
9346bc1e9cdSJohn Baldwin mtx_unlock(&sem_lock);
935efaa6588SAlfred Perlstein error = EBUSY;
936efaa6588SAlfred Perlstein goto err;
937efaa6588SAlfred Perlstein }
9386bc1e9cdSJohn Baldwin ks->ks_flags |= KS_DEAD;
939efaa6588SAlfred Perlstein mtx_unlock(&sem_lock);
9406bc1e9cdSJohn Baldwin
9416bc1e9cdSJohn Baldwin error = kern_close(td, uap->id);
9426bc1e9cdSJohn Baldwin err:
9436bc1e9cdSJohn Baldwin fdrop(fp, td);
944efaa6588SAlfred Perlstein return (error);
945efaa6588SAlfred Perlstein }
946efaa6588SAlfred Perlstein
9470e5d5bc2SKonstantin Belousov static struct syscall_helper_data ksem_syscalls[] = {
9480e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_init),
9490e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_open),
9500e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_unlink),
9510e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_close),
9520e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_post),
9530e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_wait),
9540e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_timedwait),
9550e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_trywait),
9560e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_getvalue),
9570e5d5bc2SKonstantin Belousov SYSCALL_INIT_HELPER(ksem_destroy),
9580e5d5bc2SKonstantin Belousov SYSCALL_INIT_LAST
9590e5d5bc2SKonstantin Belousov };
9606bc1e9cdSJohn Baldwin
9610e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
9620e5d5bc2SKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
9630e5d5bc2SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h>
9640e5d5bc2SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h>
9650e5d5bc2SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h>
9660e5d5bc2SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
9676bc1e9cdSJohn Baldwin
9680e5d5bc2SKonstantin Belousov int
freebsd32_ksem_init(struct thread * td,struct freebsd32_ksem_init_args * uap)9690e5d5bc2SKonstantin Belousov freebsd32_ksem_init(struct thread *td, struct freebsd32_ksem_init_args *uap)
9700e5d5bc2SKonstantin Belousov {
9716bc1e9cdSJohn Baldwin
972265a4b83SBrooks Davis return (ksem_create(td, NULL, (semid_t *)uap->idp, S_IRWXU | S_IRWXG, uap->value,
9730e5d5bc2SKonstantin Belousov 0, 1));
9740e5d5bc2SKonstantin Belousov }
9750e5d5bc2SKonstantin Belousov
9760e5d5bc2SKonstantin Belousov int
freebsd32_ksem_open(struct thread * td,struct freebsd32_ksem_open_args * uap)9770e5d5bc2SKonstantin Belousov freebsd32_ksem_open(struct thread *td, struct freebsd32_ksem_open_args *uap)
9780e5d5bc2SKonstantin Belousov {
9790e5d5bc2SKonstantin Belousov
9800e5d5bc2SKonstantin Belousov if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
9810e5d5bc2SKonstantin Belousov return (EINVAL);
982265a4b83SBrooks Davis return (ksem_create(td, uap->name, (semid_t *)uap->idp, uap->mode, uap->value,
9830e5d5bc2SKonstantin Belousov uap->oflag, 1));
9840e5d5bc2SKonstantin Belousov }
9850e5d5bc2SKonstantin Belousov
9860e5d5bc2SKonstantin Belousov int
freebsd32_ksem_timedwait(struct thread * td,struct freebsd32_ksem_timedwait_args * uap)9870e5d5bc2SKonstantin Belousov freebsd32_ksem_timedwait(struct thread *td,
9880e5d5bc2SKonstantin Belousov struct freebsd32_ksem_timedwait_args *uap)
9890e5d5bc2SKonstantin Belousov {
9900e5d5bc2SKonstantin Belousov struct timespec32 abstime32;
9910e5d5bc2SKonstantin Belousov struct timespec *ts, abstime;
9920e5d5bc2SKonstantin Belousov int error;
9930e5d5bc2SKonstantin Belousov
9940e5d5bc2SKonstantin Belousov /*
9950e5d5bc2SKonstantin Belousov * We allow a null timespec (wait forever).
9960e5d5bc2SKonstantin Belousov */
9970e5d5bc2SKonstantin Belousov if (uap->abstime == NULL)
9980e5d5bc2SKonstantin Belousov ts = NULL;
9990e5d5bc2SKonstantin Belousov else {
10000e5d5bc2SKonstantin Belousov error = copyin(uap->abstime, &abstime32, sizeof(abstime32));
10010e5d5bc2SKonstantin Belousov if (error != 0)
10020e5d5bc2SKonstantin Belousov return (error);
10030e5d5bc2SKonstantin Belousov CP(abstime32, abstime, tv_sec);
10040e5d5bc2SKonstantin Belousov CP(abstime32, abstime, tv_nsec);
10050e5d5bc2SKonstantin Belousov if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
10060e5d5bc2SKonstantin Belousov return (EINVAL);
10070e5d5bc2SKonstantin Belousov ts = &abstime;
10080e5d5bc2SKonstantin Belousov }
10090e5d5bc2SKonstantin Belousov return (kern_sem_wait(td, uap->id, 0, ts));
10100e5d5bc2SKonstantin Belousov }
10110e5d5bc2SKonstantin Belousov
10120e5d5bc2SKonstantin Belousov static struct syscall_helper_data ksem32_syscalls[] = {
10130e5d5bc2SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_ksem_init),
10140e5d5bc2SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_ksem_open),
10158451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_unlink),
10168451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_close),
10178451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_post),
10188451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_wait),
10190e5d5bc2SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_ksem_timedwait),
10208451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_trywait),
10218451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_getvalue),
10228451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(ksem_destroy),
10230e5d5bc2SKonstantin Belousov SYSCALL_INIT_LAST
10240e5d5bc2SKonstantin Belousov };
10250e5d5bc2SKonstantin Belousov #endif
10266bc1e9cdSJohn Baldwin
1027590f242cSRobert Watson static int
ksem_module_init(void)10286bc1e9cdSJohn Baldwin ksem_module_init(void)
1029590f242cSRobert Watson {
10306bc1e9cdSJohn Baldwin int error;
1031590f242cSRobert Watson
10326bc1e9cdSJohn Baldwin mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
10336bc1e9cdSJohn Baldwin mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
10346bc1e9cdSJohn Baldwin sx_init(&ksem_dict_lock, "ksem dictionary");
10356bc1e9cdSJohn Baldwin ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
10362e7758a8SJohn Baldwin p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 200112L);
10376bc1e9cdSJohn Baldwin p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
10386bc1e9cdSJohn Baldwin p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
1039590f242cSRobert Watson
1040e015b1abSMateusz Guzik error = syscall_helper_register(ksem_syscalls, SY_THR_STATIC_KLD);
10410e5d5bc2SKonstantin Belousov if (error)
10420e5d5bc2SKonstantin Belousov return (error);
10430e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
1044e015b1abSMateusz Guzik error = syscall32_helper_register(ksem32_syscalls, SY_THR_STATIC_KLD);
10450e5d5bc2SKonstantin Belousov if (error)
10460e5d5bc2SKonstantin Belousov return (error);
10470e5d5bc2SKonstantin Belousov #endif
10486bc1e9cdSJohn Baldwin return (0);
1049590f242cSRobert Watson }
1050590f242cSRobert Watson
1051c3053131SPoul-Henning Kamp static void
ksem_module_destroy(void)10526bc1e9cdSJohn Baldwin ksem_module_destroy(void)
1053993182e5SAlexander Leidinger {
1054993182e5SAlexander Leidinger
10550e5d5bc2SKonstantin Belousov #ifdef COMPAT_FREEBSD32
10560e5d5bc2SKonstantin Belousov syscall32_helper_unregister(ksem32_syscalls);
10570e5d5bc2SKonstantin Belousov #endif
10580e5d5bc2SKonstantin Belousov syscall_helper_unregister(ksem_syscalls);
1059efaa6588SAlfred Perlstein
10602e7758a8SJohn Baldwin p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 0);
10616bc1e9cdSJohn Baldwin hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
10626bc1e9cdSJohn Baldwin sx_destroy(&ksem_dict_lock);
10636bc1e9cdSJohn Baldwin mtx_destroy(&ksem_count_lock);
10646bc1e9cdSJohn Baldwin mtx_destroy(&sem_lock);
106532863754SKonstantin Belousov p31b_unsetcfg(CTL_P1003_1B_SEM_VALUE_MAX);
106632863754SKonstantin Belousov p31b_unsetcfg(CTL_P1003_1B_SEM_NSEMS_MAX);
1067efaa6588SAlfred Perlstein }
1068efaa6588SAlfred Perlstein
1069efaa6588SAlfred Perlstein static int
sem_modload(struct module * module,int cmd,void * arg)1070efaa6588SAlfred Perlstein sem_modload(struct module *module, int cmd, void *arg)
1071efaa6588SAlfred Perlstein {
1072efaa6588SAlfred Perlstein int error = 0;
1073efaa6588SAlfred Perlstein
1074efaa6588SAlfred Perlstein switch (cmd) {
1075efaa6588SAlfred Perlstein case MOD_LOAD:
10766bc1e9cdSJohn Baldwin error = ksem_module_init();
10776bc1e9cdSJohn Baldwin if (error)
10786bc1e9cdSJohn Baldwin ksem_module_destroy();
1079efaa6588SAlfred Perlstein break;
10808e230e30SRobert Watson
1081efaa6588SAlfred Perlstein case MOD_UNLOAD:
10826bc1e9cdSJohn Baldwin mtx_lock(&ksem_count_lock);
1083efaa6588SAlfred Perlstein if (nsems != 0) {
1084efaa6588SAlfred Perlstein error = EOPNOTSUPP;
10856bc1e9cdSJohn Baldwin mtx_unlock(&ksem_count_lock);
1086efaa6588SAlfred Perlstein break;
1087efaa6588SAlfred Perlstein }
10886bc1e9cdSJohn Baldwin ksem_dead = 1;
10896bc1e9cdSJohn Baldwin mtx_unlock(&ksem_count_lock);
10906bc1e9cdSJohn Baldwin ksem_module_destroy();
1091efaa6588SAlfred Perlstein break;
10928e230e30SRobert Watson
1093efaa6588SAlfred Perlstein case MOD_SHUTDOWN:
1094efaa6588SAlfred Perlstein break;
1095efaa6588SAlfred Perlstein default:
1096efaa6588SAlfred Perlstein error = EINVAL;
1097efaa6588SAlfred Perlstein break;
1098efaa6588SAlfred Perlstein }
1099efaa6588SAlfred Perlstein return (error);
1100efaa6588SAlfred Perlstein }
1101efaa6588SAlfred Perlstein
1102efaa6588SAlfred Perlstein static moduledata_t sem_mod = {
1103efaa6588SAlfred Perlstein "sem",
1104efaa6588SAlfred Perlstein &sem_modload,
1105efaa6588SAlfred Perlstein NULL
1106efaa6588SAlfred Perlstein };
1107efaa6588SAlfred Perlstein
1108efaa6588SAlfred Perlstein DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
1109efaa6588SAlfred Perlstein MODULE_VERSION(sem, 1);
1110