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 static MALLOC_DEFINE(M_MSG
, "msg", "SVID compatible message queues");
38 static void msginit (void *);
43 static void msg_freehdr (struct msg
*msghdr
);
46 struct msg
*msg_next
; /* next msg in the chain */
47 long msg_type
; /* type of this message */
48 /* >0 -> type of this message */
49 /* 0 -> free header */
50 u_short msg_ts
; /* size of this message */
51 short msg_spot
; /* location of start of msg in buffer */
56 #define MSGSSZ 8 /* Each segment must be 2^N long */
59 #define MSGSEG 2048 /* must be less than 32767 */
61 #define MSGMAX (MSGSSZ*MSGSEG)
63 #define MSGMNB 2048 /* max # of bytes in a queue */
73 * Based on the configuration parameters described in an SVR2 (yes, two)
74 * config(1m) man page.
76 * Each message is broken up and stored in segments that are msgssz bytes
77 * long. For efficiency reasons, this should be a power of two. Also,
78 * it doesn't make sense if it is less than 8 or greater than about 256.
79 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
80 * two between 8 and 1024 inclusive (and panic's if it isn't).
82 struct msginfo msginfo
= {
83 MSGMAX
, /* max chars in a message */
84 MSGMNI
, /* # of message queue identifiers */
85 MSGMNB
, /* max chars in a queue */
86 MSGTQL
, /* max messages in system */
87 MSGSSZ
, /* size of a message segment */
88 /* (must be small power of 2 greater than 4) */
89 MSGSEG
/* number of message segments */
93 * macros to convert between msqid_ds's and msqid's.
94 * (specific to this implementation)
96 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
97 #define MSQID_IX(id) ((id) & 0xffff)
98 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
101 * The rest of this file is specific to this particular implementation.
105 short next
; /* next segment in buffer */
106 /* -1 -> available */
107 /* 0..(MSGSEG-1) -> index of next segment */
110 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
112 static int nfree_msgmaps
; /* # of free map entries */
113 static short free_msgmaps
; /* head of linked list of free map entries */
114 static struct msg
*free_msghdrs
;/* list of free msg headers */
115 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
116 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
117 static struct msg
*msghdrs
; /* MSGTQL msg headers */
118 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
119 static struct lwkt_token msg_token
= LWKT_TOKEN_INITIALIZER(msg_token
);
126 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
127 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
128 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
129 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
130 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
133 * msginfo.msgssz should be a power of two for efficiency reasons.
134 * It is also pretty silly if msginfo.msgssz is less than 8
135 * or greater than about 256 so ...
139 while (i
< 1024 && i
!= msginfo
.msgssz
)
141 if (i
!= msginfo
.msgssz
) {
142 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
144 panic("msginfo.msgssz not a small power of 2");
147 if (msginfo
.msgseg
> 32767) {
148 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
149 panic("msginfo.msgseg > 32767");
152 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
154 msgmaps
[i
-1].next
= i
;
155 msgmaps
[i
].next
= -1; /* implies entry is available */
158 nfree_msgmaps
= msginfo
.msgseg
;
160 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
161 msghdrs
[i
].msg_type
= 0;
163 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
164 msghdrs
[i
].msg_next
= NULL
;
166 free_msghdrs
= &msghdrs
[0];
168 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
169 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
170 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
171 msqids
[i
].msg_perm
.mode
= 0;
174 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
);
177 msg_freehdr(struct msg
*msghdr
)
179 while (msghdr
->msg_ts
> 0) {
181 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
182 panic("msghdr->msg_spot out of range");
183 next
= msgmaps
[msghdr
->msg_spot
].next
;
184 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
185 free_msgmaps
= msghdr
->msg_spot
;
187 msghdr
->msg_spot
= next
;
188 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
189 msghdr
->msg_ts
-= msginfo
.msgssz
;
193 if (msghdr
->msg_spot
!= -1)
194 panic("msghdr->msg_spot != -1");
195 msghdr
->msg_next
= free_msghdrs
;
196 free_msghdrs
= msghdr
;
203 sys_msgctl(struct msgctl_args
*uap
)
205 struct thread
*td
= curthread
;
206 struct proc
*p
= td
->td_proc
;
207 int msqid
= uap
->msqid
;
209 struct msqid_ds
*user_msqptr
= uap
->buf
;
211 struct msqid_ds msqbuf
;
212 struct msqid_ds
*msqptr
;
215 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
218 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
221 lwkt_gettoken(&msg_token
);
222 msqid
= IPCID_TO_IX(msqid
);
224 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
226 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
233 msqptr
= &msqids
[msqid
];
235 if (msqptr
->msg_qbytes
== 0) {
237 kprintf("no such msqid\n");
242 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
244 kprintf("wrong sequence number\n");
256 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
258 /* Free the message headers */
259 msghdr
= msqptr
->msg_first
;
260 while (msghdr
!= NULL
) {
261 struct msg
*msghdr_tmp
;
263 /* Free the segments of each message */
264 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
267 msghdr
= msghdr
->msg_next
;
268 msg_freehdr(msghdr_tmp
);
271 if (msqptr
->msg_cbytes
!= 0)
272 panic("msg_cbytes is screwed up");
273 if (msqptr
->msg_qnum
!= 0)
274 panic("msg_qnum is screwed up");
276 msqptr
->msg_qbytes
= 0; /* Mark it as free */
278 wakeup((caddr_t
)msqptr
);
284 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
286 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
288 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
289 eval
= priv_check(td
, PRIV_ROOT
);
293 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
295 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
298 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
300 if (msqbuf
.msg_qbytes
== 0) {
302 kprintf("can't reduce msg_qbytes to 0\n");
304 eval
= EINVAL
; /* non-standard errno! */
307 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
308 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
309 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
310 (msqbuf
.msg_perm
.mode
& 0777);
311 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
312 msqptr
->msg_ctime
= time_second
;
316 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
318 kprintf("requester doesn't have read access\n");
323 eval
= copyout(msqptr
, user_msqptr
, sizeof(struct msqid_ds
));
328 kprintf("invalid command %d\n", cmd
);
334 lwkt_reltoken(&msg_token
);
336 uap
->sysmsg_result
= rval
;
344 sys_msgget(struct msgget_args
*uap
)
346 struct thread
*td
= curthread
;
349 int msgflg
= uap
->msgflg
;
350 struct ucred
*cred
= td
->td_ucred
;
351 struct msqid_ds
*msqptr
= NULL
;
354 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
356 if (!jail_sysvipc_allowed
&& cred
->cr_prison
!= NULL
)
360 lwkt_gettoken(&msg_token
);
362 if (key
!= IPC_PRIVATE
) {
363 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
364 msqptr
= &msqids
[msqid
];
365 if (msqptr
->msg_qbytes
!= 0 &&
366 msqptr
->msg_perm
.key
== key
)
369 if (msqid
< msginfo
.msgmni
) {
371 kprintf("found public key\n");
373 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
375 kprintf("not exclusive\n");
380 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
382 kprintf("requester doesn't have 0%o access\n",
392 kprintf("need to allocate the msqid_ds\n");
394 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
395 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
397 * Look for an unallocated and unlocked msqid_ds.
398 * msqid_ds's can be locked by msgsnd or msgrcv while
399 * they are copying the message in/out. We can't
400 * re-use the entry until they release it.
402 msqptr
= &msqids
[msqid
];
403 if (msqptr
->msg_qbytes
== 0 &&
404 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
407 if (msqid
== msginfo
.msgmni
) {
409 kprintf("no more msqid_ds's available\n");
415 kprintf("msqid %d is available\n", msqid
);
417 msqptr
->msg_perm
.key
= key
;
418 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
419 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
420 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
421 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
422 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
423 /* Make sure that the returned msqid is unique */
424 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
425 msqptr
->msg_first
= NULL
;
426 msqptr
->msg_last
= NULL
;
427 msqptr
->msg_cbytes
= 0;
428 msqptr
->msg_qnum
= 0;
429 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
430 msqptr
->msg_lspid
= 0;
431 msqptr
->msg_lrpid
= 0;
432 msqptr
->msg_stime
= 0;
433 msqptr
->msg_rtime
= 0;
434 msqptr
->msg_ctime
= time_second
;
437 kprintf("didn't find it and wasn't asked to create it\n");
443 lwkt_reltoken(&msg_token
);
444 /* Construct the unique msqid */
446 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
454 sys_msgsnd(struct msgsnd_args
*uap
)
456 struct thread
*td
= curthread
;
457 int msqid
= uap
->msqid
;
458 const void *user_msgp
= uap
->msgp
;
459 size_t msgsz
= uap
->msgsz
;
460 int msgflg
= uap
->msgflg
;
461 int segs_needed
, eval
;
462 struct msqid_ds
*msqptr
;
467 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
471 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
474 lwkt_gettoken(&msg_token
);
475 msqid
= IPCID_TO_IX(msqid
);
477 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
479 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
486 msqptr
= &msqids
[msqid
];
487 if (msqptr
->msg_qbytes
== 0) {
489 kprintf("no such message queue id\n");
494 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
496 kprintf("wrong sequence number\n");
502 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_W
))) {
504 kprintf("requester doesn't have write access\n");
510 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
512 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
516 int need_more_resources
= 0;
520 * (inside this loop in case msg_qbytes changes while we sleep)
523 if (msgsz
> msqptr
->msg_qbytes
) {
525 kprintf("msgsz > msqptr->msg_qbytes\n");
531 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
533 kprintf("msqid is locked\n");
535 need_more_resources
= 1;
537 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
539 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
541 need_more_resources
= 1;
543 if (segs_needed
> nfree_msgmaps
) {
545 kprintf("segs_needed > nfree_msgmaps\n");
547 need_more_resources
= 1;
549 if (free_msghdrs
== NULL
) {
551 kprintf("no more msghdrs\n");
553 need_more_resources
= 1;
556 if (need_more_resources
) {
559 if ((msgflg
& IPC_NOWAIT
) != 0) {
561 kprintf("need more resources but caller doesn't want to wait\n");
567 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
569 kprintf("we don't own the msqid_ds\n");
573 /* Force later arrivals to wait for our
576 kprintf("we own the msqid_ds\n");
578 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
582 kprintf("goodnight\n");
584 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
586 kprintf("good morning, eval=%d\n", eval
);
589 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
592 kprintf("msgsnd: interrupted system call\n");
599 * Make sure that the msq queue still exists
602 if (msqptr
->msg_qbytes
== 0) {
604 kprintf("msqid deleted\n");
612 kprintf("got all the resources that we need\n");
619 * We have the resources that we need.
623 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
624 panic("msg_perm.mode & MSG_LOCKED");
625 if (segs_needed
> nfree_msgmaps
)
626 panic("segs_needed > nfree_msgmaps");
627 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
628 panic("msgsz + msg_cbytes > msg_qbytes");
629 if (free_msghdrs
== NULL
)
630 panic("no more msghdrs");
633 * Re-lock the msqid_ds in case we page-fault when copying in the
637 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
638 panic("msqid_ds is already locked");
639 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
642 * Allocate a message header
645 msghdr
= free_msghdrs
;
646 free_msghdrs
= msghdr
->msg_next
;
647 msghdr
->msg_spot
= -1;
648 msghdr
->msg_ts
= msgsz
;
651 * Allocate space for the message
654 while (segs_needed
> 0) {
655 if (nfree_msgmaps
<= 0)
656 panic("not enough msgmaps");
657 if (free_msgmaps
== -1)
658 panic("nil free_msgmaps");
661 panic("next too low #1");
662 if (next
>= msginfo
.msgseg
)
663 panic("next out of range #1");
665 kprintf("allocating segment %d to message\n", next
);
667 free_msgmaps
= msgmaps
[next
].next
;
669 msgmaps
[next
].next
= msghdr
->msg_spot
;
670 msghdr
->msg_spot
= next
;
675 * Copy in the message type
678 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
679 sizeof(msghdr
->msg_type
))) != 0) {
681 kprintf("error %d copying the message type\n", eval
);
684 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
685 wakeup((caddr_t
)msqptr
);
688 user_msgp
= (const char *)user_msgp
+ sizeof(msghdr
->msg_type
);
691 * Validate the message type
694 if (msghdr
->msg_type
< 1) {
696 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
697 wakeup((caddr_t
)msqptr
);
699 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
706 * Copy in the message body
709 next
= msghdr
->msg_spot
;
712 if (msgsz
> msginfo
.msgssz
)
713 tlen
= msginfo
.msgssz
;
717 panic("next too low #2");
718 if (next
>= msginfo
.msgseg
)
719 panic("next out of range #2");
720 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
723 kprintf("error %d copying in message segment\n", eval
);
726 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
727 wakeup((caddr_t
)msqptr
);
731 user_msgp
= (const char *)user_msgp
+ tlen
;
732 next
= msgmaps
[next
].next
;
735 panic("didn't use all the msg segments");
738 * We've got the message. Unlock the msqid_ds.
741 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
744 * Make sure that the msqid_ds is still allocated.
747 if (msqptr
->msg_qbytes
== 0) {
749 wakeup((caddr_t
)msqptr
);
755 * Put the message into the queue
758 if (msqptr
->msg_first
== NULL
) {
759 msqptr
->msg_first
= msghdr
;
760 msqptr
->msg_last
= msghdr
;
762 msqptr
->msg_last
->msg_next
= msghdr
;
763 msqptr
->msg_last
= msghdr
;
765 msqptr
->msg_last
->msg_next
= NULL
;
767 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
769 msqptr
->msg_lspid
= td
->td_proc
->p_pid
;
770 msqptr
->msg_stime
= time_second
;
772 wakeup((caddr_t
)msqptr
);
775 lwkt_reltoken(&msg_token
);
777 uap
->sysmsg_result
= 0;
785 sys_msgrcv(struct msgrcv_args
*uap
)
787 struct thread
*td
= curthread
;
788 int msqid
= uap
->msqid
;
789 void *user_msgp
= uap
->msgp
;
790 size_t msgsz
= uap
->msgsz
;
791 long msgtyp
= uap
->msgtyp
;
792 int msgflg
= uap
->msgflg
;
794 struct msqid_ds
*msqptr
;
800 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
801 msgsz
, msgtyp
, msgflg
);
804 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
807 lwkt_gettoken(&msg_token
);
808 msqid
= IPCID_TO_IX(msqid
);
810 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
812 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
819 msqptr
= &msqids
[msqid
];
820 if (msqptr
->msg_qbytes
== 0) {
822 kprintf("no such message queue id\n");
827 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
829 kprintf("wrong sequence number\n");
835 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_R
))) {
837 kprintf("requester doesn't have read access\n");
843 while (msghdr
== NULL
) {
845 msghdr
= msqptr
->msg_first
;
846 if (msghdr
!= NULL
) {
847 if (msgsz
< msghdr
->msg_ts
&&
848 (msgflg
& MSG_NOERROR
) == 0) {
850 kprintf("first message on the queue is too big (want %d, got %d)\n",
851 msgsz
, msghdr
->msg_ts
);
856 if (msqptr
->msg_first
== msqptr
->msg_last
) {
857 msqptr
->msg_first
= NULL
;
858 msqptr
->msg_last
= NULL
;
860 msqptr
->msg_first
= msghdr
->msg_next
;
861 if (msqptr
->msg_first
== NULL
)
862 panic("msg_first/last screwed up #1");
866 struct msg
*previous
;
870 prev
= &(msqptr
->msg_first
);
871 while ((msghdr
= *prev
) != NULL
) {
873 * Is this message's type an exact match or is
874 * this message's type less than or equal to
875 * the absolute value of a negative msgtyp?
876 * Note that the second half of this test can
877 * NEVER be true if msgtyp is positive since
878 * msg_type is always positive!
881 if (msgtyp
== msghdr
->msg_type
||
882 msghdr
->msg_type
<= -msgtyp
) {
884 kprintf("found message type %d, requested %d\n",
885 msghdr
->msg_type
, msgtyp
);
887 if (msgsz
< msghdr
->msg_ts
&&
888 (msgflg
& MSG_NOERROR
) == 0) {
890 kprintf("requested message on the queue is too big (want %d, got %d)\n",
891 msgsz
, msghdr
->msg_ts
);
896 *prev
= msghdr
->msg_next
;
897 if (msghdr
== msqptr
->msg_last
) {
898 if (previous
== NULL
) {
901 panic("msg_first/last screwed up #2");
909 panic("msg_first/last screwed up #3");
917 prev
= &(msghdr
->msg_next
);
922 * We've either extracted the msghdr for the appropriate
923 * message or there isn't one.
924 * If there is one then bail out of this loop.
931 * Hmph! No message found. Does the user want to wait?
934 if ((msgflg
& IPC_NOWAIT
) != 0) {
936 kprintf("no appropriate message found (msgtyp=%d)\n",
939 /* The SVID says to return ENOMSG. */
943 /* Unfortunately, BSD doesn't define that code yet! */
950 * Wait for something to happen
954 kprintf("msgrcv: goodnight\n");
956 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
958 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
963 kprintf("msgsnd: interrupted system call\n");
970 * Make sure that the msq queue still exists
973 if (msqptr
->msg_qbytes
== 0 ||
974 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
976 kprintf("msqid deleted\n");
984 * Return the message to the user.
986 * First, do the bookkeeping (before we risk being interrupted).
989 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
991 msqptr
->msg_lrpid
= td
->td_proc
->p_pid
;
992 msqptr
->msg_rtime
= time_second
;
995 * Make msgsz the actual amount that we'll be returning.
996 * Note that this effectively truncates the message if it is too long
997 * (since msgsz is never increased).
1001 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1004 if (msgsz
> msghdr
->msg_ts
)
1005 msgsz
= msghdr
->msg_ts
;
1008 * Return the type to the user.
1011 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1012 sizeof(msghdr
->msg_type
));
1015 kprintf("error (%d) copying out message type\n", eval
);
1017 msg_freehdr(msghdr
);
1018 wakeup((caddr_t
)msqptr
);
1021 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1024 * Return the segments to the user
1027 next
= msghdr
->msg_spot
;
1028 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1031 if (msgsz
- len
> msginfo
.msgssz
)
1032 tlen
= msginfo
.msgssz
;
1036 panic("next too low #3");
1037 if (next
>= msginfo
.msgseg
)
1038 panic("next out of range #3");
1039 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1043 kprintf("error (%d) copying out message segment\n",
1046 msg_freehdr(msghdr
);
1047 wakeup((caddr_t
)msqptr
);
1050 user_msgp
= (char *)user_msgp
+ tlen
;
1051 next
= msgmaps
[next
].next
;
1055 * Done, return the actual number of bytes copied out.
1058 msg_freehdr(msghdr
);
1059 wakeup((caddr_t
)msqptr
);
1062 lwkt_reltoken(&msg_token
);
1064 uap
->sysmsg_result
= msgsz
;
1069 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1071 return (SYSCTL_OUT(req
, msqids
,
1072 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1075 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1076 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1077 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1079 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0,
1080 "Max characters in message");
1081 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0,
1082 "Max message queue identifiers");
1083 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0,
1084 "Max characters in message queue");
1085 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0,
1086 "Max SVID messages in system");
1087 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0,
1088 "Power-of-two size of a message segment");
1089 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0,
1090 "Number of message segments");
1091 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1092 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");