blob: a3673a09069a7e71dbda2f3d0b24296a87704f74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * POSIX message queues filesystem for Linux.
3 *
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
Michal Wronskif66e9282006-10-03 23:23:27 +02005 * Michal Wronski (michal.wronski@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
10 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050011 * Audit: George Wilson (ltcgcw@us.ibm.com)
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This file is released under the GPL.
14 */
15
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
George C. Wilson20ca73b2006-05-24 16:09:55 -050029#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080031#include <linux/mutex.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070032#include <linux/nsproxy.h>
33#include <linux/pid.h>
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070034#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/sock.h>
37#include "util.h"
38
39#define MQUEUE_MAGIC 0x19800202
40#define DIRENT_SIZE 20
41#define FILENT_SIZE 80
42
43#define SEND 0
44#define RECV 1
45
46#define STATE_NONE 0
47#define STATE_PENDING 1
48#define STATE_READY 2
49
Joe Kortyb231cca42008-10-18 20:28:32 -070050/*
51 * Define the ranges various user-specified maximum values can
52 * be set to.
53 */
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59struct 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 */
64};
65
66struct mqueue_inode_info {
67 spinlock_t lock;
68 struct inode vfs_inode;
69 wait_queue_head_t wait_q;
70
71 struct msg_msg **messages;
72 struct mq_attr attr;
73
74 struct sigevent notify;
Cedric Le Goatera03fcb72006-10-02 02:17:26 -070075 struct pid* notify_owner;
Adrian Bunk338cec32005-09-10 00:26:54 -070076 struct user_struct *user; /* user who created, for accounting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct sock *notify_sock;
78 struct sk_buff *notify_cookie;
79
80 /* for tasks waiting for free space and messages, respectively */
81 struct ext_wait_queue e_wait_q[2];
82
83 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
84};
85
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080086static const struct inode_operations mqueue_dir_inode_operations;
Arjan van de Ven9a321442007-02-12 00:55:35 -080087static const struct file_operations mqueue_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct super_operations mqueue_super_ops;
89static void remove_notification(struct mqueue_inode_info *info);
90
91static spinlock_t mq_lock;
Christoph Lametere18b8902006-12-06 20:33:20 -080092static struct kmem_cache *mqueue_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static struct ctl_table_header * mq_sysctl_table;
95
96static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
97{
98 return container_of(inode, struct mqueue_inode_info, vfs_inode);
99}
100
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700101void mq_init_ns(struct ipc_namespace *ns)
102{
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);
108}
109
110void mq_exit_ns(struct ipc_namespace *ns)
111{
112 /* will need to clear out ns->mq_mnt->mnt_sb->s_fs_info here */
113 mntput(ns->mq_mnt);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
117 struct mq_attr *attr)
118{
David Howells86a264a2008-11-14 10:39:18 +1100119 struct user_struct *u = current_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct inode *inode;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700121 struct ipc_namespace *ipc_ns = &init_ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 inode = new_inode(sb);
124 if (inode) {
125 inode->i_mode = mode;
David Howells414c0702008-11-14 10:39:06 +1100126 inode->i_uid = current_fsuid();
127 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 inode->i_mtime = inode->i_ctime = inode->i_atime =
129 CURRENT_TIME;
130
131 if (S_ISREG(mode)) {
132 struct mqueue_inode_info *info;
133 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long mq_bytes, mq_msg_tblsz;
135
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;
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700145 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 info->qsize = 0;
147 info->user = NULL; /* set when all is ok */
148 memset(&info->attr, 0, sizeof(info->attr));
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700149 info->attr.mq_maxmsg = ipc_ns->mq_msg_max;
150 info->attr.mq_msgsize = ipc_ns->mq_msgsize_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (attr) {
152 info->attr.mq_maxmsg = attr->mq_maxmsg;
153 info->attr.mq_msgsize = attr->mq_msgsize;
154 }
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));
158
159 spin_lock(&mq_lock);
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);
164 goto out_inode;
165 }
166 u->mq_bytes += mq_bytes;
167 spin_unlock(&mq_lock);
168
169 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
170 if (!info->messages) {
171 spin_lock(&mq_lock);
172 u->mq_bytes -= mq_bytes;
173 spin_unlock(&mq_lock);
174 goto out_inode;
175 }
176 /* all is ok */
177 info->user = get_uid(u);
178 } else if (S_ISDIR(mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700179 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* 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;
184 }
185 }
186 return inode;
187out_inode:
188 make_bad_inode(inode);
189 iput(inode);
190 return NULL;
191}
192
193static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
194{
195 struct inode *inode;
196
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;
201
202 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
203 if (!inode)
204 return -ENOMEM;
205
206 sb->s_root = d_alloc_root(inode);
207 if (!sb->s_root) {
208 iput(inode);
209 return -ENOMEM;
210 }
211
212 return 0;
213}
214
David Howells454e2392006-06-23 02:02:57 -0700215static int mqueue_get_sb(struct file_system_type *fs_type,
216 int flags, const char *dev_name,
217 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
David Howells454e2392006-06-23 02:02:57 -0700219 return get_sb_single(fs_type, flags, data, mqueue_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700222static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
225
Christoph Lametera35afb82007-05-16 22:10:57 -0700226 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static struct inode *mqueue_alloc_inode(struct super_block *sb)
230{
231 struct mqueue_inode_info *ei;
232
Christoph Lametere94b1762006-12-06 20:33:17 -0800233 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!ei)
235 return NULL;
236 return &ei->vfs_inode;
237}
238
239static void mqueue_destroy_inode(struct inode *inode)
240{
241 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
242}
243
244static void mqueue_delete_inode(struct inode *inode)
245{
246 struct mqueue_inode_info *info;
247 struct user_struct *user;
248 unsigned long mq_bytes;
249 int i;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700250 struct ipc_namespace *ipc_ns = &init_ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (S_ISDIR(inode->i_mode)) {
253 clear_inode(inode);
254 return;
255 }
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);
262
263 clear_inode(inode);
264
265 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
266 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
267 user = info->user;
268 if (user) {
269 spin_lock(&mq_lock);
270 user->mq_bytes -= mq_bytes;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700271 ipc_ns->mq_queues_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 spin_unlock(&mq_lock);
273 free_uid(user);
274 }
275}
276
277static int mqueue_create(struct inode *dir, struct dentry *dentry,
278 int mode, struct nameidata *nd)
279{
280 struct inode *inode;
281 struct mq_attr *attr = dentry->d_fsdata;
282 int error;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700283 struct ipc_namespace *ipc_ns = &init_ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 spin_lock(&mq_lock);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700286 if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
287 !capable(CAP_SYS_RESOURCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 error = -ENOSPC;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700289 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700291 ipc_ns->mq_queues_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 spin_unlock(&mq_lock);
293
294 inode = mqueue_get_inode(dir->i_sb, mode, attr);
295 if (!inode) {
296 error = -ENOMEM;
297 spin_lock(&mq_lock);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700298 ipc_ns->mq_queues_count--;
299 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 dir->i_size += DIRENT_SIZE;
303 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
304
305 d_instantiate(dentry, inode);
306 dget(dentry);
307 return 0;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700308out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_unlock(&mq_lock);
310 return error;
311}
312
313static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
314{
315 struct inode *inode = dentry->d_inode;
316
317 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
318 dir->i_size -= DIRENT_SIZE;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700319 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 dput(dentry);
321 return 0;
322}
323
324/*
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)
330*/
331static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700332 size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Josef Sipek6d630792006-12-08 02:37:11 -0800334 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 char buffer[FILENT_SIZE];
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700336 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 spin_lock(&info->lock);
339 snprintf(buffer, sizeof(buffer),
340 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
341 info->qsize,
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,
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800346 pid_vnr(info->notify_owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock(&info->lock);
348 buffer[sizeof(buffer)-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700350 ret = simple_read_from_buffer(u_data, count, off, buffer,
351 strlen(buffer));
352 if (ret <= 0)
353 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Josef Sipek6d630792006-12-08 02:37:11 -0800355 filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700359static int mqueue_flush_file(struct file *filp, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Josef Sipek6d630792006-12-08 02:37:11 -0800361 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 spin_lock(&info->lock);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700364 if (task_tgid(current) == info->notify_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 remove_notification(info);
366
367 spin_unlock(&info->lock);
368 return 0;
369}
370
371static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
372{
Josef Sipek6d630792006-12-08 02:37:11 -0800373 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int retval = 0;
375
376 poll_wait(filp, &info->wait_q, poll_tab);
377
378 spin_lock(&info->lock);
379 if (info->attr.mq_curmsgs)
380 retval = POLLIN | POLLRDNORM;
381
382 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
383 retval |= POLLOUT | POLLWRNORM;
384 spin_unlock(&info->lock);
385
386 return retval;
387}
388
389/* Adds current to info->e_wait_q[sr] before element with smaller prio */
390static void wq_add(struct mqueue_inode_info *info, int sr,
391 struct ext_wait_queue *ewp)
392{
393 struct ext_wait_queue *walk;
394
395 ewp->task = current;
396
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);
400 return;
401 }
402 }
403 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
404}
405
406/*
407 * Puts current task to sleep. Caller must hold queue lock. After return
408 * lock isn't held.
409 * sr: SEND or RECV
410 */
411static int wq_sleep(struct mqueue_inode_info *info, int sr,
412 long timeout, struct ext_wait_queue *ewp)
413{
414 int retval;
415 signed long time;
416
417 wq_add(info, sr, ewp);
418
419 for (;;) {
420 set_current_state(TASK_INTERRUPTIBLE);
421
422 spin_unlock(&info->lock);
423 time = schedule_timeout(timeout);
424
425 while (ewp->state == STATE_PENDING)
426 cpu_relax();
427
428 if (ewp->state == STATE_READY) {
429 retval = 0;
430 goto out;
431 }
432 spin_lock(&info->lock);
433 if (ewp->state == STATE_READY) {
434 retval = 0;
435 goto out_unlock;
436 }
437 if (signal_pending(current)) {
438 retval = -ERESTARTSYS;
439 break;
440 }
441 if (time == 0) {
442 retval = -ETIMEDOUT;
443 break;
444 }
445 }
446 list_del(&ewp->list);
447out_unlock:
448 spin_unlock(&info->lock);
449out:
450 return retval;
451}
452
453/*
454 * Returns waiting task that should be serviced first or NULL if none exists
455 */
456static struct ext_wait_queue *wq_get_first_waiter(
457 struct mqueue_inode_info *info, int sr)
458{
459 struct list_head *ptr;
460
461 ptr = info->e_wait_q[sr].list.prev;
462 if (ptr == &info->e_wait_q[sr].list)
463 return NULL;
464 return list_entry(ptr, struct ext_wait_queue, list);
465}
466
467/* Auxiliary functions to manipulate messages' list */
468static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
469{
470 int k;
471
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];
475 k--;
476 }
477 info->attr.mq_curmsgs++;
478 info->qsize += ptr->m_ts;
479 info->messages[k + 1] = ptr;
480}
481
482static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
483{
484 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
485 return info->messages[info->attr.mq_curmsgs];
486}
487
488static inline void set_cookie(struct sk_buff *skb, char code)
489{
490 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
491}
492
493/*
494 * The next function is only to split too long sys_mq_timedsend
495 */
496static void __do_notify(struct mqueue_inode_info *info)
497{
498 /* notification
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
502 * synchronously. */
503 if (info->notify_owner &&
504 info->attr.mq_curmsgs == 1) {
505 struct siginfo sig_i;
506 switch (info->notify.sigev_notify) {
507 case SIGEV_NONE:
508 break;
509 case SIGEV_SIGNAL:
510 /* sends signal */
511
512 sig_i.si_signo = info->notify.sigev_signo;
513 sig_i.si_errno = 0;
514 sig_i.si_code = SI_MESGQ;
515 sig_i.si_value = info->notify.sigev_value;
Sukadev Bhattiprolua6684992009-01-07 18:08:50 -0800516 sig_i.si_pid = task_tgid_nr_ns(current,
517 ns_of_pid(info->notify_owner));
David Howells414c0702008-11-14 10:39:06 +1100518 sig_i.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700520 kill_pid_info(info->notify.sigev_signo,
521 &sig_i, info->notify_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case SIGEV_THREAD:
524 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700525 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527 }
528 /* after notification unregisters process */
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700529 put_pid(info->notify_owner);
530 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 wake_up(&info->wait_q);
533}
534
Al Viroc32c8af2008-12-14 03:46:48 -0500535static long prepare_timeout(struct timespec *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Al Viroc32c8af2008-12-14 03:46:48 -0500537 struct timespec nowts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 long timeout;
539
Al Viroc32c8af2008-12-14 03:46:48 -0500540 if (p) {
541 if (unlikely(p->tv_nsec < 0 || p->tv_sec < 0
542 || p->tv_nsec >= NSEC_PER_SEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
544 nowts = CURRENT_TIME;
545 /* first subtract as jiffies can't be too big */
Al Viroc32c8af2008-12-14 03:46:48 -0500546 p->tv_sec -= nowts.tv_sec;
547 if (p->tv_nsec < nowts.tv_nsec) {
548 p->tv_nsec += NSEC_PER_SEC;
549 p->tv_sec--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Al Viroc32c8af2008-12-14 03:46:48 -0500551 p->tv_nsec -= nowts.tv_nsec;
552 if (p->tv_sec < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554
Al Viroc32c8af2008-12-14 03:46:48 -0500555 timeout = timespec_to_jiffies(p) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 } else
557 return MAX_SCHEDULE_TIMEOUT;
558
559 return timeout;
560}
561
562static void remove_notification(struct mqueue_inode_info *info)
563{
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700564 if (info->notify_owner != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 info->notify.sigev_notify == SIGEV_THREAD) {
566 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700567 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700569 put_pid(info->notify_owner);
570 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700573static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
576 return 0;
577 if (capable(CAP_SYS_RESOURCE)) {
578 if (attr->mq_maxmsg > HARD_MSGMAX)
579 return 0;
580 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700581 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
582 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584 }
585 /* check for overflow */
586 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
587 return 0;
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))
591 return 0;
592 return 1;
593}
594
595/*
596 * Invoked when creating a new queue via sys_mq_open
597 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700598static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
David Howells745ca242008-11-14 10:39:22 +1100602 const struct cred *cred = current_cred();
Dave Hansen4a3fd212008-02-15 14:37:48 -0800603 struct file *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int ret;
605
Al Viro564f6992008-12-14 04:02:26 -0500606 if (attr) {
Alexander Viro7c7dce92006-01-14 15:29:55 -0500607 ret = -EINVAL;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700608 if (!mq_attr_ok(ipc_ns, attr))
Alexander Viro7c7dce92006-01-14 15:29:55 -0500609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* store for use during create */
Al Viro564f6992008-12-14 04:02:26 -0500611 dentry->d_fsdata = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
Al Viroce3b0f82009-03-29 19:08:22 -0400614 mode &= ~current_umask();
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700615 ret = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800616 if (ret)
617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
619 dentry->d_fsdata = NULL;
620 if (ret)
Dave Hansen4a3fd212008-02-15 14:37:48 -0800621 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700623 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800624 /*
625 * dentry_open() took a persistent mnt_want_write(),
626 * so we can now drop this one.
627 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700628 mnt_drop_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800629 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dave Hansen4a3fd212008-02-15 14:37:48 -0800631out_drop_write:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700632 mnt_drop_write(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500633out:
634 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700635 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500636 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/* Opens existing queue */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700640static struct file *do_open(struct ipc_namespace *ipc_ns,
641 struct dentry *dentry, int oflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howells745ca242008-11-14 10:39:22 +1100643 const struct cred *cred = current_cred();
644
645 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
646 MAY_READ | MAY_WRITE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Alexander Viro7c7dce92006-01-14 15:29:55 -0500648 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
649 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700650 mntput(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return ERR_PTR(-EINVAL);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Al Virof419a2e2008-07-22 00:07:17 -0400654 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
Alexander Viro7c7dce92006-01-14 15:29:55 -0500655 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700656 mntput(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ERR_PTR(-EACCES);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700660 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Heiko Carstensd5460c92009-01-14 14:14:27 +0100663SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, mode_t, mode,
664 struct mq_attr __user *, u_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct dentry *dentry;
667 struct file *filp;
668 char *name;
Al Viro564f6992008-12-14 04:02:26 -0500669 struct mq_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int fd, error;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700671 struct ipc_namespace *ipc_ns = &init_ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Al Viro564f6992008-12-14 04:02:26 -0500673 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
674 return -EFAULT;
675
676 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -0500677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (IS_ERR(name = getname(u_name)))
679 return PTR_ERR(name);
680
Ulrich Drepper269f2132008-05-03 15:28:45 -0400681 fd = get_unused_fd_flags(O_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (fd < 0)
683 goto out_putname;
684
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700685 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (IS_ERR(dentry)) {
688 error = PTR_ERR(dentry);
689 goto out_err;
690 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700691 mntget(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (oflag & O_CREAT) {
694 if (dentry->d_inode) { /* entry already exists */
Al Viro5a190ae2007-06-07 12:19:32 -0400695 audit_inode(name, dentry);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500696 error = -EEXIST;
697 if (oflag & O_EXCL)
698 goto out;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700699 filp = do_open(ipc_ns, dentry, oflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700701 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
702 dentry, oflag, mode,
Al Viro564f6992008-12-14 04:02:26 -0500703 u_attr ? &attr : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Alexander Viro7c7dce92006-01-14 15:29:55 -0500705 } else {
706 error = -ENOENT;
707 if (!dentry->d_inode)
708 goto out;
Al Viro5a190ae2007-06-07 12:19:32 -0400709 audit_inode(name, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700710 filp = do_open(ipc_ns, dentry, oflag);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (IS_ERR(filp)) {
714 error = PTR_ERR(filp);
715 goto out_putfd;
716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 fd_install(fd, filp);
719 goto out_upsem;
720
Alexander Viro7c7dce92006-01-14 15:29:55 -0500721out:
722 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700723 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500724out_putfd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 put_unused_fd(fd);
726out_err:
727 fd = error;
728out_upsem:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700729 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730out_putname:
731 putname(name);
732 return fd;
733}
734
Heiko Carstensd5460c92009-01-14 14:14:27 +0100735SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 int err;
738 char *name;
739 struct dentry *dentry;
740 struct inode *inode = NULL;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700741 struct ipc_namespace *ipc_ns = &init_ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 name = getname(u_name);
744 if (IS_ERR(name))
745 return PTR_ERR(name);
746
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700747 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
Peter Zijlstra7a434812007-03-06 01:42:09 -0800748 I_MUTEX_PARENT);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700749 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (IS_ERR(dentry)) {
751 err = PTR_ERR(dentry);
752 goto out_unlock;
753 }
754
755 if (!dentry->d_inode) {
756 err = -ENOENT;
757 goto out_err;
758 }
759
760 inode = dentry->d_inode;
761 if (inode)
762 atomic_inc(&inode->i_count);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700763 err = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800764 if (err)
765 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700767 mnt_drop_write(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768out_err:
769 dput(dentry);
770
771out_unlock:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700772 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 putname(name);
774 if (inode)
775 iput(inode);
776
777 return err;
778}
779
780/* Pipelined send and receive functions.
781 *
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
786 * receiver.
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
Serge E. Hallyn7b7a3172006-03-28 01:56:23 -0800790 * ipc/sem.c for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
792 * The same algorithm is used for senders.
793 */
794
795/* pipelined_send() - send a message directly to the task waiting in
796 * sys_mq_timedreceive() (without inserting message into a queue).
797 */
798static inline void pipelined_send(struct mqueue_inode_info *info,
799 struct msg_msg *message,
800 struct ext_wait_queue *receiver)
801{
802 receiver->msg = message;
803 list_del(&receiver->list);
804 receiver->state = STATE_PENDING;
805 wake_up_process(receiver->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700806 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 receiver->state = STATE_READY;
808}
809
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). */
812static inline void pipelined_receive(struct mqueue_inode_info *info)
813{
814 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
815
816 if (!sender) {
817 /* for poll */
818 wake_up_interruptible(&info->wait_q);
819 return;
820 }
821 msg_insert(sender->msg, info);
822 list_del(&sender->list);
823 sender->state = STATE_PENDING;
824 wake_up_process(sender->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700825 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 sender->state = STATE_READY;
827}
828
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100829SYSCALL_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct file *filp;
834 struct inode *inode;
835 struct ext_wait_queue wait;
836 struct ext_wait_queue *receiver;
837 struct msg_msg *msg_ptr;
838 struct mqueue_inode_info *info;
Al Viroc32c8af2008-12-14 03:46:48 -0500839 struct timespec ts, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 long timeout;
841 int ret;
842
Al Viroc32c8af2008-12-14 03:46:48 -0500843 if (u_abs_timeout) {
844 if (copy_from_user(&ts, u_abs_timeout,
845 sizeof(struct timespec)))
846 return -EFAULT;
847 p = &ts;
848 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
851 return -EINVAL;
852
Al Viroc32c8af2008-12-14 03:46:48 -0500853 audit_mq_sendrecv(mqdes, msg_len, msg_prio, p);
854 timeout = prepare_timeout(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 ret = -EBADF;
857 filp = fget(mqdes);
858 if (unlikely(!filp))
859 goto out;
860
Josef Sipek6d630792006-12-08 02:37:11 -0800861 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (unlikely(filp->f_op != &mqueue_file_operations))
863 goto out_fput;
864 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400865 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
868 goto out_fput;
869
870 if (unlikely(msg_len > info->attr.mq_msgsize)) {
871 ret = -EMSGSIZE;
872 goto out_fput;
873 }
874
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);
880 goto out_fput;
881 }
882 msg_ptr->m_ts = msg_len;
883 msg_ptr->m_type = msg_prio;
884
885 spin_lock(&info->lock);
886
887 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
888 if (filp->f_flags & O_NONBLOCK) {
889 spin_unlock(&info->lock);
890 ret = -EAGAIN;
891 } else if (unlikely(timeout < 0)) {
892 spin_unlock(&info->lock);
893 ret = timeout;
894 } else {
895 wait.task = current;
896 wait.msg = (void *) msg_ptr;
897 wait.state = STATE_NONE;
898 ret = wq_sleep(info, SEND, timeout, &wait);
899 }
900 if (ret < 0)
901 free_msg(msg_ptr);
902 } else {
903 receiver = wq_get_first_waiter(info, RECV);
904 if (receiver) {
905 pipelined_send(info, msg_ptr, receiver);
906 } else {
907 /* adds message to the queue */
908 msg_insert(msg_ptr, info);
909 __do_notify(info);
910 }
911 inode->i_atime = inode->i_mtime = inode->i_ctime =
912 CURRENT_TIME;
913 spin_unlock(&info->lock);
914 ret = 0;
915 }
916out_fput:
917 fput(filp);
918out:
919 return ret;
920}
921
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100922SYSCALL_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 long timeout;
927 ssize_t ret;
928 struct msg_msg *msg_ptr;
929 struct file *filp;
930 struct inode *inode;
931 struct mqueue_inode_info *info;
932 struct ext_wait_queue wait;
Al Viroc32c8af2008-12-14 03:46:48 -0500933 struct timespec ts, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Al Viroc32c8af2008-12-14 03:46:48 -0500935 if (u_abs_timeout) {
936 if (copy_from_user(&ts, u_abs_timeout,
937 sizeof(struct timespec)))
938 return -EFAULT;
939 p = &ts;
940 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500941
Al Viroc32c8af2008-12-14 03:46:48 -0500942 audit_mq_sendrecv(mqdes, msg_len, 0, p);
943 timeout = prepare_timeout(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 ret = -EBADF;
946 filp = fget(mqdes);
947 if (unlikely(!filp))
948 goto out;
949
Josef Sipek6d630792006-12-08 02:37:11 -0800950 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (unlikely(filp->f_op != &mqueue_file_operations))
952 goto out_fput;
953 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400954 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (unlikely(!(filp->f_mode & FMODE_READ)))
957 goto out_fput;
958
959 /* checks if buffer is big enough */
960 if (unlikely(msg_len < info->attr.mq_msgsize)) {
961 ret = -EMSGSIZE;
962 goto out_fput;
963 }
964
965 spin_lock(&info->lock);
966 if (info->attr.mq_curmsgs == 0) {
967 if (filp->f_flags & O_NONBLOCK) {
968 spin_unlock(&info->lock);
969 ret = -EAGAIN;
970 msg_ptr = NULL;
971 } else if (unlikely(timeout < 0)) {
972 spin_unlock(&info->lock);
973 ret = timeout;
974 msg_ptr = NULL;
975 } else {
976 wait.task = current;
977 wait.state = STATE_NONE;
978 ret = wq_sleep(info, RECV, timeout, &wait);
979 msg_ptr = wait.msg;
980 }
981 } else {
982 msg_ptr = msg_get(info);
983
984 inode->i_atime = inode->i_mtime = inode->i_ctime =
985 CURRENT_TIME;
986
987 /* There is now free space in queue. */
988 pipelined_receive(info);
989 spin_unlock(&info->lock);
990 ret = 0;
991 }
992 if (ret == 0) {
993 ret = msg_ptr->m_ts;
994
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)) {
997 ret = -EFAULT;
998 }
999 free_msg(msg_ptr);
1000 }
1001out_fput:
1002 fput(filp);
1003out:
1004 return ret;
1005}
1006
1007/*
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.
1011 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001012SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1013 const struct sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 int ret;
1016 struct file *filp;
1017 struct sock *sock;
1018 struct inode *inode;
1019 struct sigevent notification;
1020 struct mqueue_inode_info *info;
1021 struct sk_buff *nc;
1022
Al Viro20114f72008-12-10 07:16:12 -05001023 if (u_notification) {
1024 if (copy_from_user(&notification, u_notification,
1025 sizeof(struct sigevent)))
1026 return -EFAULT;
1027 }
1028
1029 audit_mq_notify(mqdes, u_notification ? &notification : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -05001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 nc = NULL;
1032 sock = NULL;
1033 if (u_notification != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1035 notification.sigev_notify != SIGEV_SIGNAL &&
1036 notification.sigev_notify != SIGEV_THREAD))
1037 return -EINVAL;
1038 if (notification.sigev_notify == SIGEV_SIGNAL &&
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001039 !valid_signal(notification.sigev_signo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -EINVAL;
1041 }
1042 if (notification.sigev_notify == SIGEV_THREAD) {
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001043 long timeo;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* create the notify skb */
1046 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1047 ret = -ENOMEM;
1048 if (!nc)
1049 goto out;
1050 ret = -EFAULT;
1051 if (copy_from_user(nc->data,
1052 notification.sigev_value.sival_ptr,
1053 NOTIFY_COOKIE_LEN)) {
1054 goto out;
1055 }
1056
1057 /* TODO: add a header? */
1058 skb_put(nc, NOTIFY_COOKIE_LEN);
1059 /* and attach it to the socket */
1060retry:
1061 filp = fget(notification.sigev_signo);
1062 ret = -EBADF;
1063 if (!filp)
1064 goto out;
1065 sock = netlink_getsockbyfilp(filp);
1066 fput(filp);
1067 if (IS_ERR(sock)) {
1068 ret = PTR_ERR(sock);
1069 sock = NULL;
1070 goto out;
1071 }
1072
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001073 timeo = MAX_SCHEDULE_TIMEOUT;
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001074 ret = netlink_attachskb(sock, nc, &timeo, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (ret == 1)
1076 goto retry;
1077 if (ret) {
1078 sock = NULL;
1079 nc = NULL;
1080 goto out;
1081 }
1082 }
1083 }
1084
1085 ret = -EBADF;
1086 filp = fget(mqdes);
1087 if (!filp)
1088 goto out;
1089
Josef Sipek6d630792006-12-08 02:37:11 -08001090 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (unlikely(filp->f_op != &mqueue_file_operations))
1092 goto out_fput;
1093 info = MQUEUE_I(inode);
1094
1095 ret = 0;
1096 spin_lock(&info->lock);
1097 if (u_notification == NULL) {
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001098 if (info->notify_owner == task_tgid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 remove_notification(info);
1100 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1101 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001102 } else if (info->notify_owner != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ret = -EBUSY;
1104 } else {
1105 switch (notification.sigev_notify) {
1106 case SIGEV_NONE:
1107 info->notify.sigev_notify = SIGEV_NONE;
1108 break;
1109 case SIGEV_THREAD:
1110 info->notify_sock = sock;
1111 info->notify_cookie = nc;
1112 sock = NULL;
1113 nc = NULL;
1114 info->notify.sigev_notify = SIGEV_THREAD;
1115 break;
1116 case SIGEV_SIGNAL:
1117 info->notify.sigev_signo = notification.sigev_signo;
1118 info->notify.sigev_value = notification.sigev_value;
1119 info->notify.sigev_notify = SIGEV_SIGNAL;
1120 break;
1121 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001122
1123 info->notify_owner = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1125 }
1126 spin_unlock(&info->lock);
1127out_fput:
1128 fput(filp);
1129out:
1130 if (sock) {
1131 netlink_detachskb(sock, nc);
1132 } else if (nc) {
1133 dev_kfree_skb(nc);
1134 }
1135 return ret;
1136}
1137
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001138SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1139 const struct mq_attr __user *, u_mqstat,
1140 struct mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 int ret;
1143 struct mq_attr mqstat, omqstat;
1144 struct file *filp;
1145 struct inode *inode;
1146 struct mqueue_inode_info *info;
1147
1148 if (u_mqstat != NULL) {
1149 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1150 return -EFAULT;
1151 if (mqstat.mq_flags & (~O_NONBLOCK))
1152 return -EINVAL;
1153 }
1154
1155 ret = -EBADF;
1156 filp = fget(mqdes);
1157 if (!filp)
1158 goto out;
1159
Josef Sipek6d630792006-12-08 02:37:11 -08001160 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (unlikely(filp->f_op != &mqueue_file_operations))
1162 goto out_fput;
1163 info = MQUEUE_I(inode);
1164
1165 spin_lock(&info->lock);
1166
1167 omqstat = info->attr;
1168 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1169 if (u_mqstat) {
Al Viro73929062008-12-10 06:58:59 -05001170 audit_mq_getsetattr(mqdes, &mqstat);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001171 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (mqstat.mq_flags & O_NONBLOCK)
1173 filp->f_flags |= O_NONBLOCK;
1174 else
1175 filp->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001176 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1179 }
1180
1181 spin_unlock(&info->lock);
1182
1183 ret = 0;
1184 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1185 sizeof(struct mq_attr)))
1186 ret = -EFAULT;
1187
1188out_fput:
1189 fput(filp);
1190out:
1191 return ret;
1192}
1193
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001194static const struct inode_operations mqueue_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 .lookup = simple_lookup,
1196 .create = mqueue_create,
1197 .unlink = mqueue_unlink,
1198};
1199
Arjan van de Ven9a321442007-02-12 00:55:35 -08001200static const struct file_operations mqueue_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 .flush = mqueue_flush_file,
1202 .poll = mqueue_poll_file,
1203 .read = mqueue_read_file,
1204};
1205
1206static 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,
1212};
1213
1214static struct file_system_type mqueue_fs_type = {
1215 .name = "mqueue",
1216 .get_sb = mqueue_get_sb,
1217 .kill_sb = kill_litter_super,
1218};
1219
Joe Kortyb231cca42008-10-18 20:28:32 -07001220static int msg_max_limit_min = MIN_MSGMAX;
1221static int msg_max_limit_max = MAX_MSGMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Joe Kortyb231cca42008-10-18 20:28:32 -07001223static int msg_maxsize_limit_min = MIN_MSGSIZEMAX;
1224static int msg_maxsize_limit_max = MAX_MSGSIZEMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226static ctl_table mq_sysctls[] = {
1227 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 .procname = "queues_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001229 .data = &init_ipc_ns.mq_queues_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .maxlen = sizeof(int),
1231 .mode = 0644,
1232 .proc_handler = &proc_dointvec,
1233 },
1234 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 .procname = "msg_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001236 .data = &init_ipc_ns.mq_msg_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .maxlen = sizeof(int),
1238 .mode = 0644,
1239 .proc_handler = &proc_dointvec_minmax,
1240 .extra1 = &msg_max_limit_min,
1241 .extra2 = &msg_max_limit_max,
1242 },
1243 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 .procname = "msgsize_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001245 .data = &init_ipc_ns.mq_msgsize_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 .maxlen = sizeof(int),
1247 .mode = 0644,
1248 .proc_handler = &proc_dointvec_minmax,
1249 .extra1 = &msg_maxsize_limit_min,
1250 .extra2 = &msg_maxsize_limit_max,
1251 },
1252 { .ctl_name = 0 }
1253};
1254
1255static ctl_table mq_sysctl_dir[] = {
1256 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 .procname = "mqueue",
1258 .mode = 0555,
1259 .child = mq_sysctls,
1260 },
1261 { .ctl_name = 0 }
1262};
1263
1264static ctl_table mq_sysctl_root[] = {
1265 {
1266 .ctl_name = CTL_FS,
1267 .procname = "fs",
1268 .mode = 0555,
1269 .child = mq_sysctl_dir,
1270 },
1271 { .ctl_name = 0 }
1272};
1273
1274static int __init init_mqueue_fs(void)
1275{
1276 int error;
1277
1278 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1279 sizeof(struct mqueue_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001280 SLAB_HWCACHE_ALIGN, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (mqueue_inode_cachep == NULL)
1282 return -ENOMEM;
1283
1284 /* ignore failues - they are not fatal */
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001285 mq_sysctl_table = register_sysctl_table(mq_sysctl_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 error = register_filesystem(&mqueue_fs_type);
1288 if (error)
1289 goto out_sysctl;
1290
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001291 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out_filesystem;
1295 }
1296
1297 /* internal initialization - not common for vfs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_lock_init(&mq_lock);
1299
1300 return 0;
1301
1302out_filesystem:
1303 unregister_filesystem(&mqueue_fs_type);
1304out_sysctl:
1305 if (mq_sysctl_table)
1306 unregister_sysctl_table(mq_sysctl_table);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001307 kmem_cache_destroy(mqueue_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return error;
1309}
1310
1311__initcall(init_mqueue_fs);