2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * POSIX message queue implementation.
31 * 1) A mqueue filesystem can be mounted, each message queue appears
32 * in mounted directory, user can change queue's permission and
33 * ownership, or remove a queue. Manually creating a file in the
34 * directory causes a message queue to be created in the kernel with
35 * default message queue attributes applied and same name used, this
36 * method is not advocated since mq_open syscall allows user to specify
37 * different attributes. Also the file system can be mounted multiple
38 * times at different mount points but shows same contents.
40 * 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
41 * but directly operate on internal data structure, this allows user to
42 * use the IPC facility without having to mount mqueue file system.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_capsicum.h"
49 #include "opt_compat.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/limits.h>
56 #include <sys/capsicum.h>
57 #include <sys/dirent.h>
58 #include <sys/event.h>
59 #include <sys/eventhandler.h>
60 #include <sys/fcntl.h>
62 #include <sys/filedesc.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/mount.h>
67 #include <sys/mqueue.h>
68 #include <sys/mutex.h>
69 #include <sys/namei.h>
70 #include <sys/posix4.h>
74 #include <sys/queue.h>
75 #include <sys/sysproto.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysent.h>
81 #include <sys/sysctl.h>
82 #include <sys/taskqueue.h>
83 #include <sys/unistd.h>
85 #include <sys/vnode.h>
86 #include <machine/atomic.h>
88 FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
91 * Limits and constants
93 #define MQFS_NAMELEN NAME_MAX
94 #define MQFS_DELEN (8 + MQFS_NAMELEN)
110 * mqfs_info: describes a mqfs instance
114 struct mqfs_node *mi_root;
115 struct unrhdr *mi_unrhdr;
119 LIST_ENTRY(mqfs_vdata) mv_link;
120 struct mqfs_node *mv_node;
121 struct vnode *mv_vnode;
126 * mqfs_node: describes a node (file or directory) within a mqfs
129 char mn_name[MQFS_NAMELEN+1];
130 struct mqfs_info *mn_info;
131 struct mqfs_node *mn_parent;
132 LIST_HEAD(,mqfs_node) mn_children;
133 LIST_ENTRY(mqfs_node) mn_sibling;
134 LIST_HEAD(,mqfs_vdata) mn_vnodes;
140 struct timespec mn_birth;
141 struct timespec mn_ctime;
142 struct timespec mn_atime;
143 struct timespec mn_mtime;
149 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
150 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
151 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
152 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
153 (fp)->f_data)->mn_data))
155 TAILQ_HEAD(msgq, mqueue_msg);
159 struct mqueue_notifier {
160 LIST_ENTRY(mqueue_notifier) nt_link;
161 struct sigevent nt_sigev;
163 struct proc *nt_proc;
176 struct selinfo mq_rsel;
177 struct selinfo mq_wsel;
178 struct mqueue_notifier *mq_notifier;
185 TAILQ_ENTRY(mqueue_msg) msg_link;
186 unsigned int msg_prio;
187 unsigned int msg_size;
188 /* following real data... */
191 static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
192 "POSIX real time message queue");
194 static int default_maxmsg = 10;
195 static int default_msgsize = 1024;
197 static int maxmsg = 100;
198 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
199 &maxmsg, 0, "Default maximum messages in queue");
200 static int maxmsgsize = 16384;
201 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
202 &maxmsgsize, 0, "Default maximum message size");
203 static int maxmq = 100;
204 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
205 &maxmq, 0, "maximum message queues");
206 static int curmq = 0;
207 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
208 &curmq, 0, "current message queue number");
209 static int unloadable = 0;
210 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
212 static eventhandler_tag exit_tag;
214 /* Only one instance per-system */
215 static struct mqfs_info mqfs_data;
216 static uma_zone_t mqnode_zone;
217 static uma_zone_t mqueue_zone;
218 static uma_zone_t mvdata_zone;
219 static uma_zone_t mqnoti_zone;
220 static struct vop_vector mqfs_vnodeops;
221 static struct fileops mqueueops;
224 * Directory structure construction and manipulation
227 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
228 const char *name, int namelen, struct ucred *cred, int mode);
229 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
230 const char *name, int namelen, struct ucred *cred, int mode);
233 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
234 const char *name, int namelen, struct ucred *cred, int mode);
235 static int mqfs_destroy(struct mqfs_node *mn);
236 static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
237 static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
238 static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
241 * Message queue construction and maniplation
243 static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
244 static void mqueue_free(struct mqueue *mq);
245 static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
246 size_t msg_len, unsigned msg_prio, int waitok,
247 const struct timespec *abs_timeout);
248 static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
249 size_t msg_len, unsigned *msg_prio, int waitok,
250 const struct timespec *abs_timeout);
251 static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
253 static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
255 static void mqueue_send_notification(struct mqueue *mq);
256 static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
257 static void mq_proc_exit(void *arg, struct proc *p);
262 static void filt_mqdetach(struct knote *kn);
263 static int filt_mqread(struct knote *kn, long hint);
264 static int filt_mqwrite(struct knote *kn, long hint);
266 struct filterops mq_rfiltops = {
268 .f_detach = filt_mqdetach,
269 .f_event = filt_mqread,
271 struct filterops mq_wfiltops = {
273 .f_detach = filt_mqdetach,
274 .f_event = filt_mqwrite,
278 * Initialize fileno bitmap
281 mqfs_fileno_init(struct mqfs_info *mi)
285 up = new_unrhdr(1, INT_MAX, NULL);
290 * Tear down fileno bitmap
293 mqfs_fileno_uninit(struct mqfs_info *mi)
298 mi->mi_unrhdr = NULL;
303 * Allocate a file number
306 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
308 /* make sure our parent has a file number */
309 if (mn->mn_parent && !mn->mn_parent->mn_fileno)
310 mqfs_fileno_alloc(mi, mn->mn_parent);
312 switch (mn->mn_type) {
316 case mqfstype_symlink:
317 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
320 KASSERT(mn->mn_parent != NULL,
321 ("mqfstype_this node has no parent"));
322 mn->mn_fileno = mn->mn_parent->mn_fileno;
324 case mqfstype_parent:
325 KASSERT(mn->mn_parent != NULL,
326 ("mqfstype_parent node has no parent"));
327 if (mn->mn_parent == mi->mi_root) {
328 mn->mn_fileno = mn->mn_parent->mn_fileno;
331 KASSERT(mn->mn_parent->mn_parent != NULL,
332 ("mqfstype_parent node has no grandparent"));
333 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
337 ("mqfs_fileno_alloc() called for unknown type node: %d",
344 * Release a file number
347 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
349 switch (mn->mn_type) {
353 case mqfstype_symlink:
354 free_unr(mi->mi_unrhdr, mn->mn_fileno);
357 case mqfstype_parent:
358 /* ignore these, as they don't "own" their file number */
362 ("mqfs_fileno_free() called for unknown type node: %d",
368 static __inline struct mqfs_node *
371 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
375 mqnode_free(struct mqfs_node *node)
377 uma_zfree(mqnode_zone, node);
381 mqnode_addref(struct mqfs_node *node)
383 atomic_fetchadd_int(&node->mn_refcount, 1);
387 mqnode_release(struct mqfs_node *node)
389 struct mqfs_info *mqfs;
392 mqfs = node->mn_info;
393 old = atomic_fetchadd_int(&node->mn_refcount, -1);
394 if (node->mn_type == mqfstype_dir ||
395 node->mn_type == mqfstype_root)
396 exp = 3; /* include . and .. */
400 int locked = sx_xlocked(&mqfs->mi_lock);
402 sx_xlock(&mqfs->mi_lock);
405 sx_xunlock(&mqfs->mi_lock);
410 * Add a node to a directory
413 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
415 KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
416 KASSERT(parent->mn_info != NULL,
417 ("%s(): parent has no mn_info", __func__));
418 KASSERT(parent->mn_type == mqfstype_dir ||
419 parent->mn_type == mqfstype_root,
420 ("%s(): parent is not a directory", __func__));
422 node->mn_info = parent->mn_info;
423 node->mn_parent = parent;
424 LIST_INIT(&node->mn_children);
425 LIST_INIT(&node->mn_vnodes);
426 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
427 mqnode_addref(parent);
431 static struct mqfs_node *
432 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
435 struct mqfs_node *node;
437 node = mqnode_alloc();
438 strncpy(node->mn_name, name, namelen);
439 node->mn_type = nodetype;
440 node->mn_refcount = 1;
441 vfs_timestamp(&node->mn_birth);
442 node->mn_ctime = node->mn_atime = node->mn_mtime
444 node->mn_uid = cred->cr_uid;
445 node->mn_gid = cred->cr_gid;
446 node->mn_mode = mode;
453 static struct mqfs_node *
454 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
455 struct ucred *cred, int mode)
457 struct mqfs_node *node;
459 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
460 if (mqfs_add_node(parent, node) != 0) {
468 * Add . and .. to a directory
471 mqfs_fixup_dir(struct mqfs_node *parent)
473 struct mqfs_node *dir;
475 dir = mqnode_alloc();
476 dir->mn_name[0] = '.';
477 dir->mn_type = mqfstype_this;
478 dir->mn_refcount = 1;
479 if (mqfs_add_node(parent, dir) != 0) {
484 dir = mqnode_alloc();
485 dir->mn_name[0] = dir->mn_name[1] = '.';
486 dir->mn_type = mqfstype_parent;
487 dir->mn_refcount = 1;
489 if (mqfs_add_node(parent, dir) != 0) {
502 static struct mqfs_node *
503 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
504 struct ucred *cred, int mode)
506 struct mqfs_node *node;
508 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
509 if (mqfs_add_node(parent, node) != 0) {
514 if (mqfs_fixup_dir(node) != 0) {
524 static struct mqfs_node *
525 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
526 struct ucred *cred, int mode)
528 struct mqfs_node *node;
530 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
531 if (mqfs_add_node(parent, node) != 0) {
541 * Destroy a node or a tree of nodes
544 mqfs_destroy(struct mqfs_node *node)
546 struct mqfs_node *parent;
548 KASSERT(node != NULL,
549 ("%s(): node is NULL", __func__));
550 KASSERT(node->mn_info != NULL,
551 ("%s(): node has no mn_info", __func__));
553 /* destroy children */
554 if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
555 while (! LIST_EMPTY(&node->mn_children))
556 mqfs_destroy(LIST_FIRST(&node->mn_children));
558 /* unlink from parent */
559 if ((parent = node->mn_parent) != NULL) {
560 KASSERT(parent->mn_info == node->mn_info,
561 ("%s(): parent has different mn_info", __func__));
562 LIST_REMOVE(node, mn_sibling);
565 if (node->mn_fileno != 0)
566 mqfs_fileno_free(node->mn_info, node);
567 if (node->mn_data != NULL)
568 mqueue_free(node->mn_data);
574 * Mount a mqfs instance
577 mqfs_mount(struct mount *mp)
581 if (mp->mnt_flag & MNT_UPDATE)
584 mp->mnt_data = &mqfs_data;
586 mp->mnt_flag |= MNT_LOCAL;
591 vfs_mountedfrom(mp, "mqueue");
592 sbp->f_bsize = PAGE_SIZE;
593 sbp->f_iosize = PAGE_SIZE;
603 * Unmount a mqfs instance
606 mqfs_unmount(struct mount *mp, int mntflags)
610 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
616 * Return a root vnode
619 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
621 struct mqfs_info *mqfs;
624 mqfs = VFSTOMQFS(mp);
625 ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
630 * Return filesystem stats
633 mqfs_statfs(struct mount *mp, struct statfs *sbp)
635 /* XXX update statistics */
640 * Initialize a mqfs instance
643 mqfs_init(struct vfsconf *vfc)
645 struct mqfs_node *root;
646 struct mqfs_info *mi;
648 mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
649 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
650 mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
651 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
652 mvdata_zone = uma_zcreate("mvdata",
653 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
654 NULL, UMA_ALIGN_PTR, 0);
655 mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
656 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
658 sx_init(&mi->mi_lock, "mqfs lock");
659 /* set up the root diretory */
660 root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
663 LIST_INIT(&root->mn_children);
664 LIST_INIT(&root->mn_vnodes);
666 mqfs_fileno_init(mi);
667 mqfs_fileno_alloc(mi, root);
668 mqfs_fixup_dir(root);
669 exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
670 EVENTHANDLER_PRI_ANY);
671 mq_fdclose = mqueue_fdclose;
672 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
677 * Destroy a mqfs instance
680 mqfs_uninit(struct vfsconf *vfc)
682 struct mqfs_info *mi;
686 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
688 mqfs_destroy(mi->mi_root);
690 mqfs_fileno_uninit(mi);
691 sx_destroy(&mi->mi_lock);
692 uma_zdestroy(mqnode_zone);
693 uma_zdestroy(mqueue_zone);
694 uma_zdestroy(mvdata_zone);
695 uma_zdestroy(mqnoti_zone);
703 do_recycle(void *context, int pending __unused)
705 struct vnode *vp = (struct vnode *)context;
715 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
717 struct mqfs_vdata *vd;
718 struct mqfs_info *mqfs;
719 struct vnode *newvpp;
724 sx_xlock(&mqfs->mi_lock);
725 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
726 if (vd->mv_vnode->v_mount == mp) {
735 sx_xunlock(&mqfs->mi_lock);
736 error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
740 sx_xunlock(&mqfs->mi_lock);
742 error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
745 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
746 error = insmntque(newvpp, mp);
750 sx_xlock(&mqfs->mi_lock);
752 * Check if it has already been allocated
753 * while we were blocked.
755 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
756 if (vd->mv_vnode->v_mount == mp) {
758 sx_xunlock(&mqfs->mi_lock);
768 vd = uma_zalloc(mvdata_zone, M_WAITOK);
772 TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
773 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
775 switch (pn->mn_type) {
777 (*vpp)->v_vflag = VV_ROOT;
781 case mqfstype_parent:
782 (*vpp)->v_type = VDIR;
785 (*vpp)->v_type = VREG;
787 case mqfstype_symlink:
788 (*vpp)->v_type = VLNK;
791 KASSERT(0, ("mqfs_allocf called for null node\n"));
793 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
795 sx_xunlock(&mqfs->mi_lock);
800 * Search a directory entry
802 static struct mqfs_node *
803 mqfs_search(struct mqfs_node *pd, const char *name, int len)
805 struct mqfs_node *pn;
807 sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
808 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
809 if (strncmp(pn->mn_name, name, len) == 0 &&
810 pn->mn_name[len] == '\0')
817 * Look up a file or directory.
820 mqfs_lookupx(struct vop_cachedlookup_args *ap)
822 struct componentname *cnp;
823 struct vnode *dvp, **vpp;
824 struct mqfs_node *pd;
825 struct mqfs_node *pn;
826 struct mqfs_info *mqfs;
827 int nameiop, flags, error, namelen;
834 pname = cnp->cn_nameptr;
835 namelen = cnp->cn_namelen;
837 flags = cnp->cn_flags;
838 nameiop = cnp->cn_nameiop;
844 if (dvp->v_type != VDIR)
847 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
851 /* shortcut: check if the name is too long */
852 if (cnp->cn_namelen >= MQFS_NAMELEN)
856 if (namelen == 1 && pname[0] == '.') {
857 if ((flags & ISLASTCN) && nameiop != LOOKUP)
866 if (cnp->cn_flags & ISDOTDOT) {
867 if (dvp->v_vflag & VV_ROOT)
869 if ((flags & ISLASTCN) && nameiop != LOOKUP)
872 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
874 error = mqfs_allocv(dvp->v_mount, vpp, pn);
875 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
880 sx_xlock(&mqfs->mi_lock);
881 pn = mqfs_search(pd, pname, namelen);
884 sx_xunlock(&mqfs->mi_lock);
889 if (nameiop == DELETE && (flags & ISLASTCN)) {
890 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
904 error = mqfs_allocv(dvp->v_mount, vpp, pn);
906 if (error == 0 && cnp->cn_flags & MAKEENTRY)
907 cache_enter(dvp, *vpp, cnp);
913 /* will create a new entry in the directory ? */
914 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
915 && (flags & ISLASTCN)) {
916 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
919 cnp->cn_flags |= SAVENAME;
920 return (EJUSTRETURN);
926 struct vop_lookup_args {
927 struct vop_generic_args a_gen;
929 struct vnode **a_vpp;
930 struct componentname *a_cnp;
935 * vnode lookup operation
938 mqfs_lookup(struct vop_cachedlookup_args *ap)
942 rc = mqfs_lookupx(ap);
947 struct vop_create_args {
949 struct vnode **a_vpp;
950 struct componentname *a_cnp;
956 * vnode creation operation
959 mqfs_create(struct vop_create_args *ap)
961 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
962 struct componentname *cnp = ap->a_cnp;
963 struct mqfs_node *pd;
964 struct mqfs_node *pn;
968 pd = VTON(ap->a_dvp);
969 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
971 mq = mqueue_alloc(NULL);
974 sx_xlock(&mqfs->mi_lock);
975 if ((cnp->cn_flags & HASBUF) == 0)
976 panic("%s: no name", __func__);
977 pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
978 cnp->cn_cred, ap->a_vap->va_mode);
980 sx_xunlock(&mqfs->mi_lock);
984 sx_xunlock(&mqfs->mi_lock);
985 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1001 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1003 struct mqfs_node *parent;
1004 struct mqfs_vdata *vd;
1007 sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1009 if (ucred->cr_uid != pn->mn_uid &&
1010 (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1012 else if (!pn->mn_deleted) {
1013 parent = pn->mn_parent;
1014 pn->mn_parent = NULL;
1016 LIST_REMOVE(pn, mn_sibling);
1017 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1018 cache_purge(vd->mv_vnode);
1019 vhold(vd->mv_vnode);
1020 taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1023 mqnode_release(parent);
1030 struct vop_remove_args {
1031 struct vnode *a_dvp;
1033 struct componentname *a_cnp;
1038 * vnode removal operation
1041 mqfs_remove(struct vop_remove_args *ap)
1043 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1044 struct mqfs_node *pn;
1047 if (ap->a_vp->v_type == VDIR)
1049 pn = VTON(ap->a_vp);
1050 sx_xlock(&mqfs->mi_lock);
1051 error = do_unlink(pn, ap->a_cnp->cn_cred);
1052 sx_xunlock(&mqfs->mi_lock);
1057 struct vop_inactive_args {
1059 struct thread *a_td;
1064 mqfs_inactive(struct vop_inactive_args *ap)
1066 struct mqfs_node *pn = VTON(ap->a_vp);
1074 struct vop_reclaim_args {
1075 struct vop_generic_args a_gen;
1077 struct thread *a_td;
1082 mqfs_reclaim(struct vop_reclaim_args *ap)
1084 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1085 struct vnode *vp = ap->a_vp;
1086 struct mqfs_node *pn;
1087 struct mqfs_vdata *vd;
1091 sx_xlock(&mqfs->mi_lock);
1093 LIST_REMOVE(vd, mv_link);
1094 uma_zfree(mvdata_zone, vd);
1096 sx_xunlock(&mqfs->mi_lock);
1101 struct vop_open_args {
1102 struct vop_generic_args a_gen;
1105 struct ucred *a_cred;
1106 struct thread *a_td;
1112 mqfs_open(struct vop_open_args *ap)
1118 struct vop_close_args {
1119 struct vop_generic_args a_gen;
1122 struct ucred *a_cred;
1123 struct thread *a_td;
1128 mqfs_close(struct vop_close_args *ap)
1134 struct vop_access_args {
1135 struct vop_generic_args a_gen;
1137 accmode_t a_accmode;
1138 struct ucred *a_cred;
1139 struct thread *a_td;
1144 * Verify permissions
1147 mqfs_access(struct vop_access_args *ap)
1149 struct vnode *vp = ap->a_vp;
1153 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1156 error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1157 vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1162 struct vop_getattr_args {
1163 struct vop_generic_args a_gen;
1165 struct vattr *a_vap;
1166 struct ucred *a_cred;
1171 * Get file attributes
1174 mqfs_getattr(struct vop_getattr_args *ap)
1176 struct vnode *vp = ap->a_vp;
1177 struct mqfs_node *pn = VTON(vp);
1178 struct vattr *vap = ap->a_vap;
1181 vap->va_type = vp->v_type;
1182 vap->va_mode = pn->mn_mode;
1184 vap->va_uid = pn->mn_uid;
1185 vap->va_gid = pn->mn_gid;
1186 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1187 vap->va_fileid = pn->mn_fileno;
1189 vap->va_blocksize = PAGE_SIZE;
1190 vap->va_bytes = vap->va_size = 0;
1191 vap->va_atime = pn->mn_atime;
1192 vap->va_mtime = pn->mn_mtime;
1193 vap->va_ctime = pn->mn_ctime;
1194 vap->va_birthtime = pn->mn_birth;
1197 vap->va_rdev = NODEV;
1199 vap->va_filerev = 0;
1204 struct vop_setattr_args {
1205 struct vop_generic_args a_gen;
1207 struct vattr *a_vap;
1208 struct ucred *a_cred;
1215 mqfs_setattr(struct vop_setattr_args *ap)
1217 struct mqfs_node *pn;
1228 if ((vap->va_type != VNON) ||
1229 (vap->va_nlink != VNOVAL) ||
1230 (vap->va_fsid != VNOVAL) ||
1231 (vap->va_fileid != VNOVAL) ||
1232 (vap->va_blocksize != VNOVAL) ||
1233 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1234 (vap->va_rdev != VNOVAL) ||
1235 ((int)vap->va_bytes != VNOVAL) ||
1236 (vap->va_gen != VNOVAL)) {
1243 if (vap->va_uid == (uid_t)VNOVAL)
1247 if (vap->va_gid == (gid_t)VNOVAL)
1252 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1254 * To modify the ownership of a file, must possess VADMIN
1257 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1261 * XXXRW: Why is there a privilege check here: shouldn't the
1262 * check in VOP_ACCESS() be enough? Also, are the group bits
1263 * below definitely right?
1265 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1266 (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1267 (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1274 if (vap->va_mode != (mode_t)VNOVAL) {
1275 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1276 (error = priv_check(td, PRIV_MQ_ADMIN)))
1278 pn->mn_mode = vap->va_mode;
1282 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1283 /* See the comment in ufs_vnops::ufs_setattr(). */
1284 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1285 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1286 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1288 if (vap->va_atime.tv_sec != VNOVAL) {
1289 pn->mn_atime = vap->va_atime;
1291 if (vap->va_mtime.tv_sec != VNOVAL) {
1292 pn->mn_mtime = vap->va_mtime;
1297 vfs_timestamp(&pn->mn_ctime);
1303 struct vop_read_args {
1304 struct vop_generic_args a_gen;
1308 struct ucred *a_cred;
1316 mqfs_read(struct vop_read_args *ap)
1319 struct vnode *vp = ap->a_vp;
1320 struct uio *uio = ap->a_uio;
1321 struct mqfs_node *pn;
1325 if (vp->v_type != VREG)
1330 snprintf(buf, sizeof(buf),
1331 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1336 buf[sizeof(buf)-1] = '\0';
1338 error = uiomove_frombuf(buf, len, uio);
1343 struct vop_readdir_args {
1344 struct vop_generic_args a_gen;
1347 struct ucred *a_cred;
1355 * Return directory entries.
1358 mqfs_readdir(struct vop_readdir_args *ap)
1361 struct mqfs_info *mi;
1362 struct mqfs_node *pd;
1363 struct mqfs_node *pn;
1364 struct dirent entry;
1366 int *tmp_ncookies = NULL;
1371 mi = VFSTOMQFS(vp->v_mount);
1375 if (vp->v_type != VDIR)
1378 if (uio->uio_offset < 0)
1381 if (ap->a_ncookies != NULL) {
1382 tmp_ncookies = ap->a_ncookies;
1383 *ap->a_ncookies = 0;
1384 ap->a_ncookies = NULL;
1390 sx_xlock(&mi->mi_lock);
1392 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1393 entry.d_reclen = sizeof(entry);
1395 mqfs_fileno_alloc(mi, pn);
1396 entry.d_fileno = pn->mn_fileno;
1397 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1398 entry.d_name[i] = pn->mn_name[i];
1399 entry.d_name[i] = 0;
1401 switch (pn->mn_type) {
1405 case mqfstype_parent:
1406 entry.d_type = DT_DIR;
1409 entry.d_type = DT_REG;
1411 case mqfstype_symlink:
1412 entry.d_type = DT_LNK;
1415 panic("%s has unexpected node type: %d", pn->mn_name,
1418 if (entry.d_reclen > uio->uio_resid)
1420 if (offset >= uio->uio_offset) {
1421 error = vfs_read_dirent(ap, &entry, offset);
1425 offset += entry.d_reclen;
1427 sx_xunlock(&mi->mi_lock);
1429 uio->uio_offset = offset;
1431 if (tmp_ncookies != NULL)
1432 ap->a_ncookies = tmp_ncookies;
1440 struct vop_mkdir_args {
1441 struct vnode *a_dvp;
1442 struvt vnode **a_vpp;
1443 struvt componentname *a_cnp;
1444 struct vattr *a_vap;
1449 * Create a directory.
1452 mqfs_mkdir(struct vop_mkdir_args *ap)
1454 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1455 struct componentname *cnp = ap->a_cnp;
1456 struct mqfs_node *pd = VTON(ap->a_dvp);
1457 struct mqfs_node *pn;
1460 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1462 sx_xlock(&mqfs->mi_lock);
1463 if ((cnp->cn_flags & HASBUF) == 0)
1464 panic("%s: no name", __func__);
1465 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1466 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1469 sx_xunlock(&mqfs->mi_lock);
1473 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1480 struct vop_rmdir_args {
1481 struct vnode *a_dvp;
1483 struct componentname *a_cnp;
1488 * Remove a directory.
1491 mqfs_rmdir(struct vop_rmdir_args *ap)
1493 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1494 struct mqfs_node *pn = VTON(ap->a_vp);
1495 struct mqfs_node *pt;
1497 if (pn->mn_type != mqfstype_dir)
1500 sx_xlock(&mqfs->mi_lock);
1501 if (pn->mn_deleted) {
1502 sx_xunlock(&mqfs->mi_lock);
1506 pt = LIST_FIRST(&pn->mn_children);
1507 pt = LIST_NEXT(pt, mn_sibling);
1508 pt = LIST_NEXT(pt, mn_sibling);
1510 sx_xunlock(&mqfs->mi_lock);
1514 pn->mn_parent = NULL;
1516 LIST_REMOVE(pn, mn_sibling);
1519 sx_xunlock(&mqfs->mi_lock);
1520 cache_purge(ap->a_vp);
1527 * Allocate a message queue
1529 static struct mqueue *
1530 mqueue_alloc(const struct mq_attr *attr)
1536 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1537 TAILQ_INIT(&mq->mq_msgq);
1539 mq->mq_maxmsg = attr->mq_maxmsg;
1540 mq->mq_msgsize = attr->mq_msgsize;
1542 mq->mq_maxmsg = default_maxmsg;
1543 mq->mq_msgsize = default_msgsize;
1545 mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1546 knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1547 knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1548 atomic_add_int(&curmq, 1);
1553 * Destroy a message queue
1556 mqueue_free(struct mqueue *mq)
1558 struct mqueue_msg *msg;
1560 while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1561 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1562 free(msg, M_MQUEUEDATA);
1565 mtx_destroy(&mq->mq_mutex);
1566 seldrain(&mq->mq_rsel);
1567 seldrain(&mq->mq_wsel);
1568 knlist_destroy(&mq->mq_rsel.si_note);
1569 knlist_destroy(&mq->mq_wsel.si_note);
1570 uma_zfree(mqueue_zone, mq);
1571 atomic_add_int(&curmq, -1);
1575 * Load a message from user space
1577 static struct mqueue_msg *
1578 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1580 struct mqueue_msg *msg;
1584 len = sizeof(struct mqueue_msg) + msg_size;
1585 msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1586 error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1589 free(msg, M_MQUEUEDATA);
1592 msg->msg_size = msg_size;
1593 msg->msg_prio = msg_prio;
1599 * Save a message to user space
1602 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1606 error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1608 if (error == 0 && msg_prio != NULL)
1609 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1614 * Free a message's memory
1616 static __inline void
1617 mqueue_freemsg(struct mqueue_msg *msg)
1619 free(msg, M_MQUEUEDATA);
1623 * Send a message. if waitok is false, thread will not be
1624 * blocked if there is no data in queue, otherwise, absolute
1625 * time will be checked.
1628 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1629 size_t msg_len, unsigned msg_prio, int waitok,
1630 const struct timespec *abs_timeout)
1632 struct mqueue_msg *msg;
1633 struct timespec ts, ts2;
1637 if (msg_prio >= MQ_PRIO_MAX)
1639 if (msg_len > mq->mq_msgsize)
1641 msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1645 /* O_NONBLOCK case */
1647 error = _mqueue_send(mq, msg, -1);
1653 /* we allow a null timeout (wait forever) */
1654 if (abs_timeout == NULL) {
1655 error = _mqueue_send(mq, msg, 0);
1661 /* send it before checking time */
1662 error = _mqueue_send(mq, msg, -1);
1666 if (error != EAGAIN)
1669 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1676 timespecsub(&ts2, &ts);
1677 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1681 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1682 error = _mqueue_send(mq, msg, tvtohz(&tv));
1683 if (error != ETIMEDOUT)
1689 mqueue_freemsg(msg);
1694 * Common routine to send a message
1697 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1699 struct mqueue_msg *msg2;
1702 mtx_lock(&mq->mq_mutex);
1703 while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1705 mtx_unlock(&mq->mq_mutex);
1709 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1710 PCATCH, "mqsend", timo);
1712 if (error == EAGAIN)
1715 if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1716 mtx_unlock(&mq->mq_mutex);
1720 if (TAILQ_EMPTY(&mq->mq_msgq)) {
1721 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1723 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1724 TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1726 TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1727 if (msg2->msg_prio < msg->msg_prio)
1730 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1734 mq->mq_totalbytes += msg->msg_size;
1735 if (mq->mq_receivers)
1736 wakeup_one(&mq->mq_receivers);
1737 else if (mq->mq_notifier != NULL)
1738 mqueue_send_notification(mq);
1739 if (mq->mq_flags & MQ_RSEL) {
1740 mq->mq_flags &= ~MQ_RSEL;
1741 selwakeup(&mq->mq_rsel);
1743 KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1744 mtx_unlock(&mq->mq_mutex);
1749 * Send realtime a signal to process which registered itself
1750 * successfully by mq_notify.
1753 mqueue_send_notification(struct mqueue *mq)
1755 struct mqueue_notifier *nt;
1760 mtx_assert(&mq->mq_mutex, MA_OWNED);
1761 nt = mq->mq_notifier;
1762 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1764 error = sigev_findtd(p, &nt->nt_sigev, &td);
1766 mq->mq_notifier = NULL;
1769 if (!KSI_ONQ(&nt->nt_ksi)) {
1770 ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1771 tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1775 mq->mq_notifier = NULL;
1779 * Get a message. if waitok is false, thread will not be
1780 * blocked if there is no data in queue, otherwise, absolute
1781 * time will be checked.
1784 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1785 size_t msg_len, unsigned *msg_prio, int waitok,
1786 const struct timespec *abs_timeout)
1788 struct mqueue_msg *msg;
1789 struct timespec ts, ts2;
1793 if (msg_len < mq->mq_msgsize)
1796 /* O_NONBLOCK case */
1798 error = _mqueue_recv(mq, &msg, -1);
1804 /* we allow a null timeout (wait forever). */
1805 if (abs_timeout == NULL) {
1806 error = _mqueue_recv(mq, &msg, 0);
1812 /* try to get a message before checking time */
1813 error = _mqueue_recv(mq, &msg, -1);
1817 if (error != EAGAIN)
1820 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1828 timespecsub(&ts2, &ts);
1829 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1833 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1834 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1837 if (error != ETIMEDOUT)
1842 error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1844 curthread->td_retval[0] = msg->msg_size;
1845 curthread->td_retval[1] = 0;
1847 mqueue_freemsg(msg);
1852 * Common routine to receive a message
1855 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1859 mtx_lock(&mq->mq_mutex);
1860 while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1862 mtx_unlock(&mq->mq_mutex);
1866 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1867 PCATCH, "mqrecv", timo);
1869 if (error == EAGAIN)
1874 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1876 mq->mq_totalbytes -= (*msg)->msg_size;
1878 wakeup_one(&mq->mq_senders);
1879 if (mq->mq_flags & MQ_WSEL) {
1880 mq->mq_flags &= ~MQ_WSEL;
1881 selwakeup(&mq->mq_wsel);
1883 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1885 if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1886 !TAILQ_EMPTY(&mq->mq_msgq)) {
1887 mqueue_send_notification(mq);
1889 mtx_unlock(&mq->mq_mutex);
1893 static __inline struct mqueue_notifier *
1894 notifier_alloc(void)
1896 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1899 static __inline void
1900 notifier_free(struct mqueue_notifier *p)
1902 uma_zfree(mqnoti_zone, p);
1905 static struct mqueue_notifier *
1906 notifier_search(struct proc *p, int fd)
1908 struct mqueue_notifier *nt;
1910 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1911 if (nt->nt_ksi.ksi_mqd == fd)
1917 static __inline void
1918 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1920 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1923 static __inline void
1924 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1926 LIST_REMOVE(nt, nt_link);
1931 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1933 struct mqueue_notifier *nt;
1935 mtx_assert(&mq->mq_mutex, MA_OWNED);
1937 nt = notifier_search(p, fd);
1939 if (mq->mq_notifier == nt)
1940 mq->mq_notifier = NULL;
1941 sigqueue_take(&nt->nt_ksi);
1942 notifier_delete(p, nt);
1948 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1949 const struct mq_attr *attr)
1951 char path[MQFS_NAMELEN + 1];
1952 struct mqfs_node *pn;
1953 struct filedesc *fdp;
1956 int fd, error, len, cmode;
1958 fdp = td->td_proc->p_fd;
1959 cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1961 if ((flags & O_CREAT) != 0 && attr != NULL) {
1962 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1964 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1968 error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1973 * The first character of name must be a slash (/) character
1974 * and the remaining characters of name cannot include any slash
1978 if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
1981 error = falloc(td, &fp, &fd, O_CLOEXEC);
1985 sx_xlock(&mqfs_data.mi_lock);
1986 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1988 if (!(flags & O_CREAT)) {
1991 mq = mqueue_alloc(attr);
1995 pn = mqfs_create_file(mqfs_data.mi_root,
1996 path + 1, len - 1, td->td_ucred,
2009 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2012 accmode_t accmode = 0;
2018 error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2019 pn->mn_gid, accmode, td->td_ucred, NULL);
2024 sx_xunlock(&mqfs_data.mi_lock);
2025 fdclose(td, fp, fd);
2031 sx_xunlock(&mqfs_data.mi_lock);
2033 finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2036 td->td_retval[0] = fd;
2042 * Syscall to open a message queue.
2045 sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
2047 struct mq_attr attr;
2050 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2052 flags = FFLAGS(uap->flags);
2053 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2054 error = copyin(uap->attr, &attr, sizeof(attr));
2058 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2059 uap->attr != NULL ? &attr : NULL));
2063 * Syscall to unlink a message queue.
2066 sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2068 char path[MQFS_NAMELEN+1];
2069 struct mqfs_node *pn;
2072 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2077 if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2080 sx_xlock(&mqfs_data.mi_lock);
2081 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2083 error = do_unlink(pn, td->td_ucred);
2086 sx_xunlock(&mqfs_data.mi_lock);
2090 typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **);
2093 * Get message queue by giving file slot
2096 _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func,
2097 struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2099 struct mqfs_node *pn;
2102 error = func(td, fd, rightsp, fpp);
2105 if (&mqueueops != (*fpp)->f_ops) {
2109 pn = (*fpp)->f_data;
2118 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2119 struct mqueue **pmq)
2121 cap_rights_t rights;
2123 return _getmq(td, fd, cap_rights_init(&rights, CAP_EVENT), fget,
2128 getmq_read(struct thread *td, int fd, struct file **fpp,
2129 struct mqfs_node **ppn, struct mqueue **pmq)
2131 cap_rights_t rights;
2133 return _getmq(td, fd, cap_rights_init(&rights, CAP_READ), fget_read,
2138 getmq_write(struct thread *td, int fd, struct file **fpp,
2139 struct mqfs_node **ppn, struct mqueue **pmq)
2141 cap_rights_t rights;
2143 return _getmq(td, fd, cap_rights_init(&rights, CAP_WRITE), fget_write,
2148 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2149 struct mq_attr *oattr)
2156 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2158 error = getmq(td, mqd, &fp, NULL, &mq);
2161 oattr->mq_maxmsg = mq->mq_maxmsg;
2162 oattr->mq_msgsize = mq->mq_msgsize;
2163 oattr->mq_curmsgs = mq->mq_curmsgs;
2166 oflag = flag = fp->f_flag;
2167 flag &= ~O_NONBLOCK;
2168 flag |= (attr->mq_flags & O_NONBLOCK);
2169 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2172 oattr->mq_flags = (O_NONBLOCK & oflag);
2178 sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2180 struct mq_attr attr, oattr;
2183 if (uap->attr != NULL) {
2184 error = copyin(uap->attr, &attr, sizeof(attr));
2188 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2192 if (uap->oattr != NULL)
2193 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2198 sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2202 struct timespec *abs_timeout, ets;
2206 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2209 if (uap->abs_timeout != NULL) {
2210 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2216 waitok = !(fp->f_flag & O_NONBLOCK);
2217 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2218 uap->msg_prio, waitok, abs_timeout);
2224 sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2228 struct timespec *abs_timeout, ets;
2231 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2234 if (uap->abs_timeout != NULL) {
2235 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2241 waitok = !(fp->f_flag & O_NONBLOCK);
2242 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2243 uap->msg_prio, waitok, abs_timeout);
2249 kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
2252 cap_rights_t rights;
2254 struct filedesc *fdp;
2257 struct file *fp, *fp2;
2258 struct mqueue_notifier *nt, *newnt = NULL;
2261 if (sigev != NULL) {
2262 if (sigev->sigev_notify != SIGEV_SIGNAL &&
2263 sigev->sigev_notify != SIGEV_THREAD_ID &&
2264 sigev->sigev_notify != SIGEV_NONE)
2266 if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2267 sigev->sigev_notify == SIGEV_THREAD_ID) &&
2268 !_SIG_VALID(sigev->sigev_signo))
2272 fdp = td->td_proc->p_fd;
2273 error = getmq(td, mqd, &fp, NULL, &mq);
2277 FILEDESC_SLOCK(fdp);
2278 fp2 = fget_locked(fdp, mqd);
2280 FILEDESC_SUNLOCK(fdp);
2285 error = cap_check(cap_rights(fdp, mqd),
2286 cap_rights_init(&rights, CAP_EVENT));
2288 FILEDESC_SUNLOCK(fdp);
2293 FILEDESC_SUNLOCK(fdp);
2297 mtx_lock(&mq->mq_mutex);
2298 FILEDESC_SUNLOCK(fdp);
2299 if (sigev != NULL) {
2300 if (mq->mq_notifier != NULL) {
2304 nt = notifier_search(p, mqd);
2306 if (newnt == NULL) {
2308 mtx_unlock(&mq->mq_mutex);
2309 newnt = notifier_alloc();
2315 sigqueue_take(&nt->nt_ksi);
2316 if (newnt != NULL) {
2317 notifier_free(newnt);
2323 ksiginfo_init(&nt->nt_ksi);
2324 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2325 nt->nt_ksi.ksi_code = SI_MESGQ;
2327 nt->nt_ksi.ksi_mqd = mqd;
2328 notifier_insert(p, nt);
2330 nt->nt_sigev = *sigev;
2331 mq->mq_notifier = nt;
2334 * if there is no receivers and message queue
2335 * is not empty, we should send notification
2336 * as soon as possible.
2338 if (mq->mq_receivers == 0 &&
2339 !TAILQ_EMPTY(&mq->mq_msgq))
2340 mqueue_send_notification(mq);
2343 notifier_remove(p, mq, mqd);
2345 mtx_unlock(&mq->mq_mutex);
2350 notifier_free(newnt);
2355 sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2357 struct sigevent ev, *evp;
2360 if (uap->sigev == NULL) {
2363 error = copyin(uap->sigev, &ev, sizeof(ev));
2368 return (kern_kmq_notify(td, uap->mqd, evp));
2372 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2374 struct filedesc *fdp;
2377 fdp = td->td_proc->p_fd;
2378 FILEDESC_LOCK_ASSERT(fdp);
2380 if (fp->f_ops == &mqueueops) {
2382 mtx_lock(&mq->mq_mutex);
2383 notifier_remove(td->td_proc, mq, fd);
2385 /* have to wakeup thread in same process */
2386 if (mq->mq_flags & MQ_RSEL) {
2387 mq->mq_flags &= ~MQ_RSEL;
2388 selwakeup(&mq->mq_rsel);
2390 if (mq->mq_flags & MQ_WSEL) {
2391 mq->mq_flags &= ~MQ_WSEL;
2392 selwakeup(&mq->mq_wsel);
2394 mtx_unlock(&mq->mq_mutex);
2399 mq_proc_exit(void *arg __unused, struct proc *p)
2401 struct filedesc *fdp;
2407 FILEDESC_SLOCK(fdp);
2408 for (i = 0; i < fdp->fd_nfiles; ++i) {
2409 fp = fget_locked(fdp, i);
2410 if (fp != NULL && fp->f_ops == &mqueueops) {
2412 mtx_lock(&mq->mq_mutex);
2413 notifier_remove(p, FPTOMQ(fp), i);
2414 mtx_unlock(&mq->mq_mutex);
2417 FILEDESC_SUNLOCK(fdp);
2418 KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2422 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2425 struct mqueue *mq = FPTOMQ(fp);
2428 mtx_lock(&mq->mq_mutex);
2429 if (events & (POLLIN | POLLRDNORM)) {
2430 if (mq->mq_curmsgs) {
2431 revents |= events & (POLLIN | POLLRDNORM);
2433 mq->mq_flags |= MQ_RSEL;
2434 selrecord(td, &mq->mq_rsel);
2437 if (events & POLLOUT) {
2438 if (mq->mq_curmsgs < mq->mq_maxmsg)
2441 mq->mq_flags |= MQ_WSEL;
2442 selrecord(td, &mq->mq_wsel);
2445 mtx_unlock(&mq->mq_mutex);
2450 mqf_close(struct file *fp, struct thread *td)
2452 struct mqfs_node *pn;
2454 fp->f_ops = &badfileops;
2457 sx_xlock(&mqfs_data.mi_lock);
2459 sx_xunlock(&mqfs_data.mi_lock);
2464 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2467 struct mqfs_node *pn = fp->f_data;
2469 bzero(st, sizeof *st);
2470 sx_xlock(&mqfs_data.mi_lock);
2471 st->st_atim = pn->mn_atime;
2472 st->st_mtim = pn->mn_mtime;
2473 st->st_ctim = pn->mn_ctime;
2474 st->st_birthtim = pn->mn_birth;
2475 st->st_uid = pn->mn_uid;
2476 st->st_gid = pn->mn_gid;
2477 st->st_mode = S_IFIFO | pn->mn_mode;
2478 sx_xunlock(&mqfs_data.mi_lock);
2483 mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
2486 struct mqfs_node *pn;
2491 sx_xlock(&mqfs_data.mi_lock);
2492 error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2496 pn->mn_mode = mode & ACCESSPERMS;
2498 sx_xunlock(&mqfs_data.mi_lock);
2503 mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2506 struct mqfs_node *pn;
2511 sx_xlock(&mqfs_data.mi_lock);
2512 if (uid == (uid_t)-1)
2514 if (gid == (gid_t)-1)
2516 if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2517 (gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
2518 (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
2523 sx_xunlock(&mqfs_data.mi_lock);
2528 mqf_kqfilter(struct file *fp, struct knote *kn)
2530 struct mqueue *mq = FPTOMQ(fp);
2533 if (kn->kn_filter == EVFILT_READ) {
2534 kn->kn_fop = &mq_rfiltops;
2535 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2536 } else if (kn->kn_filter == EVFILT_WRITE) {
2537 kn->kn_fop = &mq_wfiltops;
2538 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2545 filt_mqdetach(struct knote *kn)
2547 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2549 if (kn->kn_filter == EVFILT_READ)
2550 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2551 else if (kn->kn_filter == EVFILT_WRITE)
2552 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2554 panic("filt_mqdetach");
2558 filt_mqread(struct knote *kn, long hint)
2560 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2562 mtx_assert(&mq->mq_mutex, MA_OWNED);
2563 return (mq->mq_curmsgs != 0);
2567 filt_mqwrite(struct knote *kn, long hint)
2569 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2571 mtx_assert(&mq->mq_mutex, MA_OWNED);
2572 return (mq->mq_curmsgs < mq->mq_maxmsg);
2576 mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2579 kif->kf_type = KF_TYPE_MQUEUE;
2583 static struct fileops mqueueops = {
2584 .fo_read = invfo_rdwr,
2585 .fo_write = invfo_rdwr,
2586 .fo_truncate = invfo_truncate,
2587 .fo_ioctl = invfo_ioctl,
2588 .fo_poll = mqf_poll,
2589 .fo_kqfilter = mqf_kqfilter,
2590 .fo_stat = mqf_stat,
2591 .fo_close = mqf_close,
2592 .fo_chmod = mqf_chmod,
2593 .fo_chown = mqf_chown,
2594 .fo_sendfile = invfo_sendfile,
2595 .fo_fill_kinfo = mqf_fill_kinfo,
2598 static struct vop_vector mqfs_vnodeops = {
2599 .vop_default = &default_vnodeops,
2600 .vop_access = mqfs_access,
2601 .vop_cachedlookup = mqfs_lookup,
2602 .vop_lookup = vfs_cache_lookup,
2603 .vop_reclaim = mqfs_reclaim,
2604 .vop_create = mqfs_create,
2605 .vop_remove = mqfs_remove,
2606 .vop_inactive = mqfs_inactive,
2607 .vop_open = mqfs_open,
2608 .vop_close = mqfs_close,
2609 .vop_getattr = mqfs_getattr,
2610 .vop_setattr = mqfs_setattr,
2611 .vop_read = mqfs_read,
2612 .vop_write = VOP_EOPNOTSUPP,
2613 .vop_readdir = mqfs_readdir,
2614 .vop_mkdir = VOP_EOPNOTSUPP,
2615 .vop_rmdir = VOP_EOPNOTSUPP
2618 static struct vfsops mqfs_vfsops = {
2619 .vfs_init = mqfs_init,
2620 .vfs_uninit = mqfs_uninit,
2621 .vfs_mount = mqfs_mount,
2622 .vfs_unmount = mqfs_unmount,
2623 .vfs_root = mqfs_root,
2624 .vfs_statfs = mqfs_statfs,
2627 static struct vfsconf mqueuefs_vfsconf = {
2628 .vfc_version = VFS_VERSION,
2629 .vfc_name = "mqueuefs",
2630 .vfc_vfsops = &mqfs_vfsops,
2632 .vfc_flags = VFCF_SYNTHETIC
2635 static struct syscall_helper_data mq_syscalls[] = {
2636 SYSCALL_INIT_HELPER(kmq_open),
2637 SYSCALL_INIT_HELPER(kmq_setattr),
2638 SYSCALL_INIT_HELPER(kmq_timedsend),
2639 SYSCALL_INIT_HELPER(kmq_timedreceive),
2640 SYSCALL_INIT_HELPER(kmq_notify),
2641 SYSCALL_INIT_HELPER(kmq_unlink),
2645 #ifdef COMPAT_FREEBSD32
2646 #include <compat/freebsd32/freebsd32.h>
2647 #include <compat/freebsd32/freebsd32_proto.h>
2648 #include <compat/freebsd32/freebsd32_signal.h>
2649 #include <compat/freebsd32/freebsd32_syscall.h>
2650 #include <compat/freebsd32/freebsd32_util.h>
2653 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2656 to->mq_flags = from->mq_flags;
2657 to->mq_maxmsg = from->mq_maxmsg;
2658 to->mq_msgsize = from->mq_msgsize;
2659 to->mq_curmsgs = from->mq_curmsgs;
2663 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2666 to->mq_flags = from->mq_flags;
2667 to->mq_maxmsg = from->mq_maxmsg;
2668 to->mq_msgsize = from->mq_msgsize;
2669 to->mq_curmsgs = from->mq_curmsgs;
2673 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2675 struct mq_attr attr;
2676 struct mq_attr32 attr32;
2679 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2681 flags = FFLAGS(uap->flags);
2682 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2683 error = copyin(uap->attr, &attr32, sizeof(attr32));
2686 mq_attr_from32(&attr32, &attr);
2688 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2689 uap->attr != NULL ? &attr : NULL));
2693 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2695 struct mq_attr attr, oattr;
2696 struct mq_attr32 attr32, oattr32;
2699 if (uap->attr != NULL) {
2700 error = copyin(uap->attr, &attr32, sizeof(attr32));
2703 mq_attr_from32(&attr32, &attr);
2705 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2709 if (uap->oattr != NULL) {
2710 mq_attr_to32(&oattr, &oattr32);
2711 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2717 freebsd32_kmq_timedsend(struct thread *td,
2718 struct freebsd32_kmq_timedsend_args *uap)
2722 struct timespec32 ets32;
2723 struct timespec *abs_timeout, ets;
2727 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2730 if (uap->abs_timeout != NULL) {
2731 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2734 CP(ets32, ets, tv_sec);
2735 CP(ets32, ets, tv_nsec);
2739 waitok = !(fp->f_flag & O_NONBLOCK);
2740 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2741 uap->msg_prio, waitok, abs_timeout);
2747 freebsd32_kmq_timedreceive(struct thread *td,
2748 struct freebsd32_kmq_timedreceive_args *uap)
2752 struct timespec32 ets32;
2753 struct timespec *abs_timeout, ets;
2756 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2759 if (uap->abs_timeout != NULL) {
2760 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2763 CP(ets32, ets, tv_sec);
2764 CP(ets32, ets, tv_nsec);
2768 waitok = !(fp->f_flag & O_NONBLOCK);
2769 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2770 uap->msg_prio, waitok, abs_timeout);
2776 freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap)
2778 struct sigevent ev, *evp;
2779 struct sigevent32 ev32;
2782 if (uap->sigev == NULL) {
2785 error = copyin(uap->sigev, &ev32, sizeof(ev32));
2788 error = convert_sigevent32(&ev32, &ev);
2793 return (kern_kmq_notify(td, uap->mqd, evp));
2796 static struct syscall_helper_data mq32_syscalls[] = {
2797 SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2798 SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2799 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2800 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
2801 SYSCALL32_INIT_HELPER(freebsd32_kmq_notify),
2802 SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2812 error = syscall_helper_register(mq_syscalls, SY_THR_STATIC_KLD);
2815 #ifdef COMPAT_FREEBSD32
2816 error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD);
2827 #ifdef COMPAT_FREEBSD32
2828 syscall32_helper_unregister(mq32_syscalls);
2830 syscall_helper_unregister(mq_syscalls);
2835 mq_modload(struct module *module, int cmd, void *arg)
2839 error = vfs_modevent(module, cmd, arg);
2858 static moduledata_t mqueuefs_mod = {
2863 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2864 MODULE_VERSION(mqueuefs, 1);