45 #include <sys/cdefs.h>
48 #include "opt_compat.h"
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/limits.h>
55 #include <sys/capability.h>
56 #include <sys/dirent.h>
57 #include <sys/event.h>
58 #include <sys/eventhandler.h>
59 #include <sys/fcntl.h>
61 #include <sys/filedesc.h>
63 #include <sys/malloc.h>
64 #include <sys/module.h>
65 #include <sys/mount.h>
66 #include <sys/mqueue.h>
67 #include <sys/mutex.h>
68 #include <sys/namei.h>
69 #include <sys/posix4.h>
73 #include <sys/queue.h>
74 #include <sys/sysproto.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysent.h>
80 #include <sys/sysctl.h>
81 #include <sys/taskqueue.h>
82 #include <sys/unistd.h>
83 #include <sys/vnode.h>
84 #include <machine/atomic.h>
86 FEATURE(p1003_1b_mqueue,
"POSIX P1003.1B message queues support");
91 #define MQFS_NAMELEN NAME_MAX
92 #define MQFS_DELEN (8 + MQFS_NAMELEN)
119 struct vnode *mv_vnode;
138 struct timespec mn_birth;
139 struct timespec mn_ctime;
140 struct timespec mn_atime;
141 struct timespec mn_mtime;
147 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
148 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
149 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
150 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
151 (fp)->f_data)->mn_data))
159 struct sigevent nt_sigev;
161 struct proc *nt_proc;
174 struct selinfo mq_rsel;
175 struct selinfo mq_wsel;
184 unsigned int msg_prio;
185 unsigned int msg_size;
190 "POSIX real time message queue");
196 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
197 &maxmsg, 0,
"Default maximum messages in queue");
199 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
200 &maxmsgsize, 0,
"Default maximum message size");
202 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
203 &maxmq, 0,
"maximum message queues");
205 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
206 &curmq, 0,
"current message queue number");
226 const char *
name,
int namelen,
struct ucred *cred,
int mode);
228 const char *
name,
int namelen,
struct ucred *cred,
int mode);
232 const char *
name,
int namelen,
struct ucred *cred,
int mode);
244 size_t msg_len,
unsigned msg_prio,
int waitok,
245 const struct timespec *abs_timeout);
247 size_t msg_len,
unsigned *msg_prio,
int waitok,
248 const struct timespec *abs_timeout);
310 switch (mn->mn_type) {
319 (
"mqfstype_this node has no parent"));
320 mn->mn_fileno = mn->
mn_parent->mn_fileno;
324 (
"mqfstype_parent node has no parent"));
326 mn->mn_fileno = mn->
mn_parent->mn_fileno;
330 (
"mqfstype_parent node has no grandparent"));
335 (
"mqfs_fileno_alloc() called for unknown type node: %d",
347 switch (mn->mn_type) {
360 (
"mqfs_fileno_free() called for unknown type node: %d",
369 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
375 uma_zfree(mqnode_zone, node);
381 atomic_fetchadd_int(&node->mn_refcount, 1);
391 old = atomic_fetchadd_int(&node->mn_refcount, -1);
398 int locked = sx_xlocked(&mqfs->
mi_lock);
413 KASSERT(parent != NULL, (
"%s(): parent is NULL", __func__));
414 KASSERT(parent->
mn_info != NULL,
415 (
"%s(): parent has no mn_info", __func__));
418 (
"%s(): parent is not a directory", __func__));
422 LIST_INIT(&node->mn_children);
423 LIST_INIT(&node->mn_vnodes);
424 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
436 strncpy(node->
mn_name, name, namelen);
437 node->mn_type = nodetype;
438 node->mn_refcount = 1;
440 node->mn_ctime = node->mn_atime = node->mn_mtime
442 node->mn_uid = cred->cr_uid;
443 node->mn_gid = cred->cr_gid;
444 node->mn_mode =
mode;
453 struct ucred *cred,
int mode)
476 dir->mn_refcount = 1;
485 dir->mn_refcount = 1;
502 struct ucred *cred,
int mode)
523 mqfs_create_link(
struct mqfs_node *parent,
const char *name,
int namelen,
524 struct ucred *cred,
int mode)
546 KASSERT(node != NULL,
547 (
"%s(): node is NULL", __func__));
549 (
"%s(): node has no mn_info", __func__));
553 while (! LIST_EMPTY(&node->mn_children))
557 if ((parent = node->
mn_parent) != NULL) {
559 (
"%s(): parent has different mn_info", __func__));
560 LIST_REMOVE(node, mn_sibling);
563 if (node->mn_fileno != 0)
565 if (node->mn_data != NULL)
579 if (mp->mnt_flag & MNT_UPDATE)
584 mp->mnt_flag |= MNT_LOCAL;
585 mp->mnt_kern_flag |= MNTK_MPSAFE;
591 sbp->f_bsize = PAGE_SIZE;
592 sbp->f_iosize = PAGE_SIZE;
609 error =
vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
618 mqfs_root(
struct mount *mp,
int flags,
struct vnode **vpp)
647 mqnode_zone = uma_zcreate(
"mqnode",
sizeof(
struct mqfs_node),
648 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
649 mqueue_zone = uma_zcreate(
"mqueue",
sizeof(
struct mqueue),
650 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
651 mvdata_zone = uma_zcreate(
"mvdata",
653 NULL, UMA_ALIGN_PTR, 0);
654 mqnoti_zone = uma_zcreate(
"mqnotifier",
sizeof(
struct mqueue_notifier),
655 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
657 sx_init(&mi->
mi_lock,
"mqfs lock");
662 LIST_INIT(&root->mn_children);
663 LIST_INIT(&root->mn_vnodes);
668 exit_tag = EVENTHANDLER_REGISTER(process_exit,
mq_proc_exit, NULL,
669 EVENTHANDLER_PRI_ANY);
671 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
685 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
691 uma_zdestroy(mqnode_zone);
692 uma_zdestroy(mqueue_zone);
693 uma_zdestroy(mvdata_zone);
694 uma_zdestroy(mqnoti_zone);
704 struct vnode *vp = (
struct vnode *)context;
718 struct vnode *newvpp;
724 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
725 if (vd->mv_vnode->v_mount == mp) {
735 error =
vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
744 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
754 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
755 if (vd->mv_vnode->v_mount == mp) {
767 vd = uma_zalloc(mvdata_zone, M_WAITOK);
772 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
774 switch (pn->mn_type) {
776 (*vpp)->v_vflag = VV_ROOT;
781 (*vpp)->v_type = VDIR;
784 (*vpp)->v_type = VREG;
787 (*vpp)->v_type = VLNK;
790 KASSERT(0, (
"mqfs_allocf called for null node\n"));
792 panic(
"%s has unexpected type: %d", pn->
mn_name, pn->mn_type);
807 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
808 if (strncmp(pn->
mn_name, name, len) == 0 &&
821 struct componentname *cnp;
822 struct vnode *dvp, **vpp;
826 int nameiop, flags, error, namelen;
833 pname = cnp->cn_nameptr;
834 namelen = cnp->cn_namelen;
836 flags = cnp->cn_flags;
837 nameiop = cnp->cn_nameiop;
843 if (dvp->v_type != VDIR)
846 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
855 if (namelen == 1 && pname[0] ==
'.') {
856 if ((flags & ISLASTCN) && nameiop != LOOKUP)
865 if (cnp->cn_flags & ISDOTDOT) {
866 if (dvp->v_vflag & VV_ROOT)
868 if ((flags & ISLASTCN) && nameiop != LOOKUP)
871 KASSERT(pd->
mn_parent, (
"non-root directory has no parent"));
874 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
888 if (nameiop == DELETE && (flags & ISLASTCN)) {
889 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
905 if (error == 0 && cnp->cn_flags & MAKEENTRY)
913 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
914 && (flags & ISLASTCN)) {
915 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
918 cnp->cn_flags |= SAVENAME;
919 return (EJUSTRETURN);
925 struct vop_lookup_args {
926 struct vop_generic_args a_gen;
928 struct vnode **a_vpp;
929 struct componentname *a_cnp;
946 struct vop_create_args {
948 struct vnode **a_vpp;
949 struct componentname *a_cnp;
961 struct componentname *cnp = ap->a_cnp;
967 pd =
VTON(ap->a_dvp);
974 if ((cnp->cn_flags & HASBUF) == 0)
975 panic(
"%s: no name", __func__);
977 cnp->cn_cred, ap->a_vap->va_mode);
984 error =
mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1008 if (ucred->cr_uid != pn->mn_uid &&
1011 else if (!pn->mn_deleted) {
1015 LIST_REMOVE(pn, mn_sibling);
1016 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1018 vhold(vd->mv_vnode);
1029 struct vop_remove_args {
1030 struct vnode *a_dvp;
1032 struct componentname *a_cnp;
1046 if (ap->a_vp->v_type == VDIR)
1048 pn =
VTON(ap->a_vp);
1050 error =
do_unlink(pn, ap->a_cnp->cn_cred);
1056 struct vop_inactive_args {
1058 struct thread *a_td;
1073 struct vop_reclaim_args {
1074 struct vop_generic_args a_gen;
1076 struct thread *a_td;
1084 struct vnode *vp = ap->a_vp;
1092 LIST_REMOVE(vd, mv_link);
1093 uma_zfree(mvdata_zone, vd);
1100 struct vop_open_args {
1101 struct vop_generic_args a_gen;
1104 struct ucred *a_cred;
1105 struct thread *a_td;
1117 struct vop_close_args {
1118 struct vop_generic_args a_gen;
1121 struct ucred *a_cred;
1122 struct thread *a_td;
1133 struct vop_access_args {
1134 struct vop_generic_args a_gen;
1136 accmode_t a_accmode;
1137 struct ucred *a_cred;
1138 struct thread *a_td;
1148 struct vnode *vp = ap->a_vp;
1152 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1155 error =
vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1156 vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1161 struct vop_getattr_args {
1162 struct vop_generic_args a_gen;
1164 struct vattr *a_vap;
1165 struct ucred *a_cred;
1175 struct vnode *vp = ap->a_vp;
1177 struct vattr *vap = ap->a_vap;
1180 vap->va_type = vp->v_type;
1181 vap->va_mode = pn->mn_mode;
1183 vap->va_uid = pn->mn_uid;
1184 vap->va_gid = pn->mn_gid;
1185 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1186 vap->va_fileid = pn->mn_fileno;
1188 vap->va_blocksize = PAGE_SIZE;
1189 vap->va_bytes = vap->va_size = 0;
1190 vap->va_atime = pn->mn_atime;
1191 vap->va_mtime = pn->mn_mtime;
1192 vap->va_ctime = pn->mn_ctime;
1193 vap->va_birthtime = pn->mn_birth;
1196 vap->va_rdev = NODEV;
1198 vap->va_filerev = 0;
1203 struct vop_setattr_args {
1204 struct vop_generic_args a_gen;
1206 struct vattr *a_vap;
1207 struct ucred *a_cred;
1227 if ((vap->va_type != VNON) ||
1228 (vap->va_nlink != VNOVAL) ||
1229 (vap->va_fsid != VNOVAL) ||
1230 (vap->va_fileid != VNOVAL) ||
1231 (vap->va_blocksize != VNOVAL) ||
1232 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1233 (vap->va_rdev != VNOVAL) ||
1234 ((
int)vap->va_bytes != VNOVAL) ||
1235 (vap->va_gen != VNOVAL)) {
1242 if (vap->va_uid == (uid_t)VNOVAL)
1246 if (vap->va_gid == (gid_t)VNOVAL)
1251 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1256 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1264 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1265 (gid != pn->mn_gid && !
groupmember(gid, ap->a_cred))) &&
1266 (error =
priv_check(td, PRIV_MQ_ADMIN)) != 0)
1273 if (vap->va_mode != (mode_t)VNOVAL) {
1274 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1277 pn->mn_mode = vap->va_mode;
1281 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1283 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1284 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1285 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1287 if (vap->va_atime.tv_sec != VNOVAL) {
1288 pn->mn_atime = vap->va_atime;
1290 if (vap->va_mtime.tv_sec != VNOVAL) {
1291 pn->mn_mtime = vap->va_mtime;
1302 struct vop_read_args {
1303 struct vop_generic_args a_gen;
1307 struct ucred *a_cred;
1318 struct vnode *vp = ap->a_vp;
1319 struct uio *uio = ap->a_uio;
1324 if (vp->v_type != VREG)
1330 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1335 buf[
sizeof(
buf)-1] =
'\0';
1342 struct vop_readdir_args {
1343 struct vop_generic_args a_gen;
1346 struct ucred *a_cred;
1363 struct dirent entry;
1365 int *tmp_ncookies = NULL;
1374 if (vp->v_type != VDIR)
1377 if (uio->uio_offset < 0)
1380 if (ap->a_ncookies != NULL) {
1381 tmp_ncookies = ap->a_ncookies;
1382 *ap->a_ncookies = 0;
1383 ap->a_ncookies = NULL;
1391 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1392 entry.d_reclen =
sizeof(entry);
1395 entry.d_fileno = pn->mn_fileno;
1397 entry.d_name[i] = pn->
mn_name[i];
1398 entry.d_name[i] = 0;
1400 switch (pn->mn_type) {
1405 entry.d_type = DT_DIR;
1408 entry.d_type = DT_REG;
1411 entry.d_type = DT_LNK;
1417 if (entry.d_reclen > uio->uio_resid)
1419 if (offset >= uio->uio_offset) {
1424 offset += entry.d_reclen;
1428 uio->uio_offset = offset;
1430 if (tmp_ncookies != NULL)
1431 ap->a_ncookies = tmp_ncookies;
1439 struct vop_mkdir_args {
1440 struct vnode *a_dvp;
1441 struvt vnode **a_vpp;
1442 struvt componentname *a_cnp;
1443 struct vattr *a_vap;
1451 mqfs_mkdir(
struct vop_mkdir_args *ap)
1454 struct componentname *cnp = ap->a_cnp;
1462 if ((cnp->cn_flags & HASBUF) == 0)
1463 panic(
"%s: no name", __func__);
1464 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1465 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1472 error =
mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1479 struct vop_rmdir_args {
1480 struct vnode *a_dvp;
1482 struct componentname *a_cnp;
1490 mqfs_rmdir(
struct vop_rmdir_args *ap)
1500 if (pn->mn_deleted) {
1505 pt = LIST_FIRST(&pn->mn_children);
1506 pt = LIST_NEXT(pt, mn_sibling);
1507 pt = LIST_NEXT(pt, mn_sibling);
1515 LIST_REMOVE(pn, mn_sibling);
1535 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1547 atomic_add_int(&curmq, 1);
1559 while ((msg = TAILQ_FIRST(&mq->
mq_msgq)) != NULL) {
1560 TAILQ_REMOVE(&mq->
mq_msgq, msg, msg_link);
1561 free(msg, M_MQUEUEDATA);
1569 uma_zfree(mqueue_zone, mq);
1570 atomic_add_int(&curmq, -1);
1584 msg =
malloc(len, M_MQUEUEDATA, M_WAITOK);
1585 error = copyin(msg_ptr, ((
char *)msg) +
sizeof(
struct mqueue_msg),
1588 free(msg, M_MQUEUEDATA);
1591 msg->msg_size = msg_size;
1592 msg->msg_prio = msg_prio;
1605 error = copyout(((
char *)msg) +
sizeof(*msg), msg_ptr,
1607 if (error == 0 && msg_prio != NULL)
1608 error = copyout(&msg->msg_prio, msg_prio,
sizeof(
int));
1615 static __inline
void
1618 free(msg, M_MQUEUEDATA);
1628 size_t msg_len,
unsigned msg_prio,
int waitok,
1629 const struct timespec *abs_timeout)
1632 struct timespec ts, ts2;
1636 if (msg_prio >= MQ_PRIO_MAX)
1653 if (abs_timeout == NULL) {
1665 if (error != EAGAIN)
1668 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1675 timespecsub(&ts2, &ts);
1676 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1680 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1682 if (error != ETIMEDOUT)
1709 PCATCH,
"mqsend", timo);
1711 if (error == EAGAIN)
1719 if (TAILQ_EMPTY(&mq->
mq_msgq)) {
1720 TAILQ_INSERT_HEAD(&mq->
mq_msgq, msg, msg_link);
1722 if (msg->msg_prio <= TAILQ_LAST(&mq->
mq_msgq, msgq)->msg_prio) {
1723 TAILQ_INSERT_TAIL(&mq->
mq_msgq, msg, msg_link);
1725 TAILQ_FOREACH(msg2, &mq->
mq_msgq, msg_link) {
1726 if (msg2->msg_prio < msg->msg_prio)
1729 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1742 KNOTE_LOCKED(&mq->
mq_rsel.si_note, 0);
1759 mtx_assert(&mq->
mq_mutex, MA_OWNED);
1761 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1768 if (!KSI_ONQ(&nt->nt_ksi)) {
1769 ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1770 tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1784 size_t msg_len,
unsigned *msg_prio,
int waitok,
1785 const struct timespec *abs_timeout)
1788 struct timespec ts, ts2;
1792 if (msg_len < mq->mq_msgsize)
1804 if (abs_timeout == NULL) {
1816 if (error != EAGAIN)
1819 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1827 timespecsub(&ts2, &ts);
1828 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1832 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1836 if (error != ETIMEDOUT)
1843 curthread->td_retval[0] = msg->msg_size;
1844 curthread->td_retval[1] = 0;
1859 while ((*msg = TAILQ_FIRST(&mq->
mq_msgq)) == NULL && error == 0) {
1866 PCATCH,
"mqrecv", timo);
1868 if (error == EAGAIN)
1873 TAILQ_REMOVE(&mq->
mq_msgq, *msg, msg_link);
1882 KNOTE_LOCKED(&mq->
mq_wsel.si_note, 0);
1895 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1898 static __inline
void
1901 uma_zfree(mqnoti_zone, p);
1909 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1910 if (nt->nt_ksi.ksi_mqd == fd)
1916 static __inline
void
1919 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1922 static __inline
void
1925 LIST_REMOVE(nt, nt_link);
1934 mtx_assert(&mq->
mq_mutex, MA_OWNED);
1948 const struct mq_attr *attr)
1952 struct filedesc *fdp;
1955 int fd, error, len, cmode;
1957 fdp = td->td_proc->p_fd;
1958 cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1960 if ((flags & O_CREAT) != 0 && attr != NULL) {
1961 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1963 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1967 error = copyinstr(upath, path,
MQFS_NAMELEN + 1, NULL);
1977 if (len < 2 || path[0] !=
'/' || index(path + 1,
'/') != NULL)
1980 error =
falloc(td, &fp, &fd, O_CLOEXEC);
1987 if (!(flags & O_CREAT)) {
1995 path + 1, len - 1, td->td_ucred,
2008 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2017 error =
vaccess(VREG, pn->mn_mode, pn->mn_uid,
2018 pn->mn_gid, accmode, td->td_ucred, NULL);
2032 finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2035 td->td_retval[0] =
fd;
2046 struct mq_attr attr;
2049 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2051 flags = FFLAGS(uap->flags);
2052 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2053 error = copyin(uap->attr, &attr,
sizeof(attr));
2058 uap->attr != NULL ? &attr : NULL));
2071 error = copyinstr(uap->path, path,
MQFS_NAMELEN + 1, NULL);
2076 if (len < 2 || path[0] !=
'/' || index(path + 1,
'/') != NULL)
2089 typedef int (*
_fgetf)(
struct thread *, int, cap_rights_t,
struct file **);
2101 error = func(td, fd, rights, fpp);
2108 pn = (*fpp)->f_data;
2120 return _getmq(td, fd, CAP_POLL_EVENT,
fget, fpp, ppn, pmq);
2139 struct mq_attr *oattr)
2146 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2148 error =
getmq(td, mqd, &fp, NULL, &mq);
2156 oflag = flag = fp->f_flag;
2157 flag &= ~O_NONBLOCK;
2158 flag |= (attr->mq_flags & O_NONBLOCK);
2159 }
while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2162 oattr->mq_flags = (O_NONBLOCK & oflag);
2170 struct mq_attr attr, oattr;
2173 if (uap->attr != NULL) {
2174 error = copyin(uap->attr, &attr,
sizeof(attr));
2182 if (uap->oattr != NULL)
2183 error = copyout(&oattr, uap->oattr,
sizeof(oattr));
2192 struct timespec *abs_timeout, ets;
2196 error =
getmq_read(td, uap->mqd, &fp, NULL, &mq);
2199 if (uap->abs_timeout != NULL) {
2200 error = copyin(uap->abs_timeout, &ets,
sizeof(ets));
2206 waitok = !(fp->f_flag & O_NONBLOCK);
2208 uap->msg_prio, waitok, abs_timeout);
2218 struct timespec *abs_timeout, ets;
2221 error =
getmq_write(td, uap->mqd, &fp, NULL, &mq);
2224 if (uap->abs_timeout != NULL) {
2225 error = copyin(uap->abs_timeout, &ets,
sizeof(ets));
2231 waitok = !(fp->f_flag & O_NONBLOCK);
2232 error =
mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2233 uap->msg_prio, waitok, abs_timeout);
2241 struct filedesc *fdp;
2244 struct file *fp, *fp2;
2248 if (sigev != NULL) {
2249 if (sigev->sigev_notify != SIGEV_SIGNAL &&
2250 sigev->sigev_notify != SIGEV_THREAD_ID &&
2251 sigev->sigev_notify != SIGEV_NONE)
2253 if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2254 sigev->sigev_notify == SIGEV_THREAD_ID) &&
2255 !_SIG_VALID(sigev->sigev_signo))
2259 fdp = td->td_proc->p_fd;
2260 error =
getmq(td, mqd, &fp, NULL, &mq);
2264 FILEDESC_SLOCK(fdp);
2265 fp2 = fget_locked(fdp, mqd);
2267 FILEDESC_SUNLOCK(fdp);
2273 FILEDESC_SUNLOCK(fdp);
2277 FILEDESC_SUNLOCK(fdp);
2282 FILEDESC_SUNLOCK(fdp);
2283 if (sigev != NULL) {
2290 if (newnt == NULL) {
2300 if (newnt != NULL) {
2307 ksiginfo_init(&nt->nt_ksi);
2308 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2309 nt->nt_ksi.ksi_code = SI_MESGQ;
2311 nt->nt_ksi.ksi_mqd = mqd;
2314 nt->nt_sigev = *sigev;
2341 struct sigevent ev, *evp;
2344 if (uap->sigev == NULL) {
2347 error = copyin(uap->sigev, &ev,
sizeof(ev));
2358 struct filedesc *fdp;
2361 fdp = td->td_proc->p_fd;
2362 FILEDESC_LOCK_ASSERT(fdp);
2385 struct filedesc *fdp;
2391 FILEDESC_SLOCK(fdp);
2392 for (i = 0; i < fdp->fd_nfiles; ++i) {
2393 fp = fget_locked(fdp, i);
2394 if (fp != NULL && fp->f_ops == &
mqueueops) {
2401 FILEDESC_SUNLOCK(fdp);
2402 KASSERT(LIST_EMPTY(&p->p_mqnotifier), (
"mq notifiers left"));
2406 mqf_read(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
2407 int flags,
struct thread *td)
2409 return (EOPNOTSUPP);
2413 mqf_write(
struct file *fp,
struct uio *uio,
struct ucred *active_cred,
2414 int flags,
struct thread *td)
2416 return (EOPNOTSUPP);
2429 struct ucred *active_cred,
struct thread *td)
2435 mqf_poll(
struct file *fp,
int events,
struct ucred *active_cred,
2442 if (events & (POLLIN | POLLRDNORM)) {
2444 revents |= events & (POLLIN | POLLRDNORM);
2450 if (events & POLLOUT) {
2477 mqf_stat(
struct file *fp,
struct stat *st,
struct ucred *active_cred,
2482 bzero(st,
sizeof *st);
2484 st->st_atim = pn->mn_atime;
2485 st->st_mtim = pn->mn_mtime;
2486 st->st_ctim = pn->mn_ctime;
2487 st->st_birthtim = pn->mn_birth;
2488 st->st_uid = pn->mn_uid;
2489 st->st_gid = pn->mn_gid;
2490 st->st_mode = S_IFIFO | pn->mn_mode;
2496 mqf_chmod(
struct file *fp, mode_t mode,
struct ucred *active_cred,
2505 error =
vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2509 pn->mn_mode = mode & ACCESSPERMS;
2516 mqf_chown(
struct file *fp, uid_t uid, gid_t gid,
struct ucred *active_cred,
2525 if (uid == (uid_t)-1)
2527 if (gid == (gid_t)-1)
2529 if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2530 (gid != pn->mn_gid && !
groupmember(gid, active_cred))) &&
2546 if (kn->kn_filter == EVFILT_READ) {
2549 }
else if (kn->kn_filter == EVFILT_WRITE) {
2562 if (kn->kn_filter == EVFILT_READ)
2564 else if (kn->kn_filter == EVFILT_WRITE)
2567 panic(
"filt_mqdetach");
2575 mtx_assert(&mq->
mq_mutex, MA_OWNED);
2584 mtx_assert(&mq->
mq_mutex, MA_OWNED);
2615 .vop_write = VOP_EOPNOTSUPP,
2617 .vop_mkdir = VOP_EOPNOTSUPP,
2618 .vop_rmdir = VOP_EOPNOTSUPP
2631 .vfc_version = VFS_VERSION,
2632 .vfc_name =
"mqueuefs",
2635 .vfc_flags = VFCF_SYNTHETIC
2639 SYSCALL_INIT_HELPER(kmq_open),
2640 SYSCALL_INIT_HELPER(kmq_setattr),
2641 SYSCALL_INIT_HELPER(kmq_timedsend),
2642 SYSCALL_INIT_HELPER(kmq_timedreceive),
2643 SYSCALL_INIT_HELPER(kmq_notify),
2644 SYSCALL_INIT_HELPER(kmq_unlink),
2649 #include <compat/compat32bit/compat32bit.h>
2650 #include <compat/compat32bit/compat32bit_proto.h>
2651 #include <compat/compat32bit/compat32bit_signal.h>
2652 #include <compat/compat32bit/compat32bit_syscall.h>
2653 #include <compat/compat32bit/compat32bit_util.h>
2656 mq_attr_from32(
const struct mq_attr32 *from,
struct mq_attr *to)
2659 to->mq_flags = from->mq_flags;
2660 to->mq_maxmsg = from->mq_maxmsg;
2661 to->mq_msgsize = from->mq_msgsize;
2662 to->mq_curmsgs = from->mq_curmsgs;
2666 mq_attr_to32(
const struct mq_attr *from,
struct mq_attr32 *to)
2669 to->mq_flags = from->mq_flags;
2670 to->mq_maxmsg = from->mq_maxmsg;
2671 to->mq_msgsize = from->mq_msgsize;
2672 to->mq_curmsgs = from->mq_curmsgs;
2676 compat32bit_kmq_open(
struct thread *td,
struct compat32bit_kmq_open_args *uap)
2678 struct mq_attr attr;
2679 struct mq_attr32 attr32;
2682 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2684 flags = FFLAGS(uap->flags);
2685 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2686 error = copyin(uap->attr, &attr32,
sizeof(attr32));
2689 mq_attr_from32(&attr32, &attr);
2692 uap->attr != NULL ? &attr : NULL));
2696 compat32bit_kmq_setattr(
struct thread *td,
struct compat32bit_kmq_setattr_args *uap)
2698 struct mq_attr attr, oattr;
2699 struct mq_attr32 attr32, oattr32;
2702 if (uap->attr != NULL) {
2703 error = copyin(uap->attr, &attr32,
sizeof(attr32));
2706 mq_attr_from32(&attr32, &attr);
2712 if (uap->oattr != NULL) {
2713 mq_attr_to32(&oattr, &oattr32);
2714 error = copyout(&oattr32, uap->oattr,
sizeof(oattr32));
2720 compat32bit_kmq_timedsend(
struct thread *td,
2721 struct compat32bit_kmq_timedsend_args *uap)
2725 struct timespec32 ets32;
2726 struct timespec *abs_timeout, ets;
2730 error =
getmq_read(td, uap->mqd, &fp, NULL, &mq);
2733 if (uap->abs_timeout != NULL) {
2734 error = copyin(uap->abs_timeout, &ets32,
sizeof(ets32));
2737 CP(ets32, ets, tv_sec);
2738 CP(ets32, ets, tv_nsec);
2742 waitok = !(fp->f_flag & O_NONBLOCK);
2743 error =
mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2744 uap->msg_prio, waitok, abs_timeout);
2750 compat32bit_kmq_timedreceive(
struct thread *td,
2751 struct compat32bit_kmq_timedreceive_args *uap)
2755 struct timespec32 ets32;
2756 struct timespec *abs_timeout, ets;
2759 error =
getmq_write(td, uap->mqd, &fp, NULL, &mq);
2762 if (uap->abs_timeout != NULL) {
2763 error = copyin(uap->abs_timeout, &ets32,
sizeof(ets32));
2766 CP(ets32, ets, tv_sec);
2767 CP(ets32, ets, tv_nsec);
2771 waitok = !(fp->f_flag & O_NONBLOCK);
2773 uap->msg_prio, waitok, abs_timeout);
2779 compat32bit_kmq_notify(
struct thread *td,
struct compat32bit_kmq_notify_args *uap)
2781 struct sigevent ev, *evp;
2782 struct sigevent32 ev32;
2785 if (uap->sigev == NULL) {
2788 error = copyin(uap->sigev, &ev32,
sizeof(ev32));
2791 error = convert_sigevent32(&ev32, &ev);
2799 static struct syscall_helper_data mq32_syscalls[] = {
2800 SYSCALL32_INIT_HELPER(compat32bit_kmq_open),
2801 SYSCALL32_INIT_HELPER(compat32bit_kmq_setattr),
2802 SYSCALL32_INIT_HELPER(compat32bit_kmq_timedsend),
2803 SYSCALL32_INIT_HELPER(compat32bit_kmq_timedreceive),
2804 SYSCALL32_INIT_HELPER(compat32bit_kmq_notify),
2805 SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2819 error = syscall32_helper_register(mq32_syscalls);
2831 syscall32_helper_unregister(mq32_syscalls);
2866 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
static void mqfs_fileno_uninit(struct mqfs_info *mi)
static void mqueue_send_notification(struct mqueue *mq)
static struct vop_vector mqfs_vnodeops
static void notifier_remove(struct proc *p, struct mqueue *mq, int fd)
int syscall_helper_unregister(struct syscall_helper_data *sd)
int tvtohz(struct timeval *tv)
static void mqfs_fileno_init(struct mqfs_info *mi)
static moduledata_t mqueuefs_mod
static int mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
static int _getmq(struct thread *td, int fd, cap_rights_t rights, _fgetf func, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static struct mqueue_notifier * notifier_search(struct proc *p, int fd)
char mn_name[MQFS_NAMELEN+1]
int uiomove_frombuf(void *buf, int buflen, struct uio *uio)
void selwakeup(struct selinfo *sip)
static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
static int mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
int syscall_helper_register(struct syscall_helper_data *sd)
int snprintf(char *str, size_t size, const char *format,...)
static int mqfs_init(struct vfsconf *vfc)
static void filt_mqdetach(struct knote *kn)
int priv_check_cred(struct ucred *cred, int priv, int flags)
int sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
void selrecord(struct thread *selector, struct selinfo *sip)
int(* _fgetf)(struct thread *, int, cap_rights_t, struct file **)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
void cache_purge(struct vnode *vp)
static int mqf_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred, struct thread *td)
int vfs_cache_lookup(struct vop_lookup_args *ap)
int cap_funwrap(struct file *fp_cap, cap_rights_t rights, struct file **fpp)
void sigqueue_take(ksiginfo_t *ksi)
FEATURE(p1003_1b_mqueue,"POSIX P1003.1B message queues support")
void(* mq_fdclose)(struct thread *td, int fd, struct file *fp)
static struct mqfs_info mqfs_data
void panic(const char *fmt,...)
int vget(struct vnode *vp, int flags, struct thread *td)
static int mqfs_close(struct vop_close_args *ap)
static void mqueue_fdclose(struct thread *td, int fd, struct file *fp)
static int mqf_close(struct file *fp, struct thread *td)
static int mqfs_read(struct vop_read_args *ap)
static __inline void mqnode_release(struct mqfs_node *node)
void knote(struct knlist *list, long hint, int lockflags)
static __inline void mqnode_addref(struct mqfs_node *node)
static int mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, struct thread *td)
int alloc_unr(struct unrhdr *uh)
static uma_zone_t mqnoti_zone
static struct vfsconf mqueuefs_vfsconf
struct mqfs_node * mn_parent
static uma_zone_t mvdata_zone
static __inline void notifier_insert(struct proc *p, struct mqueue_notifier *nt)
int falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
static int mq_modload(struct module *module, int cmd, void *arg)
struct filterops mq_rfiltops
static int mqfs_lookupx(struct vop_cachedlookup_args *ap)
void knlist_destroy(struct knlist *knl)
static __inline void mqnode_free(struct mqfs_node *node)
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)
static int mqfs_remove(struct vop_remove_args *ap)
static __inline struct mqueue_notifier * notifier_alloc(void)
int fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
struct vfsconfhead vfsconf
static int mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
static int mqfs_mount(struct mount *mp)
static int mqfs_reclaim(struct vop_reclaim_args *ap)
static int mqfs_lookup(struct vop_cachedlookup_args *ap)
void vdrop(struct vnode *vp)
static __inline void notifier_delete(struct proc *p, struct mqueue_notifier *nt)
void wakeup_one(void *ident)
static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
static int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
static eventhandler_tag exit_tag
static void mq_proc_exit(void *arg, struct proc *p)
static int mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
void vfs_timestamp(struct timespec *tsp)
static int mqfs_unmount(struct mount *mp, int mntflags)
int priv_check(struct thread *td, int priv)
struct vop_vector default_vnodeops
static int mqunload(void)
static int mqfs_setattr(struct vop_setattr_args *ap)
static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,"POSIX real time message queue")
void vput(struct vnode *vp)
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 struct mqueue_msg * mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
static __inline void mqueue_freemsg(struct mqueue_msg *msg)
static struct mqueue * mqueue_alloc(const struct mq_attr *attr)
int vrecycle(struct vnode *vp, struct thread *td)
void vfs_getnewfsid(struct mount *mp)
static int default_msgsize
void p31b_setcfg(int num, int value)
static int filt_mqread(struct knote *kn, long hint)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
static int mqfs_access(struct vop_access_args *ap)
static uma_zone_t mqnode_zone
static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
int sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
static int mqf_kqfilter(struct file *fp, struct knote *kn)
void seldrain(struct selinfo *sip)
void vfs_mountedfrom(struct mount *mp, const char *from)
static int mqfs_destroy(struct mqfs_node *mn)
static int mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
int sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
static struct mqfs_node * mqfs_search(struct mqfs_node *pd, const char *name, int len)
static int filt_mqwrite(struct knote *kn, long hint)
int sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
static int mqfs_statfs(struct mount *mp, struct statfs *sbp)
static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
int vfs_modevent(module_t mod, int type, void *data)
struct unrhdr * new_unrhdr(int low, int high, struct mtx *mutex)
static struct vfsops mqfs_vfsops
int sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
static int default_maxmsg
static int mqfs_fixup_dir(struct mqfs_node *parent)
static int mqfs_create(struct vop_create_args *ap)
static int mqfs_uninit(struct vfsconf *vfc)
void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
int getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, struct vnode **vpp)
static int mqf_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
static __inline struct mqfs_node * mqnode_alloc(void)
static int mqfs_readdir(struct vop_readdir_args *ap)
void getnanotime(struct timespec *tsp)
void cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
static int kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr, struct mq_attr *oattr)
static struct syscall_helper_data mq_syscalls[]
static struct mqfs_node * mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode, int nodetype)
SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,&maxmsg, 0,"Default maximum messages in queue")
int groupmember(gid_t gid, struct ucred *cred)
static MALLOC_DEFINE(M_MQUEUEDATA,"mqdata","mqueue data")
static uma_zone_t mqueue_zone
static void mqueue_free(struct mqueue *mq)
static void do_recycle(void *context, int pending __unused)
int vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
void free(void *addr, struct malloc_type *mtp)
int taskqueue_enqueue(struct taskqueue *queue, struct task *task)
static int mqfs_open(struct vop_open_args *ap)
static int kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
TAILQ_HEAD(msgq, mqueue_msg)
struct mqfs_info * mn_info
static struct mqfs_node * mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen, struct ucred *cred, int mode)
void finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE)
void vhold(struct vnode *vp)
static int mqfs_getattr(struct vop_getattr_args *ap)
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
int insmntque(struct vnode *vp, struct mount *mp)
int sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
static int mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
static int mqfs_inactive(struct vop_inactive_args *ap)
int fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
static __inline int getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static int mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred, struct thread *td)
int tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
void vgone(struct vnode *vp)
static __inline int getmq_read(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
static __inline int getmq_write(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
int fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
void delete_unrhdr(struct unrhdr *uh)
int vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
static int mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
void mtx_destroy(struct mtx *m)
struct filterops mq_wfiltops
static int kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode, const struct mq_attr *attr)
void free_unr(struct unrhdr *uh, u_int item)
static int mqueue_receive(struct mqueue *mq, char *msg_ptr, size_t msg_len, unsigned *msg_prio, int waitok, const struct timespec *abs_timeout)
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
int sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
struct mqfs_node * mi_root
static struct fileops mqueueops
struct unrhdr * mi_unrhdr
static __inline void notifier_free(struct mqueue_notifier *p)
MODULE_VERSION(mqueuefs, 1)
struct mqueue_notifier * mq_notifier
void sx_destroy(struct sx *sx)
struct fileops badfileops