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