Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / ipc / msg.c
blobde2c406b975d41594e23affc95fd1a34fcc9e278
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 <manfreds@colorfullife.com>
18 #include <linux/config.h>
19 #include <linux/malloc.h>
20 #include <linux/msg.h>
21 #include <linux/spinlock.h>
22 #include <linux/init.h>
23 #include <linux/proc_fs.h>
24 #include <linux/list.h>
25 #include <asm/uaccess.h>
26 #include "util.h"
28 /* sysctl: */
29 int msg_ctlmax = MSGMAX;
30 int msg_ctlmnb = MSGMNB;
31 int msg_ctlmni = MSGMNI;
33 /* one msg_receiver structure for each sleeping receiver */
34 struct msg_receiver {
35 struct list_head r_list;
36 struct task_struct* r_tsk;
38 int r_mode;
39 long r_msgtype;
40 long r_maxsize;
42 struct msg_msg* volatile r_msg;
45 /* one msg_sender for each sleeping sender */
46 struct msg_sender {
47 struct list_head list;
48 struct task_struct* tsk;
51 struct msg_msgseg {
52 struct msg_msgseg* next;
53 /* the next part of the message follows immediately */
55 /* one msg_msg structure for each message */
56 struct msg_msg {
57 struct list_head m_list;
58 long m_type;
59 int m_ts; /* message text size */
60 struct msg_msgseg* next;
61 /* the actual message follows immediately */
64 #define DATALEN_MSG (PAGE_SIZE-sizeof(struct msg_msg))
65 #define DATALEN_SEG (PAGE_SIZE-sizeof(struct msg_msgseg))
67 /* one msq_queue structure for each present queue on the system */
68 struct msg_queue {
69 struct kern_ipc_perm q_perm;
70 time_t q_stime; /* last msgsnd time */
71 time_t q_rtime; /* last msgrcv time */
72 time_t q_ctime; /* last change time */
73 unsigned long q_cbytes; /* current number of bytes on queue */
74 unsigned long q_qnum; /* number of messages in queue */
75 unsigned long q_qbytes; /* max number of bytes on queue */
76 pid_t q_lspid; /* pid of last msgsnd */
77 pid_t q_lrpid; /* last receive pid */
79 struct list_head q_messages;
80 struct list_head q_receivers;
81 struct list_head q_senders;
84 #define SEARCH_ANY 1
85 #define SEARCH_EQUAL 2
86 #define SEARCH_NOTEQUAL 3
87 #define SEARCH_LESSEQUAL 4
89 static atomic_t msg_bytes = ATOMIC_INIT(0);
90 static atomic_t msg_hdrs = ATOMIC_INIT(0);
92 static struct ipc_ids msg_ids;
94 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
95 #define msg_unlock(id) ipc_unlock(&msg_ids,id)
96 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
97 #define msg_checkid(msq, msgid) \
98 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
99 #define msg_buildid(id, seq) \
100 ipc_buildid(&msg_ids, id, seq)
102 static void freeque (int id);
103 static int newque (key_t key, int msgflg);
104 #ifdef CONFIG_PROC_FS
105 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
106 #endif
108 void __init msg_init (void)
110 ipc_init_ids(&msg_ids,msg_ctlmni);
112 #ifdef CONFIG_PROC_FS
113 create_proc_read_entry("sysvipc/msg", 0, 0, sysvipc_msg_read_proc, NULL);
114 #endif
117 static int newque (key_t key, int msgflg)
119 int id;
120 struct msg_queue *msq;
122 msq = (struct msg_queue *) kmalloc (sizeof (*msq), GFP_KERNEL);
123 if (!msq)
124 return -ENOMEM;
125 id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
126 if(id == -1) {
127 kfree(msq);
128 return -ENOSPC;
130 msq->q_perm.mode = (msgflg & S_IRWXUGO);
131 msq->q_perm.key = key;
133 msq->q_stime = msq->q_rtime = 0;
134 msq->q_ctime = CURRENT_TIME;
135 msq->q_cbytes = msq->q_qnum = 0;
136 msq->q_qbytes = msg_ctlmnb;
137 msq->q_lspid = msq->q_lrpid = 0;
138 INIT_LIST_HEAD(&msq->q_messages);
139 INIT_LIST_HEAD(&msq->q_receivers);
140 INIT_LIST_HEAD(&msq->q_senders);
141 msg_unlock(id);
143 return msg_buildid(id,msq->q_perm.seq);
146 static void free_msg(struct msg_msg* msg)
148 struct msg_msgseg* seg;
149 seg = msg->next;
150 kfree(msg);
151 while(seg != NULL) {
152 struct msg_msgseg* tmp = seg->next;
153 kfree(seg);
154 seg = tmp;
158 static struct msg_msg* load_msg(void* src, int len)
160 struct msg_msg* msg;
161 struct msg_msgseg** pseg;
162 int err;
163 int alen;
165 alen = len;
166 if(alen > DATALEN_MSG)
167 alen = DATALEN_MSG;
169 msg = (struct msg_msg *) kmalloc (sizeof(*msg) + alen, GFP_KERNEL);
170 if(msg==NULL)
171 return ERR_PTR(-ENOMEM);
173 msg->next = NULL;
175 if (copy_from_user(msg+1, src, alen)) {
176 err = -EFAULT;
177 goto out_err;
180 len -= alen;
181 src = ((char*)src)+alen;
182 pseg = &msg->next;
183 while(len > 0) {
184 struct msg_msgseg* seg;
185 alen = len;
186 if(alen > DATALEN_SEG)
187 alen = DATALEN_SEG;
188 seg = (struct msg_msgseg *) kmalloc (sizeof(*seg) + alen, GFP_KERNEL);
189 if(seg==NULL) {
190 err=-ENOMEM;
191 goto out_err;
193 *pseg = seg;
194 seg->next = NULL;
195 if(copy_from_user (seg+1, src, alen)) {
196 err = -EFAULT;
197 goto out_err;
199 pseg = &seg->next;
200 len -= alen;
201 src = ((char*)src)+alen;
203 return msg;
205 out_err:
206 free_msg(msg);
207 return ERR_PTR(err);
210 static int store_msg(void* dest, struct msg_msg* msg, int len)
212 int alen;
213 struct msg_msgseg *seg;
215 alen = len;
216 if(alen > DATALEN_MSG)
217 alen = DATALEN_MSG;
218 if(copy_to_user (dest, msg+1, alen))
219 return -1;
221 len -= alen;
222 dest = ((char*)dest)+alen;
223 seg = msg->next;
224 while(len > 0) {
225 alen = len;
226 if(alen > DATALEN_SEG)
227 alen = DATALEN_SEG;
228 if(copy_to_user (dest, seg+1, alen))
229 return -1;
230 len -= alen;
231 dest = ((char*)dest)+alen;
232 seg=seg->next;
234 return 0;
237 static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
239 mss->tsk=current;
240 current->state=TASK_INTERRUPTIBLE;
241 list_add_tail(&mss->list,&msq->q_senders);
244 static inline void ss_del(struct msg_sender* mss)
246 if(mss->list.next != NULL)
247 list_del(&mss->list);
250 static void ss_wakeup(struct list_head* h, int kill)
252 struct list_head *tmp;
254 tmp = h->next;
255 while (tmp != h) {
256 struct msg_sender* mss;
258 mss = list_entry(tmp,struct msg_sender,list);
259 tmp = tmp->next;
260 if(kill)
261 mss->list.next=NULL;
262 wake_up_process(mss->tsk);
266 static void expunge_all(struct msg_queue* msq, int res)
268 struct list_head *tmp;
270 tmp = msq->q_receivers.next;
271 while (tmp != &msq->q_receivers) {
272 struct msg_receiver* msr;
274 msr = list_entry(tmp,struct msg_receiver,r_list);
275 tmp = tmp->next;
276 msr->r_msg = ERR_PTR(res);
277 wake_up_process(msr->r_tsk);
281 static void freeque (int id)
283 struct msg_queue *msq;
284 struct list_head *tmp;
286 msq = msg_rmid(id);
288 expunge_all(msq,-EIDRM);
289 ss_wakeup(&msq->q_senders,1);
290 msg_unlock(id);
292 tmp = msq->q_messages.next;
293 while(tmp != &msq->q_messages) {
294 struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
295 tmp = tmp->next;
296 atomic_dec(&msg_hdrs);
297 free_msg(msg);
299 atomic_sub(msq->q_cbytes, &msg_bytes);
300 kfree(msq);
303 asmlinkage long sys_msgget (key_t key, int msgflg)
305 int id, ret = -EPERM;
306 struct msg_queue *msq;
308 down(&msg_ids.sem);
309 if (key == IPC_PRIVATE)
310 ret = newque(key, msgflg);
311 else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
312 if (!(msgflg & IPC_CREAT))
313 ret = -ENOENT;
314 else
315 ret = newque(key, msgflg);
316 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
317 ret = -EEXIST;
318 } else {
319 msq = msg_lock(id);
320 if(msq==NULL)
321 BUG();
322 if (ipcperms(&msq->q_perm, msgflg))
323 ret = -EACCES;
324 else
325 ret = msg_buildid(id, msq->q_perm.seq);
326 msg_unlock(id);
328 up(&msg_ids.sem);
329 return ret;
332 static inline unsigned long copy_msqid_to_user(void *buf, struct msqid64_ds *in, int version)
334 switch(version) {
335 case IPC_64:
336 return copy_to_user (buf, in, sizeof(*in));
337 case IPC_OLD:
339 struct msqid_ds out;
341 memset(&out,0,sizeof(out));
343 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
345 out.msg_stime = in->msg_stime;
346 out.msg_rtime = in->msg_rtime;
347 out.msg_ctime = in->msg_ctime;
349 if(in->msg_cbytes > USHRT_MAX)
350 out.msg_cbytes = USHRT_MAX;
351 else
352 out.msg_cbytes = in->msg_cbytes;
353 out.msg_lcbytes = in->msg_cbytes;
355 if(in->msg_qnum > USHRT_MAX)
356 out.msg_qnum = USHRT_MAX;
357 else
358 out.msg_qnum = in->msg_qnum;
360 if(in->msg_qbytes > USHRT_MAX)
361 out.msg_qbytes = USHRT_MAX;
362 else
363 out.msg_qbytes = in->msg_qbytes;
364 out.msg_lqbytes = in->msg_qbytes;
366 out.msg_lspid = in->msg_lspid;
367 out.msg_lrpid = in->msg_lrpid;
369 return copy_to_user (buf, &out, sizeof(out));
371 default:
372 return -EINVAL;
376 struct msq_setbuf {
377 unsigned long qbytes;
378 uid_t uid;
379 gid_t gid;
380 mode_t mode;
383 static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void *buf, int version)
385 switch(version) {
386 case IPC_64:
388 struct msqid64_ds tbuf;
390 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
391 return -EFAULT;
393 out->qbytes = tbuf.msg_qbytes;
394 out->uid = tbuf.msg_perm.uid;
395 out->gid = tbuf.msg_perm.gid;
396 out->mode = tbuf.msg_perm.mode;
398 return 0;
400 case IPC_OLD:
402 struct msqid_ds tbuf_old;
404 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
405 return -EFAULT;
407 out->uid = tbuf_old.msg_perm.uid;
408 out->gid = tbuf_old.msg_perm.gid;
409 out->mode = tbuf_old.msg_perm.mode;
411 if(tbuf_old.msg_qbytes == 0)
412 out->qbytes = tbuf_old.msg_lqbytes;
413 else
414 out->qbytes = tbuf_old.msg_qbytes;
416 return 0;
418 default:
419 return -EINVAL;
423 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
425 int err, version;
426 struct msg_queue *msq;
427 struct msq_setbuf setbuf;
428 struct kern_ipc_perm *ipcp;
430 if (msqid < 0 || cmd < 0)
431 return -EINVAL;
433 version = ipc_parse_version(&cmd);
435 switch (cmd) {
436 case IPC_INFO:
437 case MSG_INFO:
439 struct msginfo msginfo;
440 int max_id;
441 if (!buf)
442 return -EFAULT;
443 /* We must not return kernel stack data.
444 * due to padding, it's not enough
445 * to set all member fields.
447 memset(&msginfo,0,sizeof(msginfo));
448 msginfo.msgmni = msg_ctlmni;
449 msginfo.msgmax = msg_ctlmax;
450 msginfo.msgmnb = msg_ctlmnb;
451 msginfo.msgssz = MSGSSZ;
452 msginfo.msgseg = MSGSEG;
453 down(&msg_ids.sem);
454 if (cmd == MSG_INFO) {
455 msginfo.msgpool = msg_ids.in_use;
456 msginfo.msgmap = atomic_read(&msg_hdrs);
457 msginfo.msgtql = atomic_read(&msg_bytes);
458 } else {
459 msginfo.msgmap = MSGMAP;
460 msginfo.msgpool = MSGPOOL;
461 msginfo.msgtql = MSGTQL;
463 max_id = msg_ids.max_id;
464 up(&msg_ids.sem);
465 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
466 return -EFAULT;
467 return (max_id < 0) ? 0: max_id;
469 case MSG_STAT:
470 case IPC_STAT:
472 struct msqid64_ds tbuf;
473 int success_return;
474 if (!buf)
475 return -EFAULT;
476 if(cmd == MSG_STAT && msqid > msg_ids.size)
477 return -EINVAL;
479 memset(&tbuf,0,sizeof(tbuf));
481 msq = msg_lock(msqid);
482 if (msq == NULL)
483 return -EINVAL;
485 if(cmd == MSG_STAT) {
486 success_return = msg_buildid(msqid, msq->q_perm.seq);
487 } else {
488 err = -EIDRM;
489 if (msg_checkid(msq,msqid))
490 goto out_unlock;
491 success_return = 0;
493 err = -EACCES;
494 if (ipcperms (&msq->q_perm, S_IRUGO))
495 goto out_unlock;
497 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
498 tbuf.msg_stime = msq->q_stime;
499 tbuf.msg_rtime = msq->q_rtime;
500 tbuf.msg_ctime = msq->q_ctime;
501 tbuf.msg_cbytes = msq->q_cbytes;
502 tbuf.msg_qnum = msq->q_qnum;
503 tbuf.msg_qbytes = msq->q_qbytes;
504 tbuf.msg_lspid = msq->q_lspid;
505 tbuf.msg_lrpid = msq->q_lrpid;
506 msg_unlock(msqid);
507 if (copy_msqid_to_user(buf, &tbuf, version))
508 return -EFAULT;
509 return success_return;
511 case IPC_SET:
512 if (!buf)
513 return -EFAULT;
514 if (copy_msqid_from_user (&setbuf, buf, version))
515 return -EFAULT;
516 break;
517 case IPC_RMID:
518 break;
519 default:
520 return -EINVAL;
523 down(&msg_ids.sem);
524 msq = msg_lock(msqid);
525 err=-EINVAL;
526 if (msq == NULL)
527 goto out_up;
529 err = -EIDRM;
530 if (msg_checkid(msq,msqid))
531 goto out_unlock_up;
532 ipcp = &msq->q_perm;
533 err = -EPERM;
534 if (current->euid != ipcp->cuid &&
535 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
536 /* We _could_ check for CAP_CHOWN above, but we don't */
537 goto out_unlock_up;
539 switch (cmd) {
540 case IPC_SET:
542 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
543 goto out_unlock_up;
544 msq->q_qbytes = setbuf.qbytes;
546 ipcp->uid = setbuf.uid;
547 ipcp->gid = setbuf.gid;
548 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
549 (S_IRWXUGO & setbuf.mode);
550 msq->q_ctime = CURRENT_TIME;
551 /* sleeping receivers might be excluded by
552 * stricter permissions.
554 expunge_all(msq,-EAGAIN);
555 /* sleeping senders might be able to send
556 * due to a larger queue size.
558 ss_wakeup(&msq->q_senders,0);
559 msg_unlock(msqid);
560 break;
562 case IPC_RMID:
563 freeque (msqid);
564 break;
566 err = 0;
567 out_up:
568 up(&msg_ids.sem);
569 return err;
570 out_unlock_up:
571 msg_unlock(msqid);
572 goto out_up;
573 out_unlock:
574 msg_unlock(msqid);
575 return err;
578 static int testmsg(struct msg_msg* msg,long type,int mode)
580 switch(mode)
582 case SEARCH_ANY:
583 return 1;
584 case SEARCH_LESSEQUAL:
585 if(msg->m_type <=type)
586 return 1;
587 break;
588 case SEARCH_EQUAL:
589 if(msg->m_type == type)
590 return 1;
591 break;
592 case SEARCH_NOTEQUAL:
593 if(msg->m_type != type)
594 return 1;
595 break;
597 return 0;
600 int inline pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
602 struct list_head* tmp;
604 tmp = msq->q_receivers.next;
605 while (tmp != &msq->q_receivers) {
606 struct msg_receiver* msr;
607 msr = list_entry(tmp,struct msg_receiver,r_list);
608 tmp = tmp->next;
609 if(testmsg(msg,msr->r_msgtype,msr->r_mode)) {
610 list_del(&msr->r_list);
611 if(msr->r_maxsize < msg->m_ts) {
612 msr->r_msg = ERR_PTR(-E2BIG);
613 wake_up_process(msr->r_tsk);
614 } else {
615 msr->r_msg = msg;
616 msq->q_lspid = msr->r_tsk->pid;
617 msq->q_rtime = CURRENT_TIME;
618 wake_up_process(msr->r_tsk);
619 return 1;
623 return 0;
626 asmlinkage long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
628 struct msg_queue *msq;
629 struct msg_msg *msg;
630 long mtype;
631 int err;
633 if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
634 return -EINVAL;
635 if (get_user(mtype, &msgp->mtype))
636 return -EFAULT;
637 if (mtype < 1)
638 return -EINVAL;
640 msg = load_msg(msgp->mtext, msgsz);
641 if(IS_ERR(msg))
642 return PTR_ERR(msg);
644 msg->m_type = mtype;
645 msg->m_ts = msgsz;
647 msq = msg_lock(msqid);
648 err=-EINVAL;
649 if(msq==NULL)
650 goto out_free;
651 retry:
652 err= -EIDRM;
653 if (msg_checkid(msq,msqid))
654 goto out_unlock_free;
656 err=-EACCES;
657 if (ipcperms(&msq->q_perm, S_IWUGO))
658 goto out_unlock_free;
660 if(msgsz + msq->q_cbytes > msq->q_qbytes ||
661 1 + msq->q_qnum > msq->q_qbytes) {
662 struct msg_sender s;
664 if(msgflg&IPC_NOWAIT) {
665 err=-EAGAIN;
666 goto out_unlock_free;
668 ss_add(msq, &s);
669 msg_unlock(msqid);
670 schedule();
671 current->state= TASK_RUNNING;
673 msq = msg_lock(msqid);
674 err = -EIDRM;
675 if(msq==NULL)
676 goto out_free;
677 ss_del(&s);
679 if (signal_pending(current)) {
680 err=-EINTR;
681 goto out_unlock_free;
683 goto retry;
686 if(!pipelined_send(msq,msg)) {
687 /* noone is waiting for this message, enqueue it */
688 list_add_tail(&msg->m_list,&msq->q_messages);
689 msq->q_cbytes += msgsz;
690 msq->q_qnum++;
691 atomic_add(msgsz,&msg_bytes);
692 atomic_inc(&msg_hdrs);
695 err = 0;
696 msg = NULL;
697 msq->q_lspid = current->pid;
698 msq->q_stime = CURRENT_TIME;
700 out_unlock_free:
701 msg_unlock(msqid);
702 out_free:
703 if(msg!=NULL)
704 free_msg(msg);
705 return err;
708 int inline convert_mode(long* msgtyp, int msgflg)
711 * find message of correct type.
712 * msgtyp = 0 => get first.
713 * msgtyp > 0 => get first message of matching type.
714 * msgtyp < 0 => get message with least type must be < abs(msgtype).
716 if(*msgtyp==0)
717 return SEARCH_ANY;
718 if(*msgtyp<0) {
719 *msgtyp=-(*msgtyp);
720 return SEARCH_LESSEQUAL;
722 if(msgflg & MSG_EXCEPT)
723 return SEARCH_NOTEQUAL;
724 return SEARCH_EQUAL;
727 asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz,
728 long msgtyp, int msgflg)
730 struct msg_queue *msq;
731 struct msg_receiver msr_d;
732 struct list_head* tmp;
733 struct msg_msg* msg, *found_msg;
734 int err;
735 int mode;
737 if (msqid < 0 || (long) msgsz < 0)
738 return -EINVAL;
739 mode = convert_mode(&msgtyp,msgflg);
741 msq = msg_lock(msqid);
742 if(msq==NULL)
743 return -EINVAL;
744 retry:
745 err=-EACCES;
746 if (ipcperms (&msq->q_perm, S_IRUGO))
747 goto out_unlock;
749 tmp = msq->q_messages.next;
750 found_msg=NULL;
751 while (tmp != &msq->q_messages) {
752 msg = list_entry(tmp,struct msg_msg,m_list);
753 if(testmsg(msg,msgtyp,mode)) {
754 found_msg = msg;
755 if(mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
756 found_msg=msg;
757 msgtyp=msg->m_type-1;
758 } else {
759 found_msg=msg;
760 break;
763 tmp = tmp->next;
765 if(found_msg) {
766 msg=found_msg;
767 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
768 err=-E2BIG;
769 goto out_unlock;
771 list_del(&msg->m_list);
772 msq->q_qnum--;
773 msq->q_rtime = CURRENT_TIME;
774 msq->q_lrpid = current->pid;
775 msq->q_cbytes -= msg->m_ts;
776 atomic_sub(msg->m_ts,&msg_bytes);
777 atomic_dec(&msg_hdrs);
778 ss_wakeup(&msq->q_senders,0);
779 msg_unlock(msqid);
780 out_success:
781 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
782 if (put_user (msg->m_type, &msgp->mtype) ||
783 store_msg(msgp->mtext, msg, msgsz)) {
784 msgsz = -EFAULT;
786 free_msg(msg);
787 return msgsz;
788 } else
790 struct msg_queue *t;
791 /* no message waiting. Prepare for pipelined
792 * receive.
794 if (msgflg & IPC_NOWAIT) {
795 err=-ENOMSG;
796 goto out_unlock;
798 list_add_tail(&msr_d.r_list,&msq->q_receivers);
799 msr_d.r_tsk = current;
800 msr_d.r_msgtype = msgtyp;
801 msr_d.r_mode = mode;
802 if(msgflg & MSG_NOERROR)
803 msr_d.r_maxsize = INT_MAX;
804 else
805 msr_d.r_maxsize = msgsz;
806 msr_d.r_msg = ERR_PTR(-EAGAIN);
807 current->state = TASK_INTERRUPTIBLE;
808 msg_unlock(msqid);
810 schedule();
811 current->state = TASK_RUNNING;
813 msg = (struct msg_msg*) msr_d.r_msg;
814 if(!IS_ERR(msg))
815 goto out_success;
817 t = msg_lock(msqid);
818 if(t==NULL)
819 msqid=-1;
820 msg = (struct msg_msg*)msr_d.r_msg;
821 if(!IS_ERR(msg)) {
822 /* our message arived while we waited for
823 * the spinlock. Process it.
825 if(msqid!=-1)
826 msg_unlock(msqid);
827 goto out_success;
829 err = PTR_ERR(msg);
830 if(err == -EAGAIN) {
831 if(msqid==-1)
832 BUG();
833 list_del(&msr_d.r_list);
834 if (signal_pending(current))
835 err=-EINTR;
836 else
837 goto retry;
840 out_unlock:
841 if(msqid!=-1)
842 msg_unlock(msqid);
843 return err;
846 #ifdef CONFIG_PROC_FS
847 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
849 off_t pos = 0;
850 off_t begin = 0;
851 int i, len = 0;
853 down(&msg_ids.sem);
854 len += sprintf(buffer, " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
856 for(i = 0; i <= msg_ids.max_id; i++) {
857 struct msg_queue * msq;
858 msq = msg_lock(i);
859 if(msq != NULL) {
860 len += sprintf(buffer + len, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
861 msq->q_perm.key,
862 msg_buildid(i,msq->q_perm.seq),
863 msq->q_perm.mode,
864 msq->q_cbytes,
865 msq->q_qnum,
866 msq->q_lspid,
867 msq->q_lrpid,
868 msq->q_perm.uid,
869 msq->q_perm.gid,
870 msq->q_perm.cuid,
871 msq->q_perm.cgid,
872 msq->q_stime,
873 msq->q_rtime,
874 msq->q_ctime);
875 msg_unlock(i);
877 pos += len;
878 if(pos < offset) {
879 len = 0;
880 begin = pos;
882 if(pos > offset + length)
883 goto done;
887 *eof = 1;
888 done:
889 up(&msg_ids.sem);
890 *start = buffer + (offset - begin);
891 len -= (offset - begin);
892 if(len > length)
893 len = length;
894 if(len < 0)
895 len = 0;
896 return len;
898 #endif