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)
199 sys_msgsys(struct msgsys_args *uap)
201 struct proc *p = curproc;
202 unsigned int which = (unsigned int)uap->which;
205 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
208 if (which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
210 bcopy(&uap->a2, &uap->which,
211 sizeof(struct msgsys_args) - offsetof(struct msgsys_args, a2));
213 error = (*msgcalls[which])(uap);
219 msg_freehdr(struct msg *msghdr)
221 while (msghdr->msg_ts > 0) {
223 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
224 panic("msghdr->msg_spot out of range");
225 next = msgmaps[msghdr->msg_spot].next;
226 msgmaps[msghdr->msg_spot].next = free_msgmaps;
227 free_msgmaps = msghdr->msg_spot;
229 msghdr->msg_spot = next;
230 if (msghdr->msg_ts >= msginfo.msgssz)
231 msghdr->msg_ts -= msginfo.msgssz;
235 if (msghdr->msg_spot != -1)
236 panic("msghdr->msg_spot != -1");
237 msghdr->msg_next = free_msghdrs;
238 free_msghdrs = msghdr;
245 sys_msgctl(struct msgctl_args *uap)
247 struct thread *td = curthread;
248 struct proc *p = td->td_proc;
249 int msqid = uap->msqid;
251 struct msqid_ds *user_msqptr = uap->buf;
253 struct msqid_ds msqbuf;
254 struct msqid_ds *msqptr;
257 kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
260 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
264 msqid = IPCID_TO_IX(msqid);
266 if (msqid < 0 || msqid >= msginfo.msgmni) {
268 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
275 msqptr = &msqids[msqid];
277 if (msqptr->msg_qbytes == 0) {
279 kprintf("no such msqid\n");
284 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
286 kprintf("wrong sequence number\n");
298 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)) != 0)
300 /* Free the message headers */
301 msghdr = msqptr->msg_first;
302 while (msghdr != NULL) {
303 struct msg *msghdr_tmp;
305 /* Free the segments of each message */
306 msqptr->msg_cbytes -= msghdr->msg_ts;
309 msghdr = msghdr->msg_next;
310 msg_freehdr(msghdr_tmp);
313 if (msqptr->msg_cbytes != 0)
314 panic("msg_cbytes is screwed up");
315 if (msqptr->msg_qnum != 0)
316 panic("msg_qnum is screwed up");
318 msqptr->msg_qbytes = 0; /* Mark it as free */
320 wakeup((caddr_t)msqptr);
326 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)) != 0)
328 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
330 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
331 eval = priv_check(td, PRIV_ROOT);
335 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
337 kprintf("can't increase msg_qbytes beyond %d (truncating)\n",
340 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
342 if (msqbuf.msg_qbytes == 0) {
344 kprintf("can't reduce msg_qbytes to 0\n");
346 eval = EINVAL; /* non-standard errno! */
349 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
350 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
351 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
352 (msqbuf.msg_perm.mode & 0777);
353 msqptr->msg_qbytes = msqbuf.msg_qbytes;
354 msqptr->msg_ctime = time_second;
358 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
360 kprintf("requester doesn't have read access\n");
365 eval = copyout(msqptr, user_msqptr, sizeof(struct msqid_ds));
370 kprintf("invalid command %d\n", cmd);
378 uap->sysmsg_result = rval;
386 sys_msgget(struct msgget_args *uap)
388 struct proc *p = curproc;
391 int msgflg = uap->msgflg;
392 struct ucred *cred = p->p_ucred;
393 struct msqid_ds *msqptr = NULL;
396 kprintf("msgget(0x%x, 0%o)\n", key, msgflg);
398 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
404 if (key != IPC_PRIVATE) {
405 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
406 msqptr = &msqids[msqid];
407 if (msqptr->msg_qbytes != 0 &&
408 msqptr->msg_perm.key == key)
411 if (msqid < msginfo.msgmni) {
413 kprintf("found public key\n");
415 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
417 kprintf("not exclusive\n");
422 if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
424 kprintf("requester doesn't have 0%o access\n",
434 kprintf("need to allocate the msqid_ds\n");
436 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
437 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
439 * Look for an unallocated and unlocked msqid_ds.
440 * msqid_ds's can be locked by msgsnd or msgrcv while
441 * they are copying the message in/out. We can't
442 * re-use the entry until they release it.
444 msqptr = &msqids[msqid];
445 if (msqptr->msg_qbytes == 0 &&
446 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
449 if (msqid == msginfo.msgmni) {
451 kprintf("no more msqid_ds's available\n");
457 kprintf("msqid %d is available\n", msqid);
459 msqptr->msg_perm.key = key;
460 msqptr->msg_perm.cuid = cred->cr_uid;
461 msqptr->msg_perm.uid = cred->cr_uid;
462 msqptr->msg_perm.cgid = cred->cr_gid;
463 msqptr->msg_perm.gid = cred->cr_gid;
464 msqptr->msg_perm.mode = (msgflg & 0777);
465 /* Make sure that the returned msqid is unique */
466 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
467 msqptr->msg_first = NULL;
468 msqptr->msg_last = NULL;
469 msqptr->msg_cbytes = 0;
470 msqptr->msg_qnum = 0;
471 msqptr->msg_qbytes = msginfo.msgmnb;
472 msqptr->msg_lspid = 0;
473 msqptr->msg_lrpid = 0;
474 msqptr->msg_stime = 0;
475 msqptr->msg_rtime = 0;
476 msqptr->msg_ctime = time_second;
479 kprintf("didn't find it and wasn't asked to create it\n");
486 /* Construct the unique msqid */
488 uap->sysmsg_result = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
496 sys_msgsnd(struct msgsnd_args *uap)
498 struct proc *p = curproc;
499 int msqid = uap->msqid;
500 void *user_msgp = uap->msgp;
501 size_t msgsz = uap->msgsz;
502 int msgflg = uap->msgflg;
503 int segs_needed, eval;
504 struct msqid_ds *msqptr;
509 kprintf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
513 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
517 msqid = IPCID_TO_IX(msqid);
519 if (msqid < 0 || msqid >= msginfo.msgmni) {
521 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
528 msqptr = &msqids[msqid];
529 if (msqptr->msg_qbytes == 0) {
531 kprintf("no such message queue id\n");
536 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
538 kprintf("wrong sequence number\n");
544 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
546 kprintf("requester doesn't have write access\n");
552 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
554 kprintf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
558 int need_more_resources = 0;
562 * (inside this loop in case msg_qbytes changes while we sleep)
565 if (msgsz > msqptr->msg_qbytes) {
567 kprintf("msgsz > msqptr->msg_qbytes\n");
573 if (msqptr->msg_perm.mode & MSG_LOCKED) {
575 kprintf("msqid is locked\n");
577 need_more_resources = 1;
579 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
581 kprintf("msgsz + msg_cbytes > msg_qbytes\n");
583 need_more_resources = 1;
585 if (segs_needed > nfree_msgmaps) {
587 kprintf("segs_needed > nfree_msgmaps\n");
589 need_more_resources = 1;
591 if (free_msghdrs == NULL) {
593 kprintf("no more msghdrs\n");
595 need_more_resources = 1;
598 if (need_more_resources) {
601 if ((msgflg & IPC_NOWAIT) != 0) {
603 kprintf("need more resources but caller doesn't want to wait\n");
609 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
611 kprintf("we don't own the msqid_ds\n");
615 /* Force later arrivals to wait for our
618 kprintf("we own the msqid_ds\n");
620 msqptr->msg_perm.mode |= MSG_LOCKED;
624 kprintf("goodnight\n");
626 eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
628 kprintf("good morning, eval=%d\n", eval);
631 msqptr->msg_perm.mode &= ~MSG_LOCKED;
634 kprintf("msgsnd: interrupted system call\n");
641 * Make sure that the msq queue still exists
644 if (msqptr->msg_qbytes == 0) {
646 kprintf("msqid deleted\n");
654 kprintf("got all the resources that we need\n");
661 * We have the resources that we need.
665 if (msqptr->msg_perm.mode & MSG_LOCKED)
666 panic("msg_perm.mode & MSG_LOCKED");
667 if (segs_needed > nfree_msgmaps)
668 panic("segs_needed > nfree_msgmaps");
669 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
670 panic("msgsz + msg_cbytes > msg_qbytes");
671 if (free_msghdrs == NULL)
672 panic("no more msghdrs");
675 * Re-lock the msqid_ds in case we page-fault when copying in the
679 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
680 panic("msqid_ds is already locked");
681 msqptr->msg_perm.mode |= MSG_LOCKED;
684 * Allocate a message header
687 msghdr = free_msghdrs;
688 free_msghdrs = msghdr->msg_next;
689 msghdr->msg_spot = -1;
690 msghdr->msg_ts = msgsz;
693 * Allocate space for the message
696 while (segs_needed > 0) {
697 if (nfree_msgmaps <= 0)
698 panic("not enough msgmaps");
699 if (free_msgmaps == -1)
700 panic("nil free_msgmaps");
703 panic("next too low #1");
704 if (next >= msginfo.msgseg)
705 panic("next out of range #1");
707 kprintf("allocating segment %d to message\n", next);
709 free_msgmaps = msgmaps[next].next;
711 msgmaps[next].next = msghdr->msg_spot;
712 msghdr->msg_spot = next;
717 * Copy in the message type
720 if ((eval = copyin(user_msgp, &msghdr->msg_type,
721 sizeof(msghdr->msg_type))) != 0) {
723 kprintf("error %d copying the message type\n", eval);
726 msqptr->msg_perm.mode &= ~MSG_LOCKED;
727 wakeup((caddr_t)msqptr);
730 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
733 * Validate the message type
736 if (msghdr->msg_type < 1) {
738 msqptr->msg_perm.mode &= ~MSG_LOCKED;
739 wakeup((caddr_t)msqptr);
741 kprintf("mtype (%d) < 1\n", msghdr->msg_type);
748 * Copy in the message body
751 next = msghdr->msg_spot;
754 if (msgsz > msginfo.msgssz)
755 tlen = msginfo.msgssz;
759 panic("next too low #2");
760 if (next >= msginfo.msgseg)
761 panic("next out of range #2");
762 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
765 kprintf("error %d copying in message segment\n", eval);
768 msqptr->msg_perm.mode &= ~MSG_LOCKED;
769 wakeup((caddr_t)msqptr);
773 user_msgp = (char *)user_msgp + tlen;
774 next = msgmaps[next].next;
777 panic("didn't use all the msg segments");
780 * We've got the message. Unlock the msqid_ds.
783 msqptr->msg_perm.mode &= ~MSG_LOCKED;
786 * Make sure that the msqid_ds is still allocated.
789 if (msqptr->msg_qbytes == 0) {
791 wakeup((caddr_t)msqptr);
797 * Put the message into the queue
800 if (msqptr->msg_first == NULL) {
801 msqptr->msg_first = msghdr;
802 msqptr->msg_last = msghdr;
804 msqptr->msg_last->msg_next = msghdr;
805 msqptr->msg_last = msghdr;
807 msqptr->msg_last->msg_next = NULL;
809 msqptr->msg_cbytes += msghdr->msg_ts;
811 msqptr->msg_lspid = p->p_pid;
812 msqptr->msg_stime = time_second;
814 wakeup((caddr_t)msqptr);
819 uap->sysmsg_result = 0;
827 sys_msgrcv(struct msgrcv_args *uap)
829 struct proc *p = curproc;
830 int msqid = uap->msqid;
831 void *user_msgp = uap->msgp;
832 size_t msgsz = uap->msgsz;
833 long msgtyp = uap->msgtyp;
834 int msgflg = uap->msgflg;
836 struct msqid_ds *msqptr;
842 kprintf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
843 msgsz, msgtyp, msgflg);
846 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
850 msqid = IPCID_TO_IX(msqid);
852 if (msqid < 0 || msqid >= msginfo.msgmni) {
854 kprintf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
861 msqptr = &msqids[msqid];
862 if (msqptr->msg_qbytes == 0) {
864 kprintf("no such message queue id\n");
869 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
871 kprintf("wrong sequence number\n");
877 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
879 kprintf("requester doesn't have read access\n");
885 while (msghdr == NULL) {
887 msghdr = msqptr->msg_first;
888 if (msghdr != NULL) {
889 if (msgsz < msghdr->msg_ts &&
890 (msgflg & MSG_NOERROR) == 0) {
892 kprintf("first message on the queue is too big (want %d, got %d)\n",
893 msgsz, msghdr->msg_ts);
898 if (msqptr->msg_first == msqptr->msg_last) {
899 msqptr->msg_first = NULL;
900 msqptr->msg_last = NULL;
902 msqptr->msg_first = msghdr->msg_next;
903 if (msqptr->msg_first == NULL)
904 panic("msg_first/last screwed up #1");
908 struct msg *previous;
912 prev = &(msqptr->msg_first);
913 while ((msghdr = *prev) != NULL) {
915 * Is this message's type an exact match or is
916 * this message's type less than or equal to
917 * the absolute value of a negative msgtyp?
918 * Note that the second half of this test can
919 * NEVER be true if msgtyp is positive since
920 * msg_type is always positive!
923 if (msgtyp == msghdr->msg_type ||
924 msghdr->msg_type <= -msgtyp) {
926 kprintf("found message type %d, requested %d\n",
927 msghdr->msg_type, msgtyp);
929 if (msgsz < msghdr->msg_ts &&
930 (msgflg & MSG_NOERROR) == 0) {
932 kprintf("requested message on the queue is too big (want %d, got %d)\n",
933 msgsz, msghdr->msg_ts);
938 *prev = msghdr->msg_next;
939 if (msghdr == msqptr->msg_last) {
940 if (previous == NULL) {
943 panic("msg_first/last screwed up #2");
951 panic("msg_first/last screwed up #3");
959 prev = &(msghdr->msg_next);
964 * We've either extracted the msghdr for the appropriate
965 * message or there isn't one.
966 * If there is one then bail out of this loop.
973 * Hmph! No message found. Does the user want to wait?
976 if ((msgflg & IPC_NOWAIT) != 0) {
978 kprintf("no appropriate message found (msgtyp=%d)\n",
981 /* The SVID says to return ENOMSG. */
985 /* Unfortunately, BSD doesn't define that code yet! */
992 * Wait for something to happen
996 kprintf("msgrcv: goodnight\n");
998 eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
1000 kprintf("msgrcv: good morning (eval=%d)\n", eval);
1005 kprintf("msgsnd: interrupted system call\n");
1012 * Make sure that the msq queue still exists
1015 if (msqptr->msg_qbytes == 0 ||
1016 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1018 kprintf("msqid deleted\n");
1026 * Return the message to the user.
1028 * First, do the bookkeeping (before we risk being interrupted).
1031 msqptr->msg_cbytes -= msghdr->msg_ts;
1033 msqptr->msg_lrpid = p->p_pid;
1034 msqptr->msg_rtime = time_second;
1037 * Make msgsz the actual amount that we'll be returning.
1038 * Note that this effectively truncates the message if it is too long
1039 * (since msgsz is never increased).
1043 kprintf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1046 if (msgsz > msghdr->msg_ts)
1047 msgsz = msghdr->msg_ts;
1050 * Return the type to the user.
1053 eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1054 sizeof(msghdr->msg_type));
1057 kprintf("error (%d) copying out message type\n", eval);
1059 msg_freehdr(msghdr);
1060 wakeup((caddr_t)msqptr);
1063 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1066 * Return the segments to the user
1069 next = msghdr->msg_spot;
1070 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1073 if (msgsz - len > msginfo.msgssz)
1074 tlen = msginfo.msgssz;
1078 panic("next too low #3");
1079 if (next >= msginfo.msgseg)
1080 panic("next out of range #3");
1081 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1085 kprintf("error (%d) copying out message segment\n",
1088 msg_freehdr(msghdr);
1089 wakeup((caddr_t)msqptr);
1092 user_msgp = (char *)user_msgp + tlen;
1093 next = msgmaps[next].next;
1097 * Done, return the actual number of bytes copied out.
1100 msg_freehdr(msghdr);
1101 wakeup((caddr_t)msqptr);
1106 uap->sysmsg_result = msgsz;
1111 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1114 return (SYSCTL_OUT(req, msqids,
1115 sizeof(struct msqid_ds) * msginfo.msgmni));
1118 TUNABLE_INT("kern.ipc.msgseg", &msginfo.msgseg);
1119 TUNABLE_INT("kern.ipc.msgssz", &msginfo.msgssz);
1120 TUNABLE_INT("kern.ipc.msgmni", &msginfo.msgmni);
1122 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1123 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0, "");
1124 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1125 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1126 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0, "");
1127 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0, "");
1128 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1129 NULL, 0, sysctl_msqids, "", "Message queue IDs");