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 <linux/syscalls.h>
28 #include <asm/current.h>
29 #include <asm/uaccess.h>
33 int msg_ctlmax = MSGMAX;
34 int msg_ctlmnb = MSGMNB;
35 int msg_ctlmni = MSGMNI;
37 /* one msg_receiver structure for each sleeping receiver */
39 struct list_head r_list;
40 struct task_struct* r_tsk;
46 struct msg_msg* volatile r_msg;
49 /* one msg_sender for each sleeping sender */
51 struct list_head list;
52 struct task_struct* tsk;
56 #define SEARCH_EQUAL 2
57 #define SEARCH_NOTEQUAL 3
58 #define SEARCH_LESSEQUAL 4
60 static atomic_t msg_bytes = ATOMIC_INIT(0);
61 static atomic_t msg_hdrs = ATOMIC_INIT(0);
63 static struct ipc_ids msg_ids;
65 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
66 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
67 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
68 #define msg_checkid(msq, msgid) \
69 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
70 #define msg_buildid(id, seq) \
71 ipc_buildid(&msg_ids, id, seq)
73 static void freeque (struct msg_queue *msq, int id);
74 static int newque (key_t key, int msgflg);
76 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
79 void __init msg_init (void)
81 ipc_init_ids(&msg_ids,msg_ctlmni);
84 create_proc_read_entry("sysvipc/msg", 0, NULL, sysvipc_msg_read_proc, NULL);
88 static int newque (key_t key, int msgflg)
92 struct msg_queue *msq;
94 msq = ipc_rcu_alloc(sizeof(*msq));
98 msq->q_perm.mode = (msgflg & S_IRWXUGO);
99 msq->q_perm.key = key;
100 msq->q_perm.xid = vx_current_xid();
102 msq->q_perm.security = NULL;
103 retval = security_msg_queue_alloc(msq);
109 id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
111 security_msg_queue_free(msq);
116 msq->q_stime = msq->q_rtime = 0;
117 msq->q_ctime = get_seconds();
118 msq->q_cbytes = msq->q_qnum = 0;
119 msq->q_qbytes = msg_ctlmnb;
120 msq->q_lspid = msq->q_lrpid = 0;
121 INIT_LIST_HEAD(&msq->q_messages);
122 INIT_LIST_HEAD(&msq->q_receivers);
123 INIT_LIST_HEAD(&msq->q_senders);
126 return msg_buildid(id,msq->q_perm.seq);
129 static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
132 current->state=TASK_INTERRUPTIBLE;
133 list_add_tail(&mss->list,&msq->q_senders);
136 static inline void ss_del(struct msg_sender* mss)
138 if(mss->list.next != NULL)
139 list_del(&mss->list);
142 static void ss_wakeup(struct list_head* h, int kill)
144 struct list_head *tmp;
148 struct msg_sender* mss;
150 mss = list_entry(tmp,struct msg_sender,list);
154 wake_up_process(mss->tsk);
158 static void expunge_all(struct msg_queue* msq, int res)
160 struct list_head *tmp;
162 tmp = msq->q_receivers.next;
163 while (tmp != &msq->q_receivers) {
164 struct msg_receiver* msr;
166 msr = list_entry(tmp,struct msg_receiver,r_list);
169 wake_up_process(msr->r_tsk);
171 msr->r_msg = ERR_PTR(res);
175 * freeque() wakes up waiters on the sender and receiver waiting queue,
176 * removes the message queue from message queue ID
177 * array, and cleans up all the messages associated with this queue.
179 * msg_ids.sem and the spinlock for this message queue is hold
180 * before freeque() is called. msg_ids.sem remains locked on exit.
182 static void freeque (struct msg_queue *msq, int id)
184 struct list_head *tmp;
186 expunge_all(msq,-EIDRM);
187 ss_wakeup(&msq->q_senders,1);
191 tmp = msq->q_messages.next;
192 while(tmp != &msq->q_messages) {
193 struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
195 atomic_dec(&msg_hdrs);
198 atomic_sub(msq->q_cbytes, &msg_bytes);
199 security_msg_queue_free(msq);
203 asmlinkage long sys_msgget (key_t key, int msgflg)
205 int id, ret = -EPERM;
206 struct msg_queue *msq;
209 if (key == IPC_PRIVATE)
210 ret = newque(key, msgflg);
211 else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
212 if (!(msgflg & IPC_CREAT))
215 ret = newque(key, msgflg);
216 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
222 if (ipcperms(&msq->q_perm, msgflg))
225 int qid = msg_buildid(id, msq->q_perm.seq);
226 ret = security_msg_queue_associate(msq, msgflg);
236 static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
240 return copy_to_user (buf, in, sizeof(*in));
245 memset(&out,0,sizeof(out));
247 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
249 out.msg_stime = in->msg_stime;
250 out.msg_rtime = in->msg_rtime;
251 out.msg_ctime = in->msg_ctime;
253 if(in->msg_cbytes > USHRT_MAX)
254 out.msg_cbytes = USHRT_MAX;
256 out.msg_cbytes = in->msg_cbytes;
257 out.msg_lcbytes = in->msg_cbytes;
259 if(in->msg_qnum > USHRT_MAX)
260 out.msg_qnum = USHRT_MAX;
262 out.msg_qnum = in->msg_qnum;
264 if(in->msg_qbytes > USHRT_MAX)
265 out.msg_qbytes = USHRT_MAX;
267 out.msg_qbytes = in->msg_qbytes;
268 out.msg_lqbytes = in->msg_qbytes;
270 out.msg_lspid = in->msg_lspid;
271 out.msg_lrpid = in->msg_lrpid;
273 return copy_to_user (buf, &out, sizeof(out));
281 unsigned long qbytes;
287 static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
292 struct msqid64_ds tbuf;
294 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
297 out->qbytes = tbuf.msg_qbytes;
298 out->uid = tbuf.msg_perm.uid;
299 out->gid = tbuf.msg_perm.gid;
300 out->mode = tbuf.msg_perm.mode;
306 struct msqid_ds tbuf_old;
308 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
311 out->uid = tbuf_old.msg_perm.uid;
312 out->gid = tbuf_old.msg_perm.gid;
313 out->mode = tbuf_old.msg_perm.mode;
315 if(tbuf_old.msg_qbytes == 0)
316 out->qbytes = tbuf_old.msg_lqbytes;
318 out->qbytes = tbuf_old.msg_qbytes;
327 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
330 struct msg_queue *msq;
331 struct msq_setbuf setbuf;
332 struct kern_ipc_perm *ipcp;
334 if (msqid < 0 || cmd < 0)
337 version = ipc_parse_version(&cmd);
343 struct msginfo msginfo;
347 /* We must not return kernel stack data.
348 * due to padding, it's not enough
349 * to set all member fields.
352 err = security_msg_queue_msgctl(NULL, cmd);
356 memset(&msginfo,0,sizeof(msginfo));
357 msginfo.msgmni = msg_ctlmni;
358 msginfo.msgmax = msg_ctlmax;
359 msginfo.msgmnb = msg_ctlmnb;
360 msginfo.msgssz = MSGSSZ;
361 msginfo.msgseg = MSGSEG;
363 if (cmd == MSG_INFO) {
364 msginfo.msgpool = msg_ids.in_use;
365 msginfo.msgmap = atomic_read(&msg_hdrs);
366 msginfo.msgtql = atomic_read(&msg_bytes);
368 msginfo.msgmap = MSGMAP;
369 msginfo.msgpool = MSGPOOL;
370 msginfo.msgtql = MSGTQL;
372 max_id = msg_ids.max_id;
374 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
376 return (max_id < 0) ? 0: max_id;
381 struct msqid64_ds tbuf;
385 if(cmd == MSG_STAT && msqid >= msg_ids.entries->size)
388 memset(&tbuf,0,sizeof(tbuf));
390 msq = msg_lock(msqid);
394 if(cmd == MSG_STAT) {
395 success_return = msg_buildid(msqid, msq->q_perm.seq);
398 if (msg_checkid(msq,msqid))
403 if (ipcperms (&msq->q_perm, S_IRUGO))
406 err = security_msg_queue_msgctl(msq, cmd);
410 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
411 tbuf.msg_stime = msq->q_stime;
412 tbuf.msg_rtime = msq->q_rtime;
413 tbuf.msg_ctime = msq->q_ctime;
414 tbuf.msg_cbytes = msq->q_cbytes;
415 tbuf.msg_qnum = msq->q_qnum;
416 tbuf.msg_qbytes = msq->q_qbytes;
417 tbuf.msg_lspid = msq->q_lspid;
418 tbuf.msg_lrpid = msq->q_lrpid;
420 if (copy_msqid_to_user(buf, &tbuf, version))
422 return success_return;
427 if (copy_msqid_from_user (&setbuf, buf, version))
437 msq = msg_lock(msqid);
443 if (msg_checkid(msq,msqid))
447 if (current->euid != ipcp->cuid &&
448 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
449 /* We _could_ check for CAP_CHOWN above, but we don't */
452 err = security_msg_queue_msgctl(msq, cmd);
460 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
463 msq->q_qbytes = setbuf.qbytes;
465 ipcp->uid = setbuf.uid;
466 ipcp->gid = setbuf.gid;
467 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
468 (S_IRWXUGO & setbuf.mode);
469 msq->q_ctime = get_seconds();
470 /* sleeping receivers might be excluded by
471 * stricter permissions.
473 expunge_all(msq,-EAGAIN);
474 /* sleeping senders might be able to send
475 * due to a larger queue size.
477 ss_wakeup(&msq->q_senders,0);
482 freeque (msq, msqid);
497 static int testmsg(struct msg_msg* msg,long type,int mode)
503 case SEARCH_LESSEQUAL:
504 if(msg->m_type <=type)
508 if(msg->m_type == type)
511 case SEARCH_NOTEQUAL:
512 if(msg->m_type != type)
519 static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
521 struct list_head* tmp;
523 tmp = msq->q_receivers.next;
524 while (tmp != &msq->q_receivers) {
525 struct msg_receiver* msr;
526 msr = list_entry(tmp,struct msg_receiver,r_list);
528 if(testmsg(msg,msr->r_msgtype,msr->r_mode) &&
529 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
530 list_del(&msr->r_list);
531 if(msr->r_maxsize < msg->m_ts) {
533 wake_up_process(msr->r_tsk);
535 msr->r_msg = ERR_PTR(-E2BIG);
538 msq->q_lrpid = msr->r_tsk->pid;
539 msq->q_rtime = get_seconds();
540 wake_up_process(msr->r_tsk);
550 asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
552 struct msg_queue *msq;
557 if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
559 if (get_user(mtype, &msgp->mtype))
564 msg = load_msg(msgp->mtext, msgsz);
571 msq = msg_lock(msqid);
577 if (msg_checkid(msq,msqid))
578 goto out_unlock_free;
584 if (ipcperms(&msq->q_perm, S_IWUGO))
585 goto out_unlock_free;
587 err = security_msg_queue_msgsnd(msq, msg, msgflg);
589 goto out_unlock_free;
591 if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
592 1 + msq->q_qnum <= msq->q_qbytes) {
596 /* queue full, wait: */
597 if(msgflg&IPC_NOWAIT) {
599 goto out_unlock_free;
606 ipc_lock_by_ptr(&msq->q_perm);
608 if (msq->q_perm.deleted) {
610 goto out_unlock_free;
614 if (signal_pending(current)) {
616 goto out_unlock_free;
620 msq->q_lspid = current->tgid;
621 msq->q_stime = get_seconds();
623 if(!pipelined_send(msq,msg)) {
624 /* noone is waiting for this message, enqueue it */
625 list_add_tail(&msg->m_list,&msq->q_messages);
626 msq->q_cbytes += msgsz;
628 atomic_add(msgsz,&msg_bytes);
629 atomic_inc(&msg_hdrs);
643 static inline int convert_mode(long* msgtyp, int msgflg)
646 * find message of correct type.
647 * msgtyp = 0 => get first.
648 * msgtyp > 0 => get first message of matching type.
649 * msgtyp < 0 => get message with least type must be < abs(msgtype).
655 return SEARCH_LESSEQUAL;
657 if(msgflg & MSG_EXCEPT)
658 return SEARCH_NOTEQUAL;
662 asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
663 long msgtyp, int msgflg)
665 struct msg_queue *msq;
669 if (msqid < 0 || (long) msgsz < 0)
671 mode = convert_mode(&msgtyp,msgflg);
673 msq = msg_lock(msqid);
677 msg = ERR_PTR(-EIDRM);
678 if (msg_checkid(msq,msqid))
682 struct msg_receiver msr_d;
683 struct list_head* tmp;
685 msg = ERR_PTR(-EACCES);
686 if (ipcperms (&msq->q_perm, S_IRUGO))
689 msg = ERR_PTR(-EAGAIN);
690 tmp = msq->q_messages.next;
691 while (tmp != &msq->q_messages) {
692 struct msg_msg *walk_msg;
693 walk_msg = list_entry(tmp,struct msg_msg,m_list);
694 if(testmsg(walk_msg,msgtyp,mode) &&
695 !security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
697 if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
699 msgtyp=walk_msg->m_type-1;
708 /* Found a suitable message. Unlink it from the queue. */
709 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
710 msg = ERR_PTR(-E2BIG);
713 list_del(&msg->m_list);
715 msq->q_rtime = get_seconds();
716 msq->q_lrpid = current->tgid;
717 msq->q_cbytes -= msg->m_ts;
718 atomic_sub(msg->m_ts,&msg_bytes);
719 atomic_dec(&msg_hdrs);
720 ss_wakeup(&msq->q_senders,0);
724 /* No message waiting. Wait for a message */
725 if (msgflg & IPC_NOWAIT) {
726 msg = ERR_PTR(-ENOMSG);
729 list_add_tail(&msr_d.r_list,&msq->q_receivers);
730 msr_d.r_tsk = current;
731 msr_d.r_msgtype = msgtyp;
733 if(msgflg & MSG_NOERROR)
734 msr_d.r_maxsize = INT_MAX;
736 msr_d.r_maxsize = msgsz;
737 msr_d.r_msg = ERR_PTR(-EAGAIN);
738 current->state = TASK_INTERRUPTIBLE;
743 /* Lockless receive, part 1:
744 * Disable preemption. We don't hold a reference to the queue
745 * and getting a reference would defeat the idea of a lockless
746 * operation, thus the code relies on rcu to guarantee the
748 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
749 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
750 * rcu_read_lock() prevents preemption between reading r_msg
751 * and the spin_lock() inside ipc_lock_by_ptr().
755 /* Lockless receive, part 2:
756 * Wait until pipelined_send or expunge_all are outside of
757 * wake_up_process(). There is a race with exit(), see
758 * ipc/mqueue.c for the details.
760 msg = (struct msg_msg*) msr_d.r_msg;
761 while (msg == NULL) {
763 msg = (struct msg_msg*) msr_d.r_msg;
766 /* Lockless receive, part 3:
767 * If there is a message or an error then accept it without
770 if(msg != ERR_PTR(-EAGAIN)) {
775 /* Lockless receive, part 3:
776 * Acquire the queue spinlock.
778 ipc_lock_by_ptr(&msq->q_perm);
781 /* Lockless receive, part 4:
782 * Repeat test after acquiring the spinlock.
784 msg = (struct msg_msg*)msr_d.r_msg;
785 if(msg != ERR_PTR(-EAGAIN))
788 list_del(&msr_d.r_list);
789 if (signal_pending(current)) {
790 msg = ERR_PTR(-ERESTARTNOHAND);
799 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
800 if (put_user (msg->m_type, &msgp->mtype) ||
801 store_msg(msgp->mtext, msg, msgsz)) {
808 #ifdef CONFIG_PROC_FS
809 static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
816 len += sprintf(buffer, " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
818 for(i = 0; i <= msg_ids.max_id; i++) {
819 struct msg_queue * msq;
822 if (!vx_check(msq->q_perm.xid, VX_IDENT)) {
826 len += sprintf(buffer + len, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
828 msg_buildid(i,msq->q_perm.seq),
848 if(pos > offset + length)
856 *start = buffer + (offset - begin);
857 len -= (offset - begin);