ipc: remove msg handling from queue scan
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / ipc / msg.c
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/msg.c
5a06a363 3 * Copyright (C) 1992 Krishna Balasubramanian
1da177e4
LT
4 *
5 * Removed all the remaining kerneld mess
6 * Catch the -EFAULT stuff properly
7 * Use GFP_KERNEL for messages as in 1.2
8 * Fixed up the unchecked user space derefs
9 * Copyright (C) 1998 Alan Cox & Andi Kleen
10 *
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
12 *
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
624dffcb 15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
073115d6
SG
16 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
1e786937
KK
19 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
23 */
24
c59ede7b 25#include <linux/capability.h>
1da177e4
LT
26#include <linux/msg.h>
27#include <linux/spinlock.h>
28#include <linux/init.h>
f7bf3df8 29#include <linux/mm.h>
1da177e4
LT
30#include <linux/proc_fs.h>
31#include <linux/list.h>
32#include <linux/security.h>
33#include <linux/sched.h>
34#include <linux/syscalls.h>
35#include <linux/audit.h>
19b4946c 36#include <linux/seq_file.h>
3e148c79 37#include <linux/rwsem.h>
1e786937 38#include <linux/nsproxy.h>
ae5e1b22 39#include <linux/ipc_namespace.h>
5f921ae9 40
1da177e4
LT
41#include <asm/current.h>
42#include <asm/uaccess.h>
43#include "util.h"
44
5a06a363
IM
45/*
46 * one msg_receiver structure for each sleeping receiver:
47 */
1da177e4 48struct msg_receiver {
5a06a363
IM
49 struct list_head r_list;
50 struct task_struct *r_tsk;
1da177e4 51
5a06a363
IM
52 int r_mode;
53 long r_msgtype;
54 long r_maxsize;
1da177e4 55
80491eb9 56 struct msg_msg *volatile r_msg;
1da177e4
LT
57};
58
59/* one msg_sender for each sleeping sender */
60struct msg_sender {
5a06a363
IM
61 struct list_head list;
62 struct task_struct *tsk;
1da177e4
LT
63};
64
65#define SEARCH_ANY 1
66#define SEARCH_EQUAL 2
67#define SEARCH_NOTEQUAL 3
68#define SEARCH_LESSEQUAL 4
69
ed2ddbf8 70#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
1da177e4 71
1e786937 72#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
1e786937 73
01b8b07a 74static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
7748dbfa 75static int newque(struct ipc_namespace *, struct ipc_params *);
1da177e4 76#ifdef CONFIG_PROC_FS
19b4946c 77static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
1da177e4
LT
78#endif
79
f7bf3df8
ND
80/*
81 * Scale msgmni with the available lowmem size: the memory dedicated to msg
82 * queues should occupy at most 1/MSG_MEM_SCALE of lowmem.
4d89dc6a
ND
83 * Also take into account the number of nsproxies created so far.
84 * This should be done staying within the (MSGMNI , IPCMNI/nr_ipc_ns) range.
f7bf3df8 85 */
b6b337ad 86void recompute_msgmni(struct ipc_namespace *ns)
f7bf3df8
ND
87{
88 struct sysinfo i;
89 unsigned long allowed;
4d89dc6a 90 int nb_ns;
f7bf3df8
ND
91
92 si_meminfo(&i);
93 allowed = (((i.totalram - i.totalhigh) / MSG_MEM_SCALE) * i.mem_unit)
94 / MSGMNB;
4d89dc6a
ND
95 nb_ns = atomic_read(&nr_ipc_ns);
96 allowed /= nb_ns;
f7bf3df8
ND
97
98 if (allowed < MSGMNI) {
99 ns->msg_ctlmni = MSGMNI;
dfcceb26 100 return;
f7bf3df8
ND
101 }
102
4d89dc6a
ND
103 if (allowed > IPCMNI / nb_ns) {
104 ns->msg_ctlmni = IPCMNI / nb_ns;
dfcceb26 105 return;
f7bf3df8
ND
106 }
107
108 ns->msg_ctlmni = allowed;
f7bf3df8
ND
109}
110
ed2ddbf8 111void msg_init_ns(struct ipc_namespace *ns)
1e786937 112{
1e786937
KK
113 ns->msg_ctlmax = MSGMAX;
114 ns->msg_ctlmnb = MSGMNB;
f7bf3df8
ND
115
116 recompute_msgmni(ns);
117
3ac88a41
KK
118 atomic_set(&ns->msg_bytes, 0);
119 atomic_set(&ns->msg_hdrs, 0);
ed2ddbf8 120 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1e786937
KK
121}
122
ae5e1b22 123#ifdef CONFIG_IPC_NS
1e786937
KK
124void msg_exit_ns(struct ipc_namespace *ns)
125{
01b8b07a 126 free_ipcs(ns, &msg_ids(ns), freeque);
7d6feeb2 127 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
1e786937 128}
ae5e1b22 129#endif
1e786937 130
5a06a363 131void __init msg_init(void)
1da177e4 132{
ed2ddbf8 133 msg_init_ns(&init_ipc_ns);
dfcceb26
ND
134
135 printk(KERN_INFO "msgmni has been set to %d\n",
136 init_ipc_ns.msg_ctlmni);
137
19b4946c
MW
138 ipc_init_proc_interface("sysvipc/msg",
139 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
1e786937 140 IPC_MSG_IDS, sysvipc_msg_proc_show);
1da177e4
LT
141}
142
3e148c79
ND
143/*
144 * msg_lock_(check_) routines are called in the paths where the rw_mutex
145 * is not held.
146 */
023a5355
ND
147static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
148{
03f02c76
ND
149 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
150
b1ed88b4
PP
151 if (IS_ERR(ipcp))
152 return (struct msg_queue *)ipcp;
153
03f02c76 154 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
155}
156
157static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
158 int id)
159{
03f02c76
ND
160 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
161
b1ed88b4
PP
162 if (IS_ERR(ipcp))
163 return (struct msg_queue *)ipcp;
164
03f02c76 165 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
166}
167
7ca7e564
ND
168static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
169{
170 ipc_rmid(&msg_ids(ns), &s->q_perm);
171}
172
f4566f04
ND
173/**
174 * newque - Create a new msg queue
175 * @ns: namespace
176 * @params: ptr to the structure that contains the key and msgflg
177 *
3e148c79 178 * Called with msg_ids.rw_mutex held (writer)
f4566f04 179 */
7748dbfa 180static int newque(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4 181{
1da177e4 182 struct msg_queue *msq;
5a06a363 183 int id, retval;
7748dbfa
ND
184 key_t key = params->key;
185 int msgflg = params->flg;
1da177e4 186
5a06a363
IM
187 msq = ipc_rcu_alloc(sizeof(*msq));
188 if (!msq)
1da177e4
LT
189 return -ENOMEM;
190
5a06a363 191 msq->q_perm.mode = msgflg & S_IRWXUGO;
1da177e4
LT
192 msq->q_perm.key = key;
193
194 msq->q_perm.security = NULL;
195 retval = security_msg_queue_alloc(msq);
196 if (retval) {
197 ipc_rcu_putref(msq);
198 return retval;
199 }
200
7ca7e564
ND
201 /*
202 * ipc_addid() locks msq
203 */
1e786937 204 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
283bb7fa 205 if (id < 0) {
1da177e4
LT
206 security_msg_queue_free(msq);
207 ipc_rcu_putref(msq);
283bb7fa 208 return id;
1da177e4
LT
209 }
210
211 msq->q_stime = msq->q_rtime = 0;
212 msq->q_ctime = get_seconds();
213 msq->q_cbytes = msq->q_qnum = 0;
1e786937 214 msq->q_qbytes = ns->msg_ctlmnb;
1da177e4
LT
215 msq->q_lspid = msq->q_lrpid = 0;
216 INIT_LIST_HEAD(&msq->q_messages);
217 INIT_LIST_HEAD(&msq->q_receivers);
218 INIT_LIST_HEAD(&msq->q_senders);
7ca7e564 219
1da177e4
LT
220 msg_unlock(msq);
221
7ca7e564 222 return msq->q_perm.id;
1da177e4
LT
223}
224
5a06a363 225static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
1da177e4 226{
5a06a363
IM
227 mss->tsk = current;
228 current->state = TASK_INTERRUPTIBLE;
229 list_add_tail(&mss->list, &msq->q_senders);
1da177e4
LT
230}
231
5a06a363 232static inline void ss_del(struct msg_sender *mss)
1da177e4 233{
5a06a363 234 if (mss->list.next != NULL)
1da177e4
LT
235 list_del(&mss->list);
236}
237
5a06a363 238static void ss_wakeup(struct list_head *h, int kill)
1da177e4
LT
239{
240 struct list_head *tmp;
241
242 tmp = h->next;
243 while (tmp != h) {
5a06a363
IM
244 struct msg_sender *mss;
245
246 mss = list_entry(tmp, struct msg_sender, list);
1da177e4 247 tmp = tmp->next;
5a06a363
IM
248 if (kill)
249 mss->list.next = NULL;
1da177e4
LT
250 wake_up_process(mss->tsk);
251 }
252}
253
5a06a363 254static void expunge_all(struct msg_queue *msq, int res)
1da177e4
LT
255{
256 struct list_head *tmp;
257
258 tmp = msq->q_receivers.next;
259 while (tmp != &msq->q_receivers) {
5a06a363
IM
260 struct msg_receiver *msr;
261
262 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4
LT
263 tmp = tmp->next;
264 msr->r_msg = NULL;
265 wake_up_process(msr->r_tsk);
266 smp_mb();
267 msr->r_msg = ERR_PTR(res);
268 }
269}
5a06a363
IM
270
271/*
272 * freeque() wakes up waiters on the sender and receiver waiting queue,
f4566f04
ND
273 * removes the message queue from message queue ID IDR, and cleans up all the
274 * messages associated with this queue.
1da177e4 275 *
3e148c79
ND
276 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
277 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
1da177e4 278 */
01b8b07a 279static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1da177e4
LT
280{
281 struct list_head *tmp;
01b8b07a 282 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
1da177e4 283
5a06a363
IM
284 expunge_all(msq, -EIDRM);
285 ss_wakeup(&msq->q_senders, 1);
7ca7e564 286 msg_rmid(ns, msq);
1da177e4 287 msg_unlock(msq);
5a06a363 288
1da177e4 289 tmp = msq->q_messages.next;
5a06a363
IM
290 while (tmp != &msq->q_messages) {
291 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
292
1da177e4 293 tmp = tmp->next;
3ac88a41 294 atomic_dec(&ns->msg_hdrs);
1da177e4
LT
295 free_msg(msg);
296 }
3ac88a41 297 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
1da177e4
LT
298 security_msg_queue_free(msq);
299 ipc_rcu_putref(msq);
300}
301
f4566f04 302/*
3e148c79 303 * Called with msg_ids.rw_mutex and ipcp locked.
f4566f04 304 */
03f02c76 305static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
7748dbfa 306{
03f02c76
ND
307 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
308
309 return security_msg_queue_associate(msq, msgflg);
7748dbfa
ND
310}
311
e48fbb69 312SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
1da177e4 313{
1e786937 314 struct ipc_namespace *ns;
7748dbfa
ND
315 struct ipc_ops msg_ops;
316 struct ipc_params msg_params;
1e786937
KK
317
318 ns = current->nsproxy->ipc_ns;
7ca7e564 319
7748dbfa
ND
320 msg_ops.getnew = newque;
321 msg_ops.associate = msg_security;
322 msg_ops.more_checks = NULL;
323
324 msg_params.key = key;
325 msg_params.flg = msgflg;
5a06a363 326
7748dbfa 327 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
1da177e4
LT
328}
329
5a06a363
IM
330static inline unsigned long
331copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
1da177e4
LT
332{
333 switch(version) {
334 case IPC_64:
5a06a363 335 return copy_to_user(buf, in, sizeof(*in));
1da177e4 336 case IPC_OLD:
5a06a363 337 {
1da177e4
LT
338 struct msqid_ds out;
339
5a06a363 340 memset(&out, 0, sizeof(out));
1da177e4
LT
341
342 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
343
344 out.msg_stime = in->msg_stime;
345 out.msg_rtime = in->msg_rtime;
346 out.msg_ctime = in->msg_ctime;
347
4be929be
AD
348 if (in->msg_cbytes > USHRT_MAX)
349 out.msg_cbytes = USHRT_MAX;
1da177e4
LT
350 else
351 out.msg_cbytes = in->msg_cbytes;
352 out.msg_lcbytes = in->msg_cbytes;
353
4be929be
AD
354 if (in->msg_qnum > USHRT_MAX)
355 out.msg_qnum = USHRT_MAX;
1da177e4
LT
356 else
357 out.msg_qnum = in->msg_qnum;
358
4be929be
AD
359 if (in->msg_qbytes > USHRT_MAX)
360 out.msg_qbytes = USHRT_MAX;
1da177e4
LT
361 else
362 out.msg_qbytes = in->msg_qbytes;
363 out.msg_lqbytes = in->msg_qbytes;
364
365 out.msg_lspid = in->msg_lspid;
366 out.msg_lrpid = in->msg_lrpid;
367
5a06a363
IM
368 return copy_to_user(buf, &out, sizeof(out));
369 }
1da177e4
LT
370 default:
371 return -EINVAL;
372 }
373}
374
5a06a363 375static inline unsigned long
016d7132 376copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
1da177e4
LT
377{
378 switch(version) {
379 case IPC_64:
016d7132 380 if (copy_from_user(out, buf, sizeof(*out)))
1da177e4 381 return -EFAULT;
1da177e4 382 return 0;
1da177e4 383 case IPC_OLD:
5a06a363 384 {
1da177e4
LT
385 struct msqid_ds tbuf_old;
386
5a06a363 387 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4
LT
388 return -EFAULT;
389
016d7132
PP
390 out->msg_perm.uid = tbuf_old.msg_perm.uid;
391 out->msg_perm.gid = tbuf_old.msg_perm.gid;
392 out->msg_perm.mode = tbuf_old.msg_perm.mode;
1da177e4 393
5a06a363 394 if (tbuf_old.msg_qbytes == 0)
016d7132 395 out->msg_qbytes = tbuf_old.msg_lqbytes;
1da177e4 396 else
016d7132 397 out->msg_qbytes = tbuf_old.msg_qbytes;
1da177e4
LT
398
399 return 0;
5a06a363 400 }
1da177e4
LT
401 default:
402 return -EINVAL;
403 }
404}
405
a0d092fc
PP
406/*
407 * This function handles some msgctl commands which require the rw_mutex
408 * to be held in write mode.
409 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
410 */
411static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
412 struct msqid_ds __user *buf, int version)
1da177e4 413{
1da177e4 414 struct kern_ipc_perm *ipcp;
f1970c48 415 struct msqid64_ds uninitialized_var(msqid64);
a0d092fc
PP
416 struct msg_queue *msq;
417 int err;
418
419 if (cmd == IPC_SET) {
016d7132 420 if (copy_msqid_from_user(&msqid64, buf, version))
a0d092fc
PP
421 return -EFAULT;
422 }
423
b0e77598 424 ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
a5f75e7f
PP
425 &msqid64.msg_perm, msqid64.msg_qbytes);
426 if (IS_ERR(ipcp))
427 return PTR_ERR(ipcp);
a0d092fc 428
a5f75e7f 429 msq = container_of(ipcp, struct msg_queue, q_perm);
a0d092fc
PP
430
431 err = security_msg_queue_msgctl(msq, cmd);
432 if (err)
433 goto out_unlock;
434
435 switch (cmd) {
436 case IPC_RMID:
437 freeque(ns, ipcp);
438 goto out_up;
439 case IPC_SET:
016d7132 440 if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
a0d092fc
PP
441 !capable(CAP_SYS_RESOURCE)) {
442 err = -EPERM;
443 goto out_unlock;
444 }
445
1efdb69b
EB
446 err = ipc_update_perm(&msqid64.msg_perm, ipcp);
447 if (err)
448 goto out_unlock;
449
016d7132 450 msq->q_qbytes = msqid64.msg_qbytes;
a0d092fc 451
a0d092fc
PP
452 msq->q_ctime = get_seconds();
453 /* sleeping receivers might be excluded by
454 * stricter permissions.
455 */
456 expunge_all(msq, -EAGAIN);
457 /* sleeping senders might be able to send
458 * due to a larger queue size.
459 */
460 ss_wakeup(&msq->q_senders, 0);
461 break;
462 default:
463 err = -EINVAL;
464 }
465out_unlock:
466 msg_unlock(msq);
467out_up:
468 up_write(&msg_ids(ns).rw_mutex);
469 return err;
470}
471
e48fbb69 472SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
a0d092fc 473{
5a06a363
IM
474 struct msg_queue *msq;
475 int err, version;
1e786937 476 struct ipc_namespace *ns;
5a06a363 477
1da177e4
LT
478 if (msqid < 0 || cmd < 0)
479 return -EINVAL;
480
481 version = ipc_parse_version(&cmd);
1e786937 482 ns = current->nsproxy->ipc_ns;
1da177e4
LT
483
484 switch (cmd) {
5a06a363
IM
485 case IPC_INFO:
486 case MSG_INFO:
487 {
1da177e4
LT
488 struct msginfo msginfo;
489 int max_id;
5a06a363 490
1da177e4
LT
491 if (!buf)
492 return -EFAULT;
5a06a363
IM
493 /*
494 * We must not return kernel stack data.
1da177e4
LT
495 * due to padding, it's not enough
496 * to set all member fields.
497 */
1da177e4
LT
498 err = security_msg_queue_msgctl(NULL, cmd);
499 if (err)
500 return err;
501
5a06a363 502 memset(&msginfo, 0, sizeof(msginfo));
1e786937
KK
503 msginfo.msgmni = ns->msg_ctlmni;
504 msginfo.msgmax = ns->msg_ctlmax;
505 msginfo.msgmnb = ns->msg_ctlmnb;
1da177e4
LT
506 msginfo.msgssz = MSGSSZ;
507 msginfo.msgseg = MSGSEG;
3e148c79 508 down_read(&msg_ids(ns).rw_mutex);
1da177e4 509 if (cmd == MSG_INFO) {
1e786937 510 msginfo.msgpool = msg_ids(ns).in_use;
3ac88a41
KK
511 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
512 msginfo.msgtql = atomic_read(&ns->msg_bytes);
1da177e4
LT
513 } else {
514 msginfo.msgmap = MSGMAP;
515 msginfo.msgpool = MSGPOOL;
516 msginfo.msgtql = MSGTQL;
517 }
7ca7e564 518 max_id = ipc_get_maxid(&msg_ids(ns));
3e148c79 519 up_read(&msg_ids(ns).rw_mutex);
5a06a363 520 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
1da177e4 521 return -EFAULT;
5a06a363 522 return (max_id < 0) ? 0 : max_id;
1da177e4 523 }
7ca7e564 524 case MSG_STAT: /* msqid is an index rather than a msg queue id */
1da177e4
LT
525 case IPC_STAT:
526 {
527 struct msqid64_ds tbuf;
528 int success_return;
5a06a363 529
1da177e4
LT
530 if (!buf)
531 return -EFAULT;
1da177e4 532
5a06a363 533 if (cmd == MSG_STAT) {
023a5355
ND
534 msq = msg_lock(ns, msqid);
535 if (IS_ERR(msq))
536 return PTR_ERR(msq);
7ca7e564 537 success_return = msq->q_perm.id;
1da177e4 538 } else {
023a5355
ND
539 msq = msg_lock_check(ns, msqid);
540 if (IS_ERR(msq))
541 return PTR_ERR(msq);
1da177e4
LT
542 success_return = 0;
543 }
544 err = -EACCES;
b0e77598 545 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
1da177e4
LT
546 goto out_unlock;
547
548 err = security_msg_queue_msgctl(msq, cmd);
549 if (err)
550 goto out_unlock;
551
023a5355
ND
552 memset(&tbuf, 0, sizeof(tbuf));
553
1da177e4
LT
554 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
555 tbuf.msg_stime = msq->q_stime;
556 tbuf.msg_rtime = msq->q_rtime;
557 tbuf.msg_ctime = msq->q_ctime;
558 tbuf.msg_cbytes = msq->q_cbytes;
559 tbuf.msg_qnum = msq->q_qnum;
560 tbuf.msg_qbytes = msq->q_qbytes;
561 tbuf.msg_lspid = msq->q_lspid;
562 tbuf.msg_lrpid = msq->q_lrpid;
563 msg_unlock(msq);
564 if (copy_msqid_to_user(buf, &tbuf, version))
565 return -EFAULT;
566 return success_return;
567 }
568 case IPC_SET:
1da177e4 569 case IPC_RMID:
a0d092fc
PP
570 err = msgctl_down(ns, msqid, cmd, buf, version);
571 return err;
1da177e4
LT
572 default:
573 return -EINVAL;
574 }
575
1da177e4
LT
576out_unlock:
577 msg_unlock(msq);
578 return err;
579}
580
5a06a363 581static int testmsg(struct msg_msg *msg, long type, int mode)
1da177e4
LT
582{
583 switch(mode)
584 {
585 case SEARCH_ANY:
586 return 1;
587 case SEARCH_LESSEQUAL:
5a06a363 588 if (msg->m_type <=type)
1da177e4
LT
589 return 1;
590 break;
591 case SEARCH_EQUAL:
5a06a363 592 if (msg->m_type == type)
1da177e4
LT
593 return 1;
594 break;
595 case SEARCH_NOTEQUAL:
5a06a363 596 if (msg->m_type != type)
1da177e4
LT
597 return 1;
598 break;
599 }
600 return 0;
601}
602
5a06a363 603static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
1da177e4 604{
5a06a363 605 struct list_head *tmp;
1da177e4
LT
606
607 tmp = msq->q_receivers.next;
608 while (tmp != &msq->q_receivers) {
5a06a363
IM
609 struct msg_receiver *msr;
610
611 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4 612 tmp = tmp->next;
5a06a363
IM
613 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
614 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
615 msr->r_msgtype, msr->r_mode)) {
616
1da177e4 617 list_del(&msr->r_list);
5a06a363 618 if (msr->r_maxsize < msg->m_ts) {
1da177e4
LT
619 msr->r_msg = NULL;
620 wake_up_process(msr->r_tsk);
621 smp_mb();
622 msr->r_msg = ERR_PTR(-E2BIG);
623 } else {
624 msr->r_msg = NULL;
b488893a 625 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
1da177e4
LT
626 msq->q_rtime = get_seconds();
627 wake_up_process(msr->r_tsk);
628 smp_mb();
629 msr->r_msg = msg;
5a06a363 630
1da177e4
LT
631 return 1;
632 }
633 }
634 }
635 return 0;
636}
637
651971cb 638long do_msgsnd(int msqid, long mtype, void __user *mtext,
639 size_t msgsz, int msgflg)
1da177e4
LT
640{
641 struct msg_queue *msq;
642 struct msg_msg *msg;
1da177e4 643 int err;
1e786937
KK
644 struct ipc_namespace *ns;
645
646 ns = current->nsproxy->ipc_ns;
5a06a363 647
1e786937 648 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
1da177e4 649 return -EINVAL;
1da177e4
LT
650 if (mtype < 1)
651 return -EINVAL;
652
651971cb 653 msg = load_msg(mtext, msgsz);
5a06a363 654 if (IS_ERR(msg))
1da177e4
LT
655 return PTR_ERR(msg);
656
657 msg->m_type = mtype;
658 msg->m_ts = msgsz;
659
023a5355
ND
660 msq = msg_lock_check(ns, msqid);
661 if (IS_ERR(msq)) {
662 err = PTR_ERR(msq);
1da177e4 663 goto out_free;
023a5355 664 }
1da177e4
LT
665
666 for (;;) {
667 struct msg_sender s;
668
5a06a363 669 err = -EACCES;
b0e77598 670 if (ipcperms(ns, &msq->q_perm, S_IWUGO))
1da177e4
LT
671 goto out_unlock_free;
672
673 err = security_msg_queue_msgsnd(msq, msg, msgflg);
674 if (err)
675 goto out_unlock_free;
676
5a06a363 677 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
1da177e4
LT
678 1 + msq->q_qnum <= msq->q_qbytes) {
679 break;
680 }
681
682 /* queue full, wait: */
5a06a363
IM
683 if (msgflg & IPC_NOWAIT) {
684 err = -EAGAIN;
1da177e4
LT
685 goto out_unlock_free;
686 }
687 ss_add(msq, &s);
688 ipc_rcu_getref(msq);
689 msg_unlock(msq);
690 schedule();
691
692 ipc_lock_by_ptr(&msq->q_perm);
693 ipc_rcu_putref(msq);
694 if (msq->q_perm.deleted) {
695 err = -EIDRM;
696 goto out_unlock_free;
697 }
698 ss_del(&s);
5a06a363 699
1da177e4 700 if (signal_pending(current)) {
5a06a363 701 err = -ERESTARTNOHAND;
1da177e4
LT
702 goto out_unlock_free;
703 }
704 }
705
b488893a 706 msq->q_lspid = task_tgid_vnr(current);
1da177e4
LT
707 msq->q_stime = get_seconds();
708
5a06a363 709 if (!pipelined_send(msq, msg)) {
25985edc 710 /* no one is waiting for this message, enqueue it */
5a06a363 711 list_add_tail(&msg->m_list, &msq->q_messages);
1da177e4
LT
712 msq->q_cbytes += msgsz;
713 msq->q_qnum++;
3ac88a41
KK
714 atomic_add(msgsz, &ns->msg_bytes);
715 atomic_inc(&ns->msg_hdrs);
1da177e4 716 }
5a06a363 717
1da177e4
LT
718 err = 0;
719 msg = NULL;
720
721out_unlock_free:
722 msg_unlock(msq);
723out_free:
5a06a363 724 if (msg != NULL)
1da177e4
LT
725 free_msg(msg);
726 return err;
727}
728
e48fbb69
HC
729SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
730 int, msgflg)
651971cb 731{
732 long mtype;
733
734 if (get_user(mtype, &msgp->mtype))
735 return -EFAULT;
736 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
737}
738
5a06a363 739static inline int convert_mode(long *msgtyp, int msgflg)
1da177e4 740{
5a06a363 741 /*
1da177e4
LT
742 * find message of correct type.
743 * msgtyp = 0 => get first.
744 * msgtyp > 0 => get first message of matching type.
5a06a363 745 * msgtyp < 0 => get message with least type must be < abs(msgtype).
1da177e4 746 */
5a06a363 747 if (*msgtyp == 0)
1da177e4 748 return SEARCH_ANY;
5a06a363
IM
749 if (*msgtyp < 0) {
750 *msgtyp = -*msgtyp;
1da177e4
LT
751 return SEARCH_LESSEQUAL;
752 }
5a06a363 753 if (msgflg & MSG_EXCEPT)
1da177e4
LT
754 return SEARCH_NOTEQUAL;
755 return SEARCH_EQUAL;
756}
757
f9dd87f4
SK
758static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
759{
760 struct msgbuf __user *msgp = dest;
761 size_t msgsz;
762
763 if (put_user(msg->m_type, &msgp->mtype))
764 return -EFAULT;
765
766 msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
767 if (store_msg(msgp->mtext, msg, msgsz))
768 return -EFAULT;
769 return msgsz;
770}
771
4a674f34 772#ifdef CONFIG_CHECKPOINT_RESTORE
3fcfe786
SK
773/*
774 * This function creates new kernel message structure, large enough to store
775 * bufsz message bytes.
776 */
4a674f34
SK
777static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
778 int msgflg, long *msgtyp,
779 unsigned long *copy_number)
780{
781 struct msg_msg *copy;
782
783 *copy_number = *msgtyp;
784 *msgtyp = 0;
785 /*
786 * Create dummy message to copy real message to.
787 */
788 copy = load_msg(buf, bufsz);
789 if (!IS_ERR(copy))
790 copy->m_ts = bufsz;
791 return copy;
792}
793
85398aa8 794static inline void free_copy(struct msg_msg *copy)
4a674f34 795{
85398aa8 796 if (copy)
4a674f34
SK
797 free_msg(copy);
798}
799#else
b30efe27
SK
800static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
801 int msgflg, long *msgtyp,
802 unsigned long *copy_number)
803{
804 return ERR_PTR(-ENOSYS);
805}
806
85398aa8
SK
807static inline void free_copy(struct msg_msg *copy)
808{
809}
4a674f34
SK
810#endif
811
f9dd87f4
SK
812long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
813 int msgflg,
814 long (*msg_handler)(void __user *, struct msg_msg *, size_t))
1da177e4
LT
815{
816 struct msg_queue *msq;
817 struct msg_msg *msg;
818 int mode;
1e786937 819 struct ipc_namespace *ns;
85398aa8 820 struct msg_msg *copy = NULL;
b30efe27 821 unsigned long copy_number = 0;
1da177e4 822
88b9e456
PH
823 ns = current->nsproxy->ipc_ns;
824
f9dd87f4 825 if (msqid < 0 || (long) bufsz < 0)
1da177e4 826 return -EINVAL;
4a674f34 827 if (msgflg & MSG_COPY) {
88b9e456
PH
828 copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax),
829 msgflg, &msgtyp, &copy_number);
4a674f34
SK
830 if (IS_ERR(copy))
831 return PTR_ERR(copy);
832 }
5a06a363 833 mode = convert_mode(&msgtyp, msgflg);
1da177e4 834
023a5355 835 msq = msg_lock_check(ns, msqid);
4a674f34 836 if (IS_ERR(msq)) {
85398aa8 837 free_copy(copy);
023a5355 838 return PTR_ERR(msq);
4a674f34 839 }
1da177e4
LT
840
841 for (;;) {
842 struct msg_receiver msr_d;
5a06a363 843 struct list_head *tmp;
4a674f34 844 long msg_counter = 0;
1da177e4
LT
845
846 msg = ERR_PTR(-EACCES);
b0e77598 847 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
1da177e4
LT
848 goto out_unlock;
849
850 msg = ERR_PTR(-EAGAIN);
851 tmp = msq->q_messages.next;
852 while (tmp != &msq->q_messages) {
853 struct msg_msg *walk_msg;
5a06a363
IM
854
855 walk_msg = list_entry(tmp, struct msg_msg, m_list);
856 if (testmsg(walk_msg, msgtyp, mode) &&
857 !security_msg_queue_msgrcv(msq, walk_msg, current,
858 msgtyp, mode)) {
859
1da177e4 860 msg = walk_msg;
5a06a363
IM
861 if (mode == SEARCH_LESSEQUAL &&
862 walk_msg->m_type != 1) {
5a06a363 863 msgtyp = walk_msg->m_type - 1;
4a674f34 864 } else if (msgflg & MSG_COPY) {
852028af 865 if (copy_number == msg_counter)
4a674f34 866 break;
2dc958fa 867 msg = ERR_PTR(-EAGAIN);
9afdacda 868 } else
1da177e4 869 break;
4a674f34 870 msg_counter++;
1da177e4
LT
871 }
872 tmp = tmp->next;
873 }
5a06a363
IM
874 if (!IS_ERR(msg)) {
875 /*
876 * Found a suitable message.
877 * Unlink it from the queue.
878 */
f9dd87f4 879 if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
1da177e4
LT
880 msg = ERR_PTR(-E2BIG);
881 goto out_unlock;
882 }
3fcfe786
SK
883 /*
884 * If we are copying, then do not unlink message and do
885 * not update queue parameters.
886 */
852028af
PH
887 if (msgflg & MSG_COPY) {
888 msg = copy_msg(msg, copy);
4a674f34 889 goto out_unlock;
852028af 890 }
1da177e4
LT
891 list_del(&msg->m_list);
892 msq->q_qnum--;
893 msq->q_rtime = get_seconds();
b488893a 894 msq->q_lrpid = task_tgid_vnr(current);
1da177e4 895 msq->q_cbytes -= msg->m_ts;
3ac88a41
KK
896 atomic_sub(msg->m_ts, &ns->msg_bytes);
897 atomic_dec(&ns->msg_hdrs);
5a06a363 898 ss_wakeup(&msq->q_senders, 0);
1da177e4
LT
899 msg_unlock(msq);
900 break;
901 }
902 /* No message waiting. Wait for a message */
903 if (msgflg & IPC_NOWAIT) {
904 msg = ERR_PTR(-ENOMSG);
905 goto out_unlock;
906 }
5a06a363 907 list_add_tail(&msr_d.r_list, &msq->q_receivers);
1da177e4
LT
908 msr_d.r_tsk = current;
909 msr_d.r_msgtype = msgtyp;
910 msr_d.r_mode = mode;
5a06a363 911 if (msgflg & MSG_NOERROR)
1da177e4 912 msr_d.r_maxsize = INT_MAX;
5a06a363 913 else
f9dd87f4 914 msr_d.r_maxsize = bufsz;
1da177e4
LT
915 msr_d.r_msg = ERR_PTR(-EAGAIN);
916 current->state = TASK_INTERRUPTIBLE;
917 msg_unlock(msq);
918
919 schedule();
920
921 /* Lockless receive, part 1:
922 * Disable preemption. We don't hold a reference to the queue
923 * and getting a reference would defeat the idea of a lockless
924 * operation, thus the code relies on rcu to guarantee the
25985edc 925 * existence of msq:
1da177e4
LT
926 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
927 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
928 * rcu_read_lock() prevents preemption between reading r_msg
929 * and the spin_lock() inside ipc_lock_by_ptr().
930 */
931 rcu_read_lock();
932
933 /* Lockless receive, part 2:
934 * Wait until pipelined_send or expunge_all are outside of
935 * wake_up_process(). There is a race with exit(), see
936 * ipc/mqueue.c for the details.
937 */
5a06a363 938 msg = (struct msg_msg*)msr_d.r_msg;
1da177e4
LT
939 while (msg == NULL) {
940 cpu_relax();
5a06a363 941 msg = (struct msg_msg *)msr_d.r_msg;
1da177e4
LT
942 }
943
944 /* Lockless receive, part 3:
945 * If there is a message or an error then accept it without
946 * locking.
947 */
5a06a363 948 if (msg != ERR_PTR(-EAGAIN)) {
1da177e4
LT
949 rcu_read_unlock();
950 break;
951 }
952
953 /* Lockless receive, part 3:
954 * Acquire the queue spinlock.
955 */
956 ipc_lock_by_ptr(&msq->q_perm);
957 rcu_read_unlock();
958
959 /* Lockless receive, part 4:
960 * Repeat test after acquiring the spinlock.
961 */
962 msg = (struct msg_msg*)msr_d.r_msg;
5a06a363 963 if (msg != ERR_PTR(-EAGAIN))
1da177e4
LT
964 goto out_unlock;
965
966 list_del(&msr_d.r_list);
967 if (signal_pending(current)) {
968 msg = ERR_PTR(-ERESTARTNOHAND);
969out_unlock:
970 msg_unlock(msq);
971 break;
972 }
973 }
4a674f34 974 if (IS_ERR(msg)) {
85398aa8 975 free_copy(copy);
5a06a363 976 return PTR_ERR(msg);
4a674f34 977 }
1da177e4 978
f9dd87f4 979 bufsz = msg_handler(buf, msg, bufsz);
1da177e4 980 free_msg(msg);
5a06a363 981
f9dd87f4 982 return bufsz;
1da177e4
LT
983}
984
e48fbb69
HC
985SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
986 long, msgtyp, int, msgflg)
651971cb 987{
f9dd87f4 988 return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
651971cb 989}
990
1da177e4 991#ifdef CONFIG_PROC_FS
19b4946c 992static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1da177e4 993{
1efdb69b 994 struct user_namespace *user_ns = seq_user_ns(s);
19b4946c
MW
995 struct msg_queue *msq = it;
996
997 return seq_printf(s,
5a06a363
IM
998 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
999 msq->q_perm.key,
7ca7e564 1000 msq->q_perm.id,
5a06a363
IM
1001 msq->q_perm.mode,
1002 msq->q_cbytes,
1003 msq->q_qnum,
1004 msq->q_lspid,
1005 msq->q_lrpid,
1efdb69b
EB
1006 from_kuid_munged(user_ns, msq->q_perm.uid),
1007 from_kgid_munged(user_ns, msq->q_perm.gid),
1008 from_kuid_munged(user_ns, msq->q_perm.cuid),
1009 from_kgid_munged(user_ns, msq->q_perm.cgid),
5a06a363
IM
1010 msq->q_stime,
1011 msq->q_rtime,
1012 msq->q_ctime);
1da177e4
LT
1013}
1014#endif