2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (michal.wronski@gmail.com)
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
11 * Audit: George Wilson (ltcgcw@us.ibm.com)
13 * This file is released under the GPL.
16 #include <linux/capability.h>
17 #include <linux/init.h>
18 #include <linux/pagemap.h>
19 #include <linux/file.h>
20 #include <linux/mount.h>
21 #include <linux/namei.h>
22 #include <linux/sysctl.h>
23 #include <linux/poll.h>
24 #include <linux/mqueue.h>
25 #include <linux/msg.h>
26 #include <linux/skbuff.h>
27 #include <linux/netlink.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/signal.h>
31 #include <linux/mutex.h>
32 #include <linux/nsproxy.h>
33 #include <linux/pid.h>
34 #include <linux/ipc_namespace.h>
39 #define MQUEUE_MAGIC 0x19800202
40 #define DIRENT_SIZE 20
41 #define FILENT_SIZE 80
47 #define STATE_PENDING 1
51 * Define the ranges various user-specified maximum values can
54 #define MIN_MSGMAX 1 /* min value for msg_max */
55 #define MAX_MSGMAX HARD_MSGMAX /* max value for msg_max */
56 #define MIN_MSGSIZEMAX 128 /* min value for msgsize_max */
57 #define MAX_MSGSIZEMAX (8192*128) /* max value for msgsize_max */
59 struct ext_wait_queue { /* queue of sleeping tasks */
60 struct task_struct *task;
61 struct list_head list;
62 struct msg_msg *msg; /* ptr of loaded message */
63 int state; /* one of STATE_* values */
66 struct mqueue_inode_info {
68 struct inode vfs_inode;
69 wait_queue_head_t wait_q;
71 struct msg_msg **messages;
74 struct sigevent notify;
75 struct pid* notify_owner;
76 struct user_struct *user; /* user who created, for accounting */
77 struct sock *notify_sock;
78 struct sk_buff *notify_cookie;
80 /* for tasks waiting for free space and messages, respectively */
81 struct ext_wait_queue e_wait_q[2];
83 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
86 static const struct inode_operations mqueue_dir_inode_operations;
87 static const struct file_operations mqueue_file_operations;
88 static struct super_operations mqueue_super_ops;
89 static void remove_notification(struct mqueue_inode_info *info);
91 static spinlock_t mq_lock;
92 static struct kmem_cache *mqueue_inode_cachep;
94 static struct ctl_table_header * mq_sysctl_table;
96 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
98 return container_of(inode, struct mqueue_inode_info, vfs_inode);
101 void mq_init_ns(struct ipc_namespace *ns)
103 ns->mq_queues_count = 0;
104 ns->mq_queues_max = DFLT_QUEUESMAX;
105 ns->mq_msg_max = DFLT_MSGMAX;
106 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
107 ns->mq_mnt = mntget(init_ipc_ns.mq_mnt);
110 void mq_exit_ns(struct ipc_namespace *ns)
112 /* will need to clear out ns->mq_mnt->mnt_sb->s_fs_info here */
116 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
117 struct mq_attr *attr)
119 struct user_struct *u = current_user();
121 struct ipc_namespace *ipc_ns = &init_ipc_ns;
123 inode = new_inode(sb);
125 inode->i_mode = mode;
126 inode->i_uid = current_fsuid();
127 inode->i_gid = current_fsgid();
128 inode->i_mtime = inode->i_ctime = inode->i_atime =
132 struct mqueue_inode_info *info;
133 struct task_struct *p = current;
134 unsigned long mq_bytes, mq_msg_tblsz;
136 inode->i_fop = &mqueue_file_operations;
137 inode->i_size = FILENT_SIZE;
138 /* mqueue specific info */
139 info = MQUEUE_I(inode);
140 spin_lock_init(&info->lock);
141 init_waitqueue_head(&info->wait_q);
142 INIT_LIST_HEAD(&info->e_wait_q[0].list);
143 INIT_LIST_HEAD(&info->e_wait_q[1].list);
144 info->messages = NULL;
145 info->notify_owner = NULL;
147 info->user = NULL; /* set when all is ok */
148 memset(&info->attr, 0, sizeof(info->attr));
149 info->attr.mq_maxmsg = ipc_ns->mq_msg_max;
150 info->attr.mq_msgsize = ipc_ns->mq_msgsize_max;
152 info->attr.mq_maxmsg = attr->mq_maxmsg;
153 info->attr.mq_msgsize = attr->mq_msgsize;
155 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
156 mq_bytes = (mq_msg_tblsz +
157 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
160 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
161 u->mq_bytes + mq_bytes >
162 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur) {
163 spin_unlock(&mq_lock);
166 u->mq_bytes += mq_bytes;
167 spin_unlock(&mq_lock);
169 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
170 if (!info->messages) {
172 u->mq_bytes -= mq_bytes;
173 spin_unlock(&mq_lock);
177 info->user = get_uid(u);
178 } else if (S_ISDIR(mode)) {
180 /* Some things misbehave if size == 0 on a directory */
181 inode->i_size = 2 * DIRENT_SIZE;
182 inode->i_op = &mqueue_dir_inode_operations;
183 inode->i_fop = &simple_dir_operations;
188 make_bad_inode(inode);
193 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
197 sb->s_blocksize = PAGE_CACHE_SIZE;
198 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
199 sb->s_magic = MQUEUE_MAGIC;
200 sb->s_op = &mqueue_super_ops;
202 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
206 sb->s_root = d_alloc_root(inode);
215 static int mqueue_get_sb(struct file_system_type *fs_type,
216 int flags, const char *dev_name,
217 void *data, struct vfsmount *mnt)
219 return get_sb_single(fs_type, flags, data, mqueue_fill_super, mnt);
222 static void init_once(void *foo)
224 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
226 inode_init_once(&p->vfs_inode);
229 static struct inode *mqueue_alloc_inode(struct super_block *sb)
231 struct mqueue_inode_info *ei;
233 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
236 return &ei->vfs_inode;
239 static void mqueue_destroy_inode(struct inode *inode)
241 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
244 static void mqueue_delete_inode(struct inode *inode)
246 struct mqueue_inode_info *info;
247 struct user_struct *user;
248 unsigned long mq_bytes;
250 struct ipc_namespace *ipc_ns = &init_ipc_ns;
252 if (S_ISDIR(inode->i_mode)) {
256 info = MQUEUE_I(inode);
257 spin_lock(&info->lock);
258 for (i = 0; i < info->attr.mq_curmsgs; i++)
259 free_msg(info->messages[i]);
260 kfree(info->messages);
261 spin_unlock(&info->lock);
265 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
266 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
270 user->mq_bytes -= mq_bytes;
271 ipc_ns->mq_queues_count--;
272 spin_unlock(&mq_lock);
277 static int mqueue_create(struct inode *dir, struct dentry *dentry,
278 int mode, struct nameidata *nd)
281 struct mq_attr *attr = dentry->d_fsdata;
283 struct ipc_namespace *ipc_ns = &init_ipc_ns;
286 if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
287 !capable(CAP_SYS_RESOURCE)) {
291 ipc_ns->mq_queues_count++;
292 spin_unlock(&mq_lock);
294 inode = mqueue_get_inode(dir->i_sb, mode, attr);
298 ipc_ns->mq_queues_count--;
302 dir->i_size += DIRENT_SIZE;
303 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
305 d_instantiate(dentry, inode);
309 spin_unlock(&mq_lock);
313 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
315 struct inode *inode = dentry->d_inode;
317 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
318 dir->i_size -= DIRENT_SIZE;
325 * This is routine for system read from queue file.
326 * To avoid mess with doing here some sort of mq_receive we allow
327 * to read only queue size & notification info (the only values
328 * that are interesting from user point of view and aren't accessible
329 * through std routines)
331 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
332 size_t count, loff_t *off)
334 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
335 char buffer[FILENT_SIZE];
338 spin_lock(&info->lock);
339 snprintf(buffer, sizeof(buffer),
340 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
342 info->notify_owner ? info->notify.sigev_notify : 0,
343 (info->notify_owner &&
344 info->notify.sigev_notify == SIGEV_SIGNAL) ?
345 info->notify.sigev_signo : 0,
346 pid_vnr(info->notify_owner));
347 spin_unlock(&info->lock);
348 buffer[sizeof(buffer)-1] = '\0';
350 ret = simple_read_from_buffer(u_data, count, off, buffer,
355 filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
359 static int mqueue_flush_file(struct file *filp, fl_owner_t id)
361 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
363 spin_lock(&info->lock);
364 if (task_tgid(current) == info->notify_owner)
365 remove_notification(info);
367 spin_unlock(&info->lock);
371 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
373 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
376 poll_wait(filp, &info->wait_q, poll_tab);
378 spin_lock(&info->lock);
379 if (info->attr.mq_curmsgs)
380 retval = POLLIN | POLLRDNORM;
382 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
383 retval |= POLLOUT | POLLWRNORM;
384 spin_unlock(&info->lock);
389 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
390 static void wq_add(struct mqueue_inode_info *info, int sr,
391 struct ext_wait_queue *ewp)
393 struct ext_wait_queue *walk;
397 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
398 if (walk->task->static_prio <= current->static_prio) {
399 list_add_tail(&ewp->list, &walk->list);
403 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
407 * Puts current task to sleep. Caller must hold queue lock. After return
411 static int wq_sleep(struct mqueue_inode_info *info, int sr,
412 long timeout, struct ext_wait_queue *ewp)
417 wq_add(info, sr, ewp);
420 set_current_state(TASK_INTERRUPTIBLE);
422 spin_unlock(&info->lock);
423 time = schedule_timeout(timeout);
425 while (ewp->state == STATE_PENDING)
428 if (ewp->state == STATE_READY) {
432 spin_lock(&info->lock);
433 if (ewp->state == STATE_READY) {
437 if (signal_pending(current)) {
438 retval = -ERESTARTSYS;
446 list_del(&ewp->list);
448 spin_unlock(&info->lock);
454 * Returns waiting task that should be serviced first or NULL if none exists
456 static struct ext_wait_queue *wq_get_first_waiter(
457 struct mqueue_inode_info *info, int sr)
459 struct list_head *ptr;
461 ptr = info->e_wait_q[sr].list.prev;
462 if (ptr == &info->e_wait_q[sr].list)
464 return list_entry(ptr, struct ext_wait_queue, list);
467 /* Auxiliary functions to manipulate messages' list */
468 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
472 k = info->attr.mq_curmsgs - 1;
473 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
474 info->messages[k + 1] = info->messages[k];
477 info->attr.mq_curmsgs++;
478 info->qsize += ptr->m_ts;
479 info->messages[k + 1] = ptr;
482 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
484 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
485 return info->messages[info->attr.mq_curmsgs];
488 static inline void set_cookie(struct sk_buff *skb, char code)
490 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
494 * The next function is only to split too long sys_mq_timedsend
496 static void __do_notify(struct mqueue_inode_info *info)
499 * invoked when there is registered process and there isn't process
500 * waiting synchronously for message AND state of queue changed from
501 * empty to not empty. Here we are sure that no one is waiting
503 if (info->notify_owner &&
504 info->attr.mq_curmsgs == 1) {
505 struct siginfo sig_i;
506 switch (info->notify.sigev_notify) {
512 sig_i.si_signo = info->notify.sigev_signo;
514 sig_i.si_code = SI_MESGQ;
515 sig_i.si_value = info->notify.sigev_value;
516 sig_i.si_pid = task_tgid_nr_ns(current,
517 ns_of_pid(info->notify_owner));
518 sig_i.si_uid = current_uid();
520 kill_pid_info(info->notify.sigev_signo,
521 &sig_i, info->notify_owner);
524 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
525 netlink_sendskb(info->notify_sock, info->notify_cookie);
528 /* after notification unregisters process */
529 put_pid(info->notify_owner);
530 info->notify_owner = NULL;
532 wake_up(&info->wait_q);
535 static long prepare_timeout(struct timespec *p)
537 struct timespec nowts;
541 if (unlikely(p->tv_nsec < 0 || p->tv_sec < 0
542 || p->tv_nsec >= NSEC_PER_SEC))
544 nowts = CURRENT_TIME;
545 /* first subtract as jiffies can't be too big */
546 p->tv_sec -= nowts.tv_sec;
547 if (p->tv_nsec < nowts.tv_nsec) {
548 p->tv_nsec += NSEC_PER_SEC;
551 p->tv_nsec -= nowts.tv_nsec;
555 timeout = timespec_to_jiffies(p) + 1;
557 return MAX_SCHEDULE_TIMEOUT;
562 static void remove_notification(struct mqueue_inode_info *info)
564 if (info->notify_owner != NULL &&
565 info->notify.sigev_notify == SIGEV_THREAD) {
566 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
567 netlink_sendskb(info->notify_sock, info->notify_cookie);
569 put_pid(info->notify_owner);
570 info->notify_owner = NULL;
573 static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
575 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
577 if (capable(CAP_SYS_RESOURCE)) {
578 if (attr->mq_maxmsg > HARD_MSGMAX)
581 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
582 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
585 /* check for overflow */
586 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
588 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
589 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
590 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
596 * Invoked when creating a new queue via sys_mq_open
598 static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
599 struct dentry *dentry, int oflag, mode_t mode,
600 struct mq_attr *attr)
602 const struct cred *cred = current_cred();
608 if (!mq_attr_ok(ipc_ns, attr))
610 /* store for use during create */
611 dentry->d_fsdata = attr;
614 mode &= ~current_umask();
615 ret = mnt_want_write(ipc_ns->mq_mnt);
618 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
619 dentry->d_fsdata = NULL;
623 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
625 * dentry_open() took a persistent mnt_want_write(),
626 * so we can now drop this one.
628 mnt_drop_write(ipc_ns->mq_mnt);
632 mnt_drop_write(ipc_ns->mq_mnt);
635 mntput(ipc_ns->mq_mnt);
639 /* Opens existing queue */
640 static struct file *do_open(struct ipc_namespace *ipc_ns,
641 struct dentry *dentry, int oflag)
643 const struct cred *cred = current_cred();
645 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
646 MAY_READ | MAY_WRITE };
648 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
650 mntput(ipc_ns->mq_mnt);
651 return ERR_PTR(-EINVAL);
654 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
656 mntput(ipc_ns->mq_mnt);
657 return ERR_PTR(-EACCES);
660 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
663 SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, mode_t, mode,
664 struct mq_attr __user *, u_attr)
666 struct dentry *dentry;
671 struct ipc_namespace *ipc_ns = &init_ipc_ns;
673 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
676 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
678 if (IS_ERR(name = getname(u_name)))
679 return PTR_ERR(name);
681 fd = get_unused_fd_flags(O_CLOEXEC);
685 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
686 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
687 if (IS_ERR(dentry)) {
688 error = PTR_ERR(dentry);
691 mntget(ipc_ns->mq_mnt);
693 if (oflag & O_CREAT) {
694 if (dentry->d_inode) { /* entry already exists */
695 audit_inode(name, dentry);
699 filp = do_open(ipc_ns, dentry, oflag);
701 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
703 u_attr ? &attr : NULL);
707 if (!dentry->d_inode)
709 audit_inode(name, dentry);
710 filp = do_open(ipc_ns, dentry, oflag);
714 error = PTR_ERR(filp);
718 fd_install(fd, filp);
723 mntput(ipc_ns->mq_mnt);
729 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
735 SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
739 struct dentry *dentry;
740 struct inode *inode = NULL;
741 struct ipc_namespace *ipc_ns = &init_ipc_ns;
743 name = getname(u_name);
745 return PTR_ERR(name);
747 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
749 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
750 if (IS_ERR(dentry)) {
751 err = PTR_ERR(dentry);
755 if (!dentry->d_inode) {
760 inode = dentry->d_inode;
762 atomic_inc(&inode->i_count);
763 err = mnt_want_write(ipc_ns->mq_mnt);
766 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
767 mnt_drop_write(ipc_ns->mq_mnt);
772 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
780 /* Pipelined send and receive functions.
782 * If a receiver finds no waiting message, then it registers itself in the
783 * list of waiting receivers. A sender checks that list before adding the new
784 * message into the message array. If there is a waiting receiver, then it
785 * bypasses the message array and directly hands the message over to the
787 * The receiver accepts the message and returns without grabbing the queue
788 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
789 * are necessary. The same algorithm is used for sysv semaphores, see
790 * ipc/sem.c for more details.
792 * The same algorithm is used for senders.
795 /* pipelined_send() - send a message directly to the task waiting in
796 * sys_mq_timedreceive() (without inserting message into a queue).
798 static inline void pipelined_send(struct mqueue_inode_info *info,
799 struct msg_msg *message,
800 struct ext_wait_queue *receiver)
802 receiver->msg = message;
803 list_del(&receiver->list);
804 receiver->state = STATE_PENDING;
805 wake_up_process(receiver->task);
807 receiver->state = STATE_READY;
810 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
811 * gets its message and put to the queue (we have one free place for sure). */
812 static inline void pipelined_receive(struct mqueue_inode_info *info)
814 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
818 wake_up_interruptible(&info->wait_q);
821 msg_insert(sender->msg, info);
822 list_del(&sender->list);
823 sender->state = STATE_PENDING;
824 wake_up_process(sender->task);
826 sender->state = STATE_READY;
829 SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
830 size_t, msg_len, unsigned int, msg_prio,
831 const struct timespec __user *, u_abs_timeout)
835 struct ext_wait_queue wait;
836 struct ext_wait_queue *receiver;
837 struct msg_msg *msg_ptr;
838 struct mqueue_inode_info *info;
839 struct timespec ts, *p = NULL;
844 if (copy_from_user(&ts, u_abs_timeout,
845 sizeof(struct timespec)))
850 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
853 audit_mq_sendrecv(mqdes, msg_len, msg_prio, p);
854 timeout = prepare_timeout(p);
861 inode = filp->f_path.dentry->d_inode;
862 if (unlikely(filp->f_op != &mqueue_file_operations))
864 info = MQUEUE_I(inode);
865 audit_inode(NULL, filp->f_path.dentry);
867 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
870 if (unlikely(msg_len > info->attr.mq_msgsize)) {
875 /* First try to allocate memory, before doing anything with
876 * existing queues. */
877 msg_ptr = load_msg(u_msg_ptr, msg_len);
878 if (IS_ERR(msg_ptr)) {
879 ret = PTR_ERR(msg_ptr);
882 msg_ptr->m_ts = msg_len;
883 msg_ptr->m_type = msg_prio;
885 spin_lock(&info->lock);
887 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
888 if (filp->f_flags & O_NONBLOCK) {
889 spin_unlock(&info->lock);
891 } else if (unlikely(timeout < 0)) {
892 spin_unlock(&info->lock);
896 wait.msg = (void *) msg_ptr;
897 wait.state = STATE_NONE;
898 ret = wq_sleep(info, SEND, timeout, &wait);
903 receiver = wq_get_first_waiter(info, RECV);
905 pipelined_send(info, msg_ptr, receiver);
907 /* adds message to the queue */
908 msg_insert(msg_ptr, info);
911 inode->i_atime = inode->i_mtime = inode->i_ctime =
913 spin_unlock(&info->lock);
922 SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
923 size_t, msg_len, unsigned int __user *, u_msg_prio,
924 const struct timespec __user *, u_abs_timeout)
928 struct msg_msg *msg_ptr;
931 struct mqueue_inode_info *info;
932 struct ext_wait_queue wait;
933 struct timespec ts, *p = NULL;
936 if (copy_from_user(&ts, u_abs_timeout,
937 sizeof(struct timespec)))
942 audit_mq_sendrecv(mqdes, msg_len, 0, p);
943 timeout = prepare_timeout(p);
950 inode = filp->f_path.dentry->d_inode;
951 if (unlikely(filp->f_op != &mqueue_file_operations))
953 info = MQUEUE_I(inode);
954 audit_inode(NULL, filp->f_path.dentry);
956 if (unlikely(!(filp->f_mode & FMODE_READ)))
959 /* checks if buffer is big enough */
960 if (unlikely(msg_len < info->attr.mq_msgsize)) {
965 spin_lock(&info->lock);
966 if (info->attr.mq_curmsgs == 0) {
967 if (filp->f_flags & O_NONBLOCK) {
968 spin_unlock(&info->lock);
971 } else if (unlikely(timeout < 0)) {
972 spin_unlock(&info->lock);
977 wait.state = STATE_NONE;
978 ret = wq_sleep(info, RECV, timeout, &wait);
982 msg_ptr = msg_get(info);
984 inode->i_atime = inode->i_mtime = inode->i_ctime =
987 /* There is now free space in queue. */
988 pipelined_receive(info);
989 spin_unlock(&info->lock);
995 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
996 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1008 * Notes: the case when user wants us to deregister (with NULL as pointer)
1009 * and he isn't currently owner of notification, will be silently discarded.
1010 * It isn't explicitly defined in the POSIX.
1012 SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1013 const struct sigevent __user *, u_notification)
1018 struct inode *inode;
1019 struct sigevent notification;
1020 struct mqueue_inode_info *info;
1023 if (u_notification) {
1024 if (copy_from_user(¬ification, u_notification,
1025 sizeof(struct sigevent)))
1029 audit_mq_notify(mqdes, u_notification ? ¬ification : NULL);
1033 if (u_notification != NULL) {
1034 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1035 notification.sigev_notify != SIGEV_SIGNAL &&
1036 notification.sigev_notify != SIGEV_THREAD))
1038 if (notification.sigev_notify == SIGEV_SIGNAL &&
1039 !valid_signal(notification.sigev_signo)) {
1042 if (notification.sigev_notify == SIGEV_THREAD) {
1045 /* create the notify skb */
1046 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1051 if (copy_from_user(nc->data,
1052 notification.sigev_value.sival_ptr,
1053 NOTIFY_COOKIE_LEN)) {
1057 /* TODO: add a header? */
1058 skb_put(nc, NOTIFY_COOKIE_LEN);
1059 /* and attach it to the socket */
1061 filp = fget(notification.sigev_signo);
1065 sock = netlink_getsockbyfilp(filp);
1068 ret = PTR_ERR(sock);
1073 timeo = MAX_SCHEDULE_TIMEOUT;
1074 ret = netlink_attachskb(sock, nc, &timeo, NULL);
1090 inode = filp->f_path.dentry->d_inode;
1091 if (unlikely(filp->f_op != &mqueue_file_operations))
1093 info = MQUEUE_I(inode);
1096 spin_lock(&info->lock);
1097 if (u_notification == NULL) {
1098 if (info->notify_owner == task_tgid(current)) {
1099 remove_notification(info);
1100 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1102 } else if (info->notify_owner != NULL) {
1105 switch (notification.sigev_notify) {
1107 info->notify.sigev_notify = SIGEV_NONE;
1110 info->notify_sock = sock;
1111 info->notify_cookie = nc;
1114 info->notify.sigev_notify = SIGEV_THREAD;
1117 info->notify.sigev_signo = notification.sigev_signo;
1118 info->notify.sigev_value = notification.sigev_value;
1119 info->notify.sigev_notify = SIGEV_SIGNAL;
1123 info->notify_owner = get_pid(task_tgid(current));
1124 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1126 spin_unlock(&info->lock);
1131 netlink_detachskb(sock, nc);
1138 SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1139 const struct mq_attr __user *, u_mqstat,
1140 struct mq_attr __user *, u_omqstat)
1143 struct mq_attr mqstat, omqstat;
1145 struct inode *inode;
1146 struct mqueue_inode_info *info;
1148 if (u_mqstat != NULL) {
1149 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1151 if (mqstat.mq_flags & (~O_NONBLOCK))
1160 inode = filp->f_path.dentry->d_inode;
1161 if (unlikely(filp->f_op != &mqueue_file_operations))
1163 info = MQUEUE_I(inode);
1165 spin_lock(&info->lock);
1167 omqstat = info->attr;
1168 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1170 audit_mq_getsetattr(mqdes, &mqstat);
1171 spin_lock(&filp->f_lock);
1172 if (mqstat.mq_flags & O_NONBLOCK)
1173 filp->f_flags |= O_NONBLOCK;
1175 filp->f_flags &= ~O_NONBLOCK;
1176 spin_unlock(&filp->f_lock);
1178 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1181 spin_unlock(&info->lock);
1184 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1185 sizeof(struct mq_attr)))
1194 static const struct inode_operations mqueue_dir_inode_operations = {
1195 .lookup = simple_lookup,
1196 .create = mqueue_create,
1197 .unlink = mqueue_unlink,
1200 static const struct file_operations mqueue_file_operations = {
1201 .flush = mqueue_flush_file,
1202 .poll = mqueue_poll_file,
1203 .read = mqueue_read_file,
1206 static struct super_operations mqueue_super_ops = {
1207 .alloc_inode = mqueue_alloc_inode,
1208 .destroy_inode = mqueue_destroy_inode,
1209 .statfs = simple_statfs,
1210 .delete_inode = mqueue_delete_inode,
1211 .drop_inode = generic_delete_inode,
1214 static struct file_system_type mqueue_fs_type = {
1216 .get_sb = mqueue_get_sb,
1217 .kill_sb = kill_litter_super,
1220 static int msg_max_limit_min = MIN_MSGMAX;
1221 static int msg_max_limit_max = MAX_MSGMAX;
1223 static int msg_maxsize_limit_min = MIN_MSGSIZEMAX;
1224 static int msg_maxsize_limit_max = MAX_MSGSIZEMAX;
1226 static ctl_table mq_sysctls[] = {
1228 .procname = "queues_max",
1229 .data = &init_ipc_ns.mq_queues_max,
1230 .maxlen = sizeof(int),
1232 .proc_handler = &proc_dointvec,
1235 .procname = "msg_max",
1236 .data = &init_ipc_ns.mq_msg_max,
1237 .maxlen = sizeof(int),
1239 .proc_handler = &proc_dointvec_minmax,
1240 .extra1 = &msg_max_limit_min,
1241 .extra2 = &msg_max_limit_max,
1244 .procname = "msgsize_max",
1245 .data = &init_ipc_ns.mq_msgsize_max,
1246 .maxlen = sizeof(int),
1248 .proc_handler = &proc_dointvec_minmax,
1249 .extra1 = &msg_maxsize_limit_min,
1250 .extra2 = &msg_maxsize_limit_max,
1255 static ctl_table mq_sysctl_dir[] = {
1257 .procname = "mqueue",
1259 .child = mq_sysctls,
1264 static ctl_table mq_sysctl_root[] = {
1269 .child = mq_sysctl_dir,
1274 static int __init init_mqueue_fs(void)
1278 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1279 sizeof(struct mqueue_inode_info), 0,
1280 SLAB_HWCACHE_ALIGN, init_once);
1281 if (mqueue_inode_cachep == NULL)
1284 /* ignore failues - they are not fatal */
1285 mq_sysctl_table = register_sysctl_table(mq_sysctl_root);
1287 error = register_filesystem(&mqueue_fs_type);
1291 init_ipc_ns.mq_mnt = kern_mount(&mqueue_fs_type);
1292 if (IS_ERR(init_ipc_ns.mq_mnt)) {
1293 error = PTR_ERR(init_ipc_ns.mq_mnt);
1294 goto out_filesystem;
1297 /* internal initialization - not common for vfs */
1298 spin_lock_init(&mq_lock);
1303 unregister_filesystem(&mqueue_fs_type);
1305 if (mq_sysctl_table)
1306 unregister_sysctl_table(mq_sysctl_table);
1307 kmem_cache_destroy(mqueue_inode_cachep);
1311 __initcall(init_mqueue_fs);