1 /* $FreeBSD: src/sys/kern/sysv_msg.c,v 1.23.2.5 2002/12/31 08:54:53 maxim Exp $ */
4 * Implementation of SVID messages
6 * Author: Daniel Boulet
8 * Copyright 1993 Daniel Boulet and RTMX Inc.
10 * This system call was implemented by Daniel Boulet under contract from RTMX.
12 * Redistribution and use in source forms, with and without modification,
13 * are permitted provided that this entire comment appears intact.
15 * Redistribution in binary form may occur without any restrictions.
16 * Obviously, it would be nice if you gave credit where credit is due
17 * but requiring it would be too onerous.
19 * This software is provided ``AS IS'' without any warranties of any kind.
22 #include "opt_sysvipc.h"
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/sysproto.h>
27 #include <sys/kernel.h>
31 #include <sys/sysent.h>
32 #include <sys/sysctl.h>
33 #include <sys/malloc.h>
36 #include <sys/mplock2.h>
38 static MALLOC_DEFINE(M_MSG
, "msg", "SVID compatible message queues");
40 static void msginit (void *);
45 static void msg_freehdr (struct msg
*msghdr
);
48 struct msg
*msg_next
; /* next msg in the chain */
49 long msg_type
; /* type of this message */
50 /* >0 -> type of this message */
51 /* 0 -> free header */
52 u_short msg_ts
; /* size of this message */
53 short msg_spot
; /* location of start of msg in buffer */
58 #define MSGSSZ 8 /* Each segment must be 2^N long */
61 #define MSGSEG 2048 /* must be less than 32767 */
63 #define MSGMAX (MSGSSZ*MSGSEG)
65 #define MSGMNB 2048 /* max # of bytes in a queue */
75 * Based on the configuration parameters described in an SVR2 (yes, two)
76 * config(1m) man page.
78 * Each message is broken up and stored in segments that are msgssz bytes
79 * long. For efficiency reasons, this should be a power of two. Also,
80 * it doesn't make sense if it is less than 8 or greater than about 256.
81 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
82 * two between 8 and 1024 inclusive (and panic's if it isn't).
84 struct msginfo msginfo
= {
85 MSGMAX
, /* max chars in a message */
86 MSGMNI
, /* # of message queue identifiers */
87 MSGMNB
, /* max chars in a queue */
88 MSGTQL
, /* max messages in system */
89 MSGSSZ
, /* size of a message segment */
90 /* (must be small power of 2 greater than 4) */
91 MSGSEG
/* number of message segments */
95 * macros to convert between msqid_ds's and msqid's.
96 * (specific to this implementation)
98 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
99 #define MSQID_IX(id) ((id) & 0xffff)
100 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
103 * The rest of this file is specific to this particular implementation.
107 short next
; /* next segment in buffer */
108 /* -1 -> available */
109 /* 0..(MSGSEG-1) -> index of next segment */
112 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
114 static int nfree_msgmaps
; /* # of free map entries */
115 static short free_msgmaps
; /* head of linked list of free map entries */
116 static struct msg
*free_msghdrs
;/* list of free msg headers */
117 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
118 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
119 static struct msg
*msghdrs
; /* MSGTQL msg headers */
120 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
127 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
128 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
129 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
130 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
131 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
134 * msginfo.msgssz should be a power of two for efficiency reasons.
135 * It is also pretty silly if msginfo.msgssz is less than 8
136 * or greater than about 256 so ...
140 while (i
< 1024 && i
!= msginfo
.msgssz
)
142 if (i
!= msginfo
.msgssz
) {
143 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
145 panic("msginfo.msgssz not a small power of 2");
148 if (msginfo
.msgseg
> 32767) {
149 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
150 panic("msginfo.msgseg > 32767");
153 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
155 msgmaps
[i
-1].next
= i
;
156 msgmaps
[i
].next
= -1; /* implies entry is available */
159 nfree_msgmaps
= msginfo
.msgseg
;
161 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
162 msghdrs
[i
].msg_type
= 0;
164 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
165 msghdrs
[i
].msg_next
= NULL
;
167 free_msghdrs
= &msghdrs
[0];
169 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
170 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
171 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
172 msqids
[i
].msg_perm
.mode
= 0;
175 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
);
178 msg_freehdr(struct msg
*msghdr
)
180 while (msghdr
->msg_ts
> 0) {
182 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
183 panic("msghdr->msg_spot out of range");
184 next
= msgmaps
[msghdr
->msg_spot
].next
;
185 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
186 free_msgmaps
= msghdr
->msg_spot
;
188 msghdr
->msg_spot
= next
;
189 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
190 msghdr
->msg_ts
-= msginfo
.msgssz
;
194 if (msghdr
->msg_spot
!= -1)
195 panic("msghdr->msg_spot != -1");
196 msghdr
->msg_next
= free_msghdrs
;
197 free_msghdrs
= msghdr
;
204 sys_msgctl(struct msgctl_args
*uap
)
206 struct thread
*td
= curthread
;
207 struct proc
*p
= td
->td_proc
;
208 int msqid
= uap
->msqid
;
210 struct msqid_ds
*user_msqptr
= uap
->buf
;
212 struct msqid_ds msqbuf
;
213 struct msqid_ds
*msqptr
;
216 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
219 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
223 msqid
= IPCID_TO_IX(msqid
);
225 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
227 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
234 msqptr
= &msqids
[msqid
];
236 if (msqptr
->msg_qbytes
== 0) {
238 kprintf("no such msqid\n");
243 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
245 kprintf("wrong sequence number\n");
257 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
259 /* Free the message headers */
260 msghdr
= msqptr
->msg_first
;
261 while (msghdr
!= NULL
) {
262 struct msg
*msghdr_tmp
;
264 /* Free the segments of each message */
265 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
268 msghdr
= msghdr
->msg_next
;
269 msg_freehdr(msghdr_tmp
);
272 if (msqptr
->msg_cbytes
!= 0)
273 panic("msg_cbytes is screwed up");
274 if (msqptr
->msg_qnum
!= 0)
275 panic("msg_qnum is screwed up");
277 msqptr
->msg_qbytes
= 0; /* Mark it as free */
279 wakeup((caddr_t
)msqptr
);
285 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
287 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
289 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
290 eval
= priv_check(td
, PRIV_ROOT
);
294 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
296 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
299 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
301 if (msqbuf
.msg_qbytes
== 0) {
303 kprintf("can't reduce msg_qbytes to 0\n");
305 eval
= EINVAL
; /* non-standard errno! */
308 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
309 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
310 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
311 (msqbuf
.msg_perm
.mode
& 0777);
312 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
313 msqptr
->msg_ctime
= time_second
;
317 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
319 kprintf("requester doesn't have read access\n");
324 eval
= copyout(msqptr
, user_msqptr
, sizeof(struct msqid_ds
));
329 kprintf("invalid command %d\n", cmd
);
337 uap
->sysmsg_result
= rval
;
345 sys_msgget(struct msgget_args
*uap
)
347 struct thread
*td
= curthread
;
350 int msgflg
= uap
->msgflg
;
351 struct ucred
*cred
= td
->td_ucred
;
352 struct msqid_ds
*msqptr
= NULL
;
355 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
357 if (!jail_sysvipc_allowed
&& cred
->cr_prison
!= NULL
)
363 if (key
!= IPC_PRIVATE
) {
364 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
365 msqptr
= &msqids
[msqid
];
366 if (msqptr
->msg_qbytes
!= 0 &&
367 msqptr
->msg_perm
.key
== key
)
370 if (msqid
< msginfo
.msgmni
) {
372 kprintf("found public key\n");
374 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
376 kprintf("not exclusive\n");
381 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
383 kprintf("requester doesn't have 0%o access\n",
393 kprintf("need to allocate the msqid_ds\n");
395 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
396 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
398 * Look for an unallocated and unlocked msqid_ds.
399 * msqid_ds's can be locked by msgsnd or msgrcv while
400 * they are copying the message in/out. We can't
401 * re-use the entry until they release it.
403 msqptr
= &msqids
[msqid
];
404 if (msqptr
->msg_qbytes
== 0 &&
405 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
408 if (msqid
== msginfo
.msgmni
) {
410 kprintf("no more msqid_ds's available\n");
416 kprintf("msqid %d is available\n", msqid
);
418 msqptr
->msg_perm
.key
= key
;
419 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
420 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
421 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
422 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
423 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
424 /* Make sure that the returned msqid is unique */
425 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
426 msqptr
->msg_first
= NULL
;
427 msqptr
->msg_last
= NULL
;
428 msqptr
->msg_cbytes
= 0;
429 msqptr
->msg_qnum
= 0;
430 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
431 msqptr
->msg_lspid
= 0;
432 msqptr
->msg_lrpid
= 0;
433 msqptr
->msg_stime
= 0;
434 msqptr
->msg_rtime
= 0;
435 msqptr
->msg_ctime
= time_second
;
438 kprintf("didn't find it and wasn't asked to create it\n");
445 /* Construct the unique msqid */
447 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
455 sys_msgsnd(struct msgsnd_args
*uap
)
457 struct thread
*td
= curthread
;
458 int msqid
= uap
->msqid
;
459 const void *user_msgp
= uap
->msgp
;
460 size_t msgsz
= uap
->msgsz
;
461 int msgflg
= uap
->msgflg
;
462 int segs_needed
, eval
;
463 struct msqid_ds
*msqptr
;
468 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
472 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
476 msqid
= IPCID_TO_IX(msqid
);
478 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
480 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
487 msqptr
= &msqids
[msqid
];
488 if (msqptr
->msg_qbytes
== 0) {
490 kprintf("no such message queue id\n");
495 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
497 kprintf("wrong sequence number\n");
503 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_W
))) {
505 kprintf("requester doesn't have write access\n");
511 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
513 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
517 int need_more_resources
= 0;
521 * (inside this loop in case msg_qbytes changes while we sleep)
524 if (msgsz
> msqptr
->msg_qbytes
) {
526 kprintf("msgsz > msqptr->msg_qbytes\n");
532 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
534 kprintf("msqid is locked\n");
536 need_more_resources
= 1;
538 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
540 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
542 need_more_resources
= 1;
544 if (segs_needed
> nfree_msgmaps
) {
546 kprintf("segs_needed > nfree_msgmaps\n");
548 need_more_resources
= 1;
550 if (free_msghdrs
== NULL
) {
552 kprintf("no more msghdrs\n");
554 need_more_resources
= 1;
557 if (need_more_resources
) {
560 if ((msgflg
& IPC_NOWAIT
) != 0) {
562 kprintf("need more resources but caller doesn't want to wait\n");
568 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
570 kprintf("we don't own the msqid_ds\n");
574 /* Force later arrivals to wait for our
577 kprintf("we own the msqid_ds\n");
579 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
583 kprintf("goodnight\n");
585 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
587 kprintf("good morning, eval=%d\n", eval
);
590 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
593 kprintf("msgsnd: interrupted system call\n");
600 * Make sure that the msq queue still exists
603 if (msqptr
->msg_qbytes
== 0) {
605 kprintf("msqid deleted\n");
613 kprintf("got all the resources that we need\n");
620 * We have the resources that we need.
624 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
625 panic("msg_perm.mode & MSG_LOCKED");
626 if (segs_needed
> nfree_msgmaps
)
627 panic("segs_needed > nfree_msgmaps");
628 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
629 panic("msgsz + msg_cbytes > msg_qbytes");
630 if (free_msghdrs
== NULL
)
631 panic("no more msghdrs");
634 * Re-lock the msqid_ds in case we page-fault when copying in the
638 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
639 panic("msqid_ds is already locked");
640 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
643 * Allocate a message header
646 msghdr
= free_msghdrs
;
647 free_msghdrs
= msghdr
->msg_next
;
648 msghdr
->msg_spot
= -1;
649 msghdr
->msg_ts
= msgsz
;
652 * Allocate space for the message
655 while (segs_needed
> 0) {
656 if (nfree_msgmaps
<= 0)
657 panic("not enough msgmaps");
658 if (free_msgmaps
== -1)
659 panic("nil free_msgmaps");
662 panic("next too low #1");
663 if (next
>= msginfo
.msgseg
)
664 panic("next out of range #1");
666 kprintf("allocating segment %d to message\n", next
);
668 free_msgmaps
= msgmaps
[next
].next
;
670 msgmaps
[next
].next
= msghdr
->msg_spot
;
671 msghdr
->msg_spot
= next
;
676 * Copy in the message type
679 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
680 sizeof(msghdr
->msg_type
))) != 0) {
682 kprintf("error %d copying the message type\n", eval
);
685 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
686 wakeup((caddr_t
)msqptr
);
689 user_msgp
= (const char *)user_msgp
+ sizeof(msghdr
->msg_type
);
692 * Validate the message type
695 if (msghdr
->msg_type
< 1) {
697 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
698 wakeup((caddr_t
)msqptr
);
700 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
707 * Copy in the message body
710 next
= msghdr
->msg_spot
;
713 if (msgsz
> msginfo
.msgssz
)
714 tlen
= msginfo
.msgssz
;
718 panic("next too low #2");
719 if (next
>= msginfo
.msgseg
)
720 panic("next out of range #2");
721 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
724 kprintf("error %d copying in message segment\n", eval
);
727 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
728 wakeup((caddr_t
)msqptr
);
732 user_msgp
= (const char *)user_msgp
+ tlen
;
733 next
= msgmaps
[next
].next
;
736 panic("didn't use all the msg segments");
739 * We've got the message. Unlock the msqid_ds.
742 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
745 * Make sure that the msqid_ds is still allocated.
748 if (msqptr
->msg_qbytes
== 0) {
750 wakeup((caddr_t
)msqptr
);
756 * Put the message into the queue
759 if (msqptr
->msg_first
== NULL
) {
760 msqptr
->msg_first
= msghdr
;
761 msqptr
->msg_last
= msghdr
;
763 msqptr
->msg_last
->msg_next
= msghdr
;
764 msqptr
->msg_last
= msghdr
;
766 msqptr
->msg_last
->msg_next
= NULL
;
768 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
770 msqptr
->msg_lspid
= td
->td_proc
->p_pid
;
771 msqptr
->msg_stime
= time_second
;
773 wakeup((caddr_t
)msqptr
);
778 uap
->sysmsg_result
= 0;
786 sys_msgrcv(struct msgrcv_args
*uap
)
788 struct thread
*td
= curthread
;
789 int msqid
= uap
->msqid
;
790 void *user_msgp
= uap
->msgp
;
791 size_t msgsz
= uap
->msgsz
;
792 long msgtyp
= uap
->msgtyp
;
793 int msgflg
= uap
->msgflg
;
795 struct msqid_ds
*msqptr
;
801 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
802 msgsz
, msgtyp
, msgflg
);
805 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
809 msqid
= IPCID_TO_IX(msqid
);
811 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
813 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
820 msqptr
= &msqids
[msqid
];
821 if (msqptr
->msg_qbytes
== 0) {
823 kprintf("no such message queue id\n");
828 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
830 kprintf("wrong sequence number\n");
836 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_R
))) {
838 kprintf("requester doesn't have read access\n");
844 while (msghdr
== NULL
) {
846 msghdr
= msqptr
->msg_first
;
847 if (msghdr
!= NULL
) {
848 if (msgsz
< msghdr
->msg_ts
&&
849 (msgflg
& MSG_NOERROR
) == 0) {
851 kprintf("first message on the queue is too big (want %d, got %d)\n",
852 msgsz
, msghdr
->msg_ts
);
857 if (msqptr
->msg_first
== msqptr
->msg_last
) {
858 msqptr
->msg_first
= NULL
;
859 msqptr
->msg_last
= NULL
;
861 msqptr
->msg_first
= msghdr
->msg_next
;
862 if (msqptr
->msg_first
== NULL
)
863 panic("msg_first/last screwed up #1");
867 struct msg
*previous
;
871 prev
= &(msqptr
->msg_first
);
872 while ((msghdr
= *prev
) != NULL
) {
874 * Is this message's type an exact match or is
875 * this message's type less than or equal to
876 * the absolute value of a negative msgtyp?
877 * Note that the second half of this test can
878 * NEVER be true if msgtyp is positive since
879 * msg_type is always positive!
882 if (msgtyp
== msghdr
->msg_type
||
883 msghdr
->msg_type
<= -msgtyp
) {
885 kprintf("found message type %d, requested %d\n",
886 msghdr
->msg_type
, msgtyp
);
888 if (msgsz
< msghdr
->msg_ts
&&
889 (msgflg
& MSG_NOERROR
) == 0) {
891 kprintf("requested message on the queue is too big (want %d, got %d)\n",
892 msgsz
, msghdr
->msg_ts
);
897 *prev
= msghdr
->msg_next
;
898 if (msghdr
== msqptr
->msg_last
) {
899 if (previous
== NULL
) {
902 panic("msg_first/last screwed up #2");
910 panic("msg_first/last screwed up #3");
918 prev
= &(msghdr
->msg_next
);
923 * We've either extracted the msghdr for the appropriate
924 * message or there isn't one.
925 * If there is one then bail out of this loop.
932 * Hmph! No message found. Does the user want to wait?
935 if ((msgflg
& IPC_NOWAIT
) != 0) {
937 kprintf("no appropriate message found (msgtyp=%d)\n",
940 /* The SVID says to return ENOMSG. */
944 /* Unfortunately, BSD doesn't define that code yet! */
951 * Wait for something to happen
955 kprintf("msgrcv: goodnight\n");
957 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
959 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
964 kprintf("msgsnd: interrupted system call\n");
971 * Make sure that the msq queue still exists
974 if (msqptr
->msg_qbytes
== 0 ||
975 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
977 kprintf("msqid deleted\n");
985 * Return the message to the user.
987 * First, do the bookkeeping (before we risk being interrupted).
990 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
992 msqptr
->msg_lrpid
= td
->td_proc
->p_pid
;
993 msqptr
->msg_rtime
= time_second
;
996 * Make msgsz the actual amount that we'll be returning.
997 * Note that this effectively truncates the message if it is too long
998 * (since msgsz is never increased).
1002 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1005 if (msgsz
> msghdr
->msg_ts
)
1006 msgsz
= msghdr
->msg_ts
;
1009 * Return the type to the user.
1012 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1013 sizeof(msghdr
->msg_type
));
1016 kprintf("error (%d) copying out message type\n", eval
);
1018 msg_freehdr(msghdr
);
1019 wakeup((caddr_t
)msqptr
);
1022 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1025 * Return the segments to the user
1028 next
= msghdr
->msg_spot
;
1029 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1032 if (msgsz
- len
> msginfo
.msgssz
)
1033 tlen
= msginfo
.msgssz
;
1037 panic("next too low #3");
1038 if (next
>= msginfo
.msgseg
)
1039 panic("next out of range #3");
1040 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1044 kprintf("error (%d) copying out message segment\n",
1047 msg_freehdr(msghdr
);
1048 wakeup((caddr_t
)msqptr
);
1051 user_msgp
= (char *)user_msgp
+ tlen
;
1052 next
= msgmaps
[next
].next
;
1056 * Done, return the actual number of bytes copied out.
1059 msg_freehdr(msghdr
);
1060 wakeup((caddr_t
)msqptr
);
1065 uap
->sysmsg_result
= msgsz
;
1070 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1073 return (SYSCTL_OUT(req
, msqids
,
1074 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1077 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1078 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1079 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1081 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0,
1082 "Max characters in message");
1083 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0,
1084 "Max message queue identifiers");
1085 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0,
1086 "Max characters in message queue");
1087 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0,
1088 "Max SVID messages in system");
1089 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0,
1090 "Power-of-two size of a message segment");
1091 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0,
1092 "Number of message segments");
1093 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1094 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");