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 <manfred@colorfullife.com>
18 #include <linux/capability.h>
19 #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>
30 #include <linux/seq_file.h>
31 #include <asm/current.h>
32 #include <asm/uaccess.h>
36 int msg_ctlmax
= MSGMAX
;
37 int msg_ctlmnb
= MSGMNB
;
38 int msg_ctlmni
= MSGMNI
;
40 /* one msg_receiver structure for each sleeping receiver */
42 struct list_head r_list
;
43 struct task_struct
* r_tsk
;
49 struct msg_msg
* volatile r_msg
;
52 /* one msg_sender for each sleeping sender */
54 struct list_head list
;
55 struct task_struct
* tsk
;
59 #define SEARCH_EQUAL 2
60 #define SEARCH_NOTEQUAL 3
61 #define SEARCH_LESSEQUAL 4
63 static atomic_t msg_bytes
= ATOMIC_INIT(0);
64 static atomic_t msg_hdrs
= ATOMIC_INIT(0);
66 static struct ipc_ids msg_ids
;
68 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
69 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
70 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
71 #define msg_checkid(msq, msgid) \
72 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
73 #define msg_buildid(id, seq) \
74 ipc_buildid(&msg_ids, id, seq)
76 static void freeque (struct msg_queue
*msq
, int id
);
77 static int newque (key_t key
, int msgflg
);
79 static int sysvipc_msg_proc_show(struct seq_file
*s
, void *it
);
82 void __init
msg_init (void)
84 ipc_init_ids(&msg_ids
,msg_ctlmni
);
85 ipc_init_proc_interface("sysvipc/msg",
86 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
88 sysvipc_msg_proc_show
);
91 static int newque (key_t key
, int msgflg
)
95 struct msg_queue
*msq
;
97 msq
= ipc_rcu_alloc(sizeof(*msq
));
101 msq
->q_perm
.mode
= (msgflg
& S_IRWXUGO
);
102 msq
->q_perm
.key
= key
;
104 msq
->q_perm
.security
= NULL
;
105 retval
= security_msg_queue_alloc(msq
);
111 id
= ipc_addid(&msg_ids
, &msq
->q_perm
, msg_ctlmni
);
113 security_msg_queue_free(msq
);
118 msq
->q_id
= msg_buildid(id
,msq
->q_perm
.seq
);
119 msq
->q_stime
= msq
->q_rtime
= 0;
120 msq
->q_ctime
= get_seconds();
121 msq
->q_cbytes
= msq
->q_qnum
= 0;
122 msq
->q_qbytes
= msg_ctlmnb
;
123 msq
->q_lspid
= msq
->q_lrpid
= 0;
124 INIT_LIST_HEAD(&msq
->q_messages
);
125 INIT_LIST_HEAD(&msq
->q_receivers
);
126 INIT_LIST_HEAD(&msq
->q_senders
);
132 static inline void ss_add(struct msg_queue
* msq
, struct msg_sender
* mss
)
135 current
->state
=TASK_INTERRUPTIBLE
;
136 list_add_tail(&mss
->list
,&msq
->q_senders
);
139 static inline void ss_del(struct msg_sender
* mss
)
141 if(mss
->list
.next
!= NULL
)
142 list_del(&mss
->list
);
145 static void ss_wakeup(struct list_head
* h
, int kill
)
147 struct list_head
*tmp
;
151 struct msg_sender
* mss
;
153 mss
= list_entry(tmp
,struct msg_sender
,list
);
157 wake_up_process(mss
->tsk
);
161 static void expunge_all(struct msg_queue
* msq
, int res
)
163 struct list_head
*tmp
;
165 tmp
= msq
->q_receivers
.next
;
166 while (tmp
!= &msq
->q_receivers
) {
167 struct msg_receiver
* msr
;
169 msr
= list_entry(tmp
,struct msg_receiver
,r_list
);
172 wake_up_process(msr
->r_tsk
);
174 msr
->r_msg
= ERR_PTR(res
);
178 * freeque() wakes up waiters on the sender and receiver waiting queue,
179 * removes the message queue from message queue ID
180 * array, and cleans up all the messages associated with this queue.
182 * msg_ids.sem and the spinlock for this message queue is hold
183 * before freeque() is called. msg_ids.sem remains locked on exit.
185 static void freeque (struct msg_queue
*msq
, int id
)
187 struct list_head
*tmp
;
189 expunge_all(msq
,-EIDRM
);
190 ss_wakeup(&msq
->q_senders
,1);
194 tmp
= msq
->q_messages
.next
;
195 while(tmp
!= &msq
->q_messages
) {
196 struct msg_msg
* msg
= list_entry(tmp
,struct msg_msg
,m_list
);
198 atomic_dec(&msg_hdrs
);
201 atomic_sub(msq
->q_cbytes
, &msg_bytes
);
202 security_msg_queue_free(msq
);
206 asmlinkage
long sys_msgget (key_t key
, int msgflg
)
208 int id
, ret
= -EPERM
;
209 struct msg_queue
*msq
;
212 if (key
== IPC_PRIVATE
)
213 ret
= newque(key
, msgflg
);
214 else if ((id
= ipc_findkey(&msg_ids
, key
)) == -1) { /* key not used */
215 if (!(msgflg
& IPC_CREAT
))
218 ret
= newque(key
, msgflg
);
219 } else if (msgflg
& IPC_CREAT
&& msgflg
& IPC_EXCL
) {
225 if (ipcperms(&msq
->q_perm
, msgflg
))
228 int qid
= msg_buildid(id
, msq
->q_perm
.seq
);
229 ret
= security_msg_queue_associate(msq
, msgflg
);
239 static inline unsigned long copy_msqid_to_user(void __user
*buf
, struct msqid64_ds
*in
, int version
)
243 return copy_to_user (buf
, in
, sizeof(*in
));
248 memset(&out
,0,sizeof(out
));
250 ipc64_perm_to_ipc_perm(&in
->msg_perm
, &out
.msg_perm
);
252 out
.msg_stime
= in
->msg_stime
;
253 out
.msg_rtime
= in
->msg_rtime
;
254 out
.msg_ctime
= in
->msg_ctime
;
256 if(in
->msg_cbytes
> USHRT_MAX
)
257 out
.msg_cbytes
= USHRT_MAX
;
259 out
.msg_cbytes
= in
->msg_cbytes
;
260 out
.msg_lcbytes
= in
->msg_cbytes
;
262 if(in
->msg_qnum
> USHRT_MAX
)
263 out
.msg_qnum
= USHRT_MAX
;
265 out
.msg_qnum
= in
->msg_qnum
;
267 if(in
->msg_qbytes
> USHRT_MAX
)
268 out
.msg_qbytes
= USHRT_MAX
;
270 out
.msg_qbytes
= in
->msg_qbytes
;
271 out
.msg_lqbytes
= in
->msg_qbytes
;
273 out
.msg_lspid
= in
->msg_lspid
;
274 out
.msg_lrpid
= in
->msg_lrpid
;
276 return copy_to_user (buf
, &out
, sizeof(out
));
284 unsigned long qbytes
;
290 static inline unsigned long copy_msqid_from_user(struct msq_setbuf
*out
, void __user
*buf
, int version
)
295 struct msqid64_ds tbuf
;
297 if (copy_from_user (&tbuf
, buf
, sizeof (tbuf
)))
300 out
->qbytes
= tbuf
.msg_qbytes
;
301 out
->uid
= tbuf
.msg_perm
.uid
;
302 out
->gid
= tbuf
.msg_perm
.gid
;
303 out
->mode
= tbuf
.msg_perm
.mode
;
309 struct msqid_ds tbuf_old
;
311 if (copy_from_user (&tbuf_old
, buf
, sizeof (tbuf_old
)))
314 out
->uid
= tbuf_old
.msg_perm
.uid
;
315 out
->gid
= tbuf_old
.msg_perm
.gid
;
316 out
->mode
= tbuf_old
.msg_perm
.mode
;
318 if(tbuf_old
.msg_qbytes
== 0)
319 out
->qbytes
= tbuf_old
.msg_lqbytes
;
321 out
->qbytes
= tbuf_old
.msg_qbytes
;
330 asmlinkage
long sys_msgctl (int msqid
, int cmd
, struct msqid_ds __user
*buf
)
333 struct msg_queue
*msq
;
334 struct msq_setbuf setbuf
;
335 struct kern_ipc_perm
*ipcp
;
337 if (msqid
< 0 || cmd
< 0)
340 version
= ipc_parse_version(&cmd
);
346 struct msginfo msginfo
;
350 /* We must not return kernel stack data.
351 * due to padding, it's not enough
352 * to set all member fields.
355 err
= security_msg_queue_msgctl(NULL
, cmd
);
359 memset(&msginfo
,0,sizeof(msginfo
));
360 msginfo
.msgmni
= msg_ctlmni
;
361 msginfo
.msgmax
= msg_ctlmax
;
362 msginfo
.msgmnb
= msg_ctlmnb
;
363 msginfo
.msgssz
= MSGSSZ
;
364 msginfo
.msgseg
= MSGSEG
;
366 if (cmd
== MSG_INFO
) {
367 msginfo
.msgpool
= msg_ids
.in_use
;
368 msginfo
.msgmap
= atomic_read(&msg_hdrs
);
369 msginfo
.msgtql
= atomic_read(&msg_bytes
);
371 msginfo
.msgmap
= MSGMAP
;
372 msginfo
.msgpool
= MSGPOOL
;
373 msginfo
.msgtql
= MSGTQL
;
375 max_id
= msg_ids
.max_id
;
377 if (copy_to_user (buf
, &msginfo
, sizeof(struct msginfo
)))
379 return (max_id
< 0) ? 0: max_id
;
384 struct msqid64_ds tbuf
;
388 if(cmd
== MSG_STAT
&& msqid
>= msg_ids
.entries
->size
)
391 memset(&tbuf
,0,sizeof(tbuf
));
393 msq
= msg_lock(msqid
);
397 if(cmd
== MSG_STAT
) {
398 success_return
= msg_buildid(msqid
, msq
->q_perm
.seq
);
401 if (msg_checkid(msq
,msqid
))
406 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
409 err
= security_msg_queue_msgctl(msq
, cmd
);
413 kernel_to_ipc64_perm(&msq
->q_perm
, &tbuf
.msg_perm
);
414 tbuf
.msg_stime
= msq
->q_stime
;
415 tbuf
.msg_rtime
= msq
->q_rtime
;
416 tbuf
.msg_ctime
= msq
->q_ctime
;
417 tbuf
.msg_cbytes
= msq
->q_cbytes
;
418 tbuf
.msg_qnum
= msq
->q_qnum
;
419 tbuf
.msg_qbytes
= msq
->q_qbytes
;
420 tbuf
.msg_lspid
= msq
->q_lspid
;
421 tbuf
.msg_lrpid
= msq
->q_lrpid
;
423 if (copy_msqid_to_user(buf
, &tbuf
, version
))
425 return success_return
;
430 if (copy_msqid_from_user (&setbuf
, buf
, version
))
432 if ((err
= audit_ipc_perms(setbuf
.qbytes
, setbuf
.uid
, setbuf
.gid
, setbuf
.mode
)))
442 msq
= msg_lock(msqid
);
448 if (msg_checkid(msq
,msqid
))
452 if (current
->euid
!= ipcp
->cuid
&&
453 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
))
454 /* We _could_ check for CAP_CHOWN above, but we don't */
457 err
= security_msg_queue_msgctl(msq
, cmd
);
465 if (setbuf
.qbytes
> msg_ctlmnb
&& !capable(CAP_SYS_RESOURCE
))
468 msq
->q_qbytes
= setbuf
.qbytes
;
470 ipcp
->uid
= setbuf
.uid
;
471 ipcp
->gid
= setbuf
.gid
;
472 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
) |
473 (S_IRWXUGO
& setbuf
.mode
);
474 msq
->q_ctime
= get_seconds();
475 /* sleeping receivers might be excluded by
476 * stricter permissions.
478 expunge_all(msq
,-EAGAIN
);
479 /* sleeping senders might be able to send
480 * due to a larger queue size.
482 ss_wakeup(&msq
->q_senders
,0);
487 freeque (msq
, msqid
);
502 static int testmsg(struct msg_msg
* msg
,long type
,int mode
)
508 case SEARCH_LESSEQUAL
:
509 if(msg
->m_type
<=type
)
513 if(msg
->m_type
== type
)
516 case SEARCH_NOTEQUAL
:
517 if(msg
->m_type
!= type
)
524 static inline int pipelined_send(struct msg_queue
* msq
, struct msg_msg
* msg
)
526 struct list_head
* tmp
;
528 tmp
= msq
->q_receivers
.next
;
529 while (tmp
!= &msq
->q_receivers
) {
530 struct msg_receiver
* msr
;
531 msr
= list_entry(tmp
,struct msg_receiver
,r_list
);
533 if(testmsg(msg
,msr
->r_msgtype
,msr
->r_mode
) &&
534 !security_msg_queue_msgrcv(msq
, msg
, msr
->r_tsk
, msr
->r_msgtype
, msr
->r_mode
)) {
535 list_del(&msr
->r_list
);
536 if(msr
->r_maxsize
< msg
->m_ts
) {
538 wake_up_process(msr
->r_tsk
);
540 msr
->r_msg
= ERR_PTR(-E2BIG
);
543 msq
->q_lrpid
= msr
->r_tsk
->pid
;
544 msq
->q_rtime
= get_seconds();
545 wake_up_process(msr
->r_tsk
);
555 asmlinkage
long sys_msgsnd (int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
, int msgflg
)
557 struct msg_queue
*msq
;
562 if (msgsz
> msg_ctlmax
|| (long) msgsz
< 0 || msqid
< 0)
564 if (get_user(mtype
, &msgp
->mtype
))
569 msg
= load_msg(msgp
->mtext
, msgsz
);
576 msq
= msg_lock(msqid
);
582 if (msg_checkid(msq
,msqid
))
583 goto out_unlock_free
;
589 if (ipcperms(&msq
->q_perm
, S_IWUGO
))
590 goto out_unlock_free
;
592 err
= security_msg_queue_msgsnd(msq
, msg
, msgflg
);
594 goto out_unlock_free
;
596 if(msgsz
+ msq
->q_cbytes
<= msq
->q_qbytes
&&
597 1 + msq
->q_qnum
<= msq
->q_qbytes
) {
601 /* queue full, wait: */
602 if(msgflg
&IPC_NOWAIT
) {
604 goto out_unlock_free
;
611 ipc_lock_by_ptr(&msq
->q_perm
);
613 if (msq
->q_perm
.deleted
) {
615 goto out_unlock_free
;
619 if (signal_pending(current
)) {
621 goto out_unlock_free
;
625 msq
->q_lspid
= current
->tgid
;
626 msq
->q_stime
= get_seconds();
628 if(!pipelined_send(msq
,msg
)) {
629 /* noone is waiting for this message, enqueue it */
630 list_add_tail(&msg
->m_list
,&msq
->q_messages
);
631 msq
->q_cbytes
+= msgsz
;
633 atomic_add(msgsz
,&msg_bytes
);
634 atomic_inc(&msg_hdrs
);
648 static inline int convert_mode(long* msgtyp
, int msgflg
)
651 * find message of correct type.
652 * msgtyp = 0 => get first.
653 * msgtyp > 0 => get first message of matching type.
654 * msgtyp < 0 => get message with least type must be < abs(msgtype).
660 return SEARCH_LESSEQUAL
;
662 if(msgflg
& MSG_EXCEPT
)
663 return SEARCH_NOTEQUAL
;
667 asmlinkage
long sys_msgrcv (int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
,
668 long msgtyp
, int msgflg
)
670 struct msg_queue
*msq
;
674 if (msqid
< 0 || (long) msgsz
< 0)
676 mode
= convert_mode(&msgtyp
,msgflg
);
678 msq
= msg_lock(msqid
);
682 msg
= ERR_PTR(-EIDRM
);
683 if (msg_checkid(msq
,msqid
))
687 struct msg_receiver msr_d
;
688 struct list_head
* tmp
;
690 msg
= ERR_PTR(-EACCES
);
691 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
694 msg
= ERR_PTR(-EAGAIN
);
695 tmp
= msq
->q_messages
.next
;
696 while (tmp
!= &msq
->q_messages
) {
697 struct msg_msg
*walk_msg
;
698 walk_msg
= list_entry(tmp
,struct msg_msg
,m_list
);
699 if(testmsg(walk_msg
,msgtyp
,mode
) &&
700 !security_msg_queue_msgrcv(msq
, walk_msg
, current
, msgtyp
, mode
)) {
702 if(mode
== SEARCH_LESSEQUAL
&& walk_msg
->m_type
!= 1) {
704 msgtyp
=walk_msg
->m_type
-1;
713 /* Found a suitable message. Unlink it from the queue. */
714 if ((msgsz
< msg
->m_ts
) && !(msgflg
& MSG_NOERROR
)) {
715 msg
= ERR_PTR(-E2BIG
);
718 list_del(&msg
->m_list
);
720 msq
->q_rtime
= get_seconds();
721 msq
->q_lrpid
= current
->tgid
;
722 msq
->q_cbytes
-= msg
->m_ts
;
723 atomic_sub(msg
->m_ts
,&msg_bytes
);
724 atomic_dec(&msg_hdrs
);
725 ss_wakeup(&msq
->q_senders
,0);
729 /* No message waiting. Wait for a message */
730 if (msgflg
& IPC_NOWAIT
) {
731 msg
= ERR_PTR(-ENOMSG
);
734 list_add_tail(&msr_d
.r_list
,&msq
->q_receivers
);
735 msr_d
.r_tsk
= current
;
736 msr_d
.r_msgtype
= msgtyp
;
738 if(msgflg
& MSG_NOERROR
)
739 msr_d
.r_maxsize
= INT_MAX
;
741 msr_d
.r_maxsize
= msgsz
;
742 msr_d
.r_msg
= ERR_PTR(-EAGAIN
);
743 current
->state
= TASK_INTERRUPTIBLE
;
748 /* Lockless receive, part 1:
749 * Disable preemption. We don't hold a reference to the queue
750 * and getting a reference would defeat the idea of a lockless
751 * operation, thus the code relies on rcu to guarantee the
753 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
754 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
755 * rcu_read_lock() prevents preemption between reading r_msg
756 * and the spin_lock() inside ipc_lock_by_ptr().
760 /* Lockless receive, part 2:
761 * Wait until pipelined_send or expunge_all are outside of
762 * wake_up_process(). There is a race with exit(), see
763 * ipc/mqueue.c for the details.
765 msg
= (struct msg_msg
*) msr_d
.r_msg
;
766 while (msg
== NULL
) {
768 msg
= (struct msg_msg
*) msr_d
.r_msg
;
771 /* Lockless receive, part 3:
772 * If there is a message or an error then accept it without
775 if(msg
!= ERR_PTR(-EAGAIN
)) {
780 /* Lockless receive, part 3:
781 * Acquire the queue spinlock.
783 ipc_lock_by_ptr(&msq
->q_perm
);
786 /* Lockless receive, part 4:
787 * Repeat test after acquiring the spinlock.
789 msg
= (struct msg_msg
*)msr_d
.r_msg
;
790 if(msg
!= ERR_PTR(-EAGAIN
))
793 list_del(&msr_d
.r_list
);
794 if (signal_pending(current
)) {
795 msg
= ERR_PTR(-ERESTARTNOHAND
);
804 msgsz
= (msgsz
> msg
->m_ts
) ? msg
->m_ts
: msgsz
;
805 if (put_user (msg
->m_type
, &msgp
->mtype
) ||
806 store_msg(msgp
->mtext
, msg
, msgsz
)) {
813 #ifdef CONFIG_PROC_FS
814 static int sysvipc_msg_proc_show(struct seq_file
*s
, void *it
)
816 struct msg_queue
*msq
= it
;
819 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",