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 #include <sys/mplock2.h>
39 static MALLOC_DEFINE(M_MSG
, "msg", "SVID compatible message queues");
41 static void msginit (void *);
46 static void msg_freehdr (struct msg
*msghdr
);
48 /* XXX casting to (sy_call_t *) is bogus, as usual. */
49 static sy_call_t
*msgcalls
[] = {
50 (sy_call_t
*)sys_msgctl
, (sy_call_t
*)sys_msgget
,
51 (sy_call_t
*)sys_msgsnd
, (sy_call_t
*)sys_msgrcv
55 struct msg
*msg_next
; /* next msg in the chain */
56 long msg_type
; /* type of this message */
57 /* >0 -> type of this message */
58 /* 0 -> free header */
59 u_short msg_ts
; /* size of this message */
60 short msg_spot
; /* location of start of msg in buffer */
65 #define MSGSSZ 8 /* Each segment must be 2^N long */
68 #define MSGSEG 2048 /* must be less than 32767 */
70 #define MSGMAX (MSGSSZ*MSGSEG)
72 #define MSGMNB 2048 /* max # of bytes in a queue */
82 * Based on the configuration parameters described in an SVR2 (yes, two)
83 * config(1m) man page.
85 * Each message is broken up and stored in segments that are msgssz bytes
86 * long. For efficiency reasons, this should be a power of two. Also,
87 * it doesn't make sense if it is less than 8 or greater than about 256.
88 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
89 * two between 8 and 1024 inclusive (and panic's if it isn't).
91 struct msginfo msginfo
= {
92 MSGMAX
, /* max chars in a message */
93 MSGMNI
, /* # of message queue identifiers */
94 MSGMNB
, /* max chars in a queue */
95 MSGTQL
, /* max messages in system */
96 MSGSSZ
, /* size of a message segment */
97 /* (must be small power of 2 greater than 4) */
98 MSGSEG
/* number of message segments */
102 * macros to convert between msqid_ds's and msqid's.
103 * (specific to this implementation)
105 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
106 #define MSQID_IX(id) ((id) & 0xffff)
107 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
110 * The rest of this file is specific to this particular implementation.
114 short next
; /* next segment in buffer */
115 /* -1 -> available */
116 /* 0..(MSGSEG-1) -> index of next segment */
119 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
121 static int nfree_msgmaps
; /* # of free map entries */
122 static short free_msgmaps
; /* head of linked list of free map entries */
123 static struct msg
*free_msghdrs
;/* list of free msg headers */
124 static char *msgpool
; /* MSGMAX byte long msg buffer pool */
125 static struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
126 static struct msg
*msghdrs
; /* MSGTQL msg headers */
127 static struct msqid_ds
*msqids
; /* MSGMNI msqid_ds struct's */
134 msginfo
.msgmax
= msginfo
.msgseg
* msginfo
.msgssz
;
135 msgpool
= kmalloc(msginfo
.msgmax
, M_MSG
, M_WAITOK
);
136 msgmaps
= kmalloc(sizeof(struct msgmap
) * msginfo
.msgseg
, M_MSG
, M_WAITOK
);
137 msghdrs
= kmalloc(sizeof(struct msg
) * msginfo
.msgtql
, M_MSG
, M_WAITOK
);
138 msqids
= kmalloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
, M_MSG
, M_WAITOK
);
141 * msginfo.msgssz should be a power of two for efficiency reasons.
142 * It is also pretty silly if msginfo.msgssz is less than 8
143 * or greater than about 256 so ...
147 while (i
< 1024 && i
!= msginfo
.msgssz
)
149 if (i
!= msginfo
.msgssz
) {
150 kprintf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
152 panic("msginfo.msgssz not a small power of 2");
155 if (msginfo
.msgseg
> 32767) {
156 kprintf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
157 panic("msginfo.msgseg > 32767");
161 panic("msgmaps is NULL");
163 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
165 msgmaps
[i
-1].next
= i
;
166 msgmaps
[i
].next
= -1; /* implies entry is available */
169 nfree_msgmaps
= msginfo
.msgseg
;
172 panic("msghdrs is NULL");
174 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
175 msghdrs
[i
].msg_type
= 0;
177 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
178 msghdrs
[i
].msg_next
= NULL
;
180 free_msghdrs
= &msghdrs
[0];
183 panic("msqids is NULL");
185 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
186 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
187 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
188 msqids
[i
].msg_perm
.mode
= 0;
191 SYSINIT(sysv_msg
, SI_SUB_SYSV_MSG
, SI_ORDER_FIRST
, msginit
, NULL
)
194 * Entry point for all MSG calls
196 * msgsys_args(int which, int a2, ...) (VARARGS)
201 sys_msgsys(struct msgsys_args
*uap
)
203 struct thread
*td
= curthread
;
204 unsigned int which
= (unsigned int)uap
->which
;
207 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
210 if (which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
212 bcopy(&uap
->a2
, &uap
->which
,
213 sizeof(struct msgsys_args
) - offsetof(struct msgsys_args
, a2
));
215 error
= (*msgcalls
[which
])(uap
);
221 msg_freehdr(struct msg
*msghdr
)
223 while (msghdr
->msg_ts
> 0) {
225 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
226 panic("msghdr->msg_spot out of range");
227 next
= msgmaps
[msghdr
->msg_spot
].next
;
228 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
229 free_msgmaps
= msghdr
->msg_spot
;
231 msghdr
->msg_spot
= next
;
232 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
233 msghdr
->msg_ts
-= msginfo
.msgssz
;
237 if (msghdr
->msg_spot
!= -1)
238 panic("msghdr->msg_spot != -1");
239 msghdr
->msg_next
= free_msghdrs
;
240 free_msghdrs
= msghdr
;
247 sys_msgctl(struct msgctl_args
*uap
)
249 struct thread
*td
= curthread
;
250 struct proc
*p
= td
->td_proc
;
251 int msqid
= uap
->msqid
;
253 struct msqid_ds
*user_msqptr
= uap
->buf
;
255 struct msqid_ds msqbuf
;
256 struct msqid_ds
*msqptr
;
259 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid
, cmd
, user_msqptr
);
262 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
266 msqid
= IPCID_TO_IX(msqid
);
268 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
270 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
277 msqptr
= &msqids
[msqid
];
279 if (msqptr
->msg_qbytes
== 0) {
281 kprintf("no such msqid\n");
286 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
288 kprintf("wrong sequence number\n");
300 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
302 /* Free the message headers */
303 msghdr
= msqptr
->msg_first
;
304 while (msghdr
!= NULL
) {
305 struct msg
*msghdr_tmp
;
307 /* Free the segments of each message */
308 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
311 msghdr
= msghdr
->msg_next
;
312 msg_freehdr(msghdr_tmp
);
315 if (msqptr
->msg_cbytes
!= 0)
316 panic("msg_cbytes is screwed up");
317 if (msqptr
->msg_qnum
!= 0)
318 panic("msg_qnum is screwed up");
320 msqptr
->msg_qbytes
= 0; /* Mark it as free */
322 wakeup((caddr_t
)msqptr
);
328 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_M
)) != 0)
330 if ((eval
= copyin(user_msqptr
, &msqbuf
, sizeof(msqbuf
))) != 0)
332 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
333 eval
= priv_check(td
, PRIV_ROOT
);
337 if (msqbuf
.msg_qbytes
> msginfo
.msgmnb
) {
339 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
342 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
344 if (msqbuf
.msg_qbytes
== 0) {
346 kprintf("can't reduce msg_qbytes to 0\n");
348 eval
= EINVAL
; /* non-standard errno! */
351 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
352 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
353 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
354 (msqbuf
.msg_perm
.mode
& 0777);
355 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
356 msqptr
->msg_ctime
= time_second
;
360 if ((eval
= ipcperm(p
, &msqptr
->msg_perm
, IPC_R
))) {
362 kprintf("requester doesn't have read access\n");
367 eval
= copyout(msqptr
, user_msqptr
, sizeof(struct msqid_ds
));
372 kprintf("invalid command %d\n", cmd
);
380 uap
->sysmsg_result
= rval
;
388 sys_msgget(struct msgget_args
*uap
)
390 struct thread
*td
= curthread
;
393 int msgflg
= uap
->msgflg
;
394 struct ucred
*cred
= td
->td_ucred
;
395 struct msqid_ds
*msqptr
= NULL
;
398 kprintf("msgget(0x%x, 0%o)\n", key
, msgflg
);
400 if (!jail_sysvipc_allowed
&& cred
->cr_prison
!= NULL
)
406 if (key
!= IPC_PRIVATE
) {
407 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
408 msqptr
= &msqids
[msqid
];
409 if (msqptr
->msg_qbytes
!= 0 &&
410 msqptr
->msg_perm
.key
== key
)
413 if (msqid
< msginfo
.msgmni
) {
415 kprintf("found public key\n");
417 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
419 kprintf("not exclusive\n");
424 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
426 kprintf("requester doesn't have 0%o access\n",
436 kprintf("need to allocate the msqid_ds\n");
438 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
439 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
441 * Look for an unallocated and unlocked msqid_ds.
442 * msqid_ds's can be locked by msgsnd or msgrcv while
443 * they are copying the message in/out. We can't
444 * re-use the entry until they release it.
446 msqptr
= &msqids
[msqid
];
447 if (msqptr
->msg_qbytes
== 0 &&
448 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
451 if (msqid
== msginfo
.msgmni
) {
453 kprintf("no more msqid_ds's available\n");
459 kprintf("msqid %d is available\n", msqid
);
461 msqptr
->msg_perm
.key
= key
;
462 msqptr
->msg_perm
.cuid
= cred
->cr_uid
;
463 msqptr
->msg_perm
.uid
= cred
->cr_uid
;
464 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
465 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
466 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
467 /* Make sure that the returned msqid is unique */
468 msqptr
->msg_perm
.seq
= (msqptr
->msg_perm
.seq
+ 1) & 0x7fff;
469 msqptr
->msg_first
= NULL
;
470 msqptr
->msg_last
= NULL
;
471 msqptr
->msg_cbytes
= 0;
472 msqptr
->msg_qnum
= 0;
473 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
474 msqptr
->msg_lspid
= 0;
475 msqptr
->msg_lrpid
= 0;
476 msqptr
->msg_stime
= 0;
477 msqptr
->msg_rtime
= 0;
478 msqptr
->msg_ctime
= time_second
;
481 kprintf("didn't find it and wasn't asked to create it\n");
488 /* Construct the unique msqid */
490 uap
->sysmsg_result
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
498 sys_msgsnd(struct msgsnd_args
*uap
)
500 struct thread
*td
= curthread
;
501 int msqid
= uap
->msqid
;
502 void *user_msgp
= uap
->msgp
;
503 size_t msgsz
= uap
->msgsz
;
504 int msgflg
= uap
->msgflg
;
505 int segs_needed
, eval
;
506 struct msqid_ds
*msqptr
;
511 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid
, user_msgp
, msgsz
,
515 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
519 msqid
= IPCID_TO_IX(msqid
);
521 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
523 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
530 msqptr
= &msqids
[msqid
];
531 if (msqptr
->msg_qbytes
== 0) {
533 kprintf("no such message queue id\n");
538 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
540 kprintf("wrong sequence number\n");
546 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_W
))) {
548 kprintf("requester doesn't have write access\n");
554 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
556 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
560 int need_more_resources
= 0;
564 * (inside this loop in case msg_qbytes changes while we sleep)
567 if (msgsz
> msqptr
->msg_qbytes
) {
569 kprintf("msgsz > msqptr->msg_qbytes\n");
575 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
577 kprintf("msqid is locked\n");
579 need_more_resources
= 1;
581 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
583 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
585 need_more_resources
= 1;
587 if (segs_needed
> nfree_msgmaps
) {
589 kprintf("segs_needed > nfree_msgmaps\n");
591 need_more_resources
= 1;
593 if (free_msghdrs
== NULL
) {
595 kprintf("no more msghdrs\n");
597 need_more_resources
= 1;
600 if (need_more_resources
) {
603 if ((msgflg
& IPC_NOWAIT
) != 0) {
605 kprintf("need more resources but caller doesn't want to wait\n");
611 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
613 kprintf("we don't own the msqid_ds\n");
617 /* Force later arrivals to wait for our
620 kprintf("we own the msqid_ds\n");
622 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
626 kprintf("goodnight\n");
628 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
630 kprintf("good morning, eval=%d\n", eval
);
633 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
636 kprintf("msgsnd: interrupted system call\n");
643 * Make sure that the msq queue still exists
646 if (msqptr
->msg_qbytes
== 0) {
648 kprintf("msqid deleted\n");
656 kprintf("got all the resources that we need\n");
663 * We have the resources that we need.
667 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
668 panic("msg_perm.mode & MSG_LOCKED");
669 if (segs_needed
> nfree_msgmaps
)
670 panic("segs_needed > nfree_msgmaps");
671 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
672 panic("msgsz + msg_cbytes > msg_qbytes");
673 if (free_msghdrs
== NULL
)
674 panic("no more msghdrs");
677 * Re-lock the msqid_ds in case we page-fault when copying in the
681 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
682 panic("msqid_ds is already locked");
683 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
686 * Allocate a message header
689 msghdr
= free_msghdrs
;
690 free_msghdrs
= msghdr
->msg_next
;
691 msghdr
->msg_spot
= -1;
692 msghdr
->msg_ts
= msgsz
;
695 * Allocate space for the message
698 while (segs_needed
> 0) {
699 if (nfree_msgmaps
<= 0)
700 panic("not enough msgmaps");
701 if (free_msgmaps
== -1)
702 panic("nil free_msgmaps");
705 panic("next too low #1");
706 if (next
>= msginfo
.msgseg
)
707 panic("next out of range #1");
709 kprintf("allocating segment %d to message\n", next
);
711 free_msgmaps
= msgmaps
[next
].next
;
713 msgmaps
[next
].next
= msghdr
->msg_spot
;
714 msghdr
->msg_spot
= next
;
719 * Copy in the message type
722 if ((eval
= copyin(user_msgp
, &msghdr
->msg_type
,
723 sizeof(msghdr
->msg_type
))) != 0) {
725 kprintf("error %d copying the message type\n", eval
);
728 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
729 wakeup((caddr_t
)msqptr
);
732 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
735 * Validate the message type
738 if (msghdr
->msg_type
< 1) {
740 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
741 wakeup((caddr_t
)msqptr
);
743 kprintf("mtype (%d) < 1\n", msghdr
->msg_type
);
750 * Copy in the message body
753 next
= msghdr
->msg_spot
;
756 if (msgsz
> msginfo
.msgssz
)
757 tlen
= msginfo
.msgssz
;
761 panic("next too low #2");
762 if (next
>= msginfo
.msgseg
)
763 panic("next out of range #2");
764 if ((eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
],
767 kprintf("error %d copying in message segment\n", eval
);
770 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
771 wakeup((caddr_t
)msqptr
);
775 user_msgp
= (char *)user_msgp
+ tlen
;
776 next
= msgmaps
[next
].next
;
779 panic("didn't use all the msg segments");
782 * We've got the message. Unlock the msqid_ds.
785 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
788 * Make sure that the msqid_ds is still allocated.
791 if (msqptr
->msg_qbytes
== 0) {
793 wakeup((caddr_t
)msqptr
);
799 * Put the message into the queue
802 if (msqptr
->msg_first
== NULL
) {
803 msqptr
->msg_first
= msghdr
;
804 msqptr
->msg_last
= msghdr
;
806 msqptr
->msg_last
->msg_next
= msghdr
;
807 msqptr
->msg_last
= msghdr
;
809 msqptr
->msg_last
->msg_next
= NULL
;
811 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
813 msqptr
->msg_lspid
= td
->td_proc
->p_pid
;
814 msqptr
->msg_stime
= time_second
;
816 wakeup((caddr_t
)msqptr
);
821 uap
->sysmsg_result
= 0;
829 sys_msgrcv(struct msgrcv_args
*uap
)
831 struct thread
*td
= curthread
;
832 int msqid
= uap
->msqid
;
833 void *user_msgp
= uap
->msgp
;
834 size_t msgsz
= uap
->msgsz
;
835 long msgtyp
= uap
->msgtyp
;
836 int msgflg
= uap
->msgflg
;
838 struct msqid_ds
*msqptr
;
844 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid
, user_msgp
,
845 msgsz
, msgtyp
, msgflg
);
848 if (!jail_sysvipc_allowed
&& td
->td_ucred
->cr_prison
!= NULL
)
852 msqid
= IPCID_TO_IX(msqid
);
854 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
856 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
863 msqptr
= &msqids
[msqid
];
864 if (msqptr
->msg_qbytes
== 0) {
866 kprintf("no such message queue id\n");
871 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
873 kprintf("wrong sequence number\n");
879 if ((eval
= ipcperm(td
->td_proc
, &msqptr
->msg_perm
, IPC_R
))) {
881 kprintf("requester doesn't have read access\n");
887 while (msghdr
== NULL
) {
889 msghdr
= msqptr
->msg_first
;
890 if (msghdr
!= NULL
) {
891 if (msgsz
< msghdr
->msg_ts
&&
892 (msgflg
& MSG_NOERROR
) == 0) {
894 kprintf("first message on the queue is too big (want %d, got %d)\n",
895 msgsz
, msghdr
->msg_ts
);
900 if (msqptr
->msg_first
== msqptr
->msg_last
) {
901 msqptr
->msg_first
= NULL
;
902 msqptr
->msg_last
= NULL
;
904 msqptr
->msg_first
= msghdr
->msg_next
;
905 if (msqptr
->msg_first
== NULL
)
906 panic("msg_first/last screwed up #1");
910 struct msg
*previous
;
914 prev
= &(msqptr
->msg_first
);
915 while ((msghdr
= *prev
) != NULL
) {
917 * Is this message's type an exact match or is
918 * this message's type less than or equal to
919 * the absolute value of a negative msgtyp?
920 * Note that the second half of this test can
921 * NEVER be true if msgtyp is positive since
922 * msg_type is always positive!
925 if (msgtyp
== msghdr
->msg_type
||
926 msghdr
->msg_type
<= -msgtyp
) {
928 kprintf("found message type %d, requested %d\n",
929 msghdr
->msg_type
, msgtyp
);
931 if (msgsz
< msghdr
->msg_ts
&&
932 (msgflg
& MSG_NOERROR
) == 0) {
934 kprintf("requested message on the queue is too big (want %d, got %d)\n",
935 msgsz
, msghdr
->msg_ts
);
940 *prev
= msghdr
->msg_next
;
941 if (msghdr
== msqptr
->msg_last
) {
942 if (previous
== NULL
) {
945 panic("msg_first/last screwed up #2");
953 panic("msg_first/last screwed up #3");
961 prev
= &(msghdr
->msg_next
);
966 * We've either extracted the msghdr for the appropriate
967 * message or there isn't one.
968 * If there is one then bail out of this loop.
975 * Hmph! No message found. Does the user want to wait?
978 if ((msgflg
& IPC_NOWAIT
) != 0) {
980 kprintf("no appropriate message found (msgtyp=%d)\n",
983 /* The SVID says to return ENOMSG. */
987 /* Unfortunately, BSD doesn't define that code yet! */
994 * Wait for something to happen
998 kprintf("msgrcv: goodnight\n");
1000 eval
= tsleep((caddr_t
)msqptr
, PCATCH
, "msgwait", 0);
1002 kprintf("msgrcv: good morning (eval=%d)\n", eval
);
1007 kprintf("msgsnd: interrupted system call\n");
1014 * Make sure that the msq queue still exists
1017 if (msqptr
->msg_qbytes
== 0 ||
1018 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1020 kprintf("msqid deleted\n");
1028 * Return the message to the user.
1030 * First, do the bookkeeping (before we risk being interrupted).
1033 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1035 msqptr
->msg_lrpid
= td
->td_proc
->p_pid
;
1036 msqptr
->msg_rtime
= time_second
;
1039 * Make msgsz the actual amount that we'll be returning.
1040 * Note that this effectively truncates the message if it is too long
1041 * (since msgsz is never increased).
1045 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1048 if (msgsz
> msghdr
->msg_ts
)
1049 msgsz
= msghdr
->msg_ts
;
1052 * Return the type to the user.
1055 eval
= copyout((caddr_t
)&(msghdr
->msg_type
), user_msgp
,
1056 sizeof(msghdr
->msg_type
));
1059 kprintf("error (%d) copying out message type\n", eval
);
1061 msg_freehdr(msghdr
);
1062 wakeup((caddr_t
)msqptr
);
1065 user_msgp
= (char *)user_msgp
+ sizeof(msghdr
->msg_type
);
1068 * Return the segments to the user
1071 next
= msghdr
->msg_spot
;
1072 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1075 if (msgsz
- len
> msginfo
.msgssz
)
1076 tlen
= msginfo
.msgssz
;
1080 panic("next too low #3");
1081 if (next
>= msginfo
.msgseg
)
1082 panic("next out of range #3");
1083 eval
= copyout((caddr_t
)&msgpool
[next
* msginfo
.msgssz
],
1087 kprintf("error (%d) copying out message segment\n",
1090 msg_freehdr(msghdr
);
1091 wakeup((caddr_t
)msqptr
);
1094 user_msgp
= (char *)user_msgp
+ tlen
;
1095 next
= msgmaps
[next
].next
;
1099 * Done, return the actual number of bytes copied out.
1102 msg_freehdr(msghdr
);
1103 wakeup((caddr_t
)msqptr
);
1108 uap
->sysmsg_result
= msgsz
;
1113 sysctl_msqids(SYSCTL_HANDLER_ARGS
)
1116 return (SYSCTL_OUT(req
, msqids
,
1117 sizeof(struct msqid_ds
) * msginfo
.msgmni
));
1120 TUNABLE_INT("kern.ipc.msgseg", &msginfo
.msgseg
);
1121 TUNABLE_INT("kern.ipc.msgssz", &msginfo
.msgssz
);
1122 TUNABLE_INT("kern.ipc.msgmni", &msginfo
.msgmni
);
1124 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmax
, CTLFLAG_RD
, &msginfo
.msgmax
, 0, "");
1125 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmni
, CTLFLAG_RD
, &msginfo
.msgmni
, 0, "");
1126 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgmnb
, CTLFLAG_RD
, &msginfo
.msgmnb
, 0, "");
1127 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgtql
, CTLFLAG_RD
, &msginfo
.msgtql
, 0, "");
1128 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgssz
, CTLFLAG_RD
, &msginfo
.msgssz
, 0, "");
1129 SYSCTL_INT(_kern_ipc
, OID_AUTO
, msgseg
, CTLFLAG_RD
, &msginfo
.msgseg
, 0, "");
1130 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, msqids
, CTLFLAG_RD
,
1131 NULL
, 0, sysctl_msqids
, "", "Message queue IDs");