blob: 48a7f17a7236397c219e03799117718fa4b2d3ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/msg.c
3 * Copyright (C) 1992 Krishna Balasubramanian
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
Christian Kujau624dffc2006-01-15 02:43:54 +010015 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Mike Waychison19b49462005-09-06 15:17:10 -070030#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080031#include <linux/mutex.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/current.h>
34#include <asm/uaccess.h>
35#include "util.h"
36
37/* sysctl: */
38int msg_ctlmax = MSGMAX;
39int msg_ctlmnb = MSGMNB;
40int msg_ctlmni = MSGMNI;
41
42/* one msg_receiver structure for each sleeping receiver */
43struct msg_receiver {
44 struct list_head r_list;
45 struct task_struct* r_tsk;
46
47 int r_mode;
48 long r_msgtype;
49 long r_maxsize;
50
51 struct msg_msg* volatile r_msg;
52};
53
54/* one msg_sender for each sleeping sender */
55struct msg_sender {
56 struct list_head list;
57 struct task_struct* tsk;
58};
59
60#define SEARCH_ANY 1
61#define SEARCH_EQUAL 2
62#define SEARCH_NOTEQUAL 3
63#define SEARCH_LESSEQUAL 4
64
65static atomic_t msg_bytes = ATOMIC_INIT(0);
66static atomic_t msg_hdrs = ATOMIC_INIT(0);
67
68static struct ipc_ids msg_ids;
69
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)
77
78static void freeque (struct msg_queue *msq, int id);
79static int newque (key_t key, int msgflg);
80#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070081static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84void __init msg_init (void)
85{
86 ipc_init_ids(&msg_ids,msg_ctlmni);
Mike Waychison19b49462005-09-06 15:17:10 -070087 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static int newque (key_t key, int msgflg)
94{
95 int id;
96 int retval;
97 struct msg_queue *msq;
98
99 msq = ipc_rcu_alloc(sizeof(*msq));
100 if (!msq)
101 return -ENOMEM;
102
103 msq->q_perm.mode = (msgflg & S_IRWXUGO);
104 msq->q_perm.key = key;
105
106 msq->q_perm.security = NULL;
107 retval = security_msg_queue_alloc(msq);
108 if (retval) {
109 ipc_rcu_putref(msq);
110 return retval;
111 }
112
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;
118 }
119
Mike Waychison19b49462005-09-06 15:17:10 -0700120 msq->q_id = msg_buildid(id,msq->q_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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);
130
Mike Waychison19b49462005-09-06 15:17:10 -0700131 return msq->q_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
135{
136 mss->tsk=current;
137 current->state=TASK_INTERRUPTIBLE;
138 list_add_tail(&mss->list,&msq->q_senders);
139}
140
141static inline void ss_del(struct msg_sender* mss)
142{
143 if(mss->list.next != NULL)
144 list_del(&mss->list);
145}
146
147static void ss_wakeup(struct list_head* h, int kill)
148{
149 struct list_head *tmp;
150
151 tmp = h->next;
152 while (tmp != h) {
153 struct msg_sender* mss;
154
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);
160 }
161}
162
163static void expunge_all(struct msg_queue* msq, int res)
164{
165 struct list_head *tmp;
166
167 tmp = msq->q_receivers.next;
168 while (tmp != &msq->q_receivers) {
169 struct msg_receiver* msr;
170
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);
177 }
178}
179/*
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.
183 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800184 * msg_ids.mutex and the spinlock for this message queue is hold
185 * before freeque() is called. msg_ids.mutex remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
187static void freeque (struct msg_queue *msq, int id)
188{
189 struct list_head *tmp;
190
191 expunge_all(msq,-EIDRM);
192 ss_wakeup(&msq->q_senders,1);
193 msq = msg_rmid(id);
194 msg_unlock(msq);
195
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);
202 }
203 atomic_sub(msq->q_cbytes, &msg_bytes);
204 security_msg_queue_free(msq);
205 ipc_rcu_putref(msq);
206}
207
208asmlinkage long sys_msgget (key_t key, int msgflg)
209{
210 int id, ret = -EPERM;
211 struct msg_queue *msq;
212
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800213 mutex_lock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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);
Eric Sesterhenn8cd52832006-03-24 18:44:22 +0100225 BUG_ON(msq==NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 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;
233 }
234 msg_unlock(msq);
235 }
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800236 mutex_unlock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return ret;
238}
239
240static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
241{
242 switch(version) {
243 case IPC_64:
244 return copy_to_user (buf, in, sizeof(*in));
245 case IPC_OLD:
246 {
247 struct msqid_ds out;
248
249 memset(&out,0,sizeof(out));
250
251 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
252
253 out.msg_stime = in->msg_stime;
254 out.msg_rtime = in->msg_rtime;
255 out.msg_ctime = in->msg_ctime;
256
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;
262
263 if(in->msg_qnum > USHRT_MAX)
264 out.msg_qnum = USHRT_MAX;
265 else
266 out.msg_qnum = in->msg_qnum;
267
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;
273
274 out.msg_lspid = in->msg_lspid;
275 out.msg_lrpid = in->msg_lrpid;
276
277 return copy_to_user (buf, &out, sizeof(out));
278 }
279 default:
280 return -EINVAL;
281 }
282}
283
284struct msq_setbuf {
285 unsigned long qbytes;
286 uid_t uid;
287 gid_t gid;
288 mode_t mode;
289};
290
291static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
292{
293 switch(version) {
294 case IPC_64:
295 {
296 struct msqid64_ds tbuf;
297
298 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
299 return -EFAULT;
300
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;
305
306 return 0;
307 }
308 case IPC_OLD:
309 {
310 struct msqid_ds tbuf_old;
311
312 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
313 return -EFAULT;
314
315 out->uid = tbuf_old.msg_perm.uid;
316 out->gid = tbuf_old.msg_perm.gid;
317 out->mode = tbuf_old.msg_perm.mode;
318
319 if(tbuf_old.msg_qbytes == 0)
320 out->qbytes = tbuf_old.msg_lqbytes;
321 else
322 out->qbytes = tbuf_old.msg_qbytes;
323
324 return 0;
325 }
326 default:
327 return -EINVAL;
328 }
329}
330
331asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
332{
333 int err, version;
334 struct msg_queue *msq;
335 struct msq_setbuf setbuf;
336 struct kern_ipc_perm *ipcp;
337
338 if (msqid < 0 || cmd < 0)
339 return -EINVAL;
340
341 version = ipc_parse_version(&cmd);
342
343 switch (cmd) {
344 case IPC_INFO:
345 case MSG_INFO:
346 {
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.
354 */
355
356 err = security_msg_queue_msgctl(NULL, cmd);
357 if (err)
358 return err;
359
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;
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800366 mutex_lock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 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;
375 }
376 max_id = msg_ids.max_id;
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800377 mutex_unlock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
379 return -EFAULT;
380 return (max_id < 0) ? 0: max_id;
381 }
382 case MSG_STAT:
383 case IPC_STAT:
384 {
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;
391
392 memset(&tbuf,0,sizeof(tbuf));
393
394 msq = msg_lock(msqid);
395 if (msq == NULL)
396 return -EINVAL;
397
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;
405 }
406 err = -EACCES;
407 if (ipcperms (&msq->q_perm, S_IRUGO))
408 goto out_unlock;
409
410 err = security_msg_queue_msgctl(msq, cmd);
411 if (err)
412 goto out_unlock;
413
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;
427 }
428 case IPC_SET:
429 if (!buf)
430 return -EFAULT;
431 if (copy_msqid_from_user (&setbuf, buf, version))
432 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 case IPC_RMID:
435 break;
436 default:
437 return -EINVAL;
438 }
439
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800440 mutex_lock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 msq = msg_lock(msqid);
442 err=-EINVAL;
443 if (msq == NULL)
444 goto out_up;
445
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;
455
456 err = security_msg_queue_msgctl(msq, cmd);
457 if (err)
458 goto out_unlock_up;
459
460 switch (cmd) {
461 case IPC_SET:
462 {
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000463 if ((err = audit_ipc_perms(setbuf.qbytes, setbuf.uid, setbuf.gid, setbuf.mode, ipcp)))
464 goto out_unlock_up;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 err = -EPERM;
467 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
468 goto out_unlock_up;
469
470 msq->q_qbytes = setbuf.qbytes;
471
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.
479 */
480 expunge_all(msq,-EAGAIN);
481 /* sleeping senders might be able to send
482 * due to a larger queue size.
483 */
484 ss_wakeup(&msq->q_senders,0);
485 msg_unlock(msq);
486 break;
487 }
488 case IPC_RMID:
489 freeque (msq, msqid);
490 break;
491 }
492 err = 0;
493out_up:
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800494 mutex_unlock(&msg_ids.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return err;
496out_unlock_up:
497 msg_unlock(msq);
498 goto out_up;
499out_unlock:
500 msg_unlock(msq);
501 return err;
502}
503
504static int testmsg(struct msg_msg* msg,long type,int mode)
505{
506 switch(mode)
507 {
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;
522 }
523 return 0;
524}
525
526static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
527{
528 struct list_head* tmp;
529
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;
551 }
552 }
553 }
554 return 0;
555}
556
557asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
558{
559 struct msg_queue *msq;
560 struct msg_msg *msg;
561 long mtype;
562 int err;
563
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;
570
571 msg = load_msg(msgp->mtext, msgsz);
572 if(IS_ERR(msg))
573 return PTR_ERR(msg);
574
575 msg->m_type = mtype;
576 msg->m_ts = msgsz;
577
578 msq = msg_lock(msqid);
579 err=-EINVAL;
580 if(msq==NULL)
581 goto out_free;
582
583 err= -EIDRM;
584 if (msg_checkid(msq,msqid))
585 goto out_unlock_free;
586
587 for (;;) {
588 struct msg_sender s;
589
590 err=-EACCES;
591 if (ipcperms(&msq->q_perm, S_IWUGO))
592 goto out_unlock_free;
593
594 err = security_msg_queue_msgsnd(msq, msg, msgflg);
595 if (err)
596 goto out_unlock_free;
597
598 if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
599 1 + msq->q_qnum <= msq->q_qbytes) {
600 break;
601 }
602
603 /* queue full, wait: */
604 if(msgflg&IPC_NOWAIT) {
605 err=-EAGAIN;
606 goto out_unlock_free;
607 }
608 ss_add(msq, &s);
609 ipc_rcu_getref(msq);
610 msg_unlock(msq);
611 schedule();
612
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;
618 }
619 ss_del(&s);
620
621 if (signal_pending(current)) {
622 err=-ERESTARTNOHAND;
623 goto out_unlock_free;
624 }
625 }
626
627 msq->q_lspid = current->tgid;
628 msq->q_stime = get_seconds();
629
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);
637 }
638
639 err = 0;
640 msg = NULL;
641
642out_unlock_free:
643 msg_unlock(msq);
644out_free:
645 if(msg!=NULL)
646 free_msg(msg);
647 return err;
648}
649
650static inline int convert_mode(long* msgtyp, int msgflg)
651{
652 /*
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).
657 */
658 if(*msgtyp==0)
659 return SEARCH_ANY;
660 if(*msgtyp<0) {
661 *msgtyp=-(*msgtyp);
662 return SEARCH_LESSEQUAL;
663 }
664 if(msgflg & MSG_EXCEPT)
665 return SEARCH_NOTEQUAL;
666 return SEARCH_EQUAL;
667}
668
669asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
670 long msgtyp, int msgflg)
671{
672 struct msg_queue *msq;
673 struct msg_msg *msg;
674 int mode;
675
676 if (msqid < 0 || (long) msgsz < 0)
677 return -EINVAL;
678 mode = convert_mode(&msgtyp,msgflg);
679
680 msq = msg_lock(msqid);
681 if(msq==NULL)
682 return -EINVAL;
683
684 msg = ERR_PTR(-EIDRM);
685 if (msg_checkid(msq,msqid))
686 goto out_unlock;
687
688 for (;;) {
689 struct msg_receiver msr_d;
690 struct list_head* tmp;
691
692 msg = ERR_PTR(-EACCES);
693 if (ipcperms (&msq->q_perm, S_IRUGO))
694 goto out_unlock;
695
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;
710 }
711 }
712 tmp = tmp->next;
713 }
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;
719 }
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;
730 }
731 /* No message waiting. Wait for a message */
732 if (msgflg & IPC_NOWAIT) {
733 msg = ERR_PTR(-ENOMSG);
734 goto out_unlock;
735 }
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);
747
748 schedule();
749
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().
759 */
760 rcu_read_lock();
761
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.
766 */
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;
771 }
772
773 /* Lockless receive, part 3:
774 * If there is a message or an error then accept it without
775 * locking.
776 */
777 if(msg != ERR_PTR(-EAGAIN)) {
778 rcu_read_unlock();
779 break;
780 }
781
782 /* Lockless receive, part 3:
783 * Acquire the queue spinlock.
784 */
785 ipc_lock_by_ptr(&msq->q_perm);
786 rcu_read_unlock();
787
788 /* Lockless receive, part 4:
789 * Repeat test after acquiring the spinlock.
790 */
791 msg = (struct msg_msg*)msr_d.r_msg;
792 if(msg != ERR_PTR(-EAGAIN))
793 goto out_unlock;
794
795 list_del(&msr_d.r_list);
796 if (signal_pending(current)) {
797 msg = ERR_PTR(-ERESTARTNOHAND);
798out_unlock:
799 msg_unlock(msq);
800 break;
801 }
802 }
803 if (IS_ERR(msg))
804 return PTR_ERR(msg);
805
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;
810 }
811 free_msg(msg);
812 return msgsz;
813}
814
815#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700816static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Mike Waychison19b49462005-09-06 15:17:10 -0700818 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Mike Waychison19b49462005-09-06 15:17:10 -0700820 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837#endif