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>
84 #include <sys/vnode.h>
85 #include <machine/atomic.h>
87 FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
90 * Limits and constants
92 #define MQFS_NAMELEN NAME_MAX
93 #define MQFS_DELEN (8 + MQFS_NAMELEN)
109 * mqfs_info: describes a mqfs instance
113 struct mqfs_node *mi_root;
114 struct unrhdr *mi_unrhdr;
118 LIST_ENTRY(mqfs_vdata) mv_link;
119 struct mqfs_node *mv_node;
120 struct vnode *mv_vnode;
125 * mqfs_node: describes a node (file or directory) within a mqfs
128 char mn_name[MQFS_NAMELEN+1];
129 struct mqfs_info *mn_info;
130 struct mqfs_node *mn_parent;
131 LIST_HEAD(,mqfs_node) mn_children;
132 LIST_ENTRY(mqfs_node) mn_sibling;
133 LIST_HEAD(,mqfs_vdata) mn_vnodes;
139 struct timespec mn_birth;
140 struct timespec mn_ctime;
141 struct timespec mn_atime;
142 struct timespec mn_mtime;
148 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
149 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
150 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
151 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
152 (fp)->f_data)->mn_data))
154 TAILQ_HEAD(msgq, mqueue_msg);
158 struct mqueue_notifier {
159 LIST_ENTRY(mqueue_notifier) nt_link;
160 struct sigevent nt_sigev;
162 struct proc *nt_proc;
175 struct selinfo mq_rsel;
176 struct selinfo mq_wsel;
177 struct mqueue_notifier *mq_notifier;
184 TAILQ_ENTRY(mqueue_msg) msg_link;
185 unsigned int msg_prio;
186 unsigned int msg_size;
187 /* following real data... */
190 static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
191 "POSIX real time message queue");
193 static int default_maxmsg = 10;
194 static int default_msgsize = 1024;
196 static int maxmsg = 100;
197 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
198 &maxmsg, 0, "Default maximum messages in queue");
199 static int maxmsgsize = 16384;
200 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
201 &maxmsgsize, 0, "Default maximum message size");
202 static int maxmq = 100;
203 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
204 &maxmq, 0, "maximum message queues");
205 static int curmq = 0;
206 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
207 &curmq, 0, "current message queue number");
208 static int unloadable = 0;
209 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
211 static eventhandler_tag exit_tag;
213 /* Only one instance per-system */
214 static struct mqfs_info mqfs_data;
215 static uma_zone_t mqnode_zone;
216 static uma_zone_t mqueue_zone;
217 static uma_zone_t mvdata_zone;
218 static uma_zone_t mqnoti_zone;
219 static struct vop_vector mqfs_vnodeops;
220 static struct fileops mqueueops;
223 * Directory structure construction and manipulation
226 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
227 const char *name, int namelen, struct ucred *cred, int mode);
228 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
229 const char *name, int namelen, struct ucred *cred, int mode);
232 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
233 const char *name, int namelen, struct ucred *cred, int mode);
234 static int mqfs_destroy(struct mqfs_node *mn);
235 static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
236 static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
237 static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
240 * Message queue construction and maniplation
242 static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
243 static void mqueue_free(struct mqueue *mq);
244 static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
245 size_t msg_len, unsigned msg_prio, int waitok,
246 const struct timespec *abs_timeout);
247 static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
248 size_t msg_len, unsigned *msg_prio, int waitok,
249 const struct timespec *abs_timeout);
250 static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
252 static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
254 static void mqueue_send_notification(struct mqueue *mq);
255 static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
256 static void mq_proc_exit(void *arg, struct proc *p);
261 static void filt_mqdetach(struct knote *kn);
262 static int filt_mqread(struct knote *kn, long hint);
263 static int filt_mqwrite(struct knote *kn, long hint);
265 struct filterops mq_rfiltops = {
267 .f_detach = filt_mqdetach,
268 .f_event = filt_mqread,
270 struct filterops mq_wfiltops = {
272 .f_detach = filt_mqdetach,
273 .f_event = filt_mqwrite,
277 * Initialize fileno bitmap
280 mqfs_fileno_init(struct mqfs_info *mi)
284 up = new_unrhdr(1, INT_MAX, NULL);
289 * Tear down fileno bitmap
292 mqfs_fileno_uninit(struct mqfs_info *mi)
297 mi->mi_unrhdr = NULL;
302 * Allocate a file number
305 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
307 /* make sure our parent has a file number */
308 if (mn->mn_parent && !mn->mn_parent->mn_fileno)
309 mqfs_fileno_alloc(mi, mn->mn_parent);
311 switch (mn->mn_type) {
315 case mqfstype_symlink:
316 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
319 KASSERT(mn->mn_parent != NULL,
320 ("mqfstype_this node has no parent"));
321 mn->mn_fileno = mn->mn_parent->mn_fileno;
323 case mqfstype_parent:
324 KASSERT(mn->mn_parent != NULL,
325 ("mqfstype_parent node has no parent"));
326 if (mn->mn_parent == mi->mi_root) {
327 mn->mn_fileno = mn->mn_parent->mn_fileno;
330 KASSERT(mn->mn_parent->mn_parent != NULL,
331 ("mqfstype_parent node has no grandparent"));
332 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
336 ("mqfs_fileno_alloc() called for unknown type node: %d",
343 * Release a file number
346 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
348 switch (mn->mn_type) {
352 case mqfstype_symlink:
353 free_unr(mi->mi_unrhdr, mn->mn_fileno);
356 case mqfstype_parent:
357 /* ignore these, as they don't "own" their file number */
361 ("mqfs_fileno_free() called for unknown type node: %d",
367 static __inline struct mqfs_node *
370 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
374 mqnode_free(struct mqfs_node *node)
376 uma_zfree(mqnode_zone, node);
380 mqnode_addref(struct mqfs_node *node)
382 atomic_fetchadd_int(&node->mn_refcount, 1);
386 mqnode_release(struct mqfs_node *node)
388 struct mqfs_info *mqfs;
391 mqfs = node->mn_info;
392 old = atomic_fetchadd_int(&node->mn_refcount, -1);
393 if (node->mn_type == mqfstype_dir ||
394 node->mn_type == mqfstype_root)
395 exp = 3; /* include . and .. */
399 int locked = sx_xlocked(&mqfs->mi_lock);
401 sx_xlock(&mqfs->mi_lock);
404 sx_xunlock(&mqfs->mi_lock);
409 * Add a node to a directory
412 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
414 KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
415 KASSERT(parent->mn_info != NULL,
416 ("%s(): parent has no mn_info", __func__));
417 KASSERT(parent->mn_type == mqfstype_dir ||
418 parent->mn_type == mqfstype_root,
419 ("%s(): parent is not a directory", __func__));
421 node->mn_info = parent->mn_info;
422 node->mn_parent = parent;
423 LIST_INIT(&node->mn_children);
424 LIST_INIT(&node->mn_vnodes);
425 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
426 mqnode_addref(parent);
430 static struct mqfs_node *
431 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
434 struct mqfs_node *node;
436 node = mqnode_alloc();
437 strncpy(node->mn_name, name, namelen);
438 node->mn_type = nodetype;
439 node->mn_refcount = 1;
440 vfs_timestamp(&node->mn_birth);
441 node->mn_ctime = node->mn_atime = node->mn_mtime
443 node->mn_uid = cred->cr_uid;
444 node->mn_gid = cred->cr_gid;
445 node->mn_mode = mode;
452 static struct mqfs_node *
453 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
454 struct ucred *cred, int mode)
456 struct mqfs_node *node;
458 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
459 if (mqfs_add_node(parent, node) != 0) {
467 * Add . and .. to a directory
470 mqfs_fixup_dir(struct mqfs_node *parent)
472 struct mqfs_node *dir;
474 dir = mqnode_alloc();
475 dir->mn_name[0] = '.';
476 dir->mn_type = mqfstype_this;
477 dir->mn_refcount = 1;
478 if (mqfs_add_node(parent, dir) != 0) {
483 dir = mqnode_alloc();
484 dir->mn_name[0] = dir->mn_name[1] = '.';
485 dir->mn_type = mqfstype_parent;
486 dir->mn_refcount = 1;
488 if (mqfs_add_node(parent, dir) != 0) {
501 static struct mqfs_node *
502 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
503 struct ucred *cred, int mode)
505 struct mqfs_node *node;
507 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
508 if (mqfs_add_node(parent, node) != 0) {
513 if (mqfs_fixup_dir(node) != 0) {
523 static struct mqfs_node *
524 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
525 struct ucred *cred, int mode)
527 struct mqfs_node *node;
529 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
530 if (mqfs_add_node(parent, node) != 0) {
540 * Destroy a node or a tree of nodes
543 mqfs_destroy(struct mqfs_node *node)
545 struct mqfs_node *parent;
547 KASSERT(node != NULL,
548 ("%s(): node is NULL", __func__));
549 KASSERT(node->mn_info != NULL,
550 ("%s(): node has no mn_info", __func__));
552 /* destroy children */
553 if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
554 while (! LIST_EMPTY(&node->mn_children))
555 mqfs_destroy(LIST_FIRST(&node->mn_children));
557 /* unlink from parent */
558 if ((parent = node->mn_parent) != NULL) {
559 KASSERT(parent->mn_info == node->mn_info,
560 ("%s(): parent has different mn_info", __func__));
561 LIST_REMOVE(node, mn_sibling);
564 if (node->mn_fileno != 0)
565 mqfs_fileno_free(node->mn_info, node);
566 if (node->mn_data != NULL)
567 mqueue_free(node->mn_data);
573 * Mount a mqfs instance
576 mqfs_mount(struct mount *mp)
580 if (mp->mnt_flag & MNT_UPDATE)
583 mp->mnt_data = &mqfs_data;
585 mp->mnt_flag |= MNT_LOCAL;
590 vfs_mountedfrom(mp, "mqueue");
591 sbp->f_bsize = PAGE_SIZE;
592 sbp->f_iosize = PAGE_SIZE;
602 * Unmount a mqfs instance
605 mqfs_unmount(struct mount *mp, int mntflags)
609 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
615 * Return a root vnode
618 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
620 struct mqfs_info *mqfs;
623 mqfs = VFSTOMQFS(mp);
624 ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
629 * Return filesystem stats
632 mqfs_statfs(struct mount *mp, struct statfs *sbp)
634 /* XXX update statistics */
639 * Initialize a mqfs instance
642 mqfs_init(struct vfsconf *vfc)
644 struct mqfs_node *root;
645 struct mqfs_info *mi;
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",
652 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
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");
658 /* set up the root diretory */
659 root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
662 LIST_INIT(&root->mn_children);
663 LIST_INIT(&root->mn_vnodes);
665 mqfs_fileno_init(mi);
666 mqfs_fileno_alloc(mi, root);
667 mqfs_fixup_dir(root);
668 exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
669 EVENTHANDLER_PRI_ANY);
670 mq_fdclose = mqueue_fdclose;
671 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
676 * Destroy a mqfs instance
679 mqfs_uninit(struct vfsconf *vfc)
681 struct mqfs_info *mi;
685 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
687 mqfs_destroy(mi->mi_root);
689 mqfs_fileno_uninit(mi);
690 sx_destroy(&mi->mi_lock);
691 uma_zdestroy(mqnode_zone);
692 uma_zdestroy(mqueue_zone);
693 uma_zdestroy(mvdata_zone);
694 uma_zdestroy(mqnoti_zone);
702 do_recycle(void *context, int pending __unused)
704 struct vnode *vp = (struct vnode *)context;
706 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
716 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
718 struct mqfs_vdata *vd;
719 struct mqfs_info *mqfs;
720 struct vnode *newvpp;
725 sx_xlock(&mqfs->mi_lock);
726 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
727 if (vd->mv_vnode->v_mount == mp) {
736 sx_xunlock(&mqfs->mi_lock);
737 error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
741 sx_xunlock(&mqfs->mi_lock);
743 error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
746 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
747 error = insmntque(newvpp, mp);
751 sx_xlock(&mqfs->mi_lock);
753 * Check if it has already been allocated
754 * while we were blocked.
756 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
757 if (vd->mv_vnode->v_mount == mp) {
759 sx_xunlock(&mqfs->mi_lock);
769 vd = uma_zalloc(mvdata_zone, M_WAITOK);
773 TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
774 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
776 switch (pn->mn_type) {
778 (*vpp)->v_vflag = VV_ROOT;
782 case mqfstype_parent:
783 (*vpp)->v_type = VDIR;
786 (*vpp)->v_type = VREG;
788 case mqfstype_symlink:
789 (*vpp)->v_type = VLNK;
792 KASSERT(0, ("mqfs_allocf called for null node\n"));
794 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
796 sx_xunlock(&mqfs->mi_lock);
801 * Search a directory entry
803 static struct mqfs_node *
804 mqfs_search(struct mqfs_node *pd, const char *name, int len)
806 struct mqfs_node *pn;
808 sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
809 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
810 if (strncmp(pn->mn_name, name, len) == 0 &&
811 pn->mn_name[len] == '\0')
818 * Look up a file or directory.
821 mqfs_lookupx(struct vop_cachedlookup_args *ap)
823 struct componentname *cnp;
824 struct vnode *dvp, **vpp;
825 struct mqfs_node *pd;
826 struct mqfs_node *pn;
827 struct mqfs_info *mqfs;
828 int nameiop, flags, error, namelen;
835 pname = cnp->cn_nameptr;
836 namelen = cnp->cn_namelen;
838 flags = cnp->cn_flags;
839 nameiop = cnp->cn_nameiop;
845 if (dvp->v_type != VDIR)
848 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
852 /* shortcut: check if the name is too long */
853 if (cnp->cn_namelen >= MQFS_NAMELEN)
857 if (namelen == 1 && pname[0] == '.') {
858 if ((flags & ISLASTCN) && nameiop != LOOKUP)
867 if (cnp->cn_flags & ISDOTDOT) {
868 if (dvp->v_vflag & VV_ROOT)
870 if ((flags & ISLASTCN) && nameiop != LOOKUP)
873 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
875 error = mqfs_allocv(dvp->v_mount, vpp, pn);
876 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
881 sx_xlock(&mqfs->mi_lock);
882 pn = mqfs_search(pd, pname, namelen);
885 sx_xunlock(&mqfs->mi_lock);
890 if (nameiop == DELETE && (flags & ISLASTCN)) {
891 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
905 error = mqfs_allocv(dvp->v_mount, vpp, pn);
907 if (error == 0 && cnp->cn_flags & MAKEENTRY)
908 cache_enter(dvp, *vpp, cnp);
914 /* will create a new entry in the directory ? */
915 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
916 && (flags & ISLASTCN)) {
917 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
920 cnp->cn_flags |= SAVENAME;
921 return (EJUSTRETURN);
927 struct vop_lookup_args {
928 struct vop_generic_args a_gen;
930 struct vnode **a_vpp;
931 struct componentname *a_cnp;
936 * vnode lookup operation
939 mqfs_lookup(struct vop_cachedlookup_args *ap)
943 rc = mqfs_lookupx(ap);
948 struct vop_create_args {
950 struct vnode **a_vpp;
951 struct componentname *a_cnp;
957 * vnode creation operation
960 mqfs_create(struct vop_create_args *ap)
962 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
963 struct componentname *cnp = ap->a_cnp;
964 struct mqfs_node *pd;
965 struct mqfs_node *pn;
969 pd = VTON(ap->a_dvp);
970 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
972 mq = mqueue_alloc(NULL);
975 sx_xlock(&mqfs->mi_lock);
976 if ((cnp->cn_flags & HASBUF) == 0)
977 panic("%s: no name", __func__);
978 pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
979 cnp->cn_cred, ap->a_vap->va_mode);
981 sx_xunlock(&mqfs->mi_lock);
985 sx_xunlock(&mqfs->mi_lock);
986 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1002 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1004 struct mqfs_node *parent;
1005 struct mqfs_vdata *vd;
1008 sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1010 if (ucred->cr_uid != pn->mn_uid &&
1011 (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1013 else if (!pn->mn_deleted) {
1014 parent = pn->mn_parent;
1015 pn->mn_parent = NULL;
1017 LIST_REMOVE(pn, mn_sibling);
1018 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1019 cache_purge(vd->mv_vnode);
1020 vhold(vd->mv_vnode);
1021 taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1024 mqnode_release(parent);
1031 struct vop_remove_args {
1032 struct vnode *a_dvp;
1034 struct componentname *a_cnp;
1039 * vnode removal operation
1042 mqfs_remove(struct vop_remove_args *ap)
1044 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1045 struct mqfs_node *pn;
1048 if (ap->a_vp->v_type == VDIR)
1050 pn = VTON(ap->a_vp);
1051 sx_xlock(&mqfs->mi_lock);
1052 error = do_unlink(pn, ap->a_cnp->cn_cred);
1053 sx_xunlock(&mqfs->mi_lock);
1058 struct vop_inactive_args {
1060 struct thread *a_td;
1065 mqfs_inactive(struct vop_inactive_args *ap)
1067 struct mqfs_node *pn = VTON(ap->a_vp);
1075 struct vop_reclaim_args {
1076 struct vop_generic_args a_gen;
1078 struct thread *a_td;
1083 mqfs_reclaim(struct vop_reclaim_args *ap)
1085 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1086 struct vnode *vp = ap->a_vp;
1087 struct mqfs_node *pn;
1088 struct mqfs_vdata *vd;
1092 sx_xlock(&mqfs->mi_lock);
1094 LIST_REMOVE(vd, mv_link);
1095 uma_zfree(mvdata_zone, vd);
1097 sx_xunlock(&mqfs->mi_lock);
1102 struct vop_open_args {
1103 struct vop_generic_args a_gen;
1106 struct ucred *a_cred;
1107 struct thread *a_td;
1113 mqfs_open(struct vop_open_args *ap)
1119 struct vop_close_args {
1120 struct vop_generic_args a_gen;
1123 struct ucred *a_cred;
1124 struct thread *a_td;
1129 mqfs_close(struct vop_close_args *ap)
1135 struct vop_access_args {
1136 struct vop_generic_args a_gen;
1138 accmode_t a_accmode;
1139 struct ucred *a_cred;
1140 struct thread *a_td;
1145 * Verify permissions
1148 mqfs_access(struct vop_access_args *ap)
1150 struct vnode *vp = ap->a_vp;
1154 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1157 error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1158 vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1163 struct vop_getattr_args {
1164 struct vop_generic_args a_gen;
1166 struct vattr *a_vap;
1167 struct ucred *a_cred;
1172 * Get file attributes
1175 mqfs_getattr(struct vop_getattr_args *ap)
1177 struct vnode *vp = ap->a_vp;
1178 struct mqfs_node *pn = VTON(vp);
1179 struct vattr *vap = ap->a_vap;
1182 vap->va_type = vp->v_type;
1183 vap->va_mode = pn->mn_mode;
1185 vap->va_uid = pn->mn_uid;
1186 vap->va_gid = pn->mn_gid;
1187 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1188 vap->va_fileid = pn->mn_fileno;
1190 vap->va_blocksize = PAGE_SIZE;
1191 vap->va_bytes = vap->va_size = 0;
1192 vap->va_atime = pn->mn_atime;
1193 vap->va_mtime = pn->mn_mtime;
1194 vap->va_ctime = pn->mn_ctime;
1195 vap->va_birthtime = pn->mn_birth;
1198 vap->va_rdev = NODEV;
1200 vap->va_filerev = 0;
1205 struct vop_setattr_args {
1206 struct vop_generic_args a_gen;
1208 struct vattr *a_vap;
1209 struct ucred *a_cred;
1216 mqfs_setattr(struct vop_setattr_args *ap)
1218 struct mqfs_node *pn;
1229 if ((vap->va_type != VNON) ||
1230 (vap->va_nlink != VNOVAL) ||
1231 (vap->va_fsid != VNOVAL) ||
1232 (vap->va_fileid != VNOVAL) ||
1233 (vap->va_blocksize != VNOVAL) ||
1234 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1235 (vap->va_rdev != VNOVAL) ||
1236 ((int)vap->va_bytes != VNOVAL) ||
1237 (vap->va_gen != VNOVAL)) {
1244 if (vap->va_uid == (uid_t)VNOVAL)
1248 if (vap->va_gid == (gid_t)VNOVAL)
1253 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1255 * To modify the ownership of a file, must possess VADMIN
1258 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1262 * XXXRW: Why is there a privilege check here: shouldn't the
1263 * check in VOP_ACCESS() be enough? Also, are the group bits
1264 * below definitely right?
1266 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1267 (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1268 (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1275 if (vap->va_mode != (mode_t)VNOVAL) {
1276 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1277 (error = priv_check(td, PRIV_MQ_ADMIN)))
1279 pn->mn_mode = vap->va_mode;
1283 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1284 /* See the comment in ufs_vnops::ufs_setattr(). */
1285 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1286 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1287 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1289 if (vap->va_atime.tv_sec != VNOVAL) {
1290 pn->mn_atime = vap->va_atime;
1292 if (vap->va_mtime.tv_sec != VNOVAL) {
1293 pn->mn_mtime = vap->va_mtime;
1298 vfs_timestamp(&pn->mn_ctime);
1304 struct vop_read_args {
1305 struct vop_generic_args a_gen;
1309 struct ucred *a_cred;
1317 mqfs_read(struct vop_read_args *ap)
1320 struct vnode *vp = ap->a_vp;
1321 struct uio *uio = ap->a_uio;
1322 struct mqfs_node *pn;
1326 if (vp->v_type != VREG)
1331 snprintf(buf, sizeof(buf),
1332 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1337 buf[sizeof(buf)-1] = '\0';
1339 error = uiomove_frombuf(buf, len, uio);
1344 struct vop_readdir_args {
1345 struct vop_generic_args a_gen;
1348 struct ucred *a_cred;
1356 * Return directory entries.
1359 mqfs_readdir(struct vop_readdir_args *ap)
1362 struct mqfs_info *mi;
1363 struct mqfs_node *pd;
1364 struct mqfs_node *pn;
1365 struct dirent entry;
1367 int *tmp_ncookies = NULL;
1372 mi = VFSTOMQFS(vp->v_mount);
1376 if (vp->v_type != VDIR)
1379 if (uio->uio_offset < 0)
1382 if (ap->a_ncookies != NULL) {
1383 tmp_ncookies = ap->a_ncookies;
1384 *ap->a_ncookies = 0;
1385 ap->a_ncookies = NULL;
1391 sx_xlock(&mi->mi_lock);
1393 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1394 entry.d_reclen = sizeof(entry);
1396 mqfs_fileno_alloc(mi, pn);
1397 entry.d_fileno = pn->mn_fileno;
1398 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1399 entry.d_name[i] = pn->mn_name[i];
1400 entry.d_name[i] = 0;
1402 switch (pn->mn_type) {
1406 case mqfstype_parent:
1407 entry.d_type = DT_DIR;
1410 entry.d_type = DT_REG;
1412 case mqfstype_symlink:
1413 entry.d_type = DT_LNK;
1416 panic("%s has unexpected node type: %d", pn->mn_name,
1419 if (entry.d_reclen > uio->uio_resid)
1421 if (offset >= uio->uio_offset) {
1422 error = vfs_read_dirent(ap, &entry, offset);
1426 offset += entry.d_reclen;
1428 sx_xunlock(&mi->mi_lock);
1430 uio->uio_offset = offset;
1432 if (tmp_ncookies != NULL)
1433 ap->a_ncookies = tmp_ncookies;
1441 struct vop_mkdir_args {
1442 struct vnode *a_dvp;
1443 struvt vnode **a_vpp;
1444 struvt componentname *a_cnp;
1445 struct vattr *a_vap;
1450 * Create a directory.
1453 mqfs_mkdir(struct vop_mkdir_args *ap)
1455 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1456 struct componentname *cnp = ap->a_cnp;
1457 struct mqfs_node *pd = VTON(ap->a_dvp);
1458 struct mqfs_node *pn;
1461 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1463 sx_xlock(&mqfs->mi_lock);
1464 if ((cnp->cn_flags & HASBUF) == 0)
1465 panic("%s: no name", __func__);
1466 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1467 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1470 sx_xunlock(&mqfs->mi_lock);
1474 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1481 struct vop_rmdir_args {
1482 struct vnode *a_dvp;
1484 struct componentname *a_cnp;
1489 * Remove a directory.
1492 mqfs_rmdir(struct vop_rmdir_args *ap)
1494 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1495 struct mqfs_node *pn = VTON(ap->a_vp);
1496 struct mqfs_node *pt;
1498 if (pn->mn_type != mqfstype_dir)
1501 sx_xlock(&mqfs->mi_lock);
1502 if (pn->mn_deleted) {
1503 sx_xunlock(&mqfs->mi_lock);
1507 pt = LIST_FIRST(&pn->mn_children);
1508 pt = LIST_NEXT(pt, mn_sibling);
1509 pt = LIST_NEXT(pt, mn_sibling);
1511 sx_xunlock(&mqfs->mi_lock);
1515 pn->mn_parent = NULL;
1517 LIST_REMOVE(pn, mn_sibling);
1520 sx_xunlock(&mqfs->mi_lock);
1521 cache_purge(ap->a_vp);
1528 * Allocate a message queue
1530 static struct mqueue *
1531 mqueue_alloc(const struct mq_attr *attr)
1537 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1538 TAILQ_INIT(&mq->mq_msgq);
1540 mq->mq_maxmsg = attr->mq_maxmsg;
1541 mq->mq_msgsize = attr->mq_msgsize;
1543 mq->mq_maxmsg = default_maxmsg;
1544 mq->mq_msgsize = default_msgsize;
1546 mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1547 knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1548 knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1549 atomic_add_int(&curmq, 1);
1554 * Destroy a message queue
1557 mqueue_free(struct mqueue *mq)
1559 struct mqueue_msg *msg;
1561 while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1562 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1563 free(msg, M_MQUEUEDATA);
1566 mtx_destroy(&mq->mq_mutex);
1567 seldrain(&mq->mq_rsel);
1568 seldrain(&mq->mq_wsel);
1569 knlist_destroy(&mq->mq_rsel.si_note);
1570 knlist_destroy(&mq->mq_wsel.si_note);
1571 uma_zfree(mqueue_zone, mq);
1572 atomic_add_int(&curmq, -1);
1576 * Load a message from user space
1578 static struct mqueue_msg *
1579 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1581 struct mqueue_msg *msg;
1585 len = sizeof(struct mqueue_msg) + msg_size;
1586 msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1587 error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1590 free(msg, M_MQUEUEDATA);
1593 msg->msg_size = msg_size;
1594 msg->msg_prio = msg_prio;
1600 * Save a message to user space
1603 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1607 error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1609 if (error == 0 && msg_prio != NULL)
1610 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1615 * Free a message's memory
1617 static __inline void
1618 mqueue_freemsg(struct mqueue_msg *msg)
1620 free(msg, M_MQUEUEDATA);
1624 * Send a message. if waitok is false, thread will not be
1625 * blocked if there is no data in queue, otherwise, absolute
1626 * time will be checked.
1629 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1630 size_t msg_len, unsigned msg_prio, int waitok,
1631 const struct timespec *abs_timeout)
1633 struct mqueue_msg *msg;
1634 struct timespec ts, ts2;
1638 if (msg_prio >= MQ_PRIO_MAX)
1640 if (msg_len > mq->mq_msgsize)
1642 msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1646 /* O_NONBLOCK case */
1648 error = _mqueue_send(mq, msg, -1);
1654 /* we allow a null timeout (wait forever) */
1655 if (abs_timeout == NULL) {
1656 error = _mqueue_send(mq, msg, 0);
1662 /* send it before checking time */
1663 error = _mqueue_send(mq, msg, -1);
1667 if (error != EAGAIN)
1670 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1677 timespecsub(&ts2, &ts);
1678 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1682 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1683 error = _mqueue_send(mq, msg, tvtohz(&tv));
1684 if (error != ETIMEDOUT)
1690 mqueue_freemsg(msg);
1695 * Common routine to send a message
1698 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1700 struct mqueue_msg *msg2;
1703 mtx_lock(&mq->mq_mutex);
1704 while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1706 mtx_unlock(&mq->mq_mutex);
1710 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1711 PCATCH, "mqsend", timo);
1713 if (error == EAGAIN)
1716 if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1717 mtx_unlock(&mq->mq_mutex);
1721 if (TAILQ_EMPTY(&mq->mq_msgq)) {
1722 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1724 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1725 TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1727 TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1728 if (msg2->msg_prio < msg->msg_prio)
1731 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1735 mq->mq_totalbytes += msg->msg_size;
1736 if (mq->mq_receivers)
1737 wakeup_one(&mq->mq_receivers);
1738 else if (mq->mq_notifier != NULL)
1739 mqueue_send_notification(mq);
1740 if (mq->mq_flags & MQ_RSEL) {
1741 mq->mq_flags &= ~MQ_RSEL;
1742 selwakeup(&mq->mq_rsel);
1744 KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1745 mtx_unlock(&mq->mq_mutex);
1750 * Send realtime a signal to process which registered itself
1751 * successfully by mq_notify.
1754 mqueue_send_notification(struct mqueue *mq)
1756 struct mqueue_notifier *nt;
1761 mtx_assert(&mq->mq_mutex, MA_OWNED);
1762 nt = mq->mq_notifier;
1763 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1765 error = sigev_findtd(p, &nt->nt_sigev, &td);
1767 mq->mq_notifier = NULL;
1770 if (!KSI_ONQ(&nt->nt_ksi)) {
1771 ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1772 tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1776 mq->mq_notifier = NULL;
1780 * Get a message. if waitok is false, thread will not be
1781 * blocked if there is no data in queue, otherwise, absolute
1782 * time will be checked.
1785 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1786 size_t msg_len, unsigned *msg_prio, int waitok,
1787 const struct timespec *abs_timeout)
1789 struct mqueue_msg *msg;
1790 struct timespec ts, ts2;
1794 if (msg_len < mq->mq_msgsize)
1797 /* O_NONBLOCK case */
1799 error = _mqueue_recv(mq, &msg, -1);
1805 /* we allow a null timeout (wait forever). */
1806 if (abs_timeout == NULL) {
1807 error = _mqueue_recv(mq, &msg, 0);
1813 /* try to get a message before checking time */
1814 error = _mqueue_recv(mq, &msg, -1);
1818 if (error != EAGAIN)
1821 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1829 timespecsub(&ts2, &ts);
1830 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1834 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1835 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1838 if (error != ETIMEDOUT)
1843 error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1845 curthread->td_retval[0] = msg->msg_size;
1846 curthread->td_retval[1] = 0;
1848 mqueue_freemsg(msg);
1853 * Common routine to receive a message
1856 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1860 mtx_lock(&mq->mq_mutex);
1861 while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1863 mtx_unlock(&mq->mq_mutex);
1867 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1868 PCATCH, "mqrecv", timo);
1870 if (error == EAGAIN)
1875 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1877 mq->mq_totalbytes -= (*msg)->msg_size;
1879 wakeup_one(&mq->mq_senders);
1880 if (mq->mq_flags & MQ_WSEL) {
1881 mq->mq_flags &= ~MQ_WSEL;
1882 selwakeup(&mq->mq_wsel);
1884 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1886 if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1887 !TAILQ_EMPTY(&mq->mq_msgq)) {
1888 mqueue_send_notification(mq);
1890 mtx_unlock(&mq->mq_mutex);
1894 static __inline struct mqueue_notifier *
1895 notifier_alloc(void)
1897 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1900 static __inline void
1901 notifier_free(struct mqueue_notifier *p)
1903 uma_zfree(mqnoti_zone, p);
1906 static struct mqueue_notifier *
1907 notifier_search(struct proc *p, int fd)
1909 struct mqueue_notifier *nt;
1911 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1912 if (nt->nt_ksi.ksi_mqd == fd)
1918 static __inline void
1919 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1921 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1924 static __inline void
1925 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1927 LIST_REMOVE(nt, nt_link);
1932 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1934 struct mqueue_notifier *nt;
1936 mtx_assert(&mq->mq_mutex, MA_OWNED);
1938 nt = notifier_search(p, fd);
1940 if (mq->mq_notifier == nt)
1941 mq->mq_notifier = NULL;
1942 sigqueue_take(&nt->nt_ksi);
1943 notifier_delete(p, nt);
1949 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1950 const struct mq_attr *attr)
1952 char path[MQFS_NAMELEN + 1];
1953 struct mqfs_node *pn;
1954 struct filedesc *fdp;
1957 int fd, error, len, cmode;
1959 fdp = td->td_proc->p_fd;
1960 cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1962 if ((flags & O_CREAT) != 0 && attr != NULL) {
1963 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1965 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1969 error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1974 * The first character of name must be a slash (/) character
1975 * and the remaining characters of name cannot include any slash
1979 if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
1982 error = falloc(td, &fp, &fd, O_CLOEXEC);
1986 sx_xlock(&mqfs_data.mi_lock);
1987 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1989 if (!(flags & O_CREAT)) {
1992 mq = mqueue_alloc(attr);
1996 pn = mqfs_create_file(mqfs_data.mi_root,
1997 path + 1, len - 1, td->td_ucred,
2010 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2013 accmode_t accmode = 0;
2019 error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2020 pn->mn_gid, accmode, td->td_ucred, NULL);
2025 sx_xunlock(&mqfs_data.mi_lock);
2026 fdclose(fdp, fp, fd, td);
2032 sx_xunlock(&mqfs_data.mi_lock);
2034 finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2037 td->td_retval[0] = fd;
2043 * Syscall to open a message queue.
2046 sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
2048 struct mq_attr attr;
2051 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2053 flags = FFLAGS(uap->flags);
2054 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2055 error = copyin(uap->attr, &attr, sizeof(attr));
2059 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2060 uap->attr != NULL ? &attr : NULL));
2064 * Syscall to unlink a message queue.
2067 sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2069 char path[MQFS_NAMELEN+1];
2070 struct mqfs_node *pn;
2073 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2078 if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2081 sx_xlock(&mqfs_data.mi_lock);
2082 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2084 error = do_unlink(pn, td->td_ucred);
2087 sx_xunlock(&mqfs_data.mi_lock);
2091 typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **);
2094 * Get message queue by giving file slot
2097 _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func,
2098 struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2100 struct mqfs_node *pn;
2103 error = func(td, fd, rightsp, fpp);
2106 if (&mqueueops != (*fpp)->f_ops) {
2110 pn = (*fpp)->f_data;
2119 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2120 struct mqueue **pmq)
2122 cap_rights_t rights;
2124 return _getmq(td, fd, cap_rights_init(&rights, CAP_EVENT), fget,
2129 getmq_read(struct thread *td, int fd, struct file **fpp,
2130 struct mqfs_node **ppn, struct mqueue **pmq)
2132 cap_rights_t rights;
2134 return _getmq(td, fd, cap_rights_init(&rights, CAP_READ), fget_read,
2139 getmq_write(struct thread *td, int fd, struct file **fpp,
2140 struct mqfs_node **ppn, struct mqueue **pmq)
2142 cap_rights_t rights;
2144 return _getmq(td, fd, cap_rights_init(&rights, CAP_WRITE), fget_write,
2149 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2150 struct mq_attr *oattr)
2157 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2159 error = getmq(td, mqd, &fp, NULL, &mq);
2162 oattr->mq_maxmsg = mq->mq_maxmsg;
2163 oattr->mq_msgsize = mq->mq_msgsize;
2164 oattr->mq_curmsgs = mq->mq_curmsgs;
2167 oflag = flag = fp->f_flag;
2168 flag &= ~O_NONBLOCK;
2169 flag |= (attr->mq_flags & O_NONBLOCK);
2170 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2173 oattr->mq_flags = (O_NONBLOCK & oflag);
2179 sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2181 struct mq_attr attr, oattr;
2184 if (uap->attr != NULL) {
2185 error = copyin(uap->attr, &attr, sizeof(attr));
2189 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2191 if (error == 0 && uap->oattr != NULL) {
2192 bzero(oattr.__reserved, sizeof(oattr.__reserved));
2193 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2199 sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2203 struct timespec *abs_timeout, ets;
2207 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2210 if (uap->abs_timeout != NULL) {
2211 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2217 waitok = !(fp->f_flag & O_NONBLOCK);
2218 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2219 uap->msg_prio, waitok, abs_timeout);
2225 sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2229 struct timespec *abs_timeout, ets;
2232 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2235 if (uap->abs_timeout != NULL) {
2236 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2242 waitok = !(fp->f_flag & O_NONBLOCK);
2243 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2244 uap->msg_prio, waitok, abs_timeout);
2250 kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
2253 cap_rights_t rights;
2255 struct filedesc *fdp;
2258 struct file *fp, *fp2;
2259 struct mqueue_notifier *nt, *newnt = NULL;
2262 if (sigev != NULL) {
2263 if (sigev->sigev_notify != SIGEV_SIGNAL &&
2264 sigev->sigev_notify != SIGEV_THREAD_ID &&
2265 sigev->sigev_notify != SIGEV_NONE)
2267 if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2268 sigev->sigev_notify == SIGEV_THREAD_ID) &&
2269 !_SIG_VALID(sigev->sigev_signo))
2273 fdp = td->td_proc->p_fd;
2274 error = getmq(td, mqd, &fp, NULL, &mq);
2278 FILEDESC_SLOCK(fdp);
2279 fp2 = fget_locked(fdp, mqd);
2281 FILEDESC_SUNLOCK(fdp);
2286 error = cap_check(cap_rights(fdp, mqd),
2287 cap_rights_init(&rights, CAP_EVENT));
2289 FILEDESC_SUNLOCK(fdp);
2294 FILEDESC_SUNLOCK(fdp);
2298 mtx_lock(&mq->mq_mutex);
2299 FILEDESC_SUNLOCK(fdp);
2300 if (sigev != NULL) {
2301 if (mq->mq_notifier != NULL) {
2305 nt = notifier_search(p, mqd);
2307 if (newnt == NULL) {
2309 mtx_unlock(&mq->mq_mutex);
2310 newnt = notifier_alloc();
2316 sigqueue_take(&nt->nt_ksi);
2317 if (newnt != NULL) {
2318 notifier_free(newnt);
2324 ksiginfo_init(&nt->nt_ksi);
2325 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2326 nt->nt_ksi.ksi_code = SI_MESGQ;
2328 nt->nt_ksi.ksi_mqd = mqd;
2329 notifier_insert(p, nt);
2331 nt->nt_sigev = *sigev;
2332 mq->mq_notifier = nt;
2335 * if there is no receivers and message queue
2336 * is not empty, we should send notification
2337 * as soon as possible.
2339 if (mq->mq_receivers == 0 &&
2340 !TAILQ_EMPTY(&mq->mq_msgq))
2341 mqueue_send_notification(mq);
2344 notifier_remove(p, mq, mqd);
2346 mtx_unlock(&mq->mq_mutex);
2351 notifier_free(newnt);
2356 sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2358 struct sigevent ev, *evp;
2361 if (uap->sigev == NULL) {
2364 error = copyin(uap->sigev, &ev, sizeof(ev));
2369 return (kern_kmq_notify(td, uap->mqd, evp));
2373 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2375 struct filedesc *fdp;
2378 fdp = td->td_proc->p_fd;
2379 FILEDESC_LOCK_ASSERT(fdp);
2381 if (fp->f_ops == &mqueueops) {
2383 mtx_lock(&mq->mq_mutex);
2384 notifier_remove(td->td_proc, mq, fd);
2386 /* have to wakeup thread in same process */
2387 if (mq->mq_flags & MQ_RSEL) {
2388 mq->mq_flags &= ~MQ_RSEL;
2389 selwakeup(&mq->mq_rsel);
2391 if (mq->mq_flags & MQ_WSEL) {
2392 mq->mq_flags &= ~MQ_WSEL;
2393 selwakeup(&mq->mq_wsel);
2395 mtx_unlock(&mq->mq_mutex);
2400 mq_proc_exit(void *arg __unused, struct proc *p)
2402 struct filedesc *fdp;
2408 FILEDESC_SLOCK(fdp);
2409 for (i = 0; i < fdp->fd_nfiles; ++i) {
2410 fp = fget_locked(fdp, i);
2411 if (fp != NULL && fp->f_ops == &mqueueops) {
2413 mtx_lock(&mq->mq_mutex);
2414 notifier_remove(p, FPTOMQ(fp), i);
2415 mtx_unlock(&mq->mq_mutex);
2418 FILEDESC_SUNLOCK(fdp);
2419 KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2423 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2424 int flags, struct thread *td)
2426 return (EOPNOTSUPP);
2430 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2431 int flags, struct thread *td)
2433 return (EOPNOTSUPP);
2437 mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
2445 mqf_ioctl(struct file *fp, u_long cmd, void *data,
2446 struct ucred *active_cred, struct thread *td)
2452 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2455 struct mqueue *mq = FPTOMQ(fp);
2458 mtx_lock(&mq->mq_mutex);
2459 if (events & (POLLIN | POLLRDNORM)) {
2460 if (mq->mq_curmsgs) {
2461 revents |= events & (POLLIN | POLLRDNORM);
2463 mq->mq_flags |= MQ_RSEL;
2464 selrecord(td, &mq->mq_rsel);
2467 if (events & POLLOUT) {
2468 if (mq->mq_curmsgs < mq->mq_maxmsg)
2471 mq->mq_flags |= MQ_WSEL;
2472 selrecord(td, &mq->mq_wsel);
2475 mtx_unlock(&mq->mq_mutex);
2480 mqf_close(struct file *fp, struct thread *td)
2482 struct mqfs_node *pn;
2484 fp->f_ops = &badfileops;
2487 sx_xlock(&mqfs_data.mi_lock);
2489 sx_xunlock(&mqfs_data.mi_lock);
2494 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2497 struct mqfs_node *pn = fp->f_data;
2499 bzero(st, sizeof *st);
2500 sx_xlock(&mqfs_data.mi_lock);
2501 st->st_atim = pn->mn_atime;
2502 st->st_mtim = pn->mn_mtime;
2503 st->st_ctim = pn->mn_ctime;
2504 st->st_birthtim = pn->mn_birth;
2505 st->st_uid = pn->mn_uid;
2506 st->st_gid = pn->mn_gid;
2507 st->st_mode = S_IFIFO | pn->mn_mode;
2508 sx_xunlock(&mqfs_data.mi_lock);
2513 mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
2516 struct mqfs_node *pn;
2521 sx_xlock(&mqfs_data.mi_lock);
2522 error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2526 pn->mn_mode = mode & ACCESSPERMS;
2528 sx_xunlock(&mqfs_data.mi_lock);
2533 mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2536 struct mqfs_node *pn;
2541 sx_xlock(&mqfs_data.mi_lock);
2542 if (uid == (uid_t)-1)
2544 if (gid == (gid_t)-1)
2546 if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2547 (gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
2548 (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
2553 sx_xunlock(&mqfs_data.mi_lock);
2558 mqf_kqfilter(struct file *fp, struct knote *kn)
2560 struct mqueue *mq = FPTOMQ(fp);
2563 if (kn->kn_filter == EVFILT_READ) {
2564 kn->kn_fop = &mq_rfiltops;
2565 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2566 } else if (kn->kn_filter == EVFILT_WRITE) {
2567 kn->kn_fop = &mq_wfiltops;
2568 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2575 filt_mqdetach(struct knote *kn)
2577 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2579 if (kn->kn_filter == EVFILT_READ)
2580 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2581 else if (kn->kn_filter == EVFILT_WRITE)
2582 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2584 panic("filt_mqdetach");
2588 filt_mqread(struct knote *kn, long hint)
2590 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2592 mtx_assert(&mq->mq_mutex, MA_OWNED);
2593 return (mq->mq_curmsgs != 0);
2597 filt_mqwrite(struct knote *kn, long hint)
2599 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2601 mtx_assert(&mq->mq_mutex, MA_OWNED);
2602 return (mq->mq_curmsgs < mq->mq_maxmsg);
2605 static struct fileops mqueueops = {
2606 .fo_read = mqf_read,
2607 .fo_write = mqf_write,
2608 .fo_truncate = mqf_truncate,
2609 .fo_ioctl = mqf_ioctl,
2610 .fo_poll = mqf_poll,
2611 .fo_kqfilter = mqf_kqfilter,
2612 .fo_stat = mqf_stat,
2613 .fo_chmod = mqf_chmod,
2614 .fo_chown = mqf_chown,
2615 .fo_close = mqf_close,
2616 .fo_sendfile = invfo_sendfile,
2619 static struct vop_vector mqfs_vnodeops = {
2620 .vop_default = &default_vnodeops,
2621 .vop_access = mqfs_access,
2622 .vop_cachedlookup = mqfs_lookup,
2623 .vop_lookup = vfs_cache_lookup,
2624 .vop_reclaim = mqfs_reclaim,
2625 .vop_create = mqfs_create,
2626 .vop_remove = mqfs_remove,
2627 .vop_inactive = mqfs_inactive,
2628 .vop_open = mqfs_open,
2629 .vop_close = mqfs_close,
2630 .vop_getattr = mqfs_getattr,
2631 .vop_setattr = mqfs_setattr,
2632 .vop_read = mqfs_read,
2633 .vop_write = VOP_EOPNOTSUPP,
2634 .vop_readdir = mqfs_readdir,
2635 .vop_mkdir = VOP_EOPNOTSUPP,
2636 .vop_rmdir = VOP_EOPNOTSUPP
2639 static struct vfsops mqfs_vfsops = {
2640 .vfs_init = mqfs_init,
2641 .vfs_uninit = mqfs_uninit,
2642 .vfs_mount = mqfs_mount,
2643 .vfs_unmount = mqfs_unmount,
2644 .vfs_root = mqfs_root,
2645 .vfs_statfs = mqfs_statfs,
2648 static struct vfsconf mqueuefs_vfsconf = {
2649 .vfc_version = VFS_VERSION,
2650 .vfc_name = "mqueuefs",
2651 .vfc_vfsops = &mqfs_vfsops,
2653 .vfc_flags = VFCF_SYNTHETIC
2656 static struct syscall_helper_data mq_syscalls[] = {
2657 SYSCALL_INIT_HELPER(kmq_open),
2658 SYSCALL_INIT_HELPER(kmq_setattr),
2659 SYSCALL_INIT_HELPER(kmq_timedsend),
2660 SYSCALL_INIT_HELPER(kmq_timedreceive),
2661 SYSCALL_INIT_HELPER(kmq_notify),
2662 SYSCALL_INIT_HELPER(kmq_unlink),
2666 #ifdef COMPAT_FREEBSD32
2667 #include <compat/freebsd32/freebsd32.h>
2668 #include <compat/freebsd32/freebsd32_proto.h>
2669 #include <compat/freebsd32/freebsd32_signal.h>
2670 #include <compat/freebsd32/freebsd32_syscall.h>
2671 #include <compat/freebsd32/freebsd32_util.h>
2674 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2677 to->mq_flags = from->mq_flags;
2678 to->mq_maxmsg = from->mq_maxmsg;
2679 to->mq_msgsize = from->mq_msgsize;
2680 to->mq_curmsgs = from->mq_curmsgs;
2684 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2687 to->mq_flags = from->mq_flags;
2688 to->mq_maxmsg = from->mq_maxmsg;
2689 to->mq_msgsize = from->mq_msgsize;
2690 to->mq_curmsgs = from->mq_curmsgs;
2694 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2696 struct mq_attr attr;
2697 struct mq_attr32 attr32;
2700 if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2702 flags = FFLAGS(uap->flags);
2703 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2704 error = copyin(uap->attr, &attr32, sizeof(attr32));
2707 mq_attr_from32(&attr32, &attr);
2709 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2710 uap->attr != NULL ? &attr : NULL));
2714 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2716 struct mq_attr attr, oattr;
2717 struct mq_attr32 attr32, oattr32;
2720 if (uap->attr != NULL) {
2721 error = copyin(uap->attr, &attr32, sizeof(attr32));
2724 mq_attr_from32(&attr32, &attr);
2726 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2728 if (error == 0 && uap->oattr != NULL) {
2729 mq_attr_to32(&oattr, &oattr32);
2730 bzero(oattr32.__reserved, sizeof(oattr32.__reserved));
2731 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2737 freebsd32_kmq_timedsend(struct thread *td,
2738 struct freebsd32_kmq_timedsend_args *uap)
2742 struct timespec32 ets32;
2743 struct timespec *abs_timeout, ets;
2747 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2750 if (uap->abs_timeout != NULL) {
2751 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2754 CP(ets32, ets, tv_sec);
2755 CP(ets32, ets, tv_nsec);
2759 waitok = !(fp->f_flag & O_NONBLOCK);
2760 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2761 uap->msg_prio, waitok, abs_timeout);
2767 freebsd32_kmq_timedreceive(struct thread *td,
2768 struct freebsd32_kmq_timedreceive_args *uap)
2772 struct timespec32 ets32;
2773 struct timespec *abs_timeout, ets;
2776 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2779 if (uap->abs_timeout != NULL) {
2780 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2783 CP(ets32, ets, tv_sec);
2784 CP(ets32, ets, tv_nsec);
2788 waitok = !(fp->f_flag & O_NONBLOCK);
2789 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2790 uap->msg_prio, waitok, abs_timeout);
2796 freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap)
2798 struct sigevent ev, *evp;
2799 struct sigevent32 ev32;
2802 if (uap->sigev == NULL) {
2805 error = copyin(uap->sigev, &ev32, sizeof(ev32));
2808 error = convert_sigevent32(&ev32, &ev);
2813 return (kern_kmq_notify(td, uap->mqd, evp));
2816 static struct syscall_helper_data mq32_syscalls[] = {
2817 SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2818 SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2819 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2820 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
2821 SYSCALL32_INIT_HELPER(freebsd32_kmq_notify),
2822 SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2832 error = syscall_helper_register(mq_syscalls);
2835 #ifdef COMPAT_FREEBSD32
2836 error = syscall32_helper_register(mq32_syscalls);
2847 #ifdef COMPAT_FREEBSD32
2848 syscall32_helper_unregister(mq32_syscalls);
2850 syscall_helper_unregister(mq_syscalls);
2855 mq_modload(struct module *module, int cmd, void *arg)
2859 error = vfs_modevent(module, cmd, arg);
2878 static moduledata_t mqueuefs_mod = {
2883 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2884 MODULE_VERSION(mqueuefs, 1);