34 #include <sys/cdefs.h>
37 #include "opt_compat.h"
38 #include "opt_posix.h"
40 #include <sys/param.h>
41 #include <sys/capability.h>
42 #include <sys/condvar.h>
43 #include <sys/fcntl.h>
45 #include <sys/filedesc.h>
46 #include <sys/fnv_hash.h>
47 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
55 #include <sys/posix4.h>
56 #include <sys/_semaphore.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>
65 #include <sys/vnode.h>
67 #include <security/mac/mac_framework.h>
69 FEATURE(p1003_1b_semaphores,
"POSIX P1003.1B semaphores support");
83 #define DP(x) printf x
95 static
MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
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;
103 #define KSEM_HASH(fnv) (&ksem_dictionary[(fnv) & ksem_hash])
105 static int nsems = 0;
107 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
108 "Number of active kernel POSIX semaphores");
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);
116 semid_t *semidp, mode_t
mode,
unsigned int value,
117 int flags,
int compat32);
119 static int ksem_get(
struct thread *td, semid_t
id, cap_rights_t rights,
121 static struct ksem *
ksem_hold(
struct ksem *ks);
141 static struct fileops ksem_ops = {
152 .fo_flags = DFLAG_PASSABLE
155 FEATURE(posix_sem,
"POSIX semaphores");
158 ksem_read(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
159 int flags,
struct thread *td)
166 ksem_write(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
167 int flags,
struct thread *td)
183 struct ucred *active_cred,
struct thread *td)
190 ksem_poll(
struct file *fp,
int events,
struct ucred *active_cred,
205 ksem_stat(
struct file *fp,
struct stat *sb,
struct ucred *active_cred,
216 error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
225 bzero(sb,
sizeof(*sb));
228 sb->st_atim = ks->ks_atime;
229 sb->st_ctim = ks->ks_ctime;
230 sb->st_mtim = ks->ks_mtime;
231 sb->st_birthtim = ks->ks_birthtime;
232 sb->st_uid = ks->ks_uid;
233 sb->st_gid = ks->ks_gid;
234 sb->st_mode = S_IFREG | ks->ks_mode;
235 mtx_unlock(&sem_lock);
251 error = mac_posixsem_check_setmode(active_cred, ks, mode);
255 error =
vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid, VADMIN,
259 ks->ks_mode = mode & ACCESSPERMS;
261 mtx_unlock(&sem_lock);
266 ksem_chown(
struct file *fp, uid_t uid, gid_t gid,
struct ucred *active_cred,
276 error = mac_posixsem_check_setowner(active_cred, ks, uid, gid);
280 if (uid == (uid_t)-1)
282 if (gid == (gid_t)-1)
284 if (((uid != ks->ks_uid && uid != active_cred->cr_uid) ||
285 (gid != ks->ks_gid && !
groupmember(gid, active_cred))) &&
291 mtx_unlock(&sem_lock);
316 mtx_lock(&ksem_count_lock);
317 if (nsems ==
p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
318 mtx_unlock(&ksem_count_lock);
322 mtx_unlock(&ksem_count_lock);
323 ks =
malloc(
sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
324 ks->ks_uid = ucred->cr_uid;
325 ks->ks_gid = ucred->cr_gid;
327 ks->ks_value =
value;
330 ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
331 refcount_init(&ks->ks_ref, 1);
333 mac_posixsem_init(ks);
334 mac_posixsem_create(ucred, ks);
344 refcount_acquire(&ks->ks_ref);
352 if (refcount_release(&ks->ks_ref)) {
354 mac_posixsem_destroy(ks);
358 mtx_lock(&ksem_count_lock);
360 mtx_unlock(&ksem_count_lock);
373 error =
vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
374 VREAD | VWRITE, ucred, NULL);
390 LIST_FOREACH(map,
KSEM_HASH(fnv), km_link) {
393 if (strcmp(map->
km_path, path) == 0)
410 LIST_INSERT_HEAD(
KSEM_HASH(fnv), map, km_link);
419 LIST_FOREACH(map,
KSEM_HASH(fnv), km_link) {
422 if (strcmp(map->
km_path, path) == 0) {
424 error = mac_posixsem_check_unlink(ucred, map->
km_ksem);
432 LIST_REMOVE(map, km_link);
447 if (ks->ks_path == NULL)
449 sx_slock(&ksem_dict_lock);
450 if (ks->ks_path != NULL)
451 strlcpy(path, ks->ks_path, size);
453 *value = ks->ks_value;
454 sx_sunlock(&ksem_dict_lock);
472 ptrs =
sizeof(semid32);
477 ptrs =
sizeof(semid);
483 return (copyout(ptr, semidp, ptrs));
489 unsigned int value,
int flags,
int compat32)
491 struct filedesc *fdp;
498 if (value > SEM_VALUE_MAX)
501 fdp = td->td_proc->p_fd;
502 mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
503 error =
falloc(td, &fp, &fd, O_CLOEXEC);
528 ks->ks_flags |= KS_ANONYMOUS;
530 path =
malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
531 error = copyinstr(name, path, MAXPATHLEN, NULL);
534 if (error == 0 && path[0] !=
'/')
543 fnv = fnv_32_str(path, FNV1_32_INIT);
544 sx_xlock(&ksem_dict_lock);
548 if (flags & O_CREAT) {
563 if ((flags & (O_CREAT | O_EXCL)) ==
568 error = mac_posixsem_check_open(td->td_ucred,
581 sx_xunlock(&ksem_dict_lock);
587 KASSERT(ks == NULL, (
"ksem_create error with a ksem"));
592 KASSERT(ks != NULL, (
"ksem_create w/o a ksem"));
594 finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
602 ksem_get(
struct thread *td, semid_t
id, cap_rights_t rights,
struct file **fpp)
608 error =
fget(td,
id, rights, &fp);
611 if (fp->f_type != DTYPE_SEM) {
616 if (ks->ks_flags & KS_DEAD) {
625 #ifndef _SYS_SYSPROTO_H_
639 #ifndef _SYS_SYSPROTO_H_
652 DP((
">>> ksem_open start, pid=%d\n", (
int)td->td_proc->p_pid));
654 if ((uap->
oflag & ~(O_CREAT | O_EXCL)) != 0)
660 #ifndef _SYS_SYSPROTO_H_
672 path =
malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
673 error = copyinstr(uap->
name, path, MAXPATHLEN, NULL);
679 fnv = fnv_32_str(path, FNV1_32_INIT);
680 sx_xlock(&ksem_dict_lock);
682 sx_xunlock(&ksem_dict_lock);
688 #ifndef _SYS_SYSPROTO_H_
705 if (ks->ks_flags & KS_ANONYMOUS) {
714 #ifndef _SYS_SYSPROTO_H_
726 error =
ksem_get(td, uap->
id, CAP_SEM_POST, &fp);
733 error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
737 if (ks->ks_value == SEM_VALUE_MAX) {
742 if (ks->ks_waiters > 0)
747 mtx_unlock(&sem_lock);
752 #ifndef _SYS_SYSPROTO_H_
764 #ifndef _SYS_SYSPROTO_H_
773 struct timespec abstime;
783 error = copyin(uap->
abstime, &abstime,
sizeof(abstime));
786 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
793 #ifndef _SYS_SYSPROTO_H_
807 struct timespec *abstime)
809 struct timespec ts1, ts2;
815 DP((
">>> kern_sem_wait entered! pid=%d\n", (
int)td->td_proc->p_pid));
816 error =
ksem_get(td,
id, CAP_SEM_WAIT, &fp);
821 DP((
">>> kern_sem_wait critical section entered! pid=%d\n",
822 (
int)td->td_proc->p_pid));
824 error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
826 DP((
"kern_sem_wait mac failed\n"));
830 DP((
"kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
832 while (ks->ks_value == 0) {
836 else if (abstime == NULL)
837 error = cv_wait_sig(&ks->ks_cv, &sem_lock);
842 timespecsub(&ts1, &ts2);
843 TIMESPEC_TO_TIMEVAL(&tv, &ts1);
848 error = cv_timedwait_sig(&ks->ks_cv,
850 if (error != EWOULDBLOCK)
859 DP((
"kern_sem_wait value post-decrement = %d\n", ks->ks_value));
862 mtx_unlock(&sem_lock);
864 DP((
"<<< kern_sem_wait leaving, pid=%d, error = %d\n",
865 (
int)td->td_proc->p_pid, error));
869 #ifndef _SYS_SYSPROTO_H_
882 error =
ksem_get(td, uap->
id, CAP_SEM_GETVALUE, &fp);
889 error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
891 mtx_unlock(&sem_lock);
898 mtx_unlock(&sem_lock);
900 error = copyout(&val, uap->
val,
sizeof(val));
904 #ifndef _SYS_SYSPROTO_H_
921 if (!(ks->ks_flags & KS_ANONYMOUS)) {
926 if (ks->ks_waiters != 0) {
927 mtx_unlock(&sem_lock);
931 ks->ks_flags |= KS_DEAD;
932 mtx_unlock(&sem_lock);
941 SYSCALL_INIT_HELPER(ksem_init),
942 SYSCALL_INIT_HELPER(ksem_open),
943 SYSCALL_INIT_HELPER(ksem_unlink),
944 SYSCALL_INIT_HELPER(ksem_close),
945 SYSCALL_INIT_HELPER(ksem_post),
946 SYSCALL_INIT_HELPER(ksem_wait),
947 SYSCALL_INIT_HELPER(ksem_timedwait),
948 SYSCALL_INIT_HELPER(ksem_trywait),
949 SYSCALL_INIT_HELPER(ksem_getvalue),
950 SYSCALL_INIT_HELPER(ksem_destroy),
955 #include <compat/compat32bit/compat32bit.h>
956 #include <compat/compat32bit/compat32bit_proto.h>
957 #include <compat/compat32bit/compat32bit_signal.h>
958 #include <compat/compat32bit/compat32bit_syscall.h>
959 #include <compat/compat32bit/compat32bit_util.h>
962 compat32bit_ksem_init(
struct thread *td,
struct compat32bit_ksem_init_args *uap)
965 return (
ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
970 compat32bit_ksem_open(
struct thread *td,
struct compat32bit_ksem_open_args *uap)
973 if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
975 return (
ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
980 compat32bit_ksem_timedwait(
struct thread *td,
981 struct compat32bit_ksem_timedwait_args *uap)
983 struct timespec32 abstime32;
984 struct timespec *
ts, abstime;
990 if (uap->abstime == NULL)
993 error = copyin(uap->abstime, &abstime32,
sizeof(abstime32));
996 CP(abstime32, abstime, tv_sec);
997 CP(abstime32, abstime, tv_nsec);
998 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
1005 static struct syscall_helper_data ksem32_syscalls[] = {
1006 SYSCALL32_INIT_HELPER(compat32bit_ksem_init),
1007 SYSCALL32_INIT_HELPER(compat32bit_ksem_open),
1008 SYSCALL32_INIT_HELPER_COMPAT(ksem_unlink),
1009 SYSCALL32_INIT_HELPER_COMPAT(ksem_close),
1010 SYSCALL32_INIT_HELPER_COMPAT(ksem_post),
1011 SYSCALL32_INIT_HELPER_COMPAT(ksem_wait),
1012 SYSCALL32_INIT_HELPER(compat32bit_ksem_timedwait),
1013 SYSCALL32_INIT_HELPER_COMPAT(ksem_trywait),
1014 SYSCALL32_INIT_HELPER_COMPAT(ksem_getvalue),
1015 SYSCALL32_INIT_HELPER_COMPAT(ksem_destroy),
1025 mtx_init(&sem_lock,
"sem", NULL, MTX_DEF);
1026 mtx_init(&ksem_count_lock,
"ksem count", NULL, MTX_DEF);
1027 sx_init(&ksem_dict_lock,
"ksem dictionary");
1028 ksem_dictionary =
hashinit(1024, M_KSEM, &ksem_hash);
1031 p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
1038 error = syscall32_helper_register(ksem32_syscalls);
1050 syscall32_helper_unregister(ksem32_syscalls);
1077 mtx_lock(&ksem_count_lock);
1080 mtx_unlock(&ksem_count_lock);
1084 mtx_unlock(&ksem_count_lock);
static int ksem_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
int syscall_helper_unregister(struct syscall_helper_data *sd)
static int ksem_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
int tvtohz(struct timeval *tv)
static void ksem_drop(struct ksem *ks)
DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST)
int kern_close(struct thread *td, int fd)
void * hashinit(int elements, struct malloc_type *type, u_long *hashmask)
int syscall_helper_register(struct syscall_helper_data *sd)
int priv_check_cred(struct ucred *cred, int priv, int flags)
static MALLOC_DEFINE(M_KSEM,"ksem","semaphore file descriptor")
static int sem_modload(struct module *module, int cmd, void *arg)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
static int ksem_get(struct thread *td, semid_t id, cap_rights_t rights, struct file **fpp)
static void ksem_info_impl(struct ksem *ks, char *path, size_t size, uint32_t *value)
void cv_destroy(struct cv *cvp)
int sys_ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
static int ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
static struct ksem * ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
void knote(struct knlist *list, long hint, int lockflags)
int sys_ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
static struct ksem * ksem_lookup(char *path, Fnv32_t fnv)
int sys_ksem_init(struct thread *td, struct ksem_init_args *uap)
SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD,&boothowto, 0,"Boot control flags, passed from loader")
void cv_signal(struct cv *cvp)
int vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, accmode_t accmode, struct ucred *cred, int *privused)
int fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
static int ksem_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
static LIST_HEAD(ksem_mapping)
int sys_ksem_close(struct thread *td, struct ksem_close_args *uap)
void(* ksem_info)(struct ksem *ks, char *path, size_t size, uint32_t *value)
void vfs_timestamp(struct timespec *tsp)
static int ksem_access(struct ksem *ks, struct ucred *ucred)
static struct ksem * ksem_hold(struct ksem *ks)
static int ksem_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
void p31b_setcfg(int num, int value)
static int ksem_kqfilter(struct file *fp, struct knote *kn)
static struct syscall_helper_data ksem_syscalls[]
static int ksem_create_copyout_semid(struct thread *td, semid_t *semidp, int fd, int compat32)
static void ksem_module_destroy(void)
static int ksem_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
static int ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
static int ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode, unsigned int value, int flags, int compat32)
void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
void getnanotime(struct timespec *tsp)
int sys_ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
void cv_init(struct cv *cvp, const char *desc)
int groupmember(gid_t gid, struct ucred *cred)
void free(void *addr, struct malloc_type *mtp)
int sys_ksem_post(struct thread *td, struct ksem_post_args *uap)
int sys_ksem_wait(struct thread *td, struct ksem_wait_args *uap)
static int ksem_closef(struct file *fp, struct thread *td)
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
const struct timespec * abstime
void p31b_unsetcfg(int num)
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
static void ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
void hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
static int ksem_module_init(void)
static int ksem_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
static int kern_sem_wait(struct thread *td, semid_t id, int tryflag, struct timespec *abstime)
void mtx_destroy(struct mtx *m)
static moduledata_t sem_mod
static int ksem_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
int sys_ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
int sys_ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
FEATURE(p1003_1b_semaphores,"POSIX P1003.1B semaphores support")
int sys_ksem_open(struct thread *td, struct ksem_open_args *uap)
void sx_destroy(struct sx *sx)