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>
32 int msg_ctlmax
= MSGMAX
;
33 int msg_ctlmnb
= MSGMNB
;
34 int msg_ctlmni
= MSGMNI
;
36 /* one msg_receiver structure for each sleeping receiver */
38 struct list_head r_list
;
39 struct task_struct
* r_tsk
;
45 struct msg_msg
* volatile r_msg
;
48 /* one msg_sender for each sleeping sender */
50 struct list_head list
;
51 struct task_struct
* tsk
;
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
);
75 static int sysvipc_msg_read_proc(char *buffer
, char **start
, off_t offset
, int length
, int *eof
, void *data
);
78 void __init
msg_init (void)
80 ipc_init_ids(&msg_ids
,msg_ctlmni
);
83 create_proc_read_entry("sysvipc/msg", 0, NULL
, sysvipc_msg_read_proc
, NULL
);
87 static int newque (key_t key
, int msgflg
)
91 struct msg_queue
*msq
;
93 msq
= ipc_rcu_alloc(sizeof(*msq
));
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
);
107 id
= ipc_addid(&msg_ids
, &msq
->q_perm
, msg_ctlmni
);
109 security_msg_queue_free(msq
);
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
);
124 return msg_buildid(id
,msq
->q_perm
.seq
);
127 static inline void ss_add(struct msg_queue
* msq
, struct msg_sender
* mss
)
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
;
146 struct msg_sender
* mss
;
148 mss
= list_entry(tmp
,struct msg_sender
,list
);
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
);
167 wake_up_process(msr
->r_tsk
);
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);
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
);
193 atomic_dec(&msg_hdrs
);
196 atomic_sub(msq
->q_cbytes
, &msg_bytes
);
197 security_msg_queue_free(msq
);
201 asmlinkage
long sys_msgget (key_t key
, int msgflg
)
203 int id
, ret
= -EPERM
;
204 struct msg_queue
*msq
;
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
))
213 ret
= newque(key
, msgflg
);
214 } else if (msgflg
& IPC_CREAT
&& msgflg
& IPC_EXCL
) {
220 if (ipcperms(&msq
->q_perm
, msgflg
))
223 int qid
= msg_buildid(id
, msq
->q_perm
.seq
);
224 ret
= security_msg_queue_associate(msq
, msgflg
);
234 static inline unsigned long copy_msqid_to_user(void __user
*buf
, struct msqid64_ds
*in
, int version
)
238 return copy_to_user (buf
, in
, sizeof(*in
));
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
;
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
;
260 out
.msg_qnum
= in
->msg_qnum
;
262 if(in
->msg_qbytes
> USHRT_MAX
)
263 out
.msg_qbytes
= USHRT_MAX
;
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
));
279 unsigned long qbytes
;
285 static inline unsigned long copy_msqid_from_user(struct msq_setbuf
*out
, void __user
*buf
, int version
)
290 struct msqid64_ds tbuf
;
292 if (copy_from_user (&tbuf
, buf
, sizeof (tbuf
)))
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
;
304 struct msqid_ds tbuf_old
;
306 if (copy_from_user (&tbuf_old
, buf
, sizeof (tbuf_old
)))
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
;
316 out
->qbytes
= tbuf_old
.msg_qbytes
;
325 asmlinkage
long sys_msgctl (int msqid
, int cmd
, struct msqid_ds __user
*buf
)
328 struct msg_queue
*msq
;
329 struct msq_setbuf setbuf
;
330 struct kern_ipc_perm
*ipcp
;
332 if (msqid
< 0 || cmd
< 0)
335 version
= ipc_parse_version(&cmd
);
341 struct msginfo msginfo
;
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
);
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
;
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
);
366 msginfo
.msgmap
= MSGMAP
;
367 msginfo
.msgpool
= MSGPOOL
;
368 msginfo
.msgtql
= MSGTQL
;
370 max_id
= msg_ids
.max_id
;
372 if (copy_to_user (buf
, &msginfo
, sizeof(struct msginfo
)))
374 return (max_id
< 0) ? 0: max_id
;
379 struct msqid64_ds tbuf
;
383 if(cmd
== MSG_STAT
&& msqid
>= msg_ids
.size
)
386 memset(&tbuf
,0,sizeof(tbuf
));
388 msq
= msg_lock(msqid
);
392 if(cmd
== MSG_STAT
) {
393 success_return
= msg_buildid(msqid
, msq
->q_perm
.seq
);
396 if (msg_checkid(msq
,msqid
))
401 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
404 err
= security_msg_queue_msgctl(msq
, cmd
);
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
;
418 if (copy_msqid_to_user(buf
, &tbuf
, version
))
420 return success_return
;
425 if (copy_msqid_from_user (&setbuf
, buf
, version
))
435 msq
= msg_lock(msqid
);
441 if (msg_checkid(msq
,msqid
))
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 */
450 err
= security_msg_queue_msgctl(msq
, cmd
);
458 if (setbuf
.qbytes
> msg_ctlmnb
&& !capable(CAP_SYS_RESOURCE
))
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);
480 freeque (msq
, msqid
);
495 static int testmsg(struct msg_msg
* msg
,long type
,int mode
)
501 case SEARCH_LESSEQUAL
:
502 if(msg
->m_type
<=type
)
506 if(msg
->m_type
== type
)
509 case SEARCH_NOTEQUAL
:
510 if(msg
->m_type
!= type
)
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
);
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
) {
531 wake_up_process(msr
->r_tsk
);
533 msr
->r_msg
= ERR_PTR(-E2BIG
);
536 msq
->q_lrpid
= msr
->r_tsk
->pid
;
537 msq
->q_rtime
= get_seconds();
538 wake_up_process(msr
->r_tsk
);
548 asmlinkage
long sys_msgsnd (int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
, int msgflg
)
550 struct msg_queue
*msq
;
555 if (msgsz
> msg_ctlmax
|| (long) msgsz
< 0 || msqid
< 0)
557 if (get_user(mtype
, &msgp
->mtype
))
562 msg
= load_msg(msgp
->mtext
, msgsz
);
569 msq
= msg_lock(msqid
);
575 if (msg_checkid(msq
,msqid
))
576 goto out_unlock_free
;
582 if (ipcperms(&msq
->q_perm
, S_IWUGO
))
583 goto out_unlock_free
;
585 err
= security_msg_queue_msgsnd(msq
, msg
, msgflg
);
587 goto out_unlock_free
;
589 if(msgsz
+ msq
->q_cbytes
<= msq
->q_qbytes
&&
590 1 + msq
->q_qnum
<= msq
->q_qbytes
) {
594 /* queue full, wait: */
595 if(msgflg
&IPC_NOWAIT
) {
597 goto out_unlock_free
;
604 ipc_lock_by_ptr(&msq
->q_perm
);
606 if (msq
->q_perm
.deleted
) {
608 goto out_unlock_free
;
612 if (signal_pending(current
)) {
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
;
626 atomic_add(msgsz
,&msg_bytes
);
627 atomic_inc(&msg_hdrs
);
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).
653 return SEARCH_LESSEQUAL
;
655 if(msgflg
& MSG_EXCEPT
)
656 return SEARCH_NOTEQUAL
;
660 asmlinkage
long sys_msgrcv (int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
,
661 long msgtyp
, int msgflg
)
663 struct msg_queue
*msq
;
667 if (msqid
< 0 || (long) msgsz
< 0)
669 mode
= convert_mode(&msgtyp
,msgflg
);
671 msq
= msg_lock(msqid
);
675 msg
= ERR_PTR(-EIDRM
);
676 if (msg_checkid(msq
,msqid
))
680 struct msg_receiver msr_d
;
681 struct list_head
* tmp
;
683 msg
= ERR_PTR(-EACCES
);
684 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
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
)) {
695 if(mode
== SEARCH_LESSEQUAL
&& walk_msg
->m_type
!= 1) {
697 msgtyp
=walk_msg
->m_type
-1;
706 /* Found a suitable message. Unlink it from the queue. */
707 if ((msgsz
< msg
->m_ts
) && !(msgflg
& MSG_NOERROR
)) {
708 msg
= ERR_PTR(-E2BIG
);
711 list_del(&msg
->m_list
);
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);
722 /* No message waiting. Wait for a message */
723 if (msgflg
& IPC_NOWAIT
) {
724 msg
= ERR_PTR(-ENOMSG
);
727 list_add_tail(&msr_d
.r_list
,&msq
->q_receivers
);
728 msr_d
.r_tsk
= current
;
729 msr_d
.r_msgtype
= msgtyp
;
731 if(msgflg
& MSG_NOERROR
)
732 msr_d
.r_maxsize
= INT_MAX
;
734 msr_d
.r_maxsize
= msgsz
;
735 msr_d
.r_msg
= ERR_PTR(-EAGAIN
);
736 current
->state
= TASK_INTERRUPTIBLE
;
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
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().
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
) {
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
768 if(msg
!= ERR_PTR(-EAGAIN
)) {
773 /* Lockless receive, part 3:
774 * Acquire the queue spinlock.
776 ipc_lock_by_ptr(&msq
->q_perm
);
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
))
786 list_del(&msr_d
.r_list
);
787 if (signal_pending(current
)) {
788 msg
= ERR_PTR(-ERESTARTNOHAND
);
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
)) {
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
)
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
;
820 len
+= sprintf(buffer
+ len
, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
822 msg_buildid(i
,msq
->q_perm
.seq
),
842 if(pos
> offset
+ length
)
850 *start
= buffer
+ (offset
- begin
);
851 len
-= (offset
- begin
);