56#include "opt_capsicum.h"
59#include <sys/kernel.h>
61#include <sys/limits.h>
62#include <sys/malloc.h>
64#include <sys/capsicum.h>
65#include <sys/dirent.h>
67#include <sys/eventhandler.h>
70#include <sys/filedesc.h>
73#include <sys/module.h>
75#include <sys/mqueue.h>
78#include <sys/posix4.h>
83#include <sys/sysproto.h>
85#include <sys/syscall.h>
86#include <sys/syscallsubr.h>
87#include <sys/sysent.h>
89#include <sys/sysctl.h>
90#include <sys/taskqueue.h>
91#include <sys/unistd.h>
94#include <machine/atomic.h>
96#include <security/audit/audit.h>
98FEATURE(p1003_1b_mqueue,
"POSIX P1003.1B message queues support");
103#define MQFS_NAMELEN NAME_MAX
104#define MQFS_DELEN (8 + MQFS_NAMELEN)
131 struct vnode *mv_vnode;
145 const void *mn_pr_root;
151 struct timespec mn_birth;
152 struct timespec mn_ctime;
153 struct timespec mn_atime;
154 struct timespec mn_mtime;
160#define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
161#define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
162#define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
163#define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
164 (fp)->f_data)->mn_data))
172 struct sigevent nt_sigev;
174 struct proc *nt_proc;
197 unsigned int msg_prio;
198 unsigned int msg_size;
203 "POSIX real time message queue");
210 &
maxmsg, 0,
"Default maximum messages in queue");
213 &
maxmsgsize, 0,
"Default maximum message size");
216 &
maxmq, 0,
"maximum message queues");
219 &
curmq, 0,
"current message queue number");
240 const char *
name,
int namelen,
struct ucred *cred,
int mode);
242 const char *
name,
int namelen,
struct ucred *cred,
int mode);
246 const char *
name,
int namelen,
struct ucred *cred,
int mode);
259 size_t msg_len,
unsigned msg_prio,
int waitok,
260 const struct timespec *abs_timeout);
262 size_t msg_len,
unsigned *msg_prio,
int waitok,
263 const struct timespec *abs_timeout);
325 switch (mn->mn_type) {
334 (
"mqfstype_this node has no parent"));
335 mn->mn_fileno = mn->
mn_parent->mn_fileno;
339 (
"mqfstype_parent node has no parent"));
341 mn->mn_fileno = mn->
mn_parent->mn_fileno;
345 (
"mqfstype_parent node has no grandparent"));
350 (
"mqfs_fileno_alloc() called for unknown type node: %d",
362 switch (mn->mn_type) {
375 (
"mqfs_fileno_free() called for unknown type node: %d",
396 atomic_add_int(&node->mn_refcount, 1);
406 old = atomic_fetchadd_int(&node->mn_refcount, -1);
413 int locked = sx_xlocked(&mqfs->
mi_lock);
428 KASSERT(
parent != NULL, (
"%s(): parent is NULL", __func__));
429 KASSERT(
parent->mn_info != NULL,
430 (
"%s(): parent has no mn_info", __func__));
433 (
"%s(): parent is not a directory", __func__));
437 LIST_INIT(&node->mn_children);
438 LIST_INIT(&node->mn_vnodes);
439 LIST_INSERT_HEAD(&
parent->mn_children, node, mn_sibling);
452 node->mn_pr_root = cred->cr_prison->pr_root;
453 node->mn_type = nodetype;
454 node->mn_refcount = 1;
456 node->mn_ctime = node->mn_atime = node->mn_mtime
458 node->mn_uid = cred->cr_uid;
459 node->mn_gid = cred->cr_gid;
460 node->mn_mode =
mode;
469 struct ucred *cred,
int mode)
492 dir->mn_refcount = 1;
501 dir->mn_refcount = 1;
518 struct ucred *cred,
int mode)
540 struct ucred *cred,
int mode)
562 KASSERT(node != NULL,
563 (
"%s(): node is NULL", __func__));
565 (
"%s(): node has no mn_info", __func__));
569 while (! LIST_EMPTY(&node->mn_children))
575 (
"%s(): parent has different mn_info", __func__));
576 LIST_REMOVE(node, mn_sibling);
579 if (node->mn_fileno != 0)
581 if (node->mn_data != NULL)
595 if (mp->mnt_flag & MNT_UPDATE)
600 mp->mnt_flag |= MNT_LOCAL;
606 sbp->f_bsize = PAGE_SIZE;
607 sbp->f_iosize = PAGE_SIZE;
624 error =
vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
661 osd_method_t methods[PR_MAXMETHOD] = {
666 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
668 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
671 NULL, UMA_ALIGN_PTR, 0);
673 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
675 sx_init(&mi->
mi_lock,
"mqfs lock");
680 LIST_INIT(&root->mn_children);
681 LIST_INIT(&root->mn_vnodes);
687 EVENTHANDLER_PRI_ANY);
689 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
705 EVENTHANDLER_DEREGISTER(process_exit,
exit_tag);
724 struct vnode *vp = (
struct vnode *)context;
726 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
740 struct vnode *newvpp;
746 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
747 if (vd->mv_vnode->v_mount == mp) {
757 error =
vget(*vpp, LK_RETRY | LK_EXCLUSIVE);
766 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
776 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
777 if (vd->mv_vnode->v_mount == mp) {
794 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
796 switch (pn->mn_type) {
798 (*vpp)->v_vflag = VV_ROOT;
803 (*vpp)->v_type = VDIR;
806 (*vpp)->v_type = VREG;
809 (*vpp)->v_type = VLNK;
812 KASSERT(0, (
"mqfs_allocf called for null node\n"));
814 panic(
"%s has unexpected type: %d", pn->
mn_name, pn->mn_type);
830 pr_root = cred->cr_prison->pr_root;
831 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
833 if ((pn->mn_pr_root == NULL || pn->mn_pr_root == pr_root) &&
847 struct componentname *cnp;
848 struct vnode *dvp, **vpp;
852 int nameiop,
flags, error, namelen;
860 pname = cnp->cn_nameptr;
861 namelen = cnp->cn_namelen;
862 flags = cnp->cn_flags;
863 nameiop = cnp->cn_nameiop;
869 if (dvp->v_type != VDIR)
872 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
881 if (namelen == 1 && pname[0] ==
'.') {
882 if ((
flags & ISLASTCN) && nameiop != LOOKUP)
891 if (cnp->cn_flags & ISDOTDOT) {
892 if (dvp->v_vflag & VV_ROOT)
894 if ((
flags & ISLASTCN) && nameiop != LOOKUP)
897 KASSERT(pd->
mn_parent, (
"non-root directory has no parent"));
900 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
906 pn =
mqfs_search(pd, pname, namelen, cnp->cn_cred);
914 if (nameiop == DELETE && (
flags & ISLASTCN)) {
915 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
931 if (error == 0 && cnp->cn_flags & MAKEENTRY)
932 cache_enter(dvp, *vpp, cnp);
939 if ((nameiop == CREATE || nameiop == RENAME) && (
flags & LOCKPARENT)
940 && (
flags & ISLASTCN)) {
941 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
944 cnp->cn_flags |= SAVENAME;
945 return (EJUSTRETURN);
951struct vop_lookup_args {
952 struct vop_generic_args a_gen;
954 struct vnode **a_vpp;
955 struct componentname *a_cnp;
972struct vop_create_args {
974 struct vnode **a_vpp;
975 struct componentname *a_cnp;
987 struct componentname *cnp = ap->a_cnp;
993 pd =
VTON(ap->a_dvp);
1000 if ((cnp->cn_flags & HASBUF) == 0)
1001 panic(
"%s: no name", __func__);
1003 cnp->cn_cred, ap->a_vap->va_mode);
1010 error =
mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1034 if (ucred->cr_uid != pn->mn_uid &&
1037 else if (!pn->mn_deleted) {
1041 LIST_REMOVE(pn, mn_sibling);
1042 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1044 vhold(vd->mv_vnode);
1055struct vop_remove_args {
1056 struct vnode *a_dvp;
1058 struct componentname *a_cnp;
1072 if (ap->a_vp->v_type == VDIR)
1074 pn =
VTON(ap->a_vp);
1076 error =
do_unlink(pn, ap->a_cnp->cn_cred);
1082struct vop_inactive_args {
1084 struct thread *a_td;
1099struct vop_reclaim_args {
1100 struct vop_generic_args a_gen;
1109 struct vnode *vp = ap->a_vp;
1117 LIST_REMOVE(vd, mv_link);
1125struct vop_open_args {
1126 struct vop_generic_args a_gen;
1129 struct ucred *a_cred;
1130 struct thread *a_td;
1142struct vop_close_args {
1143 struct vop_generic_args a_gen;
1146 struct ucred *a_cred;
1147 struct thread *a_td;
1158struct vop_access_args {
1159 struct vop_generic_args a_gen;
1161 accmode_t a_accmode;
1162 struct ucred *a_cred;
1163 struct thread *a_td;
1173 struct vnode *vp = ap->a_vp;
1177 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1180 error =
vaccess(vp->v_type, vattr.va_mode, vattr.va_uid, vattr.va_gid,
1181 ap->a_accmode, ap->a_cred);
1186struct vop_getattr_args {
1187 struct vop_generic_args a_gen;
1189 struct vattr *a_vap;
1190 struct ucred *a_cred;
1200 struct vnode *vp = ap->a_vp;
1202 struct vattr *vap = ap->a_vap;
1205 vap->va_type = vp->v_type;
1206 vap->va_mode = pn->mn_mode;
1208 vap->va_uid = pn->mn_uid;
1209 vap->va_gid = pn->mn_gid;
1210 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1211 vap->va_fileid = pn->mn_fileno;
1213 vap->va_blocksize = PAGE_SIZE;
1214 vap->va_bytes = vap->va_size = 0;
1215 vap->va_atime = pn->mn_atime;
1216 vap->va_mtime = pn->mn_mtime;
1217 vap->va_ctime = pn->mn_ctime;
1218 vap->va_birthtime = pn->mn_birth;
1221 vap->va_rdev = NODEV;
1223 vap->va_filerev = 0;
1228struct vop_setattr_args {
1229 struct vop_generic_args a_gen;
1231 struct vattr *a_vap;
1232 struct ucred *a_cred;
1252 if ((vap->va_type != VNON) ||
1253 (vap->va_nlink != VNOVAL) ||
1254 (vap->va_fsid != VNOVAL) ||
1255 (vap->va_fileid != VNOVAL) ||
1256 (vap->va_blocksize != VNOVAL) ||
1257 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1258 (vap->va_rdev != VNOVAL) ||
1259 ((
int)vap->va_bytes != VNOVAL) ||
1260 (vap->va_gen != VNOVAL)) {
1267 if (vap->va_uid == (uid_t)VNOVAL)
1271 if (vap->va_gid == (gid_t)VNOVAL)
1276 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1281 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1289 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1290 (gid != pn->mn_gid && !
groupmember(gid, ap->a_cred))) &&
1291 (error =
priv_check(td, PRIV_MQ_ADMIN)) != 0)
1298 if (vap->va_mode != (mode_t)VNOVAL) {
1299 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1302 pn->mn_mode = vap->va_mode;
1306 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1308 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1309 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1310 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1312 if (vap->va_atime.tv_sec != VNOVAL) {
1313 pn->mn_atime = vap->va_atime;
1315 if (vap->va_mtime.tv_sec != VNOVAL) {
1316 pn->mn_mtime = vap->va_mtime;
1327struct vop_read_args {
1328 struct vop_generic_args a_gen;
1332 struct ucred *a_cred;
1343 struct vnode *vp = ap->a_vp;
1344 struct uio *uio = ap->a_uio;
1348 if (vp->v_type != VREG)
1353 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1358 buf[
sizeof(
buf)-1] =
'\0';
1365struct vop_readdir_args {
1366 struct vop_generic_args a_gen;
1369 struct ucred *a_cred;
1372 uint64_t **a_cookies;
1386 struct dirent entry;
1388 const void *pr_root;
1389 int *tmp_ncookies = NULL;
1398 if (vp->v_type != VDIR)
1401 if (uio->uio_offset < 0)
1404 if (ap->a_ncookies != NULL) {
1405 tmp_ncookies = ap->a_ncookies;
1406 *ap->a_ncookies = 0;
1407 ap->a_ncookies = NULL;
1413 pr_root = ap->a_cred->cr_prison->pr_root;
1416 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1417 entry.d_reclen =
sizeof(entry);
1423 if (pn->mn_pr_root != NULL && pn->mn_pr_root != pr_root)
1427 entry.d_fileno = pn->mn_fileno;
1428 entry.d_off = offset + entry.d_reclen;
1430 entry.d_name[i] = pn->
mn_name[i];
1432 switch (pn->mn_type) {
1437 entry.d_type = DT_DIR;
1440 entry.d_type = DT_REG;
1443 entry.d_type = DT_LNK;
1449 dirent_terminate(&entry);
1450 if (entry.d_reclen > uio->uio_resid)
1452 if (offset >= uio->uio_offset) {
1457 offset += entry.d_reclen;
1461 uio->uio_offset = offset;
1463 if (tmp_ncookies != NULL)
1464 ap->a_ncookies = tmp_ncookies;
1472struct vop_mkdir_args {
1473 struct vnode *a_dvp;
1474 struvt vnode **a_vpp;
1475 struvt componentname *a_cnp;
1476 struct vattr *a_vap;
1484mqfs_mkdir(
struct vop_mkdir_args *ap)
1487 struct componentname *cnp = ap->a_cnp;
1495 if ((cnp->cn_flags & HASBUF) == 0)
1496 panic(
"%s: no name", __func__);
1497 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1498 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1505 error =
mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1512struct vop_rmdir_args {
1513 struct vnode *a_dvp;
1515 struct componentname *a_cnp;
1523mqfs_rmdir(
struct vop_rmdir_args *ap)
1533 if (pn->mn_deleted) {
1538 pt = LIST_FIRST(&pn->mn_children);
1539 pt = LIST_NEXT(pt, mn_sibling);
1540 pt = LIST_NEXT(pt, mn_sibling);
1548 LIST_REMOVE(pn, mn_sibling);
1564 const struct prison *
pr = obj;
1567 struct vnode *pr_root;
1569 pr_root =
pr->pr_root;
1570 if (
pr->pr_parent->pr_root == pr_root)
1572 TAILQ_FOREACH(tpr, &
allprison, pr_list) {
1573 if (tpr !=
pr && tpr->pr_root == pr_root)
1583 if (pn->mn_pr_root == pr_root)
1584 (void)
do_unlink(pn, curthread->td_ucred);
1609 mtx_init(&mq->
mq_mutex,
"mqueue lock", NULL, MTX_DEF);
1612 atomic_add_int(&
curmq, 1);
1624 while ((msg = TAILQ_FIRST(&mq->
mq_msgq)) != NULL) {
1625 TAILQ_REMOVE(&mq->
mq_msgq, msg, msg_link);
1626 free(msg, M_MQUEUEDATA);
1635 atomic_add_int(&
curmq, -1);
1649 msg =
malloc(len, M_MQUEUEDATA, M_WAITOK);
1650 error = copyin(msg_ptr, ((
char *)msg) +
sizeof(
struct mqueue_msg),
1653 free(msg, M_MQUEUEDATA);
1656 msg->msg_size = msg_size;
1657 msg->msg_prio = msg_prio;
1670 error = copyout(((
char *)msg) +
sizeof(*msg), msg_ptr,
1672 if (error == 0 && msg_prio != NULL)
1673 error = copyout(&msg->msg_prio, msg_prio,
sizeof(
int));
1683 free(msg, M_MQUEUEDATA);
1693 size_t msg_len,
unsigned msg_prio,
int waitok,
1694 const struct timespec *abs_timeout)
1697 struct timespec
ts, ts2;
1701 if (msg_prio >= MQ_PRIO_MAX)
1718 if (abs_timeout == NULL) {
1730 if (error != EAGAIN)
1733 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1739 timespecsub(abs_timeout, &
ts, &ts2);
1740 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1744 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1746 if (error != ETIMEDOUT)
1773 PCATCH,
"mqsend", timo);
1775 if (error == EAGAIN)
1783 if (TAILQ_EMPTY(&mq->
mq_msgq)) {
1784 TAILQ_INSERT_HEAD(&mq->
mq_msgq, msg, msg_link);
1786 if (msg->msg_prio <= TAILQ_LAST(&mq->
mq_msgq, msgq)->msg_prio) {
1787 TAILQ_INSERT_TAIL(&mq->
mq_msgq, msg, msg_link);
1789 TAILQ_FOREACH(msg2, &mq->
mq_msgq, msg_link) {
1790 if (msg2->msg_prio < msg->msg_prio)
1793 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1806 KNOTE_LOCKED(&mq->
mq_rsel.si_note, 0);
1823 mtx_assert(&mq->
mq_mutex, MA_OWNED);
1825 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1832 if (!KSI_ONQ(&nt->nt_ksi)) {
1833 ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1834 tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1848 size_t msg_len,
unsigned *msg_prio,
int waitok,
1849 const struct timespec *abs_timeout)
1852 struct timespec
ts, ts2;
1856 if (msg_len < mq->mq_msgsize)
1868 if (abs_timeout == NULL) {
1880 if (error != EAGAIN)
1883 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1890 timespecsub(abs_timeout, &
ts, &ts2);
1891 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1895 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1899 if (error != ETIMEDOUT)
1906 curthread->td_retval[0] = msg->msg_size;
1907 curthread->td_retval[1] = 0;
1922 while ((*msg = TAILQ_FIRST(&mq->
mq_msgq)) == NULL && error == 0) {
1929 PCATCH,
"mqrecv", timo);
1931 if (error == EAGAIN)
1936 TAILQ_REMOVE(&mq->
mq_msgq, *msg, msg_link);
1945 KNOTE_LOCKED(&mq->
mq_wsel.si_note, 0);
1958 return (uma_zalloc(
mqnoti_zone, M_WAITOK | M_ZERO));
1972 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1973 if (nt->nt_ksi.ksi_mqd ==
fd)
1982 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1988 LIST_REMOVE(nt, nt_link);
1997 mtx_assert(&mq->
mq_mutex, MA_OWNED);
2011 const struct mq_attr *attr)
2015 struct pwddesc *pdp;
2018 int fd, error, len, cmode;
2020 AUDIT_ARG_FFLAGS(
flags);
2021 AUDIT_ARG_MODE(
mode);
2023 pdp = td->td_proc->p_pd;
2024 cmode = (((
mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT);
2026 if ((
flags & O_CREAT) != 0 && attr != NULL) {
2027 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg >
maxmsg)
2029 if (attr->mq_msgsize <= 0 || attr->mq_msgsize >
maxmsgsize)
2043 if (len < 2 ||
path[0] !=
'/' || strchr(
path + 1,
'/') != NULL)
2049 if (strcmp(
path,
"/.") == 0 || strcmp(
path,
"/..") == 0)
2051 AUDIT_ARG_UPATH1_CANON(
path);
2053 error = falloc(td, &fp, &
fd, O_CLOEXEC);
2060 if (!(
flags & O_CREAT)) {
2068 path + 1, len - 1, td->td_ucred,
2081 if ((
flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2090 error =
vaccess(VREG, pn->mn_mode, pn->mn_uid,
2091 pn->mn_gid,
accmode, td->td_ucred);
2105 finit(fp,
flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2108 td->td_retval[0] =
fd;
2119 struct mq_attr attr;
2122 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2124 flags = FFLAGS(uap->flags);
2125 if ((
flags & O_CREAT) != 0 && uap->attr != NULL) {
2126 error = copyin(uap->attr, &attr,
sizeof(attr));
2131 uap->attr != NULL ? &attr : NULL));
2149 if (len < 2 ||
path[0] !=
'/' || strchr(
path + 1,
'/') != NULL)
2151 if (strcmp(
path,
"/.") == 0 || strcmp(
path,
"/..") == 0)
2153 AUDIT_ARG_UPATH1_CANON(
path);
2165typedef int (*
_fgetf)(
struct thread *, int, cap_rights_t *,
struct file **);
2177 error = func(td,
fd, rightsp, fpp);
2184 pn = (*fpp)->f_data;
2221 struct mq_attr *oattr)
2229 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2231 error =
getmq(td, mqd, &fp, NULL, &mq);
2239 oflag =
flag = fp->f_flag;
2240 flag &= ~O_NONBLOCK;
2241 flag |= (attr->mq_flags & O_NONBLOCK);
2242 }
while (atomic_cmpset_int(&fp->f_flag, oflag,
flag) == 0);
2245 oattr->mq_flags = (O_NONBLOCK & oflag);
2253 struct mq_attr attr, oattr;
2256 if (uap->attr != NULL) {
2257 error = copyin(uap->attr, &attr,
sizeof(attr));
2263 if (error == 0 && uap->oattr != NULL) {
2264 bzero(oattr.__reserved,
sizeof(oattr.__reserved));
2265 error = copyout(&oattr, uap->oattr,
sizeof(oattr));
2275 struct timespec *abs_timeout, ets;
2279 AUDIT_ARG_FD(uap->mqd);
2280 error =
getmq_read(td, uap->mqd, &fp, NULL, &mq);
2283 if (uap->abs_timeout != NULL) {
2284 error = copyin(uap->abs_timeout, &ets,
sizeof(ets));
2290 waitok = !(fp->f_flag & O_NONBLOCK);
2292 uap->msg_prio, waitok, abs_timeout);
2303 struct timespec *abs_timeout, ets;
2306 AUDIT_ARG_FD(uap->mqd);
2307 error =
getmq_write(td, uap->mqd, &fp, NULL, &mq);
2310 if (uap->abs_timeout != NULL) {
2311 error = copyin(uap->abs_timeout, &ets,
sizeof(ets));
2317 waitok = !(fp->f_flag & O_NONBLOCK);
2318 error =
mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2319 uap->msg_prio, waitok, abs_timeout);
2328 struct filedesc *fdp;
2331 struct file *fp, *fp2;
2336 if (sigev != NULL) {
2337 if (sigev->sigev_notify != SIGEV_SIGNAL &&
2338 sigev->sigev_notify != SIGEV_THREAD_ID &&
2339 sigev->sigev_notify != SIGEV_NONE)
2341 if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2342 sigev->sigev_notify == SIGEV_THREAD_ID) &&
2343 !_SIG_VALID(sigev->sigev_signo))
2347 fdp = td->td_proc->p_fd;
2348 error =
getmq(td, mqd, &fp, NULL, &mq);
2352 FILEDESC_SLOCK(fdp);
2353 fp2 = fget_noref(fdp, mqd);
2355 FILEDESC_SUNLOCK(fdp);
2362 FILEDESC_SUNLOCK(fdp);
2367 FILEDESC_SUNLOCK(fdp);
2372 FILEDESC_SUNLOCK(fdp);
2373 if (sigev != NULL) {
2380 if (newnt == NULL) {
2390 if (newnt != NULL) {
2397 ksiginfo_init(&nt->nt_ksi);
2398 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2399 nt->nt_ksi.ksi_code = SI_MESGQ;
2401 nt->nt_ksi.ksi_mqd = mqd;
2404 nt->nt_sigev = *sigev;
2431 struct sigevent
ev, *evp;
2434 if (uap->sigev == NULL) {
2437 error = copyin(uap->sigev, &
ev,
sizeof(
ev));
2450 struct filedesc *fdp;
2452 fdp = td->td_proc->p_fd;
2453 FILEDESC_LOCK_ASSERT(fdp);
2477 struct filedesc *fdp;
2483 FILEDESC_SLOCK(fdp);
2484 for (i = 0; i < fdp->fd_nfiles; ++i) {
2485 fp = fget_noref(fdp, i);
2486 if (fp != NULL && fp->f_ops == &
mqueueops) {
2493 FILEDESC_SUNLOCK(fdp);
2494 KASSERT(LIST_EMPTY(&p->p_mqnotifier), (
"mq notifiers left"));
2498mqf_poll(
struct file *fp,
int events,
struct ucred *active_cred,
2505 if (events & (POLLIN | POLLRDNORM)) {
2507 revents |= events & (POLLIN | POLLRDNORM);
2513 if (events & POLLOUT) {
2540mqf_stat(
struct file *fp,
struct stat *
st,
struct ucred *active_cred)
2544 bzero(
st,
sizeof *
st);
2546 st->st_atim = pn->mn_atime;
2547 st->st_mtim = pn->mn_mtime;
2548 st->st_ctim = pn->mn_ctime;
2549 st->st_birthtim = pn->mn_birth;
2550 st->st_uid = pn->mn_uid;
2551 st->st_gid = pn->mn_gid;
2552 st->st_mode = S_IFIFO | pn->mn_mode;
2567 error =
vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2571 pn->mn_mode =
mode & ACCESSPERMS;
2578mqf_chown(
struct file *fp, uid_t uid, gid_t gid,
struct ucred *active_cred,
2587 if (uid == (uid_t)-1)
2589 if (gid == (gid_t)-1)
2591 if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2592 (gid != pn->mn_gid && !
groupmember(gid, active_cred))) &&
2608 if (kn->kn_filter == EVFILT_READ) {
2611 }
else if (kn->kn_filter == EVFILT_WRITE) {
2624 if (kn->kn_filter == EVFILT_READ)
2626 else if (kn->kn_filter == EVFILT_WRITE)
2629 panic(
"filt_mqdetach");
2637 mtx_assert(&mq->
mq_mutex, MA_OWNED);
2646 mtx_assert(&mq->
mq_mutex, MA_OWNED);
2654 kif->kf_type = KF_TYPE_MQUEUE;
2671 .fo_flags = DFLAG_PASSABLE,
2688 .vop_write = VOP_EOPNOTSUPP,
2690 .vop_mkdir = VOP_EOPNOTSUPP,
2691 .vop_rmdir = VOP_EOPNOTSUPP
2705 .vfc_version = VFS_VERSION,
2706 .vfc_name =
"mqueuefs",
2709 .vfc_flags = VFCF_SYNTHETIC
2713 SYSCALL_INIT_HELPER(kmq_open),
2714 SYSCALL_INIT_HELPER_F(kmq_setattr, SYF_CAPENABLED),
2715 SYSCALL_INIT_HELPER_F(kmq_timedsend, SYF_CAPENABLED),
2716 SYSCALL_INIT_HELPER_F(kmq_timedreceive, SYF_CAPENABLED),
2717 SYSCALL_INIT_HELPER_F(kmq_notify, SYF_CAPENABLED),
2718 SYSCALL_INIT_HELPER(kmq_unlink),
2722#ifdef COMPAT_FREEBSD32
2723#include <compat/freebsd32/freebsd32.h>
2724#include <compat/freebsd32/freebsd32_proto.h>
2725#include <compat/freebsd32/freebsd32_signal.h>
2726#include <compat/freebsd32/freebsd32_syscall.h>
2727#include <compat/freebsd32/freebsd32_util.h>
2730mq_attr_from32(
const struct mq_attr32 *from,
struct mq_attr *to)
2733 to->mq_flags = from->mq_flags;
2734 to->mq_maxmsg = from->mq_maxmsg;
2735 to->mq_msgsize = from->mq_msgsize;
2736 to->mq_curmsgs = from->mq_curmsgs;
2740mq_attr_to32(
const struct mq_attr *from,
struct mq_attr32 *to)
2743 to->mq_flags = from->mq_flags;
2744 to->mq_maxmsg = from->mq_maxmsg;
2745 to->mq_msgsize = from->mq_msgsize;
2746 to->mq_curmsgs = from->mq_curmsgs;
2750freebsd32_kmq_open(
struct thread *td,
struct freebsd32_kmq_open_args *uap)
2752 struct mq_attr attr;
2753 struct mq_attr32 attr32;
2756 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2758 flags = FFLAGS(uap->flags);
2759 if ((
flags & O_CREAT) != 0 && uap->attr != NULL) {
2760 error = copyin(uap->attr, &attr32,
sizeof(attr32));
2763 mq_attr_from32(&attr32, &attr);
2766 uap->attr != NULL ? &attr : NULL));
2770freebsd32_kmq_setattr(
struct thread *td,
struct freebsd32_kmq_setattr_args *uap)
2772 struct mq_attr attr, oattr;
2773 struct mq_attr32 attr32, oattr32;
2776 if (uap->attr != NULL) {
2777 error = copyin(uap->attr, &attr32,
sizeof(attr32));
2780 mq_attr_from32(&attr32, &attr);
2784 if (error == 0 && uap->oattr != NULL) {
2785 mq_attr_to32(&oattr, &oattr32);
2786 bzero(oattr32.__reserved,
sizeof(oattr32.__reserved));
2787 error = copyout(&oattr32, uap->oattr,
sizeof(oattr32));
2793freebsd32_kmq_timedsend(
struct thread *td,
2794 struct freebsd32_kmq_timedsend_args *uap)
2798 struct timespec32 ets32;
2799 struct timespec *abs_timeout, ets;
2803 AUDIT_ARG_FD(uap->mqd);
2804 error =
getmq_write(td, uap->mqd, &fp, NULL, &mq);
2807 if (uap->abs_timeout != NULL) {
2808 error = copyin(uap->abs_timeout, &ets32,
sizeof(ets32));
2811 CP(ets32, ets, tv_sec);
2812 CP(ets32, ets, tv_nsec);
2816 waitok = !(fp->f_flag & O_NONBLOCK);
2817 error =
mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2818 uap->msg_prio, waitok, abs_timeout);
2825freebsd32_kmq_timedreceive(
struct thread *td,
2826 struct freebsd32_kmq_timedreceive_args *uap)
2830 struct timespec32 ets32;
2831 struct timespec *abs_timeout, ets;
2834 AUDIT_ARG_FD(uap->mqd);
2835 error =
getmq_read(td, uap->mqd, &fp, NULL, &mq);
2838 if (uap->abs_timeout != NULL) {
2839 error = copyin(uap->abs_timeout, &ets32,
sizeof(ets32));
2842 CP(ets32, ets, tv_sec);
2843 CP(ets32, ets, tv_nsec);
2847 waitok = !(fp->f_flag & O_NONBLOCK);
2849 uap->msg_prio, waitok, abs_timeout);
2856freebsd32_kmq_notify(
struct thread *td,
struct freebsd32_kmq_notify_args *uap)
2858 struct sigevent
ev, *evp;
2859 struct sigevent32 ev32;
2862 if (uap->sigev == NULL) {
2865 error = copyin(uap->sigev, &ev32,
sizeof(ev32));
2868 error = convert_sigevent32(&ev32, &
ev);
2876static struct syscall_helper_data mq32_syscalls[] = {
2877 SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2878 SYSCALL32_INIT_HELPER_F(freebsd32_kmq_setattr, SYF_CAPENABLED),
2879 SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedsend, SYF_CAPENABLED),
2880 SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedreceive, SYF_CAPENABLED),
2881 SYSCALL32_INIT_HELPER_F(freebsd32_kmq_notify, SYF_CAPENABLED),
2882 SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2895#ifdef COMPAT_FREEBSD32
2896 error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD);
2907#ifdef COMPAT_FREEBSD32
2908 syscall32_helper_unregister(mq32_syscalls);
static struct bt_table st
int tvtohz(struct timeval *tv)
int invfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
int invfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
void __read_mostly(* mq_fdclose)(struct thread *td, int fd, struct file *fp)
int fget_write(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
void fdclose(struct thread *td, struct file *fp, int idx)
int fget(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
struct fileops badfileops
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
int fget_read(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
int invfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, struct thread *td)
int invfo_rdwr(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
void knlist_destroy(struct knlist *knl)
void knote(struct knlist *list, long hint, int lockflags)
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
struct prisonlist allprison
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
static struct pollrec pr[POLL_LIST_LEN]
int priv_check_cred(struct ucred *cred, int priv)
int priv_check(struct thread *td, int priv)
int groupmember(gid_t gid, struct ucred *cred)
void panic(const char *fmt,...)
void sigqueue_take(ksiginfo_t *ksi)
int tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
int sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
void sx_destroy(struct sx *sx)
void wakeup_one(const void *ident)
int syscall_helper_unregister(struct syscall_helper_data *sd)
int syscall_helper_register(struct syscall_helper_data *sd, int flags)
void getnanotime(struct timespec *tsp)
void p31b_setcfg(int num, int value)
struct mqfs_node * mi_root
struct unrhdr * mi_unrhdr
char mn_name[MQFS_NAMELEN+1]
struct mqfs_node * mn_parent
struct mqfs_info * mn_info
struct mqueue_notifier * mq_notifier
__read_mostly cap_rights_t cap_event_rights
__read_mostly cap_rights_t cap_write_rights
__read_mostly cap_rights_t cap_read_rights
int snprintf(char *str, size_t size, const char *format,...)
int taskqueue_enqueue(struct taskqueue *queue, struct task *task)
int uiomove_frombuf(void *buf, int buflen, struct uio *uio)
int alloc_unr(struct unrhdr *uh)
void delete_unrhdr(struct unrhdr *uh)
struct unrhdr * new_unrhdr(int low, int high, struct mtx *mutex)
void free_unr(struct unrhdr *uh, u_int item)
void seldrain(struct selinfo *sip)
void selrecord(struct thread *selector, struct selinfo *sip)
void selwakeup(struct selinfo *sip)
int sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
static int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
static int mqfs_open(struct vop_open_args *ap)
static __inline void mqnode_addref(struct mqfs_node *node)
static int mqfs_destroy(struct mqfs_node *mn)
static __inline void notifier_delete(struct proc *p, struct mqueue_notifier *nt)
struct filterops mq_rfiltops
static __inline int getmq_read(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static __inline void notifier_free(struct mqueue_notifier *p)
int sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
static int mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
static uma_zone_t mqnode_zone
static struct syscall_helper_data mq_syscalls[]
static __inline void mqnode_free(struct mqfs_node *node)
static void mq_proc_exit(void *arg, struct proc *p)
static int mqfs_init(struct vfsconf *vfc)
static struct vop_vector mqfs_vnodeops
static struct vfsops mqfs_vfsops
static int mqf_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
VFS_VOP_VECTOR_REGISTER(mqfs_vnodeops)
static int _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static int mqfs_getattr(struct vop_getattr_args *ap)
static __inline void mqueue_freemsg(struct mqueue_msg *msg)
static int filt_mqread(struct knote *kn, long hint)
static void mqfs_fileno_uninit(struct mqfs_info *mi)
static struct mqfs_node * mqfs_search(struct mqfs_node *pd, const char *name, int len, struct ucred *cred)
static struct mqfs_node * mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode, int nodetype)
static void mqueue_send_notification(struct mqueue *mq)
static int mqfs_readdir(struct vop_readdir_args *ap)
static __inline struct mqueue_notifier * notifier_alloc(void)
static int mqfs_close(struct vop_close_args *ap)
DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE)
static int mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
static __inline int getmq_write(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static int kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
static int mqf_kqfilter(struct file *fp, struct knote *kn)
static int mqfs_reclaim(struct vop_reclaim_args *ap)
static uma_zone_t mvdata_zone
int sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
static int mqfs_fixup_dir(struct mqfs_node *parent)
static int mqfs_mount(struct mount *mp)
static eventhandler_tag exit_tag
static unsigned mqfs_osd_jail_slot
static int mqunload(void)
static int kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr, struct mq_attr *oattr)
TAILQ_HEAD(msgq, mqueue_msg)
static struct mqfs_node * mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen, struct ucred *cred, int mode)
static __inline void mqnode_release(struct mqfs_node *node)
struct filterops mq_wfiltops
static void filt_mqdetach(struct knote *kn)
static int mqfs_unmount(struct mount *mp, int mntflags)
static int mqfs_inactive(struct vop_inactive_args *ap)
static int mqfs_uninit(struct vfsconf *vfc)
static int mq_modload(struct module *module, int cmd, void *arg)
static int mqfs_prison_remove(void *obj, void *data)
int sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
static int mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
static int mqfs_statfs(struct mount *mp, struct statfs *sbp)
static int mqf_close(struct file *fp, struct thread *td)
static uma_zone_t mqnoti_zone
static moduledata_t mqueuefs_mod
static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
static int mqueue_send(struct mqueue *mq, const char *msg_ptr, size_t msg_len, unsigned msg_prio, int waitok, const struct timespec *abs_timeout)
static int default_maxmsg
static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
static struct mqueue_msg * mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
static __inline struct mqfs_node * mqnode_alloc(void)
static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
static int kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode, const struct mq_attr *attr)
static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "POSIX real time message queue")
int sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
static void mqfs_fileno_init(struct mqfs_info *mi)
int(* _fgetf)(struct thread *, int, cap_rights_t *, struct file **)
static int mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
static int mqfs_lookup(struct vop_cachedlookup_args *ap)
static int default_msgsize
SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW, &maxmsg, 0, "Default maximum messages in queue")
static struct fileops mqueueops
static int mqfs_lookupx(struct vop_cachedlookup_args *ap)
static void mqueue_fdclose(struct thread *td, int fd, struct file *fp)
static int mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred)
static int mqfs_read(struct vop_read_args *ap)
static int mqfs_setattr(struct vop_setattr_args *ap)
static __inline void notifier_insert(struct proc *p, struct mqueue_notifier *nt)
static struct mqfs_info mqfs_data
static void do_recycle(void *context, int pending __unused)
static void notifier_remove(struct proc *p, struct mqueue *mq, int fd)
static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
static struct vfsconf mqueuefs_vfsconf
FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support")
static uma_zone_t mqueue_zone
static struct mqueue_notifier * notifier_search(struct proc *p, int fd)
static int mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
static struct mqueue * mqueue_alloc(const struct mq_attr *attr)
static int mqfs_create(struct vop_create_args *ap)
static int mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data")
static int mqfs_remove(struct vop_remove_args *ap)
static int filt_mqwrite(struct knote *kn, long hint)
static __inline int getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static int mqfs_access(struct vop_access_args *ap)
static int mqueue_receive(struct mqueue *mq, char *msg_ptr, size_t msg_len, unsigned *msg_prio, int waitok, const struct timespec *abs_timeout)
static void mqueue_free(struct mqueue *mq)
int sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
MODULE_VERSION(mqueuefs, 1)
int vfs_cache_lookup(struct vop_lookup_args *ap)
void cache_purge(struct vnode *vp)
struct vop_vector default_vnodeops
struct vfsconfhead vfsconf
int vfs_modevent(module_t mod, int type, void *data)
void vfs_mountedfrom(struct mount *mp, const char *from)
void vhold(struct vnode *vp)
int getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, struct vnode **vpp)
int vrecycle(struct vnode *vp)
int insmntque(struct vnode *vp, struct mount *mp)
int vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
int vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
int vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, accmode_t accmode, struct ucred *cred)
void vgone(struct vnode *vp)
void vfs_timestamp(struct timespec *tsp)
void vput(struct vnode *vp)
void vfs_getnewfsid(struct mount *mp)
void vdrop(struct vnode *vp)
int vget(struct vnode *vp, int flags)