2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (Michal.Wronski@motorola.com)
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
11 * This file is released under the GPL.
14 #include <linux/capability.h>
15 #include <linux/init.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/mount.h>
19 #include <linux/namei.h>
20 #include <linux/sysctl.h>
21 #include <linux/poll.h>
22 #include <linux/mqueue.h>
23 #include <linux/msg.h>
24 #include <linux/skbuff.h>
25 #include <linux/netlink.h>
26 #include <linux/syscalls.h>
27 #include <linux/signal.h>
28 #include <linux/vs_context.h>
29 #include <linux/vs_limit.h>
33 #define MQUEUE_MAGIC 0x19800202
34 #define DIRENT_SIZE 20
35 #define FILENT_SIZE 80
41 #define STATE_PENDING 1
46 #define CTL_QUEUESMAX 2
48 #define CTL_MSGSIZEMAX 4
51 #define DFLT_QUEUESMAX 256 /* max number of message queues */
52 #define DFLT_MSGMAX 10 /* max number of messages in each queue */
53 #define HARD_MSGMAX (131072/sizeof(void*))
54 #define DFLT_MSGSIZEMAX 8192 /* max message size */
56 #define NOTIFY_COOKIE_LEN 32
58 struct ext_wait_queue { /* queue of sleeping tasks */
59 struct task_struct *task;
60 struct list_head list;
61 struct msg_msg *msg; /* ptr of loaded message */
62 int state; /* one of STATE_* values */
65 struct mqueue_inode_info {
67 struct inode vfs_inode;
68 wait_queue_head_t wait_q;
70 struct msg_msg **messages;
73 struct sigevent notify;
75 struct user_struct *user; /* user who created, for accounting */
76 struct sock *notify_sock;
77 struct sk_buff *notify_cookie;
79 /* for tasks waiting for free space and messages, respectively */
80 struct ext_wait_queue e_wait_q[2];
82 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
85 static struct inode_operations mqueue_dir_inode_operations;
86 static struct file_operations mqueue_file_operations;
87 static struct super_operations mqueue_super_ops;
88 static void remove_notification(struct mqueue_inode_info *info);
90 static spinlock_t mq_lock;
91 static kmem_cache_t *mqueue_inode_cachep;
92 static struct vfsmount *mqueue_mnt;
94 static unsigned int queues_count;
95 static unsigned int queues_max = DFLT_QUEUESMAX;
96 static unsigned int msg_max = DFLT_MSGMAX;
97 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
99 static struct ctl_table_header * mq_sysctl_table;
101 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
103 return container_of(inode, struct mqueue_inode_info, vfs_inode);
106 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
107 struct mq_attr *attr)
111 inode = new_inode(sb);
113 inode->i_mode = mode;
114 inode->i_uid = current->fsuid;
115 inode->i_gid = current->fsgid;
116 inode->i_blksize = PAGE_CACHE_SIZE;
118 inode->i_mtime = inode->i_ctime = inode->i_atime =
122 struct mqueue_inode_info *info;
123 struct task_struct *p = current;
124 struct user_struct *u = p->user;
125 unsigned long mq_bytes, mq_msg_tblsz;
127 inode->i_fop = &mqueue_file_operations;
128 inode->i_size = FILENT_SIZE;
129 /* mqueue specific info */
130 info = MQUEUE_I(inode);
131 spin_lock_init(&info->lock);
132 init_waitqueue_head(&info->wait_q);
133 INIT_LIST_HEAD(&info->e_wait_q[0].list);
134 INIT_LIST_HEAD(&info->e_wait_q[1].list);
135 info->messages = NULL;
136 info->notify_owner = 0;
138 info->user = NULL; /* set when all is ok */
139 memset(&info->attr, 0, sizeof(info->attr));
140 info->attr.mq_maxmsg = DFLT_MSGMAX;
141 info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
143 info->attr.mq_maxmsg = attr->mq_maxmsg;
144 info->attr.mq_msgsize = attr->mq_msgsize;
146 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
147 mq_bytes = (mq_msg_tblsz +
148 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
151 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
152 u->mq_bytes + mq_bytes >
153 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur ||
154 !vx_ipcmsg_avail(p->vx_info, mq_bytes)) {
155 spin_unlock(&mq_lock);
158 u->mq_bytes += mq_bytes;
159 vx_ipcmsg_add(p->vx_info, u, mq_bytes);
160 spin_unlock(&mq_lock);
162 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
163 if (!info->messages) {
165 u->mq_bytes -= mq_bytes;
166 vx_ipcmsg_sub(p->vx_info, u, mq_bytes);
167 spin_unlock(&mq_lock);
171 info->user = get_uid(u);
172 } else if (S_ISDIR(mode)) {
174 /* Some things misbehave if size == 0 on a directory */
175 inode->i_size = 2 * DIRENT_SIZE;
176 inode->i_op = &mqueue_dir_inode_operations;
177 inode->i_fop = &simple_dir_operations;
182 make_bad_inode(inode);
187 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
191 sb->s_blocksize = PAGE_CACHE_SIZE;
192 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
193 sb->s_magic = MQUEUE_MAGIC;
194 sb->s_op = &mqueue_super_ops;
196 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
200 sb->s_root = d_alloc_root(inode);
209 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
210 int flags, const char *dev_name,
213 return get_sb_single(fs_type, flags, data, mqueue_fill_super);
216 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
218 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
220 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
221 SLAB_CTOR_CONSTRUCTOR)
222 inode_init_once(&p->vfs_inode);
225 static struct inode *mqueue_alloc_inode(struct super_block *sb)
227 struct mqueue_inode_info *ei;
229 ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
232 return &ei->vfs_inode;
235 static void mqueue_destroy_inode(struct inode *inode)
237 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
240 static void mqueue_delete_inode(struct inode *inode)
242 struct mqueue_inode_info *info;
243 struct user_struct *user;
244 unsigned long mq_bytes;
247 if (S_ISDIR(inode->i_mode)) {
251 info = MQUEUE_I(inode);
252 spin_lock(&info->lock);
253 for (i = 0; i < info->attr.mq_curmsgs; i++)
254 free_msg(info->messages[i]);
255 kfree(info->messages);
256 spin_unlock(&info->lock);
260 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
261 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
264 struct vx_info *vxi = lookup_vx_info(user->xid);
267 user->mq_bytes -= mq_bytes;
268 vx_ipcmsg_sub(vxi, user, mq_bytes);
270 spin_unlock(&mq_lock);
276 static int mqueue_create(struct inode *dir, struct dentry *dentry,
277 int mode, struct nameidata *nd)
280 struct mq_attr *attr = dentry->d_fsdata;
284 if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
289 spin_unlock(&mq_lock);
291 inode = mqueue_get_inode(dir->i_sb, mode, attr);
299 dir->i_size += DIRENT_SIZE;
300 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
302 d_instantiate(dentry, inode);
306 spin_unlock(&mq_lock);
310 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
312 struct inode *inode = dentry->d_inode;
314 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
315 dir->i_size -= DIRENT_SIZE;
322 * This is routine for system read from queue file.
323 * To avoid mess with doing here some sort of mq_receive we allow
324 * to read only queue size & notification info (the only values
325 * that are interesting from user point of view and aren't accessible
326 * through std routines)
328 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
329 size_t count, loff_t * off)
331 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
332 char buffer[FILENT_SIZE];
339 spin_lock(&info->lock);
340 snprintf(buffer, sizeof(buffer),
341 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
343 info->notify_owner ? info->notify.sigev_notify : 0,
344 (info->notify_owner &&
345 info->notify.sigev_notify == SIGEV_SIGNAL) ?
346 info->notify.sigev_signo : 0,
348 spin_unlock(&info->lock);
349 buffer[sizeof(buffer)-1] = '\0';
350 slen = strlen(buffer)+1;
356 if (o + count > slen)
359 if (copy_to_user(u_data, buffer + o, count))
363 filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
367 static int mqueue_flush_file(struct file *filp)
369 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
371 spin_lock(&info->lock);
372 if (current->tgid == info->notify_owner)
373 remove_notification(info);
375 spin_unlock(&info->lock);
379 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
381 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
384 poll_wait(filp, &info->wait_q, poll_tab);
386 spin_lock(&info->lock);
387 if (info->attr.mq_curmsgs)
388 retval = POLLIN | POLLRDNORM;
390 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
391 retval |= POLLOUT | POLLWRNORM;
392 spin_unlock(&info->lock);
397 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
398 static void wq_add(struct mqueue_inode_info *info, int sr,
399 struct ext_wait_queue *ewp)
401 struct ext_wait_queue *walk;
405 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
406 if (walk->task->static_prio <= current->static_prio) {
407 list_add_tail(&ewp->list, &walk->list);
411 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
415 * Puts current task to sleep. Caller must hold queue lock. After return
419 static int wq_sleep(struct mqueue_inode_info *info, int sr,
420 long timeout, struct ext_wait_queue *ewp)
425 wq_add(info, sr, ewp);
428 set_current_state(TASK_INTERRUPTIBLE);
430 spin_unlock(&info->lock);
431 time = schedule_timeout(timeout);
433 while (ewp->state == STATE_PENDING)
436 if (ewp->state == STATE_READY) {
440 spin_lock(&info->lock);
441 if (ewp->state == STATE_READY) {
445 if (signal_pending(current)) {
446 retval = -ERESTARTSYS;
454 list_del(&ewp->list);
456 spin_unlock(&info->lock);
462 * Returns waiting task that should be serviced first or NULL if none exists
464 static struct ext_wait_queue *wq_get_first_waiter(
465 struct mqueue_inode_info *info, int sr)
467 struct list_head *ptr;
469 ptr = info->e_wait_q[sr].list.prev;
470 if (ptr == &info->e_wait_q[sr].list)
472 return list_entry(ptr, struct ext_wait_queue, list);
475 /* Auxiliary functions to manipulate messages' list */
476 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
480 k = info->attr.mq_curmsgs - 1;
481 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
482 info->messages[k + 1] = info->messages[k];
485 info->attr.mq_curmsgs++;
486 info->qsize += ptr->m_ts;
487 info->messages[k + 1] = ptr;
490 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
492 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
493 return info->messages[info->attr.mq_curmsgs];
496 static inline void set_cookie(struct sk_buff *skb, char code)
498 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
502 * The next function is only to split too long sys_mq_timedsend
504 static void __do_notify(struct mqueue_inode_info *info)
507 * invoked when there is registered process and there isn't process
508 * waiting synchronously for message AND state of queue changed from
509 * empty to not empty. Here we are sure that no one is waiting
511 if (info->notify_owner &&
512 info->attr.mq_curmsgs == 1) {
513 struct siginfo sig_i;
514 switch (info->notify.sigev_notify) {
520 sig_i.si_signo = info->notify.sigev_signo;
522 sig_i.si_code = SI_MESGQ;
523 sig_i.si_value = info->notify.sigev_value;
524 sig_i.si_pid = current->tgid;
525 sig_i.si_uid = current->uid;
527 kill_proc_info(info->notify.sigev_signo,
528 &sig_i, info->notify_owner);
531 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
532 netlink_sendskb(info->notify_sock,
533 info->notify_cookie, 0);
536 /* after notification unregisters process */
537 info->notify_owner = 0;
539 wake_up(&info->wait_q);
542 static long prepare_timeout(const struct timespec __user *u_arg)
544 struct timespec ts, nowts;
548 if (unlikely(copy_from_user(&ts, u_arg,
549 sizeof(struct timespec))))
552 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
553 || ts.tv_nsec >= NSEC_PER_SEC))
555 nowts = CURRENT_TIME;
556 /* first subtract as jiffies can't be too big */
557 ts.tv_sec -= nowts.tv_sec;
558 if (ts.tv_nsec < nowts.tv_nsec) {
559 ts.tv_nsec += NSEC_PER_SEC;
562 ts.tv_nsec -= nowts.tv_nsec;
566 timeout = timespec_to_jiffies(&ts) + 1;
568 return MAX_SCHEDULE_TIMEOUT;
573 static void remove_notification(struct mqueue_inode_info *info)
575 if (info->notify_owner != 0 &&
576 info->notify.sigev_notify == SIGEV_THREAD) {
577 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
578 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
580 info->notify_owner = 0;
583 static int mq_attr_ok(struct mq_attr *attr)
585 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
587 if (capable(CAP_SYS_RESOURCE)) {
588 if (attr->mq_maxmsg > HARD_MSGMAX)
591 if (attr->mq_maxmsg > msg_max ||
592 attr->mq_msgsize > msgsize_max)
595 /* check for overflow */
596 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
598 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
599 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
600 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
606 * Invoked when creating a new queue via sys_mq_open
608 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
609 int oflag, mode_t mode, struct mq_attr __user *u_attr)
616 if (copy_from_user(&attr, u_attr, sizeof(attr)))
619 if (!mq_attr_ok(&attr))
621 /* store for use during create */
622 dentry->d_fsdata = &attr;
625 mode &= ~current->fs->umask;
626 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
627 dentry->d_fsdata = NULL;
631 return dentry_open(dentry, mqueue_mnt, oflag);
639 /* Opens existing queue */
640 static struct file *do_open(struct dentry *dentry, int oflag)
642 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
643 MAY_READ | MAY_WRITE };
645 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
648 return ERR_PTR(-EINVAL);
651 if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
654 return ERR_PTR(-EACCES);
657 return dentry_open(dentry, mqueue_mnt, oflag);
660 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
661 struct mq_attr __user *u_attr)
663 struct dentry *dentry;
668 if (IS_ERR(name = getname(u_name)))
669 return PTR_ERR(name);
671 fd = get_unused_fd();
675 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
676 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
677 if (IS_ERR(dentry)) {
678 error = PTR_ERR(dentry);
683 if (oflag & O_CREAT) {
684 if (dentry->d_inode) { /* entry already exists */
688 filp = do_open(dentry, oflag);
690 filp = do_create(mqueue_mnt->mnt_root, dentry,
691 oflag, mode, u_attr);
695 if (!dentry->d_inode)
697 filp = do_open(dentry, oflag);
701 error = PTR_ERR(filp);
705 set_close_on_exec(fd, 1);
706 fd_install(fd, filp);
717 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
723 asmlinkage long sys_mq_unlink(const char __user *u_name)
727 struct dentry *dentry;
728 struct inode *inode = NULL;
730 name = getname(u_name);
732 return PTR_ERR(name);
734 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
735 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
736 if (IS_ERR(dentry)) {
737 err = PTR_ERR(dentry);
741 if (!dentry->d_inode) {
746 inode = dentry->d_inode;
748 atomic_inc(&inode->i_count);
750 err = vfs_unlink(dentry->d_parent->d_inode, dentry, NULL);
755 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
763 /* Pipelined send and receive functions.
765 * If a receiver finds no waiting message, then it registers itself in the
766 * list of waiting receivers. A sender checks that list before adding the new
767 * message into the message array. If there is a waiting receiver, then it
768 * bypasses the message array and directly hands the message over to the
770 * The receiver accepts the message and returns without grabbing the queue
771 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
772 * are necessary. The same algorithm is used for sysv semaphores, see
773 * ipc/sem.c fore more details.
775 * The same algorithm is used for senders.
778 /* pipelined_send() - send a message directly to the task waiting in
779 * sys_mq_timedreceive() (without inserting message into a queue).
781 static inline void pipelined_send(struct mqueue_inode_info *info,
782 struct msg_msg *message,
783 struct ext_wait_queue *receiver)
785 receiver->msg = message;
786 list_del(&receiver->list);
787 receiver->state = STATE_PENDING;
788 wake_up_process(receiver->task);
790 receiver->state = STATE_READY;
793 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
794 * gets its message and put to the queue (we have one free place for sure). */
795 static inline void pipelined_receive(struct mqueue_inode_info *info)
797 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
801 wake_up_interruptible(&info->wait_q);
804 msg_insert(sender->msg, info);
805 list_del(&sender->list);
806 sender->state = STATE_PENDING;
807 wake_up_process(sender->task);
809 sender->state = STATE_READY;
812 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
813 size_t msg_len, unsigned int msg_prio,
814 const struct timespec __user *u_abs_timeout)
818 struct ext_wait_queue wait;
819 struct ext_wait_queue *receiver;
820 struct msg_msg *msg_ptr;
821 struct mqueue_inode_info *info;
825 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
828 timeout = prepare_timeout(u_abs_timeout);
835 inode = filp->f_dentry->d_inode;
836 if (unlikely(filp->f_op != &mqueue_file_operations))
838 info = MQUEUE_I(inode);
840 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
843 if (unlikely(msg_len > info->attr.mq_msgsize)) {
848 /* First try to allocate memory, before doing anything with
849 * existing queues. */
850 msg_ptr = load_msg(u_msg_ptr, msg_len);
851 if (IS_ERR(msg_ptr)) {
852 ret = PTR_ERR(msg_ptr);
855 msg_ptr->m_ts = msg_len;
856 msg_ptr->m_type = msg_prio;
858 spin_lock(&info->lock);
860 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
861 if (filp->f_flags & O_NONBLOCK) {
862 spin_unlock(&info->lock);
864 } else if (unlikely(timeout < 0)) {
865 spin_unlock(&info->lock);
869 wait.msg = (void *) msg_ptr;
870 wait.state = STATE_NONE;
871 ret = wq_sleep(info, SEND, timeout, &wait);
876 receiver = wq_get_first_waiter(info, RECV);
878 pipelined_send(info, msg_ptr, receiver);
880 /* adds message to the queue */
881 msg_insert(msg_ptr, info);
884 inode->i_atime = inode->i_mtime = inode->i_ctime =
886 spin_unlock(&info->lock);
895 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
896 size_t msg_len, unsigned int __user *u_msg_prio,
897 const struct timespec __user *u_abs_timeout)
901 struct msg_msg *msg_ptr;
904 struct mqueue_inode_info *info;
905 struct ext_wait_queue wait;
907 timeout = prepare_timeout(u_abs_timeout);
914 inode = filp->f_dentry->d_inode;
915 if (unlikely(filp->f_op != &mqueue_file_operations))
917 info = MQUEUE_I(inode);
919 if (unlikely(!(filp->f_mode & FMODE_READ)))
922 /* checks if buffer is big enough */
923 if (unlikely(msg_len < info->attr.mq_msgsize)) {
928 spin_lock(&info->lock);
929 if (info->attr.mq_curmsgs == 0) {
930 if (filp->f_flags & O_NONBLOCK) {
931 spin_unlock(&info->lock);
934 } else if (unlikely(timeout < 0)) {
935 spin_unlock(&info->lock);
940 wait.state = STATE_NONE;
941 ret = wq_sleep(info, RECV, timeout, &wait);
945 msg_ptr = msg_get(info);
947 inode->i_atime = inode->i_mtime = inode->i_ctime =
950 /* There is now free space in queue. */
951 pipelined_receive(info);
952 spin_unlock(&info->lock);
958 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
959 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
971 * Notes: the case when user wants us to deregister (with NULL as pointer)
972 * and he isn't currently owner of notification, will be silently discarded.
973 * It isn't explicitly defined in the POSIX.
975 asmlinkage long sys_mq_notify(mqd_t mqdes,
976 const struct sigevent __user *u_notification)
982 struct sigevent notification;
983 struct mqueue_inode_info *info;
988 if (u_notification != NULL) {
989 if (copy_from_user(¬ification, u_notification,
990 sizeof(struct sigevent)))
993 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
994 notification.sigev_notify != SIGEV_SIGNAL &&
995 notification.sigev_notify != SIGEV_THREAD))
997 if (notification.sigev_notify == SIGEV_SIGNAL &&
998 !valid_signal(notification.sigev_signo)) {
1001 if (notification.sigev_notify == SIGEV_THREAD) {
1002 /* create the notify skb */
1003 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1008 if (copy_from_user(nc->data,
1009 notification.sigev_value.sival_ptr,
1010 NOTIFY_COOKIE_LEN)) {
1014 /* TODO: add a header? */
1015 skb_put(nc, NOTIFY_COOKIE_LEN);
1016 /* and attach it to the socket */
1018 filp = fget(notification.sigev_signo);
1022 sock = netlink_getsockbyfilp(filp);
1025 ret = PTR_ERR(sock);
1030 ret = netlink_attachskb(sock, nc, 0,
1031 MAX_SCHEDULE_TIMEOUT, NULL);
1047 inode = filp->f_dentry->d_inode;
1048 if (unlikely(filp->f_op != &mqueue_file_operations))
1050 info = MQUEUE_I(inode);
1053 spin_lock(&info->lock);
1054 if (u_notification == NULL) {
1055 if (info->notify_owner == current->tgid) {
1056 remove_notification(info);
1057 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1059 } else if (info->notify_owner != 0) {
1062 switch (notification.sigev_notify) {
1064 info->notify.sigev_notify = SIGEV_NONE;
1067 info->notify_sock = sock;
1068 info->notify_cookie = nc;
1071 info->notify.sigev_notify = SIGEV_THREAD;
1074 info->notify.sigev_signo = notification.sigev_signo;
1075 info->notify.sigev_value = notification.sigev_value;
1076 info->notify.sigev_notify = SIGEV_SIGNAL;
1079 info->notify_owner = current->tgid;
1080 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1082 spin_unlock(&info->lock);
1087 netlink_detachskb(sock, nc);
1094 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1095 const struct mq_attr __user *u_mqstat,
1096 struct mq_attr __user *u_omqstat)
1099 struct mq_attr mqstat, omqstat;
1101 struct inode *inode;
1102 struct mqueue_inode_info *info;
1104 if (u_mqstat != NULL) {
1105 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1107 if (mqstat.mq_flags & (~O_NONBLOCK))
1116 inode = filp->f_dentry->d_inode;
1117 if (unlikely(filp->f_op != &mqueue_file_operations))
1119 info = MQUEUE_I(inode);
1121 spin_lock(&info->lock);
1123 omqstat = info->attr;
1124 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1126 if (mqstat.mq_flags & O_NONBLOCK)
1127 filp->f_flags |= O_NONBLOCK;
1129 filp->f_flags &= ~O_NONBLOCK;
1131 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1134 spin_unlock(&info->lock);
1137 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1138 sizeof(struct mq_attr)))
1147 static struct inode_operations mqueue_dir_inode_operations = {
1148 .lookup = simple_lookup,
1149 .create = mqueue_create,
1150 .unlink = mqueue_unlink,
1153 static struct file_operations mqueue_file_operations = {
1154 .flush = mqueue_flush_file,
1155 .poll = mqueue_poll_file,
1156 .read = mqueue_read_file,
1159 static struct super_operations mqueue_super_ops = {
1160 .alloc_inode = mqueue_alloc_inode,
1161 .destroy_inode = mqueue_destroy_inode,
1162 .statfs = simple_statfs,
1163 .delete_inode = mqueue_delete_inode,
1164 .drop_inode = generic_delete_inode,
1167 static struct file_system_type mqueue_fs_type = {
1169 .get_sb = mqueue_get_sb,
1170 .kill_sb = kill_litter_super,
1173 static int msg_max_limit_min = DFLT_MSGMAX;
1174 static int msg_max_limit_max = HARD_MSGMAX;
1176 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1177 static int msg_maxsize_limit_max = INT_MAX;
1179 static ctl_table mq_sysctls[] = {
1181 .ctl_name = CTL_QUEUESMAX,
1182 .procname = "queues_max",
1183 .data = &queues_max,
1184 .maxlen = sizeof(int),
1186 .proc_handler = &proc_dointvec,
1189 .ctl_name = CTL_MSGMAX,
1190 .procname = "msg_max",
1192 .maxlen = sizeof(int),
1194 .proc_handler = &proc_dointvec_minmax,
1195 .extra1 = &msg_max_limit_min,
1196 .extra2 = &msg_max_limit_max,
1199 .ctl_name = CTL_MSGSIZEMAX,
1200 .procname = "msgsize_max",
1201 .data = &msgsize_max,
1202 .maxlen = sizeof(int),
1204 .proc_handler = &proc_dointvec_minmax,
1205 .extra1 = &msg_maxsize_limit_min,
1206 .extra2 = &msg_maxsize_limit_max,
1211 static ctl_table mq_sysctl_dir[] = {
1213 .ctl_name = FS_MQUEUE,
1214 .procname = "mqueue",
1216 .child = mq_sysctls,
1221 static ctl_table mq_sysctl_root[] = {
1226 .child = mq_sysctl_dir,
1231 static int __init init_mqueue_fs(void)
1235 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1236 sizeof(struct mqueue_inode_info), 0,
1237 SLAB_HWCACHE_ALIGN, init_once, NULL);
1238 if (mqueue_inode_cachep == NULL)
1241 /* ignore failues - they are not fatal */
1242 mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1244 error = register_filesystem(&mqueue_fs_type);
1248 if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1249 error = PTR_ERR(mqueue_mnt);
1250 goto out_filesystem;
1253 /* internal initialization - not common for vfs */
1255 spin_lock_init(&mq_lock);
1260 unregister_filesystem(&mqueue_fs_type);
1262 if (mq_sysctl_table)
1263 unregister_sysctl_table(mq_sysctl_table);
1264 if (kmem_cache_destroy(mqueue_inode_cachep)) {
1266 "mqueue_inode_cache: not all structures were freed\n");
1271 __initcall(init_mqueue_fs);