]> bbs.cooldavid.org Git - net-next-2.6.git/blame - ipc/msg.c
IPC: make struct ipc_ids static in ipc_namespace
[net-next-2.6.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/slab.h>
27#include <linux/msg.h>
28#include <linux/spinlock.h>
29#include <linux/init.h>
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)
1b531f21 73#define msg_buildid(id, seq) ipc_buildid(id, seq)
1e786937 74
7ca7e564 75static void freeque(struct ipc_namespace *, struct msg_queue *);
7748dbfa 76static int newque(struct ipc_namespace *, struct ipc_params *);
1da177e4 77#ifdef CONFIG_PROC_FS
19b4946c 78static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
1da177e4
LT
79#endif
80
ed2ddbf8 81void msg_init_ns(struct ipc_namespace *ns)
1e786937 82{
1e786937
KK
83 ns->msg_ctlmax = MSGMAX;
84 ns->msg_ctlmnb = MSGMNB;
85 ns->msg_ctlmni = MSGMNI;
3ac88a41
KK
86 atomic_set(&ns->msg_bytes, 0);
87 atomic_set(&ns->msg_hdrs, 0);
ed2ddbf8 88 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1e786937
KK
89}
90
ae5e1b22 91#ifdef CONFIG_IPC_NS
1e786937
KK
92void msg_exit_ns(struct ipc_namespace *ns)
93{
1e786937 94 struct msg_queue *msq;
b1ed88b4 95 struct kern_ipc_perm *perm;
7ca7e564
ND
96 int next_id;
97 int total, in_use;
1e786937 98
3e148c79 99 down_write(&msg_ids(ns).rw_mutex);
7ca7e564
ND
100
101 in_use = msg_ids(ns).in_use;
102
103 for (total = 0, next_id = 0; total < in_use; next_id++) {
b1ed88b4
PP
104 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
105 if (perm == NULL)
1e786937 106 continue;
b1ed88b4
PP
107 ipc_lock_by_ptr(perm);
108 msq = container_of(perm, struct msg_queue, q_perm);
7ca7e564
ND
109 freeque(ns, msq);
110 total++;
1e786937 111 }
3e148c79
ND
112
113 up_write(&msg_ids(ns).rw_mutex);
1e786937 114}
ae5e1b22 115#endif
1e786937 116
5a06a363 117void __init msg_init(void)
1da177e4 118{
ed2ddbf8 119 msg_init_ns(&init_ipc_ns);
19b4946c
MW
120 ipc_init_proc_interface("sysvipc/msg",
121 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
1e786937 122 IPC_MSG_IDS, sysvipc_msg_proc_show);
1da177e4
LT
123}
124
3e148c79
ND
125/*
126 * This routine is called in the paths where the rw_mutex is held to protect
127 * access to the idr tree.
128 */
129static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
130 int id)
131{
132 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
133
b1ed88b4
PP
134 if (IS_ERR(ipcp))
135 return (struct msg_queue *)ipcp;
136
3e148c79
ND
137 return container_of(ipcp, struct msg_queue, q_perm);
138}
139
140/*
141 * msg_lock_(check_) routines are called in the paths where the rw_mutex
142 * is not held.
143 */
023a5355
ND
144static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
145{
03f02c76
ND
146 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
147
b1ed88b4
PP
148 if (IS_ERR(ipcp))
149 return (struct msg_queue *)ipcp;
150
03f02c76 151 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
152}
153
154static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
155 int id)
156{
03f02c76
ND
157 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
158
b1ed88b4
PP
159 if (IS_ERR(ipcp))
160 return (struct msg_queue *)ipcp;
161
03f02c76 162 return container_of(ipcp, struct msg_queue, q_perm);
023a5355
ND
163}
164
7ca7e564
ND
165static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
166{
167 ipc_rmid(&msg_ids(ns), &s->q_perm);
168}
169
f4566f04
ND
170/**
171 * newque - Create a new msg queue
172 * @ns: namespace
173 * @params: ptr to the structure that contains the key and msgflg
174 *
3e148c79 175 * Called with msg_ids.rw_mutex held (writer)
f4566f04 176 */
7748dbfa 177static int newque(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4 178{
1da177e4 179 struct msg_queue *msq;
5a06a363 180 int id, retval;
7748dbfa
ND
181 key_t key = params->key;
182 int msgflg = params->flg;
1da177e4 183
5a06a363
IM
184 msq = ipc_rcu_alloc(sizeof(*msq));
185 if (!msq)
1da177e4
LT
186 return -ENOMEM;
187
5a06a363 188 msq->q_perm.mode = msgflg & S_IRWXUGO;
1da177e4
LT
189 msq->q_perm.key = key;
190
191 msq->q_perm.security = NULL;
192 retval = security_msg_queue_alloc(msq);
193 if (retval) {
194 ipc_rcu_putref(msq);
195 return retval;
196 }
197
7ca7e564
ND
198 /*
199 * ipc_addid() locks msq
200 */
1e786937 201 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
283bb7fa 202 if (id < 0) {
1da177e4
LT
203 security_msg_queue_free(msq);
204 ipc_rcu_putref(msq);
283bb7fa 205 return id;
1da177e4
LT
206 }
207
1b531f21 208 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
1da177e4
LT
209 msq->q_stime = msq->q_rtime = 0;
210 msq->q_ctime = get_seconds();
211 msq->q_cbytes = msq->q_qnum = 0;
1e786937 212 msq->q_qbytes = ns->msg_ctlmnb;
1da177e4
LT
213 msq->q_lspid = msq->q_lrpid = 0;
214 INIT_LIST_HEAD(&msq->q_messages);
215 INIT_LIST_HEAD(&msq->q_receivers);
216 INIT_LIST_HEAD(&msq->q_senders);
7ca7e564 217
1da177e4
LT
218 msg_unlock(msq);
219
7ca7e564 220 return msq->q_perm.id;
1da177e4
LT
221}
222
5a06a363 223static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
1da177e4 224{
5a06a363
IM
225 mss->tsk = current;
226 current->state = TASK_INTERRUPTIBLE;
227 list_add_tail(&mss->list, &msq->q_senders);
1da177e4
LT
228}
229
5a06a363 230static inline void ss_del(struct msg_sender *mss)
1da177e4 231{
5a06a363 232 if (mss->list.next != NULL)
1da177e4
LT
233 list_del(&mss->list);
234}
235
5a06a363 236static void ss_wakeup(struct list_head *h, int kill)
1da177e4
LT
237{
238 struct list_head *tmp;
239
240 tmp = h->next;
241 while (tmp != h) {
5a06a363
IM
242 struct msg_sender *mss;
243
244 mss = list_entry(tmp, struct msg_sender, list);
1da177e4 245 tmp = tmp->next;
5a06a363
IM
246 if (kill)
247 mss->list.next = NULL;
1da177e4
LT
248 wake_up_process(mss->tsk);
249 }
250}
251
5a06a363 252static void expunge_all(struct msg_queue *msq, int res)
1da177e4
LT
253{
254 struct list_head *tmp;
255
256 tmp = msq->q_receivers.next;
257 while (tmp != &msq->q_receivers) {
5a06a363
IM
258 struct msg_receiver *msr;
259
260 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4
LT
261 tmp = tmp->next;
262 msr->r_msg = NULL;
263 wake_up_process(msr->r_tsk);
264 smp_mb();
265 msr->r_msg = ERR_PTR(res);
266 }
267}
5a06a363
IM
268
269/*
270 * freeque() wakes up waiters on the sender and receiver waiting queue,
f4566f04
ND
271 * removes the message queue from message queue ID IDR, and cleans up all the
272 * messages associated with this queue.
1da177e4 273 *
3e148c79
ND
274 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
275 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
1da177e4 276 */
7ca7e564 277static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
1da177e4
LT
278{
279 struct list_head *tmp;
280
5a06a363
IM
281 expunge_all(msq, -EIDRM);
282 ss_wakeup(&msq->q_senders, 1);
7ca7e564 283 msg_rmid(ns, msq);
1da177e4 284 msg_unlock(msq);
5a06a363 285
1da177e4 286 tmp = msq->q_messages.next;
5a06a363
IM
287 while (tmp != &msq->q_messages) {
288 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
289
1da177e4 290 tmp = tmp->next;
3ac88a41 291 atomic_dec(&ns->msg_hdrs);
1da177e4
LT
292 free_msg(msg);
293 }
3ac88a41 294 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
1da177e4
LT
295 security_msg_queue_free(msq);
296 ipc_rcu_putref(msq);
297}
298
f4566f04 299/*
3e148c79 300 * Called with msg_ids.rw_mutex and ipcp locked.
f4566f04 301 */
03f02c76 302static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
7748dbfa 303{
03f02c76
ND
304 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
305
306 return security_msg_queue_associate(msq, msgflg);
7748dbfa
ND
307}
308
5a06a363 309asmlinkage long sys_msgget(key_t key, int msgflg)
1da177e4 310{
1e786937 311 struct ipc_namespace *ns;
7748dbfa
ND
312 struct ipc_ops msg_ops;
313 struct ipc_params msg_params;
1e786937
KK
314
315 ns = current->nsproxy->ipc_ns;
7ca7e564 316
7748dbfa
ND
317 msg_ops.getnew = newque;
318 msg_ops.associate = msg_security;
319 msg_ops.more_checks = NULL;
320
321 msg_params.key = key;
322 msg_params.flg = msgflg;
5a06a363 323
7748dbfa 324 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
1da177e4
LT
325}
326
5a06a363
IM
327static inline unsigned long
328copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
1da177e4
LT
329{
330 switch(version) {
331 case IPC_64:
5a06a363 332 return copy_to_user(buf, in, sizeof(*in));
1da177e4 333 case IPC_OLD:
5a06a363 334 {
1da177e4
LT
335 struct msqid_ds out;
336
5a06a363 337 memset(&out, 0, sizeof(out));
1da177e4
LT
338
339 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
340
341 out.msg_stime = in->msg_stime;
342 out.msg_rtime = in->msg_rtime;
343 out.msg_ctime = in->msg_ctime;
344
5a06a363 345 if (in->msg_cbytes > USHRT_MAX)
1da177e4
LT
346 out.msg_cbytes = USHRT_MAX;
347 else
348 out.msg_cbytes = in->msg_cbytes;
349 out.msg_lcbytes = in->msg_cbytes;
350
5a06a363 351 if (in->msg_qnum > USHRT_MAX)
1da177e4
LT
352 out.msg_qnum = USHRT_MAX;
353 else
354 out.msg_qnum = in->msg_qnum;
355
5a06a363 356 if (in->msg_qbytes > USHRT_MAX)
1da177e4
LT
357 out.msg_qbytes = USHRT_MAX;
358 else
359 out.msg_qbytes = in->msg_qbytes;
360 out.msg_lqbytes = in->msg_qbytes;
361
362 out.msg_lspid = in->msg_lspid;
363 out.msg_lrpid = in->msg_lrpid;
364
5a06a363
IM
365 return copy_to_user(buf, &out, sizeof(out));
366 }
1da177e4
LT
367 default:
368 return -EINVAL;
369 }
370}
371
372struct msq_setbuf {
373 unsigned long qbytes;
374 uid_t uid;
375 gid_t gid;
376 mode_t mode;
377};
378
5a06a363
IM
379static inline unsigned long
380copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
1da177e4
LT
381{
382 switch(version) {
383 case IPC_64:
5a06a363 384 {
1da177e4
LT
385 struct msqid64_ds tbuf;
386
5a06a363 387 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
1da177e4
LT
388 return -EFAULT;
389
390 out->qbytes = tbuf.msg_qbytes;
391 out->uid = tbuf.msg_perm.uid;
392 out->gid = tbuf.msg_perm.gid;
393 out->mode = tbuf.msg_perm.mode;
394
395 return 0;
5a06a363 396 }
1da177e4 397 case IPC_OLD:
5a06a363 398 {
1da177e4
LT
399 struct msqid_ds tbuf_old;
400
5a06a363 401 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4
LT
402 return -EFAULT;
403
404 out->uid = tbuf_old.msg_perm.uid;
405 out->gid = tbuf_old.msg_perm.gid;
406 out->mode = tbuf_old.msg_perm.mode;
407
5a06a363 408 if (tbuf_old.msg_qbytes == 0)
1da177e4
LT
409 out->qbytes = tbuf_old.msg_lqbytes;
410 else
411 out->qbytes = tbuf_old.msg_qbytes;
412
413 return 0;
5a06a363 414 }
1da177e4
LT
415 default:
416 return -EINVAL;
417 }
418}
419
5a06a363 420asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
1da177e4 421{
1da177e4 422 struct kern_ipc_perm *ipcp;
8e1c091c 423 struct msq_setbuf uninitialized_var(setbuf);
5a06a363
IM
424 struct msg_queue *msq;
425 int err, version;
1e786937 426 struct ipc_namespace *ns;
5a06a363 427
1da177e4
LT
428 if (msqid < 0 || cmd < 0)
429 return -EINVAL;
430
431 version = ipc_parse_version(&cmd);
1e786937 432 ns = current->nsproxy->ipc_ns;
1da177e4
LT
433
434 switch (cmd) {
5a06a363
IM
435 case IPC_INFO:
436 case MSG_INFO:
437 {
1da177e4
LT
438 struct msginfo msginfo;
439 int max_id;
5a06a363 440
1da177e4
LT
441 if (!buf)
442 return -EFAULT;
5a06a363
IM
443 /*
444 * We must not return kernel stack data.
1da177e4
LT
445 * due to padding, it's not enough
446 * to set all member fields.
447 */
1da177e4
LT
448 err = security_msg_queue_msgctl(NULL, cmd);
449 if (err)
450 return err;
451
5a06a363 452 memset(&msginfo, 0, sizeof(msginfo));
1e786937
KK
453 msginfo.msgmni = ns->msg_ctlmni;
454 msginfo.msgmax = ns->msg_ctlmax;
455 msginfo.msgmnb = ns->msg_ctlmnb;
1da177e4
LT
456 msginfo.msgssz = MSGSSZ;
457 msginfo.msgseg = MSGSEG;
3e148c79 458 down_read(&msg_ids(ns).rw_mutex);
1da177e4 459 if (cmd == MSG_INFO) {
1e786937 460 msginfo.msgpool = msg_ids(ns).in_use;
3ac88a41
KK
461 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
462 msginfo.msgtql = atomic_read(&ns->msg_bytes);
1da177e4
LT
463 } else {
464 msginfo.msgmap = MSGMAP;
465 msginfo.msgpool = MSGPOOL;
466 msginfo.msgtql = MSGTQL;
467 }
7ca7e564 468 max_id = ipc_get_maxid(&msg_ids(ns));
3e148c79 469 up_read(&msg_ids(ns).rw_mutex);
5a06a363 470 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
1da177e4 471 return -EFAULT;
5a06a363 472 return (max_id < 0) ? 0 : max_id;
1da177e4 473 }
7ca7e564 474 case MSG_STAT: /* msqid is an index rather than a msg queue id */
1da177e4
LT
475 case IPC_STAT:
476 {
477 struct msqid64_ds tbuf;
478 int success_return;
5a06a363 479
1da177e4
LT
480 if (!buf)
481 return -EFAULT;
1da177e4 482
5a06a363 483 if (cmd == MSG_STAT) {
023a5355
ND
484 msq = msg_lock(ns, msqid);
485 if (IS_ERR(msq))
486 return PTR_ERR(msq);
7ca7e564 487 success_return = msq->q_perm.id;
1da177e4 488 } else {
023a5355
ND
489 msq = msg_lock_check(ns, msqid);
490 if (IS_ERR(msq))
491 return PTR_ERR(msq);
1da177e4
LT
492 success_return = 0;
493 }
494 err = -EACCES;
5a06a363 495 if (ipcperms(&msq->q_perm, S_IRUGO))
1da177e4
LT
496 goto out_unlock;
497
498 err = security_msg_queue_msgctl(msq, cmd);
499 if (err)
500 goto out_unlock;
501
023a5355
ND
502 memset(&tbuf, 0, sizeof(tbuf));
503
1da177e4
LT
504 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
505 tbuf.msg_stime = msq->q_stime;
506 tbuf.msg_rtime = msq->q_rtime;
507 tbuf.msg_ctime = msq->q_ctime;
508 tbuf.msg_cbytes = msq->q_cbytes;
509 tbuf.msg_qnum = msq->q_qnum;
510 tbuf.msg_qbytes = msq->q_qbytes;
511 tbuf.msg_lspid = msq->q_lspid;
512 tbuf.msg_lrpid = msq->q_lrpid;
513 msg_unlock(msq);
514 if (copy_msqid_to_user(buf, &tbuf, version))
515 return -EFAULT;
516 return success_return;
517 }
518 case IPC_SET:
519 if (!buf)
520 return -EFAULT;
5a06a363 521 if (copy_msqid_from_user(&setbuf, buf, version))
1da177e4 522 return -EFAULT;
1da177e4
LT
523 break;
524 case IPC_RMID:
525 break;
526 default:
527 return -EINVAL;
528 }
529
3e148c79
ND
530 down_write(&msg_ids(ns).rw_mutex);
531 msq = msg_lock_check_down(ns, msqid);
023a5355
ND
532 if (IS_ERR(msq)) {
533 err = PTR_ERR(msq);
1da177e4 534 goto out_up;
023a5355 535 }
1da177e4 536
1da177e4 537 ipcp = &msq->q_perm;
073115d6
SG
538
539 err = audit_ipc_obj(ipcp);
540 if (err)
541 goto out_unlock_up;
8e1c091c 542 if (cmd == IPC_SET) {
5a06a363
IM
543 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
544 setbuf.mode);
ac03221a
LK
545 if (err)
546 goto out_unlock_up;
547 }
073115d6 548
1da177e4 549 err = -EPERM;
5a06a363 550 if (current->euid != ipcp->cuid &&
1da177e4 551 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
5a06a363 552 /* We _could_ check for CAP_CHOWN above, but we don't */
1da177e4
LT
553 goto out_unlock_up;
554
555 err = security_msg_queue_msgctl(msq, cmd);
556 if (err)
557 goto out_unlock_up;
558
559 switch (cmd) {
560 case IPC_SET:
561 {
562 err = -EPERM;
1e786937 563 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
1da177e4
LT
564 goto out_unlock_up;
565
566 msq->q_qbytes = setbuf.qbytes;
567
568 ipcp->uid = setbuf.uid;
569 ipcp->gid = setbuf.gid;
5a06a363
IM
570 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
571 (S_IRWXUGO & setbuf.mode);
1da177e4
LT
572 msq->q_ctime = get_seconds();
573 /* sleeping receivers might be excluded by
574 * stricter permissions.
575 */
5a06a363 576 expunge_all(msq, -EAGAIN);
1da177e4
LT
577 /* sleeping senders might be able to send
578 * due to a larger queue size.
579 */
5a06a363 580 ss_wakeup(&msq->q_senders, 0);
1da177e4
LT
581 msg_unlock(msq);
582 break;
583 }
584 case IPC_RMID:
7ca7e564 585 freeque(ns, msq);
1da177e4
LT
586 break;
587 }
588 err = 0;
589out_up:
3e148c79 590 up_write(&msg_ids(ns).rw_mutex);
1da177e4
LT
591 return err;
592out_unlock_up:
593 msg_unlock(msq);
594 goto out_up;
595out_unlock:
596 msg_unlock(msq);
597 return err;
598}
599
5a06a363 600static int testmsg(struct msg_msg *msg, long type, int mode)
1da177e4
LT
601{
602 switch(mode)
603 {
604 case SEARCH_ANY:
605 return 1;
606 case SEARCH_LESSEQUAL:
5a06a363 607 if (msg->m_type <=type)
1da177e4
LT
608 return 1;
609 break;
610 case SEARCH_EQUAL:
5a06a363 611 if (msg->m_type == type)
1da177e4
LT
612 return 1;
613 break;
614 case SEARCH_NOTEQUAL:
5a06a363 615 if (msg->m_type != type)
1da177e4
LT
616 return 1;
617 break;
618 }
619 return 0;
620}
621
5a06a363 622static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
1da177e4 623{
5a06a363 624 struct list_head *tmp;
1da177e4
LT
625
626 tmp = msq->q_receivers.next;
627 while (tmp != &msq->q_receivers) {
5a06a363
IM
628 struct msg_receiver *msr;
629
630 msr = list_entry(tmp, struct msg_receiver, r_list);
1da177e4 631 tmp = tmp->next;
5a06a363
IM
632 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
633 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
634 msr->r_msgtype, msr->r_mode)) {
635
1da177e4 636 list_del(&msr->r_list);
5a06a363 637 if (msr->r_maxsize < msg->m_ts) {
1da177e4
LT
638 msr->r_msg = NULL;
639 wake_up_process(msr->r_tsk);
640 smp_mb();
641 msr->r_msg = ERR_PTR(-E2BIG);
642 } else {
643 msr->r_msg = NULL;
b488893a 644 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
1da177e4
LT
645 msq->q_rtime = get_seconds();
646 wake_up_process(msr->r_tsk);
647 smp_mb();
648 msr->r_msg = msg;
5a06a363 649
1da177e4
LT
650 return 1;
651 }
652 }
653 }
654 return 0;
655}
656
651971cb 657long do_msgsnd(int msqid, long mtype, void __user *mtext,
658 size_t msgsz, int msgflg)
1da177e4
LT
659{
660 struct msg_queue *msq;
661 struct msg_msg *msg;
1da177e4 662 int err;
1e786937
KK
663 struct ipc_namespace *ns;
664
665 ns = current->nsproxy->ipc_ns;
5a06a363 666
1e786937 667 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
1da177e4 668 return -EINVAL;
1da177e4
LT
669 if (mtype < 1)
670 return -EINVAL;
671
651971cb 672 msg = load_msg(mtext, msgsz);
5a06a363 673 if (IS_ERR(msg))
1da177e4
LT
674 return PTR_ERR(msg);
675
676 msg->m_type = mtype;
677 msg->m_ts = msgsz;
678
023a5355
ND
679 msq = msg_lock_check(ns, msqid);
680 if (IS_ERR(msq)) {
681 err = PTR_ERR(msq);
1da177e4 682 goto out_free;
023a5355 683 }
1da177e4
LT
684
685 for (;;) {
686 struct msg_sender s;
687
5a06a363 688 err = -EACCES;
1da177e4
LT
689 if (ipcperms(&msq->q_perm, S_IWUGO))
690 goto out_unlock_free;
691
692 err = security_msg_queue_msgsnd(msq, msg, msgflg);
693 if (err)
694 goto out_unlock_free;
695
5a06a363 696 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
1da177e4
LT
697 1 + msq->q_qnum <= msq->q_qbytes) {
698 break;
699 }
700
701 /* queue full, wait: */
5a06a363
IM
702 if (msgflg & IPC_NOWAIT) {
703 err = -EAGAIN;
1da177e4
LT
704 goto out_unlock_free;
705 }
706 ss_add(msq, &s);
707 ipc_rcu_getref(msq);
708 msg_unlock(msq);
709 schedule();
710
711 ipc_lock_by_ptr(&msq->q_perm);
712 ipc_rcu_putref(msq);
713 if (msq->q_perm.deleted) {
714 err = -EIDRM;
715 goto out_unlock_free;
716 }
717 ss_del(&s);
5a06a363 718
1da177e4 719 if (signal_pending(current)) {
5a06a363 720 err = -ERESTARTNOHAND;
1da177e4
LT
721 goto out_unlock_free;
722 }
723 }
724
b488893a 725 msq->q_lspid = task_tgid_vnr(current);
1da177e4
LT
726 msq->q_stime = get_seconds();
727
5a06a363 728 if (!pipelined_send(msq, msg)) {
1da177e4 729 /* noone is waiting for this message, enqueue it */
5a06a363 730 list_add_tail(&msg->m_list, &msq->q_messages);
1da177e4
LT
731 msq->q_cbytes += msgsz;
732 msq->q_qnum++;
3ac88a41
KK
733 atomic_add(msgsz, &ns->msg_bytes);
734 atomic_inc(&ns->msg_hdrs);
1da177e4 735 }
5a06a363 736
1da177e4
LT
737 err = 0;
738 msg = NULL;
739
740out_unlock_free:
741 msg_unlock(msq);
742out_free:
5a06a363 743 if (msg != NULL)
1da177e4
LT
744 free_msg(msg);
745 return err;
746}
747
651971cb 748asmlinkage long
749sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
750{
751 long mtype;
752
753 if (get_user(mtype, &msgp->mtype))
754 return -EFAULT;
755 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
756}
757
5a06a363 758static inline int convert_mode(long *msgtyp, int msgflg)
1da177e4 759{
5a06a363 760 /*
1da177e4
LT
761 * find message of correct type.
762 * msgtyp = 0 => get first.
763 * msgtyp > 0 => get first message of matching type.
5a06a363 764 * msgtyp < 0 => get message with least type must be < abs(msgtype).
1da177e4 765 */
5a06a363 766 if (*msgtyp == 0)
1da177e4 767 return SEARCH_ANY;
5a06a363
IM
768 if (*msgtyp < 0) {
769 *msgtyp = -*msgtyp;
1da177e4
LT
770 return SEARCH_LESSEQUAL;
771 }
5a06a363 772 if (msgflg & MSG_EXCEPT)
1da177e4
LT
773 return SEARCH_NOTEQUAL;
774 return SEARCH_EQUAL;
775}
776
651971cb 777long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
778 size_t msgsz, long msgtyp, int msgflg)
1da177e4
LT
779{
780 struct msg_queue *msq;
781 struct msg_msg *msg;
782 int mode;
1e786937 783 struct ipc_namespace *ns;
1da177e4
LT
784
785 if (msqid < 0 || (long) msgsz < 0)
786 return -EINVAL;
5a06a363 787 mode = convert_mode(&msgtyp, msgflg);
1e786937 788 ns = current->nsproxy->ipc_ns;
1da177e4 789
023a5355
ND
790 msq = msg_lock_check(ns, msqid);
791 if (IS_ERR(msq))
792 return PTR_ERR(msq);
1da177e4
LT
793
794 for (;;) {
795 struct msg_receiver msr_d;
5a06a363 796 struct list_head *tmp;
1da177e4
LT
797
798 msg = ERR_PTR(-EACCES);
5a06a363 799 if (ipcperms(&msq->q_perm, S_IRUGO))
1da177e4
LT
800 goto out_unlock;
801
802 msg = ERR_PTR(-EAGAIN);
803 tmp = msq->q_messages.next;
804 while (tmp != &msq->q_messages) {
805 struct msg_msg *walk_msg;
5a06a363
IM
806
807 walk_msg = list_entry(tmp, struct msg_msg, m_list);
808 if (testmsg(walk_msg, msgtyp, mode) &&
809 !security_msg_queue_msgrcv(msq, walk_msg, current,
810 msgtyp, mode)) {
811
1da177e4 812 msg = walk_msg;
5a06a363
IM
813 if (mode == SEARCH_LESSEQUAL &&
814 walk_msg->m_type != 1) {
815 msg = walk_msg;
816 msgtyp = walk_msg->m_type - 1;
1da177e4 817 } else {
5a06a363 818 msg = walk_msg;
1da177e4
LT
819 break;
820 }
821 }
822 tmp = tmp->next;
823 }
5a06a363
IM
824 if (!IS_ERR(msg)) {
825 /*
826 * Found a suitable message.
827 * Unlink it from the queue.
828 */
1da177e4
LT
829 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
830 msg = ERR_PTR(-E2BIG);
831 goto out_unlock;
832 }
833 list_del(&msg->m_list);
834 msq->q_qnum--;
835 msq->q_rtime = get_seconds();
b488893a 836 msq->q_lrpid = task_tgid_vnr(current);
1da177e4 837 msq->q_cbytes -= msg->m_ts;
3ac88a41
KK
838 atomic_sub(msg->m_ts, &ns->msg_bytes);
839 atomic_dec(&ns->msg_hdrs);
5a06a363 840 ss_wakeup(&msq->q_senders, 0);
1da177e4
LT
841 msg_unlock(msq);
842 break;
843 }
844 /* No message waiting. Wait for a message */
845 if (msgflg & IPC_NOWAIT) {
846 msg = ERR_PTR(-ENOMSG);
847 goto out_unlock;
848 }
5a06a363 849 list_add_tail(&msr_d.r_list, &msq->q_receivers);
1da177e4
LT
850 msr_d.r_tsk = current;
851 msr_d.r_msgtype = msgtyp;
852 msr_d.r_mode = mode;
5a06a363 853 if (msgflg & MSG_NOERROR)
1da177e4 854 msr_d.r_maxsize = INT_MAX;
5a06a363 855 else
1da177e4
LT
856 msr_d.r_maxsize = msgsz;
857 msr_d.r_msg = ERR_PTR(-EAGAIN);
858 current->state = TASK_INTERRUPTIBLE;
859 msg_unlock(msq);
860
861 schedule();
862
863 /* Lockless receive, part 1:
864 * Disable preemption. We don't hold a reference to the queue
865 * and getting a reference would defeat the idea of a lockless
866 * operation, thus the code relies on rcu to guarantee the
867 * existance of msq:
868 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
869 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
870 * rcu_read_lock() prevents preemption between reading r_msg
871 * and the spin_lock() inside ipc_lock_by_ptr().
872 */
873 rcu_read_lock();
874
875 /* Lockless receive, part 2:
876 * Wait until pipelined_send or expunge_all are outside of
877 * wake_up_process(). There is a race with exit(), see
878 * ipc/mqueue.c for the details.
879 */
5a06a363 880 msg = (struct msg_msg*)msr_d.r_msg;
1da177e4
LT
881 while (msg == NULL) {
882 cpu_relax();
5a06a363 883 msg = (struct msg_msg *)msr_d.r_msg;
1da177e4
LT
884 }
885
886 /* Lockless receive, part 3:
887 * If there is a message or an error then accept it without
888 * locking.
889 */
5a06a363 890 if (msg != ERR_PTR(-EAGAIN)) {
1da177e4
LT
891 rcu_read_unlock();
892 break;
893 }
894
895 /* Lockless receive, part 3:
896 * Acquire the queue spinlock.
897 */
898 ipc_lock_by_ptr(&msq->q_perm);
899 rcu_read_unlock();
900
901 /* Lockless receive, part 4:
902 * Repeat test after acquiring the spinlock.
903 */
904 msg = (struct msg_msg*)msr_d.r_msg;
5a06a363 905 if (msg != ERR_PTR(-EAGAIN))
1da177e4
LT
906 goto out_unlock;
907
908 list_del(&msr_d.r_list);
909 if (signal_pending(current)) {
910 msg = ERR_PTR(-ERESTARTNOHAND);
911out_unlock:
912 msg_unlock(msq);
913 break;
914 }
915 }
916 if (IS_ERR(msg))
5a06a363 917 return PTR_ERR(msg);
1da177e4
LT
918
919 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
651971cb 920 *pmtype = msg->m_type;
921 if (store_msg(mtext, msg, msgsz))
5a06a363 922 msgsz = -EFAULT;
651971cb 923
1da177e4 924 free_msg(msg);
5a06a363 925
1da177e4
LT
926 return msgsz;
927}
928
651971cb 929asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
930 long msgtyp, int msgflg)
931{
932 long err, mtype;
933
934 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
935 if (err < 0)
936 goto out;
937
938 if (put_user(mtype, &msgp->mtype))
939 err = -EFAULT;
940out:
941 return err;
942}
943
1da177e4 944#ifdef CONFIG_PROC_FS
19b4946c 945static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1da177e4 946{
19b4946c
MW
947 struct msg_queue *msq = it;
948
949 return seq_printf(s,
5a06a363
IM
950 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
951 msq->q_perm.key,
7ca7e564 952 msq->q_perm.id,
5a06a363
IM
953 msq->q_perm.mode,
954 msq->q_cbytes,
955 msq->q_qnum,
956 msq->q_lspid,
957 msq->q_lrpid,
958 msq->q_perm.uid,
959 msq->q_perm.gid,
960 msq->q_perm.cuid,
961 msq->q_perm.cgid,
962 msq->q_stime,
963 msq->q_rtime,
964 msq->q_ctime);
1da177e4
LT
965}
966#endif