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>
32 #include <sys/sysent.h>
33 #include <sys/sysctl.h>
34 #include <sys/malloc.h>
37 static MALLOC_DEFINE(M_MSG
, "msg", "SVID compatible message queues");
39 static void msginit (void *);
44 static void msg_freehdr (struct msg
*msghdr
);
46 /* XXX casting to (sy_call_t *) is bogus, as usual. */
47 static sy_call_t
*msgcalls
[] = {
48 (sy_call_t
*)sys_msgctl
, (sy_call_t
*)sys_msgget
,
49 (sy_call_t
*)sys_msgsnd
, (sy_call_t
*)sys_msgrcv
53 struct msg
*msg_next
; /* next msg in the chain */
54 long msg_type
; /* type of this message */
55 /* >0 -> type of this message */
56 /* 0 -> free header */
57 u_short msg_ts
; /* size of this message */
58 short msg_spot
; /* location of start of msg in buffer */
63 #define MSGSSZ 8 /* Each segment must be 2^N long */
66 #define MSGSEG 2048 /* must be less than 32767 */
68 #define MSGMAX (MSGSSZ*MSGSEG)
70 #define MSGMNB 2048 /* max # of bytes in a queue */
80 * Based on the configuration parameters described in an SVR2 (yes, two)
81 * config(1m) man page.
83 * Each message is broken up and stored in segments that are msgssz bytes
84 * long. For efficiency reasons, this should be a power of two. Also,
85 * it doesn't make sense if it is less than 8 or greater than about 256.
86 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
87 * two between 8 and 1024 inclusive (and panic's if it isn't).
89 struct msginfo msginfo
= {
90 MSGMAX
, /* max chars in a message */
91 MSGMNI
, /* # of message queue identifiers */
92 MSGMNB
, /* max chars in a queue */
93 MSGTQL
, /* max messages in system */
94 MSGSSZ
, /* size of a message segment */
95 /* (must be small power of 2 greater than 4) */
96 MSGSEG
/* number of message segments */
100 * macros to convert between msqid_ds's and msqid's.
101 * (specific to this implementation)
103 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
104 #define MSQID_IX(id) ((id) & 0xffff)
105 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
108 * The rest of this file is specific to this particular implementation.
112 short next
; /* next segment in buffer */
113 /* -1 -> available */
114 /* 0..(MSGSEG-1) -> index of next segment */
117 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
119 static int nfree_msgmaps
; /* # of free map entries */
120 static short free_msgmaps
; /* head of linked list of free map entries */
121 static struct msg
*free_msghdrs
;/* list of free msg headers */
122 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
123 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
124 static struct msg
*msghdrs
; /* MSGTQL msg headers */
125 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
132 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
133 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
134 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
135 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
136 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
139 * msginfo.msgssz should be a power of two for efficiency reasons.
140 * It is also pretty silly if msginfo.msgssz is less than 8
141 * or greater than about 256 so ...
145 while (i
< 1024 && i
!= msginfo
.msgssz
)
147 if (i
!= msginfo
.msgssz
) {
148 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
150 panic("msginfo.msgssz not a small power of 2");
153 if (msginfo
.msgseg
> 32767) {
154 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
155 panic("msginfo.msgseg > 32767");
159 panic("msgmaps is NULL");
161 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
163 msgmaps
[i
-1].next
= i
;
164 msgmaps
[i
].next
= -1; /* implies entry is available */
167 nfree_msgmaps
= msginfo
.msgseg
;
170 panic("msghdrs is NULL");
172 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
173 msghdrs
[i
].msg_type
= 0;
175 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
176 msghdrs
[i
].msg_next
= NULL
;
178 free_msghdrs
= &msghdrs
[0];
181 panic("msqids is NULL");
183 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
184 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
185 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
186 msqids
[i
].msg_perm
.mode
= 0;
189 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
)
192 * Entry point for all MSG calls
194 * msgsys_args(int which, int a2, ...) (VARARGS)
197 sys_msgsys(struct msgsys_args
*uap
)
199 struct proc
*p
= curproc
;
200 unsigned int which
= (unsigned int)uap
->which
;
202 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
205 if (which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
207 bcopy(&uap
->a2
, &uap
->which
,
208 sizeof(struct msgsys_args
) - offsetof(struct msgsys_args
, a2
));
209 return ((*msgcalls
[which
])(uap
));
213 msg_freehdr(struct msg
*msghdr
)
215 while (msghdr
->msg_ts
> 0) {
217 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
218 panic("msghdr->msg_spot out of range");
219 next
= msgmaps
[msghdr
->msg_spot
].next
;
220 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
221 free_msgmaps
= msghdr
->msg_spot
;
223 msghdr
->msg_spot
= next
;
224 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
225 msghdr
->msg_ts
-= msginfo
.msgssz
;
229 if (msghdr
->msg_spot
!= -1)
230 panic("msghdr->msg_spot != -1");
231 msghdr
->msg_next
= free_msghdrs
;
232 free_msghdrs
= msghdr
;
236 sys_msgctl(struct msgctl_args
*uap
)
238 struct thread
*td
= curthread
;
239 struct proc
*p
= td
->td_proc
;
240 int msqid
= uap
->msqid
;
242 struct msqid_ds
*user_msqptr
= uap
->buf
;
244 struct msqid_ds msqbuf
;
245 struct msqid_ds
*msqptr
;
248 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
251 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
254 msqid
= IPCID_TO_IX(msqid
);
256 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
258 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
264 msqptr
= &msqids
[msqid
];
266 if (msqptr
->msg_qbytes
== 0) {
268 kprintf("no such msqid\n");
272 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
274 kprintf("wrong sequence number\n");
287 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
289 /* Free the message headers */
290 msghdr
= msqptr
->msg_first
;
291 while (msghdr
!= NULL
) {
292 struct msg
*msghdr_tmp
;
294 /* Free the segments of each message */
295 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
298 msghdr
= msghdr
->msg_next
;
299 msg_freehdr(msghdr_tmp
);
302 if (msqptr
->msg_cbytes
!= 0)
303 panic("msg_cbytes is screwed up");
304 if (msqptr
->msg_qnum
!= 0)
305 panic("msg_qnum is screwed up");
307 msqptr
->msg_qbytes
= 0; /* Mark it as free */
309 wakeup((caddr_t
)msqptr
);
315 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)))
317 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
319 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
320 eval
= priv_check(td
, PRIV_ROOT
);
324 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
326 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
329 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
331 if (msqbuf
.msg_qbytes
== 0) {
333 kprintf("can't reduce msg_qbytes to 0\n");
335 return(EINVAL
); /* non-standard errno! */
337 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
338 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
339 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
340 (msqbuf
.msg_perm
.mode
& 0777);
341 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
342 msqptr
->msg_ctime
= time_second
;
346 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
348 kprintf("requester doesn't have read access\n");
352 eval
= copyout((caddr_t
)msqptr
, user_msqptr
,
353 sizeof(struct msqid_ds
));
358 kprintf("invalid command %d\n", cmd
);
364 uap
->sysmsg_result
= rval
;
369 sys_msgget(struct msgget_args
*uap
)
371 struct proc
*p
= curproc
;
374 int msgflg
= uap
->msgflg
;
375 struct ucred
*cred
= p
->p_ucred
;
376 struct msqid_ds
*msqptr
= NULL
;
379 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
382 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
385 if (key
!= IPC_PRIVATE
) {
386 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
387 msqptr
= &msqids
[msqid
];
388 if (msqptr
->msg_qbytes
!= 0 &&
389 msqptr
->msg_perm
.key
== key
)
392 if (msqid
< msginfo
.msgmni
) {
394 kprintf("found public key\n");
396 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
398 kprintf("not exclusive\n");
402 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
404 kprintf("requester doesn't have 0%o access\n",
414 kprintf("need to allocate the msqid_ds\n");
416 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
417 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
419 * Look for an unallocated and unlocked msqid_ds.
420 * msqid_ds's can be locked by msgsnd or msgrcv while
421 * they are copying the message in/out. We can't
422 * re-use the entry until they release it.
424 msqptr
= &msqids
[msqid
];
425 if (msqptr
->msg_qbytes
== 0 &&
426 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
429 if (msqid
== msginfo
.msgmni
) {
431 kprintf("no more msqid_ds's available\n");
436 kprintf("msqid %d is available\n", msqid
);
438 msqptr
->msg_perm
.key
= key
;
439 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
440 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
441 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
442 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
443 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
444 /* Make sure that the returned msqid is unique */
445 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
446 msqptr
->msg_first
= NULL
;
447 msqptr
->msg_last
= NULL
;
448 msqptr
->msg_cbytes
= 0;
449 msqptr
->msg_qnum
= 0;
450 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
451 msqptr
->msg_lspid
= 0;
452 msqptr
->msg_lrpid
= 0;
453 msqptr
->msg_stime
= 0;
454 msqptr
->msg_rtime
= 0;
455 msqptr
->msg_ctime
= time_second
;
458 kprintf("didn't find it and wasn't asked to create it\n");
464 /* Construct the unique msqid */
465 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
470 sys_msgsnd(struct msgsnd_args
*uap
)
472 struct proc
*p
= curproc
;
473 int msqid
= uap
->msqid
;
474 void *user_msgp
= uap
->msgp
;
475 size_t msgsz
= uap
->msgsz
;
476 int msgflg
= uap
->msgflg
;
477 int segs_needed
, eval
;
478 struct msqid_ds
*msqptr
;
483 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
487 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
490 msqid
= IPCID_TO_IX(msqid
);
492 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
494 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
500 msqptr
= &msqids
[msqid
];
501 if (msqptr
->msg_qbytes
== 0) {
503 kprintf("no such message queue id\n");
507 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
509 kprintf("wrong sequence number\n");
514 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_W
))) {
516 kprintf("requester doesn't have write access\n");
521 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
523 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
527 int need_more_resources
= 0;
531 * (inside this loop in case msg_qbytes changes while we sleep)
534 if (msgsz
> msqptr
->msg_qbytes
) {
536 kprintf("msgsz > msqptr->msg_qbytes\n");
541 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
543 kprintf("msqid is locked\n");
545 need_more_resources
= 1;
547 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
549 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
551 need_more_resources
= 1;
553 if (segs_needed
> nfree_msgmaps
) {
555 kprintf("segs_needed > nfree_msgmaps\n");
557 need_more_resources
= 1;
559 if (free_msghdrs
== NULL
) {
561 kprintf("no more msghdrs\n");
563 need_more_resources
= 1;
566 if (need_more_resources
) {
569 if ((msgflg
& IPC_NOWAIT
) != 0) {
571 kprintf("need more resources but caller doesn't want to wait\n");
576 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
578 kprintf("we don't own the msqid_ds\n");
582 /* Force later arrivals to wait for our
585 kprintf("we own the msqid_ds\n");
587 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
591 kprintf("goodnight\n");
593 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
595 kprintf("good morning, eval=%d\n", eval
);
598 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
601 kprintf("msgsnd: interrupted system call\n");
607 * Make sure that the msq queue still exists
610 if (msqptr
->msg_qbytes
== 0) {
612 kprintf("msqid deleted\n");
619 kprintf("got all the resources that we need\n");
626 * We have the resources that we need.
630 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
631 panic("msg_perm.mode & MSG_LOCKED");
632 if (segs_needed
> nfree_msgmaps
)
633 panic("segs_needed > nfree_msgmaps");
634 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
635 panic("msgsz + msg_cbytes > msg_qbytes");
636 if (free_msghdrs
== NULL
)
637 panic("no more msghdrs");
640 * Re-lock the msqid_ds in case we page-fault when copying in the
644 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
645 panic("msqid_ds is already locked");
646 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
649 * Allocate a message header
652 msghdr
= free_msghdrs
;
653 free_msghdrs
= msghdr
->msg_next
;
654 msghdr
->msg_spot
= -1;
655 msghdr
->msg_ts
= msgsz
;
658 * Allocate space for the message
661 while (segs_needed
> 0) {
662 if (nfree_msgmaps
<= 0)
663 panic("not enough msgmaps");
664 if (free_msgmaps
== -1)
665 panic("nil free_msgmaps");
668 panic("next too low #1");
669 if (next
>= msginfo
.msgseg
)
670 panic("next out of range #1");
672 kprintf("allocating segment %d to message\n", next
);
674 free_msgmaps
= msgmaps
[next
].next
;
676 msgmaps
[next
].next
= msghdr
->msg_spot
;
677 msghdr
->msg_spot
= next
;
682 * Copy in the message type
685 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
686 sizeof(msghdr
->msg_type
))) != 0) {
688 kprintf("error %d copying the message type\n", eval
);
691 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
692 wakeup((caddr_t
)msqptr
);
695 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
698 * Validate the message type
701 if (msghdr
->msg_type
< 1) {
703 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
704 wakeup((caddr_t
)msqptr
);
706 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
712 * Copy in the message body
715 next
= msghdr
->msg_spot
;
718 if (msgsz
> msginfo
.msgssz
)
719 tlen
= msginfo
.msgssz
;
723 panic("next too low #2");
724 if (next
>= msginfo
.msgseg
)
725 panic("next out of range #2");
726 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
729 kprintf("error %d copying in message segment\n", eval
);
732 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
733 wakeup((caddr_t
)msqptr
);
737 user_msgp
= (char *)user_msgp
+ tlen
;
738 next
= msgmaps
[next
].next
;
741 panic("didn't use all the msg segments");
744 * We've got the message. Unlock the msqid_ds.
747 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
750 * Make sure that the msqid_ds is still allocated.
753 if (msqptr
->msg_qbytes
== 0) {
755 wakeup((caddr_t
)msqptr
);
760 * Put the message into the queue
763 if (msqptr
->msg_first
== NULL
) {
764 msqptr
->msg_first
= msghdr
;
765 msqptr
->msg_last
= msghdr
;
767 msqptr
->msg_last
->msg_next
= msghdr
;
768 msqptr
->msg_last
= msghdr
;
770 msqptr
->msg_last
->msg_next
= NULL
;
772 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
774 msqptr
->msg_lspid
= p
->p_pid
;
775 msqptr
->msg_stime
= time_second
;
777 wakeup((caddr_t
)msqptr
);
778 uap
->sysmsg_result
= 0;
783 sys_msgrcv(struct msgrcv_args
*uap
)
785 struct proc
*p
= curproc
;
786 int msqid
= uap
->msqid
;
787 void *user_msgp
= uap
->msgp
;
788 size_t msgsz
= uap
->msgsz
;
789 long msgtyp
= uap
->msgtyp
;
790 int msgflg
= uap
->msgflg
;
792 struct msqid_ds
*msqptr
;
798 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
799 msgsz
, msgtyp
, msgflg
);
802 if (!jail_sysvipc_allowed
&& p
->p_ucred
->cr_prison
!= NULL
)
805 msqid
= IPCID_TO_IX(msqid
);
807 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
809 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
815 msqptr
= &msqids
[msqid
];
816 if (msqptr
->msg_qbytes
== 0) {
818 kprintf("no such message queue id\n");
822 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
824 kprintf("wrong sequence number\n");
829 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
831 kprintf("requester doesn't have read access\n");
837 while (msghdr
== NULL
) {
839 msghdr
= msqptr
->msg_first
;
840 if (msghdr
!= NULL
) {
841 if (msgsz
< msghdr
->msg_ts
&&
842 (msgflg
& MSG_NOERROR
) == 0) {
844 kprintf("first message on the queue is too big (want %d, got %d)\n",
845 msgsz
, msghdr
->msg_ts
);
849 if (msqptr
->msg_first
== msqptr
->msg_last
) {
850 msqptr
->msg_first
= NULL
;
851 msqptr
->msg_last
= NULL
;
853 msqptr
->msg_first
= msghdr
->msg_next
;
854 if (msqptr
->msg_first
== NULL
)
855 panic("msg_first/last screwed up #1");
859 struct msg
*previous
;
863 prev
= &(msqptr
->msg_first
);
864 while ((msghdr
= *prev
) != NULL
) {
866 * Is this message's type an exact match or is
867 * this message's type less than or equal to
868 * the absolute value of a negative msgtyp?
869 * Note that the second half of this test can
870 * NEVER be true if msgtyp is positive since
871 * msg_type is always positive!
874 if (msgtyp
== msghdr
->msg_type
||
875 msghdr
->msg_type
<= -msgtyp
) {
877 kprintf("found message type %d, requested %d\n",
878 msghdr
->msg_type
, msgtyp
);
880 if (msgsz
< msghdr
->msg_ts
&&
881 (msgflg
& MSG_NOERROR
) == 0) {
883 kprintf("requested message on the queue is too big (want %d, got %d)\n",
884 msgsz
, msghdr
->msg_ts
);
888 *prev
= msghdr
->msg_next
;
889 if (msghdr
== msqptr
->msg_last
) {
890 if (previous
== NULL
) {
893 panic("msg_first/last screwed up #2");
901 panic("msg_first/last screwed up #3");
909 prev
= &(msghdr
->msg_next
);
914 * We've either extracted the msghdr for the appropriate
915 * message or there isn't one.
916 * If there is one then bail out of this loop.
923 * Hmph! No message found. Does the user want to wait?
926 if ((msgflg
& IPC_NOWAIT
) != 0) {
928 kprintf("no appropriate message found (msgtyp=%d)\n",
931 /* The SVID says to return ENOMSG. */
935 /* Unfortunately, BSD doesn't define that code yet! */
941 * Wait for something to happen
945 kprintf("msgrcv: goodnight\n");
947 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
949 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
954 kprintf("msgsnd: interrupted system call\n");
960 * Make sure that the msq queue still exists
963 if (msqptr
->msg_qbytes
== 0 ||
964 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
966 kprintf("msqid deleted\n");
973 * Return the message to the user.
975 * First, do the bookkeeping (before we risk being interrupted).
978 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
980 msqptr
->msg_lrpid
= p
->p_pid
;
981 msqptr
->msg_rtime
= time_second
;
984 * Make msgsz the actual amount that we'll be returning.
985 * Note that this effectively truncates the message if it is too long
986 * (since msgsz is never increased).
990 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
993 if (msgsz
> msghdr
->msg_ts
)
994 msgsz
= msghdr
->msg_ts
;
997 * Return the type to the user.
1000 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1001 sizeof(msghdr
->msg_type
));
1004 kprintf("error (%d) copying out message type\n", eval
);
1006 msg_freehdr(msghdr
);
1007 wakeup((caddr_t
)msqptr
);
1010 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1013 * Return the segments to the user
1016 next
= msghdr
->msg_spot
;
1017 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1020 if (msgsz
- len
> msginfo
.msgssz
)
1021 tlen
= msginfo
.msgssz
;
1025 panic("next too low #3");
1026 if (next
>= msginfo
.msgseg
)
1027 panic("next out of range #3");
1028 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1032 kprintf("error (%d) copying out message segment\n",
1035 msg_freehdr(msghdr
);
1036 wakeup((caddr_t
)msqptr
);
1039 user_msgp
= (char *)user_msgp
+ tlen
;
1040 next
= msgmaps
[next
].next
;
1044 * Done, return the actual number of bytes copied out.
1047 msg_freehdr(msghdr
);
1048 wakeup((caddr_t
)msqptr
);
1049 uap
->sysmsg_result
= msgsz
;
1054 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1057 return (SYSCTL_OUT(req
, msqids
,
1058 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1061 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1062 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1063 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1065 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0, "");
1066 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0, "");
1067 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0, "");
1068 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0, "");
1069 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0, "");
1070 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0, "");
1071 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1072 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");