blob: c82d7b51ef6801d4c477b59386ca7cfe9c5d0cc5 [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
Christoph Lametere18b8902006-12-06 20:33:20 -080091static struct kmem_cache *mqueue_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93static struct ctl_table_header * mq_sysctl_table;
94
95static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
96{
97 return container_of(inode, struct mqueue_inode_info, vfs_inode);
98}
99
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700100/*
101 * This routine should be called with the mq_lock held.
102 */
103static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700104{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700105 return get_ipc_ns(inode->i_sb->s_fs_info);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700106}
107
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700108static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700109{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700110 struct ipc_namespace *ns;
111
112 spin_lock(&mq_lock);
113 ns = __get_ns_from_inode(inode);
114 spin_unlock(&mq_lock);
115 return ns;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700116}
117
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700118static struct inode *mqueue_get_inode(struct super_block *sb,
119 struct ipc_namespace *ipc_ns, int mode,
120 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David Howells86a264a2008-11-14 10:39:18 +1100122 struct user_struct *u = current_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct inode *inode;
124
125 inode = new_inode(sb);
126 if (inode) {
127 inode->i_mode = mode;
David Howells414c0702008-11-14 10:39:06 +1100128 inode->i_uid = current_fsuid();
129 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 inode->i_mtime = inode->i_ctime = inode->i_atime =
131 CURRENT_TIME;
132
133 if (S_ISREG(mode)) {
134 struct mqueue_inode_info *info;
135 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long mq_bytes, mq_msg_tblsz;
137
138 inode->i_fop = &mqueue_file_operations;
139 inode->i_size = FILENT_SIZE;
140 /* mqueue specific info */
141 info = MQUEUE_I(inode);
142 spin_lock_init(&info->lock);
143 init_waitqueue_head(&info->wait_q);
144 INIT_LIST_HEAD(&info->e_wait_q[0].list);
145 INIT_LIST_HEAD(&info->e_wait_q[1].list);
146 info->messages = NULL;
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700147 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 info->qsize = 0;
149 info->user = NULL; /* set when all is ok */
150 memset(&info->attr, 0, sizeof(info->attr));
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700151 info->attr.mq_maxmsg = ipc_ns->mq_msg_max;
152 info->attr.mq_msgsize = ipc_ns->mq_msgsize_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (attr) {
154 info->attr.mq_maxmsg = attr->mq_maxmsg;
155 info->attr.mq_msgsize = attr->mq_msgsize;
156 }
157 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
158 mq_bytes = (mq_msg_tblsz +
159 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
160
161 spin_lock(&mq_lock);
162 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
163 u->mq_bytes + mq_bytes >
164 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur) {
165 spin_unlock(&mq_lock);
166 goto out_inode;
167 }
168 u->mq_bytes += mq_bytes;
169 spin_unlock(&mq_lock);
170
171 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
172 if (!info->messages) {
173 spin_lock(&mq_lock);
174 u->mq_bytes -= mq_bytes;
175 spin_unlock(&mq_lock);
176 goto out_inode;
177 }
178 /* all is ok */
179 info->user = get_uid(u);
180 } else if (S_ISDIR(mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700181 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Some things misbehave if size == 0 on a directory */
183 inode->i_size = 2 * DIRENT_SIZE;
184 inode->i_op = &mqueue_dir_inode_operations;
185 inode->i_fop = &simple_dir_operations;
186 }
187 }
188 return inode;
189out_inode:
190 make_bad_inode(inode);
191 iput(inode);
192 return NULL;
193}
194
195static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
196{
197 struct inode *inode;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700198 struct ipc_namespace *ns = data;
199 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 sb->s_blocksize = PAGE_CACHE_SIZE;
202 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
203 sb->s_magic = MQUEUE_MAGIC;
204 sb->s_op = &mqueue_super_ops;
205
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700206 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO,
207 NULL);
208 if (!inode) {
209 error = -ENOMEM;
210 goto out;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 sb->s_root = d_alloc_root(inode);
214 if (!sb->s_root) {
215 iput(inode);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700216 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700219out:
220 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
David Howells454e2392006-06-23 02:02:57 -0700223static int mqueue_get_sb(struct file_system_type *fs_type,
224 int flags, const char *dev_name,
225 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700227 if (!(flags & MS_KERNMOUNT))
228 data = current->nsproxy->ipc_ns;
229 return get_sb_ns(fs_type, flags, data, mqueue_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700232static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
235
Christoph Lametera35afb82007-05-16 22:10:57 -0700236 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static struct inode *mqueue_alloc_inode(struct super_block *sb)
240{
241 struct mqueue_inode_info *ei;
242
Christoph Lametere94b1762006-12-06 20:33:17 -0800243 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!ei)
245 return NULL;
246 return &ei->vfs_inode;
247}
248
249static void mqueue_destroy_inode(struct inode *inode)
250{
251 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
252}
253
254static void mqueue_delete_inode(struct inode *inode)
255{
256 struct mqueue_inode_info *info;
257 struct user_struct *user;
258 unsigned long mq_bytes;
259 int i;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700260 struct ipc_namespace *ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (S_ISDIR(inode->i_mode)) {
263 clear_inode(inode);
264 return;
265 }
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700266 ipc_ns = get_ns_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 info = MQUEUE_I(inode);
268 spin_lock(&info->lock);
269 for (i = 0; i < info->attr.mq_curmsgs; i++)
270 free_msg(info->messages[i]);
271 kfree(info->messages);
272 spin_unlock(&info->lock);
273
274 clear_inode(inode);
275
276 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
277 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
278 user = info->user;
279 if (user) {
280 spin_lock(&mq_lock);
281 user->mq_bytes -= mq_bytes;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700282 /*
283 * get_ns_from_inode() ensures that the
284 * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
285 * to which we now hold a reference, or it is NULL.
286 * We can't put it here under mq_lock, though.
287 */
288 if (ipc_ns)
289 ipc_ns->mq_queues_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 spin_unlock(&mq_lock);
291 free_uid(user);
292 }
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700293 if (ipc_ns)
294 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static int mqueue_create(struct inode *dir, struct dentry *dentry,
298 int mode, struct nameidata *nd)
299{
300 struct inode *inode;
301 struct mq_attr *attr = dentry->d_fsdata;
302 int error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700303 struct ipc_namespace *ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 spin_lock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700306 ipc_ns = __get_ns_from_inode(dir);
307 if (!ipc_ns) {
308 error = -EACCES;
309 goto out_unlock;
310 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700311 if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
312 !capable(CAP_SYS_RESOURCE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 error = -ENOSPC;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700314 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700316 ipc_ns->mq_queues_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_unlock(&mq_lock);
318
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700319 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!inode) {
321 error = -ENOMEM;
322 spin_lock(&mq_lock);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700323 ipc_ns->mq_queues_count--;
324 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700327 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 dir->i_size += DIRENT_SIZE;
329 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
330
331 d_instantiate(dentry, inode);
332 dget(dentry);
333 return 0;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700334out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_unlock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700336 if (ipc_ns)
337 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return error;
339}
340
341static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
342{
343 struct inode *inode = dentry->d_inode;
344
345 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
346 dir->i_size -= DIRENT_SIZE;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700347 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dput(dentry);
349 return 0;
350}
351
352/*
353* This is routine for system read from queue file.
354* To avoid mess with doing here some sort of mq_receive we allow
355* to read only queue size & notification info (the only values
356* that are interesting from user point of view and aren't accessible
357* through std routines)
358*/
359static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700360 size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Josef Sipek6d630792006-12-08 02:37:11 -0800362 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 char buffer[FILENT_SIZE];
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700364 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 spin_lock(&info->lock);
367 snprintf(buffer, sizeof(buffer),
368 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
369 info->qsize,
370 info->notify_owner ? info->notify.sigev_notify : 0,
371 (info->notify_owner &&
372 info->notify.sigev_notify == SIGEV_SIGNAL) ?
373 info->notify.sigev_signo : 0,
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800374 pid_vnr(info->notify_owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_unlock(&info->lock);
376 buffer[sizeof(buffer)-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700378 ret = simple_read_from_buffer(u_data, count, off, buffer,
379 strlen(buffer));
380 if (ret <= 0)
381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Josef Sipek6d630792006-12-08 02:37:11 -0800383 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 -0700384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700387static int mqueue_flush_file(struct file *filp, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Josef Sipek6d630792006-12-08 02:37:11 -0800389 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 spin_lock(&info->lock);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700392 if (task_tgid(current) == info->notify_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 remove_notification(info);
394
395 spin_unlock(&info->lock);
396 return 0;
397}
398
399static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
400{
Josef Sipek6d630792006-12-08 02:37:11 -0800401 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int retval = 0;
403
404 poll_wait(filp, &info->wait_q, poll_tab);
405
406 spin_lock(&info->lock);
407 if (info->attr.mq_curmsgs)
408 retval = POLLIN | POLLRDNORM;
409
410 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
411 retval |= POLLOUT | POLLWRNORM;
412 spin_unlock(&info->lock);
413
414 return retval;
415}
416
417/* Adds current to info->e_wait_q[sr] before element with smaller prio */
418static void wq_add(struct mqueue_inode_info *info, int sr,
419 struct ext_wait_queue *ewp)
420{
421 struct ext_wait_queue *walk;
422
423 ewp->task = current;
424
425 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
426 if (walk->task->static_prio <= current->static_prio) {
427 list_add_tail(&ewp->list, &walk->list);
428 return;
429 }
430 }
431 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
432}
433
434/*
435 * Puts current task to sleep. Caller must hold queue lock. After return
436 * lock isn't held.
437 * sr: SEND or RECV
438 */
439static int wq_sleep(struct mqueue_inode_info *info, int sr,
440 long timeout, struct ext_wait_queue *ewp)
441{
442 int retval;
443 signed long time;
444
445 wq_add(info, sr, ewp);
446
447 for (;;) {
448 set_current_state(TASK_INTERRUPTIBLE);
449
450 spin_unlock(&info->lock);
451 time = schedule_timeout(timeout);
452
453 while (ewp->state == STATE_PENDING)
454 cpu_relax();
455
456 if (ewp->state == STATE_READY) {
457 retval = 0;
458 goto out;
459 }
460 spin_lock(&info->lock);
461 if (ewp->state == STATE_READY) {
462 retval = 0;
463 goto out_unlock;
464 }
465 if (signal_pending(current)) {
466 retval = -ERESTARTSYS;
467 break;
468 }
469 if (time == 0) {
470 retval = -ETIMEDOUT;
471 break;
472 }
473 }
474 list_del(&ewp->list);
475out_unlock:
476 spin_unlock(&info->lock);
477out:
478 return retval;
479}
480
481/*
482 * Returns waiting task that should be serviced first or NULL if none exists
483 */
484static struct ext_wait_queue *wq_get_first_waiter(
485 struct mqueue_inode_info *info, int sr)
486{
487 struct list_head *ptr;
488
489 ptr = info->e_wait_q[sr].list.prev;
490 if (ptr == &info->e_wait_q[sr].list)
491 return NULL;
492 return list_entry(ptr, struct ext_wait_queue, list);
493}
494
495/* Auxiliary functions to manipulate messages' list */
496static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
497{
498 int k;
499
500 k = info->attr.mq_curmsgs - 1;
501 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
502 info->messages[k + 1] = info->messages[k];
503 k--;
504 }
505 info->attr.mq_curmsgs++;
506 info->qsize += ptr->m_ts;
507 info->messages[k + 1] = ptr;
508}
509
510static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
511{
512 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
513 return info->messages[info->attr.mq_curmsgs];
514}
515
516static inline void set_cookie(struct sk_buff *skb, char code)
517{
518 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
519}
520
521/*
522 * The next function is only to split too long sys_mq_timedsend
523 */
524static void __do_notify(struct mqueue_inode_info *info)
525{
526 /* notification
527 * invoked when there is registered process and there isn't process
528 * waiting synchronously for message AND state of queue changed from
529 * empty to not empty. Here we are sure that no one is waiting
530 * synchronously. */
531 if (info->notify_owner &&
532 info->attr.mq_curmsgs == 1) {
533 struct siginfo sig_i;
534 switch (info->notify.sigev_notify) {
535 case SIGEV_NONE:
536 break;
537 case SIGEV_SIGNAL:
538 /* sends signal */
539
540 sig_i.si_signo = info->notify.sigev_signo;
541 sig_i.si_errno = 0;
542 sig_i.si_code = SI_MESGQ;
543 sig_i.si_value = info->notify.sigev_value;
Sukadev Bhattiprolua6684992009-01-07 18:08:50 -0800544 sig_i.si_pid = task_tgid_nr_ns(current,
545 ns_of_pid(info->notify_owner));
David Howells414c0702008-11-14 10:39:06 +1100546 sig_i.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700548 kill_pid_info(info->notify.sigev_signo,
549 &sig_i, info->notify_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case SIGEV_THREAD:
552 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700553 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 }
556 /* after notification unregisters process */
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700557 put_pid(info->notify_owner);
558 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 wake_up(&info->wait_q);
561}
562
Al Viroc32c8af2008-12-14 03:46:48 -0500563static long prepare_timeout(struct timespec *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Al Viroc32c8af2008-12-14 03:46:48 -0500565 struct timespec nowts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 long timeout;
567
Al Viroc32c8af2008-12-14 03:46:48 -0500568 if (p) {
569 if (unlikely(p->tv_nsec < 0 || p->tv_sec < 0
570 || p->tv_nsec >= NSEC_PER_SEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -EINVAL;
572 nowts = CURRENT_TIME;
573 /* first subtract as jiffies can't be too big */
Al Viroc32c8af2008-12-14 03:46:48 -0500574 p->tv_sec -= nowts.tv_sec;
575 if (p->tv_nsec < nowts.tv_nsec) {
576 p->tv_nsec += NSEC_PER_SEC;
577 p->tv_sec--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Al Viroc32c8af2008-12-14 03:46:48 -0500579 p->tv_nsec -= nowts.tv_nsec;
580 if (p->tv_sec < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582
Al Viroc32c8af2008-12-14 03:46:48 -0500583 timeout = timespec_to_jiffies(p) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 } else
585 return MAX_SCHEDULE_TIMEOUT;
586
587 return timeout;
588}
589
590static void remove_notification(struct mqueue_inode_info *info)
591{
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700592 if (info->notify_owner != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 info->notify.sigev_notify == SIGEV_THREAD) {
594 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700595 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700597 put_pid(info->notify_owner);
598 info->notify_owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700601static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
604 return 0;
605 if (capable(CAP_SYS_RESOURCE)) {
606 if (attr->mq_maxmsg > HARD_MSGMAX)
607 return 0;
608 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700609 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
610 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612 }
613 /* check for overflow */
614 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
615 return 0;
616 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
617 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
618 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
619 return 0;
620 return 1;
621}
622
623/*
624 * Invoked when creating a new queue via sys_mq_open
625 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700626static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
627 struct dentry *dentry, int oflag, mode_t mode,
628 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
David Howells745ca242008-11-14 10:39:22 +1100630 const struct cred *cred = current_cred();
Dave Hansen4a3fd212008-02-15 14:37:48 -0800631 struct file *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int ret;
633
Al Viro564f6992008-12-14 04:02:26 -0500634 if (attr) {
Alexander Viro7c7dce92006-01-14 15:29:55 -0500635 ret = -EINVAL;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700636 if (!mq_attr_ok(ipc_ns, attr))
Alexander Viro7c7dce92006-01-14 15:29:55 -0500637 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* store for use during create */
Al Viro564f6992008-12-14 04:02:26 -0500639 dentry->d_fsdata = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Al Viroce3b0f82009-03-29 19:08:22 -0400642 mode &= ~current_umask();
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700643 ret = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800644 if (ret)
645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
647 dentry->d_fsdata = NULL;
648 if (ret)
Dave Hansen4a3fd212008-02-15 14:37:48 -0800649 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700651 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800652 /*
653 * dentry_open() took a persistent mnt_want_write(),
654 * so we can now drop this one.
655 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700656 mnt_drop_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800657 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dave Hansen4a3fd212008-02-15 14:37:48 -0800659out_drop_write:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700660 mnt_drop_write(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500661out:
662 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700663 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500664 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/* Opens existing queue */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700668static struct file *do_open(struct ipc_namespace *ipc_ns,
669 struct dentry *dentry, int oflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
David Howells745ca242008-11-14 10:39:22 +1100671 const struct cred *cred = current_cred();
672
673 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
674 MAY_READ | MAY_WRITE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Alexander Viro7c7dce92006-01-14 15:29:55 -0500676 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
677 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700678 mntput(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return ERR_PTR(-EINVAL);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Al Virof419a2e2008-07-22 00:07:17 -0400682 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
Alexander Viro7c7dce92006-01-14 15:29:55 -0500683 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700684 mntput(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return ERR_PTR(-EACCES);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700688 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Heiko Carstensd5460c92009-01-14 14:14:27 +0100691SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, mode_t, mode,
692 struct mq_attr __user *, u_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct dentry *dentry;
695 struct file *filp;
696 char *name;
Al Viro564f6992008-12-14 04:02:26 -0500697 struct mq_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int fd, error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700699 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Al Viro564f6992008-12-14 04:02:26 -0500701 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
702 return -EFAULT;
703
704 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -0500705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (IS_ERR(name = getname(u_name)))
707 return PTR_ERR(name);
708
Ulrich Drepper269f2132008-05-03 15:28:45 -0400709 fd = get_unused_fd_flags(O_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (fd < 0)
711 goto out_putname;
712
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700713 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
714 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (IS_ERR(dentry)) {
716 error = PTR_ERR(dentry);
717 goto out_err;
718 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700719 mntget(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (oflag & O_CREAT) {
722 if (dentry->d_inode) { /* entry already exists */
Al Viro5a190ae2007-06-07 12:19:32 -0400723 audit_inode(name, dentry);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500724 error = -EEXIST;
725 if (oflag & O_EXCL)
726 goto out;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700727 filp = do_open(ipc_ns, dentry, oflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700729 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
730 dentry, oflag, mode,
Al Viro564f6992008-12-14 04:02:26 -0500731 u_attr ? &attr : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Alexander Viro7c7dce92006-01-14 15:29:55 -0500733 } else {
734 error = -ENOENT;
735 if (!dentry->d_inode)
736 goto out;
Al Viro5a190ae2007-06-07 12:19:32 -0400737 audit_inode(name, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700738 filp = do_open(ipc_ns, dentry, oflag);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (IS_ERR(filp)) {
742 error = PTR_ERR(filp);
743 goto out_putfd;
744 }
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 fd_install(fd, filp);
747 goto out_upsem;
748
Alexander Viro7c7dce92006-01-14 15:29:55 -0500749out:
750 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700751 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500752out_putfd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 put_unused_fd(fd);
754out_err:
755 fd = error;
756out_upsem:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700757 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758out_putname:
759 putname(name);
760 return fd;
761}
762
Heiko Carstensd5460c92009-01-14 14:14:27 +0100763SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 int err;
766 char *name;
767 struct dentry *dentry;
768 struct inode *inode = NULL;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700769 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 name = getname(u_name);
772 if (IS_ERR(name))
773 return PTR_ERR(name);
774
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700775 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
Peter Zijlstra7a434812007-03-06 01:42:09 -0800776 I_MUTEX_PARENT);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700777 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (IS_ERR(dentry)) {
779 err = PTR_ERR(dentry);
780 goto out_unlock;
781 }
782
783 if (!dentry->d_inode) {
784 err = -ENOENT;
785 goto out_err;
786 }
787
788 inode = dentry->d_inode;
789 if (inode)
790 atomic_inc(&inode->i_count);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700791 err = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800792 if (err)
793 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700795 mnt_drop_write(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796out_err:
797 dput(dentry);
798
799out_unlock:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700800 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 putname(name);
802 if (inode)
803 iput(inode);
804
805 return err;
806}
807
808/* Pipelined send and receive functions.
809 *
810 * If a receiver finds no waiting message, then it registers itself in the
811 * list of waiting receivers. A sender checks that list before adding the new
812 * message into the message array. If there is a waiting receiver, then it
813 * bypasses the message array and directly hands the message over to the
814 * receiver.
815 * The receiver accepts the message and returns without grabbing the queue
816 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
817 * are necessary. The same algorithm is used for sysv semaphores, see
Serge E. Hallyn7b7a3172006-03-28 01:56:23 -0800818 * ipc/sem.c for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * The same algorithm is used for senders.
821 */
822
823/* pipelined_send() - send a message directly to the task waiting in
824 * sys_mq_timedreceive() (without inserting message into a queue).
825 */
826static inline void pipelined_send(struct mqueue_inode_info *info,
827 struct msg_msg *message,
828 struct ext_wait_queue *receiver)
829{
830 receiver->msg = message;
831 list_del(&receiver->list);
832 receiver->state = STATE_PENDING;
833 wake_up_process(receiver->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700834 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 receiver->state = STATE_READY;
836}
837
838/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
839 * gets its message and put to the queue (we have one free place for sure). */
840static inline void pipelined_receive(struct mqueue_inode_info *info)
841{
842 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
843
844 if (!sender) {
845 /* for poll */
846 wake_up_interruptible(&info->wait_q);
847 return;
848 }
849 msg_insert(sender->msg, info);
850 list_del(&sender->list);
851 sender->state = STATE_PENDING;
852 wake_up_process(sender->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700853 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 sender->state = STATE_READY;
855}
856
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100857SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
858 size_t, msg_len, unsigned int, msg_prio,
859 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct file *filp;
862 struct inode *inode;
863 struct ext_wait_queue wait;
864 struct ext_wait_queue *receiver;
865 struct msg_msg *msg_ptr;
866 struct mqueue_inode_info *info;
Al Viroc32c8af2008-12-14 03:46:48 -0500867 struct timespec ts, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 long timeout;
869 int ret;
870
Al Viroc32c8af2008-12-14 03:46:48 -0500871 if (u_abs_timeout) {
872 if (copy_from_user(&ts, u_abs_timeout,
873 sizeof(struct timespec)))
874 return -EFAULT;
875 p = &ts;
876 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
879 return -EINVAL;
880
Al Viroc32c8af2008-12-14 03:46:48 -0500881 audit_mq_sendrecv(mqdes, msg_len, msg_prio, p);
882 timeout = prepare_timeout(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 ret = -EBADF;
885 filp = fget(mqdes);
886 if (unlikely(!filp))
887 goto out;
888
Josef Sipek6d630792006-12-08 02:37:11 -0800889 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (unlikely(filp->f_op != &mqueue_file_operations))
891 goto out_fput;
892 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400893 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
896 goto out_fput;
897
898 if (unlikely(msg_len > info->attr.mq_msgsize)) {
899 ret = -EMSGSIZE;
900 goto out_fput;
901 }
902
903 /* First try to allocate memory, before doing anything with
904 * existing queues. */
905 msg_ptr = load_msg(u_msg_ptr, msg_len);
906 if (IS_ERR(msg_ptr)) {
907 ret = PTR_ERR(msg_ptr);
908 goto out_fput;
909 }
910 msg_ptr->m_ts = msg_len;
911 msg_ptr->m_type = msg_prio;
912
913 spin_lock(&info->lock);
914
915 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
916 if (filp->f_flags & O_NONBLOCK) {
917 spin_unlock(&info->lock);
918 ret = -EAGAIN;
919 } else if (unlikely(timeout < 0)) {
920 spin_unlock(&info->lock);
921 ret = timeout;
922 } else {
923 wait.task = current;
924 wait.msg = (void *) msg_ptr;
925 wait.state = STATE_NONE;
926 ret = wq_sleep(info, SEND, timeout, &wait);
927 }
928 if (ret < 0)
929 free_msg(msg_ptr);
930 } else {
931 receiver = wq_get_first_waiter(info, RECV);
932 if (receiver) {
933 pipelined_send(info, msg_ptr, receiver);
934 } else {
935 /* adds message to the queue */
936 msg_insert(msg_ptr, info);
937 __do_notify(info);
938 }
939 inode->i_atime = inode->i_mtime = inode->i_ctime =
940 CURRENT_TIME;
941 spin_unlock(&info->lock);
942 ret = 0;
943 }
944out_fput:
945 fput(filp);
946out:
947 return ret;
948}
949
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100950SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
951 size_t, msg_len, unsigned int __user *, u_msg_prio,
952 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 long timeout;
955 ssize_t ret;
956 struct msg_msg *msg_ptr;
957 struct file *filp;
958 struct inode *inode;
959 struct mqueue_inode_info *info;
960 struct ext_wait_queue wait;
Al Viroc32c8af2008-12-14 03:46:48 -0500961 struct timespec ts, *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Al Viroc32c8af2008-12-14 03:46:48 -0500963 if (u_abs_timeout) {
964 if (copy_from_user(&ts, u_abs_timeout,
965 sizeof(struct timespec)))
966 return -EFAULT;
967 p = &ts;
968 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500969
Al Viroc32c8af2008-12-14 03:46:48 -0500970 audit_mq_sendrecv(mqdes, msg_len, 0, p);
971 timeout = prepare_timeout(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 ret = -EBADF;
974 filp = fget(mqdes);
975 if (unlikely(!filp))
976 goto out;
977
Josef Sipek6d630792006-12-08 02:37:11 -0800978 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (unlikely(filp->f_op != &mqueue_file_operations))
980 goto out_fput;
981 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400982 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (unlikely(!(filp->f_mode & FMODE_READ)))
985 goto out_fput;
986
987 /* checks if buffer is big enough */
988 if (unlikely(msg_len < info->attr.mq_msgsize)) {
989 ret = -EMSGSIZE;
990 goto out_fput;
991 }
992
993 spin_lock(&info->lock);
994 if (info->attr.mq_curmsgs == 0) {
995 if (filp->f_flags & O_NONBLOCK) {
996 spin_unlock(&info->lock);
997 ret = -EAGAIN;
998 msg_ptr = NULL;
999 } else if (unlikely(timeout < 0)) {
1000 spin_unlock(&info->lock);
1001 ret = timeout;
1002 msg_ptr = NULL;
1003 } else {
1004 wait.task = current;
1005 wait.state = STATE_NONE;
1006 ret = wq_sleep(info, RECV, timeout, &wait);
1007 msg_ptr = wait.msg;
1008 }
1009 } else {
1010 msg_ptr = msg_get(info);
1011
1012 inode->i_atime = inode->i_mtime = inode->i_ctime =
1013 CURRENT_TIME;
1014
1015 /* There is now free space in queue. */
1016 pipelined_receive(info);
1017 spin_unlock(&info->lock);
1018 ret = 0;
1019 }
1020 if (ret == 0) {
1021 ret = msg_ptr->m_ts;
1022
1023 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
1024 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1025 ret = -EFAULT;
1026 }
1027 free_msg(msg_ptr);
1028 }
1029out_fput:
1030 fput(filp);
1031out:
1032 return ret;
1033}
1034
1035/*
1036 * Notes: the case when user wants us to deregister (with NULL as pointer)
1037 * and he isn't currently owner of notification, will be silently discarded.
1038 * It isn't explicitly defined in the POSIX.
1039 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001040SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1041 const struct sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int ret;
1044 struct file *filp;
1045 struct sock *sock;
1046 struct inode *inode;
1047 struct sigevent notification;
1048 struct mqueue_inode_info *info;
1049 struct sk_buff *nc;
1050
Al Viro20114f72008-12-10 07:16:12 -05001051 if (u_notification) {
1052 if (copy_from_user(&notification, u_notification,
1053 sizeof(struct sigevent)))
1054 return -EFAULT;
1055 }
1056
1057 audit_mq_notify(mqdes, u_notification ? &notification : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -05001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 nc = NULL;
1060 sock = NULL;
1061 if (u_notification != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1063 notification.sigev_notify != SIGEV_SIGNAL &&
1064 notification.sigev_notify != SIGEV_THREAD))
1065 return -EINVAL;
1066 if (notification.sigev_notify == SIGEV_SIGNAL &&
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001067 !valid_signal(notification.sigev_signo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EINVAL;
1069 }
1070 if (notification.sigev_notify == SIGEV_THREAD) {
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001071 long timeo;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* create the notify skb */
1074 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1075 ret = -ENOMEM;
1076 if (!nc)
1077 goto out;
1078 ret = -EFAULT;
1079 if (copy_from_user(nc->data,
1080 notification.sigev_value.sival_ptr,
1081 NOTIFY_COOKIE_LEN)) {
1082 goto out;
1083 }
1084
1085 /* TODO: add a header? */
1086 skb_put(nc, NOTIFY_COOKIE_LEN);
1087 /* and attach it to the socket */
1088retry:
1089 filp = fget(notification.sigev_signo);
1090 ret = -EBADF;
1091 if (!filp)
1092 goto out;
1093 sock = netlink_getsockbyfilp(filp);
1094 fput(filp);
1095 if (IS_ERR(sock)) {
1096 ret = PTR_ERR(sock);
1097 sock = NULL;
1098 goto out;
1099 }
1100
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001101 timeo = MAX_SCHEDULE_TIMEOUT;
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001102 ret = netlink_attachskb(sock, nc, &timeo, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (ret == 1)
1104 goto retry;
1105 if (ret) {
1106 sock = NULL;
1107 nc = NULL;
1108 goto out;
1109 }
1110 }
1111 }
1112
1113 ret = -EBADF;
1114 filp = fget(mqdes);
1115 if (!filp)
1116 goto out;
1117
Josef Sipek6d630792006-12-08 02:37:11 -08001118 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (unlikely(filp->f_op != &mqueue_file_operations))
1120 goto out_fput;
1121 info = MQUEUE_I(inode);
1122
1123 ret = 0;
1124 spin_lock(&info->lock);
1125 if (u_notification == NULL) {
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001126 if (info->notify_owner == task_tgid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 remove_notification(info);
1128 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1129 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001130 } else if (info->notify_owner != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ret = -EBUSY;
1132 } else {
1133 switch (notification.sigev_notify) {
1134 case SIGEV_NONE:
1135 info->notify.sigev_notify = SIGEV_NONE;
1136 break;
1137 case SIGEV_THREAD:
1138 info->notify_sock = sock;
1139 info->notify_cookie = nc;
1140 sock = NULL;
1141 nc = NULL;
1142 info->notify.sigev_notify = SIGEV_THREAD;
1143 break;
1144 case SIGEV_SIGNAL:
1145 info->notify.sigev_signo = notification.sigev_signo;
1146 info->notify.sigev_value = notification.sigev_value;
1147 info->notify.sigev_notify = SIGEV_SIGNAL;
1148 break;
1149 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001150
1151 info->notify_owner = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1153 }
1154 spin_unlock(&info->lock);
1155out_fput:
1156 fput(filp);
1157out:
1158 if (sock) {
1159 netlink_detachskb(sock, nc);
1160 } else if (nc) {
1161 dev_kfree_skb(nc);
1162 }
1163 return ret;
1164}
1165
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001166SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1167 const struct mq_attr __user *, u_mqstat,
1168 struct mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 int ret;
1171 struct mq_attr mqstat, omqstat;
1172 struct file *filp;
1173 struct inode *inode;
1174 struct mqueue_inode_info *info;
1175
1176 if (u_mqstat != NULL) {
1177 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1178 return -EFAULT;
1179 if (mqstat.mq_flags & (~O_NONBLOCK))
1180 return -EINVAL;
1181 }
1182
1183 ret = -EBADF;
1184 filp = fget(mqdes);
1185 if (!filp)
1186 goto out;
1187
Josef Sipek6d630792006-12-08 02:37:11 -08001188 inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (unlikely(filp->f_op != &mqueue_file_operations))
1190 goto out_fput;
1191 info = MQUEUE_I(inode);
1192
1193 spin_lock(&info->lock);
1194
1195 omqstat = info->attr;
1196 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1197 if (u_mqstat) {
Al Viro73929062008-12-10 06:58:59 -05001198 audit_mq_getsetattr(mqdes, &mqstat);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001199 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (mqstat.mq_flags & O_NONBLOCK)
1201 filp->f_flags |= O_NONBLOCK;
1202 else
1203 filp->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001204 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1207 }
1208
1209 spin_unlock(&info->lock);
1210
1211 ret = 0;
1212 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1213 sizeof(struct mq_attr)))
1214 ret = -EFAULT;
1215
1216out_fput:
1217 fput(filp);
1218out:
1219 return ret;
1220}
1221
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001222static const struct inode_operations mqueue_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .lookup = simple_lookup,
1224 .create = mqueue_create,
1225 .unlink = mqueue_unlink,
1226};
1227
Arjan van de Ven9a321442007-02-12 00:55:35 -08001228static const struct file_operations mqueue_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 .flush = mqueue_flush_file,
1230 .poll = mqueue_poll_file,
1231 .read = mqueue_read_file,
1232};
1233
1234static struct super_operations mqueue_super_ops = {
1235 .alloc_inode = mqueue_alloc_inode,
1236 .destroy_inode = mqueue_destroy_inode,
1237 .statfs = simple_statfs,
1238 .delete_inode = mqueue_delete_inode,
1239 .drop_inode = generic_delete_inode,
1240};
1241
1242static struct file_system_type mqueue_fs_type = {
1243 .name = "mqueue",
1244 .get_sb = mqueue_get_sb,
1245 .kill_sb = kill_litter_super,
1246};
1247
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001248int mq_init_ns(struct ipc_namespace *ns)
1249{
1250 ns->mq_queues_count = 0;
1251 ns->mq_queues_max = DFLT_QUEUESMAX;
1252 ns->mq_msg_max = DFLT_MSGMAX;
1253 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
1254
1255 ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1256 if (IS_ERR(ns->mq_mnt)) {
1257 int err = PTR_ERR(ns->mq_mnt);
1258 ns->mq_mnt = NULL;
1259 return err;
1260 }
1261 return 0;
1262}
1263
1264void mq_clear_sbinfo(struct ipc_namespace *ns)
1265{
1266 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1267}
1268
1269void mq_put_mnt(struct ipc_namespace *ns)
1270{
1271 mntput(ns->mq_mnt);
1272}
1273
Joe Kortyb231cca42008-10-18 20:28:32 -07001274static int msg_max_limit_min = MIN_MSGMAX;
1275static int msg_max_limit_max = MAX_MSGMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Joe Kortyb231cca42008-10-18 20:28:32 -07001277static int msg_maxsize_limit_min = MIN_MSGSIZEMAX;
1278static int msg_maxsize_limit_max = MAX_MSGSIZEMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280static ctl_table mq_sysctls[] = {
1281 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 .procname = "queues_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001283 .data = &init_ipc_ns.mq_queues_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 .maxlen = sizeof(int),
1285 .mode = 0644,
1286 .proc_handler = &proc_dointvec,
1287 },
1288 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 .procname = "msg_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001290 .data = &init_ipc_ns.mq_msg_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 .maxlen = sizeof(int),
1292 .mode = 0644,
1293 .proc_handler = &proc_dointvec_minmax,
1294 .extra1 = &msg_max_limit_min,
1295 .extra2 = &msg_max_limit_max,
1296 },
1297 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .procname = "msgsize_max",
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001299 .data = &init_ipc_ns.mq_msgsize_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 .maxlen = sizeof(int),
1301 .mode = 0644,
1302 .proc_handler = &proc_dointvec_minmax,
1303 .extra1 = &msg_maxsize_limit_min,
1304 .extra2 = &msg_maxsize_limit_max,
1305 },
1306 { .ctl_name = 0 }
1307};
1308
1309static ctl_table mq_sysctl_dir[] = {
1310 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 .procname = "mqueue",
1312 .mode = 0555,
1313 .child = mq_sysctls,
1314 },
1315 { .ctl_name = 0 }
1316};
1317
1318static ctl_table mq_sysctl_root[] = {
1319 {
1320 .ctl_name = CTL_FS,
1321 .procname = "fs",
1322 .mode = 0555,
1323 .child = mq_sysctl_dir,
1324 },
1325 { .ctl_name = 0 }
1326};
1327
1328static int __init init_mqueue_fs(void)
1329{
1330 int error;
1331
1332 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1333 sizeof(struct mqueue_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001334 SLAB_HWCACHE_ALIGN, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (mqueue_inode_cachep == NULL)
1336 return -ENOMEM;
1337
1338 /* ignore failues - they are not fatal */
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001339 mq_sysctl_table = register_sysctl_table(mq_sysctl_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 error = register_filesystem(&mqueue_fs_type);
1342 if (error)
1343 goto out_sysctl;
1344
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001345 spin_lock_init(&mq_lock);
1346
1347 init_ipc_ns.mq_mnt = kern_mount_data(&mqueue_fs_type, &init_ipc_ns);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -07001348 if (IS_ERR(init_ipc_ns.mq_mnt)) {
1349 error = PTR_ERR(init_ipc_ns.mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto out_filesystem;
1351 }
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return 0;
1354
1355out_filesystem:
1356 unregister_filesystem(&mqueue_fs_type);
1357out_sysctl:
1358 if (mq_sysctl_table)
1359 unregister_sysctl_table(mq_sysctl_table);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001360 kmem_cache_destroy(mqueue_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return error;
1362}
1363
1364__initcall(init_mqueue_fs);