xref: /freebsd-12.1/sys/kern/uipc_mqueue.c (revision 66ace87d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 David Xu <[email protected]>
5  * Copyright (c) 2016-2017 Robert N. M. Watson
6  * All rights reserved.
7  *
8  * Portions of this software were developed by BAE Systems, the University of
9  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
10  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
11  * Computing (TC) research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 /*
37  * POSIX message queue implementation.
38  *
39  * 1) A mqueue filesystem can be mounted, each message queue appears
40  *    in mounted directory, user can change queue's permission and
41  *    ownership, or remove a queue. Manually creating a file in the
42  *    directory causes a message queue to be created in the kernel with
43  *    default message queue attributes applied and same name used, this
44  *    method is not advocated since mq_open syscall allows user to specify
45  *    different attributes. Also the file system can be mounted multiple
46  *    times at different mount points but shows same contents.
47  *
48  * 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
49  *    but directly operate on internal data structure, this allows user to
50  *    use the IPC facility without having to mount mqueue file system.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include "opt_capsicum.h"
57 
58 #include <sys/param.h>
59 #include <sys/kernel.h>
60 #include <sys/systm.h>
61 #include <sys/limits.h>
62 #include <sys/malloc.h>
63 #include <sys/buf.h>
64 #include <sys/capsicum.h>
65 #include <sys/dirent.h>
66 #include <sys/event.h>
67 #include <sys/eventhandler.h>
68 #include <sys/fcntl.h>
69 #include <sys/file.h>
70 #include <sys/filedesc.h>
71 #include <sys/jail.h>
72 #include <sys/lock.h>
73 #include <sys/module.h>
74 #include <sys/mount.h>
75 #include <sys/mqueue.h>
76 #include <sys/mutex.h>
77 #include <sys/namei.h>
78 #include <sys/posix4.h>
79 #include <sys/poll.h>
80 #include <sys/priv.h>
81 #include <sys/proc.h>
82 #include <sys/queue.h>
83 #include <sys/sysproto.h>
84 #include <sys/stat.h>
85 #include <sys/syscall.h>
86 #include <sys/syscallsubr.h>
87 #include <sys/sysent.h>
88 #include <sys/sx.h>
89 #include <sys/sysctl.h>
90 #include <sys/taskqueue.h>
91 #include <sys/unistd.h>
92 #include <sys/user.h>
93 #include <sys/vnode.h>
94 #include <machine/atomic.h>
95 
96 #include <security/audit/audit.h>
97 
98 FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
99 
100 /*
101  * Limits and constants
102  */
103 #define	MQFS_NAMELEN		NAME_MAX
104 #define MQFS_DELEN		(8 + MQFS_NAMELEN)
105 
106 /* node types */
107 typedef enum {
108 	mqfstype_none = 0,
109 	mqfstype_root,
110 	mqfstype_dir,
111 	mqfstype_this,
112 	mqfstype_parent,
113 	mqfstype_file,
114 	mqfstype_symlink,
115 } mqfs_type_t;
116 
117 struct mqfs_node;
118 
119 /*
120  * mqfs_info: describes a mqfs instance
121  */
122 struct mqfs_info {
123 	struct sx		mi_lock;
124 	struct mqfs_node	*mi_root;
125 	struct unrhdr		*mi_unrhdr;
126 };
127 
128 struct mqfs_vdata {
129 	LIST_ENTRY(mqfs_vdata)	mv_link;
130 	struct mqfs_node	*mv_node;
131 	struct vnode		*mv_vnode;
132 	struct task		mv_task;
133 };
134 
135 /*
136  * mqfs_node: describes a node (file or directory) within a mqfs
137  */
138 struct mqfs_node {
139 	char			mn_name[MQFS_NAMELEN+1];
140 	struct mqfs_info	*mn_info;
141 	struct mqfs_node	*mn_parent;
142 	LIST_HEAD(,mqfs_node)	mn_children;
143 	LIST_ENTRY(mqfs_node)	mn_sibling;
144 	LIST_HEAD(,mqfs_vdata)	mn_vnodes;
145 	const void		*mn_pr_root;
146 	int			mn_refcount;
147 	mqfs_type_t		mn_type;
148 	int			mn_deleted;
149 	uint32_t		mn_fileno;
150 	void			*mn_data;
151 	struct timespec		mn_birth;
152 	struct timespec		mn_ctime;
153 	struct timespec		mn_atime;
154 	struct timespec		mn_mtime;
155 	uid_t			mn_uid;
156 	gid_t			mn_gid;
157 	int			mn_mode;
158 };
159 
160 #define	VTON(vp)	(((struct mqfs_vdata *)((vp)->v_data))->mv_node)
161 #define VTOMQ(vp) 	((struct mqueue *)(VTON(vp)->mn_data))
162 #define	VFSTOMQFS(m)	((struct mqfs_info *)((m)->mnt_data))
163 #define	FPTOMQ(fp)	((struct mqueue *)(((struct mqfs_node *) \
164 				(fp)->f_data)->mn_data))
165 
166 TAILQ_HEAD(msgq, mqueue_msg);
167 
168 struct mqueue;
169 
170 struct mqueue_notifier {
171 	LIST_ENTRY(mqueue_notifier)	nt_link;
172 	struct sigevent			nt_sigev;
173 	ksiginfo_t			nt_ksi;
174 	struct proc			*nt_proc;
175 };
176 
177 struct mqueue {
178 	struct mtx	mq_mutex;
179 	int		mq_flags;
180 	long		mq_maxmsg;
181 	long		mq_msgsize;
182 	long		mq_curmsgs;
183 	long		mq_totalbytes;
184 	struct msgq	mq_msgq;
185 	int		mq_receivers;
186 	int		mq_senders;
187 	struct selinfo	mq_rsel;
188 	struct selinfo	mq_wsel;
189 	struct mqueue_notifier	*mq_notifier;
190 };
191 
192 #define	MQ_RSEL		0x01
193 #define	MQ_WSEL		0x02
194 
195 struct mqueue_msg {
196 	TAILQ_ENTRY(mqueue_msg)	msg_link;
197 	unsigned int	msg_prio;
198 	unsigned int	msg_size;
199 	/* following real data... */
200 };
201 
202 static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
203 	"POSIX real time message queue");
204 
205 static int	default_maxmsg  = 10;
206 static int	default_msgsize = 1024;
207 
208 static int	maxmsg = 100;
209 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
210     &maxmsg, 0, "Default maximum messages in queue");
211 static int	maxmsgsize = 16384;
212 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
213     &maxmsgsize, 0, "Default maximum message size");
214 static int	maxmq = 100;
215 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
216     &maxmq, 0, "maximum message queues");
217 static int	curmq = 0;
218 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
219     &curmq, 0, "current message queue number");
220 static int	unloadable = 0;
221 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
222 
223 static eventhandler_tag exit_tag;
224 
225 /* Only one instance per-system */
226 static struct mqfs_info		mqfs_data;
227 static uma_zone_t		mqnode_zone;
228 static uma_zone_t		mqueue_zone;
229 static uma_zone_t		mvdata_zone;
230 static uma_zone_t		mqnoti_zone;
231 static struct vop_vector	mqfs_vnodeops;
232 static struct fileops		mqueueops;
233 static unsigned			mqfs_osd_jail_slot;
234 
235 /*
236  * Directory structure construction and manipulation
237  */
238 #ifdef notyet
239 static struct mqfs_node	*mqfs_create_dir(struct mqfs_node *parent,
240 	const char *name, int namelen, struct ucred *cred, int mode);
241 static struct mqfs_node	*mqfs_create_link(struct mqfs_node *parent,
242 	const char *name, int namelen, struct ucred *cred, int mode);
243 #endif
244 
245 static struct mqfs_node	*mqfs_create_file(struct mqfs_node *parent,
246 	const char *name, int namelen, struct ucred *cred, int mode);
247 static int	mqfs_destroy(struct mqfs_node *mn);
248 static void	mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
249 static void	mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
250 static int	mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
251 static int	mqfs_prison_remove(void *obj, void *data);
252 
253 /*
254  * Message queue construction and maniplation
255  */
256 static struct mqueue	*mqueue_alloc(const struct mq_attr *attr);
257 static void	mqueue_free(struct mqueue *mq);
258 static int	mqueue_send(struct mqueue *mq, const char *msg_ptr,
259 			size_t msg_len, unsigned msg_prio, int waitok,
260 			const struct timespec *abs_timeout);
261 static int	mqueue_receive(struct mqueue *mq, char *msg_ptr,
262 			size_t msg_len, unsigned *msg_prio, int waitok,
263 			const struct timespec *abs_timeout);
264 static int	_mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
265 			int timo);
266 static int	_mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
267 			int timo);
268 static void	mqueue_send_notification(struct mqueue *mq);
269 static void	mqueue_fdclose(struct thread *td, int fd, struct file *fp);
270 static void	mq_proc_exit(void *arg, struct proc *p);
271 
272 /*
273  * kqueue filters
274  */
275 static void	filt_mqdetach(struct knote *kn);
276 static int	filt_mqread(struct knote *kn, long hint);
277 static int	filt_mqwrite(struct knote *kn, long hint);
278 
279 struct filterops mq_rfiltops = {
280 	.f_isfd = 1,
281 	.f_detach = filt_mqdetach,
282 	.f_event = filt_mqread,
283 };
284 struct filterops mq_wfiltops = {
285 	.f_isfd = 1,
286 	.f_detach = filt_mqdetach,
287 	.f_event = filt_mqwrite,
288 };
289 
290 /*
291  * Initialize fileno bitmap
292  */
293 static void
mqfs_fileno_init(struct mqfs_info * mi)294 mqfs_fileno_init(struct mqfs_info *mi)
295 {
296 	struct unrhdr *up;
297 
298 	up = new_unrhdr(1, INT_MAX, NULL);
299 	mi->mi_unrhdr = up;
300 }
301 
302 /*
303  * Tear down fileno bitmap
304  */
305 static void
mqfs_fileno_uninit(struct mqfs_info * mi)306 mqfs_fileno_uninit(struct mqfs_info *mi)
307 {
308 	struct unrhdr *up;
309 
310 	up = mi->mi_unrhdr;
311 	mi->mi_unrhdr = NULL;
312 	delete_unrhdr(up);
313 }
314 
315 /*
316  * Allocate a file number
317  */
318 static void
mqfs_fileno_alloc(struct mqfs_info * mi,struct mqfs_node * mn)319 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
320 {
321 	/* make sure our parent has a file number */
322 	if (mn->mn_parent && !mn->mn_parent->mn_fileno)
323 		mqfs_fileno_alloc(mi, mn->mn_parent);
324 
325 	switch (mn->mn_type) {
326 	case mqfstype_root:
327 	case mqfstype_dir:
328 	case mqfstype_file:
329 	case mqfstype_symlink:
330 		mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
331 		break;
332 	case mqfstype_this:
333 		KASSERT(mn->mn_parent != NULL,
334 		    ("mqfstype_this node has no parent"));
335 		mn->mn_fileno = mn->mn_parent->mn_fileno;
336 		break;
337 	case mqfstype_parent:
338 		KASSERT(mn->mn_parent != NULL,
339 		    ("mqfstype_parent node has no parent"));
340 		if (mn->mn_parent == mi->mi_root) {
341 			mn->mn_fileno = mn->mn_parent->mn_fileno;
342 			break;
343 		}
344 		KASSERT(mn->mn_parent->mn_parent != NULL,
345 		    ("mqfstype_parent node has no grandparent"));
346 		mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
347 		break;
348 	default:
349 		KASSERT(0,
350 		    ("mqfs_fileno_alloc() called for unknown type node: %d",
351 			mn->mn_type));
352 		break;
353 	}
354 }
355 
356 /*
357  * Release a file number
358  */
359 static void
mqfs_fileno_free(struct mqfs_info * mi,struct mqfs_node * mn)360 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
361 {
362 	switch (mn->mn_type) {
363 	case mqfstype_root:
364 	case mqfstype_dir:
365 	case mqfstype_file:
366 	case mqfstype_symlink:
367 		free_unr(mi->mi_unrhdr, mn->mn_fileno);
368 		break;
369 	case mqfstype_this:
370 	case mqfstype_parent:
371 		/* ignore these, as they don't "own" their file number */
372 		break;
373 	default:
374 		KASSERT(0,
375 		    ("mqfs_fileno_free() called for unknown type node: %d",
376 			mn->mn_type));
377 		break;
378 	}
379 }
380 
381 static __inline struct mqfs_node *
mqnode_alloc(void)382 mqnode_alloc(void)
383 {
384 	return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
385 }
386 
387 static __inline void
mqnode_free(struct mqfs_node * node)388 mqnode_free(struct mqfs_node *node)
389 {
390 	uma_zfree(mqnode_zone, node);
391 }
392 
393 static __inline void
mqnode_addref(struct mqfs_node * node)394 mqnode_addref(struct mqfs_node *node)
395 {
396 	atomic_add_int(&node->mn_refcount, 1);
397 }
398 
399 static __inline void
mqnode_release(struct mqfs_node * node)400 mqnode_release(struct mqfs_node *node)
401 {
402 	struct mqfs_info *mqfs;
403 	int old, exp;
404 
405 	mqfs = node->mn_info;
406 	old = atomic_fetchadd_int(&node->mn_refcount, -1);
407 	if (node->mn_type == mqfstype_dir ||
408 	    node->mn_type == mqfstype_root)
409 		exp = 3; /* include . and .. */
410 	else
411 		exp = 1;
412 	if (old == exp) {
413 		int locked = sx_xlocked(&mqfs->mi_lock);
414 		if (!locked)
415 			sx_xlock(&mqfs->mi_lock);
416 		mqfs_destroy(node);
417 		if (!locked)
418 			sx_xunlock(&mqfs->mi_lock);
419 	}
420 }
421 
422 /*
423  * Add a node to a directory
424  */
425 static int
mqfs_add_node(struct mqfs_node * parent,struct mqfs_node * node)426 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
427 {
428 	KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
429 	KASSERT(parent->mn_info != NULL,
430 	    ("%s(): parent has no mn_info", __func__));
431 	KASSERT(parent->mn_type == mqfstype_dir ||
432 	    parent->mn_type == mqfstype_root,
433 	    ("%s(): parent is not a directory", __func__));
434 
435 	node->mn_info = parent->mn_info;
436 	node->mn_parent = parent;
437 	LIST_INIT(&node->mn_children);
438 	LIST_INIT(&node->mn_vnodes);
439 	LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
440 	mqnode_addref(parent);
441 	return (0);
442 }
443 
444 static struct mqfs_node *
mqfs_create_node(const char * name,int namelen,struct ucred * cred,int mode,int nodetype)445 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
446 	int nodetype)
447 {
448 	struct mqfs_node *node;
449 
450 	node = mqnode_alloc();
451 	strncpy(node->mn_name, name, namelen);
452 	node->mn_pr_root = cred->cr_prison->pr_root;
453 	node->mn_type = nodetype;
454 	node->mn_refcount = 1;
455 	vfs_timestamp(&node->mn_birth);
456 	node->mn_ctime = node->mn_atime = node->mn_mtime
457 		= node->mn_birth;
458 	node->mn_uid = cred->cr_uid;
459 	node->mn_gid = cred->cr_gid;
460 	node->mn_mode = mode;
461 	return (node);
462 }
463 
464 /*
465  * Create a file
466  */
467 static struct mqfs_node *
mqfs_create_file(struct mqfs_node * parent,const char * name,int namelen,struct ucred * cred,int mode)468 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
469 	struct ucred *cred, int mode)
470 {
471 	struct mqfs_node *node;
472 
473 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
474 	if (mqfs_add_node(parent, node) != 0) {
475 		mqnode_free(node);
476 		return (NULL);
477 	}
478 	return (node);
479 }
480 
481 /*
482  * Add . and .. to a directory
483  */
484 static int
mqfs_fixup_dir(struct mqfs_node * parent)485 mqfs_fixup_dir(struct mqfs_node *parent)
486 {
487 	struct mqfs_node *dir;
488 
489 	dir = mqnode_alloc();
490 	dir->mn_name[0] = '.';
491 	dir->mn_type = mqfstype_this;
492 	dir->mn_refcount = 1;
493 	if (mqfs_add_node(parent, dir) != 0) {
494 		mqnode_free(dir);
495 		return (-1);
496 	}
497 
498 	dir = mqnode_alloc();
499 	dir->mn_name[0] = dir->mn_name[1] = '.';
500 	dir->mn_type = mqfstype_parent;
501 	dir->mn_refcount = 1;
502 
503 	if (mqfs_add_node(parent, dir) != 0) {
504 		mqnode_free(dir);
505 		return (-1);
506 	}
507 
508 	return (0);
509 }
510 
511 #ifdef notyet
512 
513 /*
514  * Create a directory
515  */
516 static struct mqfs_node *
mqfs_create_dir(struct mqfs_node * parent,const char * name,int namelen,struct ucred * cred,int mode)517 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
518 	struct ucred *cred, int mode)
519 {
520 	struct mqfs_node *node;
521 
522 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
523 	if (mqfs_add_node(parent, node) != 0) {
524 		mqnode_free(node);
525 		return (NULL);
526 	}
527 
528 	if (mqfs_fixup_dir(node) != 0) {
529 		mqfs_destroy(node);
530 		return (NULL);
531 	}
532 	return (node);
533 }
534 
535 /*
536  * Create a symlink
537  */
538 static struct mqfs_node *
mqfs_create_link(struct mqfs_node * parent,const char * name,int namelen,struct ucred * cred,int mode)539 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
540 	struct ucred *cred, int mode)
541 {
542 	struct mqfs_node *node;
543 
544 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
545 	if (mqfs_add_node(parent, node) != 0) {
546 		mqnode_free(node);
547 		return (NULL);
548 	}
549 	return (node);
550 }
551 
552 #endif
553 
554 /*
555  * Destroy a node or a tree of nodes
556  */
557 static int
mqfs_destroy(struct mqfs_node * node)558 mqfs_destroy(struct mqfs_node *node)
559 {
560 	struct mqfs_node *parent;
561 
562 	KASSERT(node != NULL,
563 	    ("%s(): node is NULL", __func__));
564 	KASSERT(node->mn_info != NULL,
565 	    ("%s(): node has no mn_info", __func__));
566 
567 	/* destroy children */
568 	if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
569 		while (! LIST_EMPTY(&node->mn_children))
570 			mqfs_destroy(LIST_FIRST(&node->mn_children));
571 
572 	/* unlink from parent */
573 	if ((parent = node->mn_parent) != NULL) {
574 		KASSERT(parent->mn_info == node->mn_info,
575 		    ("%s(): parent has different mn_info", __func__));
576 		LIST_REMOVE(node, mn_sibling);
577 	}
578 
579 	if (node->mn_fileno != 0)
580 		mqfs_fileno_free(node->mn_info, node);
581 	if (node->mn_data != NULL)
582 		mqueue_free(node->mn_data);
583 	mqnode_free(node);
584 	return (0);
585 }
586 
587 /*
588  * Mount a mqfs instance
589  */
590 static int
mqfs_mount(struct mount * mp)591 mqfs_mount(struct mount *mp)
592 {
593 	struct statfs *sbp;
594 
595 	if (mp->mnt_flag & MNT_UPDATE)
596 		return (EOPNOTSUPP);
597 
598 	mp->mnt_data = &mqfs_data;
599 	MNT_ILOCK(mp);
600 	mp->mnt_flag |= MNT_LOCAL;
601 	MNT_IUNLOCK(mp);
602 	vfs_getnewfsid(mp);
603 
604 	sbp = &mp->mnt_stat;
605 	vfs_mountedfrom(mp, "mqueue");
606 	sbp->f_bsize = PAGE_SIZE;
607 	sbp->f_iosize = PAGE_SIZE;
608 	sbp->f_blocks = 1;
609 	sbp->f_bfree = 0;
610 	sbp->f_bavail = 0;
611 	sbp->f_files = 1;
612 	sbp->f_ffree = 0;
613 	return (0);
614 }
615 
616 /*
617  * Unmount a mqfs instance
618  */
619 static int
mqfs_unmount(struct mount * mp,int mntflags)620 mqfs_unmount(struct mount *mp, int mntflags)
621 {
622 	int error;
623 
624 	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
625 	    curthread);
626 	return (error);
627 }
628 
629 /*
630  * Return a root vnode
631  */
632 static int
mqfs_root(struct mount * mp,int flags,struct vnode ** vpp)633 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
634 {
635 	struct mqfs_info *mqfs;
636 	int ret;
637 
638 	mqfs = VFSTOMQFS(mp);
639 	ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
640 	return (ret);
641 }
642 
643 /*
644  * Return filesystem stats
645  */
646 static int
mqfs_statfs(struct mount * mp,struct statfs * sbp)647 mqfs_statfs(struct mount *mp, struct statfs *sbp)
648 {
649 	/* XXX update statistics */
650 	return (0);
651 }
652 
653 /*
654  * Initialize a mqfs instance
655  */
656 static int
mqfs_init(struct vfsconf * vfc)657 mqfs_init(struct vfsconf *vfc)
658 {
659 	struct mqfs_node *root;
660 	struct mqfs_info *mi;
661 	osd_method_t methods[PR_MAXMETHOD] = {
662 	    [PR_METHOD_REMOVE] = mqfs_prison_remove,
663 	};
664 
665 	mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
666 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
667 	mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
668 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
669 	mvdata_zone = uma_zcreate("mvdata",
670 		sizeof(struct mqfs_vdata), NULL, NULL, NULL,
671 		NULL, UMA_ALIGN_PTR, 0);
672 	mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
673 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
674 	mi = &mqfs_data;
675 	sx_init(&mi->mi_lock, "mqfs lock");
676 	/* set up the root diretory */
677 	root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
678 		mqfstype_root);
679 	root->mn_info = mi;
680 	LIST_INIT(&root->mn_children);
681 	LIST_INIT(&root->mn_vnodes);
682 	mi->mi_root = root;
683 	mqfs_fileno_init(mi);
684 	mqfs_fileno_alloc(mi, root);
685 	mqfs_fixup_dir(root);
686 	exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
687 	    EVENTHANDLER_PRI_ANY);
688 	mq_fdclose = mqueue_fdclose;
689 	p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
690 	mqfs_osd_jail_slot = osd_jail_register(NULL, methods);
691 	return (0);
692 }
693 
694 /*
695  * Destroy a mqfs instance
696  */
697 static int
mqfs_uninit(struct vfsconf * vfc)698 mqfs_uninit(struct vfsconf *vfc)
699 {
700 	struct mqfs_info *mi;
701 
702 	if (!unloadable)
703 		return (EOPNOTSUPP);
704 	osd_jail_deregister(mqfs_osd_jail_slot);
705 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
706 	mi = &mqfs_data;
707 	mqfs_destroy(mi->mi_root);
708 	mi->mi_root = NULL;
709 	mqfs_fileno_uninit(mi);
710 	sx_destroy(&mi->mi_lock);
711 	uma_zdestroy(mqnode_zone);
712 	uma_zdestroy(mqueue_zone);
713 	uma_zdestroy(mvdata_zone);
714 	uma_zdestroy(mqnoti_zone);
715 	return (0);
716 }
717 
718 /*
719  * task routine
720  */
721 static void
do_recycle(void * context,int pending __unused)722 do_recycle(void *context, int pending __unused)
723 {
724 	struct vnode *vp = (struct vnode *)context;
725 
726 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
727 	vrecycle(vp);
728 	VOP_UNLOCK(vp, 0);
729 	vdrop(vp);
730 }
731 
732 /*
733  * Allocate a vnode
734  */
735 static int
mqfs_allocv(struct mount * mp,struct vnode ** vpp,struct mqfs_node * pn)736 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
737 {
738 	struct mqfs_vdata *vd;
739 	struct mqfs_info  *mqfs;
740 	struct vnode *newvpp;
741 	int error;
742 
743 	mqfs = pn->mn_info;
744 	*vpp = NULL;
745 	sx_xlock(&mqfs->mi_lock);
746 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
747 		if (vd->mv_vnode->v_mount == mp) {
748 			vhold(vd->mv_vnode);
749 			break;
750 		}
751 	}
752 
753 	if (vd != NULL) {
754 found:
755 		*vpp = vd->mv_vnode;
756 		sx_xunlock(&mqfs->mi_lock);
757 		error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
758 		vdrop(*vpp);
759 		return (error);
760 	}
761 	sx_xunlock(&mqfs->mi_lock);
762 
763 	error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
764 	if (error)
765 		return (error);
766 	vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
767 	error = insmntque(newvpp, mp);
768 	if (error != 0)
769 		return (error);
770 
771 	sx_xlock(&mqfs->mi_lock);
772 	/*
773 	 * Check if it has already been allocated
774 	 * while we were blocked.
775 	 */
776 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
777 		if (vd->mv_vnode->v_mount == mp) {
778 			vhold(vd->mv_vnode);
779 			sx_xunlock(&mqfs->mi_lock);
780 
781 			vgone(newvpp);
782 			vput(newvpp);
783 			goto found;
784 		}
785 	}
786 
787 	*vpp = newvpp;
788 
789 	vd = uma_zalloc(mvdata_zone, M_WAITOK);
790 	(*vpp)->v_data = vd;
791 	vd->mv_vnode = *vpp;
792 	vd->mv_node = pn;
793 	TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
794 	LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
795 	mqnode_addref(pn);
796 	switch (pn->mn_type) {
797 	case mqfstype_root:
798 		(*vpp)->v_vflag = VV_ROOT;
799 		/* fall through */
800 	case mqfstype_dir:
801 	case mqfstype_this:
802 	case mqfstype_parent:
803 		(*vpp)->v_type = VDIR;
804 		break;
805 	case mqfstype_file:
806 		(*vpp)->v_type = VREG;
807 		break;
808 	case mqfstype_symlink:
809 		(*vpp)->v_type = VLNK;
810 		break;
811 	case mqfstype_none:
812 		KASSERT(0, ("mqfs_allocf called for null node\n"));
813 	default:
814 		panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
815 	}
816 	sx_xunlock(&mqfs->mi_lock);
817 	return (0);
818 }
819 
820 /*
821  * Search a directory entry
822  */
823 static struct mqfs_node *
mqfs_search(struct mqfs_node * pd,const char * name,int len,struct ucred * cred)824 mqfs_search(struct mqfs_node *pd, const char *name, int len, struct ucred *cred)
825 {
826 	struct mqfs_node *pn;
827 	const void *pr_root;
828 
829 	sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
830 	pr_root = cred->cr_prison->pr_root;
831 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
832 		/* Only match names within the same prison root directory */
833 		if ((pn->mn_pr_root == NULL || pn->mn_pr_root == pr_root) &&
834 		    strncmp(pn->mn_name, name, len) == 0 &&
835 		    pn->mn_name[len] == '\0')
836 			return (pn);
837 	}
838 	return (NULL);
839 }
840 
841 /*
842  * Look up a file or directory.
843  */
844 static int
mqfs_lookupx(struct vop_cachedlookup_args * ap)845 mqfs_lookupx(struct vop_cachedlookup_args *ap)
846 {
847 	struct componentname *cnp;
848 	struct vnode *dvp, **vpp;
849 	struct mqfs_node *pd;
850 	struct mqfs_node *pn;
851 	struct mqfs_info *mqfs;
852 	int nameiop, flags, error, namelen;
853 	char *pname;
854 	struct thread *td;
855 
856 	cnp = ap->a_cnp;
857 	vpp = ap->a_vpp;
858 	dvp = ap->a_dvp;
859 	pname = cnp->cn_nameptr;
860 	namelen = cnp->cn_namelen;
861 	td = cnp->cn_thread;
862 	flags = cnp->cn_flags;
863 	nameiop = cnp->cn_nameiop;
864 	pd = VTON(dvp);
865 	pn = NULL;
866 	mqfs = pd->mn_info;
867 	*vpp = NULLVP;
868 
869 	if (dvp->v_type != VDIR)
870 		return (ENOTDIR);
871 
872 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
873 	if (error)
874 		return (error);
875 
876 	/* shortcut: check if the name is too long */
877 	if (cnp->cn_namelen >= MQFS_NAMELEN)
878 		return (ENOENT);
879 
880 	/* self */
881 	if (namelen == 1 && pname[0] == '.') {
882 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
883 			return (EINVAL);
884 		pn = pd;
885 		*vpp = dvp;
886 		VREF(dvp);
887 		return (0);
888 	}
889 
890 	/* parent */
891 	if (cnp->cn_flags & ISDOTDOT) {
892 		if (dvp->v_vflag & VV_ROOT)
893 			return (EIO);
894 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
895 			return (EINVAL);
896 		VOP_UNLOCK(dvp, 0);
897 		KASSERT(pd->mn_parent, ("non-root directory has no parent"));
898 		pn = pd->mn_parent;
899 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
900 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
901 		return (error);
902 	}
903 
904 	/* named node */
905 	sx_xlock(&mqfs->mi_lock);
906 	pn = mqfs_search(pd, pname, namelen, cnp->cn_cred);
907 	if (pn != NULL)
908 		mqnode_addref(pn);
909 	sx_xunlock(&mqfs->mi_lock);
910 
911 	/* found */
912 	if (pn != NULL) {
913 		/* DELETE */
914 		if (nameiop == DELETE && (flags & ISLASTCN)) {
915 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
916 			if (error) {
917 				mqnode_release(pn);
918 				return (error);
919 			}
920 			if (*vpp == dvp) {
921 				VREF(dvp);
922 				*vpp = dvp;
923 				mqnode_release(pn);
924 				return (0);
925 			}
926 		}
927 
928 		/* allocate vnode */
929 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
930 		mqnode_release(pn);
931 		if (error == 0 && cnp->cn_flags & MAKEENTRY)
932 			cache_enter(dvp, *vpp, cnp);
933 		return (error);
934 	}
935 
936 	/* not found */
937 
938 	/* will create a new entry in the directory ? */
939 	if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
940 	    && (flags & ISLASTCN)) {
941 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
942 		if (error)
943 			return (error);
944 		cnp->cn_flags |= SAVENAME;
945 		return (EJUSTRETURN);
946 	}
947 	return (ENOENT);
948 }
949 
950 #if 0
951 struct vop_lookup_args {
952 	struct vop_generic_args a_gen;
953 	struct vnode *a_dvp;
954 	struct vnode **a_vpp;
955 	struct componentname *a_cnp;
956 };
957 #endif
958 
959 /*
960  * vnode lookup operation
961  */
962 static int
mqfs_lookup(struct vop_cachedlookup_args * ap)963 mqfs_lookup(struct vop_cachedlookup_args *ap)
964 {
965 	int rc;
966 
967 	rc = mqfs_lookupx(ap);
968 	return (rc);
969 }
970 
971 #if 0
972 struct vop_create_args {
973 	struct vnode *a_dvp;
974 	struct vnode **a_vpp;
975 	struct componentname *a_cnp;
976 	struct vattr *a_vap;
977 };
978 #endif
979 
980 /*
981  * vnode creation operation
982  */
983 static int
mqfs_create(struct vop_create_args * ap)984 mqfs_create(struct vop_create_args *ap)
985 {
986 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
987 	struct componentname *cnp = ap->a_cnp;
988 	struct mqfs_node *pd;
989 	struct mqfs_node *pn;
990 	struct mqueue *mq;
991 	int error;
992 
993 	pd = VTON(ap->a_dvp);
994 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
995 		return (ENOTDIR);
996 	mq = mqueue_alloc(NULL);
997 	if (mq == NULL)
998 		return (EAGAIN);
999 	sx_xlock(&mqfs->mi_lock);
1000 	if ((cnp->cn_flags & HASBUF) == 0)
1001 		panic("%s: no name", __func__);
1002 	pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
1003 		cnp->cn_cred, ap->a_vap->va_mode);
1004 	if (pn == NULL) {
1005 		sx_xunlock(&mqfs->mi_lock);
1006 		error = ENOSPC;
1007 	} else {
1008 		mqnode_addref(pn);
1009 		sx_xunlock(&mqfs->mi_lock);
1010 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1011 		mqnode_release(pn);
1012 		if (error)
1013 			mqfs_destroy(pn);
1014 		else
1015 			pn->mn_data = mq;
1016 	}
1017 	if (error)
1018 		mqueue_free(mq);
1019 	return (error);
1020 }
1021 
1022 /*
1023  * Remove an entry
1024  */
1025 static
do_unlink(struct mqfs_node * pn,struct ucred * ucred)1026 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1027 {
1028 	struct mqfs_node *parent;
1029 	struct mqfs_vdata *vd;
1030 	int error = 0;
1031 
1032 	sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1033 
1034 	if (ucred->cr_uid != pn->mn_uid &&
1035 	    (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1036 		error = EACCES;
1037 	else if (!pn->mn_deleted) {
1038 		parent = pn->mn_parent;
1039 		pn->mn_parent = NULL;
1040 		pn->mn_deleted = 1;
1041 		LIST_REMOVE(pn, mn_sibling);
1042 		LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1043 			cache_purge(vd->mv_vnode);
1044 			vhold(vd->mv_vnode);
1045 			taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1046 		}
1047 		mqnode_release(pn);
1048 		mqnode_release(parent);
1049 	} else
1050 		error = ENOENT;
1051 	return (error);
1052 }
1053 
1054 #if 0
1055 struct vop_remove_args {
1056 	struct vnode *a_dvp;
1057 	struct vnode *a_vp;
1058 	struct componentname *a_cnp;
1059 };
1060 #endif
1061 
1062 /*
1063  * vnode removal operation
1064  */
1065 static int
mqfs_remove(struct vop_remove_args * ap)1066 mqfs_remove(struct vop_remove_args *ap)
1067 {
1068 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1069 	struct mqfs_node *pn;
1070 	int error;
1071 
1072 	if (ap->a_vp->v_type == VDIR)
1073                 return (EPERM);
1074 	pn = VTON(ap->a_vp);
1075 	sx_xlock(&mqfs->mi_lock);
1076 	error = do_unlink(pn, ap->a_cnp->cn_cred);
1077 	sx_xunlock(&mqfs->mi_lock);
1078 	return (error);
1079 }
1080 
1081 #if 0
1082 struct vop_inactive_args {
1083 	struct vnode *a_vp;
1084 	struct thread *a_td;
1085 };
1086 #endif
1087 
1088 static int
mqfs_inactive(struct vop_inactive_args * ap)1089 mqfs_inactive(struct vop_inactive_args *ap)
1090 {
1091 	struct mqfs_node *pn = VTON(ap->a_vp);
1092 
1093 	if (pn->mn_deleted)
1094 		vrecycle(ap->a_vp);
1095 	return (0);
1096 }
1097 
1098 #if 0
1099 struct vop_reclaim_args {
1100 	struct vop_generic_args a_gen;
1101 	struct vnode *a_vp;
1102 	struct thread *a_td;
1103 };
1104 #endif
1105 
1106 static int
mqfs_reclaim(struct vop_reclaim_args * ap)1107 mqfs_reclaim(struct vop_reclaim_args *ap)
1108 {
1109 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1110 	struct vnode *vp = ap->a_vp;
1111 	struct mqfs_node *pn;
1112 	struct mqfs_vdata *vd;
1113 
1114 	vd = vp->v_data;
1115 	pn = vd->mv_node;
1116 	sx_xlock(&mqfs->mi_lock);
1117 	vp->v_data = NULL;
1118 	LIST_REMOVE(vd, mv_link);
1119 	uma_zfree(mvdata_zone, vd);
1120 	mqnode_release(pn);
1121 	sx_xunlock(&mqfs->mi_lock);
1122 	return (0);
1123 }
1124 
1125 #if 0
1126 struct vop_open_args {
1127 	struct vop_generic_args a_gen;
1128 	struct vnode *a_vp;
1129 	int a_mode;
1130 	struct ucred *a_cred;
1131 	struct thread *a_td;
1132 	struct file *a_fp;
1133 };
1134 #endif
1135 
1136 static int
mqfs_open(struct vop_open_args * ap)1137 mqfs_open(struct vop_open_args *ap)
1138 {
1139 	return (0);
1140 }
1141 
1142 #if 0
1143 struct vop_close_args {
1144 	struct vop_generic_args a_gen;
1145 	struct vnode *a_vp;
1146 	int a_fflag;
1147 	struct ucred *a_cred;
1148 	struct thread *a_td;
1149 };
1150 #endif
1151 
1152 static int
mqfs_close(struct vop_close_args * ap)1153 mqfs_close(struct vop_close_args *ap)
1154 {
1155 	return (0);
1156 }
1157 
1158 #if 0
1159 struct vop_access_args {
1160 	struct vop_generic_args a_gen;
1161 	struct vnode *a_vp;
1162 	accmode_t a_accmode;
1163 	struct ucred *a_cred;
1164 	struct thread *a_td;
1165 };
1166 #endif
1167 
1168 /*
1169  * Verify permissions
1170  */
1171 static int
mqfs_access(struct vop_access_args * ap)1172 mqfs_access(struct vop_access_args *ap)
1173 {
1174 	struct vnode *vp = ap->a_vp;
1175 	struct vattr vattr;
1176 	int error;
1177 
1178 	error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1179 	if (error)
1180 		return (error);
1181 	error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1182 	    vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1183 	return (error);
1184 }
1185 
1186 #if 0
1187 struct vop_getattr_args {
1188 	struct vop_generic_args a_gen;
1189 	struct vnode *a_vp;
1190 	struct vattr *a_vap;
1191 	struct ucred *a_cred;
1192 };
1193 #endif
1194 
1195 /*
1196  * Get file attributes
1197  */
1198 static int
mqfs_getattr(struct vop_getattr_args * ap)1199 mqfs_getattr(struct vop_getattr_args *ap)
1200 {
1201 	struct vnode *vp = ap->a_vp;
1202 	struct mqfs_node *pn = VTON(vp);
1203 	struct vattr *vap = ap->a_vap;
1204 	int error = 0;
1205 
1206 	vap->va_type = vp->v_type;
1207 	vap->va_mode = pn->mn_mode;
1208 	vap->va_nlink = 1;
1209 	vap->va_uid = pn->mn_uid;
1210 	vap->va_gid = pn->mn_gid;
1211 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1212 	vap->va_fileid = pn->mn_fileno;
1213 	vap->va_size = 0;
1214 	vap->va_blocksize = PAGE_SIZE;
1215 	vap->va_bytes = vap->va_size = 0;
1216 	vap->va_atime = pn->mn_atime;
1217 	vap->va_mtime = pn->mn_mtime;
1218 	vap->va_ctime = pn->mn_ctime;
1219 	vap->va_birthtime = pn->mn_birth;
1220 	vap->va_gen = 0;
1221 	vap->va_flags = 0;
1222 	vap->va_rdev = NODEV;
1223 	vap->va_bytes = 0;
1224 	vap->va_filerev = 0;
1225 	return (error);
1226 }
1227 
1228 #if 0
1229 struct vop_setattr_args {
1230 	struct vop_generic_args a_gen;
1231 	struct vnode *a_vp;
1232 	struct vattr *a_vap;
1233 	struct ucred *a_cred;
1234 };
1235 #endif
1236 /*
1237  * Set attributes
1238  */
1239 static int
mqfs_setattr(struct vop_setattr_args * ap)1240 mqfs_setattr(struct vop_setattr_args *ap)
1241 {
1242 	struct mqfs_node *pn;
1243 	struct vattr *vap;
1244 	struct vnode *vp;
1245 	struct thread *td;
1246 	int c, error;
1247 	uid_t uid;
1248 	gid_t gid;
1249 
1250 	td = curthread;
1251 	vap = ap->a_vap;
1252 	vp = ap->a_vp;
1253 	if ((vap->va_type != VNON) ||
1254 	    (vap->va_nlink != VNOVAL) ||
1255 	    (vap->va_fsid != VNOVAL) ||
1256 	    (vap->va_fileid != VNOVAL) ||
1257 	    (vap->va_blocksize != VNOVAL) ||
1258 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1259 	    (vap->va_rdev != VNOVAL) ||
1260 	    ((int)vap->va_bytes != VNOVAL) ||
1261 	    (vap->va_gen != VNOVAL)) {
1262 		return (EINVAL);
1263 	}
1264 
1265 	pn = VTON(vp);
1266 
1267 	error = c = 0;
1268 	if (vap->va_uid == (uid_t)VNOVAL)
1269 		uid = pn->mn_uid;
1270 	else
1271 		uid = vap->va_uid;
1272 	if (vap->va_gid == (gid_t)VNOVAL)
1273 		gid = pn->mn_gid;
1274 	else
1275 		gid = vap->va_gid;
1276 
1277 	if (uid != pn->mn_uid || gid != pn->mn_gid) {
1278 		/*
1279 		 * To modify the ownership of a file, must possess VADMIN
1280 		 * for that file.
1281 		 */
1282 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1283 			return (error);
1284 
1285 		/*
1286 		 * XXXRW: Why is there a privilege check here: shouldn't the
1287 		 * check in VOP_ACCESS() be enough?  Also, are the group bits
1288 		 * below definitely right?
1289 		 */
1290 		if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1291 		    (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1292 		    (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1293 			return (error);
1294 		pn->mn_uid = uid;
1295 		pn->mn_gid = gid;
1296 		c = 1;
1297 	}
1298 
1299 	if (vap->va_mode != (mode_t)VNOVAL) {
1300 		if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1301 		    (error = priv_check(td, PRIV_MQ_ADMIN)))
1302 			return (error);
1303 		pn->mn_mode = vap->va_mode;
1304 		c = 1;
1305 	}
1306 
1307 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1308 		/* See the comment in ufs_vnops::ufs_setattr(). */
1309 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1310 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1311 		    (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1312 			return (error);
1313 		if (vap->va_atime.tv_sec != VNOVAL) {
1314 			pn->mn_atime = vap->va_atime;
1315 		}
1316 		if (vap->va_mtime.tv_sec != VNOVAL) {
1317 			pn->mn_mtime = vap->va_mtime;
1318 		}
1319 		c = 1;
1320 	}
1321 	if (c) {
1322 		vfs_timestamp(&pn->mn_ctime);
1323 	}
1324 	return (0);
1325 }
1326 
1327 #if 0
1328 struct vop_read_args {
1329 	struct vop_generic_args a_gen;
1330 	struct vnode *a_vp;
1331 	struct uio *a_uio;
1332 	int a_ioflag;
1333 	struct ucred *a_cred;
1334 };
1335 #endif
1336 
1337 /*
1338  * Read from a file
1339  */
1340 static int
mqfs_read(struct vop_read_args * ap)1341 mqfs_read(struct vop_read_args *ap)
1342 {
1343 	char buf[80];
1344 	struct vnode *vp = ap->a_vp;
1345 	struct uio *uio = ap->a_uio;
1346 	struct mqueue *mq;
1347 	int len, error;
1348 
1349 	if (vp->v_type != VREG)
1350 		return (EINVAL);
1351 
1352 	mq = VTOMQ(vp);
1353 	snprintf(buf, sizeof(buf),
1354 		"QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1355 		mq->mq_totalbytes,
1356 		mq->mq_maxmsg,
1357 		mq->mq_curmsgs,
1358 		mq->mq_msgsize);
1359 	buf[sizeof(buf)-1] = '\0';
1360 	len = strlen(buf);
1361 	error = uiomove_frombuf(buf, len, uio);
1362 	return (error);
1363 }
1364 
1365 #if 0
1366 struct vop_readdir_args {
1367 	struct vop_generic_args a_gen;
1368 	struct vnode *a_vp;
1369 	struct uio *a_uio;
1370 	struct ucred *a_cred;
1371 	int *a_eofflag;
1372 	int *a_ncookies;
1373 	u_long **a_cookies;
1374 };
1375 #endif
1376 
1377 /*
1378  * Return directory entries.
1379  */
1380 static int
mqfs_readdir(struct vop_readdir_args * ap)1381 mqfs_readdir(struct vop_readdir_args *ap)
1382 {
1383 	struct vnode *vp;
1384 	struct mqfs_info *mi;
1385 	struct mqfs_node *pd;
1386 	struct mqfs_node *pn;
1387 	struct dirent entry;
1388 	struct uio *uio;
1389 	const void *pr_root;
1390 	int *tmp_ncookies = NULL;
1391 	off_t offset;
1392 	int error, i;
1393 
1394 	vp = ap->a_vp;
1395 	mi = VFSTOMQFS(vp->v_mount);
1396 	pd = VTON(vp);
1397 	uio = ap->a_uio;
1398 
1399 	if (vp->v_type != VDIR)
1400 		return (ENOTDIR);
1401 
1402 	if (uio->uio_offset < 0)
1403 		return (EINVAL);
1404 
1405 	if (ap->a_ncookies != NULL) {
1406 		tmp_ncookies = ap->a_ncookies;
1407 		*ap->a_ncookies = 0;
1408 		ap->a_ncookies = NULL;
1409         }
1410 
1411 	error = 0;
1412 	offset = 0;
1413 
1414 	pr_root = ap->a_cred->cr_prison->pr_root;
1415 	sx_xlock(&mi->mi_lock);
1416 
1417 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1418 		entry.d_reclen = sizeof(entry);
1419 
1420 		/*
1421 		 * Only show names within the same prison root directory
1422 		 * (or not associated with a prison, e.g. "." and "..").
1423 		 */
1424 		if (pn->mn_pr_root != NULL && pn->mn_pr_root != pr_root)
1425 			continue;
1426 		if (!pn->mn_fileno)
1427 			mqfs_fileno_alloc(mi, pn);
1428 		entry.d_fileno = pn->mn_fileno;
1429 		entry.d_off = offset + entry.d_reclen;
1430 		for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1431 			entry.d_name[i] = pn->mn_name[i];
1432 		entry.d_namlen = i;
1433 		switch (pn->mn_type) {
1434 		case mqfstype_root:
1435 		case mqfstype_dir:
1436 		case mqfstype_this:
1437 		case mqfstype_parent:
1438 			entry.d_type = DT_DIR;
1439 			break;
1440 		case mqfstype_file:
1441 			entry.d_type = DT_REG;
1442 			break;
1443 		case mqfstype_symlink:
1444 			entry.d_type = DT_LNK;
1445 			break;
1446 		default:
1447 			panic("%s has unexpected node type: %d", pn->mn_name,
1448 				pn->mn_type);
1449 		}
1450 		dirent_terminate(&entry);
1451 		if (entry.d_reclen > uio->uio_resid)
1452                         break;
1453 		if (offset >= uio->uio_offset) {
1454 			error = vfs_read_dirent(ap, &entry, offset);
1455                         if (error)
1456                                 break;
1457                 }
1458                 offset += entry.d_reclen;
1459 	}
1460 	sx_xunlock(&mi->mi_lock);
1461 
1462 	uio->uio_offset = offset;
1463 
1464 	if (tmp_ncookies != NULL)
1465 		ap->a_ncookies = tmp_ncookies;
1466 
1467 	return (error);
1468 }
1469 
1470 #ifdef notyet
1471 
1472 #if 0
1473 struct vop_mkdir_args {
1474 	struct vnode *a_dvp;
1475 	struvt vnode **a_vpp;
1476 	struvt componentname *a_cnp;
1477 	struct vattr *a_vap;
1478 };
1479 #endif
1480 
1481 /*
1482  * Create a directory.
1483  */
1484 static int
mqfs_mkdir(struct vop_mkdir_args * ap)1485 mqfs_mkdir(struct vop_mkdir_args *ap)
1486 {
1487 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1488 	struct componentname *cnp = ap->a_cnp;
1489 	struct mqfs_node *pd = VTON(ap->a_dvp);
1490 	struct mqfs_node *pn;
1491 	int error;
1492 
1493 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1494 		return (ENOTDIR);
1495 	sx_xlock(&mqfs->mi_lock);
1496 	if ((cnp->cn_flags & HASBUF) == 0)
1497 		panic("%s: no name", __func__);
1498 	pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1499 		ap->a_vap->cn_cred, ap->a_vap->va_mode);
1500 	if (pn != NULL)
1501 		mqnode_addref(pn);
1502 	sx_xunlock(&mqfs->mi_lock);
1503 	if (pn == NULL) {
1504 		error = ENOSPC;
1505 	} else {
1506 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1507 		mqnode_release(pn);
1508 	}
1509 	return (error);
1510 }
1511 
1512 #if 0
1513 struct vop_rmdir_args {
1514 	struct vnode *a_dvp;
1515 	struct vnode *a_vp;
1516 	struct componentname *a_cnp;
1517 };
1518 #endif
1519 
1520 /*
1521  * Remove a directory.
1522  */
1523 static int
mqfs_rmdir(struct vop_rmdir_args * ap)1524 mqfs_rmdir(struct vop_rmdir_args *ap)
1525 {
1526 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1527 	struct mqfs_node *pn = VTON(ap->a_vp);
1528 	struct mqfs_node *pt;
1529 
1530 	if (pn->mn_type != mqfstype_dir)
1531 		return (ENOTDIR);
1532 
1533 	sx_xlock(&mqfs->mi_lock);
1534 	if (pn->mn_deleted) {
1535 		sx_xunlock(&mqfs->mi_lock);
1536 		return (ENOENT);
1537 	}
1538 
1539 	pt = LIST_FIRST(&pn->mn_children);
1540 	pt = LIST_NEXT(pt, mn_sibling);
1541 	pt = LIST_NEXT(pt, mn_sibling);
1542 	if (pt != NULL) {
1543 		sx_xunlock(&mqfs->mi_lock);
1544 		return (ENOTEMPTY);
1545 	}
1546 	pt = pn->mn_parent;
1547 	pn->mn_parent = NULL;
1548 	pn->mn_deleted = 1;
1549 	LIST_REMOVE(pn, mn_sibling);
1550 	mqnode_release(pn);
1551 	mqnode_release(pt);
1552 	sx_xunlock(&mqfs->mi_lock);
1553 	cache_purge(ap->a_vp);
1554 	return (0);
1555 }
1556 
1557 #endif /* notyet */
1558 
1559 /*
1560  * See if this prison root is obsolete, and clean up associated queues if it is.
1561  */
1562 static int
mqfs_prison_remove(void * obj,void * data __unused)1563 mqfs_prison_remove(void *obj, void *data __unused)
1564 {
1565 	const struct prison *pr = obj;
1566 	const struct prison *tpr;
1567 	struct mqfs_node *pn, *tpn;
1568 	int found;
1569 
1570 	found = 0;
1571 	TAILQ_FOREACH(tpr, &allprison, pr_list) {
1572 		if (tpr->pr_root == pr->pr_root && tpr != pr && tpr->pr_ref > 0)
1573 			found = 1;
1574 	}
1575 	if (!found) {
1576 		/*
1577 		 * No jails are rooted in this directory anymore,
1578 		 * so no queues should be either.
1579 		 */
1580 		sx_xlock(&mqfs_data.mi_lock);
1581 		LIST_FOREACH_SAFE(pn, &mqfs_data.mi_root->mn_children,
1582 		    mn_sibling, tpn) {
1583 			if (pn->mn_pr_root == pr->pr_root)
1584 				(void)do_unlink(pn, curthread->td_ucred);
1585 		}
1586 		sx_xunlock(&mqfs_data.mi_lock);
1587 	}
1588 	return (0);
1589 }
1590 
1591 /*
1592  * Allocate a message queue
1593  */
1594 static struct mqueue *
mqueue_alloc(const struct mq_attr * attr)1595 mqueue_alloc(const struct mq_attr *attr)
1596 {
1597 	struct mqueue *mq;
1598 
1599 	if (curmq >= maxmq)
1600 		return (NULL);
1601 	mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1602 	TAILQ_INIT(&mq->mq_msgq);
1603 	if (attr != NULL) {
1604 		mq->mq_maxmsg = attr->mq_maxmsg;
1605 		mq->mq_msgsize = attr->mq_msgsize;
1606 	} else {
1607 		mq->mq_maxmsg = default_maxmsg;
1608 		mq->mq_msgsize = default_msgsize;
1609 	}
1610 	mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1611 	knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1612 	knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1613 	atomic_add_int(&curmq, 1);
1614 	return (mq);
1615 }
1616 
1617 /*
1618  * Destroy a message queue
1619  */
1620 static void
mqueue_free(struct mqueue * mq)1621 mqueue_free(struct mqueue *mq)
1622 {
1623 	struct mqueue_msg *msg;
1624 
1625 	while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1626 		TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1627 		free(msg, M_MQUEUEDATA);
1628 	}
1629 
1630 	mtx_destroy(&mq->mq_mutex);
1631 	seldrain(&mq->mq_rsel);
1632 	seldrain(&mq->mq_wsel);
1633 	knlist_destroy(&mq->mq_rsel.si_note);
1634 	knlist_destroy(&mq->mq_wsel.si_note);
1635 	uma_zfree(mqueue_zone, mq);
1636 	atomic_add_int(&curmq, -1);
1637 }
1638 
1639 /*
1640  * Load a message from user space
1641  */
1642 static struct mqueue_msg *
mqueue_loadmsg(const char * msg_ptr,size_t msg_size,int msg_prio)1643 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1644 {
1645 	struct mqueue_msg *msg;
1646 	size_t len;
1647 	int error;
1648 
1649 	len = sizeof(struct mqueue_msg) + msg_size;
1650 	msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1651 	error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1652 	    msg_size);
1653 	if (error) {
1654 		free(msg, M_MQUEUEDATA);
1655 		msg = NULL;
1656 	} else {
1657 		msg->msg_size = msg_size;
1658 		msg->msg_prio = msg_prio;
1659 	}
1660 	return (msg);
1661 }
1662 
1663 /*
1664  * Save a message to user space
1665  */
1666 static int
mqueue_savemsg(struct mqueue_msg * msg,char * msg_ptr,int * msg_prio)1667 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1668 {
1669 	int error;
1670 
1671 	error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1672 		msg->msg_size);
1673 	if (error == 0 && msg_prio != NULL)
1674 		error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1675 	return (error);
1676 }
1677 
1678 /*
1679  * Free a message's memory
1680  */
1681 static __inline void
mqueue_freemsg(struct mqueue_msg * msg)1682 mqueue_freemsg(struct mqueue_msg *msg)
1683 {
1684 	free(msg, M_MQUEUEDATA);
1685 }
1686 
1687 /*
1688  * Send a message. if waitok is false, thread will not be
1689  * blocked if there is no data in queue, otherwise, absolute
1690  * time will be checked.
1691  */
1692 int
mqueue_send(struct mqueue * mq,const char * msg_ptr,size_t msg_len,unsigned msg_prio,int waitok,const struct timespec * abs_timeout)1693 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1694 	size_t msg_len, unsigned msg_prio, int waitok,
1695 	const struct timespec *abs_timeout)
1696 {
1697 	struct mqueue_msg *msg;
1698 	struct timespec ts, ts2;
1699 	struct timeval tv;
1700 	int error;
1701 
1702 	if (msg_prio >= MQ_PRIO_MAX)
1703 		return (EINVAL);
1704 	if (msg_len > mq->mq_msgsize)
1705 		return (EMSGSIZE);
1706 	msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1707 	if (msg == NULL)
1708 		return (EFAULT);
1709 
1710 	/* O_NONBLOCK case */
1711 	if (!waitok) {
1712 		error = _mqueue_send(mq, msg, -1);
1713 		if (error)
1714 			goto bad;
1715 		return (0);
1716 	}
1717 
1718 	/* we allow a null timeout (wait forever) */
1719 	if (abs_timeout == NULL) {
1720 		error = _mqueue_send(mq, msg, 0);
1721 		if (error)
1722 			goto bad;
1723 		return (0);
1724 	}
1725 
1726 	/* send it before checking time */
1727 	error = _mqueue_send(mq, msg, -1);
1728 	if (error == 0)
1729 		return (0);
1730 
1731 	if (error != EAGAIN)
1732 		goto bad;
1733 
1734 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1735 		error = EINVAL;
1736 		goto bad;
1737 	}
1738 	for (;;) {
1739 		getnanotime(&ts);
1740 		timespecsub(abs_timeout, &ts, &ts2);
1741 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1742 			error = ETIMEDOUT;
1743 			break;
1744 		}
1745 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1746 		error = _mqueue_send(mq, msg, tvtohz(&tv));
1747 		if (error != ETIMEDOUT)
1748 			break;
1749 	}
1750 	if (error == 0)
1751 		return (0);
1752 bad:
1753 	mqueue_freemsg(msg);
1754 	return (error);
1755 }
1756 
1757 /*
1758  * Common routine to send a message
1759  */
1760 static int
_mqueue_send(struct mqueue * mq,struct mqueue_msg * msg,int timo)1761 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1762 {
1763 	struct mqueue_msg *msg2;
1764 	int error = 0;
1765 
1766 	mtx_lock(&mq->mq_mutex);
1767 	while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1768 		if (timo < 0) {
1769 			mtx_unlock(&mq->mq_mutex);
1770 			return (EAGAIN);
1771 		}
1772 		mq->mq_senders++;
1773 		error = msleep(&mq->mq_senders, &mq->mq_mutex,
1774 			    PCATCH, "mqsend", timo);
1775 		mq->mq_senders--;
1776 		if (error == EAGAIN)
1777 			error = ETIMEDOUT;
1778 	}
1779 	if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1780 		mtx_unlock(&mq->mq_mutex);
1781 		return (error);
1782 	}
1783 	error = 0;
1784 	if (TAILQ_EMPTY(&mq->mq_msgq)) {
1785 		TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1786 	} else {
1787 		if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1788 			TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1789 		} else {
1790 			TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1791 				if (msg2->msg_prio < msg->msg_prio)
1792 					break;
1793 			}
1794 			TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1795 		}
1796 	}
1797 	mq->mq_curmsgs++;
1798 	mq->mq_totalbytes += msg->msg_size;
1799 	if (mq->mq_receivers)
1800 		wakeup_one(&mq->mq_receivers);
1801 	else if (mq->mq_notifier != NULL)
1802 		mqueue_send_notification(mq);
1803 	if (mq->mq_flags & MQ_RSEL) {
1804 		mq->mq_flags &= ~MQ_RSEL;
1805 		selwakeup(&mq->mq_rsel);
1806 	}
1807 	KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1808 	mtx_unlock(&mq->mq_mutex);
1809 	return (0);
1810 }
1811 
1812 /*
1813  * Send realtime a signal to process which registered itself
1814  * successfully by mq_notify.
1815  */
1816 static void
mqueue_send_notification(struct mqueue * mq)1817 mqueue_send_notification(struct mqueue *mq)
1818 {
1819 	struct mqueue_notifier *nt;
1820 	struct thread *td;
1821 	struct proc *p;
1822 	int error;
1823 
1824 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1825 	nt = mq->mq_notifier;
1826 	if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1827 		p = nt->nt_proc;
1828 		error = sigev_findtd(p, &nt->nt_sigev, &td);
1829 		if (error) {
1830 			mq->mq_notifier = NULL;
1831 			return;
1832 		}
1833 		if (!KSI_ONQ(&nt->nt_ksi)) {
1834 			ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1835 			tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1836 		}
1837 		PROC_UNLOCK(p);
1838 	}
1839 	mq->mq_notifier = NULL;
1840 }
1841 
1842 /*
1843  * Get a message. if waitok is false, thread will not be
1844  * blocked if there is no data in queue, otherwise, absolute
1845  * time will be checked.
1846  */
1847 int
mqueue_receive(struct mqueue * mq,char * msg_ptr,size_t msg_len,unsigned * msg_prio,int waitok,const struct timespec * abs_timeout)1848 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1849 	size_t msg_len, unsigned *msg_prio, int waitok,
1850 	const struct timespec *abs_timeout)
1851 {
1852 	struct mqueue_msg *msg;
1853 	struct timespec ts, ts2;
1854 	struct timeval tv;
1855 	int error;
1856 
1857 	if (msg_len < mq->mq_msgsize)
1858 		return (EMSGSIZE);
1859 
1860 	/* O_NONBLOCK case */
1861 	if (!waitok) {
1862 		error = _mqueue_recv(mq, &msg, -1);
1863 		if (error)
1864 			return (error);
1865 		goto received;
1866 	}
1867 
1868 	/* we allow a null timeout (wait forever). */
1869 	if (abs_timeout == NULL) {
1870 		error = _mqueue_recv(mq, &msg, 0);
1871 		if (error)
1872 			return (error);
1873 		goto received;
1874 	}
1875 
1876 	/* try to get a message before checking time */
1877 	error = _mqueue_recv(mq, &msg, -1);
1878 	if (error == 0)
1879 		goto received;
1880 
1881 	if (error != EAGAIN)
1882 		return (error);
1883 
1884 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1885 		error = EINVAL;
1886 		return (error);
1887 	}
1888 
1889 	for (;;) {
1890 		getnanotime(&ts);
1891 		timespecsub(abs_timeout, &ts, &ts2);
1892 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1893 			error = ETIMEDOUT;
1894 			return (error);
1895 		}
1896 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1897 		error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1898 		if (error == 0)
1899 			break;
1900 		if (error != ETIMEDOUT)
1901 			return (error);
1902 	}
1903 
1904 received:
1905 	error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1906 	if (error == 0) {
1907 		curthread->td_retval[0] = msg->msg_size;
1908 		curthread->td_retval[1] = 0;
1909 	}
1910 	mqueue_freemsg(msg);
1911 	return (error);
1912 }
1913 
1914 /*
1915  * Common routine to receive a message
1916  */
1917 static int
_mqueue_recv(struct mqueue * mq,struct mqueue_msg ** msg,int timo)1918 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1919 {
1920 	int error = 0;
1921 
1922 	mtx_lock(&mq->mq_mutex);
1923 	while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1924 		if (timo < 0) {
1925 			mtx_unlock(&mq->mq_mutex);
1926 			return (EAGAIN);
1927 		}
1928 		mq->mq_receivers++;
1929 		error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1930 			    PCATCH, "mqrecv", timo);
1931 		mq->mq_receivers--;
1932 		if (error == EAGAIN)
1933 			error = ETIMEDOUT;
1934 	}
1935 	if (*msg != NULL) {
1936 		error = 0;
1937 		TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1938 		mq->mq_curmsgs--;
1939 		mq->mq_totalbytes -= (*msg)->msg_size;
1940 		if (mq->mq_senders)
1941 			wakeup_one(&mq->mq_senders);
1942 		if (mq->mq_flags & MQ_WSEL) {
1943 			mq->mq_flags &= ~MQ_WSEL;
1944 			selwakeup(&mq->mq_wsel);
1945 		}
1946 		KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1947 	}
1948 	if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1949 	    !TAILQ_EMPTY(&mq->mq_msgq)) {
1950 		mqueue_send_notification(mq);
1951 	}
1952 	mtx_unlock(&mq->mq_mutex);
1953 	return (error);
1954 }
1955 
1956 static __inline struct mqueue_notifier *
notifier_alloc(void)1957 notifier_alloc(void)
1958 {
1959 	return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1960 }
1961 
1962 static __inline void
notifier_free(struct mqueue_notifier * p)1963 notifier_free(struct mqueue_notifier *p)
1964 {
1965 	uma_zfree(mqnoti_zone, p);
1966 }
1967 
1968 static struct mqueue_notifier *
notifier_search(struct proc * p,int fd)1969 notifier_search(struct proc *p, int fd)
1970 {
1971 	struct mqueue_notifier *nt;
1972 
1973 	LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1974 		if (nt->nt_ksi.ksi_mqd == fd)
1975 			break;
1976 	}
1977 	return (nt);
1978 }
1979 
1980 static __inline void
notifier_insert(struct proc * p,struct mqueue_notifier * nt)1981 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1982 {
1983 	LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1984 }
1985 
1986 static __inline void
notifier_delete(struct proc * p,struct mqueue_notifier * nt)1987 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1988 {
1989 	LIST_REMOVE(nt, nt_link);
1990 	notifier_free(nt);
1991 }
1992 
1993 static void
notifier_remove(struct proc * p,struct mqueue * mq,int fd)1994 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1995 {
1996 	struct mqueue_notifier *nt;
1997 
1998 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1999 	PROC_LOCK(p);
2000 	nt = notifier_search(p, fd);
2001 	if (nt != NULL) {
2002 		if (mq->mq_notifier == nt)
2003 			mq->mq_notifier = NULL;
2004 		sigqueue_take(&nt->nt_ksi);
2005 		notifier_delete(p, nt);
2006 	}
2007 	PROC_UNLOCK(p);
2008 }
2009 
2010 static int
kern_kmq_open(struct thread * td,const char * upath,int flags,mode_t mode,const struct mq_attr * attr)2011 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
2012     const struct mq_attr *attr)
2013 {
2014 	char path[MQFS_NAMELEN + 1];
2015 	struct mqfs_node *pn;
2016 	struct filedesc *fdp;
2017 	struct file *fp;
2018 	struct mqueue *mq;
2019 	int fd, error, len, cmode;
2020 
2021 	AUDIT_ARG_FFLAGS(flags);
2022 	AUDIT_ARG_MODE(mode);
2023 
2024 	fdp = td->td_proc->p_fd;
2025 	cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
2026 	mq = NULL;
2027 	if ((flags & O_CREAT) != 0 && attr != NULL) {
2028 		if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
2029 			return (EINVAL);
2030 		if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
2031 			return (EINVAL);
2032 	}
2033 
2034 	error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
2035         if (error)
2036 		return (error);
2037 
2038 	/*
2039 	 * The first character of name must be a slash  (/) character
2040 	 * and the remaining characters of name cannot include any slash
2041 	 * characters.
2042 	 */
2043 	len = strlen(path);
2044 	if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2045 		return (EINVAL);
2046 	AUDIT_ARG_UPATH1_CANON(path);
2047 
2048 	error = falloc(td, &fp, &fd, O_CLOEXEC);
2049 	if (error)
2050 		return (error);
2051 
2052 	sx_xlock(&mqfs_data.mi_lock);
2053 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred);
2054 	if (pn == NULL) {
2055 		if (!(flags & O_CREAT)) {
2056 			error = ENOENT;
2057 		} else {
2058 			mq = mqueue_alloc(attr);
2059 			if (mq == NULL) {
2060 				error = ENFILE;
2061 			} else {
2062 				pn = mqfs_create_file(mqfs_data.mi_root,
2063 				         path + 1, len - 1, td->td_ucred,
2064 					 cmode);
2065 				if (pn == NULL) {
2066 					error = ENOSPC;
2067 					mqueue_free(mq);
2068 				}
2069 			}
2070 		}
2071 
2072 		if (error == 0) {
2073 			pn->mn_data = mq;
2074 		}
2075 	} else {
2076 		if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2077 			error = EEXIST;
2078 		} else {
2079 			accmode_t accmode = 0;
2080 
2081 			if (flags & FREAD)
2082 				accmode |= VREAD;
2083 			if (flags & FWRITE)
2084 				accmode |= VWRITE;
2085 			error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2086 				    pn->mn_gid, accmode, td->td_ucred, NULL);
2087 		}
2088 	}
2089 
2090 	if (error) {
2091 		sx_xunlock(&mqfs_data.mi_lock);
2092 		fdclose(td, fp, fd);
2093 		fdrop(fp, td);
2094 		return (error);
2095 	}
2096 
2097 	mqnode_addref(pn);
2098 	sx_xunlock(&mqfs_data.mi_lock);
2099 
2100 	finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2101 	    &mqueueops);
2102 
2103 	td->td_retval[0] = fd;
2104 	fdrop(fp, td);
2105 	return (0);
2106 }
2107 
2108 /*
2109  * Syscall to open a message queue.
2110  */
2111 int
sys_kmq_open(struct thread * td,struct kmq_open_args * uap)2112 sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
2113 {
2114 	struct mq_attr attr;
2115 	int flags, error;
2116 
2117 	if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2118 		return (EINVAL);
2119 	flags = FFLAGS(uap->flags);
2120 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2121 		error = copyin(uap->attr, &attr, sizeof(attr));
2122 		if (error)
2123 			return (error);
2124 	}
2125 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2126 	    uap->attr != NULL ? &attr : NULL));
2127 }
2128 
2129 /*
2130  * Syscall to unlink a message queue.
2131  */
2132 int
sys_kmq_unlink(struct thread * td,struct kmq_unlink_args * uap)2133 sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2134 {
2135 	char path[MQFS_NAMELEN+1];
2136 	struct mqfs_node *pn;
2137 	int error, len;
2138 
2139 	error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2140         if (error)
2141 		return (error);
2142 
2143 	len = strlen(path);
2144 	if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2145 		return (EINVAL);
2146 	AUDIT_ARG_UPATH1_CANON(path);
2147 
2148 	sx_xlock(&mqfs_data.mi_lock);
2149 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred);
2150 	if (pn != NULL)
2151 		error = do_unlink(pn, td->td_ucred);
2152 	else
2153 		error = ENOENT;
2154 	sx_xunlock(&mqfs_data.mi_lock);
2155 	return (error);
2156 }
2157 
2158 typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **);
2159 
2160 /*
2161  * Get message queue by giving file slot
2162  */
2163 static int
_getmq(struct thread * td,int fd,cap_rights_t * rightsp,_fgetf func,struct file ** fpp,struct mqfs_node ** ppn,struct mqueue ** pmq)2164 _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func,
2165        struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2166 {
2167 	struct mqfs_node *pn;
2168 	int error;
2169 
2170 	error = func(td, fd, rightsp, fpp);
2171 	if (error)
2172 		return (error);
2173 	if (&mqueueops != (*fpp)->f_ops) {
2174 		fdrop(*fpp, td);
2175 		return (EBADF);
2176 	}
2177 	pn = (*fpp)->f_data;
2178 	if (ppn)
2179 		*ppn = pn;
2180 	if (pmq)
2181 		*pmq = pn->mn_data;
2182 	return (0);
2183 }
2184 
2185 static __inline int
getmq(struct thread * td,int fd,struct file ** fpp,struct mqfs_node ** ppn,struct mqueue ** pmq)2186 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2187 	struct mqueue **pmq)
2188 {
2189 
2190 	return _getmq(td, fd, &cap_event_rights, fget,
2191 	    fpp, ppn, pmq);
2192 }
2193 
2194 static __inline int
getmq_read(struct thread * td,int fd,struct file ** fpp,struct mqfs_node ** ppn,struct mqueue ** pmq)2195 getmq_read(struct thread *td, int fd, struct file **fpp,
2196 	 struct mqfs_node **ppn, struct mqueue **pmq)
2197 {
2198 
2199 	return _getmq(td, fd, &cap_read_rights, fget_read,
2200 	    fpp, ppn, pmq);
2201 }
2202 
2203 static __inline int
getmq_write(struct thread * td,int fd,struct file ** fpp,struct mqfs_node ** ppn,struct mqueue ** pmq)2204 getmq_write(struct thread *td, int fd, struct file **fpp,
2205 	struct mqfs_node **ppn, struct mqueue **pmq)
2206 {
2207 
2208 	return _getmq(td, fd, &cap_write_rights, fget_write,
2209 	    fpp, ppn, pmq);
2210 }
2211 
2212 static int
kern_kmq_setattr(struct thread * td,int mqd,const struct mq_attr * attr,struct mq_attr * oattr)2213 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2214     struct mq_attr *oattr)
2215 {
2216 	struct mqueue *mq;
2217 	struct file *fp;
2218 	u_int oflag, flag;
2219 	int error;
2220 
2221 	AUDIT_ARG_FD(mqd);
2222 	if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2223 		return (EINVAL);
2224 	error = getmq(td, mqd, &fp, NULL, &mq);
2225 	if (error)
2226 		return (error);
2227 	oattr->mq_maxmsg  = mq->mq_maxmsg;
2228 	oattr->mq_msgsize = mq->mq_msgsize;
2229 	oattr->mq_curmsgs = mq->mq_curmsgs;
2230 	if (attr != NULL) {
2231 		do {
2232 			oflag = flag = fp->f_flag;
2233 			flag &= ~O_NONBLOCK;
2234 			flag |= (attr->mq_flags & O_NONBLOCK);
2235 		} while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2236 	} else
2237 		oflag = fp->f_flag;
2238 	oattr->mq_flags = (O_NONBLOCK & oflag);
2239 	fdrop(fp, td);
2240 	return (error);
2241 }
2242 
2243 int
sys_kmq_setattr(struct thread * td,struct kmq_setattr_args * uap)2244 sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2245 {
2246 	struct mq_attr attr, oattr;
2247 	int error;
2248 
2249 	if (uap->attr != NULL) {
2250 		error = copyin(uap->attr, &attr, sizeof(attr));
2251 		if (error != 0)
2252 			return (error);
2253 	}
2254 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2255 	    &oattr);
2256 	if (error == 0 && uap->oattr != NULL) {
2257 		bzero(oattr.__reserved, sizeof(oattr.__reserved));
2258 		error = copyout(&oattr, uap->oattr, sizeof(oattr));
2259 	}
2260 	return (error);
2261 }
2262 
2263 int
sys_kmq_timedreceive(struct thread * td,struct kmq_timedreceive_args * uap)2264 sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2265 {
2266 	struct mqueue *mq;
2267 	struct file *fp;
2268 	struct timespec *abs_timeout, ets;
2269 	int error;
2270 	int waitok;
2271 
2272 	AUDIT_ARG_FD(uap->mqd);
2273 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2274 	if (error)
2275 		return (error);
2276 	if (uap->abs_timeout != NULL) {
2277 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2278 		if (error != 0)
2279 			goto out;
2280 		abs_timeout = &ets;
2281 	} else
2282 		abs_timeout = NULL;
2283 	waitok = !(fp->f_flag & O_NONBLOCK);
2284 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2285 		uap->msg_prio, waitok, abs_timeout);
2286 out:
2287 	fdrop(fp, td);
2288 	return (error);
2289 }
2290 
2291 int
sys_kmq_timedsend(struct thread * td,struct kmq_timedsend_args * uap)2292 sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2293 {
2294 	struct mqueue *mq;
2295 	struct file *fp;
2296 	struct timespec *abs_timeout, ets;
2297 	int error, waitok;
2298 
2299 	AUDIT_ARG_FD(uap->mqd);
2300 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2301 	if (error)
2302 		return (error);
2303 	if (uap->abs_timeout != NULL) {
2304 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2305 		if (error != 0)
2306 			goto out;
2307 		abs_timeout = &ets;
2308 	} else
2309 		abs_timeout = NULL;
2310 	waitok = !(fp->f_flag & O_NONBLOCK);
2311 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2312 		uap->msg_prio, waitok, abs_timeout);
2313 out:
2314 	fdrop(fp, td);
2315 	return (error);
2316 }
2317 
2318 static int
kern_kmq_notify(struct thread * td,int mqd,struct sigevent * sigev)2319 kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
2320 {
2321 	struct filedesc *fdp;
2322 	struct proc *p;
2323 	struct mqueue *mq;
2324 	struct file *fp, *fp2;
2325 	struct mqueue_notifier *nt, *newnt = NULL;
2326 	int error;
2327 
2328 	AUDIT_ARG_FD(mqd);
2329 	if (sigev != NULL) {
2330 		if (sigev->sigev_notify != SIGEV_SIGNAL &&
2331 		    sigev->sigev_notify != SIGEV_THREAD_ID &&
2332 		    sigev->sigev_notify != SIGEV_NONE)
2333 			return (EINVAL);
2334 		if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2335 		    sigev->sigev_notify == SIGEV_THREAD_ID) &&
2336 		    !_SIG_VALID(sigev->sigev_signo))
2337 			return (EINVAL);
2338 	}
2339 	p = td->td_proc;
2340 	fdp = td->td_proc->p_fd;
2341 	error = getmq(td, mqd, &fp, NULL, &mq);
2342 	if (error)
2343 		return (error);
2344 again:
2345 	FILEDESC_SLOCK(fdp);
2346 	fp2 = fget_locked(fdp, mqd);
2347 	if (fp2 == NULL) {
2348 		FILEDESC_SUNLOCK(fdp);
2349 		error = EBADF;
2350 		goto out;
2351 	}
2352 #ifdef CAPABILITIES
2353 	error = cap_check(cap_rights(fdp, mqd), &cap_event_rights);
2354 	if (error) {
2355 		FILEDESC_SUNLOCK(fdp);
2356 		goto out;
2357 	}
2358 #endif
2359 	if (fp2 != fp) {
2360 		FILEDESC_SUNLOCK(fdp);
2361 		error = EBADF;
2362 		goto out;
2363 	}
2364 	mtx_lock(&mq->mq_mutex);
2365 	FILEDESC_SUNLOCK(fdp);
2366 	if (sigev != NULL) {
2367 		if (mq->mq_notifier != NULL) {
2368 			error = EBUSY;
2369 		} else {
2370 			PROC_LOCK(p);
2371 			nt = notifier_search(p, mqd);
2372 			if (nt == NULL) {
2373 				if (newnt == NULL) {
2374 					PROC_UNLOCK(p);
2375 					mtx_unlock(&mq->mq_mutex);
2376 					newnt = notifier_alloc();
2377 					goto again;
2378 				}
2379 			}
2380 
2381 			if (nt != NULL) {
2382 				sigqueue_take(&nt->nt_ksi);
2383 				if (newnt != NULL) {
2384 					notifier_free(newnt);
2385 					newnt = NULL;
2386 				}
2387 			} else {
2388 				nt = newnt;
2389 				newnt = NULL;
2390 				ksiginfo_init(&nt->nt_ksi);
2391 				nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2392 				nt->nt_ksi.ksi_code = SI_MESGQ;
2393 				nt->nt_proc = p;
2394 				nt->nt_ksi.ksi_mqd = mqd;
2395 				notifier_insert(p, nt);
2396 			}
2397 			nt->nt_sigev = *sigev;
2398 			mq->mq_notifier = nt;
2399 			PROC_UNLOCK(p);
2400 			/*
2401 			 * if there is no receivers and message queue
2402 			 * is not empty, we should send notification
2403 			 * as soon as possible.
2404 			 */
2405 			if (mq->mq_receivers == 0 &&
2406 			    !TAILQ_EMPTY(&mq->mq_msgq))
2407 				mqueue_send_notification(mq);
2408 		}
2409 	} else {
2410 		notifier_remove(p, mq, mqd);
2411 	}
2412 	mtx_unlock(&mq->mq_mutex);
2413 
2414 out:
2415 	fdrop(fp, td);
2416 	if (newnt != NULL)
2417 		notifier_free(newnt);
2418 	return (error);
2419 }
2420 
2421 int
sys_kmq_notify(struct thread * td,struct kmq_notify_args * uap)2422 sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2423 {
2424 	struct sigevent ev, *evp;
2425 	int error;
2426 
2427 	if (uap->sigev == NULL) {
2428 		evp = NULL;
2429 	} else {
2430 		error = copyin(uap->sigev, &ev, sizeof(ev));
2431 		if (error != 0)
2432 			return (error);
2433 		evp = &ev;
2434 	}
2435 	return (kern_kmq_notify(td, uap->mqd, evp));
2436 }
2437 
2438 static void
mqueue_fdclose(struct thread * td,int fd,struct file * fp)2439 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2440 {
2441 	struct mqueue *mq;
2442 #ifdef INVARIANTS
2443 	struct filedesc *fdp;
2444 
2445 	fdp = td->td_proc->p_fd;
2446 	FILEDESC_LOCK_ASSERT(fdp);
2447 #endif
2448 
2449 	if (fp->f_ops == &mqueueops) {
2450 		mq = FPTOMQ(fp);
2451 		mtx_lock(&mq->mq_mutex);
2452 		notifier_remove(td->td_proc, mq, fd);
2453 
2454 		/* have to wakeup thread in same process */
2455 		if (mq->mq_flags & MQ_RSEL) {
2456 			mq->mq_flags &= ~MQ_RSEL;
2457 			selwakeup(&mq->mq_rsel);
2458 		}
2459 		if (mq->mq_flags & MQ_WSEL) {
2460 			mq->mq_flags &= ~MQ_WSEL;
2461 			selwakeup(&mq->mq_wsel);
2462 		}
2463 		mtx_unlock(&mq->mq_mutex);
2464 	}
2465 }
2466 
2467 static void
mq_proc_exit(void * arg __unused,struct proc * p)2468 mq_proc_exit(void *arg __unused, struct proc *p)
2469 {
2470 	struct filedesc *fdp;
2471 	struct file *fp;
2472 	struct mqueue *mq;
2473 	int i;
2474 
2475 	fdp = p->p_fd;
2476 	FILEDESC_SLOCK(fdp);
2477 	for (i = 0; i < fdp->fd_nfiles; ++i) {
2478 		fp = fget_locked(fdp, i);
2479 		if (fp != NULL && fp->f_ops == &mqueueops) {
2480 			mq = FPTOMQ(fp);
2481 			mtx_lock(&mq->mq_mutex);
2482 			notifier_remove(p, FPTOMQ(fp), i);
2483 			mtx_unlock(&mq->mq_mutex);
2484 		}
2485 	}
2486 	FILEDESC_SUNLOCK(fdp);
2487 	KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2488 }
2489 
2490 static int
mqf_poll(struct file * fp,int events,struct ucred * active_cred,struct thread * td)2491 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2492 	struct thread *td)
2493 {
2494 	struct mqueue *mq = FPTOMQ(fp);
2495 	int revents = 0;
2496 
2497 	mtx_lock(&mq->mq_mutex);
2498 	if (events & (POLLIN | POLLRDNORM)) {
2499 		if (mq->mq_curmsgs) {
2500 			revents |= events & (POLLIN | POLLRDNORM);
2501 		} else {
2502 			mq->mq_flags |= MQ_RSEL;
2503 			selrecord(td, &mq->mq_rsel);
2504  		}
2505 	}
2506 	if (events & POLLOUT) {
2507 		if (mq->mq_curmsgs < mq->mq_maxmsg)
2508 			revents |= POLLOUT;
2509 		else {
2510 			mq->mq_flags |= MQ_WSEL;
2511 			selrecord(td, &mq->mq_wsel);
2512 		}
2513 	}
2514 	mtx_unlock(&mq->mq_mutex);
2515 	return (revents);
2516 }
2517 
2518 static int
mqf_close(struct file * fp,struct thread * td)2519 mqf_close(struct file *fp, struct thread *td)
2520 {
2521 	struct mqfs_node *pn;
2522 
2523 	fp->f_ops = &badfileops;
2524 	pn = fp->f_data;
2525 	fp->f_data = NULL;
2526 	sx_xlock(&mqfs_data.mi_lock);
2527 	mqnode_release(pn);
2528 	sx_xunlock(&mqfs_data.mi_lock);
2529 	return (0);
2530 }
2531 
2532 static int
mqf_stat(struct file * fp,struct stat * st,struct ucred * active_cred,struct thread * td)2533 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2534 	struct thread *td)
2535 {
2536 	struct mqfs_node *pn = fp->f_data;
2537 
2538 	bzero(st, sizeof *st);
2539 	sx_xlock(&mqfs_data.mi_lock);
2540 	st->st_atim = pn->mn_atime;
2541 	st->st_mtim = pn->mn_mtime;
2542 	st->st_ctim = pn->mn_ctime;
2543 	st->st_birthtim = pn->mn_birth;
2544 	st->st_uid = pn->mn_uid;
2545 	st->st_gid = pn->mn_gid;
2546 	st->st_mode = S_IFIFO | pn->mn_mode;
2547 	sx_xunlock(&mqfs_data.mi_lock);
2548 	return (0);
2549 }
2550 
2551 static int
mqf_chmod(struct file * fp,mode_t mode,struct ucred * active_cred,struct thread * td)2552 mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
2553     struct thread *td)
2554 {
2555 	struct mqfs_node *pn;
2556 	int error;
2557 
2558 	error = 0;
2559 	pn = fp->f_data;
2560 	sx_xlock(&mqfs_data.mi_lock);
2561 	error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2562 	    active_cred, NULL);
2563 	if (error != 0)
2564 		goto out;
2565 	pn->mn_mode = mode & ACCESSPERMS;
2566 out:
2567 	sx_xunlock(&mqfs_data.mi_lock);
2568 	return (error);
2569 }
2570 
2571 static int
mqf_chown(struct file * fp,uid_t uid,gid_t gid,struct ucred * active_cred,struct thread * td)2572 mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2573     struct thread *td)
2574 {
2575 	struct mqfs_node *pn;
2576 	int error;
2577 
2578 	error = 0;
2579 	pn = fp->f_data;
2580 	sx_xlock(&mqfs_data.mi_lock);
2581 	if (uid == (uid_t)-1)
2582 		uid = pn->mn_uid;
2583 	if (gid == (gid_t)-1)
2584 		gid = pn->mn_gid;
2585 	if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2586 	    (gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
2587 	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
2588 		goto out;
2589 	pn->mn_uid = uid;
2590 	pn->mn_gid = gid;
2591 out:
2592 	sx_xunlock(&mqfs_data.mi_lock);
2593 	return (error);
2594 }
2595 
2596 static int
mqf_kqfilter(struct file * fp,struct knote * kn)2597 mqf_kqfilter(struct file *fp, struct knote *kn)
2598 {
2599 	struct mqueue *mq = FPTOMQ(fp);
2600 	int error = 0;
2601 
2602 	if (kn->kn_filter == EVFILT_READ) {
2603 		kn->kn_fop = &mq_rfiltops;
2604 		knlist_add(&mq->mq_rsel.si_note, kn, 0);
2605 	} else if (kn->kn_filter == EVFILT_WRITE) {
2606 		kn->kn_fop = &mq_wfiltops;
2607 		knlist_add(&mq->mq_wsel.si_note, kn, 0);
2608 	} else
2609 		error = EINVAL;
2610 	return (error);
2611 }
2612 
2613 static void
filt_mqdetach(struct knote * kn)2614 filt_mqdetach(struct knote *kn)
2615 {
2616 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2617 
2618 	if (kn->kn_filter == EVFILT_READ)
2619 		knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2620 	else if (kn->kn_filter == EVFILT_WRITE)
2621 		knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2622 	else
2623 		panic("filt_mqdetach");
2624 }
2625 
2626 static int
filt_mqread(struct knote * kn,long hint)2627 filt_mqread(struct knote *kn, long hint)
2628 {
2629 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2630 
2631 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2632 	return (mq->mq_curmsgs != 0);
2633 }
2634 
2635 static int
filt_mqwrite(struct knote * kn,long hint)2636 filt_mqwrite(struct knote *kn, long hint)
2637 {
2638 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2639 
2640 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2641 	return (mq->mq_curmsgs < mq->mq_maxmsg);
2642 }
2643 
2644 static int
mqf_fill_kinfo(struct file * fp,struct kinfo_file * kif,struct filedesc * fdp)2645 mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2646 {
2647 
2648 	kif->kf_type = KF_TYPE_MQUEUE;
2649 	return (0);
2650 }
2651 
2652 static struct fileops mqueueops = {
2653 	.fo_read		= invfo_rdwr,
2654 	.fo_write		= invfo_rdwr,
2655 	.fo_truncate		= invfo_truncate,
2656 	.fo_ioctl		= invfo_ioctl,
2657 	.fo_poll		= mqf_poll,
2658 	.fo_kqfilter		= mqf_kqfilter,
2659 	.fo_stat		= mqf_stat,
2660 	.fo_close		= mqf_close,
2661 	.fo_chmod		= mqf_chmod,
2662 	.fo_chown		= mqf_chown,
2663 	.fo_sendfile		= invfo_sendfile,
2664 	.fo_fill_kinfo		= mqf_fill_kinfo,
2665 };
2666 
2667 static struct vop_vector mqfs_vnodeops = {
2668 	.vop_default 		= &default_vnodeops,
2669 	.vop_access		= mqfs_access,
2670 	.vop_cachedlookup	= mqfs_lookup,
2671 	.vop_lookup		= vfs_cache_lookup,
2672 	.vop_reclaim		= mqfs_reclaim,
2673 	.vop_create		= mqfs_create,
2674 	.vop_remove		= mqfs_remove,
2675 	.vop_inactive		= mqfs_inactive,
2676 	.vop_open		= mqfs_open,
2677 	.vop_close		= mqfs_close,
2678 	.vop_getattr		= mqfs_getattr,
2679 	.vop_setattr		= mqfs_setattr,
2680 	.vop_read		= mqfs_read,
2681 	.vop_write		= VOP_EOPNOTSUPP,
2682 	.vop_readdir		= mqfs_readdir,
2683 	.vop_mkdir		= VOP_EOPNOTSUPP,
2684 	.vop_rmdir		= VOP_EOPNOTSUPP
2685 };
2686 
2687 static struct vfsops mqfs_vfsops = {
2688 	.vfs_init 		= mqfs_init,
2689 	.vfs_uninit		= mqfs_uninit,
2690 	.vfs_mount		= mqfs_mount,
2691 	.vfs_unmount		= mqfs_unmount,
2692 	.vfs_root		= mqfs_root,
2693 	.vfs_statfs		= mqfs_statfs,
2694 };
2695 
2696 static struct vfsconf mqueuefs_vfsconf = {
2697 	.vfc_version = VFS_VERSION,
2698 	.vfc_name = "mqueuefs",
2699 	.vfc_vfsops = &mqfs_vfsops,
2700 	.vfc_typenum = -1,
2701 	.vfc_flags = VFCF_SYNTHETIC
2702 };
2703 
2704 static struct syscall_helper_data mq_syscalls[] = {
2705 	SYSCALL_INIT_HELPER(kmq_open),
2706 	SYSCALL_INIT_HELPER_F(kmq_setattr, SYF_CAPENABLED),
2707 	SYSCALL_INIT_HELPER_F(kmq_timedsend, SYF_CAPENABLED),
2708 	SYSCALL_INIT_HELPER_F(kmq_timedreceive, SYF_CAPENABLED),
2709 	SYSCALL_INIT_HELPER_F(kmq_notify, SYF_CAPENABLED),
2710 	SYSCALL_INIT_HELPER(kmq_unlink),
2711 	SYSCALL_INIT_LAST
2712 };
2713 
2714 #ifdef COMPAT_FREEBSD32
2715 #include <compat/freebsd32/freebsd32.h>
2716 #include <compat/freebsd32/freebsd32_proto.h>
2717 #include <compat/freebsd32/freebsd32_signal.h>
2718 #include <compat/freebsd32/freebsd32_syscall.h>
2719 #include <compat/freebsd32/freebsd32_util.h>
2720 
2721 static void
mq_attr_from32(const struct mq_attr32 * from,struct mq_attr * to)2722 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2723 {
2724 
2725 	to->mq_flags = from->mq_flags;
2726 	to->mq_maxmsg = from->mq_maxmsg;
2727 	to->mq_msgsize = from->mq_msgsize;
2728 	to->mq_curmsgs = from->mq_curmsgs;
2729 }
2730 
2731 static void
mq_attr_to32(const struct mq_attr * from,struct mq_attr32 * to)2732 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2733 {
2734 
2735 	to->mq_flags = from->mq_flags;
2736 	to->mq_maxmsg = from->mq_maxmsg;
2737 	to->mq_msgsize = from->mq_msgsize;
2738 	to->mq_curmsgs = from->mq_curmsgs;
2739 }
2740 
2741 int
freebsd32_kmq_open(struct thread * td,struct freebsd32_kmq_open_args * uap)2742 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2743 {
2744 	struct mq_attr attr;
2745 	struct mq_attr32 attr32;
2746 	int flags, error;
2747 
2748 	if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2749 		return (EINVAL);
2750 	flags = FFLAGS(uap->flags);
2751 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2752 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2753 		if (error)
2754 			return (error);
2755 		mq_attr_from32(&attr32, &attr);
2756 	}
2757 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2758 	    uap->attr != NULL ? &attr : NULL));
2759 }
2760 
2761 int
freebsd32_kmq_setattr(struct thread * td,struct freebsd32_kmq_setattr_args * uap)2762 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2763 {
2764 	struct mq_attr attr, oattr;
2765 	struct mq_attr32 attr32, oattr32;
2766 	int error;
2767 
2768 	if (uap->attr != NULL) {
2769 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2770 		if (error != 0)
2771 			return (error);
2772 		mq_attr_from32(&attr32, &attr);
2773 	}
2774 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2775 	    &oattr);
2776 	if (error == 0 && uap->oattr != NULL) {
2777 		mq_attr_to32(&oattr, &oattr32);
2778 		bzero(oattr32.__reserved, sizeof(oattr32.__reserved));
2779 		error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2780 	}
2781 	return (error);
2782 }
2783 
2784 int
freebsd32_kmq_timedsend(struct thread * td,struct freebsd32_kmq_timedsend_args * uap)2785 freebsd32_kmq_timedsend(struct thread *td,
2786     struct freebsd32_kmq_timedsend_args *uap)
2787 {
2788 	struct mqueue *mq;
2789 	struct file *fp;
2790 	struct timespec32 ets32;
2791 	struct timespec *abs_timeout, ets;
2792 	int error;
2793 	int waitok;
2794 
2795 	AUDIT_ARG_FD(uap->mqd);
2796 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2797 	if (error)
2798 		return (error);
2799 	if (uap->abs_timeout != NULL) {
2800 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2801 		if (error != 0)
2802 			goto out;
2803 		CP(ets32, ets, tv_sec);
2804 		CP(ets32, ets, tv_nsec);
2805 		abs_timeout = &ets;
2806 	} else
2807 		abs_timeout = NULL;
2808 	waitok = !(fp->f_flag & O_NONBLOCK);
2809 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2810 		uap->msg_prio, waitok, abs_timeout);
2811 out:
2812 	fdrop(fp, td);
2813 	return (error);
2814 }
2815 
2816 int
freebsd32_kmq_timedreceive(struct thread * td,struct freebsd32_kmq_timedreceive_args * uap)2817 freebsd32_kmq_timedreceive(struct thread *td,
2818     struct freebsd32_kmq_timedreceive_args *uap)
2819 {
2820 	struct mqueue *mq;
2821 	struct file *fp;
2822 	struct timespec32 ets32;
2823 	struct timespec *abs_timeout, ets;
2824 	int error, waitok;
2825 
2826 	AUDIT_ARG_FD(uap->mqd);
2827 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2828 	if (error)
2829 		return (error);
2830 	if (uap->abs_timeout != NULL) {
2831 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2832 		if (error != 0)
2833 			goto out;
2834 		CP(ets32, ets, tv_sec);
2835 		CP(ets32, ets, tv_nsec);
2836 		abs_timeout = &ets;
2837 	} else
2838 		abs_timeout = NULL;
2839 	waitok = !(fp->f_flag & O_NONBLOCK);
2840 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2841 		uap->msg_prio, waitok, abs_timeout);
2842 out:
2843 	fdrop(fp, td);
2844 	return (error);
2845 }
2846 
2847 int
freebsd32_kmq_notify(struct thread * td,struct freebsd32_kmq_notify_args * uap)2848 freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap)
2849 {
2850 	struct sigevent ev, *evp;
2851 	struct sigevent32 ev32;
2852 	int error;
2853 
2854 	if (uap->sigev == NULL) {
2855 		evp = NULL;
2856 	} else {
2857 		error = copyin(uap->sigev, &ev32, sizeof(ev32));
2858 		if (error != 0)
2859 			return (error);
2860 		error = convert_sigevent32(&ev32, &ev);
2861 		if (error != 0)
2862 			return (error);
2863 		evp = &ev;
2864 	}
2865 	return (kern_kmq_notify(td, uap->mqd, evp));
2866 }
2867 
2868 static struct syscall_helper_data mq32_syscalls[] = {
2869 	SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2870 	SYSCALL32_INIT_HELPER_F(freebsd32_kmq_setattr, SYF_CAPENABLED),
2871 	SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedsend, SYF_CAPENABLED),
2872 	SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedreceive, SYF_CAPENABLED),
2873 	SYSCALL32_INIT_HELPER_F(freebsd32_kmq_notify, SYF_CAPENABLED),
2874 	SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2875 	SYSCALL_INIT_LAST
2876 };
2877 #endif
2878 
2879 static int
mqinit(void)2880 mqinit(void)
2881 {
2882 	int error;
2883 
2884 	error = syscall_helper_register(mq_syscalls, SY_THR_STATIC_KLD);
2885 	if (error != 0)
2886 		return (error);
2887 #ifdef COMPAT_FREEBSD32
2888 	error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD);
2889 	if (error != 0)
2890 		return (error);
2891 #endif
2892 	return (0);
2893 }
2894 
2895 static int
mqunload(void)2896 mqunload(void)
2897 {
2898 
2899 #ifdef COMPAT_FREEBSD32
2900 	syscall32_helper_unregister(mq32_syscalls);
2901 #endif
2902 	syscall_helper_unregister(mq_syscalls);
2903 	return (0);
2904 }
2905 
2906 static int
mq_modload(struct module * module,int cmd,void * arg)2907 mq_modload(struct module *module, int cmd, void *arg)
2908 {
2909 	int error = 0;
2910 
2911 	error = vfs_modevent(module, cmd, arg);
2912 	if (error != 0)
2913 		return (error);
2914 
2915 	switch (cmd) {
2916 	case MOD_LOAD:
2917 		error = mqinit();
2918 		if (error != 0)
2919 			mqunload();
2920 		break;
2921 	case MOD_UNLOAD:
2922 		error = mqunload();
2923 		break;
2924 	default:
2925 		break;
2926 	}
2927 	return (error);
2928 }
2929 
2930 static moduledata_t mqueuefs_mod = {
2931 	"mqueuefs",
2932 	mq_modload,
2933 	&mqueuefs_vfsconf
2934 };
2935 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2936 MODULE_VERSION(mqueuefs, 1);
2937