1655291f2SDavid Xu /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 38a36da99SPedro F. Giffuni * 4655291f2SDavid Xu * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 515bcf785SRobert Watson * Copyright (c) 2016-2017 Robert N. M. Watson 6655291f2SDavid Xu * All rights reserved. 7655291f2SDavid Xu * 815bcf785SRobert Watson * Portions of this software were developed by BAE Systems, the University of 915bcf785SRobert Watson * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 1015bcf785SRobert Watson * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 1115bcf785SRobert Watson * Computing (TC) research program. 1215bcf785SRobert Watson * 13655291f2SDavid Xu * Redistribution and use in source and binary forms, with or without 14655291f2SDavid Xu * modification, are permitted provided that the following conditions 15655291f2SDavid Xu * are met: 16655291f2SDavid Xu * 1. Redistributions of source code must retain the above copyright 17655291f2SDavid Xu * notice, this list of conditions and the following disclaimer. 18655291f2SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright 19655291f2SDavid Xu * notice, this list of conditions and the following disclaimer in the 20655291f2SDavid Xu * documentation and/or other materials provided with the distribution. 21655291f2SDavid Xu * 22655291f2SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23655291f2SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24655291f2SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25655291f2SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26655291f2SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27655291f2SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28655291f2SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29655291f2SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30655291f2SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31655291f2SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32655291f2SDavid Xu * SUCH DAMAGE. 33655291f2SDavid Xu * 34655291f2SDavid Xu */ 35655291f2SDavid Xu 36655291f2SDavid Xu /* 37655291f2SDavid Xu * POSIX message queue implementation. 38655291f2SDavid Xu * 39655291f2SDavid Xu * 1) A mqueue filesystem can be mounted, each message queue appears 40655291f2SDavid Xu * in mounted directory, user can change queue's permission and 41655291f2SDavid Xu * ownership, or remove a queue. Manually creating a file in the 42655291f2SDavid Xu * directory causes a message queue to be created in the kernel with 43655291f2SDavid Xu * default message queue attributes applied and same name used, this 44655291f2SDavid Xu * method is not advocated since mq_open syscall allows user to specify 45655291f2SDavid Xu * different attributes. Also the file system can be mounted multiple 46655291f2SDavid Xu * times at different mount points but shows same contents. 47655291f2SDavid Xu * 48655291f2SDavid Xu * 2) Standard POSIX message queue API. The syscalls do not use vfs layer, 49655291f2SDavid Xu * but directly operate on internal data structure, this allows user to 50655291f2SDavid Xu * use the IPC facility without having to mount mqueue file system. 51655291f2SDavid Xu */ 52655291f2SDavid Xu 53655291f2SDavid Xu #include <sys/cdefs.h> 54655291f2SDavid Xu __FBSDID("$FreeBSD$"); 55655291f2SDavid Xu 562609222aSPawel Jakub Dawidek #include "opt_capsicum.h" 57afde2b65SKonstantin Belousov 58655291f2SDavid Xu #include <sys/param.h> 59655291f2SDavid Xu #include <sys/kernel.h> 60655291f2SDavid Xu #include <sys/systm.h> 61655291f2SDavid Xu #include <sys/limits.h> 62adb023aeSJamie Gritton #include <sys/malloc.h> 63655291f2SDavid Xu #include <sys/buf.h> 644a144410SRobert Watson #include <sys/capsicum.h> 65655291f2SDavid Xu #include <sys/dirent.h> 66655291f2SDavid Xu #include <sys/event.h> 67b2f92ef9SDavid Xu #include <sys/eventhandler.h> 68655291f2SDavid Xu #include <sys/fcntl.h> 69655291f2SDavid Xu #include <sys/file.h> 705ee2d4acSDavid Xu #include <sys/filedesc.h> 71adb023aeSJamie Gritton #include <sys/jail.h> 72655291f2SDavid Xu #include <sys/lock.h> 73655291f2SDavid Xu #include <sys/module.h> 74655291f2SDavid Xu #include <sys/mount.h> 75655291f2SDavid Xu #include <sys/mqueue.h> 76655291f2SDavid Xu #include <sys/mutex.h> 77655291f2SDavid Xu #include <sys/namei.h> 786aeb05d7STom Rhodes #include <sys/posix4.h> 79655291f2SDavid Xu #include <sys/poll.h> 80acd3428bSRobert Watson #include <sys/priv.h> 81655291f2SDavid Xu #include <sys/proc.h> 82655291f2SDavid Xu #include <sys/queue.h> 83655291f2SDavid Xu #include <sys/sysproto.h> 84655291f2SDavid Xu #include <sys/stat.h> 85655291f2SDavid Xu #include <sys/syscall.h> 86655291f2SDavid Xu #include <sys/syscallsubr.h> 875ee2d4acSDavid Xu #include <sys/sysent.h> 88655291f2SDavid Xu #include <sys/sx.h> 89655291f2SDavid Xu #include <sys/sysctl.h> 90655291f2SDavid Xu #include <sys/taskqueue.h> 91655291f2SDavid Xu #include <sys/unistd.h> 929696feebSJohn Baldwin #include <sys/user.h> 93655291f2SDavid Xu #include <sys/vnode.h> 94655291f2SDavid Xu #include <machine/atomic.h> 95655291f2SDavid Xu 9615bcf785SRobert Watson #include <security/audit/audit.h> 9715bcf785SRobert Watson 98de5b1952SAlexander Leidinger FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support"); 99de5b1952SAlexander Leidinger 100655291f2SDavid Xu /* 101655291f2SDavid Xu * Limits and constants 102655291f2SDavid Xu */ 103655291f2SDavid Xu #define MQFS_NAMELEN NAME_MAX 104655291f2SDavid Xu #define MQFS_DELEN (8 + MQFS_NAMELEN) 105655291f2SDavid Xu 106655291f2SDavid Xu /* node types */ 107655291f2SDavid Xu typedef enum { 108655291f2SDavid Xu mqfstype_none = 0, 109655291f2SDavid Xu mqfstype_root, 110655291f2SDavid Xu mqfstype_dir, 111655291f2SDavid Xu mqfstype_this, 112655291f2SDavid Xu mqfstype_parent, 113655291f2SDavid Xu mqfstype_file, 114655291f2SDavid Xu mqfstype_symlink, 115655291f2SDavid Xu } mqfs_type_t; 116655291f2SDavid Xu 117655291f2SDavid Xu struct mqfs_node; 118655291f2SDavid Xu 119655291f2SDavid Xu /* 120655291f2SDavid Xu * mqfs_info: describes a mqfs instance 121655291f2SDavid Xu */ 122655291f2SDavid Xu struct mqfs_info { 123655291f2SDavid Xu struct sx mi_lock; 124655291f2SDavid Xu struct mqfs_node *mi_root; 125655291f2SDavid Xu struct unrhdr *mi_unrhdr; 126655291f2SDavid Xu }; 127655291f2SDavid Xu 128655291f2SDavid Xu struct mqfs_vdata { 129655291f2SDavid Xu LIST_ENTRY(mqfs_vdata) mv_link; 130655291f2SDavid Xu struct mqfs_node *mv_node; 131655291f2SDavid Xu struct vnode *mv_vnode; 132655291f2SDavid Xu struct task mv_task; 133655291f2SDavid Xu }; 134655291f2SDavid Xu 135655291f2SDavid Xu /* 136655291f2SDavid Xu * mqfs_node: describes a node (file or directory) within a mqfs 137655291f2SDavid Xu */ 138655291f2SDavid Xu struct mqfs_node { 139655291f2SDavid Xu char mn_name[MQFS_NAMELEN+1]; 140655291f2SDavid Xu struct mqfs_info *mn_info; 141655291f2SDavid Xu struct mqfs_node *mn_parent; 142655291f2SDavid Xu LIST_HEAD(,mqfs_node) mn_children; 143655291f2SDavid Xu LIST_ENTRY(mqfs_node) mn_sibling; 144655291f2SDavid Xu LIST_HEAD(,mqfs_vdata) mn_vnodes; 145adb023aeSJamie Gritton const void *mn_pr_root; 146655291f2SDavid Xu int mn_refcount; 147655291f2SDavid Xu mqfs_type_t mn_type; 148655291f2SDavid Xu int mn_deleted; 14960ae52f7SEd Schouten uint32_t mn_fileno; 150655291f2SDavid Xu void *mn_data; 151655291f2SDavid Xu struct timespec mn_birth; 152655291f2SDavid Xu struct timespec mn_ctime; 153655291f2SDavid Xu struct timespec mn_atime; 154655291f2SDavid Xu struct timespec mn_mtime; 155655291f2SDavid Xu uid_t mn_uid; 156655291f2SDavid Xu gid_t mn_gid; 157655291f2SDavid Xu int mn_mode; 158655291f2SDavid Xu }; 159655291f2SDavid Xu 160655291f2SDavid Xu #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node) 161655291f2SDavid Xu #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data)) 162655291f2SDavid Xu #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data)) 163b2f92ef9SDavid Xu #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \ 164b2f92ef9SDavid Xu (fp)->f_data)->mn_data)) 165b2f92ef9SDavid Xu 166655291f2SDavid Xu TAILQ_HEAD(msgq, mqueue_msg); 167655291f2SDavid Xu 168655291f2SDavid Xu struct mqueue; 169b2f92ef9SDavid Xu 170b2f92ef9SDavid Xu struct mqueue_notifier { 171b2f92ef9SDavid Xu LIST_ENTRY(mqueue_notifier) nt_link; 172b2f92ef9SDavid Xu struct sigevent nt_sigev; 173b2f92ef9SDavid Xu ksiginfo_t nt_ksi; 174b2f92ef9SDavid Xu struct proc *nt_proc; 175655291f2SDavid Xu }; 176655291f2SDavid Xu 177655291f2SDavid Xu struct mqueue { 178655291f2SDavid Xu struct mtx mq_mutex; 179655291f2SDavid Xu int mq_flags; 180655291f2SDavid Xu long mq_maxmsg; 181655291f2SDavid Xu long mq_msgsize; 182655291f2SDavid Xu long mq_curmsgs; 183655291f2SDavid Xu long mq_totalbytes; 184655291f2SDavid Xu struct msgq mq_msgq; 185655291f2SDavid Xu int mq_receivers; 186655291f2SDavid Xu int mq_senders; 187655291f2SDavid Xu struct selinfo mq_rsel; 188655291f2SDavid Xu struct selinfo mq_wsel; 189b2f92ef9SDavid Xu struct mqueue_notifier *mq_notifier; 190655291f2SDavid Xu }; 191655291f2SDavid Xu 192655291f2SDavid Xu #define MQ_RSEL 0x01 193655291f2SDavid Xu #define MQ_WSEL 0x02 194655291f2SDavid Xu 195655291f2SDavid Xu struct mqueue_msg { 196655291f2SDavid Xu TAILQ_ENTRY(mqueue_msg) msg_link; 197655291f2SDavid Xu unsigned int msg_prio; 198655291f2SDavid Xu unsigned int msg_size; 199655291f2SDavid Xu /* following real data... */ 200655291f2SDavid Xu }; 201655291f2SDavid Xu 2027029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 203655291f2SDavid Xu "POSIX real time message queue"); 204655291f2SDavid Xu 205655291f2SDavid Xu static int default_maxmsg = 10; 206655291f2SDavid Xu static int default_msgsize = 1024; 207655291f2SDavid Xu 2089947b459SDavid Xu static int maxmsg = 100; 209655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW, 210655291f2SDavid Xu &maxmsg, 0, "Default maximum messages in queue"); 211655291f2SDavid Xu static int maxmsgsize = 16384; 212655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW, 213655291f2SDavid Xu &maxmsgsize, 0, "Default maximum message size"); 214655291f2SDavid Xu static int maxmq = 100; 215655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW, 216655291f2SDavid Xu &maxmq, 0, "maximum message queues"); 217655291f2SDavid Xu static int curmq = 0; 218655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW, 219655291f2SDavid Xu &curmq, 0, "current message queue number"); 220655291f2SDavid Xu static int unloadable = 0; 221655291f2SDavid Xu static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data"); 222655291f2SDavid Xu 223b2f92ef9SDavid Xu static eventhandler_tag exit_tag; 224b2f92ef9SDavid Xu 225655291f2SDavid Xu /* Only one instance per-system */ 226655291f2SDavid Xu static struct mqfs_info mqfs_data; 227655291f2SDavid Xu static uma_zone_t mqnode_zone; 228655291f2SDavid Xu static uma_zone_t mqueue_zone; 229655291f2SDavid Xu static uma_zone_t mvdata_zone; 230b2f92ef9SDavid Xu static uma_zone_t mqnoti_zone; 231655291f2SDavid Xu static struct vop_vector mqfs_vnodeops; 232655291f2SDavid Xu static struct fileops mqueueops; 233adb023aeSJamie Gritton static unsigned mqfs_osd_jail_slot; 234655291f2SDavid Xu 235655291f2SDavid Xu /* 236655291f2SDavid Xu * Directory structure construction and manipulation 237655291f2SDavid Xu */ 238655291f2SDavid Xu #ifdef notyet 239655291f2SDavid Xu static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent, 240ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 241102178d0SDavid Xu static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent, 242ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 243655291f2SDavid Xu #endif 244655291f2SDavid Xu 245655291f2SDavid Xu static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent, 246ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 247655291f2SDavid Xu static int mqfs_destroy(struct mqfs_node *mn); 248655291f2SDavid Xu static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn); 249655291f2SDavid Xu static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn); 250655291f2SDavid Xu static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn); 251add14c83SJamie Gritton static int mqfs_prison_remove(void *obj, void *data); 252655291f2SDavid Xu 253655291f2SDavid Xu /* 254655291f2SDavid Xu * Message queue construction and maniplation 255655291f2SDavid Xu */ 256655291f2SDavid Xu static struct mqueue *mqueue_alloc(const struct mq_attr *attr); 257655291f2SDavid Xu static void mqueue_free(struct mqueue *mq); 258655291f2SDavid Xu static int mqueue_send(struct mqueue *mq, const char *msg_ptr, 259655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 260655291f2SDavid Xu const struct timespec *abs_timeout); 261655291f2SDavid Xu static int mqueue_receive(struct mqueue *mq, char *msg_ptr, 262655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 263655291f2SDavid Xu const struct timespec *abs_timeout); 264655291f2SDavid Xu static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, 265655291f2SDavid Xu int timo); 266655291f2SDavid Xu static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, 267655291f2SDavid Xu int timo); 268655291f2SDavid Xu static void mqueue_send_notification(struct mqueue *mq); 269b2f92ef9SDavid Xu static void mqueue_fdclose(struct thread *td, int fd, struct file *fp); 270b2f92ef9SDavid Xu static void mq_proc_exit(void *arg, struct proc *p); 271655291f2SDavid Xu 272b2f92ef9SDavid Xu /* 273b2f92ef9SDavid Xu * kqueue filters 274b2f92ef9SDavid Xu */ 275655291f2SDavid Xu static void filt_mqdetach(struct knote *kn); 276655291f2SDavid Xu static int filt_mqread(struct knote *kn, long hint); 277655291f2SDavid Xu static int filt_mqwrite(struct knote *kn, long hint); 278655291f2SDavid Xu 279e76d823bSRobert Watson struct filterops mq_rfiltops = { 280e76d823bSRobert Watson .f_isfd = 1, 281e76d823bSRobert Watson .f_detach = filt_mqdetach, 282e76d823bSRobert Watson .f_event = filt_mqread, 283e76d823bSRobert Watson }; 284e76d823bSRobert Watson struct filterops mq_wfiltops = { 285e76d823bSRobert Watson .f_isfd = 1, 286e76d823bSRobert Watson .f_detach = filt_mqdetach, 287e76d823bSRobert Watson .f_event = filt_mqwrite, 288e76d823bSRobert Watson }; 289655291f2SDavid Xu 290655291f2SDavid Xu /* 291655291f2SDavid Xu * Initialize fileno bitmap 292655291f2SDavid Xu */ 293655291f2SDavid Xu static void 294655291f2SDavid Xu mqfs_fileno_init(struct mqfs_info *mi) 295655291f2SDavid Xu { 296655291f2SDavid Xu struct unrhdr *up; 297655291f2SDavid Xu 298655291f2SDavid Xu up = new_unrhdr(1, INT_MAX, NULL); 299655291f2SDavid Xu mi->mi_unrhdr = up; 300655291f2SDavid Xu } 301655291f2SDavid Xu 302655291f2SDavid Xu /* 303655291f2SDavid Xu * Tear down fileno bitmap 304655291f2SDavid Xu */ 305655291f2SDavid Xu static void 306655291f2SDavid Xu mqfs_fileno_uninit(struct mqfs_info *mi) 307655291f2SDavid Xu { 308655291f2SDavid Xu struct unrhdr *up; 309655291f2SDavid Xu 310655291f2SDavid Xu up = mi->mi_unrhdr; 311655291f2SDavid Xu mi->mi_unrhdr = NULL; 312655291f2SDavid Xu delete_unrhdr(up); 313655291f2SDavid Xu } 314655291f2SDavid Xu 315655291f2SDavid Xu /* 316655291f2SDavid Xu * Allocate a file number 317655291f2SDavid Xu */ 318b2f92ef9SDavid Xu static void 319655291f2SDavid Xu mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn) 320655291f2SDavid Xu { 321655291f2SDavid Xu /* make sure our parent has a file number */ 322655291f2SDavid Xu if (mn->mn_parent && !mn->mn_parent->mn_fileno) 323655291f2SDavid Xu mqfs_fileno_alloc(mi, mn->mn_parent); 324655291f2SDavid Xu 325655291f2SDavid Xu switch (mn->mn_type) { 326655291f2SDavid Xu case mqfstype_root: 327655291f2SDavid Xu case mqfstype_dir: 328655291f2SDavid Xu case mqfstype_file: 329655291f2SDavid Xu case mqfstype_symlink: 330655291f2SDavid Xu mn->mn_fileno = alloc_unr(mi->mi_unrhdr); 331655291f2SDavid Xu break; 332655291f2SDavid Xu case mqfstype_this: 333655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 334655291f2SDavid Xu ("mqfstype_this node has no parent")); 335655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 336655291f2SDavid Xu break; 337655291f2SDavid Xu case mqfstype_parent: 338655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 339655291f2SDavid Xu ("mqfstype_parent node has no parent")); 340655291f2SDavid Xu if (mn->mn_parent == mi->mi_root) { 341655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 342655291f2SDavid Xu break; 343655291f2SDavid Xu } 344655291f2SDavid Xu KASSERT(mn->mn_parent->mn_parent != NULL, 345655291f2SDavid Xu ("mqfstype_parent node has no grandparent")); 346655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno; 347655291f2SDavid Xu break; 348655291f2SDavid Xu default: 349655291f2SDavid Xu KASSERT(0, 350655291f2SDavid Xu ("mqfs_fileno_alloc() called for unknown type node: %d", 351655291f2SDavid Xu mn->mn_type)); 352655291f2SDavid Xu break; 353655291f2SDavid Xu } 354655291f2SDavid Xu } 355655291f2SDavid Xu 356655291f2SDavid Xu /* 357655291f2SDavid Xu * Release a file number 358655291f2SDavid Xu */ 359b2f92ef9SDavid Xu static void 360655291f2SDavid Xu mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn) 361655291f2SDavid Xu { 362655291f2SDavid Xu switch (mn->mn_type) { 363655291f2SDavid Xu case mqfstype_root: 364655291f2SDavid Xu case mqfstype_dir: 365655291f2SDavid Xu case mqfstype_file: 366655291f2SDavid Xu case mqfstype_symlink: 367655291f2SDavid Xu free_unr(mi->mi_unrhdr, mn->mn_fileno); 368655291f2SDavid Xu break; 369655291f2SDavid Xu case mqfstype_this: 370655291f2SDavid Xu case mqfstype_parent: 371655291f2SDavid Xu /* ignore these, as they don't "own" their file number */ 372655291f2SDavid Xu break; 373655291f2SDavid Xu default: 374655291f2SDavid Xu KASSERT(0, 375655291f2SDavid Xu ("mqfs_fileno_free() called for unknown type node: %d", 376655291f2SDavid Xu mn->mn_type)); 377655291f2SDavid Xu break; 378655291f2SDavid Xu } 379655291f2SDavid Xu } 380655291f2SDavid Xu 381655291f2SDavid Xu static __inline struct mqfs_node * 382655291f2SDavid Xu mqnode_alloc(void) 383655291f2SDavid Xu { 384655291f2SDavid Xu return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO); 385655291f2SDavid Xu } 386655291f2SDavid Xu 387655291f2SDavid Xu static __inline void 388655291f2SDavid Xu mqnode_free(struct mqfs_node *node) 389655291f2SDavid Xu { 390655291f2SDavid Xu uma_zfree(mqnode_zone, node); 391655291f2SDavid Xu } 392655291f2SDavid Xu 393655291f2SDavid Xu static __inline void 394655291f2SDavid Xu mqnode_addref(struct mqfs_node *node) 395655291f2SDavid Xu { 396f71ef9b6SMark Johnston atomic_add_int(&node->mn_refcount, 1); 397655291f2SDavid Xu } 398655291f2SDavid Xu 399655291f2SDavid Xu static __inline void 400655291f2SDavid Xu mqnode_release(struct mqfs_node *node) 401655291f2SDavid Xu { 402fbc48e97SDavid Xu struct mqfs_info *mqfs; 403655291f2SDavid Xu int old, exp; 404655291f2SDavid Xu 405fbc48e97SDavid Xu mqfs = node->mn_info; 406655291f2SDavid Xu old = atomic_fetchadd_int(&node->mn_refcount, -1); 407655291f2SDavid Xu if (node->mn_type == mqfstype_dir || 408655291f2SDavid Xu node->mn_type == mqfstype_root) 409655291f2SDavid Xu exp = 3; /* include . and .. */ 410655291f2SDavid Xu else 411655291f2SDavid Xu exp = 1; 412fbc48e97SDavid Xu if (old == exp) { 413fbc48e97SDavid Xu int locked = sx_xlocked(&mqfs->mi_lock); 414fbc48e97SDavid Xu if (!locked) 415fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 416655291f2SDavid Xu mqfs_destroy(node); 417fbc48e97SDavid Xu if (!locked) 418fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 419fbc48e97SDavid Xu } 420655291f2SDavid Xu } 421655291f2SDavid Xu 422655291f2SDavid Xu /* 423655291f2SDavid Xu * Add a node to a directory 424655291f2SDavid Xu */ 425655291f2SDavid Xu static int 426655291f2SDavid Xu mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node) 427655291f2SDavid Xu { 428655291f2SDavid Xu KASSERT(parent != NULL, ("%s(): parent is NULL", __func__)); 429655291f2SDavid Xu KASSERT(parent->mn_info != NULL, 430655291f2SDavid Xu ("%s(): parent has no mn_info", __func__)); 431655291f2SDavid Xu KASSERT(parent->mn_type == mqfstype_dir || 432655291f2SDavid Xu parent->mn_type == mqfstype_root, 433655291f2SDavid Xu ("%s(): parent is not a directory", __func__)); 434655291f2SDavid Xu 435655291f2SDavid Xu node->mn_info = parent->mn_info; 436655291f2SDavid Xu node->mn_parent = parent; 437655291f2SDavid Xu LIST_INIT(&node->mn_children); 438655291f2SDavid Xu LIST_INIT(&node->mn_vnodes); 439655291f2SDavid Xu LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling); 440655291f2SDavid Xu mqnode_addref(parent); 441655291f2SDavid Xu return (0); 442655291f2SDavid Xu } 443655291f2SDavid Xu 444ba0360b1SDavid Xu static struct mqfs_node * 445ba0360b1SDavid Xu mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode, 446ba0360b1SDavid Xu int nodetype) 447ba0360b1SDavid Xu { 448ba0360b1SDavid Xu struct mqfs_node *node; 449ba0360b1SDavid Xu 450ba0360b1SDavid Xu node = mqnode_alloc(); 451ba0360b1SDavid Xu strncpy(node->mn_name, name, namelen); 452adb023aeSJamie Gritton node->mn_pr_root = cred->cr_prison->pr_root; 453ba0360b1SDavid Xu node->mn_type = nodetype; 454ba0360b1SDavid Xu node->mn_refcount = 1; 455fbc48e97SDavid Xu vfs_timestamp(&node->mn_birth); 456ba0360b1SDavid Xu node->mn_ctime = node->mn_atime = node->mn_mtime 457ba0360b1SDavid Xu = node->mn_birth; 458ba0360b1SDavid Xu node->mn_uid = cred->cr_uid; 459ba0360b1SDavid Xu node->mn_gid = cred->cr_gid; 460ba0360b1SDavid Xu node->mn_mode = mode; 461ba0360b1SDavid Xu return (node); 462ba0360b1SDavid Xu } 463ba0360b1SDavid Xu 464ba0360b1SDavid Xu /* 465ba0360b1SDavid Xu * Create a file 466ba0360b1SDavid Xu */ 467ba0360b1SDavid Xu static struct mqfs_node * 468ba0360b1SDavid Xu mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen, 469ba0360b1SDavid Xu struct ucred *cred, int mode) 470ba0360b1SDavid Xu { 471ba0360b1SDavid Xu struct mqfs_node *node; 472ba0360b1SDavid Xu 473ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file); 474ba0360b1SDavid Xu if (mqfs_add_node(parent, node) != 0) { 475ba0360b1SDavid Xu mqnode_free(node); 476ba0360b1SDavid Xu return (NULL); 477ba0360b1SDavid Xu } 478ba0360b1SDavid Xu return (node); 479ba0360b1SDavid Xu } 480ba0360b1SDavid Xu 481655291f2SDavid Xu /* 482655291f2SDavid Xu * Add . and .. to a directory 483655291f2SDavid Xu */ 484655291f2SDavid Xu static int 485655291f2SDavid Xu mqfs_fixup_dir(struct mqfs_node *parent) 486655291f2SDavid Xu { 487655291f2SDavid Xu struct mqfs_node *dir; 488655291f2SDavid Xu 489655291f2SDavid Xu dir = mqnode_alloc(); 490655291f2SDavid Xu dir->mn_name[0] = '.'; 491655291f2SDavid Xu dir->mn_type = mqfstype_this; 492655291f2SDavid Xu dir->mn_refcount = 1; 493655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 494655291f2SDavid Xu mqnode_free(dir); 495655291f2SDavid Xu return (-1); 496655291f2SDavid Xu } 497655291f2SDavid Xu 498655291f2SDavid Xu dir = mqnode_alloc(); 499655291f2SDavid Xu dir->mn_name[0] = dir->mn_name[1] = '.'; 500655291f2SDavid Xu dir->mn_type = mqfstype_parent; 501655291f2SDavid Xu dir->mn_refcount = 1; 502655291f2SDavid Xu 503655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 504655291f2SDavid Xu mqnode_free(dir); 505655291f2SDavid Xu return (-1); 506655291f2SDavid Xu } 507655291f2SDavid Xu 508655291f2SDavid Xu return (0); 509655291f2SDavid Xu } 510655291f2SDavid Xu 511655291f2SDavid Xu #ifdef notyet 512655291f2SDavid Xu 513655291f2SDavid Xu /* 514655291f2SDavid Xu * Create a directory 515655291f2SDavid Xu */ 516b2f92ef9SDavid Xu static struct mqfs_node * 517ba0360b1SDavid Xu mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen, 518ba0360b1SDavid Xu struct ucred *cred, int mode) 519655291f2SDavid Xu { 520ba0360b1SDavid Xu struct mqfs_node *node; 521655291f2SDavid Xu 522ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir); 523ba0360b1SDavid Xu if (mqfs_add_node(parent, node) != 0) { 524ba0360b1SDavid Xu mqnode_free(node); 525655291f2SDavid Xu return (NULL); 526655291f2SDavid Xu } 527655291f2SDavid Xu 528ba0360b1SDavid Xu if (mqfs_fixup_dir(node) != 0) { 529ba0360b1SDavid Xu mqfs_destroy(node); 530655291f2SDavid Xu return (NULL); 531655291f2SDavid Xu } 532ba0360b1SDavid Xu return (node); 533655291f2SDavid Xu } 534b2f92ef9SDavid Xu 535b2f92ef9SDavid Xu /* 536b2f92ef9SDavid Xu * Create a symlink 537b2f92ef9SDavid Xu */ 538b2f92ef9SDavid Xu static struct mqfs_node * 539ba0360b1SDavid Xu mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen, 540ba0360b1SDavid Xu struct ucred *cred, int mode) 541b2f92ef9SDavid Xu { 542b2f92ef9SDavid Xu struct mqfs_node *node; 543b2f92ef9SDavid Xu 544ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink); 545655291f2SDavid Xu if (mqfs_add_node(parent, node) != 0) { 546655291f2SDavid Xu mqnode_free(node); 547655291f2SDavid Xu return (NULL); 548655291f2SDavid Xu } 549655291f2SDavid Xu return (node); 550655291f2SDavid Xu } 551655291f2SDavid Xu 552ba0360b1SDavid Xu #endif 553ba0360b1SDavid Xu 554655291f2SDavid Xu /* 555655291f2SDavid Xu * Destroy a node or a tree of nodes 556655291f2SDavid Xu */ 557b2f92ef9SDavid Xu static int 558655291f2SDavid Xu mqfs_destroy(struct mqfs_node *node) 559655291f2SDavid Xu { 560655291f2SDavid Xu struct mqfs_node *parent; 561655291f2SDavid Xu 562655291f2SDavid Xu KASSERT(node != NULL, 563655291f2SDavid Xu ("%s(): node is NULL", __func__)); 564655291f2SDavid Xu KASSERT(node->mn_info != NULL, 565655291f2SDavid Xu ("%s(): node has no mn_info", __func__)); 566655291f2SDavid Xu 567655291f2SDavid Xu /* destroy children */ 568655291f2SDavid Xu if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root) 569655291f2SDavid Xu while (! LIST_EMPTY(&node->mn_children)) 570655291f2SDavid Xu mqfs_destroy(LIST_FIRST(&node->mn_children)); 571655291f2SDavid Xu 572655291f2SDavid Xu /* unlink from parent */ 573655291f2SDavid Xu if ((parent = node->mn_parent) != NULL) { 574655291f2SDavid Xu KASSERT(parent->mn_info == node->mn_info, 575655291f2SDavid Xu ("%s(): parent has different mn_info", __func__)); 576655291f2SDavid Xu LIST_REMOVE(node, mn_sibling); 577655291f2SDavid Xu } 578655291f2SDavid Xu 579655291f2SDavid Xu if (node->mn_fileno != 0) 580655291f2SDavid Xu mqfs_fileno_free(node->mn_info, node); 581655291f2SDavid Xu if (node->mn_data != NULL) 582655291f2SDavid Xu mqueue_free(node->mn_data); 583655291f2SDavid Xu mqnode_free(node); 584655291f2SDavid Xu return (0); 585655291f2SDavid Xu } 586655291f2SDavid Xu 587655291f2SDavid Xu /* 588655291f2SDavid Xu * Mount a mqfs instance 589655291f2SDavid Xu */ 590655291f2SDavid Xu static int 591dfd233edSAttilio Rao mqfs_mount(struct mount *mp) 592655291f2SDavid Xu { 593655291f2SDavid Xu struct statfs *sbp; 594655291f2SDavid Xu 595655291f2SDavid Xu if (mp->mnt_flag & MNT_UPDATE) 596655291f2SDavid Xu return (EOPNOTSUPP); 597655291f2SDavid Xu 598655291f2SDavid Xu mp->mnt_data = &mqfs_data; 5995da56ddbSTor Egge MNT_ILOCK(mp); 600655291f2SDavid Xu mp->mnt_flag |= MNT_LOCAL; 6015da56ddbSTor Egge MNT_IUNLOCK(mp); 602655291f2SDavid Xu vfs_getnewfsid(mp); 603655291f2SDavid Xu 604655291f2SDavid Xu sbp = &mp->mnt_stat; 605655291f2SDavid Xu vfs_mountedfrom(mp, "mqueue"); 606655291f2SDavid Xu sbp->f_bsize = PAGE_SIZE; 607655291f2SDavid Xu sbp->f_iosize = PAGE_SIZE; 608655291f2SDavid Xu sbp->f_blocks = 1; 609655291f2SDavid Xu sbp->f_bfree = 0; 610655291f2SDavid Xu sbp->f_bavail = 0; 611655291f2SDavid Xu sbp->f_files = 1; 612655291f2SDavid Xu sbp->f_ffree = 0; 613655291f2SDavid Xu return (0); 614655291f2SDavid Xu } 615655291f2SDavid Xu 616655291f2SDavid Xu /* 617655291f2SDavid Xu * Unmount a mqfs instance 618655291f2SDavid Xu */ 619655291f2SDavid Xu static int 620dfd233edSAttilio Rao mqfs_unmount(struct mount *mp, int mntflags) 621655291f2SDavid Xu { 622655291f2SDavid Xu int error; 623655291f2SDavid Xu 624dfd233edSAttilio Rao error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, 625dfd233edSAttilio Rao curthread); 626655291f2SDavid Xu return (error); 627655291f2SDavid Xu } 628655291f2SDavid Xu 629655291f2SDavid Xu /* 630655291f2SDavid Xu * Return a root vnode 631655291f2SDavid Xu */ 632655291f2SDavid Xu static int 633dfd233edSAttilio Rao mqfs_root(struct mount *mp, int flags, struct vnode **vpp) 634655291f2SDavid Xu { 635655291f2SDavid Xu struct mqfs_info *mqfs; 636655291f2SDavid Xu int ret; 637655291f2SDavid Xu 638655291f2SDavid Xu mqfs = VFSTOMQFS(mp); 639655291f2SDavid Xu ret = mqfs_allocv(mp, vpp, mqfs->mi_root); 640655291f2SDavid Xu return (ret); 641655291f2SDavid Xu } 642655291f2SDavid Xu 643655291f2SDavid Xu /* 644655291f2SDavid Xu * Return filesystem stats 645655291f2SDavid Xu */ 646655291f2SDavid Xu static int 647dfd233edSAttilio Rao mqfs_statfs(struct mount *mp, struct statfs *sbp) 648655291f2SDavid Xu { 649655291f2SDavid Xu /* XXX update statistics */ 650655291f2SDavid Xu return (0); 651655291f2SDavid Xu } 652655291f2SDavid Xu 653655291f2SDavid Xu /* 654655291f2SDavid Xu * Initialize a mqfs instance 655655291f2SDavid Xu */ 656655291f2SDavid Xu static int 657655291f2SDavid Xu mqfs_init(struct vfsconf *vfc) 658655291f2SDavid Xu { 659655291f2SDavid Xu struct mqfs_node *root; 660655291f2SDavid Xu struct mqfs_info *mi; 661adb023aeSJamie Gritton osd_method_t methods[PR_MAXMETHOD] = { 662add14c83SJamie Gritton [PR_METHOD_REMOVE] = mqfs_prison_remove, 663adb023aeSJamie Gritton }; 664655291f2SDavid Xu 665655291f2SDavid Xu mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node), 666655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 667655291f2SDavid Xu mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue), 668655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 669655291f2SDavid Xu mvdata_zone = uma_zcreate("mvdata", 670655291f2SDavid Xu sizeof(struct mqfs_vdata), NULL, NULL, NULL, 671655291f2SDavid Xu NULL, UMA_ALIGN_PTR, 0); 672b2f92ef9SDavid Xu mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier), 673655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 674655291f2SDavid Xu mi = &mqfs_data; 675655291f2SDavid Xu sx_init(&mi->mi_lock, "mqfs lock"); 676655291f2SDavid Xu /* set up the root diretory */ 677ba0360b1SDavid Xu root = mqfs_create_node("/", 1, curthread->td_ucred, 01777, 678ba0360b1SDavid Xu mqfstype_root); 679655291f2SDavid Xu root->mn_info = mi; 680655291f2SDavid Xu LIST_INIT(&root->mn_children); 681655291f2SDavid Xu LIST_INIT(&root->mn_vnodes); 682655291f2SDavid Xu mi->mi_root = root; 683655291f2SDavid Xu mqfs_fileno_init(mi); 684655291f2SDavid Xu mqfs_fileno_alloc(mi, root); 685655291f2SDavid Xu mqfs_fixup_dir(root); 686b2f92ef9SDavid Xu exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL, 687b2f92ef9SDavid Xu EVENTHANDLER_PRI_ANY); 688b2f92ef9SDavid Xu mq_fdclose = mqueue_fdclose; 6895ee2d4acSDavid Xu p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING); 690add14c83SJamie Gritton mqfs_osd_jail_slot = osd_jail_register(NULL, methods); 691655291f2SDavid Xu return (0); 692655291f2SDavid Xu } 693655291f2SDavid Xu 694655291f2SDavid Xu /* 695655291f2SDavid Xu * Destroy a mqfs instance 696655291f2SDavid Xu */ 697655291f2SDavid Xu static int 698655291f2SDavid Xu mqfs_uninit(struct vfsconf *vfc) 699655291f2SDavid Xu { 700655291f2SDavid Xu struct mqfs_info *mi; 701655291f2SDavid Xu 702655291f2SDavid Xu if (!unloadable) 703655291f2SDavid Xu return (EOPNOTSUPP); 704add14c83SJamie Gritton osd_jail_deregister(mqfs_osd_jail_slot); 705b2f92ef9SDavid Xu EVENTHANDLER_DEREGISTER(process_exit, exit_tag); 706655291f2SDavid Xu mi = &mqfs_data; 707655291f2SDavid Xu mqfs_destroy(mi->mi_root); 708655291f2SDavid Xu mi->mi_root = NULL; 709655291f2SDavid Xu mqfs_fileno_uninit(mi); 710655291f2SDavid Xu sx_destroy(&mi->mi_lock); 711b2f92ef9SDavid Xu uma_zdestroy(mqnode_zone); 712b2f92ef9SDavid Xu uma_zdestroy(mqueue_zone); 713b2f92ef9SDavid Xu uma_zdestroy(mvdata_zone); 714b2f92ef9SDavid Xu uma_zdestroy(mqnoti_zone); 715655291f2SDavid Xu return (0); 716655291f2SDavid Xu } 717655291f2SDavid Xu 718655291f2SDavid Xu /* 719655291f2SDavid Xu * task routine 720655291f2SDavid Xu */ 721655291f2SDavid Xu static void 722655291f2SDavid Xu do_recycle(void *context, int pending __unused) 723655291f2SDavid Xu { 724655291f2SDavid Xu struct vnode *vp = (struct vnode *)context; 725655291f2SDavid Xu 7268e31b510SBryan Drewery vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 727af6e6b87SEdward Tomasz Napierala vrecycle(vp); 728b249ce48SMateusz Guzik VOP_UNLOCK(vp); 729655291f2SDavid Xu vdrop(vp); 730655291f2SDavid Xu } 731655291f2SDavid Xu 732655291f2SDavid Xu /* 733655291f2SDavid Xu * Allocate a vnode 734655291f2SDavid Xu */ 735b2f92ef9SDavid Xu static int 736655291f2SDavid Xu mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn) 737655291f2SDavid Xu { 738655291f2SDavid Xu struct mqfs_vdata *vd; 739fbc48e97SDavid Xu struct mqfs_info *mqfs; 740fbc48e97SDavid Xu struct vnode *newvpp; 741655291f2SDavid Xu int error; 742655291f2SDavid Xu 743fbc48e97SDavid Xu mqfs = pn->mn_info; 744fbc48e97SDavid Xu *vpp = NULL; 745fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 746655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 747fbc48e97SDavid Xu if (vd->mv_vnode->v_mount == mp) { 748fbc48e97SDavid Xu vhold(vd->mv_vnode); 749655291f2SDavid Xu break; 750655291f2SDavid Xu } 751fbc48e97SDavid Xu } 752655291f2SDavid Xu 753655291f2SDavid Xu if (vd != NULL) { 754fbc48e97SDavid Xu found: 755655291f2SDavid Xu *vpp = vd->mv_vnode; 756fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 757a92a971bSMateusz Guzik error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE); 758fbc48e97SDavid Xu vdrop(*vpp); 759fbc48e97SDavid Xu return (error); 760655291f2SDavid Xu } 761fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 762655291f2SDavid Xu 763fbc48e97SDavid Xu error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp); 764655291f2SDavid Xu if (error) 765655291f2SDavid Xu return (error); 766fbc48e97SDavid Xu vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY); 767fbc48e97SDavid Xu error = insmntque(newvpp, mp); 768fbc48e97SDavid Xu if (error != 0) 76961b9d89fSTor Egge return (error); 770fbc48e97SDavid Xu 771fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 772fbc48e97SDavid Xu /* 773fbc48e97SDavid Xu * Check if it has already been allocated 774fbc48e97SDavid Xu * while we were blocked. 775fbc48e97SDavid Xu */ 776fbc48e97SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 777fbc48e97SDavid Xu if (vd->mv_vnode->v_mount == mp) { 778fbc48e97SDavid Xu vhold(vd->mv_vnode); 779fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 780fbc48e97SDavid Xu 781fbc48e97SDavid Xu vgone(newvpp); 782fbc48e97SDavid Xu vput(newvpp); 783fbc48e97SDavid Xu goto found; 78461b9d89fSTor Egge } 785fbc48e97SDavid Xu } 786fbc48e97SDavid Xu 787fbc48e97SDavid Xu *vpp = newvpp; 788fbc48e97SDavid Xu 789655291f2SDavid Xu vd = uma_zalloc(mvdata_zone, M_WAITOK); 790655291f2SDavid Xu (*vpp)->v_data = vd; 791655291f2SDavid Xu vd->mv_vnode = *vpp; 792655291f2SDavid Xu vd->mv_node = pn; 793655291f2SDavid Xu TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp); 794655291f2SDavid Xu LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link); 795655291f2SDavid Xu mqnode_addref(pn); 796655291f2SDavid Xu switch (pn->mn_type) { 797655291f2SDavid Xu case mqfstype_root: 798655291f2SDavid Xu (*vpp)->v_vflag = VV_ROOT; 799655291f2SDavid Xu /* fall through */ 800655291f2SDavid Xu case mqfstype_dir: 801655291f2SDavid Xu case mqfstype_this: 802655291f2SDavid Xu case mqfstype_parent: 803655291f2SDavid Xu (*vpp)->v_type = VDIR; 804655291f2SDavid Xu break; 805655291f2SDavid Xu case mqfstype_file: 806655291f2SDavid Xu (*vpp)->v_type = VREG; 807655291f2SDavid Xu break; 808655291f2SDavid Xu case mqfstype_symlink: 809655291f2SDavid Xu (*vpp)->v_type = VLNK; 810655291f2SDavid Xu break; 811655291f2SDavid Xu case mqfstype_none: 812655291f2SDavid Xu KASSERT(0, ("mqfs_allocf called for null node\n")); 813655291f2SDavid Xu default: 814655291f2SDavid Xu panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type); 815655291f2SDavid Xu } 816fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 817655291f2SDavid Xu return (0); 818655291f2SDavid Xu } 819655291f2SDavid Xu 820655291f2SDavid Xu /* 821655291f2SDavid Xu * Search a directory entry 822655291f2SDavid Xu */ 823655291f2SDavid Xu static struct mqfs_node * 824adb023aeSJamie Gritton mqfs_search(struct mqfs_node *pd, const char *name, int len, struct ucred *cred) 825655291f2SDavid Xu { 826655291f2SDavid Xu struct mqfs_node *pn; 827adb023aeSJamie Gritton const void *pr_root; 828655291f2SDavid Xu 829fbc48e97SDavid Xu sx_assert(&pd->mn_info->mi_lock, SX_LOCKED); 830adb023aeSJamie Gritton pr_root = cred->cr_prison->pr_root; 831655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 832adb023aeSJamie Gritton /* Only match names within the same prison root directory */ 833adb023aeSJamie Gritton if ((pn->mn_pr_root == NULL || pn->mn_pr_root == pr_root) && 834adb023aeSJamie Gritton strncmp(pn->mn_name, name, len) == 0 && 8351cbae705SEd Schouten pn->mn_name[len] == '\0') 836655291f2SDavid Xu return (pn); 837655291f2SDavid Xu } 838655291f2SDavid Xu return (NULL); 839655291f2SDavid Xu } 840655291f2SDavid Xu 841655291f2SDavid Xu /* 842873fbcd7SRobert Watson * Look up a file or directory. 843655291f2SDavid Xu */ 844655291f2SDavid Xu static int 845655291f2SDavid Xu mqfs_lookupx(struct vop_cachedlookup_args *ap) 846655291f2SDavid Xu { 847655291f2SDavid Xu struct componentname *cnp; 848655291f2SDavid Xu struct vnode *dvp, **vpp; 849655291f2SDavid Xu struct mqfs_node *pd; 850655291f2SDavid Xu struct mqfs_node *pn; 851fbc48e97SDavid Xu struct mqfs_info *mqfs; 852655291f2SDavid Xu int nameiop, flags, error, namelen; 853655291f2SDavid Xu char *pname; 854655291f2SDavid Xu struct thread *td; 855655291f2SDavid Xu 856b4a58fbfSMateusz Guzik td = curthread; 857655291f2SDavid Xu cnp = ap->a_cnp; 858655291f2SDavid Xu vpp = ap->a_vpp; 859655291f2SDavid Xu dvp = ap->a_dvp; 860655291f2SDavid Xu pname = cnp->cn_nameptr; 861655291f2SDavid Xu namelen = cnp->cn_namelen; 862655291f2SDavid Xu flags = cnp->cn_flags; 863655291f2SDavid Xu nameiop = cnp->cn_nameiop; 864655291f2SDavid Xu pd = VTON(dvp); 865655291f2SDavid Xu pn = NULL; 866fbc48e97SDavid Xu mqfs = pd->mn_info; 867655291f2SDavid Xu *vpp = NULLVP; 868655291f2SDavid Xu 869655291f2SDavid Xu if (dvp->v_type != VDIR) 870655291f2SDavid Xu return (ENOTDIR); 871655291f2SDavid Xu 872b4a58fbfSMateusz Guzik error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 873655291f2SDavid Xu if (error) 874655291f2SDavid Xu return (error); 875655291f2SDavid Xu 876655291f2SDavid Xu /* shortcut: check if the name is too long */ 877655291f2SDavid Xu if (cnp->cn_namelen >= MQFS_NAMELEN) 878655291f2SDavid Xu return (ENOENT); 879655291f2SDavid Xu 880655291f2SDavid Xu /* self */ 881655291f2SDavid Xu if (namelen == 1 && pname[0] == '.') { 882655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 883655291f2SDavid Xu return (EINVAL); 884655291f2SDavid Xu pn = pd; 885655291f2SDavid Xu *vpp = dvp; 886655291f2SDavid Xu VREF(dvp); 887655291f2SDavid Xu return (0); 888655291f2SDavid Xu } 889655291f2SDavid Xu 890655291f2SDavid Xu /* parent */ 891655291f2SDavid Xu if (cnp->cn_flags & ISDOTDOT) { 892655291f2SDavid Xu if (dvp->v_vflag & VV_ROOT) 893655291f2SDavid Xu return (EIO); 894655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 895655291f2SDavid Xu return (EINVAL); 896b249ce48SMateusz Guzik VOP_UNLOCK(dvp); 897655291f2SDavid Xu KASSERT(pd->mn_parent, ("non-root directory has no parent")); 898655291f2SDavid Xu pn = pd->mn_parent; 899655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 900cb05b60aSAttilio Rao vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 901655291f2SDavid Xu return (error); 902655291f2SDavid Xu } 903655291f2SDavid Xu 904655291f2SDavid Xu /* named node */ 905fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 906adb023aeSJamie Gritton pn = mqfs_search(pd, pname, namelen, cnp->cn_cred); 907fbc48e97SDavid Xu if (pn != NULL) 908fbc48e97SDavid Xu mqnode_addref(pn); 909fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 910655291f2SDavid Xu 911655291f2SDavid Xu /* found */ 912655291f2SDavid Xu if (pn != NULL) { 913655291f2SDavid Xu /* DELETE */ 914655291f2SDavid Xu if (nameiop == DELETE && (flags & ISLASTCN)) { 915655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 916fbc48e97SDavid Xu if (error) { 917fbc48e97SDavid Xu mqnode_release(pn); 918655291f2SDavid Xu return (error); 919fbc48e97SDavid Xu } 920655291f2SDavid Xu if (*vpp == dvp) { 921655291f2SDavid Xu VREF(dvp); 922655291f2SDavid Xu *vpp = dvp; 923fbc48e97SDavid Xu mqnode_release(pn); 924655291f2SDavid Xu return (0); 925655291f2SDavid Xu } 926655291f2SDavid Xu } 927655291f2SDavid Xu 928655291f2SDavid Xu /* allocate vnode */ 929655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 930fbc48e97SDavid Xu mqnode_release(pn); 931655291f2SDavid Xu if (error == 0 && cnp->cn_flags & MAKEENTRY) 932655291f2SDavid Xu cache_enter(dvp, *vpp, cnp); 933655291f2SDavid Xu return (error); 934655291f2SDavid Xu } 935655291f2SDavid Xu 936655291f2SDavid Xu /* not found */ 937655291f2SDavid Xu 938655291f2SDavid Xu /* will create a new entry in the directory ? */ 939655291f2SDavid Xu if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT) 940655291f2SDavid Xu && (flags & ISLASTCN)) { 941655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 942655291f2SDavid Xu if (error) 943655291f2SDavid Xu return (error); 944655291f2SDavid Xu return (EJUSTRETURN); 945655291f2SDavid Xu } 946655291f2SDavid Xu return (ENOENT); 947655291f2SDavid Xu } 948655291f2SDavid Xu 949655291f2SDavid Xu #if 0 950655291f2SDavid Xu struct vop_lookup_args { 951655291f2SDavid Xu struct vop_generic_args a_gen; 952655291f2SDavid Xu struct vnode *a_dvp; 953655291f2SDavid Xu struct vnode **a_vpp; 954655291f2SDavid Xu struct componentname *a_cnp; 955655291f2SDavid Xu }; 956655291f2SDavid Xu #endif 957655291f2SDavid Xu 958655291f2SDavid Xu /* 959655291f2SDavid Xu * vnode lookup operation 960655291f2SDavid Xu */ 961655291f2SDavid Xu static int 962655291f2SDavid Xu mqfs_lookup(struct vop_cachedlookup_args *ap) 963655291f2SDavid Xu { 964655291f2SDavid Xu int rc; 965655291f2SDavid Xu 966655291f2SDavid Xu rc = mqfs_lookupx(ap); 967655291f2SDavid Xu return (rc); 968655291f2SDavid Xu } 969655291f2SDavid Xu 970655291f2SDavid Xu #if 0 971655291f2SDavid Xu struct vop_create_args { 972655291f2SDavid Xu struct vnode *a_dvp; 973655291f2SDavid Xu struct vnode **a_vpp; 974655291f2SDavid Xu struct componentname *a_cnp; 975655291f2SDavid Xu struct vattr *a_vap; 976655291f2SDavid Xu }; 977655291f2SDavid Xu #endif 978655291f2SDavid Xu 979655291f2SDavid Xu /* 980655291f2SDavid Xu * vnode creation operation 981655291f2SDavid Xu */ 982655291f2SDavid Xu static int 983655291f2SDavid Xu mqfs_create(struct vop_create_args *ap) 984655291f2SDavid Xu { 985655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 986655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 987655291f2SDavid Xu struct mqfs_node *pd; 988655291f2SDavid Xu struct mqfs_node *pn; 989655291f2SDavid Xu struct mqueue *mq; 990655291f2SDavid Xu int error; 991655291f2SDavid Xu 992655291f2SDavid Xu pd = VTON(ap->a_dvp); 993655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 994655291f2SDavid Xu return (ENOTDIR); 995655291f2SDavid Xu mq = mqueue_alloc(NULL); 996655291f2SDavid Xu if (mq == NULL) 997655291f2SDavid Xu return (EAGAIN); 998655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 999ba0360b1SDavid Xu pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen, 1000ba0360b1SDavid Xu cnp->cn_cred, ap->a_vap->va_mode); 1001fbc48e97SDavid Xu if (pn == NULL) { 1002fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 1003ba0360b1SDavid Xu error = ENOSPC; 1004fbc48e97SDavid Xu } else { 1005fbc48e97SDavid Xu mqnode_addref(pn); 1006fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 1007655291f2SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 1008fbc48e97SDavid Xu mqnode_release(pn); 1009ba0360b1SDavid Xu if (error) 1010ba0360b1SDavid Xu mqfs_destroy(pn); 1011ba0360b1SDavid Xu else 1012ba0360b1SDavid Xu pn->mn_data = mq; 1013ba0360b1SDavid Xu } 1014ba0360b1SDavid Xu if (error) 1015ba0360b1SDavid Xu mqueue_free(mq); 1016655291f2SDavid Xu return (error); 1017655291f2SDavid Xu } 1018655291f2SDavid Xu 1019655291f2SDavid Xu /* 1020655291f2SDavid Xu * Remove an entry 1021655291f2SDavid Xu */ 1022655291f2SDavid Xu static 1023655291f2SDavid Xu int do_unlink(struct mqfs_node *pn, struct ucred *ucred) 1024655291f2SDavid Xu { 1025655291f2SDavid Xu struct mqfs_node *parent; 1026655291f2SDavid Xu struct mqfs_vdata *vd; 1027655291f2SDavid Xu int error = 0; 1028655291f2SDavid Xu 1029655291f2SDavid Xu sx_assert(&pn->mn_info->mi_lock, SX_LOCKED); 1030655291f2SDavid Xu 1031655291f2SDavid Xu if (ucred->cr_uid != pn->mn_uid && 1032cc426dd3SMateusz Guzik (error = priv_check_cred(ucred, PRIV_MQ_ADMIN)) != 0) 1033655291f2SDavid Xu error = EACCES; 1034655291f2SDavid Xu else if (!pn->mn_deleted) { 1035655291f2SDavid Xu parent = pn->mn_parent; 1036655291f2SDavid Xu pn->mn_parent = NULL; 1037655291f2SDavid Xu pn->mn_deleted = 1; 1038655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 1039655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 1040655291f2SDavid Xu cache_purge(vd->mv_vnode); 1041655291f2SDavid Xu vhold(vd->mv_vnode); 1042655291f2SDavid Xu taskqueue_enqueue(taskqueue_thread, &vd->mv_task); 1043655291f2SDavid Xu } 1044655291f2SDavid Xu mqnode_release(pn); 1045655291f2SDavid Xu mqnode_release(parent); 1046655291f2SDavid Xu } else 1047655291f2SDavid Xu error = ENOENT; 1048655291f2SDavid Xu return (error); 1049655291f2SDavid Xu } 1050655291f2SDavid Xu 1051655291f2SDavid Xu #if 0 1052655291f2SDavid Xu struct vop_remove_args { 1053655291f2SDavid Xu struct vnode *a_dvp; 1054655291f2SDavid Xu struct vnode *a_vp; 1055655291f2SDavid Xu struct componentname *a_cnp; 1056655291f2SDavid Xu }; 1057655291f2SDavid Xu #endif 1058655291f2SDavid Xu 1059655291f2SDavid Xu /* 1060655291f2SDavid Xu * vnode removal operation 1061655291f2SDavid Xu */ 1062655291f2SDavid Xu static int 1063655291f2SDavid Xu mqfs_remove(struct vop_remove_args *ap) 1064655291f2SDavid Xu { 1065655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1066655291f2SDavid Xu struct mqfs_node *pn; 1067655291f2SDavid Xu int error; 1068655291f2SDavid Xu 1069655291f2SDavid Xu if (ap->a_vp->v_type == VDIR) 1070655291f2SDavid Xu return (EPERM); 1071655291f2SDavid Xu pn = VTON(ap->a_vp); 1072655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1073655291f2SDavid Xu error = do_unlink(pn, ap->a_cnp->cn_cred); 1074655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1075655291f2SDavid Xu return (error); 1076655291f2SDavid Xu } 1077655291f2SDavid Xu 1078655291f2SDavid Xu #if 0 1079655291f2SDavid Xu struct vop_inactive_args { 1080655291f2SDavid Xu struct vnode *a_vp; 1081655291f2SDavid Xu struct thread *a_td; 1082655291f2SDavid Xu }; 1083655291f2SDavid Xu #endif 1084655291f2SDavid Xu 1085655291f2SDavid Xu static int 1086655291f2SDavid Xu mqfs_inactive(struct vop_inactive_args *ap) 1087655291f2SDavid Xu { 1088655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 1089655291f2SDavid Xu 1090655291f2SDavid Xu if (pn->mn_deleted) 1091af6e6b87SEdward Tomasz Napierala vrecycle(ap->a_vp); 1092655291f2SDavid Xu return (0); 1093655291f2SDavid Xu } 1094655291f2SDavid Xu 1095655291f2SDavid Xu #if 0 1096655291f2SDavid Xu struct vop_reclaim_args { 1097655291f2SDavid Xu struct vop_generic_args a_gen; 1098655291f2SDavid Xu struct vnode *a_vp; 1099655291f2SDavid Xu }; 1100655291f2SDavid Xu #endif 1101655291f2SDavid Xu 1102655291f2SDavid Xu static int 1103655291f2SDavid Xu mqfs_reclaim(struct vop_reclaim_args *ap) 1104655291f2SDavid Xu { 1105655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount); 1106655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1107655291f2SDavid Xu struct mqfs_node *pn; 1108655291f2SDavid Xu struct mqfs_vdata *vd; 1109655291f2SDavid Xu 1110655291f2SDavid Xu vd = vp->v_data; 1111655291f2SDavid Xu pn = vd->mv_node; 1112655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1113655291f2SDavid Xu vp->v_data = NULL; 1114655291f2SDavid Xu LIST_REMOVE(vd, mv_link); 1115655291f2SDavid Xu uma_zfree(mvdata_zone, vd); 1116655291f2SDavid Xu mqnode_release(pn); 1117655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1118655291f2SDavid Xu return (0); 1119655291f2SDavid Xu } 1120655291f2SDavid Xu 1121655291f2SDavid Xu #if 0 1122655291f2SDavid Xu struct vop_open_args { 1123655291f2SDavid Xu struct vop_generic_args a_gen; 1124655291f2SDavid Xu struct vnode *a_vp; 1125655291f2SDavid Xu int a_mode; 1126655291f2SDavid Xu struct ucred *a_cred; 1127655291f2SDavid Xu struct thread *a_td; 1128d6da6408SKonstantin Belousov struct file *a_fp; 1129655291f2SDavid Xu }; 1130655291f2SDavid Xu #endif 1131655291f2SDavid Xu 1132655291f2SDavid Xu static int 1133655291f2SDavid Xu mqfs_open(struct vop_open_args *ap) 1134655291f2SDavid Xu { 1135655291f2SDavid Xu return (0); 1136655291f2SDavid Xu } 1137655291f2SDavid Xu 1138655291f2SDavid Xu #if 0 1139655291f2SDavid Xu struct vop_close_args { 1140655291f2SDavid Xu struct vop_generic_args a_gen; 1141655291f2SDavid Xu struct vnode *a_vp; 1142655291f2SDavid Xu int a_fflag; 1143655291f2SDavid Xu struct ucred *a_cred; 1144655291f2SDavid Xu struct thread *a_td; 1145655291f2SDavid Xu }; 1146655291f2SDavid Xu #endif 1147655291f2SDavid Xu 1148655291f2SDavid Xu static int 1149655291f2SDavid Xu mqfs_close(struct vop_close_args *ap) 1150655291f2SDavid Xu { 1151655291f2SDavid Xu return (0); 1152655291f2SDavid Xu } 1153655291f2SDavid Xu 1154655291f2SDavid Xu #if 0 1155655291f2SDavid Xu struct vop_access_args { 1156655291f2SDavid Xu struct vop_generic_args a_gen; 1157655291f2SDavid Xu struct vnode *a_vp; 115815bc6b2bSEdward Tomasz Napierala accmode_t a_accmode; 1159655291f2SDavid Xu struct ucred *a_cred; 1160655291f2SDavid Xu struct thread *a_td; 1161655291f2SDavid Xu }; 1162655291f2SDavid Xu #endif 1163655291f2SDavid Xu 1164655291f2SDavid Xu /* 1165655291f2SDavid Xu * Verify permissions 1166655291f2SDavid Xu */ 1167655291f2SDavid Xu static int 1168655291f2SDavid Xu mqfs_access(struct vop_access_args *ap) 1169655291f2SDavid Xu { 1170655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1171655291f2SDavid Xu struct vattr vattr; 1172655291f2SDavid Xu int error; 1173655291f2SDavid Xu 11740359a12eSAttilio Rao error = VOP_GETATTR(vp, &vattr, ap->a_cred); 1175655291f2SDavid Xu if (error) 1176655291f2SDavid Xu return (error); 1177d292b194SMateusz Guzik error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid, vattr.va_gid, 1178d292b194SMateusz Guzik ap->a_accmode, ap->a_cred); 1179655291f2SDavid Xu return (error); 1180655291f2SDavid Xu } 1181655291f2SDavid Xu 1182655291f2SDavid Xu #if 0 1183655291f2SDavid Xu struct vop_getattr_args { 1184655291f2SDavid Xu struct vop_generic_args a_gen; 1185655291f2SDavid Xu struct vnode *a_vp; 1186655291f2SDavid Xu struct vattr *a_vap; 1187655291f2SDavid Xu struct ucred *a_cred; 1188655291f2SDavid Xu }; 1189655291f2SDavid Xu #endif 1190655291f2SDavid Xu 1191655291f2SDavid Xu /* 1192655291f2SDavid Xu * Get file attributes 1193655291f2SDavid Xu */ 1194655291f2SDavid Xu static int 1195655291f2SDavid Xu mqfs_getattr(struct vop_getattr_args *ap) 1196655291f2SDavid Xu { 1197655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1198655291f2SDavid Xu struct mqfs_node *pn = VTON(vp); 1199655291f2SDavid Xu struct vattr *vap = ap->a_vap; 1200655291f2SDavid Xu int error = 0; 1201655291f2SDavid Xu 1202655291f2SDavid Xu vap->va_type = vp->v_type; 1203655291f2SDavid Xu vap->va_mode = pn->mn_mode; 1204655291f2SDavid Xu vap->va_nlink = 1; 1205655291f2SDavid Xu vap->va_uid = pn->mn_uid; 1206655291f2SDavid Xu vap->va_gid = pn->mn_gid; 1207655291f2SDavid Xu vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1208655291f2SDavid Xu vap->va_fileid = pn->mn_fileno; 1209655291f2SDavid Xu vap->va_size = 0; 1210655291f2SDavid Xu vap->va_blocksize = PAGE_SIZE; 1211655291f2SDavid Xu vap->va_bytes = vap->va_size = 0; 1212655291f2SDavid Xu vap->va_atime = pn->mn_atime; 1213655291f2SDavid Xu vap->va_mtime = pn->mn_mtime; 1214655291f2SDavid Xu vap->va_ctime = pn->mn_ctime; 1215655291f2SDavid Xu vap->va_birthtime = pn->mn_birth; 1216655291f2SDavid Xu vap->va_gen = 0; 1217655291f2SDavid Xu vap->va_flags = 0; 12184c5a20e3SKonstantin Belousov vap->va_rdev = NODEV; 1219655291f2SDavid Xu vap->va_bytes = 0; 1220655291f2SDavid Xu vap->va_filerev = 0; 1221655291f2SDavid Xu return (error); 1222655291f2SDavid Xu } 1223655291f2SDavid Xu 1224655291f2SDavid Xu #if 0 1225655291f2SDavid Xu struct vop_setattr_args { 1226655291f2SDavid Xu struct vop_generic_args a_gen; 1227655291f2SDavid Xu struct vnode *a_vp; 1228655291f2SDavid Xu struct vattr *a_vap; 1229655291f2SDavid Xu struct ucred *a_cred; 1230655291f2SDavid Xu }; 1231655291f2SDavid Xu #endif 1232655291f2SDavid Xu /* 1233655291f2SDavid Xu * Set attributes 1234655291f2SDavid Xu */ 1235655291f2SDavid Xu static int 1236655291f2SDavid Xu mqfs_setattr(struct vop_setattr_args *ap) 1237655291f2SDavid Xu { 1238655291f2SDavid Xu struct mqfs_node *pn; 1239655291f2SDavid Xu struct vattr *vap; 1240655291f2SDavid Xu struct vnode *vp; 12410359a12eSAttilio Rao struct thread *td; 1242655291f2SDavid Xu int c, error; 1243655291f2SDavid Xu uid_t uid; 1244655291f2SDavid Xu gid_t gid; 1245655291f2SDavid Xu 12460359a12eSAttilio Rao td = curthread; 1247655291f2SDavid Xu vap = ap->a_vap; 1248655291f2SDavid Xu vp = ap->a_vp; 1249655291f2SDavid Xu if ((vap->va_type != VNON) || 1250655291f2SDavid Xu (vap->va_nlink != VNOVAL) || 1251655291f2SDavid Xu (vap->va_fsid != VNOVAL) || 1252655291f2SDavid Xu (vap->va_fileid != VNOVAL) || 1253655291f2SDavid Xu (vap->va_blocksize != VNOVAL) || 1254655291f2SDavid Xu (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1255655291f2SDavid Xu (vap->va_rdev != VNOVAL) || 1256655291f2SDavid Xu ((int)vap->va_bytes != VNOVAL) || 1257655291f2SDavid Xu (vap->va_gen != VNOVAL)) { 1258655291f2SDavid Xu return (EINVAL); 1259655291f2SDavid Xu } 1260655291f2SDavid Xu 1261655291f2SDavid Xu pn = VTON(vp); 1262655291f2SDavid Xu 1263655291f2SDavid Xu error = c = 0; 1264655291f2SDavid Xu if (vap->va_uid == (uid_t)VNOVAL) 1265655291f2SDavid Xu uid = pn->mn_uid; 1266655291f2SDavid Xu else 1267655291f2SDavid Xu uid = vap->va_uid; 1268655291f2SDavid Xu if (vap->va_gid == (gid_t)VNOVAL) 1269655291f2SDavid Xu gid = pn->mn_gid; 1270655291f2SDavid Xu else 1271655291f2SDavid Xu gid = vap->va_gid; 1272655291f2SDavid Xu 1273655291f2SDavid Xu if (uid != pn->mn_uid || gid != pn->mn_gid) { 1274655291f2SDavid Xu /* 1275655291f2SDavid Xu * To modify the ownership of a file, must possess VADMIN 1276655291f2SDavid Xu * for that file. 1277655291f2SDavid Xu */ 12780359a12eSAttilio Rao if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td))) 1279655291f2SDavid Xu return (error); 1280acd3428bSRobert Watson 1281acd3428bSRobert Watson /* 1282acd3428bSRobert Watson * XXXRW: Why is there a privilege check here: shouldn't the 1283acd3428bSRobert Watson * check in VOP_ACCESS() be enough? Also, are the group bits 1284acd3428bSRobert Watson * below definitely right? 1285acd3428bSRobert Watson */ 1286655291f2SDavid Xu if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid || 1287655291f2SDavid Xu (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) && 12880359a12eSAttilio Rao (error = priv_check(td, PRIV_MQ_ADMIN)) != 0) 1289655291f2SDavid Xu return (error); 1290655291f2SDavid Xu pn->mn_uid = uid; 1291655291f2SDavid Xu pn->mn_gid = gid; 1292655291f2SDavid Xu c = 1; 1293655291f2SDavid Xu } 1294655291f2SDavid Xu 1295655291f2SDavid Xu if (vap->va_mode != (mode_t)VNOVAL) { 1296655291f2SDavid Xu if ((ap->a_cred->cr_uid != pn->mn_uid) && 12970359a12eSAttilio Rao (error = priv_check(td, PRIV_MQ_ADMIN))) 1298655291f2SDavid Xu return (error); 1299655291f2SDavid Xu pn->mn_mode = vap->va_mode; 1300655291f2SDavid Xu c = 1; 1301655291f2SDavid Xu } 1302655291f2SDavid Xu 1303655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1304655291f2SDavid Xu /* See the comment in ufs_vnops::ufs_setattr(). */ 13050359a12eSAttilio Rao if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) && 1306655291f2SDavid Xu ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 13070359a12eSAttilio Rao (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td)))) 1308655291f2SDavid Xu return (error); 1309655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL) { 1310655291f2SDavid Xu pn->mn_atime = vap->va_atime; 1311655291f2SDavid Xu } 1312655291f2SDavid Xu if (vap->va_mtime.tv_sec != VNOVAL) { 1313655291f2SDavid Xu pn->mn_mtime = vap->va_mtime; 1314655291f2SDavid Xu } 1315655291f2SDavid Xu c = 1; 1316655291f2SDavid Xu } 1317655291f2SDavid Xu if (c) { 1318655291f2SDavid Xu vfs_timestamp(&pn->mn_ctime); 1319655291f2SDavid Xu } 1320655291f2SDavid Xu return (0); 1321655291f2SDavid Xu } 1322655291f2SDavid Xu 1323655291f2SDavid Xu #if 0 1324655291f2SDavid Xu struct vop_read_args { 1325655291f2SDavid Xu struct vop_generic_args a_gen; 1326655291f2SDavid Xu struct vnode *a_vp; 1327655291f2SDavid Xu struct uio *a_uio; 1328655291f2SDavid Xu int a_ioflag; 1329655291f2SDavid Xu struct ucred *a_cred; 1330655291f2SDavid Xu }; 1331655291f2SDavid Xu #endif 1332655291f2SDavid Xu 1333655291f2SDavid Xu /* 1334655291f2SDavid Xu * Read from a file 1335655291f2SDavid Xu */ 1336655291f2SDavid Xu static int 1337655291f2SDavid Xu mqfs_read(struct vop_read_args *ap) 1338655291f2SDavid Xu { 1339655291f2SDavid Xu char buf[80]; 1340655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1341655291f2SDavid Xu struct uio *uio = ap->a_uio; 1342655291f2SDavid Xu struct mqueue *mq; 1343655291f2SDavid Xu int len, error; 1344655291f2SDavid Xu 1345655291f2SDavid Xu if (vp->v_type != VREG) 1346655291f2SDavid Xu return (EINVAL); 1347655291f2SDavid Xu 1348655291f2SDavid Xu mq = VTOMQ(vp); 1349655291f2SDavid Xu snprintf(buf, sizeof(buf), 1350655291f2SDavid Xu "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n", 1351655291f2SDavid Xu mq->mq_totalbytes, 1352655291f2SDavid Xu mq->mq_maxmsg, 1353655291f2SDavid Xu mq->mq_curmsgs, 1354655291f2SDavid Xu mq->mq_msgsize); 1355655291f2SDavid Xu buf[sizeof(buf)-1] = '\0'; 1356655291f2SDavid Xu len = strlen(buf); 1357655291f2SDavid Xu error = uiomove_frombuf(buf, len, uio); 1358655291f2SDavid Xu return (error); 1359655291f2SDavid Xu } 1360655291f2SDavid Xu 1361655291f2SDavid Xu #if 0 1362655291f2SDavid Xu struct vop_readdir_args { 1363655291f2SDavid Xu struct vop_generic_args a_gen; 1364655291f2SDavid Xu struct vnode *a_vp; 1365655291f2SDavid Xu struct uio *a_uio; 1366655291f2SDavid Xu struct ucred *a_cred; 1367655291f2SDavid Xu int *a_eofflag; 1368655291f2SDavid Xu int *a_ncookies; 1369b214fcceSAlan Somers uint64_t **a_cookies; 1370655291f2SDavid Xu }; 1371655291f2SDavid Xu #endif 1372655291f2SDavid Xu 1373655291f2SDavid Xu /* 1374655291f2SDavid Xu * Return directory entries. 1375655291f2SDavid Xu */ 1376655291f2SDavid Xu static int 1377655291f2SDavid Xu mqfs_readdir(struct vop_readdir_args *ap) 1378655291f2SDavid Xu { 1379655291f2SDavid Xu struct vnode *vp; 1380655291f2SDavid Xu struct mqfs_info *mi; 1381655291f2SDavid Xu struct mqfs_node *pd; 1382655291f2SDavid Xu struct mqfs_node *pn; 1383655291f2SDavid Xu struct dirent entry; 1384655291f2SDavid Xu struct uio *uio; 1385adb023aeSJamie Gritton const void *pr_root; 1386655291f2SDavid Xu int *tmp_ncookies = NULL; 1387655291f2SDavid Xu off_t offset; 1388655291f2SDavid Xu int error, i; 1389655291f2SDavid Xu 1390655291f2SDavid Xu vp = ap->a_vp; 1391655291f2SDavid Xu mi = VFSTOMQFS(vp->v_mount); 1392655291f2SDavid Xu pd = VTON(vp); 1393655291f2SDavid Xu uio = ap->a_uio; 1394655291f2SDavid Xu 1395655291f2SDavid Xu if (vp->v_type != VDIR) 1396655291f2SDavid Xu return (ENOTDIR); 1397655291f2SDavid Xu 1398655291f2SDavid Xu if (uio->uio_offset < 0) 1399655291f2SDavid Xu return (EINVAL); 1400655291f2SDavid Xu 1401655291f2SDavid Xu if (ap->a_ncookies != NULL) { 1402655291f2SDavid Xu tmp_ncookies = ap->a_ncookies; 1403655291f2SDavid Xu *ap->a_ncookies = 0; 1404655291f2SDavid Xu ap->a_ncookies = NULL; 1405655291f2SDavid Xu } 1406655291f2SDavid Xu 1407655291f2SDavid Xu error = 0; 1408655291f2SDavid Xu offset = 0; 1409655291f2SDavid Xu 1410adb023aeSJamie Gritton pr_root = ap->a_cred->cr_prison->pr_root; 1411655291f2SDavid Xu sx_xlock(&mi->mi_lock); 1412655291f2SDavid Xu 1413655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 1414655291f2SDavid Xu entry.d_reclen = sizeof(entry); 141544c16975SJamie Gritton 1416adb023aeSJamie Gritton /* 1417adb023aeSJamie Gritton * Only show names within the same prison root directory 1418adb023aeSJamie Gritton * (or not associated with a prison, e.g. "." and ".."). 1419adb023aeSJamie Gritton */ 1420adb023aeSJamie Gritton if (pn->mn_pr_root != NULL && pn->mn_pr_root != pr_root) 1421adb023aeSJamie Gritton continue; 1422655291f2SDavid Xu if (!pn->mn_fileno) 1423655291f2SDavid Xu mqfs_fileno_alloc(mi, pn); 1424655291f2SDavid Xu entry.d_fileno = pn->mn_fileno; 142590f580b9SMark Johnston entry.d_off = offset + entry.d_reclen; 1426655291f2SDavid Xu for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i) 1427655291f2SDavid Xu entry.d_name[i] = pn->mn_name[i]; 1428655291f2SDavid Xu entry.d_namlen = i; 1429655291f2SDavid Xu switch (pn->mn_type) { 1430655291f2SDavid Xu case mqfstype_root: 1431655291f2SDavid Xu case mqfstype_dir: 1432655291f2SDavid Xu case mqfstype_this: 1433655291f2SDavid Xu case mqfstype_parent: 1434655291f2SDavid Xu entry.d_type = DT_DIR; 1435655291f2SDavid Xu break; 1436655291f2SDavid Xu case mqfstype_file: 1437655291f2SDavid Xu entry.d_type = DT_REG; 1438655291f2SDavid Xu break; 1439655291f2SDavid Xu case mqfstype_symlink: 1440655291f2SDavid Xu entry.d_type = DT_LNK; 1441655291f2SDavid Xu break; 1442655291f2SDavid Xu default: 1443655291f2SDavid Xu panic("%s has unexpected node type: %d", pn->mn_name, 1444655291f2SDavid Xu pn->mn_type); 1445655291f2SDavid Xu } 14466d2e2df7SMark Johnston dirent_terminate(&entry); 1447655291f2SDavid Xu if (entry.d_reclen > uio->uio_resid) 1448655291f2SDavid Xu break; 1449655291f2SDavid Xu if (offset >= uio->uio_offset) { 1450655291f2SDavid Xu error = vfs_read_dirent(ap, &entry, offset); 1451655291f2SDavid Xu if (error) 1452655291f2SDavid Xu break; 1453655291f2SDavid Xu } 1454655291f2SDavid Xu offset += entry.d_reclen; 1455655291f2SDavid Xu } 1456655291f2SDavid Xu sx_xunlock(&mi->mi_lock); 1457655291f2SDavid Xu 1458655291f2SDavid Xu uio->uio_offset = offset; 1459655291f2SDavid Xu 1460655291f2SDavid Xu if (tmp_ncookies != NULL) 1461655291f2SDavid Xu ap->a_ncookies = tmp_ncookies; 1462655291f2SDavid Xu 1463655291f2SDavid Xu return (error); 1464655291f2SDavid Xu } 1465655291f2SDavid Xu 1466655291f2SDavid Xu #ifdef notyet 1467655291f2SDavid Xu 1468655291f2SDavid Xu #if 0 1469655291f2SDavid Xu struct vop_mkdir_args { 1470655291f2SDavid Xu struct vnode *a_dvp; 1471655291f2SDavid Xu struvt vnode **a_vpp; 1472655291f2SDavid Xu struvt componentname *a_cnp; 1473655291f2SDavid Xu struct vattr *a_vap; 1474655291f2SDavid Xu }; 1475655291f2SDavid Xu #endif 1476655291f2SDavid Xu 1477655291f2SDavid Xu /* 1478655291f2SDavid Xu * Create a directory. 1479655291f2SDavid Xu */ 1480655291f2SDavid Xu static int 1481655291f2SDavid Xu mqfs_mkdir(struct vop_mkdir_args *ap) 1482655291f2SDavid Xu { 1483655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1484655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 1485655291f2SDavid Xu struct mqfs_node *pd = VTON(ap->a_dvp); 1486655291f2SDavid Xu struct mqfs_node *pn; 1487655291f2SDavid Xu int error; 1488655291f2SDavid Xu 1489655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 1490655291f2SDavid Xu return (ENOTDIR); 1491655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1492ba0360b1SDavid Xu pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen, 1493ba0360b1SDavid Xu ap->a_vap->cn_cred, ap->a_vap->va_mode); 1494fbc48e97SDavid Xu if (pn != NULL) 1495fbc48e97SDavid Xu mqnode_addref(pn); 1496655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1497fbc48e97SDavid Xu if (pn == NULL) { 1498fbc48e97SDavid Xu error = ENOSPC; 1499fbc48e97SDavid Xu } else { 1500fbc48e97SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 1501fbc48e97SDavid Xu mqnode_release(pn); 1502fbc48e97SDavid Xu } 1503655291f2SDavid Xu return (error); 1504655291f2SDavid Xu } 1505655291f2SDavid Xu 1506655291f2SDavid Xu #if 0 1507655291f2SDavid Xu struct vop_rmdir_args { 1508655291f2SDavid Xu struct vnode *a_dvp; 1509655291f2SDavid Xu struct vnode *a_vp; 1510655291f2SDavid Xu struct componentname *a_cnp; 1511655291f2SDavid Xu }; 1512655291f2SDavid Xu #endif 1513655291f2SDavid Xu 1514655291f2SDavid Xu /* 1515655291f2SDavid Xu * Remove a directory. 1516655291f2SDavid Xu */ 1517655291f2SDavid Xu static int 1518655291f2SDavid Xu mqfs_rmdir(struct vop_rmdir_args *ap) 1519655291f2SDavid Xu { 1520655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1521655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 1522655291f2SDavid Xu struct mqfs_node *pt; 1523655291f2SDavid Xu 1524655291f2SDavid Xu if (pn->mn_type != mqfstype_dir) 1525655291f2SDavid Xu return (ENOTDIR); 1526655291f2SDavid Xu 1527655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1528655291f2SDavid Xu if (pn->mn_deleted) { 1529655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1530655291f2SDavid Xu return (ENOENT); 1531655291f2SDavid Xu } 1532655291f2SDavid Xu 1533655291f2SDavid Xu pt = LIST_FIRST(&pn->mn_children); 1534655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1535655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1536655291f2SDavid Xu if (pt != NULL) { 1537655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1538655291f2SDavid Xu return (ENOTEMPTY); 1539655291f2SDavid Xu } 1540655291f2SDavid Xu pt = pn->mn_parent; 1541655291f2SDavid Xu pn->mn_parent = NULL; 1542655291f2SDavid Xu pn->mn_deleted = 1; 1543655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 1544655291f2SDavid Xu mqnode_release(pn); 1545655291f2SDavid Xu mqnode_release(pt); 1546655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1547655291f2SDavid Xu cache_purge(ap->a_vp); 1548655291f2SDavid Xu return (0); 1549655291f2SDavid Xu } 1550655291f2SDavid Xu 1551655291f2SDavid Xu #endif /* notyet */ 1552655291f2SDavid Xu 1553adb023aeSJamie Gritton /* 1554add14c83SJamie Gritton * See if this prison root is obsolete, and clean up associated queues if it is. 1555adb023aeSJamie Gritton */ 1556adb023aeSJamie Gritton static int 1557add14c83SJamie Gritton mqfs_prison_remove(void *obj, void *data __unused) 1558adb023aeSJamie Gritton { 1559add14c83SJamie Gritton const struct prison *pr = obj; 156025c2c952SJamie Gritton struct prison *tpr; 1561adb023aeSJamie Gritton struct mqfs_node *pn, *tpn; 1562f7496dcaSJamie Gritton struct vnode *pr_root; 1563adb023aeSJamie Gritton 1564f7496dcaSJamie Gritton pr_root = pr->pr_root; 1565f7496dcaSJamie Gritton if (pr->pr_parent->pr_root == pr_root) 1566f7496dcaSJamie Gritton return (0); 1567add14c83SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1568f7496dcaSJamie Gritton if (tpr != pr && tpr->pr_root == pr_root) 1569f7496dcaSJamie Gritton return (0); 1570adb023aeSJamie Gritton } 1571adb023aeSJamie Gritton /* 1572adb023aeSJamie Gritton * No jails are rooted in this directory anymore, 1573adb023aeSJamie Gritton * so no queues should be either. 1574adb023aeSJamie Gritton */ 1575adb023aeSJamie Gritton sx_xlock(&mqfs_data.mi_lock); 1576adb023aeSJamie Gritton LIST_FOREACH_SAFE(pn, &mqfs_data.mi_root->mn_children, 1577adb023aeSJamie Gritton mn_sibling, tpn) { 1578f7496dcaSJamie Gritton if (pn->mn_pr_root == pr_root) 1579adb023aeSJamie Gritton (void)do_unlink(pn, curthread->td_ucred); 1580adb023aeSJamie Gritton } 1581adb023aeSJamie Gritton sx_xunlock(&mqfs_data.mi_lock); 1582add14c83SJamie Gritton return (0); 1583adb023aeSJamie Gritton } 1584adb023aeSJamie Gritton 1585655291f2SDavid Xu /* 1586655291f2SDavid Xu * Allocate a message queue 1587655291f2SDavid Xu */ 1588655291f2SDavid Xu static struct mqueue * 1589655291f2SDavid Xu mqueue_alloc(const struct mq_attr *attr) 1590655291f2SDavid Xu { 1591655291f2SDavid Xu struct mqueue *mq; 1592655291f2SDavid Xu 1593655291f2SDavid Xu if (curmq >= maxmq) 1594655291f2SDavid Xu return (NULL); 1595655291f2SDavid Xu mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO); 1596655291f2SDavid Xu TAILQ_INIT(&mq->mq_msgq); 1597655291f2SDavid Xu if (attr != NULL) { 1598655291f2SDavid Xu mq->mq_maxmsg = attr->mq_maxmsg; 1599655291f2SDavid Xu mq->mq_msgsize = attr->mq_msgsize; 1600655291f2SDavid Xu } else { 1601655291f2SDavid Xu mq->mq_maxmsg = default_maxmsg; 1602655291f2SDavid Xu mq->mq_msgsize = default_msgsize; 1603655291f2SDavid Xu } 1604b042e976SDavid Xu mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF); 1605d8b0556cSKonstantin Belousov knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex); 1606d8b0556cSKonstantin Belousov knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex); 1607655291f2SDavid Xu atomic_add_int(&curmq, 1); 1608655291f2SDavid Xu return (mq); 1609655291f2SDavid Xu } 1610655291f2SDavid Xu 1611655291f2SDavid Xu /* 1612655291f2SDavid Xu * Destroy a message queue 1613655291f2SDavid Xu */ 1614655291f2SDavid Xu static void 1615655291f2SDavid Xu mqueue_free(struct mqueue *mq) 1616655291f2SDavid Xu { 1617655291f2SDavid Xu struct mqueue_msg *msg; 1618655291f2SDavid Xu 1619655291f2SDavid Xu while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) { 1620655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link); 16211ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1622655291f2SDavid Xu } 1623655291f2SDavid Xu 1624655291f2SDavid Xu mtx_destroy(&mq->mq_mutex); 16256aba400aSAttilio Rao seldrain(&mq->mq_rsel); 16266aba400aSAttilio Rao seldrain(&mq->mq_wsel); 1627655291f2SDavid Xu knlist_destroy(&mq->mq_rsel.si_note); 1628655291f2SDavid Xu knlist_destroy(&mq->mq_wsel.si_note); 1629655291f2SDavid Xu uma_zfree(mqueue_zone, mq); 1630655291f2SDavid Xu atomic_add_int(&curmq, -1); 1631655291f2SDavid Xu } 1632655291f2SDavid Xu 1633655291f2SDavid Xu /* 1634655291f2SDavid Xu * Load a message from user space 1635655291f2SDavid Xu */ 1636655291f2SDavid Xu static struct mqueue_msg * 1637655291f2SDavid Xu mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio) 1638655291f2SDavid Xu { 1639655291f2SDavid Xu struct mqueue_msg *msg; 1640655291f2SDavid Xu size_t len; 1641655291f2SDavid Xu int error; 1642655291f2SDavid Xu 1643655291f2SDavid Xu len = sizeof(struct mqueue_msg) + msg_size; 16441ede983cSDag-Erling Smørgrav msg = malloc(len, M_MQUEUEDATA, M_WAITOK); 1645655291f2SDavid Xu error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg), 1646655291f2SDavid Xu msg_size); 1647655291f2SDavid Xu if (error) { 16481ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1649655291f2SDavid Xu msg = NULL; 1650655291f2SDavid Xu } else { 1651655291f2SDavid Xu msg->msg_size = msg_size; 1652655291f2SDavid Xu msg->msg_prio = msg_prio; 1653655291f2SDavid Xu } 1654655291f2SDavid Xu return (msg); 1655655291f2SDavid Xu } 1656655291f2SDavid Xu 1657655291f2SDavid Xu /* 1658655291f2SDavid Xu * Save a message to user space 1659655291f2SDavid Xu */ 1660655291f2SDavid Xu static int 1661655291f2SDavid Xu mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio) 1662655291f2SDavid Xu { 1663655291f2SDavid Xu int error; 1664655291f2SDavid Xu 1665655291f2SDavid Xu error = copyout(((char *)msg) + sizeof(*msg), msg_ptr, 1666655291f2SDavid Xu msg->msg_size); 1667b2f92ef9SDavid Xu if (error == 0 && msg_prio != NULL) 1668655291f2SDavid Xu error = copyout(&msg->msg_prio, msg_prio, sizeof(int)); 1669655291f2SDavid Xu return (error); 1670655291f2SDavid Xu } 1671655291f2SDavid Xu 1672655291f2SDavid Xu /* 1673655291f2SDavid Xu * Free a message's memory 1674655291f2SDavid Xu */ 1675655291f2SDavid Xu static __inline void 1676655291f2SDavid Xu mqueue_freemsg(struct mqueue_msg *msg) 1677655291f2SDavid Xu { 16781ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1679655291f2SDavid Xu } 1680655291f2SDavid Xu 1681655291f2SDavid Xu /* 1682655291f2SDavid Xu * Send a message. if waitok is false, thread will not be 1683655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1684655291f2SDavid Xu * time will be checked. 1685655291f2SDavid Xu */ 1686655291f2SDavid Xu int 1687655291f2SDavid Xu mqueue_send(struct mqueue *mq, const char *msg_ptr, 1688655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 1689655291f2SDavid Xu const struct timespec *abs_timeout) 1690655291f2SDavid Xu { 1691655291f2SDavid Xu struct mqueue_msg *msg; 1692afde2b65SKonstantin Belousov struct timespec ts, ts2; 1693655291f2SDavid Xu struct timeval tv; 1694655291f2SDavid Xu int error; 1695655291f2SDavid Xu 1696a6de716dSDavid Xu if (msg_prio >= MQ_PRIO_MAX) 1697a6de716dSDavid Xu return (EINVAL); 1698655291f2SDavid Xu if (msg_len > mq->mq_msgsize) 1699655291f2SDavid Xu return (EMSGSIZE); 1700655291f2SDavid Xu msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio); 1701655291f2SDavid Xu if (msg == NULL) 1702655291f2SDavid Xu return (EFAULT); 1703655291f2SDavid Xu 1704655291f2SDavid Xu /* O_NONBLOCK case */ 1705655291f2SDavid Xu if (!waitok) { 1706655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1707655291f2SDavid Xu if (error) 1708655291f2SDavid Xu goto bad; 1709655291f2SDavid Xu return (0); 1710655291f2SDavid Xu } 1711655291f2SDavid Xu 1712655291f2SDavid Xu /* we allow a null timeout (wait forever) */ 1713655291f2SDavid Xu if (abs_timeout == NULL) { 1714655291f2SDavid Xu error = _mqueue_send(mq, msg, 0); 1715655291f2SDavid Xu if (error) 1716655291f2SDavid Xu goto bad; 1717655291f2SDavid Xu return (0); 1718655291f2SDavid Xu } 1719655291f2SDavid Xu 1720655291f2SDavid Xu /* send it before checking time */ 1721655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1722655291f2SDavid Xu if (error == 0) 1723655291f2SDavid Xu return (0); 1724655291f2SDavid Xu 1725655291f2SDavid Xu if (error != EAGAIN) 1726655291f2SDavid Xu goto bad; 1727655291f2SDavid Xu 1728afde2b65SKonstantin Belousov if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) { 1729655291f2SDavid Xu error = EINVAL; 1730655291f2SDavid Xu goto bad; 1731655291f2SDavid Xu } 1732655291f2SDavid Xu for (;;) { 1733a6de716dSDavid Xu getnanotime(&ts); 17346040822cSAlan Somers timespecsub(abs_timeout, &ts, &ts2); 1735655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1736655291f2SDavid Xu error = ETIMEDOUT; 1737655291f2SDavid Xu break; 1738655291f2SDavid Xu } 1739655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1740655291f2SDavid Xu error = _mqueue_send(mq, msg, tvtohz(&tv)); 1741655291f2SDavid Xu if (error != ETIMEDOUT) 1742655291f2SDavid Xu break; 1743655291f2SDavid Xu } 1744655291f2SDavid Xu if (error == 0) 1745655291f2SDavid Xu return (0); 1746655291f2SDavid Xu bad: 1747655291f2SDavid Xu mqueue_freemsg(msg); 1748655291f2SDavid Xu return (error); 1749655291f2SDavid Xu } 1750655291f2SDavid Xu 1751655291f2SDavid Xu /* 1752655291f2SDavid Xu * Common routine to send a message 1753655291f2SDavid Xu */ 1754655291f2SDavid Xu static int 1755655291f2SDavid Xu _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo) 1756655291f2SDavid Xu { 1757655291f2SDavid Xu struct mqueue_msg *msg2; 1758655291f2SDavid Xu int error = 0; 1759655291f2SDavid Xu 1760655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1761655291f2SDavid Xu while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) { 1762655291f2SDavid Xu if (timo < 0) { 1763655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1764655291f2SDavid Xu return (EAGAIN); 1765655291f2SDavid Xu } 1766655291f2SDavid Xu mq->mq_senders++; 1767655291f2SDavid Xu error = msleep(&mq->mq_senders, &mq->mq_mutex, 17680f180a7cSJohn Baldwin PCATCH, "mqsend", timo); 1769655291f2SDavid Xu mq->mq_senders--; 1770655291f2SDavid Xu if (error == EAGAIN) 1771655291f2SDavid Xu error = ETIMEDOUT; 1772655291f2SDavid Xu } 1773655291f2SDavid Xu if (mq->mq_curmsgs >= mq->mq_maxmsg) { 1774655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1775655291f2SDavid Xu return (error); 1776655291f2SDavid Xu } 1777655291f2SDavid Xu error = 0; 1778655291f2SDavid Xu if (TAILQ_EMPTY(&mq->mq_msgq)) { 1779655291f2SDavid Xu TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link); 1780655291f2SDavid Xu } else { 1781655291f2SDavid Xu if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) { 1782655291f2SDavid Xu TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link); 1783655291f2SDavid Xu } else { 1784655291f2SDavid Xu TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) { 1785655291f2SDavid Xu if (msg2->msg_prio < msg->msg_prio) 1786655291f2SDavid Xu break; 1787655291f2SDavid Xu } 1788655291f2SDavid Xu TAILQ_INSERT_BEFORE(msg2, msg, msg_link); 1789655291f2SDavid Xu } 1790655291f2SDavid Xu } 1791655291f2SDavid Xu mq->mq_curmsgs++; 1792655291f2SDavid Xu mq->mq_totalbytes += msg->msg_size; 1793655291f2SDavid Xu if (mq->mq_receivers) 1794655291f2SDavid Xu wakeup_one(&mq->mq_receivers); 1795655291f2SDavid Xu else if (mq->mq_notifier != NULL) 1796655291f2SDavid Xu mqueue_send_notification(mq); 1797655291f2SDavid Xu if (mq->mq_flags & MQ_RSEL) { 1798655291f2SDavid Xu mq->mq_flags &= ~MQ_RSEL; 179903f70aecSDavid Xu selwakeup(&mq->mq_rsel); 1800655291f2SDavid Xu } 1801655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_rsel.si_note, 0); 1802655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1803655291f2SDavid Xu return (0); 1804655291f2SDavid Xu } 1805655291f2SDavid Xu 1806655291f2SDavid Xu /* 1807655291f2SDavid Xu * Send realtime a signal to process which registered itself 1808655291f2SDavid Xu * successfully by mq_notify. 1809655291f2SDavid Xu */ 1810655291f2SDavid Xu static void 1811655291f2SDavid Xu mqueue_send_notification(struct mqueue *mq) 1812655291f2SDavid Xu { 1813b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1814cf7d9a8cSDavid Xu struct thread *td; 1815b2f92ef9SDavid Xu struct proc *p; 1816cf7d9a8cSDavid Xu int error; 1817655291f2SDavid Xu 1818655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 1819b2f92ef9SDavid Xu nt = mq->mq_notifier; 1820052ea11cSDavid Xu if (nt->nt_sigev.sigev_notify != SIGEV_NONE) { 1821b2f92ef9SDavid Xu p = nt->nt_proc; 1822cf7d9a8cSDavid Xu error = sigev_findtd(p, &nt->nt_sigev, &td); 1823cf7d9a8cSDavid Xu if (error) { 1824cf7d9a8cSDavid Xu mq->mq_notifier = NULL; 1825cf7d9a8cSDavid Xu return; 1826cf7d9a8cSDavid Xu } 1827cf7d9a8cSDavid Xu if (!KSI_ONQ(&nt->nt_ksi)) { 1828cf7d9a8cSDavid Xu ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev); 1829cf7d9a8cSDavid Xu tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi); 1830cf7d9a8cSDavid Xu } 1831b2f92ef9SDavid Xu PROC_UNLOCK(p); 1832052ea11cSDavid Xu } 1833655291f2SDavid Xu mq->mq_notifier = NULL; 1834655291f2SDavid Xu } 1835655291f2SDavid Xu 1836655291f2SDavid Xu /* 1837655291f2SDavid Xu * Get a message. if waitok is false, thread will not be 1838655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1839655291f2SDavid Xu * time will be checked. 1840655291f2SDavid Xu */ 1841655291f2SDavid Xu int 1842655291f2SDavid Xu mqueue_receive(struct mqueue *mq, char *msg_ptr, 1843655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 1844655291f2SDavid Xu const struct timespec *abs_timeout) 1845655291f2SDavid Xu { 1846655291f2SDavid Xu struct mqueue_msg *msg; 1847afde2b65SKonstantin Belousov struct timespec ts, ts2; 1848655291f2SDavid Xu struct timeval tv; 1849655291f2SDavid Xu int error; 1850655291f2SDavid Xu 1851655291f2SDavid Xu if (msg_len < mq->mq_msgsize) 1852655291f2SDavid Xu return (EMSGSIZE); 1853655291f2SDavid Xu 1854655291f2SDavid Xu /* O_NONBLOCK case */ 1855655291f2SDavid Xu if (!waitok) { 1856655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1857655291f2SDavid Xu if (error) 1858655291f2SDavid Xu return (error); 1859655291f2SDavid Xu goto received; 1860655291f2SDavid Xu } 1861655291f2SDavid Xu 1862655291f2SDavid Xu /* we allow a null timeout (wait forever). */ 1863655291f2SDavid Xu if (abs_timeout == NULL) { 1864655291f2SDavid Xu error = _mqueue_recv(mq, &msg, 0); 1865655291f2SDavid Xu if (error) 1866655291f2SDavid Xu return (error); 1867655291f2SDavid Xu goto received; 1868655291f2SDavid Xu } 1869655291f2SDavid Xu 1870655291f2SDavid Xu /* try to get a message before checking time */ 1871655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1872655291f2SDavid Xu if (error == 0) 1873655291f2SDavid Xu goto received; 1874655291f2SDavid Xu 1875655291f2SDavid Xu if (error != EAGAIN) 1876655291f2SDavid Xu return (error); 1877655291f2SDavid Xu 1878afde2b65SKonstantin Belousov if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) { 1879655291f2SDavid Xu error = EINVAL; 1880655291f2SDavid Xu return (error); 1881655291f2SDavid Xu } 1882655291f2SDavid Xu 1883655291f2SDavid Xu for (;;) { 1884a6de716dSDavid Xu getnanotime(&ts); 18856040822cSAlan Somers timespecsub(abs_timeout, &ts, &ts2); 1886655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1887655291f2SDavid Xu error = ETIMEDOUT; 1888655291f2SDavid Xu return (error); 1889655291f2SDavid Xu } 1890655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1891655291f2SDavid Xu error = _mqueue_recv(mq, &msg, tvtohz(&tv)); 1892655291f2SDavid Xu if (error == 0) 1893655291f2SDavid Xu break; 1894655291f2SDavid Xu if (error != ETIMEDOUT) 1895655291f2SDavid Xu return (error); 1896655291f2SDavid Xu } 1897655291f2SDavid Xu 1898655291f2SDavid Xu received: 1899655291f2SDavid Xu error = mqueue_savemsg(msg, msg_ptr, msg_prio); 1900655291f2SDavid Xu if (error == 0) { 1901655291f2SDavid Xu curthread->td_retval[0] = msg->msg_size; 1902655291f2SDavid Xu curthread->td_retval[1] = 0; 1903655291f2SDavid Xu } 1904655291f2SDavid Xu mqueue_freemsg(msg); 1905655291f2SDavid Xu return (error); 1906655291f2SDavid Xu } 1907655291f2SDavid Xu 1908655291f2SDavid Xu /* 1909655291f2SDavid Xu * Common routine to receive a message 1910655291f2SDavid Xu */ 1911655291f2SDavid Xu static int 1912655291f2SDavid Xu _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo) 1913655291f2SDavid Xu { 1914655291f2SDavid Xu int error = 0; 1915655291f2SDavid Xu 1916655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1917655291f2SDavid Xu while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) { 1918655291f2SDavid Xu if (timo < 0) { 1919655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1920655291f2SDavid Xu return (EAGAIN); 1921655291f2SDavid Xu } 1922655291f2SDavid Xu mq->mq_receivers++; 1923655291f2SDavid Xu error = msleep(&mq->mq_receivers, &mq->mq_mutex, 19240f180a7cSJohn Baldwin PCATCH, "mqrecv", timo); 1925655291f2SDavid Xu mq->mq_receivers--; 1926655291f2SDavid Xu if (error == EAGAIN) 1927655291f2SDavid Xu error = ETIMEDOUT; 1928655291f2SDavid Xu } 1929655291f2SDavid Xu if (*msg != NULL) { 1930655291f2SDavid Xu error = 0; 1931655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link); 1932655291f2SDavid Xu mq->mq_curmsgs--; 1933655291f2SDavid Xu mq->mq_totalbytes -= (*msg)->msg_size; 1934655291f2SDavid Xu if (mq->mq_senders) 1935655291f2SDavid Xu wakeup_one(&mq->mq_senders); 1936655291f2SDavid Xu if (mq->mq_flags & MQ_WSEL) { 1937655291f2SDavid Xu mq->mq_flags &= ~MQ_WSEL; 193803f70aecSDavid Xu selwakeup(&mq->mq_wsel); 1939655291f2SDavid Xu } 1940655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_wsel.si_note, 0); 1941655291f2SDavid Xu } 1942655291f2SDavid Xu if (mq->mq_notifier != NULL && mq->mq_receivers == 0 && 1943655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) { 1944655291f2SDavid Xu mqueue_send_notification(mq); 1945655291f2SDavid Xu } 1946655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1947655291f2SDavid Xu return (error); 1948655291f2SDavid Xu } 1949655291f2SDavid Xu 1950b2f92ef9SDavid Xu static __inline struct mqueue_notifier * 1951b2f92ef9SDavid Xu notifier_alloc(void) 1952655291f2SDavid Xu { 1953b2f92ef9SDavid Xu return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO)); 1954655291f2SDavid Xu } 1955655291f2SDavid Xu 1956655291f2SDavid Xu static __inline void 1957b2f92ef9SDavid Xu notifier_free(struct mqueue_notifier *p) 1958655291f2SDavid Xu { 1959b2f92ef9SDavid Xu uma_zfree(mqnoti_zone, p); 1960b2f92ef9SDavid Xu } 1961b2f92ef9SDavid Xu 1962b2f92ef9SDavid Xu static struct mqueue_notifier * 1963b2f92ef9SDavid Xu notifier_search(struct proc *p, int fd) 1964b2f92ef9SDavid Xu { 1965b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1966b2f92ef9SDavid Xu 1967b2f92ef9SDavid Xu LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) { 19689da8a32aSDavid Xu if (nt->nt_ksi.ksi_mqd == fd) 1969b2f92ef9SDavid Xu break; 1970b2f92ef9SDavid Xu } 1971b2f92ef9SDavid Xu return (nt); 1972b2f92ef9SDavid Xu } 1973b2f92ef9SDavid Xu 1974102178d0SDavid Xu static __inline void 1975b2f92ef9SDavid Xu notifier_insert(struct proc *p, struct mqueue_notifier *nt) 1976b2f92ef9SDavid Xu { 1977b2f92ef9SDavid Xu LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link); 1978b2f92ef9SDavid Xu } 1979b2f92ef9SDavid Xu 1980102178d0SDavid Xu static __inline void 1981b2f92ef9SDavid Xu notifier_delete(struct proc *p, struct mqueue_notifier *nt) 1982b2f92ef9SDavid Xu { 1983b2f92ef9SDavid Xu LIST_REMOVE(nt, nt_link); 1984b2f92ef9SDavid Xu notifier_free(nt); 1985b2f92ef9SDavid Xu } 1986b2f92ef9SDavid Xu 1987b2f92ef9SDavid Xu static void 1988b2f92ef9SDavid Xu notifier_remove(struct proc *p, struct mqueue *mq, int fd) 1989b2f92ef9SDavid Xu { 1990b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1991b2f92ef9SDavid Xu 1992b2f92ef9SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 1993b2f92ef9SDavid Xu PROC_LOCK(p); 1994b2f92ef9SDavid Xu nt = notifier_search(p, fd); 1995b2f92ef9SDavid Xu if (nt != NULL) { 1996b2f92ef9SDavid Xu if (mq->mq_notifier == nt) 1997b2f92ef9SDavid Xu mq->mq_notifier = NULL; 1998b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 1999b2f92ef9SDavid Xu notifier_delete(p, nt); 2000b2f92ef9SDavid Xu } 2001b2f92ef9SDavid Xu PROC_UNLOCK(p); 2002655291f2SDavid Xu } 2003655291f2SDavid Xu 2004afde2b65SKonstantin Belousov static int 2005afde2b65SKonstantin Belousov kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode, 2006afde2b65SKonstantin Belousov const struct mq_attr *attr) 2007655291f2SDavid Xu { 2008655291f2SDavid Xu char path[MQFS_NAMELEN + 1]; 2009655291f2SDavid Xu struct mqfs_node *pn; 201085078b85SConrad Meyer struct pwddesc *pdp; 2011655291f2SDavid Xu struct file *fp; 2012655291f2SDavid Xu struct mqueue *mq; 2013afde2b65SKonstantin Belousov int fd, error, len, cmode; 2014655291f2SDavid Xu 201515bcf785SRobert Watson AUDIT_ARG_FFLAGS(flags); 201615bcf785SRobert Watson AUDIT_ARG_MODE(mode); 201715bcf785SRobert Watson 201885078b85SConrad Meyer pdp = td->td_proc->p_pd; 201985078b85SConrad Meyer cmode = (((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT); 2020f72b11a4SDavid Xu mq = NULL; 2021afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && attr != NULL) { 2022afde2b65SKonstantin Belousov if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg) 2023655291f2SDavid Xu return (EINVAL); 2024afde2b65SKonstantin Belousov if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize) 2025655291f2SDavid Xu return (EINVAL); 2026afde2b65SKonstantin Belousov } 2027655291f2SDavid Xu 2028afde2b65SKonstantin Belousov error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL); 2029655291f2SDavid Xu if (error) 2030655291f2SDavid Xu return (error); 2031655291f2SDavid Xu 2032655291f2SDavid Xu /* 2033655291f2SDavid Xu * The first character of name must be a slash (/) character 2034655291f2SDavid Xu * and the remaining characters of name cannot include any slash 2035655291f2SDavid Xu * characters. 2036655291f2SDavid Xu */ 2037655291f2SDavid Xu len = strlen(path); 2038dc15eac0SEd Schouten if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL) 2039655291f2SDavid Xu return (EINVAL); 204045d314c5SConrad Meyer /* 204145d314c5SConrad Meyer * "." and ".." are magic directories, populated on the fly, and cannot 204245d314c5SConrad Meyer * be opened as queues. 204345d314c5SConrad Meyer */ 204445d314c5SConrad Meyer if (strcmp(path, "/.") == 0 || strcmp(path, "/..") == 0) 204545d314c5SConrad Meyer return (EINVAL); 204615bcf785SRobert Watson AUDIT_ARG_UPATH1_CANON(path); 2047655291f2SDavid Xu 2048b68cf25fSJilles Tjoelker error = falloc(td, &fp, &fd, O_CLOEXEC); 2049655291f2SDavid Xu if (error) 2050655291f2SDavid Xu return (error); 2051655291f2SDavid Xu 2052655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2053adb023aeSJamie Gritton pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred); 2054655291f2SDavid Xu if (pn == NULL) { 2055655291f2SDavid Xu if (!(flags & O_CREAT)) { 2056655291f2SDavid Xu error = ENOENT; 2057655291f2SDavid Xu } else { 2058afde2b65SKonstantin Belousov mq = mqueue_alloc(attr); 2059b2f92ef9SDavid Xu if (mq == NULL) { 2060b2f92ef9SDavid Xu error = ENFILE; 2061b2f92ef9SDavid Xu } else { 2062655291f2SDavid Xu pn = mqfs_create_file(mqfs_data.mi_root, 2063ba0360b1SDavid Xu path + 1, len - 1, td->td_ucred, 2064ba0360b1SDavid Xu cmode); 2065655291f2SDavid Xu if (pn == NULL) { 2066655291f2SDavid Xu error = ENOSPC; 2067b2f92ef9SDavid Xu mqueue_free(mq); 2068b2f92ef9SDavid Xu } 2069655291f2SDavid Xu } 2070655291f2SDavid Xu } 2071655291f2SDavid Xu 2072655291f2SDavid Xu if (error == 0) { 2073655291f2SDavid Xu pn->mn_data = mq; 2074655291f2SDavid Xu } 2075655291f2SDavid Xu } else { 2076655291f2SDavid Xu if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) { 2077655291f2SDavid Xu error = EEXIST; 2078655291f2SDavid Xu } else { 207915bc6b2bSEdward Tomasz Napierala accmode_t accmode = 0; 2080655291f2SDavid Xu 2081655291f2SDavid Xu if (flags & FREAD) 208215bc6b2bSEdward Tomasz Napierala accmode |= VREAD; 2083655291f2SDavid Xu if (flags & FWRITE) 208415bc6b2bSEdward Tomasz Napierala accmode |= VWRITE; 2085655291f2SDavid Xu error = vaccess(VREG, pn->mn_mode, pn->mn_uid, 2086d292b194SMateusz Guzik pn->mn_gid, accmode, td->td_ucred); 2087655291f2SDavid Xu } 2088655291f2SDavid Xu } 2089655291f2SDavid Xu 2090655291f2SDavid Xu if (error) { 2091655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 209290f54cbfSMateusz Guzik fdclose(td, fp, fd); 2093655291f2SDavid Xu fdrop(fp, td); 2094655291f2SDavid Xu return (error); 2095655291f2SDavid Xu } 2096655291f2SDavid Xu 2097655291f2SDavid Xu mqnode_addref(pn); 2098655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2099655291f2SDavid Xu 2100397c19d1SJeff Roberson finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn, 2101397c19d1SJeff Roberson &mqueueops); 2102655291f2SDavid Xu 2103655291f2SDavid Xu td->td_retval[0] = fd; 2104655291f2SDavid Xu fdrop(fp, td); 2105655291f2SDavid Xu return (0); 2106655291f2SDavid Xu } 2107655291f2SDavid Xu 2108655291f2SDavid Xu /* 2109afde2b65SKonstantin Belousov * Syscall to open a message queue. 2110afde2b65SKonstantin Belousov */ 2111afde2b65SKonstantin Belousov int 21128451d0ddSKip Macy sys_kmq_open(struct thread *td, struct kmq_open_args *uap) 2113afde2b65SKonstantin Belousov { 2114afde2b65SKonstantin Belousov struct mq_attr attr; 2115afde2b65SKonstantin Belousov int flags, error; 2116afde2b65SKonstantin Belousov 21176fbdb9f4SJilles Tjoelker if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC) 2118afde2b65SKonstantin Belousov return (EINVAL); 2119afde2b65SKonstantin Belousov flags = FFLAGS(uap->flags); 2120afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && uap->attr != NULL) { 2121afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr, sizeof(attr)); 2122afde2b65SKonstantin Belousov if (error) 2123afde2b65SKonstantin Belousov return (error); 2124afde2b65SKonstantin Belousov } 2125afde2b65SKonstantin Belousov return (kern_kmq_open(td, uap->path, flags, uap->mode, 2126afde2b65SKonstantin Belousov uap->attr != NULL ? &attr : NULL)); 2127afde2b65SKonstantin Belousov } 2128afde2b65SKonstantin Belousov 2129afde2b65SKonstantin Belousov /* 2130873fbcd7SRobert Watson * Syscall to unlink a message queue. 2131655291f2SDavid Xu */ 2132655291f2SDavid Xu int 21338451d0ddSKip Macy sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap) 2134655291f2SDavid Xu { 2135655291f2SDavid Xu char path[MQFS_NAMELEN+1]; 2136655291f2SDavid Xu struct mqfs_node *pn; 2137655291f2SDavid Xu int error, len; 2138655291f2SDavid Xu 2139655291f2SDavid Xu error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL); 2140655291f2SDavid Xu if (error) 2141655291f2SDavid Xu return (error); 2142655291f2SDavid Xu 2143655291f2SDavid Xu len = strlen(path); 2144dc15eac0SEd Schouten if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL) 2145655291f2SDavid Xu return (EINVAL); 214645d314c5SConrad Meyer if (strcmp(path, "/.") == 0 || strcmp(path, "/..") == 0) 214745d314c5SConrad Meyer return (EINVAL); 214815bcf785SRobert Watson AUDIT_ARG_UPATH1_CANON(path); 2149655291f2SDavid Xu 2150655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2151adb023aeSJamie Gritton pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred); 2152655291f2SDavid Xu if (pn != NULL) 2153655291f2SDavid Xu error = do_unlink(pn, td->td_ucred); 2154655291f2SDavid Xu else 2155655291f2SDavid Xu error = ENOENT; 2156655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2157655291f2SDavid Xu return (error); 2158655291f2SDavid Xu } 2159655291f2SDavid Xu 21607008be5bSPawel Jakub Dawidek typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **); 2161655291f2SDavid Xu 2162655291f2SDavid Xu /* 2163655291f2SDavid Xu * Get message queue by giving file slot 2164655291f2SDavid Xu */ 2165655291f2SDavid Xu static int 21667008be5bSPawel Jakub Dawidek _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func, 2167655291f2SDavid Xu struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq) 2168655291f2SDavid Xu { 2169655291f2SDavid Xu struct mqfs_node *pn; 2170655291f2SDavid Xu int error; 2171655291f2SDavid Xu 21727008be5bSPawel Jakub Dawidek error = func(td, fd, rightsp, fpp); 2173655291f2SDavid Xu if (error) 2174655291f2SDavid Xu return (error); 2175655291f2SDavid Xu if (&mqueueops != (*fpp)->f_ops) { 2176655291f2SDavid Xu fdrop(*fpp, td); 2177655291f2SDavid Xu return (EBADF); 2178655291f2SDavid Xu } 2179b2f92ef9SDavid Xu pn = (*fpp)->f_data; 2180655291f2SDavid Xu if (ppn) 2181655291f2SDavid Xu *ppn = pn; 2182655291f2SDavid Xu if (pmq) 2183655291f2SDavid Xu *pmq = pn->mn_data; 2184655291f2SDavid Xu return (0); 2185655291f2SDavid Xu } 2186655291f2SDavid Xu 2187655291f2SDavid Xu static __inline int 2188655291f2SDavid Xu getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, 2189655291f2SDavid Xu struct mqueue **pmq) 2190655291f2SDavid Xu { 21917008be5bSPawel Jakub Dawidek 2192cbd92ce6SMatt Macy return _getmq(td, fd, &cap_event_rights, fget, 21937008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2194655291f2SDavid Xu } 2195655291f2SDavid Xu 2196655291f2SDavid Xu static __inline int 2197655291f2SDavid Xu getmq_read(struct thread *td, int fd, struct file **fpp, 2198655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2199655291f2SDavid Xu { 22007008be5bSPawel Jakub Dawidek 2201cbd92ce6SMatt Macy return _getmq(td, fd, &cap_read_rights, fget_read, 22027008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2203655291f2SDavid Xu } 2204655291f2SDavid Xu 2205655291f2SDavid Xu static __inline int 2206655291f2SDavid Xu getmq_write(struct thread *td, int fd, struct file **fpp, 2207655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2208655291f2SDavid Xu { 22097008be5bSPawel Jakub Dawidek 2210cbd92ce6SMatt Macy return _getmq(td, fd, &cap_write_rights, fget_write, 22117008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2212655291f2SDavid Xu } 2213655291f2SDavid Xu 2214afde2b65SKonstantin Belousov static int 2215afde2b65SKonstantin Belousov kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr, 2216afde2b65SKonstantin Belousov struct mq_attr *oattr) 2217655291f2SDavid Xu { 2218655291f2SDavid Xu struct mqueue *mq; 2219655291f2SDavid Xu struct file *fp; 2220397c19d1SJeff Roberson u_int oflag, flag; 2221655291f2SDavid Xu int error; 2222655291f2SDavid Xu 222315bcf785SRobert Watson AUDIT_ARG_FD(mqd); 2224afde2b65SKonstantin Belousov if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0) 2225655291f2SDavid Xu return (EINVAL); 2226afde2b65SKonstantin Belousov error = getmq(td, mqd, &fp, NULL, &mq); 2227655291f2SDavid Xu if (error) 2228655291f2SDavid Xu return (error); 2229afde2b65SKonstantin Belousov oattr->mq_maxmsg = mq->mq_maxmsg; 2230afde2b65SKonstantin Belousov oattr->mq_msgsize = mq->mq_msgsize; 2231afde2b65SKonstantin Belousov oattr->mq_curmsgs = mq->mq_curmsgs; 2232afde2b65SKonstantin Belousov if (attr != NULL) { 2233397c19d1SJeff Roberson do { 2234397c19d1SJeff Roberson oflag = flag = fp->f_flag; 2235397c19d1SJeff Roberson flag &= ~O_NONBLOCK; 2236afde2b65SKonstantin Belousov flag |= (attr->mq_flags & O_NONBLOCK); 2237397c19d1SJeff Roberson } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0); 2238397c19d1SJeff Roberson } else 2239397c19d1SJeff Roberson oflag = fp->f_flag; 2240afde2b65SKonstantin Belousov oattr->mq_flags = (O_NONBLOCK & oflag); 2241655291f2SDavid Xu fdrop(fp, td); 2242afde2b65SKonstantin Belousov return (error); 2243afde2b65SKonstantin Belousov } 2244afde2b65SKonstantin Belousov 2245afde2b65SKonstantin Belousov int 22468451d0ddSKip Macy sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap) 2247afde2b65SKonstantin Belousov { 2248afde2b65SKonstantin Belousov struct mq_attr attr, oattr; 2249afde2b65SKonstantin Belousov int error; 2250afde2b65SKonstantin Belousov 2251afde2b65SKonstantin Belousov if (uap->attr != NULL) { 2252afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr, sizeof(attr)); 2253afde2b65SKonstantin Belousov if (error != 0) 2254afde2b65SKonstantin Belousov return (error); 2255afde2b65SKonstantin Belousov } 2256afde2b65SKonstantin Belousov error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL, 2257afde2b65SKonstantin Belousov &oattr); 2258ae44bb01SKonstantin Belousov if (error == 0 && uap->oattr != NULL) { 2259ae44bb01SKonstantin Belousov bzero(oattr.__reserved, sizeof(oattr.__reserved)); 2260655291f2SDavid Xu error = copyout(&oattr, uap->oattr, sizeof(oattr)); 2261ae44bb01SKonstantin Belousov } 2262655291f2SDavid Xu return (error); 2263655291f2SDavid Xu } 2264655291f2SDavid Xu 2265655291f2SDavid Xu int 22668451d0ddSKip Macy sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap) 2267655291f2SDavid Xu { 2268655291f2SDavid Xu struct mqueue *mq; 2269655291f2SDavid Xu struct file *fp; 2270afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2271655291f2SDavid Xu int error; 2272655291f2SDavid Xu int waitok; 2273655291f2SDavid Xu 227415bcf785SRobert Watson AUDIT_ARG_FD(uap->mqd); 2275655291f2SDavid Xu error = getmq_read(td, uap->mqd, &fp, NULL, &mq); 2276655291f2SDavid Xu if (error) 2277655291f2SDavid Xu return (error); 2278afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2279afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets, sizeof(ets)); 2280afde2b65SKonstantin Belousov if (error != 0) 2281051e692aSEd Maste goto out; 2282afde2b65SKonstantin Belousov abs_timeout = &ets; 2283afde2b65SKonstantin Belousov } else 2284afde2b65SKonstantin Belousov abs_timeout = NULL; 2285655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2286655291f2SDavid Xu error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len, 2287afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2288051e692aSEd Maste out: 2289655291f2SDavid Xu fdrop(fp, td); 2290655291f2SDavid Xu return (error); 2291655291f2SDavid Xu } 2292655291f2SDavid Xu 2293655291f2SDavid Xu int 22948451d0ddSKip Macy sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap) 2295655291f2SDavid Xu { 2296655291f2SDavid Xu struct mqueue *mq; 2297655291f2SDavid Xu struct file *fp; 2298afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2299655291f2SDavid Xu int error, waitok; 2300655291f2SDavid Xu 230115bcf785SRobert Watson AUDIT_ARG_FD(uap->mqd); 2302655291f2SDavid Xu error = getmq_write(td, uap->mqd, &fp, NULL, &mq); 2303655291f2SDavid Xu if (error) 2304655291f2SDavid Xu return (error); 2305afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2306afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets, sizeof(ets)); 2307afde2b65SKonstantin Belousov if (error != 0) 2308051e692aSEd Maste goto out; 2309afde2b65SKonstantin Belousov abs_timeout = &ets; 2310afde2b65SKonstantin Belousov } else 2311afde2b65SKonstantin Belousov abs_timeout = NULL; 2312655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2313655291f2SDavid Xu error = mqueue_send(mq, uap->msg_ptr, uap->msg_len, 2314afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2315051e692aSEd Maste out: 2316655291f2SDavid Xu fdrop(fp, td); 2317655291f2SDavid Xu return (error); 2318655291f2SDavid Xu } 2319655291f2SDavid Xu 2320058262c6SKonstantin Belousov static int 2321058262c6SKonstantin Belousov kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev) 2322655291f2SDavid Xu { 2323b2f92ef9SDavid Xu struct filedesc *fdp; 2324b2f92ef9SDavid Xu struct proc *p; 2325655291f2SDavid Xu struct mqueue *mq; 2326a9d2f8d8SRobert Watson struct file *fp, *fp2; 2327b2f92ef9SDavid Xu struct mqueue_notifier *nt, *newnt = NULL; 2328655291f2SDavid Xu int error; 2329655291f2SDavid Xu 233015bcf785SRobert Watson AUDIT_ARG_FD(mqd); 2331058262c6SKonstantin Belousov if (sigev != NULL) { 2332058262c6SKonstantin Belousov if (sigev->sigev_notify != SIGEV_SIGNAL && 2333058262c6SKonstantin Belousov sigev->sigev_notify != SIGEV_THREAD_ID && 2334058262c6SKonstantin Belousov sigev->sigev_notify != SIGEV_NONE) 2335655291f2SDavid Xu return (EINVAL); 2336058262c6SKonstantin Belousov if ((sigev->sigev_notify == SIGEV_SIGNAL || 2337058262c6SKonstantin Belousov sigev->sigev_notify == SIGEV_THREAD_ID) && 2338058262c6SKonstantin Belousov !_SIG_VALID(sigev->sigev_signo)) 2339655291f2SDavid Xu return (EINVAL); 2340655291f2SDavid Xu } 2341058262c6SKonstantin Belousov p = td->td_proc; 2342058262c6SKonstantin Belousov fdp = td->td_proc->p_fd; 2343058262c6SKonstantin Belousov error = getmq(td, mqd, &fp, NULL, &mq); 2344655291f2SDavid Xu if (error) 2345655291f2SDavid Xu return (error); 2346b2f92ef9SDavid Xu again: 23475e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 2348f17ef286SMateusz Guzik fp2 = fget_noref(fdp, mqd); 2349a9d2f8d8SRobert Watson if (fp2 == NULL) { 2350a9d2f8d8SRobert Watson FILEDESC_SUNLOCK(fdp); 2351a9d2f8d8SRobert Watson error = EBADF; 2352a9d2f8d8SRobert Watson goto out; 2353a9d2f8d8SRobert Watson } 23542609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 2355cbd92ce6SMatt Macy error = cap_check(cap_rights(fdp, mqd), &cap_event_rights); 2356a9d2f8d8SRobert Watson if (error) { 2357a9d2f8d8SRobert Watson FILEDESC_SUNLOCK(fdp); 2358a9d2f8d8SRobert Watson goto out; 2359a9d2f8d8SRobert Watson } 23602609222aSPawel Jakub Dawidek #endif 2361a9d2f8d8SRobert Watson if (fp2 != fp) { 23625e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2363b2f92ef9SDavid Xu error = EBADF; 2364b2f92ef9SDavid Xu goto out; 2365b2f92ef9SDavid Xu } 2366655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 23675e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2368058262c6SKonstantin Belousov if (sigev != NULL) { 2369655291f2SDavid Xu if (mq->mq_notifier != NULL) { 2370655291f2SDavid Xu error = EBUSY; 2371052ea11cSDavid Xu } else { 2372b2f92ef9SDavid Xu PROC_LOCK(p); 2373058262c6SKonstantin Belousov nt = notifier_search(p, mqd); 2374b2f92ef9SDavid Xu if (nt == NULL) { 2375b2f92ef9SDavid Xu if (newnt == NULL) { 2376b2f92ef9SDavid Xu PROC_UNLOCK(p); 2377b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2378b2f92ef9SDavid Xu newnt = notifier_alloc(); 2379b2f92ef9SDavid Xu goto again; 2380b2f92ef9SDavid Xu } 2381b2f92ef9SDavid Xu } 2382b2f92ef9SDavid Xu 2383b2f92ef9SDavid Xu if (nt != NULL) { 2384b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 2385b2f92ef9SDavid Xu if (newnt != NULL) { 2386b2f92ef9SDavid Xu notifier_free(newnt); 2387b2f92ef9SDavid Xu newnt = NULL; 2388b2f92ef9SDavid Xu } 2389b2f92ef9SDavid Xu } else { 2390b2f92ef9SDavid Xu nt = newnt; 2391b2f92ef9SDavid Xu newnt = NULL; 2392b2f92ef9SDavid Xu ksiginfo_init(&nt->nt_ksi); 2393b2f92ef9SDavid Xu nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT; 2394b2f92ef9SDavid Xu nt->nt_ksi.ksi_code = SI_MESGQ; 2395b2f92ef9SDavid Xu nt->nt_proc = p; 2396058262c6SKonstantin Belousov nt->nt_ksi.ksi_mqd = mqd; 2397b2f92ef9SDavid Xu notifier_insert(p, nt); 2398b2f92ef9SDavid Xu } 2399058262c6SKonstantin Belousov nt->nt_sigev = *sigev; 2400b2f92ef9SDavid Xu mq->mq_notifier = nt; 2401b2f92ef9SDavid Xu PROC_UNLOCK(p); 2402655291f2SDavid Xu /* 2403b2f92ef9SDavid Xu * if there is no receivers and message queue 2404b2f92ef9SDavid Xu * is not empty, we should send notification 2405b2f92ef9SDavid Xu * as soon as possible. 2406655291f2SDavid Xu */ 2407655291f2SDavid Xu if (mq->mq_receivers == 0 && 2408655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) 2409655291f2SDavid Xu mqueue_send_notification(mq); 2410655291f2SDavid Xu } 2411655291f2SDavid Xu } else { 2412058262c6SKonstantin Belousov notifier_remove(p, mq, mqd); 2413655291f2SDavid Xu } 2414655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2415b2f92ef9SDavid Xu 2416b2f92ef9SDavid Xu out: 2417655291f2SDavid Xu fdrop(fp, td); 2418b2f92ef9SDavid Xu if (newnt != NULL) 2419b2f92ef9SDavid Xu notifier_free(newnt); 2420655291f2SDavid Xu return (error); 2421655291f2SDavid Xu } 2422655291f2SDavid Xu 2423058262c6SKonstantin Belousov int 2424058262c6SKonstantin Belousov sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap) 2425058262c6SKonstantin Belousov { 2426058262c6SKonstantin Belousov struct sigevent ev, *evp; 2427058262c6SKonstantin Belousov int error; 2428058262c6SKonstantin Belousov 2429058262c6SKonstantin Belousov if (uap->sigev == NULL) { 2430058262c6SKonstantin Belousov evp = NULL; 2431058262c6SKonstantin Belousov } else { 2432058262c6SKonstantin Belousov error = copyin(uap->sigev, &ev, sizeof(ev)); 2433058262c6SKonstantin Belousov if (error != 0) 2434058262c6SKonstantin Belousov return (error); 2435058262c6SKonstantin Belousov evp = &ev; 2436058262c6SKonstantin Belousov } 2437058262c6SKonstantin Belousov return (kern_kmq_notify(td, uap->mqd, evp)); 2438058262c6SKonstantin Belousov } 2439058262c6SKonstantin Belousov 2440b2f92ef9SDavid Xu static void 2441b2f92ef9SDavid Xu mqueue_fdclose(struct thread *td, int fd, struct file *fp) 2442b2f92ef9SDavid Xu { 2443b2f92ef9SDavid Xu struct mqueue *mq; 24444949ad72SMatt Macy #ifdef INVARIANTS 24454949ad72SMatt Macy struct filedesc *fdp; 2446b2f92ef9SDavid Xu 2447b2f92ef9SDavid Xu fdp = td->td_proc->p_fd; 24485e3f7694SRobert Watson FILEDESC_LOCK_ASSERT(fdp); 24494949ad72SMatt Macy #endif 24505e3f7694SRobert Watson 2451b2f92ef9SDavid Xu if (fp->f_ops == &mqueueops) { 2452b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2453b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2454b2f92ef9SDavid Xu notifier_remove(td->td_proc, mq, fd); 2455b2f92ef9SDavid Xu 2456b2f92ef9SDavid Xu /* have to wakeup thread in same process */ 2457b2f92ef9SDavid Xu if (mq->mq_flags & MQ_RSEL) { 2458b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_RSEL; 245903f70aecSDavid Xu selwakeup(&mq->mq_rsel); 2460b2f92ef9SDavid Xu } 2461b2f92ef9SDavid Xu if (mq->mq_flags & MQ_WSEL) { 2462b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_WSEL; 246303f70aecSDavid Xu selwakeup(&mq->mq_wsel); 2464b2f92ef9SDavid Xu } 2465b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2466b2f92ef9SDavid Xu } 2467b2f92ef9SDavid Xu } 2468b2f92ef9SDavid Xu 2469b2f92ef9SDavid Xu static void 2470b2f92ef9SDavid Xu mq_proc_exit(void *arg __unused, struct proc *p) 2471b2f92ef9SDavid Xu { 2472b2f92ef9SDavid Xu struct filedesc *fdp; 2473b2f92ef9SDavid Xu struct file *fp; 2474b2f92ef9SDavid Xu struct mqueue *mq; 2475b2f92ef9SDavid Xu int i; 2476b2f92ef9SDavid Xu 2477b2f92ef9SDavid Xu fdp = p->p_fd; 24785e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 2479b2f92ef9SDavid Xu for (i = 0; i < fdp->fd_nfiles; ++i) { 2480f17ef286SMateusz Guzik fp = fget_noref(fdp, i); 2481b2f92ef9SDavid Xu if (fp != NULL && fp->f_ops == &mqueueops) { 2482b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2483b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2484b2f92ef9SDavid Xu notifier_remove(p, FPTOMQ(fp), i); 2485b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2486b2f92ef9SDavid Xu } 2487b2f92ef9SDavid Xu } 24885e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2489b2f92ef9SDavid Xu KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left")); 2490b2f92ef9SDavid Xu } 2491b2f92ef9SDavid Xu 2492655291f2SDavid Xu static int 2493655291f2SDavid Xu mqf_poll(struct file *fp, int events, struct ucred *active_cred, 2494655291f2SDavid Xu struct thread *td) 2495655291f2SDavid Xu { 2496655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2497655291f2SDavid Xu int revents = 0; 2498655291f2SDavid Xu 2499655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 2500655291f2SDavid Xu if (events & (POLLIN | POLLRDNORM)) { 2501655291f2SDavid Xu if (mq->mq_curmsgs) { 2502655291f2SDavid Xu revents |= events & (POLLIN | POLLRDNORM); 2503655291f2SDavid Xu } else { 2504655291f2SDavid Xu mq->mq_flags |= MQ_RSEL; 2505655291f2SDavid Xu selrecord(td, &mq->mq_rsel); 2506655291f2SDavid Xu } 2507655291f2SDavid Xu } 2508655291f2SDavid Xu if (events & POLLOUT) { 2509655291f2SDavid Xu if (mq->mq_curmsgs < mq->mq_maxmsg) 2510655291f2SDavid Xu revents |= POLLOUT; 2511655291f2SDavid Xu else { 2512655291f2SDavid Xu mq->mq_flags |= MQ_WSEL; 2513655291f2SDavid Xu selrecord(td, &mq->mq_wsel); 2514655291f2SDavid Xu } 2515655291f2SDavid Xu } 2516655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2517655291f2SDavid Xu return (revents); 2518655291f2SDavid Xu } 2519655291f2SDavid Xu 2520655291f2SDavid Xu static int 2521655291f2SDavid Xu mqf_close(struct file *fp, struct thread *td) 2522655291f2SDavid Xu { 2523655291f2SDavid Xu struct mqfs_node *pn; 2524655291f2SDavid Xu 2525655291f2SDavid Xu fp->f_ops = &badfileops; 2526b2f92ef9SDavid Xu pn = fp->f_data; 2527655291f2SDavid Xu fp->f_data = NULL; 2528655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2529655291f2SDavid Xu mqnode_release(pn); 2530655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2531655291f2SDavid Xu return (0); 2532655291f2SDavid Xu } 2533655291f2SDavid Xu 2534655291f2SDavid Xu static int 25352b68eb8eSMateusz Guzik mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred) 2536655291f2SDavid Xu { 2537b2f92ef9SDavid Xu struct mqfs_node *pn = fp->f_data; 2538655291f2SDavid Xu 2539655291f2SDavid Xu bzero(st, sizeof *st); 25409c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 2541510ea843SEd Schouten st->st_atim = pn->mn_atime; 2542510ea843SEd Schouten st->st_mtim = pn->mn_mtime; 2543510ea843SEd Schouten st->st_ctim = pn->mn_ctime; 2544510ea843SEd Schouten st->st_birthtim = pn->mn_birth; 2545655291f2SDavid Xu st->st_uid = pn->mn_uid; 2546655291f2SDavid Xu st->st_gid = pn->mn_gid; 2547655291f2SDavid Xu st->st_mode = S_IFIFO | pn->mn_mode; 25489c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 2549655291f2SDavid Xu return (0); 2550655291f2SDavid Xu } 2551655291f2SDavid Xu 2552655291f2SDavid Xu static int 25539c00bb91SKonstantin Belousov mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 25549c00bb91SKonstantin Belousov struct thread *td) 25559c00bb91SKonstantin Belousov { 25569c00bb91SKonstantin Belousov struct mqfs_node *pn; 25579c00bb91SKonstantin Belousov int error; 25589c00bb91SKonstantin Belousov 25599c00bb91SKonstantin Belousov error = 0; 25609c00bb91SKonstantin Belousov pn = fp->f_data; 25619c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 25629c00bb91SKonstantin Belousov error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN, 2563d292b194SMateusz Guzik active_cred); 25649c00bb91SKonstantin Belousov if (error != 0) 25659c00bb91SKonstantin Belousov goto out; 25669c00bb91SKonstantin Belousov pn->mn_mode = mode & ACCESSPERMS; 25679c00bb91SKonstantin Belousov out: 25689c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 25699c00bb91SKonstantin Belousov return (error); 25709c00bb91SKonstantin Belousov } 25719c00bb91SKonstantin Belousov 25729c00bb91SKonstantin Belousov static int 25739c00bb91SKonstantin Belousov mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 25749c00bb91SKonstantin Belousov struct thread *td) 25759c00bb91SKonstantin Belousov { 25769c00bb91SKonstantin Belousov struct mqfs_node *pn; 25779c00bb91SKonstantin Belousov int error; 25789c00bb91SKonstantin Belousov 25799c00bb91SKonstantin Belousov error = 0; 25809c00bb91SKonstantin Belousov pn = fp->f_data; 25819c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 25829c00bb91SKonstantin Belousov if (uid == (uid_t)-1) 25839c00bb91SKonstantin Belousov uid = pn->mn_uid; 25849c00bb91SKonstantin Belousov if (gid == (gid_t)-1) 25859c00bb91SKonstantin Belousov gid = pn->mn_gid; 25869c00bb91SKonstantin Belousov if (((uid != pn->mn_uid && uid != active_cred->cr_uid) || 25879c00bb91SKonstantin Belousov (gid != pn->mn_gid && !groupmember(gid, active_cred))) && 2588cc426dd3SMateusz Guzik (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN))) 25899c00bb91SKonstantin Belousov goto out; 25909c00bb91SKonstantin Belousov pn->mn_uid = uid; 25919c00bb91SKonstantin Belousov pn->mn_gid = gid; 25929c00bb91SKonstantin Belousov out: 25939c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 25949c00bb91SKonstantin Belousov return (error); 25959c00bb91SKonstantin Belousov } 25969c00bb91SKonstantin Belousov 25979c00bb91SKonstantin Belousov static int 2598655291f2SDavid Xu mqf_kqfilter(struct file *fp, struct knote *kn) 2599655291f2SDavid Xu { 2600655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2601655291f2SDavid Xu int error = 0; 2602655291f2SDavid Xu 2603655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) { 2604655291f2SDavid Xu kn->kn_fop = &mq_rfiltops; 2605655291f2SDavid Xu knlist_add(&mq->mq_rsel.si_note, kn, 0); 2606655291f2SDavid Xu } else if (kn->kn_filter == EVFILT_WRITE) { 2607655291f2SDavid Xu kn->kn_fop = &mq_wfiltops; 2608655291f2SDavid Xu knlist_add(&mq->mq_wsel.si_note, kn, 0); 2609655291f2SDavid Xu } else 2610655291f2SDavid Xu error = EINVAL; 2611655291f2SDavid Xu return (error); 2612655291f2SDavid Xu } 2613655291f2SDavid Xu 2614655291f2SDavid Xu static void 2615655291f2SDavid Xu filt_mqdetach(struct knote *kn) 2616655291f2SDavid Xu { 2617655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2618655291f2SDavid Xu 2619655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) 2620655291f2SDavid Xu knlist_remove(&mq->mq_rsel.si_note, kn, 0); 2621655291f2SDavid Xu else if (kn->kn_filter == EVFILT_WRITE) 2622655291f2SDavid Xu knlist_remove(&mq->mq_wsel.si_note, kn, 0); 2623655291f2SDavid Xu else 2624655291f2SDavid Xu panic("filt_mqdetach"); 2625655291f2SDavid Xu } 2626655291f2SDavid Xu 2627655291f2SDavid Xu static int 2628655291f2SDavid Xu filt_mqread(struct knote *kn, long hint) 2629655291f2SDavid Xu { 2630655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2631655291f2SDavid Xu 2632655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2633655291f2SDavid Xu return (mq->mq_curmsgs != 0); 2634655291f2SDavid Xu } 2635655291f2SDavid Xu 2636655291f2SDavid Xu static int 2637655291f2SDavid Xu filt_mqwrite(struct knote *kn, long hint) 2638655291f2SDavid Xu { 2639655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2640655291f2SDavid Xu 2641655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2642655291f2SDavid Xu return (mq->mq_curmsgs < mq->mq_maxmsg); 2643655291f2SDavid Xu } 2644655291f2SDavid Xu 26459696feebSJohn Baldwin static int 26469696feebSJohn Baldwin mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 26479696feebSJohn Baldwin { 26489696feebSJohn Baldwin 26499696feebSJohn Baldwin kif->kf_type = KF_TYPE_MQUEUE; 26509696feebSJohn Baldwin return (0); 26519696feebSJohn Baldwin } 26529696feebSJohn Baldwin 2653655291f2SDavid Xu static struct fileops mqueueops = { 26542d69d0dcSJohn Baldwin .fo_read = invfo_rdwr, 26552d69d0dcSJohn Baldwin .fo_write = invfo_rdwr, 26562d69d0dcSJohn Baldwin .fo_truncate = invfo_truncate, 26572d69d0dcSJohn Baldwin .fo_ioctl = invfo_ioctl, 2658655291f2SDavid Xu .fo_poll = mqf_poll, 2659655291f2SDavid Xu .fo_kqfilter = mqf_kqfilter, 2660655291f2SDavid Xu .fo_stat = mqf_stat, 26612d69d0dcSJohn Baldwin .fo_close = mqf_close, 26629c00bb91SKonstantin Belousov .fo_chmod = mqf_chmod, 26639c00bb91SKonstantin Belousov .fo_chown = mqf_chown, 2664ca04d21dSGleb Smirnoff .fo_sendfile = invfo_sendfile, 26659696feebSJohn Baldwin .fo_fill_kinfo = mqf_fill_kinfo, 266642ec4f05SWarner Losh .fo_flags = DFLAG_PASSABLE, 2667655291f2SDavid Xu }; 2668655291f2SDavid Xu 2669655291f2SDavid Xu static struct vop_vector mqfs_vnodeops = { 2670655291f2SDavid Xu .vop_default = &default_vnodeops, 2671655291f2SDavid Xu .vop_access = mqfs_access, 2672655291f2SDavid Xu .vop_cachedlookup = mqfs_lookup, 2673655291f2SDavid Xu .vop_lookup = vfs_cache_lookup, 2674655291f2SDavid Xu .vop_reclaim = mqfs_reclaim, 2675655291f2SDavid Xu .vop_create = mqfs_create, 2676655291f2SDavid Xu .vop_remove = mqfs_remove, 2677655291f2SDavid Xu .vop_inactive = mqfs_inactive, 2678655291f2SDavid Xu .vop_open = mqfs_open, 2679655291f2SDavid Xu .vop_close = mqfs_close, 2680655291f2SDavid Xu .vop_getattr = mqfs_getattr, 2681655291f2SDavid Xu .vop_setattr = mqfs_setattr, 2682655291f2SDavid Xu .vop_read = mqfs_read, 2683655291f2SDavid Xu .vop_write = VOP_EOPNOTSUPP, 2684655291f2SDavid Xu .vop_readdir = mqfs_readdir, 2685655291f2SDavid Xu .vop_mkdir = VOP_EOPNOTSUPP, 2686655291f2SDavid Xu .vop_rmdir = VOP_EOPNOTSUPP 2687655291f2SDavid Xu }; 26886fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(mqfs_vnodeops); 2689655291f2SDavid Xu 2690655291f2SDavid Xu static struct vfsops mqfs_vfsops = { 2691655291f2SDavid Xu .vfs_init = mqfs_init, 2692655291f2SDavid Xu .vfs_uninit = mqfs_uninit, 2693655291f2SDavid Xu .vfs_mount = mqfs_mount, 2694655291f2SDavid Xu .vfs_unmount = mqfs_unmount, 2695655291f2SDavid Xu .vfs_root = mqfs_root, 2696655291f2SDavid Xu .vfs_statfs = mqfs_statfs, 2697655291f2SDavid Xu }; 2698655291f2SDavid Xu 2699afde2b65SKonstantin Belousov static struct vfsconf mqueuefs_vfsconf = { 2700afde2b65SKonstantin Belousov .vfc_version = VFS_VERSION, 2701afde2b65SKonstantin Belousov .vfc_name = "mqueuefs", 2702afde2b65SKonstantin Belousov .vfc_vfsops = &mqfs_vfsops, 2703afde2b65SKonstantin Belousov .vfc_typenum = -1, 2704afde2b65SKonstantin Belousov .vfc_flags = VFCF_SYNTHETIC 2705afde2b65SKonstantin Belousov }; 2706655291f2SDavid Xu 2707afde2b65SKonstantin Belousov static struct syscall_helper_data mq_syscalls[] = { 2708afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_open), 27095cead591SKonstantin Belousov SYSCALL_INIT_HELPER_F(kmq_setattr, SYF_CAPENABLED), 27105cead591SKonstantin Belousov SYSCALL_INIT_HELPER_F(kmq_timedsend, SYF_CAPENABLED), 27115cead591SKonstantin Belousov SYSCALL_INIT_HELPER_F(kmq_timedreceive, SYF_CAPENABLED), 27125cead591SKonstantin Belousov SYSCALL_INIT_HELPER_F(kmq_notify, SYF_CAPENABLED), 2713afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_unlink), 2714afde2b65SKonstantin Belousov SYSCALL_INIT_LAST 2715afde2b65SKonstantin Belousov }; 2716afde2b65SKonstantin Belousov 2717afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2718afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32.h> 2719afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h> 2720058262c6SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h> 2721afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h> 2722afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h> 2723afde2b65SKonstantin Belousov 2724afde2b65SKonstantin Belousov static void 2725afde2b65SKonstantin Belousov mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to) 2726afde2b65SKonstantin Belousov { 2727afde2b65SKonstantin Belousov 2728afde2b65SKonstantin Belousov to->mq_flags = from->mq_flags; 2729afde2b65SKonstantin Belousov to->mq_maxmsg = from->mq_maxmsg; 2730afde2b65SKonstantin Belousov to->mq_msgsize = from->mq_msgsize; 2731afde2b65SKonstantin Belousov to->mq_curmsgs = from->mq_curmsgs; 2732afde2b65SKonstantin Belousov } 2733afde2b65SKonstantin Belousov 2734afde2b65SKonstantin Belousov static void 2735afde2b65SKonstantin Belousov mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to) 2736afde2b65SKonstantin Belousov { 2737afde2b65SKonstantin Belousov 2738afde2b65SKonstantin Belousov to->mq_flags = from->mq_flags; 2739afde2b65SKonstantin Belousov to->mq_maxmsg = from->mq_maxmsg; 2740afde2b65SKonstantin Belousov to->mq_msgsize = from->mq_msgsize; 2741afde2b65SKonstantin Belousov to->mq_curmsgs = from->mq_curmsgs; 2742afde2b65SKonstantin Belousov } 2743afde2b65SKonstantin Belousov 2744afde2b65SKonstantin Belousov int 2745afde2b65SKonstantin Belousov freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap) 2746afde2b65SKonstantin Belousov { 2747afde2b65SKonstantin Belousov struct mq_attr attr; 2748afde2b65SKonstantin Belousov struct mq_attr32 attr32; 2749afde2b65SKonstantin Belousov int flags, error; 2750afde2b65SKonstantin Belousov 27516fbdb9f4SJilles Tjoelker if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC) 2752afde2b65SKonstantin Belousov return (EINVAL); 2753afde2b65SKonstantin Belousov flags = FFLAGS(uap->flags); 2754afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && uap->attr != NULL) { 2755afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr32, sizeof(attr32)); 2756afde2b65SKonstantin Belousov if (error) 2757afde2b65SKonstantin Belousov return (error); 2758afde2b65SKonstantin Belousov mq_attr_from32(&attr32, &attr); 2759afde2b65SKonstantin Belousov } 2760afde2b65SKonstantin Belousov return (kern_kmq_open(td, uap->path, flags, uap->mode, 2761afde2b65SKonstantin Belousov uap->attr != NULL ? &attr : NULL)); 2762afde2b65SKonstantin Belousov } 2763afde2b65SKonstantin Belousov 2764afde2b65SKonstantin Belousov int 2765afde2b65SKonstantin Belousov freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap) 2766afde2b65SKonstantin Belousov { 2767afde2b65SKonstantin Belousov struct mq_attr attr, oattr; 2768afde2b65SKonstantin Belousov struct mq_attr32 attr32, oattr32; 2769afde2b65SKonstantin Belousov int error; 2770afde2b65SKonstantin Belousov 2771afde2b65SKonstantin Belousov if (uap->attr != NULL) { 2772afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr32, sizeof(attr32)); 2773afde2b65SKonstantin Belousov if (error != 0) 2774afde2b65SKonstantin Belousov return (error); 2775afde2b65SKonstantin Belousov mq_attr_from32(&attr32, &attr); 2776afde2b65SKonstantin Belousov } 2777afde2b65SKonstantin Belousov error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL, 2778afde2b65SKonstantin Belousov &oattr); 2779ae44bb01SKonstantin Belousov if (error == 0 && uap->oattr != NULL) { 2780afde2b65SKonstantin Belousov mq_attr_to32(&oattr, &oattr32); 2781ae44bb01SKonstantin Belousov bzero(oattr32.__reserved, sizeof(oattr32.__reserved)); 2782afde2b65SKonstantin Belousov error = copyout(&oattr32, uap->oattr, sizeof(oattr32)); 2783afde2b65SKonstantin Belousov } 2784afde2b65SKonstantin Belousov return (error); 2785afde2b65SKonstantin Belousov } 2786afde2b65SKonstantin Belousov 2787afde2b65SKonstantin Belousov int 2788afde2b65SKonstantin Belousov freebsd32_kmq_timedsend(struct thread *td, 2789afde2b65SKonstantin Belousov struct freebsd32_kmq_timedsend_args *uap) 2790afde2b65SKonstantin Belousov { 2791afde2b65SKonstantin Belousov struct mqueue *mq; 2792afde2b65SKonstantin Belousov struct file *fp; 2793afde2b65SKonstantin Belousov struct timespec32 ets32; 2794afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2795afde2b65SKonstantin Belousov int error; 2796afde2b65SKonstantin Belousov int waitok; 2797afde2b65SKonstantin Belousov 279815bcf785SRobert Watson AUDIT_ARG_FD(uap->mqd); 279928f865b0SPawel Jakub Dawidek error = getmq_write(td, uap->mqd, &fp, NULL, &mq); 2800afde2b65SKonstantin Belousov if (error) 2801afde2b65SKonstantin Belousov return (error); 2802afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2803afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets32, sizeof(ets32)); 2804afde2b65SKonstantin Belousov if (error != 0) 2805f3719206SEd Maste goto out; 2806afde2b65SKonstantin Belousov CP(ets32, ets, tv_sec); 2807afde2b65SKonstantin Belousov CP(ets32, ets, tv_nsec); 2808afde2b65SKonstantin Belousov abs_timeout = &ets; 2809afde2b65SKonstantin Belousov } else 2810afde2b65SKonstantin Belousov abs_timeout = NULL; 2811afde2b65SKonstantin Belousov waitok = !(fp->f_flag & O_NONBLOCK); 2812afde2b65SKonstantin Belousov error = mqueue_send(mq, uap->msg_ptr, uap->msg_len, 2813afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2814f3719206SEd Maste out: 2815afde2b65SKonstantin Belousov fdrop(fp, td); 2816afde2b65SKonstantin Belousov return (error); 2817afde2b65SKonstantin Belousov } 2818afde2b65SKonstantin Belousov 2819afde2b65SKonstantin Belousov int 2820afde2b65SKonstantin Belousov freebsd32_kmq_timedreceive(struct thread *td, 2821afde2b65SKonstantin Belousov struct freebsd32_kmq_timedreceive_args *uap) 2822afde2b65SKonstantin Belousov { 2823afde2b65SKonstantin Belousov struct mqueue *mq; 2824afde2b65SKonstantin Belousov struct file *fp; 2825afde2b65SKonstantin Belousov struct timespec32 ets32; 2826afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2827afde2b65SKonstantin Belousov int error, waitok; 2828afde2b65SKonstantin Belousov 282915bcf785SRobert Watson AUDIT_ARG_FD(uap->mqd); 283028f865b0SPawel Jakub Dawidek error = getmq_read(td, uap->mqd, &fp, NULL, &mq); 2831afde2b65SKonstantin Belousov if (error) 2832afde2b65SKonstantin Belousov return (error); 2833afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2834afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets32, sizeof(ets32)); 2835afde2b65SKonstantin Belousov if (error != 0) 2836051e692aSEd Maste goto out; 2837afde2b65SKonstantin Belousov CP(ets32, ets, tv_sec); 2838afde2b65SKonstantin Belousov CP(ets32, ets, tv_nsec); 2839afde2b65SKonstantin Belousov abs_timeout = &ets; 2840afde2b65SKonstantin Belousov } else 2841afde2b65SKonstantin Belousov abs_timeout = NULL; 2842afde2b65SKonstantin Belousov waitok = !(fp->f_flag & O_NONBLOCK); 2843afde2b65SKonstantin Belousov error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len, 2844afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2845051e692aSEd Maste out: 2846afde2b65SKonstantin Belousov fdrop(fp, td); 2847afde2b65SKonstantin Belousov return (error); 2848afde2b65SKonstantin Belousov } 2849afde2b65SKonstantin Belousov 2850058262c6SKonstantin Belousov int 2851058262c6SKonstantin Belousov freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap) 2852058262c6SKonstantin Belousov { 2853058262c6SKonstantin Belousov struct sigevent ev, *evp; 2854058262c6SKonstantin Belousov struct sigevent32 ev32; 2855058262c6SKonstantin Belousov int error; 2856058262c6SKonstantin Belousov 2857058262c6SKonstantin Belousov if (uap->sigev == NULL) { 2858058262c6SKonstantin Belousov evp = NULL; 2859058262c6SKonstantin Belousov } else { 2860058262c6SKonstantin Belousov error = copyin(uap->sigev, &ev32, sizeof(ev32)); 2861058262c6SKonstantin Belousov if (error != 0) 2862058262c6SKonstantin Belousov return (error); 2863058262c6SKonstantin Belousov error = convert_sigevent32(&ev32, &ev); 2864058262c6SKonstantin Belousov if (error != 0) 2865058262c6SKonstantin Belousov return (error); 2866058262c6SKonstantin Belousov evp = &ev; 2867058262c6SKonstantin Belousov } 2868058262c6SKonstantin Belousov return (kern_kmq_notify(td, uap->mqd, evp)); 2869058262c6SKonstantin Belousov } 2870058262c6SKonstantin Belousov 2871afde2b65SKonstantin Belousov static struct syscall_helper_data mq32_syscalls[] = { 2872afde2b65SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_open), 28735cead591SKonstantin Belousov SYSCALL32_INIT_HELPER_F(freebsd32_kmq_setattr, SYF_CAPENABLED), 28745cead591SKonstantin Belousov SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedsend, SYF_CAPENABLED), 28755cead591SKonstantin Belousov SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedreceive, SYF_CAPENABLED), 28765cead591SKonstantin Belousov SYSCALL32_INIT_HELPER_F(freebsd32_kmq_notify, SYF_CAPENABLED), 28778451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink), 2878afde2b65SKonstantin Belousov SYSCALL_INIT_LAST 2879afde2b65SKonstantin Belousov }; 2880afde2b65SKonstantin Belousov #endif 2881afde2b65SKonstantin Belousov 2882afde2b65SKonstantin Belousov static int 2883afde2b65SKonstantin Belousov mqinit(void) 2884afde2b65SKonstantin Belousov { 2885afde2b65SKonstantin Belousov int error; 2886afde2b65SKonstantin Belousov 2887e015b1abSMateusz Guzik error = syscall_helper_register(mq_syscalls, SY_THR_STATIC_KLD); 2888afde2b65SKonstantin Belousov if (error != 0) 2889afde2b65SKonstantin Belousov return (error); 2890afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2891e015b1abSMateusz Guzik error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD); 2892afde2b65SKonstantin Belousov if (error != 0) 2893afde2b65SKonstantin Belousov return (error); 2894afde2b65SKonstantin Belousov #endif 2895afde2b65SKonstantin Belousov return (0); 2896afde2b65SKonstantin Belousov } 2897afde2b65SKonstantin Belousov 2898afde2b65SKonstantin Belousov static int 2899afde2b65SKonstantin Belousov mqunload(void) 2900afde2b65SKonstantin Belousov { 2901afde2b65SKonstantin Belousov 2902afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2903afde2b65SKonstantin Belousov syscall32_helper_unregister(mq32_syscalls); 2904afde2b65SKonstantin Belousov #endif 2905afde2b65SKonstantin Belousov syscall_helper_unregister(mq_syscalls); 2906afde2b65SKonstantin Belousov return (0); 2907afde2b65SKonstantin Belousov } 2908afde2b65SKonstantin Belousov 2909afde2b65SKonstantin Belousov static int 2910afde2b65SKonstantin Belousov mq_modload(struct module *module, int cmd, void *arg) 2911afde2b65SKonstantin Belousov { 2912afde2b65SKonstantin Belousov int error = 0; 2913afde2b65SKonstantin Belousov 2914afde2b65SKonstantin Belousov error = vfs_modevent(module, cmd, arg); 2915afde2b65SKonstantin Belousov if (error != 0) 2916afde2b65SKonstantin Belousov return (error); 2917afde2b65SKonstantin Belousov 2918afde2b65SKonstantin Belousov switch (cmd) { 2919afde2b65SKonstantin Belousov case MOD_LOAD: 2920afde2b65SKonstantin Belousov error = mqinit(); 2921afde2b65SKonstantin Belousov if (error != 0) 2922afde2b65SKonstantin Belousov mqunload(); 2923afde2b65SKonstantin Belousov break; 2924afde2b65SKonstantin Belousov case MOD_UNLOAD: 2925afde2b65SKonstantin Belousov error = mqunload(); 2926afde2b65SKonstantin Belousov break; 2927afde2b65SKonstantin Belousov default: 2928afde2b65SKonstantin Belousov break; 2929afde2b65SKonstantin Belousov } 2930afde2b65SKonstantin Belousov return (error); 2931afde2b65SKonstantin Belousov } 2932afde2b65SKonstantin Belousov 2933afde2b65SKonstantin Belousov static moduledata_t mqueuefs_mod = { 2934afde2b65SKonstantin Belousov "mqueuefs", 2935afde2b65SKonstantin Belousov mq_modload, 2936afde2b65SKonstantin Belousov &mqueuefs_vfsconf 2937afde2b65SKonstantin Belousov }; 2938afde2b65SKonstantin Belousov DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE); 293947bf2cf9SDavid Xu MODULE_VERSION(mqueuefs, 1); 2940