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