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