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/malloc.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 <asm/uaccess.h>
29 int msg_ctlmax
= MSGMAX
;
30 int msg_ctlmnb
= MSGMNB
;
31 int msg_ctlmni
= MSGMNI
;
33 /* one msg_receiver structure for each sleeping receiver */
35 struct list_head r_list
;
36 struct task_struct
* r_tsk
;
42 struct msg_msg
* volatile r_msg
;
45 /* one msg_sender for each sleeping sender */
47 struct list_head list
;
48 struct task_struct
* tsk
;
52 struct msg_msgseg
* next
;
53 /* the next part of the message follows immediately */
55 /* one msg_msg structure for each message */
57 struct list_head m_list
;
59 int m_ts
; /* message text size */
60 struct msg_msgseg
* next
;
61 /* the actual message follows immediately */
64 #define DATALEN_MSG (PAGE_SIZE-sizeof(struct msg_msg))
65 #define DATALEN_SEG (PAGE_SIZE-sizeof(struct msg_msgseg))
67 /* one msq_queue structure for each present queue on the system */
69 struct kern_ipc_perm q_perm
;
70 time_t q_stime
; /* last msgsnd time */
71 time_t q_rtime
; /* last msgrcv time */
72 time_t q_ctime
; /* last change time */
73 unsigned long q_cbytes
; /* current number of bytes on queue */
74 unsigned long q_qnum
; /* number of messages in queue */
75 unsigned long q_qbytes
; /* max number of bytes on queue */
76 pid_t q_lspid
; /* pid of last msgsnd */
77 pid_t q_lrpid
; /* last receive pid */
79 struct list_head q_messages
;
80 struct list_head q_receivers
;
81 struct list_head q_senders
;
85 #define SEARCH_EQUAL 2
86 #define SEARCH_NOTEQUAL 3
87 #define SEARCH_LESSEQUAL 4
89 static atomic_t msg_bytes
= ATOMIC_INIT(0);
90 static atomic_t msg_hdrs
= ATOMIC_INIT(0);
92 static struct ipc_ids msg_ids
;
94 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
95 #define msg_unlock(id) ipc_unlock(&msg_ids,id)
96 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
97 #define msg_checkid(msq, msgid) \
98 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
99 #define msg_buildid(id, seq) \
100 ipc_buildid(&msg_ids, id, seq)
102 static void freeque (int id
);
103 static int newque (key_t key
, int msgflg
);
104 #ifdef CONFIG_PROC_FS
105 static int sysvipc_msg_read_proc(char *buffer
, char **start
, off_t offset
, int length
, int *eof
, void *data
);
108 void __init
msg_init (void)
110 ipc_init_ids(&msg_ids
,msg_ctlmni
);
112 #ifdef CONFIG_PROC_FS
113 create_proc_read_entry("sysvipc/msg", 0, 0, sysvipc_msg_read_proc
, NULL
);
117 static int newque (key_t key
, int msgflg
)
120 struct msg_queue
*msq
;
122 msq
= (struct msg_queue
*) kmalloc (sizeof (*msq
), GFP_KERNEL
);
125 id
= ipc_addid(&msg_ids
, &msq
->q_perm
, msg_ctlmni
);
130 msq
->q_perm
.mode
= (msgflg
& S_IRWXUGO
);
131 msq
->q_perm
.key
= key
;
133 msq
->q_stime
= msq
->q_rtime
= 0;
134 msq
->q_ctime
= CURRENT_TIME
;
135 msq
->q_cbytes
= msq
->q_qnum
= 0;
136 msq
->q_qbytes
= msg_ctlmnb
;
137 msq
->q_lspid
= msq
->q_lrpid
= 0;
138 INIT_LIST_HEAD(&msq
->q_messages
);
139 INIT_LIST_HEAD(&msq
->q_receivers
);
140 INIT_LIST_HEAD(&msq
->q_senders
);
143 return msg_buildid(id
,msq
->q_perm
.seq
);
146 static void free_msg(struct msg_msg
* msg
)
148 struct msg_msgseg
* seg
;
152 struct msg_msgseg
* tmp
= seg
->next
;
158 static struct msg_msg
* load_msg(void* src
, int len
)
161 struct msg_msgseg
** pseg
;
166 if(alen
> DATALEN_MSG
)
169 msg
= (struct msg_msg
*) kmalloc (sizeof(*msg
) + alen
, GFP_KERNEL
);
171 return ERR_PTR(-ENOMEM
);
175 if (copy_from_user(msg
+1, src
, alen
)) {
181 src
= ((char*)src
)+alen
;
184 struct msg_msgseg
* seg
;
186 if(alen
> DATALEN_SEG
)
188 seg
= (struct msg_msgseg
*) kmalloc (sizeof(*seg
) + alen
, GFP_KERNEL
);
195 if(copy_from_user (seg
+1, src
, alen
)) {
201 src
= ((char*)src
)+alen
;
210 static int store_msg(void* dest
, struct msg_msg
* msg
, int len
)
213 struct msg_msgseg
*seg
;
216 if(alen
> DATALEN_MSG
)
218 if(copy_to_user (dest
, msg
+1, alen
))
222 dest
= ((char*)dest
)+alen
;
226 if(alen
> DATALEN_SEG
)
228 if(copy_to_user (dest
, seg
+1, alen
))
231 dest
= ((char*)dest
)+alen
;
237 static inline void ss_add(struct msg_queue
* msq
, struct msg_sender
* mss
)
240 current
->state
=TASK_INTERRUPTIBLE
;
241 list_add_tail(&mss
->list
,&msq
->q_senders
);
244 static inline void ss_del(struct msg_sender
* mss
)
246 if(mss
->list
.next
!= NULL
)
247 list_del(&mss
->list
);
250 static void ss_wakeup(struct list_head
* h
, int kill
)
252 struct list_head
*tmp
;
256 struct msg_sender
* mss
;
258 mss
= list_entry(tmp
,struct msg_sender
,list
);
262 wake_up_process(mss
->tsk
);
266 static void expunge_all(struct msg_queue
* msq
, int res
)
268 struct list_head
*tmp
;
270 tmp
= msq
->q_receivers
.next
;
271 while (tmp
!= &msq
->q_receivers
) {
272 struct msg_receiver
* msr
;
274 msr
= list_entry(tmp
,struct msg_receiver
,r_list
);
276 msr
->r_msg
= ERR_PTR(res
);
277 wake_up_process(msr
->r_tsk
);
281 static void freeque (int id
)
283 struct msg_queue
*msq
;
284 struct list_head
*tmp
;
288 expunge_all(msq
,-EIDRM
);
289 ss_wakeup(&msq
->q_senders
,1);
292 tmp
= msq
->q_messages
.next
;
293 while(tmp
!= &msq
->q_messages
) {
294 struct msg_msg
* msg
= list_entry(tmp
,struct msg_msg
,m_list
);
296 atomic_dec(&msg_hdrs
);
299 atomic_sub(msq
->q_cbytes
, &msg_bytes
);
303 asmlinkage
long sys_msgget (key_t key
, int msgflg
)
305 int id
, ret
= -EPERM
;
306 struct msg_queue
*msq
;
309 if (key
== IPC_PRIVATE
)
310 ret
= newque(key
, msgflg
);
311 else if ((id
= ipc_findkey(&msg_ids
, key
)) == -1) { /* key not used */
312 if (!(msgflg
& IPC_CREAT
))
315 ret
= newque(key
, msgflg
);
316 } else if (msgflg
& IPC_CREAT
&& msgflg
& IPC_EXCL
) {
322 if (ipcperms(&msq
->q_perm
, msgflg
))
325 ret
= msg_buildid(id
, msq
->q_perm
.seq
);
332 static inline unsigned long copy_msqid_to_user(void *buf
, struct msqid64_ds
*in
, int version
)
336 return copy_to_user (buf
, in
, sizeof(*in
));
341 memset(&out
,0,sizeof(out
));
343 ipc64_perm_to_ipc_perm(&in
->msg_perm
, &out
.msg_perm
);
345 out
.msg_stime
= in
->msg_stime
;
346 out
.msg_rtime
= in
->msg_rtime
;
347 out
.msg_ctime
= in
->msg_ctime
;
349 if(in
->msg_cbytes
> USHRT_MAX
)
350 out
.msg_cbytes
= USHRT_MAX
;
352 out
.msg_cbytes
= in
->msg_cbytes
;
353 out
.msg_lcbytes
= in
->msg_cbytes
;
355 if(in
->msg_qnum
> USHRT_MAX
)
356 out
.msg_qnum
= USHRT_MAX
;
358 out
.msg_qnum
= in
->msg_qnum
;
360 if(in
->msg_qbytes
> USHRT_MAX
)
361 out
.msg_qbytes
= USHRT_MAX
;
363 out
.msg_qbytes
= in
->msg_qbytes
;
364 out
.msg_lqbytes
= in
->msg_qbytes
;
366 out
.msg_lspid
= in
->msg_lspid
;
367 out
.msg_lrpid
= in
->msg_lrpid
;
369 return copy_to_user (buf
, &out
, sizeof(out
));
377 unsigned long qbytes
;
383 static inline unsigned long copy_msqid_from_user(struct msq_setbuf
*out
, void *buf
, int version
)
388 struct msqid64_ds tbuf
;
390 if (copy_from_user (&tbuf
, buf
, sizeof (tbuf
)))
393 out
->qbytes
= tbuf
.msg_qbytes
;
394 out
->uid
= tbuf
.msg_perm
.uid
;
395 out
->gid
= tbuf
.msg_perm
.gid
;
396 out
->mode
= tbuf
.msg_perm
.mode
;
402 struct msqid_ds tbuf_old
;
404 if (copy_from_user (&tbuf_old
, buf
, sizeof (tbuf_old
)))
407 out
->uid
= tbuf_old
.msg_perm
.uid
;
408 out
->gid
= tbuf_old
.msg_perm
.gid
;
409 out
->mode
= tbuf_old
.msg_perm
.mode
;
411 if(tbuf_old
.msg_qbytes
== 0)
412 out
->qbytes
= tbuf_old
.msg_lqbytes
;
414 out
->qbytes
= tbuf_old
.msg_qbytes
;
423 asmlinkage
long sys_msgctl (int msqid
, int cmd
, struct msqid_ds
*buf
)
426 struct msg_queue
*msq
;
427 struct msq_setbuf setbuf
;
428 struct kern_ipc_perm
*ipcp
;
430 if (msqid
< 0 || cmd
< 0)
433 version
= ipc_parse_version(&cmd
);
439 struct msginfo msginfo
;
443 /* We must not return kernel stack data.
444 * due to padding, it's not enough
445 * to set all member fields.
447 memset(&msginfo
,0,sizeof(msginfo
));
448 msginfo
.msgmni
= msg_ctlmni
;
449 msginfo
.msgmax
= msg_ctlmax
;
450 msginfo
.msgmnb
= msg_ctlmnb
;
451 msginfo
.msgssz
= MSGSSZ
;
452 msginfo
.msgseg
= MSGSEG
;
454 if (cmd
== MSG_INFO
) {
455 msginfo
.msgpool
= msg_ids
.in_use
;
456 msginfo
.msgmap
= atomic_read(&msg_hdrs
);
457 msginfo
.msgtql
= atomic_read(&msg_bytes
);
459 msginfo
.msgmap
= MSGMAP
;
460 msginfo
.msgpool
= MSGPOOL
;
461 msginfo
.msgtql
= MSGTQL
;
463 max_id
= msg_ids
.max_id
;
465 if (copy_to_user (buf
, &msginfo
, sizeof(struct msginfo
)))
467 return (max_id
< 0) ? 0: max_id
;
472 struct msqid64_ds tbuf
;
476 if(cmd
== MSG_STAT
&& msqid
> msg_ids
.size
)
479 memset(&tbuf
,0,sizeof(tbuf
));
481 msq
= msg_lock(msqid
);
485 if(cmd
== MSG_STAT
) {
486 success_return
= msg_buildid(msqid
, msq
->q_perm
.seq
);
489 if (msg_checkid(msq
,msqid
))
494 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
497 kernel_to_ipc64_perm(&msq
->q_perm
, &tbuf
.msg_perm
);
498 tbuf
.msg_stime
= msq
->q_stime
;
499 tbuf
.msg_rtime
= msq
->q_rtime
;
500 tbuf
.msg_ctime
= msq
->q_ctime
;
501 tbuf
.msg_cbytes
= msq
->q_cbytes
;
502 tbuf
.msg_qnum
= msq
->q_qnum
;
503 tbuf
.msg_qbytes
= msq
->q_qbytes
;
504 tbuf
.msg_lspid
= msq
->q_lspid
;
505 tbuf
.msg_lrpid
= msq
->q_lrpid
;
507 if (copy_msqid_to_user(buf
, &tbuf
, version
))
509 return success_return
;
514 if (copy_msqid_from_user (&setbuf
, buf
, version
))
524 msq
= msg_lock(msqid
);
530 if (msg_checkid(msq
,msqid
))
534 if (current
->euid
!= ipcp
->cuid
&&
535 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
))
536 /* We _could_ check for CAP_CHOWN above, but we don't */
542 if (setbuf
.qbytes
> msg_ctlmnb
&& !capable(CAP_SYS_RESOURCE
))
544 msq
->q_qbytes
= setbuf
.qbytes
;
546 ipcp
->uid
= setbuf
.uid
;
547 ipcp
->gid
= setbuf
.gid
;
548 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
) |
549 (S_IRWXUGO
& setbuf
.mode
);
550 msq
->q_ctime
= CURRENT_TIME
;
551 /* sleeping receivers might be excluded by
552 * stricter permissions.
554 expunge_all(msq
,-EAGAIN
);
555 /* sleeping senders might be able to send
556 * due to a larger queue size.
558 ss_wakeup(&msq
->q_senders
,0);
578 static int testmsg(struct msg_msg
* msg
,long type
,int mode
)
584 case SEARCH_LESSEQUAL
:
585 if(msg
->m_type
<=type
)
589 if(msg
->m_type
== type
)
592 case SEARCH_NOTEQUAL
:
593 if(msg
->m_type
!= type
)
600 int inline pipelined_send(struct msg_queue
* msq
, struct msg_msg
* msg
)
602 struct list_head
* tmp
;
604 tmp
= msq
->q_receivers
.next
;
605 while (tmp
!= &msq
->q_receivers
) {
606 struct msg_receiver
* msr
;
607 msr
= list_entry(tmp
,struct msg_receiver
,r_list
);
609 if(testmsg(msg
,msr
->r_msgtype
,msr
->r_mode
)) {
610 list_del(&msr
->r_list
);
611 if(msr
->r_maxsize
< msg
->m_ts
) {
612 msr
->r_msg
= ERR_PTR(-E2BIG
);
613 wake_up_process(msr
->r_tsk
);
616 msq
->q_lspid
= msr
->r_tsk
->pid
;
617 msq
->q_rtime
= CURRENT_TIME
;
618 wake_up_process(msr
->r_tsk
);
626 asmlinkage
long sys_msgsnd (int msqid
, struct msgbuf
*msgp
, size_t msgsz
, int msgflg
)
628 struct msg_queue
*msq
;
633 if (msgsz
> msg_ctlmax
|| (long) msgsz
< 0 || msqid
< 0)
635 if (get_user(mtype
, &msgp
->mtype
))
640 msg
= load_msg(msgp
->mtext
, msgsz
);
647 msq
= msg_lock(msqid
);
653 if (msg_checkid(msq
,msqid
))
654 goto out_unlock_free
;
657 if (ipcperms(&msq
->q_perm
, S_IWUGO
))
658 goto out_unlock_free
;
660 if(msgsz
+ msq
->q_cbytes
> msq
->q_qbytes
||
661 1 + msq
->q_qnum
> msq
->q_qbytes
) {
664 if(msgflg
&IPC_NOWAIT
) {
666 goto out_unlock_free
;
671 current
->state
= TASK_RUNNING
;
673 msq
= msg_lock(msqid
);
679 if (signal_pending(current
)) {
681 goto out_unlock_free
;
686 if(!pipelined_send(msq
,msg
)) {
687 /* noone is waiting for this message, enqueue it */
688 list_add_tail(&msg
->m_list
,&msq
->q_messages
);
689 msq
->q_cbytes
+= msgsz
;
691 atomic_add(msgsz
,&msg_bytes
);
692 atomic_inc(&msg_hdrs
);
697 msq
->q_lspid
= current
->pid
;
698 msq
->q_stime
= CURRENT_TIME
;
708 int inline convert_mode(long* msgtyp
, int msgflg
)
711 * find message of correct type.
712 * msgtyp = 0 => get first.
713 * msgtyp > 0 => get first message of matching type.
714 * msgtyp < 0 => get message with least type must be < abs(msgtype).
720 return SEARCH_LESSEQUAL
;
722 if(msgflg
& MSG_EXCEPT
)
723 return SEARCH_NOTEQUAL
;
727 asmlinkage
long sys_msgrcv (int msqid
, struct msgbuf
*msgp
, size_t msgsz
,
728 long msgtyp
, int msgflg
)
730 struct msg_queue
*msq
;
731 struct msg_receiver msr_d
;
732 struct list_head
* tmp
;
733 struct msg_msg
* msg
, *found_msg
;
737 if (msqid
< 0 || (long) msgsz
< 0)
739 mode
= convert_mode(&msgtyp
,msgflg
);
741 msq
= msg_lock(msqid
);
746 if (ipcperms (&msq
->q_perm
, S_IRUGO
))
749 tmp
= msq
->q_messages
.next
;
751 while (tmp
!= &msq
->q_messages
) {
752 msg
= list_entry(tmp
,struct msg_msg
,m_list
);
753 if(testmsg(msg
,msgtyp
,mode
)) {
755 if(mode
== SEARCH_LESSEQUAL
&& msg
->m_type
!= 1) {
757 msgtyp
=msg
->m_type
-1;
767 if ((msgsz
< msg
->m_ts
) && !(msgflg
& MSG_NOERROR
)) {
771 list_del(&msg
->m_list
);
773 msq
->q_rtime
= CURRENT_TIME
;
774 msq
->q_lrpid
= current
->pid
;
775 msq
->q_cbytes
-= msg
->m_ts
;
776 atomic_sub(msg
->m_ts
,&msg_bytes
);
777 atomic_dec(&msg_hdrs
);
778 ss_wakeup(&msq
->q_senders
,0);
781 msgsz
= (msgsz
> msg
->m_ts
) ? msg
->m_ts
: msgsz
;
782 if (put_user (msg
->m_type
, &msgp
->mtype
) ||
783 store_msg(msgp
->mtext
, msg
, msgsz
)) {
791 /* no message waiting. Prepare for pipelined
794 if (msgflg
& IPC_NOWAIT
) {
798 list_add_tail(&msr_d
.r_list
,&msq
->q_receivers
);
799 msr_d
.r_tsk
= current
;
800 msr_d
.r_msgtype
= msgtyp
;
802 if(msgflg
& MSG_NOERROR
)
803 msr_d
.r_maxsize
= INT_MAX
;
805 msr_d
.r_maxsize
= msgsz
;
806 msr_d
.r_msg
= ERR_PTR(-EAGAIN
);
807 current
->state
= TASK_INTERRUPTIBLE
;
811 current
->state
= TASK_RUNNING
;
813 msg
= (struct msg_msg
*) msr_d
.r_msg
;
820 msg
= (struct msg_msg
*)msr_d
.r_msg
;
822 /* our message arived while we waited for
823 * the spinlock. Process it.
833 list_del(&msr_d
.r_list
);
834 if (signal_pending(current
))
846 #ifdef CONFIG_PROC_FS
847 static int sysvipc_msg_read_proc(char *buffer
, char **start
, off_t offset
, int length
, int *eof
, void *data
)
854 len
+= sprintf(buffer
, " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
856 for(i
= 0; i
<= msg_ids
.max_id
; i
++) {
857 struct msg_queue
* msq
;
860 len
+= sprintf(buffer
+ len
, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
862 msg_buildid(i
,msq
->q_perm
.seq
),
882 if(pos
> offset
+ length
)
890 *start
= buffer
+ (offset
- begin
);
891 len
-= (offset
- begin
);