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.17 2006/12/24 00:47:54 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
);
134 panic("msgpool is NULL");
135 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
137 panic("msgmaps is NULL");
138 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
140 panic("msghdrs is NULL");
141 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
143 panic("msqids is NULL");
146 * msginfo.msgssz should be a power of two for efficiency reasons.
147 * It is also pretty silly if msginfo.msgssz is less than 8
148 * or greater than about 256 so ...
152 while (i
< 1024 && i
!= msginfo
.msgssz
)
154 if (i
!= msginfo
.msgssz
) {
155 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
157 panic("msginfo.msgssz not a small power of 2");
160 if (msginfo
.msgseg
> 32767) {
161 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
162 panic("msginfo.msgseg > 32767");
166 panic("msgmaps is NULL");
168 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
170 msgmaps
[i
-1].next
= i
;
171 msgmaps
[i
].next
= -1; /* implies entry is available */
174 nfree_msgmaps
= msginfo
.msgseg
;
177 panic("msghdrs is NULL");
179 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
180 msghdrs
[i
].msg_type
= 0;
182 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
183 msghdrs
[i
].msg_next
= NULL
;
185 free_msghdrs
= &msghdrs
[0];
188 panic("msqids is NULL");
190 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
191 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
192 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
193 msqids
[i
].msg_perm
.mode
= 0;
196 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
)
199 * Entry point for all MSG calls
201 * msgsys_args(int which, int a2, ...) (VARARGS)
204 sys_msgsys(struct msgsys_args
*uap
)
206 struct proc
*p
= curproc
;
207 unsigned int which
= (unsigned int)uap
->which
;
209 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
212 if (which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
214 bcopy(&uap
->a2
, &uap
->which
,
215 sizeof(struct msgsys_args
) - offsetof(struct msgsys_args
, a2
));
216 return ((*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
;
243 sys_msgctl(struct msgctl_args
*uap
)
245 struct thread
*td
= curthread
;
246 struct proc
*p
= td
->td_proc
;
247 int msqid
= uap
->msqid
;
249 struct msqid_ds
*user_msqptr
= uap
->buf
;
251 struct msqid_ds msqbuf
;
252 struct msqid_ds
*msqptr
;
255 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
258 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
261 msqid
= IPCID_TO_IX(msqid
);
263 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
265 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
271 msqptr
= &msqids
[msqid
];
273 if (msqptr
->msg_qbytes
== 0) {
275 kprintf("no such msqid\n");
279 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
281 kprintf("wrong sequence number\n");
294 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
296 /* Free the message headers */
297 msghdr
= msqptr
->msg_first
;
298 while (msghdr
!= NULL
) {
299 struct msg
*msghdr_tmp
;
301 /* Free the segments of each message */
302 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
305 msghdr
= msghdr
->msg_next
;
306 msg_freehdr(msghdr_tmp
);
309 if (msqptr
->msg_cbytes
!= 0)
310 panic("msg_cbytes is screwed up");
311 if (msqptr
->msg_qnum
!= 0)
312 panic("msg_qnum is screwed up");
314 msqptr
->msg_qbytes
= 0; /* Mark it as free */
316 wakeup((caddr_t
)msqptr
);
322 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
324 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
326 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
331 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
333 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
336 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
338 if (msqbuf
.msg_qbytes
== 0) {
340 kprintf("can't reduce msg_qbytes to 0\n");
342 return(EINVAL
); /* non-standard errno! */
344 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
345 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
346 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
347 (msqbuf
.msg_perm
.mode
& 0777);
348 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
349 msqptr
->msg_ctime
= time_second
;
353 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
355 kprintf("requester doesn't have read access\n");
359 eval
= copyout((caddr_t
)msqptr
, user_msqptr
,
360 sizeof(struct msqid_ds
));
365 kprintf("invalid command %d\n", cmd
);
371 uap
->sysmsg_result
= rval
;
376 sys_msgget(struct msgget_args
*uap
)
378 struct proc
*p
= curproc
;
381 int msgflg
= uap
->msgflg
;
382 struct ucred
*cred
= p
->p_ucred
;
383 struct msqid_ds
*msqptr
= NULL
;
386 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
389 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
392 if (key
!= IPC_PRIVATE
) {
393 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
394 msqptr
= &msqids
[msqid
];
395 if (msqptr
->msg_qbytes
!= 0 &&
396 msqptr
->msg_perm
.key
== key
)
399 if (msqid
< msginfo
.msgmni
) {
401 kprintf("found public key\n");
403 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
405 kprintf("not exclusive\n");
409 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
411 kprintf("requester doesn't have 0%o access\n",
421 kprintf("need to allocate the msqid_ds\n");
423 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
424 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
426 * Look for an unallocated and unlocked msqid_ds.
427 * msqid_ds's can be locked by msgsnd or msgrcv while
428 * they are copying the message in/out. We can't
429 * re-use the entry until they release it.
431 msqptr
= &msqids
[msqid
];
432 if (msqptr
->msg_qbytes
== 0 &&
433 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
436 if (msqid
== msginfo
.msgmni
) {
438 kprintf("no more msqid_ds's available\n");
443 kprintf("msqid %d is available\n", msqid
);
445 msqptr
->msg_perm
.key
= key
;
446 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
447 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
448 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
449 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
450 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
451 /* Make sure that the returned msqid is unique */
452 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
453 msqptr
->msg_first
= NULL
;
454 msqptr
->msg_last
= NULL
;
455 msqptr
->msg_cbytes
= 0;
456 msqptr
->msg_qnum
= 0;
457 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
458 msqptr
->msg_lspid
= 0;
459 msqptr
->msg_lrpid
= 0;
460 msqptr
->msg_stime
= 0;
461 msqptr
->msg_rtime
= 0;
462 msqptr
->msg_ctime
= time_second
;
465 kprintf("didn't find it and wasn't asked to create it\n");
471 /* Construct the unique msqid */
472 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
477 sys_msgsnd(struct msgsnd_args
*uap
)
479 struct proc
*p
= curproc
;
480 int msqid
= uap
->msqid
;
481 void *user_msgp
= uap
->msgp
;
482 size_t msgsz
= uap
->msgsz
;
483 int msgflg
= uap
->msgflg
;
484 int segs_needed
, eval
;
485 struct msqid_ds
*msqptr
;
490 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
494 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
497 msqid
= IPCID_TO_IX(msqid
);
499 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
501 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
507 msqptr
= &msqids
[msqid
];
508 if (msqptr
->msg_qbytes
== 0) {
510 kprintf("no such message queue id\n");
514 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
516 kprintf("wrong sequence number\n");
521 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_W
))) {
523 kprintf("requester doesn't have write access\n");
528 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
530 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
534 int need_more_resources
= 0;
538 * (inside this loop in case msg_qbytes changes while we sleep)
541 if (msgsz
> msqptr
->msg_qbytes
) {
543 kprintf("msgsz > msqptr->msg_qbytes\n");
548 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
550 kprintf("msqid is locked\n");
552 need_more_resources
= 1;
554 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
556 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
558 need_more_resources
= 1;
560 if (segs_needed
> nfree_msgmaps
) {
562 kprintf("segs_needed > nfree_msgmaps\n");
564 need_more_resources
= 1;
566 if (free_msghdrs
== NULL
) {
568 kprintf("no more msghdrs\n");
570 need_more_resources
= 1;
573 if (need_more_resources
) {
576 if ((msgflg
& IPC_NOWAIT
) != 0) {
578 kprintf("need more resources but caller doesn't want to wait\n");
583 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
585 kprintf("we don't own the msqid_ds\n");
589 /* Force later arrivals to wait for our
592 kprintf("we own the msqid_ds\n");
594 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
598 kprintf("goodnight\n");
600 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
602 kprintf("good morning, eval=%d\n", eval
);
605 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
608 kprintf("msgsnd: interrupted system call\n");
614 * Make sure that the msq queue still exists
617 if (msqptr
->msg_qbytes
== 0) {
619 kprintf("msqid deleted\n");
626 kprintf("got all the resources that we need\n");
633 * We have the resources that we need.
637 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
638 panic("msg_perm.mode & MSG_LOCKED");
639 if (segs_needed
> nfree_msgmaps
)
640 panic("segs_needed > nfree_msgmaps");
641 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
642 panic("msgsz + msg_cbytes > msg_qbytes");
643 if (free_msghdrs
== NULL
)
644 panic("no more msghdrs");
647 * Re-lock the msqid_ds in case we page-fault when copying in the
651 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
652 panic("msqid_ds is already locked");
653 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
656 * Allocate a message header
659 msghdr
= free_msghdrs
;
660 free_msghdrs
= msghdr
->msg_next
;
661 msghdr
->msg_spot
= -1;
662 msghdr
->msg_ts
= msgsz
;
665 * Allocate space for the message
668 while (segs_needed
> 0) {
669 if (nfree_msgmaps
<= 0)
670 panic("not enough msgmaps");
671 if (free_msgmaps
== -1)
672 panic("nil free_msgmaps");
675 panic("next too low #1");
676 if (next
>= msginfo
.msgseg
)
677 panic("next out of range #1");
679 kprintf("allocating segment %d to message\n", next
);
681 free_msgmaps
= msgmaps
[next
].next
;
683 msgmaps
[next
].next
= msghdr
->msg_spot
;
684 msghdr
->msg_spot
= next
;
689 * Copy in the message type
692 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
693 sizeof(msghdr
->msg_type
))) != 0) {
695 kprintf("error %d copying the message type\n", eval
);
698 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
699 wakeup((caddr_t
)msqptr
);
702 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
705 * Validate the message type
708 if (msghdr
->msg_type
< 1) {
710 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
711 wakeup((caddr_t
)msqptr
);
713 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
719 * Copy in the message body
722 next
= msghdr
->msg_spot
;
725 if (msgsz
> msginfo
.msgssz
)
726 tlen
= msginfo
.msgssz
;
730 panic("next too low #2");
731 if (next
>= msginfo
.msgseg
)
732 panic("next out of range #2");
733 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
736 kprintf("error %d copying in message segment\n", eval
);
739 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
740 wakeup((caddr_t
)msqptr
);
744 user_msgp
= (char *)user_msgp
+ tlen
;
745 next
= msgmaps
[next
].next
;
748 panic("didn't use all the msg segments");
751 * We've got the message. Unlock the msqid_ds.
754 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
757 * Make sure that the msqid_ds is still allocated.
760 if (msqptr
->msg_qbytes
== 0) {
762 wakeup((caddr_t
)msqptr
);
767 * Put the message into the queue
770 if (msqptr
->msg_first
== NULL
) {
771 msqptr
->msg_first
= msghdr
;
772 msqptr
->msg_last
= msghdr
;
774 msqptr
->msg_last
->msg_next
= msghdr
;
775 msqptr
->msg_last
= msghdr
;
777 msqptr
->msg_last
->msg_next
= NULL
;
779 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
781 msqptr
->msg_lspid
= p
->p_pid
;
782 msqptr
->msg_stime
= time_second
;
784 wakeup((caddr_t
)msqptr
);
785 uap
->sysmsg_result
= 0;
790 sys_msgrcv(struct msgrcv_args
*uap
)
792 struct proc
*p
= curproc
;
793 int msqid
= uap
->msqid
;
794 void *user_msgp
= uap
->msgp
;
795 size_t msgsz
= uap
->msgsz
;
796 long msgtyp
= uap
->msgtyp
;
797 int msgflg
= uap
->msgflg
;
799 struct msqid_ds
*msqptr
;
805 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
806 msgsz
, msgtyp
, msgflg
);
809 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
812 msqid
= IPCID_TO_IX(msqid
);
814 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
816 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
822 msqptr
= &msqids
[msqid
];
823 if (msqptr
->msg_qbytes
== 0) {
825 kprintf("no such message queue id\n");
829 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
831 kprintf("wrong sequence number\n");
836 if ((eval
= ipcperm(p
, &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
);
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
);
895 *prev
= msghdr
->msg_next
;
896 if (msghdr
== msqptr
->msg_last
) {
897 if (previous
== NULL
) {
900 panic("msg_first/last screwed up #2");
908 panic("msg_first/last screwed up #3");
916 prev
= &(msghdr
->msg_next
);
921 * We've either extracted the msghdr for the appropriate
922 * message or there isn't one.
923 * If there is one then bail out of this loop.
930 * Hmph! No message found. Does the user want to wait?
933 if ((msgflg
& IPC_NOWAIT
) != 0) {
935 kprintf("no appropriate message found (msgtyp=%d)\n",
938 /* The SVID says to return ENOMSG. */
942 /* Unfortunately, BSD doesn't define that code yet! */
948 * Wait for something to happen
952 kprintf("msgrcv: goodnight\n");
954 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
956 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
961 kprintf("msgsnd: interrupted system call\n");
967 * Make sure that the msq queue still exists
970 if (msqptr
->msg_qbytes
== 0 ||
971 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
973 kprintf("msqid deleted\n");
980 * Return the message to the user.
982 * First, do the bookkeeping (before we risk being interrupted).
985 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
987 msqptr
->msg_lrpid
= p
->p_pid
;
988 msqptr
->msg_rtime
= time_second
;
991 * Make msgsz the actual amount that we'll be returning.
992 * Note that this effectively truncates the message if it is too long
993 * (since msgsz is never increased).
997 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1000 if (msgsz
> msghdr
->msg_ts
)
1001 msgsz
= msghdr
->msg_ts
;
1004 * Return the type to the user.
1007 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1008 sizeof(msghdr
->msg_type
));
1011 kprintf("error (%d) copying out message type\n", eval
);
1013 msg_freehdr(msghdr
);
1014 wakeup((caddr_t
)msqptr
);
1017 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1020 * Return the segments to the user
1023 next
= msghdr
->msg_spot
;
1024 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1027 if (msgsz
- len
> msginfo
.msgssz
)
1028 tlen
= msginfo
.msgssz
;
1032 panic("next too low #3");
1033 if (next
>= msginfo
.msgseg
)
1034 panic("next out of range #3");
1035 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1039 kprintf("error (%d) copying out message segment\n",
1042 msg_freehdr(msghdr
);
1043 wakeup((caddr_t
)msqptr
);
1046 user_msgp
= (char *)user_msgp
+ tlen
;
1047 next
= msgmaps
[next
].next
;
1051 * Done, return the actual number of bytes copied out.
1054 msg_freehdr(msghdr
);
1055 wakeup((caddr_t
)msqptr
);
1056 uap
->sysmsg_result
= msgsz
;
1061 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1064 return (SYSCTL_OUT(req
, msqids
,
1065 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1068 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1069 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1070 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1072 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0, "");
1073 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0, "");
1074 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0, "");
1075 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0, "");
1076 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0, "");
1077 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0, "");
1078 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1079 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");