[PATCH] mark f_ops const in the inode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / ipc / msg.c
blob48a7f17a7236397c219e03799117718fa4b2d3ec
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>
18 #include <linux/capability.h>
19 #include <linux/config.h>
20 #include <linux/slab.h>
21 #include <linux/msg.h>
22 #include <linux/spinlock.h>
23 #include <linux/init.h>
24 #include <linux/proc_fs.h>
25 #include <linux/list.h>
26 #include <linux/security.h>
27 #include <linux/sched.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/seq_file.h>
31 #include <linux/mutex.h>
33 #include <asm/current.h>
34 #include <asm/uaccess.h>
35 #include "util.h"
37 /* sysctl: */
38 int msg_ctlmax = MSGMAX;
39 int msg_ctlmnb = MSGMNB;
40 int msg_ctlmni = MSGMNI;
42 /* one msg_receiver structure for each sleeping receiver */
43 struct msg_receiver {
44 struct list_head r_list;
45 struct task_struct* r_tsk;
47 int r_mode;
48 long r_msgtype;
49 long r_maxsize;
51 struct msg_msg* volatile r_msg;
54 /* one msg_sender for each sleeping sender */
55 struct msg_sender {
56 struct list_head list;
57 struct task_struct* tsk;
60 #define SEARCH_ANY 1
61 #define SEARCH_EQUAL 2
62 #define SEARCH_NOTEQUAL 3
63 #define SEARCH_LESSEQUAL 4
65 static atomic_t msg_bytes = ATOMIC_INIT(0);
66 static atomic_t msg_hdrs = ATOMIC_INIT(0);
68 static struct ipc_ids msg_ids;
70 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
71 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
72 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
73 #define msg_checkid(msq, msgid) \
74 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
75 #define msg_buildid(id, seq) \
76 ipc_buildid(&msg_ids, id, seq)
78 static void freeque (struct msg_queue *msq, int id);
79 static int newque (key_t key, int msgflg);
80 #ifdef CONFIG_PROC_FS
81 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
82 #endif
84 void __init msg_init (void)
86 ipc_init_ids(&msg_ids,msg_ctlmni);
87 ipc_init_proc_interface("sysvipc/msg",
88 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
89 &msg_ids,
90 sysvipc_msg_proc_show);
93 static int newque (key_t key, int msgflg)
95 int id;
96 int retval;
97 struct msg_queue *msq;
99 msq = ipc_rcu_alloc(sizeof(*msq));
100 if (!msq)
101 return -ENOMEM;
103 msq->q_perm.mode = (msgflg & S_IRWXUGO);
104 msq->q_perm.key = key;
106 msq->q_perm.security = NULL;
107 retval = security_msg_queue_alloc(msq);
108 if (retval) {
109 ipc_rcu_putref(msq);
110 return retval;
113 id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
114 if(id == -1) {
115 security_msg_queue_free(msq);
116 ipc_rcu_putref(msq);
117 return -ENOSPC;
120 msq->q_id = msg_buildid(id,msq->q_perm.seq);
121 msq->q_stime = msq->q_rtime = 0;
122 msq->q_ctime = get_seconds();
123 msq->q_cbytes = msq->q_qnum = 0;
124 msq->q_qbytes = msg_ctlmnb;
125 msq->q_lspid = msq->q_lrpid = 0;
126 INIT_LIST_HEAD(&msq->q_messages);
127 INIT_LIST_HEAD(&msq->q_receivers);
128 INIT_LIST_HEAD(&msq->q_senders);
129 msg_unlock(msq);
131 return msq->q_id;
134 static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
136 mss->tsk=current;
137 current->state=TASK_INTERRUPTIBLE;
138 list_add_tail(&mss->list,&msq->q_senders);
141 static inline void ss_del(struct msg_sender* mss)
143 if(mss->list.next != NULL)
144 list_del(&mss->list);
147 static void ss_wakeup(struct list_head* h, int kill)
149 struct list_head *tmp;
151 tmp = h->next;
152 while (tmp != h) {
153 struct msg_sender* mss;
155 mss = list_entry(tmp,struct msg_sender,list);
156 tmp = tmp->next;
157 if(kill)
158 mss->list.next=NULL;
159 wake_up_process(mss->tsk);
163 static void expunge_all(struct msg_queue* msq, int res)
165 struct list_head *tmp;
167 tmp = msq->q_receivers.next;
168 while (tmp != &msq->q_receivers) {
169 struct msg_receiver* msr;
171 msr = list_entry(tmp,struct msg_receiver,r_list);
172 tmp = tmp->next;
173 msr->r_msg = NULL;
174 wake_up_process(msr->r_tsk);
175 smp_mb();
176 msr->r_msg = ERR_PTR(res);
180 * freeque() wakes up waiters on the sender and receiver waiting queue,
181 * removes the message queue from message queue ID
182 * array, and cleans up all the messages associated with this queue.
184 * msg_ids.mutex and the spinlock for this message queue is hold
185 * before freeque() is called. msg_ids.mutex remains locked on exit.
187 static void freeque (struct msg_queue *msq, int id)
189 struct list_head *tmp;
191 expunge_all(msq,-EIDRM);
192 ss_wakeup(&msq->q_senders,1);
193 msq = msg_rmid(id);
194 msg_unlock(msq);
196 tmp = msq->q_messages.next;
197 while(tmp != &msq->q_messages) {
198 struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
199 tmp = tmp->next;
200 atomic_dec(&msg_hdrs);
201 free_msg(msg);
203 atomic_sub(msq->q_cbytes, &msg_bytes);
204 security_msg_queue_free(msq);
205 ipc_rcu_putref(msq);
208 asmlinkage long sys_msgget (key_t key, int msgflg)
210 int id, ret = -EPERM;
211 struct msg_queue *msq;
213 mutex_lock(&msg_ids.mutex);
214 if (key == IPC_PRIVATE)
215 ret = newque(key, msgflg);
216 else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
217 if (!(msgflg & IPC_CREAT))
218 ret = -ENOENT;
219 else
220 ret = newque(key, msgflg);
221 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
222 ret = -EEXIST;
223 } else {
224 msq = msg_lock(id);
225 BUG_ON(msq==NULL);
226 if (ipcperms(&msq->q_perm, msgflg))
227 ret = -EACCES;
228 else {
229 int qid = msg_buildid(id, msq->q_perm.seq);
230 ret = security_msg_queue_associate(msq, msgflg);
231 if (!ret)
232 ret = qid;
234 msg_unlock(msq);
236 mutex_unlock(&msg_ids.mutex);
237 return ret;
240 static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
242 switch(version) {
243 case IPC_64:
244 return copy_to_user (buf, in, sizeof(*in));
245 case IPC_OLD:
247 struct msqid_ds out;
249 memset(&out,0,sizeof(out));
251 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
253 out.msg_stime = in->msg_stime;
254 out.msg_rtime = in->msg_rtime;
255 out.msg_ctime = in->msg_ctime;
257 if(in->msg_cbytes > USHRT_MAX)
258 out.msg_cbytes = USHRT_MAX;
259 else
260 out.msg_cbytes = in->msg_cbytes;
261 out.msg_lcbytes = in->msg_cbytes;
263 if(in->msg_qnum > USHRT_MAX)
264 out.msg_qnum = USHRT_MAX;
265 else
266 out.msg_qnum = in->msg_qnum;
268 if(in->msg_qbytes > USHRT_MAX)
269 out.msg_qbytes = USHRT_MAX;
270 else
271 out.msg_qbytes = in->msg_qbytes;
272 out.msg_lqbytes = in->msg_qbytes;
274 out.msg_lspid = in->msg_lspid;
275 out.msg_lrpid = in->msg_lrpid;
277 return copy_to_user (buf, &out, sizeof(out));
279 default:
280 return -EINVAL;
284 struct msq_setbuf {
285 unsigned long qbytes;
286 uid_t uid;
287 gid_t gid;
288 mode_t mode;
291 static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
293 switch(version) {
294 case IPC_64:
296 struct msqid64_ds tbuf;
298 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
299 return -EFAULT;
301 out->qbytes = tbuf.msg_qbytes;
302 out->uid = tbuf.msg_perm.uid;
303 out->gid = tbuf.msg_perm.gid;
304 out->mode = tbuf.msg_perm.mode;
306 return 0;
308 case IPC_OLD:
310 struct msqid_ds tbuf_old;
312 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
313 return -EFAULT;
315 out->uid = tbuf_old.msg_perm.uid;
316 out->gid = tbuf_old.msg_perm.gid;
317 out->mode = tbuf_old.msg_perm.mode;
319 if(tbuf_old.msg_qbytes == 0)
320 out->qbytes = tbuf_old.msg_lqbytes;
321 else
322 out->qbytes = tbuf_old.msg_qbytes;
324 return 0;
326 default:
327 return -EINVAL;
331 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
333 int err, version;
334 struct msg_queue *msq;
335 struct msq_setbuf setbuf;
336 struct kern_ipc_perm *ipcp;
338 if (msqid < 0 || cmd < 0)
339 return -EINVAL;
341 version = ipc_parse_version(&cmd);
343 switch (cmd) {
344 case IPC_INFO:
345 case MSG_INFO:
347 struct msginfo msginfo;
348 int max_id;
349 if (!buf)
350 return -EFAULT;
351 /* We must not return kernel stack data.
352 * due to padding, it's not enough
353 * to set all member fields.
356 err = security_msg_queue_msgctl(NULL, cmd);
357 if (err)
358 return err;
360 memset(&msginfo,0,sizeof(msginfo));
361 msginfo.msgmni = msg_ctlmni;
362 msginfo.msgmax = msg_ctlmax;
363 msginfo.msgmnb = msg_ctlmnb;
364 msginfo.msgssz = MSGSSZ;
365 msginfo.msgseg = MSGSEG;
366 mutex_lock(&msg_ids.mutex);
367 if (cmd == MSG_INFO) {
368 msginfo.msgpool = msg_ids.in_use;
369 msginfo.msgmap = atomic_read(&msg_hdrs);
370 msginfo.msgtql = atomic_read(&msg_bytes);
371 } else {
372 msginfo.msgmap = MSGMAP;
373 msginfo.msgpool = MSGPOOL;
374 msginfo.msgtql = MSGTQL;
376 max_id = msg_ids.max_id;
377 mutex_unlock(&msg_ids.mutex);
378 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
379 return -EFAULT;
380 return (max_id < 0) ? 0: max_id;
382 case MSG_STAT:
383 case IPC_STAT:
385 struct msqid64_ds tbuf;
386 int success_return;
387 if (!buf)
388 return -EFAULT;
389 if(cmd == MSG_STAT && msqid >= msg_ids.entries->size)
390 return -EINVAL;
392 memset(&tbuf,0,sizeof(tbuf));
394 msq = msg_lock(msqid);
395 if (msq == NULL)
396 return -EINVAL;
398 if(cmd == MSG_STAT) {
399 success_return = msg_buildid(msqid, msq->q_perm.seq);
400 } else {
401 err = -EIDRM;
402 if (msg_checkid(msq,msqid))
403 goto out_unlock;
404 success_return = 0;
406 err = -EACCES;
407 if (ipcperms (&msq->q_perm, S_IRUGO))
408 goto out_unlock;
410 err = security_msg_queue_msgctl(msq, cmd);
411 if (err)
412 goto out_unlock;
414 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
415 tbuf.msg_stime = msq->q_stime;
416 tbuf.msg_rtime = msq->q_rtime;
417 tbuf.msg_ctime = msq->q_ctime;
418 tbuf.msg_cbytes = msq->q_cbytes;
419 tbuf.msg_qnum = msq->q_qnum;
420 tbuf.msg_qbytes = msq->q_qbytes;
421 tbuf.msg_lspid = msq->q_lspid;
422 tbuf.msg_lrpid = msq->q_lrpid;
423 msg_unlock(msq);
424 if (copy_msqid_to_user(buf, &tbuf, version))
425 return -EFAULT;
426 return success_return;
428 case IPC_SET:
429 if (!buf)
430 return -EFAULT;
431 if (copy_msqid_from_user (&setbuf, buf, version))
432 return -EFAULT;
433 break;
434 case IPC_RMID:
435 break;
436 default:
437 return -EINVAL;
440 mutex_lock(&msg_ids.mutex);
441 msq = msg_lock(msqid);
442 err=-EINVAL;
443 if (msq == NULL)
444 goto out_up;
446 err = -EIDRM;
447 if (msg_checkid(msq,msqid))
448 goto out_unlock_up;
449 ipcp = &msq->q_perm;
450 err = -EPERM;
451 if (current->euid != ipcp->cuid &&
452 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
453 /* We _could_ check for CAP_CHOWN above, but we don't */
454 goto out_unlock_up;
456 err = security_msg_queue_msgctl(msq, cmd);
457 if (err)
458 goto out_unlock_up;
460 switch (cmd) {
461 case IPC_SET:
463 if ((err = audit_ipc_perms(setbuf.qbytes, setbuf.uid, setbuf.gid, setbuf.mode, ipcp)))
464 goto out_unlock_up;
466 err = -EPERM;
467 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
468 goto out_unlock_up;
470 msq->q_qbytes = setbuf.qbytes;
472 ipcp->uid = setbuf.uid;
473 ipcp->gid = setbuf.gid;
474 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
475 (S_IRWXUGO & setbuf.mode);
476 msq->q_ctime = get_seconds();
477 /* sleeping receivers might be excluded by
478 * stricter permissions.
480 expunge_all(msq,-EAGAIN);
481 /* sleeping senders might be able to send
482 * due to a larger queue size.
484 ss_wakeup(&msq->q_senders,0);
485 msg_unlock(msq);
486 break;
488 case IPC_RMID:
489 freeque (msq, msqid);
490 break;
492 err = 0;
493 out_up:
494 mutex_unlock(&msg_ids.mutex);
495 return err;
496 out_unlock_up:
497 msg_unlock(msq);
498 goto out_up;
499 out_unlock:
500 msg_unlock(msq);
501 return err;
504 static int testmsg(struct msg_msg* msg,long type,int mode)
506 switch(mode)
508 case SEARCH_ANY:
509 return 1;
510 case SEARCH_LESSEQUAL:
511 if(msg->m_type <=type)
512 return 1;
513 break;
514 case SEARCH_EQUAL:
515 if(msg->m_type == type)
516 return 1;
517 break;
518 case SEARCH_NOTEQUAL:
519 if(msg->m_type != type)
520 return 1;
521 break;
523 return 0;
526 static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
528 struct list_head* tmp;
530 tmp = msq->q_receivers.next;
531 while (tmp != &msq->q_receivers) {
532 struct msg_receiver* msr;
533 msr = list_entry(tmp,struct msg_receiver,r_list);
534 tmp = tmp->next;
535 if(testmsg(msg,msr->r_msgtype,msr->r_mode) &&
536 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
537 list_del(&msr->r_list);
538 if(msr->r_maxsize < msg->m_ts) {
539 msr->r_msg = NULL;
540 wake_up_process(msr->r_tsk);
541 smp_mb();
542 msr->r_msg = ERR_PTR(-E2BIG);
543 } else {
544 msr->r_msg = NULL;
545 msq->q_lrpid = msr->r_tsk->pid;
546 msq->q_rtime = get_seconds();
547 wake_up_process(msr->r_tsk);
548 smp_mb();
549 msr->r_msg = msg;
550 return 1;
554 return 0;
557 asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
559 struct msg_queue *msq;
560 struct msg_msg *msg;
561 long mtype;
562 int err;
564 if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
565 return -EINVAL;
566 if (get_user(mtype, &msgp->mtype))
567 return -EFAULT;
568 if (mtype < 1)
569 return -EINVAL;
571 msg = load_msg(msgp->mtext, msgsz);
572 if(IS_ERR(msg))
573 return PTR_ERR(msg);
575 msg->m_type = mtype;
576 msg->m_ts = msgsz;
578 msq = msg_lock(msqid);
579 err=-EINVAL;
580 if(msq==NULL)
581 goto out_free;
583 err= -EIDRM;
584 if (msg_checkid(msq,msqid))
585 goto out_unlock_free;
587 for (;;) {
588 struct msg_sender s;
590 err=-EACCES;
591 if (ipcperms(&msq->q_perm, S_IWUGO))
592 goto out_unlock_free;
594 err = security_msg_queue_msgsnd(msq, msg, msgflg);
595 if (err)
596 goto out_unlock_free;
598 if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
599 1 + msq->q_qnum <= msq->q_qbytes) {
600 break;
603 /* queue full, wait: */
604 if(msgflg&IPC_NOWAIT) {
605 err=-EAGAIN;
606 goto out_unlock_free;
608 ss_add(msq, &s);
609 ipc_rcu_getref(msq);
610 msg_unlock(msq);
611 schedule();
613 ipc_lock_by_ptr(&msq->q_perm);
614 ipc_rcu_putref(msq);
615 if (msq->q_perm.deleted) {
616 err = -EIDRM;
617 goto out_unlock_free;
619 ss_del(&s);
621 if (signal_pending(current)) {
622 err=-ERESTARTNOHAND;
623 goto out_unlock_free;
627 msq->q_lspid = current->tgid;
628 msq->q_stime = get_seconds();
630 if(!pipelined_send(msq,msg)) {
631 /* noone is waiting for this message, enqueue it */
632 list_add_tail(&msg->m_list,&msq->q_messages);
633 msq->q_cbytes += msgsz;
634 msq->q_qnum++;
635 atomic_add(msgsz,&msg_bytes);
636 atomic_inc(&msg_hdrs);
639 err = 0;
640 msg = NULL;
642 out_unlock_free:
643 msg_unlock(msq);
644 out_free:
645 if(msg!=NULL)
646 free_msg(msg);
647 return err;
650 static inline int convert_mode(long* msgtyp, int msgflg)
653 * find message of correct type.
654 * msgtyp = 0 => get first.
655 * msgtyp > 0 => get first message of matching type.
656 * msgtyp < 0 => get message with least type must be < abs(msgtype).
658 if(*msgtyp==0)
659 return SEARCH_ANY;
660 if(*msgtyp<0) {
661 *msgtyp=-(*msgtyp);
662 return SEARCH_LESSEQUAL;
664 if(msgflg & MSG_EXCEPT)
665 return SEARCH_NOTEQUAL;
666 return SEARCH_EQUAL;
669 asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
670 long msgtyp, int msgflg)
672 struct msg_queue *msq;
673 struct msg_msg *msg;
674 int mode;
676 if (msqid < 0 || (long) msgsz < 0)
677 return -EINVAL;
678 mode = convert_mode(&msgtyp,msgflg);
680 msq = msg_lock(msqid);
681 if(msq==NULL)
682 return -EINVAL;
684 msg = ERR_PTR(-EIDRM);
685 if (msg_checkid(msq,msqid))
686 goto out_unlock;
688 for (;;) {
689 struct msg_receiver msr_d;
690 struct list_head* tmp;
692 msg = ERR_PTR(-EACCES);
693 if (ipcperms (&msq->q_perm, S_IRUGO))
694 goto out_unlock;
696 msg = ERR_PTR(-EAGAIN);
697 tmp = msq->q_messages.next;
698 while (tmp != &msq->q_messages) {
699 struct msg_msg *walk_msg;
700 walk_msg = list_entry(tmp,struct msg_msg,m_list);
701 if(testmsg(walk_msg,msgtyp,mode) &&
702 !security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
703 msg = walk_msg;
704 if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
705 msg=walk_msg;
706 msgtyp=walk_msg->m_type-1;
707 } else {
708 msg=walk_msg;
709 break;
712 tmp = tmp->next;
714 if(!IS_ERR(msg)) {
715 /* Found a suitable message. Unlink it from the queue. */
716 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
717 msg = ERR_PTR(-E2BIG);
718 goto out_unlock;
720 list_del(&msg->m_list);
721 msq->q_qnum--;
722 msq->q_rtime = get_seconds();
723 msq->q_lrpid = current->tgid;
724 msq->q_cbytes -= msg->m_ts;
725 atomic_sub(msg->m_ts,&msg_bytes);
726 atomic_dec(&msg_hdrs);
727 ss_wakeup(&msq->q_senders,0);
728 msg_unlock(msq);
729 break;
731 /* No message waiting. Wait for a message */
732 if (msgflg & IPC_NOWAIT) {
733 msg = ERR_PTR(-ENOMSG);
734 goto out_unlock;
736 list_add_tail(&msr_d.r_list,&msq->q_receivers);
737 msr_d.r_tsk = current;
738 msr_d.r_msgtype = msgtyp;
739 msr_d.r_mode = mode;
740 if(msgflg & MSG_NOERROR)
741 msr_d.r_maxsize = INT_MAX;
742 else
743 msr_d.r_maxsize = msgsz;
744 msr_d.r_msg = ERR_PTR(-EAGAIN);
745 current->state = TASK_INTERRUPTIBLE;
746 msg_unlock(msq);
748 schedule();
750 /* Lockless receive, part 1:
751 * Disable preemption. We don't hold a reference to the queue
752 * and getting a reference would defeat the idea of a lockless
753 * operation, thus the code relies on rcu to guarantee the
754 * existance of msq:
755 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
756 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
757 * rcu_read_lock() prevents preemption between reading r_msg
758 * and the spin_lock() inside ipc_lock_by_ptr().
760 rcu_read_lock();
762 /* Lockless receive, part 2:
763 * Wait until pipelined_send or expunge_all are outside of
764 * wake_up_process(). There is a race with exit(), see
765 * ipc/mqueue.c for the details.
767 msg = (struct msg_msg*) msr_d.r_msg;
768 while (msg == NULL) {
769 cpu_relax();
770 msg = (struct msg_msg*) msr_d.r_msg;
773 /* Lockless receive, part 3:
774 * If there is a message or an error then accept it without
775 * locking.
777 if(msg != ERR_PTR(-EAGAIN)) {
778 rcu_read_unlock();
779 break;
782 /* Lockless receive, part 3:
783 * Acquire the queue spinlock.
785 ipc_lock_by_ptr(&msq->q_perm);
786 rcu_read_unlock();
788 /* Lockless receive, part 4:
789 * Repeat test after acquiring the spinlock.
791 msg = (struct msg_msg*)msr_d.r_msg;
792 if(msg != ERR_PTR(-EAGAIN))
793 goto out_unlock;
795 list_del(&msr_d.r_list);
796 if (signal_pending(current)) {
797 msg = ERR_PTR(-ERESTARTNOHAND);
798 out_unlock:
799 msg_unlock(msq);
800 break;
803 if (IS_ERR(msg))
804 return PTR_ERR(msg);
806 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
807 if (put_user (msg->m_type, &msgp->mtype) ||
808 store_msg(msgp->mtext, msg, msgsz)) {
809 msgsz = -EFAULT;
811 free_msg(msg);
812 return msgsz;
815 #ifdef CONFIG_PROC_FS
816 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
818 struct msg_queue *msq = it;
820 return seq_printf(s,
821 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
822 msq->q_perm.key,
823 msq->q_id,
824 msq->q_perm.mode,
825 msq->q_cbytes,
826 msq->q_qnum,
827 msq->q_lspid,
828 msq->q_lrpid,
829 msq->q_perm.uid,
830 msq->q_perm.gid,
831 msq->q_perm.cuid,
832 msq->q_perm.cgid,
833 msq->q_stime,
834 msq->q_rtime,
835 msq->q_ctime);
837 #endif