MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / ipc / msg.c
bloba992a1fdb4ecb92d11072f4199f37a160a17a5d2
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/slab.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 <linux/security.h>
26 #include <linux/sched.h>
27 #include <asm/current.h>
28 #include <asm/uaccess.h>
29 #include "util.h"
31 /* sysctl: */
32 int msg_ctlmax = MSGMAX;
33 int msg_ctlmnb = MSGMNB;
34 int msg_ctlmni = MSGMNI;
36 /* one msg_receiver structure for each sleeping receiver */
37 struct msg_receiver {
38 struct list_head r_list;
39 struct task_struct* r_tsk;
41 int r_mode;
42 long r_msgtype;
43 long r_maxsize;
45 struct msg_msg* volatile r_msg;
48 /* one msg_sender for each sleeping sender */
49 struct msg_sender {
50 struct list_head list;
51 struct task_struct* tsk;
54 #define SEARCH_ANY 1
55 #define SEARCH_EQUAL 2
56 #define SEARCH_NOTEQUAL 3
57 #define SEARCH_LESSEQUAL 4
59 static atomic_t msg_bytes = ATOMIC_INIT(0);
60 static atomic_t msg_hdrs = ATOMIC_INIT(0);
62 static struct ipc_ids msg_ids;
64 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
65 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
66 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
67 #define msg_checkid(msq, msgid) \
68 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
69 #define msg_buildid(id, seq) \
70 ipc_buildid(&msg_ids, id, seq)
72 static void freeque (struct msg_queue *msq, int id);
73 static int newque (key_t key, int msgflg);
74 #ifdef CONFIG_PROC_FS
75 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
76 #endif
78 void __init msg_init (void)
80 ipc_init_ids(&msg_ids,msg_ctlmni);
82 #ifdef CONFIG_PROC_FS
83 create_proc_read_entry("sysvipc/msg", 0, NULL, sysvipc_msg_read_proc, NULL);
84 #endif
87 static int newque (key_t key, int msgflg)
89 int id;
90 int retval;
91 struct msg_queue *msq;
93 msq = ipc_rcu_alloc(sizeof(*msq));
94 if (!msq)
95 return -ENOMEM;
97 msq->q_perm.mode = (msgflg & S_IRWXUGO);
98 msq->q_perm.key = key;
100 msq->q_perm.security = NULL;
101 retval = security_msg_queue_alloc(msq);
102 if (retval) {
103 ipc_rcu_putref(msq);
104 return retval;
107 id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
108 if(id == -1) {
109 security_msg_queue_free(msq);
110 ipc_rcu_putref(msq);
111 return -ENOSPC;
114 msq->q_stime = msq->q_rtime = 0;
115 msq->q_ctime = get_seconds();
116 msq->q_cbytes = msq->q_qnum = 0;
117 msq->q_qbytes = msg_ctlmnb;
118 msq->q_lspid = msq->q_lrpid = 0;
119 INIT_LIST_HEAD(&msq->q_messages);
120 INIT_LIST_HEAD(&msq->q_receivers);
121 INIT_LIST_HEAD(&msq->q_senders);
122 msg_unlock(msq);
124 return msg_buildid(id,msq->q_perm.seq);
127 static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
129 mss->tsk=current;
130 current->state=TASK_INTERRUPTIBLE;
131 list_add_tail(&mss->list,&msq->q_senders);
134 static inline void ss_del(struct msg_sender* mss)
136 if(mss->list.next != NULL)
137 list_del(&mss->list);
140 static void ss_wakeup(struct list_head* h, int kill)
142 struct list_head *tmp;
144 tmp = h->next;
145 while (tmp != h) {
146 struct msg_sender* mss;
148 mss = list_entry(tmp,struct msg_sender,list);
149 tmp = tmp->next;
150 if(kill)
151 mss->list.next=NULL;
152 wake_up_process(mss->tsk);
156 static void expunge_all(struct msg_queue* msq, int res)
158 struct list_head *tmp;
160 tmp = msq->q_receivers.next;
161 while (tmp != &msq->q_receivers) {
162 struct msg_receiver* msr;
164 msr = list_entry(tmp,struct msg_receiver,r_list);
165 tmp = tmp->next;
166 msr->r_msg = NULL;
167 wake_up_process(msr->r_tsk);
168 smp_mb();
169 msr->r_msg = ERR_PTR(res);
173 * freeque() wakes up waiters on the sender and receiver waiting queue,
174 * removes the message queue from message queue ID
175 * array, and cleans up all the messages associated with this queue.
177 * msg_ids.sem and the spinlock for this message queue is hold
178 * before freeque() is called. msg_ids.sem remains locked on exit.
180 static void freeque (struct msg_queue *msq, int id)
182 struct list_head *tmp;
184 expunge_all(msq,-EIDRM);
185 ss_wakeup(&msq->q_senders,1);
186 msq = msg_rmid(id);
187 msg_unlock(msq);
189 tmp = msq->q_messages.next;
190 while(tmp != &msq->q_messages) {
191 struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
192 tmp = tmp->next;
193 atomic_dec(&msg_hdrs);
194 free_msg(msg);
196 atomic_sub(msq->q_cbytes, &msg_bytes);
197 security_msg_queue_free(msq);
198 ipc_rcu_putref(msq);
201 asmlinkage long sys_msgget (key_t key, int msgflg)
203 int id, ret = -EPERM;
204 struct msg_queue *msq;
206 down(&msg_ids.sem);
207 if (key == IPC_PRIVATE)
208 ret = newque(key, msgflg);
209 else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
210 if (!(msgflg & IPC_CREAT))
211 ret = -ENOENT;
212 else
213 ret = newque(key, msgflg);
214 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
215 ret = -EEXIST;
216 } else {
217 msq = msg_lock(id);
218 if(msq==NULL)
219 BUG();
220 if (ipcperms(&msq->q_perm, msgflg))
221 ret = -EACCES;
222 else {
223 int qid = msg_buildid(id, msq->q_perm.seq);
224 ret = security_msg_queue_associate(msq, msgflg);
225 if (!ret)
226 ret = qid;
228 msg_unlock(msq);
230 up(&msg_ids.sem);
231 return ret;
234 static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
236 switch(version) {
237 case IPC_64:
238 return copy_to_user (buf, in, sizeof(*in));
239 case IPC_OLD:
241 struct msqid_ds out;
243 memset(&out,0,sizeof(out));
245 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
247 out.msg_stime = in->msg_stime;
248 out.msg_rtime = in->msg_rtime;
249 out.msg_ctime = in->msg_ctime;
251 if(in->msg_cbytes > USHRT_MAX)
252 out.msg_cbytes = USHRT_MAX;
253 else
254 out.msg_cbytes = in->msg_cbytes;
255 out.msg_lcbytes = in->msg_cbytes;
257 if(in->msg_qnum > USHRT_MAX)
258 out.msg_qnum = USHRT_MAX;
259 else
260 out.msg_qnum = in->msg_qnum;
262 if(in->msg_qbytes > USHRT_MAX)
263 out.msg_qbytes = USHRT_MAX;
264 else
265 out.msg_qbytes = in->msg_qbytes;
266 out.msg_lqbytes = in->msg_qbytes;
268 out.msg_lspid = in->msg_lspid;
269 out.msg_lrpid = in->msg_lrpid;
271 return copy_to_user (buf, &out, sizeof(out));
273 default:
274 return -EINVAL;
278 struct msq_setbuf {
279 unsigned long qbytes;
280 uid_t uid;
281 gid_t gid;
282 mode_t mode;
285 static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
287 switch(version) {
288 case IPC_64:
290 struct msqid64_ds tbuf;
292 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
293 return -EFAULT;
295 out->qbytes = tbuf.msg_qbytes;
296 out->uid = tbuf.msg_perm.uid;
297 out->gid = tbuf.msg_perm.gid;
298 out->mode = tbuf.msg_perm.mode;
300 return 0;
302 case IPC_OLD:
304 struct msqid_ds tbuf_old;
306 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
307 return -EFAULT;
309 out->uid = tbuf_old.msg_perm.uid;
310 out->gid = tbuf_old.msg_perm.gid;
311 out->mode = tbuf_old.msg_perm.mode;
313 if(tbuf_old.msg_qbytes == 0)
314 out->qbytes = tbuf_old.msg_lqbytes;
315 else
316 out->qbytes = tbuf_old.msg_qbytes;
318 return 0;
320 default:
321 return -EINVAL;
325 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
327 int err, version;
328 struct msg_queue *msq;
329 struct msq_setbuf setbuf;
330 struct kern_ipc_perm *ipcp;
332 if (msqid < 0 || cmd < 0)
333 return -EINVAL;
335 version = ipc_parse_version(&cmd);
337 switch (cmd) {
338 case IPC_INFO:
339 case MSG_INFO:
341 struct msginfo msginfo;
342 int max_id;
343 if (!buf)
344 return -EFAULT;
345 /* We must not return kernel stack data.
346 * due to padding, it's not enough
347 * to set all member fields.
350 err = security_msg_queue_msgctl(NULL, cmd);
351 if (err)
352 return err;
354 memset(&msginfo,0,sizeof(msginfo));
355 msginfo.msgmni = msg_ctlmni;
356 msginfo.msgmax = msg_ctlmax;
357 msginfo.msgmnb = msg_ctlmnb;
358 msginfo.msgssz = MSGSSZ;
359 msginfo.msgseg = MSGSEG;
360 down(&msg_ids.sem);
361 if (cmd == MSG_INFO) {
362 msginfo.msgpool = msg_ids.in_use;
363 msginfo.msgmap = atomic_read(&msg_hdrs);
364 msginfo.msgtql = atomic_read(&msg_bytes);
365 } else {
366 msginfo.msgmap = MSGMAP;
367 msginfo.msgpool = MSGPOOL;
368 msginfo.msgtql = MSGTQL;
370 max_id = msg_ids.max_id;
371 up(&msg_ids.sem);
372 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
373 return -EFAULT;
374 return (max_id < 0) ? 0: max_id;
376 case MSG_STAT:
377 case IPC_STAT:
379 struct msqid64_ds tbuf;
380 int success_return;
381 if (!buf)
382 return -EFAULT;
383 if(cmd == MSG_STAT && msqid >= msg_ids.size)
384 return -EINVAL;
386 memset(&tbuf,0,sizeof(tbuf));
388 msq = msg_lock(msqid);
389 if (msq == NULL)
390 return -EINVAL;
392 if(cmd == MSG_STAT) {
393 success_return = msg_buildid(msqid, msq->q_perm.seq);
394 } else {
395 err = -EIDRM;
396 if (msg_checkid(msq,msqid))
397 goto out_unlock;
398 success_return = 0;
400 err = -EACCES;
401 if (ipcperms (&msq->q_perm, S_IRUGO))
402 goto out_unlock;
404 err = security_msg_queue_msgctl(msq, cmd);
405 if (err)
406 goto out_unlock;
408 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
409 tbuf.msg_stime = msq->q_stime;
410 tbuf.msg_rtime = msq->q_rtime;
411 tbuf.msg_ctime = msq->q_ctime;
412 tbuf.msg_cbytes = msq->q_cbytes;
413 tbuf.msg_qnum = msq->q_qnum;
414 tbuf.msg_qbytes = msq->q_qbytes;
415 tbuf.msg_lspid = msq->q_lspid;
416 tbuf.msg_lrpid = msq->q_lrpid;
417 msg_unlock(msq);
418 if (copy_msqid_to_user(buf, &tbuf, version))
419 return -EFAULT;
420 return success_return;
422 case IPC_SET:
423 if (!buf)
424 return -EFAULT;
425 if (copy_msqid_from_user (&setbuf, buf, version))
426 return -EFAULT;
427 break;
428 case IPC_RMID:
429 break;
430 default:
431 return -EINVAL;
434 down(&msg_ids.sem);
435 msq = msg_lock(msqid);
436 err=-EINVAL;
437 if (msq == NULL)
438 goto out_up;
440 err = -EIDRM;
441 if (msg_checkid(msq,msqid))
442 goto out_unlock_up;
443 ipcp = &msq->q_perm;
444 err = -EPERM;
445 if (current->euid != ipcp->cuid &&
446 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
447 /* We _could_ check for CAP_CHOWN above, but we don't */
448 goto out_unlock_up;
450 err = security_msg_queue_msgctl(msq, cmd);
451 if (err)
452 goto out_unlock_up;
454 switch (cmd) {
455 case IPC_SET:
457 err = -EPERM;
458 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
459 goto out_unlock_up;
461 msq->q_qbytes = setbuf.qbytes;
463 ipcp->uid = setbuf.uid;
464 ipcp->gid = setbuf.gid;
465 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
466 (S_IRWXUGO & setbuf.mode);
467 msq->q_ctime = get_seconds();
468 /* sleeping receivers might be excluded by
469 * stricter permissions.
471 expunge_all(msq,-EAGAIN);
472 /* sleeping senders might be able to send
473 * due to a larger queue size.
475 ss_wakeup(&msq->q_senders,0);
476 msg_unlock(msq);
477 break;
479 case IPC_RMID:
480 freeque (msq, msqid);
481 break;
483 err = 0;
484 out_up:
485 up(&msg_ids.sem);
486 return err;
487 out_unlock_up:
488 msg_unlock(msq);
489 goto out_up;
490 out_unlock:
491 msg_unlock(msq);
492 return err;
495 static int testmsg(struct msg_msg* msg,long type,int mode)
497 switch(mode)
499 case SEARCH_ANY:
500 return 1;
501 case SEARCH_LESSEQUAL:
502 if(msg->m_type <=type)
503 return 1;
504 break;
505 case SEARCH_EQUAL:
506 if(msg->m_type == type)
507 return 1;
508 break;
509 case SEARCH_NOTEQUAL:
510 if(msg->m_type != type)
511 return 1;
512 break;
514 return 0;
517 static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
519 struct list_head* tmp;
521 tmp = msq->q_receivers.next;
522 while (tmp != &msq->q_receivers) {
523 struct msg_receiver* msr;
524 msr = list_entry(tmp,struct msg_receiver,r_list);
525 tmp = tmp->next;
526 if(testmsg(msg,msr->r_msgtype,msr->r_mode) &&
527 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
528 list_del(&msr->r_list);
529 if(msr->r_maxsize < msg->m_ts) {
530 msr->r_msg = NULL;
531 wake_up_process(msr->r_tsk);
532 smp_mb();
533 msr->r_msg = ERR_PTR(-E2BIG);
534 } else {
535 msr->r_msg = NULL;
536 msq->q_lrpid = msr->r_tsk->pid;
537 msq->q_rtime = get_seconds();
538 wake_up_process(msr->r_tsk);
539 smp_mb();
540 msr->r_msg = msg;
541 return 1;
545 return 0;
548 asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
550 struct msg_queue *msq;
551 struct msg_msg *msg;
552 long mtype;
553 int err;
555 if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
556 return -EINVAL;
557 if (get_user(mtype, &msgp->mtype))
558 return -EFAULT;
559 if (mtype < 1)
560 return -EINVAL;
562 msg = load_msg(msgp->mtext, msgsz);
563 if(IS_ERR(msg))
564 return PTR_ERR(msg);
566 msg->m_type = mtype;
567 msg->m_ts = msgsz;
569 msq = msg_lock(msqid);
570 err=-EINVAL;
571 if(msq==NULL)
572 goto out_free;
574 err= -EIDRM;
575 if (msg_checkid(msq,msqid))
576 goto out_unlock_free;
578 for (;;) {
579 struct msg_sender s;
581 err=-EACCES;
582 if (ipcperms(&msq->q_perm, S_IWUGO))
583 goto out_unlock_free;
585 err = security_msg_queue_msgsnd(msq, msg, msgflg);
586 if (err)
587 goto out_unlock_free;
589 if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
590 1 + msq->q_qnum <= msq->q_qbytes) {
591 break;
594 /* queue full, wait: */
595 if(msgflg&IPC_NOWAIT) {
596 err=-EAGAIN;
597 goto out_unlock_free;
599 ss_add(msq, &s);
600 ipc_rcu_getref(msq);
601 msg_unlock(msq);
602 schedule();
604 ipc_lock_by_ptr(&msq->q_perm);
605 ipc_rcu_putref(msq);
606 if (msq->q_perm.deleted) {
607 err = -EIDRM;
608 goto out_unlock_free;
610 ss_del(&s);
612 if (signal_pending(current)) {
613 err=-ERESTARTNOHAND;
614 goto out_unlock_free;
618 msq->q_lspid = current->tgid;
619 msq->q_stime = get_seconds();
621 if(!pipelined_send(msq,msg)) {
622 /* noone is waiting for this message, enqueue it */
623 list_add_tail(&msg->m_list,&msq->q_messages);
624 msq->q_cbytes += msgsz;
625 msq->q_qnum++;
626 atomic_add(msgsz,&msg_bytes);
627 atomic_inc(&msg_hdrs);
630 err = 0;
631 msg = NULL;
633 out_unlock_free:
634 msg_unlock(msq);
635 out_free:
636 if(msg!=NULL)
637 free_msg(msg);
638 return err;
641 static inline int convert_mode(long* msgtyp, int msgflg)
644 * find message of correct type.
645 * msgtyp = 0 => get first.
646 * msgtyp > 0 => get first message of matching type.
647 * msgtyp < 0 => get message with least type must be < abs(msgtype).
649 if(*msgtyp==0)
650 return SEARCH_ANY;
651 if(*msgtyp<0) {
652 *msgtyp=-(*msgtyp);
653 return SEARCH_LESSEQUAL;
655 if(msgflg & MSG_EXCEPT)
656 return SEARCH_NOTEQUAL;
657 return SEARCH_EQUAL;
660 asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
661 long msgtyp, int msgflg)
663 struct msg_queue *msq;
664 struct msg_msg *msg;
665 int mode;
667 if (msqid < 0 || (long) msgsz < 0)
668 return -EINVAL;
669 mode = convert_mode(&msgtyp,msgflg);
671 msq = msg_lock(msqid);
672 if(msq==NULL)
673 return -EINVAL;
675 msg = ERR_PTR(-EIDRM);
676 if (msg_checkid(msq,msqid))
677 goto out_unlock;
679 for (;;) {
680 struct msg_receiver msr_d;
681 struct list_head* tmp;
683 msg = ERR_PTR(-EACCES);
684 if (ipcperms (&msq->q_perm, S_IRUGO))
685 goto out_unlock;
687 msg = ERR_PTR(-EAGAIN);
688 tmp = msq->q_messages.next;
689 while (tmp != &msq->q_messages) {
690 struct msg_msg *walk_msg;
691 walk_msg = list_entry(tmp,struct msg_msg,m_list);
692 if(testmsg(walk_msg,msgtyp,mode) &&
693 !security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
694 msg = walk_msg;
695 if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
696 msg=walk_msg;
697 msgtyp=walk_msg->m_type-1;
698 } else {
699 msg=walk_msg;
700 break;
703 tmp = tmp->next;
705 if(!IS_ERR(msg)) {
706 /* Found a suitable message. Unlink it from the queue. */
707 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
708 msg = ERR_PTR(-E2BIG);
709 goto out_unlock;
711 list_del(&msg->m_list);
712 msq->q_qnum--;
713 msq->q_rtime = get_seconds();
714 msq->q_lrpid = current->tgid;
715 msq->q_cbytes -= msg->m_ts;
716 atomic_sub(msg->m_ts,&msg_bytes);
717 atomic_dec(&msg_hdrs);
718 ss_wakeup(&msq->q_senders,0);
719 msg_unlock(msq);
720 break;
722 /* No message waiting. Wait for a message */
723 if (msgflg & IPC_NOWAIT) {
724 msg = ERR_PTR(-ENOMSG);
725 goto out_unlock;
727 list_add_tail(&msr_d.r_list,&msq->q_receivers);
728 msr_d.r_tsk = current;
729 msr_d.r_msgtype = msgtyp;
730 msr_d.r_mode = mode;
731 if(msgflg & MSG_NOERROR)
732 msr_d.r_maxsize = INT_MAX;
733 else
734 msr_d.r_maxsize = msgsz;
735 msr_d.r_msg = ERR_PTR(-EAGAIN);
736 current->state = TASK_INTERRUPTIBLE;
737 msg_unlock(msq);
739 schedule();
741 /* Lockless receive, part 1:
742 * Disable preemption. We don't hold a reference to the queue
743 * and getting a reference would defeat the idea of a lockless
744 * operation, thus the code relies on rcu to guarantee the
745 * existance of msq:
746 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
747 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
748 * rcu_read_lock() prevents preemption between reading r_msg
749 * and the spin_lock() inside ipc_lock_by_ptr().
751 rcu_read_lock();
753 /* Lockless receive, part 2:
754 * Wait until pipelined_send or expunge_all are outside of
755 * wake_up_process(). There is a race with exit(), see
756 * ipc/mqueue.c for the details.
758 msg = (struct msg_msg*) msr_d.r_msg;
759 while (msg == NULL) {
760 cpu_relax();
761 msg = (struct msg_msg*) msr_d.r_msg;
764 /* Lockless receive, part 3:
765 * If there is a message or an error then accept it without
766 * locking.
768 if(msg != ERR_PTR(-EAGAIN)) {
769 rcu_read_unlock();
770 break;
773 /* Lockless receive, part 3:
774 * Acquire the queue spinlock.
776 ipc_lock_by_ptr(&msq->q_perm);
777 rcu_read_unlock();
779 /* Lockless receive, part 4:
780 * Repeat test after acquiring the spinlock.
782 msg = (struct msg_msg*)msr_d.r_msg;
783 if(msg != ERR_PTR(-EAGAIN))
784 goto out_unlock;
786 list_del(&msr_d.r_list);
787 if (signal_pending(current)) {
788 msg = ERR_PTR(-ERESTARTNOHAND);
789 out_unlock:
790 msg_unlock(msq);
791 break;
794 if (IS_ERR(msg))
795 return PTR_ERR(msg);
797 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
798 if (put_user (msg->m_type, &msgp->mtype) ||
799 store_msg(msgp->mtext, msg, msgsz)) {
800 msgsz = -EFAULT;
802 free_msg(msg);
803 return msgsz;
806 #ifdef CONFIG_PROC_FS
807 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
809 off_t pos = 0;
810 off_t begin = 0;
811 int i, len = 0;
813 down(&msg_ids.sem);
814 len += sprintf(buffer, " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
816 for(i = 0; i <= msg_ids.max_id; i++) {
817 struct msg_queue * msq;
818 msq = msg_lock(i);
819 if(msq != NULL) {
820 len += sprintf(buffer + len, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
821 msq->q_perm.key,
822 msg_buildid(i,msq->q_perm.seq),
823 msq->q_perm.mode,
824 msq->q_cbytes,
825 msq->q_qnum,
826 msq->q_lspid,
827 msq->q_lrpid,
828 msq->q_perm.uid,
829 msq->q_perm.gid,
830 msq->q_perm.cuid,
831 msq->q_perm.cgid,
832 msq->q_stime,
833 msq->q_rtime,
834 msq->q_ctime);
835 msg_unlock(msq);
837 pos += len;
838 if(pos < offset) {
839 len = 0;
840 begin = pos;
842 if(pos > offset + length)
843 goto done;
847 *eof = 1;
848 done:
849 up(&msg_ids.sem);
850 *start = buffer + (offset - begin);
851 len -= (offset - begin);
852 if(len > length)
853 len = length;
854 if(len < 0)
855 len = 0;
856 return len;
858 #endif