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
);
47 /* XXX casting to (sy_call_t *) is bogus, as usual. */
48 static sy_call_t
*msgcalls
[] = {
49 (sy_call_t
*)sys_msgctl
, (sy_call_t
*)sys_msgget
,
50 (sy_call_t
*)sys_msgsnd
, (sy_call_t
*)sys_msgrcv
54 struct msg
*msg_next
; /* next msg in the chain */
55 long msg_type
; /* type of this message */
56 /* >0 -> type of this message */
57 /* 0 -> free header */
58 u_short msg_ts
; /* size of this message */
59 short msg_spot
; /* location of start of msg in buffer */
64 #define MSGSSZ 8 /* Each segment must be 2^N long */
67 #define MSGSEG 2048 /* must be less than 32767 */
69 #define MSGMAX (MSGSSZ*MSGSEG)
71 #define MSGMNB 2048 /* max # of bytes in a queue */
81 * Based on the configuration parameters described in an SVR2 (yes, two)
82 * config(1m) man page.
84 * Each message is broken up and stored in segments that are msgssz bytes
85 * long. For efficiency reasons, this should be a power of two. Also,
86 * it doesn't make sense if it is less than 8 or greater than about 256.
87 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
88 * two between 8 and 1024 inclusive (and panic's if it isn't).
90 struct msginfo msginfo
= {
91 MSGMAX
, /* max chars in a message */
92 MSGMNI
, /* # of message queue identifiers */
93 MSGMNB
, /* max chars in a queue */
94 MSGTQL
, /* max messages in system */
95 MSGSSZ
, /* size of a message segment */
96 /* (must be small power of 2 greater than 4) */
97 MSGSEG
/* number of message segments */
101 * macros to convert between msqid_ds's and msqid's.
102 * (specific to this implementation)
104 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
105 #define MSQID_IX(id) ((id) & 0xffff)
106 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
109 * The rest of this file is specific to this particular implementation.
113 short next
; /* next segment in buffer */
114 /* -1 -> available */
115 /* 0..(MSGSEG-1) -> index of next segment */
118 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
120 static int nfree_msgmaps
; /* # of free map entries */
121 static short free_msgmaps
; /* head of linked list of free map entries */
122 static struct msg
*free_msghdrs
;/* list of free msg headers */
123 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
124 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
125 static struct msg
*msghdrs
; /* MSGTQL msg headers */
126 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
133 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
134 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
135 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
136 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
137 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
140 * msginfo.msgssz should be a power of two for efficiency reasons.
141 * It is also pretty silly if msginfo.msgssz is less than 8
142 * or greater than about 256 so ...
146 while (i
< 1024 && i
!= msginfo
.msgssz
)
148 if (i
!= msginfo
.msgssz
) {
149 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
151 panic("msginfo.msgssz not a small power of 2");
154 if (msginfo
.msgseg
> 32767) {
155 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
156 panic("msginfo.msgseg > 32767");
160 panic("msgmaps is NULL");
162 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
164 msgmaps
[i
-1].next
= i
;
165 msgmaps
[i
].next
= -1; /* implies entry is available */
168 nfree_msgmaps
= msginfo
.msgseg
;
171 panic("msghdrs is NULL");
173 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
174 msghdrs
[i
].msg_type
= 0;
176 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
177 msghdrs
[i
].msg_next
= NULL
;
179 free_msghdrs
= &msghdrs
[0];
182 panic("msqids is NULL");
184 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
185 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
186 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
187 msqids
[i
].msg_perm
.mode
= 0;
190 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
)
193 * Entry point for all MSG calls
195 * msgsys_args(int which, int a2, ...) (VARARGS)
200 sys_msgsys(struct msgsys_args
*uap
)
202 struct thread
*td
= curthread
;
203 unsigned int which
= (unsigned int)uap
->which
;
206 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
209 if (which
>= NELEM(msgcalls
))
211 bcopy(&uap
->a2
, &uap
->which
,
212 sizeof(struct msgsys_args
) - offsetof(struct msgsys_args
, a2
));
214 error
= (*msgcalls
[which
])(uap
);
220 msg_freehdr(struct msg
*msghdr
)
222 while (msghdr
->msg_ts
> 0) {
224 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
225 panic("msghdr->msg_spot out of range");
226 next
= msgmaps
[msghdr
->msg_spot
].next
;
227 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
228 free_msgmaps
= msghdr
->msg_spot
;
230 msghdr
->msg_spot
= next
;
231 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
232 msghdr
->msg_ts
-= msginfo
.msgssz
;
236 if (msghdr
->msg_spot
!= -1)
237 panic("msghdr->msg_spot != -1");
238 msghdr
->msg_next
= free_msghdrs
;
239 free_msghdrs
= msghdr
;
246 sys_msgctl(struct msgctl_args
*uap
)
248 struct thread
*td
= curthread
;
249 struct proc
*p
= td
->td_proc
;
250 int msqid
= uap
->msqid
;
252 struct msqid_ds
*user_msqptr
= uap
->buf
;
254 struct msqid_ds msqbuf
;
255 struct msqid_ds
*msqptr
;
258 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
261 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
265 msqid
= IPCID_TO_IX(msqid
);
267 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
269 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
276 msqptr
= &msqids
[msqid
];
278 if (msqptr
->msg_qbytes
== 0) {
280 kprintf("no such msqid\n");
285 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
287 kprintf("wrong sequence number\n");
299 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
301 /* Free the message headers */
302 msghdr
= msqptr
->msg_first
;
303 while (msghdr
!= NULL
) {
304 struct msg
*msghdr_tmp
;
306 /* Free the segments of each message */
307 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
310 msghdr
= msghdr
->msg_next
;
311 msg_freehdr(msghdr_tmp
);
314 if (msqptr
->msg_cbytes
!= 0)
315 panic("msg_cbytes is screwed up");
316 if (msqptr
->msg_qnum
!= 0)
317 panic("msg_qnum is screwed up");
319 msqptr
->msg_qbytes
= 0; /* Mark it as free */
321 wakeup((caddr_t
)msqptr
);
327 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
329 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
331 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
332 eval
= priv_check(td
, PRIV_ROOT
);
336 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
338 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
341 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
343 if (msqbuf
.msg_qbytes
== 0) {
345 kprintf("can't reduce msg_qbytes to 0\n");
347 eval
= EINVAL
; /* non-standard errno! */
350 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
351 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
352 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
353 (msqbuf
.msg_perm
.mode
& 0777);
354 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
355 msqptr
->msg_ctime
= time_second
;
359 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
361 kprintf("requester doesn't have read access\n");
366 eval
= copyout(msqptr
, user_msqptr
, sizeof(struct msqid_ds
));
371 kprintf("invalid command %d\n", cmd
);
379 uap
->sysmsg_result
= rval
;
387 sys_msgget(struct msgget_args
*uap
)
389 struct thread
*td
= curthread
;
392 int msgflg
= uap
->msgflg
;
393 struct ucred
*cred
= td
->td_ucred
;
394 struct msqid_ds
*msqptr
= NULL
;
397 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
399 if (!jail_sysvipc_allowed
&& cred
->cr_prison
!= NULL
)
405 if (key
!= IPC_PRIVATE
) {
406 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
407 msqptr
= &msqids
[msqid
];
408 if (msqptr
->msg_qbytes
!= 0 &&
409 msqptr
->msg_perm
.key
== key
)
412 if (msqid
< msginfo
.msgmni
) {
414 kprintf("found public key\n");
416 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
418 kprintf("not exclusive\n");
423 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
425 kprintf("requester doesn't have 0%o access\n",
435 kprintf("need to allocate the msqid_ds\n");
437 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
438 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
440 * Look for an unallocated and unlocked msqid_ds.
441 * msqid_ds's can be locked by msgsnd or msgrcv while
442 * they are copying the message in/out. We can't
443 * re-use the entry until they release it.
445 msqptr
= &msqids
[msqid
];
446 if (msqptr
->msg_qbytes
== 0 &&
447 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
450 if (msqid
== msginfo
.msgmni
) {
452 kprintf("no more msqid_ds's available\n");
458 kprintf("msqid %d is available\n", msqid
);
460 msqptr
->msg_perm
.key
= key
;
461 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
462 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
463 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
464 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
465 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
466 /* Make sure that the returned msqid is unique */
467 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
468 msqptr
->msg_first
= NULL
;
469 msqptr
->msg_last
= NULL
;
470 msqptr
->msg_cbytes
= 0;
471 msqptr
->msg_qnum
= 0;
472 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
473 msqptr
->msg_lspid
= 0;
474 msqptr
->msg_lrpid
= 0;
475 msqptr
->msg_stime
= 0;
476 msqptr
->msg_rtime
= 0;
477 msqptr
->msg_ctime
= time_second
;
480 kprintf("didn't find it and wasn't asked to create it\n");
487 /* Construct the unique msqid */
489 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
497 sys_msgsnd(struct msgsnd_args
*uap
)
499 struct thread
*td
= curthread
;
500 int msqid
= uap
->msqid
;
501 void *user_msgp
= uap
->msgp
;
502 size_t msgsz
= uap
->msgsz
;
503 int msgflg
= uap
->msgflg
;
504 int segs_needed
, eval
;
505 struct msqid_ds
*msqptr
;
510 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
514 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
518 msqid
= IPCID_TO_IX(msqid
);
520 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
522 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
529 msqptr
= &msqids
[msqid
];
530 if (msqptr
->msg_qbytes
== 0) {
532 kprintf("no such message queue id\n");
537 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
539 kprintf("wrong sequence number\n");
545 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_W
))) {
547 kprintf("requester doesn't have write access\n");
553 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
555 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
559 int need_more_resources
= 0;
563 * (inside this loop in case msg_qbytes changes while we sleep)
566 if (msgsz
> msqptr
->msg_qbytes
) {
568 kprintf("msgsz > msqptr->msg_qbytes\n");
574 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
576 kprintf("msqid is locked\n");
578 need_more_resources
= 1;
580 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
582 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
584 need_more_resources
= 1;
586 if (segs_needed
> nfree_msgmaps
) {
588 kprintf("segs_needed > nfree_msgmaps\n");
590 need_more_resources
= 1;
592 if (free_msghdrs
== NULL
) {
594 kprintf("no more msghdrs\n");
596 need_more_resources
= 1;
599 if (need_more_resources
) {
602 if ((msgflg
& IPC_NOWAIT
) != 0) {
604 kprintf("need more resources but caller doesn't want to wait\n");
610 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
612 kprintf("we don't own the msqid_ds\n");
616 /* Force later arrivals to wait for our
619 kprintf("we own the msqid_ds\n");
621 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
625 kprintf("goodnight\n");
627 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
629 kprintf("good morning, eval=%d\n", eval
);
632 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
635 kprintf("msgsnd: interrupted system call\n");
642 * Make sure that the msq queue still exists
645 if (msqptr
->msg_qbytes
== 0) {
647 kprintf("msqid deleted\n");
655 kprintf("got all the resources that we need\n");
662 * We have the resources that we need.
666 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
667 panic("msg_perm.mode & MSG_LOCKED");
668 if (segs_needed
> nfree_msgmaps
)
669 panic("segs_needed > nfree_msgmaps");
670 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
671 panic("msgsz + msg_cbytes > msg_qbytes");
672 if (free_msghdrs
== NULL
)
673 panic("no more msghdrs");
676 * Re-lock the msqid_ds in case we page-fault when copying in the
680 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
681 panic("msqid_ds is already locked");
682 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
685 * Allocate a message header
688 msghdr
= free_msghdrs
;
689 free_msghdrs
= msghdr
->msg_next
;
690 msghdr
->msg_spot
= -1;
691 msghdr
->msg_ts
= msgsz
;
694 * Allocate space for the message
697 while (segs_needed
> 0) {
698 if (nfree_msgmaps
<= 0)
699 panic("not enough msgmaps");
700 if (free_msgmaps
== -1)
701 panic("nil free_msgmaps");
704 panic("next too low #1");
705 if (next
>= msginfo
.msgseg
)
706 panic("next out of range #1");
708 kprintf("allocating segment %d to message\n", next
);
710 free_msgmaps
= msgmaps
[next
].next
;
712 msgmaps
[next
].next
= msghdr
->msg_spot
;
713 msghdr
->msg_spot
= next
;
718 * Copy in the message type
721 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
722 sizeof(msghdr
->msg_type
))) != 0) {
724 kprintf("error %d copying the message type\n", eval
);
727 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
728 wakeup((caddr_t
)msqptr
);
731 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
734 * Validate the message type
737 if (msghdr
->msg_type
< 1) {
739 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
740 wakeup((caddr_t
)msqptr
);
742 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
749 * Copy in the message body
752 next
= msghdr
->msg_spot
;
755 if (msgsz
> msginfo
.msgssz
)
756 tlen
= msginfo
.msgssz
;
760 panic("next too low #2");
761 if (next
>= msginfo
.msgseg
)
762 panic("next out of range #2");
763 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
766 kprintf("error %d copying in message segment\n", eval
);
769 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
770 wakeup((caddr_t
)msqptr
);
774 user_msgp
= (char *)user_msgp
+ tlen
;
775 next
= msgmaps
[next
].next
;
778 panic("didn't use all the msg segments");
781 * We've got the message. Unlock the msqid_ds.
784 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
787 * Make sure that the msqid_ds is still allocated.
790 if (msqptr
->msg_qbytes
== 0) {
792 wakeup((caddr_t
)msqptr
);
798 * Put the message into the queue
801 if (msqptr
->msg_first
== NULL
) {
802 msqptr
->msg_first
= msghdr
;
803 msqptr
->msg_last
= msghdr
;
805 msqptr
->msg_last
->msg_next
= msghdr
;
806 msqptr
->msg_last
= msghdr
;
808 msqptr
->msg_last
->msg_next
= NULL
;
810 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
812 msqptr
->msg_lspid
= td
->td_proc
->p_pid
;
813 msqptr
->msg_stime
= time_second
;
815 wakeup((caddr_t
)msqptr
);
820 uap
->sysmsg_result
= 0;
828 sys_msgrcv(struct msgrcv_args
*uap
)
830 struct thread
*td
= curthread
;
831 int msqid
= uap
->msqid
;
832 void *user_msgp
= uap
->msgp
;
833 size_t msgsz
= uap
->msgsz
;
834 long msgtyp
= uap
->msgtyp
;
835 int msgflg
= uap
->msgflg
;
837 struct msqid_ds
*msqptr
;
843 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
844 msgsz
, msgtyp
, msgflg
);
847 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
851 msqid
= IPCID_TO_IX(msqid
);
853 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
855 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
862 msqptr
= &msqids
[msqid
];
863 if (msqptr
->msg_qbytes
== 0) {
865 kprintf("no such message queue id\n");
870 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
872 kprintf("wrong sequence number\n");
878 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_R
))) {
880 kprintf("requester doesn't have read access\n");
886 while (msghdr
== NULL
) {
888 msghdr
= msqptr
->msg_first
;
889 if (msghdr
!= NULL
) {
890 if (msgsz
< msghdr
->msg_ts
&&
891 (msgflg
& MSG_NOERROR
) == 0) {
893 kprintf("first message on the queue is too big (want %d, got %d)\n",
894 msgsz
, msghdr
->msg_ts
);
899 if (msqptr
->msg_first
== msqptr
->msg_last
) {
900 msqptr
->msg_first
= NULL
;
901 msqptr
->msg_last
= NULL
;
903 msqptr
->msg_first
= msghdr
->msg_next
;
904 if (msqptr
->msg_first
== NULL
)
905 panic("msg_first/last screwed up #1");
909 struct msg
*previous
;
913 prev
= &(msqptr
->msg_first
);
914 while ((msghdr
= *prev
) != NULL
) {
916 * Is this message's type an exact match or is
917 * this message's type less than or equal to
918 * the absolute value of a negative msgtyp?
919 * Note that the second half of this test can
920 * NEVER be true if msgtyp is positive since
921 * msg_type is always positive!
924 if (msgtyp
== msghdr
->msg_type
||
925 msghdr
->msg_type
<= -msgtyp
) {
927 kprintf("found message type %d, requested %d\n",
928 msghdr
->msg_type
, msgtyp
);
930 if (msgsz
< msghdr
->msg_ts
&&
931 (msgflg
& MSG_NOERROR
) == 0) {
933 kprintf("requested message on the queue is too big (want %d, got %d)\n",
934 msgsz
, msghdr
->msg_ts
);
939 *prev
= msghdr
->msg_next
;
940 if (msghdr
== msqptr
->msg_last
) {
941 if (previous
== NULL
) {
944 panic("msg_first/last screwed up #2");
952 panic("msg_first/last screwed up #3");
960 prev
= &(msghdr
->msg_next
);
965 * We've either extracted the msghdr for the appropriate
966 * message or there isn't one.
967 * If there is one then bail out of this loop.
974 * Hmph! No message found. Does the user want to wait?
977 if ((msgflg
& IPC_NOWAIT
) != 0) {
979 kprintf("no appropriate message found (msgtyp=%d)\n",
982 /* The SVID says to return ENOMSG. */
986 /* Unfortunately, BSD doesn't define that code yet! */
993 * Wait for something to happen
997 kprintf("msgrcv: goodnight\n");
999 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
1001 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
1006 kprintf("msgsnd: interrupted system call\n");
1013 * Make sure that the msq queue still exists
1016 if (msqptr
->msg_qbytes
== 0 ||
1017 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1019 kprintf("msqid deleted\n");
1027 * Return the message to the user.
1029 * First, do the bookkeeping (before we risk being interrupted).
1032 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1034 msqptr
->msg_lrpid
= td
->td_proc
->p_pid
;
1035 msqptr
->msg_rtime
= time_second
;
1038 * Make msgsz the actual amount that we'll be returning.
1039 * Note that this effectively truncates the message if it is too long
1040 * (since msgsz is never increased).
1044 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1047 if (msgsz
> msghdr
->msg_ts
)
1048 msgsz
= msghdr
->msg_ts
;
1051 * Return the type to the user.
1054 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1055 sizeof(msghdr
->msg_type
));
1058 kprintf("error (%d) copying out message type\n", eval
);
1060 msg_freehdr(msghdr
);
1061 wakeup((caddr_t
)msqptr
);
1064 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1067 * Return the segments to the user
1070 next
= msghdr
->msg_spot
;
1071 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1074 if (msgsz
- len
> msginfo
.msgssz
)
1075 tlen
= msginfo
.msgssz
;
1079 panic("next too low #3");
1080 if (next
>= msginfo
.msgseg
)
1081 panic("next out of range #3");
1082 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1086 kprintf("error (%d) copying out message segment\n",
1089 msg_freehdr(msghdr
);
1090 wakeup((caddr_t
)msqptr
);
1093 user_msgp
= (char *)user_msgp
+ tlen
;
1094 next
= msgmaps
[next
].next
;
1098 * Done, return the actual number of bytes copied out.
1101 msg_freehdr(msghdr
);
1102 wakeup((caddr_t
)msqptr
);
1107 uap
->sysmsg_result
= msgsz
;
1112 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1115 return (SYSCTL_OUT(req
, msqids
,
1116 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1119 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1120 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1121 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1123 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0,
1124 "Max characters in message");
1125 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0,
1126 "Max message queue identifiers");
1127 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0,
1128 "Max characters in message queue");
1129 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0,
1130 "Max SVID messages in system");
1131 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0,
1132 "Power-of-two size of a message segment");
1133 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0,
1134 "Number of message segments");
1135 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1136 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");