vermilion.c: use ALIGN(), not __ALIGN_MASK()
[linux-2.6/openmoko-kernel/knife-kernel.git] / ipc / msg.c
blobec0c724054b95ce733eee1510d044df7d95bea20
1 /*
2 * linux/ipc/msg.c
3 * Copyright (C) 1992 Krishna Balasubramanian
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
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
25 #include <linux/capability.h>
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>
36 #include <linux/seq_file.h>
37 #include <linux/rwsem.h>
38 #include <linux/nsproxy.h>
40 #include <asm/current.h>
41 #include <asm/uaccess.h>
42 #include "util.h"
45 * one msg_receiver structure for each sleeping receiver:
47 struct msg_receiver {
48 struct list_head r_list;
49 struct task_struct *r_tsk;
51 int r_mode;
52 long r_msgtype;
53 long r_maxsize;
55 struct msg_msg *volatile r_msg;
58 /* one msg_sender for each sleeping sender */
59 struct msg_sender {
60 struct list_head list;
61 struct task_struct *tsk;
64 #define SEARCH_ANY 1
65 #define SEARCH_EQUAL 2
66 #define SEARCH_NOTEQUAL 3
67 #define SEARCH_LESSEQUAL 4
69 static struct ipc_ids init_msg_ids;
71 #define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
73 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
74 #define msg_buildid(id, seq) ipc_buildid(id, seq)
76 static void freeque(struct ipc_namespace *, struct msg_queue *);
77 static int newque(struct ipc_namespace *, struct ipc_params *);
78 #ifdef CONFIG_PROC_FS
79 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
80 #endif
82 static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
84 ns->ids[IPC_MSG_IDS] = ids;
85 ns->msg_ctlmax = MSGMAX;
86 ns->msg_ctlmnb = MSGMNB;
87 ns->msg_ctlmni = MSGMNI;
88 atomic_set(&ns->msg_bytes, 0);
89 atomic_set(&ns->msg_hdrs, 0);
90 ipc_init_ids(ids);
93 int msg_init_ns(struct ipc_namespace *ns)
95 struct ipc_ids *ids;
97 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
98 if (ids == NULL)
99 return -ENOMEM;
101 __msg_init_ns(ns, ids);
102 return 0;
105 void msg_exit_ns(struct ipc_namespace *ns)
107 struct msg_queue *msq;
108 struct kern_ipc_perm *perm;
109 int next_id;
110 int total, in_use;
112 down_write(&msg_ids(ns).rw_mutex);
114 in_use = msg_ids(ns).in_use;
116 for (total = 0, next_id = 0; total < in_use; next_id++) {
117 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
118 if (perm == NULL)
119 continue;
120 ipc_lock_by_ptr(perm);
121 msq = container_of(perm, struct msg_queue, q_perm);
122 freeque(ns, msq);
123 total++;
126 up_write(&msg_ids(ns).rw_mutex);
128 kfree(ns->ids[IPC_MSG_IDS]);
129 ns->ids[IPC_MSG_IDS] = NULL;
132 void __init msg_init(void)
134 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
135 ipc_init_proc_interface("sysvipc/msg",
136 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
137 IPC_MSG_IDS, sysvipc_msg_proc_show);
141 * This routine is called in the paths where the rw_mutex is held to protect
142 * access to the idr tree.
144 static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
145 int id)
147 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
149 if (IS_ERR(ipcp))
150 return (struct msg_queue *)ipcp;
152 return container_of(ipcp, struct msg_queue, q_perm);
156 * msg_lock_(check_) routines are called in the paths where the rw_mutex
157 * is not held.
159 static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
161 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
163 if (IS_ERR(ipcp))
164 return (struct msg_queue *)ipcp;
166 return container_of(ipcp, struct msg_queue, q_perm);
169 static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
170 int id)
172 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
174 if (IS_ERR(ipcp))
175 return (struct msg_queue *)ipcp;
177 return container_of(ipcp, struct msg_queue, q_perm);
180 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
182 ipc_rmid(&msg_ids(ns), &s->q_perm);
186 * newque - Create a new msg queue
187 * @ns: namespace
188 * @params: ptr to the structure that contains the key and msgflg
190 * Called with msg_ids.rw_mutex held (writer)
192 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
194 struct msg_queue *msq;
195 int id, retval;
196 key_t key = params->key;
197 int msgflg = params->flg;
199 msq = ipc_rcu_alloc(sizeof(*msq));
200 if (!msq)
201 return -ENOMEM;
203 msq->q_perm.mode = msgflg & S_IRWXUGO;
204 msq->q_perm.key = key;
206 msq->q_perm.security = NULL;
207 retval = security_msg_queue_alloc(msq);
208 if (retval) {
209 ipc_rcu_putref(msq);
210 return retval;
214 * ipc_addid() locks msq
216 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
217 if (id < 0) {
218 security_msg_queue_free(msq);
219 ipc_rcu_putref(msq);
220 return id;
223 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
224 msq->q_stime = msq->q_rtime = 0;
225 msq->q_ctime = get_seconds();
226 msq->q_cbytes = msq->q_qnum = 0;
227 msq->q_qbytes = ns->msg_ctlmnb;
228 msq->q_lspid = msq->q_lrpid = 0;
229 INIT_LIST_HEAD(&msq->q_messages);
230 INIT_LIST_HEAD(&msq->q_receivers);
231 INIT_LIST_HEAD(&msq->q_senders);
233 msg_unlock(msq);
235 return msq->q_perm.id;
238 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
240 mss->tsk = current;
241 current->state = TASK_INTERRUPTIBLE;
242 list_add_tail(&mss->list, &msq->q_senders);
245 static inline void ss_del(struct msg_sender *mss)
247 if (mss->list.next != NULL)
248 list_del(&mss->list);
251 static void ss_wakeup(struct list_head *h, int kill)
253 struct list_head *tmp;
255 tmp = h->next;
256 while (tmp != h) {
257 struct msg_sender *mss;
259 mss = list_entry(tmp, struct msg_sender, list);
260 tmp = tmp->next;
261 if (kill)
262 mss->list.next = NULL;
263 wake_up_process(mss->tsk);
267 static void expunge_all(struct msg_queue *msq, int res)
269 struct list_head *tmp;
271 tmp = msq->q_receivers.next;
272 while (tmp != &msq->q_receivers) {
273 struct msg_receiver *msr;
275 msr = list_entry(tmp, struct msg_receiver, r_list);
276 tmp = tmp->next;
277 msr->r_msg = NULL;
278 wake_up_process(msr->r_tsk);
279 smp_mb();
280 msr->r_msg = ERR_PTR(res);
285 * freeque() wakes up waiters on the sender and receiver waiting queue,
286 * removes the message queue from message queue ID IDR, and cleans up all the
287 * messages associated with this queue.
289 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
290 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
292 static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
294 struct list_head *tmp;
296 expunge_all(msq, -EIDRM);
297 ss_wakeup(&msq->q_senders, 1);
298 msg_rmid(ns, msq);
299 msg_unlock(msq);
301 tmp = msq->q_messages.next;
302 while (tmp != &msq->q_messages) {
303 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
305 tmp = tmp->next;
306 atomic_dec(&ns->msg_hdrs);
307 free_msg(msg);
309 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
310 security_msg_queue_free(msq);
311 ipc_rcu_putref(msq);
315 * Called with msg_ids.rw_mutex and ipcp locked.
317 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
319 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
321 return security_msg_queue_associate(msq, msgflg);
324 asmlinkage long sys_msgget(key_t key, int msgflg)
326 struct ipc_namespace *ns;
327 struct ipc_ops msg_ops;
328 struct ipc_params msg_params;
330 ns = current->nsproxy->ipc_ns;
332 msg_ops.getnew = newque;
333 msg_ops.associate = msg_security;
334 msg_ops.more_checks = NULL;
336 msg_params.key = key;
337 msg_params.flg = msgflg;
339 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
342 static inline unsigned long
343 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
345 switch(version) {
346 case IPC_64:
347 return copy_to_user(buf, in, sizeof(*in));
348 case IPC_OLD:
350 struct msqid_ds out;
352 memset(&out, 0, sizeof(out));
354 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
356 out.msg_stime = in->msg_stime;
357 out.msg_rtime = in->msg_rtime;
358 out.msg_ctime = in->msg_ctime;
360 if (in->msg_cbytes > USHRT_MAX)
361 out.msg_cbytes = USHRT_MAX;
362 else
363 out.msg_cbytes = in->msg_cbytes;
364 out.msg_lcbytes = in->msg_cbytes;
366 if (in->msg_qnum > USHRT_MAX)
367 out.msg_qnum = USHRT_MAX;
368 else
369 out.msg_qnum = in->msg_qnum;
371 if (in->msg_qbytes > USHRT_MAX)
372 out.msg_qbytes = USHRT_MAX;
373 else
374 out.msg_qbytes = in->msg_qbytes;
375 out.msg_lqbytes = in->msg_qbytes;
377 out.msg_lspid = in->msg_lspid;
378 out.msg_lrpid = in->msg_lrpid;
380 return copy_to_user(buf, &out, sizeof(out));
382 default:
383 return -EINVAL;
387 struct msq_setbuf {
388 unsigned long qbytes;
389 uid_t uid;
390 gid_t gid;
391 mode_t mode;
394 static inline unsigned long
395 copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
397 switch(version) {
398 case IPC_64:
400 struct msqid64_ds tbuf;
402 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
403 return -EFAULT;
405 out->qbytes = tbuf.msg_qbytes;
406 out->uid = tbuf.msg_perm.uid;
407 out->gid = tbuf.msg_perm.gid;
408 out->mode = tbuf.msg_perm.mode;
410 return 0;
412 case IPC_OLD:
414 struct msqid_ds tbuf_old;
416 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
417 return -EFAULT;
419 out->uid = tbuf_old.msg_perm.uid;
420 out->gid = tbuf_old.msg_perm.gid;
421 out->mode = tbuf_old.msg_perm.mode;
423 if (tbuf_old.msg_qbytes == 0)
424 out->qbytes = tbuf_old.msg_lqbytes;
425 else
426 out->qbytes = tbuf_old.msg_qbytes;
428 return 0;
430 default:
431 return -EINVAL;
435 asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
437 struct kern_ipc_perm *ipcp;
438 struct msq_setbuf uninitialized_var(setbuf);
439 struct msg_queue *msq;
440 int err, version;
441 struct ipc_namespace *ns;
443 if (msqid < 0 || cmd < 0)
444 return -EINVAL;
446 version = ipc_parse_version(&cmd);
447 ns = current->nsproxy->ipc_ns;
449 switch (cmd) {
450 case IPC_INFO:
451 case MSG_INFO:
453 struct msginfo msginfo;
454 int max_id;
456 if (!buf)
457 return -EFAULT;
459 * We must not return kernel stack data.
460 * due to padding, it's not enough
461 * to set all member fields.
463 err = security_msg_queue_msgctl(NULL, cmd);
464 if (err)
465 return err;
467 memset(&msginfo, 0, sizeof(msginfo));
468 msginfo.msgmni = ns->msg_ctlmni;
469 msginfo.msgmax = ns->msg_ctlmax;
470 msginfo.msgmnb = ns->msg_ctlmnb;
471 msginfo.msgssz = MSGSSZ;
472 msginfo.msgseg = MSGSEG;
473 down_read(&msg_ids(ns).rw_mutex);
474 if (cmd == MSG_INFO) {
475 msginfo.msgpool = msg_ids(ns).in_use;
476 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
477 msginfo.msgtql = atomic_read(&ns->msg_bytes);
478 } else {
479 msginfo.msgmap = MSGMAP;
480 msginfo.msgpool = MSGPOOL;
481 msginfo.msgtql = MSGTQL;
483 max_id = ipc_get_maxid(&msg_ids(ns));
484 up_read(&msg_ids(ns).rw_mutex);
485 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
486 return -EFAULT;
487 return (max_id < 0) ? 0 : max_id;
489 case MSG_STAT: /* msqid is an index rather than a msg queue id */
490 case IPC_STAT:
492 struct msqid64_ds tbuf;
493 int success_return;
495 if (!buf)
496 return -EFAULT;
498 if (cmd == MSG_STAT) {
499 msq = msg_lock(ns, msqid);
500 if (IS_ERR(msq))
501 return PTR_ERR(msq);
502 success_return = msq->q_perm.id;
503 } else {
504 msq = msg_lock_check(ns, msqid);
505 if (IS_ERR(msq))
506 return PTR_ERR(msq);
507 success_return = 0;
509 err = -EACCES;
510 if (ipcperms(&msq->q_perm, S_IRUGO))
511 goto out_unlock;
513 err = security_msg_queue_msgctl(msq, cmd);
514 if (err)
515 goto out_unlock;
517 memset(&tbuf, 0, sizeof(tbuf));
519 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
520 tbuf.msg_stime = msq->q_stime;
521 tbuf.msg_rtime = msq->q_rtime;
522 tbuf.msg_ctime = msq->q_ctime;
523 tbuf.msg_cbytes = msq->q_cbytes;
524 tbuf.msg_qnum = msq->q_qnum;
525 tbuf.msg_qbytes = msq->q_qbytes;
526 tbuf.msg_lspid = msq->q_lspid;
527 tbuf.msg_lrpid = msq->q_lrpid;
528 msg_unlock(msq);
529 if (copy_msqid_to_user(buf, &tbuf, version))
530 return -EFAULT;
531 return success_return;
533 case IPC_SET:
534 if (!buf)
535 return -EFAULT;
536 if (copy_msqid_from_user(&setbuf, buf, version))
537 return -EFAULT;
538 break;
539 case IPC_RMID:
540 break;
541 default:
542 return -EINVAL;
545 down_write(&msg_ids(ns).rw_mutex);
546 msq = msg_lock_check_down(ns, msqid);
547 if (IS_ERR(msq)) {
548 err = PTR_ERR(msq);
549 goto out_up;
552 ipcp = &msq->q_perm;
554 err = audit_ipc_obj(ipcp);
555 if (err)
556 goto out_unlock_up;
557 if (cmd == IPC_SET) {
558 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
559 setbuf.mode);
560 if (err)
561 goto out_unlock_up;
564 err = -EPERM;
565 if (current->euid != ipcp->cuid &&
566 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
567 /* We _could_ check for CAP_CHOWN above, but we don't */
568 goto out_unlock_up;
570 err = security_msg_queue_msgctl(msq, cmd);
571 if (err)
572 goto out_unlock_up;
574 switch (cmd) {
575 case IPC_SET:
577 err = -EPERM;
578 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
579 goto out_unlock_up;
581 msq->q_qbytes = setbuf.qbytes;
583 ipcp->uid = setbuf.uid;
584 ipcp->gid = setbuf.gid;
585 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
586 (S_IRWXUGO & setbuf.mode);
587 msq->q_ctime = get_seconds();
588 /* sleeping receivers might be excluded by
589 * stricter permissions.
591 expunge_all(msq, -EAGAIN);
592 /* sleeping senders might be able to send
593 * due to a larger queue size.
595 ss_wakeup(&msq->q_senders, 0);
596 msg_unlock(msq);
597 break;
599 case IPC_RMID:
600 freeque(ns, msq);
601 break;
603 err = 0;
604 out_up:
605 up_write(&msg_ids(ns).rw_mutex);
606 return err;
607 out_unlock_up:
608 msg_unlock(msq);
609 goto out_up;
610 out_unlock:
611 msg_unlock(msq);
612 return err;
615 static int testmsg(struct msg_msg *msg, long type, int mode)
617 switch(mode)
619 case SEARCH_ANY:
620 return 1;
621 case SEARCH_LESSEQUAL:
622 if (msg->m_type <=type)
623 return 1;
624 break;
625 case SEARCH_EQUAL:
626 if (msg->m_type == type)
627 return 1;
628 break;
629 case SEARCH_NOTEQUAL:
630 if (msg->m_type != type)
631 return 1;
632 break;
634 return 0;
637 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
639 struct list_head *tmp;
641 tmp = msq->q_receivers.next;
642 while (tmp != &msq->q_receivers) {
643 struct msg_receiver *msr;
645 msr = list_entry(tmp, struct msg_receiver, r_list);
646 tmp = tmp->next;
647 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
648 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
649 msr->r_msgtype, msr->r_mode)) {
651 list_del(&msr->r_list);
652 if (msr->r_maxsize < msg->m_ts) {
653 msr->r_msg = NULL;
654 wake_up_process(msr->r_tsk);
655 smp_mb();
656 msr->r_msg = ERR_PTR(-E2BIG);
657 } else {
658 msr->r_msg = NULL;
659 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
660 msq->q_rtime = get_seconds();
661 wake_up_process(msr->r_tsk);
662 smp_mb();
663 msr->r_msg = msg;
665 return 1;
669 return 0;
672 long do_msgsnd(int msqid, long mtype, void __user *mtext,
673 size_t msgsz, int msgflg)
675 struct msg_queue *msq;
676 struct msg_msg *msg;
677 int err;
678 struct ipc_namespace *ns;
680 ns = current->nsproxy->ipc_ns;
682 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
683 return -EINVAL;
684 if (mtype < 1)
685 return -EINVAL;
687 msg = load_msg(mtext, msgsz);
688 if (IS_ERR(msg))
689 return PTR_ERR(msg);
691 msg->m_type = mtype;
692 msg->m_ts = msgsz;
694 msq = msg_lock_check(ns, msqid);
695 if (IS_ERR(msq)) {
696 err = PTR_ERR(msq);
697 goto out_free;
700 for (;;) {
701 struct msg_sender s;
703 err = -EACCES;
704 if (ipcperms(&msq->q_perm, S_IWUGO))
705 goto out_unlock_free;
707 err = security_msg_queue_msgsnd(msq, msg, msgflg);
708 if (err)
709 goto out_unlock_free;
711 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
712 1 + msq->q_qnum <= msq->q_qbytes) {
713 break;
716 /* queue full, wait: */
717 if (msgflg & IPC_NOWAIT) {
718 err = -EAGAIN;
719 goto out_unlock_free;
721 ss_add(msq, &s);
722 ipc_rcu_getref(msq);
723 msg_unlock(msq);
724 schedule();
726 ipc_lock_by_ptr(&msq->q_perm);
727 ipc_rcu_putref(msq);
728 if (msq->q_perm.deleted) {
729 err = -EIDRM;
730 goto out_unlock_free;
732 ss_del(&s);
734 if (signal_pending(current)) {
735 err = -ERESTARTNOHAND;
736 goto out_unlock_free;
740 msq->q_lspid = task_tgid_vnr(current);
741 msq->q_stime = get_seconds();
743 if (!pipelined_send(msq, msg)) {
744 /* noone is waiting for this message, enqueue it */
745 list_add_tail(&msg->m_list, &msq->q_messages);
746 msq->q_cbytes += msgsz;
747 msq->q_qnum++;
748 atomic_add(msgsz, &ns->msg_bytes);
749 atomic_inc(&ns->msg_hdrs);
752 err = 0;
753 msg = NULL;
755 out_unlock_free:
756 msg_unlock(msq);
757 out_free:
758 if (msg != NULL)
759 free_msg(msg);
760 return err;
763 asmlinkage long
764 sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
766 long mtype;
768 if (get_user(mtype, &msgp->mtype))
769 return -EFAULT;
770 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
773 static inline int convert_mode(long *msgtyp, int msgflg)
776 * find message of correct type.
777 * msgtyp = 0 => get first.
778 * msgtyp > 0 => get first message of matching type.
779 * msgtyp < 0 => get message with least type must be < abs(msgtype).
781 if (*msgtyp == 0)
782 return SEARCH_ANY;
783 if (*msgtyp < 0) {
784 *msgtyp = -*msgtyp;
785 return SEARCH_LESSEQUAL;
787 if (msgflg & MSG_EXCEPT)
788 return SEARCH_NOTEQUAL;
789 return SEARCH_EQUAL;
792 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
793 size_t msgsz, long msgtyp, int msgflg)
795 struct msg_queue *msq;
796 struct msg_msg *msg;
797 int mode;
798 struct ipc_namespace *ns;
800 if (msqid < 0 || (long) msgsz < 0)
801 return -EINVAL;
802 mode = convert_mode(&msgtyp, msgflg);
803 ns = current->nsproxy->ipc_ns;
805 msq = msg_lock_check(ns, msqid);
806 if (IS_ERR(msq))
807 return PTR_ERR(msq);
809 for (;;) {
810 struct msg_receiver msr_d;
811 struct list_head *tmp;
813 msg = ERR_PTR(-EACCES);
814 if (ipcperms(&msq->q_perm, S_IRUGO))
815 goto out_unlock;
817 msg = ERR_PTR(-EAGAIN);
818 tmp = msq->q_messages.next;
819 while (tmp != &msq->q_messages) {
820 struct msg_msg *walk_msg;
822 walk_msg = list_entry(tmp, struct msg_msg, m_list);
823 if (testmsg(walk_msg, msgtyp, mode) &&
824 !security_msg_queue_msgrcv(msq, walk_msg, current,
825 msgtyp, mode)) {
827 msg = walk_msg;
828 if (mode == SEARCH_LESSEQUAL &&
829 walk_msg->m_type != 1) {
830 msg = walk_msg;
831 msgtyp = walk_msg->m_type - 1;
832 } else {
833 msg = walk_msg;
834 break;
837 tmp = tmp->next;
839 if (!IS_ERR(msg)) {
841 * Found a suitable message.
842 * Unlink it from the queue.
844 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
845 msg = ERR_PTR(-E2BIG);
846 goto out_unlock;
848 list_del(&msg->m_list);
849 msq->q_qnum--;
850 msq->q_rtime = get_seconds();
851 msq->q_lrpid = task_tgid_vnr(current);
852 msq->q_cbytes -= msg->m_ts;
853 atomic_sub(msg->m_ts, &ns->msg_bytes);
854 atomic_dec(&ns->msg_hdrs);
855 ss_wakeup(&msq->q_senders, 0);
856 msg_unlock(msq);
857 break;
859 /* No message waiting. Wait for a message */
860 if (msgflg & IPC_NOWAIT) {
861 msg = ERR_PTR(-ENOMSG);
862 goto out_unlock;
864 list_add_tail(&msr_d.r_list, &msq->q_receivers);
865 msr_d.r_tsk = current;
866 msr_d.r_msgtype = msgtyp;
867 msr_d.r_mode = mode;
868 if (msgflg & MSG_NOERROR)
869 msr_d.r_maxsize = INT_MAX;
870 else
871 msr_d.r_maxsize = msgsz;
872 msr_d.r_msg = ERR_PTR(-EAGAIN);
873 current->state = TASK_INTERRUPTIBLE;
874 msg_unlock(msq);
876 schedule();
878 /* Lockless receive, part 1:
879 * Disable preemption. We don't hold a reference to the queue
880 * and getting a reference would defeat the idea of a lockless
881 * operation, thus the code relies on rcu to guarantee the
882 * existance of msq:
883 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
884 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
885 * rcu_read_lock() prevents preemption between reading r_msg
886 * and the spin_lock() inside ipc_lock_by_ptr().
888 rcu_read_lock();
890 /* Lockless receive, part 2:
891 * Wait until pipelined_send or expunge_all are outside of
892 * wake_up_process(). There is a race with exit(), see
893 * ipc/mqueue.c for the details.
895 msg = (struct msg_msg*)msr_d.r_msg;
896 while (msg == NULL) {
897 cpu_relax();
898 msg = (struct msg_msg *)msr_d.r_msg;
901 /* Lockless receive, part 3:
902 * If there is a message or an error then accept it without
903 * locking.
905 if (msg != ERR_PTR(-EAGAIN)) {
906 rcu_read_unlock();
907 break;
910 /* Lockless receive, part 3:
911 * Acquire the queue spinlock.
913 ipc_lock_by_ptr(&msq->q_perm);
914 rcu_read_unlock();
916 /* Lockless receive, part 4:
917 * Repeat test after acquiring the spinlock.
919 msg = (struct msg_msg*)msr_d.r_msg;
920 if (msg != ERR_PTR(-EAGAIN))
921 goto out_unlock;
923 list_del(&msr_d.r_list);
924 if (signal_pending(current)) {
925 msg = ERR_PTR(-ERESTARTNOHAND);
926 out_unlock:
927 msg_unlock(msq);
928 break;
931 if (IS_ERR(msg))
932 return PTR_ERR(msg);
934 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
935 *pmtype = msg->m_type;
936 if (store_msg(mtext, msg, msgsz))
937 msgsz = -EFAULT;
939 free_msg(msg);
941 return msgsz;
944 asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
945 long msgtyp, int msgflg)
947 long err, mtype;
949 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
950 if (err < 0)
951 goto out;
953 if (put_user(mtype, &msgp->mtype))
954 err = -EFAULT;
955 out:
956 return err;
959 #ifdef CONFIG_PROC_FS
960 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
962 struct msg_queue *msq = it;
964 return seq_printf(s,
965 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
966 msq->q_perm.key,
967 msq->q_perm.id,
968 msq->q_perm.mode,
969 msq->q_cbytes,
970 msq->q_qnum,
971 msq->q_lspid,
972 msq->q_lrpid,
973 msq->q_perm.uid,
974 msq->q_perm.gid,
975 msq->q_perm.cuid,
976 msq->q_perm.cgid,
977 msq->q_stime,
978 msq->q_rtime,
979 msq->q_ctime);
981 #endif