1 /* $FreeBSD: src/sys/kern/sysv_msg.c,v 1.23.2.5 2002/12/31 08:54:53 maxim Exp $ */
2 /* $DragonFly: src/sys/kern/sysv_msg.c,v 1.18 2008/01/06 16:55:51 swildner Exp $ */
5 * Implementation of SVID messages
7 * Author: Daniel Boulet
9 * Copyright 1993 Daniel Boulet and RTMX Inc.
11 * This system call was implemented by Daniel Boulet under contract from RTMX.
13 * Redistribution and use in source forms, with and without modification,
14 * are permitted provided that this entire comment appears intact.
16 * Redistribution in binary form may occur without any restrictions.
17 * Obviously, it would be nice if you gave credit where credit is due
18 * but requiring it would be too onerous.
20 * This software is provided ``AS IS'' without any warranties of any kind.
23 #include "opt_sysvipc.h"
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/sysproto.h>
28 #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
);
45 /* XXX casting to (sy_call_t *) is bogus, as usual. */
46 static sy_call_t
*msgcalls
[] = {
47 (sy_call_t
*)sys_msgctl
, (sy_call_t
*)sys_msgget
,
48 (sy_call_t
*)sys_msgsnd
, (sy_call_t
*)sys_msgrcv
52 struct msg
*msg_next
; /* next msg in the chain */
53 long msg_type
; /* type of this message */
54 /* >0 -> type of this message */
55 /* 0 -> free header */
56 u_short msg_ts
; /* size of this message */
57 short msg_spot
; /* location of start of msg in buffer */
62 #define MSGSSZ 8 /* Each segment must be 2^N long */
65 #define MSGSEG 2048 /* must be less than 32767 */
67 #define MSGMAX (MSGSSZ*MSGSEG)
69 #define MSGMNB 2048 /* max # of bytes in a queue */
79 * Based on the configuration parameters described in an SVR2 (yes, two)
80 * config(1m) man page.
82 * Each message is broken up and stored in segments that are msgssz bytes
83 * long. For efficiency reasons, this should be a power of two. Also,
84 * it doesn't make sense if it is less than 8 or greater than about 256.
85 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
86 * two between 8 and 1024 inclusive (and panic's if it isn't).
88 struct msginfo msginfo
= {
89 MSGMAX
, /* max chars in a message */
90 MSGMNI
, /* # of message queue identifiers */
91 MSGMNB
, /* max chars in a queue */
92 MSGTQL
, /* max messages in system */
93 MSGSSZ
, /* size of a message segment */
94 /* (must be small power of 2 greater than 4) */
95 MSGSEG
/* number of message segments */
99 * macros to convert between msqid_ds's and msqid's.
100 * (specific to this implementation)
102 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
103 #define MSQID_IX(id) ((id) & 0xffff)
104 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
107 * The rest of this file is specific to this particular implementation.
111 short next
; /* next segment in buffer */
112 /* -1 -> available */
113 /* 0..(MSGSEG-1) -> index of next segment */
116 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
118 static int nfree_msgmaps
; /* # of free map entries */
119 static short free_msgmaps
; /* head of linked list of free map entries */
120 static struct msg
*free_msghdrs
;/* list of free msg headers */
121 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
122 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
123 static struct msg
*msghdrs
; /* MSGTQL msg headers */
124 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
131 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
132 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
133 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
134 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
135 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
138 * msginfo.msgssz should be a power of two for efficiency reasons.
139 * It is also pretty silly if msginfo.msgssz is less than 8
140 * or greater than about 256 so ...
144 while (i
< 1024 && i
!= msginfo
.msgssz
)
146 if (i
!= msginfo
.msgssz
) {
147 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
149 panic("msginfo.msgssz not a small power of 2");
152 if (msginfo
.msgseg
> 32767) {
153 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
154 panic("msginfo.msgseg > 32767");
158 panic("msgmaps is NULL");
160 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
162 msgmaps
[i
-1].next
= i
;
163 msgmaps
[i
].next
= -1; /* implies entry is available */
166 nfree_msgmaps
= msginfo
.msgseg
;
169 panic("msghdrs is NULL");
171 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
172 msghdrs
[i
].msg_type
= 0;
174 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
175 msghdrs
[i
].msg_next
= NULL
;
177 free_msghdrs
= &msghdrs
[0];
180 panic("msqids is NULL");
182 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
183 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
184 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
185 msqids
[i
].msg_perm
.mode
= 0;
188 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
)
191 * Entry point for all MSG calls
193 * msgsys_args(int which, int a2, ...) (VARARGS)
196 sys_msgsys(struct msgsys_args
*uap
)
198 struct proc
*p
= curproc
;
199 unsigned int which
= (unsigned int)uap
->which
;
201 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
204 if (which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
206 bcopy(&uap
->a2
, &uap
->which
,
207 sizeof(struct msgsys_args
) - offsetof(struct msgsys_args
, a2
));
208 return ((*msgcalls
[which
])(uap
));
212 msg_freehdr(struct msg
*msghdr
)
214 while (msghdr
->msg_ts
> 0) {
216 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
217 panic("msghdr->msg_spot out of range");
218 next
= msgmaps
[msghdr
->msg_spot
].next
;
219 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
220 free_msgmaps
= msghdr
->msg_spot
;
222 msghdr
->msg_spot
= next
;
223 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
224 msghdr
->msg_ts
-= msginfo
.msgssz
;
228 if (msghdr
->msg_spot
!= -1)
229 panic("msghdr->msg_spot != -1");
230 msghdr
->msg_next
= free_msghdrs
;
231 free_msghdrs
= msghdr
;
235 sys_msgctl(struct msgctl_args
*uap
)
237 struct thread
*td
= curthread
;
238 struct proc
*p
= td
->td_proc
;
239 int msqid
= uap
->msqid
;
241 struct msqid_ds
*user_msqptr
= uap
->buf
;
243 struct msqid_ds msqbuf
;
244 struct msqid_ds
*msqptr
;
247 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
250 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
253 msqid
= IPCID_TO_IX(msqid
);
255 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
257 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
263 msqptr
= &msqids
[msqid
];
265 if (msqptr
->msg_qbytes
== 0) {
267 kprintf("no such msqid\n");
271 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
273 kprintf("wrong sequence number\n");
286 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
288 /* Free the message headers */
289 msghdr
= msqptr
->msg_first
;
290 while (msghdr
!= NULL
) {
291 struct msg
*msghdr_tmp
;
293 /* Free the segments of each message */
294 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
297 msghdr
= msghdr
->msg_next
;
298 msg_freehdr(msghdr_tmp
);
301 if (msqptr
->msg_cbytes
!= 0)
302 panic("msg_cbytes is screwed up");
303 if (msqptr
->msg_qnum
!= 0)
304 panic("msg_qnum is screwed up");
306 msqptr
->msg_qbytes
= 0; /* Mark it as free */
308 wakeup((caddr_t
)msqptr
);
314 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
316 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
318 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
323 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
325 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
328 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
330 if (msqbuf
.msg_qbytes
== 0) {
332 kprintf("can't reduce msg_qbytes to 0\n");
334 return(EINVAL
); /* non-standard errno! */
336 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
337 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
338 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
339 (msqbuf
.msg_perm
.mode
& 0777);
340 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
341 msqptr
->msg_ctime
= time_second
;
345 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
347 kprintf("requester doesn't have read access\n");
351 eval
= copyout((caddr_t
)msqptr
, user_msqptr
,
352 sizeof(struct msqid_ds
));
357 kprintf("invalid command %d\n", cmd
);
363 uap
->sysmsg_result
= rval
;
368 sys_msgget(struct msgget_args
*uap
)
370 struct proc
*p
= curproc
;
373 int msgflg
= uap
->msgflg
;
374 struct ucred
*cred
= p
->p_ucred
;
375 struct msqid_ds
*msqptr
= NULL
;
378 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
381 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
384 if (key
!= IPC_PRIVATE
) {
385 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
386 msqptr
= &msqids
[msqid
];
387 if (msqptr
->msg_qbytes
!= 0 &&
388 msqptr
->msg_perm
.key
== key
)
391 if (msqid
< msginfo
.msgmni
) {
393 kprintf("found public key\n");
395 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
397 kprintf("not exclusive\n");
401 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
403 kprintf("requester doesn't have 0%o access\n",
413 kprintf("need to allocate the msqid_ds\n");
415 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
416 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
418 * Look for an unallocated and unlocked msqid_ds.
419 * msqid_ds's can be locked by msgsnd or msgrcv while
420 * they are copying the message in/out. We can't
421 * re-use the entry until they release it.
423 msqptr
= &msqids
[msqid
];
424 if (msqptr
->msg_qbytes
== 0 &&
425 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
428 if (msqid
== msginfo
.msgmni
) {
430 kprintf("no more msqid_ds's available\n");
435 kprintf("msqid %d is available\n", msqid
);
437 msqptr
->msg_perm
.key
= key
;
438 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
439 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
440 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
441 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
442 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
443 /* Make sure that the returned msqid is unique */
444 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
445 msqptr
->msg_first
= NULL
;
446 msqptr
->msg_last
= NULL
;
447 msqptr
->msg_cbytes
= 0;
448 msqptr
->msg_qnum
= 0;
449 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
450 msqptr
->msg_lspid
= 0;
451 msqptr
->msg_lrpid
= 0;
452 msqptr
->msg_stime
= 0;
453 msqptr
->msg_rtime
= 0;
454 msqptr
->msg_ctime
= time_second
;
457 kprintf("didn't find it and wasn't asked to create it\n");
463 /* Construct the unique msqid */
464 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
469 sys_msgsnd(struct msgsnd_args
*uap
)
471 struct proc
*p
= curproc
;
472 int msqid
= uap
->msqid
;
473 void *user_msgp
= uap
->msgp
;
474 size_t msgsz
= uap
->msgsz
;
475 int msgflg
= uap
->msgflg
;
476 int segs_needed
, eval
;
477 struct msqid_ds
*msqptr
;
482 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
486 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
489 msqid
= IPCID_TO_IX(msqid
);
491 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
493 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
499 msqptr
= &msqids
[msqid
];
500 if (msqptr
->msg_qbytes
== 0) {
502 kprintf("no such message queue id\n");
506 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
508 kprintf("wrong sequence number\n");
513 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_W
))) {
515 kprintf("requester doesn't have write access\n");
520 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
522 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
526 int need_more_resources
= 0;
530 * (inside this loop in case msg_qbytes changes while we sleep)
533 if (msgsz
> msqptr
->msg_qbytes
) {
535 kprintf("msgsz > msqptr->msg_qbytes\n");
540 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
542 kprintf("msqid is locked\n");
544 need_more_resources
= 1;
546 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
548 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
550 need_more_resources
= 1;
552 if (segs_needed
> nfree_msgmaps
) {
554 kprintf("segs_needed > nfree_msgmaps\n");
556 need_more_resources
= 1;
558 if (free_msghdrs
== NULL
) {
560 kprintf("no more msghdrs\n");
562 need_more_resources
= 1;
565 if (need_more_resources
) {
568 if ((msgflg
& IPC_NOWAIT
) != 0) {
570 kprintf("need more resources but caller doesn't want to wait\n");
575 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
577 kprintf("we don't own the msqid_ds\n");
581 /* Force later arrivals to wait for our
584 kprintf("we own the msqid_ds\n");
586 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
590 kprintf("goodnight\n");
592 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
594 kprintf("good morning, eval=%d\n", eval
);
597 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
600 kprintf("msgsnd: interrupted system call\n");
606 * Make sure that the msq queue still exists
609 if (msqptr
->msg_qbytes
== 0) {
611 kprintf("msqid deleted\n");
618 kprintf("got all the resources that we need\n");
625 * We have the resources that we need.
629 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
630 panic("msg_perm.mode & MSG_LOCKED");
631 if (segs_needed
> nfree_msgmaps
)
632 panic("segs_needed > nfree_msgmaps");
633 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
634 panic("msgsz + msg_cbytes > msg_qbytes");
635 if (free_msghdrs
== NULL
)
636 panic("no more msghdrs");
639 * Re-lock the msqid_ds in case we page-fault when copying in the
643 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
644 panic("msqid_ds is already locked");
645 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
648 * Allocate a message header
651 msghdr
= free_msghdrs
;
652 free_msghdrs
= msghdr
->msg_next
;
653 msghdr
->msg_spot
= -1;
654 msghdr
->msg_ts
= msgsz
;
657 * Allocate space for the message
660 while (segs_needed
> 0) {
661 if (nfree_msgmaps
<= 0)
662 panic("not enough msgmaps");
663 if (free_msgmaps
== -1)
664 panic("nil free_msgmaps");
667 panic("next too low #1");
668 if (next
>= msginfo
.msgseg
)
669 panic("next out of range #1");
671 kprintf("allocating segment %d to message\n", next
);
673 free_msgmaps
= msgmaps
[next
].next
;
675 msgmaps
[next
].next
= msghdr
->msg_spot
;
676 msghdr
->msg_spot
= next
;
681 * Copy in the message type
684 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
685 sizeof(msghdr
->msg_type
))) != 0) {
687 kprintf("error %d copying the message type\n", eval
);
690 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
691 wakeup((caddr_t
)msqptr
);
694 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
697 * Validate the message type
700 if (msghdr
->msg_type
< 1) {
702 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
703 wakeup((caddr_t
)msqptr
);
705 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
711 * Copy in the message body
714 next
= msghdr
->msg_spot
;
717 if (msgsz
> msginfo
.msgssz
)
718 tlen
= msginfo
.msgssz
;
722 panic("next too low #2");
723 if (next
>= msginfo
.msgseg
)
724 panic("next out of range #2");
725 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
728 kprintf("error %d copying in message segment\n", eval
);
731 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
732 wakeup((caddr_t
)msqptr
);
736 user_msgp
= (char *)user_msgp
+ tlen
;
737 next
= msgmaps
[next
].next
;
740 panic("didn't use all the msg segments");
743 * We've got the message. Unlock the msqid_ds.
746 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
749 * Make sure that the msqid_ds is still allocated.
752 if (msqptr
->msg_qbytes
== 0) {
754 wakeup((caddr_t
)msqptr
);
759 * Put the message into the queue
762 if (msqptr
->msg_first
== NULL
) {
763 msqptr
->msg_first
= msghdr
;
764 msqptr
->msg_last
= msghdr
;
766 msqptr
->msg_last
->msg_next
= msghdr
;
767 msqptr
->msg_last
= msghdr
;
769 msqptr
->msg_last
->msg_next
= NULL
;
771 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
773 msqptr
->msg_lspid
= p
->p_pid
;
774 msqptr
->msg_stime
= time_second
;
776 wakeup((caddr_t
)msqptr
);
777 uap
->sysmsg_result
= 0;
782 sys_msgrcv(struct msgrcv_args
*uap
)
784 struct proc
*p
= curproc
;
785 int msqid
= uap
->msqid
;
786 void *user_msgp
= uap
->msgp
;
787 size_t msgsz
= uap
->msgsz
;
788 long msgtyp
= uap
->msgtyp
;
789 int msgflg
= uap
->msgflg
;
791 struct msqid_ds
*msqptr
;
797 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
798 msgsz
, msgtyp
, msgflg
);
801 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
804 msqid
= IPCID_TO_IX(msqid
);
806 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
808 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
814 msqptr
= &msqids
[msqid
];
815 if (msqptr
->msg_qbytes
== 0) {
817 kprintf("no such message queue id\n");
821 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
823 kprintf("wrong sequence number\n");
828 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
830 kprintf("requester doesn't have read access\n");
836 while (msghdr
== NULL
) {
838 msghdr
= msqptr
->msg_first
;
839 if (msghdr
!= NULL
) {
840 if (msgsz
< msghdr
->msg_ts
&&
841 (msgflg
& MSG_NOERROR
) == 0) {
843 kprintf("first message on the queue is too big (want %d, got %d)\n",
844 msgsz
, msghdr
->msg_ts
);
848 if (msqptr
->msg_first
== msqptr
->msg_last
) {
849 msqptr
->msg_first
= NULL
;
850 msqptr
->msg_last
= NULL
;
852 msqptr
->msg_first
= msghdr
->msg_next
;
853 if (msqptr
->msg_first
== NULL
)
854 panic("msg_first/last screwed up #1");
858 struct msg
*previous
;
862 prev
= &(msqptr
->msg_first
);
863 while ((msghdr
= *prev
) != NULL
) {
865 * Is this message's type an exact match or is
866 * this message's type less than or equal to
867 * the absolute value of a negative msgtyp?
868 * Note that the second half of this test can
869 * NEVER be true if msgtyp is positive since
870 * msg_type is always positive!
873 if (msgtyp
== msghdr
->msg_type
||
874 msghdr
->msg_type
<= -msgtyp
) {
876 kprintf("found message type %d, requested %d\n",
877 msghdr
->msg_type
, msgtyp
);
879 if (msgsz
< msghdr
->msg_ts
&&
880 (msgflg
& MSG_NOERROR
) == 0) {
882 kprintf("requested message on the queue is too big (want %d, got %d)\n",
883 msgsz
, msghdr
->msg_ts
);
887 *prev
= msghdr
->msg_next
;
888 if (msghdr
== msqptr
->msg_last
) {
889 if (previous
== NULL
) {
892 panic("msg_first/last screwed up #2");
900 panic("msg_first/last screwed up #3");
908 prev
= &(msghdr
->msg_next
);
913 * We've either extracted the msghdr for the appropriate
914 * message or there isn't one.
915 * If there is one then bail out of this loop.
922 * Hmph! No message found. Does the user want to wait?
925 if ((msgflg
& IPC_NOWAIT
) != 0) {
927 kprintf("no appropriate message found (msgtyp=%d)\n",
930 /* The SVID says to return ENOMSG. */
934 /* Unfortunately, BSD doesn't define that code yet! */
940 * Wait for something to happen
944 kprintf("msgrcv: goodnight\n");
946 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
948 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
953 kprintf("msgsnd: interrupted system call\n");
959 * Make sure that the msq queue still exists
962 if (msqptr
->msg_qbytes
== 0 ||
963 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
965 kprintf("msqid deleted\n");
972 * Return the message to the user.
974 * First, do the bookkeeping (before we risk being interrupted).
977 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
979 msqptr
->msg_lrpid
= p
->p_pid
;
980 msqptr
->msg_rtime
= time_second
;
983 * Make msgsz the actual amount that we'll be returning.
984 * Note that this effectively truncates the message if it is too long
985 * (since msgsz is never increased).
989 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
992 if (msgsz
> msghdr
->msg_ts
)
993 msgsz
= msghdr
->msg_ts
;
996 * Return the type to the user.
999 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1000 sizeof(msghdr
->msg_type
));
1003 kprintf("error (%d) copying out message type\n", eval
);
1005 msg_freehdr(msghdr
);
1006 wakeup((caddr_t
)msqptr
);
1009 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1012 * Return the segments to the user
1015 next
= msghdr
->msg_spot
;
1016 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1019 if (msgsz
- len
> msginfo
.msgssz
)
1020 tlen
= msginfo
.msgssz
;
1024 panic("next too low #3");
1025 if (next
>= msginfo
.msgseg
)
1026 panic("next out of range #3");
1027 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1031 kprintf("error (%d) copying out message segment\n",
1034 msg_freehdr(msghdr
);
1035 wakeup((caddr_t
)msqptr
);
1038 user_msgp
= (char *)user_msgp
+ tlen
;
1039 next
= msgmaps
[next
].next
;
1043 * Done, return the actual number of bytes copied out.
1046 msg_freehdr(msghdr
);
1047 wakeup((caddr_t
)msqptr
);
1048 uap
->sysmsg_result
= msgsz
;
1053 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1056 return (SYSCTL_OUT(req
, msqids
,
1057 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1060 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1061 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1062 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1064 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0, "");
1065 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0, "");
1066 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0, "");
1067 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0, "");
1068 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0, "");
1069 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0, "");
1070 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1071 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");